def plot_pr_curve_mmr(Metric, fin_list_graphs, out_comp_nm):

    n_divs = 10
    scores_list = [float(pred_complex[1]) for pred_complex in fin_list_graphs]
    #print(scores_list)
    min_score = min(scores_list)
    interval_len = (max(scores_list) - min_score) / float(n_divs)
    thresholds = [min_score + i * interval_len for i in range(n_divs)]

    precs = []
    recalls = []
    for thres in thresholds:
        # list of indices with scores greater than the threshold
        col_inds = [j for j, score in enumerate(scores_list) if score >= thres]
        prec_MMR, recall_MMR, f1_MMR, max_matching_edges = f1_mmr(
            Metric[:, col_inds])

        precs.append(prec_MMR)
        recalls.append(recall_MMR)

    fig = plt_figure()
    plt_plot(recalls, precs, '.-')
    plt_ylabel("Precision")
    plt_xlabel("Recall")
    plt_title("PR curve for MMR measure")
    plt_savefig(out_comp_nm + '_pr_mmr.png')
    plt_close(fig)
def plot_size_dists(complex_graphs, test_complex_graphs, out_comp_nm):
    train_sizes = [len(comp) for comp in complex_graphs]
    test_sizes = [len(comp) for comp in test_complex_graphs]
    try:
        fig = plt_figure()
        sns_distplot(train_sizes, hist=False, label="train")
        sns_distplot(test_sizes, hist=False, label="test")
        plt_savefig(out_comp_nm + '_size_dists_train_test.png')
        plt_close(fig)
    except:
        print("Cannot plot")
Exemple #3
0
    def save_figs(self, prefix):
        for name, plotter in self.plotters.items():
            savename = prefix + '_' + name + '.pdf'
            savepath = self.fig_path / savename
            fig = plotter.get_figure()
            fig.savefig(str(savepath), format='pdf')

            if self.__saved_figures.get(name) is None:
                self.__saved_figures[name] = [str(savepath)]
            else:
                self.__saved_figures[name] += [str(savepath)]

            plt_close('all')
def plot_f1_scores(best_matches, out_comp_nm, suffix, prefix):
    # plot histogram of F1 scores
    max_f1_scores = [match[2] for match in best_matches]

    avged_f1_score = sum(max_f1_scores) / float(len(max_f1_scores))

    f1_score_counts = Counter()

    for score in max_f1_scores:
        f1_score_counts[score] += 1

    n_perfect_matches = 0
    if 1 in f1_score_counts:
        n_perfect_matches = f1_score_counts[1]

    n_no_matches = 0
    if 0 in f1_score_counts:
        n_no_matches = f1_score_counts[0]

    if len(set(max_f1_scores)) > 1:
        fig = plt_figure(figsize=(12, 10))
        plt_rcParams["font.family"] = "Times New Roman"
        plt_rcParams["font.size"] = 16
        sns_distplot(max_f1_scores, hist=True, kde=False)
        plt_xlabel("F1 score")
        plt_ylabel('Frequency')
        plt_title(prefix + "F1 score distribution")
        plt_savefig(out_comp_nm + suffix + '_f1_scores_histogram.eps',
                    dpi=350,
                    format='eps')
        plt_savefig(out_comp_nm + suffix + '_f1_scores_histogram.tiff',
                    dpi=350,
                    format='tiff')
        plt_savefig(out_comp_nm + suffix + '_f1_scores_histogram.jpg',
                    dpi=350,
                    format='jpg')

        plt_close(fig)

    with open(out_comp_nm + '_metrics.out', "a") as fid:
        print(prefix, file=fid)
        print("Averaged F1 score = %.3f" % avged_f1_score, file=fid)
        print("No. of perfectly recalled matches = %d" % n_perfect_matches,
              file=fid)
        print("No. of matches not recalled at all = %d" % n_no_matches,
              file=fid)
    return avged_f1_score
    def __init__(self, parent=None):
        img_size = 35 # square dimension of output image
        data = np.mgrid[0:255:255j, 0:255][0]

        removed_cmaps = ['Accent', 'Paired', 'Dark', 'Pastel', 'tab', 'Set', 'flag', '_r', 'gray', 'Greys']
        kept_cmaps = [i for i in mpl_cmaps() if not any([(j in i) for j in removed_cmaps])]

        root = QFileInfo(__file__).absolutePath()
        gradient_icons = [i.split('.')[0] for i in os.listdir(root+"/../../styles/assets/icons/gradients/")]

        for cmap_name in kept_cmaps:
            if cmap_name not in gradient_icons:
                cmap = cm.get_cmap(cmap_name)
                sizes = np.shape(data)
                fig = plt_figure(figsize=(1,1))
                #fig.set_size_inches(1. * sizes[0] / sizes[1], 1, forward=False)
                ax = plt_Axes(fig, [0.,0.,1.,1.])
                ax.set_axis_off()
                fig.add_axes(ax)
                ax.imshow(data[::-1], cmap)
                plt_savefig("../../styles/assets/icons/gradients/{}.png".format(cmap_name), dpi=img_size)
                plt_close()
def plot_size_dists(known_complex_nodes_list, fin_list_graphs, sizes_orig,
                    out_comp_nm):
    sizes_known = [len(comp) for comp in known_complex_nodes_list]
    # Size distributions
    sizes_new = [len(comp[0]) for comp in fin_list_graphs]
    fig = plt_figure(figsize=(8, 6), dpi=96)
    plt_rc('font', size=14)

    if len(set(sizes_known)) <= 1:
        return
    sns_distplot(sizes_known, hist=False, label="known")
    if len(set(sizes_orig)) <= 1:
        return
    sns_distplot(sizes_orig, hist=False, label="predicted")
    if len(set(sizes_new)) <= 1:
        return
    sns_distplot(sizes_new, hist=False, label="predicted_known_prots")
    plt_ylabel("Probability density")
    plt_xlabel("Complex Size (number of proteins)")
    plt_title("Complex size distributions")
    plt_savefig(out_comp_nm + '_size_dists_known_pred.png')
    plt_close(fig)
o_f = args.out_file_suffix
df = pd.DataFrame(all_sets)
df = df.set_index(par)
df = df.sort_index()
df.to_csv('./' + args.direct + '/' + par + o_f + '_metrics.csv')

if args.direct == 'humap':
    df = df.drop('2stage clustering')
df[list(df.columns)] = df[list(df.columns)].astype(float)
print(df.idxmax())
#print(df)
fig = plt_figure()
df.plot(subplots=True, style='-.', layout=(7, 2), figsize=(15, 15))
plt_savefig('./' + args.direct + '/' + par + o_f + '_metrics.png')
plt_close(fig)

# Plot PR curves for MMR and Qi et al based metrics
lists = sorted(zip(*[recalls_MMR, precisions_MMR]))
recalls_MMR, precisions_MMR = list(zip(*lists))

lists = sorted(zip(*[recalls_qi, precisions_qi]))
recalls_qi, precisions_qi = list(zip(*lists))
fig = plt_figure()
plt_plot(recalls_MMR, precisions_MMR, 'k.-')
plt_plot(recalls_qi, precisions_qi, 'b.-')
plt_xlabel('Recall')
plt_ylabel('Precision')
plt_title('PR curve over ' + par)
plt_legend(['MMR', 'Qi'])
plt_savefig('./' + args.direct + '/pr_' + par + o_f + '_MMR_qi.png')
def r6_dnn_image_display(target_dirname, dnn_image_obj=None, show_fig=False):
    LOGGER.info('{}: r6: Turning upsampled envelope into image...'.format(target_dirname))
    if dnn_image_obj is None:
        dnn_image_obj = loadmat(os_path_join(target_dirname, DNN_IMAGE_FNAME))

    beam_position_x_up = dnn_image_obj['beam_position_x_up']
    depth = dnn_image_obj['depth']
    envUp_dB = dnn_image_obj['envUp_dB']
    env_up = dnn_image_obj['envUp']

    LOGGER.debug('{}: r6: Finished loading vars'.format(target_dirname))
    x = np_squeeze(beam_position_x_up) # beam_position_x_up
    y = np_squeeze(depth) # depth

    LOGGER.debug('{}: r6: Finished squeezing x, y'.format(target_dirname))
    fig, ax = plt_subplots()
    LOGGER.debug('{}: r6: Finished plt.figure'.format(target_dirname))
    image = ax.imshow(envUp_dB, vmin=-60, vmax=0, cmap='gray', aspect='auto', extent=[x[0]*1000, x[-1]*1000, y[-1]*1000, y[0]*1000])
    ax.set_aspect('equal')
    LOGGER.debug('{}: r6: Finished plt.imshow'.format(target_dirname))
    fig.colorbar(image)
    LOGGER.debug('{}: r6: Finished plt.colorbar'.format(target_dirname))
    # plt_xlabel('lateral (mm)', fontsize=FONT_SIZE)
    ax.set_xlabel('lateral (mm)', fontsize=FONT_SIZE)
    # plt_ylabel('axial (mm)', fontsize=FONT_SIZE)
    ax.set_ylabel('axial (mm)', fontsize=FONT_SIZE)
    LOGGER.debug('{}: r6: Finished plt.xlabel/ylabel'.format(target_dirname))

    # if show_fig is True:
    # plt_show(block=False)

    # Save image to file
    dnn_image_path = os_path_join(target_dirname, DNN_IMAGE_SAVE_FNAME)
    fig.savefig(dnn_image_path)
    plt_close(fig)

    LOGGER.debug('{}: r6: Finished saving figure'.format(target_dirname))
    # scan_battery_dirname = os_path_dirname(target_dirname)
    # process_scripts_dirpath = os_path_join(scan_battery_dirname, PROCESS_SCRIPTS_DIRNAME)

    # circle_radius = load_single_value(process_scripts_dirpath, CIRCLE_RADIUS_FNAME)
    # circle_coords_x = load_single_value(process_scripts_dirpath, CIRCLE_COORDS_X_FNAME)
    # circle_coords_y = load_single_value(process_scripts_dirpath, CIRCLE_COORDS_Y_FNAME)

    # xx, yy = np_meshgrid(x, y)
    # mask_in = get_circular_mask(xx, yy, (circle_coords_x, circle_coords_y), circle_radius)
    #
    # # mask_in
    # # create rectangular region outside lesion
    # box_xmin_right = load_single_value(process_scripts_dirpath, BOX_XMIN_RIGHT_FNAME)
    # box_xmax_right = load_single_value(process_scripts_dirpath, BOX_XMAX_RIGHT_FNAME)
    #
    # box_xmin_left = load_single_value(process_scripts_dirpath, BOX_XMIN_LEFT_FNAME)
    # box_xmax_left = load_single_value(process_scripts_dirpath, BOX_XMAX_LEFT_FNAME)
    #
    # # Box shares y position and height with circle (diameter)
    # ymin = circle_coords_y - circle_radius
    # ymax = circle_coords_y + circle_radius
    # mask_out_left = (xx >= box_xmin_left) * (xx <= box_xmax_left) * (yy >= ymin) * (yy <= ymax)
    # mask_out_right = get_rectangle_mask(xx, yy, box_xmin_right, box_xmax_right, ymin, ymax)
    # mask_out = mask_out_left | mask_out_right

    # Display circle and boxes
    # with_circle = envUp_dB.copy()
    #
    #
    # with_circle[mask_out_left+mask_in+mask_out_right] = 0
    #
    # plt.figure(figsize=(12,16))
    # plt.imshow(with_circle, vmin=-60, vmax=0, cmap='gray', aspect='auto', extent = [beam_position_x_up[0]*1000,beam_position_x_up[-1]*1000,depth[-1]*1000, depth[0]*1000])
    # plt.colorbar()
    # FONT_SIZE = 20
    # plt.xlabel('lateral (mm)', fontsize=FONT_SIZE)
    # plt.ylabel('axial (mm)', fontsize=FONT_SIZE)

    # plt.show()

    # Calculate image statistics
    # print('r6: env_up.shape =', env_up.shape)
    mask_in, mask_out = get_masks(target_dirname)
    LOGGER.debug('{}: r6: Finished loading masks'.format(target_dirname))
    # print('r6: mask_in.shape={}, mask_out.shape={}'.format(mask_in.shape, mask_out.shape))
    env_up_inside_lesion = env_up[mask_in]
    mean_in = env_up_inside_lesion.mean()
    var_in = env_up_inside_lesion.var(ddof=1) # ddof is important cuz Matlab

    env_up_outside_lesion = env_up[mask_out]
    mean_out = env_up_outside_lesion.mean()
    var_out = env_up_outside_lesion.var(ddof=1) # ddof is important cuz Matlab

    LOGGER.debug('{}: r6: Finished mean and var calculations'.format(target_dirname))
    CR = -20 * math_log10(mean_in / mean_out)
    CNR = 20 * math_log10(abs(mean_in - mean_out)/math_sqrt(var_in + var_out))
    SNR = mean_out / math_sqrt(var_out)

    LOGGER.debug('{}: r6: Finished speckle stats calculations'.format(target_dirname))
    # Save image statistics to file
    speckle_stats = [CR, CNR, SNR, mean_in, mean_out, var_in, var_out]
    speckle_stats_path = os_path_join(target_dirname, SPECKLE_STATS_FNAME)

    with open(speckle_stats_path, 'w') as f:
        f.write("\n".join([str(item) for item in speckle_stats]))

    LOGGER.debug('{}: r6: Finished saving .txt'.format(target_dirname))
    # Also save image statistics json as a redundant (but more readable) method
    speckle_stats_dict = {
        'CR': CR,
        'CNR': CNR,
        'SNR': SNR,
        'mean_inside_lesion': mean_in,
        'variance_inside_lesion': var_in,
        'mean_outside_lesion': mean_out,
        'variance_outside_lesion': var_out,
    }

    speckle_stats_dict_path = os_path_join(target_dirname, SPECKLE_STATS_DICT_FNAME)

    with open(speckle_stats_dict_path, 'w') as f:
        json_dump(speckle_stats_dict, f, indent=4)
    LOGGER.debug('{}: r6: Finished saving .json'.format(target_dirname))

    LOGGER.info('{}: r6 Done'.format(target_dirname))