Skip to content

Commit

Permalink
Merge pull request #35 from ntraut/master
Browse files Browse the repository at this point in the history
skip subject only if last freesurfer output is present
  • Loading branch information
chrisgorgo authored Oct 25, 2017
2 parents b3220a0 + 1152a23 commit 119a738
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def run(command, env={}, ignore_errors=False):
if glob(os.path.join(args.bids_dir, "sub-*", "ses-*")):
subjects = [subject_dir.split("-")[-1] for subject_dir in subject_dirs]
for subject_label in subjects:
session_dirs = glob(os.path.join(args.bids_dir,"sub-%s"%subject_label,"ses-*"))
session_dirs = glob(os.path.join(args.bids_dir, "sub-%s"%subject_label, "ses-*"))
sessions = [os.path.split(dr)[-1].split("-")[-1] for dr in session_dirs]
n_valid_sessions = 0
for session_label in sessions:
Expand Down Expand Up @@ -161,36 +161,36 @@ def run(command, env={}, ignore_errors=False):
for subject_label in subjects_to_analyze:
if glob(os.path.join(args.bids_dir, "sub-%s"%subject_label, "ses-*")):
T1s = glob(os.path.join(args.bids_dir,
"sub-%s"%subject_label,
"ses-*",
"anat",
"%s_T1w.nii*"%acq_tpl))
"sub-%s"%subject_label,
"ses-*",
"anat",
"%s_T1w.nii*"%acq_tpl))
sessions = set([os.path.normpath(t1).split(os.sep)[-3].split("-")[-1] for t1 in T1s])
if args.session_label:
sessions = sessions.intersection(args.session_label)

if len(sessions) > 0 and longitudinal_study == True:
timepoints = ["sub-%s_ses-%s"%(subject_label, session_label) for session_label in sessions]
if ('cross-sectional' in args.steps):
if 'cross-sectional' in args.steps:
# Running each session separately, prior to doing longitudinal pipeline
for session_label in sessions:
T1s = glob(os.path.join(args.bids_dir,
"sub-%s"%subject_label,
"ses-%s"%session_label,
"anat",
"%s_T1w.nii*"%acq_tpl))
"sub-%s"%subject_label,
"ses-%s"%session_label,
"anat",
"%s_T1w.nii*"%acq_tpl))
input_args = ""
for T1 in T1s:
if (round(max(nibabel.load(T1).header.get_zooms()),1) < 1.0 and args.hires_mode == "auto") or args.hires_mode == "enable":
if (round(max(nibabel.load(T1).header.get_zooms()), 1) < 1.0 and args.hires_mode == "auto") or args.hires_mode == "enable":
input_args += " -hires"
input_args += " -i %s"%T1

T2s = glob(os.path.join(args.bids_dir, "sub-%s"%subject_label,
"ses-%s"%session_label, "anat",
"*%s_T2w.nii*"%acq_t2))
FLAIRs = glob(os.path.join(args.bids_dir, "sub-%s"%subject_label,
"ses-%s"%session_label, "anat",
"*%s_FLAIR.nii*"%acq_t2))
"ses-%s"%session_label, "anat",
"*%s_FLAIR.nii*"%acq_t2))
if args.refine_pial == "T2":
for T2 in T2s:
if max(nibabel.load(T2).header.get_zooms()) < 1.2:
Expand Down Expand Up @@ -219,7 +219,7 @@ def run(command, env={}, ignore_errors=False):
print("DELETING OUTPUT SUBJECT DIR AND RE-RUNNING COMMAND:")
print(cmd)
run(cmd)
elif os.path.isfile(os.path.join(output_dir, fsid, "mri/aseg.mgz")):
elif os.path.isfile(os.path.join(output_dir, fsid, "label/BA_exvivo.thresh.ctab")):
print("SUBJECT ALREADY SEGMENTED, SKIPPING")
elif os.path.exists(os.path.join(output_dir, fsid)):
print("SUBJECT DIR ALREADY EXISTS (without IsRunning.lh+rh), RUNNING COMMAND:")
Expand All @@ -228,8 +228,8 @@ def run(command, env={}, ignore_errors=False):
else:
print(cmd)
run(cmd)
if ('template' in args.steps):

if 'template' in args.steps:
# creating a subject specific template
input_args = " ".join(["-tp %s"%tp for tp in timepoints])
fsid = "sub-%s"%subject_label
Expand All @@ -240,12 +240,12 @@ def run(command, env={}, ignore_errors=False):
stages,
args.n_cpus)

if os.path.isfile(os.path.join(output_dir, fsid,"scripts/IsRunning.lh+rh")):
if os.path.isfile(os.path.join(output_dir, fsid, "scripts/IsRunning.lh+rh")):
rmtree(os.path.join(output_dir, fsid))
print("DELETING OUTPUT SUBJECT DIR AND RE-RUNNING COMMAND:")
print(cmd)
run(cmd)
elif os.path.isfile(os.path.join(output_dir, fsid, "mri/aseg.mgz")):
elif os.path.isfile(os.path.join(output_dir, fsid, "label/BA_exvivo.thresh.ctab")):
print("TEMPLATE ALREADY CREATED, SKIPPING")
elif os.path.exists(os.path.join(output_dir, fsid)):
print("SUBJECT DIR ALREADY EXISTS (without IsRunning.lh+rh), RUNNING COMMAND:")
Expand All @@ -254,8 +254,8 @@ def run(command, env={}, ignore_errors=False):
else:
print(cmd)
run(cmd)
if ('longitudinal' in args.steps):

if 'longitudinal' in args.steps:
for tp in timepoints:
# longitudinally process all timepoints
fsid = "sub-%s"%subject_label
Expand All @@ -271,7 +271,7 @@ def run(command, env={}, ignore_errors=False):
print("DELETING OUTPUT SUBJECT DIR AND RE-RUNNING COMMAND:")
print(cmd)
run(cmd)
elif os.path.isfile(os.path.join(output_dir, tp + ".long." + fsid, "mri/aseg.mgz")):
elif os.path.isfile(os.path.join(output_dir, tp + ".long." + fsid, "label/BA_exvivo.thresh.ctab")):
print("SUBJECT ALREADY SEGMENTED, SKIPPING")
else:
print(cmd)
Expand Down Expand Up @@ -328,6 +328,8 @@ def run(command, env={}, ignore_errors=False):
print("DELETING OUTPUT SUBJECT DIR AND RE-RUNNING COMMAND:")
print(cmd)
run(cmd)
elif os.path.isfile(os.path.join(output_dir, fsid, "label/BA_exvivo.thresh.ctab")):
print("SUBJECT ALREADY SEGMENTED, SKIPPING")
elif os.path.exists(os.path.join(output_dir, fsid)):
print("SUBJECT DIR ALREADY EXISTS (without IsRunning.lh+rh), RUNNING COMMAND:")
print(resume_cmd)
Expand Down Expand Up @@ -384,6 +386,8 @@ def run(command, env={}, ignore_errors=False):
print("DELETING OUTPUT SUBJECT DIR AND RE-RUNNING COMMAND:")
print(cmd)
run(cmd)
elif os.path.isfile(os.path.join(output_dir, fsid, "label/BA_exvivo.thresh.ctab")):
print("SUBJECT ALREADY SEGMENTED, SKIPPING")
elif os.path.exists(os.path.join(output_dir, fsid)):
print("SUBJECT DIR ALREADY EXISTS (without IsRunning.lh+rh), RUNNING COMMAND:")
print(resume_cmd)
Expand Down

0 comments on commit 119a738

Please sign in to comment.