2929from .io import (read_json ,
3030 write_json ,
3131 parse_bids_fname_keyvals ,
32- parse_dcm2niix_fname ,
3332 safe_copy ,
3433 create_file_if_missing ,
35- strip_extensions )
34+ nii_to_json )
3635
3736
3837def add_participant_record (studydir , subject , age , sex ):
@@ -141,15 +140,15 @@ def purpose_handling(bids_meta,
141140
142141 print (' Identifying fieldmap image type' )
143142
144- if scan_seq == 'GR' :
143+ if 'GR' in scan_seq :
145144
146145 print (' Gradient echo fieldmap detected' )
147146 print (' Identifying magnitude and phase images' )
148147
149148 # Update BIDS filenames according to BIDS Fieldmap Case (1 or 2 - see specification)
150149 bids_nii_fname , bids_json_fname = fmaps .handle_fmap_case (work_json_fname , bids_nii_fname , bids_json_fname )
151150
152- elif scan_seq == 'EP' :
151+ elif 'EP' in scan_seq :
153152
154153 print (' EPI fieldmap detected' )
155154
@@ -164,7 +163,7 @@ def purpose_handling(bids_meta,
164163
165164 elif bids_purpose == 'anat' :
166165
167- if scan_seq == 'GR_IR' :
166+ if 'GR' in scan_seq and 'IR' in scan_seq :
168167
169168 print (' IR-prepared GRE detected - likely T1w MPRAGE or MEMPRAGE' )
170169
@@ -180,13 +179,13 @@ def purpose_handling(bids_meta,
180179 bids_nii_fname , bids_json_fname = d2n .handle_bias_recon (
181180 work_json_fname , bids_json_fname , key_flags ['Recon' ], nii_ext )
182181
183- elif scan_seq == 'SE' :
182+ elif 'SE' in scan_seq :
184183
185184 print (' Spin echo detected - likely T1w or T2w anatomic image' )
186185 bids_nii_fname , bids_json_fname = d2n .handle_bias_recon (
187186 work_json_fname , bids_json_fname , key_flags ['Recon' ], nii_ext )
188187
189- elif scan_seq == 'GR' :
188+ elif 'GR' in scan_seq :
190189
191190 print (' Gradient echo detected' )
192191
@@ -345,7 +344,7 @@ def bids_legalize_keys(keys):
345344 return keys
346345
347346
348- def auto_run_no (file_list , prot_dict ):
347+ def auto_run_no (d2n_nii_list , prot_dict ):
349348 """
350349 Search for duplicate series names in dcm2niix output file list
351350 Return inferred run numbers accounting for duplication and multiple recons from single acquisition
@@ -358,8 +357,8 @@ def auto_run_no(file_list, prot_dict):
358357 - If no duplicates of a given series are found, drop the run- key from the BIDS filename
359358 - Current dcm2niix version: 1.0.20211006
360359
361- :param file_list : list of str
362- Nifti file name list
360+ :param d2n_nii_list : list of str
361+ dcm2niix output Nifti filename list
363362 :param prot_dict: dictionary
364363 Protocol translation dictionary
365364 :return: run_num, array of int
@@ -368,17 +367,22 @@ def auto_run_no(file_list, prot_dict):
368367 # Construct list of series descriptions and original numbers from file names
369368 series_id_list = []
370369
371- for fname in file_list :
370+ # Loop over all
371+ for nii_fname in d2n_nii_list :
372372
373- # Parse dcm2niix filename into relevant keys, including suffix
374- info = parse_dcm2niix_fname (fname )
373+ # Load JSON sidecar for this Nifti image
374+ json_fname = nii_to_json (nii_fname , '.nii.gz' )
375+ bids_info = read_json (json_fname )
375376
376- ser_desc = info ['SerDesc' ]
377- echo_no = info ['EchoNo' ]
378- suffix = info ['Suffix' ]
377+ ser_desc = bids_info ['SeriesDescription' ].replace (' ' , '_' )
378+ if 'EchoNumber' in bids_info .keys ():
379+ echo_no = bids_info ['EchoNumber' ]
380+ else :
381+ echo_no = 1
382+ recon_type = '-' .join (bids_info ['ImageType' ])
379383
380384 if ser_desc in prot_dict :
381- _ , bids_stub , _ = prot_dict [info [ 'SerDesc' ] ]
385+ _ , bids_stub , _ = prot_dict [ser_desc ]
382386 else :
383387 print ('' )
384388 print ('* Series description {} missing from code/Protocol_Translator.json' .format (ser_desc ))
@@ -387,7 +391,7 @@ def auto_run_no(file_list, prot_dict):
387391 sys .exit (1 )
388392
389393 # Construct a unique series identifier including echo number and suffix
390- series_id = f"{ bids_stub } _ { echo_no } _{ suffix } "
394+ series_id = f"{ bids_stub } _ECHO { echo_no } _{ recon_type } "
391395
392396 # Add to list
393397 series_id_list .append (series_id )
@@ -396,7 +400,7 @@ def auto_run_no(file_list, prot_dict):
396400 unique_series_ids = set (series_id_list )
397401
398402 # Init vector of run numbers and max run numbers for each series
399- run_no = np .zeros (len (file_list )).astype (int )
403+ run_no = np .zeros (len (d2n_nii_list )).astype (int )
400404
401405 # Loop over unique series descriptions
402406 for unique_series_id in unique_series_ids :
0 commit comments