Skip to content

Commit aeece25

Browse files
committed
Add KeyData.run_value()
1 parent b48c82b commit aeece25

3 files changed

Lines changed: 38 additions & 1 deletion

File tree

extra_data/keydata.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class KeyData:
105105
"""
106106
def __init__(
107107
self, source, key, *, train_ids, files, section, dtype, eshape,
108-
inc_suspect_trains=True,
108+
is_single_run, inc_suspect_trains=True,
109109
):
110110
self.source = source
111111
self.key = key
@@ -115,6 +115,7 @@ def __init__(
115115
self.dtype = dtype
116116
self.entry_shape = eshape
117117
self.ndim = len(eshape) + 1
118+
self.is_single_run = is_single_run
118119
self.inc_suspect_trains = inc_suspect_trains
119120

120121
def _find_chunks(self):
@@ -343,6 +344,7 @@ def _only_tids(self, tids, files=None):
343344
section=self.section,
344345
dtype=self.dtype,
345346
eshape=self.entry_shape,
347+
is_single_run=self.is_single_run,
346348
inc_suspect_trains=self.inc_suspect_trains,
347349
)
348350

@@ -472,6 +474,29 @@ def as_single_value(self, rtol=1e-5, atol=0.0, reduce_by=None):
472474

473475
return value
474476

477+
def run_value(self, allow_multi_run=False):
478+
"""Get the RUN value for this key if it exists.
479+
480+
This method is intended for use with data from a single run. If you
481+
combine data from multiple runs, it will raise MultiRunError.
482+
483+
Returns the RUN parameter value corresponding to this key.
484+
"""
485+
486+
from .sourcedata import SourceData # Prevent cyclic import.
487+
488+
# Construct minimal SourceData object to obtain RUN value.
489+
return SourceData(
490+
self.source,
491+
sel_keys=None,
492+
train_ids=self.train_ids,
493+
files=self.files,
494+
section=self.section,
495+
canonical_name=self.source,
496+
is_single_run=self.is_single_run,
497+
inc_suspect_trains=self.inc_suspect_trains
498+
).run_value(self.key, allow_multi_run=allow_multi_run)
499+
475500
# Getting data as different kinds of array: -------------------------------
476501

477502
def ndarray(self, roi=(), out=None):

extra_data/sourcedata.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ def __getitem__(self, key):
118118
section=self.section,
119119
dtype=ds0.dtype,
120120
eshape=ds0.shape[1:],
121+
is_single_run=self.is_single_run,
121122
inc_suspect_trains=self.inc_suspect_trains,
122123
)
123124

extra_data/tests/test_keydata.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,17 @@ def test_single_value(mock_sa3_control_data, monkeypatch):
363363
np.testing.assert_equal(intensity.as_single_value(rtol=1), np.median(data))
364364

365365

366+
def test_run_value(mock_sa3_control_data):
367+
f = H5File(mock_sa3_control_data)
368+
369+
flux = f['SA3_XTD10_XGM/XGM/DOOCS', 'pulseEnergy.photonFlux']
370+
assert flux.run_value() == 0.0
371+
372+
imager = f['SA3_XTD10_IMGFEL/CAM/BEAMVIEW:daqOutput', 'data.image.pixels']
373+
with pytest.raises(ValueError):
374+
assert imager.run_value()
375+
376+
366377
def test_ndarray_out(mock_spb_raw_run):
367378
f = RunDirectory(mock_spb_raw_run)
368379
cam = f['SPB_IRU_CAM/CAM/SIDEMIC:daqOutput', 'data.image.dims']

0 commit comments

Comments
 (0)