Skip to content

Commit 85189f1

Browse files
tsaloyarikoptic
authored andcommitted
Fix bugs.
1 parent bdcbf2e commit 85189f1

File tree

1 file changed

+33
-28
lines changed

1 file changed

+33
-28
lines changed

heudiconv/convert.py

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def update_complex_name(fileinfo, this_prefix_basename, suffix):
239239
with magnitude/phase reconstruction.
240240
"""
241241
unsupported_types = ['_bold', '_phase']
242-
if suffix in unsupported_types:
242+
if any(ut in this_prefix_basename for ut in unsupported_types):
243243
return this_prefix_basename
244244

245245
# Check to see if it is magnitude or phase reconstruction:
@@ -252,7 +252,6 @@ def update_complex_name(fileinfo, this_prefix_basename, suffix):
252252

253253
# Insert reconstruction label
254254
if not ('_part-%s' % mag_or_phase) in this_prefix_basename:
255-
256255
# If "_part-" is specified, prepend the 'mag_or_phase' value.
257256
if '_part-' in this_prefix_basename:
258257
raise BIDSError(
@@ -261,7 +260,7 @@ def update_complex_name(fileinfo, this_prefix_basename, suffix):
261260
)
262261

263262
# 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:
265264
for label in ['_run', '_echo', '_sbref']:
266265
if (label in this_prefix_basename):
267266
this_prefix_basename = this_prefix_basename.replace(
@@ -277,46 +276,53 @@ def update_multiecho_name(fileinfo, this_prefix_basename, echo_times, suffix):
277276
sequence.
278277
"""
279278
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):
281280
return this_prefix_basename
282281

283282
# Get the EchoNumber from json file info. If not present, use EchoTime
284283
if 'EchoNumber' in fileinfo.keys():
285284
echo_number = fileinfo['EchoNumber']
286285
else:
287286
echo_number = echo_times.index(fileinfo['EchoTime']) + 1
287+
filetype = '_' + this_prefix_basename.split('_')[-1]
288288

289-
# Now, decide where to insert it.
290289
# 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:
293292
this_prefix_basename = this_prefix_basename.replace(
294-
imgtype, "_echo-%d%s" % (echo_number, imgtype)
293+
filetype, "_echo-%s%s" % (echo_number, filetype)
295294
)
296295
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+
297302
return this_prefix_basename
298303

299304

300-
def update_uncombined_name(fileinfo, this_prefix_basename, coil_names, suffix):
305+
def update_uncombined_name(fileinfo, this_prefix_basename, channel_names, suffix):
301306
"""
302307
Insert `_channel-<num>` entity into filename if data are from a sequence
303308
with "save uncombined".
304309
"""
305310
# 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]
307315

308316
# 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:
312319
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)
314321
)
315322
break
316-
317-
if (label in this_prefix_basename):
323+
elif (label in this_prefix_basename):
318324
this_prefix_basename = this_prefix_basename.replace(
319-
label, "_channel-%s%s" % (coil_number, label)
325+
label, "_channel-%s%s" % (channel_number, label)
320326
)
321327
break
322328
return this_prefix_basename
@@ -636,14 +642,13 @@ def save_converted_files(res, item_dicoms, bids_options, outtype, prefix, outnam
636642
channel_names = [v for v in channel_names if v]
637643
channel_names = sorted(list(set(channel_names)))
638644
image_types = [v for v in image_types if v]
639-
image_types = sorted(list(set(image_types)))
640645

641646
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
643648

644649
# 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])
647652
is_complex = magnitude_found and phase_found
648653

649654
### 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
655660
# and we don't want to modify it for all the bids_files):
656661
this_prefix_basename = prefix_basename
657662

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-
664663
# Update name if multi-echo
665664
# (Note: it can be _sbref and multiecho, so don't use "elif"):
666665
# 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
670669
bids_meta, this_prefix_basename, echo_times, suffix
671670
)
672671

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+
673678
# Update name if uncombined (channel-level) data
674679
if bids_file and is_uncombined:
675680
this_prefix_basename = update_uncombined_name(
676-
bids_meta, this_prefix_basename, channel_names
681+
bids_meta, this_prefix_basename, channel_names, suffix
677682
)
678683

679684
# Fallback option:

0 commit comments

Comments
 (0)