Skip to content

Commit 1d6b41b

Browse files
committed
BF(TST): fix the test for annonimization so it actually tests
The problem was that the glob expressions were off and no files matched in either case and then empty lists matched just fine. I have fixed up the test so it - does verify that anonimization changed the subject id if anonimization script was provided, and does not if it was not - also converted to use f""strings instead of .format
1 parent 74550b8 commit 1d6b41b

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

heudiconv/tests/test_regression.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from glob import glob
33
import os
44
import os.path as op
5+
import re
56

67
import pytest
78

@@ -19,30 +20,48 @@
1920

2021

2122
@pytest.mark.skipif(not have_datalad, reason="no datalad")
22-
@pytest.mark.parametrize('subject', ['sub-sid000143'])
23+
@pytest.mark.parametrize('subject', ['sid000143'])
2324
@pytest.mark.parametrize('heuristic', ['reproin.py'])
2425
@pytest.mark.parametrize('anon_cmd', [None, 'anonymize_script.py'])
2526
def test_conversion(tmpdir, subject, heuristic, anon_cmd):
2627
tmpdir.chdir()
2728
try:
2829
datadir = fetch_data(tmpdir.strpath,
2930
"dbic/QA", # path from datalad database root
30-
getpath=op.join('sourcedata', subject))
31+
getpath=op.join('sourcedata', f'sub-{subject}'))
3132
except IncompleteResultsError as exc:
3233
pytest.skip("Failed to fetch test data: %s" % str(exc))
3334
outdir = tmpdir.mkdir('out').strpath
3435

3536
args = gen_heudiconv_args(
3637
datadir, outdir, subject, heuristic, anon_cmd,
37-
template=op.join('sourcedata/{subject}/*/*/*.tgz')
38+
template='sourcedata/sub-{subject}/*/*/*.tgz'
3839
)
3940
runner(args) # run conversion
4041

42+
# Get the possibly anonymized subject id and verify that it was
43+
# anonymized or not:
44+
subject_maybe_anon = glob(f'{outdir}/sub-*')
45+
assert len(subject_maybe_anon) == 1 # just one should be there
46+
subject_maybe_anon = op.basename(subject_maybe_anon[0])[4:]
47+
48+
if anon_cmd:
49+
assert subject_maybe_anon != subject
50+
else:
51+
assert subject_maybe_anon == subject
52+
4153
# verify functionals were converted
42-
assert (
43-
glob('{}/{}/func/*'.format(outdir, subject)) ==
44-
glob('{}/{}/func/*'.format(datadir, subject))
45-
)
54+
outfiles = sorted([f[len(outdir):] for f in glob(f'{outdir}/sub-{subject_maybe_anon}/func/*')])
55+
assert outfiles
56+
datafiles = sorted([f[len(datadir):] for f in glob(f'{datadir}/sub-{subject}/ses-*/func/*')])
57+
# original data has ses- but because we are converting only func, and not
58+
# providing any session, we will not "match". Let's strip away the session
59+
datafiles = [re.sub(r'[/\\_]ses-[^/\\_]*', '', f) for f in datafiles]
60+
if not anon_cmd:
61+
assert outfiles == datafiles
62+
else:
63+
assert outfiles != datafiles # sid was anonymized
64+
assert len(outfiles) == len(datafiles) # but we have the same number of files
4665

4766
# compare some json metadata
4867
json_ = '{}/task-rest_acq-24mm64sl1000tr32te600dyn_bold.json'.format

0 commit comments

Comments
 (0)