From df86ebdc9b7e925e06a5d2cf315486d0cca625c6 Mon Sep 17 00:00:00 2001 From: Istvan N Huszar Date: Thu, 12 Nov 2026 15:37:35 -0500 Subject: [PATCH 1/2] Map DATATYPE_INT images to int32 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 ' --- CHANGELOG.md | 10 ++++++++++ ismrmrd/image.py | 2 +- tests/test_hdf5.py | 22 ++++++++++++++++++++++ tests/test_image.py | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 884f3f4..3caca60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ # Changelog +## Unreleased + +### Bug fixes + +- Images of type `DATATYPE_INT` now use `int32`, as the ISMRMRD format + defines, instead of the platform `int` (`int64` on Linux and macOS). + `int32` arrays were refused by `Image.from_array()`, `int64` arrays were + labelled `DATATYPE_INT` and then failed in `Dataset.append_image()`, and + `DATATYPE_INT` images were read back as `int64`. + ## v1.15.0 (unreleased) ### Breaking changes diff --git a/ismrmrd/image.py b/ismrmrd/image.py index b4ecb27..b10b771 100644 --- a/ismrmrd/image.py +++ b/ismrmrd/image.py @@ -18,7 +18,7 @@ DATATYPE_USHORT: np.dtype('uint16'), DATATYPE_SHORT: np.dtype('int16'), DATATYPE_UINT: np.dtype('uint32'), - DATATYPE_INT: np.dtype('int'), + DATATYPE_INT: np.dtype('int32'), DATATYPE_FLOAT: np.dtype('float32'), DATATYPE_DOUBLE: np.dtype('float64'), DATATYPE_CXFLOAT: np.dtype('complex64'), diff --git a/tests/test_hdf5.py b/tests/test_hdf5.py index e3a6bbe..11c188d 100644 --- a/tests/test_hdf5.py +++ b/tests/test_hdf5.py @@ -80,6 +80,28 @@ def test_read_and_write_images_to_hdf5(): compare_images(img_a, img_b) +@pytest.mark.parametrize('dtype', [np.uint16, np.int16, np.uint32, np.int32, + np.float32, np.float64, + np.complex64, np.complex128]) +def test_read_and_write_image_data_types_to_hdf5(dtype): + filename = os.path.join(temp_dir, 'read_write_image_data_types.h5') + + image_data = np.arange(24, dtype=dtype).reshape(2, 3, 4) + image = ismrmrd.Image.from_array(image_data) + + dataset = ismrmrd.Dataset(filename) + dataset.append_image('images', image) + dataset.close() + + dataset = ismrmrd.Dataset(filename) + read_image = dataset.read_image('images', 0) + dataset.close() + + assert read_image.data_type == image.data_type + assert read_image.data.dtype == dtype + assert np.array_equal(read_image.data.squeeze(axis=0), image_data) + + def test_read_and_write_waveforms_to_hdf5(): filename = os.path.join(temp_dir, 'read_write_waveforms.h5') diff --git a/tests/test_image.py b/tests/test_image.py index d60e28c..26893cd 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -136,3 +136,37 @@ def test_serialization_with_header_fields(): def test_serialization_from_too_few_bytes(): with pytest.raises(ValueError): ismrmrd.Image.from_bytes(b'') + + +mrd_image_data_types = [ + (ismrmrd.DATATYPE_USHORT, np.uint16), + (ismrmrd.DATATYPE_SHORT, np.int16), + (ismrmrd.DATATYPE_UINT, np.uint32), + (ismrmrd.DATATYPE_INT, np.int32), + (ismrmrd.DATATYPE_FLOAT, np.float32), + (ismrmrd.DATATYPE_DOUBLE, np.float64), + (ismrmrd.DATATYPE_CXFLOAT, np.complex64), + (ismrmrd.DATATYPE_CXDOUBLE, np.complex128), +] + + +@pytest.mark.parametrize('data_type, dtype', mrd_image_data_types) +def test_from_array_sets_data_type(data_type, dtype): + image_data = np.arange(6, dtype=dtype).reshape(1, 2, 3) + image = ismrmrd.Image.from_array(image_data) + assert image.data_type == data_type + assert image.data.dtype == dtype + assert np.array_equal(image_data, image.data.squeeze(axis=0)) + + +@pytest.mark.parametrize('data_type, dtype', mrd_image_data_types) +def test_header_data_type_sets_dtype(data_type, dtype): + head = ismrmrd.ImageHeader() + head.data_type = data_type + head.channels = 1 + head.matrix_size[0] = 3 + head.matrix_size[1] = 2 + head.matrix_size[2] = 1 + image = ismrmrd.Image(head) + assert image.data.dtype == dtype + From c2704ce60df95bb7d5317aa35da0afe59c7a43f4 Mon Sep 17 00:00:00 2001 From: Istvan N Huszar Date: Thu, 12 Nov 2026 15:39:57 -0500 Subject: [PATCH 2/2] Raise a clear TypeError for unsupported image dtypes 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) --- CHANGELOG.md | 4 ++++ ismrmrd/image.py | 6 ++++-- tests/test_image.py | 8 ++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3caca60..58dffc7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,10 @@ `int32` arrays were refused by `Image.from_array()`, `int64` arrays were labelled `DATATYPE_INT` and then failed in `Dataset.append_image()`, and `DATATYPE_INT` images were read back as `int64`. +- `Image.from_array()` and `Image(data=...)` now raise a `TypeError` that + lists the supported dtypes when given an array type that ISMRMRD images do + not define, such as `int64`. `Image(data=...)` used to raise a bare + `KeyError`. ## v1.15.0 (unreleased) diff --git a/ismrmrd/image.py b/ismrmrd/image.py index b10b771..6c5db4a 100644 --- a/ismrmrd/image.py +++ b/ismrmrd/image.py @@ -37,7 +37,9 @@ def get_dtype_from_data_type(val): def get_data_type_from_dtype(dtype): type = inverse_dtype_mapping.get(dtype) if type is None: - raise TypeError("Datatype not supported: " + str(dtype)) + supported = ", ".join(str(d) for d in dtype_mapping.values()) + raise TypeError("Datatype not supported: " + str(dtype) + + ". ISMRMRD images support: " + supported) return type @@ -250,7 +252,7 @@ def __init__(self, head=None, attribute_string=None, meta=None, data=None): def create_consistent_header(header, data): if data is not None: - header.data_type = inverse_dtype_mapping[data.dtype] + header.data_type = get_data_type_from_dtype(data.dtype) header.channels = data.shape[0] header.matrix_size = (data.shape[3], data.shape[2], data.shape[1]) diff --git a/tests/test_image.py b/tests/test_image.py index 26893cd..ab28468 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -170,3 +170,11 @@ def test_header_data_type_sets_dtype(data_type, dtype): image = ismrmrd.Image(head) assert image.data.dtype == dtype + +@pytest.mark.parametrize('dtype', [np.int64, np.uint64, np.int8, np.float16]) +def test_unsupported_dtype_raises(dtype): + image_data = np.arange(6, dtype=dtype).reshape(1, 2, 3) + with pytest.raises(TypeError, match='int32'): + ismrmrd.Image.from_array(image_data) + with pytest.raises(TypeError, match='int32'): + ismrmrd.Image(data=image_data)