err_H_eff8,
                            ls='--',
                            color='green',
                            label='CE')
            if l == 1:  ## M=40
                axs[0].scatter(chi_val, err_H_eff5, marker='^', color='green')
                axs[0].plot(chi_val, err_H_eff5, ls='--', color='green')
                axs[1].scatter(chi_val, err_H_eff8, marker='^', color='green')
                axs[1].plot(chi_val, err_H_eff8, ls='--', color='green')
            if l == 2:  ## M=70
                axs[0].scatter(chi_val, err_H_eff5, marker='^', color='blue')
                axs[0].plot(chi_val, err_H_eff5, ls='--', color='blue')
                axs[1].scatter(chi_val, err_H_eff8, marker='^', color='blue')
                axs[1].plot(chi_val, err_H_eff8, ls='--', color='blue')

red = mpatches.Patch(color='red', label='M={}'.format(int(M[0])))
#green=mpatches.Patch(color='green', label='M={}'.format(int(M[1])))
#blue=mpatches.Patch(color='blue', label='M={}'.format(int(M[2])))

axs[0].set(xlabel='$\\chi_1 = \\chi_2$', ylabel='$\\Delta H_{eff5}$')
#axs[0].ticklabel_format(style='sci', axis='y', scilimits=(0,0))
#axs[1].ticklabel_format(style='sci', axis='y', scilimits=(0,0))
axs[1].set(xlabel='$\\chi_1 = \\chi_2$', ylabel='$\\Delta H_{eff8}$')
for m in [0, 1]:
    axs[m].grid(color="lightgrey", ls="--")
    axs[m].set_axisbelow(True)

for ax in axs.flat:
    ax.xaxis.label.set_fontsize(12)
    ax.yaxis.label.set_fontsize(12)
Пример #2
0
        a2 = torch.from_numpy(a2)

        Z3 = numpy.dot(a2, W_Hidden_Layer)  # R 1*10 = R 1*31 R 31*10
        Z3 = torch.from_numpy(Z3)

        # print("predicted %f label %f" % (numpy.argmax(Z3), numpy.argmax(label)))
        if (numpy.argmax(Z3) == numpy.argmax(label)):
            accurracy += 1

    y[1].append(accurracy / var.N_IMAGES_TEST * 100)
    print("Valeurs bien predit: %d " % (accurracy))
    print("Valeurs mal predit:  %d " % (var.N_IMAGES_TEST))
    print("Taux de reussite:    %f" % (accurracy / var.N_IMAGES_TEST * 100))
    print("Taux d'erreur:       %f" % (100 -
                                       (accurracy / var.N_IMAGES_TEST * 100)))
    #

fig0 = plt.figure()
ax0 = fig0.add_subplot(111)
ax0.grid(True)
gridlines = ax0.get_xgridlines() + ax0.get_ygridlines()
plt.yscale('linear')
plt.xscale('linear')
for line in gridlines:
    line.set_linestyle('-.')
plt.plot(y[0], y[1], 'bs', y[0], y[1])
plt.ylabel('Accuracy')
plt.xlabel('Neurones')
blue_patch = mpatches.Patch(color='blue', label='Accuracy')
plt.legend(handles=[blue_patch])
plt.show()
    else:
        snps.loc[i, 'ref_aa'] = get_aa(get_complement(ref_codon))
        snps.loc[i, 'mod_aa'] = get_aa(get_complement(ref_codon))

        
    # Annotate Synonymous & Non-synomymous mutations
    if snps.loc[i, 'ref_aa'] == snps.loc[i, 'mod_aa']:
        snps.loc[i, 'kind'] = 'Synonymous'
    else:
        snps.loc[i, 'kind'] = 'Non-synonymous'

        
# Populate kind column with information for Intergenic SNPs
snps['kind'].fillna('Intergenic', inplace=True)


#Plot bar chart. 
x=[1,2,3,4,5,6,7,8,9]
y=snps[['Contig','kind','Position']].groupby(['Contig','kind']).count().values
one = mpatches.Patch(color = "green", label = "1")
two = mpatches.Patch(color = "red", label = "2")
three = mpatches.Patch(color = "blue", label = "3")

plt.bar(x, y, color = ['green','red','blue','green','red','blue','green','red','blue'], edgecolor="black")
plt.xticks([2,5,8], ['Non-synonymous', 'Synonymous', 'Intergenic'], fontsize = 7)
plt.yticks(fontsize = 7)
plt.ylabel("Number of SNPs", fontname = "Arial", fontsize = 7)
plt.title("Mutation profile", fontname = "Arial", fontsize = 7)
plt.legend(handles = [one, two, three], title = "Contig", fontsize='xx-small')
plt.savefig("outputplot", format ='pdf')
Пример #4
0
    def render_objects(self, dispatcher, frame_id):
        '''
        Render the scene into a figure
        '''
        dynamics = dispatcher.get_dynamics()
        conditions = dispatcher.get_conditions()
        kinematics = dispatcher.get_kinematics()
        # Reset figure and create subplot
        self.fig.clear()
        self.ax = self.fig.add_subplot(111)
        #self.ax.axis('equal') # do not use : it resizes the viewport during simulation
        self.ax.margins(0.05)
        #self.ax.set_aspect('equal') # break
        self.ax.autoscale(enable=False)
        fig_size = self.fig.get_size_inches()
        ratio = fig_size[0] / fig_size[1]
        width = self.max[0] - self.min[0]
        height = self.max[1] - self.min[1]
        expected_width = height * ratio
        offset = (expected_width - width) / 2
        self.ax.set_xlim(self.min[0] - offset, self.max[0] + offset)
        self.ax.set_ylim(self.min[1], self.max[1])
        # Statistics for legend
        total_constraints = 0
        total_nodes = 0
        total_node_blocks = 0
        total_constraint_blocks = 0

        # Set label
        plt.title(f'Implicit Solver - frame {frame_id}', fontdict=self.font)
        #plt.xlabel('x (m)')
        #plt.ylabel('y (m)')

        # Draw constraints
        for name in conditions:
            metadata = dispatcher.get_metadata(obj=name)
            total_constraints += metadata['num_constraints']
            total_constraint_blocks += metadata['num_blocks']
            render_prefs = metadata.get("render_prefs", None)
            if render_prefs is None:
                continue

            segs = dispatcher.get_segments_from_constraint(condition=name)
            line_segments = collections.LineCollection(
                segs,
                linewidths=render_prefs['width'],
                colors=render_prefs['color'],
                linestyles=render_prefs['style'],
                alpha=render_prefs['alpha'])

            self.ax.add_collection(line_segments)

        # Draw nodes
        for name in dynamics:
            metadata = dispatcher.get_metadata(obj=name)
            total_nodes += metadata['num_nodes']
            total_node_blocks += metadata['num_blocks']
            render_prefs = metadata.get("render_prefs", None)
            if render_prefs is None:
                continue

            dynamic_data = dispatcher.get_nodes_from_dynamic(dynamic=name)
            x, y = zip(*dynamic_data)
            self.ax.plot(x,
                         y,
                         '.',
                         alpha=render_prefs['alpha'],
                         color=render_prefs['color'],
                         markersize=render_prefs['width'])

        # Draw kinematics
        for name in kinematics:
            metadata = dispatcher.get_metadata(obj=name)
            render_prefs = metadata.get("render_prefs", None)
            if render_prefs is None:
                continue

            normals = dispatcher.get_normals_from_kinematic(kinematic=name)
            line_normals = collections.LineCollection(
                normals,
                linewidths=1,
                colors=render_prefs['color'],
                alpha=render_prefs['alpha'])

            self.ax.add_collection(line_normals)

            triangles = []
            shape = dispatcher.get_shape_from_kinematic(kinematic=name)
            for face_id in shape.face:
                v0 = shape.vertex[face_id[0]]
                v1 = shape.vertex[face_id[1]]
                v2 = shape.vertex[face_id[2]]
                triangles.append([v0, v1, v2])

            collec = collections.PolyCollection(
                triangles,
                facecolors=render_prefs['color'],
                edgecolors=render_prefs['color'],
                alpha=render_prefs['alpha'])
            self.ax.add_collection(collec)

        # Add Legend
        node_patch = patches.Patch(color='blue', label=f'{total_nodes} nodes')
        nblock_patch = patches.Patch(color='lightblue',
                                     label=f'{total_node_blocks} node blocks')
        cnt_patch = patches.Patch(color='green',
                                  label=f'{total_constraints} constraints')
        cblock_patch = patches.Patch(
            color='lightgreen',
            label=f'{total_constraint_blocks} constraint blocks')
        plt.legend(handles=[node_patch, nblock_patch, cnt_patch, cblock_patch],
                   loc='lower left')
        plt.show()
def create_waffle_chart(categories,
                        values,
                        height,
                        width,
                        colormap,
                        value_sign=''):

    # compute the proportion of each category with respect to the total
    total_values = sum(values)
    category_proportions = [(float(value) / total_values) for value in values]

    # compute the total number of tiles
    total_num_tiles = width * height  # total number of tiles
    print('Total number of tiles is', total_num_tiles)

    # compute the number of tiles for each catagory
    tiles_per_category = [
        round(proportion * total_num_tiles)
        for proportion in category_proportions
    ]

    # print out number of tiles per category
    for i, tiles in enumerate(tiles_per_category):
        print(df_dsn.index.values[i] + ': ' + str(tiles))

    # initialize the waffle chart as an empty matrix
    waffle_chart = np.zeros((height, width))

    # define indices to loop through waffle chart
    category_index = 0
    tile_index = 0

    # populate the waffle chart
    for col in range(width):
        for row in range(height):
            tile_index += 1

            # if the number of tiles populated for the current category
            # is equal to its corresponding allocated tiles...
            if tile_index > sum(tiles_per_category[0:category_index]):
                # ...proceed to the next category
                category_index += 1

            # set the class value to an integer, which increases with class
            waffle_chart[row, col] = category_index

    # instantiate a new figure object
    fig = plt.figure()

    # use matshow to display the waffle chart
    colormap = plt.cm.coolwarm
    plt.matshow(waffle_chart, cmap=colormap)
    plt.colorbar()

    # get the axis
    ax = plt.gca()

    # set minor ticks
    ax.set_xticks(np.arange(-.5, (width), 1), minor=True)
    ax.set_yticks(np.arange(-.5, (height), 1), minor=True)

    # add dridlines based on minor ticks
    ax.grid(which='minor', color='w', linestyle='-', linewidth=2)

    plt.xticks([])
    plt.yticks([])

    # compute cumulative sum of individual categories to match color schemes between chart and legend
    values_cumsum = np.cumsum(values)
    total_values = values_cumsum[len(values_cumsum) - 1]

    # create legend
    legend_handles = []
    for i, category in enumerate(categories):
        if value_sign == '%':
            label_str = category + ' (' + str(values[i]) + value_sign + ')'
        else:
            label_str = category + ' (' + value_sign + str(values[i]) + ')'

        color_val = colormap(float(values_cumsum[i]) / total_values)
        legend_handles.append(mpatches.Patch(color=color_val, label=label_str))

    # add legend to chart
    plt.legend(handles=legend_handles,
               loc='lower center',
               ncol=len(categories),
               bbox_to_anchor=(0., -0.2, 0.95, .1))
Пример #6
0
            s_df, g_df, r_df, a = agg_distributions(z_stats, in_shp)

            #Create Summary Table
            agg_tbl = make_table(s_df, g_df, r_df)
            oName = r"C:\workspace\GLCM\output\2014_09" + os.sep + variable + "_aggragrated_" + meter + "_distribution.csv"
            agg_tbl.to_csv(oName, sep=',')

            oName = r"C:\workspace\GLCM\output\2014_09" + os.sep + variable + "_aggragrated_" + meter + ".png"

            plot_agg_table(agg_tbl, oName, meter)

            oName = r"C:\workspace\GLCM\output\2014_09" + os.sep + variable + "_aggragrated_" + meter + "_distribution.png"

            #legend stuff
            blue = mpatches.Patch(color='blue', label='Sand')
            green = mpatches.Patch(color='green', label='Gravel')
            red = mpatches.Patch(color='red', label='Boulders')

            fig = plt.figure(figsize=(6, 2))
            ax = fig.add_subplot(1, 1, 1)
            try:
                s_df.plot.hist(ax=ax,
                               bins=50,
                               legend=False,
                               rot=45,
                               zorder=1,
                               color='blue')
            except:
                pass
Пример #7
0
def view_clusters(X_reduced_tsne, X_reduced_pca, X_reduced_svd):
    f, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(24, 6))
    # labels = ['No Fraud', 'Fraud']
    f.suptitle('Clusters using Dimensionality Reduction', fontsize=14)

    import matplotlib.patches as mpatches
    blue_patch = mpatches.Patch(color='#0A0AFF', label='No Fraud')
    red_patch = mpatches.Patch(color='#AF0000', label='Fraud')

    # t-SNE scatter plot
    ax1.scatter(X_reduced_tsne[:, 0],
                X_reduced_tsne[:, 1],
                c=(y == 0),
                cmap='coolwarm',
                label='No Fraud',
                linewidths=2)
    ax1.scatter(X_reduced_tsne[:, 0],
                X_reduced_tsne[:, 1],
                c=(y == 1),
                cmap='coolwarm',
                label='Fraud',
                linewidths=2)
    ax1.set_title('t-SNE', fontsize=14)
    ax1.grid(True)
    ax1.legend(handles=[blue_patch, red_patch])

    # PCA scatter plot
    ax2.scatter(X_reduced_pca[:, 0],
                X_reduced_pca[:, 1],
                c=(y == 0),
                cmap='coolwarm',
                label='No Fraud',
                linewidths=2)
    ax2.scatter(X_reduced_pca[:, 0],
                X_reduced_pca[:, 1],
                c=(y == 1),
                cmap='coolwarm',
                label='Fraud',
                linewidths=2)
    ax2.set_title('PCA', fontsize=14)
    ax2.grid(True)
    ax2.legend(handles=[blue_patch, red_patch])

    # TruncatedSVD scatter plot
    ax3.scatter(X_reduced_svd[:, 0],
                X_reduced_svd[:, 1],
                c=(y == 0),
                cmap='coolwarm',
                label='No Fraud',
                linewidths=2)
    ax3.scatter(X_reduced_svd[:, 0],
                X_reduced_svd[:, 1],
                c=(y == 1),
                cmap='coolwarm',
                label='Fraud',
                linewidths=2)
    ax3.set_title('Truncated SVD', fontsize=14)

    ax3.grid(True)
    ax3.legend(handles=[blue_patch, red_patch])
    plt.show()
def plot_latent_stats(dataset,
                      model,
                      label=None,
                      tasks=None,
                      n_points=None,
                      layers=[0],
                      legend=True,
                      out=None,
                      balanced=False):

    if balanced and tasks != None:
        ids = set()
        task_ids = []
        #        pdb.set_trace()
        for t in tasks:
            task_ids.append(get_balanced_ids(dataset.metadata[t], n_points))
            ids = ids.union(set(task_ids[-1]))
        ids = list(ids)
        task_ids = [
            np.array([ids.index(x) for x in task_id]) for task_id in task_ids
        ]
    else:
        ids = dataset.data.shape[
            0] if n_points is None else np.random.permutation(
                dataset.data.shape[0])[:n_points]
        task_ids = [] if tasks is None else [range(len(ids))] * len(tasks)

    ids = np.array(ids)

    data = model.format_input_data(dataset.data[ids])
    y = model.format_label_data(dataset.metadata.get(label))
    if not y is None:
        y = y[ids]

    vae_out = model.encode(data, y=y)

    figs = []
    for layer in layers:
        zs = [x.cpu().detach().cpu().numpy() for x in vae_out[0][layer]]
        latent_dim = zs[0].shape[1]
        id_range = np.array(list(range(latent_dim)))
        if tasks is None:
            fig = plt.figure('latent statistics for layer %d' % layer)
            ax1 = fig.add_subplot(211)
            ax1.set_title('variance of latent positions')
            ax2 = fig.add_subplot(212)
            ax2.set_title('mean of variances per axis')
            pos_var = [np.std(zs[0], 0)]
            var_mean = [np.mean(zs[1], 0)]
            width = 1 / len(pos_var)
            cmap = get_cmap(len(pos_var))
            for i in range(len(pos_var)):
                ax1.bar(id_range + i * width, pos_var[i], width)
                ax2.bar(id_range + i * width, var_mean[i], width)
    #        ax1.set_xticklabels(np.arange(latent_dim), np.arange(latent_dim))
    #        ax2.set_xticklabels(np.arange(latent_dim), np.arange(latent_dim))
            if not out is None:
                fig.savefig(out + '_layer%d.svg' % layer, format="svg")
            figs.append(fig)
        else:
            if not issubclass(type(tasks), list):
                tasks = [tasks]
            for t, task in enumerate(tasks):
                print('-- plotting task %s' % task)
                fig = plt.figure('latent statistics for layer %d, task %s' %
                                 (layer, task))
                ax1 = fig.add_subplot(211)
                ax1.set_title('variance of latent positions')
                ax2 = fig.add_subplot(212)
                ax2.set_title('mean of variances per axis')
                # get classes
                class_ids, classes = get_class_ids(dataset,
                                                   task,
                                                   ids=ids[task_ids[t]])
                # get data
                pos_var = []
                var_mean = []
                width = 1 / len(class_ids)
                cmap = get_cmap(len(class_ids))
                handles = []
                for i, c in enumerate(class_ids):
                    pos_var.append(np.std(zs[0][class_ids[i]], 0))
                    var_mean.append(np.mean(zs[1][class_ids[i]], 0))
                    ax1.bar(id_range + i * width,
                            pos_var[i],
                            width,
                            color=cmap(i))
                    ax2.bar(id_range + i * width,
                            var_mean[i],
                            width,
                            color=cmap(i))
                if legend:
                    handles = []
                    class_names = {
                        v: k
                        for k, v in dataset.classes[task].items()
                    }
                    for i in classes:
                        patch = mpatches.Patch(color=cmap(i),
                                               label=class_names[i])
                        handles.append(patch)
                    fig.legend(handles=handles)
                if not out is None:
                    title = out + '_layer%d_%s.svg' % (layer, task)
                    fig.savefig(title, format="svg")
                figs.append(fig)

    # plot histograms
    return figs
def plot_latent_dists(dataset,
                      model,
                      label=None,
                      tasks=None,
                      bins=20,
                      layers=[0],
                      n_points=None,
                      dims=None,
                      legend=True,
                      split=False,
                      out=None,
                      relief=True,
                      ids=None,
                      **kwargs):
    # get data ids
    if n_points is None:
        ids = np.arange(dataset.data.shape[0]) if ids is None else ids
        data = dataset.data
        y = dataset.metadata.get(label)
    else:
        ids = np.random.permutation(
            dataset.data.shape[0])[:n_points] if ids is None else ids
        data = dataset.data[ids]
        y = dataset.metadata.get(label)
        if not y is None:
            y = y[ids]
    y = model.format_label_data(y)
    data = model.format_input_data(data)

    if dims is None:
        dims = list(range(model.platent[layer]['dim']))

    # get latent space
    with torch.no_grad():
        vae_out = model.encode(data, y=y)
        # get latent means of corresponding parameters

    # get
    figs = []

    for layer in layers:
        zs = model.platent[layer]['dist'](
            *vae_out[0][layer]).mean.cpu().detach().numpy()
        if split:
            if tasks is None:
                for dim in dims:
                    fig = plt.figure('dim %d' % dim, figsize=(20, 10))
                    hist, edges = np.histogram(zs[:, dim], bins=bins)
                    plt.bar(edges[:-1],
                            hist,
                            edges[1:] - edges[:-1],
                            align='edge')
                    if not out is None:
                        prefix = out.split('/')[:-1]
                        fig.savefig(prefix + '/dists/' + out.split('/')[-1] +
                                    '_%d_dim%d.svg' % (layer, dim))
                    figs.append(fig)
            else:
                if not os.path.isdir(out + '/dists'):
                    os.makedirs(out + '/dists')
                for t in range(len(tasks)):
                    class_ids, classes = get_class_ids(dataset,
                                                       tasks[t],
                                                       ids=ids)
                    cmap = get_cmap(len(class_ids))
                    for dim in dims:
                        fig = plt.figure('dim %d' % dim, figsize=(20, 10))
                        ax = fig.gca(projection='3d') if relief else fig.gca()
                        for k, cl in enumerate(class_ids):
                            hist, edges = np.histogram(zs[cl, dim], bins=bins)
                            colors = cmap(k)
                            if relief:
                                ax.bar3d(edges[:-1],
                                         k * np.ones_like(hist),
                                         np.zeros_like(hist),
                                         edges[1:] - edges[:-1],
                                         np.ones_like(hist),
                                         hist,
                                         color=colors)
                                ax.view_init(30, 30)
                            else:
                                ax.bar(edges[:-1],
                                       hist,
                                       edges[1:] - edges[:-1],
                                       align='edge')
                        if legend and not dataset.classes.get(
                                tasks[t]) is None:
                            handles = []
                            class_names = {
                                v: k
                                for k, v in dataset.classes[tasks[t]].items()
                            }
                            for i in classes:
                                patch = mpatches.Patch(color=cmap(i),
                                                       label=class_names[i])
                                handles.append(patch)
                            fig.legend(handles=handles)
                        if not out is None:
                            prefix = out.split('/')[:-1]
                            fig.savefig('/'.join(prefix) + '/dists/' +
                                        out.split('/')[-1] +
                                        '_%d_%s_dim%d.svg' %
                                        (layer, tasks[t], dim))
    #                    plt.close('all')
                        figs.append(fig)
        else:
            if tasks is None:
                dim1, dim2 = get_divs(len(dims))
                fig, axes = plt.subplots(dim1, dim2, figsize=(20, 10))
                for i in range(axes.shape[0]):
                    for j in range(axes.shape[1]):
                        current_id = i * dim2 + j
                        hist, edges = np.histogram(zs[:, dims[current_id]],
                                                   bins=bins)
                        axes[i, j].bar(edges[:-1],
                                       hist,
                                       edges[1:] - edges[:-1],
                                       align='edge')
                        axes[i, j].set_title('axis %d' % dims[current_id])
                if not out is None:
                    prefix = out.split('/')[:-1]
                    fig.savefig(out + '_0.svg' % layer)
                figs.append(fig)
            else:
                dim1, dim2 = get_divs(len(dims))
                for t in range(len(tasks)):
                    class_ids, classes = get_class_ids(dataset,
                                                       tasks[t],
                                                       ids=ids)
                    cmap = get_cmap(len(class_ids))
                    if relief:
                        fig, axes = plt.subplots(
                            dim1,
                            dim2,
                            figsize=(20, 10),
                            subplot_kw={'projection': '3d'})
                    else:
                        print('hello')
                        fig, axes = plt.subplots(dim1, dim2, figsize=(20, 10))

    #                pdb.set_trace()
                    for i in range(axes.shape[0]):
                        dim_y = 0 if len(axes.shape) == 1 else axes.shape[1]
                        for j in range(dim_y):
                            current_id = i * dim2 + j
                            for k, cl in enumerate(class_ids):
                                hist, edges = np.histogram(
                                    zs[cl, dims[current_id]], bins=bins)
                                colors = cmap(k)
                                if relief:
                                    axes[i, j].bar3d(edges[:-1],
                                                     k * np.ones_like(hist),
                                                     np.zeros_like(hist),
                                                     edges[1:] - edges[:-1],
                                                     np.ones_like(hist),
                                                     hist,
                                                     color=colors,
                                                     alpha=0.1)
                                    axes[i, j].view_init(30, 30)
                                else:
                                    axes[i, j].bar(edges[:-1],
                                                   hist,
                                                   edges[1:] - edges[:-1],
                                                   align='edge')
                                axes[i,
                                     j].set_title('axis %d' % dims[current_id])

                    if legend and not dataset.classes.get(tasks[t]) is None:
                        handles = []
                        class_names = {
                            v: k
                            for k, v in dataset.classes[tasks[t]].items()
                        }
                        for i in classes:
                            patch = mpatches.Patch(color=cmap(i),
                                                   label=class_names[i])
                            handles.append(patch)
                        fig.legend(handles=handles)

                    if not out is None:
                        prefix = out.split('/')[:-1]
                        fig.savefig(out + '_%d_%s.svg' % (layer, tasks[t]))
                    figs.append(fig)
    return figs
Пример #10
0
def pplot(df, x=None, y=None, color=None,
          linestyle=None, marker=None,ls=None, **kwdargs):
    df_data = df.copy()
    c_split = [""]
    l_split = [""]
    m_split = [""]
    markers = itertools.cycle(["v","o","<",3,4,"+"])
    linestyles = itertools.cycle(['-','--','-.',':'])
    colors = itertools.cycle(["r","y","g","b","p"])
    if not ls==None:
        linestyle = ls
    if x == None:
        x_data = df.index()
    else:
        x_data = df[x]
        df_data = df_data.drop(x,axis=1)

    if not color == None:
        c_split = df[color].unique()

        df_data = df_data.drop(color,axis=1)
    if not linestyle == None:

        l_split = df[linestyle].unique()

        df_data = df_data.drop(linestyle,axis=1)

    if not marker == None:
        m_split = df[marker].unique()

        df_data = df_data.drop(marker,axis=1)
    c_dict = dict(zip(c_split,colors))
    l_dict = dict(zip(l_split,linestyles))
    m_dict = dict(zip(m_split,markers))
    if y == None:
        y=df_data.columns
    elif isinstance(y,str):
        y = [y]

    for c_cond, col in c_dict.items():
        if c_cond == "":
            sub1=df
        else:
            sub1 = df[df[color]==c_cond]
        for m_cond, mark in m_dict.items():
            if m_cond == "":
                sub2 = sub1
            else:
                sub2 = sub1[sub1[marker]==m_cond]
            for l_cond, ls in l_dict.items():
                if l_cond == "":
                    sub3 = sub2
                else:
                    sub3 = sub2[sub2[linestyle]==l_cond]
                if x == None:
                    x_data = sub3.index()
                else:
                    x_data = sub3[x]
                for y_col in y:
                    y_data= sub3[y]
                    plt.plot(x_data.values, y_data.values, color = col,
                             marker=mark, ls=ls, **kwdargs)
                    plt.ylabel(y_col)
    color_handles = [ mlines.Line2D([],[],ls="",label=color)]
    marker_handles = [ mlines.Line2D([],[],ls="",label=marker) ]
    ls_handles = [ mlines.Line2D([],[],ls="",label=linestyle) ]
    for c_cond, col in sorted(c_dict.items()):
        color_handles.append( mpatches.Patch(color=col, label=c_cond) )
    for m_cond, mark in sorted(m_dict.items()):
        marker_handles.append( mlines.Line2D([], [], color='black', marker=mark,
                          markersize=10, label=m_cond, ls="") )
    for l_cond, ls in sorted(l_dict.items()):
        ls_handles.append( mlines.Line2D([], [], color='black', ls=ls,
                          label=l_cond) )

    l1 = plt.legend(handles=color_handles, loc=2)
    l2 = plt.legend(handles=marker_handles, loc=4)
    l3 = plt.legend(handles=ls_handles, loc=1)
    ax = plt.gca().add_artist(l1)
    ax = plt.gca().add_artist(l2)
    ax = plt.gca().add_artist(l3)
    plt.xlabel(x)
Пример #11
0
    def plot_pollster_house_effects(self, samples, hebrew=True):
        """
        Plot the house effects of each pollster per party.
        """
        import matplotlib.pyplot as plt
        import matplotlib.patches as mpatches
        import matplotlib.ticker as ticker
        from bidi import algorithm as bidialg

        house_effects = samples.transpose(2, 1, 0)
        fe = self.forecast_model

        actual_pollsters = [
            i for i in fe.dynamics.pollster_mapping.items() if i[1] is not None
        ]
        pollster_ids = [
            fe.pollster_ids[pollster]
            for _, pollster in sorted(actual_pollsters, key=lambda i: i[1])
        ]

        plots = []
        for i, party in enumerate(fe.party_ids):

            def pollster_label(pi, pollster_id):
                perc = '%.2f %%' % (100 * house_effects[i][pi].mean())
                if hebrew and len(
                        fe.config['pollsters'][pollster_id]['hname']) > 0:
                    label = perc + ' :' + bidialg.get_display(
                        fe.config['pollsters'][pollster_id]['hname'])
                else:
                    label = fe.config['pollsters'][pollster_id][
                        'name'] + ': ' + perc
                return label

            cpalette = sns.color_palette("cubehelix", len(pollster_ids))
            patches = [
                mpatches.Patch(color=cpalette[pi],
                               label=pollster_label(pi, pollster))
                for pi, pollster in enumerate(pollster_ids)
            ]

            fig, ax = plt.subplots(figsize=(10, 2))
            fig.set_facecolor('white')
            legend = fig.legend(handles=patches, loc='best', ncol=2)
            if hebrew:
                for col in legend._legend_box._children[-1]._children:
                    for c in col._children:
                        c._children.reverse()
                    col.align = "right"
            ax.set_title(
                bidialg.get_display(fe.parties[party]['hname']
                                    ) if hebrew else fe.parties[party]['name'])
            for pi, pollster_house_effects in enumerate(house_effects[i]):
                sns.kdeplot(100 * pollster_house_effects,
                            shade=True,
                            ax=ax,
                            color=cpalette[pi])
            ax.xaxis.set_major_formatter(ticker.PercentFormatter(decimals=1))
            ax.yaxis.set_major_formatter(ticker.PercentFormatter(decimals=1))
            plots += [ax]
            fig.text(.5,
                     1.05,
                     bidialg.get_display('הטיית הסוקרים')
                     if hebrew else 'House Effects',
                     ha='center',
                     fontsize='xx-large')
            fig.text(.5,
                     .05,
                     'Generated using pyHoshen © 2019 - 2021',
                     ha='center')
Пример #12
0
    def render(self, sim, path, routing, action):
        # print("\t All paths [wl-node,service-node: ", routing.controlServices)
        # print("RENDERING  action")
        # print("\t Service: ", action[0])
        # print("\t ID_S: ", action[1])
        # print("\t Node: ", action[2])
        # print("Action: ", action[3])
        # sys.exit()

        if self.pos == None:  # Only the first time
            pos = nx.get_node_attributes(sim.topology.G, 'pos')
            if len(pos) > 0:
                for k in pos.keys():
                    pos[k] = np.array(eval(pos[k]))
                self.pos = pos
            else:
                self.pos = nx.random_layout(sim.topology.G)

            image_dir = Path(path + "results/images/")
            image_dir.mkdir(parents=True, exist_ok=True)
            self.image_dir = str(image_dir)

        tab20 = plt.cm.get_cmap('tab20', self.total_services + 5)
        bounds = range(self.total_services + 5)
        newcolors = tab20(np.linspace(0, 1, self.total_services + 5))
        newcolors[0] = np.array([250.0 / 256.0, 250. / 256., 250. / 256., 1])
        newcmp = mpl.colors.ListedColormap(newcolors)
        norm = mpl.colors.BoundaryNorm(bounds, newcmp.N)

        fig, ax = plt.subplots(figsize=(16.0, 10.0))
        # left, bottom, width, height = ax.get_position().bounds
        #

        nx.draw(sim.topology.G,
                self.pos,
                with_labels=False,
                node_size=1,
                node_color="#1260A0",
                edge_color="gray",
                node_shape="o",
                font_size=7,
                font_color="white",
                ax=ax)

        width = ax.get_xlim()[1]
        top = ax.get_ylim()[1]
        # Some viz. vars.
        piesize = .06
        p2 = piesize / 2.5

        try:
            idApp = int(action[0].split("_")[0])
        except ValueError:  #It triggers when the simulation ends: action(null)
            idApp = 0

        color_app = newcmp(idApp)

        ##########
        # Textual data
        ##########
        plt.text(0, top, "Simulation time: %i" % sim.env.now, {
            'color': color_app,
            'fontsize': 12
        })

        info_text = "Action: %s" % action[3]
        plt.text(0, top - 0.2, info_text, {'color': color_app, 'fontsize': 14})

        info_text = "by Service: S%i on Node: N%i" % (action[1], action[2])
        plt.text(0, top - 0.4, info_text, {'color': color_app, 'fontsize': 14})

        # Get the POLICY FILE
        # As the service is named: "idApp_IdModule", we can get the app id from there.
        dataApps = json.load(open(path + 'appDefinition.json'))
        rule_policy = ""

        try:
            for app in dataApps:
                if app["id"] == idApp:
                    rule_policy = app["profile_rules"]
                    break
        except UnboundLocalError:
            print("- WARNING - Rendering the image of the last case")
            None  #Render the last case

        info_text = "App: %i with policy: %s" % (idApp, rule_policy)
        plt.text(0, top - 0.6, info_text, {'color': color_app, 'fontsize': 12})

        info_text = "Debug file: rules_swi_UID%i_n%i_s%i_X_%i.pl" % (
            self.UID, action[2], action[1], sim.env.now)
        plt.text(0, top - 0.8, info_text, {'color': color_app, 'fontsize': 12})

        # Labels on nodes
        for x in sim.topology.G.nodes:
            ax.text(self.pos[x][0] + (width / 45),
                    self.pos[x][1] + (width / 45),
                    "N%i" % (x),
                    fontsize=10)

        #TODO IMPROVE THE GENERATION OF THE LEGEND according with APP & Policies
        # APP Legend
        if not "closers" in self.image_dir:
            # DEFAULT Legends apps
            legendItems = []
            for i in range(1, len(dataApps) + 1):
                color_app = newcmp(i)
                legendItems.append(
                    mpatches.Patch(color=color_app, label='App: %i' % i))
            plt.legend(handles=legendItems, title="title")

        else:
            # SPECIFIC LEGEND for experiment: I_II_III
            offset = 0
            text = ["Get_Closer", "Get_Closer_II", "Get_Closer_III"]
            for i in range(0, len(dataApps)):
                color_app = newcmp(i + 1)
                if (i % 2 == 0):
                    ax.annotate("%s" % text[offset],
                                xy=(0.95, 1.0 - ((i + offset) * 0.05)),
                                xycoords='axes fraction',
                                textcoords='offset points',
                                size=14,
                                bbox=dict(boxstyle="round",
                                          fc="white",
                                          ec="none"))
                    offset += 1

                ax.annotate("App: %i" % (i + 1),
                            xy=(0.95, 1.0 - ((i + offset) * 0.05)),
                            xycoords='axes fraction',
                            textcoords='offset points',
                            size=14,
                            bbox=dict(boxstyle="round",
                                      fc=color_app,
                                      ec="none"))

        # Plotting users dots
        self.__draw_controlUser = {}
        nodes_with_users = self.get_nodes_with_users(routing)

        # DEBUG CODE
        # print("Nodes with users",nodes_with_users)
        # print("ROUTING",routing.controlServices)

        #PRINT ALL USERS
        for node in nodes_with_users:
            # print(node)
            for app in nodes_with_users[node]:
                self.__draw_user(node, int(app), ax, newcolors)

        # LAST step:
        # Displaying capacity, changing node shape
        trans = ax.transData.transform
        trans2 = fig.transFigure.inverted().transform
        data_occupation = self.get_nodes_with_services(sim)

        # Generate node shape
        for n in sim.topology.G.nodes():
            xx, yy = trans(self.pos[n])  # figure coordinates
            xa, ya = trans2((xx, yy))  # axes coordinates
            a = plt.axes([xa - p2, ya - p2, piesize, piesize])
            a.set_aspect('equal')
            # Include the current instance service identificator close to the node
            if idApp in data_occupation[n] and action[2] == n:
                plt.text(xa + piesize * 10, ya + (piesize * 30),
                         "S%i" % action[1], {
                             'color': newcmp(idApp),
                             'fontsize': 16
                         })

            a.imshow(data_occupation[n],
                     cmap=newcmp,
                     interpolation='none',
                     norm=norm)
            a.axes.get_yaxis().set_visible(False)
            a.axes.get_xaxis().set_visible(False)

        canvas = plt.get_current_fig_manager().canvas
        canvas.draw()
        pil_image = Image.frombytes('RGB', canvas.get_width_height(),
                                    canvas.tostring_rgb())
        pil_image.save(self.image_dir + "/network_%05d.png" % self.image_id)
        self.image_id += 1

        plt.close(fig)
        # print("Rendering fILE: %s"%(self.image_dir + "/network_%05d.png" % self.image_id))
        return self.image_dir + "/network_%05d.png" % self.image_id
def evaluate(results, baseline_error_dict, params="magnitude"):
    """
    Visualization code to display results of various learners.
	
	Parameters
	----------
	learners : sklearn model object
		 a list of supervised learners
	results : 
		a list of dictionaries of the statistic results from 'train_predict()'
	mean_absolute_error : float
	mean_squared_error : float
    """

    # Create figure
    fig, ax = pl.subplots(2, 3, figsize=(15, 9))

    # Constants
    bar_width = 0.3
    #colors = ['#A00000', '#00A000']
    colors = ['#A00000', '#00A0A0', '#00A000']

    if params == "magnitude":
        idx = 0
    if params == "angle":
        idx = 1

    # Super loop to plot six panels of data
    for k, learner in enumerate(results.keys()):
        for j, metric in enumerate([
                'train_time', 'mean_absolute_error_train',
                'mean_squared_error_train', 'pred_time',
                'mean_absolute_error_test', 'mean_squared_error_test'
        ]):
            for i in np.arange(3):

                # Creative plot code
                ax[j / 3, j % 3].bar(i + k * bar_width,
                                     results[learner][i][metric][idx],
                                     width=bar_width,
                                     color=colors[k])
                ax[j / 3, j % 3].set_xticks([0.45, 1.45, 2.45])
                ax[j / 3, j % 3].set_xticklabels(["30%", "50%", "100%"])
                ax[j / 3, j % 3].set_xlabel("Training Set Size")
                ax[j / 3, j % 3].set_xlim((-0.1, 3.0))

    # Add unique y-labels
    ax[0, 0].set_ylabel("Time (in seconds)")
    ax[0, 1].set_ylabel("Mean Absolute Error")
    ax[0, 2].set_ylabel("Mean Squared Error")
    ax[1, 0].set_ylabel("Time (in seconds)")
    ax[1, 1].set_ylabel("Mean Absolute Error")
    ax[1, 2].set_ylabel("Mean Squared Error")

    # Add titles
    ax[0, 0].set_title("Model Training")
    ax[0, 1].set_title("MAE  on Training Subset")
    ax[0, 2].set_title("MSE on Training Subset")
    ax[1, 0].set_title("Model Predicting")
    ax[1, 1].set_title("MAE on Testing Set")
    ax[1, 2].set_title("MSE on Testing Set")

    # Add horizontal lines for baseline model
    ax[0, 1].axhline(y=baseline_error_dict["baseline_prediction_MAE_train"],
                     xmin=-0.1,
                     xmax=3.0,
                     linewidth=1,
                     color='k',
                     linestyle='dashed')
    ax[1, 1].axhline(y=baseline_error_dict["baseline_prediction_MAE_test"],
                     xmin=-0.1,
                     xmax=3.0,
                     linewidth=1,
                     color='k',
                     linestyle='dashed')
    ax[0, 2].axhline(y=baseline_error_dict["baseline_prediction_MSE_train"],
                     xmin=-0.1,
                     xmax=3.0,
                     linewidth=1,
                     color='k',
                     linestyle='dashed')
    ax[1, 2].axhline(y=baseline_error_dict["baseline_prediction_MSE_test"],
                     xmin=-0.1,
                     xmax=3.0,
                     linewidth=1,
                     color='k',
                     linestyle='dashed')

    #    ax[0, 2].axhline(y = f1, xmin = -0.1, xmax = 3.0,
    #                     linewidth = 1, color = 'k', linestyle = 'dashed')
    #    ax[1, 2].axhline(y = f1, xmin = -0.1, xmax = 3.0, linewidth = 1,
    #                     color = 'k', linestyle = 'dashed')

    # Set y-limits for score panels
    ax[0, 1].set_ylim((0, 1))
    ax[0, 2].set_ylim((0, 1))
    ax[1, 1].set_ylim((0, 1))
    ax[1, 2].set_ylim((0, 1))

    # Create patches for the legend
    patches = []
    for i, learner in enumerate(results.keys()):
        patches.append(mpatches.Patch(color=colors[i], label=learner))
    pl.legend(handles = patches, bbox_to_anchor = (-.80, 2.53), \
               loc = 'upper center', borderaxespad = 0., ncol = 3, fontsize = 'x-large')

    # Aesthetics
    pl.suptitle("Performance Metrics for Three Supervised Learning Models",
                fontsize=16,
                y=1.10)
    pl.tight_layout()
    pl.show()
Пример #14
0
x_100, y_100 = m3(long_100, lat_100)
m3.plot(x_100, y_100, 'go', markersize=5, color='r')
lat_ = list(terror[terror['casualities'] < 75].latitude)
long_ = list(terror[terror['casualities'] < 75].longitude)
x_, y_ = m3(long_, lat_)
m3.plot(x_, y_, 'go', markersize=2, color='b', alpha=0.4)
m3.drawcoastlines()
m3.drawcountries()
m3.fillcontinents(lake_color='aqua')
m3.drawmapboundary(fill_color='aqua')
fig = plt.gcf()
fig.set_size_inches(10, 6)
plt.title('Total Terrorist Attacks')
plt.legend(loc='lower left',
           handles=[
               mpatches.Patch(color='b', label="< 75 casualities"),
               mpatches.Patch(color='red', label='> 75 casualities')
           ])
plt.savefig('/var/www/html/gif/data/staticfig.png', dpi=300)

# gif animation
fig = plt.figure(figsize=(10, 8))


def animate(Year):
    ax = plt.axes()
    ax.clear()
    ax.set_title('Animation Of Terrorist Activities' + '\n' + 'Year:' +
                 str(Year))
    m6 = Basemap(projection='mill',
                 llcrnrlat=-80,
def train_autoencoder():
    #Get and prep data
    file_names = ["index_flex_out.csv","middle_flex_out.csv","ring_flex_out.csv","pinky_flex_out.csv","thumb_flex_out.csv"]
    """while True:
        name = raw_input("Please input a file name to run t-SNE on (or \"quit\" to stop inputting): ")
        if name == "quit":
            break
        file_names.append(name)"""

    data = []
    for i in range(len(file_names)):
        with open(file_names[i]) as file:
            reader = list(csv.reader(file))
            data.append([])
            for j in range(len(reader)):
                data[i].append([])
                for k in range(len(reader[j])):
                    data[i][j].append(float(reader[j][k]))

    index_end = len(data[0])
    middle_end = len(data[1]) + index_end
    ring_end = len(data[2]) + middle_end
    pinky_end = len(data[3]) + ring_end

    formatted_data = np.asarray(data[0] + data[1] + data[2] + data[3] + data[4])

    # keep track of normalization factor
    normalization_factor = np.linalg.norm(formatted_data)#np.amax(formatted_data)
  
    # normalize data
    normalized_data = formatted_data / normalization_factor

    print(np.amax((normalized_data * normalization_factor) - formatted_data))

    #FOLLOWING MODIFIED FROM: https://blog.keras.io/building-autoencoders-in-keras.html
    # this is the size of our encoded representations
    encoding_dim = 2  # 2 dimensions to plot on X,Y chart

    # this is our input placeholder
    input_vec = Input(shape=(20,))

    # Hidden layer to help with condensing
    #encode_layer = Dense(10, activation='sigmoid')(input_vec)

    # "encoded" is the encoded representation of the input
    encoded = Dense(encoding_dim, activation=None)(input_vec)

    # Hidden layer to help with expanding
    #decode_layer = Dense(10, activation='sigmoid')(encoded)

    # "decoded" is the lossy reconstruction of the input
    decoded = Dense(20, activation=None)(encoded)

    # this model maps an input to its reconstruction
    autoencoder = Model(input_vec, decoded)

    # this model maps an input to its encoded representation
    encoder = Model(input_vec, encoded)

    # create a placeholder for an encoded (2-dimensional) input
    encoded_input = Input(shape=(encoding_dim,))
    # retrieve the last layer of the autoencoder model
    decoder_layer = autoencoder.layers[-1]
    # create the decoder model
    decoder = Model(encoded_input, decoder_layer(encoded_input))

    autoencoder.compile(optimizer='sgd', loss='mse')

    autoencoder.fit(normalized_data, normalized_data,
                epochs=300,
                batch_size=256,
                shuffle=False,
                validation_data=(normalized_data, normalized_data))
    
    encoded_pts = encoder.predict(normalized_data)

    plt.plot(encoded_pts[:index_end,0], encoded_pts[:index_end,1], 'ro',
        encoded_pts[index_end:middle_end,0], encoded_pts[index_end:middle_end,1], 'bo',
        encoded_pts[middle_end:ring_end,0], encoded_pts[middle_end:ring_end,1], 'go',
        encoded_pts[ring_end:pinky_end,0], encoded_pts[ring_end:pinky_end,1], 'co',
        encoded_pts[pinky_end:,0], encoded_pts[pinky_end:,1], 'mo'
        )
    
    red_patch = mpatches.Patch(color='red', label='index flexion')
    blue_patch = mpatches.Patch(color='blue', label='middle flexion')
    green_patch = mpatches.Patch(color='green', label='ring flexion')
    cyan_patch = mpatches.Patch(color='cyan', label='pinky flexion')
    magenta_patch = mpatches.Patch(color='magenta', label='thumb flexion')
    plt.title("Autoencoder")
    plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0.)
    plt.legend(handles=[red_patch, blue_patch, green_patch, cyan_patch, magenta_patch])
    plt.show()

    decoded_pts = decoder.predict(encoded_pts)
    print(np.amax((normalization_factor * decoded_pts) - formatted_data))
    print(np.amax(decoded_pts - normalized_data))
    print(normalized_data)
def plot_latent2(dataset,
                 model,
                 transformation,
                 n_points=None,
                 tasks=None,
                 classes=None,
                 label=None,
                 balanced=False,
                 legend=True,
                 sample=False,
                 layers=0,
                 color_map="plasma",
                 zoom=10,
                 out=None,
                 verbose=False,
                 *args,
                 **kwargs):
    # select points
    if balanced and tasks != None:
        ids = set()
        task_ids = []
        #        pdb.set_trace()
        for t in tasks:
            task_ids.append(get_balanced_ids(dataset.metadata[t], n_points))
            ids = ids.union(set(task_ids[-1]))
        ids = list(ids)
        task_ids = [
            np.array([ids.index(x) for x in task_id]) for task_id in task_ids
        ]
    else:
        ids = dataset.data.shape[
            0] if n_points is None else np.random.permutation(
                dataset.data.shape[0])[:n_points]
        task_ids = [] if tasks is None else [range(len(ids))] * len(tasks)

    ids = np.array(ids)

    data = model.format_input_data(dataset.data[ids])
    metadata = model.format_label_data(
        dataset.metadata[label][ids]) if not label is None else None
    output = model.forward(data, y=metadata, *args, **kwargs)
    figs = []

    for layer in layers:
        # make manifold

        if tasks is None:
            fig = plt.figure('latent plot of layer %d' % layer)

            current_z = model.platent[layer]['dist'](
                *output['z_params_enc'][layer]).mean.detach().numpy()
            if current_z.shape(1) > 2:
                current_z = transformation.fit_transform(current_z)

            plt.scatter(output['z_params_enc'][layer][:, 0],
                        output['z_params_enc'][layer][:, 1])
            plt.title('latent plot of layer %d' % layer)
            if not out is None:
                fig.savefig(out + '_layer%d.svg' % layer, format="svg")
            figs.append(fig)
        else:
            if not issubclass(type(tasks), list):
                tasks = [tasks]
            current_z = model.platent[layer]['dist'](
                *output['z_params_enc'][layer]).mean.detach().numpy()
            if current_z.shape[1] > 2:
                current_z = transformation.fit_transform(current_z)
            for id_task, task in enumerate(tasks):
                print('-- plotting task %s' % task)
                fig = plt.figure('latent plot of layer %d, task %s' %
                                 (layer, task))
                #            pdb.set_trace()
                meta = np.array(dataset.metadata[task])[ids[task_ids[id_task]]]
                _, classes = get_class_ids(dataset,
                                           task,
                                           ids=ids[task_ids[id_task]])
                #            pdb.set_trace()

                cmap = get_cmap(len(classes))
                current_z = current_z[
                    task_ids[id_task], :] if balanced else current_z
                plt.scatter(current_z[:, 0], current_z[:, 1], c=cmap(meta))
                if legend:
                    handles = []
                    if dataset.classes.get(task) != None:
                        class_names = {
                            v: k
                            for k, v in dataset.classes[task].items()
                        }
                        for cl in classes:
                            patch = mpatches.Patch(color=cmap(cl),
                                                   label=class_names[cl])
                            handles.append(patch)
                        fig.legend(handles=handles)

                figs.append(fig)
                if not out is None:
                    title = out + '_layer%d_%s.svg' % (layer, task)
                    fig.savefig(title, format="svg")
    return figs
Пример #17
0
res_D = pickle.load(open(savename,'rb'))
const_test_rew_summary = res_D['zero_test']

all_patches = []

con_rew = np.array(const_test_rew_summary)
mean_con = con_rew.mean(0)
std_con = con_rew.std(0)
if if_filtering==True:
    mean_window_size = 15
    mean_order = 3
    std_window_size = 45
    std_order = 2
    mean_con = scipy.signal.savgol_filter(mean_con, mean_window_size, mean_order)
    std_con = scipy.signal.savgol_filter(std_con, std_window_size, std_order)
x = [i for i in range(len(mean_con))]
plt.plot(x,mean_con,color=(0.5,0.1,0.1), linewidth=2.0)
plt.fill_between(x, mean_con-std_con, mean_con+std_con,color=(0.5,0.1,0.1), alpha=0.5)
all_patches.append(mpatches.Patch(color=(0.5,0.1,0.1), label='zero_test_rew_summary'))

for l in con_rew:
    plt.plot(x,l,color=(0.1,0.5,0.1), linewidth=2.0)

plt.legend(handles=all_patches)
axes = plt.gca()
axes.set_ylim([-500,6000])
plt.title(savename)
plt.show()
#from IPython import embed;embed()
Пример #18
0
def tensorboard_writer(
    queue: mp.Queue,
    log_dir: str,
    parameters: List[str],
    labels: List[str],
    static_args_ini: str,
    num_basis: int,
    val_coefficients: Optional[torch.Tensor]=None,
    val_gts: Optional[torch.Tensor]=None,
    figure_titles: Optional[List[str]]=None,
):

    # suppress luminosity distance debug messages
    logger = logging.getLogger('bilby')
    logger.propagate = False
    logger.setLevel(logging.WARNING)

    if log_dir is None:
        tb = SummaryWriter()
    else:
        tb = SummaryWriter(log_dir)

    _, static_args = read_ini_config(static_args_ini)
    ifos = ('H1', 'L1')
    interferometers = {'H1': 'Hanford', 'L1': 'Livingston', 'V1': 'Virgo', 'K1': 'KAGRA'}
    basis_dir = Path('/mnt/datahole/daniel/gravflows/datasets/basis/')
    basis = SVDBasis(basis_dir, static_args_ini, ifos, preload=False)
    basis.load(time_translations=False, verbose=False)
    basis.truncate(num_basis)

    val_coefficients = val_coefficients.numpy()
    for j in range(val_coefficients.shape[0]):
        fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(16, 4))
        
        for i, ifo in enumerate(ifos):
            reconstruction = val_coefficients[j, i] @ basis.Vh[i]
            reconstruction = FrequencySeries(reconstruction, delta_f=static_args['delta_f'])
            strain = reconstruction.to_timeseries(delta_t=static_args['delta_t'])
            ax.plot(strain.sample_times, strain, label=interferometers[ifo], alpha=0.6)
            
        ax.set_title(f'Reconstructed {figure_titles[j]} Strain')
        ax.set_xlabel('Time (s)')
        ax.set_ylabel('Strain')  # units?
        ax.set_xlim((static_args['seconds_before_event']-1, static_args['seconds_before_event']+1))
        ax.legend(loc='upper left')
        ax.grid('both')

        # ax.axvline(static_args['seconds_before_event'], color='r', linestyle='--')  # merger time marker
        # ax.set_xticks([static_args['seconds_before_event']], minor=True)  # add low frequency cutoff to ticks
        # ax.set_xticklabels(['$t_{c}$'], minor=True, color='r')

        tb.add_figure(f'reconstructions/{figure_titles[j]}', fig)

    del reconstruction
    del val_coefficients
    del basis

    # bilby setup - specify the output directory and the name of the bilby run
    result = bilby.result.read_in_result(
        outdir='bilby_runs/GW150914',
        label='GW150914'
    )

    bilby_parameters = [
        'mass_1', 'mass_2', 'phase', 'geocent_time',
        'luminosity_distance', 'a_1', 'a_2', 'tilt_1', 'tilt_2',
        'phi_12', 'phi_jl', 'theta_jn', 'psi', 'ra', 'dec'
    ]
    bilby_samples = result.posterior[bilby_parameters].values

    # # Shift the time of coalescence by the trigger time
    bilby_samples[:,3] = bilby_samples[:,3] - Merger('GW150914').time

    bilby_df = pd.DataFrame(bilby_samples.astype(np.float32), columns=bilby_parameters)
    bilby_df = bilby_df.rename(columns={'luminosity_distance': 'distance', 'geocent_time': 'time'})
    bilby_df = bilby_df.loc[:, parameters]

    domain = [
        [10, 80],  # mass 1
        [10, 80],  # mass 2
        [0, 2*np.pi],  # phase 
        [0, 1],  # a_1
        [0, 1],  # a 2
        [0, np.pi],  # tilt 1
        [0, np.pi],  # tilt 2
        [0, 2*np.pi],  # phi_12
        [0, 2*np.pi],  # phi_jl
        [0, np.pi],  # theta_jn
        [0, np.pi],  # psi
        [0, 2*np.pi],  # ra
        [-np.pi/2, np.pi/2],  # dec
        # [0.005,0.055],  # tc
        [100,800],  # distance
    ]

    cosmoprior = bilby.gw.prior.UniformSourceFrame(
        name='luminosity_distance',
        minimum=1e2,
        maximum=1e3,
    )

    while True:
        try:
            epoch, scalars, samples = queue.get()

            if samples is not None:
                # requires (batch, samples, parameters)
                assert len(samples.shape) == 3, "samples must be of shape (batch, samples, parameters)"

                if figure_titles is not None:
                    # to do - better handling of passing figure info through queue
                    assert samples.shape[0] == len(figure_titles), (
                        "sample.shape[0] and figure_titles must have matching lengths"
                    )
                else:
                    figure_titles = ['']*samples.shape[0]

            for key, value in scalars.items():
                tb.add_scalar(key, value, epoch)
            
            if samples is not None:
                assert isinstance(samples, torch.Tensor)
                for i in range(samples.shape[0]):

                    fig = plt.figure(figsize=(20,21))

                    if i == 0:
                        # GW150914 ONLY - hardcoded to first position
                        samples_df = pd.DataFrame(samples[i].numpy(), columns=parameters)
                        weights = cosmoprior.prob(samples_df['distance'])
                        weights = weights / np.mean(weights)
                        

                        corner.corner(
                            bilby_df,
                            fig=fig,
                            labels=labels,
                            levels=[0.5, 0.9],
                            quantiles=[0.25, 0.75],
                            color='tab:orange',
                            scale_hist=True,
                            plot_datapoints=False,
                        )

                        corner.corner(
                            samples_df,
                            fig=fig,
                            levels=[0.5, 0.9],
                            quantiles=[0.25, 0.75],
                            color='tab:blue',
                            scale_hist=True,
                            plot_datapoints=False,
                            show_titles=True,
                            weights=weights * len(bilby_samples) / len(samples_df),
                            range=domain,
                        )

                        fig.legend(
                            handles=[
                                mpatches.Patch(color='tab:blue', label='Neural Spline Flow'),
                                mpatches.Patch(color='tab:orange', label='Bilby (dynesty)')],
                            loc='upper right',
                            fontsize=16, 
                        )

                    else:
                        samples_df = pd.DataFrame(samples[i].numpy(), columns=parameters)
                        weights = cosmoprior.prob(samples_df['distance'])
                        weights = weights / np.mean(weights)

                        corner.corner(
                            samples_df,
                            fig=fig,
                            labels=labels,
                            levels=[0.5, 0.9],
                            quantiles=[0.25, 0.75],
                            color='tab:blue',
                            truth_color='tab:orange',
                            scale_hist=True,
                            plot_datapoints=False,
                            show_titles=True,
                            truths=val_gts[i].numpy() if val_gts is not None else None,
                            weights=weights * len(bilby_samples) / len(samples_df),
                            range=domain,
                        )
                        
                        fig.legend(
                            handles=[
                                mpatches.Patch(color='tab:blue', label='Neural Spline Flow'),
                                mpatches.Patch(color='tab:orange', label='Ground Truth')],
                            loc='upper right',
                            fontsize=16, 
                        )

                    fig.suptitle(f'{figure_titles[i]} Parameter Estimation', fontsize=18)
                    
                    # fig.savefig(f'gwpe/figures/{figure_titles[i]}.png')

                    tb.add_figure(f'posteriors/{figure_titles[i]}', fig, epoch)

            tb.flush()

        except Exception as e:
            # warning: assertions may not trigger exception to exit process
            traceback.print_exc()
            os.kill(os.getpid(), signal.SIGSTOP)  # to do: check kill command
Пример #19
0
        price_difference = nw_price_difference
        day = nw_day

        plt.style.use('bmh')
        fig, ax = plt.subplots(figsize=(14, 9))
        plt.style.context('dark_background')

        plt.title(item_name)
        # plt.plot(day,iffted_abnormal_search,price_difference)
        # plt.plot(day,ratio)
        # plt.plot(day,product)
        print(len(day), len(iffted_abnormal_search), len(price_difference))

        plt.plot(day, extrapolation, 'b', label='pattern')
        plt.plot(day, iffted_abnormal_search, 'r', price_difference, 'g')
        red_patch = mpatches.Patch(color='red', label='경향성 예측')
        blue_patch = mpatches.Patch(color='blue', label='패턴 예측')
        green_patch = mpatches.Patch(color='green', label='실 주가 변동량')
        plt.legend(handles=[red_patch, blue_patch, green_patch])
        # plt.plot(abnormal_search)
        plt.xlabel('날짜')
        plt.ylabel('주가 변동량')

        plt.show()
    except IndexError:
        print("IndexError in", item_name)


def show(a=3):
    plt.title(item_name)
    if (a == 0):
Пример #20
0
def plot(title: str, page: str, entries: object):
    entries.sort(key=lambda x: x['startedDateTime'])
    size = page.split('/')[-2]
    numObjects = page.split('/')[-1].split('.')[0].split('-')[-1]

    start_time = datetime.strptime(entries[0]['startedDateTime'],
                                   ISO_8601_FORMAT)

    captions = list(map(lambda x: x['request']['url'].split('/')[-1], entries))
    captions.insert(0, '')

    fig = plt.figure()
    plt.title(title)
    plt.yticks(np.arange(len(entries) + 1), captions)
    plt.ylim(0, len(entries) + 1)

    if size == '10kb':
        if numObjects == '1':
            plt.xlim(0, 500)
        elif numObjects == '10':
            plt.xlim(0, 1500)
        elif numObjects == '100':
            plt.xlim(0, 5000)
    elif size == '100kb':
        if numObjects == '1':
            plt.xlim(0, 1000)
        elif numObjects == '10':
            plt.xlim(0, 5000)
        elif numObjects == '100':
            plt.xlim(0, 15000)
    elif size == '1000kb':
        if numObjects == '1':
            plt.xlim(0, 3000)
        elif numObjects == '10':
            plt.xlim(0, 10000)
        elif numObjects == '100':
            plt.xlim(0, 20000)

    plt.autoscale(False)
    plt.xlabel('Time (ms)')
    plt.legend(handles=[
        mpatches.Patch(color='green', label='connect'),
        mpatches.Patch(color='cyan', label='send'),
        mpatches.Patch(color='yellow', label='wait'),
        mpatches.Patch(color='magenta', label='receive')
    ])

    for i, entry in enumerate(entries):
        start = datetime.strptime(entry['startedDateTime'], ISO_8601_FORMAT)
        end = start + timedelta(milliseconds=entry['time'])
        connect, send, wait, receive, = itemgetter('connect', 'send', 'wait',
                                                   'receive')(entry['timings'])

        y = i + 1
        xstart = (start - start_time) / timedelta(milliseconds=1)
        xstop = (end - start_time) / timedelta(milliseconds=1)

        # Total time
        plt.hlines(y, xstart, xstop, 'r', lw=4)
        # line_height = len(entries) / 40
        # plt.vlines(xstart, y+line_height, y-line_height, 'k', lw=2)
        # plt.vlines(xstop, y+line_height, y-line_height, 'k', lw=2)

        # Connect time: green
        if connect != -1:
            plt.hlines(y, xstart, xstart + connect, 'g', lw=4)
            xstart += connect

        # Send time: cyan
        plt.hlines(y, xstart, xstart + send, 'c', lw=4)
        xstart += send

        # Wait time: yellow
        plt.hlines(y, xstart, xstart + wait, 'y', lw=4)
        xstart += wait

        # Receive time: magenta
        plt.hlines(y, xstart, xstart + receive, 'm', lw=4)
        xstart += receive

    # plt.show()
    graph_dir = Path.joinpath(Path.home(), 'quic', 'graphs', size, numObjects,
                              title)
    Path(graph_dir).mkdir(parents=True, exist_ok=True)

    graph_file = Path.joinpath(graph_dir, 'graph.png')
    if os.path.isfile(graph_file):
        os.remove(graph_file)

    fig.savefig(graph_file, dpi=fig.dpi)
    plt.close(fig=fig)
Пример #21
0
    label_list = []

    for day in daylist:
        y = np.asarray(day[3])
        # day.append(savitzky_golay(y, 101, 3))   #Index 4
        day.append("None")
        day.append(savitzky_golay(y, 361, 3))  #Index 5
        day.append(colors[0])
        day.append(colors[0])
        colors.remove(colors[0])

        #sm1 = plt.plot(x, day[4], color=day[6])
        sm2 = plt.plot(x, day[5], color=day[7])

        label_list.append(
            mpatches.Patch(color=day[6],
                           label=day[0] + ', order 3, window size 361'))

    # Ticks at even hours
    plt.xticks(np.arange(0, 25, 2))
    # Cut off endpoints fix; only show current day
    plt.xlim([0, 24])

    plt.xlabel('Time of day (hours)')
    plt.ylabel('Smoothed received signal strength (dBm)')

    #Additional 15dBm on top for legend
    #plt.ylim([plt.gca().get_ylim()[0],plt.gca().get_ylim()[1]+15])

    #plt.legend(handles=label_list,prop={'size':5})

    plt.savefig("" + sys.argv[8] + "_" + day[0] + "_" + "chan" +
Пример #22
0
def evaluate(results, accuracy, f1):
    """
    Visualization code to display results of various learners.
    
    inputs:
      - learners: a list of supervised learners
      - stats: a list of dictionaries of the statistic results from 'train_predict()'
      - accuracy: The score for the naive predictor
      - f1: The score for the naive predictor
    """
  
    # Create figure
    fig, ax = pl.subplots(2, 3, figsize = (11,7))

    # Constants
    bar_width = 0.3
    colors = ['#A00000','#00A0A0','#00A000']
    
    # Super loop to plot four panels of data
    for k, learner in enumerate(results.keys()):
        for j, metric in enumerate(['train_time', 'acc_train', 'f_train', 'pred_time', 'acc_test', 'f_test']):
            for i in np.arange(3):
                
                # Creative plot code
                ax[j//3, j%3].bar(i+k*bar_width, results[learner][i][metric], width = bar_width, color = colors[k])
                ax[j//3, j%3].set_xticks([0.45, 1.45, 2.45])
                ax[j//3, j%3].set_xticklabels(["1%", "10%", "100%"])
                ax[j//3, j%3].set_xlabel("Training Set Size")
                ax[j//3, j%3].set_xlim((-0.1, 3.0))
    
    # Add unique y-labels
    ax[0, 0].set_ylabel("Time (in seconds)")
    ax[0, 1].set_ylabel("Accuracy Score")
    ax[0, 2].set_ylabel("F-score")
    ax[1, 0].set_ylabel("Time (in seconds)")
    ax[1, 1].set_ylabel("Accuracy Score")
    ax[1, 2].set_ylabel("F-score")
    
    # Add titles
    ax[0, 0].set_title("Model Training")
    ax[0, 1].set_title("Accuracy Score on Training Subset")
    ax[0, 2].set_title("F-score on Training Subset")
    ax[1, 0].set_title("Model Predicting")
    ax[1, 1].set_title("Accuracy Score on Testing Set")
    ax[1, 2].set_title("F-score on Testing Set")
    
    # Add horizontal lines for naive predictors
    ax[0, 1].axhline(y = accuracy, xmin = -0.1, xmax = 3.0, linewidth = 1, color = 'k', linestyle = 'dashed')
    ax[1, 1].axhline(y = accuracy, xmin = -0.1, xmax = 3.0, linewidth = 1, color = 'k', linestyle = 'dashed')
    ax[0, 2].axhline(y = f1, xmin = -0.1, xmax = 3.0, linewidth = 1, color = 'k', linestyle = 'dashed')
    ax[1, 2].axhline(y = f1, xmin = -0.1, xmax = 3.0, linewidth = 1, color = 'k', linestyle = 'dashed')
    
    # Set y-limits for score panels
    ax[0, 1].set_ylim((0, 1))
    ax[0, 2].set_ylim((0, 1))
    ax[1, 1].set_ylim((0, 1))
    ax[1, 2].set_ylim((0, 1))

    # Create patches for the legend
    patches = []
    for i, learner in enumerate(results.keys()):
        patches.append(mpatches.Patch(color = colors[i], label = learner))
    pl.legend(handles = patches, bbox_to_anchor = (-.80, 2.53), \
               loc = 'upper center', borderaxespad = 0., ncol = 3, fontsize = 'x-large')
    
    # Aesthetics
    pl.suptitle("Performance Metrics for Three Supervised Learning Models", fontsize = 16, y = 1.10)
    pl.tight_layout()
    pl.show()
# add gridlines based on minor ticks
ax.grid(which='minor', color='w', linestyle='-', linewidth=2)

plt.xticks([])
plt.yticks([])

# compute cumulative sum of individual categories to match color schemes between chart and legend
values_cumsum = np.cumsum(df_dsn['Total'])
total_values = values_cumsum[len(values_cumsum) - 1]

# create legend
legend_handles = []
for i, category in enumerate(df_dsn.index.values):
    label_str = category + ' (' + str(df_dsn['Total'][i]) + ')'
    color_val = colormap(float(values_cumsum[i]) / total_values)
    legend_handles.append(mpatches.Patch(color=color_val, label=label_str))

# add legend to chart
plt.legend(handles=legend_handles,
           loc='lower center',
           ncol=len(df_dsn.index.values),
           bbox_to_anchor=(0., -0.2, 0.95, .1))


def create_waffle_chart(categories,
                        values,
                        height,
                        width,
                        colormap,
                        value_sign=''):
Пример #24
0
              weight='bold')
cbPDF2 = mpl.colorbar.ColorbarBase(ax_cbPDF2,
                                   cmap=cmapcbPDF2,
                                   norm=normcbPDF,
                                   orientation='horizontal')
cbPDF2.set_ticks([])
cbPDF2.set_label('Space of solution with maximal depth 11 km')
ax_cbPDF2.text(0.035,
               0.03,
               'Increasing weight',
               ha='center',
               va='center',
               rotation=0,
               color='White',
               weight='bold')
Io_uncertainties = mpatches.Patch(color='PaleVioletRed',
                                  alpha=0.3,
                                  label='I0 uncertainties')
Ibin_plt, = ax_Ifit.plot([], [],
                         'd',
                         markerfacecolor='k',
                         markeredgecolor='k',
                         label='Binned intensity with RAVG method')
Iobs_plt, = ax_Ifit.plot([], [],
                         '.',
                         color='Gray',
                         label='Observed intensities')
ax_Ifit.legend(handles=[Io_uncertainties, Ibin_plt, Iobs_plt], loc=4)

plt.savefig('Figure7.png', dpi=200, bbox_inches='tight')
Пример #25
0
 def plot_summary(self, filename=None, colors=None, plot=True):
     """Method to generate a visual summary of different characteristics of the given library. The class methods
     are used with their standard options.
 
     :param filename: {str} path to save the generated plot to.
     :param colors: {str / list} color or list of colors to use for plotting. e.g. '#4E395D', 'red', 'k'
     :param plot: {boolean} whether the plot should be created or just the features are calculated
     :return: visual summary (plot) of the library characteristics (if ``plot=True``).
     :Example:
     
     >>> g = GlobalAnalysis([seqs1, seqs2, seqs3])  # seqs being lists / arrays of sequences
     >>> g.plot_summary()
     
     .. image:: ../docs/static/summary.png
         :height: 600px
     """
     # calculate all global properties
     self.calc_len()
     self.calc_aa_freq(plot=False)
     self.calc_charge(ph=7.4, amide=True)
     self.calc_H()
     self.calc_uH()
     
     if plot:
         
         # plot settings
         fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(25, 15))
         ((ax2, ax5, ax1), (ax3, ax4, ax6)) = axes
         plt.suptitle('Summary', fontweight='bold', fontsize=16.)
         labels = self.libnames
         if not colors:
             colors = ['#FA6900', '#69D2E7', '#542437', '#53777A', '#CCFC8E', '#9CC4E4']
         num = len(labels)
         
         for a in [ax1, ax2, ax3, ax4, ax5, ax6]:
             # only left and bottom axes, no box
             a.spines['right'].set_visible(False)
             a.spines['top'].set_visible(False)
             a.xaxis.set_ticks_position('bottom')
             a.yaxis.set_ticks_position('left')
         
         # 1 length box plot
         box = ax1.boxplot(self.len, notch=1, vert=1, patch_artist=True)
         plt.setp(box['whiskers'], color='black')
         plt.setp(box['medians'], linestyle='-', linewidth=1.5, color='black')
         for p, patch in enumerate(box['boxes']):
             patch.set(facecolor=colors[p], edgecolor='black', alpha=0.8)
         ax1.set_ylabel('Sequence Length', fontweight='bold', fontsize=14.)
         ax1.set_xticks([x + 1 for x in range(len(labels))])
         ax1.set_xticklabels(labels, fontweight='bold')
         
         # 2 AA bar plot
         d_aa = count_aas('')
         hands = [mpatches.Patch(label=labels[i], facecolor=colors[i], alpha=0.8) for i in range(len(labels))]
         w = .9 / num  # bar width
         offsets = np.arange(start=-w, step=w, stop=num * w)  # bar offsets if many libs
         for i, l in enumerate(self.aafreq):
             for a in range(20):
                 ax2.bar(a - offsets[i], l[a], w, color=colors[i], alpha=0.8)
         ax2.set_xlim([-1., 20.])
         ax2.set_ylim([0, 1.05 * np.max(self.aafreq)])
         ax2.set_xticks(range(20))
         ax2.set_xticklabels(d_aa.keys(), fontweight='bold')
         ax2.set_ylabel('Fraction', fontweight='bold', fontsize=14.)
         ax2.set_xlabel('Amino Acids', fontweight='bold', fontsize=14.)
         ax2.legend(handles=hands, labels=labels)
         
         # 3 hydophobicity violin plot
         for i, l in enumerate(self.H):
             vplot = ax3.violinplot(l, positions=[i + 1], widths=0.5, showmeans=True, showmedians=False)
             # crappy adaptions of violin dictionary elements
             vplot['cbars'].set_edgecolor('black')
             vplot['cmins'].set_edgecolor('black')
             vplot['cmeans'].set_edgecolor('black')
             vplot['cmaxes'].set_edgecolor('black')
             vplot['cmeans'].set_linestyle('--')
             for pc in vplot['bodies']:
                 pc.set_facecolor(colors[i])
                 pc.set_alpha(0.8)
                 pc.set_edgecolor('black')
                 pc.set_linewidth(1.5)
                 pc.set_alpha(0.7)
                 pc.set_label(labels[i])
         ax3.set_xticks([x + 1 for x in range(len(labels))])
         ax3.set_xticklabels(labels, fontweight='bold')
         ax3.set_ylabel('Global Hydrophobicity', fontweight='bold', fontsize=14.)
         
         # 4 hydrophobic moment violin plot
         for i, l in enumerate(self.uH):
             vplot = ax4.violinplot(l, positions=[i + 1], widths=0.5, showmeans=True, showmedians=False)
             # crappy adaptions of violin dictionary elements
             vplot['cbars'].set_edgecolor('black')
             vplot['cmins'].set_edgecolor('black')
             vplot['cmeans'].set_edgecolor('black')
             vplot['cmaxes'].set_edgecolor('black')
             vplot['cmeans'].set_linestyle('--')
             for pc in vplot['bodies']:
                 pc.set_facecolor(colors[i])
                 pc.set_alpha(0.8)
                 pc.set_edgecolor('black')
                 pc.set_linewidth(1.5)
                 pc.set_alpha(0.7)
                 pc.set_label(labels[i])
         ax4.set_xticks([x + 1 for x in range(len(labels))])
         ax4.set_xticklabels(labels, fontweight='bold')
         ax4.set_ylabel('Global Hydrophobic Moment', fontweight='bold', fontsize=14.)
         
         # 5 charge histogram
         if self.shapes:  # if the library consists of different sized sub libraries
             bwidth = 1. / len(self.shapes)
             for i, c in enumerate(self.charge):
                 counts, bins = np.histogram(c, range=[-5, 20], bins=25, normed=True)
                 ax5.bar(bins[1:] + i * bwidth, counts, bwidth, color=colors[i], label=labels[i], alpha=0.8)
                 # ax5.hist(c, bins, alpha=0.7, align=alignments[i], rwidth=0.95 / len(self.shapes), histtype='bar',
                 #         normed=1, label=labels[i], color=colors[i])
         else:
             ax5.hist(self.charge, 25, normed=1, alpha=0.8, align='left', rwidth=0.95, histtype='bar', label=labels,
                      color=colors[:num])
         ax5.set_xlabel('Global Charge', fontweight='bold', fontsize=14.)
         ax5.set_ylabel('Fraction', fontweight='bold', fontsize=14.)
         ax5.set_xlim(-6, 21)
         ax5.text(0.95, 0.8, b'amide: $true$', verticalalignment='center', horizontalalignment='right',
                  transform=ax5.transAxes, fontsize=15)
         ax5.text(0.95, 0.75, b'pH:  $7.4$', verticalalignment='center', horizontalalignment='right',
                  transform=ax5.transAxes, fontsize=15)
         ax5.legend()
         
         # 6 3D plot
         ax6.spines['left'].set_visible(False)
         ax6.spines['bottom'].set_visible(False)
         ax6.set_xticks([])
         ax6.set_yticks([])
         ax6 = fig.add_subplot(2, 3, 6, projection='3d')
         for i, l in enumerate(range(num)):
             xt = self.H[l]  # find all values in x for the given target
             yt = self.charge[l]  # find all values in y for the given target
             zt = self.uH[l]  # find all values in y for the given target
             ax6.scatter(xt, yt, zt, c=colors[l], alpha=.8, s=25, label=labels[i])
         
         ax6.set_xlabel('H', fontweight='bold', fontsize=14.)
         ax6.set_ylabel('Charge', fontweight='bold', fontsize=14.)
         ax6.set_zlabel('uH', fontweight='bold', fontsize=14.)
         data_c = [item for sublist in self.charge for item in sublist]  # flatten charge data into one list
         data_H = [item for sublist in self.H for item in sublist]  # flatten H data into one list
         data_uH = [item for sublist in self.uH for item in sublist]  # flatten uH data into one list
         ax6.set_xlim([np.min(data_H), np.max(data_H)])
         ax6.set_ylim([np.min(data_c), np.max(data_c)])
         ax6.set_zlim([np.min(data_uH), np.max(data_uH)])
         ax6.legend(loc='best')
         
         if filename:
             plt.savefig(filename, dpi=200)
         else:
             plt.show()
Пример #26
0
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import numpy as np

plt.figure(figsize=[10, 8])
labels = ['0', '1', '2', '3', '4', '5']
plt.rcParams.update({'font.size': 32})

y1 = [0.9000, 0.9438, 0.9796, 0.9384, 0.8273]
y2 = [0.9583, 0.7952, 0.4733, 0.8095, 0.7667]
x = [1, 2, 3, 4, 5]

ax = plt.subplot(111)
ax.scatter(x, y1, color='b')
ax.plot(x, y1, color='b')
ax.scatter(x, y2, color='r')
ax.plot(x, y2, color='r')

plt.xticks(np.linspace(0, 5, 6), labels)
plt.yticks(np.linspace(0, 1, 11))
ax.set_ylabel('F-measure')
ax.set_xlabel('nr of dependencies')
glm = mpatches.Patch(color='b', label='HAD')
lasso = mpatches.Patch(color='r', label='AD')

plt.legend(handles=[glm, lasso])
#plt.suptitle("Number of dependencies")

plt.show()
def plot_grid(data,
              intervals,
              nodes,
              filename,
              interval_width_s=None,
              title=None,
              x_axis_range=None,
              has_ground_truth=None):

    if x_axis_range is not None:
        data = data[:, x_axis_range[0]:x_axis_range[1]]

    missing_color = "white"
    # TP=0, TN=1, FP=2, FN=3, NODE_ABSENT=4, P=5, N=6
    if has_ground_truth:
        tp_color = 'lime'
        fn_color = 'olivedrab'
        tn_color = 'red'
        fp_color = 'rosybrown'
        cmap = colors.ListedColormap(
            [tp_color, tn_color, fp_color, fn_color, missing_color])
    else:
        p_color = 'lime'
        n_color = 'red'
        np.where(data == P, 0, data)
        np.where(data == N, 1, data)
        np.where(data == NODE_ABSENT, 2, data)
        cmap = colors.ListedColormap([missing_color, p_color, n_color])

    bounds = [-0.5, 0.5, 1.5, 2.5, 3.5, 4.5]
    norm = colors.BoundaryNorm(bounds, cmap.N)

    fig, ax = plt.subplots(figsize=(150, 25))
    ax.imshow(data, cmap=cmap, norm=norm)

    y_major_ticks = np.arange(0, len(nodes), 1)
    y_minor_ticks = np.arange(-.5, len(nodes), 1)

    if x_axis_range is not None:
        x_major_ticks = np.arange(x_axis_range[0], x_axis_range[1], 1)
        x_minor_ticks = np.arange(x_axis_range[0] - .5, x_axis_range[1], 1)
    else:
        x_major_ticks = np.arange(0, len(intervals), 1)
        x_minor_ticks = np.arange(-.5, len(intervals), 1)

    ax.set_xticks(x_major_ticks)
    ax.set_xticks(x_minor_ticks, minor=True)

    ax.set_yticks(y_major_ticks)
    ax.set_yticks(y_minor_ticks, minor=True)

    ax.set_yticklabels(labels=nodes)

    ax.grid(which='minor', alpha=1.0, color='black', linewidth=2)
    ax.grid(which='major', alpha=0.0)

    if has_ground_truth:
        tp_patch = mpatches.Patch(color=tp_color, label='TP')
        fn_patch = mpatches.Patch(color=fn_color, label='FN')
        tn_patch = mpatches.Patch(color=tn_color, label='TN')
        fp_patch = mpatches.Patch(color=fp_color, label='FP')
        missing_patch = mpatches.Patch(color=missing_color, label='Missing')
        plt.legend(
            handles=[tp_patch, fn_patch, tn_patch, fp_patch, missing_patch])
    else:
        p_patch = mpatches.Patch(color=p_color, label='P')
        n_patch = mpatches.Patch(color=n_color, label='N')
        missing_patch = mpatches.Patch(color=missing_color, label='Missing')
        plt.legend(handles=[p_patch, n_patch, missing_patch])

    plt.xlabel(
        f'Intervals (width= {interval_width_s if interval_width_s is not None else "-"} seconds)'
    )
    plt.ylabel('Nodes')

    if title is not None:
        plt.title(title, fontsize='x-large')

    print(f"Saving grid view to: {filename}")
    plt.savefig(filename)
Пример #28
0
    def plot(self):
        '''
        Plot waterfall data
        '''
        self.figure.clear()
        self.rect_locations = np.arange(
            len(self.waterfall_data['Best response percent change']))
        self.ax = self.figure.add_subplot(111)
        self.patches = []
        self.bar_labels = self.waterfall_data['Overall response']

        if self.settings_update == False:
            self.ax.tick_params(
                axis='x',  # changes apply to the x-axis
                which='both',  # both major and minor ticks are affected
                bottom='on',  # ticks along the bottom edge are off
                top='on',  # ticks along the top edge are off
                labelbottom='on')  # labels along the bottom edge are off
            self.ax.axhline(y=20,
                            linestyle='--',
                            c='k',
                            alpha=0.5,
                            lw=2.0,
                            label='twenty_percent')
            self.ax.axhline(y=-30,
                            linestyle='--',
                            c='k',
                            alpha=0.5,
                            lw=2.0,
                            label='thirty_percent')
            self.ax.axhline(y=0, c='k', alpha=1, lw=2.0, label='zero_percent')
            self.ax.grid(color='k', axis='y', alpha=0.25)
            self.bar_colors = self.get_bar_colors(
                self.waterfall_data['Overall response'])
            self.rects = self.ax.bar(
                self.rect_locations,
                self.waterfall_data['Best response percent change'],
                color=self.bar_colors,
                label=self.waterfall_data['Overall response'])
            for key in self.keys_and_colors.keys():
                self.patches.append(
                    mpatches.Patch(color=self.keys_and_colors[key], label=key))
            self.ax.legend(handles=self.patches)
        else:
            self.ax.legend([])
            #settings were updated, we received them and stored in variable self.gen_settings
            self.ax.set_title(self.gen_settings[0])
            self.ax.set_xlabel(self.gen_settings[1])
            self.ax.set_ylabel(self.gen_settings[2])
            if self.gen_settings[3][0]:
                self.ax.axhline(y=20,
                                linestyle='--',
                                c='k',
                                alpha=0.5,
                                lw=2.0,
                                label='twenty_percent')
            if self.gen_settings[3][1]:
                self.ax.axhline(y=-30,
                                linestyle='--',
                                c='k',
                                alpha=0.5,
                                lw=2.0,
                                label='thirty_percent')
            if self.gen_settings[3][2]:
                self.ax.axhline(y=0,
                                c='k',
                                alpha=1,
                                lw=2.0,
                                label='zero_percent')

            if self.gen_settings[4][0] and ~self.gen_settings[6]:
                #show responses as labels, default color bars
                #legend depends on user specified keys
                self.rects = self.ax.bar(
                    self.rect_locations,
                    self.waterfall_data['Best response percent change'],
                    label=self.waterfall_data['Overall response'])
                self.add_labels(self.ax, self.rects, self.waterfall_data, 1)
            elif self.gen_settings[4][1]:
                #color bars with response type
                self.bar_colors = self.get_bar_colors(
                    self.waterfall_data['Overall response'])
                self.rects = self.ax.bar(
                    self.rect_locations,
                    self.waterfall_data['Best response percent change'],
                    color=self.bar_colors,
                    label=self.waterfall_data['Overall response'])
                for key in self.keys_and_colors.keys():
                    self.patches.append(
                        mpatches.Patch(color=self.keys_and_colors[key],
                                       label=key))
                self.ax.legend(handles=self.patches)

            elif self.gen_settings[4][2]:
                #response not shown as color coding, custom color code the bars
                self.bar_labels = self.gen_settings[7]
                self.bar_colors = self.get_bar_colors(self.gen_settings[7])
                used_keys = list(set(self.gen_settings[7]))
                self.rects = self.ax.bar(
                    self.rect_locations,
                    self.waterfall_data['Best response percent change'],
                    color=self.bar_colors,
                    label=self.gen_settings[7])
                for key in used_keys:
                    self.patches.append(
                        mpatches.Patch(color=self.keys_and_colors[key],
                                       label=key))
                self.ax.legend(handles=self.patches)
            else:
                self.rects = self.ax.bar(
                    self.rect_locations,
                    self.waterfall_data['Best response percent change'],
                    label=self.waterfall_data['Overall response'])

            if self.gen_settings[5]:
                self.plot_table()

            if self.gen_settings[6] and ~self.gen_settings[4][0]:
                self.add_labels(self.ax, self.rects, self.waterfall_data, 0)

        self.ax.grid(color='k', axis='y', alpha=0.25)
        self.canvas.draw()
        self.generated_rectangles_signal.emit([self.rects, self.bar_labels])
by_hsv = sorted((tuple(mcolors.rgb_to_hsv(mcolors.to_rgba(color)[:3])), name)
                for name, color in colors.items())
sorted_cnames = [name for hsv, name in by_hsv]
#Getting random colours for each row
color_indices = list(
    np.random.choice(len(sorted_cnames), size=len(data), replace=False))

# plotting

colors = ['red', 'blue', 'yellow', 'green']
ligther_colors = ['lavenderblush', 'azure', 'papayawhip', 'honeydew']
mng = plt.get_current_fig_manager()
mng.full_screen_toggle()

patient_groupnames = list(table1part.keys())
legend_patches = [(mpatches.Patch(color=colors[i],
                                  label=patient_groupnames[i]))
                  for i in range(len(data))]


def plot_asindividual(patientgroupsmean, patientgroupslower,
                      patientgroupsupper):
    for data_count in range(len(data)):
        fig = plt.figure()
        title_fig = ''.join(x for x in patient_groupnames[data_count]).title()
        ranges = [(10, 80), (15, 40), (110, 164), (55, 90), (5, 10)]
        radar = ComplexRadar(fig, variables, ranges)
        radar.fill(patientgroupsupper[data_count],
                   ligther_colors[data_count],
                   alpha=1)
        radar.fill(patientgroupslower[data_count], 'white', alpha=1)
        radar.plot(patientgroupsmean[data_count],
Пример #30
0
    def plot_block_section(self,
                           solution: Solution,
                           cell_number: int,
                           block: np.ndarray = None,
                           direction: str = "y",
                           interpolation: str = 'none',
                           show_data: bool = False,
                           show_faults: bool = False,
                           show_topo: bool = False,
                           block_type=None,
                           ve: float = 1,
                           **kwargs):
        """Plot a section of the block model

        Args:
            solution (Solution): [description]
            cell_number (int): Section position of the array to plot.
            block (np.ndarray, optional): Lithology block. Defaults to None.
            direction (str, optional): Cartesian direction to be plotted
                ("x", "y", "z"). Defaults to "y".
            interpolation (str, optional): Type of interpolation of plt.imshow.
                Defaults to 'none'. Acceptable values are ('none' ,'nearest',
                'bilinear', 'bicubic', 'spline16', 'spline36', 'hanning',
                'hamming', 'hermite', 'kaiser', 'quadric', 'catrom', 'gaussian',
                'bessel', 'mitchell', 'sinc', 'lanczos'.
            show_data (bool, optional): Plots input data on-top of block
                section. Defaults to False.
            show_faults (bool, optional): Plot fault line on-top of block
                section. Defaults to False.
            show_topo (bool, optional): Plots block section with topography.
                Defaults to True.
            block_type ([type], optional): [description]. Defaults to None.
            ve (float, optional): Vertical exaggeration. Defaults to 1.

        Returns:
            (gempy.plot.visualization_2d.PlotData2D) Block section plot.
        """
        if block is None:
            _block = solution.lith_block
        else:
            _block = block
            if _block.dtype == bool:
                kwargs['cmap'] = 'viridis'
                kwargs['norm'] = None

        if block_type is not None:
            raise NotImplementedError

        plot_block = _block.reshape(self.model.grid.regular_grid.resolution[0],
                                    self.model.grid.regular_grid.resolution[1],
                                    self.model.grid.regular_grid.resolution[2])
        _a, _b, _c, extent_val, x, y = self._slice(direction, cell_number)[:-2]

        if show_data:
            self.plot_data(direction, 'all')
        # TODO: plot_topo option - need fault_block for that

        # apply vertical exageration
        if direction in ("x", "y"):
            aspect = ve
        else:
            aspect = 1

        if 'cmap' not in kwargs:
            kwargs['cmap'] = self._cmap
        if 'norm' not in kwargs:
            kwargs['norm'] = self._norm

        im = plt.imshow(plot_block[_a, _b, _c].T,
                        origin="bottom",
                        extent=extent_val,
                        interpolation=interpolation,
                        aspect=aspect,
                        **kwargs)

        if extent_val[3] < 0:  # correct vertical orientation of plot
            plt.gca().invert_yaxis()  # if maximum vertical extent negative

        if show_faults:
            self.extract_fault_lines(cell_number, direction)

        if show_topo:
            if self.model.grid.topography is not None:
                if direction == 'z':
                    plt.contour(self.model.grid.topography.values_3D[:, :, 2],
                                extent=extent_val,
                                cmap='Greys')
                else:
                    self.plot_topography(cell_number=cell_number,
                                         direction=direction)

        if not show_data:
            import matplotlib.patches as mpatches
            patches = [
                mpatches.Patch(color=color, label=surface)
                for surface, color in self._color_lot.items()
            ]
            plt.legend(handles=patches,
                       bbox_to_anchor=(1.05, 1),
                       loc=2,
                       borderaxespad=0.)

        plt.xlabel(x)
        plt.ylabel(y)
        return plt.gcf()