Skip to content
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
13 changes: 7 additions & 6 deletions docs/user-guide/common/beam-center-finder.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@
"metadata": {},
"outputs": [],
"source": [
"raw = workflow.compute(RawDetector[SampleRun])['spectrum', :61440]\n",
"workflow[BeamCenter] = sc.vector([0, 0, 0], unit='m')\n",
"raw = workflow.compute(DetectorData[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 @@ -305,10 +306,10 @@
"outputs": [],
"source": [
"workflow[Filename[SampleRun]] = isis.data.sans2d_tutorial_sample_run()\n",
"detector = workflow.compute(RawDetector[SampleRun])['spectrum', :61440].assign_masks(\n",
" masked.masks\n",
")\n",
"workflow[RawDetector[SampleRun]] = detector"
"workflow[BeamCenter] = sc.vector([0, 0, 0], unit='m')\n",
"detector = workflow.compute(NeXusDetector[SampleRun]).copy()\n",
"detector['data'] = detector['data']['spectrum', :61440].assign_masks(masked.masks)\n",
"workflow[NeXusDetector[SampleRun]] = detector"
]
},
{
Expand Down Expand Up @@ -449,7 +450,7 @@
"workflow[WavelengthBands] = None\n",
"kwargs = dict( # noqa: C408\n",
" workflow=workflow,\n",
" detector=detector,\n",
" detector=detector['data'],\n",
" norm=workflow.compute(NormWavelengthTerm[SampleRun]),\n",
")"
]
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ requires-python = ">=3.10"
dependencies = [
"dask",
"graphviz",
"essreduce>=24.08.0",
"essreduce>=24.08.1",
"numpy",
"pandas",
"plopp",
Expand Down
2 changes: 1 addition & 1 deletion requirements/base.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# The following was generated by 'tox -e deps', DO NOT EDIT MANUALLY!
dask
graphviz
essreduce>=24.08.0
essreduce>=24.08.1
numpy
pandas
plopp
Expand Down
8 changes: 5 additions & 3 deletions requirements/base.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SHA1:df4cf8a4a39fd2d213b55c8c11559ef6e4fe6fe1
# SHA1:4cb11db62c9d3136130eb212e7bcf639441c13e6
#
# This file is autogenerated by pip-compile-multi
# To update, run:
Expand All @@ -23,7 +23,7 @@ dask==2024.8.1
# via -r base.in
decorator==5.1.1
# via ipython
essreduce==24.8.0
essreduce==24.8.1
# via -r base.in
exceptiongroup==1.2.2
# via ipython
Expand Down Expand Up @@ -120,7 +120,9 @@ pytz==2024.1
pyyaml==6.0.2
# via dask
sciline==24.6.2
# via -r base.in
# via
# -r base.in
# essreduce
scipp==24.8.0
# via
# -r base.in
Expand Down
11 changes: 5 additions & 6 deletions src/ess/isissans/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@

import importlib.metadata

from . import components, general, io, sans2d, zoom
from .components import DetectorBankOffset, MonitorOffset, SampleOffset
from .general import default_parameters
from . import general, io, sans2d, zoom
from .general import default_parameters, SampleOffset, MonitorOffset, DetectorBankOffset
from .io import CalibrationFilename
from .visualization import plot_flat_detector_xy

Expand All @@ -14,17 +13,17 @@
except importlib.metadata.PackageNotFoundError:
__version__ = "0.0.0"

providers = components.providers + general.providers + io.providers
providers = general.providers

del importlib

__all__ = [
'CalibrationFilename',
'DetectorBankOffset',
'io',
'MonitorOffset',
'providers',
'SampleOffset',
'io',
'providers',
'plot_flat_detector_xy',
'sans2d',
'default_parameters',
Expand Down
68 changes: 0 additions & 68 deletions src/ess/isissans/components.py

This file was deleted.

4 changes: 3 additions & 1 deletion src/ess/isissans/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ def zoom_tutorial_calibration() -> Filename[CalibrationFilename]:


def zoom_tutorial_sample_run() -> Filename[SampleRun]:
return _zoom_registry.get_path('ZOOM00034786.nxs.h5.zip', unzip=True)[0]
return Filename[SampleRun](
_zoom_registry.get_path('ZOOM00034786.nxs.h5.zip', unzip=True)[0]
)


def zoom_tutorial_empty_beam_run() -> Filename[EmptyBeamRun]:
Expand Down
117 changes: 85 additions & 32 deletions src/ess/isissans/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,68 @@
Providers for the ISIS instruments.
"""

from typing import NewType

import sciline
import scipp as sc

from ..sans.types import (
from ess.sans.types import (
BeamCenter,
CalibratedDetector,
CalibratedMonitor,
CorrectForGravity,
DetectorData,
DetectorIDs,
DetectorPixelShape,
DetectorPositionOffset,
DimsToKeep,
Incident,
LabFrameTransform,
MonitorData,
MonitorPositionOffset,
MonitorType,
NeXusDetector,
NeXusMonitor,
NeXusMonitorName,
NonBackgroundWavelengthRange,
RawDetector,
RawMonitor,
RawMonitorData,
RunNumber,
RunTitle,
RunType,
SamplePosition,
SampleRun,
ScatteringRunType,
SourcePosition,
TofData,
TofMonitor,
Transmission,
WavelengthBands,
WavelengthMask,
)
from .components import DetectorBankOffset, MonitorOffset, SampleOffset

from .io import LoadedFileContents
from .mantidio import Period


class MonitorOffset(sciline.Scope[MonitorType, sc.Variable], sc.Variable):
"""
Offset for monitor position for all runs.
"""


DetectorBankOffset = NewType('DetectorBankOffset', sc.Variable)
SampleOffset = NewType('SampleOffset', sc.Variable)


def default_parameters() -> dict:
return {
CorrectForGravity: False,
DimsToKeep: (),
MonitorOffset[Incident]: MonitorOffset(sc.vector([0, 0, 0], unit='m')),
MonitorOffset[Transmission]: MonitorOffset(sc.vector([0, 0, 0], unit='m')),
MonitorOffset[Incident]: MonitorOffset[Incident](
sc.vector([0, 0, 0], unit='m')
),
MonitorOffset[Transmission]: MonitorOffset[Transmission](
sc.vector([0, 0, 0], unit='m')
),
DetectorBankOffset: DetectorBankOffset(sc.vector([0, 0, 0], unit='m')),
SampleOffset: SampleOffset(sc.vector([0, 0, 0], unit='m')),
NonBackgroundWavelengthRange: None,
Expand All @@ -52,45 +75,70 @@ def default_parameters() -> dict:
}


def get_detector_data(
dg: LoadedFileContents[RunType],
sample_offset: SampleOffset,
detector_bank_offset: DetectorBankOffset,
) -> RawDetector[RunType]:
def to_detector_position_offset(
global_offset: DetectorBankOffset, beam_center: BeamCenter
) -> DetectorPositionOffset[RunType]:
return DetectorPositionOffset[RunType](global_offset - beam_center)


def to_monitor_position_offset(
global_offset: MonitorOffset[MonitorType],
) -> MonitorPositionOffset[RunType, MonitorType]:
return MonitorPositionOffset[RunType, MonitorType](global_offset)
Comment on lines +78 to +87
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two helpers replace logic in the removed components.py, which was mostly duplicating more generic code.



def get_source_position(dg: LoadedFileContents[RunType]) -> SourcePosition[RunType]:
"""Get source position from raw data."""
return SourcePosition[RunType](dg['data'].coords['source_position'])


def get_sample_position(
dg: LoadedFileContents[RunType], offset: SampleOffset
) -> SamplePosition[RunType]:
"""Get sample position from raw data and apply user offset."""
return SamplePosition[RunType](
dg['data'].coords['sample_position'] + offset.to(unit='m')
)


def get_detector_data(dg: LoadedFileContents[RunType]) -> NeXusDetector[RunType]:
"""Get detector data and apply user offsets to raw data.

Parameters
----------
dg:
Data loaded with Mantid and converted to Scipp.
sample_offset:
Sample offset.
detector_bank_offset:
Detector bank offset.
"""
data = dg['data']
sample_pos = data.coords['sample_position']
sample_pos = sample_pos + sample_offset.to(unit=sample_pos.unit, copy=False)
pos = data.coords['position']
pos = pos + detector_bank_offset.to(unit=pos.unit, copy=False)
return RawDetector[RunType](
dg['data'].assign_coords(position=pos, sample_position=sample_pos)
# The generic NeXus workflow will try to extract 'data' from this, which is exactly
# what we also have in the Mantid data. We use the generic workflow since it also
# applies offsets, etc.
return NeXusDetector[RunType](dg)


def get_monitor_data(
dg: LoadedFileContents[RunType], nexus_name: NeXusMonitorName[MonitorType]
) -> NeXusMonitor[RunType, MonitorType]:
# The generic NeXus workflow will try to extract 'data' from this, which is exactly
# what we also have in the Mantid data. We use the generic workflow since it also
# applies offsets, etc.
monitor = dg['monitors'][nexus_name]['data']
return NeXusMonitor[RunType, MonitorType](
sc.DataGroup(data=monitor, position=monitor.coords['position'])
)


def assemble_detector_data(
def dummy_assemble_detector_data(
detector: CalibratedDetector[RunType],
) -> DetectorData[RunType]:
"""Dummy assembly of detector data, detector already contains neutron data."""
return DetectorData[RunType](detector)


def get_monitor_data(
dg: LoadedFileContents[RunType], nexus_name: NeXusMonitorName[MonitorType]
) -> RawMonitor[RunType, MonitorType]:
# See https://github.com/scipp/sciline/issues/52 why copy needed
mon = dg['monitors'][nexus_name]['data'].copy()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the need for this copy now gone? The issue is still open...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we are not actually accessing a value property here?

return RawMonitor[RunType, MonitorType](mon)
def dummy_assemble_monitor_data(
monitor: CalibratedMonitor[RunType, MonitorType],
) -> MonitorData[RunType, MonitorType]:
"""Dummy assembly of monitor data, monitor already contains neutron data."""
return MonitorData[RunType, MonitorType](monitor)


def data_to_tof(
Expand All @@ -102,7 +150,7 @@ def data_to_tof(


def monitor_to_tof(
da: RawMonitorData[RunType, MonitorType],
da: MonitorData[RunType, MonitorType],
) -> TofMonitor[RunType, MonitorType]:
"""Dummy conversion of monitor data to time-of-flight data.
The monitor data already has a time-of-flight coordinate."""
Expand Down Expand Up @@ -166,7 +214,12 @@ def get_detector_ids_from_sample_run(data: TofData[SampleRun]) -> DetectorIDs:


providers = (
assemble_detector_data,
dummy_assemble_detector_data,
dummy_assemble_monitor_data,
to_detector_position_offset,
to_monitor_position_offset,
get_source_position,
get_sample_position,
get_detector_data,
get_detector_ids_from_sample_run,
get_monitor_data,
Expand Down
Loading