37
37
async def sum_spectra (spectra : Collection [DaeSpectra ]) -> sc .Variable | sc .DataArray :
38
38
"""Read and sum a number of spectra from the DAE.
39
39
40
- Returns a scipp scalar, which has .value and .variance properties for accessing the sum
41
- and variance respectively of the summed counts.
40
+ Args:
41
+ spectra: a Collection type object of DAE spectra
42
+
43
+ Returns:
44
+ scipp :external+scipp:py:obj:`Variable <scipp.Variable>`
45
+ or :external+scipp:py:obj:`DataArray <scipp.DataArray>` describing
46
+ the sum of the provided spectra.
42
47
43
- More info on scipp scalars can be found here: https://scipp.github.io/generated/functions/scipp.scalar.html
44
48
"""
45
49
logger .info ("Summing %d spectra using scipp" , len (spectra ))
46
50
summed_counts = sc .scalar (value = 0 , unit = sc .units .counts , dtype = "float64" )
@@ -56,13 +60,14 @@ def tof_bounded_spectra(
56
60
"""Sum a set of neutron spectra between the specified time of flight bounds.
57
61
58
62
Args:
59
- bounds: A scipp array of size 2, no variances, unit of us,
60
- where the second element must be larger than the first.
63
+ bounds: A scipp :external+scipp:py:obj:` array <scipp.array>` of size 2, no variances, unit
64
+ of us, where the second element must be larger than the first.
61
65
62
- Returns a scipp scalar, which has .value and .variance properties for accessing the sum
63
- and variance respectively of the summed counts.
66
+ :rtype:
67
+ scipp :external+scipp:py:obj:`scalar <scipp.scalar>`
64
68
65
- More info on scipp arrays and scalars can be found here: https://scipp.github.io/generated/functions/scipp.scalar.html
69
+ Returns a scipp :external+scipp:py:obj:`scalar <scipp.scalar>`, which has .value and .variance
70
+ properties for accessing the sum and variance respectively of the summed counts.
66
71
67
72
"""
68
73
bounds_value = 2
@@ -88,17 +93,22 @@ def wavelength_bounded_spectra(
88
93
"""Sum a set of neutron spectra between the specified wavelength bounds.
89
94
90
95
Args:
91
- bounds: A scipp array of size 2 of wavelength bounds, in units of angstrom,
92
- where the second element must be larger than the first.
93
- total_flight_path_length: A scipp scalar of Ltotal (total flight path length), the path
94
- length from neutron source to detector or monitor, in units of meters.
96
+ bounds: A scipp :external+scipp:py:obj:`array <scipp.array>` of size 2 of wavelength bounds,
97
+ in units of angstrom, where the second element must be larger than the first.
98
+ total_flight_path_length: A scipp :external+scipp:py:obj:`scalar <scipp.scalar>` of Ltotal
99
+ (total flight path length), the path length from neutron source to detector or monitor,
100
+ in units of meters.
101
+
102
+ :rtype:
103
+ scipp :external+scipp:py:obj:`scalar <scipp.scalar>`
95
104
96
105
Time of flight is converted to wavelength using scipp neutron's library function
97
- `wavelength_from_tof`, more info on which can be found here:
98
- https://scipp.github.io/scippneutron/generated/modules/scippneutron.conversion.tof.wavelength_from_tof.html
106
+ `wavelength_from_tof`, more info on which can be found here:
107
+ :external+scippneutron:py:obj:`wavelength_from_tof
108
+ <scippneutron.conversion.tof.wavelength_from_tof>`
99
109
100
- Returns a scipp scalar, which has .value and .variance properties for accessing the sum
101
- and variance respectively of the summed counts.
110
+ Returns a scipp :external+scipp:py:obj:` scalar <scipp.scalar>` , which has .value and .variance
111
+ properties for accessing the sum and variance respectively of the summed counts.
102
112
103
113
"""
104
114
bounds_value = 2
@@ -125,6 +135,60 @@ async def sum_spectra_with_wavelength(
125
135
return sum_spectra_with_wavelength
126
136
127
137
138
+ def polarization (
139
+ a : sc .Variable | sc .DataArray , b : sc .Variable | sc .DataArray
140
+ ) -> sc .Variable | sc .DataArray :
141
+ """Calculate polarization value and propagate uncertainties.
142
+
143
+ This function computes the polarization given by the formula (a-b)/(a+b)
144
+ and propagates the uncertainties associated with a and b.
145
+
146
+ Args:
147
+ a: scipp :external+scipp:py:obj:`Variable <scipp.Variable>`
148
+ or :external+scipp:py:obj:`DataArray <scipp.DataArray>`
149
+ b: scipp :external+scipp:py:obj:`Variable <scipp.Variable>`
150
+ or :external+scipp:py:obj:`DataArray <scipp.DataArray>`
151
+
152
+ Returns:
153
+ polarization, ``(a - b) / (a + b)``, as a scipp
154
+ :external+scipp:py:obj:`Variable <scipp.Variable>`
155
+ or :external+scipp:py:obj:`DataArray <scipp.DataArray>`
156
+
157
+ On SANS instruments e.g. LARMOR, A and B correspond to intensity in different DAE
158
+ periods (before/after switching a flipper) and the output is interpreted as a neutron
159
+ polarization ratio.
160
+
161
+ On reflectometry instruments e.g. POLREF, the situation is the same as on LARMOR.
162
+
163
+ On muon instruments, A and B correspond to measuring from forward/backward detector
164
+ banks, and the output is interpreted as a muon asymmetry.
165
+
166
+ """
167
+ if a .unit != b .unit :
168
+ raise ValueError ("The units of a and b are not equivalent." )
169
+ if a .sizes != b .sizes :
170
+ raise ValueError ("Dimensions/shape of a and b must match." )
171
+
172
+ # This line allows for dims, units, and dtype to be handled by scipp
173
+ polarization_value = (a - b ) / (a + b )
174
+
175
+ variances_a = a .variances
176
+ variances_b = b .variances
177
+ values_a = a .values
178
+ values_b = b .values
179
+
180
+ # Calculate partial derivatives
181
+ partial_a = 2 * values_b / (values_a + values_b ) ** 2
182
+ partial_b = - 2 * values_a / (values_a + values_b ) ** 2
183
+
184
+ variance_return = (partial_a ** 2 * variances_a ) + (partial_b ** 2 * variances_b )
185
+
186
+ # Propagate uncertainties
187
+ polarization_value .variances = variance_return
188
+
189
+ return polarization_value
190
+
191
+
128
192
class ScalarNormalizer (Reducer , StandardReadable , ABC ):
129
193
"""Sum a set of user-specified spectra, then normalize by a scalar signal."""
130
194
0 commit comments