essreduce 25.11.1__py3-none-any.whl → 25.11.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -20,6 +20,7 @@ from ._nexus_loader import (
20
20
  load_all_components,
21
21
  load_component,
22
22
  load_data,
23
+ load_from_path,
23
24
  open_component_group,
24
25
  open_nexus_file,
25
26
  )
@@ -33,6 +34,7 @@ __all__ = [
33
34
  'load_all_components',
34
35
  'load_component',
35
36
  'load_data',
37
+ 'load_from_path',
36
38
  'open_component_group',
37
39
  'open_nexus_file',
38
40
  'types',
@@ -8,7 +8,7 @@ from collections.abc import Generator, Mapping
8
8
  from contextlib import AbstractContextManager, contextmanager, nullcontext
9
9
  from dataclasses import dataclass
10
10
  from math import prod
11
- from typing import TypeVar, cast
11
+ from typing import Any, TypeVar, cast
12
12
 
13
13
  import scipp as sc
14
14
  import scippnexus as snx
@@ -42,6 +42,31 @@ class NoLockingIfNeededType:
42
42
  NoLockingIfNeeded = NoLockingIfNeededType()
43
43
 
44
44
 
45
+ def load_from_path(
46
+ location: NeXusLocationSpec,
47
+ definitions: Mapping | NoNewDefinitionsType = NoNewDefinitions,
48
+ ) -> Any:
49
+ """Load a field or group from a NeXus file given its location.
50
+
51
+ Parameters
52
+ ----------
53
+ location:
54
+ Location of the field within the NeXus file (filename, entry name, selection).
55
+ definitions:
56
+ Application definitions to use for the file.
57
+
58
+ Returns
59
+ -------
60
+ :
61
+ The loaded field (as a variable, data array, or raw python object) or group
62
+ (as a data group).
63
+ """
64
+ with open_nexus_file(location.filename, definitions=definitions) as f:
65
+ entry = _unique_child_group(f, snx.NXentry, location.entry_name)
66
+ item = entry[location.component_name]
67
+ return item[location.selection]
68
+
69
+
45
70
  def load_component(
46
71
  location: NeXusLocationSpec,
47
72
  *,
@@ -691,16 +691,12 @@ def LoadMonitorWorkflow(
691
691
 
692
692
 
693
693
  def LoadDetectorWorkflow(
694
- *,
695
- run_types: Iterable[sciline.typing.Key],
696
- monitor_types: Iterable[sciline.typing.Key],
694
+ *, run_types: Iterable[sciline.typing.Key]
697
695
  ) -> sciline.Pipeline:
698
696
  """Generic workflow for loading detector data from a NeXus file."""
699
697
  wf = sciline.Pipeline(
700
698
  (*_common_providers, *_detector_providers),
701
- constraints=_gather_constraints(
702
- run_types=run_types, monitor_types=monitor_types
703
- ),
699
+ constraints=_gather_constraints(run_types=run_types, monitor_types=[]),
704
700
  )
705
701
  wf[DetectorBankSizes] = DetectorBankSizes({})
706
702
  wf[PreopenNeXusFile] = PreopenNeXusFile(False)
@@ -28,6 +28,7 @@ from .types import (
28
28
  PulseStrideOffset,
29
29
  TimeOfFlightLookupTable,
30
30
  TimeOfFlightLookupTableFilename,
31
+ ToaDetector,
31
32
  TofDetector,
32
33
  TofMonitor,
33
34
  )
@@ -51,6 +52,7 @@ __all__ = [
51
52
  "TimeOfFlightLookupTable",
52
53
  "TimeOfFlightLookupTableFilename",
53
54
  "TimeResolution",
55
+ "ToaDetector",
54
56
  "TofDetector",
55
57
  "TofLookupTableWorkflow",
56
58
  "TofMonitor",
@@ -36,6 +36,7 @@ from .types import (
36
36
  MonitorLtotal,
37
37
  PulseStrideOffset,
38
38
  TimeOfFlightLookupTable,
39
+ ToaDetector,
39
40
  TofDetector,
40
41
  TofMonitor,
41
42
  )
@@ -196,12 +197,32 @@ def _guess_pulse_stride_offset(
196
197
  return sorted(tofs, key=lambda x: sc.isnan(tofs[x]).sum())[0]
197
198
 
198
199
 
199
- def _time_of_flight_data_events(
200
+ def _prepare_tof_interpolation_inputs(
200
201
  da: sc.DataArray,
201
202
  lookup: sc.DataArray,
202
203
  ltotal: sc.Variable,
203
- pulse_stride_offset: int,
204
- ) -> sc.DataArray:
204
+ pulse_stride_offset: int | None,
205
+ ) -> dict:
206
+ """
207
+ Prepare the inputs required for the time-of-flight interpolation.
208
+ This function is used when computing the time-of-flight for event data, and for
209
+ computing the time-of-arrival for event data (as they both require guessing the
210
+ pulse_stride_offset if not provided).
211
+
212
+ Parameters
213
+ ----------
214
+ da:
215
+ Data array with event data.
216
+ lookup:
217
+ Lookup table giving time-of-flight as a function of distance and time of
218
+ arrival.
219
+ ltotal:
220
+ Total length of the flight path from the source to the detector.
221
+ pulse_stride_offset:
222
+ When pulse-skipping, the offset of the first pulse in the stride. This is
223
+ typically zero but can be a small integer < pulse_stride.
224
+ If None, a guess is made.
225
+ """
205
226
  etos = da.bins.coords["event_time_offset"].to(dtype=float, copy=False)
206
227
  eto_unit = elem_unit(etos)
207
228
 
@@ -259,12 +280,34 @@ def _time_of_flight_data_events(
259
280
  pulse_index += pulse_stride_offset
260
281
  pulse_index %= pulse_stride
261
282
 
262
- # Compute time-of-flight for all neutrons using the interpolator
263
- tofs = interp(
283
+ return {
284
+ "eto": etos,
285
+ "pulse_index": pulse_index,
286
+ "pulse_period": pulse_period,
287
+ "interp": interp,
288
+ "ltotal": ltotal,
289
+ }
290
+
291
+
292
+ def _time_of_flight_data_events(
293
+ da: sc.DataArray,
294
+ lookup: sc.DataArray,
295
+ ltotal: sc.Variable,
296
+ pulse_stride_offset: int | None,
297
+ ) -> sc.DataArray:
298
+ inputs = _prepare_tof_interpolation_inputs(
299
+ da=da,
300
+ lookup=lookup,
264
301
  ltotal=ltotal,
265
- event_time_offset=etos,
266
- pulse_index=pulse_index,
267
- pulse_period=pulse_period,
302
+ pulse_stride_offset=pulse_stride_offset,
303
+ )
304
+
305
+ # Compute time-of-flight for all neutrons using the interpolator
306
+ tofs = inputs["interp"](
307
+ ltotal=inputs["ltotal"],
308
+ event_time_offset=inputs["eto"],
309
+ pulse_index=inputs["pulse_index"],
310
+ pulse_period=inputs["pulse_period"],
268
311
  )
269
312
 
270
313
  parts = da.bins.constituents
@@ -416,6 +459,53 @@ def monitor_time_of_flight_data(
416
459
  )
417
460
 
418
461
 
462
+ def detector_time_of_arrival_data(
463
+ detector_data: RawDetector[RunType],
464
+ lookup: TimeOfFlightLookupTable,
465
+ ltotal: DetectorLtotal[RunType],
466
+ pulse_stride_offset: PulseStrideOffset,
467
+ ) -> ToaDetector[RunType]:
468
+ """
469
+ Convert the time-of-flight data to time-of-arrival data using a lookup table.
470
+ The output data will have a time-of-arrival coordinate.
471
+ The time-of-arrival is the time since the neutron was emitted from the source.
472
+ It is basically equal to event_time_offset + pulse_index * pulse_period.
473
+
474
+ Parameters
475
+ ----------
476
+ da:
477
+ Raw detector data loaded from a NeXus file, e.g., NXdetector containing
478
+ NXevent_data.
479
+ lookup:
480
+ Lookup table giving time-of-flight as a function of distance and time of
481
+ arrival.
482
+ ltotal:
483
+ Total length of the flight path from the source to the detector.
484
+ pulse_stride_offset:
485
+ When pulse-skipping, the offset of the first pulse in the stride. This is
486
+ typically zero but can be a small integer < pulse_stride.
487
+ """
488
+ if detector_data.bins is None:
489
+ raise NotImplementedError(
490
+ "Computing time-of-arrival in histogram mode is not implemented yet."
491
+ )
492
+ inputs = _prepare_tof_interpolation_inputs(
493
+ da=detector_data,
494
+ lookup=lookup,
495
+ ltotal=ltotal,
496
+ pulse_stride_offset=pulse_stride_offset,
497
+ )
498
+ parts = detector_data.bins.constituents
499
+ parts["data"] = inputs["eto"]
500
+ # The pulse index is None if pulse_stride == 1 (i.e., no pulse skipping)
501
+ if inputs["pulse_index"] is not None:
502
+ parts["data"] = parts["data"] + inputs["pulse_index"] * inputs["pulse_period"]
503
+ result = detector_data.bins.assign_coords(
504
+ toa=sc.bins(**parts, validate_indices=False)
505
+ )
506
+ return result
507
+
508
+
419
509
  def providers() -> tuple[Callable]:
420
510
  """
421
511
  Providers of the time-of-flight workflow.
@@ -425,4 +515,5 @@ def providers() -> tuple[Callable]:
425
515
  monitor_time_of_flight_data,
426
516
  detector_ltotal_from_straight_line_approximation,
427
517
  monitor_ltotal_from_straight_line_approximation,
518
+ detector_time_of_arrival_data,
428
519
  )
@@ -56,7 +56,7 @@ class FakeBeamline:
56
56
  else tof_pkg.Clockwise,
57
57
  open=ch.slit_begin,
58
58
  close=ch.slit_end,
59
- phase=abs(ch.phase),
59
+ phase=ch.phase if ch.frequency.value > 0.0 else -ch.phase,
60
60
  distance=sc.norm(ch.axle_position - source_position),
61
61
  name=name,
62
62
  )
@@ -420,7 +420,7 @@ def simulate_chopper_cascade_using_tof(
420
420
  else tof.Clockwise,
421
421
  open=ch.slit_begin,
422
422
  close=ch.slit_end,
423
- phase=abs(ch.phase),
423
+ phase=ch.phase if ch.frequency.value > 0.0 else -ch.phase,
424
424
  distance=sc.norm(
425
425
  ch.axle_position - source_position.to(unit=ch.axle_position.unit)
426
426
  ),
@@ -37,5 +37,17 @@ class TofDetector(sl.Scope[RunType, sc.DataArray], sc.DataArray):
37
37
  """Detector data with time-of-flight coordinate."""
38
38
 
39
39
 
40
+ class ToaDetector(sl.Scope[RunType, sc.DataArray], sc.DataArray):
41
+ """Detector data with time-of-arrival coordinate.
42
+
43
+ When the pulse stride is 1 (i.e., no pulse skipping), the time-of-arrival is the
44
+ same as the event_time_offset. When pulse skipping is used, the time-of-arrival is
45
+ the event_time_offset + pulse_offset * pulse_period.
46
+ This means that the time-of-arrival is basically the event_time_offset wrapped
47
+ over the frame period instead of the pulse period
48
+ (where frame_period = pulse_stride * pulse_period).
49
+ """
50
+
51
+
40
52
  class TofMonitor(sl.Scope[RunType, MonitorType, sc.DataArray], sc.DataArray):
41
53
  """Monitor data with time-of-flight coordinate."""
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: essreduce
3
- Version: 25.11.1
3
+ Version: 25.11.2
4
4
  Summary: Common data reduction tools for the ESS facility
5
5
  Author: Scipp contributors
6
6
  License-Expression: BSD-3-Clause
@@ -12,21 +12,21 @@ ess/reduce/live/__init__.py,sha256=jPQVhihRVNtEDrE20PoKkclKV2aBF1lS7cCHootgFgI,2
12
12
  ess/reduce/live/raw.py,sha256=d86s5fBjdf6tI_FWNMG4ZG67GCY2JADOobrinncNIjE,24367
13
13
  ess/reduce/live/roi.py,sha256=Hs-pW98k41WU6Kl3UQ41kQawk80c2QNOQ_WNctLzDPE,3795
14
14
  ess/reduce/live/workflow.py,sha256=bsbwvTqPhRO6mC__3b7MgU7DWwAnOvGvG-t2n22EKq8,4285
15
- ess/reduce/nexus/__init__.py,sha256=8TLqqALxRV5_QdCwLAjq3r6xneq6rzdh2dD7d9AXkYo,1065
16
- ess/reduce/nexus/_nexus_loader.py,sha256=8jN97CbFaSJ6XZEFzkoeAGyol8WpLTUZsHkUWu8WHsU,23136
15
+ ess/reduce/nexus/__init__.py,sha256=xXc982vZqRba4jR4z5hA2iim17Z7niw4KlS1aLFbn1Q,1107
16
+ ess/reduce/nexus/_nexus_loader.py,sha256=5J26y_t-kabj0ik0jf3OLSYda3lDLDQhvPd2_ro7Q_0,23927
17
17
  ess/reduce/nexus/json_generator.py,sha256=ME2Xn8L7Oi3uHJk9ZZdCRQTRX-OV_wh9-DJn07Alplk,2529
18
18
  ess/reduce/nexus/json_nexus.py,sha256=QrVc0p424nZ5dHX9gebAJppTw6lGZq9404P_OFl1giA,10282
19
19
  ess/reduce/nexus/types.py,sha256=g5oBBEYPH7urF1tDP0tqXtixhQN8JDpe8vmiKrPiUW0,9320
20
- ess/reduce/nexus/workflow.py,sha256=nU_YzaRJO5yUGNIHVPScJF_oEddZxaA2gChPF9mMhDQ,25031
20
+ ess/reduce/nexus/workflow.py,sha256=KRzG_flkAGNCkwDGwhTjX3h3Hi4GexMEz84trMw7HIg,24944
21
21
  ess/reduce/scripts/grow_nexus.py,sha256=hET3h06M0xlJd62E3palNLFvJMyNax2kK4XyJcOhl-I,3387
22
- ess/reduce/time_of_flight/__init__.py,sha256=Av6Pu_AKO0tF8IsWXchUXjjHps7ts2NFVnui6k3Eq-o,1425
23
- ess/reduce/time_of_flight/eto_to_tof.py,sha256=aPPQYbbz_73eq5dQCS97q02D2UUFntOfow2lWkkRyxg,14993
24
- ess/reduce/time_of_flight/fakes.py,sha256=0gtbSX3ZQilaM4ZP5dMr3fqbnhpyoVsZX2YEb8GgREE,4489
22
+ ess/reduce/time_of_flight/__init__.py,sha256=jn8x9rZ6PzyP_wK8ACd3cg9rOpDAu_IqHyTNSeKfVn0,1461
23
+ ess/reduce/time_of_flight/eto_to_tof.py,sha256=NN-UeVuFVg_WIe6ePAxN7HSxFxd-1BCrdasFB5WEfHw,18166
24
+ ess/reduce/time_of_flight/fakes.py,sha256=4viK0OsmwEHSc5bDqSNN0jdo982D-oO4phdLC6vZLng,4527
25
25
  ess/reduce/time_of_flight/interpolator_numba.py,sha256=wh2YS3j2rOu30v1Ok3xNHcwS7t8eEtZyZvbfXOCtgrQ,3835
26
26
  ess/reduce/time_of_flight/interpolator_scipy.py,sha256=_InoAPuMm2qhJKZQBAHOGRFqtvvuQ8TStoN7j_YgS4M,1853
27
- ess/reduce/time_of_flight/lut.py,sha256=bNf3EmMw86pp3lB499zvKEkCkcDlqS2eHFvyVX4bCBk,18750
27
+ ess/reduce/time_of_flight/lut.py,sha256=k8_aLQSK4kwvpOIOJgUs8J0DgromMieiKYSl7PTBC18,18788
28
28
  ess/reduce/time_of_flight/resample.py,sha256=Opmi-JA4zNH725l9VB99U4O9UlM37f5ACTCGtwBcows,3718
29
- ess/reduce/time_of_flight/types.py,sha256=VhJYLcLA_NfU-lILFNxjBkQUaC8AeNr_DJMeQuNgZOc,1302
29
+ ess/reduce/time_of_flight/types.py,sha256=xq-CjRZIzcfknqeNcUXMIZxvfK9eYmAJtXv-i4sJamE,1855
30
30
  ess/reduce/time_of_flight/workflow.py,sha256=mkgESvQ5Yt3CyAsa1iewkjBOHUqrHm5rRc1EhOQRewQ,2213
31
31
  ess/reduce/widgets/__init__.py,sha256=SoSHBv8Dc3QXV9HUvPhjSYWMwKTGYZLpsWwsShIO97Q,5325
32
32
  ess/reduce/widgets/_base.py,sha256=_wN3FOlXgx_u0c-A_3yyoIH-SdUvDENGgquh9S-h5GI,4852
@@ -40,9 +40,9 @@ ess/reduce/widgets/_spinner.py,sha256=2VY4Fhfa7HMXox2O7UbofcdKsYG-AJGrsgGJB85nDX
40
40
  ess/reduce/widgets/_string_widget.py,sha256=iPAdfANyXHf-nkfhgkyH6gQDklia0LebLTmwi3m-iYQ,1482
41
41
  ess/reduce/widgets/_switchable_widget.py,sha256=fjKz99SKLhIF1BLgGVBSKKn3Lu_jYBwDYGeAjbJY3Q8,2390
42
42
  ess/reduce/widgets/_vector_widget.py,sha256=aTaBqCFHZQhrIoX6-sSqFWCPePEW8HQt5kUio8jP1t8,1203
43
- essreduce-25.11.1.dist-info/licenses/LICENSE,sha256=nVEiume4Qj6jMYfSRjHTM2jtJ4FGu0g-5Sdh7osfEYw,1553
44
- essreduce-25.11.1.dist-info/METADATA,sha256=YJBtrUy0Gw60xZHi0TrmkBhC4XroNNC_CIEL0qRD5z8,1937
45
- essreduce-25.11.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
46
- essreduce-25.11.1.dist-info/entry_points.txt,sha256=PMZOIYzCifHMTe4pK3HbhxUwxjFaZizYlLD0td4Isb0,66
47
- essreduce-25.11.1.dist-info/top_level.txt,sha256=0JxTCgMKPLKtp14wb1-RKisQPQWX7i96innZNvHBr-s,4
48
- essreduce-25.11.1.dist-info/RECORD,,
43
+ essreduce-25.11.2.dist-info/licenses/LICENSE,sha256=nVEiume4Qj6jMYfSRjHTM2jtJ4FGu0g-5Sdh7osfEYw,1553
44
+ essreduce-25.11.2.dist-info/METADATA,sha256=BiPKZU7f3M7bof_N1dQWwIfG8TAEHATY18zU6s4ihxk,1937
45
+ essreduce-25.11.2.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
46
+ essreduce-25.11.2.dist-info/entry_points.txt,sha256=PMZOIYzCifHMTe4pK3HbhxUwxjFaZizYlLD0td4Isb0,66
47
+ essreduce-25.11.2.dist-info/top_level.txt,sha256=0JxTCgMKPLKtp14wb1-RKisQPQWX7i96innZNvHBr-s,4
48
+ essreduce-25.11.2.dist-info/RECORD,,