Skip to content

Commit

Permalink
Fix and rename
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonHeybrock committed Jul 24, 2024
1 parent 128cc7a commit eb0cd45
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 65 deletions.
16 changes: 8 additions & 8 deletions src/ess/isissans/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import scipp as sc

from ..sans.types import (
ConfiguredReducibleData,
ConfiguredReducibleMonitor,
MonitorType,
RawData,
RawDetector,
RawDetectorData,
RawMonitor,
RawMonitorData,
RunType,
ScatteringRunType,
)
Expand All @@ -25,10 +25,10 @@ class MonitorOffset(sciline.Scope[MonitorType, sc.Variable], sc.Variable):


def apply_component_user_offsets_to_raw_data(
data: RawData[ScatteringRunType],
data: RawDetector[ScatteringRunType],
sample_offset: SampleOffset,
detector_bank_offset: DetectorBankOffset,
) -> ConfiguredReducibleData[ScatteringRunType]:
) -> RawDetectorData[ScatteringRunType]:
"""Apply user offsets to raw data.
Parameters
Expand All @@ -47,13 +47,13 @@ def apply_component_user_offsets_to_raw_data(
)
pos = data.coords['position']
data.coords['position'] = pos + detector_bank_offset.to(unit=pos.unit, copy=False)
return ConfiguredReducibleData[ScatteringRunType](data)
return RawDetectorData[ScatteringRunType](data)


def apply_component_user_offsets_to_raw_monitor(
monitor_data: RawMonitor[RunType, MonitorType],
monitor_offset: MonitorOffset[MonitorType],
) -> ConfiguredReducibleMonitor[RunType, MonitorType]:
) -> RawMonitorData[RunType, MonitorType]:
"""Apply user offsets to raw monitor.
Parameters
----------
Expand All @@ -65,7 +65,7 @@ def apply_component_user_offsets_to_raw_monitor(
monitor_data = monitor_data.copy(deep=False)
pos = monitor_data.coords['position']
monitor_data.coords['position'] = pos + monitor_offset.to(unit=pos.unit, copy=False)
return ConfiguredReducibleMonitor[RunType, MonitorType](monitor_data)
return RawMonitorData[RunType, MonitorType](monitor_data)


providers = (
Expand Down
14 changes: 7 additions & 7 deletions src/ess/isissans/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import scipp as sc

from ..sans.types import (
ConfiguredReducibleData,
ConfiguredReducibleMonitor,
CorrectForGravity,
DetectorIDs,
DetectorPixelShape,
Expand All @@ -18,8 +16,10 @@
MonitorType,
NeXusMonitorName,
NonBackgroundWavelengthRange,
RawData,
RawDetector,
RawDetectorData,
RawMonitor,
RawMonitorData,
RunNumber,
RunTitle,
RunType,
Expand Down Expand Up @@ -53,8 +53,8 @@ def default_parameters() -> dict:

def get_detector_data(
dg: LoadedFileContents[RunType],
) -> RawData[RunType]:
return RawData[RunType](dg['data'])
) -> RawDetector[RunType]:
return RawDetector[RunType](dg['data'])


def get_monitor_data(
Expand All @@ -66,15 +66,15 @@ def get_monitor_data(


def data_to_tof(
da: ConfiguredReducibleData[ScatteringRunType],
da: RawDetectorData[ScatteringRunType],
) -> TofData[ScatteringRunType]:
"""Dummy conversion of data to time-of-flight data.
The data already has a time-of-flight coordinate."""
return TofData[ScatteringRunType](da)


def monitor_to_tof(
da: ConfiguredReducibleMonitor[RunType, MonitorType],
da: RawMonitorData[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
55 changes: 28 additions & 27 deletions src/ess/loki/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@

from ..sans.common import gravity_vector
from ..sans.types import (
ConfiguredReducibleData,
ConfiguredReducibleMonitor,
CorrectForGravity,
DetectorEventData,
DetectorPixelShape,
DimsToKeep,
Incident,
LabFrameTransform,
LoadedNeXusDetector,
LoadedNeXusMonitor,
MonitorEventData,
MonitorType,
NeXusDetector,
NeXusMonitor,
NeXusMonitorName,
NonBackgroundWavelengthRange,
PixelShapePath,
RawData,
RawDetector,
RawDetectorData,
RawMonitor,
RawMonitorData,
RawSample,
RawSource,
RunType,
Expand Down Expand Up @@ -105,20 +105,20 @@ def get_sample_position(


def get_detector_data(
detector: LoadedNeXusDetector[ScatteringRunType],
) -> RawData[ScatteringRunType]:
detector: NeXusDetector[ScatteringRunType],
) -> RawDetector[ScatteringRunType]:
da = nexus.extract_detector_data(detector)
if (reshape := DETECTOR_BANK_RESHAPING.get(detector['detector_name'])) is not None:
da = reshape(da)
return RawData[ScatteringRunType](da)
return RawDetector[ScatteringRunType](da)


def get_monitor_data(
monitor: LoadedNeXusMonitor[RunType, MonitorType],
monitor: NeXusMonitor[RunType, MonitorType],
) -> RawMonitor[RunType, MonitorType]:
out = nexus.extract_monitor_data(monitor).copy(deep=False)
out.coords['position'] = monitor['position']
return RawMonitor[RunType, MonitorType](out)
return RawMonitor[RunType, MonitorType](
nexus.extract_monitor_data(monitor).assign_coords(position=monitor['position'])
)


def _add_variances_and_coordinates(
Expand All @@ -139,17 +139,17 @@ def _add_variances_and_coordinates(
return out


def patch_detector_data(
detector_data: RawData[ScatteringRunType],
def assemble_detector_data(
detector_data: RawDetector[ScatteringRunType],
event_data: DetectorEventData[ScatteringRunType],
source_position: SourcePosition[ScatteringRunType],
sample_position: SamplePosition[ScatteringRunType],
) -> ConfiguredReducibleData[ScatteringRunType]:
) -> RawDetectorData[ScatteringRunType]:
grouped = nexus.group_event_data(
event_data=event_data, detector_number=detector_data.coords['detector_number']
)
detector_data.data = grouped.data
return ConfiguredReducibleData[ScatteringRunType](
return RawDetectorData[ScatteringRunType](
_add_variances_and_coordinates(
da=detector_data,
source_position=source_position,
Expand All @@ -158,14 +158,15 @@ def patch_detector_data(
)


def patch_monitor_data(
def assemble_monitor_data(
monitor_data: RawMonitor[RunType, MonitorType],
event_data: MonitorEventData[RunType, MonitorType],
source_position: SourcePosition[RunType],
) -> ConfiguredReducibleMonitor[RunType, MonitorType]:
monitor_data.data = event_data.data
return ConfiguredReducibleMonitor[RunType, MonitorType](
_add_variances_and_coordinates(da=monitor_data, source_position=source_position)
) -> RawMonitorData[RunType, MonitorType]:
meta = monitor_data.drop_coords('event_time_zero')
da = event_data.assign_coords(meta.coords).assign_masks(meta.masks)
return RawMonitorData[RunType, MonitorType](
_add_variances_and_coordinates(da=da, source_position=source_position)
)


Expand All @@ -177,26 +178,26 @@ def _convert_to_tof(da: sc.DataArray) -> sc.DataArray:


def data_to_tof(
da: ConfiguredReducibleData[ScatteringRunType],
da: RawDetectorData[ScatteringRunType],
) -> TofData[ScatteringRunType]:
return TofData[ScatteringRunType](_convert_to_tof(da))


def monitor_to_tof(
da: ConfiguredReducibleMonitor[RunType, MonitorType],
da: RawMonitorData[RunType, MonitorType],
) -> TofMonitor[RunType, MonitorType]:
return TofMonitor[RunType, MonitorType](_convert_to_tof(da))


def detector_pixel_shape(
detector: LoadedNeXusDetector[ScatteringRunType],
detector: NeXusDetector[ScatteringRunType],
pixel_shape_path: PixelShapePath,
) -> DetectorPixelShape[ScatteringRunType]:
return DetectorPixelShape[ScatteringRunType](detector[pixel_shape_path])


def detector_lab_frame_transform(
detector: LoadedNeXusDetector[ScatteringRunType],
detector: NeXusDetector[ScatteringRunType],
transform_path: TransformationPath,
) -> LabFrameTransform[ScatteringRunType]:
return LabFrameTransform[ScatteringRunType](detector[transform_path])
Expand All @@ -209,8 +210,8 @@ def detector_lab_frame_transform(
get_monitor_data,
get_sample_position,
get_source_position,
patch_detector_data,
patch_monitor_data,
assemble_detector_data,
assemble_monitor_data,
data_to_tof,
monitor_to_tof,
)
12 changes: 6 additions & 6 deletions src/ess/loki/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
from ess.sans.types import (
DetectorEventData,
Filename,
LoadedNeXusDetector,
LoadedNeXusMonitor,
MonitorEventData,
MonitorType,
NeXusDetector,
NeXusDetectorName,
NeXusMonitor,
NeXusMonitorName,
RawSample,
RawSource,
Expand All @@ -37,7 +37,7 @@ def load_nexus_source(file_path: Filename[RunType]) -> RawSource[RunType]:

def load_nexus_detector(
file_path: Filename[RunType], detector_name: NeXusDetectorName
) -> LoadedNeXusDetector[RunType]:
) -> NeXusDetector[RunType]:
# Events will be loaded later. Should we set something else as data instead, or
# use different NeXus definitions to completely bypass the (empty) event load?
dg = nexus.load_detector(
Expand All @@ -47,14 +47,14 @@ def load_nexus_detector(
)
# The name is required later, e.g., for determining logical detector shape
dg['detector_name'] = detector_name
return LoadedNeXusDetector[RunType](dg)
return NeXusDetector[RunType](dg)


def load_nexus_monitor(
file_path: Filename[RunType],
monitor_name: NeXusMonitorName[MonitorType],
) -> LoadedNeXusMonitor[RunType, MonitorType]:
return LoadedNeXusMonitor[RunType, MonitorType](
) -> NeXusMonitor[RunType, MonitorType]:
return NeXusMonitor[RunType, MonitorType](
nexus.load_monitor(
file_path=file_path,
monitor_name=monitor_name,
Expand Down
4 changes: 2 additions & 2 deletions src/ess/loki/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import scipp as sc
import scippnexus as snx
from ess.loki.general import (
assemble_monitor_data,
get_monitor_data,
get_source_position,
monitor_to_tof,
patch_monitor_data,
)
from ess.loki.io import load_nexus_monitor, load_nexus_source
from ess.sans.conversions import monitor_to_wavelength, sans_monitor
Expand Down Expand Up @@ -82,7 +82,7 @@ def _build_pipeline(self) -> sciline.Pipeline:
load_nexus_source,
get_source_position,
get_monitor_data,
patch_monitor_data,
assemble_monitor_data,
monitor_to_tof,
sans_monitor,
monitor_to_wavelength,
Expand Down
4 changes: 2 additions & 2 deletions src/ess/sans/masking.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
MaskedData,
MaskedDetectorIDs,
PixelMaskFilename,
RawData,
RawDetector,
SampleRun,
ScatteringRunType,
TofData,
)


def get_detector_ids_from_detector(data: RawData[SampleRun]) -> DetectorIDs:
def get_detector_ids_from_detector(data: RawDetector[SampleRun]) -> DetectorIDs:
"""Extract detector IDs from a detector."""
return DetectorIDs(
data.coords[
Expand Down
14 changes: 6 additions & 8 deletions src/ess/sans/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,12 @@ class SolidAngle(sciline.Scope[ScatteringRunType, sc.DataArray], sc.DataArray):
"""Solid angle of detector pixels seen from sample position"""


class LoadedNeXusDetector(sciline.Scope[RunType, sc.DataGroup], sc.DataGroup):
class NeXusDetector(sciline.Scope[RunType, sc.DataGroup], sc.DataGroup):
"""Detector data, loaded from a NeXus file, containing not only neutron events
but also pixel shape information, transformations, ..."""


class LoadedNeXusMonitor(
class NeXusMonitor(
sciline.ScopeTwoParams[RunType, MonitorType, sc.DataGroup], sc.DataGroup
):
"""Monitor data loaded from a NeXus file, containing not only neutron events
Expand All @@ -239,13 +239,11 @@ class MonitorEventData(
"""Event data loaded from a monitor in a NeXus file"""


class RawData(sciline.Scope[ScatteringRunType, sc.DataArray], sc.DataArray):
"""Raw detector data"""
class RawDetector(sciline.Scope[ScatteringRunType, sc.DataArray], sc.DataArray):
"""Raw detector data component extracted from :py:class:`NeXusDetector`"""


class ConfiguredReducibleData(
sciline.Scope[ScatteringRunType, sc.DataArray], sc.DataArray
):
class RawDetectorData(sciline.Scope[ScatteringRunType, sc.DataArray], sc.DataArray):
"""Raw event data where variances and necessary coordinates
(e.g. sample and source position) have been added, and where optionally some
user configuration was applied to some of the coordinates."""
Expand Down Expand Up @@ -342,7 +340,7 @@ class RawMonitor(
"""Raw monitor data"""


class ConfiguredReducibleMonitor(
class RawMonitorData(
sciline.ScopeTwoParams[RunType, MonitorType, sc.DataArray], sc.DataArray
):
"""Raw monitor data where variances and necessary coordinates
Expand Down
Loading

0 comments on commit eb0cd45

Please sign in to comment.