import numpy as np
import h5py
from sklearn import svm, cross_validation
from sklearn.naive_bayes import MultinomialNB

"""
Explanation: Prediction of text with Nexa Letter Latency.
This notbook is for seeing how much the delay between the code vector and the code is related to the accuaracy of the prediciton.
End of explanation
"""


# First we load the file 
file_location = '../results_database/text_wall_street_big.hdf5'
f = h5py.File(file_location, 'r')

# Now we need to get the letters and align them
text_directory = '../data/wall_street_letters.npy'
letters_sequence = np.load(text_directory)
Nletters = len(letters_sequence)
symbols = set(letters_sequence)

# Load the particular example
Nspatial_clusters = 5
Ntime_clusters = 15
Nembedding = 3

run_name = '/low-resolution'
parameters_string = '/' + str(Nspatial_clusters)
parameters_string += '-' + str(Ntime_clusters)
parameters_string += '-' + str(Nembedding)

nexa = f[run_name + parameters_string]

# Now we load the time and the code vectors
time = nexa['time']
code_vectors = nexa['code-vectors']
code_vectors_distance = nexa['code-vectors-distance']
code_vectors_softmax = nexa['code-vectors-softmax']
code_vectors_winner = nexa['code-vectors-winner']

"""
Explanation: Load the data
End of explanation
"""


N = 50000  # Amount of data
delays = np.arange(0, 10)
accuracy = []

# Make prediction with scikit-learn
for delay in delays:
    X = code_vectors_winner[:(N - delay)]
    y = letters_sequence[delay:N]
    X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.10)

    clf = svm.SVC(C=1.0, cache_size=200, kernel='linear')
    clf.fit(X_train, y_train)
    score = clf.score(X_test, y_test) * 100.0
    accuracy.append(score)
    print('delay', delay)
    print('score', score)


"""
Explanation: Study the Latency of the Data by Accuracy
Make prediction with winner takes all
Make the prediction for each delay. This takes a bit
End of explanation
"""


import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
plt.plot(delays, accuracy, 'o-', lw=2, markersize=10)
plt.xlabel('Delays')
plt.ylim([0, 105])
plt.xlim([-0.5, 10])
plt.ylabel('Accuracy %')
plt.title('Delays vs Accuracy')
fig = plt.gcf()
fig.set_size_inches((12, 9))

"""
Explanation: Plot it
End of explanation
"""


from sklearn import preprocessing

N = 50000  # Amount of data
delays = np.arange(0, 10)
accuracy_std = []

# Make prediction with scikit-learn
for delay in delays:
    X = code_vectors_winner[:(N - delay)]
    y = letters_sequence[delay:N]
    X = preprocessing.scale(X)
    X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.10)

    clf = svm.SVC(C=1.0, cache_size=200, kernel='linear')
    clf.fit(X_train, y_train)
    score = clf.score(X_test, y_test) * 100.0
    accuracy_std.append(score)
    print('delay', delay)
    print('score', score)

"""
Explanation: Make predictions with representation standarization
End of explanation
"""


plt.plot(delays, accuracy, 'o-', lw=2, markersize=10., label='Accuracy')
plt.plot(delays, accuracy_std, 'o-', lw=2, markersize=10, label='Standarized Representations')
plt.xlabel('Delays')
plt.ylim([0, 105])
plt.xlim([-0.5, 10])
plt.ylabel('Accuracy %')
plt.title('Delays vs Accuracy')
fig = plt.gcf()
fig.set_size_inches((12, 9))
plt.legend()

"""
Explanation: Plot it
End of explanation
"""


accuracy_softmax = []

# Make prediction with scikit-learn
for delay in delays:
    X = code_vectors_softmax[:(N - delay)]
    y = letters_sequence[delay:N]
    X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.10)

    clf = svm.SVC(C=1.0, cache_size=200, kernel='linear')
    clf.fit(X_train, y_train)
    score = clf.score(X_test, y_test) * 100.0
    accuracy_softmax.append(score)
    print('delay', delay)
    print('score', score)


"""
Explanation: Make prediction with softmax
End of explanation
"""


accuracy_softmax_std = []

# Make prediction with scikit-learn
for delay in delays:
    X = code_vectors_winner[:(N - delay)]
    y = letters_sequence[delay:N]
    X = preprocessing.scale(X)
    X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.10)

    clf = svm.SVC(C=1.0, cache_size=200, kernel='linear')
    clf.fit(X_train, y_train)
    score = clf.score(X_test, y_test) * 100.0
    accuracy_softmax_std.append(score)
    print('delay', delay)
    print('score', score)

plt.plot(delays, accuracy_softmax, 'o-', lw=2, markersize=10., label='Accuracy')
plt.plot(delays, accuracy_softmax_std, 'o-', lw=2, markersize=10, label='Standarized Representations')
plt.xlabel('Delays')
plt.ylim([0, 105])
plt.xlim([-0.5, 10])
plt.ylabel('Accuracy %')
plt.title('Delays vs Accuracy (Softmax)')
fig = plt.gcf()
fig.set_size_inches((12, 9))
plt.legend()

"""
Explanation: Standarized predictions with softmax
End of explanation
"""


