Example: Inspecting a NeXus file#

To demonstrate that the h5rdmtoolbox works with NeXus files, we download a file and inspect it. Note that by “works” we don’t mean that the data model of NeXus is understood by the toolbox. Rather, we want to demonstrate that we can identify specific data in the file, plot it, and apply a minimal example of convention and layout validation that we will quickly design here.

import h5rdmtoolbox as h5tbx

1. Get the file#

import requests
import pathlib

if not pathlib.Path('file.nxs').exists():
    r = requests.get('https://github.com/nexusformat/exampledata/raw/master/Soleil/hdf5/file_1.nxs')
    with open('file.nxs', 'wb') as f:
        f.write(r.content)

2. Visit (Dump) the file#

h5tbx.dump('file.nxs')
/home/docs/checkouts/readthedocs.org/user_builds/h5rdmtoolbox/checkouts/v2.2.1/h5rdmtoolbox/_repr.py:594: RuntimeWarning: Cannot decode attribute value for units
  warnings.warn(f'Cannot decode attribute value for {name}', RuntimeWarning)
    • HDF5_Version: 1.6.5
    • NeXus_version: {Version of NeXus API used in writing the file}
    • creator: Synchrotron SOLEIL
    • file_name: {File name of original NeXus file}
    • file_time: {Date and time of file creation}
    • file_update_time: {Date and time of last file change at close}
      • NX_class: NXentry
      • name: {Entry Name}
        : [|S7]
        • description: Name of entry DTD
        • version: 1.0
        (1) [int32]
        • description: Duration of measurement
        : [|S24]
        • description: Ending time of measurement
        : [|S26]
        (1) [int32]
        • description: Number of run or scan stored in this entry
        : [|S24]
        • description: Starting time of measurement
        : [|S8]
        • description: Extended title for entry
        • NX_class: NXinstrument
        • name: {Name of instrument}
          • NX_class: NXinsertion_device
          • name: {Name of insertion device}
            (1) [float64]
            • description: gap
            • timestamp: 2008-10-23T15-06-53.989
            • units: mm
          • NX_class: NXsource
          • name: source
            (1) [float64]
            • description: Accelerator current
            • timestamp: 2008-10-23T15-06-53.956
            • units: mA
            : [|S9]
            • description: synchrotron operating mode
            • timestamp: 2008-10-23T15-06-53.959
            : [|S6]
            • description: Name of source
            : [|S5]
            : [|S24]
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [float64]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-06-53.962
              • units: h
              : [|S82]
              • description: Data description
          • NX_class: NXdetector
          • name: {Name of detector bank}
            (1) [float64]
            • description: Dynamic Attribute from Publisher device
            • timestamp: 2008-10-23T15-06-55.778
            : [|S5]
            • description: Description of type such as He3 gas cylinder, He3 PSD, scintillator, fission chamber, proportio...
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [float64]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-09-38.425
              • units: ms
              : [|S79]
              • description: Data description
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [int32]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-09-38.441
              • units:
              : [|S87]
              • description: Data description
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [int32]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-09-38.464
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [int32]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-09-38.458
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [int32]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-09-38.476
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [int32]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-09-38.470
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [float64]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-09-38.432
              : [|S13]
              • description: Data description
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [float64]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-09-38.498
              • units: ms
              : [|S36]
              • description: Data description
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [int32]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-09-38.494
              : [|S92]
              • description: Data description
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [int32]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-09-38.447
              • units:
              : [|S125]
              • description: Data description
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [uint8]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-09-38.453
              : [|S69]
              • description: Data description
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [int32]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-09-38.482
              : [|S42]
              • description: Data description
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [int32]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-09-38.489
              : [|S40]
              • description: Data description
          • NX_class: NXsai_controller
          • name: {controller name}
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [float64]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-06-53.999
              • units: Hz
              : [|S29]
              • description: Data description
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [float64]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-06-54.002
              • units: ms
              : [|S23]
              • description: Data description
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [int32]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-06-54.005
              • units:
              : [|S148]
              • description: Data description
          • NX_class: NXsai_controller
          • name: {controller name}
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [float64]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-06-54.021
              • units: Hz
              : [|S29]
              • description: Data description
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [float64]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-06-54.031
              • units: ms
              : [|S23]
              • description: Data description
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [int32]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-06-54.041
              • units:
              : [|S148]
              • description: Data description
          • NX_class: NXsai_controller
          • name: {controller name}
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [float64]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-06-54.058
              • units: Hz
              : [|S29]
              • description: Data description
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [float64]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-06-54.068
              • units: ms
              : [|S23]
              • description: Data description
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [int32]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-06-54.096
              • units:
              : [|S148]
              • description: Data description
          • NX_class: NXmonochromator
          • name: name of monochromator
            (1) [float64]
            • description: energy selected
            • timestamp: 2008-10-23T15-06-54.136
            • units: keV
            (1) [float64]
            • description: wavelength selected
            • timestamp: 2008-10-23T15-06-54.129
            • units: b'\xc5'
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-06-54.147
            • units: mm
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-06-54.145
            • units: mm
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-09-50.618
            • units: mm
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-09-50.617
            • units: mm
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-06-54.158
            • units: mm
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-06-54.157
            • units: mm
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-09-50.628
            • units: mm
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-09-50.626
            • units: mm
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-06-54.176
            • units: mm
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-06-54.174
            • units: mm
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-09-50.639
            • units: mm
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-09-50.638
            • units: mm
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-06-54.209
            • units: mm
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-06-54.208
            • units: mm
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-09-50.650
            • units: mm
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-09-50.648
            • units: mm
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S23]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-06-54.217
            • units: mRad
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-06-54.217
            • units: mRad
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S23]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-06-54.226
            • units: mm
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-06-54.225
            • units: mm
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S23]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-06-54.235
            • units: mRad
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-06-54.234
            • units: mRad
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S23]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-06-54.245
            • units: mm
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-06-54.244
            • units: mm
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-06-54.258
            • units: mm
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-06-54.256
            • units: mm
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-09-50.660
            • units: mm
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-09-50.658
            • units: mm
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-06-54.268
            • units: mm
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-06-54.267
            • units: mm
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-09-50.670
            • units: mm
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-09-50.668
            • units: mm
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-06-54.326
            • units: mm
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-06-54.325
            • units: mm
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-09-50.682
            • units: mm
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-09-50.680
            • units: mm
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-06-54.343
            • units: mm
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-06-54.341
            • units: mm
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-09-50.740
            • units: mm
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-09-50.704
            • units: mm
          • NX_class: NXintensity_monitor
          • name: {monitor name}
            (10) [float64]
            • description: average intensity
            • timestamp: 2008-10-23T15-09-49.712
            • units: A
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [float64]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-09-49.500
              • units: A
              : [|S91]
              • description: Data description
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [float64]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-09-49.490
              • units:
              : [|S46]
              • description: Data description
            • NX_class: NXtechnical_data
            • name: {Data name}
              : [|S37]
              • description: Data values
              • long_name: {Title of technical data}
              : [|S49]
              • description: Data description
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [float64]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-09-49.503
              • units: V
              : [|S26]
              • description: Data description
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-06-54.353
            • units: User Unit
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-06-54.352
            • units: User Unit
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-09-50.769
            • units: User Unit
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-09-50.767
            • units: User Unit
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-06-54.379
            • units: User Unit
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-06-54.378
            • units: User Unit
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-09-50.780
            • units: User Unit
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-09-50.778
            • units: User Unit
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-06-54.409
            • units: User Unit
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-06-54.407
            • units: User Unit
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-09-50.790
            • units: User Unit
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-09-50.788
            • units: User Unit
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-06-54.425
            • units: User Unit
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-06-54.422
            • units: User Unit
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-09-50.799
            • units: User Unit
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-09-50.798
            • units: User Unit
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-06-54.437
            • units: User Unit
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-06-54.435
            • units: User Unit
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-09-50.808
            • units: User Unit
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-09-50.807
            • units: User Unit
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-06-54.451
            • units: User Unit
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-06-54.449
            • units: User Unit
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-09-50.818
            • units: User Unit
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-09-50.816
            • units: User Unit
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-06-54.461
            • units: User Unit
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-06-54.459
            • units: User Unit
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-09-50.827
            • units: User Unit
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-09-50.825
            • units: User Unit
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-06-54.471
            • units: User Unit
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-06-54.470
            • units: User Unit
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-09-50.836
            • units: User Unit
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-09-50.835
            • units: User Unit
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-06-54.499
            • units: User Unit
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-06-54.497
            • units: User Unit
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-06-54.524
            • units: User Unit
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-06-54.522
            • units: User Unit
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S24]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-06-54.487
            • units: User Unit
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-06-54.485
            • units: mm
          • NX_class: NXdetector
          • name: {Name of detector bank}
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [float64]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-09-38.425
              • units: ms
              : [|S79]
              • description: Data description
          • NX_class: NXdetector
          • name: {Name of detector bank}
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [float64]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-06-55.540
              • units: ms
              : [|S79]
              • description: Data description
          • NX_class: NXintensity_monitor
          • name: {monitor name}
            (10) [float64]
            • description: average intensity
            • timestamp: 2008-10-23T15-09-50.343
            • units: A
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [float64]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-09-50.060
              • units: A
              : [|S91]
              • description: Data description
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [float64]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-09-49.963
              • units:
              : [|S46]
              • description: Data description
            • NX_class: NXtechnical_data
            • name: {Data name}
              : [|S38]
              • description: Data values
              • long_name: {Title of technical data}
              : [|S50]
              • description: Data description
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [float64]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-09-50.174
              • units: V
              : [|S26]
              • description: Data description
          • NX_class: NXintensity_monitor
          • name: {monitor name}
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [float64]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-06-55.642
              • units: A
              : [|S91]
              • description: Data description
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [float64]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-06-55.631
              • units:
              : [|S46]
              • description: Data description
            • NX_class: NXtechnical_data
            • name: {Data name}
              : [|S38]
              • description: Data values
              • long_name: {Title of technical data}
              : [|S50]
              • description: Data description
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [float64]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-06-55.646
              • units: V
              : [|S26]
              • description: Data description
          • NX_class: NXintensity_monitor
          • name: {monitor name}
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [float64]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-06-55.668
              • units: A
              : [|S91]
              • description: Data description
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [float64]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-06-55.657
              • units:
              : [|S46]
              • description: Data description
            • NX_class: NXtechnical_data
            • name: {Data name}
              : [|S37]
              • description: Data values
              • long_name: {Title of technical data}
              : [|S49]
              • description: Data description
            • NX_class: NXtechnical_data
            • name: {Data name}
              (1) [float64]
              • description: Data values
              • long_name: {Title of technical data}
              • timestamp: 2008-10-23T15-06-55.671
              • units: V
              : [|S26]
              • description: Data description
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S24]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-06-55.686
            • units: User Unit
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-06-55.684
            • units: User Unit
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S24]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-06-55.695
            • units: User Unit
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-06-55.694
            • units: User Unit
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S24]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-06-55.706
            • units: User Unit
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-06-55.704
            • units: User Unit
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S24]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-06-55.714
            • units: User Unit
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-06-55.713
            • units: User Unit
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S24]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-06-55.807
            • units: User Unit
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-06-55.805
            • units: User Unit
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S24]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-06-55.819
            • units: User Unit
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-06-55.817
            • units: mm
          • NX_class: NXpositioner
          • name: {Name of positioner}
            : [|S26]
            • description: Hardware device record, e.g. EPICS process variable, taco/tango ...
            (1) [float64]
            • description: offset added to values to meet user's needings
            • timestamp: 2008-10-23T15-06-55.828
            • units: User Unit
            (1) [float64]
            • description: raw value of positioner - need [n] as may be scanned
            • timestamp: 2008-10-23T15-06-55.826
            • units: User Unit
        • NX_class: NXsample
        • name: {name of the sample}
          • NX_class: NXenvironment
          • name: {Name of sample environment}
            • NX_class: NXsensor
            • name: {Name of sensor}
              : [|S11]
              • description: what we measure "temperature | pH | magnetic_field | electric field | conductivity | resistance ...
              : [|S17]
              • description: Sensor identification code/model number
              : [|S25]
              • description: Name for the sensor
              • NX_class: NXtechnical_data
              • name: {Data name}
                (1) [float64]
                • description: Data values
                • long_name: {Title of technical data}
                • timestamp: 2008-10-23T15-06-55.523
                • units: b'\xb0C'
                : [|S26]
                • description: Data description
              • NX_class: NXtechnical_data
              • name: {Data name}
                (1) [float64]
                • description: Data values
                • long_name: {Title of technical data}
                • timestamp: 2008-10-23T15-06-55.412
                • units: b'\xb0C'
                : [|S26]
                • description: Data description
              • NX_class: NXtechnical_data
              • name: {Data name}
                (1) [float64]
                • description: Data values
                • long_name: {Title of technical data}
                • timestamp: 2008-10-23T15-06-55.525
                • units: b'\xb0C'
                : [|S50]
                • description: Data description
        • NX_class: NXuser
          : [|S40]
          • description: Address of user
          : [|S46]
          • description: Affiliation of user
          : [|S10]
          • description: Email of user
          : [|S12]
          • description: Fax number of user
          : [|S11]
          • description: Name of user responsible for this entry
          : [|S8]
          • description: role of user responsible for this entry
          : [|S13]
          • description: Telephone number of user
        • NX_class: NXnote
        • name: {name of note}
          : [|S10]
          • description: Binary note data - if text, line terminator is \r\n.
          : [|S10]
          • description: Mime content type of note data field e.g. image/jpeg, text/plain, text/html
        • NX_class: NXnote
        • name: {name of note}
          : [|S268]
          • description: Binary note data - if text, line terminator is \r\n.
          : [|S10]
          • description: Mime content type of note data field e.g. image/jpeg, text/plain, text/html
        • NX_class: NXdata
        • name: {Name of data block}
          (1024, 1024) [uint16]
          • description: Read : selected Image (selectFrame and selectROI)
          • long_name: image_data
          • signal: 1
          • timestamp: 2008-10-23T15-09-39.175
        • NX_class: NXdata
        • name: {Name of data block}
          (1024, 1024) [uint16]
          • description: Read : selected Image (selectFrame and selectROI)
          • long_name: image_data
          • signal: 1
          • timestamp: 2008-10-23T15-09-40.333
        • NX_class: NXdata
        • name: {Name of data block}
          (1024, 1024) [uint16]
          • description: Read : selected Image (selectFrame and selectROI)
          • long_name: image_data
          • signal: 1
          • timestamp: 2008-10-23T15-09-41.365
        • NX_class: NXdata
        • name: {Name of data block}
          (1024, 1024) [uint16]
          • description: Read : selected Image (selectFrame and selectROI)
          • long_name: image_data
          • signal: 1
          • timestamp: 2008-10-23T15-09-42.397
        • NX_class: NXdata
        • name: {Name of data block}
          (1024, 1024) [uint16]
          • description: Read : selected Image (selectFrame and selectROI)
          • long_name: image_data
          • signal: 1
          • timestamp: 2008-10-23T15-09-43.600
        • NX_class: NXdata
        • name: {Name of data block}
          (1024, 1024) [uint16]
          • description: Read : selected Image (selectFrame and selectROI)
          • long_name: image_data
          • signal: 1
          • timestamp: 2008-10-23T15-09-44.639
        • NX_class: NXdata
        • name: {Name of data block}
          (1024, 1024) [uint16]
          • description: Read : selected Image (selectFrame and selectROI)
          • long_name: image_data
          • signal: 1
          • timestamp: 2008-10-23T15-09-45.788
        • NX_class: NXdata
        • name: {Name of data block}
          (1024, 1024) [uint16]
          • description: Read : selected Image (selectFrame and selectROI)
          • long_name: image_data
          • signal: 1
          • timestamp: 2008-10-23T15-09-46.823
        • NX_class: NXdata
        • name: {Name of data block}
          (1024, 1024) [uint16]
          • description: Read : selected Image (selectFrame and selectROI)
          • long_name: image_data
          • signal: 1
          • timestamp: 2008-10-23T15-09-47.887
        • NX_class: NXdata
        • name: {Name of data block}
          (1024, 1024) [uint16]
          • description: Read : selected Image (selectFrame and selectROI)
          • long_name: image_data
          • signal: 1
          • timestamp: 2008-10-23T15-09-48.922
        • NX_class: NXsample
        • name: {name of the sample}
          • NX_class: NXnote
          • name: {name of note}
            : [|S18]
            • description: Binary note data - if text, line terminator is \r\n.
            : [|S10]
            • description: Mime content type of note data field e.g. image/jpeg, text/plain, text/html

3. Find and plot data#

obj = h5tbx.database.FileDB('file.nxs').find_one({'$name': {'$basename': 'image#5'}},   
                                                 recursive=True)
obj
<LGroup "/NDX3_00021_ND__DLPC__00001/image#5" in "file.nxs">
obj.data[()].plot()  # note, we can access children (e.g. dataset "data") of the (lazy) group
<matplotlib.collections.QuadMesh at 0x731dbef319c0>
../_images/29ebd1f05592b6f9963e76ae4740d368f7b8af2dea42e9eaa74450e9234471ec.png

4. Write a standard attribute for timestamp#

If you carefully check the dump output, you will find, that all numeric (!) datasets have the attribute “timestamp”. All of them? Well, let’s write a convention, which defines that providing the attribute “timestamp” with every numeric dataset is required. The timestamp value must have a specific format.

Let’s create the standard name first:

4.1 Standard Name “timestamp”#

from h5rdmtoolbox.convention import StandardAttribute

from pydantic import BaseModel, ValidationError
from pydantic.functional_validators import AfterValidator, Annotated
from datetime import datetime
from typing import Union

def parse_datetime(v: Union[str, datetime]) -> str:
    """validation function used by the pydantic model class"""
    _format = '%Y-%m-%dT%H-%M-%S.%f'
    if isinstance(v, datetime):
        return v.strftime(_format)
    return datetime.strptime(v, _format).strftime(_format)

class NexusTimeStampObject(BaseModel):
    """The standard name table of the convention."""
    value: Annotated[str, AfterValidator(parse_datetime)]

NexusTimeStamp = StandardAttribute('timestamp',
                                   validator=NexusTimeStampObject,
                                   target_method='create_dataset',
                                   description='Timestamp of writing the data',
                                  default_value='$EMPTY')

4.2 Build the convention#

from h5rdmtoolbox.convention import Convention

cv = Convention(name='nexus', contact=h5tbx.__author__)

cv.add_standard_attribute(NexusTimeStamp)
cv.register()

h5tbx.use(cv)
using("nexus")

First test the convention on a temporary file:

with h5tbx.File() as h5:
    h5.create_dataset('test', data=4, timestamp='2008-10-23T15-09-39.175')
    h5.dump()
      4 [int64]
      • timestamp: 2008-10-23T15-09-39.175000
# should raise an error due to NO timestamp
with h5tbx.File() as h5:
    try:
        h5.create_dataset('test', data=4)
    except h5tbx.errors.StandardAttributeError as e:
        print(e)
Convention "nexus" expects standard attribute "timestamp" to be provided as an argument during dataset creation.
# should raise an error due to a wrong timestamp
with h5tbx.File() as h5:
    try:
        h5.create_dataset('test', data=4, timestamp='2008 10 23 15-09-39.175')
    except h5tbx.errors.StandardAttributeError as e:
        print(e)
Validation of "2008 10 23 15-09-39.175" for standard attribute "timestamp" failed.
Expected fields: {'value': FieldInfo(annotation=str, required=True, metadata=[AfterValidator(func=<function parse_datetime at 0x731dbef8f130>)])}
Pydantic error: 1 validation error for NexusTimeStampObject
value
  Value error, time data '2008 10 23 15-09-39.175' does not match format '%Y-%m-%dT%H-%M-%S.%f' [type=value_error, input_value='2008 10 23 15-09-39.175', input_type=str]
    For further information visit https://errors.pydantic.dev/2.10/v/value_error

4.3 Validate the NeXus file based on the convention:#

We find, that two datasets in fact have no timestamp!

invalid_objs = cv.validate('file.nxs')
invalid_objs
[MissingAttribute(/NDX3_00021_ND__DLPC__00001/duration, timestamp),
 MissingAttribute(/NDX3_00021_ND__DLPC__00001/run_number, timestamp)]

Let’s convince ourselves:

obj = h5tbx.database.FileDB('file.nxs').find_one({'$name': invalid_objs[0].object_name})
obj.attrs.get('timestamp', 'missing!!!')
'missing!!!'
h5tbx.use(None)
using("h5py")

5. Validating the file structure with a layout definition#

The structure or layout defines which groups, datasets and attributes need to exist and in which arrangement. This cannot be defined/checked during file creation (using standard attributes).

Let’s define part of a potential layout for the NeXus file in words first and then write the code:

  1. Ten datasets starting with “image#” and ending with a number between 0 and 9

  2. The group user must exist, which has the datasets “name” with the attribute “description”

The layout is defined by specifying queries, which correspond to what we are expecting in the file. If it is not found, the layout specification failed:

from h5rdmtoolbox import layout

lay = layout.Layout()

# 1.) 10 images:
spec_10images = lay.add(h5tbx.database.FileDB.find,
                      flt={'$name': {'$regex': '.*/image#[0-9]$'}},
                      n=10)

# 2.) User group. Identify by attribute "NX_class":
spec_userGroupExists = lay.add(h5tbx.database.FileDB.find,
                               flt={'NX_class': b'NXuser'},
                               n=None)

# within the User group, find group "name":
spec_UserName = spec_userGroupExists.add(
    h5tbx.database.ObjDB.find_one,
    flt={'$name': {'$basename': 'name'}},
    n=1
)

# within the Name group find dataset "name" with a certain "description":
spec_UserNameDescription = spec_UserName.add(
    h5tbx.database.ObjDB.find_one,
    flt={'$name': {'$basename': 'name'}, 'description': b'Name of user responsible for this entry'},  # !binary string!
    n=1
)

Validate the file:

res = lay.validate('file.nxs')
res.is_valid()
True