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

import numpy as np

ARE = []
with open('./inference_log/SE_prosody_Daudio_woZ_set05_b512_es750_L1_H200_dr0.9_03-20-15-42.txt') as f:
    ARE = f.readlines()[1].split(' ')
    ARE = [int(x) for x in ARE]

TRE = []
with open('./inference_log/SE_nlp_Daudio_woZ_set05_b512_es128_L1_H400_G1_dr0.9_03-20-16-44.txt') as f:
    TRE = f.readlines()[1].split(' ')
    TRE = [int(x) for x in TRE]

MDRE = []
with open('./inference_log/SE_multi_prosody_half_Daudio_woZ_set05_b256_esA750_LA1_HA400_drA0.9_esT128_LT1_HT200_G1_drT0.9_03-20-15-42.txt') as f:
    MDRE = f.readlines()[1].split(' ')
    MDRE = [int(x) for x in MDRE]

MDREA = []
with open('./inference_log/SE_multi_attn_prosody_half_Daudio_woZ_set05_b256_esA750_LA1_HA400_drA0.9_esT128_LT1_HT100_G1_drT0.9_03-20-16-01.txt') as f:
    MDREA = f.readlines()[1].split(' ')
    MDREA = [int(x) for x in MDREA]

label = np.load('../data/processed/IEMOCAP/four_category/audio_woZ_set05/test_label.npy')





# +
import numpy as np

def plot_confusion_matrix(cm,
                          target_names,
                          title='Confusion matrix',
                          cmap=None,
                          color_bar=False,
                          normalize=True):
    """
    given a sklearn confusion matrix (cm), make a nice plot

    Arguments
    ---------
    cm:           confusion matrix from sklearn.metrics.confusion_matrix

    target_names: given classification classes such as [0, 1, 2]
                  the class names, for example: ['high', 'medium', 'low']

    title:        the text to display at the top of the matrix

    cmap:         the gradient of the values displayed from matplotlib.pyplot.cm
                  see http://matplotlib.org/examples/color/colormaps_reference.html
                  plt.get_cmap('jet') or plt.cm.Blues

    normalize:    If False, plot the raw numbers
                  If True, plot the proportions

    Usage
    -----
    plot_confusion_matrix(cm           = cm,                  # confusion matrix created by
                                                              # sklearn.metrics.confusion_matrix
                          normalize    = True,                # show proportions
                          target_names = y_labels_vals,       # list of names of the classes
                          title        = best_estimator_name) # title of graph

    Citiation
    ---------
    http://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html

    """
    import matplotlib.pyplot as plt
    import matplotlib 
    import numpy as np
    import itertools

    # https://matplotlib.org/examples/pylab_examples/fonts_demo.html
    font = {'family' : 'sans-serif',
        'weight' : 'medium',      
        'size'   : 15}

    matplotlib.rc('font', **font)
    
    
    accuracy = np.trace(cm) / float(np.sum(cm))
    misclass = 1 - accuracy

    if cmap is None:
        cmap = plt.get_cmap('Blues')

    plt.figure(figsize=(8, 6))
    plt.imshow(cm, interpolation='nearest', cmap=cmap,
              )
               
#     plt.title(title)
    if color_bar:
        plt.colorbar()

    if target_names is not None:
        tick_marks = np.arange(len(target_names))
        plt.xticks(tick_marks, target_names, rotation=45)
#         plt.xticks(tick_marks, target_names)
        plt.yticks(tick_marks, target_names)

    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]


    thresh = cm.max() / 1.5 if normalize else cm.max() / 2
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        if normalize:
            plt.text(j, i, "{:0.2f}".format(cm[i, j]*100),
                     horizontalalignment="center",
                     color="white" if cm[i, j] > thresh else "black")
        else:
            plt.text(j, i, "{:,}".format(cm[i, j]),
                     horizontalalignment="center",
                     color="white" if cm[i, j] > thresh else "black")

#     plt.tight_layout()
    plt.ylabel('True class')
#     plt.xlabel('Predicted label\naccuracy={:0.4f}; misclass={:0.4f}'.format(accuracy, misclass))
    plt.xlabel('Predicted class')
    plt.show()
# -



plot_confusion_matrix(cm = np.array([[ 1098,  1934,   807],
                                              [  604,  4392,  6233],
                                              [  162,  2362, 31760]]), 
                      normalize    = True,
                      target_names = ['high', 'medium', 'low'],
                      color_bar = True,
                      title        = "Confusion Matrix")





data = ARE
hist = np.zeros([4,4])
for i, j in zip(label, data):
    hist[i][j] += 1
plot_confusion_matrix(cm = hist, 
                        normalize    = True,
                        color_bar = True,
                        target_names = ['angry', 'happy', 'sad', 'neutral'],
                        title        = "Confusion Matrix")

data = TRE
hist = np.zeros([4,4])
for i, j in zip(label, data):
    hist[i][j] += 1
plot_confusion_matrix(cm = hist, 
                        normalize    = True,
                        color_bar = True,
                        target_names = ['angry', 'happy', 'sad', 'neutral'],
                        title        = "Confusion Matrix")

data = MDRE
hist = np.zeros([4,4])
for i, j in zip(label, data):
    hist[i][j] += 1
plot_confusion_matrix(cm = hist, 
                        normalize    = True,
                        color_bar = True,
                        target_names = ['angry', 'happy', 'sad', 'neutral'],
                        title        = "Confusion Matrix")

data = MDREA
hist = np.zeros([4,4])
for i, j in zip(label, data):
    hist[i][j] += 1
plot_confusion_matrix(cm = hist, 
                        normalize    = True,
                        color_bar = True,
                        target_names = ['angry', 'happy', 'sad', 'neutral'],
                        title        = "Confusion Matrix")











StarcoderdataJupyterScriptsDedupFiltered