####################################################################################################################################################  
#                                                                                                                                                  #
# This file has been generated by Kevin Koh:                                                                                                       #
#     Created by              : Kevin Koh                                                                                                          #    
#     Created on              : 15-02-2014                                                                                                         #
#     Directory               : /Library/                                                                                                          #
#     Purpose                 : This file will perform analysis on user test scores                                                                #
#                                                                                                                                                  #
####################################################################################################################################################

from Values import *
from Key import *
from Scored import *
from Summary import *
from Data import *
from Graph import *
import math
import csv

#Main analytics runner
class Analyze(object):

	#This is the default constructor
	def __init__(self):
		self.dataSet = [] # List of data values
		self.debug = False

	def setDebug(self, status):
		self.debug = status

	def printDebug(self, statement):
		if self.debug:
			print (statement)

	def importData(self, data):
		self.dataSet.append(data)

	def runConsistencyCheck():
		pass
		#Yet to implement

	def calculateSD(self, dataPoints):
		if type(dataPoints) is list:
			runningSum = 0
			total = 0
			for point in dataPoints:
				runningSum += point
				total += 1
			mean = runningSum/total
			variance = 0
			count = 0
			for point in dataPoints:
				variance += pow(mean - point, 2)
				count += 1
			variance = variance/count
			standardDeviation = math.sqrt(variance)
			return standardDeviation
		elif type(dataPoints) is dict:
			runningSum = 0
			total = 0
			for point in dataPoints.keys():
				runningSum += dataPoints[point]
				total += 1
			mean = runningSum/total
			variance = 0
			count = 0
			for point in dataPoints.keys():
				variance += pow(mean - dataPoints[point], 2)
				count += 1
			variance = variance/count
			standardDeviation = math.sqrt(variance)
			return standardDeviation		

	def missVariance(self, type):
		dataDict = getSectionInfo(type, "Miss")
		meanDict = {}
		totalMean = 0
		for typeNum in dataDict.keys():
			sumType = 0
			for x in dataDict[typeNum]:
				sumType += x
			mean = sumType/len(dataDict[typeNum])
			meanDict[typeNum] = mean
			totalMean += mean
		totalMean = totalMean/len(dataDict.keys())
		var = 0
		for key in meanDict.keys():
			var += pow(totalMean - meanDict[key], 2)
		var = var/len(meanDict.keys())
		standardDev = math.sqrt(var)
		# determine if any section types fall too far away from the standard deviation
		#Unfinished

	def getSectionInfo(self, type, qStat):

		if qStat == "Miss":
			stat = 'm'
		elif qStat == "Correct":
			stat = 'c'
		elif qStat == "Blank":
			stat = 'b'
		elif qStat == "Score":
			stat = 's'

		if type == WRITING_TYPE:
			size = WRITING_TYPES
			tag = "W"
		elif type == MATH_TYPE:
			size == MATH_TYPES
			tag = "M"
		elif type == READING_TYPE:
			size == READING_TYPES
			tag = "R"

		outputDict = {}

		for data in dataSet:
			for i in range(1, size + 1):
				fulltag = tag + str(i)
				printDebug("Looking in " + fulltag)
				questionStats = data[type].stats[fulltag]
				if stat == 's':
					number = scoreCalculate(questionStats.c, questionStats.m, questionStats.t)
				else:
					number = questionStats.qStat/questionStats.t
				if fulltag in outputDict.keys():
					outputDict[fulltag].append(number)
				else:
					intoDict = []
					intoDict.append(number)
					outputDict[fulltag] = intoDict

	def scoreCalculate(correct, miss, total):
		score = correct - miss/4
		# This may have to be more extensively customized based upon subject test,
		# section specific scoring, etc.










		