|
| 1 | +import os |
| 2 | +import tempfile |
| 3 | + |
| 4 | +import h5py |
| 5 | +import numpy as np |
| 6 | +import pytest |
| 7 | + |
| 8 | +from ess.reduce.scripts.grow_nexus import grow_nexus_file |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def nexus_file(): |
| 13 | + with tempfile.TemporaryDirectory() as tmp: |
| 14 | + path = os.path.join(tmp, 'test.nxs') |
| 15 | + with h5py.File(path, 'a') as hf: |
| 16 | + entry = hf.create_group('entry') |
| 17 | + entry.attrs['NX_class'] = 'NXentry' |
| 18 | + |
| 19 | + instrument = entry.create_group('instrument') |
| 20 | + instrument.attrs['NX_class'] = 'NXinstrument' |
| 21 | + |
| 22 | + for group, nxclass in ( |
| 23 | + ('detector', 'NXdetector'), |
| 24 | + ('monitor', 'NXmonitor'), |
| 25 | + ): |
| 26 | + detector = instrument.create_group(group) |
| 27 | + detector.attrs['NX_class'] = nxclass |
| 28 | + |
| 29 | + event_data = detector.create_group('event_data') |
| 30 | + event_data.attrs['NX_class'] = 'NXevent_data' |
| 31 | + |
| 32 | + event_data.create_dataset( |
| 33 | + 'event_index', |
| 34 | + data=np.array([2, 4, 6]), |
| 35 | + maxshape=(None,), |
| 36 | + chunks=True, |
| 37 | + ) |
| 38 | + event_data.create_dataset( |
| 39 | + 'event_time_zero', |
| 40 | + data=np.array([0, 1, 2]), |
| 41 | + maxshape=(None,), |
| 42 | + chunks=True, |
| 43 | + ) |
| 44 | + event_data.create_dataset( |
| 45 | + 'event_id', |
| 46 | + data=np.array([0, 1, 2, 0, 1, 2]), |
| 47 | + maxshape=(None,), |
| 48 | + chunks=True, |
| 49 | + ) |
| 50 | + event_data.create_dataset( |
| 51 | + 'event_time_offset', |
| 52 | + data=np.array([1, 2, 1, 2, 1, 2]), |
| 53 | + maxshape=(None,), |
| 54 | + chunks=True, |
| 55 | + ) |
| 56 | + |
| 57 | + yield path |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.parametrize('monitor_scale', (1, 2, None)) |
| 61 | +@pytest.mark.parametrize('detector_scale', (1, 2)) |
| 62 | +def test_grow_nexus(nexus_file, detector_scale, monitor_scale): |
| 63 | + grow_nexus_file( |
| 64 | + filename=nexus_file, detector_scale=detector_scale, monitor_scale=monitor_scale |
| 65 | + ) |
| 66 | + |
| 67 | + monitor_scale = monitor_scale if monitor_scale is not None else detector_scale |
| 68 | + |
| 69 | + with h5py.File(nexus_file, 'r') as f: |
| 70 | + for detector, scale in zip( |
| 71 | + ('detector', 'monitor'), (detector_scale, monitor_scale) |
| 72 | + ): |
| 73 | + np.testing.assert_equal( |
| 74 | + [scale * i for i in [2, 4, 6]], |
| 75 | + f[f'entry/instrument/{detector}/event_data/event_index'][()], |
| 76 | + ) |
| 77 | + np.testing.assert_equal( |
| 78 | + scale * [0, 1, 2, 0, 1, 2], |
| 79 | + f[f'entry/instrument/{detector}/event_data/event_id'][()], |
| 80 | + ) |
| 81 | + np.testing.assert_equal( |
| 82 | + scale * [1, 2, 1, 2, 1, 2], |
| 83 | + f[f'entry/instrument/{detector}/event_data/event_time_offset'][()], |
| 84 | + ) |
0 commit comments