16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295 | class Plotter:
def __init__(self):
# Plotting engine
self._engine = DEFAULT_ENGINE
# X-axis limits
self._x_min = DEFAULT_MIN
self._x_max = DEFAULT_MAX
# Chart height
self.height = DEFAULT_HEIGHT
# Plotter instance
self._plotter = PlotterFactory.create_plotter(self._engine)
@property
def engine(self):
"""Returns the current plotting engine name."""
return self._engine
@engine.setter
def engine(self, new_engine):
"""Sets the current plotting engine name and updates the plotter instance."""
new_plotter = PlotterFactory.create_plotter(new_engine)
if new_plotter is None:
return
self._engine = new_engine
self._plotter = new_plotter
print(paragraph('Current plotter changed to'))
print(self._engine)
@property
def x_min(self):
"""Returns the minimum x-axis limit."""
return self._x_min
@x_min.setter
def x_min(self, value):
"""Sets the minimum x-axis limit."""
if value is not None:
self._x_min = value
else:
self._x_min = DEFAULT_MIN
@property
def x_max(self):
"""Returns the maximum x-axis limit."""
return self._x_max
@x_max.setter
def x_max(self, value):
"""Sets the maximum x-axis limit."""
if value is not None:
self._x_max = value
else:
self._x_max = DEFAULT_MAX
@property
def height(self):
"""Returns the chart height."""
return self._height
@height.setter
def height(self, value):
"""Sets the chart height."""
if value is not None:
self._height = value
else:
self._height = DEFAULT_HEIGHT
def show_config(self):
"""
Displays the current configuration settings.
"""
columns_headers = ['Parameter', 'Value']
columns_alignment = ['left', 'left']
columns_data = [
['Plotting engine', self.engine],
['x-axis limits', f'[{self.x_min}, {self.x_max}]'],
['Chart height', self.height],
]
print(paragraph('Current plotter configuration'))
render_table(
columns_headers=columns_headers,
columns_alignment=columns_alignment,
columns_data=columns_data,
)
def show_supported_engines(self):
"""
Displays the supported plotting engines.
"""
columns_headers = ['Engine', 'Description']
columns_alignment = ['left', 'left']
columns_data = []
for name, config in PlotterFactory._SUPPORTED_ENGINES_DICT.items():
description = config.get('description', 'No description provided.')
columns_data.append([name, description])
print(paragraph('Supported plotter engines'))
render_table(
columns_headers=columns_headers,
columns_alignment=columns_alignment,
columns_data=columns_data,
)
def plot_meas(self, pattern, expt_name, expt_type, x_min=None, x_max=None, d_spacing=False):
if pattern.x is None:
error(f'No data available for experiment {expt_name}')
return
if pattern.meas is None:
error(f'No measured data available for experiment {expt_name}')
return
if d_spacing:
x_array = pattern.d
else:
x_array = pattern.x
x = self._filtered_y_array(
y_array=x_array,
x_array=x_array,
x_min=x_min,
x_max=x_max,
)
y_meas = self._filtered_y_array(
y_array=pattern.meas,
x_array=x_array,
x_min=x_min,
x_max=x_max,
)
y_series = [y_meas]
y_labels = ['meas']
if d_spacing:
axes_labels = DEFAULT_AXES_LABELS[expt_type.scattering_type.value]['d-spacing']
else:
axes_labels = DEFAULT_AXES_LABELS[expt_type.scattering_type.value][expt_type.beam_mode.value]
self._plotter.plot(
x=x,
y_series=y_series,
labels=y_labels,
axes_labels=axes_labels,
title=f"Measured data for experiment 🔬 '{expt_name}'",
height=self.height,
)
def plot_calc(
self,
pattern,
expt_name,
expt_type,
x_min=None,
x_max=None,
d_spacing=False,
):
if pattern.x is None:
error(f'No data available for experiment {expt_name}')
return
if pattern.calc is None:
print(f'No calculated data available for experiment {expt_name}')
return
if d_spacing:
x_array = pattern.d
else:
x_array = pattern.x
x = self._filtered_y_array(
y_array=x_array,
x_array=x_array,
x_min=x_min,
x_max=x_max,
)
y_calc = self._filtered_y_array(
y_array=pattern.calc,
x_array=x_array,
x_min=x_min,
x_max=x_max,
)
y_series = [y_calc]
y_labels = ['calc']
if d_spacing:
axes_labels = DEFAULT_AXES_LABELS[expt_type.scattering_type.value]['d-spacing']
else:
axes_labels = DEFAULT_AXES_LABELS[expt_type.scattering_type.value][expt_type.beam_mode.value]
self._plotter.plot(
x=x,
y_series=y_series,
labels=y_labels,
axes_labels=axes_labels,
title=f"Calculated data for experiment 🔬 '{expt_name}'",
height=self.height,
)
def plot_meas_vs_calc(
self,
pattern,
expt_name,
expt_type,
x_min=None,
x_max=None,
show_residual=False,
d_spacing=False,
):
if pattern.x is None:
print(error(f'No data available for experiment {expt_name}'))
return
if pattern.meas is None:
print(error(f'No measured data available for experiment {expt_name}'))
return
if pattern.calc is None:
print(error(f'No calculated data available for experiment {expt_name}'))
return
if d_spacing:
x_array = pattern.d
else:
x_array = pattern.x
x = self._filtered_y_array(
y_array=x_array,
x_array=x_array,
x_min=x_min,
x_max=x_max,
)
y_meas = self._filtered_y_array(
y_array=pattern.meas,
x_array=x_array,
x_min=x_min,
x_max=x_max,
)
y_calc = self._filtered_y_array(
y_array=pattern.calc,
x_array=x_array,
x_min=x_min,
x_max=x_max,
)
y_series = [y_meas, y_calc]
y_labels = ['meas', 'calc']
if d_spacing:
axes_labels = DEFAULT_AXES_LABELS[expt_type.scattering_type.value]['d-spacing']
else:
axes_labels = DEFAULT_AXES_LABELS[expt_type.scattering_type.value][expt_type.beam_mode.value]
if show_residual:
y_resid = y_meas - y_calc
y_series.append(y_resid)
y_labels.append('resid')
self._plotter.plot(
x=x,
y_series=y_series,
labels=y_labels,
axes_labels=axes_labels,
title=f"Measured vs Calculated data for experiment 🔬 '{expt_name}'",
height=self.height,
)
def _filtered_y_array(
self,
y_array,
x_array,
x_min,
x_max,
):
if x_min is None:
x_min = self.x_min
if x_max is None:
x_max = self.x_max
mask = (x_array >= x_min) & (x_array <= x_max)
filtered_y_array = y_array[mask]
return filtered_y_array
|