diff --git a/CHANGELOG.md b/CHANGELOG.md index 884f3f4..58dffc7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,19 @@ # 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`. +- `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) ### Breaking changes diff --git a/ismrmrd/image.py b/ismrmrd/image.py index b4ecb27..6c5db4a 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'), @@ -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_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..ab28468 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -136,3 +136,45 @@ 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 + + +@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)