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