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