import pandas as pd

import matplotlib.pyplot as plt
from sklearn import svm

"""
Explanation: DAT210x - Programming with Python for DS
Module6- Lab2
End of explanation
"""


def load(path_train, path_test):
    # Load up the data.
    
    # You probably could have written this easily:
    with open(path_test, 'r')  as f: testing  = pd.read_csv(f)
    with open(path_train, 'r') as f: training = pd.read_csv(f)

    # The number of samples between training and testing can vary
    # But the number of features better remain the same!
    n_features = testing.shape[1]

    X_test  = testing.ix[:,:n_features-1]
    X_train = training.ix[:,:n_features-1]
    y_test  = testing.ix[:,n_features-1:].values.ravel()
    y_train = training.ix[:,n_features-1:].values.ravel()

    # Special:
    # ...
    
    return X_train, X_test, y_train, y_test

def peekData(X_train):
    # The 'targets' or labels are stored in y. The 'samples' or data is stored in X
    print("Peeking your data...")
    fig = plt.figure()
    fig.set_tight_layout(True)

    cnt = 0
    for col in range(5):
        for row in range(10):
            plt.subplot(5, 10, cnt + 1)
            plt.imshow(X_train.ix[cnt,:].reshape(8,8), cmap=plt.cm.gray_r, interpolation='nearest')
            plt.axis('off')
            cnt += 1
            
    plt.show()

def drawPredictions(X_train, X_test, y_train, y_test):
    fig = plt.figure()
    fig.set_tight_layout(True)
    
    # Make some guesses
    y_guess = model.predict(X_test)

    # INFO: This is the second lab we're demonstrating how to
    # do multi-plots using matplot lab. In the next assignment(s),
    # it'll be your responsibility to use this and assignment #1
    # as tutorials to add in the plotting code yourself!
    num_rows = 10
    num_cols = 5

    index = 0
    for col in range(num_cols):
        for row in range(num_rows):
            plt.subplot(num_cols, num_rows, index + 1)

            # 8x8 is the size of the image, 64 pixels
            plt.imshow(X_test.ix[index,:].reshape(8,8), cmap=plt.cm.gray_r, interpolation='nearest')

            # Green = Guessed right
            # Red = Fail!
            fontcolor = 'g' if y_test[index] == y_guess[index] else 'r'
            plt.title('Label: %i' % y_guess[index], fontsize=6, color=fontcolor)
            plt.axis('off')
            index += 1
    plt.show()

"""
Explanation: The dataset used in this lab comes from https://archive.ics.uci.edu/ml/datasets/Optical+Recognition+of+Handwritten+Digits
At face value, this looks like an easy lab, but it has many parts to it, so prepare yourself by reading through it fully before starting.
Convenience Functions
End of explanation
"""


# TODO: Pass in the file paths to the .tra and the .tes files:
X_train, X_test, y_train, y_test = load('', '')

"""
Explanation: The Assignment
End of explanation
"""


peekData(X_train)

"""
Explanation: Get to know your data. It seems its already well organized in [n_samples, n_features] form. Your dataset looks like (4389, 784). Also your labels are already shaped as [n_samples].
End of explanation
"""


print("Training SVC Classifier...")

# .. your code here ..

"""
Explanation: Create an SVC classifier. Leave C=1, but set gamma to 0.001 and set the kernel to linear. Then train the model on the training data and labels:
End of explanation
"""


print("Scoring SVC Classifier...")

# .. your code here ..
print("Score:\n", score)

# Let's get some visual confirmation of accuracy:
drawPredictions(X_train, X_test, y_train, y_test)

"""
Explanation: Calculate the score of your SVC against the testing data:
End of explanation
"""


# .. your code here ..

print("1000th test label: ", true_1000th_test_value)

"""
Explanation: Print out the TRUE value of the 1000th digit in the test set. By TRUE value, we mean, the actual provided, ground-truth label for that sample:
End of explanation
"""


# .. your code here ..

print("1000th test prediction: ", guess_1000th_test_value)

"""
Explanation: Predict the value of the 1000th digit in the test set. Was your model's prediction correct? If you get a warning on your predict line, look at the notes from the previous module's labs.
End of explanation
"""


# .. your code here ..

"""
Explanation: Use imshow() to display the 1000th test image, so you can visually check if it was a hard image, or an easy image:
End of explanation
"""


