24
24
save_scans_key ,
25
25
tuneup_bids_json_files ,
26
26
add_participant_record ,
27
+ BIDSException
27
28
)
28
29
from .dicoms import (
29
30
group_dicoms_into_seqinfos ,
@@ -486,73 +487,83 @@ def save_converted_files(res, item_dicoms, bids, outtype, prefix, outname_bids,
486
487
# series. To do that, the most straightforward way is to read the
487
488
# echo times for all bids_files and see if they are all the same or not.
488
489
489
- # Get the echo times:
490
- echoTimes = [load_json (bids_file ).get ('EchoTime' ) for bids_file in bids_files ]
490
+ # Get the echo times while not breaking non-BIDS compliance
491
+ echo_times = []
492
+ for bids_file in bids_files :
493
+ if bids_file :
494
+ echo_times .append (load_json (bids_file ).get ('EchoTime' ))
491
495
492
496
# To see if the echo times are the same, convert it to a set and see if
493
497
# only one remains:
494
- if ( len (set (echoTimes )) == 1 ): multiecho = False
495
- else : multiecho = True
498
+ multiecho = False
499
+ if echo_times :
500
+ multiecho = len (set (echo_times )) == 1
496
501
497
- ### Loop through the bids_files, set the output name and save files ###
502
+ ### Loop through the bids_files, set the output name and save files
498
503
499
504
for fl , suffix , bids_file in zip (res_files , suffixes , bids_files ):
500
505
# load the json file info:
501
- fileinfo = load_json (bids_file )
506
+ # TODO: time performance
507
+ if bids_file :
508
+ fileinfo = load_json (bids_file )
502
509
503
- # set the prefix basename for this specific file (we'll modify it, and
504
- # we don't want to modify it for all the bids_files):
510
+ # set the prefix basename for this specific file (we'll modify it,
511
+ # and we don't want to modify it for all the bids_files):
505
512
this_prefix_basename = prefix_basename
506
513
507
514
# _sbref sequences reconstructing magnitude and phase generate
508
515
# two NIfTI files IN THE SAME SERIES, so we cannot just add
509
516
# the suffix, if we want to be bids compliant:
510
- if ( bids and (this_prefix_basename [ - 6 :] == '_sbref' ) ):
517
+ if (bids_file and (this_prefix_basename . endswith ( '_sbref' )) ):
511
518
# Check to see if it is magnitude or phase reconstruction:
512
- if ('M' in fileinfo .get ('ImageType' )): mag_or_phase = 'magnitude'
513
- elif ('P' in fileinfo .get ('ImageType' )): mag_or_phase = 'phase'
514
- else : mag_or_phase = suffix
519
+ if 'M' in fileinfo .get ('ImageType' ):
520
+ mag_or_phase = 'magnitude'
521
+ elif 'P' in fileinfo .get ('ImageType' ):
522
+ mag_or_phase = 'phase'
523
+ else :
524
+ mag_or_phase = suffix
515
525
516
- # If "_rec-'mag_or_phase'" is not already there, check where to insert it:
526
+ # Insert reconstruction label
517
527
if not (("_rec-%s" % mag_or_phase ) in this_prefix_basename ):
518
528
519
- # If "_rec-" is specified, append the 'mag_or_phase' value.
529
+ # If "_rec-" is specified, prepend the 'mag_or_phase' value.
520
530
if ('_rec-' in this_prefix_basename ):
521
- spt = this_prefix_basename . split ( '_rec-' , 1 )
522
- # grab the reconstruction type (grab whatever we have before the next "_"):
523
- spt_spt = spt [ 1 ]. split ( '_' , 1 )
524
- # update 'this_prefix_basename':
525
- this_prefix_basename = "%s_rec-%s-%s_%s" % ( spt [ 0 ], spt_spt [ 0 ], mag_or_phase , spt_spt [ 1 ] )
531
+ raise BIDSException (
532
+ "Reconstruction label for multi-echo single-band"
533
+ " reference images will be automatically set, remove"
534
+ " from heuristic"
535
+ )
526
536
527
537
# If not, insert "_rec-" + 'mag_or_phase' into the prefix_basename
528
538
# **before** "_run", "_echo" or "_sbref", whichever appears first:
529
- else :
530
- for my_str in ['_run' , '_echo' , '_sbref' ]:
531
- if (my_str in this_prefix_basename ):
532
- spt = this_prefix_basename .split (my_str , 1 )
533
- this_prefix_basename = "%s_rec-%s%s%s" % (spt [0 ], mag_or_phase , my_str , spt [1 ])
534
- break
535
-
536
- # Now check if this run is multi-echo (Note: it can be _sbref and multiecho, so
537
- # don't use "elif"):
538
- # For multi-echo sequences, we have to specify the echo number in the file name:
539
- if ( bids and multiecho ):
539
+ for label in ['_run' , '_echo' , '_sbref' ]:
540
+ if (label in this_prefix_basename ):
541
+ this_prefix_basename = this_prefix_basename .replace (
542
+ label , "_rec-%s%s" % (mag_or_phase , label )
543
+ )
544
+ break
545
+
546
+ # Now check if this run is multi-echo
547
+ # (Note: it can be _sbref and multiecho, so don't use "elif"):
548
+ # For multi-echo sequences, we have to specify the echo number in
549
+ # the file name:
550
+ if bids and multiecho :
540
551
# Get the EchoNumber from json file info. If not present, it's echo-1
541
- echoNumber = ( fileinfo .get ('EchoNumber' ) or 1 )
552
+ echo_number = fileinfo .get ('EchoNumber' , 1 )
553
+
542
554
555
+ supported_multiecho = ['_bold' , '_sbref' , '_T1w' ] # epi?
543
556
# Now, decide where to insert it.
544
557
# Insert it **before** the following string(s), whichever appears first.
545
- # (Note: If you decide to support multi-echo for other sequences (e.g.
546
- # ME-MPRAGE), add the string before which you want to add the echo number
547
- # to the list below):
548
- for my_str in ['_bold' , '_sbref' , '_T1w' ]:
549
- if (my_str in this_prefix_basename ):
550
- spt = this_prefix_basename .split (my_str , 1 )
551
- this_prefix_basename = "%s_echo-%s%s%s" % (spt [0 ], echoNumber , my_str , spt [1 ])
558
+ for imgtype in ['_bold' , '_sbref' , '_T1w' ]:
559
+ if (imgtype in this_prefix_basename ):
560
+ this_prefix_basename = this_prefix_basename .replace (
561
+ imgtype , "_echo-%d%s" % (echo_number , imgtype )
562
+ )
552
563
break
553
564
554
565
# For Scout runs with multiple NIfTI images per run:
555
- if ( bids and ('scout' in this_prefix_basename .lower ()) ):
566
+ if (bids and ('scout' in this_prefix_basename .lower ())):
556
567
# in some cases (more than one slice slab), there are several
557
568
# NIfTI images in the scout run, so distinguish them with "_acq-"
558
569
spt = this_prefix_basename .split ('_acq-Scout' , 1 )
@@ -562,15 +573,16 @@ def save_converted_files(res, item_dicoms, bids, outtype, prefix, outname_bids,
562
573
# If we have failed to modify this_prefix_basename, because it didn't fall
563
574
# into any of the options above, just add the suffix at the end:
564
575
if ( this_prefix_basename == prefix_basename ):
565
- this_prefix_basename = "%s%s" % ( this_prefix_basename , suffix )
576
+ this_prefix_basename += suffix
566
577
567
- # Finally, form the outname by stitch the directory and outtype:
568
- outname = "%s/%s.%s" % (prefix_dirname , this_prefix_basename , outtype )
578
+ # Finally, form the outname by stitching the directory and outtype:
579
+ outname = op .join (prefix_dirname , this_prefix_basename )
580
+ outfile = outname + '.' + outtype
569
581
570
582
# Write the files needed:
571
- safe_copyfile (fl , outname , overwrite )
583
+ safe_copyfile (fl , outfile , overwrite )
572
584
if bids_file :
573
- outname_bids_file = "%s.json" % (outname . strip ( outtype ) )
585
+ outname_bids_file = "%s.json" % (outname )
574
586
safe_copyfile (bids_file , outname_bids_file , overwrite )
575
587
bids_outfiles .append (outname_bids_file )
576
588
# res_files is not a list
0 commit comments