示例#1
0
def optimizeColormap(name, cmap_type=None, l_range=(0.0, 1.0)):
    cmap = plt.get_cmap(name)
    # Get values, discard alpha
    x = np.linspace(0, 1, 256)
    values = cmap(x)[:, :3]
    lab_colors = []
    for rgb in values:
        lab_colors.append(convert_color(sRGBColor(*rgb), target_cs=LabColor))

    if cmap_type == "flat":
        mean = np.mean([_i.lab_l for _i in lab_colors])
        target_lightness = optimalLightness(len(x),
                                            cmap_type=cmap_type,
                                            l_range=(mean, mean))
    else:
        target_lightness = optimalLightness(
            len(x), cmap_type=cmap_type, l_range=l_range) * 100.0

    for color, lightness in zip(lab_colors, target_lightness):
        color.lab_l = lightness
    # Go back to rbg.
    rgb_colors = [convert_color(_i, target_cs=sRGBColor) for _i in lab_colors]
    # Clamp values as colorspace of LAB is larger then sRGB.
    rgb_colors = [(_i.clamped_rgb_r, _i.clamped_rgb_g, _i.clamped_rgb_b)
                  for _i in rgb_colors]

    cm = matplotlib.colors.LinearSegmentedColormap.from_list(name=name +
                                                             "_optimized",
                                                             colors=rgb_colors)
    return cm
def all_core_periphery(networks_orig, networks_opt):
    output = np.zeros((10, 15, 4))
    output_std = np.zeros((10, 15, 4))
    for j in range(10):
        classifications, per_comm_assignments, _, _ = core_periphery_analysis(
            networks_orig[j])
        for i in range(15):
            classified_vals, _ = classify_vals(classifications,
                                               networks_orig[j],
                                               networks_opt[j][i],
                                               per_comm_assignments)
            for k in range(len(classified_vals)):
                output[j][i][k] = np.mean(classified_vals[k])
                output_std[j][i][k] = np.std(classified_vals[k])
    return output, output_std
    bin_size = 500
    new_pairs = []
    stdev_pairs = []
    j = 0
    # while data[j][0] == 0:
    #     j += 1
    while j < len(data):
        count, x, y = 0, [], []
        while count < bin_size and j + count < len(data):
            x.append(data[j + count][0])
            y.append(data[j + count][1])
            count += 1
        if count < bin_size:
            break
        new_pairs.append((np.mean(x), np.mean(y)))
        stdev_pairs.append((np.std(x), np.std(y)))
        j += count
    new_pairs = np.array(new_pairs)
    stdev_pairs = np.array(stdev_pairs)
    plt.figure(7, figsize=(5.5, 4.5))
    ax = plt.gca()
    plt.scatter(new_pairs[:, 0], new_pairs[:, 1], color='sandybrown', s=30)
    plt.xlabel('Edge degree centrality', fontsize=16)
    plt.ylabel('Optimal weight scaling')
    #plt.ylim([-0.1,3.25])
    plt.ticklabel_format(axis='x', style='sci')
    ax.xaxis.major.formatter.set_powerlimits((0, 0))
    ax.xaxis.major.formatter._useMathText = True
    plt.tight_layout()
    #plt.errorbar(new_pairs[:, 0], new_pairs[:, 1], xerr = stdev_pairs[:,0], yerr=stdev_pairs[:,1], fmt='o', capsize = 2, elinewidth= .5)
示例#4
0
def getWindow(
        image,
        tx_coords,  # x,y
        tx_angle,  # radians
        img_coords,  # x_min, x_max, y_min, y_max
        width=0.8,
        scale=1):
    Nx, Ny = image.shape

    N = Nx

    img_width = img_coords[1] - img_coords[0]
    img_length = img_coords[3] - img_coords[2]
    x_mean = np.mean(img_coords[0:2])
    y_mean = np.mean(img_coords[2:4])
    dx = float(img_width) / Nx
    dy = float(img_length) / Ny

    image_window = np.ones(image.shape)
    x_old = np.zeros(N)
    x_new = np.zeros(N)

    #    tx_img_range = img_coords[2]
    r = img_coords[2] + dy / 2
    has_overflowed = False
    for row in range(Ny):
        row_data = image_window[:, row]

        x_cut = r * np.sin(tx_angle / 2)

        #          x_cut_rel = x_cut / extent[0]
        #          w_idx = np.linspace(0,1,N)

        x_old[:] = np.linspace(x_mean - (2 - width) * x_cut,
                               x_mean + (2 - width) * x_cut,
                               N)  #*0.5/img_width + 0.5
        x_new[:] = np.linspace(img_coords[0], img_coords[1],
                               Nx)  #*0.5/img_width + 0.5

        up = x_new[x_new > x_old[0]]
        updown = up[up <= x_old[-1]]

        #       print(x_cut, updown.shape[0])

        Nlower = Nx - up.shape[0]
        Nhigher = Nx - updown.shape[0] - Nlower

        transition = (0.5 + 0.5 * np.cos(
            np.linspace(0, np.pi, int(Nx * width * x_cut /
                                      (2 * img_width))))) * scale + (1 - scale)

        #       fn = None
        #       if row == 0:
        #          fn = pl.figure()
        #          ax = fn.add_subplot(121)
        #          ax.plot(transition)

        Nt = transition.shape[0]
        if N <= 2 * Nt:
            w = np.ones(N)
            has_overflowed = True
        else:
            w = np.hstack(
                (2 - scale - transition, np.ones(N - 2 * Nt), transition))

        w_new_center = np.interpolate1D(x_old,
                                        w,
                                        updown,
                                        kind='linear',
                                        fill_value=1 - scale)

        w_new = np.hstack((np.ones(Nlower) * (1 - scale), w_new_center,
                           np.ones(Nhigher) * (1 - scale)))

        image_window[:, row] = w_new

        #       if row == 0:
        #          ax = fn.add_subplot(122)
        #          ax.plot(w_new)
        #          fn.savefig('test2.eps')

        r = r + dy

    if has_overflowed:
        print("WARNING: Not making a window")

    return image_window