def create_anat_json(bxh_file, full_output): logging.info('---START: create_anat_json---') with open(bxh_file) as fd: bxh_contents = xmltodict.parse(fd.read()) #Put together dictionary of things to write to the sidecar .json file #Make sure the anat field template file is where it should be here = os.path.dirname(os.path.realpath(__file__)) anat_field_file = os.path.join(here, 'info_field_files', 'anat_info_fields.json') if not os.path.exists(anat_field_file): logging.error('Anatomical image sidecar template file cannot be found!') logging.error('It should be here: '+str(anat_field_file)) raise RuntimeError('Missing anatomical sidecar template file!') out_dict = bxh_pick_fields.bxh_pick(anat_field_file, bxh_as_dict=bxh_contents) #Make sure echo time is in seconds et = out_dict['EchoTime'] if et > 1: logging.info('Echo Time is greater than 1, assuming it is in ms.') et = et/1000 out_dict['EchoTime'] = et out_string = json.dumps(out_dict, indent=4) logging.info('Writing sidecar anat file: '+str(full_output)) with open(full_output, 'w') as out_file: out_file.write(out_string) logging.info('---FINISHED: create_anat_json---')
def create_ncanda_json(bxh_file, full_output): logging.info('---START: create_ncanda_fmap_json---') with open(bxh_file) as fd: bxh_contents = xmltodict.parse(fd.read()) #Put together dictionary of things to write to the sidecar .json file #Make sure the fmap field template file is where it should be here = os.path.dirname(os.path.realpath(__file__)) fmap_field_file = os.path.join(here, 'info_field_files', 'fmap_info_fields.json') if not os.path.exists(fmap_field_file): logging.error('Fmap image sidecar template file cannot be found!') logging.error('It should be here: ' + str(fmap_field_file)) raise RuntimeError('Missing fmap sidecar template file!') out_dict = bxh_pick_fields.bxh_pick(fmap_field_file, bxh_as_dict=bxh_contents) #Pull apart the two echo times and make sure they are in seconds et = out_dict['EchoTime'] if not (len(et.split(' ')) > 1): logging.error('Only one TE found in .bxh header for ncanda fmap!') logging.error('There should be exactly two!') logging.error('Header entry as read by .bxh_pick: ' + str(et)) logging.error('.bxh file: ' + str(bxh_file)) raise RuntimeError('One TE found in ncanda .bxh header!') et_one = float(et.split(' ')[0]) et_two = float(et.split(' ')[1]) if et_one > 1: logging.info('Echo Time is greater than 1, assuming it is in ms.') et_one = et_one / 1000 out_dict['EchoTime1'] = et_one if et_two > 1: logging.info('Echo Time is greater than 1, assuming it is in ms.') et_two = et_two / 1000 out_dict['EchoTime2'] = et_two #Remove the old EchoTime dictionary entry, quietly out_dict['EchoTime'] = None out_dict.pop('EchoTime') out_string = json.dumps(out_dict, indent=4) logging.info('Writing sidecar fmap file: ' + str(full_output)) with open(full_output, 'w') as out_file: out_file.write(out_string) logging.info('---FINISHED: create_ncanda_fmap_json---')
def create_dwi_json(bxh_file, full_output): logging.info('---START: create_dwi_json---') with open(bxh_file) as fd: bxh_contents = xmltodict.parse(fd.read()) #Pull the task name out of the output file name taskname = os.path.split(full_output)[-1].split('task-')[-1].split('_')[0] ##TODO: Clean this up! Probably move all these to a template file. #Calculate total readout time #Put together dictionary of things to write to the sidecar .json file #Make sure the func field template file is where it should be here = os.path.dirname(os.path.realpath(__file__)) func_field_file = os.path.join(here, 'info_field_files', 'dwi_info_fields.json') if not os.path.exists(func_field_file): logging.error('DWI image sidecar template file cannot be found!') logging.error('It should be here: ' + str(func_field_file)) raise RuntimeError('Missing functional sidecar template file!') out_dict = bxh_pick_fields.bxh_pick(func_field_file, bxh_as_dict=bxh_contents) #Make sure the tr is in seconds tr = out_dict['RepetitionTime'] if tr > 50: logging.info( 'TR in bxh file is greater than 50, assuming it is in ms.') tr = tr / 1000 out_dict['RepetitionTime'] = tr #Make sure echo time is in seconds et = out_dict['EchoTime'] if et > 1: logging.info('Echo Time is greater than 1, assuming it is in ms.') et = et / 1000 out_dict['EchoTime'] = et out_string = json.dumps(out_dict, indent=4) logging.info('Writing sidecar func file: ' + str(full_output)) with open(full_output, 'w') as out_file: out_file.write(out_string) logging.info('---FINISHED: create_dwi_json---')
def create_bold_json(bxh_file, full_output): logging.info('---START: create_bold_json---') with open(bxh_file) as fd: bxh_contents = xmltodict.parse(fd.read()) #Pull the task name out of the output file name taskname = os.path.split(full_output)[-1].split('task-')[-1].split('_')[0] ##TODO: Clean this up! Probably move all these to a template file. #Calculate total readout time #Look for the TR try: tr = float(bxh_contents['bxh']['acquisitiondata']['tr']) except: logging.error('TR could not be found in passed .bxh!') logging.error('bxh_file: '+str(bxh_file)) raise RuntimeError('TR could not be found in .bxh.') #Put together dictionary of things to write to the sidecar .json file #Make sure the func field template file is where it should be here = os.path.dirname(os.path.realpath(__file__)) func_field_file = os.path.join(here, 'info_field_files', 'func_info_fields.json') if not os.path.exists(func_field_file): logging.error('Functional image sidecar template file cannot be found!') logging.error('It should be here: '+str(func_field_file)) raise RuntimeError('Missing functional sidecar template file!') out_dict = bxh_pick_fields.bxh_pick(func_field_file, bxh_as_dict=bxh_contents) #Make sure the tr is in seconds tr = out_dict['RepetitionTime'] if tr > 50: logging.info('TR in bxh file is greater than 50, assuming it is in ms.') tr = tr/1000 out_dict['RepetitionTime'] = tr #Make sure echo time is in seconds et = out_dict['EchoTime'] if et > 1: logging.info('Echo Time is greater than 1, assuming it is in ms.') et = et/1000 out_dict['EchoTime'] = et #Calculate slice timing for element in bxh_contents['bxh']['datarec']['dimension']: if element['@type'] == 'z': slice_order_list = element['datapoints']['#text'] num_slices = float(element['size']) factor = tr/num_slices st = [] for s in slice_order_list.split(' '): st.append(factor * (int(s)-1)) out_dict['TaskName'] = taskname out_dict['SliceTiming'] = st out_string = json.dumps(out_dict, indent=4) logging.info('Writing sidecar func file: '+str(full_output)) with open(full_output, 'w') as out_file: out_file.write(out_string) logging.info('---FINISHED: create_bold_json---')