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

 1 from pathlib import Path
 2 from nexusformat.nexus import nxopen
 3 
 4 filename = str(
 5     Path(__file__).absolute().parent.parent
 6     / "simple_example_basic"
 7     / "simple_example_basic.nexus.hdf5"
 8 )
 9 with 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 
15 nx_data.plot() # plot the data using Matplotlib
16 
17 print(f"file: {f.nxfilename}")
18 print(f"signal: {signal.nxname}")
19 print(f"axes: {axes.nxname}")
20 print(f"{axes.nxname} {signal.nxname}")
21 for 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

 1 file: simple_example_basic.nexus.hdf5
 2 signal: /entry/mr_scan/I00
 3 axes: /entry/mr_scan/mr
 4 /entry/mr_scan/mr /entry/mr_scan/I00
 5 17.92608 1037
 6 17.92591 1318
 7 17.92575 1704
 8 17.92558 2857
 9 17.92541 4516
10 17.92525 9998
11 17.92508 23819
12 17.92491 31662
13 17.92475 40458
14 17.92458 49087
15 17.92441 56514
16 17.92425 63499
17 17.92408 66802
18 17.92391 66863
19 17.92375 66599
20 17.92358 66206
21 17.92341 65747
22 17.92325 65250
23 17.92308 64129
24 17.92291 63044
25 17.92275 60796
26 17.92258 56795
27 17.92241 51550
28 17.92225 43710
29 17.92208 29315
30 17.92191 19782
31 17.92175 12992
32 17.92158 6622
33 17.92141 4198
34 17.92125 2248
35 17.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