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
16 changes: 8 additions & 8 deletions src/ess/dream/io/geant4.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
CalibrationData,
CalibrationFilename,
Filename,
NeXusDetector,
NeXusDetectorDimensions,
NeXusDetectorName,
RawDetector,
RawDetectorData,
RawSample,
RawSource,
ReducibleDetectorData,
Expand Down Expand Up @@ -64,16 +64,16 @@ def load_geant4_csv(file_path: Filename[RunType]) -> AllRawDetectors[RunType]:

def extract_geant4_detector(
detectors: AllRawDetectors[RunType], detector_name: NeXusDetectorName
) -> RawDetector[RunType]:
) -> NeXusDetector[RunType]:
"""Extract a single detector from a loaded GEANT4 simulation."""
return RawDetector[RunType](detectors["instrument"][detector_name])
return NeXusDetector[RunType](detectors["instrument"][detector_name])


def extract_geant4_detector_data(
detector: RawDetector[RunType],
) -> RawDetectorData[RunType]:
detector: NeXusDetector[RunType],
) -> RawDetector[RunType]:
"""Extract the histogram or event data from a loaded GEANT4 detector."""
return RawDetectorData[RunType](extract_detector_data(detector))
return RawDetector[RunType](extract_detector_data(detector))


def _load_raw_events(file_path: str) -> sc.DataArray:
Expand Down Expand Up @@ -176,7 +176,7 @@ def get_sample_position(raw_sample: RawSample[RunType]) -> SamplePosition[RunTyp


def patch_detector_data(
detector_data: RawDetectorData[RunType],
detector_data: RawDetector[RunType],
source_position: SourcePosition[RunType],
sample_position: SamplePosition[RunType],
) -> ReducibleDetectorData[RunType]:
Expand All @@ -188,7 +188,7 @@ def patch_detector_data(


def geant4_detector_dimensions(
data: RawDetectorData[SampleRun],
data: RawDetector[SampleRun],
) -> NeXusDetectorDimensions:
# For geant4 data, we group by detector identifier, so the data already has
# logical dimensions, so we simply return the dimensions of the detector.
Expand Down
104 changes: 7 additions & 97 deletions src/ess/dream/io/nexus.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,7 @@
but it is not possible to reshape the data into all the logical dimensions.
"""

import scipp as sc
from ess.reduce import nexus

from ess.powder.types import (
Filename,
LoadedNeXusDetector,
NeXusDetectorName,
RawDetectorData,
RawSample,
RawSource,
ReducibleDetectorData,
RunType,
SamplePosition,
SourcePosition,
)
from ess import powder

DETECTOR_BANK_SIZES = {
"endcap_backward_detector": {
Expand All @@ -51,95 +37,19 @@
"strip": 256,
"counter": 2,
},
"high_resolution_detector": {
"strip": 32,
"other": -1,
},
"high_resolution_detector": {"strip": 32, "other": -1},
"sans_detector": lambda x: x.fold(
dim="detector_number",
sizes={
"strip": 32,
"other": -1,
},
sizes={"strip": 32, "other": -1},
),
}


def load_nexus_sample(file_path: Filename[RunType]) -> RawSample[RunType]:
return RawSample[RunType](nexus.load_sample(file_path))


def dummy_load_sample(file_path: Filename[RunType]) -> RawSample[RunType]:
"""
In test files there is not always a sample, so we need a dummy.
"""
return RawSample[RunType](
sc.DataGroup({'position': sc.vector(value=[0, 0, 0], unit='m')})
)


def load_nexus_source(file_path: Filename[RunType]) -> RawSource[RunType]:
return RawSource[RunType](nexus.load_source(file_path))


def load_nexus_detector(
file_path: Filename[RunType], detector_name: NeXusDetectorName
) -> LoadedNeXusDetector[RunType]:
out = nexus.load_detector(file_path=file_path, detector_name=detector_name)
out.pop("pixel_shape", None)
return LoadedNeXusDetector[RunType](out)


def get_source_position(
raw_source: RawSource[RunType],
) -> SourcePosition[RunType]:
return SourcePosition[RunType](raw_source["position"])


def get_sample_position(
raw_sample: RawSample[RunType],
) -> SamplePosition[RunType]:
return SamplePosition[RunType](raw_sample["position"])


def get_detector_data(
detector: LoadedNeXusDetector[RunType],
detector_name: NeXusDetectorName,
) -> RawDetectorData[RunType]:
da = nexus.extract_detector_data(detector)
if detector_name in DETECTOR_BANK_SIZES:
da = da.fold(dim="detector_number", sizes=DETECTOR_BANK_SIZES[detector_name])
return RawDetectorData[RunType](da)


def patch_detector_data(
detector_data: RawDetectorData[RunType],
source_position: SourcePosition[RunType],
sample_position: SamplePosition[RunType],
) -> ReducibleDetectorData[RunType]:
"""
Patch a detector data object with source and sample positions.
Also adds variances to the event data if they are missing.
"""
out = detector_data.copy(deep=False)
if out.bins is not None:
content = out.bins.constituents["data"]
if content.variances is None:
content.variances = content.values
out.coords["sample_position"] = sample_position
out.coords["source_position"] = source_position
return ReducibleDetectorData[RunType](out)
def dream_detector_bank_sizes() -> powder.types.DetectorBankSizes | None:
return powder.types.DetectorBankSizes(DETECTOR_BANK_SIZES)


providers = (
load_nexus_sample,
load_nexus_source,
load_nexus_detector,
get_source_position,
get_sample_position,
get_detector_data,
patch_detector_data,
)
providers = (*powder.nexus.providers, dream_detector_bank_sizes)
"""
Providers for loading and processing DREAM NeXus data.
Providers for loading and processing NeXus data.
"""
2 changes: 2 additions & 0 deletions src/ess/powder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
smoothing,
)
from .masking import with_pixel_mask_filenames
from . import nexus

try:
__version__ = importlib.metadata.version(__package__ or __name__)
Expand All @@ -39,6 +40,7 @@
"filtering",
"grouping",
"masking",
"nexus",
"providers",
"smoothing",
"with_pixel_mask_filenames",
Expand Down
Loading