Skip to content

Commit 95e574b

Browse files
committed
Fix bugs.
1 parent e21a9ba commit 95e574b

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed

heudiconv/convert.py

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

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

250250
# Insert reconstruction label
251251
if not ('_part-%s' % mag_or_phase) in this_prefix_basename:
252-
253252
# If "_part-" is specified, prepend the 'mag_or_phase' value.
254253
if '_part-' in this_prefix_basename:
255254
raise BIDSError(
@@ -258,7 +257,7 @@ def update_complex_name(fileinfo, this_prefix_basename, suffix):
258257
)
259258

260259
# 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:
262261
for label in ['_run', '_echo', '_sbref']:
263262
if (label in this_prefix_basename):
264263
this_prefix_basename = this_prefix_basename.replace(
@@ -274,46 +273,53 @@ def update_multiecho_name(fileinfo, this_prefix_basename, echo_times, suffix):
274273
sequence.
275274
"""
276275
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):
278277
return this_prefix_basename
279278

280279
# Get the EchoNumber from json file info. If not present, use EchoTime
281280
if 'EchoNumber' in fileinfo.keys():
282281
echo_number = fileinfo['EchoNumber']
283282
else:
284283
echo_number = echo_times.index(fileinfo['EchoTime']) + 1
284+
filetype = '_' + this_prefix_basename.split('_')[-1]
285285

286-
# Now, decide where to insert it.
287286
# 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:
290289
this_prefix_basename = this_prefix_basename.replace(
291-
imgtype, "_echo-%d%s" % (echo_number, imgtype)
290+
filetype, "_echo-%s%s" % (echo_number, filetype)
292291
)
293292
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+
294299
return this_prefix_basename
295300

296301

297-
def update_uncombined_name(fileinfo, this_prefix_basename, coil_names, suffix):
302+
def update_uncombined_name(fileinfo, this_prefix_basename, channel_names, suffix):
298303
"""
299304
Insert `_channel-<num>` entity into filename if data are from a sequence
300305
with "save uncombined".
301306
"""
302307
# 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]
304312

305313
# 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:
309316
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)
311318
)
312319
break
313-
314-
if (label in this_prefix_basename):
320+
elif (label in this_prefix_basename):
315321
this_prefix_basename = this_prefix_basename.replace(
316-
label, "_channel-%s%s" % (coil_number, label)
322+
label, "_channel-%s%s" % (channel_number, label)
317323
)
318324
break
319325
return this_prefix_basename
@@ -630,14 +636,13 @@ def save_converted_files(res, item_dicoms, bids_options, outtype, prefix, outnam
630636
channel_names = [v for v in channel_names if v]
631637
channel_names = sorted(list(set(channel_names)))
632638
image_types = [v for v in image_types if v]
633-
image_types = sorted(list(set(image_types)))
634639

635640
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
637642

638643
# 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])
641646
is_complex = magnitude_found and phase_found
642647

643648
### 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
646651
# TODO: monitor conversion duration
647652
if bids_file:
648653
fileinfo = load_json(bids_file)
649-
print(suffix)
650654

651655
# set the prefix basename for this specific file (we'll modify it,
652656
# and we don't want to modify it for all the bids_files):
653657
this_prefix_basename = prefix_basename
654658

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-
661659
# Update name if multi-echo
662660
# (Note: it can be _sbref and multiecho, so don't use "elif"):
663661
# 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
667665
fileinfo, this_prefix_basename, echo_times, suffix
668666
)
669667

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+
670674
# Update name if uncombined (channel-level) data
671675
if bids_file and is_uncombined:
672676
this_prefix_basename = update_uncombined_name(
673-
fileinfo, this_prefix_basename, channel_names
677+
fileinfo, this_prefix_basename, channel_names, suffix
674678
)
675679

676680
# Fallback option:

0 commit comments

Comments
 (0)