-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract common parts to ess/reflectometry
- Loading branch information
Showing
3 changed files
with
93 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import scipp as sc | ||
|
||
from .supermirror import ( | ||
Alpha, | ||
CriticalEdge, | ||
MValue, | ||
supermirror_reflectivity, | ||
) | ||
from .types import ( | ||
QBins, | ||
ReducedReference, | ||
ReducibleData, | ||
Reference, | ||
ReferenceRun, | ||
ReflectivityOverQ, | ||
ReflectivityOverZW, | ||
Sample, | ||
WavelengthBins, | ||
) | ||
|
||
|
||
def reduce_reference( | ||
reference: ReducibleData[ReferenceRun], | ||
wavelength_bins: WavelengthBins, | ||
critical_edge: CriticalEdge, | ||
mvalue: MValue, | ||
alpha: Alpha, | ||
) -> ReducedReference: | ||
""" | ||
Reduces the reference measurement to estimate the | ||
intensity distribution in the detector for | ||
an ideal sample with reflectivity :math:`R = 1`. | ||
""" | ||
R = supermirror_reflectivity( | ||
reference.bins.coords['Q'], | ||
c=critical_edge, | ||
m=mvalue, | ||
alpha=alpha, | ||
) | ||
reference.bins.masks['invalid'] = sc.isnan(R) | ||
reference /= R | ||
return reference.bins.concat(('stripe',)).hist(wavelength=wavelength_bins) | ||
|
||
|
||
def reduce_sample_over_q( | ||
sample: Sample, | ||
reference: Reference, | ||
qbins: QBins, | ||
) -> ReflectivityOverQ: | ||
""" | ||
Computes reflectivity as ratio of | ||
sample intensity and intensity from a sample | ||
with ideal reflectivity. | ||
Returns reflectivity as a function of :math:`Q`. | ||
""" | ||
h = reference.flatten(to='Q').hist(Q=qbins) | ||
R = sample.bins.concat().bin(Q=qbins) / h.data | ||
R.coords['Q_resolution'] = sc.sqrt( | ||
( | ||
(reference * reference.coords['Q_resolution'] ** 2) | ||
.flatten(to='Q') | ||
.hist(Q=qbins) | ||
) | ||
/ h | ||
).data | ||
return R | ||
|
||
|
||
def reduce_sample_over_zw( | ||
sample: Sample, | ||
reference: Reference, | ||
wbins: WavelengthBins, | ||
) -> ReflectivityOverZW: | ||
""" | ||
Computes reflectivity as ratio of | ||
sample intensity and intensity from a sample | ||
with ideal reflectivity. | ||
Returns reflectivity as a function of ``blade``, ``wire`` and :math:`\\wavelength`. | ||
""" | ||
R = sample.bins.concat(('stripe',)).bin(wavelength=wbins) / reference.data | ||
R.masks["too_few_events"] = reference.data < sc.scalar(1, unit="counts") | ||
return R | ||
|
||
|
||
providers = ( | ||
reduce_reference, | ||
reduce_sample_over_q, | ||
reduce_sample_over_zw, | ||
) |