Skip to content

Added proper handling of nifti RGB formats#46

Open
baishi wants to merge 2 commits into
HumanBrainProject:masterfrom
Neuroinflab:master
Open

Added proper handling of nifti RGB formats#46
baishi wants to merge 2 commits into
HumanBrainProject:masterfrom
Neuroinflab:master

Conversation

@baishi
Copy link
Copy Markdown

@baishi baishi commented Feb 18, 2026

Two formats of NIfTI are properly supported

  1. The format of (x, y, z, t, (uint8, uint8, uint8)) is supported and mmap works out of the box
  2. The format of (x, y, z, t, [('R', 'u1'), ('G', 'u1'), ('B', 'u1')]) structured rgb format is also supported. However, due to limitation of nibabel, the full dataset need to be loaded into memory.
    A warning is printed for this case

Shi Bai added 2 commits February 19, 2026 05:05
1. The format of (x, y, z, t, (uint8, uint8, uint8)) is supported and
   mmap works out of the box
2. The format of (x, y, z, t, [('R', 'u1'), ('G', 'u1'), ('B', 'u1')])
   structured rgb format is also supported. However, due to limitation
   of nibabel, the full dataset need to be loaded into memory.
   A warning is printed for this case
@xgui3783
Copy link
Copy Markdown
Collaborator

@baishi thanks for the PR.

Is there a sample nifti where we can write test to prevent regression in the future?

@baishi
Copy link
Copy Markdown
Author

baishi commented Feb 28, 2026

@baishi thanks for the PR.

Is there a sample nifti where we can write test to prevent regression in the future?

sample_tuple.nii.gz

sample_structure.nii.gz

I have attached two sample image to test against.
sample_tuple uses 3 tuple of 8 bits (datatype 2) on 5th dimension (dim[5] a.k.a x, y, z, t, rgb) to store the value
and
sample_structure uses 1 value of 24-bit (datatype 128) on 5th dimension to store the structured rgb value.

Copy link
Copy Markdown
Collaborator

@xgui3783 xgui3783 left a comment

Choose a reason for hiding this comment

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

Apologies it took so long.

Thanks for the PR.

If you find you have less time, feel free to edit the PR to allow maintainers to change the PR, so we can update the proposed change.

Comment on lines 363 to +374
@@ -325,6 +371,7 @@ def volume_file_to_precomputed(volume_filename,
new_proxy[2*third:]
], axis=-1)
img = nibabel.Nifti1Image(new_dataobj, img.affine)
"""
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

we can just remove this bit, right?

"does not lose range/accuracy.",
input_dtype.name, output_dtype.name)
if input_dtype == [('R', 'u1'), ('G', 'u1'), ('B', 'u1')]:
# numpy cannot case RGB*u1 to uint8, the rgb structure need to be handled separately
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

case -> cast

?

Comment on lines +342 to +344
logger.warning('The mmap access for structured RGB nifti format is not supported, reading all data into memory')
logger.warning('This process may use a lot of memory, be warned')
volume = proxy[:].view(dtype=np.uint8, type=np.ndarray).reshape(proxy.shape[0:4] + (3,))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

hmm, my personal expectation is that it should raise, with how to remedy (aka, omit --mmap flag, and expect logs of memory usage)



# RGB nifti file has two arrangements, number 1 datatype as integer values with axis 4 to be the components
# the other type is datatype == 128, then the data is stored structurally
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

is there a doc for this? if we do, it would be great to reference it here.

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.

2 participants