Skip to content

Commit b8c622e

Browse files
committed
Simplifies exclusion handling in data processing
1 parent d709730 commit b8c622e

File tree

10 files changed

+8209
-51
lines changed

10 files changed

+8209
-51
lines changed

src/easydiffraction/analysis/calculation.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ def set_calculator(self, engine: str) -> None:
3131
"""
3232
self._calculator = self.calculator_factory.create_calculator(engine)
3333

34-
def calculate_structure_factors(self, sample_models: SampleModels, experiments: Experiments) -> Optional[List[Any]]:
34+
def calculate_structure_factors(self,
35+
sample_models: SampleModels,
36+
experiments: Experiments) -> Optional[List[Any]]:
3537
"""
3638
Calculate HKL intensities (structure factors) for sample models and experiments.
3739
@@ -44,15 +46,17 @@ def calculate_structure_factors(self, sample_models: SampleModels, experiments:
4446
"""
4547
return self._calculator.calculate_structure_factors(sample_models, experiments)
4648

47-
def calculate_pattern(self, sample_models: SampleModels, experiment: Experiment) -> np.ndarray:
49+
def calculate_pattern(self,
50+
sample_models: SampleModels,
51+
experiment: Experiment) -> np.ndarray:
4852
"""
49-
Generate diffraction pattern based on sample models and experiment.
53+
Calculate diffraction pattern based on sample models and experiment.
5054
5155
Args:
5256
sample_models: Collection of sample models.
5357
experiment: A single experiment object.
5458
5559
Returns:
56-
Diffraction pattern generated by the backend calculator.
60+
Diffraction pattern calculated by the backend calculator.
5761
"""
5862
return self._calculator.calculate_pattern(sample_models, experiment)

src/easydiffraction/analysis/calculators/calculator_cryspy.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ def _recreate_cryspy_dict(self,
127127

128128
cryspy_model_id = f'crystal_{sample_model.name}'
129129
cryspy_model_dict = cryspy_dict[cryspy_model_id]
130+
130131
# Cell
131132
cryspy_cell = cryspy_model_dict['unit_cell_parameters']
132133
cryspy_cell[0] = sample_model.cell.length_a.value
@@ -135,6 +136,7 @@ def _recreate_cryspy_dict(self,
135136
cryspy_cell[3] = np.deg2rad(sample_model.cell.angle_alpha.value)
136137
cryspy_cell[4] = np.deg2rad(sample_model.cell.angle_beta.value)
137138
cryspy_cell[5] = np.deg2rad(sample_model.cell.angle_gamma.value)
139+
138140
# Atomic coordinates
139141
cryspy_xyz = cryspy_model_dict['atom_fract_xyz']
140142
for idx, atom_site in enumerate(sample_model.atom_sites):

src/easydiffraction/analysis/minimization.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,6 @@ def _residual_function(self,
153153
y_meas_su: np.ndarray = experiment.datastore.pattern.meas_su
154154
diff = ((y_meas - y_calc) / y_meas_su)
155155

156-
# Exclude points that are marked as excluded
157-
excluded = experiment.datastore.pattern.excluded
158-
if excluded is not None:
159-
diff = diff[~excluded]
160-
161156
# Residuals are squared before going into reduced chi-squared
162157
diff *= np.sqrt(weight)
163158

src/easydiffraction/analysis/reliability_factors.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,6 @@ def get_reliability_inputs(sample_models: SampleModels,
132132
if y_meas_su is None:
133133
y_meas_su = np.ones_like(y_meas)
134134

135-
# Exclude points that are marked as excluded
136-
excluded = experiment.datastore.pattern.excluded
137-
if excluded is not None:
138-
y_meas = y_meas[~excluded]
139-
y_calc = y_calc[~excluded]
140-
if y_meas_su is not None:
141-
y_meas_su = y_meas_su[~excluded]
142-
143135
y_obs_all.extend(y_meas)
144136
y_calc_all.extend(y_calc)
145137
y_err_all.extend(y_meas_su)

src/easydiffraction/experiments/collections/excluded_regions.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,15 @@ def on_item_added(self, item: ExcludedRegion) -> None:
6161
"""
6262
experiment = self._parent
6363
pattern = experiment.datastore.pattern
64-
x = pattern.x
6564

6665
# Boolean mask for points within the new excluded region
67-
in_region = (x >= item.minimum.value) & (x <= item.maximum.value)
66+
in_region = ((pattern.full_x >= item.minimum.value) &
67+
(pattern.full_x <= item.maximum.value))
6868

6969
# Update the exclusion mask
7070
pattern.excluded[in_region] = True
71+
72+
# Update the excluded points in the datastore
73+
pattern.x = pattern.full_x[~pattern.excluded]
74+
pattern.meas = pattern.full_meas[~pattern.excluded]
75+
pattern.meas_su = pattern.full_meas_su[~pattern.excluded]

src/easydiffraction/experiments/experiment.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,23 @@ def _load_ascii_data_to_experiment(self, data_path: str) -> None:
286286
sy: np.ndarray = data[:, 2] if data.shape[1] > 2 else np.sqrt(y)
287287

288288
# Attach the data to the experiment's datastore
289+
290+
# The full pattern data
291+
self.datastore.pattern.full_x = x
292+
self.datastore.pattern.full_meas = y
293+
self.datastore.pattern.full_meas_su = sy
294+
295+
# The pattern data used for fitting (without excluded points)
296+
# This is the same as full_x, full_meas, full_meas_su by default
289297
self.datastore.pattern.x = x
290298
self.datastore.pattern.meas = y
291299
self.datastore.pattern.meas_su = sy
292-
self.datastore.pattern.excluded = np.full(x.shape, fill_value=False, dtype=bool) # No excluded points by default
300+
301+
# Excluded mask
302+
# No excluded points by default
303+
self.datastore.pattern.excluded = np.full(x.shape,
304+
fill_value=False,
305+
dtype=bool)
293306

294307
print(paragraph("Data loaded successfully"))
295308
print(f"Experiment 🔬 '{self.name}'. Number of data points: {len(x)}")

src/easydiffraction/plotting/plotting.py

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,6 @@ def plot_meas(self,
140140
x_min=x_min,
141141
x_max=x_max)
142142

143-
# Exclude points based on the pattern's excluded mask
144-
if pattern.excluded is not None:
145-
excluded = self._filtered_y_array(y_array=pattern.excluded,
146-
x_array=pattern.x,
147-
x_min=x_min,
148-
x_max=x_max)
149-
x = x[~excluded]
150-
y_meas = y_meas[~excluded]
151-
152143
y_series = [y_meas]
153144
y_labels = ['meas']
154145

@@ -185,15 +176,6 @@ def plot_calc(self,
185176
x_min=x_min,
186177
x_max=x_max)
187178

188-
# Exclude points based on the pattern's excluded mask
189-
if pattern.excluded is not None:
190-
excluded = self._filtered_y_array(y_array=pattern.excluded,
191-
x_array=pattern.x,
192-
x_min=x_min,
193-
x_max=x_max)
194-
x = x[~excluded]
195-
y_calc = y_calc[~excluded]
196-
197179
y_series = [y_calc]
198180
y_labels = ['calc']
199181

@@ -238,16 +220,6 @@ def plot_meas_vs_calc(self,
238220
x_min=x_min,
239221
x_max=x_max)
240222

241-
# Exclude points based on the pattern's excluded mask
242-
if pattern.excluded is not None:
243-
excluded = self._filtered_y_array(y_array=pattern.excluded,
244-
x_array=pattern.x,
245-
x_min=x_min,
246-
x_max=x_max)
247-
x = x[~excluded]
248-
y_meas = y_meas[~excluded]
249-
y_calc = y_calc[~excluded]
250-
251223
y_series = [y_meas, y_calc]
252224
y_labels = ['meas', 'calc']
253225

tutorials/cryst-struct_pd-neut-tof_NCAF-WISH.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# structure using time-of-flight neutron powder diffraction data from WISH at
66
# ISIS.
77
#
8-
# Two datasets from detector banks 5_6 and 4_7 are used for joint fitting.
8+
# Two datasets from detector banks 5+6 and 4+7 are used for joint fitting.
99

1010
# %% [markdown]
1111
# ## Import Library
@@ -70,10 +70,10 @@
7070
# ### Create Experiment
7171

7272
# %%
73-
expt56 = Experiment('wish_5_6', beam_mode='time-of-flight', data_path='data/wish_ncaf_5_6.xye')
73+
expt56 = Experiment('wish_5_6', beam_mode='time-of-flight', data_path='data/wish_ncaf_5_6.xys')
7474

7575
# %%
76-
expt47 = Experiment('wish_4_7', beam_mode='time-of-flight', data_path='data/wish_ncaf_4_7.xye')
76+
expt47 = Experiment('wish_4_7', beam_mode='time-of-flight', data_path='data/wish_ncaf_4_7.xys')
7777

7878
# %% [markdown]
7979
# ### Set Instrument
@@ -190,6 +190,17 @@
190190
# %%
191191
expt47.linked_phases.add('ncaf', scale=2.0)
192192

193+
# %% [markdown]
194+
# ### Set Excluded Regions
195+
196+
# %%
197+
expt56.excluded_regions.add(minimum=0, maximum=10010)
198+
expt56.excluded_regions.add(minimum=100010, maximum=200000)
199+
200+
# %%
201+
expt47.excluded_regions.add(minimum=0, maximum=10006)
202+
expt47.excluded_regions.add(minimum=100004, maximum=200000)
203+
193204
# %% [markdown]
194205
# ## Define Project
195206
#

0 commit comments

Comments
 (0)