Skip to content

Commit 15940b7

Browse files
committed
FIX: handle better dicom-only conversion
- It won't try to embed files that do not exist - It won't create directories if output is in bids format
1 parent 9a83bfe commit 15940b7

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

heudiconv/convert.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,10 @@ def convert(items, converter, scaninfo_suffix, custom_callable, with_prov,
244244
lgr.info('Converting %s (%d DICOMs) -> %s . '
245245
'Converter: %s . Output types: %s',
246246
prefix, len(item_dicoms), prefix_dirname, converter, outtypes)
247-
if not op.exists(prefix_dirname):
247+
# We want to create this dir only if we are converting it to nifti,
248+
# or if we're using BIDS
249+
dicom_only = len(outtypes) == 1 and outtypes[0] == 'dicom'
250+
if not(dicom_only and bids) and not op.exists(prefix_dirname):
248251
os.makedirs(prefix_dirname)
249252

250253
for outtype in outtypes:
@@ -254,6 +257,9 @@ def convert(items, converter, scaninfo_suffix, custom_callable, with_prov,
254257

255258
seqtype = op.basename(op.dirname(prefix)) if bids else None
256259

260+
# set empty outname and scaninfo in case we only want dicoms
261+
outname = ''
262+
scaninfo = ''
257263
if outtype == 'dicom':
258264
convert_dicom(item_dicoms, bids, prefix,
259265
outdir, tempdirs, symlink, overwrite)
@@ -299,17 +305,17 @@ def convert(items, converter, scaninfo_suffix, custom_callable, with_prov,
299305
"multiple files")
300306
elif not bids_outfiles:
301307
lgr.debug("No BIDS files were produced, nothing to embed to then")
302-
else:
308+
elif outname:
303309
embed_metadata_from_dicoms(bids, item_dicoms, outname, outname_bids,
304310
prov_file, scaninfo, tempdirs, with_prov,
305311
min_meta)
306-
if op.exists(scaninfo):
312+
if scaninfo and op.exists(scaninfo):
307313
lgr.info("Post-treating %s file", scaninfo)
308314
treat_infofile(scaninfo)
309315

310316
# this may not always be the case: ex. fieldmap1, fieldmap2
311317
# will address after refactor
312-
if op.exists(outname):
318+
if outname and op.exists(outname):
313319
set_readonly(outname)
314320

315321
if custom_callable is not None:

tests/test_heuristics.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from heudiconv.cli.run import main as runner
22

33
import os
4+
import os.path as op
45
from mock import patch
56
from six.moves import StringIO
67

@@ -98,7 +99,7 @@ def test_scans_keys_reproin(tmpdir, invocation):
9899
args += invocation
99100
runner(args.split())
100101
# for now check it exists
101-
scans_keys = glob(pjoin(tmpdir.strpath, '*/*/*/*/*.tsv'))
102+
scans_keys = glob(pjoin(tmpdir.strpath, '*/*/*/*/*/*.tsv'))
102103
assert(len(scans_keys) == 1)
103104
with open(scans_keys[0]) as f:
104105
reader = csv.reader(f, delimiter='\t')
@@ -123,3 +124,23 @@ def test_ls(stdout):
123124
out = stdout.getvalue()
124125
assert 'StudySessionInfo(locator=' in out
125126
assert 'Halchenko/Yarik/950_bids_test4' in out
127+
128+
129+
def test_scout_conversion(tmpdir):
130+
args = (
131+
"-b -f reproin --files %s -o %s"
132+
% (TESTS_DATA_PATH, tmpdir)
133+
).split(' ')
134+
runner(args)
135+
tmppath = tmpdir.strpath
136+
137+
assert not op.exists(pjoin(
138+
tmppath,
139+
'Halchenko/Yarik/950_bids_test4/sub-phantom1sid1/ses-localizer/anat'))
140+
141+
assert op.exists(pjoin(
142+
tmppath,
143+
'Halchenko/Yarik/950_bids_test4/sourcedata/sub-phantom1sid1/'
144+
'ses-localizer/anat/sub-phantom1sid1_ses-localizer_scout.dicom.tgz'
145+
)
146+
)

0 commit comments

Comments
 (0)