Find plottable data in a NeXus HDF5 file¶
Let’s make a new reader that follows the chain of
attributes (@default
, @signal
, and @axes
)
to find the default plottable data. We’ll use the
same data file as the previous example.
Our demo here assumes one-dimensional data.
(For higher dimensionality data,
we’ll need more complexity when handling the
@axes
attribute and we’ll to check the
field sizes. See section Find the plottable data,
subsection Version 3, for the details.)
reader_attributes_trail.py: Read a NeXus HDF5 file using Python
1from pathlib import Path
2from nexusformat.nexus import nxopen
3
4filename = str(
5 Path(__file__).absolute().parent.parent
6 / "simple_example_basic"
7 / "simple_example_basic.nexus.hdf5"
8)
9with nxopen(filename) as f:
10 # find the default NXdata group
11 nx_data = f.get_default()
12 signal = nx_data.nxsignal
13 axes = nx_data.nxaxes[0]
14
15nx_data.plot() # plot the data using Matplotlib
16
17print(f"file: {f.nxfilename}")
18print(f"signal: {signal.nxname}")
19print(f"axes: {axes.nxname}")
20print(f"{axes.nxname} {signal.nxname}")
21for x, y in zip(axes, signal):
22 print(x, y)
1from pathlib import Path
2import h5py
3
4filename = str(
5 Path(__file__).absolute().parent.parent
6 / "simple_example_basic"
7 / "simple_example_basic.nexus.hdf5"
8)
9with h5py.File(filename, "r") as nx:
10 # find the default NXentry group
11 nx_entry = nx[nx.attrs["default"]]
12 # find the default NXdata group
13 nx_data = nx_entry[nx_entry.attrs["default"]]
14 # find the signal field
15 signal = nx_data[nx_data.attrs["signal"]]
16 # find the axes field(s)
17 attr_axes = nx_data.attrs["axes"]
18 if isinstance(attr_axes, (set, tuple, list)):
19 # but check that attr_axes only describes 1-D data
20 if len(attr_axes) == 1:
21 attr_axes = attr_axes[0]
22 else:
23 raise ValueError(f"expected 1-D data but @axes={attr_axes}")
24 axes = nx_data[attr_axes]
25
26 print(f"file: {nx.filename}")
27 print(f"signal: {signal.name}")
28 print(f"axes: {axes.name}")
29 print(f"{axes.name} {signal.name}")
30 for x, y in zip(axes, signal):
31 print(x, y)
Output from reader_attributes_trail.py
is shown next.
Output from reader_attributes_trail.py
1file: simple_example_basic.nexus.hdf5
2signal: /entry/mr_scan/I00
3axes: /entry/mr_scan/mr
4/entry/mr_scan/mr /entry/mr_scan/I00
517.92608 1037
617.92591 1318
717.92575 1704
817.92558 2857
917.92541 4516
1017.92525 9998
1117.92508 23819
1217.92491 31662
1317.92475 40458
1417.92458 49087
1517.92441 56514
1617.92425 63499
1717.92408 66802
1817.92391 66863
1917.92375 66599
2017.92358 66206
2117.92341 65747
2217.92325 65250
2317.92308 64129
2417.92291 63044
2517.92275 60796
2617.92258 56795
2717.92241 51550
2817.92225 43710
2917.92208 29315
3017.92191 19782
3117.92175 12992
3217.92158 6622
3317.92141 4198
3417.92125 2248
3517.92108 1321
downloads¶
The Python code and files related to this section may be downloaded from the following table.
file |
description |
---|---|
h5py code to read NeXus HDF5 file and find plottable data |
|
nexusformat code to read NeXus HDF5 file and find plottable data |