import datetime
import gc
import numpy as np
import os
import random
from scipy import misc
import string
import time
import sys
import sklearn.metrics as skm
import collections
from sklearn.svm import SVC
import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot as plt
from sklearn import metrics
import dwdii_bc_model_helper_old as bc

random.seed(20275)
np.set_printoptions(precision=2)

"""
Explanation: 3 Classes (normal, benign, malignant) Prediction with SVM
A support vector classification machine with the RBF Kernel (C=1 and gamma=0.001) was built here. And two sets of image data were tested with the model. 

For Raw DDSM images, SVM model had an overall accuracy of 53.9%.
For Threshold images, SVM model had an overall accuracy of 54.3%.
End of explanation
"""


imagePath = "png"
trainDataPath = "data/ddsm_train.csv"
testDataPath = "data/ddsm_test.csv"
imgResize = (150, 150)

os.listdir('data')

metaData, meta2, mCounts = bc.load_training_metadata(trainDataPath, balanceViaRemoval=True, verbose=True)

# Actually load some representative data for model experimentation
maxData = len(metaData)
X_data, Y_data = bc.load_data(trainDataPath, imagePath, 
                              maxData = maxData, 
                              verboseFreq = 50, 
                              imgResize=imgResize)
print X_data.shape
print Y_data.shape

# Actually load some representative data for model experimentation
maxData = len(metaData)
X_test, Y_test = bc.load_data(testDataPath, imagePath, 
                              maxData = maxData, 
                              verboseFreq = 50, 
                              imgResize=imgResize)
print X_test.shape
print Y_test.shape

X_train = X_data
Y_train = Y_data

print X_train.shape
print X_test.shape

print Y_train.shape
print Y_test.shape

def yDist(y):
    bcCounts = collections.defaultdict(int)
    for a in range(0, y.shape[0]):
        bcCounts[y[a][0]] += 1
    return bcCounts

print "Y_train Dist: " + str(yDist(Y_train))
print "Y_test Dist: " + str(yDist(Y_test))

# Load the bc array for our count in the model definition
bcTypes = bc.bcNumerics()
print bcTypes
print len(bcTypes)

X_train_s = X_train.reshape((2132,-1))

X_test_s = X_test.reshape((536,-1))

Y_train_s = Y_train.ravel()

model = SVC(C=1.0, gamma=0.001, kernel='rbf')

model.fit(X_train_s,Y_train_s)

predicted = model.predict(X_test_s)
expected = Y_test

svm_matrix = skm.confusion_matrix(Y_test, predicted)
svm_matrix

print metrics.accuracy_score(expected,predicted)

numBC = bc.numericBC()
class_names = numBC.values()
np.set_printoptions(precision=2)

# Plot non-normalized confusion matrix
plt.figure()
bc.plot_confusion_matrix(svm_matrix, classes=class_names,
                      title='Confusion Matrix without normalization')
plt.savefig('raw_class3_o_norm.png')

from IPython.display import Image
Image(filename='raw_class3_o_norm.png')

plt.figure()
bc.plot_confusion_matrix(svm_matrix, classes=class_names, normalize=True,
                      title='Confusion Matrix with normalization')
plt.savefig('raw_class3_norm.png')

# Load the image we just saved
from IPython.display import Image
Image(filename='raw_class3_norm.png')

"""
Explanation: Raw DDSM images
End of explanation
"""


imagePath = "DDSM_threshold"
trainDataPath = "data/ddsm_train.csv"
testDataPath = "data/ddsm_test.csv"
imgResize = (150, 150)

os.listdir('data')

metaData, meta2, mCounts = bc.load_training_metadata(trainDataPath, balanceViaRemoval=True, verbose=True)

# Actually load some representative data for model experimentation
maxData = len(metaData)
X_data, Y_data = bc.load_data(trainDataPath, imagePath, 
                              maxData = maxData, 
                              verboseFreq = 50, 
                              imgResize=imgResize)
print X_data.shape
print Y_data.shape

# Actually load some representative data for model experimentation
maxData = len(metaData)
X_test, Y_test = bc.load_data(testDataPath, imagePath, 
                              maxData = maxData, 
                              verboseFreq = 50, 
                              imgResize=imgResize)
print X_test.shape
print Y_test.shape

X_train = X_data
Y_train = Y_data

print X_train.shape
print X_test.shape

print Y_train.shape
print Y_test.shape

def yDist(y):
    bcCounts = collections.defaultdict(int)
    for a in range(0, y.shape[0]):
        bcCounts[y[a][0]] += 1
    return bcCounts

print "Y_train Dist: " + str(yDist(Y_train))
print "Y_test Dist: " + str(yDist(Y_test))

# Load the bc array for our count in the model definition
bcTypes = bc.bcNumerics()
print bcTypes
print len(bcTypes)

X_train_s = X_train.reshape((2024,-1))
X_test_s = X_test.reshape((516,-1))
Y_train_s = Y_train.ravel()

model = SVC(C=1.0, gamma=0.001, kernel='rbf')

model.fit(X_train_s,Y_train_s)

predicted = model.predict(X_test_s)
expected = Y_test

svm_matrix = skm.confusion_matrix(Y_test, predicted)
svm_matrix

print metrics.accuracy_score(expected,predicted)

numBC = bc.numericBC()
class_names = numBC.values()
np.set_printoptions(precision=2)

# Plot non-normalized confusion matrix
plt.figure()
bc.plot_confusion_matrix(svm_matrix, classes=class_names,
                      title='Confusion Matrix without normalization')
plt.savefig('threshold_class3_o_norm.png')

from IPython.display import Image
Image(filename='threshold_class3_o_norm.png')

plt.figure()
bc.plot_confusion_matrix(svm_matrix, classes=class_names, normalize=True,
                      title='Confusion Matrix with normalization')
plt.savefig('threshold_class3_norm.png')

# Load the image we just saved
from IPython.display import Image
Image(filename='threshold_class3_norm.png')

"""
Explanation: Threshold Images
End of explanation
"""


