Performance improvements, broke the background patches

This commit is contained in:
Kienan Stewart 2015-03-22 18:54:30 -04:00
parent f328a6dae6
commit 974a189e52
1 changed files with 47 additions and 29 deletions

76
main.py
View File

@ -31,8 +31,9 @@ import os
import os.path
import pygame.image
import Queue
import StringIO
import cStringIO
import temp_log
import time
import threading
dataThread = None
@ -97,6 +98,8 @@ class MainWindow(FloatLayout):
else:
self.lastStatus = {'status' : 'paused', 'message' : 'Data reception halted by user'}
Logger.debug('MainWindow: state set to ' + str(self.state) + ' : ignoring data')
if dataThreadDataQueue.qsize():
Logger.debug('Queue Size: ' + str(dataThreadDataQueue.qsize()))
except Queue.Empty:
pass
@ -389,6 +392,7 @@ class PlotWidget(Image):
self.data = numpy.array([], dtype=float);
self.lastTime = 0
self.lastTemperature = 0
self.lastSave = 0
self.figure = matplotlib.pyplot.figure()
self.plot_axes = self.figure.add_subplot(1, 1, 1)
self.plot_axes.hold(False)
@ -420,15 +424,17 @@ class PlotWidget(Image):
matplotlib.patches.Rectangle(origin, width, height,
fc = color, visible = True,
fill = True)
#matplotlib.patches.Polygon(vertices, facecolor = color)
)
legend_handles.append(p)
legend_strings.append(name)
self.figure.legend(legend_handles, legend_strings, 'upper left',
# Disabled for the moment
self.figure.legend(legend_handles, legend_strings, 'lower right',
title = 'Enzyme Activity Zones')
return legend_handles
def clear_data(self):
self.update_output_files(True)
self.data = numpy.array([], dtype=float);
self.reference_profile_file = ''
self.reference_profile = None
@ -458,9 +464,7 @@ class PlotWidget(Image):
self.texture.flip_vertical()
# Update output files, if any
# @TODO Add in an update interval so the disk isn't hammered on every single update
self.update_image_output_file()
self.update_raw_data_output_file()
self.update_output_files()
def to_image_data(self):
# Add in polygons for hilightning particular temperature ranges of interest.
@ -470,42 +474,52 @@ class PlotWidget(Image):
temp_max = 80
handles = []
if self.data.any():
data = numpy.copy(self.data)
data = data.transpose()
#data = numpy.copy(self.data)
data = self.data.transpose()
t, temperature = numpy.split(data, 2, axis = 0)
if t[0][-1] > time_max:
time_max = t[0][-1]
if numpy.nanmax(temperature[0]) > temp_max:
temp_max = numpy.nanmax(temperature[0])
#if numpy.nanmax(temperature[0]) > temp_max:
# temp_max = numpy.nanmax(temperature[0])
plot_args.extend((t[0], temperature[0], 'b'))
handles.append('Recorded Temperature Profile')
if self.reference_profile is not None and self.reference_profile.any():
reference_data = numpy.copy(self.reference_profile)
reference_data = reference_data.transpose()
#reference_data = numpy.copy(self.reference_profile)
reference_data = self.reference_profile.transpose()
t2, reference_temperature = numpy.split(reference_data, 2, axis = 0)
# Add t[0] to all reference points to make it fit on the current graph
if t:
t2 = numpy.add(t2, t[0][0])
t2 = numpy.add(t2, t[0][0])
if t2[0][-1] > time_max:
time_max = t2[0][-1]
if numpy.nanmax(reference_temperature[0]) > temp_max:
temp_max = numpy.nanmax(reference_temperature[0])
#if numpy.nanmax(reference_temperature[0]) > temp_max:
# temp_max = numpy.nanmax(reference_temperature[0])
plot_args.extend((t2[0], reference_temperature[0], 'r'))
handles.append('Reference Temperature Profile')
lines = self.plot_axes.plot(*plot_args)
# Patches must be changed after the axes are plotted.
self.update_patches(time_max)
self.plot_axes.legend(lines, handles, 'upper right', title = 'Temperature Profiles')
if not lines:
# Set a default in case no data is plotted. This may allow patches to show,
# and it will look a little nicer before recording starts.
self.plot_axes.set_xlim(0, time_max)
self.plot_axes.set_ylim(0, temp_max)
image_data = StringIO.StringIO()
self.update_patches()
# Disabled the legend for the moment.
# @TODO Make a small subplot next to the main plot into which legends may be placed.
#self.plot_axes.legend(lines, handles, 'lower right', title = 'Temperature Profiles')
# Set a default in case no data is plotted. This may allow patches to show,
# and it will look a little nicer before recording starts.
#self.plot_axes.set_xlim(0, time_max)
self.plot_axes.set_ylim(0, temp_max)
image_data = cStringIO.StringIO()
self.figure.savefig(image_data, format = 'png')
image_data.seek(0)
self._image_raw_data = image_data
return self._image_raw_data
self._image_raw_data = image_data.getvalue()
return image_data
def update_output_files(self, force = False):
# Do this only once every 15 seconds to avoid hitting the disk frequently
Logger.debug('update_output_files called')
if force or time.time() - self.lastSave > 15:
Logger.debug('update_output_files going ahead')
self.update_raw_data_output_file()
self.update_image_output_file()
self.lastSave = time.time()
def update_raw_data_output_file(self):
@ -515,11 +529,15 @@ class PlotWidget(Image):
def update_image_output_file(self, image_data = None):
if not image_data:
image_data = self.to_image_data()
image_data = self._image_raw_data
if self.image_output_file and image_data:
f = open(self.image_output_file, 'w')
f.write(image_data.read())
f.close()
if f:
Logger.debug('Updated image output file')
f.write(image_data)
f.close()
else:
Logger.debug('update_image_output_file: Unable to open file ' + self.image_output_file)
def on_lastTemperature(self, value):