Skip to content

Commit 5a4e1a6

Browse files
committed
added session inclusion/exclusion
1 parent 75e29f1 commit 5a4e1a6

File tree

3 files changed

+90
-49
lines changed

3 files changed

+90
-49
lines changed

README.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,37 +35,43 @@ This software can be installed via source or via pip from PyPi with `pip install
3535
**_NOTE:_** This project is currently in beta release, some features listed below may not be available for version numbers < 1.0.0
3636

3737
```bash
38-
usage: petdeface.py [-h] [--anat_only] [--participant_label PARTICIPANT_LABEL [PARTICIPANT_LABEL ...]] [--session SESSION] [--docker] [--singularity] [--n_procs N_PROCS] [--skip_bids_validator] [--version] [--placement PLACEMENT] [--remove_existing] [--preview_pics]
39-
[--excludeparticipant EXCLUDEPARTICIPANT [EXCLUDEPARTICIPANT ...]]
38+
usage: petdeface.py [-h] [--anat_only] [--participant_label PARTICIPANT_LABEL [PARTICIPANT_LABEL ...]] [--docker] [--singularity] [--n_procs N_PROCS]
39+
[--skip_bids_validator] [--version] [--placement PLACEMENT] [--remove_existing] [--preview_pics]
40+
[--exclude_participant EXCLUDE_PARTICIPANT [EXCLUDE_PARTICIPANT ...]] [--session SESSION [SESSION ...]]
41+
[--exclude_session EXCLUDE_SESSION [EXCLUDE_SESSION ...]]
4042
bids_dir [output_dir] [analysis_level]
4143

4244
PetDeface
4345

4446
positional arguments:
4547
bids_dir The directory with the input dataset
4648
output_dir The directory where the output files should be stored, if not supplied will default to <bids_dir>/derivatives/petdeface
47-
analysis_level This BIDS app always operates at the participant level, if this argument is changed it will be ignored and run as a participant level analysis
49+
analysis_level This BIDS app always operates at the participant level, if this argument is changed it will be ignored and run as a participant level
50+
analysis
4851

4952
options:
5053
-h, --help show this help message and exit
5154
--anat_only, -a Only deface anatomical images
5255
--participant_label PARTICIPANT_LABEL [PARTICIPANT_LABEL ...], -pl PARTICIPANT_LABEL [PARTICIPANT_LABEL ...]
5356
The label(s) of the participant/subject to be processed. When specifying multiple subjects separate them with spaces.
54-
--session SESSION, -ses SESSION
55-
The label of the session to be processed.
5657
--docker, -d Run in docker container
5758
--singularity, -si Run in singularity container
5859
--n_procs N_PROCS Number of processors to use when running the workflow
5960
--skip_bids_validator
6061
--version, -v show program's version number and exit
6162
--placement PLACEMENT, -p PLACEMENT
62-
Where to place the defaced images. Options are 'adjacent': next to the bids_dir (default) in a folder appended with _defaced'inplace': defaces the dataset in place, e.g. replaces faced PET and T1w images w/ defaced at bids_dir'derivatives': does all of the
63-
defacing within the derivatives folder in bids_dir.
63+
Where to place the defaced images. Options are 'adjacent': next to the bids_dir (default) in a folder appended with _defaced'inplace':
64+
defaces the dataset in place, e.g. replaces faced PET and T1w images w/ defaced at bids_dir'derivatives': does all of the defacing within
65+
the derivatives folder in bids_dir.
6466
--remove_existing, -r
6567
Remove existing output files in output_dir.
6668
--preview_pics Create preview pictures of defacing, defaults to false for docker
67-
--excludeparticipant EXCLUDEPARTICIPANT [EXCLUDEPARTICIPANT ...]
68-
Exclude a subject(s) from the defacing workflow. e.g. --excludeparticipant sub-01 sub-02
69+
--exclude_participant EXCLUDE_PARTICIPANT [EXCLUDE_PARTICIPANT ...]
70+
Exclude a subject(s) from the defacing workflow. e.g. --exclude_participant sub-01 sub-02
71+
--session SESSION [SESSION ...]
72+
Select only a specific session(s) to include in the defacing workflow
73+
--exclude_session EXCLUDE_SESSION [EXCLUDE_SESSION ...]
74+
Select a specific session(s) to exclude from the defacing workflow
6975
```
7076
7177
Working example usage:

docs/usage.rst

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -86,37 +86,43 @@ The number of processors made available to PETdeface can be set by the `--n_proc
8686
Additional options can be found in the help menu::
8787

8888
petdeface -h
89-
usage: petdeface.py [-h] [--anat_only] [--participant_label PARTICIPANT_LABEL [PARTICIPANT_LABEL ...]] [--session SESSION] [--docker] [--singularity] [--n_procs N_PROCS] [--skip_bids_validator] [--version] [--placement PLACEMENT] [--remove_existing] [--preview_pics]
90-
[--excludeparticipant EXCLUDEPARTICIPANT [EXCLUDEPARTICIPANT ...]]
89+
usage: petdeface.py [-h] [--anat_only] [--participant_label PARTICIPANT_LABEL [PARTICIPANT_LABEL ...]] [--docker] [--singularity] [--n_procs N_PROCS]
90+
[--skip_bids_validator] [--version] [--placement PLACEMENT] [--remove_existing] [--preview_pics]
91+
[--exclude_participant EXCLUDE_PARTICIPANT [EXCLUDE_PARTICIPANT ...]] [--session SESSION [SESSION ...]]
92+
[--exclude_session EXCLUDE_SESSION [EXCLUDE_SESSION ...]]
9193
bids_dir [output_dir] [analysis_level]
9294

93-
PetDeface
94-
95-
positional arguments:
96-
bids_dir The directory with the input dataset
97-
output_dir The directory where the output files should be stored, if not supplied will default to <bids_dir>/derivatives/petdeface
98-
analysis_level This BIDS app always operates at the participant level, if this argument is changed it will be ignored and run as a participant level analysis
99-
100-
options:
101-
-h, --help show this help message and exit
102-
--anat_only, -a Only deface anatomical images
103-
--participant_label PARTICIPANT_LABEL [PARTICIPANT_LABEL ...], -pl PARTICIPANT_LABEL [PARTICIPANT_LABEL ...]
104-
The label(s) of the participant/subject to be processed. When specifying multiple subjects separate them with spaces.
105-
--session SESSION, -ses SESSION
106-
The label of the session to be processed.
107-
--docker, -d Run in docker container
108-
--singularity, -si Run in singularity container
109-
--n_procs N_PROCS Number of processors to use when running the workflow
110-
--skip_bids_validator
111-
--version, -v show program's version number and exit
112-
--placement PLACEMENT, -p PLACEMENT
113-
Where to place the defaced images. Options are 'adjacent': next to the bids_dir (default) in a folder appended with _defaced'inplace': defaces the dataset in place, e.g. replaces faced PET and T1w images w/ defaced at bids_dir'derivatives': does all of the
114-
defacing within the derivatives folder in bids_dir.
115-
--remove_existing, -r
116-
Remove existing output files in output_dir.
117-
--preview_pics Create preview pictures of defacing, defaults to false for docker
118-
--excludeparticipant EXCLUDEPARTICIPANT [EXCLUDEPARTICIPANT ...]
119-
Exclude a subject(s) from the defacing workflow. e.g. --excludeparticipant sub-01 sub-02
95+
PetDeface
96+
97+
positional arguments:
98+
bids_dir The directory with the input dataset
99+
output_dir The directory where the output files should be stored, if not supplied will default to <bids_dir>/derivatives/petdeface
100+
analysis_level This BIDS app always operates at the participant level, if this argument is changed it will be ignored and run as a participant level
101+
analysis
102+
103+
options:
104+
-h, --help show this help message and exit
105+
--anat_only, -a Only deface anatomical images
106+
--participant_label PARTICIPANT_LABEL [PARTICIPANT_LABEL ...], -pl PARTICIPANT_LABEL [PARTICIPANT_LABEL ...]
107+
The label(s) of the participant/subject to be processed. When specifying multiple subjects separate them with spaces.
108+
--docker, -d Run in docker container
109+
--singularity, -si Run in singularity container
110+
--n_procs N_PROCS Number of processors to use when running the workflow
111+
--skip_bids_validator
112+
--version, -v show program's version number and exit
113+
--placement PLACEMENT, -p PLACEMENT
114+
Where to place the defaced images. Options are 'adjacent': next to the bids_dir (default) in a folder appended with _defaced'inplace':
115+
defaces the dataset in place, e.g. replaces faced PET and T1w images w/ defaced at bids_dir'derivatives': does all of the defacing within
116+
the derivatives folder in bids_dir.
117+
--remove_existing, -r
118+
Remove existing output files in output_dir.
119+
--preview_pics Create preview pictures of defacing, defaults to false for docker
120+
--exclude_participant EXCLUDE_PARTICIPANT [EXCLUDE_PARTICIPANT ...]
121+
Exclude a subject(s) from the defacing workflow. e.g. --exclude_participant sub-01 sub-02
122+
--session SESSION [SESSION ...]
123+
Select only a specific session(s) to include in the defacing workflow
124+
--exclude_session EXCLUDE_SESSION [EXCLUDE_SESSION ...]
125+
Select a specific session(s) to exclude from the defacing workflow
120126

121127
Docker Based
122128
------------

petdeface/petdeface.py

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -249,15 +249,15 @@ def deface(args: Union[dict, argparse.Namespace]) -> None:
249249
)
250250

251251
# check to see if any subjects are excluded from the defacing workflow
252-
if args.excludeparticipant != []:
252+
if args.exclude_participant != []:
253253
print(
254-
f"Removing the following subjects {args.excludeparticipant} from the defacing workflow"
254+
f"Removing the following subjects {args.exclude_participant} from the defacing workflow"
255255
)
256-
args.excludeparticipant = [
257-
subject.replace("sub-", "") for subject in args.excludeparticipant
256+
args.exclude_participant = [
257+
subject.replace("sub-", "") for subject in args.exclude_participant
258258
]
259259
subjects = [
260-
subject for subject in subjects if subject not in args.excludeparticipant
260+
subject for subject in subjects if subject not in args.exclude_participant
261261
]
262262

263263
print(f"Subjects remaining in the defacing workflow: {subjects}")
@@ -277,6 +277,8 @@ def deface(args: Union[dict, argparse.Namespace]) -> None:
277277
args.bids_dir,
278278
preview_pics=args.preview_pics,
279279
anat_only=args.anat_only,
280+
session=args.session,
281+
exclude_session=args.exclude_session,
280282
)
281283
except FileNotFoundError:
282284
single_subject_wf = None
@@ -303,6 +305,8 @@ def init_single_subject_wf(
303305
output_dir: pathlib.Path = None,
304306
preview_pics=False,
305307
anat_only=False,
308+
session=[],
309+
exclude_session=[],
306310
) -> Workflow:
307311
"""
308312
Organize the preprocessing pipeline for a single subject.
@@ -317,6 +321,10 @@ def init_single_subject_wf(
317321
:type preview_pics: bool, optional
318322
:param anat_only: _description_, defaults to False
319323
:type anat_only: bool, optional
324+
:param session: _description_, will default to only selecting session(s) supplied to this argument, defaults to []
325+
:type session: list, optional
326+
:param exclude_session: _description_, will exclude any session(s) supplied to this argument, defaults to []
327+
:type exclude_session: list, optional
320328
:raises FileNotFoundError: _description_
321329
:return: _description_
322330
:rtype: Workflow
@@ -333,6 +341,13 @@ def init_single_subject_wf(
333341
if subject_data is None:
334342
raise FileNotFoundError(f"Could not find data for subject sub-{subject_id}")
335343

344+
# we combine the sessions to include and exclude into a single set of sessions to exclude from
345+
# the set of all sessions
346+
if session:
347+
sessions_to_exclude = list(set(bids_data.get_sessions()) - (set(bids_data.get_sessions()) & set(session)) | set(exclude_session))
348+
else:
349+
sessions_to_exclude = exclude_session
350+
336351
# check if any t1w images exist for the pet images
337352
for pet_image, t1w_image in subject_data.items():
338353
if t1w_image == "":
@@ -426,6 +441,10 @@ def init_single_subject_wf(
426441
ses_id = ""
427442
pet_string = f"sub-{subject_id}"
428443

444+
# skip anything in the exclude list
445+
if ses_id.replace("ses-", "") in sessions_to_exclude:
446+
continue
447+
429448
# collect run info from pet file
430449
try:
431450
run_id = "_" + re.search("run-[^_|\/]*", str(pet_file)).group(0)
@@ -744,7 +763,9 @@ def __init__(
744763
remove_existing=True,
745764
placement="adjacent",
746765
preview_pics=True,
747-
excludeparticipant=[],
766+
exclude_participant=[],
767+
session=[],
768+
exclude_session=[],
748769
):
749770
self.bids_dir = bids_dir
750771
self.remove_existing = remove_existing
@@ -755,7 +776,9 @@ def __init__(
755776
self.n_procs = n_procs
756777
self.skip_bids_validator = skip_bids_validator
757778
self.preview_pics = preview_pics
758-
self.excludeparticipant = excludeparticipant
779+
self.exclude_participant = exclude_participant
780+
self.session = session
781+
self.exclude_session = exclude_session
759782

760783
# check if freesurfer license is valid
761784
self.fs_license = check_valid_fs_license()
@@ -785,7 +808,9 @@ def run(self):
785808
"placement": self.placement,
786809
"remove_existing": self.remove_existing,
787810
"preview_pics": self.preview_pics,
788-
"excludeparticipant": self.excludeparticipant,
811+
"exclude_participant": self.exclude_participant,
812+
"session": self.session,
813+
"exclude_session": self.exclude_session,
789814
}
790815
)
791816
wrap_up_defacing(
@@ -886,13 +911,15 @@ def cli():
886911
default=False,
887912
)
888913
parser.add_argument(
889-
"--excludeparticipant",
890-
help="Exclude a subject(s) from the defacing workflow. e.g. --excludeparticipant sub-01 sub-02",
914+
"--exclude_participant",
915+
help="Exclude a subject(s) from the defacing workflow. e.g. --exclude_participant sub-01 sub-02",
891916
type=str,
892917
nargs="+",
893918
required=False,
894919
default=[],
895920
)
921+
parser.add_argument("--session", help="Select only a specific session(s) to include in the defacing workflow", type=str, nargs="+", required=False, default=[])
922+
parser.add_argument("--exclude_session", help="Select a specific session(s) to exclude from the defacing workflow", type=str, nargs="+", required=False, default=[])
896923

897924
arguments = parser.parse_args()
898925
return arguments
@@ -1087,7 +1114,9 @@ def main(): # noqa: max-complexity: 12
10871114
remove_existing=args.remove_existing,
10881115
placement=args.placement,
10891116
preview_pics=args.preview_pics,
1090-
excludeparticipant=args.excludeparticipant,
1117+
exclude_participant=args.exclude_participant,
1118+
session=args.session,
1119+
exclude_session=args.exclude_session,
10911120
)
10921121
petdeface.run()
10931122

0 commit comments

Comments
 (0)