Ok, so I was just going for proof of concept here and I did manage to get this working. (Discalimer: I have been told this is something that should be using threading but I'm new to python and that might be out of my grasp currently) Its a program that reads a csv file and plots the data, then repeats. I have a program writing the csv and I am able to see the plot being created in real time which is exactly what I wanted. What I don't undestand fully is why its crashing. I understand my code is pretty sloppy but any mouse click anywhere on the screen or even just tabbing to a different screen causes my program to stop responding. Is there an easy fix to this?
import csv
import time
import matplotlib.pyplot as plt
import matplotlib
filename = 'sample.csv'
callback = ''
global counter
results =''
counter = 0
timestr = []
thickness = []
thick = []
def csv_process(filename, callback, delay=1):
class waiter:
def __init__(self, fd, delay=1):
self.fd = fd
self.delay = delay
def __iter__(self):
return self
def next(self):
while True:
line = fd.readline()
if line:
return line
time.sleep(self.delay)
results()
def __next__(self): # bc im using Python3.4
return self.next()
with open(filename, "rt") as fd:
rows = csv.reader(waiter(fd, delay), delimiter=',')
for row in rows:
callback(row)
results()
def process_row(row):
global time, thickness, counter, timestr, thick, results
counter = counter+1
if counter > 5: #csv file has 5 lines of header data
hrs = float(row[0][11:13]) #had a hard time getting the data into a format I could plot
mins = float(row[0][14:16])
secs = float(row[0][17:22])
timestr.append(hrs*1440+mins*60+secs)
thick = float(row[3])
thickness.append(thick)
return thickness, timestr
fig = plt.figure()
axes = fig.add_subplot(111)
line, = axes.plot([], [], '.')
plt.ion()
def results():
global line
line.set_data(timestr, thickness)
plt.show()
while 1:
plt.draw()
axes.relim()
axes.autoscale(True,'both',True)
axes.autoscale_view(True,True,True)
return line
if __name__ == "__main__":
csv_process(filename, process_row)
Sample csv data:
% Comments:
% Samples Averaged: 1
% Factor: 1
% Units: um
% A,B,C,D,E,F,G,H,I,J
2015-08-14 13:09:52.809000,1,0,1257.57,,,,0,0,-1
2015-08-14 13:09:52.824000,1,0,1257.57,,,,0,0,-1
2015-08-14 13:09:52.839000,1,0,1257.57,,,,0,0,-1
2015-08-14 13:09:52.854000,1,0,1257.57,,,,0,0,-1
2015-08-14 13:09:52.869000,1,0,1257.57,,,,0,0,-1
2015-08-14 13:09:52.884000,1,0,1257.57,,,,0,0,-1
2015-08-14 13:09:52.899000,1,0,1257.57,,,,0,0,-1