1
1
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
2
2
# vi: set ft=python sts=4 ts=4 sw=4 et:
3
3
"""Parser."""
4
+ from __future__ import annotations
5
+
4
6
import sys
7
+ import typing as ty
5
8
6
9
from .. import config
7
10
11
+ if ty .TYPE_CHECKING :
12
+ from bids .layout import BIDSLayout
13
+
8
14
9
15
def _build_parser ():
10
16
"""Build parser object."""
@@ -19,9 +25,20 @@ def _build_parser():
19
25
20
26
def _path_exists (path , parser ):
21
27
"""Ensure a given path exists."""
22
- if path is None or not Path (path ).exists ():
28
+ if path is None :
29
+ raise parser .error ("No value provided!" )
30
+ path = Path (path ).absolute ()
31
+ if not path .exists ():
23
32
raise parser .error (f"Path does not exist: <{ path } >." )
24
- return Path (path ).absolute ()
33
+ return path
34
+
35
+ def _dir_not_empty (path , parser ):
36
+ path = _path_exists (path , parser )
37
+ if not path .is_dir ():
38
+ raise parser .error (f"Path is not a directory <{ path } >." )
39
+ for f in path .iterdir ():
40
+ return path
41
+ raise parser .error (f"Directory found with no contents <{ path } >." )
25
42
26
43
def _is_file (path , parser ):
27
44
"""Ensure a given path exists and it is a file."""
@@ -87,6 +104,7 @@ def _slice_time_ref(value, parser):
87
104
formatter_class = ArgumentDefaultsHelpFormatter ,
88
105
)
89
106
PathExists = partial (_path_exists , parser = parser )
107
+ DirNotEmpty = partial (_dir_not_empty , parser = parser )
90
108
IsFile = partial (_is_file , parser = parser )
91
109
PositiveInt = partial (_min_one , parser = parser )
92
110
SliceTimeRef = partial (_slice_time_ref , parser = parser )
@@ -635,7 +653,7 @@ def _slice_time_ref(value, parser):
635
653
)
636
654
g_baby .add_argument (
637
655
"--segmentation-atlases-dir" ,
638
- type = PathExists ,
656
+ type = DirNotEmpty ,
639
657
help = "Directory containing precalculated segmentations to use for JointLabelFusion." ,
640
658
)
641
659
g_baby .add_argument (
@@ -647,7 +665,7 @@ def _slice_time_ref(value, parser):
647
665
g_baby .add_argument (
648
666
"-d" ,
649
667
"--derivatives" ,
650
- type = PathExists ,
668
+ type = DirNotEmpty ,
651
669
nargs = "+" ,
652
670
help = "One or more directory containing pre-computed derivatives." ,
653
671
)
@@ -822,42 +840,40 @@ def parse_args(args=None, namespace=None):
822
840
)
823
841
824
842
config .execution .participant_label = sorted (participant_label )
843
+
844
+ config .execution .unique_labels = compute_subworkflows (
845
+ layout = config .execution .layout ,
846
+ participant_ids = config .execution .participant_label ,
847
+ session_ids = config .execution .session_id ,
848
+ )
825
849
config .workflow .skull_strip_template = config .workflow .skull_strip_template [0 ]
826
- config .execution .unique_labels = compute_subworkflows ()
827
850
828
851
# finally, write config to file
829
852
config_file = config .execution .work_dir / config .execution .run_uuid / "config.toml"
830
853
config_file .parent .mkdir (exist_ok = True , parents = True )
831
854
config .to_filename (config_file )
832
855
833
856
834
- def compute_subworkflows () -> list :
857
+ def compute_subworkflows (
858
+ * ,
859
+ layout : 'BIDSLayout' ,
860
+ participant_ids : list ,
861
+ session_ids : list | None = None ,
862
+ ) -> list :
835
863
"""
836
864
Query all available participants and sessions, and construct the combinations of the
837
865
subworkflows needed.
838
866
"""
839
867
from niworkflows .utils .bids import collect_participants
840
868
841
- from nibabies import config
842
-
843
869
# consists of (subject_id, session_id) tuples
844
870
subworkflows = []
845
871
846
- subject_list = collect_participants (
847
- config .execution .layout ,
848
- participant_label = config .execution .participant_label ,
849
- strict = True ,
850
- )
851
-
872
+ subject_list = collect_participants (layout , participant_ids , strict = True )
852
873
for subject in subject_list :
853
874
# Due to rapidly changing morphometry of the population
854
875
# Ensure each subject session is processed individually
855
- sessions = (
856
- config .execution .session_id
857
- or config .execution .layout .get_sessions (scope = 'raw' , subject = subject )
858
- or [None ]
859
- )
860
- # grab participant age per session
876
+ sessions = session_ids or layout .get_sessions (scope = 'raw' , subject = subject ) or [None ]
861
877
for session in sessions :
862
878
subworkflows .append ((subject , session ))
863
879
return subworkflows
0 commit comments