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)

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

reader_attributes_trail.py

h5py code to read NeXus HDF5 file and find plottable data

nexusformat/reader_attributes_trail.py

nexusformat code to read NeXus HDF5 file and find plottable data