Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexplained flip of the PE direction under some unknown circumstances #218

Open
oesteban opened this issue Aug 2, 2021 · 10 comments · Fixed by #237
Open

Unexplained flip of the PE direction under some unknown circumstances #218

oesteban opened this issue Aug 2, 2021 · 10 comments · Fixed by #237
Assignees
Labels
bug Something isn't working
Milestone

Comments

@oesteban
Copy link
Member

oesteban commented Aug 2, 2021

Using @mgxd's dataset in the debugging of the fMRIPrep + SDCFlows integration, we seem to have hit a difficult bug for which I haven't found the failure point (and hence the fix).

In order to permit progress on nipreps/fmriprep#2392, I'm separating this problem that seems to do with some idiosyncratic parameter (likely related to orientation headers) that makes the problem apparent.

@oesteban oesteban added the bug Something isn't working label Aug 2, 2021
@oesteban oesteban added this to the 2.1.0 milestone Aug 2, 2021
@oesteban oesteban self-assigned this Aug 2, 2021
@oesteban oesteban added this to Triage in fMRIPrep - Development Road Map via automation Aug 2, 2021
@oesteban oesteban moved this from Triage to maint/20.x in fMRIPrep - Development Road Map Aug 2, 2021
@effigies
Copy link
Member

effigies commented Aug 2, 2021

I vaguely recall FSL making strong assumptions about an image being left or right handed in some context. Could be related?

@oesteban
Copy link
Member Author

oesteban commented Aug 2, 2021

The flip is A/P so I'm afraid this is genuinely an SDCFlows problem. It also occurs that correction is correctly performed on the reference (i.e., a combination of EPI scans that were fed into topup) but then incorrectly applied on the target bold.

Registration of the bold target and the EPI reference seems okay (no gross error there), and I've checked the implementation a few times not.

@effigies
Copy link
Member

Is it possible that we have, e.g., an LPS file that we're reorienting to RAS without flipping the PhaseEncodingDirection polarity?

@oesteban
Copy link
Member Author

It is very possible, probably top 1 suspect. The alternative is also true: we have an LPS file and we are flipping two times.

@effigies
Copy link
Member

Well, ds003130 has A/P and P/A bold series, sbrefs and EPI fieldmaps. Everything's in LAS. Should be a good test dataset.

oesteban added a commit that referenced this issue Sep 30, 2021
After much thought, I have come to the conclusion that the VOX2RAS
affine must be applied in all cases, regardless of whether the dataset
is oblique or not.

After all, the VSM (voxel-shift-map) is no more than a regularly gridded
field of voxel coordinates.
The transformation between voxels and mm is biyective and univocal, and
formalized by the affine matrix of the image.

This PR simplifies the calculation, and theoretically should resolve
most of the issues we are experiencing when resampling data.

Resolves: #218.
Resolves: #236.

Related: nipreps/fmriprep#2210.
oesteban added a commit that referenced this issue Sep 30, 2021
After much thought, I have come to the conclusion that the VOX2RAS
affine must be applied in all cases, regardless of whether the dataset
is oblique or not.

After all, the VSM (voxel-shift-map) is no more than a regularly gridded
field of voxel coordinates.
The transformation between voxels and mm is biyective and univocal, and
formalized by the affine matrix of the image.

This PR simplifies the calculation, and theoretically should resolve
most of the issues we are experiencing when resampling data.

Resolves: #218.
Resolves: #236.

Related: nipreps/fmriprep#2210.
oesteban added a commit that referenced this issue Sep 30, 2021
After much thought, I have come to the conclusion that the VOX2RAS
affine must be applied in all cases, regardless of whether the dataset
is oblique or not.

After all, the VSM (voxel-shift-map) is no more than a regularly gridded
field of voxel coordinates.
The transformation between voxels and mm is biyective and univocal, and
formalized by the affine matrix of the image.

This PR simplifies the calculation, and theoretically should resolve
most of the issues we are experiencing when resampling data.

Resolves: #218.
Resolves: #236.

Related: nipreps/fmriprep#2210.
fMRIPrep - Development Road Map automation moved this from maint/20.x to Done Oct 1, 2021
@oesteban
Copy link
Member Author

oesteban commented Oct 6, 2021

@mgxd - after processing your data with the new version, I found that the problem persists.

Then, I visually checked the distortion of the two EPI blips under fmap/ and the bold task under func/. Simple screening makes apparent that the distortion of the BOLD image is consistent with that of sub-voice969_ses-1-acq-func_dir-AP_run-02_epi.nii.gz, while metadata says otherwise:

(base) oesteban@myelin:/data/datasets/fmriprep-tests/mgxd/sub-voice969/ses-1$ grep PhaseEncodingDirection fmap/sub-voice969_ses-1_acq-func_dir-AP_run-02_epi.json 
    "InPlanePhaseEncodingDirectionDICOM": "COL",
    "PhaseEncodingDirection": "j-",
(base) oesteban@myelin:/data/datasets/fmriprep-tests/mgxd/sub-voice969/ses-1$ grep PhaseEncodingDirection func/sub-voice969_ses-1_task-emosent_run-01_bold.json 
   "InPlanePhaseEncodingDirectionDICOM": "COL", 
   "PhaseEncodingDirection": "j", 

In other words, either the functional or both fieldmaps have reversed PE settings - by the looks of the distortion I'd say the functional is wrong. Flipping the setting for the functional resolved the issue.

Affines don't show any axes flip or reordering that could affect the interpretation of the metadata by SDCFlows (although, the inconsistency above is independent of SDCFlows' interpretations):

In [3]: nb.load("func/sub-voice969_ses-1_task-emosent_run-01_bold.nii.gz").affine[:3, :3]
Out[3]: 
array([[-1.99918818e+00,  1.50208490e-03,  6.26566336e-02],
       [-2.04624012e-02,  1.84701359e+00, -8.43580365e-01],
       [ 5.31794392e-02,  7.67162681e-01,  2.03087330e+00]])

In [4]: nb.load("fmap/sub-voice969_ses-1_acq-func_dir-AP_run-02_epi.nii.gz").affine[:3, :3]
Out[4]: 
array([[-1.99963975, -0.02582768, -0.02781582],
       [-0.01320271,  1.84736598, -0.76620144],
       [-0.03558761,  0.76587981,  1.84720373]])

I think we can keep this closed - please let us know if you make in further investigation on this particular problem.

@oesteban
Copy link
Member Author

oesteban commented Oct 6, 2021

I stand corrected - editing the metadata PhaseEncodingDirection did not work. Something deeper might be happening. Reopening and investigating tomorrow.

@oesteban oesteban reopened this Oct 6, 2021
fMRIPrep - Development Road Map automation moved this from Done to In progress Oct 6, 2021
@oesteban oesteban modified the milestones: 2.1.0, 2.0.8 Oct 6, 2021
@mgxd
Copy link
Contributor

mgxd commented Oct 7, 2021

@oesteban looking through fMRIPrep's 21.0.0 Dockerfile, I noticed we are not using topup from FSL6 - could that partially explain this?

@oesteban
Copy link
Member Author

oesteban commented Oct 8, 2021

I doubt it because the field map looks okay. Also, I'm running locally with FSL 6, I believe.

@oesteban oesteban modified the milestones: 2.0.8, 2.0.10 Nov 24, 2021
@effigies effigies modified the milestones: 2.0.10, 20.0.13 Feb 14, 2022
@effigies
Copy link
Member

Probably more an artifact of failing to account for rotation of splines

oesteban added a commit that referenced this issue Nov 19, 2022
A cythonized implementation of the tensor product B-Splines is proposed.

Resolves: #289.
Resolves: #218.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: In Progress
3 participants