Skip to content

Commit

Permalink
pattern: Merged plot_all back into PatternDB
Browse files Browse the repository at this point in the history
  • Loading branch information
Somerandomguy10111 committed Dec 18, 2024
1 parent 2d34839 commit 1b9eaba
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
34 changes: 31 additions & 3 deletions xrdpattern/pattern/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import random
from typing import Optional

from matplotlib import pyplot as plt

from holytools.logging import LoggerFactory
from holytools.userIO import TrackedCollection
from xrdpattern.parsing import MasterParser, Formats
from xrdpattern.xrd import XrayInfo, XrdData
from .visualization import histograms, plot_all
from .visualization import histograms, multiplot
from .pattern import XrdPattern

patterdb_logger = LoggerFactory.get_logger(name=__name__)
Expand Down Expand Up @@ -119,9 +121,35 @@ def __eq__(self, other : PatternDB):
return False
return True

def show_all(self, single_plot : bool = False, limit_patterns : int = 100):
def show_all(self, single_plot : bool = False, limit_patterns : int = 100, title : Optional[str] = None):
patterns = self.patterns if len(self.patterns) <= limit_patterns else random.sample(self.patterns, limit_patterns)
plot_all(patterns=patterns, single_plot=single_plot, db_name=self.name)
if single_plot:
data = [p.get_pattern_data() for p in patterns]
fig, ax = plt.subplots(dpi=600)
for x, y in data:
ax.plot(x, y, linewidth=0.25, alpha=0.75)

ax.set_xlabel(r'$2\theta$ [$^\circ$]')
ax.set_ylabel('Standardized relative intensity (a.u.)')
if title:
ax.set_title(title)
else:
ax.set_title(f'XRD Patterns from {self.name}')
plt.show()

else:
batch_size = 32
j = 0
while j < len(patterns):
pattern_batch = patterns[j:j + batch_size]
for k, p in enumerate(pattern_batch):
p.metadata.filename = p.get_name() or f'pattern_{j + k}'
multiplot(patterns=pattern_batch, start_idx=j)
j += batch_size

user_input = input(f'Press enter to continue or q to quit')
if user_input.lower() == 'q':
break

def show_histograms(self, save_fpath : Optional[str] = None, attach_colorbar : bool = True):
histograms(patterns=self.patterns, attach_colorbar=attach_colorbar, save_fpath=save_fpath)
Expand Down
26 changes: 0 additions & 26 deletions xrdpattern/pattern/visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,6 @@

# -----------------------------------------

def plot_all(patterns : list[XrdPattern], db_name : Optional[str] = None, single_plot : bool = False):
if single_plot:
data = [p.get_pattern_data() for p in patterns]
fig, ax = plt.subplots(dpi=600)
for x, y in data:
ax.plot(x, y, linewidth=0.25, linestyle='--', alpha=0.75)

ax.set_xlabel(r'$2\theta$ [$^\circ$]')
ax.set_ylabel('Standardized relative intensity (a.u.)')
if db_name:
ax.set_title(f'{len(patterns)} of patterns from {db_name}')
plt.show()

else:
batch_size = 32
j = 0
while j < len(patterns):
pattern_batch = patterns[j:j + batch_size]
for k, p in enumerate(pattern_batch):
p.metadata.filename = p.get_name() or f'pattern_{j + k}'
multiplot(patterns=pattern_batch, start_idx=j)
j += batch_size

user_input = input(f'Press enter to continue or q to quit')
if user_input.lower() == 'q':
break

def multiplot(patterns : list[XrdPattern], start_idx : int):
labels = [p.get_name() for p in patterns]
Expand Down

0 comments on commit 1b9eaba

Please sign in to comment.