From 733d68ee2586254274282381647f8be93c3c74c6 Mon Sep 17 00:00:00 2001 From: Mo <42825943+lemo202@users.noreply.github.com> Date: Wed, 28 Nov 2018 16:15:38 -0500 Subject: [PATCH] Add files via upload --- GUI/GUIFunctions.py | 497 ++++++++++++++++++++++++++++++++++++ GUI/GUI_final.py | 528 +++++++++++++++++++++++++++++++++++++++ GUI/multi_param_learn.py | 369 +++++++++++++++++++++++++++ 3 files changed, 1394 insertions(+) create mode 100644 GUI/GUIFunctions.py create mode 100644 GUI/GUI_final.py create mode 100644 GUI/multi_param_learn.py diff --git a/GUI/GUIFunctions.py b/GUI/GUIFunctions.py new file mode 100644 index 0000000..df1a0ec --- /dev/null +++ b/GUI/GUIFunctions.py @@ -0,0 +1,497 @@ +""" +CS 50100 Group 16 +Written by Ashley Dicks + +Script to be used with GUI for mavlink UAV data to be analyized + +By: +1. Extract raw drone data from Mission Planner output +2. Be able to user specify the parameter +3. Create a new data array with only this parameter +4. Use the automatic splice method to detect disturbances +5. Run the unlabled disturbances through the ML algorithm to lable + +Functions: +* csvParaExtract(): +* plotParaData(): +* + +""" + +from autoSpliceClass import AnomalyDetection +from matplotlib import pyplot +import math +from matplotlib.collections import LineCollection +from matplotlib.colors import ListedColormap, BoundaryNorm +import matplotlib.patches as mpatches + +import numpy as np +import datetime +import os +import random ## for fake labels ## + +# import mavlink parameter format +from MavlinkParameters import mavlink_types +from MavlinkParameters import mavlink_param +from MavlinkParameters import mavlink_index +from MavlinkParameters import mavlink_rate +## RAW DATA EXTRACTION FUNCTIONS ## + +# function to create time series for flight of specified paramter +def csvParaExtract(file_name,para_name): + # Create Data + mavlink_data = [] + for typ in range(len(mavlink_types)): + typdata = [] + for _ in range(len(mavlink_param[typ])): + typdata.append([]) + mavlink_data.append(typdata) + + #read raw data file + filelist = [] + with open(file_name) as f: + for line in f: + filelist.append(line.split(',')) + f.close() + print("Generating time series for "+para_name+" from file "+file_name) + # read all data from file into parameter lists + for row in filelist: + for typ in range(len(mavlink_types)): + if (str(row[9]) == mavlink_types[typ]): + for param in range(len(mavlink_param[typ])): + if(param==0): + mavlink_data[typ][param].append(row[mavlink_index[typ][param]-1]) + else: + mavlink_data[typ][param].append(float(row[mavlink_index[typ][param]-1])) + data = [] + for typ in range(len(mavlink_types)): + for param in range(len(mavlink_param[typ])): + if mavlink_param[typ][param] == para_name: + for i in range(len(mavlink_data[typ][param])): + data.append(mavlink_data[typ][param][i]) + rate = mavlink_rate[typ] + mav_type = mavlink_types[typ] + + print("Done collecting "+para_name+" data!") + return data,rate,mav_type + +# funciton to plot specified parameter time series data +def plotParaData(data): + pyplot.figure(1) + pyplot.plot(data) + pyplot.show() + + +## SPLICING FUNCTIONS ## + +# manually select individual anomalies from TS data +def manParseData(data,name): + x_pts = [] + y_pts = [] + fig, ax = pyplot.subplots(figsize=(15, 7)) + ax.plot(data) + pyplot.title("Complete Time Series (Parameter: "+ name+")", size=20) + txt = "INSTRUCTIONS: Click on points inbetween each anomaly viewed, starting at the front of the first anomaly and ending at the end of the last anomaly. EXIT when done! " + fig.text(.5, .05, txt, ha='center') + line, = ax.plot(x_pts, y_pts, marker="o") + # function used for manual selection + def onpick(event): + m_x, m_y = event.x, event.y + x, y = ax.transData.inverted().transform([m_x, m_y]) + x_pts.append(x) + y_pts.append(y) + line.set_xdata(x_pts) + line.set_ydata(y_pts) + fig.canvas.draw() + fig.canvas.mpl_connect('button_press_event', onpick) + pyplot.show() + index_x = [int(x_pts[i]) for i in range(len(x_pts))] + anoms = [] + # create n new array for anamoly ts from dataset + for i in range(0, len(index_x) - 1): + # x_pts are where anomalies begin and end + start = index_x[i] + end = index_x[i + 1] + anoms.append(data[start:end]) + return anoms, index_x,x_pts +def manParseData2(data,x_pts): + + index_x = [int(x_pts[i]) for i in range(len(x_pts))] + anoms = [] + # create n new array for anamoly ts from dataset + for i in range(0, len(index_x) - 1): + # x_pts are where anomalies begin and end + start = index_x[i] + end = index_x[i + 1] + anoms.append(data[start:end]) + return anoms, index_x + + +# clean TS data at 400 and -400 threshold +# for other parameters: +# should use user defined threshold +# based on visual TS plot (idea is to get rid of noise) +def cleanTSData(ts_data): + clean_data = [] + for i in range(len(ts_data)): + if ts_data[i] < -400 or ts_data[i] > 400: + clean_data.append(ts_data[i]) + else: + clean_data.append(0) + return clean_data + + +def parseData(data,index_x): + + anoms = [] + + for i in range(0,len(index_x)-1,2): + + start = index_x[i] + + end = index_x[i+1] + + anoms.append(data[start:end]) + + return anoms, index_x + +# creats an array of arrays containing only anomaly subsets of ts +# def parseData(data): +# array = [] +# firstZero = False +# newArray = [] +# lengthThreashHold = 10 +# zeroThreashHold = 5 +# zeroCount = 0 +# +# for i in range(len(data) - 1): +# if data[i] == 0 and firstZero and zeroCount >= zeroThreashHold: +# if len(newArray) >= lengthThreashHold: +# newArray.append(0) +# array.append(newArray) +# newArray = [0] +# firstZero = False +# zeroCount = 0 +# elif (data[i] != 0 and firstZero): +# newArray.append(data[i]) +# elif data[i] != 0 and firstZero == False: +# firstZero = True +# newArray.append(data[i]) +# zeroCount = 0 +# elif data[i] == 0: +# zeroCount += 1 +# +# if len(newArray) >= lengthThreashHold: +# newArray.append(0) +# array.append(newArray) +# return array + +# goes through anomalies and creates each subset to be the same length +def anomLen(anom,rate): + lens = [] + desire_len = int(rate*6) + if desire_len%2!=0: + desire_len=desire_len+1 + # print (desire_len) + mean = 0 + + + for j in range(len(anom)): + if anom[j] == []: + pass + else: + mean = mean + sum(anom[j]) / len(anom[j]) + ts_mean = mean / len(anom) + lens.append(len(anom[j])) + # print("mean:", ts_mean) + max_len = desire_len # max(lens) + for k in range(len(anom)): + + + if len(anom[k]) <= max_len: + + for l in range(len(anom[k]), max_len): + anom[k].append(ts_mean) + # print ("1",anom[k]) + + min_num1 = int(anom[k].index(max(anom[k])) - max_len / 2) + max_num1 = int(anom[k].index(max(anom[k])) + max_len / 2) + + if min_num1 < 0: + + for l in range(0, math.ceil(max_len / 2 - anom[k].index(max(anom[k]))) - 1): + anom[k].insert(0, ts_mean) + + anom[k] = anom[k][0:max_len] + + else: + + for l in range(0, math.ceil(anom[k].index(max(anom[k])) - max_len / 2)): + anom[k].append(ts_mean) + # anom[k].remove(anom[k][0]) + + anom[k] = anom[k][min_num1:max_num1] + # print("2",anom[k]) + + if len(anom[k]) > max_len: + # print ("3") + + min_num2 = int(anom[k].index(max(anom[k])) - max_len / 2) + max_num2 = int(anom[k].index(max(anom[k])) + max_len / 2) + + if min_num2 < 0: + for l in range(0, math.ceil(max_len / 2 - anom[k].index(max(anom[k]))) - 1): + anom[k].insert(0, ts_mean) + + anom[k] = anom[k][0:max_len] + + elif max_num2 > len(anom[k]): + for l in range(0, int(max_len / 2) - len(anom[k]) + anom[k].index(max(anom[k])) + 1): + anom[k].insert(len(anom[k]), ts_mean) + anom[k] = anom[k][min_num2 + 1:len(anom[k])] + else: + anom[k] = anom[k][min_num2:max_num2] + + + # lens = [] + # desire_len = int(rate*6) + # for j in range(len(anom)): + # lens.append(len(anom[j])) + # max_len = desire_len # max(lens) + # max_len_loc = lens.index(max(lens)) + # for k in range(len(anom)): + # if len(anom[k]) < max_len: + # for l in range(int((max_len - len(anom[k]))/2)): + # anom[k].append(0) + # # anom[k].insert(0,0) + # for k in range(len(anom)): + # if len(anom[k]) < max_len: + # for l in range(len(anom[k]), max_len): + # anom[k].append(0) + # # anom[k].insert(0,0) + # else: + # for l in range (max_len,len(anom[k])): + # anom[k].remove(anom[k][max_len]) + return anom + +# save anomalies to .txt files - to be used for learning algorithm +def saveAnom(para_name, eq_anom, mav_type): + # print("eq_anom1:",eq_anom) + filename = "test_" + str(mav_type) + "_" + str(para_name) + ".txt" + if os.path.exists(filename): + os.remove(filename) # remove if already exists + file = open(filename, 'w') + for i in range(len(eq_anom)): + file.write(' '.join(map(str, eq_anom[i]))) + file.write("\n") + file.close() + print("Disturbances successfully saved as: " + filename) + + return str(para_name) + +## LABEL ANALYSIS PLOTS/ FIGURES ## + +# basic plot of original time series data +def plotTSData(ts_data,paraname): + + pyplot.figure("Test data time series (parameter: "+ paraname+")") + pyplot.plot(ts_data) + pyplot.show() + + +# plot with disturbance type +def distLabel(data, index_x, labels, para_name): + colors = ["green", "red", "firebrick","orange","yellow","blue","navy","royalblue"] + + + colored_data = [] + for i in range(len(labels)): + if labels[i] == 1: + colored_data.append(colors[0]) + elif labels[i] == 2: + colored_data.append(colors[1]) + elif labels[i] == 3: + colored_data.append(colors[2]) + elif labels[i] == 4: + colored_data.append(colors[3]) + elif labels[i] == 5: + colored_data.append(colors[4]) + elif labels[i] == 6: + colored_data.append(colors[5]) + elif labels[i] == 7: + colored_data.append(colors[6]) + else: + colored_data.append(colors[7]) + + + # append end of TS color and x_val + index_x.append(len(data)) + colored_data.append("black") + + # legend info + red_patch = mpatches.Patch(color='red', label=' Impact (Front Left)') + green_patch = mpatches.Patch(color='green', label='Hover') + blue_patch = mpatches.Patch(color='blue', label='Gust (from Left)') + firebrick_patch = mpatches.Patch(color='firebrick', label='Impact (Front Right)') + navy_patch = mpatches.Patch(color='navy', label='Gust (from Right)') + orange_patch = mpatches.Patch(color='orange', label='Impact (Back Left)') + yellow_patch = mpatches.Patch(color='yellow', label='Impact (Back Right)') + royalblue_patch = mpatches.Patch(color='royalblue', label='Gust (from front)') + + + # plot + fig, ax = pyplot.subplots(figsize=(15, 7)) + ax.plot(data) + pp = pyplot.plot(data[0:index_x[0]], c="black") + fig.canvas.draw() + for j in range(0, len(index_x) - 1): + x_vals = [] + for k in range(index_x[j], index_x[j + 1]): + x_vals.append(k) + line, = ax.plot(x_vals, data[index_x[j]:index_x[j + 1]], c=colored_data[j]) + # pt, = ax.plot(x_vals[0],0,c="black",marker="o",markersize=6) + fig.canvas.draw() + pyplot.legend(handles=[green_patch,royalblue_patch,blue_patch,navy_patch,orange_patch, red_patch,firebrick_patch,yellow_patch]) + pyplot.title("Prediction result with parameter ("+str(para_name) + ")", size=24) + pyplot.xlabel("Time", size=12) + pyplot.ylabel(str(para_name), size=12) + pyplot.show() + +def distLabelAuto(data,index_x,labels,para_name): + colors = ["green", "red", "firebrick", "orange", "yellow", "blue", "navy", "royalblue"] + + colored_data = [] + for i in range(len(labels)): + if labels[i] == 1: + colored_data.append(colors[0]) + elif labels[i] == 2: + colored_data.append(colors[1]) + elif labels[i] == 3: + colored_data.append(colors[2]) + elif labels[i] == 4: + colored_data.append(colors[3]) + elif labels[i] == 5: + colored_data.append(colors[4]) + elif labels[i] == 6: + colored_data.append(colors[5]) + elif labels[i] == 7: + colored_data.append(colors[6]) + else: + colored_data.append(colors[7]) + + + # append end of TS color and x_val + index_x.append(len(data)) + colored_data.append("black") + + # legend info + red_patch = mpatches.Patch(color='red', label=' Impact (Front Left)') + green_patch = mpatches.Patch(color='green', label='Hover') + blue_patch = mpatches.Patch(color='blue', label='Gust (from Left)') + firebrick_patch = mpatches.Patch(color='firebrick', label='Impact (Front Right)') + navy_patch = mpatches.Patch(color='navy', label='Gust (from Right)') + orange_patch = mpatches.Patch(color='orange', label='Impact (Back Left)') + yellow_patch = mpatches.Patch(color='yellow', label='Impact (Back Right)') + royalblue_patch = mpatches.Patch(color='royalblue', label='Gust (from front)') + + + # plot + fig, ax = pyplot.subplots(figsize=(15, 7)) + ax.plot(data) + pp = pyplot.plot(data[0:index_x[0]],c="black") + fig.canvas.draw() + for j in range(0,len(index_x)-1,2): + x_vals = [] + x_vals_off =[] + for k in range(index_x[j],index_x[j+1]): + x_vals.append(k) + for l in range(index_x[j+1],index_x[j+2]): + x_vals_off.append(l) + line, = ax.plot(x_vals,data[index_x[j]:index_x[j+1]],c=colored_data[int(j/2)]) + line, = ax.plot(x_vals_off, data[index_x[j+1]:index_x[j+2]],c="black") + #pt, = ax.plot(x_vals[0],0,c="black",marker="o",markersize=6) + fig.canvas.draw() + pyplot.legend(handles=[green_patch,royalblue_patch,blue_patch,navy_patch,orange_patch, red_patch,firebrick_patch,yellow_patch]) + pyplot.title("Prediction result with parameter ("+str(para_name) + ")", size=24) + pyplot.xlabel("Time",size=12) + pyplot.ylabel(str(para_name),size=12) + pyplot.show() + + + +# MAIN FOR GUI +def main(file,paraname,paraname_readable): + + + # FROM GUI INPUT + file_name=file + para_name =paraname + + data,rate,mav_type = csvParaExtract(file_name,para_name) + + anom,index_x,xpts = manParseData(data, paraname_readable) ## EXAMPLE for manual anomaly selection + + eq_anom = anomLen(anom,rate) + + # saveAnom(para_name, eq_anom, mav_type) + + return data,index_x,para_name,xpts,eq_anom + +def main2(file,paraname): + + file_name=file + para_name =paraname + + data,rate,mav_type = csvParaExtract(file_name,para_name) + + anomalyDetection = AnomalyDetection(data) ## EXAMPLE for automatic anomaly selection + anom,index_x = anomalyDetection.parseData() + eq_anom = anomLen(anom,rate) + + eq_anom = anomLen(anom,rate) + # saveAnom(para_name, eq_anom, mav_type) + return data, index_x, para_name, eq_anom + +def main3(file,paraname,xpts): + + file_name=file + para_name =paraname + + data,rate,mav_type = csvParaExtract(file_name,para_name) + + anom,index_x = manParseData2(data, xpts) ## EXAMPLE for manual anomaly selection + # print (xpts) + # print (data) + # print (anom) + # print (index_x) + # for x in range(len(anom)): + # print ("x: ",anom[x]) + + eq_anom = anomLen(anom,rate) + + # saveAnom(para_name, eq_anom, mav_type) + return eq_anom + + +# main2('2018-10-07 13-49-59.csv','XGyro',"X Gyro") + +# main("2018-11-17 12-37-40.csv",'YGyro','Y Gyro') + +def main4(file,paraname,index_x): + + + + file_name = file + + para_name = paraname + + data, rate, mav_type = csvParaExtract(file_name, para_name) + + anom, index_x = parseData(data, index_x) + + eq_anom = anomLen(anom,rate) + + # saveAnom(para_name, eq_anom, mav_type) + return eq_anom + + diff --git a/GUI/GUI_final.py b/GUI/GUI_final.py new file mode 100644 index 0000000..57db5a9 --- /dev/null +++ b/GUI/GUI_final.py @@ -0,0 +1,528 @@ +# -*- coding: utf-8 -*- + +# **************************** My import ********************************* +from PyQt5 import QtCore, QtGui, QtWidgets +from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit, QFileDialog +from matplotlib.figure import Figure +import GUIFunctions +# import SpliceDataFunctions +# import Learn +# import Learn_original +import sys +from parameterGUI import Ui_parameterSelection +import multi_param_learn +from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas +from splicing import Ui_Splicing_mode_Dialog + +# **************************** My import ********************************* + +# **************************** Class Canvas ***************************** +class Figure_Canvas(FigureCanvas): + + def __init__(self, parent=None, width=3.28, height=2, dpi=100): + fig = Figure(figsize=(width, height), dpi=dpi) + + FigureCanvas.__init__(self, fig) # + FigureCanvas.updateGeometry(self) + self.setParent(parent) + + # Figure_Canvas.set_window_title(self,"hello") + + self.axes = fig.add_subplot(111) + + def test(self,a): + self.axes.plot(a) + # a=SpliceDataFunctions.getTSData(Ui_DroneGUI.readedFileList[0]) + # self.axes.plot(a) + # print (Ui_DroneGUI().return_plot()) + +# **************************** Class Canvas ***************************** + + + + + +class Ui_DroneGUI(QWidget): +# **************************** My initialtion ************************ + def __init__(self, parent=None): + super(Ui_DroneGUI, self).__init__() + self.setupUi() + self.ui = Ui_parameterSelection() +# **************************** My initialtion ************************ + def setupUi(self): + DroneGUI.setObjectName("DroneGUI") + DroneGUI.resize(616, 508) + self.centralwidget = QtWidgets.QWidget(DroneGUI) + self.centralwidget.setObjectName("centralwidget") + self.line = QtWidgets.QFrame(self.centralwidget) + self.line.setGeometry(QtCore.QRect(230, 0, 20, 491)) + self.line.setFrameShape(QtWidgets.QFrame.VLine) + self.line.setFrameShadow(QtWidgets.QFrame.Sunken) + self.line.setObjectName("line") + self.tab = QtWidgets.QTabWidget(self.centralwidget) + self.tab.setGeometry(QtCore.QRect(260, 20, 351, 471)) + self.tab.setObjectName("tab") + self.Data = QtWidgets.QWidget() + self.Data.setObjectName("Data") + self.layoutWidget = QtWidgets.QWidget(self.Data) + self.layoutWidget.setGeometry(QtCore.QRect(150, 390, 158, 25)) + self.layoutWidget.setObjectName("layoutWidget") + self.gridLayout_2 = QtWidgets.QGridLayout(self.layoutWidget) + self.gridLayout_2.setContentsMargins(0, 0, 0, 0) + self.gridLayout_2.setObjectName("gridLayout_2") + self.pushButton_6 = QtWidgets.QPushButton(self.layoutWidget) + self.pushButton_6.setObjectName("pushButton_6") + self.gridLayout_2.addWidget(self.pushButton_6, 0, 0, 1, 1) + self.pushButton_7 = QtWidgets.QPushButton(self.layoutWidget) + self.pushButton_7.setObjectName("pushButton_7") + self.gridLayout_2.addWidget(self.pushButton_7, 0, 1, 1, 1) + self.listWidget = QtWidgets.QListWidget(self.Data) + self.listWidget.setGeometry(QtCore.QRect(20, 40, 299, 150)) + self.listWidget.setObjectName("listWidget") + self.label_2 = QtWidgets.QLabel(self.Data) + self.label_2.setGeometry(QtCore.QRect(20, 20, 299, 13)) + self.label_2.setObjectName("label_2") + self.listWidget_2 = QtWidgets.QListWidget(self.Data) + self.listWidget_2.setGeometry(QtCore.QRect(20, 230, 299, 150)) + self.listWidget_2.setObjectName("listWidget_2") + self.label_1 = QtWidgets.QLabel(self.Data) + self.label_1.setGeometry(QtCore.QRect(20, 211, 320, 16)) + self.label_1.setObjectName("label_1") + self.tab.addTab(self.Data, "") + self.Parameter = QtWidgets.QWidget() + self.Parameter.setObjectName("Parameter") + self.listWidget_3 = QtWidgets.QListWidget(self.Parameter) + self.listWidget_3.setGeometry(QtCore.QRect(30, 50, 281, 321)) + self.listWidget_3.setObjectName("listWidget_3") + self.label = QtWidgets.QLabel(self.Parameter) + self.label.setGeometry(QtCore.QRect(30, 20, 111, 31)) + self.label.setObjectName("label") + self.pushButton_11 = QtWidgets.QPushButton(self.Parameter) + self.pushButton_11.setGeometry(QtCore.QRect(220, 390, 75, 23)) + self.pushButton_11.setObjectName("pushButton_11") + self.tab.addTab(self.Parameter, "") + self.Graph = QtWidgets.QWidget() + self.Graph.setObjectName("Graph") + self.graphicsView = QtWidgets.QGraphicsView(self.Graph) + self.graphicsView.setGeometry(QtCore.QRect(10, 100, 331, 311)) + self.graphicsView.setObjectName("graphicsView") + self.pushButton_1 = QtWidgets.QPushButton(self.Graph) + self.pushButton_1.setGeometry(QtCore.QRect(10, 20, 199, 23)) + self.pushButton_1.setObjectName("pushButton_1") + self.pushButton_3 = QtWidgets.QPushButton(self.Graph) + self.pushButton_3.setGeometry(QtCore.QRect(10, 60, 199, 23)) + self.pushButton_3.setObjectName("pushButton_3") + self.tab.addTab(self.Graph, "") + self.Report = QtWidgets.QWidget() + self.Report.setObjectName("Report") + self.textBrowser = QtWidgets.QTextBrowser(self.Report) + self.textBrowser.setGeometry(QtCore.QRect(20, 30, 311, 341)) + self.textBrowser.setObjectName("textBrowser") + self.pushButton = QtWidgets.QPushButton(self.Report) + self.pushButton.setGeometry(QtCore.QRect(110, 390, 131, 23)) + self.pushButton.setObjectName("pushButton") + self.tab.addTab(self.Report, "") + self.layoutWidget1 = QtWidgets.QWidget(self.centralwidget) + self.layoutWidget1.setGeometry(QtCore.QRect(20, 30, 201, 431)) + self.layoutWidget1.setObjectName("layoutWidget1") + self.gridLayout = QtWidgets.QGridLayout(self.layoutWidget1) + self.gridLayout.setContentsMargins(0, 0, 0, 0) + self.gridLayout.setObjectName("gridLayout") + self.pushButton_4 = QtWidgets.QPushButton(self.layoutWidget1) + self.pushButton_4.setObjectName("pushButton_4") + self.gridLayout.addWidget(self.pushButton_4, 4, 0, 1, 1) + self.pushButton_5 = QtWidgets.QPushButton(self.layoutWidget1) + self.pushButton_5.setObjectName("pushButton_5") + self.gridLayout.addWidget(self.pushButton_5, 5, 0, 1, 1) + self.pushButton_2 = QtWidgets.QPushButton(self.layoutWidget1) + self.pushButton_2.setObjectName("pushButton_2") + self.gridLayout.addWidget(self.pushButton_2, 1, 0, 1, 1) + self.pushButton_13 = QtWidgets.QPushButton(self.layoutWidget1) + self.pushButton_13.setObjectName("pushButton_13") + self.gridLayout.addWidget(self.pushButton_13, 0, 0, 1, 1) + DroneGUI.setCentralWidget(self.centralwidget) + self.statusbar = QtWidgets.QStatusBar(DroneGUI) + self.statusbar.setObjectName("statusbar") + DroneGUI.setStatusBar(self.statusbar) + self.Training_Data = QtWidgets.QAction(DroneGUI) + self.Training_Data.setObjectName("Training_Data") + self.Test_Data = QtWidgets.QAction(DroneGUI) + self.Test_Data.setObjectName("Test_Data") + self.Current_Datasets = QtWidgets.QAction(DroneGUI) + self.Current_Datasets.setObjectName("Current_Datasets") + self.Splice_Data = QtWidgets.QAction(DroneGUI) + self.Splice_Data.setObjectName("Splice_Data") + self.Edit_Selected_Datasets = QtWidgets.QAction(DroneGUI) + self.Edit_Selected_Datasets.setObjectName("Edit_Selected_Datasets") + self.Feature_Vector_Plotting = QtWidgets.QAction(DroneGUI) + self.Feature_Vector_Plotting.setObjectName("Feature_Vector_Plotting") + self.actionConfusion_Matrix = QtWidgets.QAction(DroneGUI) + self.actionConfusion_Matrix.setObjectName("actionConfusion_Matrix") + self.actionTest_Data_Analysis_Report = QtWidgets.QAction(DroneGUI) + self.actionTest_Data_Analysis_Report.setObjectName("actionTest_Data_Analysis_Report") + self.Edit_Feature_Vector = QtWidgets.QAction(DroneGUI) + self.Edit_Feature_Vector.setObjectName("Edit_Feature_Vector") + self.Manual_Mode_Switch = QtWidgets.QAction(DroneGUI) + self.Manual_Mode_Switch.setObjectName("Manual_Mode_Switch") + + self.retranslateUi(DroneGUI) + self.tab.setCurrentIndex(0) + QtCore.QMetaObject.connectSlotsByName(DroneGUI) + + +# **************************** My Code_Multi-selectionmode ***************** + self.listWidget.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection) + self.listWidget_2.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection) + self.listWidget_3.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection) +# **************************** My Code_Multi-selectionmode ***************** + +# **************************** My Code_select parameter********************* + self.pushButton_2.clicked.connect(self.show_parameter) +# **************************** My Code_select parameter********************* + +# **************************** My Code_Qlistwidget_2 *********************** + self.pushButton_6.clicked.connect(self.addtolist) + self.pushButton_7.clicked.connect(self.removeitem) +# **************************** My Code_Qlistwidget_2 *********************** + +# **************************** My Code_Trigger open file ******************* + self.pushButton_13.clicked.connect(self.import_training_data) +# **************************** My Code_Trigger open file ******************* + +# **************************** My Code_splice data automatically *********** + self.pushButton_4.clicked.connect(self.datasplicing_mode_selection) +# **************************** My Code_splice data automatically *********** + +# **************************** My code_show training data******************* + self.pushButton_1.clicked.connect(self.traning_data_display) +# **************************** My code_show traing data********************* + +# **************************** My code_show test data ********************** + self.pushButton_3.clicked.connect(self.test_data_display) +# **************************** My code_show test data ********************** + +# **************************** My code_splicing the data button clicked **** + self.pushButton_5.clicked.connect(self.machinelearning) +# **************************** My code_splicing the data button clicked **** + +# **************************** My code_delete parameter ******************** + self.pushButton_11.clicked.connect(self.del_parameter) +# **************************** My code_delete parameter ******************** + + + def retranslateUi(self, DroneGUI): + _translate = QtCore.QCoreApplication.translate + DroneGUI.setWindowTitle(_translate("DroneGUI", "DroneGUI")) + self.pushButton_6.setText(_translate("DroneGUI", "Add")) + self.pushButton_7.setText(_translate("DroneGUI", "Delete")) + self.label_2.setText(_translate("DroneGUI", "All Test Datasets")) + self.label_1.setText(_translate("DroneGUI", "Selected Test Datasets to Predict (1 file per run)")) + self.tab.setTabText(self.tab.indexOf(self.Data), _translate("DroneGUI", "Data")) + self.label.setText(_translate("DroneGUI", "Parameter Selected")) + self.pushButton_11.setText(_translate("DroneGUI", "Delete")) + self.tab.setTabText(self.tab.indexOf(self.Parameter), _translate("DroneGUI", "Parameter")) + self.pushButton_1.setText(_translate("DroneGUI", "Show Training Data Profile")) + self.pushButton_3.setText(_translate("DroneGUI", "Show Test Data Profile")) + self.tab.setTabText(self.tab.indexOf(self.Graph), _translate("DroneGUI", "Prediction")) + self.pushButton.setText(_translate("DroneGUI", "Make Report")) + self.tab.setTabText(self.tab.indexOf(self.Report), _translate("DroneGUI", "Report")) + self.pushButton_4.setText(_translate("DroneGUI", "Splice Test datasets")) + self.pushButton_5.setText(_translate("DroneGUI", "Predict Disturbance")) + self.pushButton_2.setText(_translate("DroneGUI", "Select Parameters")) + self.pushButton_13.setText(_translate("DroneGUI", "Import Test Data")) + self.Training_Data.setText(_translate("DroneGUI", "Training Data")) + self.Test_Data.setText(_translate("DroneGUI", "Test Data")) + self.Current_Datasets.setText(_translate("DroneGUI", "Current Selected Datasets")) + self.Splice_Data.setText(_translate("DroneGUI", "Splice Data and labelling")) + self.Edit_Selected_Datasets.setText(_translate("DroneGUI", "Edit Selected Datasets")) + self.Feature_Vector_Plotting.setText(_translate("DroneGUI", "Feature Vector Plotting")) + self.actionConfusion_Matrix.setText(_translate("DroneGUI", "Confusion Matrix")) + self.actionTest_Data_Analysis_Report.setText(_translate("DroneGUI", "Test Data Analysis Report")) + self.Edit_Feature_Vector.setText(_translate("DroneGUI", "Edit Feature Vector")) + self.Manual_Mode_Switch.setText(_translate("DroneGUI", "Manual Mode Switch")) + +# ******************************************************************************* +# ******************************* ------------------- ************************** +# **************************** | Define functions | ************************ +# ****************************** ------------------- ************************ +# ******************************************************************************* + +# **************************** My Code_import training data********************* + def import_training_data(self): + self.tab.setCurrentIndex(0) + + options = QFileDialog.Options() + options |= QFileDialog.DontUseNativeDialog + files, _ = QFileDialog.getOpenFileNames(self, "Training datasets import)", "", + "All Files (*);;Python Files (*.py)", options=options) + # if files: + # print(files) + + for i in range(len(files)): + item = QtWidgets.QListWidgetItem(files[i]) + self.listWidget.addItem(item) + + + +# **************************** My Code_import training data********************* + +# **************************** My Code_def_addtolist & remove******************** + + def addtolist(self): + + + if self.listWidget_2.count() is 1: + pass + else: + self.listWidget_2.addItem(self.listWidget.selectedItems()[0].text()) + # for i in range (len(selecteditem)): + # self.listWidget_2.addItem(self.listWidget.selectedItems()[i].text()) + self.listWidget.clearSelection() + + def removeitem(self): + selecteditem=self.listWidget.selectedItems() + for i in selecteditem: + self.listWidget.takeItem(self.listWidget.row(i)) + + selecteditem2=self.listWidget_2.selectedItems() + + for i in selecteditem2: + self.listWidget_2.takeItem(self.listWidget_2.row(i)) + +# **************************** My Code_define addtolist & remove**************** + +# **************************** My Code_data select paramter ************* + + def show_parameter(self): + self.tab.setCurrentIndex(1) + self.parameter=QtWidgets.QWidget() + self.ui=Ui_parameterSelection() + self.ui.setupUi(self.parameter) + self.parameter.show() + self.ui.Confirmation_buttonBox.accepted.connect(self.confirmed) + + + def confirmed(self): + self.para_dic2={"Pitch":"mavlink_ahrs3_t_pitch", + "Yaw":"mavlink_ahrs3_t_yaw","Pitch rate":"mavlink_attitude_t_pitch rate", + "Roll rate":"mavlink_attitude_t_roll rate", + "Yaw angle":"mavlink_attitude_t_yaw angle" ,"Roll":"mavlink_ahrs3_t_roll", + "Altitude":"mavlink_ahrs3_t_altitude","X Accel":"mavlink_raw_imu_t_Xaccel", + "X Gyro": "mavlink_raw_imu_t_XGyro","X Mag": "mavlink_raw_imu_t_XMag", + "Y Accel":"mavlink_raw_imu_t_Yaccel", "Y Gyro":"mavlink_raw_imu_t_YGyro", "Y Mag": "mavlink_raw_imu_t_YMag", + "Z Accel":"mavlink_raw_imu_t_Zaccel","Z Gyro":"mavlink_raw_imu_t_ZGyro","Z Mag":"mavlink_raw_imu_t_ZMag", + "Z vibration":"mavlink_vibration_t_vibration_z","Yaw rate":"mavlink_attitude_t_yaw rate"} + self.para_dic = {"Pitch": "pitch", + "Yaw": "yaw", "Pitch rate": "pitch rate", + "Roll rate": "roll rate", + "Yaw angle": "yaw angle", + "Roll": "roll", + "Altitude": "altitude", "X Accel": "Xaccel", + "X Gyro": "XGyro", "X Mag": "XMag", + "Y Accel": "Yaccel", "Y Gyro": "YGyro", + "Y Mag": "YMag", + "Z Accel": "Zaccel", "Z Gyro": "ZGyro", + "Z Mag": "ZMag", + "Z vibration": "vibration_z","Yaw rate":"yaw rate"} + + global selection + global selection_name + self.selection_name=[] + self.selection = [] + + if self.ui.Pitch.isChecked(): + # self.selection.append("mavlink_nav_controller_output_t_nav_pitch") + self.selection_name.append("Pitch") + if self.ui.Yaw.isChecked(): + # self.selection.append("mavlink_ahrs3_t_yaw") + self.selection_name.append("Yaw") + if self.ui.Pitch_rate.isChecked(): + # self.selection.append("mavlink_attitude_t_pitch rate") + self.selection_name.append("Pitch rate") + if self.ui.Roll_rate.isChecked(): + # self.selection.append("mavlink_attitude_t_roll rate") + self.selection_name.append("Roll rate") + if self.ui.Yaw_angle.isChecked(): + # self.selection.append("mavlink_attitude_t_yaw angle") + self.selection_name.append("Yaw angle") + if self.ui.Roll.isChecked(): + # self.selection.append("mavlink_nav_controller_output_t_nav_roll") + self.selection_name.append("Roll") + if self.ui.Altitude.isChecked(): + # self.selection.append("mavlink_ahrs2_t_altitude") + self.selection_name.append("Altitude") + if self.ui.X_Accel.isChecked(): + # self.selection.append("mavlink_raw_imu_t_Xaccel") + self.selection_name.append("X Accel") + if self.ui.X_Gyro.isChecked(): + # self.selection.append("mavlink_raw_imu_t_XGyro") + self.selection_name.append("X Gyro") + if self.ui.X_Mag.isChecked(): + # self.selection.append("mavlink_raw_imu_t_XMag") + self.selection_name.append("X Mag") + if self.ui.Y_Accel.isChecked(): + # self.selection.append("mavlink_raw_imu_t_Yaccel") + self.selection_name.append("Y Accel") + if self.ui.Y_Gyro.isChecked(): + # self.selection.append("mavlink_raw_imu_t_YGyro") + self.selection_name.append("Y Gyro") + if self.ui.Y_Mag.isChecked(): + # self.selection.append("mavlink_raw_imu_t_YMag") + self.selection_name.append("Y Mag") + if self.ui.Z_Accel.isChecked(): + # self.selection.append("mavlink_raw_imu_t_Zaccel") + self.selection_name.append("Z Accel") + if self.ui.Z_Gyro.isChecked(): + # self.selection.append("mavlink_raw_imu_t_ZGyro") + self.selection_name.append("Z Gyro") + if self.ui.Z_Mag.isChecked(): + # self.selection.append("mavlink_raw_imu_t_ZMag") + self.selection_name.append("Z Mag") + if self.ui.Z_vibration.isChecked(): + # self.selection.append("mavlink_vibration_t_vibration_z") + self.selection_name.append("Z vibration") + if self.ui.Yaw_rate.isChecked(): + # self.selection.append("mavlink_vibration_t_vibration_z") + self.selection_name.append("Yaw rate") + self.parameter.close() + + + for i in range(len(self.selection_name)): + self.listWidget_3.addItem(self.selection_name[i]) + + + + +# **************************** My Code_data select paramter ************* + +# **************************** My Code_delete parameter ***************** + + def del_parameter(self): + selecteditem3 = self.listWidget_3.selectedItems() + for i in selecteditem3: + self.listWidget_3.takeItem(self.listWidget_3.row(i)) + + +# **************************** My Code_delete parameter ***************** + +# **************************** My Code_data splice ******************** + def datasplicing_mode_selection(self): + + self.mode_selection=QtWidgets.QDialog() + self.ui2=Ui_Splicing_mode_Dialog() + self.ui2.setupUi(self.mode_selection) + self.mode_selection.show() + self.ui2.buttonBox.accepted.connect(self.datasplicing) + # self.ui.Confirmation_buttonBox.accepted.connect(self.confirmed) + + def datasplicing(self): + self.tab.setCurrentIndex(2) + self.para_selection = [] + self.para_selection2 = [] + self.para_selection_name = [] + self.x_test_final = [] + + for i in range(self.listWidget_3.count()): + self.para_selection_name.append(self.listWidget_3.item(i).text()) + self.para_selection.append(self.para_dic[self.listWidget_3.item(i).text()]) + self.para_selection2.append(self.para_dic2[self.listWidget_3.item(i).text()]) + self.readedFileList = [self.listWidget_2.item(i).text() for i in range(self.listWidget_2.count())] + # print (self.readedFileList,self.para_selection,self.para_selection_name,self.para_selection2) + + print("Selected parameters are:", self.para_selection_name) + + + + if self.ui2.radioButton.isChecked(): #manual + self.b = GUIFunctions.main(self.readedFileList[0], self.para_selection[0], self.para_selection_name[0]) + self.x_test_final.append(self.b[4]) + self.check = 0 + + for i in range (1,len(self.para_selection)): + + self.x_test=GUIFunctions.main3(self.readedFileList[0],self.para_selection[i],self.b[3]) + self.x_test_final.append(self.x_test) + + + if self.ui2.radioButton_2.isChecked(): #automatic + self.check=1 + self.b = GUIFunctions.main2(self.readedFileList[0], self.para_selection[0]) + self.x_test_final.append(self.b[3]) + + for i in range(1, len(self.para_selection)): + # GUIFunctions.main3(self.readedFileList[0],self.para_selection[i],self.b[1]) + self.x_test2=GUIFunctions.main4(self.readedFileList[0], self.para_selection[i], self.b[1]) + + self.x_test_final.append(self.x_test2) + + self.dr = Figure_Canvas() + + # a = GUIFunctions.csvParaExtract(self.readedFileList[0], self.para_selection[0])[0] + + # a = SpliceDataFunctions.getTSData(self.readedFileList[0]) + self.dr.test(self.b[0]) + self.graphicscene = QtWidgets.QGraphicsScene() # + self.graphicscene.addWidget(self.dr) # + self.graphicsView.setScene(self.graphicscene) # + self.graphicsView.show() # + # self.graphicsView.setAutoFillBackground() + + # self.setCentralWidget(self.graphicsView) + # self.graphicsView.setFixedSize(100, 600) + + +# **************************** My Code_data splice ******************** + +# **************************** My Code_display prediction **************** + def machinelearning(self): + + text, ok = QInputDialog.getText(self, 'Input Dialog', + 'Enter K value: ') + if ok: + self.k_value=int(text) + # print (self.k_value) + + self.learning = multi_param_learn.multi_param_learn(self.para_selection2,None,'Data/') + + + # self.label_result=Learn_original.machine_learning(self.learning[0],self.learning[1],self.learning[2],self.learning[3]) + self.label_result = multi_param_learn.machine_learning(self.x_test_final, self.learning[1], self.learning[2],self.learning[3],self.learning[4],self.learning[5],self.learning[6],self.k_value) + + print ("detection result: ",self.label_result[0]) + + print("majority voting result: ",self.label_result[1]) + + + multi_param_learn.test_data_plot(self.para_selection_name[0],self.x_test_final[0]) + # print (self.label_result) + if self.check==0: + GUIFunctions.distLabel(self.b[0],self.b[1],self.label_result[1],self.para_selection_name[0]) + if self.check==1: + GUIFunctions.distLabelAuto(self.b[0],self.b[1],self.label_result[1],self.para_selection_name[0]) + + + +# **************************** My Code_display prediction **************** + + +# **************************** My Code_display training data ************ + def traning_data_display(self): + learning = multi_param_learn.multi_param_learn(self.para_selection2,None,'Data/') + multi_param_learn.training_data_plot(self.para_selection_name[0],learning[1][0],learning[2][0],learning[3][0]) + +# **************************** My Code_display training data ************ + +# **************************** My Code_display test data ************ + def test_data_display(self): + self.a= GUIFunctions.csvParaExtract(self.readedFileList[0],self.para_selection[0])[0] + # a= SpliceDataFunctions.getTSData(self.readedFileList[0]) + GUIFunctions.plotTSData(self.a,self.para_selection_name[0]) + # SpliceDataFunctions.plotTSData() +# **************************** My Code_display test data ************ +if __name__ == "__main__": + app = QtWidgets.QApplication(sys.argv) + DroneGUI = QtWidgets.QMainWindow() + ui = Ui_DroneGUI() + # ui.setupUi(DroneGUI) + DroneGUI.show() + sys.exit(app.exec_()) diff --git a/GUI/multi_param_learn.py b/GUI/multi_param_learn.py new file mode 100644 index 0000000..7df7bef --- /dev/null +++ b/GUI/multi_param_learn.py @@ -0,0 +1,369 @@ +import matplotlib.pyplot as plt +import numpy as np + +from knndtw import KnnDtw +from knndtw import ProgressBar +from scipy import stats +import math + +# from k_fold_cv import k_fold_cross_val + + + +"let's try to create a feature vector with multiple parameters" + +def multi_param_learn(param_list,param_weights,datapath): + labels = {1: 'Hover', 2: 'Impact (Front Left)', 3: 'Impact (Front Right)', 4: 'Impact (Back Left)', + 5: 'Impact (Back Right)', + 6: 'Gust (from Left)', 7: 'Gust (from Right)', 8: 'Gust (from front)'} + + + x_train_final = [] + y_train_final = [] + # x_test_final = [] + if param_weights != None: + if len(param_list) != len(param_weights): + raise Exception('When using weights, there must one weight for each parameter!') + para_weights = dict(zip(param_list,param_weights)) + else: + para_weights=None + for dataparam in param_list: + + trainingdatafile = datapath + 'train_' + dataparam + '.txt' + traininglabelfile = datapath + 'train_labels.txt' + + testdatafile = 'test_' + dataparam + '.txt' + # testlabelfile = datapath + 'test_labels.txt' + + # Open training data file, x:data, y:label + x_train_file = open(trainingdatafile, 'r') + y_train_file = open(traininglabelfile, 'r') + + #Open test data file, x:data, y:label + # x_test_file = open(testdatafile, 'r') + # # y_test_file = open(testlabelfile, 'r') + # + + # Create empty lists + x_train = [] + y_train = [] + # x_test = [] + y_test = [] + + # Mapping table for classes + + + i = 0 + # Loop through datasets + for x in x_train_file: + x_train.append([float(ts) for ts in x.split()]) + for y in y_train_file: + y_train.append(int(y.rstrip('\n'))) + + # for x in x_test_file: + # x_test.append([float(ts) for ts in x.split()]) + + # for y in y_test_file: + # y_test.append(int(y.rstrip('\n'))) + + #close data files + x_train_file.close() + y_train_file.close() + # x_test_file.close() + # y_test_file.close() + # Convert to numpy for efficiency + # print (x_train) + # print (x_test) + # print("eq_anom2_x_text:", x_test) + + x_train_final.append(x_train) + y_train_final.append(y_train) + # x_test_final.append(x_test) + + # print(x_test_final) + # print(x_train_final) + # + + # x_train_final = np.array(x_train_final) + # y_train_final = np.array(y_train_final) + # x_test_final = np.array(x_test_final) + # # y_test = np.array(y_test) + # print (len(x_test_final[0][0])) + # + # print(x_test_final) + # print(x_train_final) + # print (np.shape(x_test_final)) + # print(np.shape(x_train_final)) + + + return "", x_train_final, y_train_final, labels,param_weights,para_weights,param_list + +def machine_learning(x_test_final, x_train_final, y_train_final, labels,param_weights,para_weights,param_list,k_value): + param_labels = [] + for i in range (0,len(x_train_final)): + #Analyze dataset + + x_train_final2 = np.array(x_train_final[i]) + y_train_final2 = np.array(y_train_final[i]) + x_test_final2 = np.array(x_test_final[i]) + + m = KnnDtw(n_neighbors=k_value, max_warping_window=100) + m.fit(x_train_final2, y_train_final2) + label, proba = m.predict(x_test_final2) + #get the weight for this parameter + if param_weights == None: + param_labels.append(label) #if we don't have weights do this + else: + weight = [para_weights[param_list[i]]] + param_labels.append(list(zip(label,weight*len(label))))#a tuple list of (label, weight) + + param_labels = np.array(param_labels) + if param_weights == None: + para_mode, para_count = stats.mode(param_labels) + para_mode = np.reshape(para_mode,(para_mode.shape[1],)) + else: #for weights + para_mode = [0]*param_labels.shape[1] + for i in range(param_labels.shape[1]): + mode_count = [0]*len(labels) #an array representing how frequent each label was used to classify a time series + col = param_labels[:,i] + for p in col: + mode_count[p[0]-1] += p[1] + para_mode[i] = mode_count.index(max(mode_count)) + 1 #the the label that was used most frequently as the overall label + #para_mode = np.reshape(para_mode,(para_mode.shape[1],)) + + #Using mode to see which classification was the most frequent for each data from all parameters used + #k_val = list(range(1,11)) + #k_fold_cross_val(k_val,x_train,y_train,6) + return param_labels,para_mode + +def test_data_plot(param_list,x_test): + plot2 = plt.figure("Test data profile (parameter: " + str(param_list) + ")", figsize=(11, 7)) + + + + colors = ['#D62728', '#2C9F2C', '#FD7F23', '#1F77B4', '#9467BD', + '#8C564A', '#7F7F7F', '#1FBECF', '#E377C2', '#BCBD27', + '#D62728', '#2C9F2C', '#FD7F23', '#1F77B4', '#9467BD', + '#8C564A', '#7F7F7F', '#1FBECF', '#E377C2', '#BCBD27', + '#D62728', '#2C9F2C', '#FD7F23', '#1F77B4', '#9467BD', + '#8C564A', '#7F7F7F', '#1FBECF', '#E377C2', '#BCBD27', + '#D62728', '#2C9F2C', '#FD7F23', '#1F77B4', '#9467BD', + '#8C564A', '#7F7F7F', '#1FBECF', '#E377C2', '#BCBD27', + '#D62728', '#2C9F2C', '#FD7F23', '#1F77B4', '#9467BD', + '#8C564A', '#7F7F7F', '#1FBECF', '#E377C2', '#BCBD27', + '#D62728', '#2C9F2C', '#FD7F23', '#1F77B4', '#9467BD', + '#8C564A', '#7F7F7F', '#1FBECF', '#E377C2', '#BCBD27', + '#D62728', '#2C9F2C', '#FD7F23', '#1F77B4', '#9467BD', + '#8C564A', '#7F7F7F', '#1FBECF', '#E377C2', '#BCBD27', + '#D62728', '#2C9F2C', '#FD7F23', '#1F77B4', '#9467BD', + '#8C564A', '#7F7F7F', '#1FBECF', '#E377C2', '#BCBD27', + '#D62728', '#2C9F2C', '#FD7F23', '#1F77B4', '#9467BD', + '#8C564A', '#7F7F7F', '#1FBECF', '#E377C2', '#BCBD27', + '#D62728', '#2C9F2C', '#FD7F23', '#1F77B4', '#9467BD', + '#8C564A', '#7F7F7F', '#1FBECF', '#E377C2', '#BCBD27', + '#D62728', '#2C9F2C', '#FD7F23', '#1F77B4', '#9467BD', + '#8C564A', '#7F7F7F', '#1FBECF', '#E377C2', '#BCBD27', + '#D62728', '#2C9F2C', '#FD7F23', '#1F77B4', '#9467BD', + '#8C564A', '#7F7F7F', '#1FBECF', '#E377C2', '#BCBD27', + ] + for i, r in enumerate ([i for i in range(len(x_test))]): + + a= math.ceil(len(x_test)/2) + + plt.subplot(a, 2, i + 1) + # plt.plot(x_test[r], label=labels[y_test[r]], color=colors[i], linewidth=2) + plt.plot(x_test[r], color=colors[i], linewidth=2) + plt.xlabel('Samples @50Hz') + # plt.legend(loc='upper left') + plt.tight_layout() + plt.show() + +def training_data_plot(param_list,x_train,y_train,labels): + + plot1 = plt.figure("Training data profile (parameter: " + str(param_list) + ")", figsize=(11, 7)) + + colors = ['#D62728', '#2C9F2C', '#FD7F23', '#1F77B4', '#9467BD', + '#8C564A', '#7F7F7F', '#1FBECF', '#E377C2', '#BCBD27', + '#D62728', '#2C9F2C', '#FD7F23', '#1F77B4', '#9467BD', + '#8C564A', '#7F7F7F', '#1FBECF', '#E377C2', '#BCBD27', + '#D62728', '#2C9F2C', '#FD7F23', '#1F77B4', '#9467BD', + '#8C564A', '#7F7F7F', '#1FBECF', '#E377C2', '#BCBD27', + '#D62728', '#2C9F2C', '#FD7F23', '#1F77B4', '#9467BD', + '#8C564A', '#7F7F7F', '#1FBECF', '#E377C2', '#BCBD27', + '#D62728', '#2C9F2C', '#FD7F23', '#1F77B4', '#9467BD', + '#8C564A', '#7F7F7F', '#1FBECF', '#E377C2', '#BCBD27', + '#D62728', '#2C9F2C', '#FD7F23', '#1F77B4', '#9467BD', + '#8C564A', '#7F7F7F', '#1FBECF', '#E377C2', '#BCBD27', + '#D62728', '#2C9F2C', '#FD7F23', '#1F77B4', '#9467BD', + '#8C564A', '#7F7F7F', '#1FBECF', '#E377C2', '#BCBD27', + '#D62728', '#2C9F2C', '#FD7F23', '#1F77B4', '#9467BD', + '#8C564A', '#7F7F7F', '#1FBECF', '#E377C2', '#BCBD27', + '#D62728', '#2C9F2C', '#FD7F23', '#1F77B4', '#9467BD', + '#8C564A', '#7F7F7F', '#1FBECF', '#E377C2', '#BCBD27', + '#D62728', '#2C9F2C', '#FD7F23', '#1F77B4', '#9467BD', + '#8C564A', '#7F7F7F', '#1FBECF', '#E377C2', '#BCBD27', + '#D62728', '#2C9F2C', '#FD7F23', '#1F77B4', '#9467BD', + '#8C564A', '#7F7F7F', '#1FBECF', '#E377C2', '#BCBD27', + '#D62728', '#2C9F2C', '#FD7F23', '#1F77B4', '#9467BD', + '#8C564A', '#7F7F7F', '#1FBECF', '#E377C2', '#BCBD27', + '#D62728', '#2C9F2C', '#FD7F23', '#1F77B4', '#9467BD', + '#8C564A', '#7F7F7F', '#1FBECF', '#E377C2', '#BCBD27', + '#D62728', '#2C9F2C', '#FD7F23', '#1F77B4', '#9467BD', + '#8C564A', '#7F7F7F', '#1FBECF', '#E377C2', '#BCBD27', + '#D62728', '#2C9F2C', '#FD7F23', '#1F77B4', '#9467BD', + '#8C564A', '#7F7F7F', '#1FBECF', '#E377C2', '#BCBD27', + + + ] + + for i, r in enumerate([i for i in range(len(x_train))]): + a = math.ceil(len(x_train) / 2) + + plt.subplot(a, 2, i + 1) + plt.plot(x_train[r], label=labels[y_train[r]], color=colors[i], linewidth=2) + plt.xlabel('Samples @50Hz') + plt.legend(loc='upper left') + plt.tight_layout() + plt.show() + + + # def test_data_plot(): + # # Plot Test data + # plot2 = plt.figure(figsize=(11, 7)) + # colors = ['#D62728', '#2C9F2C', '#FD7F23', '#1F77B4', '#9467BD', + # '#8C564A', '#7F7F7F', '#1FBECF', '#E377C2', '#BCBD27','#BCBD27','#BCBD27','#BCBD27','#BCBD27','#BCBD27','#BCBD27','#BCBD27','#BCBD27','#BCBD27','#BCBD27','#BCBD27','#BCBD27','#BCBD27','#BCBD27','#BCBD27','#BCBD27','#BCBD27','#BCBD27'] + # for i, r in enumerate([0, 1, 2, 3, 4, 5,6,7,8,9,10,11,12,13,14]): + # plt.subplot(8, 2, i + 1) + # # plt.plot(x_test[r], label=labels[y_test[r]], color=colors[i], linewidth=2) + # plt.plot(x_test[r], color=colors[i], linewidth=2) + # plt.xlabel('Samples @50Hz') + # # plt.legend(loc='upper left') + # plt.tight_layout() + # plt.show() + # + # def confusion_matrix_plot(): + # # Confusion Matrix + # conf_mat = confusion_matrix(label, y_test) + # + # fig = plt.figure(figsize=(3, 3)) + # width = np.shape(conf_mat)[1] + # height = np.shape(conf_mat)[0] + # + # res = plt.imshow(np.array(conf_mat), cmap=plt.cm.summer, interpolation='nearest') + # for i, row in enumerate(conf_mat): + # for j, c in enumerate(row): + # if c > 0: + # plt.text(j - .2, i + .1, c, fontsize=16) + # + # plt.title('Confusion Matrix for ' + dataparam) + # plt.xlabel('Data') + # plt.ylabel('ML Identification') + # _ = plt.xticks(range(3), [l for l in labels.values()], rotation=90) + # _ = plt.yticks(range(3), [l for l in labels.values()]) + # plt.show() + # + # + # + # # traning_data_plot() + # # test_data_plot() + + # + # + # x_test,x_train,y_train,labels=learn("mavlink_raw_imu_t_XGyro") + # machine_learning(x_test,x_train,y_train,labels) + + ##plot train data + #plt.figure(figsize=(11,7)) + #colors = ['#D62728','#2C9F2C','#FD7F23','#1F77B4','#9467BD', + # '#8C564A','#7F7F7F','#1FBECF','#E377C2','#BCBD27', + # '#D62728','#2C9F2C'] + #for i, r in enumerate([0,1,2,3,5,6,7,8,9,10,11,12]): + # plt.subplot(7,2,i+1) + # plt.plot(x_train[r], label=labels[y_train[r]], color=colors[i], linewidth=2) + # plt.xlabel('Samples @50Hz') + # plt.legend(loc='upper left') + # plt.tight_layout() + # + ##Plot Test data + #plt.figure(figsize=(11,7)) + #colors = ['#D62728','#2C9F2C','#FD7F23','#1F77B4','#9467BD', + # '#8C564A','#7F7F7F','#1FBECF','#E377C2','#BCBD27'] + #for i, r in enumerate([0,1,2,3,4,5]): + # plt.subplot(3,2,i+1) + # plt.plot(x_test[r], label=labels[y_test[r]], color=colors[i], linewidth=2) + # plt.xlabel('Samples @50Hz') + # plt.legend(loc='upper left') + # plt.tight_layout() + + # #Analyze dataset + # m = KnnDtw(n_neighbors=3, max_warping_window=100) + # m.fit(x_train, y_train) + # label, proba = m.predict(x_test) + # #get the weight for this parameter + # if param_weights == None: + # param_labels.append(label) #if we don't have weights do this + # else: + # weight = [para_weights[dataparam]] + # param_labels.append(list(zip(label,weight*len(label))))#a tuple list of (label, weight) + + # + # param_labels = np.array(param_labels) + # if param_weights == None: + # para_mode, para_count = stats.mode(param_labels) + # para_mode = np.reshape(para_mode,(para_mode.shape[1],)) + # else: #for weights + # para_mode = [0]*param_labels.shape[1] + # for i in range(param_labels.shape[1]): + # mode_count = [0]*len(labels) #an array representing how frequent each label was used to classify a time series + # col = param_labels[:,i] + # for p in col: + # mode_count[p[0]-1] += p[1] + # para_mode[i] = mode_count.index(max(mode_count)) + 1 #the the label that was used most frequently as the overall label + # #para_mode = np.reshape(para_mode,(para_mode.shape[1],)) + + #Using mode to see which classification was the most frequent for each data from all parameters used + #k_val = list(range(1,11)) + #k_fold_cross_val(k_val,x_train,y_train,6) + +# +# #Classification report +# """ASSUMPTION: +# We're trying to see accuracy of labelling as a result of multi param voting, but +# we are only comparing to one y_test belonging to one (last) parameter with the current implementation +# we're assuming that y_test is the same across all param which builds on the assumption that +# train/test data for all param are from the same time period! +# """ +# from sklearn.metrics import classification_report, confusion_matrix +# print(classification_report(para_mode, y_test, +# target_names=[l for l in labels.values()])) +# +# +# #Confusion Matrix +# conf_mat = confusion_matrix(para_mode, y_test) +# +# fig = plt.figure(figsize=(8,8)) +# width = np.shape(conf_mat)[1] +# height = np.shape(conf_mat)[0] +# +# res = plt.imshow(np.array(conf_mat), cmap=plt.cm.summer, interpolation='nearest') +# for i, row in enumerate(conf_mat): +# for j, c in enumerate(row): +# if c>0: +# plt.text(j-.2, i+.1, c, fontsize=16) +# +# #cb = fig.colorbar(res) +# plt.title('Confusion Matrix for ' + ', '.join([name for name in param_list])) +# plt.xlabel('Data') +# plt.ylabel('ML Identification') +# _ = plt.xticks(range(9), [l for l in labels.values()], rotation=90) +# _ = plt.yticks(range(9), [l for l in labels.values()]) +# +# return label + + + +# # #testing +# x_test_final, x_train_final, y_train_final, labels,param_weights,para_weights,param_list=multi_param_learn(['mavlink_attitude_t_yaw angle','mavlink_ahrs3_t_yaw'],None,'Data/') +# machine_learning(x_test_final, x_train_final, y_train_final, labels,param_weights,para_weights,param_list) + +