# -*- coding: utf-8 -*-
"""
Created on Sun Nov 05 21:20:13 2017

@author: Jordan
Results analysis on RF Results on wrist. Recorded Nov 2017 with wrist mounted device
"""

from __future__ import division
import numpy as np
import glob, os
import matplotlib.pyplot as plt
import plotly.plotly as py
import plotly.graph_objs as go

path = '../Investigation2/'
fileCoeff = 'coeff.csv'
fileRaw = 'raw.csv'
fileScaled = 'scaled.csv'
fileCam = 'webcam.csv'

coeff = np.genfromtxt(path+fileCoeff,delimiter=',')
raw = np.genfromtxt(path+fileRaw,delimiter=',')
scaled = np.genfromtxt(path+fileScaled,delimiter=',')
camDistances = np.genfromtxt(path+fileCam,delimiter=',')

slopes = coeff[0,:]
intercepts = coeff[1,:]

distances = range(5,55,5) # Distance axis
trialNo = range(1,raw.shape[1]+1)


print('slope stats: mean:%.2f stdev:%.2f min:%.2f max:%.2f'%(np.mean(slopes),np.std(slopes),np.max(slopes),np.min(slopes)))

#%% Functions
def makeHeatmap(data,distances,trialNo,nameOut,vmax):
    data = np.abs(data)
    fig,axis = plt.subplots()
    heatmap = axis.pcolor(data,cmap=plt.cm.Blues,vmax=vmax)
    axis.set_yticks(np.arange(data.shape[0])+0.5, minor=False)
    axis.set_xticks(np.arange(data.shape[1])+0.5, minor=False)
    plt.title('Heatmap'+nameOut)
    plt.ylabel('Distance (cm)')
    plt.xlabel('Trial Number')
    axis.set_yticklabels(distances)
    axis.set_xticklabels(trialNo)
    plt.colorbar(heatmap)
    plt.savefig('Heatmap'+nameOut+'.png',dpi=100)

#%% Plot Slope data
N = len(slopes)
ind = np.arange(N)
width = 0.5
fig, ax = plt.subplots()
rects1 = ax.bar(ind,slopes,width,color='r')
ax.set_title('Slope values for %i Trials'%N,fontsize=16)
ax.set_ylabel('slope value')
ax.set_xlabel('Trial')
plt.show()

N = len(intercepts)
ind = np.arange(N)
width = 0.5
fig, ax = plt.subplots()
rects1 = ax.bar(ind,intercepts,width,color='r')
ax.set_title('Intercept values for %i Trials'%N,fontsize=16)
ax.set_ylabel('Intercept value, cm')
ax.set_xlabel('Trial')
plt.show()

#%% Scale Data using a room average
avSlope = np.mean(slopes)
avIntercept = np.mean(intercepts)
roomScaled = (raw - avIntercept) / avSlope # Scale our values with an average slope and intercept value for the N trials
np.savetxt(path+'roomScaled.csv',roomScaled,delimiter=',')

#%% Error comparison
errorScaled = scaled - camDistances
errorRoomAvg = roomScaled - camDistances
errorRoomAvgRel = (roomScaled - camDistances)/camDistances # Relative error or % error
errorRaw = raw - camDistances
maxError = np.max((errorScaled,errorRoomAvg,errorRaw))

#%% Heatmaps

makeHeatmap(errorScaled,distances,trialNo,'Scaled',maxError)
makeHeatmap(errorRoomAvg,distances,trialNo,'Avg',maxError)
makeHeatmap(errorRaw,distances,trialNo,'Raw',maxError)

#%% Histogram
fig1 = plt.figure()
plt.hist((errorRoomAvg),bins=3)
plt.title('Error Histogram')

#%% Examine error with distance
errorWDistance=np.mean(np.abs(errorRoomAvg),axis=1)


Ndistances = len(errorWDistance)
ind = np.arange(Ndistances)
width = 0.5
fig, ax = plt.subplots()
rects1 = ax.bar(ind,errorWDistance,width,color='r')
ax.set_title('Error at different distances %i Trials'%N,fontsize=16)
ax.set_ylabel('Average error, cm')
ax.set_xlabel('Distance (cm)')
ax.set_xticklabels(distances)
ax.set_xticks(np.arange(Ndistances), minor=False)
plt.show()

# Error as a function of angle
errorWTrials = np.mean(np.abs(errorRoomAvg),axis=0)
angles=[0,0,22.5,22.5,45,45,67.5,67.5,0,0,22.5,22.5,45,45,67.5,67.5,0,22.5,45,67.5]
covered=[0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0]
fig2 = plt.figure()
plt.scatter(angles,errorWTrials)
plt.title('Error With Angle')

fig3 = plt.figure()
plt.scatter(covered,errorWTrials)
plt.title('Error of covered and non covered antenna')

#%% Averaged bar charts - Angles
indAngle = []
for angle in angles:
	indAngle.append(angle/22.5)
angleLabels=['0','22.5','45','67.5']
e1=[]
e2=[]
e3=[]
e4=[]
errorWAngle=[e1,e2,e3,e4]

for i in range(len(indAngle)):
	ind = int(indAngle[i])
	val = errorWTrials[i]
	errorWAngle[ind].append(val)

erroravgWAngle = np.mean(errorWAngle,axis=1)

Ndistances = len(erroravgWAngle)
ind = np.arange(Ndistances)
width = 0.5
fig, ax = plt.subplots()
rects1 = ax.bar(ind,erroravgWAngle,width,color='r')
ax.set_title('Error at different distances for %i Trials'%N,fontsize=16)
ax.set_ylabel('Average error, cm')
ax.set_xlabel('Angle')
ax.set_xticklabels(angleLabels)
ax.set_xticks(np.arange(Ndistances), minor=False)
plt.show()

#%% Obstruction: Covered vs Uncovered

angleLabels=['Uncovered','Covered']
e1=[]
e2=[]
errorWCover=[e1,e2]

for i in range(len(covered)):
	ind = int(covered[i]) # flag for covered or not (covered=1)
	val = errorWTrials[i] # Average value for that trial
	errorWCover[ind].append(val)

erroravgWCover=[]
erroravgWCover.append(np.mean(errorWCover[0]))
erroravgWCover.append(np.mean(errorWCover[1]))

Ndistances = len(angleLabels)
ind = np.arange(Ndistances)
width = 0.5
fig, ax = plt.subplots()
rects1 = ax.bar(ind,erroravgWCover,width,color='r')
ax.set_title('Covered and uncovered Antenna, for %i Trials'%N,fontsize=16)
ax.set_ylabel('Average error, cm')
ax.set_xlabel('Angle')
ax.set_xticklabels(angleLabels)
ax.set_xticks(np.arange(Ndistances), minor=False)
plt.show()

# Export results for BIOROB
outputArray = np.array((errorWTrials,covered))
np.savetxt('errorCovered.csv',np.transpose(outputArray),delimiter=',')

#%% Output for BIOROB
# Create masks
maskCovered = ()
maskUncovered = ()
for i in range(len(covered)):
	if covered[i] == 1:
		maskCovered = maskCovered + (i,)
	else:
		maskUncovered = maskUncovered + (i,)
errorCovered = errorRoomAvg[:,maskCovered]
errorUncovered = errorRoomAvg[:,maskUncovered]
data = [[i for row in errorCovered for i in row],[i for row in errorUncovered for i in row]]
dataOut = np.hstack((np.array(data[0]),np.array(data[1])))
dataOut = np.reshape(dataOut,(len(dataOut),1))
label=[1 for i in data[0]] + [0 for i in data[1]]
dataLabel = np.array(label)
dataLabel = np.reshape(dataLabel,(len(dataLabel),1))
dataOut = np.hstack((dataOut,dataLabel))
np.savetxt('errorCovered.csv',dataOut, delimiter = ',')
