コード例 #1
0
def plot_and_save_expanded_data_for_samples(gate_expander, model, data_input,
                                            sample_idxs_to_plot):
    for sample_idx in sample_idxs_to_plot:
        plotter = DataAndGatesPlotterDepthOne(model,
                                              np.concatenate(data_input.x_tr))
        plotter.plot_single_sample_with_gate(data_input.x_tr[sample_idx],
                                             data_input.idxs_tr[sample_idx],
                                             data_input.y_tr[sample_idx],
                                             plt.gca(),
                                             include_diagnostics=False)

        expanded_data = gate_expander.expanded_data_per_sample[sample_idx]
        if not (expanded_data.shape[0] == 0):
            print(expanded_data.shape)
            plotter.plot_single_sample_with_gate(
                expanded_data,
                data_input.idxs_tr[sample_idx],
                data_input.y_tr[sample_idx],
                plt.gca(),
                color='r',
                include_diagnostics=False)

        clusters = gate_expander.clusterers_per_sample[
            sample_idx].cluster_centers_
        plt.gca().scatter(clusters[:, 0], clusters[:, 1], color='b', s=2)
        plt.savefig('expanded_sample%d.png' % data_input.idxs_tr[sample_idx])
        plt.clf()
コード例 #2
0
def make_umap_plots_per_sample(model, data_input, sample_idxs_to_plot, plots_per_row=5, figlen=7, savename='plots_per_sample.png', background_data_to_plot=None, color='b', expanded_data_per_sample=None, sample_names_to_true_features=None, BALL=False):


    if len(sample_idxs_to_plot) == 0:
        print('idxs are empty!')
        return None
    vals_to_delete = []
    for i, idx in enumerate(sample_idxs_to_plot):
        if not (idx in data_input.sample_names_all):
            print('Sample %d not in training data' %idx)
            vals_to_delete.append(idx)
    for val in vals_to_delete:
        del sample_idxs_to_plot[sample_idxs_to_plot.index(val)]
    plotter = DataAndGatesPlotterDepthOne(model, [])
    idxs_in_data_input = [[1, data_input.idxs_tr.index(idx)] if idx in data_input.idxs_tr else [0, data_input.idxs_te.index(idx)] for idx in sample_idxs_to_plot]

    n_samples_to_plot =  len(sample_idxs_to_plot)

    evenly_divides = not(n_samples_to_plot % plots_per_row)
    n_rows = n_samples_to_plot//plots_per_row
    if not(evenly_divides):
        n_rows += 1
    
    print(n_rows, plots_per_row) 
    fig, axes = plt.subplots(n_rows, plots_per_row, figsize=((figlen) * plots_per_row, (figlen) * n_rows),sharex=True, sharey=True)

    #fig.suptitle('UMAP Embedding and Learned Gates per Sample')
    
    
        
    axes = [axes] if len(axes.shape) == 1 else axes

    if not (background_data_to_plot is None):
        for i in range(n_rows):
            for j in range(plots_per_row):
                axes[i][j].scatter(
                    background_data_to_plot[:, 0],
                    background_data_to_plot[:, 1],
                    c='lightgrey', s=1/100, alpha=.5 
                )                      

    axes[0][0].set_xlim(0, 1)
    axes[0][0].set_ylim(0, 1)
    fig.tight_layout(pad=1.3)
    row_start_idx = 0
    for i, row in enumerate(range(n_rows)):
        sample_row_idxs = sample_idxs_to_plot[row_start_idx: row_start_idx + plots_per_row]
        for j, sample_idx in enumerate(sample_row_idxs):
            cur_axis = axes[i][j]
            data_input_matching_idx = idxs_in_data_input[row_start_idx + j][1]
            if idxs_in_data_input[j][0]:
                sample = data_input.x_tr[data_input_matching_idx]
                label = data_input.y_tr[data_input_matching_idx]
            else:
                sample = data_input.x_te[data_input_matching_idx]
                label = data_input.y_te[data_input_matching_idx]
            name = sample_idxs_to_plot[row_start_idx + j]
            true_feature = None
            if sample_names_to_true_features:
                true_feature = sample_names_to_true_features[name]
            if not BALL:
                plotter.plot_single_sample_with_gate(
                    sample, name, label,
                    cur_axis, size=1, color='b',
                    true_feature=true_feature
                )
            else:
                plotter.plot_single_sample_with_gate(
                    sample, name, label,
                    cur_axis, size=1, color='b',
                    true_feature=true_feature,
                    BALL=True
                )
            if not (expanded_data_per_sample is None):
                expanded_data = expanded_data_per_sample[data_input_matching_idx]
                cur_axis.scatter(expanded_data[:, 0], expanded_data[:, 1], s=1, color='r')
        row_start_idx += plots_per_row

 
    plt.savefig(savename)