|
| 1 | +# Copyright (c) DataLab Platform Developers, BSD 3-Clause license, see LICENSE file. |
| 2 | + |
| 3 | +""" |
| 4 | +NeXus scans and multidimensional views test |
| 5 | +-------------------------------------------- |
| 6 | +
|
| 7 | +A scan written with the legacy NeXus conventions (as recent SOLEIL scans) is |
| 8 | +imported with its axes: its counter versus the scanned motor and its detector |
| 9 | +spectra as an image with the motor along Y. The frames of an image stack whose |
| 10 | +view is enabled in the HDF5 browser are imported as images titled and located |
| 11 | +by the motor position, and their origin survives a workspace round trip. |
| 12 | +""" |
| 13 | + |
| 14 | +# guitest: show |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +import json |
| 19 | +import os.path as osp |
| 20 | + |
| 21 | +import h5py |
| 22 | +import numpy as np |
| 23 | +import pytest |
| 24 | +from sigima.objects import get_experimental_source |
| 25 | +from sigimax.h5.selection import NDView |
| 26 | +from sigimax.widgets.h5browser import H5BrowserDialog |
| 27 | + |
| 28 | +from datalab.tests import datalab_test_app_context, helpers |
| 29 | + |
| 30 | + |
| 31 | +def _write_scan(fname: str) -> None: |
| 32 | + """Write a step scan: legacy axis attributes, a counter, a detector writing a |
| 33 | + spectrum per point and a detector writing an image per point""" |
| 34 | + with h5py.File(fname, "w") as h5file: |
| 35 | + entry = h5file.create_group("scan_0494") |
| 36 | + entry.attrs["NX_class"] = "NXentry" |
| 37 | + entry["title"] = "LaB6 calibration" |
| 38 | + scan = entry.create_group("scan_data") |
| 39 | + scan.attrs["NX_class"] = "NXdata" |
| 40 | + scan["actuator_1_1"] = np.array([-8.0, -7.5, -7.0]) |
| 41 | + scan["actuator_1_1"].attrs.update(axis=np.int32(1), primary=np.int32(1)) |
| 42 | + scan["actuator_1_1"].attrs["units"] = "deg" |
| 43 | + scan["counter"] = np.array([5.0, 9.0, 4.0]) |
| 44 | + scan["spectra"] = np.arange(3 * 16, dtype=np.int32).reshape(3, 16) |
| 45 | + scan["frames"] = np.arange(3 * 4 * 5, dtype=np.uint16).reshape(3, 4, 5) |
| 46 | + for name in ("counter", "spectra", "frames"): |
| 47 | + scan[name].attrs["signal"] = np.int32(1) |
| 48 | + |
| 49 | + |
| 50 | +def _nexus_objects(panel, signal: str) -> list: |
| 51 | + """Return the objects of a panel imported from a signal of an NXdata group""" |
| 52 | + return [ |
| 53 | + obj |
| 54 | + for obj in panel.objmodel.get_all_objects() |
| 55 | + if (get_experimental_source(obj) or {}).get("selection", {}).get("signal") |
| 56 | + == signal |
| 57 | + ] |
| 58 | + |
| 59 | + |
| 60 | +def test_scan_frames_and_workspace(monkeypatch: pytest.MonkeyPatch) -> None: |
| 61 | + """Legacy scan counters and detectors, then frames of a stack enabled in the |
| 62 | + browser, and workspace round trip""" |
| 63 | + original_open = H5BrowserDialog.open_file |
| 64 | + |
| 65 | + def open_and_enable_frames(dialog: H5BrowserDialog, fname: str) -> None: |
| 66 | + """Open the file, and enable the import of the first two frames""" |
| 67 | + original_open(dialog, fname) |
| 68 | + tree = dialog.browser.tree |
| 69 | + item = tree.find_node_item(tree.topLevelItem(0), "/scan_0494/scan_data::frames") |
| 70 | + tree.get_node(item).set_view(NDView((1, 2), (0,), 0, 1)) |
| 71 | + tree.set_item_importable(item, True) |
| 72 | + |
| 73 | + monkeypatch.setattr(H5BrowserDialog, "open_file", open_and_enable_frames) |
| 74 | + with helpers.WorkdirRestoringTempDir() as tmpdir: |
| 75 | + nxsname = osp.join(tmpdir, "scan.nxs") |
| 76 | + _write_scan(nxsname) |
| 77 | + wsname = osp.join(tmpdir, "workspace.h5") |
| 78 | + with datalab_test_app_context(console=False, exec_loop=False) as win: |
| 79 | + win.h5inputoutput.import_files([nxsname], True, False) |
| 80 | + (counter,) = _nexus_objects(win.signalpanel, "counter") |
| 81 | + np.testing.assert_array_equal(counter.x, [-8.0, -7.5, -7.0]) |
| 82 | + assert counter.xunit == "deg" |
| 83 | + source = get_experimental_source(counter) |
| 84 | + assert source["details"] == {"entry_title": "LaB6 calibration"} |
| 85 | + (spectra,) = _nexus_objects(win.imagepanel, "spectra") |
| 86 | + assert spectra.data.shape == (3, 16) |
| 87 | + assert (spectra.y0, spectra.dy) == (-8.0, 0.5) |
| 88 | + frames = _nexus_objects(win.imagepanel, "frames") |
| 89 | + assert len(frames) == 2 |
| 90 | + assert frames[1].title.startswith("frames[1, :, :] (actuator_1_1=-7.5)") |
| 91 | + np.testing.assert_array_equal( |
| 92 | + frames[1].data, np.arange(20, 40).reshape(4, 5) |
| 93 | + ) |
| 94 | + win.save_h5_workspace(wsname) |
| 95 | + |
| 96 | + win.load_h5_workspace([wsname], reset_all=True) |
| 97 | + frame = _nexus_objects(win.imagepanel, "frames")[1] |
| 98 | + source = get_experimental_source(frame) |
| 99 | + assert source["selection"]["view"] == {"displayed": [1, 2], "indices": [1]} |
| 100 | + assert source["selection"]["positions"] == {"actuator_1_1": -7.5} |
| 101 | + json.dumps(frame.metadata["experimental_source"]) |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == "__main__": |
| 105 | + pytest.main([__file__]) |
0 commit comments