Skip to content

Commit 3bce864

Browse files
committed
added tests for no t1w and invalid bids dir
1 parent f83773a commit 3bce864

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

data/participants.tsv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
participant_id height weight age gender
2-
sub-01 178 58 28 male
2+
sub-01 178 58 28 male

petdeface/petdeface.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ def __init__(
764764
anat_only=False,
765765
subject="",
766766
n_procs=2,
767-
skip_bids_validator=True,
767+
skip_bids_validator=False,
768768
remove_existing=True,
769769
placement="adjacent",
770770
preview_pics=True,

tests/test_dir_layouts.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import shutil
44
import bids
55
from petdeface.petdeface import PetDeface
6+
from petdeface.utils import InvalidBIDSDataset
67
from os import cpu_count
78
from bids.layout import BIDSLayout
89
import subprocess
@@ -34,6 +35,7 @@ def test_anat_in_first_session_folder():
3435
/ "derivatives"
3536
/ "petdeface",
3637
n_procs=nthreads,
38+
preview_pics=False,
3739
)
3840
petdeface.run()
3941

@@ -80,6 +82,7 @@ def test_anat_in_each_session_folder():
8082
/ "derivatives"
8183
/ "petdeface",
8284
n_procs=nthreads,
85+
preview_pics=False,
8386
)
8487
petdeface.run()
8588

@@ -117,5 +120,54 @@ def test_anat_in_subject_folder():
117120
/ "derivatives"
118121
/ "petdeface",
119122
n_procs=nthreads,
123+
preview_pics=False,
120124
)
121125
petdeface.run()
126+
127+
def test_no_anat():
128+
# create a temporary directory to copy the existing dataset into
129+
with tempfile.TemporaryDirectory() as tmpdir:
130+
shutil.copytree(data_dir, Path(tmpdir) / "no_anat")
131+
132+
subject_folder = Path(tmpdir) / "no_anat" / "sub-01"
133+
# next we delete the anat fold in the subject folder
134+
shutil.rmtree(subject_folder / "ses-baseline" / "anat")
135+
136+
# run petdeface on the copied dataset
137+
petdeface = PetDeface(
138+
Path(tmpdir) / "no_anat",
139+
output_dir=Path(tmpdir)
140+
/ "no_anat_defaced"
141+
/ "derivatives"
142+
/ "petdeface",
143+
n_procs=nthreads,
144+
)
145+
146+
# now we want to assert that this pipeline crashes and print the error
147+
with pytest.raises(FileNotFoundError) as exc_info:
148+
petdeface.run()
149+
150+
def test_invalid_bids():
151+
with tempfile.TemporaryDirectory() as tmpdir:
152+
shutil.copytree(data_dir, Path(tmpdir) / "invalid")
153+
# rename the files in the pet folder to a different subject id
154+
subject_folder = Path(tmpdir) / "invalid" / "sub-01"
155+
pet_folder = subject_folder / "ses-baseline" / "pet"
156+
for file in pet_folder.glob("sub-01_*"):
157+
shutil.move(
158+
file,
159+
pet_folder / file.name.replace("sub-01", "sub-01-bestsubject")
160+
)
161+
162+
# run petdeface on the invalid dataset
163+
petdeface = PetDeface(
164+
Path(tmpdir) / "invalid",
165+
output_dir=Path(tmpdir) / "invalid_defaced" / "derivatives" / "petdeface",
166+
n_procs=nthreads,
167+
)
168+
169+
# Run it and see what error gets raised
170+
with pytest.raises(InvalidBIDSDataset) as exc_info:
171+
petdeface.run()
172+
assert "Dataset at" in str(exc_info.value)
173+

0 commit comments

Comments
 (0)