<filename>utils/plot_comparison.py
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.gridspec as gridspec
import numpy as np
import os

comparison_dir = '/home/rgunti/data/Thesis/experiments/experiments_comparison'

exp_dir_61 = '/home/rgunti/data/Thesis/experiments/6.1_int_RetinaNet/plots'
exp_dir_62 = '/home/rgunti/data/Thesis/experiments/6.2_int_DCRetinaNet/plots'

exp_dir_631 = '/home/rgunti/data/Thesis/experiments/6.3.1_int_DCRetinaNet_unfreeze/plots'

exp_dir_632_int_r_wocrop = '/home/rgunti/data/Thesis/experiments/6.3.2_int_RetinaNet_wocrop/plots'
exp_dir_632_int_dcr_wocrop = '/home/rgunti/data/Thesis/experiments/6.3.2_int_DCRetinaNet_wocrop/plots'

exp_dir_633_pre_r = '/home/rgunti/data/Thesis/experiments/6.3.3_pre_RetinaNet/plots'
exp_dir_633_int_dcr = '/home/rgunti/data/Thesis/experiments/6.3.3_int_DCRetinaNet/plots'

exp_dir1 = exp_dir_61
exp_dir2 = exp_dir_62

exp_dir1 = exp_dir_62
exp_dir2 = exp_dir_631

exp_dir1 = exp_dir_62
exp_dir2 = exp_dir_632_int_dcr_wocrop

exp_dir1 = exp_dir_62
exp_dir2 = exp_dir_633_int_dcr

bool_plot_all_folds = True
bool_plot_prc = True
bool_plot_overall_with_wbc = True

exp_pairs = [
            [exp_dir_61, exp_dir_62],
            [exp_dir_62, exp_dir_631],
            [exp_dir_62, exp_dir_632_int_dcr_wocrop],
            [exp_dir_62, exp_dir_633_int_dcr]
            ]

def get_data_string(dir):
    data_string = 'data_pp'
    data_string += '_int' if 'int' in str(dir) else ''
    data_string += '_wocrop' if 'wocrop' in str(dir) else ''
    print(dir, data_string)
    return data_string

for exp_pair_ind in range(len(exp_pairs)):
    exp_dir1, exp_dir2 = exp_pairs[exp_pair_ind]
    print("Experiment pairs : ", exp_dir1.split('/')[-2], exp_dir2.split('/')[-2])

    target_dir = os.path.join(comparison_dir, exp_dir1.split('/')[-2] + '_vs_' + exp_dir2.split('/')[-2])
    if not os.path.exists(os.path.join(comparison_dir, target_dir)):
        os.mkdir(target_dir)

    if bool_plot_all_folds:
        print("Comparing all folds with and without WBC")
        image_datas = []
        folds = list(range(5))
        folds.append('overall')
        print(folds)
        for fold in folds:
            print("Fold:{}".format(fold))
            image_datas = []
            tl_path = os.path.join(exp_dir1, 'test_fold_{}_{}_without_wbc.png'.format(fold, get_data_string(exp_dir1)))
            tr_path = os.path.join(exp_dir2, 'test_fold_{}_{}_without_wbc.png'.format(fold, get_data_string(exp_dir2)))
            bl_path = os.path.join(exp_dir1, 'test_fold_{}_{}_with_wbc.png'.format(fold, get_data_string(exp_dir1)))
            br_path = os.path.join(exp_dir2, 'test_fold_{}_{}_with_wbc.png'.format(fold, get_data_string(exp_dir2)))

            image_datas.append(mpimg.imread(tl_path))
            image_datas.append(mpimg.imread(tr_path))
            image_datas.append(mpimg.imread(bl_path))
            image_datas.append(mpimg.imread(br_path))

            f, axarr = plt.subplots(2,2)
            for ind1 in range(len(axarr)):
                for ind2 in range(len(axarr[ind1])):
                    axarr[ind1, ind2].imshow(image_datas[ind1*2 + ind2])
                    axarr[ind1, ind2].axis('off')

            plt.subplots_adjust(wspace=0, hspace=0)
            plt.savefig(os.path.join(target_dir, 'test_{}.png'.format(fold)), dpi = 800, bbox_inches='tight')

    if bool_plot_prc:
        print("Comparing Precision-Recall Curves")
        prc_datas = []
        prc_left_path = os.path.join(exp_dir1, 'test_fold_overall_{}_with_wbc_prc.png'.format(get_data_string(exp_dir1)))
        prc_right_path = os.path.join(exp_dir2, 'test_fold_overall_{}_with_wbc_prc.png'.format(get_data_string(exp_dir2)))
        prc_datas.append(mpimg.imread(prc_left_path))
        prc_datas.append(mpimg.imread(prc_right_path))
        f, axarr = plt.subplots(1,2)
        axarr[0].imshow(prc_datas[0])
        axarr[0].axis('off')
        axarr[1].imshow(prc_datas[1])
        axarr[1].axis('off')
        plt.subplots_adjust(wspace=0, hspace=0)
        plt.savefig(os.path.join(target_dir, 'test_prc.png'), dpi = 800, bbox_inches='tight')

    if bool_plot_overall_with_wbc:
        print("Comparing overall with WBC")
        prc_datas = []
        prc_left_path = os.path.join(exp_dir1, 'test_fold_overall_{}_with_wbc.png'.format(get_data_string(exp_dir1)))
        prc_right_path = os.path.join(exp_dir2, 'test_fold_overall_{}_with_wbc.png'.format(get_data_string(exp_dir2)))
        prc_datas.append(mpimg.imread(prc_left_path))
        prc_datas.append(mpimg.imread(prc_right_path))
        f, axarr = plt.subplots(1,2)
        axarr[0].imshow(prc_datas[0])
        axarr[0].axis('off')
        axarr[1].imshow(prc_datas[1])
        axarr[1].axis('off')
        plt.subplots_adjust(wspace=0, hspace=0)
        file_name = exp_dir1.split('/')[-2] + '_vs_' + exp_dir2.split('/')[-2] + '.png'
        plt.savefig(os.path.join(target_dir, file_name), dpi = 800, bbox_inches='tight')
