nipreps / mriqc

Automated Quality Control and visual reports for Quality Assessment of structural (T1w, T2w) and functional MRI of the brain
http://mriqc.readthedocs.io
Apache License 2.0
299 stars 132 forks source link

FIX: SynthStrip preprocessing miscalculating new shape after reorientation #1021

Closed oesteban closed 2 years ago

oesteban commented 2 years ago

The calculation of the new shape within the preprocessing for SynthStrip was done in XYZ coordinates (i.e., extents for each physical axis were estimated and divided by the new 1mm pixel size). However, the new axis sizes did not account for data being stored with IJK -> XZY convention (corresponding to LIA).

This PR accounts for this miscalculation.

Resolves: #999.

oesteban commented 2 years ago

cc/ @psadil could you give this a try?

Basically, you can do this offline with:

import nibabel as nb
from mriqc.synthstrip.cli import conform

conform(nb.load("input_image.nii.gz")).to_filename("conformed.nii.gz")

With the old code, you should see the wrong image shape (considering the reference that of synthstrip). With this PR you should see the shortest axis put before.

Let me know what you find.

oesteban commented 2 years ago

I'm re-reading the original issue, and thought a bit about your solution:

    # ...calculate target_affine above here...
    # Get corner voxel centers in mm
    corners_xyz = np.abs(
        affine
        @ target_affine
        @ np.hstack((corner_centers_ijk, np.ones((len(corner_centers_ijk), 1)))).T
    )

This indeed works because it implicitly reorders the shapes. I feel my patch is more explicit and, therefore less prone to future regressions.

oesteban commented 2 years ago

Alright, I'm pretty confident this is the fix -- I'm going to do the wrong thing and merge this to then release if tests pass on master, on a Friday evening.

psadil commented 2 years ago

Confirming that, with the new changes, the original image I'd been working with has not been cropped. So, looks good to me