|
2 | 2 | from glob import glob |
3 | 3 | import os |
4 | 4 | import os.path as op |
| 5 | +import re |
5 | 6 |
|
6 | 7 | import pytest |
7 | 8 |
|
|
19 | 20 |
|
20 | 21 |
|
21 | 22 | @pytest.mark.skipif(not have_datalad, reason="no datalad") |
22 | | -@pytest.mark.parametrize('subject', ['sub-sid000143']) |
| 23 | +@pytest.mark.parametrize('subject', ['sid000143']) |
23 | 24 | @pytest.mark.parametrize('heuristic', ['reproin.py']) |
24 | 25 | @pytest.mark.parametrize('anon_cmd', [None, 'anonymize_script.py']) |
25 | 26 | def test_conversion(tmpdir, subject, heuristic, anon_cmd): |
26 | 27 | tmpdir.chdir() |
27 | 28 | try: |
28 | 29 | datadir = fetch_data(tmpdir.strpath, |
29 | 30 | "dbic/QA", # path from datalad database root |
30 | | - getpath=op.join('sourcedata', subject)) |
| 31 | + getpath=op.join('sourcedata', f'sub-{subject}')) |
31 | 32 | except IncompleteResultsError as exc: |
32 | 33 | pytest.skip("Failed to fetch test data: %s" % str(exc)) |
33 | 34 | outdir = tmpdir.mkdir('out').strpath |
34 | 35 |
|
35 | 36 | args = gen_heudiconv_args( |
36 | 37 | datadir, outdir, subject, heuristic, anon_cmd, |
37 | | - template=op.join('sourcedata/{subject}/*/*/*.tgz') |
| 38 | + template='sourcedata/sub-{subject}/*/*/*.tgz' |
38 | 39 | ) |
39 | 40 | runner(args) # run conversion |
40 | 41 |
|
| 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 | + |
41 | 53 | # 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 |
46 | 65 |
|
47 | 66 | # compare some json metadata |
48 | 67 | json_ = '{}/task-rest_acq-24mm64sl1000tr32te600dyn_bold.json'.format |
|
0 commit comments