Skip to content

Commit 4d5ba31

Browse files
authored
Use LiquidsReflectometer Stitching Utilities (#219)
1 parent 6285870 commit 4d5ba31

2 files changed

Lines changed: 37 additions & 182 deletions

File tree

src/refred/reduction/calculate_sf_overlap_range.py

Lines changed: 0 additions & 142 deletions
This file was deleted.
Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
import numpy as np
1+
from lr_reduction.scaling_factors import OverlapScalingFactor, ReducedData, scaling_factor_critical_edge
22

3-
from refred.reduction.calculate_sf_overlap_range import CalculateSFoverlapRange
3+
4+
def _lconfig_to_reduced_data(lconfig) -> ReducedData:
5+
"""Convert LConfigDataset to the ReducedData dataclass used in scaling factor calculation"""
6+
return ReducedData(
7+
q=lconfig.reduce_q_axis,
8+
r=lconfig.reduce_y_axis,
9+
err=lconfig.reduce_e_axis,
10+
)
411

512

613
class ParentHandler(object):
@@ -13,31 +20,19 @@ def _calculateSFCE(self, first_q_row: int, data_type="absolute"):
1320
"""
1421
Scaling factor calculation of Critical Edge (CE)
1522
"""
16-
_q_min = float(str(self.parent.ui.sf_qmin_value.text()))
17-
_q_max = float(str(self.parent.ui.sf_qmax_value.text()))
18-
19-
values = np.zeros(0)
20-
errors = np.zeros(0)
23+
q_min = float(str(self.parent.ui.sf_qmin_value.text()))
24+
q_max = float(str(self.parent.ui.sf_qmax_value.text()))
2125

22-
for row_index in range(self.n_runs):
23-
reduction_config = self.parent.big_table_data.reduction_config(row_index)
24-
low_bound = reduction_config.reduce_q_axis >= _q_min
25-
high_bound = reduction_config.reduce_q_axis <= _q_max
26-
indices = np.argwhere(low_bound & high_bound).T[0]
26+
# Build list of ReducedData for all runs
27+
all_data = [
28+
_lconfig_to_reduced_data(self.parent.big_table_data.reduction_config(row_index))
29+
for row_index in range(self.n_runs)
30+
]
2731

28-
_values_i = reduction_config.reduce_y_axis[indices]
29-
values = np.concatenate((values, _values_i))
30-
31-
_errors_i = reduction_config.reduce_e_axis[indices]
32-
errors = np.concatenate((errors, _errors_i))
33-
34-
if len(values) > 1:
35-
_sf = 1 / np.average(values, weights=1 / errors**2)
36-
else:
37-
_sf = 1
32+
sf = scaling_factor_critical_edge(q_min, q_max, all_data)
3833

3934
# Save the SF in the lowest-q run (first in q-sorted order)
40-
self.saveSFinLConfig(self.parent.big_table_data.reduction_config(first_q_row), _sf, data_type=data_type)
35+
self.saveSFinLConfig(self.parent.big_table_data.reduction_config(first_q_row), sf, data_type=data_type)
4136

4237
def saveSFinLConfig(self, lconfig, sf, data_type="absolute"):
4338
if data_type == "absolute":
@@ -46,7 +41,6 @@ def saveSFinLConfig(self, lconfig, sf, data_type="absolute"):
4641
lconfig.sf_auto = sf
4742
else:
4843
lconfig.sf_manual = sf
49-
5044
return lconfig
5145

5246
def getLConfig(self, row_index):
@@ -57,11 +51,11 @@ def getLConfig(self, row_index):
5751

5852
class AbsoluteNormalization(ParentHandler):
5953
"""
60-
this class performs the absolute normalization of reduced data
54+
Absolute normalization of reduced data.
6155
"""
6256

6357
def __init__(self, parent=None, row_index=0, n_runs=1):
64-
super(AbsoluteNormalization, self).__init__(parent=parent, row_index=row_index, n_runs=n_runs)
58+
super().__init__(parent=parent, row_index=row_index, n_runs=n_runs)
6559

6660
def run(self):
6761
sorted_indices = self.parent.big_table_data.get_q_sorted_indices()
@@ -84,16 +78,16 @@ def copySFtoOtherAngles(self, first_q_row: int):
8478
ce_lconfig = self.getLConfig(first_q_row)
8579
_sf = ce_lconfig.sf_abs_normalization
8680
lconfig = self.getLConfig(self.row_index)
87-
lconfig = self.saveSFinLConfig(lconfig, _sf, data_type="absolute")
81+
self.saveSFinLConfig(lconfig, _sf, data_type="absolute")
8882

8983

9084
class AutomaticStitching(ParentHandler):
9185
"""
92-
automatic stitching of the reduced data using the Q range to calculate the CE
86+
Automatic stitching of the reduced data using the Q overlap range.
9387
"""
9488

9589
def __init__(self, parent=None, row_index=0, n_runs=1):
96-
super(AutomaticStitching, self).__init__(parent=parent, row_index=row_index, n_runs=n_runs)
90+
super().__init__(parent=parent, row_index=row_index, n_runs=n_runs)
9791

9892
def run(self):
9993
self.use_first_angle_range()
@@ -109,40 +103,43 @@ def use_first_angle_range(self):
109103

110104
def _calculateSFOtherAngles(self):
111105
"""
112-
Scaling factor calculation of other angles.
113-
Uses q-sorted order to find the correct left (lower-q) neighbor.
106+
Scaling factor calculation for non-first-angle runs
114107
"""
115108
sorted_indices = self.parent.big_table_data.get_q_sorted_indices()
116109

117-
# Find the position of self.row_index in q-sorted order
118110
try:
119111
q_position = sorted_indices.index(self.row_index)
120112
except ValueError:
121113
return
122114

123-
# The left neighbor in q-space is the previous entry in sorted order
124115
left_row = sorted_indices[q_position - 1]
125116
right_row = self.row_index
126117

127118
left_lconfig = self.getLConfig(left_row)
128119
right_lconfig = self.getLConfig(right_row)
129120

130-
calculate_sf = CalculateSFoverlapRange(left_lconfig, right_lconfig)
131-
_sf = 1.0 / calculate_sf.getSF()
132-
right_lconfig.sf_auto = _sf
121+
left_data = _lconfig_to_reduced_data(left_lconfig)
122+
right_data = _lconfig_to_reduced_data(right_lconfig)
123+
124+
calculator = OverlapScalingFactor(
125+
left_data=left_data,
126+
right_data=right_data,
127+
sf_auto=left_lconfig.sf_auto, # propagate the cumulative auto-SF of the left neighbor
128+
)
129+
sf = 1.0 / calculator.get_scaling_factor()
130+
right_lconfig.sf_auto = sf
133131

134132

135133
class ManualStitching(ParentHandler):
136134
"""
137-
manual stitching of the data. The program will simply used the data defined
138-
in the main table to scaled the data
135+
Manual stitching: uses the sf_manual value from the table as-is.
139136
"""
140137

141138
def __init__(self, parent=None, row_index=0, n_runs=1):
142-
super(ManualStitching, self).__init__(parent=parent, row_index=row_index, n_runs=n_runs)
139+
super().__init__(parent=parent, row_index=row_index, n_runs=n_runs)
143140

144141
def run(self):
145142
ce_lconfig = self.getLConfig(self.row_index)
146143
_sf = ce_lconfig.sf_manual
147144
lconfig = self.getLConfig(self.row_index)
148-
lconfig = self.saveSFinLConfig(lconfig, _sf, data_type="manual")
145+
self.saveSFinLConfig(lconfig, _sf, data_type="manual")

0 commit comments

Comments
 (0)