diff --git a/PlotCanvas.py b/PlotCanvas.py index 1634c996..a41e3715 100644 --- a/PlotCanvas.py +++ b/PlotCanvas.py @@ -22,6 +22,21 @@ log = logging.getLogger('base') class CanvasCache(QtCore.QObject): + """ + + Case story #1: + + 1) No objects in the project. + 2) Object is created (new_object() emits object_created(obj)). + on_object_created() adds (i) object to collection and emits + (ii) new_object_available() then calls (iii) object.plot() + 3) object.plot() creates axes if necessary on + app.collection.figure. Then plots on it. + 4) Plots on a cache-size canvas (in background). + 5) Plot completes. Bitmap is generated. + 6) Visible canvas is painted. + + """ # Signals: # A bitmap is ready to be displayed. diff --git a/tests/canvas/performance.py b/tests/canvas/performance.py new file mode 100644 index 00000000..9478bcbf --- /dev/null +++ b/tests/canvas/performance.py @@ -0,0 +1,95 @@ +from __future__ import division +import matplotlib +matplotlib.use('Agg') +import matplotlib.pyplot as plt +import numpy as np +import cStringIO +from matplotlib.backends.backend_agg import FigureCanvasAgg +from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg +from matplotlib.figure import Figure +import cProfile +import sys + + +def gen_data(): + N = 100000 + x = np.random.rand(N) * 10 + y = np.random.rand(N) * 10 + colors = np.random.rand(N) + area = np.pi * (15 * np.random.rand(N))**2 # 0 to 15 point radiuses + data = x, y, area, colors + return data + + +# @profile +def large_plot(data): + x, y, area, colors = data + + fig = Figure(figsize=(10, 10), dpi=80) + axes = fig.add_axes([0.0, 0.0, 1.0, 1.0], alpha=1.0) + axes.set_frame_on(False) + axes.set_xticks([]) + axes.set_yticks([]) + # axes.set_xlim(0, 10) + # axes.set_ylim(0, 10) + + axes.scatter(x, y, s=area, c=colors, alpha=0.5) + + axes.set_xlim(0, 10) + axes.set_ylim(0, 10) + + canvas = FigureCanvasAgg(fig) + canvas.draw() + # canvas = FigureCanvasQTAgg(fig) + # buf = canvas.tostring_rgb() + buf = fig.canvas.tostring_rgb() + + ncols, nrows = fig.canvas.get_width_height() + img = np.fromstring(buf, dtype=np.uint8).reshape(nrows, ncols, 3) + + return img + + +def small_plot(data): + x, y, area, colors = data + + fig = Figure(figsize=(3, 3), dpi=80) + axes = fig.add_axes([0.0, 0.0, 1.0, 1.0], alpha=1.0) + axes.set_frame_on(False) + axes.set_xticks([]) + axes.set_yticks([]) + # axes.set_xlim(5, 6) + # axes.set_ylim(5, 6) + + axes.scatter(x, y, s=area, c=colors, alpha=0.5) + + axes.set_xlim(4, 7) + axes.set_ylim(4, 7) + + canvas = FigureCanvasAgg(fig) + canvas.draw() + # canvas = FigureCanvasQTAgg(fig) + # buf = canvas.tostring_rgb() + buf = fig.canvas.tostring_rgb() + + ncols, nrows = fig.canvas.get_width_height() + img = np.fromstring(buf, dtype=np.uint8).reshape(nrows, ncols, 3) + + return img + +def doit(): + d = gen_data() + img = large_plot(d) + return img + + +if __name__ == "__main__": + + d = gen_data() + + if sys.argv[1] == 'large': + cProfile.runctx('large_plot(d)', None, locals()) + else: + cProfile.runctx('small_plot(d)', None, locals()) + + diff --git a/tests/canvas/prof.sh b/tests/canvas/prof.sh new file mode 100755 index 00000000..b9075848 --- /dev/null +++ b/tests/canvas/prof.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +echo "*** LARGE ***" +python performance.py large | egrep "(\(scatter\))|(\(draw\))|(tostring_rgb)|(fromstring)" +echo "*** SMALL ***" +python performance.py small | egrep "(\(scatter\))|(\(draw\))|(tostring_rgb)|(fromstring)" \ No newline at end of file