Skip to content

Commit 5bda4f8

Browse files
authored
Merge pull request #704 from European-XFEL/feat/keydata-train-index
Add KeyData.train_index_bounds
2 parents 6b9cc18 + ffdd5dd commit 5bda4f8

3 files changed

Lines changed: 51 additions & 1 deletion

File tree

extra_data/keydata.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,29 @@ def train_id_coordinates(self):
536536
]
537537
return np.concatenate(chunks_trainids)
538538

539+
def train_index_bounds(self, labelled=False):
540+
"""Generate first and last indices of trains to use alongside data
541+
from ``.ndarray()``.
542+
543+
If *labelled* is True, returns a pandas dataframe with columns start
544+
and stop. Otherwise, returns a tuple of two NumPy arrays.
545+
"""
546+
547+
counts = self.data_counts(labelled)
548+
start = counts.copy()
549+
550+
if labelled:
551+
start.iloc[0] = 0
552+
start.iloc[1:] = counts.cumsum().iloc[:-1]
553+
stop = start + counts
554+
import pandas as pd
555+
return pd.concat(
556+
[start.rename('start'), stop.rename('stop')], axis=1)
557+
else:
558+
start[0] = 0
559+
start[1:] = counts.cumsum()[:-1]
560+
return start, start + counts
561+
539562
def xarray(self, extra_dims=None, roi=(), name=None, extra_coords=None):
540563
"""Load this data as a labelled xarray array or dataset.
541564

extra_data/tests/mockdata/xgm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class XGM(DeviceBase):
3131
('pulseEnergy/crossUsed', 'f4', ()),
3232
('pulseEnergy/gammaUsed', 'f4', ()),
3333
('pulseEnergy/gmdError', 'i4', ()),
34-
('pulseEnergy/nummberOfBrunches', 'f4', ()),
34+
('pulseEnergy/nummberOfBunches', 'f4', ()),
3535
('pulseEnergy/photonFlux', 'f4', ()),
3636
('pulseEnergy/pressure', 'f4', ()),
3737
('pulseEnergy/temperature', 'f4', ()),

extra_data/tests/test_keydata.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,33 @@ def test_data_counts_missing_train(fxe_run_module_offset):
248248
np.testing.assert_array_equal(arr, 128)
249249

250250

251+
@pytest.mark.parametrize('labelled', [True, False])
252+
def test_train_index_bounds(mock_spb_raw_run, labelled):
253+
run = RunDirectory(mock_spb_raw_run)
254+
255+
agipd_m0 = run['SPB_DET_AGIPD1M-1/DET/0CH0:xtdf', 'image.pulseId']
256+
bounds = agipd_m0.train_index_bounds(labelled)
257+
258+
if labelled:
259+
start, stop = bounds['start'], bounds['stop']
260+
else:
261+
start, stop = bounds
262+
263+
np.testing.assert_array_equal(start, np.arange(0, 4032+1, 64))
264+
np.testing.assert_array_equal(stop, start+64)
265+
266+
xgm = run['SPB_XTD9_XGM/DOOCS/MAIN', 'pulseEnergy.photonFlux']
267+
bounds = xgm.train_index_bounds(labelled)
268+
269+
if labelled:
270+
start, stop = bounds['start'], bounds['stop']
271+
else:
272+
start, stop = bounds
273+
274+
np.testing.assert_array_equal(start, np.arange(len(start)))
275+
np.testing.assert_array_equal(stop, start+1)
276+
277+
251278
def test_select_by(mock_spb_raw_run):
252279
run = RunDirectory(mock_spb_raw_run)
253280
am0 = run['SPB_DET_AGIPD1M-1/DET/0CH0:xtdf', 'image.data']

0 commit comments

Comments
 (0)