Skip to content

Commit 734c5b4

Browse files
committed
FIX+ENH: Relative paths, add caching mechanism
1 parent 9ab4988 commit 734c5b4

File tree

1 file changed

+43
-19
lines changed

1 file changed

+43
-19
lines changed

nibabies/interfaces/mcribs.py

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import os
21
import shutil
32
from pathlib import Path
43

@@ -14,6 +13,11 @@
1413

1514
class MCRIBReconAllInputSpec(CommandLineInputSpec):
1615
# Input structure massaging
16+
outdir = Directory(
17+
exists=True,
18+
hash_files=False,
19+
desc='Path to save output, or path of existing MCRIBS output',
20+
)
1721
subjects_dir = Directory(
1822
exists=True,
1923
hash_files=False,
@@ -100,6 +104,21 @@ class MCRIBReconAll(CommandLine):
100104
_cmd = 'MCRIBReconAll'
101105
input_spec = MCRIBReconAllInputSpec
102106
output_spec = MCRIBReconAllOutputSpec
107+
_no_run = False
108+
109+
@property
110+
def cmdline(self):
111+
cmd = super().cmdline
112+
# Avoid processing if valid
113+
if self.inputs.outdir:
114+
sid = self.inputs.subject_id
115+
logf = self.inputs.outdir / sid / 'logs' / f'{sid}.log'
116+
if logf.exists():
117+
logtxt = logf.read_text().splitlines()[-3:]
118+
self._no_run = 'Finished without error' in logtxt
119+
if self._no_run:
120+
return "echo MCRIBSReconAll: nothing to do"
121+
return cmd
103122

104123
def _setup_directory_structure(self, mcribs_dir: Path) -> None:
105124
'''
@@ -138,9 +157,9 @@ def _setup_directory_structure(self, mcribs_dir: Path) -> None:
138157
t2wiso = root / 'RawT2RadiologicalIsotropic' / f'{sid}.nii.gz'
139158
t2wiso.parent.mkdir(**mkdir_kw)
140159
if not t2wiso.exists():
141-
t2wiso.symlink_to(f'../../RawT2/{sid}.nii.gz')
160+
t2wiso.symlink_to(f'../RawT2/{sid}.nii.gz')
142161

143-
n4 = root / sid / 'N4' / f'{sid}.nii.gz'
162+
n4 = root / 'TissueSegDrawEM' / sid / 'N4' / f'{sid}.nii.gz'
144163
n4.parent.mkdir(**mkdir_kw)
145164
if not n4.exists():
146165
n4.symlink_to(f'../../../RawT2/{sid}.nii.gz')
@@ -151,37 +170,42 @@ def _setup_directory_structure(self, mcribs_dir: Path) -> None:
151170
tisseg = root / 'TissueSeg' / f'{sid}_all_labels.nii.gz'
152171
tisseg.parent.mkdir(**mkdir_kw)
153172
if not tisseg.exists():
154-
shutil.copy(self.inputs.segmentation, str(tisseg))
173+
shutil.copy(self.inputs.segmentation_file, str(tisseg))
155174
manedit = tisseg.parent / f'{sid}_all_labels_manedit.nii.gz'
156175
if not manedit.exists():
157176
manedit.symlink_to(tisseg.name)
158177

159178
if self.inputs.surfrecon:
179+
t2wseg = root / 'TissueSeg' / f'{sid}_t2w_restore.nii.gz'
180+
if not t2wseg.exists():
181+
t2wseg.symlink_to(f'../RawT2/{sid}.nii.gz')
182+
160183
surfrec = root / 'SurfReconDeformable' / sid / 'temp' / 't2w-image.nii.gz'
161184
surfrec.parent.mkdir(**mkdir_kw)
162185
if not surfrec.exists():
163-
surfrec.symlink_to(f'../../../../RawT2/{sid}.nii.gz')
164-
165-
# TODO: T1w -> <subject_id>/RawT1RadiologicalIsotropic/<subjectid>.nii.gz
186+
surfrec.symlink_to(f'../../../RawT2/{sid}.nii.gz')
187+
# TODO?: T1w -> <subject_id>/RawT1RadiologicalIsotropic/<subjectid>.nii.gz
166188
return
167189

168190
def _run_interface(self, runtime):
169191
# if users wish to preserve their runs
170-
mcribs_dir = os.getenv('MCRIBS_SUBJECTS')
171-
if mcribs_dir is None or not Path(mcribs_dir).exists():
172-
mcribs_dir = runtime.cwd / 'mcribs'
173-
self._mcribs_dir = mcribs_dir
174-
self._setup_directory_structure(mcribs_dir)
175-
# runs in CWD
176-
os.chdir(mcribs_dir / self.inputs.subject_id)
192+
mcribs_dir = self.inputs.outdir or Path(runtime.cwd) / 'mcribs'
193+
self._mcribs_dir = Path(mcribs_dir)
194+
self._setup_directory_structure(self._mcribs_dir)
195+
# overwrite CWD to be in MCRIB subject's directory
196+
runtime.cwd = str(self._mcribs_dir / self.inputs.subject_id)
177197
return super()._run_interface(runtime)
178198

179199
def _list_outputs(self):
180200
outputs = self._outputs().get()
181-
outputs['mcribs_dir'] = self._mcribs_dir
201+
outputs['mcribs_dir'] = str(self._mcribs_dir)
202+
203+
# Copy freesurfer directory into FS subjects dir
204+
sid = self.inputs.subject_id
205+
mcribs_fs = self._mcribs_dir / sid / 'freesurfer' / sid
206+
if mcribs_fs.exists():
207+
dst = Path(self.inputs.subjects_dir) / self.inputs.subject_id
208+
if not dst.exists():
209+
shutil.copytree(mcribs_fs, dst)
182210

183-
# TODO: Copy freesurfer directory into FS subjects dir
184-
# fs_outputs = self._mcribs_dir / self.inputs.subject_id / 'freesurfer'
185-
# if fs_outputs.exists():
186-
# pass
187211
return outputs

0 commit comments

Comments
 (0)