Skip to content

Commit 8be2e9b

Browse files
authored
Merge pull request #306 from mgxd/fix/embed
fix: embedding issue
2 parents b85e5e1 + f1d7f44 commit 8be2e9b

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

heudiconv/dicoms.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ def _assign_dicom_time(ti):
353353
return outtar
354354

355355

356-
def embed_nifti(dcmfiles, niftifile, infofile, bids_info, force, min_meta):
356+
def embed_nifti(dcmfiles, niftifile, infofile, bids_info, min_meta):
357357
"""
358358
359359
If `niftifile` doesn't exist, it gets created out of the `dcmfiles` stack,
@@ -370,7 +370,6 @@ def embed_nifti(dcmfiles, niftifile, infofile, bids_info, force, min_meta):
370370
niftifile
371371
infofile
372372
bids_info
373-
force
374373
min_meta
375374
376375
Returns
@@ -387,10 +386,11 @@ def embed_nifti(dcmfiles, niftifile, infofile, bids_info, force, min_meta):
387386

388387
if not min_meta:
389388
import dcmstack as ds
390-
stack = ds.parse_and_stack(dcmfiles, force=force).values()
389+
stack = ds.parse_and_stack(dcmfiles, force=True).values()
391390
if len(stack) > 1:
392391
raise ValueError('Found multiple series')
393-
stack = stack[0]
392+
# may be odict now - iter to be safe
393+
stack = next(iter(stack))
394394

395395
#Create the nifti image using the data array
396396
if not op.exists(niftifile):
@@ -458,21 +458,18 @@ def embed_metadata_from_dicoms(bids, item_dicoms, outname, outname_bids,
458458
item_dicoms = list(map(op.abspath, item_dicoms))
459459

460460
embedfunc = Node(Function(input_names=['dcmfiles', 'niftifile', 'infofile',
461-
'bids_info', 'force', 'min_meta'],
461+
'bids_info', 'min_meta'],
462462
output_names=['outfile', 'meta'],
463463
function=embed_nifti),
464464
name='embedder')
465465
embedfunc.inputs.dcmfiles = item_dicoms
466466
embedfunc.inputs.niftifile = op.abspath(outname)
467467
embedfunc.inputs.infofile = op.abspath(scaninfo)
468468
embedfunc.inputs.min_meta = min_meta
469-
if bids:
470-
embedfunc.inputs.bids_info = load_json(op.abspath(outname_bids))
471-
else:
472-
embedfunc.inputs.bids_info = None
473-
embedfunc.inputs.force = True
469+
embedfunc.inputs.bids_info = load_json(op.abspath(outname_bids)) if bids else None
474470
embedfunc.base_dir = tmpdir
475471
cwd = os.getcwd()
472+
476473
lgr.debug("Embedding into %s based on dicoms[0]=%s for nifti %s",
477474
scaninfo, item_dicoms[0], outname)
478475
try:

heudiconv/tests/test_dicoms.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import os.path as op
2+
import json
23

34
import pytest
45

56
from heudiconv.external.pydicom import dcm
67
from heudiconv.cli.run import main as runner
7-
from heudiconv.dicoms import parse_private_csa_header
8+
from heudiconv.dicoms import parse_private_csa_header, embed_nifti
89
from .utils import TESTS_DATA_PATH
910

1011
# Public: Private DICOM tags
@@ -23,3 +24,37 @@ def test_private_csa_header(tmpdir):
2324
assert parse_private_csa_header(dcm_data, pub, priv) != ''
2425
# and quickly run heudiconv with no conversion
2526
runner(['--files', dcm_file, '-c' 'none', '-f', 'reproin'])
27+
28+
29+
def test_nifti_embed(tmpdir):
30+
"""Test dcmstack's additional fields"""
31+
tmpdir.chdir()
32+
# set up testing files
33+
dcmfiles = [op.join(TESTS_DATA_PATH, 'axasc35.dcm')]
34+
infofile = 'infofile.json'
35+
36+
# 1) nifti does not exist
37+
out = embed_nifti(dcmfiles, 'nifti.nii', 'infofile.json', None, False)
38+
# string -> json
39+
out = json.loads(out)
40+
# should have created nifti file
41+
assert op.exists('nifti.nii')
42+
43+
# 2) nifti exists
44+
nifti, info = embed_nifti(dcmfiles, 'nifti.nii', 'infofile.json', None, False)
45+
assert op.exists(nifti)
46+
assert op.exists(info)
47+
with open(info) as fp:
48+
out2 = json.load(fp)
49+
50+
assert out == out2
51+
52+
# 3) with existing metadata
53+
bids = {"existing": "data"}
54+
nifti, info = embed_nifti(dcmfiles, 'nifti.nii', 'infofile.json', bids, False)
55+
with open(info) as fp:
56+
out3 = json.load(fp)
57+
58+
assert out3["existing"]
59+
del out3["existing"]
60+
assert out3 == out2 == out

0 commit comments

Comments
 (0)