Skip to content

Commit 79ddce2

Browse files
Merge pull request #78 from scipp/nexus
Add NeXus loading helpers
2 parents f0a2f56 + 78b985a commit 79ddce2

File tree

8 files changed

+387
-135
lines changed

8 files changed

+387
-135
lines changed

src/ess/dream/io/geant4.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
CalibrationData,
1111
CalibrationFilename,
1212
Filename,
13+
NeXusDetector,
1314
NeXusDetectorDimensions,
1415
NeXusDetectorName,
1516
RawDetector,
16-
RawDetectorData,
1717
RawSample,
1818
RawSource,
1919
ReducibleDetectorData,
@@ -64,16 +64,16 @@ def load_geant4_csv(file_path: Filename[RunType]) -> AllRawDetectors[RunType]:
6464

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

7171

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

7878

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

177177

178178
def patch_detector_data(
179-
detector_data: RawDetectorData[RunType],
179+
detector_data: RawDetector[RunType],
180180
source_position: SourcePosition[RunType],
181181
sample_position: SamplePosition[RunType],
182182
) -> ReducibleDetectorData[RunType]:
@@ -188,7 +188,7 @@ def patch_detector_data(
188188

189189

190190
def geant4_detector_dimensions(
191-
data: RawDetectorData[SampleRun],
191+
data: RawDetector[SampleRun],
192192
) -> NeXusDetectorDimensions:
193193
# For geant4 data, we group by detector identifier, so the data already has
194194
# logical dimensions, so we simply return the dimensions of the detector.

src/ess/dream/io/nexus.py

Lines changed: 7 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,7 @@
1313
but it is not possible to reshape the data into all the logical dimensions.
1414
"""
1515

16-
import scipp as sc
17-
from ess.reduce import nexus
18-
19-
from ess.powder.types import (
20-
Filename,
21-
LoadedNeXusDetector,
22-
NeXusDetectorName,
23-
RawDetectorData,
24-
RawSample,
25-
RawSource,
26-
ReducibleDetectorData,
27-
RunType,
28-
SamplePosition,
29-
SourcePosition,
30-
)
16+
from ess import powder
3117

3218
DETECTOR_BANK_SIZES = {
3319
"endcap_backward_detector": {
@@ -51,95 +37,19 @@
5137
"strip": 256,
5238
"counter": 2,
5339
},
54-
"high_resolution_detector": {
55-
"strip": 32,
56-
"other": -1,
57-
},
40+
"high_resolution_detector": {"strip": 32, "other": -1},
5841
"sans_detector": lambda x: x.fold(
5942
dim="detector_number",
60-
sizes={
61-
"strip": 32,
62-
"other": -1,
63-
},
43+
sizes={"strip": 32, "other": -1},
6444
),
6545
}
6646

6747

68-
def load_nexus_sample(file_path: Filename[RunType]) -> RawSample[RunType]:
69-
return RawSample[RunType](nexus.load_sample(file_path))
70-
71-
72-
def dummy_load_sample(file_path: Filename[RunType]) -> RawSample[RunType]:
73-
"""
74-
In test files there is not always a sample, so we need a dummy.
75-
"""
76-
return RawSample[RunType](
77-
sc.DataGroup({'position': sc.vector(value=[0, 0, 0], unit='m')})
78-
)
79-
80-
81-
def load_nexus_source(file_path: Filename[RunType]) -> RawSource[RunType]:
82-
return RawSource[RunType](nexus.load_source(file_path))
83-
84-
85-
def load_nexus_detector(
86-
file_path: Filename[RunType], detector_name: NeXusDetectorName
87-
) -> LoadedNeXusDetector[RunType]:
88-
out = nexus.load_detector(file_path=file_path, detector_name=detector_name)
89-
out.pop("pixel_shape", None)
90-
return LoadedNeXusDetector[RunType](out)
91-
92-
93-
def get_source_position(
94-
raw_source: RawSource[RunType],
95-
) -> SourcePosition[RunType]:
96-
return SourcePosition[RunType](raw_source["position"])
97-
98-
99-
def get_sample_position(
100-
raw_sample: RawSample[RunType],
101-
) -> SamplePosition[RunType]:
102-
return SamplePosition[RunType](raw_sample["position"])
103-
104-
105-
def get_detector_data(
106-
detector: LoadedNeXusDetector[RunType],
107-
detector_name: NeXusDetectorName,
108-
) -> RawDetectorData[RunType]:
109-
da = nexus.extract_detector_data(detector)
110-
if detector_name in DETECTOR_BANK_SIZES:
111-
da = da.fold(dim="detector_number", sizes=DETECTOR_BANK_SIZES[detector_name])
112-
return RawDetectorData[RunType](da)
113-
114-
115-
def patch_detector_data(
116-
detector_data: RawDetectorData[RunType],
117-
source_position: SourcePosition[RunType],
118-
sample_position: SamplePosition[RunType],
119-
) -> ReducibleDetectorData[RunType]:
120-
"""
121-
Patch a detector data object with source and sample positions.
122-
Also adds variances to the event data if they are missing.
123-
"""
124-
out = detector_data.copy(deep=False)
125-
if out.bins is not None:
126-
content = out.bins.constituents["data"]
127-
if content.variances is None:
128-
content.variances = content.values
129-
out.coords["sample_position"] = sample_position
130-
out.coords["source_position"] = source_position
131-
return ReducibleDetectorData[RunType](out)
48+
def dream_detector_bank_sizes() -> powder.types.DetectorBankSizes | None:
49+
return powder.types.DetectorBankSizes(DETECTOR_BANK_SIZES)
13250

13351

134-
providers = (
135-
load_nexus_sample,
136-
load_nexus_source,
137-
load_nexus_detector,
138-
get_source_position,
139-
get_sample_position,
140-
get_detector_data,
141-
patch_detector_data,
142-
)
52+
providers = (*powder.nexus.providers, dream_detector_bank_sizes)
14353
"""
144-
Providers for loading and processing DREAM NeXus data.
54+
Providers for loading and processing NeXus data.
14555
"""

src/ess/powder/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
smoothing,
1616
)
1717
from .masking import with_pixel_mask_filenames
18+
from . import nexus
1819

1920
try:
2021
__version__ = importlib.metadata.version(__package__ or __name__)
@@ -39,6 +40,7 @@
3940
"filtering",
4041
"grouping",
4142
"masking",
43+
"nexus",
4244
"providers",
4345
"smoothing",
4446
"with_pixel_mask_filenames",

0 commit comments

Comments
 (0)