Example #1
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
def calc_con_colors(con_values, high_low_diff):
    M = con_values.shape[0]
    stat_data = utils.calc_stat_data(con_values, STAT_AVG, axis=con_values.ndim-1)
    # con_colors = utils.arr_to_colors(stat_data, 0, 1)[:, :3]
    # con_colors = utils.arr_to_colors_two_colors_maps(stat_data, 0, 1, 'RdPu', 'hot', 0.05, flip_cm_big=True)[:, :3]
    from src.mmvt_addon import colors_utils
    red = np.array(colors_utils.name_to_rgb('red')) / 255.0
    blue = np.array(colors_utils.name_to_rgb('blue')) / 255.0
    magenta = np.array(colors_utils.name_to_rgb('magenta')) / 255.0
    green = np.array(colors_utils.name_to_rgb('green')) / 255.0
    if con_values.ndim == 2:
        con_colors = np.zeros((M, 3))
        con_colors[(stat_data <= 0.05) & (high_low_diff >= 0)] = red
        con_colors[(stat_data <= 0.05) & (high_low_diff < 0)] = blue
        con_colors[(stat_data > 0.05) & (high_low_diff >= 0)] = magenta
        con_colors[(stat_data > 0.05) & (high_low_diff < 0)] = green
    elif con_values.ndim == 3:
        W = con_values.shape[1]
        con_colors = np.zeros((M, W, 3))
        for w in range(W):
            stat_w = stat_data[:, w]
            high_low_diff_w = high_low_diff[:, w]
            sig_high = (abs(stat_w) >= -np.log10(0.05)) & (high_low_diff_w >= 0)
            sig_low =  (abs(stat_w) >= -np.log10(0.05)) & (high_low_diff_w < 0)
            print(w, sig_high, sig_low)
            con_colors[sig_high, w] = red
            con_colors[sig_low, w] = blue
            con_colors[(abs(stat_w) < -np.log10(0.05)) & (high_low_diff_w >= 0), w] = (1, 1, 1)
            con_colors[(abs(stat_w) < -np.log10(0.05)) & (high_low_diff_w < 0), w] = (1, 1, 1)
    # con_colors = con_colors[:, :, :, np.newaxis]
    return con_colors
Example #3
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
Example #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
Example #5
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
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
Example #7
0
def calc_con_colors(con_values, high_low_diff):
    M = con_values.shape[0]
    stat_data = utils.calc_stat_data(con_values,
                                     STAT_AVG,
                                     axis=con_values.ndim - 1)
    # con_colors = utils.arr_to_colors(stat_data, 0, 1)[:, :3]
    # con_colors = utils.arr_to_colors_two_colors_maps(stat_data, 0, 1, 'RdPu', 'hot', 0.05, flip_cm_big=True)[:, :3]
    from src.mmvt_addon import colors_utils
    red = np.array(colors_utils.name_to_rgb('red')) / 255.0
    blue = np.array(colors_utils.name_to_rgb('blue')) / 255.0
    magenta = np.array(colors_utils.name_to_rgb('magenta')) / 255.0
    green = np.array(colors_utils.name_to_rgb('green')) / 255.0
    if con_values.ndim == 2:
        con_colors = np.zeros((M, 3))
        con_colors[(stat_data <= 0.05) & (high_low_diff >= 0)] = red
        con_colors[(stat_data <= 0.05) & (high_low_diff < 0)] = blue
        con_colors[(stat_data > 0.05) & (high_low_diff >= 0)] = magenta
        con_colors[(stat_data > 0.05) & (high_low_diff < 0)] = green
    elif con_values.ndim == 3:
        W = con_values.shape[1]
        con_colors = np.zeros((M, W, 3))
        for w in range(W):
            stat_w = stat_data[:, w]
            high_low_diff_w = high_low_diff[:, w]
            sig_high = (abs(stat_w) >= -np.log10(0.05)) & (high_low_diff_w >=
                                                           0)
            sig_low = (abs(stat_w) >= -np.log10(0.05)) & (high_low_diff_w < 0)
            print(w, sig_high, sig_low)
            con_colors[sig_high, w] = red
            con_colors[sig_low, w] = blue
            con_colors[(abs(stat_w) < -np.log10(0.05)) &
                       (high_low_diff_w >= 0), w] = (1, 1, 1)
            con_colors[(abs(stat_w) < -np.log10(0.05)) & (high_low_diff_w < 0),
                       w] = (1, 1, 1)
    # con_colors = con_colors[:, :, :, np.newaxis]
    return con_colors