Skip to content

Commit

Permalink
restructure the type system to avoid nested parameterized types
Browse files Browse the repository at this point in the history
  • Loading branch information
jokasimr committed Oct 23, 2023
1 parent 8578278 commit 2d60e60
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 102 deletions.
30 changes: 8 additions & 22 deletions docs/examples/amor.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,18 @@
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"import scipp as sc\n",
"import sciline\n",
"from essreflectometry.amor.load import load\n",
"from essreflectometry.amor.beamline import make_beamline\n",
"from essreflectometry.amor.conversions import specular_reflection\n",
"from essreflectometry.amor.resolution import compute_resolution\n",
"from essreflectometry.amor.normalize import normalize_by_supermirror\n",
"from essreflectometry.amor.calibrations import supermirror_calibration\n",
"from essreflectometry.reflectometry.corrections import footprint_correction, normalize_by_counts\n",
"from essreflectometry.reflectometry.conversions import providers\n",
"from essreflectometry.amor import providers as amor\n",
"from essreflectometry.reflectometry import providers as reflectometry\n",
"from essreflectometry.reflectometry.types import (\n",
" ThetaBins, WavelengthBins, Sample, Reference, Sample, SampleRotation, Filename, Raw, BeamlineParams,\n",
" ThetaData, WavelengthData, HistogrammedByQ, QDataWithResolutions, QData, QBins, NormalizedIOverQ\n",
" Sample, Reference, Sample, SampleRotation, Filename,\n",
" ThetaData, QBins, NormalizedIOverQ, QStd\n",
")\n",
"\n",
"\n",
"pipeline = sciline.Pipeline(\n",
" [load, make_beamline, specular_reflection, footprint_correction,\n",
" compute_resolution, normalize_by_counts, supermirror_calibration, normalize_by_supermirror]\n",
" + providers,\n",
" amor + reflectometry,\n",
" params={\n",
" ThetaBins: sc.linspace(dim='theta', start=0, stop=np.pi/2, num=2, unit='rad'),\n",
" WavelengthBins: sc.array(dims=['wavelength'], values=[2.4, 16.0], unit='angstrom'),\n",
" QBins: sc.geomspace(dim='Q', start=0.008, stop=0.075, num=200, unit='1/angstrom'),\n",
"\n",
" SampleRotation[Sample]: sc.scalar(0.7989, unit='deg'),\n",
Expand All @@ -40,7 +27,7 @@
" }\n",
")\n",
"\n",
"pipeline.visualize(NormalizedIOverQ)"
"pipeline.visualize((NormalizedIOverQ, QStd))"
]
},
{
Expand Down Expand Up @@ -75,9 +62,8 @@
"metadata": {},
"outputs": [],
"source": [
"import plopp\n",
"\n",
"[]"
"res = pipeline.compute((NormalizedIOverQ, QStd))\n",
"res[QStd].plot()"
]
}
],
Expand Down
18 changes: 15 additions & 3 deletions src/essreflectometry/amor/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
# flake8: noqa: F401
from . import calibrations, conversions, data, normalize, resolution, tools
from .beamline import instrument_view_components, make_beamline
from itertools import chain

from . import calibrations, conversions, load, normalize, resolution, tools
from .beamline import instrument_view_components
from .instrument_view import instrument_view
from .load import load

providers = list(
chain(
load.providers,
calibrations.providers,
conversions.providers,
normalize.providers,
resolution.providers,
beamline.providers,
)
)
4 changes: 4 additions & 0 deletions src/essreflectometry/amor/beamline.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
def make_beamline(
sample_rotation: SampleRotation[Run],
) -> BeamlineParams[Run]:
# TODO
beam_size: sc.Variable = None
sample_size: sc.Variable = None
detector_spatial_resolution: sc.Variable = None
Expand Down Expand Up @@ -132,3 +133,6 @@ def instrument_view_components(da: sc.DataArray) -> dict:
'type': 'disk',
},
}


providers = [make_beamline]
11 changes: 7 additions & 4 deletions src/essreflectometry/amor/calibrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import scipp as sc

# from ..reflectometry import orso
from ..reflectometry.types import CalibratedReference, HistogrammedByQ, QData, Reference
from ..reflectometry.types import CalibratedReference, Histogrammed, Reference


def supermirror_calibration(
data_array: HistogrammedByQ[QData[Reference]],
) -> CalibratedReference:
data_array: Histogrammed[Reference],
) -> Histogrammed[CalibratedReference]:
# TODO
m_value: sc.Variable = None
critical_edge: sc.Variable = None
Expand Down Expand Up @@ -47,7 +47,7 @@ def supermirror_calibration(
# ]
# except KeyError:
# orso.not_found_warning()
return CalibratedReference(data_array_cal)
return Histogrammed[CalibratedReference](data_array_cal)


def calibration_factor(
Expand Down Expand Up @@ -90,3 +90,6 @@ def calibration_factor(
nq = 1.0 / (1.0 - alpha * (q - critical_edge))
calibration_factor = sc.where(q < max_q, lim + (1 - lim) * nq, sc.scalar(1.0))
return calibration_factor


providers = [supermirror_calibration]
4 changes: 3 additions & 1 deletion src/essreflectometry/amor/conversions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
from typing import NewType
import scipp as sc

from ..reflectometry.conversions import specular_reflection as spec_relf_graph
Expand Down Expand Up @@ -31,3 +30,6 @@ def specular_reflection() -> SpecularReflectionCoordTransformGraph:
graph = spec_relf_graph()
graph['incident_beam'] = incident_beam
return SpecularReflectionCoordTransformGraph(graph)


providers = [specular_reflection]
3 changes: 3 additions & 0 deletions src/essreflectometry/amor/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,6 @@ def populate_orso(orso: Any, data: sc.DataGroup, filename: str) -> Any:
'%Y-%m-%d',
)
orso.data_source.measurement.data_files = [filename]


providers = [load]
14 changes: 8 additions & 6 deletions src/essreflectometry/amor/normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
# from ..reflectometry import orso
from ..reflectometry.types import (
CalibratedReference,
HistogrammedByQ,
NormalizedData,
Normalized,
NormalizedIOverQ,
QDataWithResolutions,
Sample,
)


def normalize_by_supermirror(
sample: NormalizedData[HistogrammedByQ[QDataWithResolutions]],
supermirror: NormalizedData[CalibratedReference],
sample: Normalized[Sample],
supermirror: Normalized[CalibratedReference],
) -> NormalizedIOverQ:
"""
Normalize the sample measurement by the (ideally calibrated) supermirror.
Expand Down Expand Up @@ -50,4 +49,7 @@ def normalize_by_supermirror(
# ].value.data_source.measurement.data_files
# except KeyError:
# orso.not_found_warning()
return normalized
return NormalizedIOverQ(normalized)


providers = [normalize_by_supermirror]
67 changes: 45 additions & 22 deletions src/essreflectometry/amor/resolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,49 @@
# Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
import scipp as sc

from ..reflectometry.types import QData, QDataWithResolutions, Sample
from ..reflectometry.types import (
AngularResolution,
Histogrammed,
QBins,
QData,
QStd,
Sample,
SampleSizeResolution,
WavelengthResolution,
)
from .tools import fwhm_to_std


def compute_resolution(da: QData[Sample]) -> QDataWithResolutions:
da.coords['wavelength_resolution'] = wavelength_resolution(
chopper_1_position=da.coords['source_chopper_1'].value['position'],
chopper_2_position=da.coords['source_chopper_2'].value['position'],
pixel_position=da.coords['position'],
def wavelength_resolution(da: QData[Sample]) -> WavelengthResolution:
return WavelengthResolution(
_wavelength_resolution(
chopper_1_position=da.coords['source_chopper_1'].value['position'],
chopper_2_position=da.coords['source_chopper_2'].value['position'],
pixel_position=da.coords['position'],
)
)
da.coords['angular_resolution'] = angular_resolution(
pixel_position=da.coords['position'],
theta=da.bins.coords['theta'],
detector_spatial_resolution=da.coords['detector_spatial_resolution'],


def angular_resolution(da: QData[Sample]) -> AngularResolution:
return AngularResolution(
_angular_resolution(
pixel_position=da.coords['position'],
theta=da.bins.coords['theta'],
detector_spatial_resolution=da.coords['detector_spatial_resolution'],
)
)
da.coords['sample_size_resolution'] = sample_size_resolution(
pixel_position=da.coords['position'],
sample_size=da.coords['sample_size'],


def sample_size_resolution(da: QData[Sample]) -> SampleSizeResolution:
return SampleSizeResolution(
_sample_size_resolution(
pixel_position=da.coords['position'],
sample_size=da.coords['sample_size'],
)
)
return QDataWithResolutions(da)


def wavelength_resolution(
def _wavelength_resolution(
chopper_1_position: sc.Variable,
chopper_2_position: sc.Variable,
pixel_position: sc.Variable,
Expand Down Expand Up @@ -57,7 +77,7 @@ def wavelength_resolution(
return fwhm_to_std(distance_between_choppers / chopper_detector_distance)


def sample_size_resolution(
def _sample_size_resolution(
pixel_position: sc.Variable, sample_size: sc.Variable
) -> sc.Variable:
"""
Expand All @@ -83,7 +103,7 @@ def sample_size_resolution(
)


def angular_resolution(
def _angular_resolution(
pixel_position: sc.Variable,
theta: sc.Variable,
detector_spatial_resolution: sc.Variable,
Expand Down Expand Up @@ -123,11 +143,11 @@ def angular_resolution(


def sigma_Q(
angular_resolution: sc.Variable,
wavelength_resolution: sc.Variable,
sample_size_resolution: sc.Variable,
q_bins: sc.Variable,
) -> sc.Variable:
angular_resolution: Histogrammed[AngularResolution],
wavelength_resolution: WavelengthResolution,
sample_size_resolution: SampleSizeResolution,
q_bins: QBins,
) -> QStd:
"""
Combine all of the components of the resolution and add Q contribution.
Expand All @@ -152,3 +172,6 @@ def sigma_Q(
+ wavelength_resolution**2
+ sample_size_resolution**2
).max('detector_number') * sc.midpoints(q_bins)


providers = [sigma_Q, angular_resolution, wavelength_resolution, sample_size_resolution]
8 changes: 8 additions & 0 deletions src/essreflectometry/reflectometry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,13 @@
# Copyright (c) 2023 Scipp contributors (https://github.com/scipp)

# flake8: noqa: F401
from itertools import chain

from . import conversions, corrections, io

providers = list(
chain(
conversions.providers,
corrections.providers,
)
)
Loading

0 comments on commit 2d60e60

Please sign in to comment.