Пример #1
0
def set_labels_colors(subject, args, stim_dict=None):
    stim_labels = stim_dict['labels'] if stim_dict else None
    if stim_dict is None:
        stim_labels, bipolar = get_stim_labels(subject, args, stim_labels)
        stim_data_fname = op.join(
            MMVT_DIR, subject, 'electrodes',
            'stim_electrodes_{}{}_{}.npz'.format(args.file_frefix,
                                                 'bipolar' if bipolar else '',
                                                 args.stim_channel))
        stim_dict = np.load(stim_data_fname)
    stim_data = stim_dict['data']
    electrode_labeling_fname = op.join(
        MMVT_DIR, subject, 'electrodes',
        '{}_{}_electrodes_cigar_r_{}_l_{}{}_stim_{}.pkl'.format(
            subject, args.atlas, args.error_radius, args.elec_length,
            '_bipolar' if args.bipolar else '', args.stim_channel))
    elecs_labeling, electrode_labeling_fname = utils.get_electrodes_labeling(
        subject,
        MMVT_DIR,
        args.atlas,
        args.bipolar,
        args.error_radius,
        args.elec_length,
        other_fname=electrode_labeling_fname)
    if elecs_labeling is None:
        print('No electrodes labeling file!')
        return
    electrodes = [e['name'] for e in elecs_labeling]
    if len(set(stim_labels) - set(electrodes)) > 0:
        print(
            "The electrodes labeling isn't calculated for all the stim electrodes!"
        )
        print(set(stim_labels) - set(electrodes))
        return
    labels_elecs_lookup, subcortical_elecs_lookup = create_labels_electordes_lookup(
        elecs_labeling)
    labels_data, data_labels_names = {}, {}
    for hemi in utils.HEMIS:
        labels_data[hemi], data_labels_names[hemi] = \
            calc_labels_data(labels_elecs_lookup[hemi], stim_data, stim_labels, args, hemi)
        output_fname = op.join(
            MMVT_DIR, subject, 'electrodes',
            'stim_labels_{}{}_{}-{}.npz'.format(
                args.file_frefix, 'bipolar' if args.bipolar else '',
                args.stim_channel, hemi))
        np.savez(output_fname,
                 data=labels_data[hemi],
                 names=data_labels_names[hemi],
                 conditions=stim_dict['conditions'])

    subcortical_data, data_subcortical_names = calc_labels_data(
        subcortical_elecs_lookup, stim_data, stim_labels, args)
    output_fname = op.join(
        MMVT_DIR, subject, 'electrodes', 'stim_subcortical_{}{}_{}.npz'.format(
            args.file_frefix, 'bipolar' if args.bipolar else '',
            args.stim_channel))
    np.savez(output_fname,
             data=subcortical_data,
             names=data_subcortical_names,
             conditions=stim_dict['conditions'])
Пример #2
0
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
Пример #3
0
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
Пример #4
0
def get_electrodes_with_cortical_vertices(mri_subject,
                                          atlas,
                                          bipolar,
                                          error_radius=3,
                                          elec_length=4):
    elecs_probs, _ = utils.get_electrodes_labeling(mri_subject, MMVT_DIR,
                                                   atlas, bipolar,
                                                   error_radius, elec_length)
    return [
        elec_probs for elec_probs in elecs_probs
        if len(elec_probs['cortical_indices_dists']) > 0
        and len(elec_probs['cortical_rois']) > 0
    ]
Пример #5
0
def set_labels_colors(subject, args, stim_dict=None):
    stim_labels = stim_dict['labels'] if stim_dict else None
    stim_labels, bipolar = get_stim_labels(subject, args, stim_labels)
    if stim_dict is None:
        stim_data_fname = op.join(MMVT_DIR, subject, 'electrodes', 'stim_electrodes_{}{}_{}.npz'.format(
                args.file_frefix, 'bipolar' if bipolar else '', args.stim_channel))
        stim_dict = np.load(stim_data_fname)
    stim_data = stim_dict['data']
    electrode_labeling_fname = op.join(MMVT_DIR, subject, 'electrodes',
            '{}_{}_electrodes_cigar_r_{}_l_{}{}_stim_{}.pkl'.format(subject, args.atlas, args.error_radius,
            args.elec_length, '_bipolar' if bipolar else '', args.stim_channel))
    elecs_labeling, electrode_labeling_fname = utils.get_electrodes_labeling(
        subject, MMVT_DIR, args.atlas, bipolar, args.error_radius, args.elec_length,
        other_fname=electrode_labeling_fname)
    if elecs_labeling is None:
        print('No electrodes labeling file!')
        return
    electrodes = [e['name'] for e in elecs_labeling]
    if len(set(stim_labels) - set(electrodes)) > 0:
        print("The electrodes labeling isn't calculated for all the stim electrodes!")
        print(set(stim_labels) - set(electrodes))
        return
    labels_elecs_lookup, subcortical_elecs_lookup = create_labels_electordes_lookup(
        subject, args.atlas, elecs_labeling, args.n_jobs)
    labels_data, labels_colors, data_labels_names = {}, {}, {}
    for hemi in utils.HEMIS:
        labels_data[hemi], labels_colors[hemi], data_labels_names[hemi] = \
            calc_labels_data(labels_elecs_lookup[hemi], stim_data, stim_labels, hemi)
        output_fname = op.join(MMVT_DIR, subject, 'electrodes', 'stim_labels_{}{}_{}-{}.npz'.format(
                args.file_frefix, 'bipolar' if bipolar else '', args.stim_channel, hemi))
        np.savez(output_fname, data=labels_data[hemi], names=data_labels_names[hemi],
                 conditions=stim_dict['conditions'], colors=labels_colors[hemi])

    subcortical_data, subcortical_colors, data_subcortical_names = calc_labels_data(
        subcortical_elecs_lookup, stim_data, stim_labels)
    output_fname = op.join(MMVT_DIR, subject, 'electrodes', 'stim_subcortical_{}{}_{}.npz'.format(
            args.file_frefix, 'bipolar' if bipolar else '', args.stim_channel))
    np.savez(output_fname, data=subcortical_data, names=data_subcortical_names,
             conditions=stim_dict['conditions'], colors=subcortical_colors)
Пример #6
0
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)))
Пример #7
0
def get_electrodes_with_cortical_vertices(mri_subject, atlas, bipolar, error_radius=3, elec_length=4):
    elecs_probs, _ = utils.get_electrodes_labeling(
        mri_subject, MMVT_DIR, atlas, bipolar, error_radius, elec_length)
    return [elec_probs for elec_probs in elecs_probs if
            len(elec_probs['cortical_indices_dists']) > 0 and len(elec_probs['cortical_rois']) > 0]