@@ -236,7 +236,7 @@ def update_complex_name(fileinfo, this_prefix_basename, suffix):
236
236
with magnitude/phase reconstruction.
237
237
"""
238
238
unsupported_types = ['_bold' , '_phase' ]
239
- if suffix in unsupported_types :
239
+ if any ( ut in this_prefix_basename for ut in unsupported_types ) :
240
240
return this_prefix_basename
241
241
242
242
# Check to see if it is magnitude or phase reconstruction:
@@ -249,7 +249,6 @@ def update_complex_name(fileinfo, this_prefix_basename, suffix):
249
249
250
250
# Insert reconstruction label
251
251
if not ('_part-%s' % mag_or_phase ) in this_prefix_basename :
252
-
253
252
# If "_part-" is specified, prepend the 'mag_or_phase' value.
254
253
if '_part-' in this_prefix_basename :
255
254
raise BIDSError (
@@ -258,7 +257,7 @@ def update_complex_name(fileinfo, this_prefix_basename, suffix):
258
257
)
259
258
260
259
# If not, insert "_part-" + 'mag_or_phase' into the prefix_basename
261
- # **before** "_run", "_echo" or "_sbref", whichever appears first:
260
+ # **before** "_run", "_echo" or "_sbref", whichever appears first:
262
261
for label in ['_run' , '_echo' , '_sbref' ]:
263
262
if (label in this_prefix_basename ):
264
263
this_prefix_basename = this_prefix_basename .replace (
@@ -274,46 +273,53 @@ def update_multiecho_name(fileinfo, this_prefix_basename, echo_times, suffix):
274
273
sequence.
275
274
"""
276
275
unsupported_types = ['_magnitude1' , '_magnitude2' , '_phasediff' , '_phase1' , '_phase2' ]
277
- if suffix in unsupported_types :
276
+ if any ( ut in this_prefix_basename for ut in unsupported_types ) :
278
277
return this_prefix_basename
279
278
280
279
# Get the EchoNumber from json file info. If not present, use EchoTime
281
280
if 'EchoNumber' in fileinfo .keys ():
282
281
echo_number = fileinfo ['EchoNumber' ]
283
282
else :
284
283
echo_number = echo_times .index (fileinfo ['EchoTime' ]) + 1
284
+ filetype = '_' + this_prefix_basename .split ('_' )[- 1 ]
285
285
286
- # Now, decide where to insert it.
287
286
# Insert it **before** the following string(s), whichever appears first.
288
- for imgtype in supported_multiecho :
289
- if ( imgtype in this_prefix_basename ) :
287
+ for label in [ '_run' , filetype ] :
288
+ if label == filetype :
290
289
this_prefix_basename = this_prefix_basename .replace (
291
- imgtype , "_echo-%d %s" % (echo_number , imgtype )
290
+ filetype , "_echo-%s %s" % (echo_number , filetype )
292
291
)
293
292
break
293
+ elif (label in this_prefix_basename ):
294
+ this_prefix_basename = this_prefix_basename .replace (
295
+ label , "_echo-%s%s" % (echo_number , label )
296
+ )
297
+ break
298
+
294
299
return this_prefix_basename
295
300
296
301
297
- def update_uncombined_name (fileinfo , this_prefix_basename , coil_names , suffix ):
302
+ def update_uncombined_name (fileinfo , this_prefix_basename , channel_names , suffix ):
298
303
"""
299
304
Insert `_channel-<num>` entity into filename if data are from a sequence
300
305
with "save uncombined".
301
306
"""
302
307
# Determine the channel number
303
- channel_number = coil_names .index (fileinfo ['CoilString' ]) + 1
308
+ channel_number = '' .join ([c for c in fileinfo ['CoilString' ] if c .isdigit ()])
309
+ if not channel_number :
310
+ channel_number = channel_names .index (fileinfo ['CoilString' ]) + 1
311
+ filetype = '_' + this_prefix_basename .split ('_' )[- 1 ]
304
312
305
313
# Insert it **before** the following string(s), whichever appears first.
306
- for label in ['_run' , '_echo' , suffix ]:
307
- if label == suffix :
308
- prefix_suffix = this_prefix_basename .split ('_' )[- 1 ]
314
+ for label in ['_run' , '_echo' , filetype ]:
315
+ if label == filetype :
309
316
this_prefix_basename = this_prefix_basename .replace (
310
- prefix_suffix , "_channel-%s_ %s" % (channel_number , prefix_suffix )
317
+ filetype , "_channel-%s %s" % (channel_number , filetype )
311
318
)
312
319
break
313
-
314
- if (label in this_prefix_basename ):
320
+ elif (label in this_prefix_basename ):
315
321
this_prefix_basename = this_prefix_basename .replace (
316
- label , "_channel-%s%s" % (coil_number , label )
322
+ label , "_channel-%s%s" % (channel_number , label )
317
323
)
318
324
break
319
325
return this_prefix_basename
@@ -630,14 +636,13 @@ def save_converted_files(res, item_dicoms, bids_options, outtype, prefix, outnam
630
636
channel_names = [v for v in channel_names if v ]
631
637
channel_names = sorted (list (set (channel_names )))
632
638
image_types = [v for v in image_types if v ]
633
- image_types = sorted (list (set (image_types )))
634
639
635
640
is_multiecho = len (echo_times ) > 1 # Check for varying echo times
636
- is_uncombined = len (coil_names ) > 1 # Check for uncombined data
641
+ is_uncombined = len (channel_names ) > 1 # Check for uncombined data
637
642
638
643
# Determine if data are complex (magnitude + phase)
639
- magnitude_found = ['M' in it for it in image_types ]
640
- phase_found = ['P' in it for it in image_types ]
644
+ magnitude_found = any ( ['M' in it for it in image_types ])
645
+ phase_found = any ( ['P' in it for it in image_types ])
641
646
is_complex = magnitude_found and phase_found
642
647
643
648
### Loop through the bids_files, set the output name and save files
@@ -646,18 +651,11 @@ def save_converted_files(res, item_dicoms, bids_options, outtype, prefix, outnam
646
651
# TODO: monitor conversion duration
647
652
if bids_file :
648
653
fileinfo = load_json (bids_file )
649
- print (suffix )
650
654
651
655
# set the prefix basename for this specific file (we'll modify it,
652
656
# and we don't want to modify it for all the bids_files):
653
657
this_prefix_basename = prefix_basename
654
658
655
- # Update name if complex data
656
- if bids_file and is_complex :
657
- this_prefix_basename = update_complex_name (
658
- fileinfo , this_prefix_basename , suffix
659
- )
660
-
661
659
# Update name if multi-echo
662
660
# (Note: it can be _sbref and multiecho, so don't use "elif"):
663
661
# For multi-echo sequences, we have to specify the echo number in
@@ -667,10 +665,16 @@ def save_converted_files(res, item_dicoms, bids_options, outtype, prefix, outnam
667
665
fileinfo , this_prefix_basename , echo_times , suffix
668
666
)
669
667
668
+ # Update name if complex data
669
+ if bids_file and is_complex :
670
+ this_prefix_basename = update_complex_name (
671
+ fileinfo , this_prefix_basename , suffix
672
+ )
673
+
670
674
# Update name if uncombined (channel-level) data
671
675
if bids_file and is_uncombined :
672
676
this_prefix_basename = update_uncombined_name (
673
- fileinfo , this_prefix_basename , channel_names
677
+ fileinfo , this_prefix_basename , channel_names , suffix
674
678
)
675
679
676
680
# Fallback option:
0 commit comments