<reponame>ritvik-chebolu/Scavenger-Hunt
# -*- coding: utf-8 -*-
"""Scavenger_Hunt

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1wb_PSlczHhRMUIoOzwW7voROtx_51R6T
"""

import matplotlib.pyplot as plt
import numpy as np

def read_waveforms(LA, RV, RA) :
    infile = open('D:\Grad\Sem 2\Visual Analytics\Week 2\waveforms.csv', 'r')

    line = infile.readline()
    wf = 0 # which waveform are we trying to read (0 = LA, 1 = RV, 2 = RA)

    while line :
        line = line.strip()
        data = line.split(',')

        for i in range(0, len(data)) : 
            data[i] = float(data[i])

        if(wf == 0) :
            LA.append(data)
        elif(wf == 1) :
            RV.append(data)
        elif(wf == 2) :
            RA.append(data)
        
        wf = (wf + 1) % 3
        line = infile.readline()

    infile.close()

def read_times(TL, TR) :
    infile = open('D:\Grad\Sem 2\Visual Analytics\Week 2\times.csv', 'r')
    line = infile.readline()
    data = line.strip().split(',')

    for i in range(0, len(data)) : 
        data[i] = float(data[i])*1000

    TL.append(data)

    line = infile.readline()
    data = line.strip().split(',')

    for i in range(0, len(data)) : 
        data[i] = float(data[i])*1000

    TR.append(data)
    infile.close()

def plot_waveforms(LA, RV, RA, TL, TR) :
    print('create your individual waveform plots here')

# make empty data and time Lists
LA_list = []
RV_list = []
RA_list = []
TL_list = []
TR_list = []

read_waveforms(LA_list, RV_list, RA_list)
read_times(TL_list, TR_list)

# convert all data and time lists to numpy arrays for plotting
LA = np.array(LA_list)
RV = np.array(RV_list)
RA = np.array(RA_list)
TL = np.array(TL_list[0])
TR = np.array(TR_list[0])

print("LA Mean:", np.mean( LA[2,:] ))
print("RV Mean:", np.mean( RV[2,:] ))
print("RA Mean:", np.mean( RA[2,:] ))

print("LA Min:", np.min( LA[2,:] ))
print("RV Min:", np.min( RV[2,:] ))
print("RA Min:", np.min( RA[2,:] ))

print("LA Max:", np.max( LA[2,:] ))
print("RV Max:", np.max( RV[2,:] ))
print("RA Max:", np.max( RA[2,:] ))

# to see the 7th instance
plt.plot(TL, LA[8, :])
plt.title('Linear Acceleration of Instance 9')
plt.xlabel('Time (ms)')
plt.ylabel('Lin Accel (g)')
plt.xticks(np.arange(0, 55, step=5))
plt.show()

num_instances = len(LA)

for i in range(0, num_instances):
  
  plt.subplot( 311 )
  plt.tight_layout()
  #<plot commands for linear acceleration waveform>
  plt.plot(TL, LA[i, :])
  plt.title('Linear Acceleration, Rotational Velocity and Rotational Acceleration of Instance ' + str(i+1))
  plt.ylabel('Lin Accel (g)')
  plt.xticks(np.arange(0, 55, step=5))  
  
  plt.subplot( 312 )
  plt.subplots_adjust(hspace=0.2)
  #<plot commands for rotational velocity waveform>
  plt.plot(TR, RV[i, :])
  #plt.title('Rotational Velocity of Instance ' + str(i+1))
  plt.ylabel('Rot Vel (rad/sec)')
  plt.xticks(np.arange(0, 55, step=5))

  plt.subplot( 313 )
  plt.subplots_adjust(hspace=0.2)
  #<plot command for rotational acceleration waveform>
  plt.plot(TR, RA[i, :])
  #plt.title('Rotational Acceleration of Instance ' + str(i+1))
  plt.xlabel('Time (ms)')
  plt.ylabel('Rot Accel (rad/sec^2)')
  plt.xticks(np.arange(0, 55, step=5))

  plt.savefig('Instance ' + str(i + 1) + '.png')
  plt.show()

MLA = []
ALA = []
PLA = []
MRV = []
ARV = []
PRV = []
MRA = []
ARA = []
PRA = []

for i in range(len(LA)):
    MLA.append(np.min(LA[i, :]))
    ALA.append(np.mean(LA[i, :]))
    PLA.append(np.max(LA[i, :]))
    MRV.append(np.min(RV[i, :]))
    ARV.append(np.mean(RV[i, :]))
    PRV.append(np.max(RV[i, :]))
    MRA.append(np.min(RA[i, :]))
    ARA.append(np.mean(RA[i, :]))
    PRA.append(np.max(RA[i, :]))

print("MLA: min = " + str(np.min(MLA)) + ", max = " + str(np.max(MLA)) + ", avg = " + str(np.mean(MLA)))
print("ALA: min = " + str(np.min(ALA)) + ", max = " + str(np.max(ALA)) + ", avg = " + str(np.mean(ALA)))
print("PLA: min = " + str(np.min(PLA)) + ", max = " + str(np.max(PLA)) + ", avg = " + str(np.mean(PLA)))

print("\nMRV: min = " + str(np.min(MRV)) + ", max = " + str(np.max(MRV)) + ", avg = " + str(np.mean(MRV)))
print("ARV: min = " + str(np.min(ARV)) + ", max = " + str(np.max(ARV)) + ", avg = " + str(np.mean(ARV)))
print("PRV: min = " + str(np.min(PRV)) + ", max = " + str(np.max(PRV)) + ", avg = " + str(np.mean(PRV)))

print("\nMRA: min = " + str(np.min(MRA)) + ", max = " + str(np.max(MRA)) + ", avg = " + str(np.mean(MRA)))
print("ARA: min = " + str(np.min(ARA)) + ", max = " + str(np.max(ARA)) + ", avg = " + str(np.mean(ARA)))
print("PRA: min = " + str(np.min(PRA)) + ", max = " + str(np.max(PRA)) + ", avg = " + str(np.mean(PRA)))

print()
plt.xlim(0, 66)
plt.xticks(np.arange(0, 66, step=5))
plt.plot(np.arange(0,65, step = 1), ARA)
plt.title('Average Rotational Acceleration')
plt.xlabel('Instance')
plt.ylabel('Rot Accel (rad/sec^2)')
plt.savefig('ARA.png')
plt.close()

# Part 4
plt.scatter(PLA, PRA)
plt.savefig('PLAvsPRA.png')
plt.xlabel("PLA")
plt.ylabel("PRA")

plt.scatter(PLA, PRV)
plt.savefig('PLAvsPRV.png')
plt.xlabel("PLA")
plt.ylabel("PRV")

plt.scatter(PRA, PRV)
plt.savefig('PRAvsPRV.png')
plt.xlabel("PRA")
plt.ylabel("PRV")

# Instance number of top 5 largest feature values

sort_index_pra = np.flip(np.argsort(np.array(PRA))[-5:]) + 1
sort_index_pla = np.flip(np.argsort(np.array(PLA))[-5:]) + 1
sort_index_prv = np.flip(np.argsort(np.array(PRV))[-5:]) + 1

print(sort_index_pra)
print(sort_index_pla)
print(sort_index_prv)

#PRA.PLA