"""
# Week 6
### Alejandro Alemany, Sara Manrriquez, and Benjamin Zaretzky
"""
import numpy as np
import pandas as pd
import zipfile
import scikitplot

from tensorflow import keras
from sklearn.metrics import classification_report
test = pd.read_csv('../input/challenges-in-representation-learning-facial-expression-recognition-challenge/test.csv')
print(test.shape)
test.head()
test['pixels'] = [np.fromstring(x, dtype=int, sep=' ').reshape(-1,48,48,1) for x in test['pixels']]
pixels = np.concatenate(test['pixels'])

print(pixels.shape)
pixels = pixels / 255
from tensorflow.keras.models import load_model

model_1 = load_model('../input/model-num-1/fer_v05_BZ.h5')
model_2 = load_model('../input/model-num-2/fer_model_v03.h5')
model_3 = load_model('../input/model-num-3/fer_model_v01.h5')

#model.summary()
test_probs_1 = model_1.predict(pixels)
print(test_probs_1.shape)

test_probs_2 = model_2.predict(pixels)
print(test_probs_2.shape)

test_probs_3 = model_3.predict(pixels)
print(test_probs_3.shape)
print(test_probs_1[:10,].round(2))
print(test_probs_2[:10,].round(2))
print(test_probs_3[:10,].round(2))
test_pred_1 = np.argmax(test_probs_1, axis=1)
print(test_pred_1[:10])

test_pred_2 = np.argmax(test_probs_2, axis=1)
print(test_pred_2[:10])

test_pred_3 = np.argmax(test_probs_3, axis=1)
print(test_pred_3[:10])
test_pred = (test_pred_1 + test_pred_1 + test_pred_1)/3
test_labels = pd.read_csv('../input/challenges-in-representation-learning-facial-expression-recognition-challenge/icml_face_data.csv')
test_labels.columns = ['emotion', 'Usage', 'pixels']
print(test_labels.shape)
test_labels = test_labels.loc[test_labels['Usage'].isin(['PublicTest', 'PrivateTest'])]
test_labels.drop(columns='Usage', inplace=True)
test_labels.head()
test_labels.shape
test_labels['predictions'] = test_pred
test_labels.head()
my_classification_report = classification_report(test_labels['emotion'], test_labels['predictions'])
print(my_classification_report)
print('Total Wrong Predictions:', np.sum(test_labels['emotion'] != test_labels['predictions']))
scikitplot.metrics.plot_confusion_matrix(test_labels['emotion'], test_labels['predictions'], figsize=(7,7))    