Skip to content

Commit cbd55e2

Browse files
committed
Merge pull request #1432 from effigies/purge_simpleitk
REF: Simplify checkT1s
2 parents 5330e5c + dba3270 commit cbd55e2

File tree

1 file changed

+20
-37
lines changed

1 file changed

+20
-37
lines changed

nipype/workflows/smri/freesurfer/autorecon1.py

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,36 @@
66
from nipype.interfaces.freesurfer import *
77
from .utils import copy_file, copy_files
88

9+
910
def checkT1s(T1_files, cw256=False):
1011
"""Verifying size of inputs and setting workflow parameters"""
11-
import SimpleITK as sitk
12-
import os
1312
import sys
14-
# check that the files are in a list
15-
if not type(T1_files) == list:
16-
T1_files = [T1_files]
13+
import nibabel as nib
14+
from nipype.utils.filemanip import filename_to_list
15+
16+
T1_files = filename_to_list(T1_files)
1717
if len(T1_files) == 0:
1818
print("ERROR: No T1's Given")
1919
sys.exit(-1)
20-
for i, t1 in enumerate(T1_files):
21-
if t1.endswith(".mgz"):
22-
# convert input fs files to NIFTI
23-
convert = MRIConvert()
24-
convert.inputs.in_file = t1
25-
convert.inputs.out_file = os.path.abspath(os.path.basename(t1).replace('.mgz', '.nii.gz'))
26-
convert.run()
27-
T1_files[i] = convert.inputs.out_file
28-
size = None
29-
origvol_names = list()
30-
for i, t1 in enumerate(T1_files):
31-
# assign an input number
32-
file_num = str(i + 1)
33-
while len(file_num) < 3:
34-
file_num = '0' + file_num
35-
origvol_names.append("{0}.mgz".format(file_num))
36-
# check the size of the image
37-
img = sitk.ReadImage(t1)
38-
if not size:
39-
size = img.GetSize()
40-
elif size != img.GetSize():
41-
print("ERROR: T1s not the same size. Cannot process {0} {1} together".format(T1_files[0],
42-
otherFilename))
20+
21+
shape = nib.load(T1_files[0]).shape
22+
for t1 in T1_files[1:]:
23+
if nib.load(t1).shape != shape:
24+
print("ERROR: T1s not the same size. Cannot process {0} and {1} "
25+
"together".format(T1_files[0], t1))
4326
sys.exit(-1)
27+
28+
origvol_names = ["{0:03d}.mgz".format(i + 1) for i in range(len(T1_files))]
29+
4430
# check if cw256 is set to crop the images if size is larger than 256
45-
if not cw256:
46-
for dim in size:
47-
if dim > 256:
48-
print("Setting MRI Convert to crop images to 256 FOV")
49-
cw256 = True
50-
if len(T1_files) > 1:
51-
resample_type = 'cubic'
52-
else:
53-
resample_type = 'interpolate'
31+
if not cw256 and any(dim > 256 for dim in shape):
32+
print("Setting MRI Convert to crop images to 256 FOV")
33+
cw256 = True
34+
35+
resample_type = 'cubic' if len(T1_files) > 1 else 'interpolate'
5436
return T1_files, cw256, resample_type, origvol_names
5537

38+
5639
def create_AutoRecon1(name="AutoRecon1", longitudinal=False, field_strength='1.5T',
5740
custom_atlas=None, plugin_args=None):
5841
"""Creates the AutoRecon1 workflow in nipype.

0 commit comments

Comments
 (0)