Пример #1
0
def showPNGBad(grid):

    import matplotlib.pyplot as plt
    """Generate a simple image of the maze."""
    plt.figure(figsize=(10, 5))
    plt.imshow(grid, cmap=plt.cm.binary, interpolation="nearest")
    plt.xticks([]), plt.yticks([])
    plt.save_fig(format="png")
def plot_error_types():
    types_dict = json.load(open("/Users/shankari/cluster_ground_truth/smoothing/other_auto/smoothing_categories"))
    types_len_dict = {}
    for (category, cat_list) in types_dict.iteritems:
        types_len_dict[category] = len(cat_list)
    plt.xlabel("Number of sections")
    plt.ylabel("Category")
    plt.tight_layout()
    plt.save_fig("/tmp/smoothing_category_plot.png")
Пример #3
0
def plot_error_types():
    types_dict = json.load(
        open(
            "/Users/shankari/cluster_ground_truth/smoothing/other_auto/smoothing_categories"
        ))
    types_len_dict = {}
    for (category, cat_list) in types_dict.iteritems:
        types_len_dict[category] = len(cat_list)
    plt.xlabel("Number of sections")
    plt.ylabel("Category")
    plt.tight_layout()
    plt.save_fig("/tmp/smoothing_category_plot.png")
Пример #4
0
 def plot_history(self, history):
     N = np.arange(0, self.NUM_EPOCHS)
     plt.figure()
     plt.plot(N, history.history['loss'], label='train_loss')
     plt.plot(N, history.history['val_loss'], label='validation_loss')
     plt.plot(N, history.history['acc'], label='train_acc')
     plt.plot(N, history.history['val_acc'], label='validation_acc')
     plt.title('Training History')
     plt.xlabel('Epoch')
     plt.ylabel('Loss / Accuracy')
     plt.legend(loc='upper right')
     plt.save_fig(self.model_path + '/training_result.png')
Пример #5
0
def process_network(G, namespace):
    print 'Nodes:', len(G)
    print 'Edges:', G.number_of_edges()
    if namespace.clustering_coefficient:
        print 'Clustering Coefficient:', nx.average_clustering(G)
    if namespace.components:
        components = nx.connected_component_subgraphs(G)
        print 'Number of Components:', len(components)
        isles = [c for c in components if len(c) == 1]
        print 'Isles:', len(isles)
        print 'Largest Component Size:', len(components[0])
    else:
        components = None
    if namespace.cpl:
        if namespace.approximate_cpl:
            average_shortest_path_length = approximate_cpl
        else:
            print 'Using full slow CPL'
            average_shortest_path_length = nx.average_shortest_path_length
        if components is None:
            components = nx.connected_component_subgraphs(G)
        for i, g in enumerate(
                g for g in components
                if float(len(g)) / float(len(G)) > namespace.component_size):
            print 'CPL %d: (%f)' % (i, float(len(g)) / float(len(G)))
            print average_shortest_path_length(g)
    if namespace.assortativity:
        print 'Assortativity: NOT IMPLEMENTED.'
    if namespace.degree_distribution:
        hst = nx.degree_histogram(G)

        plt.subplot(121)
        plt.xscale('log')
        plt.yscale('log')
        plt.title("Degree Distribution")
        plt.ylabel("Occurrencies")
        plt.xlabel("Degree")
        plt.plot(range(len(hst)), hst, marker='+')

        plt.subplot(122)
        ccdf = pynetsym.mathutil.ccdf(hst)
        plt.xscale('log')
        plt.yscale('log')
        plt.title("CCDF Degree Distribution")
        plt.ylabel("$P(X>x)$")
        plt.xlabel("Degree")
        plt.plot(range(len(ccdf)), ccdf, color='red')

        if namespace.degree_distribution_out is None:
            plt.show()
        else:
            plt.save_fig(namespace.degree_distribution_out)
Пример #6
0
def classify(x_train, y_train, x_test, classifier):
    '''
    from rachel's pipeline_library
    '''
    model = classifier
    model.fit(x_train, y_train)
    if str(classifier).split('(')[0] == 'LinearSVC':
        predicted_scores = model.decision_function(x_test)
    else:
        predicted_scores = model.predict_proba(x_test)[:, 1]
    plt.hist(predicted_scores)
    plt.save_fig()
    return predicted_scores
Пример #7
0
def make_plot():
    with open('i2.dump', 'rb') as dump_file:
        polies = pickle.load(dump_file)
    plt = GeoPlot()
    plt.add_collection(
        PatchCollection(polies,
                        alpha=0.5,
                        edgecolor='black',
                        linewidth=0.001,
                        match_original=True))
    plt.set_xlim(-120, -40)
    plt.set_ylim(-15, 80)
    plt.save_fig('../figures/i2.pdf')
Пример #8
0
def make_heatmap(sim,names=None,outname=None):
    fig = plt.figure()
    ax = sns.heatmap(sim)
    if names is not None:
        ax.set_xticklabels(names,rotation=90)
        ax.set_yticklabels(names[::-1],rotation=0)
    plt.title('Cosine Similarity of Sound Features')
    plt.tight_layout()
    if outname:
        plt.save_fig(outname)
        return
    plt.show()
    return
Пример #9
0
def process_network(G, namespace):
    print 'Nodes:', len(G)
    print 'Edges:', G.number_of_edges()
    if namespace.clustering_coefficient:
        print 'Clustering Coefficient:', nx.average_clustering(G)
    if namespace.components:
        components = nx.connected_component_subgraphs(G)
        print 'Number of Components:', len(components)
        isles = [c for c in components if len(c) == 1]
        print 'Isles:', len(isles)
        print 'Largest Component Size:', len(components[0])
    else: components = None
    if namespace.cpl:
        if namespace.approximate_cpl:
            average_shortest_path_length = approximate_cpl
        else:
            print 'Using full slow CPL'
            average_shortest_path_length = nx.average_shortest_path_length
        if components is None:
            components = nx.connected_component_subgraphs(G)
        for i, g in enumerate(g for g in components if
                float(len(g))/float(len(G)) >
                namespace.component_size):
            print 'CPL %d: (%f)' % (i, float(len(g))/float(len(G)))
            print average_shortest_path_length(g)
    if namespace.assortativity:
        print 'Assortativity: NOT IMPLEMENTED.'
    if namespace.degree_distribution:
        hst = nx.degree_histogram(G)

        plt.subplot(121)
        plt.xscale('log')
        plt.yscale('log')
        plt.title("Degree Distribution")
        plt.ylabel("Occurrencies")
        plt.xlabel("Degree")
        plt.plot(range(len(hst)), hst, marker='+')

        plt.subplot(122)
        ccdf = pynetsym.mathutil.ccdf(hst)
        plt.xscale('log')
        plt.yscale('log')
        plt.title("CCDF Degree Distribution")
        plt.ylabel("$P(X>x)$")
        plt.xlabel("Degree")
        plt.plot(range(len(ccdf)), ccdf, color='red')

        if namespace.degree_distribution_out is None:
            plt.show()
        else:
            plt.save_fig(namespace.degree_distribution_out)
Пример #10
0
    def __init__(self):
        root = '.data'
        train_data = torchvision.datasets.CIFAR10(root,
                                                  train=True,
                                                  transform=None,
                                                  target_transform=None,
                                                  download=True)
        test_data = torchvision.datasets.CIFAR10(root,
                                                 train=False,
                                                 transform=None,
                                                 target_transform=None,
                                                 download=True)
        print(len(train_data), len(test_data))
        CLUSTERS_PATH = '.data/clusters.pkl'
        if not os.path.exists(CLUSTERS_PATH):
            # get random 5 pixels per image and stack them all up as rgb values to get half a million random pixels
            pluck_rgb = lambda x: torch.from_numpy(np.array(x)).view(
                32 * 32, 3)[torch.randperm(32 * 32)[:5], :]
            px = torch.cat([pluck_rgb(x) for x, y in train_data],
                           dim=0).float()
            print(px.size())

            ncluster = 512
            with torch.no_grad():
                C = ImageDataset.kmeans(px, ncluster, niter=8)
            with open(CLUSTERS_PATH, 'wb') as f:
                pickle.dump(C, f)
        else:
            with open(CLUSTERS_PATH, 'rb') as f:
                C = pickle.load(f)

        # encode the training examples with our codebook to visualize how much we've lost in the discretization
        n_samples = 16
        ncol = 8
        nrow = n_samples // ncol + 1
        plt.figure(figsize=(20, 10))
        for i in range(n_samples):
            # encode and decode random data
            x, y = train_data[np.random.randint(0, len(train_data))]
            xpt = torch.from_numpy(np.array(x)).float().view(32 * 32, 3)
            ix = ((xpt[:, None, :] - C[None, :, :])**2).sum(-1).argmin(
                1)  # cluster assignments for each pixel

            # these images should look normal ideally
            plt.subplot(nrow, ncol, i + 1)
            plt.plot(C[ix].view(32, 32, 3).numpy().astype(np.uint8))

        plt.save_fig('temp.png')
Пример #11
0
def plot_spectrum():
    spect_file=np.load(spect_file_path + '.npy')
    energy_centers=spect_file[::,0]
    spect_medians={}
    keys=spect_file[0,1].keys()
    print 'The keys: ', keys
    for key in keys:
        spect_medians[key]=np.array([spect_file[::,1][en][key][1] for en in range(len(energy_centers))])
    for key in keys:
        plt.plot(energy_centers,spect_medians[key],label=key)
    plt.xscale('log')
    plt.yscale('log')
    plt.ylim([1e-8,1e-4]);
    plt.ylabel('$E^2 dN / dE$ [ GeV / cm$^2$ / s / sr]',fontsize=16)
    plt.xlabel('E [ GeV ]',fontsize=16)
    plt.legend()
    plt.save_fig(b.plots_dir + spect_plot_tag + '.pdf')
Пример #12
0
def hists(files,legend,**kwargs): #plot histogram of RSSIs from each file
    fig, ax=plt.subplots()
    print('avgs:')
    cmap=plt.cm.get_cmap('tab20')
    i=0
    k1={**kwargs}
    k1.pop('adver',None)
    for f in files:
        r=rssis(f,**kwargs)
        print(f+':'+str(np.mean(r)))
        ax.hist(r,label=' '+f,color=cmap(i/20),bins=np.max(r)-np.min(r)+1,**k1)
        i+=1
    if legend:
        ax.legend()
    ax.set_xlabel('RSSIs')
    plt.show()
    plt.save_fig(DA_+'tmp.png')
Пример #13
0
def Multi_KNN_Class(X,
                    y,
                    test_s=0.1,
                    neighbors=[1, 2, 3],
                    p_val=[1, 2],
                    leaf=[30],
                    iterations=20,
                    fig_s=(15, 9),
                    path=os.getcwd(),
                    plot=False,
                    verbose=True):
    """test out all combinations of hyperparameters to find the best model configuration. Returns statistics for mean and standard
          deviation of accuracy over the amount of iterations for each hyperparameter settings."""
    mu_sigma_list = []
    for s in list(set(itertools.product(neighbors, p_val, leaf))):
        i, j, k = s
        acc_list = []
        knn = KNeighborsClassifier(n_neighbors=i, p=j, leaf_size=k)
        for r in range(iterations):
            X_train, X_val, y_train, y_val = train_test_split(X,
                                                              y,
                                                              test_size=test_s)
            knn.fit(X_train, y_train)
            acc = knn.score(X_val, y_val)
            acc_list.append(acc)

        mu = np.mean(acc_list)
        sigma = np.std(acc_list)
        mu_sigma_list.append(('{}_NN__p_{}__leafsize_{}'.format(i, j,
                                                                k), mu, sigma))
        if verbose:
            print('{}_NN__p_{}__leafsize_{}'.format(i, j, k), mu, sigma)

        if plot:
            x_axis = list(range(len(acc_list)))
            plt.figure(figsize=fig_s)
            text = 'Accuracy: {}_NN__p_{}__leafsize_{}'.format(i, j, k)
            plt.title(text)
            plt.plot(x_axis, acc_list)
            plt.save_fig(path + '/{}.png'.format(text))
    return mu_sigma_list
Пример #14
0
def run_training(model, data, optimizer, loss_function, epochs, generator):
    t_loss = []
    e_loss = []
    for epoch in range(epochs):
        train_loss = train(model, data(0, val_split), optimizer, loss_function, epoch)
        eval_loss = evaluate(model, data(val_split, 1), loss_function, epoch)

        t_plot = np.asarray(t_loss.append(train_loss))
        e_plot = np.asarray(e_loss.append(eval_loss))
        t_plot = np.reshape(-1, t_plot.shape[-1])
        e_plot = np.reshape(-1, e_plot.shape[-1])

        iters = np.arange(0, t_plot.shape[0]*100, 100)
        plt.plot(t_plot, iters, 'r', e_plot, 'b', linewidth=2.0)
        plt.save_fig(params['plot_save_path'])
        plt.clf()

        if epoch%10 == 0:
            save_checkpoints(model, optim, epoch, params['save_dir'])
            #TODO: Setup a random spectrogram and save_path
            spec2wav(generator, model, spectrogram, save_path)
    times.append(frame_number)    
    if (zeros/nonzeros) <= proportion_dark:
        newfmf.add_frame(frame, timestamp)
        sumframe = sumframe + frame
        listynew.append(zeros/nonzeros)
        timesnew.append(frame_number)
    else:
        pass

    
newfmf.close()


#plot proportion of black pixels before and after
plt.plot(times, listy, color='b')
plt.plot(timesnew, listynew, color='r')
plt.save_fig(sys.argv[2] + '_zeros.svg')
plt.show()

#plot average intensity (vignette)
vignette = sumframe / len(timesnew)

plt.imshow(vignette)
plt.save_fig(sys.argv[2] + '_vignette.svg')
plt.show()

#Save vignette values
DataFrame(vignette).to_csv(sys.argv[2] + '_array.csv', sep=',')

print 'done.'
Пример #16
0
filenames = glob.glob(config.savedir + config.dataname + '*.hd5')
print(filenames)
dataset = functions.DaskArray_hd5loadDataset(filenames, verbose=config.verbose)
print('dataset loaded with keys:')
print(dataset.keys())
Y = dataset['Y']
data = []
for pipeline in dataset.keys():
    if pipeline in config.pipelines:
        data.append(dataset[pipeline])
        if config.visualize == True:
            for j in range(5):
                i = random.randint(0, dataset[pipeline].shape[0])
                plt.title(pipeline + str(Y[i].compute()))
                plt.plot(dataset[pipeline][i, :].compute())
                plt.save_fig('./figures/' + pipeline +
                             str(Y[i].compute() + '.png'))
X = functions.da.concatenate(data, axis=1)

######################################make NN ################################################################
scaler = StandardScaler(copy=True)
le = LabelEncoder()
enc = OneHotEncoder(sparse=False)
epochs = 20
functions.np.random.seed(0)
print(Y[0:10].compute())
encY = le.fit_transform(Y.ravel())
dummy_y = enc.fit_transform(encY.reshape(-1, 1))

print(dummy_y)
print('scaling X')
if config.scaleX == True:
Пример #17
0
 def plot_the_Graph(self,graph,filename):
     nx.draw(graph)
     write_path=self.basic_path[5]
     plt.save_fig(write_path+filename)
Пример #18
0
def save_fig(fig_id, tight_layout=True, fig_extension="png", resolution=300):
    path = os.path.join(IMGS_DIR, fig_id + "." + fig_extension)
    print("Salving figure", fig_id)
    if tight_layout:
        plt.tight_layout()
    plt.save_fig(path, format=fig_extension, dpi=resolution)
Пример #19
0
def plot_video_timelines(
    video_rows,
    show_segment_boundaries=False,
    segment_color_map={ 
        'commercial': 'LightGray', 'non-commercial': 'LightCoral'
    },
    interval_label_color_map={},
    discrete_label_shape_map={},
    max_length=None, # timedelta for video length
    out_file=None,   # save to a file (e.g., a pdf)
    show_legend=True,
    min_y_margin=None
):
    fig = plt.figure()
    fig.set_size_inches(14, 2 * len(video_rows))
    ax = fig.add_subplot(111)
    
    # Plot grid for x axis
    ax.grid(color='Grey', which='major', linestyle='-', linewidth=1, axis='x')
    ax.grid(color='LightGrey', which='minor', linestyle='--', linewidth=1, axis='x')
    
    # Plot grid separating the shows on the y axis
    ax.grid(color='LightGrey', which='minor', linestyle='--', linewidth=1, axis='y')

    # Do not add labels more than once so they appear only once in the
    # legend
    defined_labels = set()
    
    y_labels = []
    for i, row in enumerate(video_rows):
        y_labels.append('[{} {}]\n{}\n(id: {})'.format(row.channel, row.date, row.show, row.id))
        
        # Plot segments
        for segment_info in row.segments:
            height = (0.05 + 0.95 * segment_info.display_value) / 2.1 # Use a little less 
                                                                   # than half of the gap
            width = segment_info.end_time.total_seconds() - segment_info.start_time.total_seconds()
            rect = Rectangle(
                (
                    segment_info.start_time.total_seconds(),
                    i
                ),
                width,
                height,
                fc=segment_color_map[segment_info.display_label],
                label=segment_info.display_label if segment_info.display_label not in defined_labels else None
            )
            defined_labels.add(segment_info.display_label)
            ax.add_patch(rect)
        
        # Plot shot boundaries
        if show_segment_boundaries:
            for segment_info in row.segments.values():
                segment_end = segment_info.end_time.total_seconds()
                plt.plot(
                    (segment_end, segment_end),
                    (i, i + 0.25),
                    'Black',
                    linewidth=1
                )
            
        vert_ofs = 0.05
        
        # Plot interval labels
        for label, intervals in row.interval_labels.items():
            for interval in intervals:
                plt.plot(
                    [td.total_seconds() for td in interval], 
                    (i - vert_ofs, i- vert_ofs), 
                    interval_label_color_map[label], 
                    linewidth=2,
                    label=label if label not in defined_labels else None
                )
                defined_labels.add(label)
            vert_ofs += 0.05
            
        # Plot discrete labels
        for label, offsets in row.discrete_labels.items():
            plt.scatter(
                [td.total_seconds() for td in offsets], 
                [i - vert_ofs] * len(offsets),
                s=2,
                c='Black',
                marker=discrete_label_shape_map[label],
                label=label if label not in defined_labels else None
            )
            defined_labels.add(label)
            vert_ofs += 0.05
            
    ax.set_yticks([0.5 + i for i in range(len(video_rows))], minor=True)
    
    # TODO: this is a hack to fix the margins
    if min_y_margin is not None:
        ax.text(-min_y_margin, 0, '.')
        
    plt.yticks(range(len(video_rows)), y_labels)
    plt.ylim([-0.5, len(video_rows) - 0.5])
    
    if max_length is None:
        max_length = max([x.length.total_seconds() for x in video_rows])
    else:
        max_length = max_length.total_seconds()
    plt.xlim([0, int(max_length)])
    
    # Minor ticks every 15 min
    ax.set_xticks(range(0, int(max_length), 900), minor=True)
    
    # Major ticks every hour
    plt.xticks(range(0, int(max_length), 3600))
    
    plt.xlabel('video time (s)')
    if show_legend:
        plt.legend(loc=0, frameon=True, edgecolor='Black', facecolor='White', framealpha=0.7)
    if out_file is not None:
        plt.save_fig(out_file)
    plt.show()
    
Пример #20
0
def plot_bird_label_scores(automated_df, human_df, save_fig=False):
    """
    Function to visualize automated and human annotation scores across an audio
    clip.

    Args:
        automated_df (Dataframe)
            - Dataframe of automated labels for one clip

        human_df (Dataframe)
            - Dataframe of human labels for one clip.

        plot_fig (boolean)
            - Whether or not the efficiency statistics should be displayed.

        save_fig (boolean)
            - Whether or not the plot should be saved within a file.

    Returns:
        Dataframe with statistics comparing the automated and human labeling.
    """
    duration = automated_df["CLIP LENGTH"].to_list()[0]
    SAMPLE_RATE = automated_df["SAMPLE RATE"].to_list()[0]
    # Initializing two arrays that will represent the
    # labels with respect to the audio clip
    # print(SIGNAL.shape)
    human_arr = np.zeros((int(SAMPLE_RATE * duration), ))
    bot_arr = np.zeros((int(SAMPLE_RATE * duration), ))

    folder_name = automated_df["FOLDER"].to_list()[0]
    clip_name = automated_df["IN FILE"].to_list()[0]
    # Placing 1s wherever the au
    for row in automated_df.index:
        minval = int(round(automated_df["OFFSET"][row] * SAMPLE_RATE, 0))
        maxval = int(
            round(
                (automated_df["OFFSET"][row] + automated_df["DURATION"][row]) *
                SAMPLE_RATE, 0))
        bot_arr[minval:maxval] = 1
    for row in human_df.index:
        minval = int(round(human_df["OFFSET"][row] * SAMPLE_RATE, 0))
        maxval = int(
            round((human_df["OFFSET"][row] + human_df["DURATION"][row]) *
                  SAMPLE_RATE, 0))
        human_arr[minval:maxval] = 1

    human_arr_flipped = 1 - human_arr
    bot_arr_flipped = 1 - bot_arr

    true_positive_arr = human_arr * bot_arr
    false_negative_arr = human_arr * bot_arr_flipped
    false_positive_arr = human_arr_flipped * bot_arr
    true_negative_arr = human_arr_flipped * bot_arr_flipped
    IoU_arr = human_arr + bot_arr
    IoU_arr[IoU_arr == 2] = 1

    plt.figure(figsize=(22, 10))
    plt.subplot(7, 1, 1)
    plt.plot(human_arr)
    plt.title("Ground Truth for " + clip_name)
    plt.subplot(7, 1, 2)
    plt.plot(bot_arr)
    plt.title("Automated Label for " + clip_name)

    # Visualizing True Positives for the Automated Labeling
    plt.subplot(7, 1, 3)
    plt.plot(true_positive_arr)
    plt.title("True Positive for " + clip_name)

    # Visualizing False Negatives for the Automated Labeling
    plt.subplot(7, 1, 4)
    plt.plot(false_negative_arr)
    plt.title("False Negative for " + clip_name)

    plt.subplot(7, 1, 5)
    plt.plot(false_positive_arr)
    plt.title("False Positive for " + clip_name)

    plt.subplot(7, 1, 6)
    plt.plot(true_negative_arr)
    plt.title("True Negative for " + clip_name)

    plt.subplot(7, 1, 7)
    plt.plot(IoU_arr)
    plt.title("Union for " + clip_name)

    plt.tight_layout()
    if save_fig:
        x = clip_name.split(".")
        clip_name = x[0]
        plt.save_fig(clip_name + "_label_plot.png")
Пример #21
0
def plot(matrix, weights=None, title="Prediction Matrix", save_plot=False):
    if save_plot and not os.path.isdir("plots"): os.makedirs("plots")

    if len(matrix[0]) == 3:  # if 1D inputs, excluding bias and ys
        fig, ax = plt.subplots()
        ax.set_title(title)
        ax.set_xlabel("i1")
        ax.set_ylabel("Classifications")

        if weights != None:
            y_min = -0.1
            y_max = 1.1
            x_min = 0.0
            x_max = 1.1
            y_res = 0.001
            x_res = 0.001
            ys = np.arange(y_min, y_max, y_res)
            xs = np.arange(x_min, x_max, x_res)
            zs = []
            for cur_y in np.arange(y_min, y_max, y_res):
                for cur_x in np.arange(x_min, x_max, x_res):
                    zs.append(predict([1.0, cur_x], weights))
            xs, ys = np.meshgrid(xs, ys)
            zs = np.array(zs)
            zs = zs.reshape(xs.shape)
            cp = plt.contourf(xs,
                              ys,
                              zs,
                              levels=[-1, -0.0001, 0, 1],
                              colors=('b', 'r'),
                              alpha=0.1)

        c1_data = [[], []]
        c0_data = [[], []]

        for i in range(len(matrix)):
            cur_i1 = matrix[i][1]
            cur_y = matrix[i][-1]

            if cur_y == 1:
                c1_data[0].append(cur_i1)
                c1_data[1].append(1.0)
            else:
                c0_data[0].append(cur_i1)
                c0_data[1].append(0.0)

        plt.xticks(np.arange(x_min, x_max, 0.1))
        plt.yticks(np.arange(y_min, y_max, 0.1))
        plt.xlim(0, 1.05)
        plt.ylim(-0.05, 1.05)

        c0s = plt.scatter(c0_data[0],
                          c0_data[1],
                          s=40.0,
                          c='r',
                          label='Class -1')
        c1s = plt.scatter(c1_data[0],
                          c1_data[1],
                          s=40.0,
                          c='b',
                          label='Class 1')

        plt.legend(fontsize=10, loc=1)

        if save_plot:
            plt.save_fig(os.path.join("plots", title + ".png"),
                         bbox_inches='tight',
                         dpi=200)
        else:
            plt.show()
        return

    if len(matrix[0]) == 4:  # if 2D inputs, excluding bias and ys
        fig, ax = plt.subplots()
        ax.set_title(title)
        ax.set_xlabel("i1")
        ax.set_ylabel("i2")

        if weights != None:
            map_min = 0.0
            map_max = 1.1
            y_res = 0.001
            x_res = 0.001
            ys = np.arange(map_min, map_max, y_res)
            xs = np.arange(map_min, map_max, x_res)
            zs = []
            for cur_y in np.arange(map_min, map_max, y_res):
                for cur_x in np.arange(map_min, map_max, x_res):
                    zs.append(predict([1.0, cur_x, cur_y], weights))
            xs, ys = np.meshgrid(xs, ys)
            zs = np.array(zs)
            zs = zs.reshape(xs.shape)
            cp = plt.contourf(xs,
                              ys,
                              zs,
                              levels=[-1, -0.0001, 0, 1],
                              colors=('b', 'r'),
                              alpha=0.1)

        c1_data = [[], []]
        c0_data = [[], []]
        for i in range(len(matrix)):
            cur_i1 = matrix[i][1]
            cur_i2 = matrix[i][2]
            cur_y = matrix[i][-1]
            if cur_y == 1:
                c1_data[0].append(cur_i1)
                c1_data[1].append(cur_i2)
            else:
                c0_data[0].append(cur_i1)
                c0_data[1].append(cur_i2)

        plt.xticks(np.arange(0.0, 1.1, 0.1))
        plt.yticks(np.arange(0.0, 1.1, 0.1))
        plt.xlim(0, 1.05)
        plt.ylim(0, 1.05)

        c0s = plt.scatter(c0_data[0],
                          c0_data[1],
                          s=40.0,
                          c='r',
                          label='Class -1')
        c1s = plt.scatter(c1_data[0],
                          c1_data[1],
                          s=40.0,
                          c='b',
                          label='Class 1')

        plt.legend(fontsize=10, loc=1)

        if save_plot:
            plt.save_fig(os.path.join("plots", title + ".png"),
                         bbox_inches='tight',
                         dpi=200)
        else:
            plt.show()
        return

    print("Matrix dimensions not covered.")
Пример #22
0
    nonzeros = float(frame[frame >= threshold].size)
    listy.append(zeros / nonzeros)
    times.append(frame_number)
    if (zeros / nonzeros) <= proportion_dark:
        newfmf.add_frame(frame, timestamp)
        sumframe = sumframe + frame
        listynew.append(zeros / nonzeros)
        timesnew.append(frame_number)
    else:
        pass

newfmf.close()

#plot proportion of black pixels before and after
plt.plot(times, listy, color='b')
plt.plot(timesnew, listynew, color='r')
plt.save_fig(sys.argv[2] + '_zeros.svg')
plt.show()

#plot average intensity (vignette)
vignette = sumframe / len(timesnew)

plt.imshow(vignette)
plt.save_fig(sys.argv[2] + '_vignette.svg')
plt.show()

#Save vignette values
DataFrame(vignette).to_csv(sys.argv[2] + '_array.csv', sep=',')

print 'done.'
    train_sample = [resize(i, input_shape) for i in train_sample]
    train_sample = np.array(train_sample)
    train_labeled_numeric = [
        test_right_generator.class_indices[j] for j in train_label
    ]
    test_l = test_right_generator.n
    counts = 0
    acc = []
    try:
        model.save_weight('siamese.h5')
    except:
        pass
    for (i, j) in test_right_generator:
        test_tmp = np.zeros_like(train_sample)
        test_tmp[:] = i[0]
        scores = model.predict([train_sample, test_tmp])
        pred_y = train_labeled_numeric[scores.argmax()]
        acc.append(pred_y == int(j[0]))
        counts += 1
        print(counts)
        if counts >= test_right_generator.n:
            break
    import pandas as pd

    h = pd.DataFrame(hist.history)
    plt.figure()
    h.plot()
    plt.save_fig('siamese.png')
    acc = sum(acc) / len(acc)
    print(acc)
Пример #24
0
                else:
                    select_id = None

            if mc_info[event_no][2] < 60:
                continue

            print(
                "Mc Info: [event_id, particle_type, energy, isCC, bjorkeny, dir_x/y/z, time]\n",
                mc_info[event_no])
            #event: (1,11,13,18,31)
            #prediction: (1,11,13,18,3)
            prediction = encoder.predict(event.reshape((1, ) + event.shape))
            prediction = np.reshape(prediction,
                                    prediction.shape[1:])  #11,13,18,3
            xyz_event = np.add(event, xs_mean)
            xyz_event = np.sum(event.reshape(data.shape[1:]),
                               axis=-1)  #11,13,18

            titles = [
                "Summed over channel id", "Neuron 1", "Neuron 2", "Neuron 3"
            ]
            fig = make_4_plots(xyz_event, prediction[:, :, :, 0],
                               prediction[:, :, :, 1], prediction[:, :, :, 2],
                               n_bins[:-1], titles)
            plt.show(fig)
            plt.save_fig("fig" + str(event_id) + ".pdf")

else:
    print("ploty type unknow")
    raise ()
Пример #25
0
def save(f):
    plt.save_fig(f)
Пример #26
0
def plot_and_save(dataframe, save_as):
    plot_combined(dataframe)
    plt.save_fig(str(save_as), bbox_inches='tight')
    plt.clf()
Пример #27
0
# Import numpy and matplotlib.pyplot
import numpy as np
import matplotlib.pyplot as plt

# Generate two 1-D arrays: u, v
u = np.linspace(-2, 2, 41)
v = np.linspace(-1, 1, 21)

# Generate 2-D arrays from u and v: X, Y
X, Y = np.meshgrid(u, v)

# Compute Z based on X and Y
Z = np.sin(3 * np.sqrt(X**2 + Y**2))

# Display the resulting image with pcolor()
plt.pcolor(Z)
plt.show()

# Save the figure to 'sine_mesh.png'
plt.save_fig('sine_mesh.png')
Пример #28
0
    if args.decay_every == -1:
        args.decay_every = np.inf

    if args.train:
        demo = False
        get = False
    else:
        demo = True
        get = True
        args.add_epochs = 0

    im = began_train(start_epoch=args.start_epoch,
                     add_epochs=args.add_epochs,
                     batch_size=args.batch_size,
                     hidden_size=args.hidden_size,
                     gpu_id=args.gpuid,
                     demo=demo,
                     get=get,
                     save_every=args.save_every,
                     decay_every=args.decay_every,
                     batch_norm=args.batch_norm,
                     start_learn_rate=args.start_learn_rate)

    if not args.train:
        import matplotlib.pyplot as plt
        for n in range(8):
            im_to_save = im[n].reshape([800, 3])
            plt.plot(im_to_save)
            plt.save_fig(args.outdir + '/out_{}.jpg'.format(n))