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

qform/sform differ #7

Closed
jcohenadad opened this issue Mar 16, 2021 · 6 comments · Fixed by #12
Closed

qform/sform differ #7

jcohenadad opened this issue Mar 16, 2021 · 6 comments · Fixed by #12
Labels
bug Something isn't working

Comments

@jcohenadad
Copy link
Member

julien-macbook:~/code/sct/data/exvivo_template/template $ fslhd template.nii.gz 
[...]
qform_name	Unknown
qform_code	0
qto_xyz:1	0.080000 0.000000 0.000000 0.000000 
qto_xyz:2	0.000000 0.080000 0.000000 0.000000 
qto_xyz:3	0.000000 0.000000 0.080000 33.680000 
qto_xyz:4	0.000000 0.000000 0.000000 1.000000 
qform_xorient	Left-to-Right
qform_yorient	Posterior-to-Anterior
qform_zorient	Inferior-to-Superior
sform_name	Unknown
sform_code	1
sto_xyz:1	-0.080000 0.000000 0.000000 0.000000 
sto_xyz:2	-0.000000 0.080000 0.000000 0.000000 
sto_xyz:3	-0.000000 0.000000 0.080000 33.680000 
sto_xyz:4	0.000000 0.000000 0.000000 1.000000 
sform_xorient	Right-to-Left
sform_yorient	Posterior-to-Anterior
sform_zorient	Inferior-to-Superior

Causing the following crash:

Straighten the spinal cord using centerline/segmentation...
Creating temporary folder (/var/folders/s8/4qnm5q1n261ch35b5kkclsb00000gn/T/sct-20210316114030.722928-straighten_spinalcord-cykcwclc)
Copy files to tmp folder...
Image /private/var/folders/s8/4qnm5q1n261ch35b5kkclsb00000gn/T/sct-20210316114011.750277-register_to_template-rthcfks3/template_seg.nii.gz has different qform and sform matrices. This can produce incorrect results. Consider setting the two matrices to be equal by running:
sct_image -i /private/var/folders/s8/4qnm5q1n261ch35b5kkclsb00000gn/T/sct-20210316114011.750277-register_to_template-rthcfks3/template_seg.nii.gz -set-sform-to-qform -o /private/var/folders/s8/4qnm5q1n261ch35b5kkclsb00000gn/T/sct-20210316114011_same-affine.750277-register_to_template-rthcfks3/template_seg.nii.gz.
Traceback (most recent call last):
  File "/Users/julien/code/sct/spinalcordtoolbox/scripts/sct_register_to_template.py", line 1281, in <module>
    main(sys.argv[1:])
  File "/Users/julien/code/sct/spinalcordtoolbox/scripts/sct_register_to_template.py", line 533, in main
    sc_straight.straighten()
  File "/Users/julien/code/sct/spinalcordtoolbox/straightening.py", line 111, in straighten
    Image(self.centerline_reference_filename, check_sform=True).save(os.path.join(path_tmp, "centerline_ref.nii.gz"))
  File "/Users/julien/code/sct/spinalcordtoolbox/image.py", line 311, in __init__
    raise ValueError("Image sform does not match qform")
ValueError: Image sform does not match qform

Suggested fix: match qform/sform and update this template.

Related to spinalcordtoolbox/spinalcordtoolbox#3174

@jcohenadad jcohenadad added the bug Something isn't working label Mar 16, 2021
@jcohenadad
Copy link
Member Author

@joshuacwnewton I'm a bit lost with this issue-- didn't we force the copy of the sform/qform to avoid this crash? Or if not, shouldn't we output a more helpful message, such as: "To fix the problem, run sct_image -i PAST-IMAGE-NAME -set-sform-to-qform"?

@jcohenadad
Copy link
Member Author

To temporarily overcome the issue i did this:

sct_image -i template.nii.gz -set-sform-to-qform
sct_image -i mask_spinalcord.nii.gz -set-sform-to-qform

@joshuacwnewton
Copy link
Member

@joshuacwnewton I'm a bit lost with this issue-- didn't we force the copy of the sform/qform to avoid this crash? Or if not, shouldn't we output a more helpful message, such as: "To fix the problem, run sct_image -i PAST-IMAGE-NAME -set-sform-to-qform"?

We do output a message, but it was written using a separate logger.error statement, so it should show up above the actual error message.

https://github.com/neuropoly/spinalcordtoolbox/blob/60ea08c9d664cf403f2982f8f5c8ae1cc5d704a3/spinalcordtoolbox/image.py#L307-L311

I agree that the log isn't clear enough the way it is now.

@jcohenadad
Copy link
Member Author

Ah! Yes you are right. It even appears in red (which is good):
image

@joshuacwnewton
Copy link
Member

Ahh, that's a useful screenshot! Because we use the absolute paths, the message is very verbose, like a wall of text. It is easy to gloss over and ignore, I think. So, some work could be done to make it more readable.

@jcohenadad
Copy link
Member Author

jcohenadad commented Mar 16, 2021

suggestion:

⚠️ The following image has different qform and sform matrices: /private/var/folders/s8/4qnm5q1n261ch35b5kkclsb00000gn/T/sct-20210316114011.750277-register_to_template-rthcfks3/template_seg.nii.gz .

This can produce incorrect results. Consider setting the two matrices to be equal by running:
sct_image -i /private/var/folders/s8/4qnm5q1n261ch35b5kkclsb00000gn/T/sct-20210316114011.750277-register_to_template-rthcfks3/template_seg.nii.gz -set-sform-to-qform -o /private/var/folders/s8/4qnm5q1n261ch35b5kkclsb00000gn/T/sct-20210316114011_same-affine.750277-register_to_template-rthcfks3/template_seg.nii.gz.

Traceback (most recent call last):
  File "/Users/julien/code/sct/spinalcordtoolbox/scripts/sct_register_to_template.py", line 1281, in <module>
    main(sys.argv[1:])
  File "/Users/julien/code/sct/spinalcordtoolbox/scripts/sct_register_to_template.py", line 533, in main
    sc_straight.straighten()
  File "/Users/julien/code/sct/spinalcordtoolbox/straightening.py", line 111, in straighten
    Image(self.centerline_reference_filename, check_sform=True).save(os.path.join(path_tmp, "centerline_ref.nii.gz"))
  File "/Users/julien/code/sct/spinalcordtoolbox/image.py", line 311, in __init__
    raise ValueError("Image sform does not match qform")
ValueError: Image sform does not match qform

jcohenadad added a commit that referenced this issue Mar 17, 2021
- Output files into 'template' folder. Fixes #5
- Instantiate new nibabel object: by default the qform_code and sform_code are 0,2
- Uses the same diagonal affine matrix for both qform/sform. Fixes #7
- Now, the translation (4th column) is only zero. Previously, it was [0, 0, 33]. There was no good reason for it.
- Convert dtype to UINT16 as it takes less space and precision is not an issue here. Related to #1
- Save with conventional name. Fixes #9
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
None yet
Development

Successfully merging a pull request may close this issue.

2 participants