Skip to content

Commit 88e1fd7

Browse files
committed
test passes
1 parent 363d427 commit 88e1fd7

File tree

2 files changed

+67
-34
lines changed

2 files changed

+67
-34
lines changed

petdeface/petdeface.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,8 +665,27 @@ def wrap_up_defacing(
665665
else:
666666
final_destination = output_dir
667667

668-
# copy original dataset to new location
668+
# copy original dataset to new location, respecting exclusions
669669
for entry in raw_only.files:
670+
# Check if this file belongs to an excluded subject
671+
should_exclude = False
672+
for excluded_subject in participant_label_exclude:
673+
# Handle both cases: excluded_subject with or without 'sub-' prefix
674+
if excluded_subject.startswith('sub-'):
675+
subject_pattern = f"/{excluded_subject}/"
676+
subject_pattern_underscore = f"/{excluded_subject}_"
677+
else:
678+
subject_pattern = f"/sub-{excluded_subject}/"
679+
subject_pattern_underscore = f"/sub-{excluded_subject}_"
680+
681+
if subject_pattern in entry or subject_pattern_underscore in entry:
682+
should_exclude = True
683+
break
684+
685+
# Skip excluded subject files, but copy everything else (including dataset-level files)
686+
if should_exclude:
687+
continue
688+
670689
copy_path = entry.replace(str(path_to_dataset), str(final_destination))
671690
pathlib.Path(copy_path).parent.mkdir(
672691
parents=True, exist_ok=True, mode=0o775

tests/test_dir_layouts.py

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -127,41 +127,55 @@ def test_anat_in_subject_folder():
127127

128128
def test_participant_exclusion():
129129
"""Test that participant exclusion works correctly by excluding sub-02"""
130-
# Use a fixed directory path for caching/faster iteration
131-
test_dir = Path("/tmp/petdeface_test_participant_exclusion")
132-
133-
# Only copy data if it doesn't exist (for caching)
134-
if not test_dir.exists():
135-
test_dir.mkdir(parents=True, exist_ok=True)
130+
with tempfile.TemporaryDirectory() as temp_dir:
131+
test_dir = Path(temp_dir)
132+
133+
# Create the test directory and copy our data
136134
shutil.copytree(data_dir, test_dir / "participant_exclusion")
137135

138-
# run petdeface on the copied dataset, excluding sub-02
139-
petdeface = PetDeface(
140-
test_dir / "participant_exclusion",
141-
output_dir=test_dir / "derivatives" / "petdeface",
142-
n_procs=nthreads,
143-
preview_pics=False,
144-
placement="adjacent",
145-
participant_label_exclude=["sub-02"], # Exclude sub-02
146-
)
147-
petdeface.run()
148-
149-
# Check the final defaced dataset directory
150-
final_defaced_dir = test_dir / "participant_exclusion_defaced"
151-
152-
# Verify that sub-02 does NOT appear anywhere in the final defaced dataset
153-
sub02_entries = list(final_defaced_dir.glob("**/sub-02*"))
154-
assert len(sub02_entries) == 0, f"sub-02 should be completely excluded from final defaced dataset, but found: {sub02_entries}"
155-
156-
# Verify that sub-01 exists and was processed
157-
assert (final_defaced_dir / "sub-01").exists(), "sub-01 should exist in final defaced dataset"
158-
159-
# Verify processing artifacts exist for sub-01
160-
derivatives_dir = final_defaced_dir / "derivatives" / "petdeface"
161-
sub01_defacemasks = list(derivatives_dir.glob("**/sub-01*defacemask*"))
162-
sub01_lta_files = list(derivatives_dir.glob("**/sub-01*.lta"))
163-
assert len(sub01_defacemasks) > 0, "sub-01 should have been processed and have defacemasks"
164-
assert len(sub01_lta_files) > 0, "sub-01 should have been processed and have lta registration files"
136+
# run petdeface on the copied dataset, excluding sub-02
137+
petdeface = PetDeface(
138+
test_dir / "participant_exclusion",
139+
n_procs=nthreads,
140+
preview_pics=False,
141+
placement="adjacent",
142+
participant_label_exclude=["sub-02"], # Exclude sub-02
143+
)
144+
petdeface.run()
145+
146+
# Check the final defaced dataset directory
147+
final_defaced_dir = test_dir / "participant_exclusion_defaced"
148+
149+
# Count files in the final defaced dataset
150+
all_files = list(final_defaced_dir.rglob("*"))
151+
all_files = [f for f in all_files if f.is_file()] # Only files, not directories
152+
153+
# Count files by subject
154+
sub01_files = [f for f in all_files if "sub-01" in str(f)]
155+
sub02_files = [f for f in all_files if "sub-02" in str(f)]
156+
157+
print(f"Total files in defaced dataset: {len(all_files)}")
158+
print(f"sub-01 files: {len(sub01_files)}")
159+
print(f"sub-02 files: {len(sub02_files)}")
160+
161+
# Verify that sub-02 does NOT appear anywhere in the final defaced dataset
162+
assert len(sub02_files) == 0, f"sub-02 should be completely excluded from final defaced dataset, but found {len(sub02_files)} files: {[str(f) for f in sub02_files]}"
163+
164+
# Verify that sub-01 exists and was processed
165+
assert len(sub01_files) > 0, "sub-01 should exist in final defaced dataset"
166+
assert (final_defaced_dir / "sub-01").exists(), "sub-01 directory should exist in final defaced dataset"
167+
168+
# Verify processing artifacts exist for sub-01 in derivatives
169+
derivatives_dir = final_defaced_dir / "derivatives" / "petdeface"
170+
if derivatives_dir.exists():
171+
sub01_defacemasks = list(derivatives_dir.glob("**/sub-01*defacemask*"))
172+
sub01_lta_files = list(derivatives_dir.glob("**/sub-01*.lta"))
173+
174+
print(f"sub-01 defacemasks found: {len(sub01_defacemasks)}")
175+
print(f"sub-01 lta files found: {len(sub01_lta_files)}")
176+
177+
assert len(sub01_defacemasks) > 0, "sub-01 should have been processed and have defacemasks"
178+
assert len(sub01_lta_files) > 0, "sub-01 should have been processed and have lta registration files"
165179

166180

167181
def test_no_anat():

0 commit comments

Comments
 (0)