Skip to content

Commit e11518a

Browse files
committed
Make sure only dwi images get *.bvec and *.bval files
`dcm2niix` produces *.bvec/*.bval files whenever it extracts an image collected with a DW sequence. However, BIDS only accepts those files in the `dwi` folders.
1 parent f757dd8 commit e11518a

File tree

4 files changed

+72
-3
lines changed

4 files changed

+72
-3
lines changed

heudiconv/convert.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -674,9 +674,14 @@ def save_converted_files(res, item_dicoms, bids_options, outtype, prefix, outnam
674674
return
675675

676676
if isdefined(res.outputs.bvecs) and isdefined(res.outputs.bvals):
677-
outname_bvecs, outname_bvals = prefix + '.bvec', prefix + '.bval'
678-
safe_movefile(res.outputs.bvecs, outname_bvecs, overwrite)
679-
safe_movefile(res.outputs.bvals, outname_bvals, overwrite)
677+
if prefix_dirname.endswith('dwi'):
678+
outname_bvecs, outname_bvals = prefix + '.bvec', prefix + '.bval'
679+
safe_movefile(res.outputs.bvecs, outname_bvecs, overwrite)
680+
safe_movefile(res.outputs.bvals, outname_bvals, overwrite)
681+
else:
682+
os.remove(res.outputs.bvecs)
683+
os.remove(res.outputs.bvals)
684+
lgr.debug("%s and %s were removmed", res.outputs.bvecs, res.outputs.bvals)
680685

681686
if isinstance(res_files, list):
682687
res_files = sorted(res_files)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""Heuristic to extract a b-value=0 DWI image (basically, a SE-EPI)
2+
both as a fmap and as dwi
3+
4+
It is used just to test that a 'DIFFUSION' image that the user
5+
chooses to extract as fmap (pepolar case) doesn't produce _bvecs/
6+
_bvals json files, while it does for dwi images
7+
"""
8+
9+
10+
def create_key(template, outtype=('nii.gz',), annotation_classes=None):
11+
if template is None or not template:
12+
raise ValueError('Template must be a valid format string')
13+
return template, outtype, annotation_classes
14+
15+
def infotodict(seqinfo):
16+
"""Heuristic evaluator for determining which runs belong where
17+
18+
allowed template fields - follow python string module:
19+
20+
item: index within category
21+
subject: participant id
22+
seqitem: run number during scanning
23+
subindex: sub index within group
24+
"""
25+
fmap = create_key('sub-{subject}/fmap/sub-{subject}_acq-b0dwi_epi')
26+
dwi = create_key('sub-{subject}/dwi/sub-{subject}_acq-b0dwi_dwi')
27+
28+
info = {fmap: [], dwi: []}
29+
for s in seqinfo:
30+
if 'DIFFUSION' in s.image_type and 'FieldMap' in s.series_description:
31+
info[fmap].append(s.series_id)
32+
info[dwi].append(s.series_id)
33+
return info
Binary file not shown.

heudiconv/tests/test_convert.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
"""Test functions in heudiconv.convert module.
22
"""
3+
import os.path as op
4+
from glob import glob
5+
36
import pytest
7+
from .utils import TESTS_DATA_PATH
48

59
from heudiconv.convert import (update_complex_name,
610
update_multiecho_name,
711
update_uncombined_name)
812
from heudiconv.bids import BIDSError
13+
from heudiconv.cli.run import main as runner
914

1015

1116
def test_update_complex_name():
@@ -76,3 +81,29 @@ def test_update_uncombined_name():
7681
out_fn_true = 'sub-X_ses-Y_task-Z_run-01_ch-04_bold'
7782
out_fn_test = update_uncombined_name(metadata, fn, channel_names)
7883
assert out_fn_test == out_fn_true
84+
85+
86+
def test_b0dwi_for_fmap(tmpdir):
87+
"""Make sure that we don't copy .bvec and .bval files
88+
when the modality is not dwi.
89+
We check it by extracting a b-value=0 dwi image as fmap and dwi
90+
"""
91+
tmppath = tmpdir.strpath
92+
subID = 'b0dwiForFmap'
93+
args = (
94+
"-c dcm2niix -o %s -b -f test_b0dwi_for_fmap --files %s -s %s"
95+
% (tmpdir, op.join(TESTS_DATA_PATH, 'b0dwiForFmap'), subID)
96+
).split(' ')
97+
runner(args)
98+
99+
# check that the fmap directory has been extracted and it doesn't contain
100+
# any *.bvec or *.bval files
101+
assert op.isdir(op.join(tmppath, 'sub-%s', 'fmap') % (subID))
102+
for ext in ['bval', 'bvec']:
103+
assert not glob(op.join(tmppath, 'sub-%s', 'fmap', 'sub-%s_*.%s') % (subID, subID, ext))
104+
105+
# check that the dwi directory has been extracted and it does contain a
106+
# *.bvec and a *.bval files
107+
assert op.isdir(op.join(tmppath, 'sub-%s', 'dwi') % (subID))
108+
for ext in ['bval', 'bvec']:
109+
assert glob(op.join(tmppath, 'sub-%s', 'dwi', 'sub-%s_*.%s') % (subID, subID, ext))

0 commit comments

Comments
 (0)