Skip to content

Commit 41bd0e4

Browse files
committed
fix: remove filtered DICOMs from file list
1 parent 6bd6b24 commit 41bd0e4

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

heudiconv/dicoms.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def validate_dicom(fl, dcmfilter):
9797
for sig in ('iop', 'ICE_Dims', 'SequenceName'):
9898
try:
9999
del mw.series_signature[sig]
100-
except Exception:
100+
except KeyError:
101101
pass
102102
# Workaround for protocol name in private siemens csa header
103103
if not getattr(mw.dcm_data, 'ProtocolName', '').strip():
@@ -112,15 +112,15 @@ def validate_dicom(fl, dcmfilter):
112112
lgr.warning(
113113
'Ignoring %s since not quite a "normal" DICOM: %s', fl, e
114114
)
115-
return None
115+
return
116116
if dcmfilter is not None and dcmfilter(mw.dcm_data):
117117
lgr.warning("Ignoring %s because of DICOM filter", fl)
118-
return None
118+
return
119119
if mw.dcm_data[0x0008, 0x0016].repval in (
120120
'Raw Data Storage',
121121
'GrayscaleSoftcopyPresentationStateStorage'
122122
):
123-
return None
123+
return
124124
try:
125125
file_studyUID = mw.dcm_data.StudyInstanceUID
126126
except AttributeError:
@@ -190,9 +190,11 @@ def group_dicoms_into_seqinfos(files, grouping, file_filter=None,
190190
grouping = custom_grouping
191191
study_customgroup = None
192192

193-
for filename in files:
193+
removeidx = []
194+
for idx, filename in enumerate(files):
194195
mwinfo = validate_dicom(filename, dcmfilter)
195196
if mwinfo is None:
197+
removeidx.append(idx)
196198
continue
197199
mw, series_id, file_studyUID = mwinfo
198200
if per_studyUID:
@@ -240,6 +242,11 @@ def group_dicoms_into_seqinfos(files, grouping, file_filter=None,
240242

241243
group_map = dict(zip(groups[0], groups[1]))
242244

245+
if removeidx:
246+
# remove non DICOMS from files
247+
for idx in sorted(removeidx, reverse=True):
248+
del files[idx]
249+
243250
seqinfos = OrderedDict()
244251
# for the next line to make any sense the series_id needs to
245252
# be sortable in a way that preserves the series order
@@ -405,7 +412,6 @@ def embed_nifti(dcmfiles, niftifile, infofile, bids_info, min_meta):
405412
"""
406413
# imports for nipype
407414
import nibabel as nb
408-
import os
409415
import os.path as op
410416
import json
411417
import re
@@ -418,7 +424,7 @@ def embed_nifti(dcmfiles, niftifile, infofile, bids_info, min_meta):
418424
# may be odict now - iter to be safe
419425
stack = next(iter(stack))
420426

421-
#Create the nifti image using the data array
427+
# Create the nifti image using the data array
422428
if not op.exists(niftifile):
423429
nifti_image = stack.to_nifti(embed_meta=True)
424430
nifti_image.to_filename(niftifile)
@@ -443,9 +449,9 @@ def embed_nifti(dcmfiles, niftifile, infofile, bids_info, min_meta):
443449
meta_info.update(bids_info)
444450
# meta_info = dict(meta_info.items() + bids_info.items())
445451
try:
446-
meta_info['TaskName'] = (re.search('(?<=_task-)\w+',
447-
op.basename(infofile))
448-
.group(0).split('_')[0])
452+
meta_info['TaskName'] = re.search(
453+
r'(?<=_task-)\w+', op.basename(infofile)
454+
).group(0).split('_')[0]
449455
except AttributeError:
450456
pass
451457
# write to outfile

0 commit comments

Comments
 (0)