Ejemplo n.º 1
0
def plot_histograms(df):
    """ plots the histograms of the columns of the input dataframe

        Args:
            df(pandas.DataFrame): input data
    """
    cols = df.columns
    ncols = len(cols)
    if ncols % 15 == 0:
        nfigs = int(ncols / 15)
    else:
        nfigs = int(np.floor(ncols / 15) + 1)
        rem = int(ncols % 15)
    t = 0
    for n in range(nfigs):
        if n < nfigs:
            f, axes = plt.subplots(5, 3, figsize=(13, 10))
            f.suptitle("Histograms. Red = diseased, green = healthy",
                       fontsize=16)

            k = 0
            for j in range(3):
                for i in range(5):
                    c = cols[i + k + t - 1]
                    axes[i, j].hist(df[c][df["num"] == 0],
                                    bins=100,
                                    color="green",
                                    alpha=.5)
                    axes[i, j].hist(df[c][df["num"] == 1],
                                    bins=100,
                                    color="red",
                                    alpha=.5)
                    axes[i, j].set_xlabel(c)
                k = k + 5
        else:
            f, axes = plt.subplots(rem, 1, figsize=(6, 10))
            f.suptitle("Histograms. Red = diseased, green = healthy",
                       fontsize=16)
            for i in range(rem):
                c = cols[-rem:][i]
                axes[i].hist(df[c], bins=100)
                axes[i].set_xlabel(c)

        t = t + 10
        plt.subplots_adjust(hspace=.5, right=.95, left=.05)

    # plt.tight_layout()
    plt.show()
Ejemplo n.º 2
0
def box_plots(df, metadata):
    """ displays boxplots for each column in the input dataframe 

        Args:
            df (pandas.DataFrame): input dataframe
    """

    scaler = MinMaxScaler()

    f, ax = plt.subplots(1, 1, figsize=(12, 8))
    k = 1
    names = []
    for i, col in enumerate(df.columns):
        if col != "num":
            names.append("  " + col)
            names.append("")
            c = df[col][(df[col] > 0) & (df['num'] == 1)].values.reshape(-1, 1)
            c = scaler.fit_transform(c)
            p1 = ax.boxplot(c[~np.isnan(c)],
                            showfliers=False,
                            positions=[k - .4],
                            patch_artist=True,
                            boxprops=dict(facecolor='red',
                                          color='red',
                                          alpha=.5),
                            whiskerprops=dict(color='red'),
                            medianprops=dict(color='black'),
                            widths=(.6))
            # ax.set_yticklabels("")

            c = df[col][(df[col] > 0) & (df['num'] == 0)].values.reshape(-1, 1)
            c = scaler.fit_transform(c)
            p2 = ax.boxplot(c[~np.isnan(c)],
                            showfliers=False,
                            positions=[k + .4],
                            patch_artist=True,
                            boxprops=dict(facecolor='green',
                                          color='green',
                                          alpha=.5),
                            whiskerprops=dict(color='green'),
                            medianprops=dict(color='black'),
                            widths=(.6))
            ticks = list(ax.get_xticks())
            ticks[-1] = np.nan
            ax.set_xticks(ticks)
            # ax.set_yticklabels(["", col])
            k += 3

    ax.set_xticklabels(names)
    ax.tick_params(width=0, length=0)
    plt.show(block=False)
Ejemplo n.º 3
0
 def __init__(self, images, *, axis='x'):
     self.ts = set('z')
     self.fig, self.ax = plt.subplots()
     self.images = tuple(image(wrap_1d_into_3d(data, shape), shape, full_lengths)
                         for (shape, full_lengths), data in images)
     self.image_number = 0
     self.axis = axis
     shape = self.images[self.image_number].shape
     self.pos = [n // 2 for n in shape] # start off in the middle along each axis
     self.ax.imshow(self.slice_through_data())
     self.aximage = self.ax.images[0]
     self.fig.canvas.mpl_connect('key_press_event', self.process_key)
     self.update()
     plt.show()
Ejemplo n.º 4
0
def coverage_ratio(df, plot=False):
    """ calculates the coverage ratio for each column
        in the input dataframe 'df' 

        Args:
            df (pandas.DataFrame)
            plot (bool): if True displays a column plot of the coverage ratios

        Returns:
            dict: dictionary with column names as keys and coverage
                  ratios as values
    """

    cov = {}
    for i, col in enumerate(df.columns):
        cov[col] = sum(df[col].notna()) / len(df)

    if plot is True:
        f, ax = plt.subplots(figsize=(12, 6))
        ax.bar(cov.keys(), cov.values())
        plt.show(block=True)

    return cov
Ejemplo n.º 5
0
def show_kd_plot(image_paths,
                 file_name,
                 filter_=False,
                 output=None,
                 custom_std=False,
                 iqr_choice=False):
    images_collection = load_batch_images(image_paths, file_name)
    original_imag = images_collection[0]
    gan_imag = images_collection[1]
    kernel_arr, local_min_max, X_plot = kernel_density(original_imag,
                                                       gan_imag,
                                                       file_name,
                                                       bandwidth=0.02,
                                                       op="min",
                                                       filter_=filter_,
                                                       custom_std=custom_std,
                                                       iqr_choice=iqr_choice)
    gan_imag2 = images_collection[2]
    kernel_arr2, local_min_max2, X_plot2 = kernel_density(
        original_imag,
        gan_imag2,
        file_name,
        bandwidth=0.02,
        op="min",
        filter_=filter_,
        custom_std=custom_std,
        iqr_choice=iqr_choice)
    gan_imag3 = images_collection[3]
    kernel_arr3, local_min_max3, X_plot3 = kernel_density(
        original_imag,
        gan_imag3,
        file_name,
        bandwidth=0.02,
        op="min",
        filter_=filter_,
        custom_std=custom_std,
        iqr_choice=iqr_choice)
    nrows = len(file_name)
    ncols = 5
    fig, axes = plt.subplots(nrows=nrows,
                             ncols=ncols,
                             figsize=(3 * ncols, 3 * nrows))
    axes = axes.reshape(nrows, ncols)
    y_min, y_max = 0, 10
    for i in range(nrows):
        # a4 = attributions_dl_base_line[i]
        axes[i, 0].imshow(original_imag[i])  # .set_title('Original')
        axes[i, 1].imshow(gan_imag[i])  # .set_title('gan-1')
        axes[i, 2].imshow(gan_imag2[i])  # .set_title('gan-2')
        axes[i, 3].imshow(gan_imag3[i])  # .set_title('gan-3')
        log_dens = kernel_arr[i]
        log_dens2 = kernel_arr2[i]
        log_dens3 = kernel_arr3[i]
        axes[i, 4].fill(X_plot[:, 0],
                        np.exp(log_dens),
                        fc='green',
                        alpha=0.5,
                        label="gan-1")
        axes[i, 4].fill(X_plot2[:, 0],
                        np.exp(log_dens2),
                        fc='blue',
                        alpha=0.3,
                        label="gan-2")
        axes[i, 4].fill(X_plot3[:, 0],
                        np.exp(log_dens3),
                        fc='red',
                        alpha=0.5,
                        label="gan-3")
        axes[i, 4].axvline(0, y_min, y_max, c="gray", alpha=0.1)
        # axes[i, 0].text(0, 5, str(i))
        axes[i, 4].set_ylim([y_min, y_max])

    if output:
        fig.savefig(output)
Ejemplo n.º 6
0
# Save 10 results with error rate lower than threshold
threshold = 300
predictions = np.where(predictions > 0.5, 1, 0)
masks = np.where(masks > 0.5, 1, 0)
good_prediction = np.zeros([predictions.shape[0], 1], np.uint8)
id_m = 0
for idx in range(predictions.shape[0]):
    esti_sample = predictions[idx]
    true_sample = masks[idx]
    esti_sample = esti_sample.reshape(
        esti_sample.shape[0] * esti_sample.shape[1] * esti_sample.shape[2], 1)
    true_sample = true_sample.reshape(
        true_sample.shape[0] * true_sample.shape[1] * true_sample.shape[2], 1)
    er = 0
    for idy in range(true_sample.shape[0]):
        if esti_sample[idy] != true_sample[idy]:
            er = er + 1
    if er < threshold:
        good_prediction[id_m] = idx
        id_m += 1

fig, ax = plt.subplots(10, 3, figsize=[15, 15])

for idx in range(10):
    ax[idx, 0].imshow(np.uint8(imgs[good_prediction[idx, 0]]))
    ax[idx, 1].imshow(np.squeeze(masks[good_prediction[idx, 0]]), cmap='gray')
    ax[idx, 2].imshow(np.squeeze(predictions[good_prediction[idx, 0]]),
                      cmap='gray')

plt.savefig(output_folder + 'sample_results.png')
import warnings
warnings.filterwarnings('ignore')

# Define the input to be tested
test_image_index = 17
test_image = test_x[[test_image_index]]
test_image_prediction = test_y[test_image_index]

with DeepExplain(session=sess) as de:
    log = model(X)
    attributions = {
        'Gradient * Input':
        de.explain('grad*input', log * test_image_prediction, X, test_image),
        'Epsilon-LRP':
        de.explain('elrp', log * test_image_prediction, X, test_image)
    }

# Plot attributions
n_cols = len(attributions) + 1
fig, axes = plt.subplots(nrows=1, ncols=n_cols, figsize=(3 * n_cols, 3))
plot(test_image.reshape(28, 28), cmap='Greys',
     axis=axes[0]).set_title('Original')
print(test_image_prediction)
for i in range(len(test_image_prediction)):
    if test_image_prediction[i] == float(1):
        print("Test output : ", i)
for i, method_name in enumerate(sorted(attributions.keys())):
    plt.savefig(
        plot(attributions[method_name].reshape(28, 28),
             xi=test_image.reshape(28, 28),
             axis=axes[1 + i]).set_title(method_name))
        dists[:, :, channel] = dist

    return dists

def show_each_channel(img, axes, row):
    w, h, channel = img.shape
    for c in range(channel):
        axes[row, c].imshow(img[:, :, c], cmap=cm.Greys_r)
    #     plt.show()
    # plt.close()

print(f'Number of patches: {len(patches_tr)}')
print(f'Number of patches expected: {len(patches_tr)*5}')
for i in range(len(patches_tr)):
    # (axis_seg, axis_bound, axis_dist, axis_color)
    fig1, axes = plt.subplots(nrows=4, ncols=5, figsize=(12, 9))
    img_aug = patches_tr[i]
    label_aug = patches_tr_ref[i]
    print('seg')
    axes[0, 0].set_ylabel('Segmentation')
    label_aug_h = tf.keras.utils.to_categorical(label_aug, number_class)
    print(label_aug_h.shape)
    show_each_channel(label_aug_h, axes, row=0)
    # All multitasking labels are saved in one-hot
    # Create labels for boundary
    print('bound')
    axes[1, 0].set_ylabel('Boundary')
    patches_bound_labels_h = get_boundary_label(label_aug_h)
    show_each_channel(patches_bound_labels_h, axes, row=1)
    # Create labels for distance
    print('dist')
if not os.path.exists(args.output_path):
    os.makedirs(args.output_path)

plt.imsave(os.path.join(args.output_path, 'pred_seg_reconstructed.jpeg'),
           img_reconstructed_rgb)

# Visualize inference per class
if args.use_multitasking:

    for i in range(len(patches_test)):
        print(f'Patch: {i}')
        # Plot predictions for each class and each task; Each row corresponds to a
        # class and has its predictions of each task
        fig1, axes = plt.subplots(nrows=args.num_classes,
                                  ncols=7,
                                  figsize=(15, 10))
        img = patches_test[i]
        img = (img * np.array([255, 255, 255])).astype(np.uint8)
        img_ref = patches_test_ref[i]
        img_ref_h = tf.keras.utils.to_categorical(img_ref, args.num_classes)
        bound_ref_h = get_boundary_label(img_ref_h)
        dist_ref_h = get_distance_label(img_ref_h)
        # Put the first plot as the patch to be observed on each row
        for n_class in range(args.num_classes):
            axes[n_class, 0].imshow(img)
            # Loop the columns to display each task prediction and reference
            # Remeber we are not displaying color preds here, since this task
            # do not use classes
            # Multiply by 2 cause its always pred and ref side by side
            for task in range(len(patches_pred) - 1):
        os.mkdir('./results/RBF')
    for i in range(10):
        id = i
        for tf in range(2):
            env = virl.Epidemic(problem_id=id, noisy=tf)
            states, rewards, actions = exec_policy(env,
                                                   rbf_func,
                                                   verbose=False)
            fig = get_fig(states, rewards)
            if tf:
                tf = 'True'
            else:
                tf = 'False'
            plt.savefig(
                dpi=300,
                fname='./results/RBF/problem_id={}_noisy={}.jpg'.format(
                    id, tf))
            print("\tproblem_id={} noisy={} Total rewards:{:.4f}".format(
                id, tf, sum(rewards)))
            plt.close()
fig, ax = plt.subplots(figsize=(8, 6))
for i in range(10):
    env = virl.Epidemic(stochastic=True)
    states, rewards, actions = exec_policy(env, rbf_func, verbose=False)
    ax.plot(np.array(states)[:, 1], label=f'draw {i}')
ax.set_xlabel('weeks since start of epidemic')
ax.set_ylabel('Number of Infectious persons')
ax.set_title('Simulation of 10 stochastic episodes with RBF policy')
ax.legend()
plt.savefig(dpi=300, fname='./results/RBF/stochastic.png')
plt.close()