# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
from deakin.edu.au import data
from deakin.edu.au import models
import deakin.edu.au.metrics as metrics
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from matplotlib.patches import Rectangle
import numpy as np
import random
from tensorflow import keras


% load_ext autoreload
% autoreload 2
% load_ext tensorboard

# -

# # Loading data

# ## CIFAR100

#Load dataset
dataset = data.Cifar100()


# ## Stanford Cars

#Load dataset
# dataset = Stanford_Cars(image_size=(64,64))
dataset = data.get_Stanford_Cars(image_size=(64, 64), batch_size=128)


# ## CU_Birds_200_2011

#Load dataset
dataset = data.get_CU_Birds_200_2011(image_size=(64, 64), batch_size=128)


# plot the tree
tree = dataset.get_tree()
tree.show()
# Set variables
epochs = 150
# VGG-19
conv_base = 'vgg19'
learning_rate = 1e-5
# NIN
# conv_base = 'nin'
# learning_rate=1e-3


# # Evaluation

# ## Load models

# +
mnets_model = keras.models.load_model("models/mnets_model_" + dataset.name + "_" + conv_base)
mout_model = keras.models.load_model("models/mout_model_" + dataset.name + "_" + conv_base)
# BCNN1_model = keras.models.load_model("models/BCNN1_model_" + dataset.name + "_" + conv_base)
BCNN1_reversed_model = keras.models.load_model("models/BCNN1_reversed_model_" + dataset.name + "_" + conv_base)
BCNN2_model = keras.models.load_model("models/BCNN2_model_" + dataset.name + "_" + conv_base)
BCNN2_reversed_model = keras.models.load_model("models/BCNN2_reversed_model_" + dataset.name + "_" + conv_base)
MLPH_model = keras.models.load_model("models/MLPH_model_" + dataset.name + "_" + conv_base)
###########################################################################################
### Loading the baseline model
###########################################################################################
loaded_baseline_model = keras.models.load_model("models/baseline_model_" + dataset.name + "_" + conv_base)
baseline_model = models.get_Baseline_model(dataset.num_classes,
                                           dataset.image_size,
                                           dataset.taxonomy,
                                           conv_base=conv_base,
                                           learning_rate=learning_rate)
baseline_model.set_weights(loaded_baseline_model.get_weights())
###########################################################################################
mcnn_bottom_up_model = keras.models.load_model("models/mcnn_bottom_up_model_" + 
                                               dataset.name + "_" + conv_base)
mcnn_mnets_bottom_up_model = keras.models.load_model("models/mcnn_mnets_bottom_up_model_" + 
                                                     dataset.name + "_" + conv_base)
mcnn_top_down_model = keras.models.load_model("models/mcnn_top_down_model_" + 
                                              dataset.name + "_" + conv_base)
mcnn_mnets_top_down_model = keras.models.load_model("models/mcnn_mnets_top_down_model_" + 
                                                    dataset.name + "_" + conv_base)
mcnn_attention_bottom_up_model = keras.models.load_model("models/mcnn_attention_bottom_up_model_" + 
                                                         dataset.name + "_" + conv_base)
mcnn_attention_mnets_bottom_up_model = keras.models.load_model("models/mcnn_attention_mnets_bottom_up_model_" + 
                                                               dataset.name + "_" + conv_base)
mcnn_attention_top_down_model = keras.models.load_model("models/mcnn_attention_top_down_model_" + 
                                                        dataset.name + "_" + conv_base)
mcnn_attention_mnets_top_down_model = keras.models.load_model("models/mcnn_attention_mnets_top_down_model_" + 
                                                              dataset.name + "_" + conv_base)


# -

# ## Make predictions

trained_models = [mnets_model, mout_model,  #BCNN1_model,
                  BCNN1_reversed_model, BCNN2_model, BCNN2_reversed_model,
                  MLPH_model, baseline_model, mcnn_bottom_up_model, mcnn_mnets_bottom_up_model,
                  mcnn_top_down_model, mcnn_mnets_top_down_model,
                  mcnn_attention_bottom_up_model, 
                  mcnn_attention_mnets_bottom_up_model, 
                  mcnn_attention_top_down_model,
                  mcnn_attention_mnets_top_down_model]
predictions = []
for model in trained_models:
    print('Prediction using: ', model.name)
    predictions.append((metrics.predict_from_pipeline(model, dataset.test_dataset)))


# ## Compute metrics

exact_match_scores = [metrics.get_exact_match(x[0], x[1]) for x in predictions]
hP_scores = [metrics.get_hierarchical_metrics(x[0], x[1], dataset.get_tree())[0] for x in predictions]
hR_scores = [metrics.get_hierarchical_metrics(x[0], x[1], dataset.get_tree())[1] for x in predictions]
hF1_scores = [metrics.get_hierarchical_metrics(x[0], x[1], dataset.get_tree())[2] for x in predictions]
h_accuracy_scores = [metrics.get_h_accuracy(x[0], x[1]) for x in predictions]
h_accuracy_scores_k2 = [metrics.get_h_accuracy(x[0], x[1], k=2) for x in predictions]
h_accuracy_scores_k5 = [metrics.get_h_accuracy(x[0], x[1], k=5) for x in predictions]
m_accuracy_scores = [metrics.get_m_accuracy(x[0], x[1]) for x in predictions]
m_accuracy_scores_k2 = [metrics.get_m_accuracy(x[0], x[1], k=2) for x in predictions]
m_accuracy_scores_k5 = [metrics.get_m_accuracy(x[0], x[1], k=5) for x in predictions]
accuracy_scores_L0 = [metrics.get_top_k_taxonomical_accuracy(x[0], x[1])[0] for x in predictions]
accuracy_scores_L1 = [metrics.get_top_k_taxonomical_accuracy(x[0], x[1])[1] for x in predictions]
accuracy_scores_L2 = [metrics.get_top_k_taxonomical_accuracy(x[0], x[1])[2] for x in predictions]
consistency = [metrics.get_consistency(x[1], dataset.get_tree()) for x in predictions]


# # CU_Birds_200_2011

# +
# Create example dataframe
df = pd.DataFrame({
    'Algorithm': ['mnets', 'mout', #'BCNN1', 
             'BCNN1_R', 'BCNN2', 'BCNN2_R', 'MLPH', 'Baseline', 
             'mcnn_bottom_up',
             'mcnn_mnets_bottom_up', 
             'mcnn_top_down', 
             'mcnn_mnets_top_down',
             #Attention models
             'mcnn_attention_bottom_up',
             'mcnn_attention_mnets_bottom_up', 
             'mcnn_attention_top_down',
             'mcnn_attention_mnets_top_down'],
    'Exact Match': exact_match_scores,
    'Hierarchical Precision': hP_scores,
    'Hierarchical Recall': hR_scores,
    'Hierarchical F1-Score': hF1_scores,
    'Harmonic Mean Accuracy': h_accuracy_scores,
    'Harmonic Mean Accuracy K=2': h_accuracy_scores_k2,
    'Harmonic Mean Accuracy K=5': h_accuracy_scores_k5,
    'Arithmetic Mean Accuracy': m_accuracy_scores,
    'Arithmetic Mean Accuracy K=2': m_accuracy_scores_k2,
    'Arithmetic Mean Accuracy K=5': m_accuracy_scores_k5,
    'Accuracy L0': accuracy_scores_L0,
    'Accuracy L1': accuracy_scores_L1,
    'Accuracy L2': accuracy_scores_L2,
    'Consistency': consistency})
df.set_index('Algorithm', inplace=True)


#####################################
### Print the performance table #####
#####################################
def highlight_max(data, color='yellow'):
    '''
    highlight the maximum in a Series or DataFrame
    '''
    attr = 'background-color: {}'.format(color)
    #remove % and cast to float
    #     data = data.replace('%','', regex=True).astype(float)
    if data.ndim == 1:  # Series from .apply(axis=0) or axis=1
        is_max = data == data.max()
        return [attr if v else '' for v in is_max]
    else:  # from .apply(axis=None)
        is_max = data == data.max().max()
        return pd.DataFrame(np.where(is_max, attr, ''),
                            index=data.index, columns=data.columns)


df.style.apply(highlight_max)

# -

# # Stanford_Cars

# +
# Create example dataframe
df = pd.DataFrame({
    'Algorithm': ['mnets', 'mout', #'BCNN1', 
             'BCNN1_R', 'BCNN2', 'BCNN2_R', 'MLPH', 'Baseline', 
             'mcnn_bottom_up',
             'mcnn_mnets_bottom_up', 
             'mcnn_top_down', 
             'mcnn_mnets_top_down',
             #Attention models
             'mcnn_attention_bottom_up',
             'mcnn_attention_mnets_bottom_up', 
             'mcnn_attention_top_down',
             'mcnn_attention_mnets_top_down'],
    'Exact Match': exact_match_scores,
    'Hierarchical Precision': hP_scores,
    'Hierarchical Recall': hR_scores,
    'Hierarchical F1-Score': hF1_scores,
    'Harmonic Mean Accuracy': h_accuracy_scores,
    'Harmonic Mean Accuracy K=2': h_accuracy_scores_k2,
    'Harmonic Mean Accuracy K=5': h_accuracy_scores_k5,
    'Arithmetic Mean Accuracy': m_accuracy_scores,
    'Arithmetic Mean Accuracy K=2': m_accuracy_scores_k2,
    'Arithmetic Mean Accuracy K=5': m_accuracy_scores_k5,
    'Accuracy L0': accuracy_scores_L0,
    'Accuracy L1': accuracy_scores_L1,
    'Accuracy L2': accuracy_scores_L2,
    'Consistency': consistency})
df.set_index('Algorithm', inplace=True)


#####################################
### Print the performance table #####
#####################################
def highlight_max(data, color='yellow'):
    '''
    highlight the maximum in a Series or DataFrame
    '''
    attr = 'background-color: {}'.format(color)
    #remove % and cast to float
    #     data = data.replace('%','', regex=True).astype(float)
    if data.ndim == 1:  # Series from .apply(axis=0) or axis=1
        is_max = data == data.max()
        return [attr if v else '' for v in is_max]
    else:  # from .apply(axis=None)
        is_max = data == data.max().max()
        return pd.DataFrame(np.where(is_max, attr, ''),
                            index=data.index, columns=data.columns)


df.style.apply(highlight_max)


# +
plt.figure(figsize=(20, 500))
metric_names = list(df.columns.values)
metric_names.remove('algo')
k = 0
for i in range(len(metric_names)):
    for j in range(i + 1, len(metric_names)):
        k += 1
        algo = list(df['algo'])
        metric_x = metric_names[i]
        values_x = list(df[metric_x])
        metric_y = metric_names[j]
        values_y = list(df[metric_y])
        buble_size = [3000 for x in range(len(values_x))]
        ax = plt.subplot(100, 3, k)

        sns.scatterplot(x=values_x, y=values_y, alpha=0.5, s=buble_size)

        ax.grid(color='black', linestyle='--', linewidth=1)
        ax.set_axisbelow(True)

        plt.xlabel(metric_x)
        plt.ylabel(metric_y)
        #For each point, we add a text inside the bubble
        for l in range(len(algo)):
            ax.text(values_x[l], values_y[l], algo[l],
                    horizontalalignment='center', size='medium', color='black', weight='semibold')

plt.tight_layout()
plt.savefig('plots/' + dataset.name + "_" + conv_base + '_bubble_plots.pdf', bbox_inches='tight')
plt.show()




# +
plt.figure(figsize=(20, 16))
for i in range(30):
    k = random.randint(0, len(dataset.X_val))
    file_name = dataset.val_filenames[k]
    y_l0 = dataset.labels[0][dataset.y_val[0][k][0]]
    y_l1 = dataset.labels[1][dataset.y_val[1][k][0]]
    y_l2 = dataset.labels[2][dataset.y_val[2][k][0]]

    ax = plt.subplot(5, 6, i + 1)
    plt.imshow(dataset.X_val[k])
    plt.title(file_name + "\n" + y_l0 + "\n" + y_l1 + "\n" + y_l2)
    plt.axis("off")

plt.tight_layout()
plt.show()

# +
plt.figure(figsize=(6.5, 4))
params = {'legend.fontsize': 18,
          'axes.labelsize': 20,
          'axes.titlesize': 14,
          'xtick.labelsize': 16,
          'ytick.labelsize': 16,
          'axes.titlepad': 25,
          'font.size': 10}
plt.rcParams.update(params)

ax = sns.scatterplot(df.exact_match, df.consistency, alpha=0.5, s=df.s)

ax.grid(color='black', linestyle='--', linewidth=1)
ax.set_axisbelow(True)

plt.xlabel("")
plt.ylabel("consistency")
# plt.title("Box plot of #candidates per cycle and system")
plt.yticks([0.2, 0.4, 0.6, 0.8, 1.0])
plt.xticks([0.0, 0.2, 0.4, 0.6, 0.8, 1.0])

ax.set_xlim(0, 0.05)
ax.set_ylim(0, 1)
plt.xlabel('Exact Match')
plt.ylabel('Consistency')
#For each point, we add a text inside the bubble
for line in range(0, df.shape[0]):
    if df.algo[line] == 'baseline':
        ax.text(df.exact_match[line], df.consistency[line], df.algo[line], verticalalignment='top',
                horizontalalignment='center', size='medium', color='black', weight='semibold')
    elif df.algo[line].startswith('BCNN'):
        ax.text(df.exact_match[line], df.consistency[line], df.algo[line], horizontalalignment='left', size='medium',
                color='black', weight='semibold')
    else:
        ax.text(df.exact_match[line], df.consistency[line], df.algo[line], horizontalalignment='center', size='medium',
                color='black', weight='semibold')

# currentAxis = plt.gca()
# currentAxis.add_patch(Rectangle((0., 0.), 1., 0.2, color = 'red', zorder=0))
# currentAxis.add_patch(Rectangle((0., 0.), .2, 1., color = 'red',zorder=0))
# currentAxis.add_patch(Rectangle((0.2, 0.2), 0.2, 0.2, color = 'red', zorder=0))

# currentAxis.add_patch(Rectangle((0.2, 0.4), 0.4, 1.0, color = 'orange', zorder=0))
# currentAxis.add_patch(Rectangle((0.4, 0.2), 1., 0.4, color = 'orange', zorder=0))
# currentAxis.add_patch(Rectangle((0.6, 0.6), 0.2, 0.2, color = 'orange', zorder=0))

# currentAxis.add_patch(Rectangle((0.8, 0.6), 0.2, 0.4, color = 'green', zorder=0))
# currentAxis.add_patch(Rectangle((0.6, 0.8), 0.4, 0.2, color = 'green', zorder=0))
# plotlim = plt.xlim() + plt.ylim()
# import matplotlib.colors

# cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", ["red","yellow","green"])

# ax.imshow([[0.5, 0.5, 0.5], [0, 0.5, 0.5], [0, 0, 0.5]],
#           cmap=cmap,
#           interpolation='bicubic',
#           extent=plotlim, vmin=0, vmax=0.5)

plt.savefig('plots/bubble_plot_em.pdf', bbox_inches='tight')
plt.show()


# +
plt.figure(figsize=(6.5, 4))
params = {'legend.fontsize': 18,
          'axes.labelsize': 20,
          'axes.titlesize': 14,
          'xtick.labelsize': 16,
          'ytick.labelsize': 16,
          'axes.titlepad': 25,
          'font.size': 10}
plt.rcParams.update(params)

ax = sns.scatterplot(df.h_accuracy, df.consistency, alpha=0.5, s=df.s)

ax.grid(color='black', linestyle='--', linewidth=1)
ax.set_axisbelow(True)

plt.xlabel("")
plt.ylabel("consistency")
# plt.title("Box plot of #candidates per cycle and system")
plt.yticks([0.2, 0.4, 0.6, 0.8, 1.0])
plt.xticks([0.0, 0.2, 0.4, 0.6, 0.8, 1.0])

ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
plt.xlabel('Harmonic Accuracy')
plt.ylabel('Consistency')
#For each point, we add a text inside the bubble
for line in range(0, df.shape[0]):
    if df.algo[line] == 'baseline':
        ax.text(df.h_accuracy[line], df.consistency[line], df.algo[line], verticalalignment='top',
                horizontalalignment='center', size='medium', color='black', weight='semibold')
    elif df.algo[line].startswith('BCNN'):
        ax.text(df.h_accuracy[line], df.consistency[line], df.algo[line], horizontalalignment='left', size='medium',
                color='black', weight='semibold')
    else:
        ax.text(df.h_accuracy[line], df.consistency[line], df.algo[line], horizontalalignment='center', size='medium',
                color='black', weight='semibold')

currentAxis = plt.gca()
currentAxis.add_patch(Rectangle((0., 0.), 1., 0.2, color='red', zorder=0))
currentAxis.add_patch(Rectangle((0., 0.), .2, 1., color='red', zorder=0))
currentAxis.add_patch(Rectangle((0.2, 0.2), 0.2, 0.2, color='red', zorder=0))

currentAxis.add_patch(Rectangle((0.2, 0.4), 0.4, 1.0, color='orange', zorder=0))
currentAxis.add_patch(Rectangle((0.4, 0.2), 1., 0.4, color='orange', zorder=0))
currentAxis.add_patch(Rectangle((0.6, 0.6), 0.2, 0.2, color='orange', zorder=0))

currentAxis.add_patch(Rectangle((0.8, 0.6), 0.2, 0.4, color='green', zorder=0))
currentAxis.add_patch(Rectangle((0.6, 0.8), 0.4, 0.2, color='green', zorder=0))
# plotlim = plt.xlim() + plt.ylim()
# import matplotlib.colors

# cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", ["red","yellow","green"])

# ax.imshow([[0.5, 0.5, 0.5], [0, 0.5, 0.5], [0, 0, 0.5]],
#           cmap=cmap,
#           interpolation='bicubic',
#           extent=plotlim, vmin=0, vmax=0.5)

plt.savefig('plots/bubble_plot_h_accuracy.pdf', bbox_inches='tight')
plt.show()


# +
plt.figure(figsize=(6.5, 4))
params = {'legend.fontsize': 18,
          'axes.labelsize': 20,
          'axes.titlesize': 14,
          'xtick.labelsize': 16,
          'ytick.labelsize': 16,
          'axes.titlepad': 25,
          'font.size': 10}
plt.rcParams.update(params)

ax = sns.scatterplot(df.h_accuracy_k2, df.consistency, alpha=0.5, s=df.s)

ax.grid(color='black', linestyle='--', linewidth=1)
ax.set_axisbelow(True)

plt.xlabel("")
plt.ylabel("consistency")
# plt.title("Box plot of #candidates per cycle and system")
plt.yticks([0.2, 0.4, 0.6, 0.8, 1.0])
plt.xticks([0.0, 0.2, 0.4, 0.6, 0.8, 1.0])

ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
plt.xlabel('Harmonic Accuracy k=2')
plt.ylabel('Consistency')
#For each point, we add a text inside the bubble
for line in range(0, df.shape[0]):
    if df.algo[line] == 'baseline':
        ax.text(df.h_accuracy_k2[line], df.consistency[line], df.algo[line], verticalalignment='top',
                horizontalalignment='center', size='medium', color='black', weight='semibold')
    elif df.algo[line].startswith('BCNN'):
        ax.text(df.h_accuracy_k2[line], df.consistency[line], df.algo[line], horizontalalignment='left', size='medium',
                color='black', weight='semibold')
    else:
        ax.text(df.h_accuracy_k2[line], df.consistency[line], df.algo[line], horizontalalignment='center',
                size='medium', color='black', weight='semibold')

currentAxis = plt.gca()
currentAxis.add_patch(Rectangle((0., 0.), 1., 0.2, color='red', zorder=0))
currentAxis.add_patch(Rectangle((0., 0.), .2, 1., color='red', zorder=0))
currentAxis.add_patch(Rectangle((0.2, 0.2), 0.2, 0.2, color='red', zorder=0))

currentAxis.add_patch(Rectangle((0.2, 0.4), 0.4, 1.0, color='orange', zorder=0))
currentAxis.add_patch(Rectangle((0.4, 0.2), 1., 0.4, color='orange', zorder=0))
currentAxis.add_patch(Rectangle((0.6, 0.6), 0.2, 0.2, color='orange', zorder=0))

currentAxis.add_patch(Rectangle((0.8, 0.6), 0.2, 0.4, color='green', zorder=0))
currentAxis.add_patch(Rectangle((0.6, 0.8), 0.4, 0.2, color='green', zorder=0))
# plotlim = plt.xlim() + plt.ylim()
# import matplotlib.colors

# cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", ["red","yellow","green"])

# ax.imshow([[0.5, 0.5, 0.5], [0, 0.5, 0.5], [0, 0, 0.5]],
#           cmap=cmap,
#           interpolation='bicubic',
#           extent=plotlim, vmin=0, vmax=0.5)

plt.savefig('plots/bubble_plot_h_accuracy_k2.pdf', bbox_inches='tight')
plt.show()


# +
plt.figure(figsize=(6.5, 4))
params = {'legend.fontsize': 18,
          'axes.labelsize': 20,
          'axes.titlesize': 14,
          'xtick.labelsize': 16,
          'ytick.labelsize': 16,
          'axes.titlepad': 25,
          'font.size': 10}
plt.rcParams.update(params)

ax = sns.scatterplot(df.h_accuracy_k5, df.consistency, alpha=0.5, s=df.s)

ax.grid(color='black', linestyle='--', linewidth=1)
ax.set_axisbelow(True)

plt.xlabel("")
plt.ylabel("consistency")
# plt.title("Box plot of #candidates per cycle and system")
plt.yticks([0.2, 0.4, 0.6, 0.8, 1.0])
plt.xticks([0.0, 0.2, 0.4, 0.6, 0.8, 1.0])

ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
plt.xlabel('Harmonic Accuracy k=5')
plt.ylabel('Consistency')
#For each point, we add a text inside the bubble
for line in range(0, df.shape[0]):
    if df.algo[line] == 'baseline':
        ax.text(df.h_accuracy_k5[line], df.consistency[line], df.algo[line], verticalalignment='top',
                horizontalalignment='center', size='medium', color='black', weight='semibold')
    elif df.algo[line].startswith('BCNN'):
        ax.text(df.h_accuracy_k5[line], df.consistency[line], df.algo[line], horizontalalignment='left', size='medium',
                color='black', weight='semibold')
    else:
        ax.text(df.h_accuracy_k5[line], df.consistency[line], df.algo[line], horizontalalignment='center',
                size='medium', color='black', weight='semibold')

currentAxis = plt.gca()
currentAxis.add_patch(Rectangle((0., 0.), 1., 0.2, color='red', zorder=0))
currentAxis.add_patch(Rectangle((0., 0.), .2, 1., color='red', zorder=0))
currentAxis.add_patch(Rectangle((0.2, 0.2), 0.2, 0.2, color='red', zorder=0))

currentAxis.add_patch(Rectangle((0.2, 0.4), 0.4, 1.0, color='orange', zorder=0))
currentAxis.add_patch(Rectangle((0.4, 0.2), 1., 0.4, color='orange', zorder=0))
currentAxis.add_patch(Rectangle((0.6, 0.6), 0.2, 0.2, color='orange', zorder=0))

currentAxis.add_patch(Rectangle((0.8, 0.6), 0.2, 0.4, color='green', zorder=0))
currentAxis.add_patch(Rectangle((0.6, 0.8), 0.4, 0.2, color='green', zorder=0))
# plotlim = plt.xlim() + plt.ylim()
# import matplotlib.colors

# cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", ["red","yellow","green"])

# ax.imshow([[0.5, 0.5, 0.5], [0, 0.5, 0.5], [0, 0, 0.5]],
#           cmap=cmap,
#           interpolation='bicubic',
#           extent=plotlim, vmin=0, vmax=0.5)

plt.savefig('plots/bubble_plot_h_accuracy_k5.pdf', bbox_inches='tight')
plt.show()


# +
plt.figure(figsize=(6.5, 4))
params = {'legend.fontsize': 18,
          'axes.labelsize': 20,
          'axes.titlesize': 14,
          'xtick.labelsize': 16,
          'ytick.labelsize': 16,
          'axes.titlepad': 25,
          'font.size': 10}
plt.rcParams.update(params)

ax = sns.scatterplot(df.m_accuracy, df.consistency, alpha=0.5, s=df.s)

ax.grid(color='black', linestyle='--', linewidth=1)
ax.set_axisbelow(True)

plt.xlabel("")
plt.ylabel("consistency")
# plt.title("Box plot of #candidates per cycle and system")
plt.yticks([0.2, 0.4, 0.6, 0.8, 1.0])
plt.xticks([0.0, 0.2, 0.4, 0.6, 0.8, 1.0])

ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
plt.xlabel('Mean Accuracy')
plt.ylabel('Consistency')
#For each point, we add a text inside the bubble
for line in range(0, df.shape[0]):
    if df.algo[line] == 'baseline':
        ax.text(df.m_accuracy[line], df.consistency[line], df.algo[line], verticalalignment='top',
                horizontalalignment='center', size='medium', color='black', weight='semibold')
    elif df.algo[line].startswith('BCNN'):
        ax.text(df.m_accuracy[line], df.consistency[line], df.algo[line], horizontalalignment='left', size='medium',
                color='black', weight='semibold')
    else:
        ax.text(df.m_accuracy[line], df.consistency[line], df.algo[line], horizontalalignment='center', size='medium',
                color='black', weight='semibold')

currentAxis = plt.gca()
currentAxis.add_patch(Rectangle((0., 0.), 1., 0.2, color='red', zorder=0))
currentAxis.add_patch(Rectangle((0., 0.), .2, 1., color='red', zorder=0))
currentAxis.add_patch(Rectangle((0.2, 0.2), 0.2, 0.2, color='red', zorder=0))

currentAxis.add_patch(Rectangle((0.2, 0.4), 0.4, 1.0, color='orange', zorder=0))
currentAxis.add_patch(Rectangle((0.4, 0.2), 1., 0.4, color='orange', zorder=0))
currentAxis.add_patch(Rectangle((0.6, 0.6), 0.2, 0.2, color='orange', zorder=0))

currentAxis.add_patch(Rectangle((0.8, 0.6), 0.2, 0.4, color='green', zorder=0))
currentAxis.add_patch(Rectangle((0.6, 0.8), 0.4, 0.2, color='green', zorder=0))
# plotlim = plt.xlim() + plt.ylim()
# import matplotlib.colors

# cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", ["red","yellow","green"])

# ax.imshow([[0.5, 0.5, 0.5], [0, 0.5, 0.5], [0, 0, 0.5]],
#           cmap=cmap,
#           interpolation='bicubic',
#           extent=plotlim, vmin=0, vmax=0.5)

plt.savefig('plots/bubble_plot_m_accuracy.pdf', bbox_inches='tight')
plt.show()

# -

preds = mout_model.predict(dataset.test_dataset)



for vec in preds[2]:
    
    z = np.sum(np.log(vec))
    if z == -np.inf:
        v = vec

v

import tensorflow as tf
y_true = [0, 1, 0, 0]
y_pred = [0, 0.99, 0, 0]
bce = tf.keras.losses.BinaryCrossentropy(from_logits=False)
bce(y_true, y_pred).numpy()

p.finfo(float).eps




StarcoderdataJupyterScriptsDedupFiltered