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)
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()
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()
def plot_mapping(curr_ds, curr_ref, ds_ind, ref_ind): tsne = TSNE(n_iter=400, verbose=VERBOSE, random_state=69) tsne.fit(curr_ds) plt.figure() coords_ds = tsne.embedding_[:, :] coords_ds[:, 1] += 100 plt.scatter(coords_ds[:, 0], coords_ds[:, 1]) tsne.fit(curr_ref) coords_ref = tsne.embedding_[:, :] plt.scatter(coords_ref[:, 0], coords_ref[:, 1]) x_list, y_list = [], [] for r_i, c_i in zip(ds_ind, ref_ind): x_list.append(coords_ds[r_i, 0]) x_list.append(coords_ref[c_i, 0]) x_list.append(None) y_list.append(coords_ds[r_i, 1]) y_list.append(coords_ref[c_i, 1]) y_list.append(None) plt.plot(x_list, y_list, 'b-', alpha=0.3) plt.show()
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
# Prediction ref_final, pre_final, prob_recontructed, ref_reconstructed, mask_no_considered_, mask_ts, time_ts = prediction( model, image_array, image_ref, final_mask, mask_ts_, patch_size, area) # Metrics cm = confusion_matrix(ref_final, pre_final) metrics = compute_metrics(ref_final, pre_final) print('Confusion matrix \n', cm) print('Accuracy: ', metrics[0]) print('F1score: ', metrics[1]) print('Recall: ', metrics[2]) print('Precision: ', metrics[3]) # Alarm area total = (cm[1, 1] + cm[0, 1]) / len(ref_final) * 100 print('Area to be analyzed', total) print('training time', end_training) print('test time', time_ts) #%% Show the results # prediction of the whole image fig1 = plt.figure('whole prediction') plt.imshow(prob_recontructed) plt.imsave('whole_pred.jpg', prob_recontructed) # Show the test tiles fig2 = plt.figure('prediction of test set') plt.imshow(prob_recontructed * mask_ts) plt.imsave('pred_test_set.jpg', prob_recontructed * mask_ts) plt.show()
warnings.filterwarnings('ignore') # Define the input to be tested test_idx = 89 xi = test_x[[test_idx]] yi = test_y[test_idx] # Create a DeepExplain context. # IMPORTANT: the network must be created within this context. # In this example we have trained the network before, so we call `model(X)` to # recreate the network graph using the same weights that have been already trained. with DeepExplain(session=sess) as de: logits = model(X) # We run `explain()` several time to compare different attribution methods attributions = {'Epsilon-LRP': de.explain('elrp', logits * yi, X, xi)} print('Done') # Plot attributions n_cols = len(attributions) + 1 fig, axes = plt.subplots(nrows=1, ncols=n_cols, figsize=(3 * n_cols, 3)) plot(xi.reshape(28, 28), cmap='Greys', axis=axes[0]).set_title('Original') print(yi) for i in range(len(yi)): if yi[i] == float(1): print("test output : ", i) for i, method_name in enumerate(sorted(attributions.keys())): plt.show( plot(attributions[method_name].reshape(28, 28), xi=xi.reshape(28, 28), axis=axes[1 + i]).set_title(method_name))