Skip to content

Commit

Permalink
Merge pull request #547 from cbinyu/fix_echo_order_in_filename
Browse files Browse the repository at this point in the history
Add test for the dataset that raised #541
  • Loading branch information
yarikoptic authored Apr 5, 2022
2 parents 772feeb + aee3d9b commit a1f63de
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
9 changes: 8 additions & 1 deletion heudiconv/heuristics/bids_ME.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@ def infotodict(seqinfo):
subindex: sub index within group
"""
bold = create_key('sub-{subject}/func/sub-{subject}_task-test_run-{item}_bold')
megre_mag = create_key('sub-{subject}/anat/sub-{subject}_part-mag_MEGRE')
megre_phase = create_key('sub-{subject}/anat/sub-{subject}_part-phase_MEGRE')

info = {bold: []}
info = {bold: [], megre_mag: [], megre_phase: []}
for s in seqinfo:
if '_ME_' in s.series_description:
info[bold].append(s.series_id)
if 'GRE_QSM' in s.series_description:
if s.image_type[2] == 'M':
info[megre_mag].append(s.series_id)
elif s.image_type[2] == 'P':
info[megre_phase].append(s.series_id)
return info
39 changes: 39 additions & 0 deletions heudiconv/tests/test_bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,23 @@
KeyInfoForForce,
BIDSFile,
)
from heudiconv.cli.run import main as runner

from .utils import (
fetch_data,
gen_heudiconv_args,
TESTS_DATA_PATH,
)

import pytest

have_datalad = True
try:
from datalad.support.exceptions import IncompleteResultsError
except ImportError:
have_datalad = False


def gen_rand_label(label_size, label_seed, seed_stdout=True):
seed(label_seed)
rand_char = ''.join(choice(string.ascii_letters) for _ in range(label_size-1))
Expand Down Expand Up @@ -1116,3 +1126,32 @@ def test_BIDSFile():
# -for an existing entity, you can overwrite it with "set":
my_bids_file.set('echo', '2')
assert my_bids_file['echo'] == '2'


@pytest.mark.skipif(not have_datalad, reason="no datalad")
def test_ME_mag_phase_conversion(tmpdir, subID='MEGRE', heuristic='bids_ME.py'):
""" Unit test for the case of multi-echo GRE data with
magnitude and phase.
The different echoes should be labeled automatically.
"""
tmpdir.chdir()
tmppath = tmpdir.strpath
try:
datadir = fetch_data(tmppath, f"dicoms/velasco/{subID}")
except IncompleteResultsError as exc:
pytest.skip("Failed to fetch test data: %s" % str(exc))

outdir = tmpdir.mkdir('out').strpath
args = gen_heudiconv_args(datadir, outdir, subID, heuristic)
runner(args) # run conversion

# Check that the expected files have been extracted.
# This also checks that the "echo" entity comes before "part":
for part in ['mag', 'phase']:
for e in range(1,4):
for ext in ['nii.gz', 'json']:
assert op.exists(
op.join(outdir, 'sub-%s', 'anat', 'sub-%s_echo-%s_part-%s_MEGRE.%s')
% (subID, subID, e, part, ext)
)

0 comments on commit a1f63de

Please sign in to comment.