Skip to content

Commit

Permalink
Added info for how to fix sMRI files.
Browse files Browse the repository at this point in the history
  • Loading branch information
cgohil8 committed Feb 9, 2024
1 parent 5ae6da3 commit 1b701f4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
29 changes: 29 additions & 0 deletions examples/fix_smri_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""Fix sform code of structurals.
This script uses FSL to set the sform code of any structural
whose sform code is not 1 or 4 to make sure it is compatible
with OSL.
Warning: this script will permanently change the SMRI file.
"""

import nibabel as nib

from osl import source_recon

source_recon.setup_fsl("/path/to/fsl")

# Paths to files to fix
files = [
"smri/sub-001.nii.gz",
"smri/sub-002.nii.gz",
]

for file in files:
smri = nib.load(file)
sformcode = smri.header.get_sform(coded=True)[-1]
if sformcode not in [1, 4]:
cmd = f"fslorient -setsformcode 1 {file}"
source_recon.rhino.utils.system_call(cmd)

print("Done”)
10 changes: 8 additions & 2 deletions osl/source_recon/rhino/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,10 @@ def _get_sform(nii_file):
if sformcode == 1 or sformcode == 4:
sform = nib.load(nii_file).header.get_sform()
else:
raise ValueError("sform code for {} is {}, and needs to be 4 or 1".format(nii_file, sformcode))
raise ValueError(
f"sform code for {nii_file} is {sformcode}, and needs to be 4 or 1.\n"
"To fix see: https://github.com/OHBA-analysis/osl/blob/main/examples/fix_smri_files.py"
)

sform = Transform("mri_voxel", "mri", sform)
return sform
Expand All @@ -299,7 +302,10 @@ def _get_mni_sform(nii_file):
if sformcode == 1 or sformcode == 4:
sform = nib.load(nii_file).header.get_sform()
else:
raise ValueError("sform code for {} is {}, and needs to be 4 or 1".format(nii_file, sformcode))
raise ValueError(
f"sform code for {nii_file} is {sformcode}, and needs to be 4 or 1.\n"
"To fix see: https://github.com/OHBA-analysis/osl/blob/main/examples/fix_smri_files.py"
)

sform = Transform("unknown", "mni_tal", sform)
return sform
Expand Down

0 comments on commit 1b701f4

Please sign in to comment.