1212from ._util import event_or_outer_coord
1313from .types import (
1414 AccumulatedProtonCharge ,
15+ BackgroundRun ,
16+ BackgroundSubtractedData ,
17+ BackgroundSubtractedDataTwoTheta ,
1518 CaveMonitor ,
1619 DataWithScatteringCoordinates ,
1720 FocussedDataDspacing ,
@@ -154,13 +157,49 @@ def _normalize_by_vanadium(
154157 return normed
155158
156159
160+ def normalize_by_vanadium_dspacing_removed_empty_can (
161+ data : BackgroundSubtractedData [SampleRun ],
162+ vanadium : FocussedDataDspacing [VanadiumRun ],
163+ uncertainty_broadcast_mode : UncertaintyBroadcastMode ,
164+ ) -> IofDspacing :
165+ """
166+ Normalize sample data by a vanadium measurement and return intensity vs. d-spacing.
167+
168+ Parameters
169+ ----------
170+ data:
171+ Sample data where events from an empty can / empty instrument measurement
172+ have been subtracted.
173+ vanadium:
174+ Vanadium data.
175+ uncertainty_broadcast_mode:
176+ Choose how uncertainties of vanadium are broadcast to the sample data.
177+ Defaults to ``UncertaintyBroadcastMode.fail``.
178+
179+ Returns
180+ -------
181+ :
182+ ``data / vanadium``.
183+ May contain a mask "zero_vanadium" which is ``True``
184+ for bins where vanadium is zero.
185+
186+ See also
187+ --------
188+ normalize_by_vanadium_dspacing:
189+ The same function but using data where no empty can has been subtracted.
190+ """
191+ return IofDspacing (
192+ _normalize_by_vanadium (data , vanadium , uncertainty_broadcast_mode )
193+ )
194+
195+
157196def normalize_by_vanadium_dspacing (
158197 data : FocussedDataDspacing [SampleRun ],
159198 vanadium : FocussedDataDspacing [VanadiumRun ],
160199 uncertainty_broadcast_mode : UncertaintyBroadcastMode ,
161200) -> IofDspacing :
162201 """
163- Normalize sample data by a vanadium measurement and return intensity vs d-spacing.
202+ Normalize sample data by a vanadium measurement and return intensity vs. d-spacing.
164203
165204 Parameters
166205 ----------
@@ -178,19 +217,62 @@ def normalize_by_vanadium_dspacing(
178217 ``data / vanadium``.
179218 May contain a mask "zero_vanadium" which is ``True``
180219 for bins where vanadium is zero.
220+
221+ See also
222+ --------
223+ normalize_by_vanadium_dspacing_removed_empty_can:
224+ The same function but using data where events from an empty can /
225+ empty instrument measurement have been subtracted.
181226 """
182227 return IofDspacing (
183228 _normalize_by_vanadium (data , vanadium , uncertainty_broadcast_mode )
184229 )
185230
186231
232+ def normalize_by_vanadium_dspacing_and_two_theta_removed_empty_can (
233+ data : BackgroundSubtractedDataTwoTheta [SampleRun ],
234+ vanadium : FocussedDataDspacingTwoTheta [VanadiumRun ],
235+ uncertainty_broadcast_mode : UncertaintyBroadcastMode ,
236+ ) -> IofDspacingTwoTheta :
237+ """
238+ Normalize sample data by a vanadium measurement and return intensity vs.
239+ (d-spacing, 2theta).
240+
241+ Parameters
242+ ----------
243+ data:
244+ Sample data where events from an empty can / empty instrument measurement
245+ have been subtracted.
246+ vanadium:
247+ Vanadium data.
248+ uncertainty_broadcast_mode:
249+ Choose how uncertainties of vanadium are broadcast to the sample data.
250+ Defaults to ``UncertaintyBroadcastMode.fail``.
251+
252+ Returns
253+ -------
254+ :
255+ ``data / vanadium``.
256+ May contain a mask "zero_vanadium" which is ``True``
257+ for bins where vanadium is zero.
258+
259+ See also
260+ --------
261+ normalize_by_vanadium_dspacing_and_two_theta:
262+ The same function but using data where no empty can has been subtracted.
263+ """
264+ return IofDspacingTwoTheta (
265+ _normalize_by_vanadium (data , vanadium , uncertainty_broadcast_mode )
266+ )
267+
268+
187269def normalize_by_vanadium_dspacing_and_two_theta (
188270 data : FocussedDataDspacingTwoTheta [SampleRun ],
189271 vanadium : FocussedDataDspacingTwoTheta [VanadiumRun ],
190272 uncertainty_broadcast_mode : UncertaintyBroadcastMode ,
191273) -> IofDspacingTwoTheta :
192274 """
193- Normalize sample data by a vanadium measurement and return intensity vs
275+ Normalize sample data by a vanadium measurement and return intensity vs.
194276 (d-spacing, 2theta).
195277
196278 Parameters
@@ -209,6 +291,12 @@ def normalize_by_vanadium_dspacing_and_two_theta(
209291 ``data / vanadium``.
210292 May contain a mask "zero_vanadium" which is ``True``
211293 for bins where vanadium is zero.
294+
295+ See also
296+ --------
297+ normalize_by_vanadium_dspacing_and_two_theta_removed_empty_can:
298+ The same function but using data where events from an empty can /
299+ empty instrument measurement have been subtracted.
212300 """
213301 return IofDspacingTwoTheta (
214302 _normalize_by_vanadium (data , vanadium , uncertainty_broadcast_mode )
@@ -335,6 +423,22 @@ def _shallow_copy(da: sc.DataArray) -> sc.DataArray:
335423 return out
336424
337425
426+ def subtract_background (
427+ data : FocussedDataDspacing [SampleRun ],
428+ background : FocussedDataDspacing [BackgroundRun ],
429+ ) -> BackgroundSubtractedData [SampleRun ]:
430+ return BackgroundSubtractedData [SampleRun ](data .bins .concatenate (- background ))
431+
432+
433+ def subtract_background_two_theta (
434+ data : FocussedDataDspacingTwoTheta [SampleRun ],
435+ background : FocussedDataDspacingTwoTheta [BackgroundRun ],
436+ ) -> BackgroundSubtractedDataTwoTheta [SampleRun ]:
437+ return BackgroundSubtractedDataTwoTheta [SampleRun ](
438+ data .bins .concatenate (- background )
439+ )
440+
441+
338442class RunNormalization (enum .Enum ):
339443 """Type of normalization applied to each run."""
340444
@@ -356,7 +460,15 @@ def insert_run_normalization(
356460 workflow .insert (normalize_by_proton_charge )
357461
358462
463+ def add_empty_can_subtraction (workflow : sciline .Pipeline ) -> None :
464+ """Insert providers to subtract empty can events from sample data."""
465+ workflow .insert (normalize_by_vanadium_dspacing_removed_empty_can )
466+ workflow .insert (normalize_by_vanadium_dspacing_and_two_theta_removed_empty_can )
467+
468+
359469providers = (
470+ subtract_background ,
471+ subtract_background_two_theta ,
360472 normalize_by_proton_charge ,
361473 normalize_by_vanadium_dspacing ,
362474 normalize_by_vanadium_dspacing_and_two_theta ,
0 commit comments