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