Skip to content

Error reading metadata with Numpy 2.4 #74

@takluyver

Description

@takluyver

Some files contain metadata fields, such as NXDetector's bit_depth_readout, in a 1D, length-1 dataset instead of a scalar. These could previously be read OK, but Numpy 2.4 is stricter about converting to a scalar, so code has started failing on files that worked before.

import sys

import h5py
import nxmx

with h5py.File(sys.argv[1]) as f:
    nxmx_data = nxmx.NXmx(f)
    nxdet = nxmx_data.entries[0].instruments[0].detectors[0]
    nxdet.bit_depth_readout
Traceback (most recent call last):
  File "/home/kluyvert/Code/instrument-geometry-info/Tools/nxmx-test.py", line 9, in <module>
    nxdet.bit_depth_readout
  File "/home/kluyvert/.conda/envs/dxtbx/lib/python3.11/functools.py", line 1001, in __get__
    val = self.func(instance)
          ^^^^^^^^^^^^^^^^^^^
  File "/home/kluyvert/.conda/envs/dxtbx/lib/python3.11/site-packages/nxmx/__init__.py", line 887, in bit_depth_readout
    return int(self._handle["bit_depth_readout"][()])
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: only 0-dimensional arrays can be converted to Python scalars

The file I'm looking at is available from this Zenodo entry - the 'master' file, although the field in question is an external link pointing to the meta file.

I'm not sure if using a length-1 dataset instead of a scalar is strictly valid, but it worked until now, and neither of the Nexus validation tools I tried told me that it's wrong, so it seems reasonable to allow it. I encountered this through dxtbx, which tries to accommodate all the quirks of how real files get written, so if this is out of scope for nxmx, I can open an issue on that project instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions