Skip to content

Commit

Permalink
Update sMRI fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
cgohil8 committed May 28, 2024
1 parent 1658b9d commit 6355d67
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 27 deletions.
71 changes: 45 additions & 26 deletions examples/misc/fix_smri_files.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,56 @@
"""Fix sform code of structurals.
"""Fix structural MRI (sMRI) files.
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.
Replaces the sform with a standard sform.
This script will first create a copy of the sMRI then change
the sform code.
Note, this script may not be needed. Only use this script if
OSL raises an error regarding the sform code of the sMRIs.
"""

# Authors: Chetan Gohil <chetan.gohil@psych.ox.ac.uk>

import os
from pathlib import Path
from shutil import copyfile

import numpy as np
import nibabel as nib

from osl import source_recon

def run(cmd):
print(cmd)
source_recon.rhino.utils.system_call(cmd)

# Paths to files to fix
files = [
"smri/sub-001.nii.gz",
"smri/sub-002.nii.gz",
# List of sMRI files we need to fix
smri_files = [
"sub-001_T1w.nii.gz",
"sub-002_T1w.nii.gz",
]

# Make output directory
output_directory = "data/smri"
run(f"mkdir -p {output_dir}")

for file in files:
# Copy the original file
run(f"cp {file} {output_dir}")
file = f"{output_dir}/{file}"

# Set the sform code to 1
run(f"fslorient -setsformcode 1 {file}")
# Directory to save fixed sMRIs to
fixed_smri_dir = "data/smri"
os.makedirs(fixed_smri_dir, exist_ok=True)

# Loop through the sMRIs
for input_smri_file in smri_files:

# Copy the original sMRI file to the output directory
input_name = Path(input_smri_file).name
output_smri_file = f"{fixed_smri_dir}/{input_name}"
print("Saving output to:", output_smri_file)
copyfile(input_smri_file, output_smri_file)

# Load the output SMRI file
smri = nib.load(output_smri_file)

# Get the original sform header
sform = smri.header.get_sform()
sform_std = np.copy(sform)

# Fix the sform header
sform_std[0, 0:4] = [-1, 0, 0, 128]
sform_std[1, 0:4] = [0, 1, 0, -128]
sform_std[2, 0:4] = [0, 0, 1, -90]
source_recon.rhino.utils.system_call(
"fslorient -setsform {} {}".format(
" ".join(map(str, sform_std.flatten())),
output_smri_file,
)
)

print("Done")
3 changes: 2 additions & 1 deletion osl/source_recon/rhino/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ def get_sform(nii_file):
else:
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/misc/fix_smri_files.py"
"If the qform code is 4 or 1, then considering passing use_qform=True.\n"
"Also see: https://github.com/OHBA-analysis/osl/blob/main/examples/misc/fix_smri_files.py"
)

sform = Transform("mri_voxel", "mri", sform)
Expand Down

0 comments on commit 6355d67

Please sign in to comment.