Created
April 8, 2016 11:33
-
-
Save fgorina/110a7cbbf772220aa7ef6bc45c2b342d to your computer and use it in GitHub Desktop.
Plot.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
import matplotlib.pyplot as plt | |
import ui | |
import dialogs | |
from os import listdir | |
from os.path import isfile, join | |
import sys | |
def plot_data(names, parsed_data, title, left_axis_vars, left_label, right_axis_vars, right_label): | |
colors=["", "b", "r", "g", "c", "m", "y", "b", "r", "g", "c", "m", "y"] | |
# print(parsed_data) | |
opt = 0 | |
for (v, s) in left_axis_vars: | |
if s != 1.0: | |
a = [x * s for x in parsed_data[v]] | |
else: | |
a = parsed_data[v] | |
plt.plot(parsed_data[0], a, colors[v]) | |
plt.ylabel(left_label) | |
if len(right_axis_vars) > 0: | |
ax2 = plt.twinx() | |
for (v, s) in right_axis_vars: | |
if s != 1.0: | |
a = [x * s for x in parsed_data[v]] | |
else: | |
a = parsed_data[v] | |
plt.plot(parsed_data[0], a, colors[v]) | |
plt.xlabel("Time") | |
plt.ylabel(right_label) | |
plt.title(title) | |
plt.show() | |
def parse_data(data): | |
parsed_data = [[],[],[],[],[],[],[],[],[],[],[]] | |
lines = data.split("\n") | |
lc = 0 | |
#print("Hi han ", len(lines), " samples") | |
for l in lines: | |
if lc != 0: | |
values = l.split("\t") | |
vc = 0 | |
# print ("There are ", len(values), " values in line", lc , "v[0] = ", values[0]) | |
if len(values) == 11: | |
for v in values: | |
#print v | |
vn = float(v) | |
parsed_data[vc].append(vn) | |
vc = vc + 1 | |
else: | |
names = l.split("\t") | |
lc = lc + 1 | |
return (names, parsed_data) | |
def filter(indata, n): | |
outdata=[] | |
queue_samples = [] | |
cn = 0 | |
for x in indata: | |
queue_samples.append(x) | |
if len(queue_samples) > n: | |
queue_samples = queue_samples[1:] | |
xs = sum(queue_samples)/len(queue_samples) | |
outdata.append(xs) | |
cn = cn+1 | |
return outdata | |
def derivate(time, data): | |
outdata = [] | |
oldx = data[0] | |
oldt = time[0] - time[1] | |
for i in range(0, len(time)): | |
dt = time[i] - oldt | |
dx = data[i] - oldx | |
if dt > 0.0: | |
d = dx / dt | |
else: | |
d = 0.0 | |
if d > 50.0: | |
d = 50.0 | |
if d < -50.0: | |
d = -50.0 | |
outdata.append(d) | |
oldx = data[i] | |
oldt = time[i] | |
return outdata | |
def getLogNames(mypath): | |
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))] | |
return onlyfiles | |
def listIndex(list, value): | |
for i in range(0,len(list)): | |
if list[i] == value: | |
return i | |
return -1 | |
def main(): | |
escalas = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 5.0, 1.0, 1.0] | |
leftAxis = [True, True, True, False, True, True, True, True, True, True, True] | |
logs = getLogNames("Logs") | |
if sys.platform == "iphoneos": | |
name = dialogs.list_dialog("Select a file", logs, False) | |
else: | |
for i in range(0, len(logs)): | |
print (i, logs[i]) | |
try: | |
f = int(raw_input("Select a file:")) | |
if f < 0 or len(logs) < f : | |
return | |
name = logs[f] | |
except ValueError: | |
return | |
if name == None: | |
return | |
path = "Logs/"+name | |
#name = "Logs/proves_1.csv" | |
with open(path, "r") as rfile_: | |
data = rfile_.read() | |
(names, parsed_data) = parse_data(data) | |
lista = names[1:] | |
if sys.platform == "iphoneos": | |
vars = dialogs.list_dialog("Select variables", lista, True) | |
print vars | |
# else: | |
# for i in range(0, len(logs)): | |
# print (i, logs[i]) | |
left = [] | |
left_label="" | |
right = [] | |
right_label="" | |
for x in vars: | |
i = listIndex(names, x) | |
print(x, i) | |
v = (i, escalas[i]) | |
if leftAxis[i]: | |
left.append(v) | |
left_label = left_label + " " + x | |
else: | |
right.append(v) | |
right_label = right_label + " " + x | |
plot_data(names, parsed_data, name,left, left_label, right, right_label) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment