Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workflow dependency restructure #155

Merged
merged 11 commits into from
Aug 7, 2024
63 changes: 36 additions & 27 deletions docs/user-guide/common/beam-center-finder.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"metadata": {},
"outputs": [],
"source": [
"raw = workflow.compute(RawData[SampleRun])['spectrum', :61440]\n",
"raw = workflow.compute(RawDetector[SampleRun])['spectrum', :61440]\n",
"\n",
"p = isis.plot_flat_detector_xy(raw.hist(), norm='log')\n",
"p.ax.plot(0, 0, '+', color='k', ms=10)\n",
Expand Down Expand Up @@ -128,6 +128,7 @@
"metadata": {},
"outputs": [],
"source": [
"workflow[BeamCenter] = sc.vector(value=[0, 0, 0], unit='m')\n",
"masked = workflow.compute(MaskedData[SampleRun])['spectrum', :61440].copy()\n",
"masked.masks['low_counts'] = masked.hist().data < sc.scalar(80.0, unit='counts')\n",
"\n",
Expand Down Expand Up @@ -156,9 +157,16 @@
"metadata": {},
"outputs": [],
"source": [
"# Insert the provider that computes the center-of-mass of the pixels\n",
"workflow.insert(sans.beam_center_finder.beam_center_from_center_of_mass)\n",
"workflow.visualize(BeamCenter, graph_attr={'rankdir': 'LR'})"
"# The center-of-mass approach is based on the MaskedData\n",
"workflow.visualize(MaskedData[SampleRun])"
]
},
{
"cell_type": "markdown",
"id": "d7aac9d0",
"metadata": {},
"source": [
"We use the workflow to fill in the required arguments and call the function:"
]
},
{
Expand All @@ -168,7 +176,7 @@
"metadata": {},
"outputs": [],
"source": [
"com = workflow.compute(BeamCenter)\n",
"com = sans.beam_center_finder.beam_center_from_center_of_mass(workflow)\n",
"com"
]
},
Expand Down Expand Up @@ -245,11 +253,7 @@
"workflow = isis.sans2d.Sans2dTutorialWorkflow()\n",
"# For real data use:\n",
"# workflow = isis.sans2d.Sans2dWorkflow()\n",
"\n",
"workflow.insert(isis.data.transmission_from_sample_run)\n",
"# Insert the provider that computes the center from I(Q) curves\n",
"workflow.insert(sans.beam_center_finder.beam_center_from_iofq)\n",
"workflow.visualize(BeamCenter, graph_attr={'rankdir': 'LR'})"
"workflow.insert(isis.data.transmission_from_sample_run)"
]
},
{
Expand Down Expand Up @@ -280,20 +284,16 @@
"\n",
"workflow[CorrectForGravity] = True\n",
"workflow[UncertaintyBroadcastMode] = UncertaintyBroadcastMode.upper_bound\n",
"workflow[sans.beam_center_finder.BeamCenterFinderQBins] = sc.linspace(\n",
" 'Q', 0.02, 0.25, 71, unit='1/angstrom'\n",
")\n",
"workflow[sans.beam_center_finder.BeamCenterFinderMinimizer] = None\n",
"workflow[sans.beam_center_finder.BeamCenterFinderTolerance] = None\n",
"workflow[DirectBeam] = None"
"workflow[DirectBeam] = None\n",
"workflow[isis.sans2d.LowCountThreshold] = sc.scalar(-1, unit=\"counts\")"
]
},
{
"cell_type": "markdown",
"id": "641be8e9",
"metadata": {},
"source": [
"Finally, we set the data to be used, including overriding with data that contains the new mask defined earlier:"
"Finally, we set the data to be used, including overriding with the new mask defined earlier:"
]
},
{
Expand All @@ -304,7 +304,10 @@
"outputs": [],
"source": [
"workflow[Filename[SampleRun]] = isis.data.sans2d_tutorial_sample_run()\n",
"workflow[MaskedData[SampleRun]] = masked"
"detector = workflow.compute(RawDetector[SampleRun])['spectrum', :61440].assign_masks(\n",
" masked.masks\n",
")\n",
"workflow[RawDetector[SampleRun]] = detector"
]
},
{
Expand All @@ -322,11 +325,15 @@
{
"cell_type": "code",
"execution_count": null,
"id": "c087798a-fc33-4292-924c-eed8e3baf36a",
"id": "329a0cd9",
"metadata": {},
"outputs": [],
"source": [
"iofq_center = workflow.compute(BeamCenter)\n",
"q_bins = sc.linspace('Q', 0.02, 0.25, 71, unit='1/angstrom')\n",
"workflow[BeamCenter] = sc.vector([0, 0, 0], unit='m')\n",
"iofq_center = sans.beam_center_finder.beam_center_from_iofq(\n",
" workflow=workflow, q_bins=q_bins\n",
")\n",
"iofq_center"
]
},
Expand Down Expand Up @@ -433,14 +440,16 @@
"metadata": {},
"outputs": [],
"source": [
"workflow = workflow.copy()\n",
"workflow[QBins] = q_bins\n",
"workflow[ReturnEvents] = False\n",
"workflow[DimsToKeep] = ()\n",
"workflow[WavelengthMask] = None\n",
"workflow[WavelengthBands] = None\n",
"kwargs = dict( # noqa: C408\n",
" data=masked,\n",
" workflow=workflow,\n",
" detector=detector,\n",
" norm=workflow.compute(NormWavelengthTerm[SampleRun]),\n",
" graph=workflow.compute(sans.conversions.ElasticCoordTransformGraph),\n",
" q_bins=workflow.compute(sans.beam_center_finder.BeamCenterFinderQBins),\n",
" wavelength_bins=workflow.compute(WavelengthBins),\n",
" transform=workflow.compute(LabFrameTransform[SampleRun]),\n",
" pixel_shape=workflow.compute(DetectorPixelShape[SampleRun]),\n",
")"
]
},
Expand Down Expand Up @@ -628,7 +637,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.10.14"
}
},
"nbformat": 4,
Expand Down
43 changes: 40 additions & 3 deletions docs/user-guide/isis/sans2d.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@
"outputs": [],
"source": [
"workflow.insert(isis.data.transmission_from_background_run)\n",
"workflow.insert(isis.data.transmission_from_sample_run)\n",
"workflow.insert(sans.beam_center_from_center_of_mass)"
"workflow.insert(isis.data.transmission_from_sample_run)"
]
},
{
Expand Down Expand Up @@ -167,6 +166,44 @@
"workflow[Filename[EmptyBeamRun]] = isis.data.sans2d_tutorial_empty_beam_run()"
]
},
{
"cell_type": "markdown",
"id": "981f1f28",
"metadata": {},
"source": [
"The beam center is not set yet.\n",
"Unless we have a previously computed value, we can do so now:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "064f87a4",
"metadata": {},
"outputs": [],
"source": [
"center = sans.beam_center_from_center_of_mass(workflow)\n",
"center"
]
},
{
"cell_type": "markdown",
"id": "7d7ce61a",
"metadata": {},
"source": [
"Remember to update the workflow with the new center:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0a4c43f9",
"metadata": {},
"outputs": [],
"source": [
"workflow[BeamCenter] = center"
]
},
{
"cell_type": "markdown",
"id": "c19eeaf0",
Expand Down Expand Up @@ -391,7 +428,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.10.14"
}
},
"nbformat": 4,
Expand Down
29 changes: 25 additions & 4 deletions docs/user-guide/isis/zoom.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@
"metadata": {},
"source": [
"We can insert steps for configuring the workflow.\n",
"In this case, we would like to use the transmission monitor from the regular background and sample runs since there was no separate transmission run.\n",
"We also want to compute the beam center using a simple center-of-mass estimation:"
"In this case, we would like to use the transmission monitor from the regular background and sample runs since there was no separate transmission run."
]
},
{
Expand All @@ -83,8 +82,7 @@
"outputs": [],
"source": [
"workflow.insert(isis.data.transmission_from_background_run)\n",
"workflow.insert(isis.data.transmission_from_sample_run)\n",
"workflow.insert(sans.beam_center_from_center_of_mass)"
"workflow.insert(isis.data.transmission_from_sample_run)"
]
},
{
Expand Down Expand Up @@ -176,6 +174,29 @@
"source": [
"## Use the workflow\n",
"\n",
"### Set or compute the beam center\n",
"\n",
"The beam center is not set by default.\n",
"We can either set it to a known value, or compute it from the data:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "568cd4be",
"metadata": {},
"outputs": [],
"source": [
"workflow[BeamCenter] = sans.beam_center_from_center_of_mass(workflow)"
]
},
{
"cell_type": "markdown",
"id": "13ddd330",
"metadata": {},
"source": [
"\n",
"\n",
"### Compute final result\n",
"\n",
"We can now compute $I(Q)$:"
Expand Down
38 changes: 5 additions & 33 deletions docs/user-guide/loki/loki-direct-beam.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -190,36 +190,8 @@
"and inserting the provider that will compute the beam center.\n",
"\n",
"For now, we compute the beam center only for the rear detector (named 'larmor_detector') but apply it to all banks (currently there is only one bank).\n",
"The beam center may need to be computed or applied differently to each bank, see [scipp/esssans#28](https://github.com/scipp/esssans/issues/28)."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bdc15ab4-6ec6-4a29-81dc-1d00a8e890dc",
"metadata": {},
"outputs": [],
"source": [
"bc_workflow = workflow.copy()\n",
"bc_workflow.insert(sans.beam_center_from_center_of_mass)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7cc2cf3b-973e-4425-93ea-deb30b12c956",
"metadata": {},
"outputs": [],
"source": [
"bc_workflow.visualize(BeamCenter, compact=True, graph_attr={'rankdir': 'LR'})"
]
},
{
"cell_type": "markdown",
"id": "b6bf5342-6768-4b65-882b-aefc9b583724",
"metadata": {},
"source": [
"We can now compute the value for the center"
"The beam center may need to be computed or applied differently to each bank, see [scipp/esssans#28](https://github.com/scipp/esssans/issues/28).\n",
"We use a center-of-mass approach to find the beam center:"
]
},
{
Expand All @@ -229,7 +201,7 @@
"metadata": {},
"outputs": [],
"source": [
"center = bc_workflow.compute(BeamCenter)\n",
"center = sans.beam_center_from_center_of_mass(workflow)\n",
"center"
]
},
Expand All @@ -238,7 +210,7 @@
"id": "0814805a-9611-4951-a015-c12ae254b099",
"metadata": {},
"source": [
"and set that value onto our original workflow"
"and set that value in our workflow"
]
},
{
Expand Down Expand Up @@ -639,7 +611,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.10.14"
}
},
"nbformat": 4,
Expand Down
22 changes: 8 additions & 14 deletions src/ess/isissans/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import scipp as sc

from ..sans.types import (
BeamCenter,
CalibratedDetector,
MonitorType,
RawDetector,
RawDetectorData,
RawMonitor,
RawMonitorData,
RunType,
Expand All @@ -24,11 +25,9 @@ class MonitorOffset(sciline.Scope[MonitorType, sc.Variable], sc.Variable):
DetectorBankOffset = NewType('DetectorBankOffset', sc.Variable)


def apply_component_user_offsets_to_raw_data(
data: RawDetector[ScatteringRunType],
sample_offset: SampleOffset,
detector_bank_offset: DetectorBankOffset,
) -> RawDetectorData[ScatteringRunType]:
def apply_beam_center(
data: RawDetector[ScatteringRunType], beam_center: BeamCenter
) -> CalibratedDetector[ScatteringRunType]:
"""Apply user offsets to raw data.

Parameters
Expand All @@ -40,14 +39,9 @@ def apply_component_user_offsets_to_raw_data(
detector_bank_offset:
Detector bank offset.
"""
data = data.copy(deep=False)
sample_pos = data.coords['sample_position']
data.coords['sample_position'] = sample_pos + sample_offset.to(
unit=sample_pos.unit, copy=False
return CalibratedDetector[ScatteringRunType](
data.assign_coords(position=data.coords['position'] - beam_center)
)
pos = data.coords['position']
data.coords['position'] = pos + detector_bank_offset.to(unit=pos.unit, copy=False)
return RawDetectorData[ScatteringRunType](data)


def apply_component_user_offsets_to_raw_monitor(
Expand All @@ -69,6 +63,6 @@ def apply_component_user_offsets_to_raw_monitor(


providers = (
apply_component_user_offsets_to_raw_data,
apply_beam_center,
apply_component_user_offsets_to_raw_monitor,
)
Loading