1010
1111import time
1212import numpy as np
13+ import sys
1314
14- from qwt .qt .QtGui import (QApplication , QPen , QBrush , QMainWindow , QGridLayout ,
15+ from qwt .qt .QtGui import (QApplication , QPen , QMainWindow , QGridLayout ,
1516 QTabWidget , QWidget , QTextEdit , QLineEdit , QFont ,
1617 QFontDatabase )
17- from qwt .qt .QtCore import QSize
1818from qwt .qt .QtCore import Qt
19- from qwt import QwtPlot , QwtSymbol , QwtPlotCurve
19+
20+ import os
21+ if os .environ .get ('USE_PYQWT5' , False ):
22+ USE_PYQWT5 = True
23+ from PyQt4 .Qwt5 import QwtPlot , QwtPlotCurve
24+ else :
25+ USE_PYQWT5 = False
26+ from qwt import QwtPlot , QwtPlotCurve # analysis:ignore
2027
2128
2229COLOR_INDEX = None
@@ -38,34 +45,40 @@ def __init__(self, title, xdata, ydata, style, symbol=None, *args):
3845 self .setTitle (title )
3946 self .setAxisTitle (QwtPlot .xBottom , 'x' )
4047 self .setAxisTitle (QwtPlot .yLeft , 'y' )
41- curve = QwtPlotCurve ()
42- curve .setPen (QPen (get_curve_color ()))
43- curve .setStyle (style )
44- curve .setRenderHint (QwtPlotCurve .RenderAntialiased )
45- if symbol is not None :
46- curve .setSymbol (symbol )
47- curve .attach (self )
48- curve .setData (xdata , ydata )
48+ for idx in range (1 , 11 ):
49+ curve = QwtPlotCurve ()
50+ curve .setPen (QPen (get_curve_color ()))
51+ curve .setStyle (style )
52+ curve .setRenderHint (QwtPlotCurve .RenderAntialiased )
53+ if symbol is not None :
54+ curve .setSymbol (symbol )
55+ curve .attach (self )
56+ curve .setData (xdata , ydata * idx )
57+ # self.setAxisScale(self.yLeft, -1.5, 1.5)
58+ # self.setAxisScale(self.xBottom, 9.9, 10.)
4959 self .replot ()
5060
5161
5262class BMWidget (QWidget ):
53- def __init__ (self , points , * args ):
63+ def __init__ (self , points , * args , ** kwargs ):
5464 super (BMWidget , self ).__init__ ()
55- self .setup (points , * args )
65+ self .setup (points , * args , ** kwargs )
5666
57- def params (self , * args ):
58- return (
59- ('Lines' , None ),
60- ('Dots' , None ),
61- )
67+ def params (self , * args , ** kwargs ):
68+ if kwargs .get ('only_lines' , False ):
69+ return (('Lines' , None ),)
70+ else :
71+ return (
72+ ('Lines' , None ),
73+ ('Dots' , None ),
74+ )
6275
63- def setup (self , points , * args ):
76+ def setup (self , points , * args , ** kwargs ):
6477 x = np .linspace (.001 , 20. , points )
6578 y = (np .sin (x )/ x )* np .cos (20 * x )
6679 layout = QGridLayout ()
6780 nbcol , col , row = 2 , 0 , 0
68- for style , symbol in self .params (* args ):
81+ for style , symbol in self .params (* args , ** kwargs ):
6982 layout .addWidget (BMPlot (style , x , y , getattr (QwtPlotCurve , style ),
7083 symbol = symbol ), row , col )
7184 col += 1
@@ -81,50 +94,69 @@ def setup(self, points, *args):
8194
8295
8396class BMText (QTextEdit ):
84- def __init__ (self , parent = None ):
97+ def __init__ (self , parent = None , title = None ):
8598 super (BMText , self ).__init__ (parent )
8699 self .setReadOnly (True )
100+ library = 'PyQwt5' if USE_PYQWT5 else 'PythonQwt'
101+ wintitle = self .parent ().windowTitle ()
102+ if not wintitle :
103+ wintitle = "Benchmark"
104+ if title is None :
105+ title = '%s example' % wintitle
106+ self .parent ().setWindowTitle ('%s [%s]' % (wintitle , library ))
87107 self .setText ("""\
88- <b>Curve benchmark example:</b><br><br>
108+ <b>%s:</b><br>
109+ (base plotting library: %s)<br><br>
89110Click on each tab to test if plotting performance is acceptable in terms of
90111GUI response time (switch between tabs, resize main windows, ...).<br>
91112<br><br>
92113<b>Benchmarks results:</b>
93- """ )
114+ """ % ( title , library ) )
94115
95116
96117class BMDemo (QMainWindow ):
118+ TITLE = 'Curve benchmark'
119+ SIZE = (1000 , 800 )
97120 def __init__ (self , max_n , parent = None , ** kwargs ):
98121 super (BMDemo , self ).__init__ (parent = parent )
99- self .setWindowTitle ('Curve benchmark' )
100- tabs = QTabWidget ()
101- self .setCentralWidget (tabs )
102- contents = BMText ()
103- tabs .addTab (contents , 'Contents' )
104- self .resize (1000 , 600 )
122+ title = self .TITLE
123+ if kwargs .get ('only_lines' , False ):
124+ title = '%s (%s)' % (title , 'only lines' )
125+ self .setWindowTitle (title )
126+ self .tabs = QTabWidget ()
127+ self .setCentralWidget (self .tabs )
128+ self .text = BMText (self )
129+ self .tabs .addTab (self .text , 'Contents' )
130+ self .resize (* self .SIZE )
105131
106132 # Force window to show up and refresh (for test purpose only)
107133 self .show ()
108134 QApplication .processEvents ()
109-
135+
110136 t0g = time .time ()
137+ self .run_benchmark (max_n , ** kwargs )
138+ dt = time .time ()- t0g
139+ self .text .append ("<br><br><u>Total elapsed time</u>: %d ms" % (dt * 1e3 ))
140+ self .tabs .setCurrentIndex (0 )
141+
142+ def process_iteration (self , title , widget , t0 ):
143+ self .tabs .addTab (widget , title )
144+ self .tabs .setCurrentWidget (widget )
145+
146+ # Force widget to refresh (for test purpose only)
147+ QApplication .processEvents ()
148+
149+ time_str = "Elapsed time: %d ms" % ((time .time ()- t0 )* 1000 )
150+ widget .text .setText (time_str )
151+ self .text .append ("<br><i>%s:</i><br>%s" % (title , time_str ))
152+
153+ def run_benchmark (self , max_n , ** kwargs ):
111154 for idx in range (4 , - 1 , - 1 ):
112155 points = max_n / 10 ** idx
113- t0 = time .time ()
114- widget = BMWidget (points )
115156 title = '%d points' % points
116- tabs .addTab (widget , title )
117- tabs .setCurrentWidget (widget )
118-
119- # Force widget to refresh (for test purpose only)
120- QApplication .processEvents ()
121-
122- time_str = "Elapsed time: %d ms" % ((time .time ()- t0 )* 1000 )
123- widget .text .setText (time_str )
124- contents .append ("<br><i>%s:</i><br>%s" % (title , time_str ))
125- dt = time .time ()- t0g
126- contents .append ("<br><br><u>Total elapsed time</u>: %d ms" % (dt * 1000 ))
127- tabs .setCurrentIndex (0 )
157+ t0 = time .time ()
158+ widget = BMWidget (points , ** kwargs )
159+ self .process_iteration (title , widget , t0 )
128160
129161
130162if __name__ == '__main__' :
@@ -133,5 +165,8 @@ def __init__(self, max_n, parent=None, **kwargs):
133165 if name in QFontDatabase ().families ():
134166 app .setFont (QFont (name ))
135167 break
136- demo = BMDemo (1000000 )
168+ kwargs = {}
169+ for arg in sys .argv [1 :]:
170+ kwargs [arg ] = True
171+ demo = BMDemo (1000000 , ** kwargs )
137172 app .exec_ ()
0 commit comments