import numpy as np
import math
import random
import string
from scipy import optimize

%matplotlib inline
import matplotlib.pyplot as plt
from IPython.html.widgets import interact

from sklearn.datasets import load_digits
digits = load_digits()
print(digits.data.shape)

"""
Explanation: Progress report
Neural Networks
Sara Jones

Abstract
This project will take hand written digits 0 to 9 and recognize them through a computer-learning program. The neural network will require a training sets to 'teach' the network how to recognize the indiviualites between the diffrent digits and return the proper identification. The Network will be required to know the diffrences between the diffrent styles of handwriting (such as bars or no bars in sevens) and account for other factors such as messy handwriting. these factors will be determined by giving weights to the characteristics of each digits (accounting for various stylization diffrences) to detrmine what factors are important for identification of a digit and what can be given less weight or even ignored in identifcation.
Base question
The base question fir this project is taking hand written numbers and recognizing the through a neural network. This will require a computerized learning system that must be trained to recognize the digits. This network should have over 90% accuracy when recognizing hand written digits.   
Additional questions
This project will also attempt to take hand written numbers that are more then one digit (10 or grater) and recognize them. This will have to take in to account stylization factors such as comas in writing larger numbers and spacing between digits. This network will also attempt to integrate more hidden layers in to the network to train and work with more accuracy and efficiency.   
Packages and Libraries Needed
End of explanation
"""


"""Neron for for detrminig what is put in and the weights of certin aspects of the input"""
def __init__(self, n_inputs ):
    self.n_inputs = n_inputs
    self.set_weights( [random.uniform(0,1) for x in range(0,n_inputs+1)] )
            
def sum(self, inputs ):
        return sum(val*self.weights[i] for i,val in enumerate(inputs))

def set_weights(self, weights ):
    self.weights = weights
        
def str_(self):
    return ( str(self.weights[:-1]),str(self.weights[-1]) )     

"""
Explanation: Core Algorithms
Neuron
End of explanation
"""


def init(self, n_neurons, n_inputs):
    self.n_neurons = n_neurons
    self.neurons = [Neuron( n_inputs ) for _ in range(0,self.n_neurons)]

def str_(self):
    return 'Layer:\n\t'+'\n\t'.join([str(neuron) for neuron in self.neurons])+''

"""
Explanation: Neuron Layer
End of explanation
"""


def learn(self, X, y, learning_rate=0.2, epochs=10000):
    X = np.atleast_2d(X)
    temp = np.ones([X.shape[0], X.shape[1]+1])
    temp[:, 0:-1] = X  
    X = temp
    y = np.array(y)

    for i in range(epochs):
        k = np.random.randint(X.shape[0])
        a = [X[i]]

        for j in range(len(self.weights)):
            a.append(self.activation(np.dot(a[j], self.weights[j])))
        error = y[i] - a[-1]
        deltas = [error * self.activation_deriv(a[-1])]

        for i in range(len(a) - 2, 0, -1):
            deltas.append(deltas[-1].dot(self.weights[i].T)*self.activation_deriv(a[i]))
        deltas.reverse()
        
        for i in range(len(self.weights)):
            layer = np.atleast_2d(a[i])
            delta = np.atleast_2d(deltas[i])
            self.weights[i] += learning_rate * layer.T.dot(delta)
        
        return self.weights[i]

"""
Explanation: Training Set
End of explanation
"""


def show_digit(i):
    plt.matshow(digits.images[i]);

print (show_digit(0))

print (show_digit(1))

print (show_digit(2))

print (show_digit(3))

print (show_digit(4))

print (show_digit(5))

print (show_digit(6))

print (show_digit(7))

print (show_digit(8))

print (show_digit(9))

interact(show_digit, i=(0,100));

"""
Explanation: Visualizations
Can be used for training set and learning to recognize the characteristics of each digit. This will be used as part of the training set.
End of explanation
"""


