@@ -97,7 +97,7 @@ def validate_dicom(fl, dcmfilter):
97
97
for sig in ('iop' , 'ICE_Dims' , 'SequenceName' ):
98
98
try :
99
99
del mw .series_signature [sig ]
100
- except Exception :
100
+ except KeyError :
101
101
pass
102
102
# Workaround for protocol name in private siemens csa header
103
103
if not getattr (mw .dcm_data , 'ProtocolName' , '' ).strip ():
@@ -112,15 +112,15 @@ def validate_dicom(fl, dcmfilter):
112
112
lgr .warning (
113
113
'Ignoring %s since not quite a "normal" DICOM: %s' , fl , e
114
114
)
115
- return None
115
+ return
116
116
if dcmfilter is not None and dcmfilter (mw .dcm_data ):
117
117
lgr .warning ("Ignoring %s because of DICOM filter" , fl )
118
- return None
118
+ return
119
119
if mw .dcm_data [0x0008 , 0x0016 ].repval in (
120
120
'Raw Data Storage' ,
121
121
'GrayscaleSoftcopyPresentationStateStorage'
122
122
):
123
- return None
123
+ return
124
124
try :
125
125
file_studyUID = mw .dcm_data .StudyInstanceUID
126
126
except AttributeError :
@@ -190,9 +190,11 @@ def group_dicoms_into_seqinfos(files, grouping, file_filter=None,
190
190
grouping = custom_grouping
191
191
study_customgroup = None
192
192
193
- for filename in files :
193
+ removeidx = []
194
+ for idx , filename in enumerate (files ):
194
195
mwinfo = validate_dicom (filename , dcmfilter )
195
196
if mwinfo is None :
197
+ removeidx .append (idx )
196
198
continue
197
199
mw , series_id , file_studyUID = mwinfo
198
200
if per_studyUID :
@@ -240,6 +242,11 @@ def group_dicoms_into_seqinfos(files, grouping, file_filter=None,
240
242
241
243
group_map = dict (zip (groups [0 ], groups [1 ]))
242
244
245
+ if removeidx :
246
+ # remove non DICOMS from files
247
+ for idx in sorted (removeidx , reverse = True ):
248
+ del files [idx ]
249
+
243
250
seqinfos = OrderedDict ()
244
251
# for the next line to make any sense the series_id needs to
245
252
# be sortable in a way that preserves the series order
@@ -405,7 +412,6 @@ def embed_nifti(dcmfiles, niftifile, infofile, bids_info, min_meta):
405
412
"""
406
413
# imports for nipype
407
414
import nibabel as nb
408
- import os
409
415
import os .path as op
410
416
import json
411
417
import re
@@ -418,7 +424,7 @@ def embed_nifti(dcmfiles, niftifile, infofile, bids_info, min_meta):
418
424
# may be odict now - iter to be safe
419
425
stack = next (iter (stack ))
420
426
421
- #Create the nifti image using the data array
427
+ # Create the nifti image using the data array
422
428
if not op .exists (niftifile ):
423
429
nifti_image = stack .to_nifti (embed_meta = True )
424
430
nifti_image .to_filename (niftifile )
@@ -443,9 +449,9 @@ def embed_nifti(dcmfiles, niftifile, infofile, bids_info, min_meta):
443
449
meta_info .update (bids_info )
444
450
# meta_info = dict(meta_info.items() + bids_info.items())
445
451
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 ]
449
455
except AttributeError :
450
456
pass
451
457
# write to outfile
0 commit comments