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

fix: dont reread files in sans2d workflow #58

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 49 additions & 70 deletions docs/examples/sans2d.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,32 @@
"id": "c21564a8-e742-4183-9edc-2c70c51d5863",
"metadata": {},
"source": [
"## Define reduction parameters\n",
"## Create pipeline using Sciline\n",
"\n",
"We use all providers available in `esssans` as well as the `sans2d`-specific providers, which include I/O and mask setup specific to the [Sans2d](https://www.isis.stfc.ac.uk/Pages/sans2d.aspx) instrument:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "025fca0b-68a7-4d6e-a82a-86713cc3fca7",
"metadata": {},
"outputs": [],
"source": [
"pipeline = sciline.Pipeline(\n",
" sans.providers + sans.sans2d.providers,\n",
" params=sans.sans2d.default_parameters,\n",
")"
]
},
{
"cell_type": "markdown",
"id": "3da2d397-6206-4ed1-a98f-11b3aaf7e5b0",
"metadata": {},
"source": [
"# Define reduction parameters\n",
"\n",
"We define a dictionary containing the reduction parameters, with keys and types given by aliases or types defined in `esssans.types`:"
"We define the reduction parameters, with keys and types given by aliases or types defined in `esssans.types`:"
]
},
{
Expand All @@ -50,55 +73,32 @@
},
"outputs": [],
"source": [
"params = sans.sans2d.default_parameters.copy()\n",
"\n",
"params[FileList[BackgroundRun]] = ['SANS2D00063159.hdf5']\n",
"params[FileList[TransmissionRun[BackgroundRun]]] = params[FileList[BackgroundRun]]\n",
"params[FileList[SampleRun]] = ['SANS2D00063114.hdf5']\n",
"params[FileList[TransmissionRun[SampleRun]]] = params[FileList[SampleRun]]\n",
"params[FileList[EmptyBeamRun]] = ['SANS2D00063091.hdf5']\n",
"params[DirectBeamFilename] = 'DIRECT_SANS2D_REAR_34327_4m_8mm_16Feb16.hdf5'\n",
"params[OutFilename] = 'reduced.nxs'\n",
"pipeline[FileList[BackgroundRun]] = ['SANS2D00063159.hdf5']\n",
"pipeline.insert(sans.transmission_from_background_run)\n",
"pipeline[FileList[SampleRun]] = ['SANS2D00063114.hdf5']\n",
"pipeline.insert(sans.transmission_from_sample_run)\n",
"pipeline[FileList[EmptyBeamRun]] = ['SANS2D00063091.hdf5']\n",
"pipeline[DirectBeamFilename] = 'DIRECT_SANS2D_REAR_34327_4m_8mm_16Feb16.hdf5'\n",
"pipeline[OutFilename] = 'reduced.nxs'\n",
"\n",
"params[WavelengthBins] = sc.linspace(\n",
"pipeline[WavelengthBins] = sc.linspace(\n",
" 'wavelength', start=2.0, stop=16.0, num=141, unit='angstrom'\n",
")\n",
"\n",
"params[sans.sans2d.LowCountThreshold] = sc.scalar(100, unit='counts')\n",
"pipeline[sans.sans2d.LowCountThreshold] = sc.scalar(100, unit='counts')\n",
"\n",
"mask_interval = sc.array(dims=['wavelength'], values=[2.21, 2.59], unit='angstrom')\n",
"params[WavelengthMask] = sc.DataArray(\n",
"pipeline[WavelengthMask] = sc.DataArray(\n",
" sc.array(dims=['wavelength'], values=[True]),\n",
" coords={'wavelength': mask_interval},\n",
")\n",
"\n",
"params[QBins] = sc.linspace(dim='Q', start=0.01, stop=0.6, num=141, unit='1/angstrom')\n",
"params[NonBackgroundWavelengthRange] = sc.array(\n",
"pipeline[QBins] = sc.linspace(dim='Q', start=0.01, stop=0.6, num=141, unit='1/angstrom')\n",
"pipeline[NonBackgroundWavelengthRange] = sc.array(\n",
" dims=['wavelength'], values=[0.7, 17.1], unit='angstrom'\n",
")\n",
"params[CorrectForGravity] = True\n",
"params[UncertaintyBroadcastMode] = UncertaintyBroadcastMode.upper_bound"
]
},
{
"cell_type": "markdown",
"id": "f2c3b542",
"metadata": {},
"source": [
"## Create pipeline using Sciline\n",
"\n",
"We use all providers available in `esssans` as well as the `sans2d`-specific providers, which include I/O and mask setup specific to the [Sans2d](https://www.isis.stfc.ac.uk/Pages/sans2d.aspx) instrument:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "17a01c27",
"metadata": {},
"outputs": [],
"source": [
"providers = sans.providers + sans.sans2d.providers\n",
"pipeline = sciline.Pipeline(providers, params=params)"
"pipeline[CorrectForGravity] = True\n",
"pipeline[UncertaintyBroadcastMode] = UncertaintyBroadcastMode.upper_bound"
]
},
{
Expand Down Expand Up @@ -178,9 +178,10 @@
"metadata": {},
"outputs": [],
"source": [
"params[UncertaintyBroadcastMode] = UncertaintyBroadcastMode.drop\n",
"pipeline_drop = sciline.Pipeline(providers, params=params)\n",
"result_drop = pipeline_drop.compute(BackgroundSubtractedIofQ)\n",
"pipeline[UncertaintyBroadcastMode] = UncertaintyBroadcastMode.drop\n",
"result_drop = pipeline.compute(BackgroundSubtractedIofQ)\n",
"# Reset the UnsertaintyBroadcastMode to the old value\n",
"pipeline[UncertaintyBroadcastMode] = UncertaintyBroadcastMode.upper_bound\n",
"sc.DataGroup(upper_bound=result, dropped=result_drop).plot(norm='log')"
]
},
Expand Down Expand Up @@ -288,10 +289,8 @@
" },\n",
" index=[Mode('upper_bound'), Mode('drop')],\n",
")\n",
"del params[UncertaintyBroadcastMode]\n",
"pl = sciline.Pipeline(providers, params=params)\n",
"pl.set_param_table(param_table)\n",
"results = pl.compute(sciline.Series[Mode, BackgroundSubtractedIofQ])\n",
"pipeline.set_param_table(param_table)\n",
"results = pipeline.compute(sciline.Series[Mode, BackgroundSubtractedIofQ])\n",
"sc.DataGroup(results).plot(norm='log')"
]
},
Expand All @@ -317,40 +316,19 @@
"outputs": [],
"source": [
"band = np.linspace(2.0, 16.0, num=11)\n",
"params[WavelengthBands] = sc.array(\n",
"pipeline[WavelengthBands] = sc.array(\n",
" dims=['band', 'wavelength'],\n",
" values=np.vstack([band[:-1], band[1:]]).T,\n",
" unit='angstrom',\n",
")"
]
},
{
"cell_type": "markdown",
"id": "84bce96e-e1f2-4a77-80ac-a926d3797020",
"metadata": {},
"source": [
"We then need to re-build the pipeline with the updated parameters"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b512a676-4875-47bc-9ac9-afcbf45c631a",
"metadata": {},
"outputs": [],
"source": [
"# Re-add the deleted param\n",
"params[UncertaintyBroadcastMode] = UncertaintyBroadcastMode.upper_bound\n",
"\n",
"pipeline = sciline.Pipeline(providers, params=params)"
]
},
{
"cell_type": "markdown",
"id": "cc3cac02-2cb4-4da5-a83d-8e6fb3d2c929",
"metadata": {},
"source": [
"and compute the result"
"Compute the result:"
]
},
{
Expand Down Expand Up @@ -398,7 +376,8 @@
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
"pygments_lexer": "ipython3",
"version": "3.10.13"
}
},
"nbformat": 4,
Expand Down
7 changes: 4 additions & 3 deletions docs/examples/zoom.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@
"source": [
"providers = sans.providers + sans.isis.providers\n",
"providers = providers + (\n",
" sans.isis.transmission_from_background_run,\n",
" sans.isis.transmission_from_sample_run,\n",
" sans.transmission_from_background_run,\n",
" sans.transmission_from_sample_run,\n",
")\n",
"pipeline = sciline.Pipeline(providers, params=params)\n",
"pipeline.set_param_table(masks)"
Expand Down Expand Up @@ -269,7 +269,8 @@
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
"pygments_lexer": "ipython3",
"version": "3.10.13"
}
},
"nbformat": 4,
Expand Down
3 changes: 3 additions & 0 deletions src/esssans/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
normalization,
sans2d,
)
from .common import transmission_from_background_run, transmission_from_sample_run
from .direct_beam import direct_beam

providers = (
Expand Down Expand Up @@ -50,4 +51,6 @@
'normalization',
'providers',
'sans2d',
'transmission_from_sample_run',
'transmission_from_background_run',
]
20 changes: 20 additions & 0 deletions src/esssans/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import scipp as sc
from scipp.constants import g

from .types import BackgroundRun, LoadedFileContents, SampleRun, TransmissionRun


def gravity_vector() -> sc.Variable:
"""
Expand Down Expand Up @@ -80,3 +82,21 @@ def mask_range(
else:
out.masks[name] = mask_values
return out


def transmission_from_sample_run(
data: LoadedFileContents[SampleRun],
) -> LoadedFileContents[TransmissionRun[SampleRun]]:
"""
Use transmission from a sample run, instead of dedicated run.
"""
return LoadedFileContents[TransmissionRun[SampleRun]](data)


def transmission_from_background_run(
data: LoadedFileContents[BackgroundRun],
) -> LoadedFileContents[TransmissionRun[BackgroundRun]]:
"""
Use transmission from a background run, instead of dedicated run.
"""
return LoadedFileContents[TransmissionRun[BackgroundRun]](data)
3 changes: 0 additions & 3 deletions src/esssans/isis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
sans2d_tube_detector_pixel_shape,
)
from . import io, masking
from .common import transmission_from_background_run, transmission_from_sample_run
from .io import CalibrationFilename, DataFolder, Filename, PixelMaskFilename
from .masking import PixelMask
from .visualization import plot_flat_detector_xy
Expand Down Expand Up @@ -39,7 +38,5 @@
'PixelMask',
'PixelMaskFilename',
'providers',
'transmission_from_background_run',
'transmission_from_sample_run',
'plot_flat_detector_xy',
]
24 changes: 0 additions & 24 deletions src/esssans/isis/common.py

This file was deleted.

Loading