Skip to content

Commit

Permalink
refactor: extract common parts to ess/reflectometry
Browse files Browse the repository at this point in the history
  • Loading branch information
jokasimr committed Dec 16, 2024
1 parent ab7fee5 commit 84e0a55
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 73 deletions.
72 changes: 0 additions & 72 deletions src/ess/amor/normalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@
ReducedReference,
ReducibleData,
Reference,
ReferenceRun,
ReflectivityOverQ,
ReflectivityOverZW,
Sample,
SampleRun,
WavelengthBins,
)
from .conversions import theta
from .resolution import (
Expand Down Expand Up @@ -71,71 +67,6 @@ def mask_events_where_supermirror_does_not_cover(
return sam


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


def evaluate_reference(
reference: ReducedReference,
sample: ReducibleData[SampleRun],
Expand Down Expand Up @@ -177,9 +108,6 @@ def evaluate_reference(


providers = (
reduce_reference,
reduce_sample_over_q,
reduce_sample_over_zw,
evaluate_reference,
mask_events_where_supermirror_does_not_cover,
)
3 changes: 2 additions & 1 deletion src/ess/reflectometry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
__version__ = "0.0.0"


from . import conversions, orso
from . import conversions, orso, normalization
from .load import load_reference, save_reference

providers = (
*conversions.providers,
*orso.providers,
*normalization.providers,
)
"""
List of providers for setting up a Sciline pipeline.
Expand Down
91 changes: 91 additions & 0 deletions src/ess/reflectometry/normalization.py
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,
)

0 comments on commit 84e0a55

Please sign in to comment.