Skip to content

Commit 55c7930

Browse files
committed
RF (not backward compatible): embed_nifti -> embed_dicom_and_nifti_metadata
That function was returning a new nifti constructed from dicoms if nifti file did not exist before. That lead to also different returns in comparison to operation whenever used on an existing nifti file. All conversions to nifti should be done centrally, using dcm2niix (via nipype_convert) and we should avoid possible other tool to create nifti files -- could lead to inconsistencies etc. Now that the function does not produce those files or changes those passed names, there is no need for it to return anything. So it would return None. Test adjusted accordingly to use nipype_convert
1 parent b8119dc commit 55c7930

File tree

2 files changed

+34
-38
lines changed

2 files changed

+34
-38
lines changed

heudiconv/dicoms.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -386,14 +386,10 @@ def _assign_dicom_time(ti):
386386
return outtar
387387

388388

389-
def embed_nifti(dcmfiles, niftifile, infofile, bids_info):
390-
"""
391-
392-
If `niftifile` doesn't exist, it gets created out of the `dcmfiles` stack,
393-
and json representation of its meta_ext is returned (bug since should return
394-
both niftifile and infofile?)
389+
def embed_dicom_and_nifti_metadata(dcmfiles, niftifile, infofile, bids_info):
390+
"""Embed metadata from nifti (affine etc) and dicoms into infofile (json)
395391
396-
if `niftifile` exists, its affine's orientation information is used while
392+
`niftifile` should exist. Its affine's orientation information is used while
397393
establishing new `NiftiImage` out of dicom stack and together with `bids_info`
398394
(if provided) is dumped into json `infofile`
399395
@@ -402,11 +398,11 @@ def embed_nifti(dcmfiles, niftifile, infofile, bids_info):
402398
dcmfiles
403399
niftifile
404400
infofile
405-
bids_info
406-
407-
Returns
408-
-------
409-
niftifile, infofile
401+
bids_info: dict
402+
Additional metadata to be embedded. `infofile` is overwritten if exists,
403+
so here you could pass some metadata which would overload (at the first
404+
level of the dict structure, no recursive fancy updates) what is obtained
405+
from nifti and dicoms
410406
411407
"""
412408
# imports for nipype
@@ -422,18 +418,21 @@ def embed_nifti(dcmfiles, niftifile, infofile, bids_info):
422418
# may be odict now - iter to be safe
423419
stack = next(iter(stack))
424420

425-
# Create the nifti image using the data array
426421
if not op.exists(niftifile):
427-
nifti_image = stack.to_nifti(embed_meta=True)
428-
nifti_image.to_filename(niftifile)
429-
return ds.NiftiWrapper(nifti_image).meta_ext.to_json()
422+
raise NotImplementedError(
423+
"%s does not exist. "
424+
"We are not producing new nifti files here any longer. "
425+
"Use dcm2niix directly or .convert.nipype_convert helper ."
426+
% niftifile
427+
)
430428

431429
orig_nii = nb.load(niftifile)
432430
aff = orig_nii.affine
433431
ornt = nb.orientations.io_orientation(aff)
434432
axcodes = nb.orientations.ornt2axcodes(ornt)
435433
new_nii = stack.to_nifti(voxel_order=''.join(axcodes), embed_meta=True)
436434
meta_info = ds.NiftiWrapper(new_nii).meta_ext.to_json()
435+
meta_info = json.loads(meta_info)
437436

438437
if bids_info:
439438
# make nice with python 3 - same behavior?
@@ -444,8 +443,6 @@ def embed_nifti(dcmfiles, niftifile, infofile, bids_info):
444443
with open(infofile, 'wt') as fp:
445444
json.dump(meta_info, fp, indent=3, sort_keys=True)
446445

447-
return niftifile, infofile
448-
449446

450447
def embed_metadata_from_dicoms(bids_options, item_dicoms, outname, outname_bids,
451448
prov_file, scaninfo, tempdirs, with_prov):
@@ -476,7 +473,7 @@ def embed_metadata_from_dicoms(bids_options, item_dicoms, outname, outname_bids,
476473
embedfunc = Node(Function(input_names=['dcmfiles', 'niftifile', 'infofile',
477474
'bids_info',],
478475
output_names=['outfile', 'meta'],
479-
function=embed_nifti),
476+
function=embed_dicom_and_nifti_metadata),
480477
name='embedder')
481478
embedfunc.inputs.dcmfiles = item_dicoms
482479
embedfunc.inputs.niftifile = op.abspath(outname)

heudiconv/tests/test_dicoms.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
from heudiconv.external.pydicom import dcm
77
from heudiconv.cli.run import main as runner
8-
from heudiconv.dicoms import parse_private_csa_header, embed_nifti
8+
from heudiconv.convert import nipype_convert
9+
from heudiconv.dicoms import parse_private_csa_header, embed_dicom_and_nifti_metadata
910
from .utils import TESTS_DATA_PATH
1011

1112
# Public: Private DICOM tags
@@ -26,35 +27,33 @@ def test_private_csa_header(tmpdir):
2627
runner(['--files', dcm_file, '-c' 'none', '-f', 'reproin'])
2728

2829

29-
def test_nifti_embed(tmpdir):
30+
def test_embed_dicom_and_nifti_metadata(tmpdir):
3031
"""Test dcmstack's additional fields"""
3132
tmpdir.chdir()
3233
# set up testing files
3334
dcmfiles = [op.join(TESTS_DATA_PATH, 'axasc35.dcm')]
3435
infofile = 'infofile.json'
3536

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')
37+
# 1) nifti does not exist -- no longer supported
38+
# we should produce nifti using our "standard" ways
39+
out_prefix = str(tmpdir / "nifti")
40+
nipype_out, prov_file = nipype_convert(
41+
dcmfiles, prefix=out_prefix, with_prov=False,
42+
bids_options=None, tmpdir=str(tmpdir))
43+
niftifile = nipype_out.outputs.converted_files
4244

45+
assert op.exists(niftifile)
4346
# 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:
47+
embed_dicom_and_nifti_metadata(dcmfiles, niftifile, infofile, None)
48+
assert op.exists(infofile)
49+
with open(infofile) as fp:
4850
out2 = json.load(fp)
4951

50-
assert out == out2
51-
5252
# 3) with existing metadata
5353
bids = {"existing": "data"}
54-
nifti, info = embed_nifti(dcmfiles, 'nifti.nii', 'infofile.json', bids, False)
55-
with open(info) as fp:
54+
embed_dicom_and_nifti_metadata(dcmfiles, niftifile, infofile, bids)
55+
with open(infofile) as fp:
5656
out3 = json.load(fp)
5757

58-
assert out3["existing"]
59-
del out3["existing"]
60-
assert out3 == out2 == out
58+
assert out3.pop("existing") == "data"
59+
assert out3 == out2

0 commit comments

Comments
 (0)