Esempio n. 1
0
def calc_connections_colors(data, labels, hemis, args):
    # stat, conditions, w, threshold=0, threshold_percentile=0, color_map='jet',
    #                         norm_by_percentile=True, norm_percs=(1, 99), symetric_colors=True):
    M = data.shape[0]
    W = data.shape[2] if args.windows == 0 else args.windows
    L = int((M * M + M) / 2 - M)
    con_indices = np.zeros((L, 2))
    con_values = np.zeros((L, W, len(args.conditions)))
    con_names = [None] * L
    con_type = np.zeros((L))
    for cond in range(len(args.conditions)):
        for w in range(W):
            for ind, (i, j) in enumerate(utils.lower_rec_indices(M)):
                if W > 1:
                    con_values[ind, w, cond] = data[i, j, w, cond]
                elif data.ndim > 2:
                    con_values[ind, w, cond] = data[i, j, cond]
                else:
                    con_values[ind, w, cond] = data[i, j]
    if len(args.conditions) > 1:
        stat_data = utils.calc_stat_data(con_values, args.stat)
    else:
        stat_data = np.squeeze(con_values)

    for ind, (i, j) in enumerate(utils.lower_rec_indices(M)):
        con_indices[ind, :] = [i, j]
        con_names[ind] = '{}-{}'.format(labels[i], labels[j])
        con_type[ind] = HEMIS_WITHIN if hemis[i] == hemis[j] else HEMIS_BETWEEN

    con_indices = con_indices.astype(np.int)
    con_names = np.array(con_names)
    if args.threshold_percentile > 0:
        args.threshold = np.percentile(np.abs(stat_data),
                                       args.threshold_percentile)
    if args.threshold >= 0:
        indices = np.where(np.abs(stat_data) > args.threshold)[0]
        # con_colors = con_colors[indices]
        con_indices = con_indices[indices]
        con_names = con_names[indices]
        con_values = con_values[indices]
        con_type = con_type[indices]
        stat_data = stat_data[indices]

    con_values = np.squeeze(con_values)
    if args.data_max == 0 and args.data_min == 0:
        data_max, data_min = utils.get_data_max_min(stat_data,
                                                    args.norm_by_percentile,
                                                    args.norm_percs)
        if args.symetric_colors and np.sign(data_max) != np.sign(data_min):
            data_minmax = max(map(abs, [data_max, data_min]))
            data_max, data_min = data_minmax, -data_minmax
    else:
        data_max, data_min = args.data_max, args.data_min
    con_colors = utils.mat_to_colors(stat_data, data_min, data_max,
                                     args.color_map)

    print(len(con_names))
    return con_colors, con_indices, con_names, con_values, con_type, data_max, data_min
Esempio n. 2
0
def calc_connections_colors(data, labels, hemis, args):
    # stat, conditions, w, threshold=0, threshold_percentile=0, color_map='jet',
    #                         norm_by_percentile=True, norm_percs=(1, 99), symetric_colors=True):
    M = data.shape[0]
    W = data.shape[2] if args.windows == 0 else args.windows
    L = int((M * M + M) / 2 - M)
    con_indices = np.zeros((L, 2))
    con_values = np.zeros((L, W, len(args.conditions)))
    con_names = [None] * L
    con_type = np.zeros((L))
    for cond in range(len(args.conditions)):
        for w in range(W):
            for ind, (i, j) in enumerate(utils.lower_rec_indices(M)):
                if W > 1 and data.ndim == 4:
                    con_values[ind, w, cond] = data[i, j, w, cond]
                elif data.ndim > 2:
                    con_values[ind, w, cond] = data[i, j, cond]
                else:
                    con_values[ind, w, cond] = data[i, j]
    if len(args.conditions) > 1:
        stat_data = utils.calc_stat_data(con_values, args.stat)
    else:
        stat_data = np.squeeze(con_values)

    for ind, (i, j) in enumerate(utils.lower_rec_indices(M)):
        con_indices[ind, :] = [i, j]
        con_names[ind] = '{}-{}'.format(labels[i], labels[j])
        con_type[ind] = HEMIS_WITHIN if hemis[i] == hemis[j] else HEMIS_BETWEEN

    con_indices = con_indices.astype(np.int)
    con_names = np.array(con_names)
    data_max, data_min = utils.get_data_max_min(stat_data, args.norm_by_percentile, args.norm_percs)
    data_minmax = max(map(abs, [data_max, data_min]))
    if args.threshold_percentile > 0:
        args.threshold = np.percentile(np.abs(stat_data), args.threshold_percentile)
    if args.threshold > data_minmax:
        raise Exception('threshold > abs(max(data)) ({})'.format(data_minmax))
    if args.threshold >= 0:
        indices = np.where(np.abs(stat_data) > args.threshold)[0]
        # con_colors = con_colors[indices]
        con_indices = con_indices[indices]
        con_names = con_names[indices]
        con_values = con_values[indices]
        con_type  = con_type[indices]
        stat_data = stat_data[indices]

    con_values = np.squeeze(con_values)
    if args.data_max == 0 and args.data_min == 0:
        if args.symetric_colors and np.sign(data_max) != np.sign(data_min):
            data_max, data_min = data_minmax, -data_minmax
    else:
        data_max, data_min = args.data_max, args.data_min
    print('data_max: {}, data_min: {}'.format(data_max, data_min))
    con_colors = utils.mat_to_colors(stat_data, data_min, data_max, args.color_map)

    print(len(con_names))
    return con_colors, con_indices, con_names, con_values, con_type, data_max, data_min
Esempio n. 3
0
def calc_connections_colors(data,
                            labels,
                            hemis,
                            stat,
                            w,
                            threshold=0,
                            threshold_percentile=0,
                            color_map='jet',
                            norm_by_percentile=True,
                            norm_percs=(1, 99)):
    M = data.shape[0]
    W = data.shape[2] if w == 0 else w
    L = int((M * M + M) / 2 - M)
    con_indices = np.zeros((L, 2))
    con_values = np.zeros((L, W, 2))
    con_names = [None] * L
    con_type = np.zeros((L))
    axis = data.ndim - 1
    coh_stat = utils.calc_stat_data(data, stat, axis=axis)
    x = coh_stat.ravel()
    data_max, data_min = utils.get_data_max_min(x, norm_by_percentile,
                                                norm_percs)
    data_minmax = max(map(abs, [data_max, data_min]))
    for cond in range(2):
        for w in range(W):
            for ind, (i, j) in enumerate(utils.lower_rec_indices(M)):
                if W > 1:
                    con_values[ind, w, cond] = data[i, j, w, cond]
                else:
                    con_values[ind, w, cond] = data[i, j, cond]
    stat_data = utils.calc_stat_data(con_values, stat)
    con_colors = utils.mat_to_colors(stat_data, -data_minmax, data_minmax,
                                     color_map)

    for ind, (i, j) in enumerate(utils.lower_rec_indices(M)):
        con_indices[ind, :] = [i, j]
        con_names[ind] = '{}-{}'.format(labels[i], labels[j])
        con_type[ind] = HEMIS_WITHIN if hemis[i] == hemis[j] else HEMIS_BETWEEN

    con_indices = con_indices.astype(np.int)
    con_names = np.array(con_names)
    if threshold_percentile > 0:
        threshold = np.percentile(np.abs(stat_data), threshold_percentile)
    if threshold > 0:
        indices = np.where(np.abs(stat_data) >= threshold)[0]
        con_colors = con_colors[indices]
        con_indices = con_indices[indices]
        con_names = con_names[indices]
        con_values = con_values[indices]
        con_type = con_type[indices]
    print(len(con_names))
    return con_colors, con_indices, con_names, con_values, con_type
Esempio n. 4
0
def calc_connections_colors(subject,
                            data,
                            labels,
                            hemis,
                            stat,
                            threshold=0,
                            color_map='jet',
                            norm_by_percentile=True,
                            norm_percs=(1, 99)):
    # cm_big='YlOrRd', cm_small='PuBu', flip_cm_big=True, flip_cm_small=False):
    M = data.shape[0]
    W = data.shape[2]
    L = int((M * M + M) / 2 - M)
    # con_colors = np.zeros((L, W, 3))
    con_indices = np.zeros((L, 2))
    con_values = np.zeros((L, W, 2))
    con_names = [None] * L
    con_type = np.zeros((L))
    coh_stat = utils.calc_stat_data(data, stat, axis=3)
    x = coh_stat.ravel()
    data_max, data_min = utils.get_data_max_min(x, norm_by_percentile,
                                                norm_percs)
    data_minmax = max(map(abs, [data_max, data_min]))
    # sm = utils.get_scalar_map(threshold, data_max, color_map=color_map)
    for cond in range(2):
        for w in range(W):
            # win_colors = utils.mat_to_colors(coh[:, :, w, cond], threshold, max_x, color_map, sm)
            # coh_arr = utils.lower_rec_to_arr(coh[:, :, w, cond])
            # win_colors = utils.arr_to_colors(coh_arr, threshold, max_x, color_map, sm)
            for ind, (i, j) in enumerate(utils.lower_rec_indices(M)):
                # con_colors[ind, w, cond, :] = win_colors[ind][:3]
                con_values[ind, w, cond] = data[i, j, w, cond]
    stat_data = utils.calc_stat_data(con_values, stat)
    con_colors = utils.mat_to_colors(stat_data, -data_minmax, data_minmax,
                                     color_map)

    for ind, (i, j) in enumerate(utils.lower_rec_indices(M)):
        con_indices[ind, :] = [i, j]
        con_names[ind] = '{}-{}'.format(labels[i].astype(str),
                                        labels[j].astype(str))
        con_type[ind] = HEMIS_WITHIN if hemis[i] == hemis[j] else HEMIS_BETWEEN

    print(L, ind)
    con_indices = con_indices.astype(np.int)
    return con_colors, con_indices, con_names, con_values, con_type
Esempio n. 5
0
def flatten_data(data, w=0):
    M = data.shape[0]
    L = int((M*M+M)/2-M)
    W = data.shape[2] if w == 0 else w
    values = np.zeros((L, W, 2))
    for cond in range(2):
        for w in range(W):
            for ind, (i, j) in enumerate(utils.lower_rec_indices(M)):
                values[ind, 0, cond] = data[i, j, cond]
    return values
def flatten_data(data, w=0):
    M = data.shape[0]
    L = int((M*M+M)/2-M)
    W = data.shape[2] if w == 0 else w
    values = np.zeros((L, W, 2))
    for cond in range(2):
        for w in range(W):
            for ind, (i, j) in enumerate(utils.lower_rec_indices(M)):
                values[ind, 0, cond] = data[i, j, cond]
    return values
def calc_connections_colors(data, labels, hemis, stat, w, threshold=0, threshold_percentile=0, color_map='jet',
                            norm_by_percentile=True, norm_percs=(1, 99)):
    M = data.shape[0]
    W = data.shape[2] if w == 0 else w
    L = int((M * M + M) / 2 - M)
    con_indices = np.zeros((L, 2))
    con_values = np.zeros((L, W, 2))
    con_names = [None] * L
    con_type = np.zeros((L))
    axis = data.ndim - 1
    coh_stat = utils.calc_stat_data(data, stat, axis=axis)
    x = coh_stat.ravel()
    data_max, data_min = utils.get_data_max_min(x, norm_by_percentile, norm_percs)
    data_minmax = max(map(abs, [data_max, data_min]))
    for cond in range(2):
        for w in range(W):
            for ind, (i, j) in enumerate(utils.lower_rec_indices(M)):
                if W > 1:
                    con_values[ind, w, cond] = data[i, j, w, cond]
                else:
                    con_values[ind, w, cond] = data[i, j, cond]
    stat_data = utils.calc_stat_data(con_values, stat)
    con_colors = utils.mat_to_colors(stat_data, -data_minmax, data_minmax, color_map)

    for ind, (i, j) in enumerate(utils.lower_rec_indices(M)):
        con_indices[ind, :] = [i, j]
        con_names[ind] = '{}-{}'.format(labels[i], labels[j])
        con_type[ind] = HEMIS_WITHIN if hemis[i] == hemis[j] else HEMIS_BETWEEN

    con_indices = con_indices.astype(np.int)
    con_names = np.array(con_names)
    if threshold_percentile > 0:
        threshold = np.percentile(np.abs(stat_data), threshold_percentile)
    if threshold > 0:
        indices = np.where(np.abs(stat_data) >= threshold)[0]
        con_colors = con_colors[indices]
        con_indices = con_indices[indices]
        con_names = con_names[indices]
        con_values = con_values[indices]
        con_type  = con_type[indices]
    print(len(con_names))
    return con_colors, con_indices, con_names, con_values, con_type
Esempio n. 8
0
def compare_coh_windows(subject, task, conditions, electrodes, freqs=((8, 12), (12, 25), (25,55), (55,110)), do_plot=False):
    electrodes_coh = np.load(op.join(ELECTRODES_DIR, subject, task, 'electrodes_coh_windows.npy'))
    meg_electrodes_coh = np.load(op.join(ELECTRODES_DIR, subject, task, 'meg_electrodes_ts_coh_windows.npy'))
    figs_fol = op.join(MMVT_DIR, subject, 'figs', 'coh_windows')
    utils.make_dir(figs_fol)
    results = []
    for cond_id, cond in enumerate(conditions):
        now = time.time()
        for freq_id, freq in enumerate(freqs):
            freq = '{}-{}'.format(*freq)
            indices = list(utils.lower_rec_indices(electrodes_coh.shape[0]))
            for ind, (i, j) in enumerate(indices):
                utils.time_to_go(now, ind, len(indices))
                meg = meg_electrodes_coh[i, j, :, freq_id, cond_id][:22]
                elc = electrodes_coh[i, j, :, freq_id, cond_id][:22]

                elc_diff = np.max(elc) - np.min(elc)
                meg *= elc_diff / (np.max(meg) - np.min(meg))
                meg += np.mean(elc) - np.mean(meg)

                if sum(meg) > len(meg) * 0.99:
                    continue
                data_diff = meg - elc
                # data_diff = data_diff / max(data_diff)
                rms = np.sqrt(np.mean(np.power(data_diff, 2)))
                corr = np.corrcoef(meg, elc)[0, 1]
                results.append(dict(elc1=electrodes[i], elc2=electrodes[j], cond=cond, freq=freq, rms=rms, corr=corr))
                if electrodes[i]=='RAF6' and electrodes[j] == 'LOF4': #corr > 10 and rms < 3:
                    plt.figure()
                    plt.plot(meg, label='prediction')
                    plt.plot(elc, label='electrode')
                    plt.legend()
                    # plt.title('{}-{} {} {}'.format(electrodes[i], electrodes[j], freq, cond)) # (rms:{:.2f})
                    plt.savefig(op.join(figs_fol, '{:.2f}-{}-{}-{}-{}.jpg'.format(rms, electrodes[i], electrodes[j], freq, cond)))
                    plt.close()

    results_fname = op.join(figs_fol, 'results{}.csv'.format('_bipolar' if bipolar else ''))
    rmss, corrs = [], []
    with open(results_fname, 'w') as output_file:
        for res in results:
            output_file.write('{},{},{},{},{},{}\n'.format(
                res['elc1'], res['elc2'], res['cond'], res['freq'], res['rms'], res['corr']))
            rmss.append(res['rms'])
            corrs.append(res['corr'])
    rmss = np.array(rmss)
    corrs = np.array(corrs)
    pass
Esempio n. 9
0
def compare_coh_windows(subject, task, conditions, electrodes, freqs=((8, 12), (12, 25), (25,55), (55,110)), do_plot=False):
    electrodes_coh = np.load(op.join(ELECTRODES_DIR, subject, task, 'electrodes_coh_windows.npy'))
    meg_electrodes_coh = np.load(op.join(ELECTRODES_DIR, subject, task, 'meg_electrodes_ts_coh_windows.npy'))
    figs_fol = op.join(MMVT_DIR, subject, 'figs', 'coh_windows')
    utils.make_dir(figs_fol)
    results = []
    for cond_id, cond in enumerate(conditions):
        now = time.time()
        for freq_id, freq in enumerate(freqs):
            freq = '{}-{}'.format(*freq)
            indices = list(utils.lower_rec_indices(electrodes_coh.shape[0]))
            for ind, (i, j) in enumerate(indices):
                utils.time_to_go(now, ind, len(indices))
                meg = meg_electrodes_coh[i, j, :, freq_id, cond_id][:22]
                elc = electrodes_coh[i, j, :, freq_id, cond_id][:22]

                elc_diff = np.max(elc) - np.min(elc)
                meg *= elc_diff / (np.max(meg) - np.min(meg))
                meg += np.mean(elc) - np.mean(meg)

                if sum(meg) > len(meg) * 0.99:
                    continue
                data_diff = meg - elc
                # data_diff = data_diff / max(data_diff)
                rms = np.sqrt(np.mean(np.power(data_diff, 2)))
                corr = np.corrcoef(meg, elc)[0, 1]
                results.append(dict(elc1=electrodes[i], elc2=electrodes[j], cond=cond, freq=freq, rms=rms, corr=corr))
                if False: #do_plot and electrodes[i]=='RPT7' and electrodes[j] == 'RPT5': #corr > 10 and rms < 3:
                    plt.figure()
                    plt.plot(meg, label='pred')
                    plt.plot(elc, label='elec')
                    plt.legend()
                    # plt.title('{}-{} {} {}'.format(electrodes[i], electrodes[j], freq, cond)) # (rms:{:.2f})
                    plt.savefig(op.join(figs_fol, '{:.2f}-{}-{}-{}-{}.jpg'.format(rms, electrodes[i], electrodes[j], freq, cond)))
                    plt.close()

    results_fname = op.join(figs_fol, 'results{}.csv'.format('_bipolar' if bipolar else ''))
    rmss, corrs = [], []
    with open(results_fname, 'w') as output_file:
        for res in results:
            output_file.write('{},{},{},{},{},{}\n'.format(
                res['elc1'], res['elc2'], res['cond'], res['freq'], res['rms'], res['corr']))
            rmss.append(res['rms'])
            corrs.append(res['corr'])
    rmss = np.array(rmss)
    corrs = np.array(corrs)
    pass