Skip to content

Commit

Permalink
added exclude list as well
Browse files Browse the repository at this point in the history
  • Loading branch information
bendhouseart committed Nov 8, 2024
1 parent ad16144 commit c15bcb4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
53 changes: 49 additions & 4 deletions petdeface/petdeface.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,42 @@ def deface(args: Union[dict, argparse.Namespace]) -> None:
if not check_valid_fs_license():
raise Exception("You need a valid FreeSurfer license to proceed!")

if args.participant_label:
participants = [args.participant_label]
else:
if args.subject:
subjects = args.subject
# if subject contains the string sub-, remove it to avoid redundancy as pybids will add it uses the
# right side of the sub- string as the subject label
if any("sub-" in subject for subject in subjects):
print("One or more subject contains sub- string")
subjects = [
subject.replace("sub-", "") for subject in subjects if "sub-" in subject
]
# raise error if a supplied subject is not contained in the dataset
participants = collect_participants(
args.bids_dir, bids_validate=~args.skip_bids_validator
)
for subject in subjects:
if subject not in participants:
raise FileNotFoundError(
f"sub-{subject} not found in dataset {args.bids_dir}"
)
else:
subjects = collect_participants(
args.bids_dir, bids_validate=~args.skip_bids_validator
)

# check to see if any subjects are excluded from the defacing workflow
if args.excludesubject != []:
print(
f"Removing the following subjects {args.excludesubject} from the defacing workflow"
)
args.excludesubject = [
subject.replace("sub-", "") for subject in args.excludesubject
]
subjects = [
subject for subject in subjects if subject not in args.excludesubject
]

print(f"Subjects remaining in the defacing workflow: {subjects}")

# clean up and create derivatives directories
if args.output_dir == "None" or args.output_dir is None:
Expand All @@ -216,7 +246,7 @@ def deface(args: Union[dict, argparse.Namespace]) -> None:

petdeface_wf = Workflow(name="petdeface_wf", base_dir=output_dir)

for subject_id in participants:
for subject_id in subjects:
try:
single_subject_wf = init_single_subject_wf(
subject_id, args.bids_dir, preview_pics=args.preview_pics
Expand Down Expand Up @@ -268,6 +298,8 @@ def init_single_subject_wf(

data = collect_anat_and_pet(bids_data)
subject_data = data.get(subject_id)
if subject_data is None:
raise FileNotFoundError(f"Could not find data for subject sub-{subject_id}")

# check if any t1w images exist for the pet images
for pet_image, t1w_image in subject_data.items():
Expand Down Expand Up @@ -672,6 +704,7 @@ def __init__(
remove_existing=True, # TODO: currently not implemented
placement="adjacent", # TODO: currently not implemented
preview_pics=True,
excludesubject=[],
):
self.bids_dir = bids_dir
self.remove_existing = remove_existing
Expand All @@ -683,6 +716,7 @@ def __init__(
self.n_procs = n_procs
self.skip_bids_validator = skip_bids_validator
self.preview_pics = preview_pics
self.excludesubject = excludesubject

# check if freesurfer license is valid
self.fs_license = check_valid_fs_license()
Expand All @@ -707,6 +741,7 @@ def run(self):
"placement": self.placement,
"remove_existing": self.remove_existing,
"preview_pics": self.preview_pics,
"excludesubject": self.excludesubject,
}
)
wrap_up_defacing(
Expand Down Expand Up @@ -746,6 +781,7 @@ def cli():
"-s",
help="The label of the subject to be processed.",
type=str,
nargs="+",
required=False,
default="",
)
Expand Down Expand Up @@ -800,6 +836,14 @@ def cli():
action="store_true",
default=False,
)
parser.add_argument(
"--excludesubject",
help="Exclude a subject(s) from the defacing workflow. e.g. --excludesubject sub-01 sub-02",
type=str,
nargs="+",
required=False,
default=[],
)

arguments = parser.parse_args()
return arguments
Expand Down Expand Up @@ -928,6 +972,7 @@ def main(): # noqa: max-complexity: 12
remove_existing=args.remove_existing,
placement=args.placement,
preview_pics=args.preview_pics,
excludesubject=args.excludesubject,
)
petdeface.run()

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "petdeface"
version = "0.1.1"
version = "0.1.2"
description = "A nipype PET and MR defacing pipeline for BIDS datasets utilizing FreeSurfer's MiDeFace."
authors = ["Martin Nørgaard <[email protected]>", "Anthony Galassi <[email protected]>", "Murat Bilgel <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit c15bcb4

Please sign in to comment.