Skip to content

Commit b8119dc

Browse files
committed
RF: if min_meta - do not bother calling embed_metadata_from_dicoms
If I saw it right, the only relevant conditioned on min_meta inside embed_metadata_from_dicoms was embedding taskname from the filename, which is not per se from dicom, so not appropriate there. Another side effect might be to miss prov information if min_meta, but since we would not be running that function and not embedding from dicoms -- I think it is ok
1 parent 3986114 commit b8119dc

File tree

2 files changed

+27
-45
lines changed

2 files changed

+27
-45
lines changed

heudiconv/convert.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,9 @@ def convert(items, converter, scaninfo_suffix, custom_callable, with_prov,
333333
"multiple files")
334334
elif not bids_outfiles:
335335
lgr.debug("No BIDS files were produced, nothing to embed to then")
336-
elif outname:
336+
elif outname and not min_meta:
337337
embed_metadata_from_dicoms(bids_options, item_dicoms, outname, outname_bids,
338-
prov_file, scaninfo, tempdirs, with_prov,
339-
min_meta)
338+
prov_file, scaninfo, tempdirs, with_prov)
340339
if scaninfo and op.exists(scaninfo):
341340
lgr.info("Post-treating %s file", scaninfo)
342341
treat_infofile(scaninfo)

heudiconv/dicoms.py

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ def _assign_dicom_time(ti):
386386
return outtar
387387

388388

389-
def embed_nifti(dcmfiles, niftifile, infofile, bids_info, min_meta):
389+
def embed_nifti(dcmfiles, niftifile, infofile, bids_info):
390390
"""
391391
392392
If `niftifile` doesn't exist, it gets created out of the `dcmfiles` stack,
@@ -403,7 +403,6 @@ def embed_nifti(dcmfiles, niftifile, infofile, bids_info, min_meta):
403403
niftifile
404404
infofile
405405
bids_info
406-
min_meta
407406
408407
Returns
409408
-------
@@ -416,44 +415,31 @@ def embed_nifti(dcmfiles, niftifile, infofile, bids_info, min_meta):
416415
import json
417416
import re
418417

419-
if not min_meta:
420-
from heudiconv.external.dcmstack import ds
421-
stack = ds.parse_and_stack(dcmfiles, force=True).values()
422-
if len(stack) > 1:
423-
raise ValueError('Found multiple series')
424-
# may be odict now - iter to be safe
425-
stack = next(iter(stack))
426-
427-
# Create the nifti image using the data array
428-
if not op.exists(niftifile):
429-
nifti_image = stack.to_nifti(embed_meta=True)
430-
nifti_image.to_filename(niftifile)
431-
return ds.NiftiWrapper(nifti_image).meta_ext.to_json()
432-
433-
orig_nii = nb.load(niftifile)
434-
aff = orig_nii.affine
435-
ornt = nb.orientations.io_orientation(aff)
436-
axcodes = nb.orientations.ornt2axcodes(ornt)
437-
new_nii = stack.to_nifti(voxel_order=''.join(axcodes), embed_meta=True)
438-
meta = ds.NiftiWrapper(new_nii).meta_ext.to_json()
439-
440-
meta_info = None if min_meta else json.loads(meta)
418+
from heudiconv.external.dcmstack import ds
419+
stack = ds.parse_and_stack(dcmfiles, force=True).values()
420+
if len(stack) > 1:
421+
raise ValueError('Found multiple series')
422+
# may be odict now - iter to be safe
423+
stack = next(iter(stack))
424+
425+
# Create the nifti image using the data array
426+
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()
430+
431+
orig_nii = nb.load(niftifile)
432+
aff = orig_nii.affine
433+
ornt = nb.orientations.io_orientation(aff)
434+
axcodes = nb.orientations.ornt2axcodes(ornt)
435+
new_nii = stack.to_nifti(voxel_order=''.join(axcodes), embed_meta=True)
436+
meta_info = ds.NiftiWrapper(new_nii).meta_ext.to_json()
441437

442438
if bids_info:
439+
# make nice with python 3 - same behavior?
440+
meta_info = meta_info.copy()
441+
meta_info.update(bids_info)
443442

444-
if min_meta:
445-
meta_info = bids_info
446-
else:
447-
# make nice with python 3 - same behavior?
448-
meta_info = meta_info.copy()
449-
meta_info.update(bids_info)
450-
# meta_info = dict(meta_info.items() + bids_info.items())
451-
try:
452-
meta_info['TaskName'] = re.search(
453-
r'(?<=_task-)\w+', op.basename(infofile)
454-
).group(0).split('_')[0]
455-
except AttributeError:
456-
pass
457443
# write to outfile
458444
with open(infofile, 'wt') as fp:
459445
json.dump(meta_info, fp, indent=3, sort_keys=True)
@@ -462,8 +448,7 @@ def embed_nifti(dcmfiles, niftifile, infofile, bids_info, min_meta):
462448

463449

464450
def embed_metadata_from_dicoms(bids_options, item_dicoms, outname, outname_bids,
465-
prov_file, scaninfo, tempdirs, with_prov,
466-
min_meta):
451+
prov_file, scaninfo, tempdirs, with_prov):
467452
"""
468453
Enhance sidecar information file with more information from DICOMs
469454
@@ -477,7 +462,6 @@ def embed_metadata_from_dicoms(bids_options, item_dicoms, outname, outname_bids,
477462
scaninfo
478463
tempdirs
479464
with_prov
480-
min_meta
481465
482466
Returns
483467
-------
@@ -490,14 +474,13 @@ def embed_metadata_from_dicoms(bids_options, item_dicoms, outname, outname_bids,
490474
item_dicoms = list(map(op.abspath, item_dicoms))
491475

492476
embedfunc = Node(Function(input_names=['dcmfiles', 'niftifile', 'infofile',
493-
'bids_info', 'min_meta'],
477+
'bids_info',],
494478
output_names=['outfile', 'meta'],
495479
function=embed_nifti),
496480
name='embedder')
497481
embedfunc.inputs.dcmfiles = item_dicoms
498482
embedfunc.inputs.niftifile = op.abspath(outname)
499483
embedfunc.inputs.infofile = op.abspath(scaninfo)
500-
embedfunc.inputs.min_meta = min_meta
501484
embedfunc.inputs.bids_info = load_json(op.abspath(outname_bids)) if (bids_options is not None) else None
502485
embedfunc.base_dir = tmpdir
503486
cwd = os.getcwd()

0 commit comments

Comments
 (0)