Skip to content

Map DATATYPE_INT images to int32 and reject unsupported dtypes clearly - #101

Open
inhuszar wants to merge 2 commits into
ismrmrd:masterfrom
inhuszar:fix/int32-int64-images
Open

inhuszar wants to merge 2 commits into
ismrmrd:masterfrom
inhuszar:fix/int32-int64-images

Conversation

@inhuszar

Copy link
Copy Markdown

ismrmrd/image.py mapped DATATYPE_INT to np.dtype('int'), which is the platform long: int64 on Linux and macOS.
The MRD specification and the C library define ISMRMRD_INT (4) as int32_t, and hdf5.py already stores INT
image data as '<i4'. This had three effects:

  • writing an int32 image raised TypeError: Datatype not supported: int32;
  • an int64 array was accepted as INT and then viewed as '<i4' with twice the elements, which failed with a broadcast
    error;
  • every INT image, including files written by the C++ library, was read back as int64.

This PR maps DATATYPE_INT to int32. It also makes both Image constructors go through
get_data_type_from_dtype(), so an unsupported dtype (int64, uint64, int8, float16) raises a TypeError that lists
the supported types instead of a KeyError or a broadcast error. MRD has no 64-bit integer type.

Tests: a round trip through from_array, the header data_type, and HDF5 write and read for all eight MRD image types,
plus a test of the error for unsupported dtypes. On master, 7 of the new tests fail; with this PR the suite passes
(125 passed). CHANGELOG entries are under "Unreleased".

inhuszar and others added 2 commits November 12, 2026 15:37
The image dtype table mapped DATATYPE_INT to np.dtype('int'), which is
the platform C long: int64 on Linux and macOS under NumPy 2. ISMRMRD
defines DATATYPE_INT as int32_t, and the HDF5 writer already stored it
as '<i4'. As a result Image.from_array() refused int32 arrays, accepted
int64 arrays as DATATYPE_INT and then failed in Dataset.append_image()
with a broadcast error (the int64 buffer viewed as '<i4' has twice as
many elements), and every DATATYPE_INT image, including files written
by the C++ library, was read back as int64.

Map DATATYPE_INT to int32. The new tests check, for each of the eight
MRD image data types, that from_array() sets the right data_type, that
a header's data_type gives the right dtype, and that an image survives
an HDF5 write and read with the same dtype and values. The int32 cases
fail before this change and the full suite passes after it.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
ISMRMRD defines eight image data types (uint16, int16, uint32, int32,
float32, float64, complex64, complex128) and no 64-bit or 8-bit
integer type. Image.from_array() rejected other dtypes with a message
that named only the offending dtype, and Image(data=...) looked the
dtype up directly in the inverse table and raised a bare KeyError.

Route both through get_data_type_from_dtype() and list the supported
dtypes in its message. The new test checks that int64, uint64, int8
and float16 arrays raise a TypeError naming the supported types from
both constructors. It fails before this change (the message did not
name the supported types, and Image(data=...) raised KeyError) and the
full suite passes after it.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant