Skip to content

Commit 9c62046

Browse files
authored
Merge pull request #151 from dartmouth-pbs/fix/emptydir2
FIX,TST: better handling of dicom-only conversion
2 parents 9a83bfe + f0133a2 commit 9c62046

File tree

4 files changed

+53
-17
lines changed

4 files changed

+53
-17
lines changed

heudiconv/convert.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,15 +236,18 @@ def convert(items, converter, scaninfo_suffix, custom_callable, with_prov,
236236

237237
prefix, outtypes, item_dicoms = item[:3]
238238
if not isinstance(outtypes, (list, tuple)):
239-
outtypes = [outtypes]
239+
outtypes = (outtypes,)
240240

241241
prefix_dirname = op.dirname(prefix + '.ext')
242242
outname_bids = prefix + '.json'
243243
bids_outfiles = []
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 = outtypes == ('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/data/01-anat-scout/0001.dcm

170 KB
Binary file not shown.

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+
tmppath = tmpdir.strpath
131+
args = (
132+
"-b -f reproin --files %s"
133+
% (TESTS_DATA_PATH)
134+
).split(' ') + ['-o', tmppath]
135+
runner(args)
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+
)

tests/test_monitor.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,26 @@
66
from tinydb import TinyDB, Query
77
from subprocess import CalledProcessError
88

9-
from heudiconv.cli.monitor import monitor, process, run_heudiconv, MASK_NEWDIR
9+
try:
10+
from heudiconv.cli.monitor import (monitor, process, run_heudiconv,
11+
MASK_NEWDIR)
12+
13+
Header = namedtuple('header', ['wd', 'mask', 'cookie', 'len'])
14+
header = Header(5, MASK_NEWDIR, 5, 5)
15+
watch_path = b'WATCHME'
16+
filename = b'FILE'
17+
type_names = b'TYPE'
18+
19+
path2 = watch_path + b'/' + filename + b'/subpath'
20+
21+
my_events = [(header, type_names, watch_path, filename),
22+
(header, type_names, path2, b'')]
23+
except AttributeError:
24+
# Import of inotify fails on mac os x with error
25+
# lsym(0x11fbeb780, inotify_init): symbol not found
26+
# because inotify doesn't seem to exist on Mac OS X
27+
my_events = []
28+
pytestmark = pytest.mark.skip(reason='Unable to import inotify')
1029

1130

1231
class MockInotifyTree(object):
@@ -26,16 +45,6 @@ def __call__(self):
2645
return self.time
2746

2847

29-
Header = namedtuple('header', ['wd', 'mask', 'cookie', 'len'])
30-
header = Header(5, MASK_NEWDIR, 5, 5)
31-
watch_path = b'WATCHME'
32-
filename = b'FILE'
33-
type_names = b'TYPE'
34-
35-
path2 = watch_path + b'/' + filename + b'/subpath'
36-
37-
my_events = [(header, type_names, watch_path, filename),
38-
(header, type_names, path2, b'')]
3948

4049
@pytest.mark.skip(reason="TODO")
4150
@patch('inotify.adapters.InotifyTree', MockInotifyTree(my_events))

0 commit comments

Comments
 (0)