Skip to content

Allow read-only h5 access, define IMTYPE_RGB, explicit error on too-large Waveforms#87

Open
kspaceKelvin wants to merge 6 commits into
ismrmrd:masterfrom
kspaceKelvin:master
Open

Allow read-only h5 access, define IMTYPE_RGB, explicit error on too-large Waveforms#87
kspaceKelvin wants to merge 6 commits into
ismrmrd:masterfrom
kspaceKelvin:master

Conversation

@kspaceKelvin
Copy link
Copy Markdown
Contributor

Comment thread ismrmrd/constants.py
IMTYPE_REAL = 3
IMTYPE_IMAG = 4
IMTYPE_COMPLEX = 5
IMTYPE_RGB = 6
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see where this is defined in the C++ implementation?

What does RGB mean for each of the possible ISMRMRD image types? i.e. How is RGB decoded from a 32-bit complex float? How many bits for each RGB channel if the image is 32-bit vs 16-bit?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this in the docs now (https://ismrmrd.readthedocs.io/en/latest/mrd_image_data.html#image-types).

But I still don't understand. How long has this been documented without actually being implemented in ISMRMRD or ismrmrd-python?

Comment thread ismrmrd/hdf5.py

class Dataset(object):
def __init__(self, filename, dataset_name="dataset", create_if_needed=True):
def __init__(self, filename, dataset_name="dataset", create_if_needed=True, mode=None):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should document the valid arguments for mode: https://docs.h5py.org/en/stable/high/file.html#opening-creating-files

Comment thread ismrmrd/waveform.py
channels, nsamples = data.shape

if nsamples > np.iinfo(np.uint16).max:
raise TypeError(f"Array has {nsamples} samples, which is greater than the maximum of {np.iinfo(np.uint16).max}")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a ValueError not a TypeError.

@naegelejd
Copy link
Copy Markdown
Contributor

I'm happy to make the suggested changes and merge this 👍

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the Python ISMRMRD helpers to (1) support more explicit control over how HDF5 files are opened, (2) expose the missing IMTYPE_RGB image type constant, and (3) fail fast when constructing Waveforms that exceed header representable sizes.

Changes:

  • Add an optional mode parameter to ismrmrd.hdf5.Dataset to control the h5py.File(...) open mode.
  • Define IMTYPE_RGB = 6 in ismrmrd.constants.
  • Add an explicit error in Waveform.from_array when number_of_samples exceeds the uint16 maximum.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
ismrmrd/waveform.py Adds a size guard when building Waveforms from numpy arrays.
ismrmrd/hdf5.py Extends Dataset to accept an explicit HDF5 open mode.
ismrmrd/constants.py Adds the missing IMTYPE_RGB constant.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread ismrmrd/waveform.py
Comment on lines 70 to +74
channels, nsamples = data.shape

if nsamples > np.iinfo(np.uint16).max:
raise TypeError(f"Array has {nsamples} samples, which is greater than the maximum of {np.iinfo(np.uint16).max}")

Comment thread ismrmrd/waveform.py
Comment on lines +72 to +74
if nsamples > np.iinfo(np.uint16).max:
raise TypeError(f"Array has {nsamples} samples, which is greater than the maximum of {np.iinfo(np.uint16).max}")

Comment thread ismrmrd/hdf5.py
Comment on lines +147 to +155
def __init__(self, filename, dataset_name="dataset", create_if_needed=True, mode=None):
# Open the file
if create_if_needed:
self._file = h5py.File(filename, 'a')
else:
self._file = h5py.File(filename, 'r+')
if mode is None:
if create_if_needed:
mode = 'a'
else:
mode = 'r+'

self._file = h5py.File(filename, mode)
Comment thread ismrmrd/hdf5.py
Comment on lines +147 to +155
def __init__(self, filename, dataset_name="dataset", create_if_needed=True, mode=None):
# Open the file
if create_if_needed:
self._file = h5py.File(filename, 'a')
else:
self._file = h5py.File(filename, 'r+')
if mode is None:
if create_if_needed:
mode = 'a'
else:
mode = 'r+'

self._file = h5py.File(filename, mode)
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.

3 participants