Skip to content

Refactor NIfTI preview picture to support RGB images - #1496

Open
MaximeBICMTL wants to merge 1 commit into
aces:mainfrom
MaximeBICMTL:rgb-nifti-preview
Open

Refactor NIfTI preview picture to support RGB images#1496
MaximeBICMTL wants to merge 1 commit into
aces:mainfrom
MaximeBICMTL:rgb-nifti-preview

Conversation

@MaximeBICMTL

@MaximeBICMTL MaximeBICMTL commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Disclaimer: The nibabel/numpy code in this PR has been written with AI assistance.

Description

While trying to implement MRI derivatives import in the LORIS BIDS importer (see #1392 for the data that we use in our integration tests, note that derivatives are currently not imported 🙀 !). I stumbled upon an error while creating the NIfTI preview picture for a certain derivative, which happened to contain an RGB NIfTI.

This PR refactors the NIfTI preview picture creation for RGB support, better overall images, and better memory efficiency.

Note that the imaging browser does not seem to be able to display the RGB picture (at least with its current integration into LORIS), but at least we still have a preview now!

CLI output

Main

(loris) lorisadmin@loris:/opt/loris/bin/mri$ time mass_nifti_pic.py --smallest-id 617 --largest-id 626 --force
Creating preview picture for NIfTI file ID 617
Creating preview picture for NIfTI file ID 618
Creating preview picture for NIfTI file ID 619
Creating preview picture for NIfTI file ID 620
Creating preview picture for NIfTI file ID 621
Creating preview picture for NIfTI file ID 622
WARNING: No file with ID 623 in the database, skipping.
Creating preview picture for NIfTI file ID 624
Creating preview picture for NIfTI file ID 625
Creating preview picture for NIfTI file ID 626
Traceback (most recent call last):
  File "/opt/loris/bin/mri/python/scripts/mass_nifti_pic.py", line 117, in <module>
    main()
  File "/opt/loris/bin/mri/python/scripts/mass_nifti_pic.py", line 73, in main
    make_pic(env, data_dir_path, file_id, args.force)
  File "/opt/loris/bin/mri/python/scripts/mass_nifti_pic.py", line 108, in make_pic
    pic_rel_path = create_nifti_preview_picture(env, nifti_file)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/loris/bin/mri/python/lib/imaging_lib/nifti_pic.py", line 43, in create_nifti_preview_picture
    data.astype(np.float32, copy=False),  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Cannot cast array data from dtype([('R', 'u1'), ('G', 'u1'), ('B', 'u1')]) to dtype('float32') according to the rule 'unsafe'

real	0m10.253s
user	0m9.798s
sys	0m0.307s

Notice (very obviously) that the NIfTI preview picture generation fails for the last image 🙀 !

This PR

(loris) lorisadmin@loris:/opt/loris/bin/mri$ time mass_nifti_pic.py --smallest-id 617 --largest-id 626 --force
Creating preview picture for NIfTI file ID 617
Creating preview picture for NIfTI file ID 618
Creating preview picture for NIfTI file ID 619
Creating preview picture for NIfTI file ID 620
Creating preview picture for NIfTI file ID 621
Creating preview picture for NIfTI file ID 622
WARNING: No file with ID 623 in the database, skipping.
Creating preview picture for NIfTI file ID 624
Creating preview picture for NIfTI file ID 625
Creating preview picture for NIfTI file ID 626

real	0m3.318s
user	0m2.990s
sys	0m0.210s

Notice that in addition to the error being gone, the time taken by the NIfTI preview picture generation is also much faster! This is not just because the exception is gone but also because the new script is generally faster and more memory-efficient for all pictures.

Visual inspection

Main

image

Notice that one picture is missing! (makes sense since there was an error during picture creation).

This PR

image

Notice that in addition to a new fancy✨ RGB preview picture, the contrast is generally much clearer, the ratio seems more in line with the acquisitions, and the previously kinda unreadable scale is gone.

@github-actions github-actions Bot added the Language: Python Issue or PR related to the Python codebase label Aug 24, 2026
@MaximeBICMTL MaximeBICMTL added Category: Bug Issue or PR that aims to report or fix a bug Category: Feature Issue or PR that aims to introduce a new feature Complexity: Medium Issue or PR that requires a moderate effort or expertise to implement, review, or test labels Aug 24, 2026
@MaximeBICMTL
MaximeBICMTL requested a review from cmadjar August 24, 2026 11:36
@MaximeBICMTL
MaximeBICMTL marked this pull request as ready for review August 24, 2026 11:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Category: Bug Issue or PR that aims to report or fix a bug Category: Feature Issue or PR that aims to introduce a new feature Complexity: Medium Issue or PR that requires a moderate effort or expertise to implement, review, or test Language: Python Issue or PR related to the Python codebase

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant