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

Merge sans2d and zoom code into isis module #77

Merged
merged 36 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
8ab0169
fix wavelength masking function
nvaytet Feb 9, 2024
a5081f9
add possibility of shifting monitor position
nvaytet Feb 9, 2024
48a657f
add general providers
nvaytet Feb 9, 2024
159b6e3
fix io providers and remove single-bin tof coordinate from mantid loa…
nvaytet Feb 9, 2024
871f327
add sans2d and zoom specific files
nvaytet Feb 9, 2024
1a6b0a8
cleanup zoom
nvaytet Feb 9, 2024
d25df8d
add dummy provider in loki for calibrated monitor
nvaytet Feb 9, 2024
7931b08
fix masking range test
nvaytet Feb 9, 2024
037ab8d
migrate sans2d tests
nvaytet Feb 9, 2024
ce7d831
remove unused files
nvaytet Feb 9, 2024
1f5e10e
update notebooks
nvaytet Feb 9, 2024
930b6b3
fix direct beam provider in tests
nvaytet Feb 9, 2024
fb27e52
fix phi coordinate in beam center finder
nvaytet Feb 9, 2024
df08d43
fix module path
nvaytet Feb 9, 2024
95829f9
finish fixing sans2d tests
nvaytet Feb 9, 2024
bf95f6b
update beam center finder notebook
nvaytet Feb 9, 2024
56b8545
flake8
nvaytet Feb 9, 2024
db411ee
formatting
nvaytet Feb 9, 2024
145371b
fix get_path call
nvaytet Feb 10, 2024
eba3fd7
fix sans notebook and make single get_detector_data
nvaytet Feb 10, 2024
2b77488
remove debugging cells from notebook
nvaytet Feb 10, 2024
60b332a
bin only once and compute bounds manually if needed
nvaytet Feb 12, 2024
75193ed
add sans.transmission_from_sample_run provider in beam center finder …
nvaytet Feb 12, 2024
54cebda
make subfolders for isis instruments
nvaytet Feb 12, 2024
87e740a
make a common isis data registry
nvaytet Feb 12, 2024
d133194
cleanup and update notebooks
nvaytet Feb 12, 2024
434595a
flake8
nvaytet Feb 12, 2024
209bf13
return CalibratedMonitor directly in loki
nvaytet Feb 12, 2024
d043a0d
formatting
nvaytet Feb 12, 2024
93e0160
fix normalization and sans2d tests
nvaytet Feb 12, 2024
14f4092
flake8
nvaytet Feb 12, 2024
c128209
Merge branch 'main' into merge-sans2d-zoom
nvaytet Feb 12, 2024
e440743
add basic zoom tests
nvaytet Feb 12, 2024
0cf1868
fix docs build
nvaytet Feb 12, 2024
6b116bc
add test to make sure workflow can run without gravity if beam center…
nvaytet Feb 12, 2024
90dc80b
add unit conversion for edges in mask_range
nvaytet Feb 13, 2024
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
1 change: 0 additions & 1 deletion docs/api-reference/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
isis
logging
loki
sans2d
types
uncertainty
```
46 changes: 30 additions & 16 deletions docs/examples/beam-center-finder.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
"source": [
"import scipp as sc\n",
"import plopp as pp\n",
"import sciline as sl\n",
"import sciline\n",
"import esssans as sans\n",
"from esssans.isis import plot_flat_detector_xy\n",
"from esssans import isis\n",
"from esssans.types import *"
]
},
Expand All @@ -51,12 +51,12 @@
"metadata": {},
"outputs": [],
"source": [
"params = sans.sans2d.default_parameters.copy()\n",
"\n",
"params[FileList[SampleRun]] = ['SANS2D00063114.hdf5']\n",
"params = {isis.Filename[SampleRun]: 'SANS2D00063114.nxs'}\n",
"\n",
"# Build the pipeline\n",
"providers = list(sans.providers + sans.sans2d.providers)\n",
"providers = list(\n",
" sans.providers + isis.providers + isis.data.providers + isis.sans2d.providers\n",
")\n",
"pipeline = sciline.Pipeline(providers, params=params)"
]
},
Expand All @@ -79,9 +79,9 @@
"metadata": {},
"outputs": [],
"source": [
"raw = pipeline.compute(RawData[SampleRun])\n",
"raw = pipeline.compute(RawData[SampleRun])['spectrum', :61440]\n",
"\n",
"p = plot_flat_detector_xy(raw.sum('tof'), norm='log')\n",
"p = isis.plot_flat_detector_xy(raw.hist(), norm='log')\n",
"p.ax.plot(0, 0, '+', color='k', ms=10)\n",
"p"
]
Expand Down Expand Up @@ -131,10 +131,10 @@
"metadata": {},
"outputs": [],
"source": [
"masked = pipeline.compute(MaskedData[SampleRun])\n",
"masked.masks['low_counts'] = masked.sum('tof').data < sc.scalar(80.0, unit='counts')\n",
"masked = pipeline.compute(MaskedData[SampleRun])['spectrum', :61440].copy()\n",
"masked.masks['low_counts'] = masked.hist().data < sc.scalar(80.0, unit='counts')\n",
"\n",
"p = plot_flat_detector_xy(masked.sum('tof'), norm='log')\n",
"p = isis.plot_flat_detector_xy(masked.hist(), norm='log')\n",
"p.ax.plot(0, 0, '+', color='k', ms=10)\n",
"p"
]
Expand Down Expand Up @@ -246,8 +246,13 @@
"params[WavelengthBins] = sc.linspace(\n",
" 'wavelength', start=2.0, stop=16.0, num=141, unit='angstrom'\n",
")\n",
"params[FileList[TransmissionRun[SampleRun]]] = params[FileList[SampleRun]]\n",
"params[FileList[EmptyBeamRun]] = ['SANS2D00063091.hdf5']\n",
"params[isis.Filename[EmptyBeamRun]] = 'SANS2D00063091.nxs'\n",
"\n",
"params[NeXusMonitorName[Incident]] = 'monitor2'\n",
"params[NeXusMonitorName[Transmission]] = 'monitor4'\n",
"\n",
"params[sans.isis.SampleOffset] = sc.vector([0.0, 0.0, 0.053], unit='m')\n",
"params[sans.isis.MonitorOffset[Transmission]] = sc.vector([0.0, 0.0, -6.719], unit='m')\n",
"\n",
"params[CorrectForGravity] = True\n",
"params[UncertaintyBroadcastMode] = UncertaintyBroadcastMode.upper_bound\n",
Expand All @@ -259,6 +264,8 @@
"providers.remove(sans.beam_center_finder.beam_center_from_center_of_mass)\n",
"providers.append(sans.beam_center_finder.beam_center_from_iofq)\n",
"\n",
"providers.append(sans.transmission_from_sample_run)\n",
"\n",
"# Build the pipeline\n",
"pipeline = sciline.Pipeline(providers, params=params)\n",
"\n",
Expand Down Expand Up @@ -306,9 +313,16 @@
"metadata": {},
"outputs": [],
"source": [
"p = plot_flat_detector_xy(masked.sum('tof'), norm='log')\n",
"p = isis.plot_flat_detector_xy(masked.hist(), norm='log')\n",
"p.ax.plot(0, 0, '+', color='k', ms=10)\n",
"p.ax.plot(iofq_center.value[0], iofq_center.value[1], 'o', color='magenta', mec='lightgray', ms=6)\n",
"p.ax.plot(\n",
" iofq_center.value[0],\n",
" iofq_center.value[1],\n",
" 'o',\n",
" color='magenta',\n",
" mec='lightgray',\n",
" ms=6,\n",
")\n",
"p"
]
},
Expand Down Expand Up @@ -357,7 +371,7 @@
},
"outputs": [],
"source": [
"p = plot_flat_detector_xy(masked.sum('tof'), norm='log')\n",
"p = isis.plot_flat_detector_xy(masked.hist(), norm='log')\n",
"p.ax.axvline(0, color='cyan')\n",
"p.ax.axhline(0, color='cyan')\n",
"p.ax.plot(0, 0, '+', color='k', ms=10)\n",
Expand Down
92 changes: 50 additions & 42 deletions docs/examples/sans2d.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -22,84 +22,92 @@
},
"outputs": [],
"source": [
"import numpy as np\n",
"import scipp as sc\n",
"import sciline\n",
"import scippneutron as scn\n",
"import plopp as pp\n",
"import esssans as sans\n",
"import esssans.isis as isis\n",
"from esssans.types import *"
]
},
{
"cell_type": "markdown",
"id": "c21564a8-e742-4183-9edc-2c70c51d5863",
"id": "3da2d397-6206-4ed1-a98f-11b3aaf7e5b0",
"metadata": {},
"source": [
"## Create pipeline using Sciline\n",
"## Define reduction parameters\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:"
"We define the reduction parameters, with keys and types given by aliases or types defined in `esssans.types`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "025fca0b-68a7-4d6e-a82a-86713cc3fca7",
"id": "d3f5e1ee-bd80-4301-9ff9-15c8ada3cf57",
"metadata": {},
"outputs": [],
"source": [
"pipeline = sciline.Pipeline(\n",
" sans.providers + sans.sans2d.providers,\n",
" params=sans.sans2d.default_parameters,\n",
")"
"params = {}\n",
"\n",
"params[DirectBeamFilename] = 'DIRECT_SANS2D_REAR_34327_4m_8mm_16Feb16.dat'\n",
"params[sans.isis.Filename[SampleRun]] = 'SANS2D00063114.nxs'\n",
"params[sans.isis.Filename[BackgroundRun]] = 'SANS2D00063159.nxs'\n",
"params[sans.isis.Filename[EmptyBeamRun]] = 'SANS2D00063091.nxs'\n",
"params[OutFilename] = 'reduced.nxs'\n",
"\n",
"params[NeXusMonitorName[Incident]] = 'monitor2'\n",
"params[NeXusMonitorName[Transmission]] = 'monitor4'\n",
"\n",
"params[sans.isis.SampleOffset] = sc.vector([0.0, 0.0, 0.053], unit='m')\n",
"params[sans.isis.MonitorOffset[Transmission]] = sc.vector([0.0, 0.0, -6.719], unit='m')\n",
"\n",
"params[WavelengthBins] = sc.linspace(\n",
" 'wavelength', start=2.0, stop=16.0, num=141, unit='angstrom'\n",
")\n",
"\n",
"params[sans.isis.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",
" 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",
" dims=['wavelength'], values=[0.7, 17.1], unit='angstrom'\n",
")\n",
"params[CorrectForGravity] = True\n",
"params[UncertaintyBroadcastMode] = UncertaintyBroadcastMode.upper_bound\n",
"params[sans.ReturnEvents] = True"
]
},
{
"cell_type": "markdown",
"id": "3da2d397-6206-4ed1-a98f-11b3aaf7e5b0",
"id": "c21564a8-e742-4183-9edc-2c70c51d5863",
"metadata": {},
"source": [
"## Define reduction parameters\n",
"## Create pipeline using Sciline\n",
"\n",
"We define the reduction parameters, with keys and types given by aliases or types defined in `esssans.types`:"
"We use all providers available in `esssans` as well as the `isis` and `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": "bc2fffe1-a694-43b7-9234-e31da42d6df3",
"metadata": {
"tags": []
},
"id": "85b955ba-a54d-4760-bb35-af9cbe1ada90",
"metadata": {},
"outputs": [],
"source": [
"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",
"pipeline[WavelengthBins] = sc.linspace(\n",
" 'wavelength', start=2.0, stop=16.0, num=141, unit='angstrom'\n",
"providers = (\n",
" sans.providers + isis.providers + isis.data.providers + isis.sans2d.providers\n",
")\n",
"\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",
"pipeline[WavelengthMask] = sc.DataArray(\n",
" sc.array(dims=['wavelength'], values=[True]),\n",
" coords={'wavelength': mask_interval},\n",
"providers = providers + (\n",
" sans.transmission_from_background_run,\n",
" sans.transmission_from_sample_run,\n",
")\n",
"\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",
"pipeline[CorrectForGravity] = True\n",
"pipeline[UncertaintyBroadcastMode] = UncertaintyBroadcastMode.upper_bound\n",
"pipeline[sans.ReturnEvents] = True"
"pipeline = sciline.Pipeline(providers=providers, params=params)"
]
},
{
Expand Down Expand Up @@ -263,7 +271,7 @@
"\n",
"display(sc.plot({str(key): results[key] for key in monitors}, norm='log'))\n",
"\n",
"display(plot_flat_detector_xy(results[MaskedData[SampleRun]].sum('tof'), norm='log'))\n",
"display(plot_flat_detector_xy(results[MaskedData[SampleRun]].hist(), norm='log'))\n",
"\n",
"wavelength = pipeline.compute(WavelengthBins)\n",
"display(\n",
Expand Down
43 changes: 32 additions & 11 deletions docs/examples/zoom.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,7 @@
")\n",
"\n",
"params[QBins] = sc.geomspace(dim='Q', start=0.004, stop=0.8, num=141, unit='1/angstrom')\n",
"qxy = {\n",
" 'Qx': sc.linspace(dim='Qx', start=-0.5, stop=0.5, num=101, unit='1/angstrom'),\n",
" 'Qy': sc.linspace(dim='Qy', start=-0.8, stop=0.8, num=101, unit='1/angstrom'),\n",
"}\n",
"# Uncomment to compute I(Qx, Qy) instead of I(Q)\n",
"# params[QxyBins] = qxy\n",
"\n",
"params[NonBackgroundWavelengthRange] = sc.array(\n",
" dims=['wavelength'], values=[0.7, 17.1], unit='angstrom'\n",
")\n",
Expand All @@ -136,7 +131,7 @@
"metadata": {},
"outputs": [],
"source": [
"providers = sans.providers + sans.isis.providers\n",
"providers = sans.providers + sans.isis.providers + sans.isis.masking.providers\n",
"providers = providers + (\n",
" sans.transmission_from_background_run,\n",
" sans.transmission_from_sample_run,\n",
Expand Down Expand Up @@ -174,7 +169,7 @@
" for provider in sans.isis.mantidio.providers:\n",
" pipeline.insert(provider)\n",
"except ImportError:\n",
" import esssans.isis.data\n",
" import esssans.isis.io\n",
"\n",
" for provider in sans.isis.data.providers:\n",
" pipeline.insert(provider)"
Expand Down Expand Up @@ -217,7 +212,7 @@
"outputs": [],
"source": [
"da = iofq.compute()\n",
"da.plot(norm='log', scale={'Q': 'log'}, aspect='equal')"
"da.plot(norm='log', scale={'Q': 'log'})"
]
},
{
Expand Down Expand Up @@ -272,6 +267,33 @@
"iofqs = {key: val if val.bins is None else val.hist() for key, val in iofqs.items()}\n",
"display(sc.plot(iofqs, norm='log', scale={'Q': 'log'}, aspect='equal'))"
]
},
{
"cell_type": "markdown",
"id": "e63318c7-4d63-4133-97c3-feb56707caba",
"metadata": {},
"source": [
"## Computing Qx/Qy\n",
"\n",
"To compute $I(Q_{x}, Q_{y})$ instead of the one-dimensional $I(Q)$,\n",
"we can simply define some `QxyBins` in our parameters:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3879702d-424a-462f-8475-6056116d7d35",
"metadata": {},
"outputs": [],
"source": [
"pipeline[QxyBins] = {\n",
" 'Qx': sc.linspace(dim='Qx', start=-0.5, stop=0.5, num=101, unit='1/angstrom'),\n",
" 'Qy': sc.linspace(dim='Qy', start=-0.8, stop=0.8, num=101, unit='1/angstrom'),\n",
"}\n",
"\n",
"iqxqy = pipeline.compute(IofQ[SampleRun])\n",
"iqxqy.plot(norm='log', aspect='equal')"
]
}
],
"metadata": {
Expand All @@ -289,8 +311,7 @@
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"pygments_lexer": "ipython3"
}
},
"nbformat": 4,
Expand Down
12 changes: 1 addition & 11 deletions src/esssans/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,7 @@
except importlib.metadata.PackageNotFoundError:
__version__ = "0.0.0"

from . import (
beam_center_finder,
common,
conversions,
i_of_q,
io,
loki,
normalization,
sans2d,
)
from . import beam_center_finder, common, conversions, i_of_q, io, loki, normalization
from .common import transmission_from_background_run, transmission_from_sample_run
from .direct_beam import direct_beam
from .types import BackgroundSubtractedIofQ, IofQ, ReturnEvents, SampleRun
Expand Down Expand Up @@ -55,7 +46,6 @@
'loki',
'normalization',
'providers',
'sans2d',
'transmission_from_sample_run',
'transmission_from_background_run',
]
17 changes: 11 additions & 6 deletions src/esssans/beam_center_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,18 @@ def _iofq_in_quadrants(
pipeline = sciline.Pipeline(providers, params=params)
pipeline[MaskedData[SampleRun]] = data
calibrated = pipeline.compute(CalibratedMaskedData[SampleRun])
phi = calibrated.transform_coords(
with_phi = calibrated.transform_coords(
'phi', graph=graph, keep_intermediate=False, keep_inputs=False
).coords['phi']
if phi.bins is not None or 'wavelength' in phi.dims:
# If gravity-correction is enabled, phi depends on wavelength (and event).
# We cannot handle this below, so we approximate phi by the mean value.
phi = phi.mean('wavelength')
)
# If gravity-correction is enabled, phi depends on wavelength (and event).
# We cannot handle this below, so we approximate phi by the mean value.
if ('phi' not in with_phi.coords) and ('phi' in with_phi.bins.coords):
# This is the case where we have a phi event coord but no coord at the top level
phi = with_phi.bins.coords['phi'].bins.mean()
else:
phi = with_phi.coords['phi']
if phi.bins is not None or 'wavelength' in phi.dims:
phi = phi.mean('wavelength')

out = {}
for i, quad in enumerate(quadrants):
Expand Down
Loading
Loading