def prepare_darpa_csv(subject, bipolar, atlas, good_channels=None, groups_ordering=None, error_radius=3, elec_length=4, p_threshold=0.05): elecs_names, elecs_coords = elec_pre.read_electrodes_file(subject, bipolar) elecs_coords_mni = fu.transform_subject_to_mni_coordinates(subject, elecs_coords, SUBJECTS_DIR) elec_pre.save_electrodes_coords(elecs_names, elecs_coords_mni, good_channels) elecs_coords_mni_dic = {elec_name:elec_coord for (elec_name,elec_coord) in zip(elecs_names, elecs_coords_mni)} elecs_probs, _ = utils.get_electrodes_labeling(subject, BLENDER_ROOT_DIR, atlas, bipolar, error_radius, elec_length) assert(len(elecs_names) == len(elecs_coords_mni) == len(elecs_probs)) most_probable_rois = elec_pre.get_most_probable_rois(elecs_probs, p_threshold, good_channels) rois_colors = elec_pre.get_rois_colors(most_probable_rois) elec_pre.save_rois_colors_legend(subject, rois_colors, bipolar) utils.make_dir(op.join(BLENDER_ROOT_DIR, 'colin27', 'coloring')) results = defaultdict(list) for elec_name, elec_probs in zip(elecs_names, elecs_probs): assert(elec_name == elec_probs['name']) if not good_channels is None and elec_name not in good_channels: continue group = get_elec_group(elec_name, bipolar) roi = elec_pre.get_most_probable_roi([*elec_probs['cortical_probs'], *elec_probs['subcortical_probs']], [*elec_probs['cortical_rois'], *elec_probs['subcortical_rois']], p_threshold) color = rois_colors[utils.get_hemi_indifferent_roi(roi)] results[group].append(dict(name=elec_name, roi=roi, color=color)) coloring_fname = 'electrodes{}_coloring.csv'.format('_bipolar' if bipolar else '') with open(op.join(OUTPUT_DIR, '{}_electrodes_info.csv'.format(subject)), 'w') as csv_file, \ open(op.join(BLENDER_ROOT_DIR, 'colin27', 'coloring', coloring_fname), 'w') as colors_csv_file: csv_writer = csv.writer(csv_file, delimiter=',') colors_csv_writer = csv.writer(colors_csv_file, delimiter=',') elec_ind = 0 for group in groups_ordering: group_res = sorted(results[group], key=lambda x:natural_keys(x['name'])) for res in group_res: csv_writer.writerow([elec_ind, res['name'], *elecs_coords_mni_dic[res['name']], res['roi'], *res['color']]) colors_csv_writer.writerow([res['name'], *res['color']]) elec_ind += 1
def prepare_darpa_csv(subject, bipolar, atlas, good_channels=None, groups_ordering=None, error_radius=3, elec_length=4, p_threshold=0.05): elecs_names, elecs_coords = elec_pre.read_electrodes_file(subject, bipolar) elecs_coords_mni = fu.transform_subject_to_mni_coordinates( subject, elecs_coords, SUBJECTS_DIR) elec_pre.save_electrodes_coords(elecs_names, elecs_coords_mni, good_channels) elecs_coords_mni_dic = { elec_name: elec_coord for (elec_name, elec_coord) in zip(elecs_names, elecs_coords_mni) } elecs_probs, _ = utils.get_electrodes_labeling(subject, BLENDER_ROOT_DIR, atlas, bipolar, error_radius, elec_length) assert (len(elecs_names) == len(elecs_coords_mni) == len(elecs_probs)) most_probable_rois = elec_pre.get_most_probable_rois( elecs_probs, p_threshold, good_channels) rois_colors = elec_pre.get_rois_colors(most_probable_rois) elec_pre.save_rois_colors_legend(subject, rois_colors, bipolar) utils.make_dir(op.join(BLENDER_ROOT_DIR, 'colin27', 'coloring')) results = defaultdict(list) for elec_name, elec_probs in zip(elecs_names, elecs_probs): assert (elec_name == elec_probs['name']) if not good_channels is None and elec_name not in good_channels: continue group = get_elec_group(elec_name, bipolar) roi = elec_pre.get_most_probable_roi( [*elec_probs['cortical_probs'], *elec_probs['subcortical_probs']], [*elec_probs['cortical_rois'], *elec_probs['subcortical_rois']], p_threshold) color = rois_colors[utils.get_hemi_indifferent_roi(roi)] results[group].append(dict(name=elec_name, roi=roi, color=color)) coloring_fname = 'electrodes{}_coloring.csv'.format( '_bipolar' if bipolar else '') with open(op.join(OUTPUT_DIR, '{}_electrodes_info.csv'.format(subject)), 'w') as csv_file, \ open(op.join(BLENDER_ROOT_DIR, 'colin27', 'coloring', coloring_fname), 'w') as colors_csv_file: csv_writer = csv.writer(csv_file, delimiter=',') colors_csv_writer = csv.writer(colors_csv_file, delimiter=',') elec_ind = 0 for group in groups_ordering: group_res = sorted(results[group], key=lambda x: natural_keys(x['name'])) for res in group_res: csv_writer.writerow([ elec_ind, res['name'], *elecs_coords_mni_dic[res['name']], res['roi'], *res['color'] ]) colors_csv_writer.writerow([res['name'], *res['color']]) elec_ind += 1
def create_electrodes_labeling_coloring(subject, bipolar, atlas, good_channels=None, error_radius=3, elec_length=4, p_threshold=0.05, legend_name='', coloring_fname=''): elecs_names, elecs_coords = read_electrodes_file(subject, bipolar) elecs_probs, electrode_labeling_fname = utils.get_electrodes_labeling( subject, MMVT_DIR, atlas, bipolar, error_radius, elec_length) if elecs_probs is None: print('No electrodes labeling file!') return if electrode_labeling_fname != op.join(MMVT_DIR, subject, 'electrodes', op.basename(electrode_labeling_fname)): shutil.copy(electrode_labeling_fname, op.join(MMVT_DIR, subject, 'electrodes', op.basename(electrode_labeling_fname))) most_probable_rois = get_most_probable_rois(elecs_probs, p_threshold, good_channels) rois_colors_rgbs, rois_colors_names = get_rois_colors(subject, atlas, most_probable_rois) save_rois_colors_legend(subject, rois_colors_rgbs, bipolar, legend_name) utils.make_dir(op.join(MMVT_DIR, subject, 'coloring')) if coloring_fname == '': coloring_fname = 'electrodes{}_coloring.csv'.format('_bipolar' if bipolar else '') coloring_fol = op.join(MMVT_DIR, subject, 'coloring') coloring_fname = op.join(coloring_fol, coloring_fname) colors_names_fname = op.join(coloring_fol, 'electrodes{}_colors_names.txt'.format('_bipolar' if bipolar else '')) elec_names_rois_colors = defaultdict(list) with open(coloring_fname, 'w') as colors_rgbs_file, open(colors_names_fname, 'w') as colors_names_file: # colors_csv_writer = csv.writer(colors_csv_file, delimiter=',') for elec_name, elec_probs in zip(elecs_names, elecs_probs): assert(elec_name == elec_probs['name']) if not good_channels is None and elec_name not in good_channels: continue roi = get_most_probable_roi([*elec_probs['cortical_probs'], *elec_probs['subcortical_probs']], [*elec_probs['cortical_rois'], *elec_probs['subcortical_rois']], p_threshold) if roi != '': inv_roi = utils.get_hemi_indifferent_roi(roi) colors_rgbs_file.write('{},{},{},{}\n'.format(elec_name, *rois_colors_rgbs[inv_roi])) colors_names_file.write('{},{},{}\n'.format(elec_name, inv_roi, rois_colors_names[inv_roi])) elec_names_rois_colors[inv_roi].append(elec_name) # colors_csv_writer.writerow([elec_name, *color_rgb]) with open(op.join(coloring_fol, 'electrodes_report.txt'), 'w') as elecs_report_file: for inv_roi, electrodes_names in elec_names_rois_colors.items(): elecs_report_file.write('{},{},{}\n'.format(inv_roi, rois_colors_names[inv_roi], ','.join(electrodes_names)))