Skip to content

Commit bcb3a73

Browse files
authored
Merge pull request #504 from tsalo/fs-check
Add node to check for subject's Freesurfer directory
2 parents 37d0d74 + fe337a8 commit bcb3a73

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

src/smriprep/workflows/surfaces.py

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -286,20 +286,30 @@ def init_surface_recon_wf(
286286
]),
287287
]) # fmt:skip
288288
else:
289-
# Pretend to be the autorecon1 node so fsnative2t1w_xfm gets run ASAP
290-
fs_base_inputs = autorecon1 = pe.Node(FreeSurferSource(), name='fs_base_inputs')
289+
# Check that the subject directory exists
290+
check_subjects_dir = pe.Node(
291+
niu.Function(
292+
function=_check_subjects_dir,
293+
input_names=['subjects_dir', 'subject_id'],
294+
output_names=['subjects_dir', 'subject_id'],
295+
),
296+
name='check_subjects_dir',
297+
)
298+
299+
# Hook up get_surfaces immediately,
300+
# pretend to be the autorecon1 node so fsnative2t1w_xfm gets run ASAP
301+
autorecon1 = get_surfaces
291302

292303
workflow.connect([
293-
(inputnode, fs_base_inputs, [
304+
(inputnode, check_subjects_dir, [
294305
('subjects_dir', 'subjects_dir'),
295306
('subject_id', 'subject_id'),
296307
]),
297-
# Generate mid-thickness surfaces
298-
(inputnode, get_surfaces, [
308+
(check_subjects_dir, get_surfaces, [
299309
('subjects_dir', 'subjects_dir'),
300310
('subject_id', 'subject_id'),
301311
]),
302-
(inputnode, save_midthickness, [
312+
(check_subjects_dir, save_midthickness, [
303313
('subjects_dir', 'base_directory'),
304314
('subject_id', 'container'),
305315
]),
@@ -1815,3 +1825,29 @@ def _select_seg(in_files, segmentation):
18151825

18161826
def _repeat(seq: list, count: int) -> list:
18171827
return seq * count
1828+
1829+
1830+
def _check_subjects_dir(subjects_dir: str, subject_id: str) -> tuple[str, str]:
1831+
"""Raise an error if subject's directory does not exist.
1832+
1833+
Parameters
1834+
----------
1835+
subjects_dir
1836+
FreeSurfer SUBJECTS_DIR
1837+
subject_id
1838+
FreeSurfer subject ID
1839+
1840+
Returns
1841+
-------
1842+
tuple
1843+
A tuple of the subjects_dir and subject_id
1844+
"""
1845+
from pathlib import Path
1846+
1847+
subjects_dir = Path(subjects_dir)
1848+
if not subjects_dir.exists():
1849+
raise FileNotFoundError(f'Subject directory {subjects_dir} does not exist.')
1850+
if not (subjects_dir / subject_id).exists():
1851+
raise FileNotFoundError(f'Subject {subject_id} does not exist in {subjects_dir}.')
1852+
1853+
return str(subjects_dir), subject_id

0 commit comments

Comments
 (0)