Esempio n. 1
0
def calc_labels_data(elecs_lookup, stim_data, stim_labels, args, hemi=None):
    stim_labels = np.array(stim_labels)
    labels_names = list(elecs_lookup.keys())
    labels_data = np.zeros(
        (len(labels_names), stim_data.shape[1], stim_data.shape[2]))
    # colors = np.zeros((*labels_data.shape, 3))
    labels_data_names = []
    label_ind = 0
    for label_name, electordes_data in elecs_lookup.items():
        if not hemi is None:
            if lu.get_hemi_from_name(label_name) != hemi:
                continue
        labels_data_names.append(label_name)
        for elec_name, elec_prob in electordes_data:
            elec_inds = np.where(stim_labels == elec_name)[0]
            if len(elec_inds) > 0:
                elec_data = stim_data[elec_inds[0], :, :] * elec_prob
                labels_data[label_ind, :, :] += elec_data
            else:
                print('calc_labels_data: No data found for {}!'.format(
                    elec_name))
        label_ind += 1
    # Calc colors for each freq
    # for freq_id in range(labels_data.shape[2]):
    #     data_min, data_max = utils.calc_min_max(labels_data[:, :, freq_id], norm_percs=args.norm_percs)
    #     colors[:, :, freq_id] = utils.mat_to_colors(
    #         labels_data[:, :, freq_id], data_min, data_max, colorsMap=args.colors_map)
    return labels_data, labels_data_names
Esempio n. 2
0
def save_sz_pick_values(subject, files_names, func_name, atlas):
    bands = dict(theta=[4, 8],
                 alpha=[8, 15],
                 beta=[15, 30],
                 gamma=[30, 55],
                 high_gamma=[65, 120])
    output_fol = utils.make_dir(
        op.join(MMVT_DIR, subject, 'labels', 'labels_data'))
    names = np.load(
        op.join(op.join(MMVT_DIR, subject, 'connectivity',
                        'labels_names.npy')))
    labels_indices = np.load(
        op.join(
            op.join(MMVT_DIR, subject, 'connectivity', 'labels_indices.npy')))
    names = names[labels_indices]
    for band_name in bands.keys():
        output_fname = op.join(output_fol,
                               '{}_{}.npz'.format(func_name, band_name))
        # if op.isfile(output_fname):
        #     continue
        fname = [
            fname for fname in files_names
            if utils.namebase(fname).endswith('SZ')
        ][0]
        file_name = utils.namebase(fname)
        con_name = 'meg_{}_mi'.format(band_name)
        input_fname = op.join(MMVT_DIR, subject, 'connectivity', file_name,
                              '{}_{}.npy'.format(con_name, func_name))
        vals = np.load(input_fname)
        data_min, data_max = np.min(vals), np.max(vals)
        t_axis = np.linspace(-2, 5, vals.shape[1])
        print('onset index: {}'.format(np.where(t_axis > 0)[0][0]))
        # Find pick in time
        t_peak = np.argmax(np.max(vals, axis=0))
        max_vals = vals[:, t_peak]
        # all_vals = np.zeros((len(names)))
        # all_vals = [max_vals[list(names).index(l)] if l in names else 0 for l in labels]
        print('Saving {}'.format(output_fname))
        np.savez(output_fname,
                 data=vals,
                 names=names,
                 atlas=atlas,
                 data_min=data_min,
                 data_max=data_max,
                 title='{}-{}'.format(func_name, band_name),
                 cmap='YlOrRd')
        for hemi in utils.HEMIS:
            labels_output_fname = op.join(
                MMVT_DIR, subject, 'meg',
                'labels_data_epilepsy_laus125_{}_{}_{}.npz'.format(
                    band_name, func_name, hemi))
            hemis = np.array([lu.get_hemi_from_name(l) == hemi for l in names])
            np.savez(labels_output_fname,
                     data=vals[hemis],
                     names=names[hemis],
                     conditions=['sz'])
Esempio n. 3
0
def create_labels_electordes_lookup(subject, atlas, elecs_labeling, n_jobs):
    # delim, pos = lu.get_hemi_delim_and_pos(elecs_labeling[0]['cortical_rois'][0])
    # atlas_labels = lu.get_atlas_labels_names(subject, atlas, delim, pos, n_jobs)
    labels_elecs_lookup = dict(rh=defaultdict(list), lh=defaultdict(list))
    subcortical_elecs_lookup = defaultdict(list)
    # for label_name in atlas_labels['rh'] + atlas_labels['lh']:
    for elec_labeling in elecs_labeling:
        for label_name, label_prob in zip(elec_labeling['cortical_rois'], elec_labeling['cortical_probs']):
            # label_ind = elec_labeling['cortical_rois'].index(label_name)
            # label_prob = elec_labeling['cortical_probs'][label_ind]
            hemi = lu.get_hemi_from_name(label_name)
            labels_elecs_lookup[hemi][label_name].append((elec_labeling['name'], label_prob))
        for label_name, label_prob in zip(elec_labeling['subcortical_rois'], elec_labeling['subcortical_probs']):
            subcortical_elecs_lookup[label_name].append((elec_labeling['name'], label_prob))
    return labels_elecs_lookup, subcortical_elecs_lookup
Esempio n. 4
0
def create_labels_electordes_lookup(elecs_labeling):
    # delim, pos = lu.get_hemi_delim_and_pos(elecs_labeling[0]['cortical_rois'][0])
    # atlas_labels = lu.get_atlas_labels_names(subject, atlas, delim, pos, n_jobs)
    labels_elecs_lookup = dict(rh=defaultdict(list), lh=defaultdict(list))
    subcortical_elecs_lookup = defaultdict(list)
    # for label_name in atlas_labels['rh'] + atlas_labels['lh']:
    for elec_labeling in elecs_labeling:
        for label_name, label_prob in zip(elec_labeling['cortical_rois'],
                                          elec_labeling['cortical_probs']):
            # label_ind = elec_labeling['cortical_rois'].index(label_name)
            # label_prob = elec_labeling['cortical_probs'][label_ind]
            hemi = lu.get_hemi_from_name(label_name)
            labels_elecs_lookup[hemi][label_name].append(
                (elec_labeling['name'], label_prob))
        for label_name, label_prob in zip(elec_labeling['subcortical_rois'],
                                          elec_labeling['subcortical_probs']):
            subcortical_elecs_lookup[label_name].append(
                (elec_labeling['name'], label_prob))
    return labels_elecs_lookup, subcortical_elecs_lookup
Esempio n. 5
0
def calc_labels_data(elecs_lookup, stim_data, stim_labels, hemi=None):
    labels_names = list(elecs_lookup.keys())
    labels_data = np.zeros((len(labels_names), stim_data.shape[1], stim_data.shape[2]))
    colors = np.zeros((*labels_data.shape, 3))
    labels_data_names = []
    label_ind = 0
    for label_name, electordes_data in elecs_lookup.items():
        if not hemi is None:
            if lu.get_hemi_from_name(label_name) != hemi:
                continue
        labels_data_names.append(label_name)
        for elec_name, elec_prob in electordes_data:
            elec_inds = np.where(stim_labels == elec_name)[0]
            if len(elec_inds) > 0:
                elec_data = stim_data[elec_inds[0], :, :] * elec_prob
                labels_data[label_ind, :, :] += elec_data
        label_ind += 1
    # Calc colors for each freq
    for freq_id in range(labels_data.shape[2]):
        data_min, data_max = utils.calc_min_max(labels_data[:, :, freq_id], norm_percs=args.norm_percs)
        colors[:, :, freq_id] = utils.mat_to_colors(
            labels_data[:, :, freq_id], data_min, data_max, colorsMap=args.colors_map)
    return labels_data, colors, labels_data_names
Esempio n. 6
0
def filter_meg_labels_ts(subject,
                         inv_method='dSPM',
                         em='mean_flip',
                         atlas='electrodes_labels',
                         low_freq_cut_off=10,
                         fs=1000,
                         do_plot=False,
                         n_jobs=4):
    folders = glob.glob(
        op.join(MMVT_DIR, subject, 'meg',
                '{}_*_*_Resting_eeg_meg_Demi_ar-epo'.format(subject)))
    now = time.time()
    for fol_ind, fol in enumerate(folders):
        utils.time_to_go(now, fol_ind, len(folders), runs_num_to_print=1)
        files = glob.glob(
            op.join(
                fol, 'labels_data_rest_{}_{}_{}_?h.npz'.format(
                    atlas, inv_method, em)))
        for fname in files:
            hemi = lu.get_hemi_from_name(utils.namebase(fname))
            d = utils.Bag(np.load(fname))
            L = d.data.shape[0]  # labels * time * epochs
            filter_data = np.empty(d.data.shape)
            params = [(d.data[label_ind], label_ind, fs, low_freq_cut_off,
                       do_plot) for label_ind in range(L)]
            results = utils.run_parallel(_filter_meg_label_ts_parallel, params,
                                         n_jobs)
            for filter_label_data, label_ind in results:
                filter_data[label_ind] = filter_label_data
            new_fname = op.join(
                fol, 'labels_data_rest_{}_{}_{}_filter_{}.npz'.format(
                    atlas, inv_method, em, hemi))
            np.savez(new_fname,
                     data=filter_data,
                     names=d.names,
                     conditions=d.conditions)