Ejemplo n.º 1
0
def draw_imgs(prefix, ratio, mode):
    lfw = load_lfw(prefix)

    frames = 0
    _X = list()
    _Y = list()
    _Z = list()
    iters = list()
    threds = list()
    accs = list()
    alphas = list()
    betas = list()
    gammas = list()
    for _iter in xrange(1000, 200000 + 1, 1000):
        _iter1, _thred1, _acc1 = lfw[_iter / 10000]
        _iter2, _thred2, _acc2 = lfw[_iter / 10000 + 1]
        _thred = _thred1 + (_thred2 - _thred1) * (_iter - _iter1) / max(
            1, (_iter2 - _iter1))
        _acc = _acc1 + (_acc2 - _acc1) * (_iter - _iter1) / max(
            1, (_iter2 - _iter1))
        iters.append(_iter)
        threds.append(_thred)
        accs.append(_acc)

        log_file = "./data/%s_%d.txt" % (prefix, _iter)
        if not os.path.exists(log_file):
            print "miss: %s" % log_file
            continue
        lines = open(log_file).readlines()

        # pdf
        pdf = list()
        for line in lines[3:bins + 3]:
            item = line.strip().split()
            item = map(lambda x: float(x), item)
            pdf.append(item[1])

        # clean pdf
        clean_pdf = list()
        for line in lines[bins + 3 + 1:bins + bins + 3 + 1]:
            item = line.strip().split()
            item = map(lambda x: float(x), item)
            clean_pdf.append(item[1])

        # noise pdf
        noise_pdf = list()
        for line in lines[bins + bins + 3 + 1 + 1:bins + bins + bins + 3 + 1 +
                          1]:
            item = line.strip().split()
            item = map(lambda x: float(x), item)
            noise_pdf.append(item[1])

        # pcf
        pcf = list()
        for line in lines[bins + bins + bins + 3 + 1 + 1 + 1:bins + bins +
                          bins + bins + 3 + 1 + 1 + 1]:
            item = line.strip().split()
            item = map(lambda x: float(x), item)
            pcf.append(item[1])

        # weight
        W = list()
        for line in lines[bins + bins + bins + bins + 3 + 1 + 1 + 1 + 1:bins +
                          bins + bins + bins + bins + 3 + 1 + 1 + 1 + 1]:
            item = line.strip().split()
            item = map(lambda x: float(x), item)
            W.append(item[1])

        X = list()
        for i in xrange(bins):
            X.append(i * 0.01 - 1.0)
        _X.append(X)
        _Y.append(bins * [_iter])
        _Z.append(mean_filter(pdf))

        if not choose_iter(_iter, 1000):
            continue

        titlesize = 44
        asize = 44
        glinewidth = 2

        fig = plt.figure(0)
        fig.set_size_inches(24, 18)
        ax = Axes3D(fig)
        #ax.set_title(r"$The\ cos\theta\ distribution\ of\ $" + str(ratio) + "%" + r"$\ noisy\ training\ data\ over\ iteration$", fontsize=titlesize)
        ax.set_xlabel(r"$cos\theta$", fontsize=asize)
        ax.set_ylabel(r"$Iter$", fontsize=asize)
        ax.set_zlabel(r"$Numbers$", fontsize=asize)
        ax.tick_params(labelsize=32)
        ax.set_xlim(-1.0, 1.0)
        ax.set_ylim(0, 200000)
        ax.set_zlim(0.0, 6000.0)
        ax.grid(True, linewidth=glinewidth)
        surf = ax.plot_surface(_X,
                               _Y,
                               _Z,
                               rstride=3,
                               cstride=3,
                               cmap=plt.cm.coolwarm,
                               linewidth=0.1,
                               antialiased=False)
        surf.set_clim([0, 6000])
        cbar = fig.colorbar(surf,
                            shrink=0.5,
                            aspect=10,
                            norm=plt.Normalize(0, 6000))
        cbar.set_ticks([0, 1000, 2000, 3000, 4000, 5000, 6000])
        cbar.set_ticklabels(["0", "1k", "2k", "3k", "4k", "5k", "6k"])
        #cbar.locator = ticker.MaxNLocator(nbins=6)
        #cbar.update_ticks()
        cbar.ax.tick_params(labelsize=24)

        #print dir(ax)
        #_ax = ax.twiny()
        #_ax.set_ylim(0.0, 1.0)
        #_ax.plot(bins * [-1.0], iters, accs, label="LFW")
        #_ax.legend()
        #ax.plot(len(iters) * [-1.0], iters, 100.0 * np.array(accs), color="k", label="LFW")
        #ax.plot(len(iters) * [-1.0], iters, 60.0 * np.array(accs), color="k", label="LFW")
        #ax.legend()

        plt.savefig("./figures/%s_3D_dist_%d.jpg" % (prefix, _iter))
        plt.close()

        frames += 1
        print "frames:", frames
        print "processed:", _iter
        sys.stdout.flush()
Ejemplo n.º 2
0
def tecplot2data(f, oszb, st_sz, filtering, filter):
    print(f'Generating data from {f} with stencil {st_sz[0]}x{st_sz[1]}')
    if os.path.isfile(os.path.join(f, 'res', 'oscillation.dat')):
        file_name = os.path.join(f, 'res', 'oscillation.dat')
    elif os.path.isfile(os.path.join(f, 'res', 'staticbubble.dat')):
        file_name = os.path.join(f, 'res', 'staticbubble.dat')
    elif os.path.isfile(os.path.join(f, 'res', 'gravitational.dat')):
        file_name = os.path.join(f, 'res', 'gravitational.dat')
    else:
        print('file not found')

    with open(file_name, 'r') as myfile:
        data = myfile.read()
        # Append 'zone t' to file for capturing blocks later
        data = data + '\nZONE T'
        # Get variables
        variables = re.split(
            r'"\n"',
            re.search(r'(?<=VARIABLES = ")\w+("\n"\w+)+(?=")', data)[0])
        # Bei StaticBubble müssen die nächsten beiden Zeilen auskommentiert werden
        # variables.remove('X')
        # variables.remove('Y')
        n_vars = len(variables)
        # Get gs
        gs = [
            int(i)
            for i in re.findall(r'\d+',
                                re.search(r'I=\d+, J=\d+, K=\d+', data)[0])
        ]
        [gs[0], gs[1], gs[2]] = [gs[1], gs[0], gs[2]]
        # Get all timesteps (blocks)
        blocks = re.findall(r'ZONE\sT[\d\D]+?(?=ZONE\sT)', data)
        print(f'len(blocks):\t{len(blocks)}')
        # Remove first block (no information)
        # blocks = blocks[1:]

        # Get x/y from first block
        # coordinates = {}
        # block = blocks[1]
        block = blocks[0]
        numbers = np.array(re.findall(r'(\-?\d\.\d+E[\+\-]\d{2})', block))
        # print(f'len(numbers):\t{len(numbers)}')
        print(f'gs:\t{gs}')
        coordinates = np.empty((2, gs[0], gs[1], gs[2]))
        # Get x coordinates
        coordinates[0, :, :, :] = np.reshape(numbers[:np.prod(gs)],
                                             (gs[0], gs[1], gs[2]))
        # Get y coordinates
        coordinates[1, :, :, :] = np.reshape(
            numbers[np.prod(gs):2 * np.prod(gs)], (gs[0], gs[1], gs[2]))
        max_x = np.max(coordinates[0, :, :, :])
        max_y = np.max(coordinates[1, :, :, :])
        delta = max_x / (gs[0] - 1)
        coordinates = np.reshape(coordinates[:, :, :, 0], (2, gs[0], gs[1]))

        # Coordinates are messed up, make own coordinate matrix
        coordinates = np.empty((2, gs[0] - 1, gs[1] - 1))
        cord_vec = np.reshape(np.array(range(0, gs[0] - 1) * delta),
                              (gs[0] - 1, 1))
        print(f'cord_vec.shape:\t{cord_vec.shape}')
        coordinates[0, :, :] = cord_vec
        cord_vec = np.reshape(np.array(range(0, gs[1] - 1) * delta),
                              (gs[1] - 1, 1))
        coordinates[1, :, :] = np.flip(cord_vec.T)
        '''
        cord_vec = np.reshape(np.array(range(0, gs[0]-1)*delta), (gs[0]-1, 1))
        coordinates[0, :, :] = cord_vec.T
        coordinates[1, :, :] = np.flip(cord_vec)
        '''

        # print('Blocks abgeschnitten!')
        # blocks = blocks[:1]
        print(f'len(numbers):\t{len(numbers)}')
        print(f'2*np.prod(gs):\t{2*np.prod(gs)}')

        values = np.empty((n_vars - 2, gs[0] - 1, gs[1] - 1))
        for v in variables:
            # j = 0: concentration, j = 1: curvature
            j = variables.index(v)
            # Assign next 128*128 values to variable v
            if j >= 2:
                values[j-2, :, :] = np.reshape(numbers[
                    2*np.prod(gs) + (j-2)*(gs[0]-1)*(gs[1]-1) :\
                    2*np.prod(gs) + (j-2)*(gs[0]-1)*(gs[1]-1)+(gs[0]-1)*(gs[1]-1)
                ], (gs[0]-1, gs[1]-1))

        # Filtering & weighting
        # Initialize kernel
        kernel = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]])
        # kernel = kernel/np.sum(kernel)
        # mask = np.where(((values[0, :, :] <= 0.03) | (values[0, :, :] >= 0.97)) & (values[1, :, :] != 0), 1, 0)
        values[1, :, :] = np.where(
            (values[0, :, :] > 0.05) & (values[0, :, :] < 0.95),
            values[1, :, :], 0)
        # '''
        # Weighten cuvature
        # Get weights in every cell
        weights = (1 - 2 * np.abs(0.5 - values[0, :, :])) * np.where(
            values[1, :, :] != 0, 1, 0)
        # Get sum of weights by folding with kernel
        weight_array = ndimage.convolve(weights,
                                        kernel,
                                        mode='constant',
                                        cval=0.0)

        # Weighten curvature by convolving values*weights with kernel
        values[1, :, :] = np.where(
            (values[0, :, :] > 0.05) & (values[0, :, :] < 0.95),
            ndimage.convolve(
                values[1, :, :] * weights, kernel, mode='constant', cval=0.0) /
            weight_array, 0)

        # '''
        '''
        # Filter curvature and expand to 0.0075 - 0.9925
        # Get weights in every cell
        weights = np.where(values[1, :, :] != 0, 1, 0)
        # Get sum of weights by folding with kernel
        weight_array = ndimage.convolve(weights, kernel, mode='constant', cval=0.0)

        # Weighten curvature by convolving values*weights with kernel
        [lower, upper] = [0.0075, 0.9925]
        # [lower, upper] = [0.05, 0.95]
        values[1, :, :] = np.where((values[0, :, :] > lower) & (values[0, :, :] < upper),
                                   ndimage.convolve(values[1, :, :], kernel, mode='constant', cval=0.0)/weight_array,
                                   0)
        # '''

        if (0 == 1):  # no export
            fig, ax = plt.subplots()
            # ax.imshow(values[0, :, :], cmap='Greys_r')
            ax.imshow(values[1, :, :],
                      cmap='viridis',
                      norm=plt.Normalize(-30, 100))
            plt.show()
        else:
            # Make figure without border
            fig = plt.figure(frameon=False)
            fig.set_size_inches(10, 10)  # Square
            # fig.set_size_inches(5,10) # For rising bubble
            # fig.set_size_inches(10,5)
            ax = plt.Axes(fig, [0., 0., 1., 1.])
            ax.set_axis_off()
            fig.add_axes(ax)
            ''' # Artefakt
            values = np.rot90(values, k=3, axes=(1, 2)) 
            sqrsize = 45
            xlower = 17
            ylower = 68
            # '''
            ''' # Zebra
            values = np.rot90(values, k=1, axes=(1, 2)) 
            sqrsize = 45
            xlower = 10
            ylower = 42
            # '''
            ''' # Falsche Werte
            values = np.rot90(values, k=1, axes=(1, 2)) 
            sqrsize = 16
            xlower = 64-16
            ylower = 128-25
            limits = [[xlower, xlower+sqrsize], [ylower, ylower+sqrsize]] # x, y
            # limits = [[0, 80], [0, 160]] # x, y
            # ''' # Horn
            '''
            values = np.rot90(values, k=1, axes=(1, 2)) 
            sqrsize = 20
            xlower = 22
            ylower = 47
            limits = [[xlower, xlower+sqrsize], [ylower, ylower+int(sqrsize/2)]] # x, y
            # '''
            # values = np.rot90(values, k=1, axes=(1, 2))
            sqrsize = 128
            xlower = 0
            ylower = 0
            limits = [[xlower, xlower + sqrsize], [ylower,
                                                   ylower + sqrsize]]  # x, y
            # '''
            y, x = np.meshgrid(
                np.linspace(limits[1][0], limits[1][1],
                            limits[1][1] - limits[1][0]),
                np.linspace(limits[0][0], limits[0][1],
                            limits[0][1] - limits[0][0]))
            # For rising bubble
            # x = x.T
            # y = y.T
            # Krümmung oszillierende Blase (-30 - 100)
            # pcm = ax.pcolormesh(x, y, values[1, limits[0][0]:limits[0][1], limits[1][0]:limits[1][1]], cmap='RdBu', norm=colors.TwoSlopeNorm(vmin=-30, vcenter=0, vmax=100))
            # Krümmung oszillierende Blase (-30 - 100) verzerrt
            # pcm = ax.pcolormesh(x, y, values[1, limits[0][0]:limits[0][1], limits[1][0]:limits[1][1]], cmap='Blues', vmin=50, vmax=100)
            # Krümmung statische Blase (-0.3 - 1)
            # pcm = ax.pcolormesh(y, x, values[1, limits[0][0]:limits[0][1], limits[1][0]:limits[1][1]], cmap='RdBu', norm=colors.TwoSlopeNorm(vmin=-0.3, vcenter=0, vmax=1))

            # Konzentration verzerrte Skala
            # pcm = ax.pcolormesh(x, y, values[0, limits[1][0]:limits[1][1], limits[0][0]:limits[0][1]], cmap='Greys_r', norm=colors.TwoSlopeNorm(vmin=0, vcenter=0.1, vmax=1))
            # Konzentration lineare Skala
            # pcm = ax.pcolormesh(x, y, values[0, limits[0][0]:limits[0][1], limits[1][0]:limits[1][1]], cmap='Greys_r', norm=colors.TwoSlopeNorm(vmin=0, vcenter=0.5, vmax=1))
            pcm = ax.pcolormesh(y,
                                x,
                                weights[limits[1][0]:limits[1][1],
                                        limits[0][0]:limits[0][1]],
                                cmap='Greys_r',
                                norm=colors.TwoSlopeNorm(vmin=0,
                                                         vcenter=0.5,
                                                         vmax=1))
            # Konzentration stark verzerrt um 0.5
            # pcm = ax.pcolormesh(x, y, values[0, limits[0][0]:limits[0][1], limits[1][0]:limits[1][1]], cmap='Greys_r', norm=colors.TwoSlopeNorm(vmin=0.485, vcenter=0.5, vmax=0.505))

            # tkz.save('result2d.tex', axis_height='7cm', axis_width='7cm')
            # '''
            plt.savefig('result2d.eps')

            with open('result2d.eps', 'r') as myfile:
                data = myfile.read()
                data = re.sub(r'fill', 'gsave fill grestore stroke', data)

            with open('result2d.eps', 'w') as myfile:
                myfile.write(data)
Ejemplo n.º 3
0
        if run == "Joint":
            y = d['-log10(pvalue)'] - 0.3
            if name in joint_patch_coords:
                x, y = joint_patch_coords[name]
            else:
                x = d['Beta'] - 0.02 * len(name)
        elif run == "Population":
            y = d['-log10(pvalue)'] - 0.2
            if name in pop_patch_coords:
                x, y = pop_patch_coords[name]
            else:
                x = d['Beta'] - 0.02 * len(name)
        elif run == "Obese":
            y = d['-log10(pvalue)'] - 0.2
            if name == "IL_27":
                x, y = 0.43, 6.2
            elif y > 5:
                x = d['Beta'] - 0.025 * len(name)
            else:
                x = d['Beta'] + 0.05
        ax.text(x=x, y=y, s=name).set_backgroundcolor("#ffffff")
    # Patching: adding a synthetic colorbar to match existing hue_norm
    norm = plt.Normalize(*colorbar_limits)
    sm = plt.cm.ScalarMappable(cmap="vlag", norm=norm)
    sm.set_array([])
    ax.figure.colorbar(sm)
    ax.set_title(run)
    plt.show()
    # TODO: do in R - ask Sara or Evelina
    is_fdr_significant, adj_pv = fdrcorrection(results["pvalue"])
Ejemplo n.º 4
0
lane1_all_data = lane1_all_data.append(sorted_by_veh_id[sorted_by_veh_id[2] == '1'])


# plots
# cf +lc lane0 with speed  color:seismic_r
fig, axs = plt.subplots(1, 1, sharex=True, sharey=True)
for allVeh_0 in pd.unique(lane0_all_data[0]):
    veh_info = lane0_all_data.loc[lane0_all_data[0] == allVeh_0]
    veh_time = veh_info[1]
    veh_position_x = veh_info[3]
    veh_speed = veh_info[5]
    if len(veh_time) <= 1:
        continue
    points = np.array([veh_time, veh_position_x]).T.reshape(-1, 1, 2)
    segments = np.concatenate([points[:-1], points[1:]], axis=1)
    norm = plt.Normalize(0, 30)
    lc = LineCollection(segments, cmap='jet_r', norm=norm)
    lc.set_array(veh_speed)
    lc.set_linewidth(0.5)
    line = axs.add_collection(lc)
fig.colorbar(line, ax=axs)
axs.set_xlim(0, duration*1.1)
axs.set_ylim(100, 1000)
plt.ylabel('Position (m)')
plt.xlabel('Time (s)')
plt.title('Lane 0 trajectory plot - volume: ' + str(input_volume) +'/h/lane')
plt.savefig(str(input_volume) + 'CF_LC_Lane0_position_with_speed', bbox_inches='tight', pad_inches=0.1)
plt.close()


# cf +lc lane1 with speed
Ejemplo n.º 5
0
 _values = []
 for i in range(_shape.GetFeatureCount()):
     _feature = _shape.GetFeature(i)
     _nhru = _feature['id']
     #print _nhru
     _row = _HRU_outputs_on_date.loc[_HRU_outputs_on_date['nhru'] ==
                                     _nhru]
     try:
         _values.append(float(_row[plotting_variable].values))
     except:
         #_values.append(np.nan)
         continue
 _values = np.array(_values)
 # Floating colorbar
 #colors = cm.jet(plt.Normalize( min(_values), max(_values)) (_values) )
 colors = cm.jet(plt.Normalize(_min, _max)(_values))
 ax.cla()
 _polys = []
 for i in range(_shape.GetFeatureCount()):
     _feature = _shape.GetFeature(i)
     _geometry = _feature.geometry()
     _boundary = _geometry.GetBoundary()
     _boundary_points = np.array(_boundary.GetPoints())
     _x = _boundary_points[:, 0] / 1000.
     _y = _boundary_points[:, 1] / 1000.
     #plt.plot(_x, _y, 'k-')
     _polys.append(
         ax.fill(_x, _y, facecolor=colors[i], edgecolor='k')[0])
 ax.set_title(plotting_variable + ': ' + date)
 ax.set_xlabel('E [km]', fontsize=16)
 ax.set_ylabel('N [km]', fontsize=16)
Ejemplo n.º 6
0
def cluster_data(data):
    names = data.columns
    edge_model = covariance.GraphLassoCV()
    data = np.array(data)

    X = data.copy().T
    X /= X.std(axis=0)

    edge_model.fit(X)
    _, labels = cluster.affinity_propagation(edge_model.covariance_)
    n_labels = labels.max()

    for i in range(n_labels + 1):
        print('Cluster %i: %s' % ((i + 1), ', '.join(names[labels == i])))

    #Visualization
    node_position_model = manifold.LocallyLinearEmbedding(n_components=2,
                                                          eigen_solver='dense',
                                                          n_neighbors=6)
    embedding = node_position_model.fit_transform(X.T).T
    plt.figure(1, facecolor='w', figsize=(10, 8))
    plt.clf()
    ax = plt.axes([0., 0., 1., 1.])
    plt.axis('off')

    # Display a graph of the partial correlations
    partial_correlations = edge_model.precision_.copy()
    d = 1 / np.sqrt(np.diag(partial_correlations))
    partial_correlations *= d
    partial_correlations *= d[:, np.newaxis]
    non_zero = (np.abs(np.triu(partial_correlations, k=1)) > 0.02)

    # Plot the nodes using the coordinates of our embedding
    plt.scatter(embedding[0],
                embedding[1],
                s=100 * d**2,
                c=labels,
                cmap=plt.cm.spectral)

    # Plot the edges
    start_idx, end_idx = np.where(non_zero)
    #a sequence of (*line0*, *line1*, *line2*), where::
    #            linen = (x0, y0), (x1, y1), ... (xm, ym)
    segments = [[embedding[:, start], embedding[:, stop]]
                for start, stop in zip(start_idx, end_idx)]
    values = np.abs(partial_correlations[non_zero])
    lc = LineCollection(segments,
                        zorder=0,
                        cmap=plt.cm.hot_r,
                        norm=plt.Normalize(0, .7 * values.max()))
    lc.set_array(values)
    lc.set_linewidths(15 * values)
    ax.add_collection(lc)

    # Add a label to each node. The challenge here is that we want to
    # position the labels to avoid overlap with other labels
    for index, (name, label,
                (x, y)) in enumerate(zip(names, labels, embedding.T)):
        name = str(name).decode('utf-8').encode('utf-8')
        dx = x - embedding[0]
        dx[index] = 1
        dy = y - embedding[1]
        dy[index] = 1
        this_dx = dx[np.argmin(np.abs(dy))]
        this_dy = dy[np.argmin(np.abs(dx))]
        if this_dx > 0:
            horizontalalignment = 'left'
            x = x + .002
        else:
            horizontalalignment = 'right'
            x = x - .002
        if this_dy > 0:
            verticalalignment = 'bottom'
            y = y + .002
        else:
            verticalalignment = 'top'
            y = y - .002
        plt.text(x,
                 y,
                 name,
                 size=10,
                 horizontalalignment=horizontalalignment,
                 verticalalignment=verticalalignment,
                 bbox=dict(facecolor='w',
                           edgecolor=plt.cm.spectral(label / float(n_labels)),
                           alpha=.6))

    plt.xlim(
        embedding[0].min() - .15 * embedding[0].ptp(),
        embedding[0].max() + .10 * embedding[0].ptp(),
    )
    plt.ylim(embedding[1].min() - .03 * embedding[1].ptp(),
             embedding[1].max() + .03 * embedding[1].ptp())
    plt.show()
Ejemplo n.º 7
0
        start = [0, *(idx + 1)]
        end = [*(idx + 1), len(x)]
        for s, e in zip(start, end):
            ax.plot(x[s:e], y[s:e], transform=ccrs.PlateCarree(), **kwargs)
    for x, y in zip(xx.transpose(), yy.transpose()):
        idx = np.argwhere(np.diff(np.sign(x % 360 - 180))).flatten()
        x360 = x % 360
        idx = idx[(x360[idx] > 10) & (x360[idx] < 350)]
        start = [0, *(idx + 1)]
        end = [*(idx + 1), len(x)]
        for s, e in zip(start, end):
            ax.plot(x[s:e], y[s:e], transform=ccrs.PlateCarree(), **kwargs)


cmap = plt.cm.get_cmap('cividis')
norm = plt.Normalize(0, 3e-20)


def enhance_gridbox_edges(box_xy: np.array, res):
    temp_proj = pyproj.Proj(
        ccrs.Gnomonic(central_longitude=np.mean(box_xy[:, 0]),
                      central_latitude=np.mean(box_xy[:, 1])).proj4_init)
    new_x = []
    new_y = []
    for (seg_x0, seg_y0), (seg_x1, seg_y1) in zip(box_xy[:-1, :],
                                                  box_xy[1:, :]):
        seg_length = np.sqrt((seg_x1 - seg_x0)**2 + (seg_y1 - seg_y0)**2)
        gx, gy = temp_proj([seg_x0, seg_x1], [seg_y0, seg_y1])
        m = (gy[1] - gy[0]) / (gx[1] - gx[0])
        b = gy[0] - m * gx[0]
        interped_gx = np.linspace(gx[0], gx[1], res)
Ejemplo n.º 8
0
def get_colors(inp, colormap, vmin=None, vmax=None):
    norm = plt.Normalize(vmin, vmax)
    return colormap(norm(inp))
Ejemplo n.º 9
0
plt.savefig('Tesla_Nickel_Price.ps')

#Scatterplot for Tesla and NASDAQ Price and Volume, grouped in days
#Reference for color bar: https://stackoverflow.com/questions/46106912/one-colorbar-for-multiple-scatter-plots
#Reference for Z-values: https://stackoverflow.com/questions/64735131/multiple-scatter-plots-and-one-colorbar

plt.figure()
plt.style.use('seaborn')
fig, ax = plt.subplots()
z1 = merged_monday['Total Volume Tesla & NASDAQ']
z2 = merged_tuesday['Total Volume Tesla & NASDAQ']
z3 = merged_wednesday['Total Volume Tesla & NASDAQ']
z4 = merged_thursday['Total Volume Tesla & NASDAQ']
z5 = merged_friday['Total Volume Tesla & NASDAQ']
mini, maxi = 0, 2
norm = plt.Normalize(mini, maxi)
zs = np.concatenate([z1, z2, z3, z4, z5], axis=0)
min_, max_ = zs.min(), zs.max()

mon_scat = plt.scatter(merged_monday['Percentage Change NASDAQ'],
                       merged_monday['Percentage Change Tesla'],
                       c=z1,
                       cmap='viridis_r',
                       norm=norm,
                       marker='o',
                       label='Monday',
                       edgecolors='black')
plt.clim(min_, max_)
tue_scat = plt.scatter(merged_tuesday['Percentage Change NASDAQ'],
                       merged_tuesday['Percentage Change Tesla'],
                       c=z2,
Ejemplo n.º 10
0
gpdf = gpdf.to_crs(epsg=3857)

fig, ax = plt.subplots(1, figsize=(20, 12))

plot = gpdf.plot(column='util', cmap='YlOrRd', linewidth=0.5, ax=ax, alpha=1)

ax.set_title('Maricopa County Pedestrian & Bike Traffic',
             fontdict={
                 'fontsize': '18',
                 'fontweight': '3'
             })

ctx.add_basemap(plot, source=ctx.providers.Stamen.TonerLite)

sm = plt.cm.ScalarMappable(cmap='YlOrRd',
                           norm=plt.Normalize(vmin=minfreq, vmax=maxfreq))
sm._A = []
cbar = fig.colorbar(sm)

fig.savefig('result/network_usage.png', bbox_inches='tight')

########################################

# query = '''
#     SELECT
#         nodes.point,
#         MIN(COUNT(*), 200) AS util
#     FROM links
#     INNER JOIN output_events
#     USING(link_id)
#     INNER JOIN nodes
Ejemplo n.º 11
0
def PlottingResults(video,
                    tmpfolder,
                    Dataframe,
                    scorer,
                    cfg,
                    showfigures,
                    suffix='.png'):
    ''' Plots poses vs time; pose x vs pose y; histogram of differences and likelihoods.'''
    plt.figure(figsize=(8, 6))
    bodyparts2plot = cfg['bodyparts']
    pcutoff = cfg['pcutoff']
    colors = get_cmap(len(bodyparts2plot), name=cfg['colormap'])
    alphavalue = cfg['alphavalue']

    for bpindex, bp in enumerate(bodyparts2plot):
        Index = Dataframe[scorer][bp]['likelihood'].values > pcutoff
        plt.plot(Dataframe[scorer][bp]['x'].values[Index],
                 Dataframe[scorer][bp]['y'].values[Index],
                 '.',
                 color=colors(bpindex),
                 alpha=alphavalue)

    plt.gca().invert_yaxis()

    sm = plt.cm.ScalarMappable(cmap=plt.get_cmap(cfg['colormap']),
                               norm=plt.Normalize(vmin=0,
                                                  vmax=len(bodyparts2plot) -
                                                  1))
    sm._A = []
    cbar = plt.colorbar(sm, ticks=range(len(bodyparts2plot)))
    cbar.set_ticklabels(bodyparts2plot)
    plt.xlabel('X position in pixels')
    plt.ylabel('Y position in pixels')
    plt.savefig(os.path.join(tmpfolder, "trajectory" + suffix))
    plt.figure(figsize=(30, 10))
    Time = np.arange(np.size(Dataframe[scorer][bodyparts2plot[0]]['x'].values))

    for bpindex, bp in enumerate(bodyparts2plot):
        Index = Dataframe[scorer][bp]['likelihood'].values > pcutoff
        plt.plot(Time[Index],
                 Dataframe[scorer][bp]['x'].values[Index],
                 '--',
                 color=colors(bpindex),
                 alpha=alphavalue)
        plt.plot(Time[Index],
                 Dataframe[scorer][bp]['y'].values[Index],
                 '-',
                 color=colors(bpindex),
                 alpha=alphavalue)

    sm = plt.cm.ScalarMappable(cmap=plt.get_cmap(cfg['colormap']),
                               norm=plt.Normalize(vmin=0,
                                                  vmax=len(bodyparts2plot) -
                                                  1))
    sm._A = []
    cbar = plt.colorbar(sm, ticks=range(len(bodyparts2plot)))
    cbar.set_ticklabels(bodyparts2plot)
    plt.xlabel('Frame Index')
    plt.ylabel('X-(dashed) and Y- (solid) position in pixels')
    plt.savefig(os.path.join(tmpfolder, "plot" + suffix))

    plt.figure(figsize=(30, 10))
    for bpindex, bp in enumerate(bodyparts2plot):
        Index = Dataframe[scorer][bp]['likelihood'].values > pcutoff
        plt.plot(Time,
                 Dataframe[scorer][bp]['likelihood'].values,
                 '-',
                 color=colors(bpindex),
                 alpha=alphavalue)

    sm = plt.cm.ScalarMappable(cmap=plt.get_cmap(cfg['colormap']),
                               norm=plt.Normalize(vmin=0,
                                                  vmax=len(bodyparts2plot) -
                                                  1))
    sm._A = []
    cbar = plt.colorbar(sm, ticks=range(len(bodyparts2plot)))
    cbar.set_ticklabels(bodyparts2plot)
    plt.xlabel('Frame Index')
    plt.ylabel('Likelihood')

    plt.savefig(os.path.join(tmpfolder, "plot-likelihood" + suffix))

    plt.figure()
    bins = np.linspace(0, np.amax(Dataframe.max()), 100)

    for bpindex, bp in enumerate(bodyparts2plot):
        Index = Dataframe[scorer][bp]['likelihood'].values < pcutoff
        X = Dataframe[scorer][bp]['x'].values
        X[Index] = np.nan
        Histogram(X, colors(bpindex), bins)
        Y = Dataframe[scorer][bp]['x'].values
        Y[Index] = np.nan
        Histogram(Y, colors(bpindex), bins)

    sm = plt.cm.ScalarMappable(cmap=plt.get_cmap(cfg['colormap']),
                               norm=plt.Normalize(vmin=0,
                                                  vmax=len(bodyparts2plot) -
                                                  1))
    sm._A = []
    cbar = plt.colorbar(sm, ticks=range(len(bodyparts2plot)))
    cbar.set_ticklabels(bodyparts2plot)
    plt.ylabel('Count')
    plt.xlabel('DeltaX and DeltaY')
    plt.savefig(os.path.join(tmpfolder, "hist" + suffix))

    if showfigures != True:
        plt.close("all")
    else:
        plt.show()
Ejemplo n.º 12
0
def cov_from_segments(gene,
                      seg_counts,
                      edge_counts,
                      edge_idx,
                      ax,
                      sample_idx=None,
                      log=False,
                      cmap_seg=None,
                      cmap_edg=None,
                      xlim=None,
                      grid=False,
                      order='C'):
    """This function takes a gene and its corresponding segment and edge counts to
    produce a coverage overview plot."""

    if sample_idx is None:
        sample_idx = sp.arange(seg_counts.shape[1])

    norm = plt.Normalize(0, sample_idx.shape[0])

    if cmap_seg is None:
        cmap_seg = plt.get_cmap('jet')
    if cmap_edg is None:
        cmap_edg = plt.get_cmap('jet')

    ### iterate over samples
    for ii, i in enumerate(sample_idx):
        ### collect count information and add segment patches
        for j in range(gene.segmentgraph.segments.shape[1]):
            s = gene.segmentgraph.segments[:, j]
            if log:
                counts = sp.log10(seg_counts[j, i] + 1)
            else:
                counts = seg_counts[j, i]
            #ax.add_patch(patches.Rectangle((s[0], 0), s[1] - s[0], counts, fill=cmap_seg(norm(ii)),
            #             edgecolor='none', alpha=0.5))
            ax.plot(s, [counts, counts],
                    '-',
                    color=cmap_seg(norm(ii)),
                    linewidth=2)

        for j in range(edge_idx.shape[0]):
            [s, t] = sp.unravel_index(edge_idx[j],
                                      gene.segmentgraph.seg_edges.shape,
                                      order=order)
            if log:
                counts = sp.log10(edge_counts[j, i] + 1)
            else:
                counts = edge_counts[j, i]
            add_intron_patch2(ax,
                              gene.segmentgraph.segments[1, s],
                              gene.segmentgraph.segments[0, t],
                              counts,
                              color=cmap_edg(norm(ii)))

    if xlim is not None:
        ax.set_xlim(xlim)

    ### draw grid
    if grid:
        ax.grid(b=True,
                which='major',
                linestyle='--',
                linewidth=0.2,
                color='#222222')
        ax.xaxis.grid(False)

    ax.set_ylim([0, ax.get_ylim()[1]])
Ejemplo n.º 13
0
wfc3 = 0.13
nircam_short = 0.031
nircam_long = 0.063

# Convert to physical kiloparsecs
r_es = np.zeros(len(papers))
for (ind, r), z in zip(enumerate(r_es_arcs), zs):
    if r_es_type[ind] == "kpc":
        r_es[ind] = r
    else:
        r_es[ind] = r / cosmo.arcsec_per_kpc_proper(z).value
    if mags[ind] < 0:
        mags[ind] = M_to_m(mags[ind], cosmo, z)

cmap = mpl.cm.get_cmap("plasma")
norm = plt.Normalize(vmin=0, vmax=1)

labels = {
    "C16": "Calvi+2016",
    "K18": "Kawamata+2018",
    "MO18": "Morishita+2018",
    "B19": "Bridge+2019",
    "G11": "Grazian+2011",
    "G12": "Grazian+2012",
    # "O16": "Oesch+2016",
    # "S18": "Salmon+2018",
    # "H20": "Holwerda+2020",
    "H07": "Hathi+2008"
}
markers = {
    "G11": "s",
Ejemplo n.º 14
0
               [activations2d[:, 1][i]
                for i in vLD] + [activations2d[:, 1][i] for i in vLG],
               color=['black'] * len(montee) + ['red'] * len(ld) +
               ['sandybrown'] * len(vSD) + ['gold'] * len(vSG) +
               ['green'] * len(vLD) + ['violet'] * len(vLG))

    # k-means

    kmeans = KMeans(n_clusters=7)
    kmeans.fit(activations2d)
    y_kmeans = kmeans.predict(activations2d)
    #fig,ax = plt.subplots()
    cvals = range(4)
    colors = ['black', 'blue', 'red', 'violet']

    norm = plt.Normalize(min(cvals), max(cvals))
    tuples = list(zip(map(norm, cvals), colors))
    cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", tuples)
    ax.scatter(activations2d[:, 0],
               activations2d[:, 1],
               c=y_kmeans,
               s=50,
               cmap=cmap)

    for i in range(len(activationAalborg)):
        ax.annotate("A" + str(i),
                    (activations2d[:, 0][i], activations2d[:, 1][i]))
    for i in range(len(activationAalborg), len(activationETrack2)):
        ax.annotate("E" + str(i - len(activationAalborg)),
                    (activations2d[:, 0][i], activations2d[:, 1][i]))
# Read image and mask
image = skimage.io.imread(os.path.join(IMAGES_DEST, image_fns[idx]))
gt = skimage.io.imread(os.path.join(MASKS_DEST, gt_fns[idx]))

# Test!
assert image.shape[:2] == gt.shape, "Wrong image or ground truth!"
assert image.dtype == gt.dtype, "Wrong data types!"
print(image.shape, gt.shape)

# Flatten to get unique -> different classes in the mask
val1 = gt.flatten()
print("Ground truth classes: ", np.unique(val1))

f, axarr = plt.subplots(1, 2, figsize=(16, 16))

norm = plt.Normalize(0, 4)  # 5 classes including BG
map_name = matplotlib.colors.LinearSegmentedColormap.from_list(
    "", ["black", "red", "yellow", "blue", "green"])
#map_name = 'magma'

# idx = 154 all classes
axarr[0].imshow(image, cmap=map_name, norm=norm)
axarr[1].imshow(gt, cmap=map_name, norm=norm)

# In[ ]:

# If using this block of code, run the primary for loop for two or three folder, for experimentation.

#f, axarr = plt.subplots(1,2, figsize=(16,16))
#ct = 3
Ejemplo n.º 16
0
dataAfterStim = [
    np.transpose(np.multiply(np.transpose(connectivity[i, :, :]), myH[i, :]))
    for i in [
        int(tmax / dt),
        int(tmax / dt + ((thetmax - tmax) / dt) / 3.),
        int(tmax / dt + ((thetmax - tmax) / dt) * 2. / 3.), -1
    ]
]

ax2A2.matshow(dataAfterStim[2], vmin=0, vmax=vmax)
ax2A2.set_xticks([])
ax2A2.set_yticks([])
ax2A2.set_xlabel('After stimulation')
sm = plt.cm.ScalarMappable(cmap=plt.cm.jet,
                           norm=plt.Normalize(vmin=0., vmax=vmax))
# fake up the array of the scalar mappable. Urgh...
sm._A = []
cax = fig.add_axes([1., 0.307, 0.02, 0.239])  # [left, bottom, width, height]
myticks = [0.0, .5, 1., 1.5]
cbar = fig.colorbar(sm, cax=cax, ticks=myticks, alpha=1.)
cbar.ax.tick_params(labelsize=30)

axSA.set_prop_cycle(
    plt.cycler('color', [colormap(i) for i in np.linspace(0, 0.9, n)]))
axSA.plot(t_ret, phi(u_ret[:, :], theta, uc), lw=5)
axSA.set_ylim([0, 1.2])
axSA.set_xlim([0, 220])
axSA.set_xticks([0, 100, 200])
axSA.set_yticks([0.5, 1])
axSA.set_xlabel('Time (ms)')
Ejemplo n.º 17
0
    def plotManifoldDistances(self,
                              segments: List[Union[MessageSegment,
                                                   TypedSegment, RawMessage,
                                                   Any]],
                              distances: numpy.ndarray,
                              labels: numpy.ndarray,
                              templates: List = None,
                              plotEdges=False,
                              countMarkers=False):
        """
        Plot distances of segments according to (presumably multidimensional) features.
        This function abstracts from the actual feature by directly taking a precomputed similarity matrix and
        arranging the segments relative to each other according to their distances using Multidimensional Scaling (MDS).
        See module `manifold` from package `sklearn`.

        If segments is a list of `TypedSegment` or `MessageSegment`, this function plots the feature values of each
        given segment overlaying each other besides the distances; they are colored according to the given labels.

        >>> from netzob.Model.Vocabulary.Messages.RawMessage import RawMessage
        >>> from utils.loader import BaseLoader
        >>> from inference.analyzers import Value
        >>>
        >>> bytedata = [
        ...     bytes([1, 2, 3, 4]),
        ...     bytes([   2, 3, 4]),
        ...     bytes([   1, 3, 4]),
        ...     bytes([   2, 4   ]),
        ...     bytes([   2, 3   ]),
        ...     bytes([20, 30, 37, 50, 69, 2, 30]),
        ...     bytes([        37,  5, 69       ]),
        ...     bytes([70, 2, 3, 4]),
        ...     bytes([3, 2, 3, 4])
        ...     ]
        >>> messages  = [RawMessage(bd) for bd in bytedata]
        >>> specimens = BaseLoader(messages)
        >>> analyzers = [Value(message) for message in messages]
        >>> segments  = [TypedSegment(analyzer, 0, len(analyzer.message.data)) for analyzer in analyzers]
        >>> for seg in segments[:4]:
        ...     seg.fieldtype = "ft1"
        >>> for seg in segments[4:6]:
        ...     seg.fieldtype = "ft2"
        >>> for seg in segments[6:]:
        ...     seg.fieldtype = "ft3"
        >>> DistanceCalculator.debug = False
        >>> dc = DistanceCalculator(segments, thresholdFunction=DistanceCalculator.neutralThreshold, thresholdArgs=None)
        Calculated distances for 37 segment pairs in ... seconds.
        >>> dp = DistancesPlotter(specimens, "test", False)
        >>> dp.plotManifoldDistances(segments, dc.distanceMatrix, numpy.array([1,2,3,1,1,0,1,0,2]))
        >>> # comment out writing of file for doctest
        >>> # dp.writeOrShowFigure()

        :param segments: If `segments` is a list of `TypedSegment`s, field types are marked as small markers
            within the label marker. labels containing "Noise" then are not explicitly marked like the other labeled
            segments
        :param distances: The precomputed similarity matrix:
            symmetric matrix, rows/columns in the order of `segments`
        :param labels: Labels of strings (or ints or any other printable type) identifying the cluster for each segment
        :param templates: Templates of clusters to be printed alongside with the feature values.
            CURRENTLY UNTESTED
        :param plotEdges: Plot of edges between each pair of segment markers.
            Caution: Adds n^2 lines which takes very long compared to the scatterplot and
            quickly becomes a huge load especially when rendering the plot as PDF.
        :param countMarkers: add text labels with information at positions with multiple markers
        """
        from sklearn import manifold
        from sklearn.decomposition import PCA

        # plot configuration
        labsize = 150  # label markers: size factor
        typsize = 30  # type markers: size factor
        # self._cm          # label color map
        fcm = cm.cubehelix  # type color map

        # identify unique labels
        allabels = set(labels)
        if all(
                isinstance(l, numpy.integer) or l.isdigit() for l in allabels
                if l != "Noise"):
            ulab = sorted(allabels,
                          key=lambda l: -1 if l == "Noise" else int(l))
        else:
            ulab = sorted(allabels)

        # subsample if segment count is larger than maxSamples
        maxSamples = 1000
        originalSegmentCount = len(segments)
        if originalSegmentCount > 2 * maxSamples:
            import math
            ratiorev = originalSegmentCount / maxSamples
            step2keep = math.floor(ratiorev)
            lab2idx = dict()
            for idx, lab in enumerate(labels):
                if lab not in lab2idx:
                    lab2idx[lab] = list()
                lab2idx[lab].append(idx)
            # copy list to remove elements without side-effects
            segments = segments.copy()
            # to save the indices to be removed
            idx2rem = list()
            # determines a subset evenly distributed over all clusters while honoring the ratio to reduce to.
            for lab, ics in lab2idx.items():
                keep = set(ics[::step2keep])
                idx2rem.extend(set(ics) - keep)
            idx2rem = sorted(idx2rem, reverse=True)
            for idx in idx2rem:
                del segments[idx]
            labels = numpy.delete(labels, idx2rem, 0)
            distances = numpy.delete(numpy.delete(distances, idx2rem, 0),
                                     idx2rem, 1)
        else:
            idx2rem = None

        # prepare MDS
        seed = numpy.random.RandomState(seed=3)
        mds = manifold.MDS(n_components=2,
                           max_iter=3000,
                           eps=1e-9,
                           random_state=seed,
                           dissimilarity="precomputed",
                           n_jobs=1)
        pos = mds.fit(distances).embedding_
        # print(distances)

        # Rotate the data
        clf = PCA(n_components=2)

        pos = clf.fit_transform(pos)

        fig = self._fig
        axMDS, axSeg = self._axes  # type: plt.Axes, plt.Axes

        if idx2rem is not None:
            axSeg.text(
                0, -5,
                'Subsampled: {} of {} segments'.format(len(segments),
                                                       originalSegmentCount))

        # omit noise in cluster labels if types are plotted anyway.
        if isinstance(segments[0], TypedSegment):
            for l in ulab:
                if isinstance(l, str) and "Noise" in l:
                    ulab.remove(l)
        elif isinstance(segments[0],
                        RawMessage) and segments[0].messageType != "Raw":
            for l in ulab:
                try:
                    if int(l) == -1:
                        ulab.remove(l)
                except ValueError as e:
                    pass  # not a problem, just keep the cluster, since its not noise.

        # prepare color space
        cIdx = [
            int(round(each))
            for each in numpy.linspace(2, self._cm.N - 2, len(ulab))
        ]
        if templates is None:
            templates = ulab
        # iterate unique labels and scatter plot each of these clusters
        for c, (l,
                t) in enumerate(zip(ulab,
                                    templates)):  # type: int, (Any, Template)
            # test with:
            # color = [list(numpy.random.randint(0, 10, 4) / 10)]
            # plt.scatter(numpy.random.randint(0,10,4), numpy.random.randint(0,10,4), c=color)
            lColor = self._cm(cIdx[c])
            class_member_mask = (labels == l)
            try:
                x = list(compress(pos[:, 0].tolist(), class_member_mask))
                y = list(compress(pos[:, 1].tolist(), class_member_mask))
                # "If you want to specify the same RGB or RGBA value for all points, use a 2-D array with a single row."
                # see https://matplotlib.org/api/_as_gen/matplotlib.pyplot.scatter.html:
                axMDS.scatter(
                    x,
                    y,
                    c=colors.to_rgba_array(lColor),
                    alpha=.6,
                    s=labsize,
                    # s=s-(c*s/len(ulab)),  #
                    lw=0,
                    label=str(l))
            except IndexError as e:
                print(pos)
                print(distances)
                print(segments)
                raise e

            if isinstance(t, Template):
                axSeg.plot(t.values, c=lColor, linewidth=4)

        # include field type labels for TypedSegments input
        if isinstance(segments[0], (TypedSegment, RawMessage)):
            if isinstance(segments[0], TypedSegment):
                ftypes = numpy.array([seg.fieldtype for seg in segments])  # PP
            elif isinstance(segments[0],
                            RawMessage) and segments[0].messageType != 'Raw':
                ftypes = numpy.array([msg.messageType
                                      for msg in segments])  # PP
            else:
                ftypes = set()
            # identify unique types
            utyp = sorted(set(ftypes))
            # prepare color space
            # noinspection PyUnresolvedReferences
            cIdx = [
                int(round(each))
                for each in numpy.linspace(30, fcm.N - 30, len(utyp))
            ]
            # iterate unique types and scatter plot each of these groups
            for n, ft in enumerate(utyp):  # PP
                fColor = fcm(cIdx[n])
                type_member_mask = (ftypes == ft)
                x = list(compress(pos[:, 0].tolist(), type_member_mask))
                y = list(compress(pos[:, 1].tolist(), type_member_mask))
                # "If you want to specify the same RGB or RGBA value for all points, use a 2-D array with a single row."
                # see https://matplotlib.org/api/_as_gen/matplotlib.pyplot.scatter.html:
                axMDS.scatter(x,
                              y,
                              c=colors.to_rgba_array(fColor),
                              alpha=1,
                              s=typsize,
                              lw=0,
                              label=str(ft))

                if isinstance(segments[0], TypedSegment):
                    for seg in compress(segments, type_member_mask):
                        axSeg.plot(seg.values, c=fColor, alpha=0.05)
        elif isinstance(segments[0], MessageSegment):
            for c, l in enumerate(ulab):
                lColor = self._cm(cIdx[c])
                class_member_mask = (labels == l)
                for seg in compress(segments, class_member_mask):
                    axSeg.plot(seg.values, c=lColor, alpha=0.1)
        else:
            axSeg.text(.5,
                       .5,
                       'nothing to plot\n(message alignment)',
                       horizontalalignment='center')

        # place the label/type legend at the best position
        if isinstance(segments[0], RawMessage):
            axMDS.legend(bbox_to_anchor=(1.04, 1),
                         scatterpoints=1,
                         shadow=False)
            axSeg.patch.set_alpha(0.0)
            axSeg.axis('off')
        else:
            axMDS.legend(scatterpoints=1, loc='best', shadow=False)

        if plotEdges:
            # plotting of edges takes a long time compared to the scatterplot (and especially when rendering the PDF)
            from matplotlib.collections import LineCollection
            # Plot the edges
            lines = [[pos[i, :], pos[j, :]] for i in range(len(pos))
                     for j in range(len(pos))]
            values = numpy.abs(distances)
            # noinspection PyUnresolvedReferences
            lc = LineCollection(lines,
                                zorder=0,
                                cmap=plt.cm.Blues,
                                norm=plt.Normalize(0, values.max()))
            # lc.set_alpha(.1)
            lc.set_array(distances.flatten())
            lc.set_linewidths(0.5 * numpy.ones(len(segments)))
            axMDS.add_collection(lc)

        if countMarkers:
            # Count markers at identical positions and plot text with information about the markers at this position
            from collections import Counter
            import math
            if isinstance(segments[0], TypedSegment):
                coordCounter = Counter([
                    (posX, posY, seg.fieldtype)
                    for seg, lab, posX, posY in zip(
                        segments, labels, pos[:, 0].tolist(), pos[:,
                                                                  1].tolist())
                ])
            else:
                coordCounter = Counter([
                    (posX, posY, lab) for lab, posX, posY in zip(
                        labels, pos[:, 0].tolist(), pos[:, 1].tolist())
                ])
            for (posX, posY, lab), cnt in coordCounter.items():
                if cnt > 1:
                    theta = hash(str(lab)) % 360
                    r = 1
                    posXr = posX + r * math.cos(theta)
                    posYr = posY + r * math.sin(theta)
                    axMDS.text(posXr,
                               posYr,
                               "{}: {}".format(lab, cnt),
                               withdash=True)

        fig.canvas.toolbar.update()
Ejemplo n.º 18
0
        outer_x, outer_y = s.R * np.sin(s.alphaFromPhi), s.R * np.cos(
            s.alphaFromPhi) - s.F * (s.n2 - 1.) - s.S * (s.n1 - 1.)

        a, b = 93, 92
        inner_x, inner_y = inner_x[:a], inner_y[:a]
        outer_x, outer_y = outer_x[:b], outer_y[:b]

        plt.plot(inner_x, inner_y, c=c, lw=2, label=str(s.F) + " " + str(s.S))
        plt.plot(-inner_x, inner_y, c=c, lw=2)
        plt.plot(outer_x, outer_y, c=c, lw=2)
        plt.plot(-outer_x, outer_y, c=c, lw=2)


###################################################################################

norm = plt.Normalize()
colors = plt.cm.Greens(norm([1, 2, 3, 4, 5, 6]))

#l0 = lens(n2 = 2.8)
#l0.solveLens()
#l0.plot(c=colors[1])

#l1 = lens(n2 = 3.0)
#l1.solveLens()
#l1.plot(c=colors[2])

#l2 = lens(n2 = 3.2)
#l2.solveLens()
#l2.plot(c=colors[3])

#l3 = lens(n2 = 3.4)
Ejemplo n.º 19
0
def visualize(model,
              graphs,
              res_dir,
              data_name,
              class_values,
              num=5,
              sort_by='prediction'):
    model.eval()
    model.to(device)
    R = []
    Y = []
    graph_loader = DataLoader(graphs, 50, shuffle=False)
    for data in tqdm(graph_loader):
        data = data.to(device)
        r = model(data).detach()
        y = data.y
        R.extend(r.view(-1).tolist())
        Y.extend(y.view(-1).tolist())
    if sort_by == 'true':  # sort graphs by their true ratings
        order = np.argsort(Y).tolist()
    elif sort_by == 'prediction':
        order = np.argsort(R).tolist()
    elif sort_by == 'random':  # randomly select graphs to visualize
        order = np.random.permutation(range(len(R))).tolist()
    highest = [PyGGraph_to_nx(graphs[i]) for i in order[-num:][::-1]]
    lowest = [PyGGraph_to_nx(graphs[i]) for i in order[:num]]
    highest_scores = [R[i] for i in order[-num:][::-1]]
    lowest_scores = [R[i] for i in order[:num]]
    highest_ys = [Y[i] for i in order[-num:][::-1]]
    lowest_ys = [Y[i] for i in order[:num]]
    scores = highest_scores + lowest_scores
    ys = highest_ys + lowest_ys
    type_to_label = {0: 'u0', 1: 'v0', 2: 'u1', 3: 'v1', 4: 'u2', 5: 'v2'}
    type_to_color = {
        0: 'xkcd:red',
        1: 'xkcd:blue',
        2: 'xkcd:orange',
        3: 'xkcd:lightblue',
        4: 'y',
        5: 'g'
    }
    plt.axis('off')
    f = plt.figure(figsize=(20, 10))
    axs = f.subplots(2, num)
    cmap = plt.cm.get_cmap('rainbow')
    vmin, vmax = min(class_values), max(class_values)
    sm = plt.cm.ScalarMappable(cmap=cmap,
                               norm=plt.Normalize(vmin=vmin, vmax=vmax))
    sm.set_array([])
    for i, g in enumerate(highest + lowest):
        u_nodes = [x for x, y in g.nodes(data=True) if y['type'] % 2 == 0]
        u0, v0 = 0, len(u_nodes)
        pos = nx.drawing.layout.bipartite_layout(g, u_nodes)
        bottom_u_node = min(pos, key=lambda x: (pos[x][0], pos[x][1]))
        bottom_v_node = min(pos, key=lambda x: (-pos[x][0], pos[x][1]))
        # swap u0 and v0 with bottom nodes if they are not already
        if u0 != bottom_u_node:
            pos[u0], pos[bottom_u_node] = pos[bottom_u_node], pos[u0]
        if v0 != bottom_v_node:
            pos[v0], pos[bottom_v_node] = pos[bottom_v_node], pos[v0]
        labels = {
            x: type_to_label[y]
            for x, y in nx.get_node_attributes(g, 'type').items()
        }
        node_colors = [
            type_to_color[y]
            for x, y in nx.get_node_attributes(g, 'type').items()
        ]
        edge_types = nx.get_edge_attributes(g, 'type')
        edge_types = [class_values[edge_types[x]] for x in g.edges()]
        axs[i // num, i % num].axis('off')
        nx.draw_networkx(
            g,
            pos,
            #labels=labels,
            with_labels=False,
            node_size=150,
            node_color=node_colors,
            edge_color=edge_types,
            ax=axs[i // num, i % num],
            edge_cmap=cmap,
            edge_vmin=vmin,
            edge_vmax=vmax,
        )
        # make u0 v0 on top of other nodes
        nx.draw_networkx_nodes(g, {u0: pos[u0]},
                               nodelist=[u0],
                               node_size=150,
                               node_color='xkcd:red',
                               ax=axs[i // num, i % num])
        nx.draw_networkx_nodes(g, {v0: pos[v0]},
                               nodelist=[v0],
                               node_size=150,
                               node_color='xkcd:blue',
                               ax=axs[i // num, i % num])
        axs[i // num,
            i % num].set_title('{:.4f} ({:})'.format(scores[i], ys[i]),
                               x=0.5,
                               y=-0.05,
                               fontsize=20)
    f.subplots_adjust(right=0.85)
    cbar_ax = f.add_axes([0.88, 0.15, 0.02, 0.7])
    if len(class_values) > 20:
        class_values = np.linspace(min(class_values),
                                   max(class_values),
                                   20,
                                   dtype=int).tolist()
    cbar = plt.colorbar(sm, cax=cbar_ax, ticks=class_values)
    cbar.ax.tick_params(labelsize=22)
    f.savefig(os.path.join(
        res_dir, "visualization_{}_{}.pdf".format(data_name, sort_by)),
              interpolation='nearest',
              bbox_inches='tight')
Ejemplo n.º 20
0
def main():
    curve_score_methods = {
        "std":
        ("Standard deviation", (0.0, 1.0), lambda d, h, b: np.std(d)),  # 1.5,
        "mean": ("Mean", (0.0, 4.0), lambda d, h, b: np.mean(d)),
        "cv": (
            "Coefficient of variation",
            (0.3, 0.8),
            lambda d, h, b: scipy.stats.variation(d),
        ),
        # "skew": ("modern skew",
        #        0.0, 3.0,
        #        lambda d, h, b: scipy.stats.skew(d)),
        "skew_normed": (
            "Skew",
            (0.0, 2.9),
            lambda d, h, b: scipy.stats.skew(d, bias=False),
        ),
        # "mode": ("Mode",
        #        0.0, 3.5,
        #        lambda d, h, b: b[h.argmax()]),
        # "num": ("# cells",
        #        0.0, 2000,
        #        lambda d, h, b: len(d)),
        # "pearson_mode_mean": ("pearson Mode mean",
        #         0.0, 1.2,
        #         joy_plots_of_gradients.pearson_mode_mean_skew),
        # "non_parameteric_skew": ("Non parameteric",
        #         0.0, 0.4,
        #         joy_plots_of_gradients.non_parametric_skew),
        "kurtosis":
        ("Kurtosis", (0.0, 8.0), lambda d, h, b: scipy.stats.kurtosis(d)),
    }

    plot_colors = [  # "mean",
        # "std",
        "cv",
        # "skew",
        # "num",
        # "skew_normed",
        "skew_normed",  # same as pandas
        # "pearson_mode_mean",
        # "non_parameteric_skew",
        # "kurtosis",
    ]

    # basedir = "../../data/bio_film_data/63xdatasets"
    #this_dir = os.path.dirname(__file__)
    this_dir = "/media/nmurphy/BF_Data_Orange/"
    #basedir = os.path.join(this_dir, "../../datasets/LSM700_63x_sigb")
    basedir = os.path.join(this_dir, "datasets/LSM700_63x_sigb")
    # cell_df = pd.read_hdf(os.path.join(basedir, "edge_redo_lh1segment_data_bg_back_bleed.h5"), "cells")
    # cell_df = pd.read_hdf(os.path.join(basedir, "new_edge_bgsubv2_maxnorm_lh1segment.h5"), "cells")
    cell_df = pd.read_hdf(os.path.join(basedir, "single_cell_data.h5"),
                          "cells")
    # cell_df = pd.read_hdf(os.path.join(basedir, "edge_redo_lh1segment_data.h5"), "cells")
    # cell_df = pd.read_hdf(os.path.join(basedir, "lh1segment_bgsub_data.h5"), "cells")
    # cell_df = cell_df[cell_df["area"] > 140]
    cell_df = cell_df[cell_df["distance"] > 2]
    time = 48  # .0
    location = "center"
    file_df = filedb.get_filedb(os.path.join(basedir, "file_list.tsv"))
    strain_map, des_strain_map = strainmap.load()

    # cbar_mins = {0: 0.5, 1:0.0}

    percentile = 0  # 99#
    # green_chan = "meannorm_green"
    # red_chan = "meannorm_red"
    rmax = 6.5
    gmax = 6.5  # 0.4
    green_chan = "green_raw_bg_mean"
    red_chan = "red_raw_bg_mean"
    rmax = 50000
    gmax = 10000
    strains = [
        ("wt_sigar_sigby", red_chan, rmax, "WT\n P$_{sigA}$-RFP"),
        ("wt_sigar_sigby", green_chan, gmax, "WT\n P$_{\mathit{sigB}}$-YFP"),
        (
            "delru_sigar_sigby",
            green_chan,
            gmax,
            "Δ$\mathit{rsbRU}$\n P$_{\mathit{sigB}}$-YFP",
        ),
        (
            "delqp_sigar_sigby",
            green_chan,
            gmax,
            "Δ$\mathit{rsbQP}$\n P$_{\mathit{sigB}}$-YFP",
        ),
    ]
    # ("2xqp_sigar_sigby", green_chan,  gmax,  "2$\\times$rsbQP\n P$_{sigB}$-YFP")]

    fig, ax = plt.subplots(len(plot_colors), len(strains), sharey=True)
    for c, (strain, chan, max_val, name) in enumerate(strains):
        strain_num = des_strain_map[strain]
        distances, sbins, histograms, stats = joy_plots_of_gradients.get_strain_result(
            file_df,
            cell_df,
            time,
            location,
            strain_num,
            chan,
            max_val,
            percentile,
            curve_score_methods,
        )
        for r, k in enumerate(plot_colors):
            color = figure_util.strain_color[strain_num.upper()]
            ax[r, c], mv, leglist = joy_plots_of_gradients.plot_curves(
                ax[r, c], color, distances, sbins, histograms, stats, k)

            if c == len(strains) - 1:
                posn = ax[r, c].get_position()
                cbax = fig.add_axes([
                    posn.x0 + posn.width + 0.0005, posn.y0, 0.015, posn.height
                ])
                label = curve_score_methods[k][0]
                min_zval = curve_score_methods[k][1][0]
                max_zval = curve_score_methods[k][1][1]
                sm = plt.cm.ScalarMappable(
                    cmap=plt.get_cmap("viridis"),
                    norm=plt.Normalize(vmin=min_zval, vmax=max_zval),
                )
                sm._A = []
                _ = plt.colorbar(sm, cax=cbax)  # , fig=fig)
                cbax.set_ylabel(label, rotation=-90, labelpad=8)
                cbax.tick_params(direction="out")

            if r == 0:
                ax[r, c].set_title(name, fontsize=6)
                ax[r, c].get_xaxis().set_ticklabels([])

            ax[r, c].set_xlim(0, max_val)

    # this didnt return the right mode for some reason
    # leg = ax[0, -1].legend(leglist)

    leg = ax[0, -1].legend(leglist, ["Mode", "Mean"],
                           loc="lower left",
                           bbox_to_anchor=(0.84, 0.97))
    leg.set_zorder(400)
    for a in ax.flatten():
        a.tick_params(direction="out")
    ax[0, 0].annotate(
        "Distance from top of biofilm (μm)",
        xy=(0, 0),
        xytext=(0.02, 0.5),
        textcoords="figure fraction",
        # arrowprops=dict(facecolor='black', shrink=0.05),
        horizontalalignment="center",
        verticalalignment="center",
        fontsize="medium",
        color=mpl.rcParams["axes.labelcolor"],
        rotation=90,
    )
    ax[1, 2].annotate(
        "Normalized fluoresence",
        xy=(0, 0),
        xytext=(0.5, 0.04),
        textcoords="figure fraction",
        # arrowprops=dict(facecolor='black', shrink=0.05),
        horizontalalignment="center",
        verticalalignment="center",
        fontsize="medium",
        color=mpl.rcParams["axes.labelcolor"],
    )
    # for a in ax[:, 0].flatten():
    #     ticklabs = a.yaxis.get_ticklabels()
    #     ticklabs = a.get_yticks()#.tolist()
    #     ticklabs[-1] = ''

    letters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
    # letter_lab = (-0.13, 0.98)
    for a, l in zip(ax.flatten(), letters):
        a.annotate(
            l,
            xy=(0, 0),
            xytext=(-0.13, 0.95),
            textcoords="axes fraction",
            # arrowprops=dict(facecolor='black', shrink=0.05),
            horizontalalignment="center",
            verticalalignment="center",
            fontsize=figure_util.letter_font_size,
            color="black",
        )
    #    a.text(letter_lab[0], letter_lab[1], l, transform=a.transAxes, fontsize=8)

    filename = "sup_meta_histo"
    width, height = figure_util.get_figsize(figure_util.fig_width_medium_pt,
                                            wf=1.0,
                                            hf=0.6)
    fig.subplots_adjust(left=0.085,
                        right=0.89,
                        top=0.89,
                        bottom=0.13,
                        hspace=0.20,
                        wspace=0.25)
    fig.set_size_inches(width, height)  # common.cm2inch(width, height))
    figure_util.save_figures(fig, filename, ["png", "pdf"], this_dir)
Ejemplo n.º 21
0
def test_nonuniformimage_setnorm():
    ax = plt.gca()
    im = NonUniformImage(ax)
    im.set_norm(plt.Normalize())
Ejemplo n.º 22
0
plt.ylim(0, 1)
plt.xlabel('x')
plt.title('test')
line_ani = animation.FuncAnimation(fig1,
                                   update_line,
                                   25,
                                   fargs=(data, l),
                                   interval=50,
                                   blit=True)

# To save the animation, use the command: line_ani.save('lines.mp4')

fig2 = plt.figure()

x = np.arange(-9, 10)
y = np.arange(-9, 10).reshape(-1, 1)
base = np.hypot(x, y)
ims = []
for add in np.arange(15):
    ims.append((plt.pcolor(x, y, base + add, norm=plt.Normalize(0, 30)), ))

im_ani = animation.ArtistAnimation(fig2,
                                   ims,
                                   interval=50,
                                   repeat_delay=3000,
                                   blit=True)
# To save this second animation with some metadata, use the following command:
# im_ani.save('im.mp4', metadata={'artist':'Guido'})

plt.show()
Ejemplo n.º 23
0
ax = plt.gca()
ax.axis('equal')
plt.plot(coordinates_center[0], coordinates_center[1], '--k', lw=0.5, alpha=0.5)
plt.plot(coordinates_left[0], coordinates_left[1], 'k', lw=0.5, alpha=0.5)
plt.plot(coordinates_right[0], coordinates_right[1], 'k', lw=0.5, alpha=0.5)

# best trajectory
#plt.plot(wx_nei[:-1], wy_nei[:-1], linestyle='', marker='D', ms=5)
for i in range(no_of_trajs):
	trajectory = np.loadtxt("coordinates_nc{}.txt".format(i+1), delimiter=',')
	x = np.array(trajectory[0])
	y = np.array(trajectory[1])
	speed = np.array(trajectory[2])
	points = np.array([x, y]).T.reshape(-1, 1, 2)
	segments = np.concatenate([points[:-1], points[1:]], axis=1)
	norm = plt.Normalize(speed.min(), speed.max())
	lc = LineCollection(segments, cmap='viridis', norm=norm)
	lc.set_array(speed)
	lc.set_linewidth(line_width)
	line = ax.add_collection(lc)
	fig.colorbar(line, ax=ax)
ax.set_xlabel('x [m]')
ax.set_ylabel('y [m]')

if SAVE_RESULTS:
	plt.savefig(filepath, dpi=600, bbox_inches='tight')

#####################################################################

plt.show()
Ejemplo n.º 24
0
def _embedding_delay_plot(signal,
                          metric_values,
                          tau_sequence,
                          tau=1,
                          metric="Mutual Information",
                          ax0=None,
                          ax1=None,
                          plot='2D'):
    """
    """
    # Prepare figure
    if ax0 is None and ax1 is None:
        fig = plt.figure(constrained_layout=False)
        spec = matplotlib.gridspec.GridSpec(ncols=1,
                                            nrows=2,
                                            height_ratios=[1, 3],
                                            width_ratios=[2])
        ax0 = fig.add_subplot(spec[0])
        if plot == '2D':
            ax1 = fig.add_subplot(spec[1])
        elif plot == '3D':
            ax1 = fig.add_subplot(spec[1], projection='3d')
    else:
        fig = None

    ax0.set_title("Optimization of Delay (tau)")
    ax0.set_xlabel("Time Delay (tau)")
    ax0.set_ylabel(metric)
    ax0.plot(tau_sequence, metric_values, color='#FFC107')
    ax0.axvline(x=tau, color='#E91E63', label='Optimal delay: ' + str(tau))
    ax0.legend(loc='upper right')
    ax1.set_title("Attractor")
    ax1.set_xlabel("Signal [i]")
    ax1.set_ylabel("Signal [i-" + str(tau) + "]")

    # Get data points, set axis limits
    embedded = complexity_embedding(signal, delay=tau, dimension=3)
    x = embedded[:, 0]
    y = embedded[:, 1]
    z = embedded[:, 2]
    ax1.set_xlim(x.min(), x.max())
    ax1.set_ylim(x.min(), x.max())

    # Colors
    norm = plt.Normalize(z.min(), z.max())
    cmap = plt.get_cmap('plasma')
    colors = cmap(norm(x))

    # Attractor for 2D vs 3D
    if plot == '2D':
        points = np.array([x, y]).T.reshape(-1, 1, 2)
        segments = np.concatenate([points[:-1], points[1:]], axis=1)
        lc = matplotlib.collections.LineCollection(segments,
                                                   cmap='plasma',
                                                   norm=norm)
        lc.set_array(z)
        line = ax1.add_collection(lc)

    elif plot == '3D':
        points = np.array([x, y, z]).T.reshape(-1, 1, 3)
        segments = np.concatenate([points[:-1], points[1:]], axis=1)
        for i in range(len(x) - 1):
            seg = segments[i]
            l, = ax1.plot(seg[:, 0], seg[:, 1], seg[:, 2], color=colors[i])
            l.set_solid_capstyle('round')
        ax1.set_zlabel("Signal [i-" + str(2 * tau) + "]")

    return fig
Ejemplo n.º 25
0
def plot_network(wn,
                 node_attribute=None,
                 link_attribute=None,
                 title=None,
                 node_size=20,
                 node_range=[None, None],
                 node_alpha=1,
                 node_cmap=None,
                 node_labels=False,
                 link_width=1,
                 link_range=[None, None],
                 link_alpha=1,
                 link_cmap=None,
                 link_labels=False,
                 add_colorbar=True,
                 node_colorbar_label='Node',
                 link_colorbar_label='Link',
                 directed=False,
                 ax=None,
                 filename=None):
    """
    Plot network graphic
	
    Parameters
    ----------
    wn : wntr WaterNetworkModel
        A WaterNetworkModel object
		
    node_attribute : None, str, list, pd.Series, or dict, optional
	
        - If node_attribute is a string, then a node attribute dictionary is
          created using node_attribute = wn.query_node_attribute(str)
        - If node_attribute is a list, then each node in the list is given a 
          value of 1.
        - If node_attribute is a pd.Series, then it should be in the format
          {nodeid: x} where nodeid is a string and x is a float. 
        - If node_attribute is a dict, then it should be in the format
          {nodeid: x} where nodeid is a string and x is a float
    
	link_attribute : None, str, list, pd.Series, or dict, optional
	
        - If link_attribute is a string, then a link attribute dictionary is
          created using edge_attribute = wn.query_link_attribute(str)
        - If link_attribute is a list, then each link in the list is given a 
          value of 1.
        - If link_attribute is a pd.Series, then it should be in the format
          {linkid: x} where linkid is a string and x is a float. 
        - If link_attribute is a dict, then it should be in the format
          {linkid: x} where linkid is a string and x is a float.
		  
    title: str, optional
        Plot title 

    node_size: int, optional
        Node size 

    node_range: list, optional
        Node range ([None,None] indicates autoscale)
        
    node_alpha: int, optional
        Node transparency
        
    node_cmap: matplotlib.pyplot.cm colormap or list of named colors, optional
        Node colormap 
        
    node_labels: bool, optional
        If True, the graph will include each node labelled with its name. 
        
    link_width: int, optional
        Link width
		
    link_range : list, optional
        Link range ([None,None] indicates autoscale)
		
    link_alpha : int, optional
        Link transparency
    
    link_cmap: matplotlib.pyplot.cm colormap or list of named colors, optional
        Link colormap
        
    link_labels: bool, optional
        If True, the graph will include each link labelled with its name.
        
    add_colorbar: bool, optional
        Add colorbar

    node_colorbar_label: str, optional
        Node colorbar label
        
    link_colorbar_label: str, optional
        Link colorbar label
        
    directed: bool, optional
        If True, plot the directed graph
    
    ax: matplotlib axes object, optional
        Axes for plotting (None indicates that a new figure with a single 
        axes will be used)
    
    filename : str, optional
        Filename used to save the figure
        
    Returns
    -------
    ax : matplotlib axes object  
    """

    if ax is None:  # create a new figure
        plt.figure(facecolor='w', edgecolor='k')
        ax = plt.gca()

    # Graph
    G = wn.get_graph()
    if not directed:
        G = G.to_undirected()

    # Position
    pos = nx.get_node_attributes(G, 'pos')
    if len(pos) == 0:
        pos = None

    # Define node properties
    add_node_colorbar = add_colorbar
    if node_attribute is not None:

        if isinstance(node_attribute, list):
            if node_cmap is None:
                node_cmap = ['red', 'red']
            add_node_colorbar = False

        if node_cmap is None:
            node_cmap = plt.cm.Spectral_r
        elif isinstance(node_cmap, list):
            if len(node_cmap) == 1:
                node_cmap = node_cmap * 2
            node_cmap = custom_colormap(len(node_cmap), node_cmap)

        node_attribute = _format_node_attribute(node_attribute, wn)
        nodelist, nodecolor = zip(*node_attribute.items())

    else:
        nodelist = None
        nodecolor = 'k'

    add_link_colorbar = add_colorbar
    if link_attribute is not None:

        if isinstance(link_attribute, list):
            if link_cmap is None:
                link_cmap = ['red', 'red']
            add_link_colorbar = False

        if link_cmap is None:
            link_cmap = plt.cm.Spectral_r
        elif isinstance(link_cmap, list):
            if len(link_cmap) == 1:
                link_cmap = link_cmap * 2
            link_cmap = custom_colormap(len(link_cmap), link_cmap)

        link_attribute = _format_link_attribute(link_attribute, wn)

        # Replace link_attribute dictionary defined as
        # {link_name: attr} with {(start_node, end_node, link_name): attr}
        attr = {}
        for link_name, value in link_attribute.items():
            link = wn.get_link(link_name)
            attr[(link.start_node_name, link.end_node_name, link_name)] = value
        link_attribute = attr

        linklist, linkcolor = zip(*link_attribute.items())
    else:
        linklist = None
        linkcolor = 'k'

    if title is not None:
        ax.set_title(title)

    edge_background = nx.draw_networkx_edges(G,
                                             pos,
                                             edge_color='grey',
                                             width=0.5,
                                             ax=ax)

    nodes = nx.draw_networkx_nodes(G,
                                   pos,
                                   nodelist=nodelist,
                                   node_color=nodecolor,
                                   node_size=node_size,
                                   alpha=node_alpha,
                                   cmap=node_cmap,
                                   vmin=node_range[0],
                                   vmax=node_range[1],
                                   linewidths=0,
                                   ax=ax)
    edges = nx.draw_networkx_edges(G,
                                   pos,
                                   edgelist=linklist,
                                   edge_color=linkcolor,
                                   width=link_width,
                                   alpha=link_alpha,
                                   edge_cmap=link_cmap,
                                   edge_vmin=link_range[0],
                                   edge_vmax=link_range[1],
                                   ax=ax)
    if node_labels:
        labels = dict(zip(wn.node_name_list, wn.node_name_list))
        nx.draw_networkx_labels(G, pos, labels, font_size=7, ax=ax)
    if link_labels:
        labels = {}
        for link_name in wn.link_name_list:
            link = wn.get_link(link_name)
            labels[(link.start_node_name, link.end_node_name)] = link_name
        nx.draw_networkx_edge_labels(G, pos, labels, font_size=7, ax=ax)
    if add_node_colorbar and node_attribute:
        clb = plt.colorbar(nodes, shrink=0.5, pad=0, ax=ax)
        clb.ax.set_title(node_colorbar_label, fontsize=10)
    if add_link_colorbar and link_attribute:
        if directed:
            vmin = min(map(abs, link_attribute.values()))
            vmax = max(map(abs, link_attribute.values()))
            sm = plt.cm.ScalarMappable(cmap=link_cmap,
                                       norm=plt.Normalize(vmin=vmin,
                                                          vmax=vmax))
            sm.set_array([])
            clb = plt.colorbar(sm, shrink=0.5, pad=0.05, ax=ax)
        else:
            clb = plt.colorbar(edges, shrink=0.5, pad=0.05, ax=ax)
        clb.ax.set_title(link_colorbar_label, fontsize=10)

    ax.axis('off')

    if filename:
        plt.savefig(filename)

    return ax
Ejemplo n.º 26
0
    ['Model Response', 'Measured Data', 'Epoched Data', 'Filter Response'])
fig.axes[0].set_ylabel('Filter Magnitude (dB) [invalid for other lines]')
fig.axes[0].set_title('')

###############################################################################
# Understanding the relation between stimulus presentation and response
# ---------------------------------------------------------------------
#
# Here we look at the effect of the interstimulus interval on the
# expected haemodynamic response. We choose a few different
# maximum and minimum
# values for the ISI. Two repeats are plotted per
# ISI to illustrate the random selection.
# Some common high pass filter values from literature are shown in red.

sm = plt.cm.ScalarMappable(cmap='viridis', norm=plt.Normalize(vmin=0, vmax=60))
fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(20, 10))
for rep in range(2):
    for column, min_isi in enumerate([0, 15]):
        for row, max_isi in enumerate([15, 30, 45, 60]):
            if max_isi >= min_isi:
                if max_isi >= min_isi:
                    raw = simulate_nirs_raw(sfreq=4.,
                                            sig_dur=60 * 60,
                                            amplitude=1.,
                                            stim_dur=5.,
                                            isi_min=min_isi,
                                            isi_max=max_isi)
                    raw._data[0] = raw._data[0] - np.mean(raw._data[0])
                    raw.pick(picks='hbo').plot_psd(average=True,
                                                   fmax=2,
Ejemplo n.º 27
0
def map_show(combined_df, detail_level, data_option, display_value,
             path_object):
    color_min = 0
    color_max = 200000

    print("Putting lipstick on the pig...")
    # TODO: extract magic numbers
    fig, ax = plt.subplots(1, figsize=(6, 2))
    ax.axis('off')
    map_title = "covid " + data_option + " by " + detail_level
    ax.set_title(map_title.title(),
                 fontdict={
                     'fontsize': '25',
                     'fontweight': '3'
                 })
    ax.annotate("Source: Harvard ",
                xy=(0.1, .08),
                xycoords='figure fraction',
                horizontalalignment='left',
                verticalalignment='top',
                fontsize=12,
                color='#555555')

    # Create colorbar as a legend
    # TODO: add second colorbar?
    sm = plt.cm.ScalarMappable(cmap='Greys',
                               norm=plt.Normalize(vmin=color_min,
                                                  vmax=color_max))

    # empty array for the data range
    sm._A = []
    # add the colorbar to the figure
    # TODO: pycharm tells me cbar is not used - what did I do? Is this not needed anymore?
    cbar = fig.colorbar(sm)

    # create map
    # TODO: extract magic numbers
    # ax=ax? that can't be right. rename?

    fig.tight_layout()
    # us_50_states_df.plot
    #    combined_df.plot(cmap='Greys', linewidth=0.8, ax=ax, edgecolor='0.8')
    print("Painting base map...")
    combined_df.plot(cmap='Greys', linewidth=0.8, ax=ax)

    # TODO: splitting by election results with county data seems to be a problem
    #  process stopped by operating system for memory issues several times
    #  after adding 'del' statements to clean some memory, pycharm crashed
    #  is there some way to make this part work, or just need a better computer?
    print("Making it political...")
    red_states_df = combined_df[combined_df.PARTY.eq('republican')]
    blue_states_df = combined_df[combined_df.PARTY.eq('democrat')]

    print("Freeing memory: combined_df")
    del combined_df

    print("Painting by election results...")
    red_states_df.plot(display_value, ax=ax, cmap='Reds')
    blue_states_df.plot(display_value, ax=ax, cmap='Blues')

    fig.tight_layout()

    # TODO: save map.png to an 'images' directory
    print("Saving map to file: ")
    map_name = display_value + " " + detail_level + " covid " + data_option + " choropleth.png"
    # Can't have / in filenames.
    map_name = map_name.replace(" ", "_")
    map_name = map_name.replace("/", "-")
    map_path = os.path.join(path_object.image_path, map_name)
    fig.savefig(map_path, dpi=300)
    print(map_name)
    print("Displaying the finished product...")
    plt.show()
    # Matplotlib gives you lots of freedom in how you save figures.
    # The code below will save the figure as a png,
    # but if you want to fiddle about some more with it in Illustrator you can also save as svg.
    # If you save as png, make sure to use a dpi of 200 or above.
    # Otherwise the map and text will look all blurry. Nobody wants that.
    # fig.savefig("map_export.png", dpi=300)

    return
Ejemplo n.º 28
0
def plot_tree(ax, tree, cm='cividis', median=None, ignore=None):
    def maybe_expand(x):
        a = np.asanyarray(x)
        return a if len(a.shape) else a[np.newaxis]

    tree = tree.map(maybe_expand)

    min_v, max_v, depth = tree.min(), tree.max(), tree.depth()
    extra_ticks = [min_v, max_v]
    min_v, max_v = get_value_range(median, min_v, max_v)

    # Normalize tree values to [0, 1]
    rng = max_v - min_v
    # if rng one of 0, +inf, -inf, or nan
    if rng == 0 or rng == float('inf') or rng == float('-inf') or rng != rng:
        rng = float('nan')
    tree = tree.map(lambda xs: [(x - min_v) / rng for x in xs])

    acc = []
    acc.append([0.5, 0, 1, 0, 2 * math.pi,
                0])  # makes sure 2pi = one revolution
    draw_tree(acc,
              tree,
              1,
              range_theta[0],
              range_theta[1],
              depth,
              False,
              False,
              ignore=ignore)
    acc = sorted(acc, key=lambda x: x[-1])  # sort by depth

    df = pd.DataFrame(acc,
                      columns=['v', 'r', 'r0', 'theta', 'dtheta', 'depth'])

    cm = plt.cm.get_cmap(cm) if isinstance(cm, str) else cm
    plot = ax.bar(df['theta'],
                  df['r'],
                  width=df['dtheta'],
                  bottom=df['r0'],
                  color=cm(df['v']),
                  align='edge')
    sm = plt.cm.ScalarMappable(cmap=cm, norm=plt.Normalize(min_v, max_v))
    sm.set_array(df['v'])
    if display_colorbar:
        cbar = plt.colorbar(sm,
                            ax=ax,
                            shrink=0.8,
                            orientation='horizontal',
                            pad=0.0)
        cbar.ax.tick_params(labelsize='small')
        cbar.ax.ticklabel_format(style='sci', axis='x', scilimits=(-3, 3))
    else:
        cbar = None
    ax.set_thetamin(range_theta[0] / 2 / math.pi * 360)
    ax.set_thetamax(range_theta[1] / 2 / math.pi * 360)
    ax.set_thetagrids([])
    ax.set_rgrids([])
    ax.grid(False)
    ax.set_axis_off()

    return plot, cbar
Ejemplo n.º 29
0
non_zero = (np.abs(np.triu(partial_correlations, k=1)) > 0.02)

# Plot the nodes using the coordinates of our embedding
plt.scatter(embedding[0], embedding[1], s=100 * d ** 2, c=labels,
            cmap=plt.cm.spectral)

# Plot the edges
start_idx, end_idx = np.where(non_zero)
#a sequence of (*line0*, *line1*, *line2*), where::
#            linen = (x0, y0), (x1, y1), ... (xm, ym)
segments = [[embedding[:, start], embedding[:, stop]]
            for start, stop in zip(start_idx, end_idx)]
values = np.abs(partial_correlations[non_zero])
lc = LineCollection(segments,
                    zorder=0, cmap=plt.cm.hot_r,
                    norm=plt.Normalize(0, .7 * values.max()))
lc.set_array(values)
lc.set_linewidths(15 * values)
ax.add_collection(lc)

# Add a label to each node. The challenge here is that we want to
# position the labels to avoid overlap with other labels
for index, (name, label, (x, y)) in enumerate(
        zip(names, labels, embedding.T)):

    dx = x - embedding[0]
    dx[index] = 1
    dy = y - embedding[1]
    dy[index] = 1
    this_dx = dx[np.argmin(np.abs(dy))]
    this_dy = dy[np.argmin(np.abs(dx))]
Ejemplo n.º 30
0
## Plot PFD average / cell and data loss / cell
if plot_sky_grid:
    plt.close()
    fig = plt.figure(figsize=(12, 4))
    val = pfd_avg.to_value(cnv.dB_W_m2)
    vmin, vmax = val.min(), val.max()
    val_norm = (val - vmin) / (vmax - vmin)
    plt.bar(
        grid_info['cell_lon_low'],
        height=grid_info['cell_lat_high'] - grid_info['cell_lat_low'],
        width=grid_info['cell_lon_high'] - grid_info['cell_lon_low'],
        bottom=grid_info['cell_lat_low'],
        color=plt.cm.viridis(val_norm),
        align='edge',
        )
    sm = plt.cm.ScalarMappable(cmap=plt.cm.viridis, norm=plt.Normalize(vmin=vmin, vmax=vmax))
    cbar = plt.colorbar(sm)
    cbar.set_label('PFD average / cell [dB(W/m2)]')
    plt.title('EPFD {:s} constellation: total data loss: {:.2f}'.format(
        constellation_name, data_loss
        ))
    plt.xlabel('Azimuth [deg]')
    plt.ylabel('Elevation [deg]')
    plt.xlim((0, 360))
    plt.ylim((0, 90))
    plt.savefig(
        pjoin(FIGPATH, '{:s}_skygrid_avg_pfd_horizontal.png'.format(fig_basename)),
        bbox_inches='tight', dpi=100,
        )
    plt.savefig(
        pjoin(FIGPATH, '{:s}_skygrid_avg_pfd_horizontal.pdf'.format(fig_basename)),