Skip to content

Commit c1959b8

Browse files
authored
workaround rel path freesurfer bug
1 parent 54767d5 commit c1959b8

File tree

1 file changed

+47
-41
lines changed

1 file changed

+47
-41
lines changed

run.py

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,19 @@ def run(command, env={}, ignore_errors=False):
6868
subject_dirs = glob(os.path.join(args.bids_dir, "sub-*"))
6969
subjects_to_analyze = [subject_dir.split("-")[-1] for subject_dir in subject_dirs]
7070

71+
# workaround for https://mail.nmr.mgh.harvard.edu/pipermail//freesurfer/2016-July/046538.html
72+
output_dir = os.path.abspath(args.output_dir)
73+
7174
# running participant level
7275
if args.analysis_level == "participant":
73-
if not os.path.exists(os.path.join(args.output_dir, "fsaverage")):
74-
run("cp -rf " + os.path.join(os.environ["SUBJECTS_DIR"], "fsaverage") + " " + os.path.join(args.output_dir, "fsaverage"),
76+
if not os.path.exists(os.path.join(output_dir, "fsaverage")):
77+
run("cp -rf " + os.path.join(os.environ["SUBJECTS_DIR"], "fsaverage") + " " + os.path.join(output_dir, "fsaverage"),
7578
ignore_errors=True)
76-
if not os.path.exists(os.path.join(args.output_dir, "lh.EC_average")):
77-
run("cp -rf " + os.path.join(os.environ["SUBJECTS_DIR"], "lh.EC_average") + " " + os.path.join(args.output_dir, "lh.EC_average"),
79+
if not os.path.exists(os.path.join(output_dir, "lh.EC_average")):
80+
run("cp -rf " + os.path.join(os.environ["SUBJECTS_DIR"], "lh.EC_average") + " " + os.path.join(output_dir, "lh.EC_average"),
7881
ignore_errors=True)
79-
if not os.path.exists(os.path.join(args.output_dir, "rh.EC_average")):
80-
run("cp -rf " + os.path.join(os.environ["SUBJECTS_DIR"], "rh.EC_average") + " " + os.path.join(args.output_dir, "rh.EC_average"),
82+
if not os.path.exists(os.path.join(output_dir, "rh.EC_average")):
83+
run("cp -rf " + os.path.join(os.environ["SUBJECTS_DIR"], "rh.EC_average") + " " + os.path.join(output_dir, "rh.EC_average"),
8184
ignore_errors=True)
8285
# find all T1s and skullstrip them
8386
for subject_label in subjects_to_analyze:
@@ -106,26 +109,26 @@ def run(command, env={}, ignore_errors=False):
106109
fsid = "sub-%s_ses-%s"%(subject_label, session_label)
107110
timepoints.append(fsid)
108111
cmd = "recon-all -subjid %s -sd %s %s -all -openmp %d"%(fsid,
109-
args.output_dir,
110-
input_args,
111-
args.n_cpus)
112+
output_dir,
113+
input_args,
114+
args.n_cpus)
112115
print(cmd)
113-
if os.path.exists(os.path.join(args.output_dir, fsid)):
114-
rmtree(os.path.join(args.output_dir, fsid))
116+
if os.path.exists(os.path.join(output_dir, fsid)):
117+
rmtree(os.path.join(output_dir, fsid))
115118
run(cmd)
116119

117120
# creating a subject specific template
118121
input_args = " ".join(["-tp %s"%tp for tp in timepoints])
119122
fsid = "sub-%s"%subject_label
120123
stages = " ".join(["-" + stage for stage in args.stages])
121124
cmd = "recon-all -base %s -sd %s %s %s -openmp %d"%(fsid,
122-
args.output_dir,
123-
input_args,
124-
stages,
125-
args.n_cpus)
125+
output_dir,
126+
input_args,
127+
stages,
128+
args.n_cpus)
126129
print(cmd)
127-
if os.path.exists(os.path.join(args.output_dir, fsid)):
128-
rmtree(os.path.join(args.output_dir, fsid))
130+
if os.path.exists(os.path.join(output_dir, fsid)):
131+
rmtree(os.path.join(output_dir, fsid))
129132
run(cmd)
130133

131134
for tp in timepoints:
@@ -134,12 +137,12 @@ def run(command, env={}, ignore_errors=False):
134137
stages = " ".join(["-" + stage for stage in args.stages])
135138
cmd = "recon-all -long %s %s -sd %s %s -openmp %d"%(tp,
136139
fsid,
137-
args.output_dir,
140+
output_dir,
138141
stages,
139142
args.n_cpus)
140143
print(cmd)
141-
if os.path.exists(os.path.join(args.output_dir, tp + ".long." + fsid)):
142-
rmtree(os.path.join(args.output_dir, tp + ".long." + fsid))
144+
if os.path.exists(os.path.join(output_dir, tp + ".long." + fsid)):
145+
rmtree(os.path.join(output_dir, tp + ".long." + fsid))
143146
run(cmd)
144147

145148
else:
@@ -156,27 +159,30 @@ def run(command, env={}, ignore_errors=False):
156159
fsid = "sub-%s"%subject_label
157160
stages = " ".join(["-" + stage for stage in args.stages])
158161
cmd = "recon-all -subjid %s -sd %s %s %s -openmp %d"%(fsid,
159-
args.output_dir,
160-
input_args,
161-
stages,
162-
args.n_cpus)
162+
output_dir,
163+
input_args,
164+
stages,
165+
args.n_cpus)
163166
print(cmd)
164-
if os.path.exists(os.path.join(args.output_dir, fsid)):
165-
rmtree(os.path.join(args.output_dir, fsid))
167+
if os.path.exists(os.path.join(output_dir, fsid)):
168+
rmtree(os.path.join(output_dir, fsid))
166169
run(cmd)
167170
elif args.analysis_level == "group": # running group level
168-
# generate study specific template
169-
fsids = ["sub-%s"%s for s in subjects_to_analyze]
170-
cmd = "make_average_subject --no-symlink --out " + args.template_name + " --subjects " + " ".join(fsids)
171-
print(cmd)
172-
if os.path.exists(os.path.join(args.output_dir, args.template_name)):
173-
rmtree(os.path.join(args.output_dir, args.template_name))
174-
run(cmd, env={"SUBJECTS_DIR": args.output_dir})
175-
for subject_label in subjects_to_analyze:
176-
for hemi in ["lh", "rh"]:
177-
tif_file = os.path.join(args.output_dir, args.template_name, hemi+".reg.template.tif")
178-
fsid = "sub-%s"%subject_label
179-
sphere_file = os.path.join(args.output_dir, fsid, "surf", hemi+".sphere")
180-
reg_file = os.path.join(args.output_dir, fsid, "surf", hemi+".sphere.reg." + args.template_name)
181-
cmd = "mris_register -curv %s %s %s"%(sphere_file, tif_file, reg_file)
182-
run(cmd, env={"SUBJECTS_DIR": args.output_dir})
171+
if len(subjects_to_analyze) > 1:
172+
# generate study specific template
173+
fsids = ["sub-%s"%s for s in subjects_to_analyze]
174+
cmd = "make_average_subject --no-symlink --out " + args.template_name + " --subjects " + " ".join(fsids)
175+
print(cmd)
176+
if os.path.exists(os.path.join(output_dir, args.template_name)):
177+
rmtree(os.path.join(output_dir, args.template_name))
178+
run(cmd, env={"SUBJECTS_DIR": output_dir})
179+
for subject_label in subjects_to_analyze:
180+
for hemi in ["lh", "rh"]:
181+
tif_file = os.path.join(output_dir, args.template_name, hemi+".reg.template.tif")
182+
fsid = "sub-%s"%subject_label
183+
sphere_file = os.path.join(output_dir, fsid, "surf", hemi+".sphere")
184+
reg_file = os.path.join(output_dir, fsid, "surf", hemi+".sphere.reg." + args.template_name)
185+
cmd = "mris_register -curv %s %s %s"%(sphere_file, tif_file, reg_file)
186+
run(cmd, env={"SUBJECTS_DIR": output_dir})
187+
else:
188+
print("Only one subject included in the analysis. Skipping group level")

0 commit comments

Comments
 (0)