|
| 1 | +"""Testing conversion with conversion saved on datalad""" |
| 2 | +from tempfile import mkdtemp |
| 3 | +import os |
| 4 | +import os.path as op |
| 5 | +import json |
| 6 | +from glob import glob |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +have_datalad = True |
| 11 | +try: |
| 12 | + from datalad import api # to pull and grab data |
| 13 | +except ImportError: |
| 14 | + have_datalad = False |
| 15 | + |
| 16 | +from heudiconv.cli.run import main as runner |
| 17 | +import heudiconv |
| 18 | + |
| 19 | + |
| 20 | +def gen_heudiconv_args(datadir, outdir, subject, heuristic_file, xargs=None): |
| 21 | + heuristic = op.realpath(op.join( |
| 22 | + op.dirname(heudiconv.__file__), |
| 23 | + '..', |
| 24 | + 'heuristics', |
| 25 | + heuristic_file)) |
| 26 | + |
| 27 | + args = ["-d", op.join(datadir, 'sourcedata/{subject}/*/*/*.tgz'), |
| 28 | + "-c", "dcm2niix", |
| 29 | + "-o", outdir, |
| 30 | + "-s", subject, |
| 31 | + "-f", heuristic, |
| 32 | + "--bids", |
| 33 | + ] |
| 34 | + if xargs: |
| 35 | + args += xargs |
| 36 | + |
| 37 | + return args |
| 38 | + |
| 39 | +def fetch_data(tmpdir, subject): |
| 40 | + """Fetches some test dicoms""" |
| 41 | + targetdir = os.path.join(tmpdir, 'QA') |
| 42 | + api.install(path=targetdir, |
| 43 | + source='///dbic/QA') |
| 44 | + api.get('{}/sourcedata/{}'.format(targetdir, subject)) |
| 45 | + return targetdir |
| 46 | + |
| 47 | +@pytest.mark.parametrize('subject', ['sub-sid000143']) |
| 48 | +@pytest.mark.parametrize('heuristic', ['dbic_bids.py']) |
| 49 | +@pytest.mark.skipif(not have_datalad, reason="no datalad") |
| 50 | +def test_conversion(tmpdir, subject, heuristic): |
| 51 | + tmpdir.chdir() |
| 52 | + datadir = fetch_data(tmpdir.strpath, subject) |
| 53 | + outdir = tmpdir.mkdir('out').strpath |
| 54 | + |
| 55 | + args = gen_heudiconv_args(datadir, outdir, subject, heuristic) |
| 56 | + runner(args) # run conversion |
| 57 | + |
| 58 | + # verify functionals were converted |
| 59 | + assert glob('{}/{}/func/*'.format(outdir, subject)) == \ |
| 60 | + glob('{}/{}/func/*'.format(datadir, subject)) |
| 61 | + |
| 62 | + # compare some json metadata |
| 63 | + json_ = '{}/task-rest_acq-24mm64sl1000tr32te600dyn_bold.json'.format |
| 64 | + orig, conv = (json.load(open(json_(datadir))), |
| 65 | + json.load(open(json_(outdir)))) |
| 66 | + keys = ['EchoTime', 'MagneticFieldStrength', 'Manufacturer', 'SliceTiming'] |
| 67 | + for key in keys: |
| 68 | + assert orig[key] == conv[key] |
0 commit comments