1
__author__="fccoelho@gmail.com"
2
__date__ ="$26/02/2009 10:44:29$"
3
__docformat__ = "restructuredtext en"
7
from SimpleXMLRPCServer import SimpleXMLRPCServer
8
#from twisted.web import xmlrpc, server
9
#from twisted.internet import reactor
10
from multiprocessing import Process
11
from threading import Thread, Lock
12
from Queue import Queue
15
Gnuplot.GnuplotOpts.prefer_inline_data = 1
16
Gnuplot.GnuplotOpts.prefer_fifo_data = 0
29
"""Decorator that places the call on a queue"""
30
def queued(self,*args,**kw):
31
Q.put((f,(self,)+args))
32
queued.__doc__ = f.__doc__
33
queued.__name__ = f.__name__
38
Real time plotting class based on Gnuplot
40
def __init__(self, persist=0,debug=0,**kwargs):
41
self.gp = Gnuplot.Gnuplot(persist = persist, debug=debug)
44
self.persist = persist
45
self.hold = 0 if 'hold' not in kwargs else kwargs['hold']
46
t= Thread(target=worker,args=())
50
def set_hold(self,on):
52
Sets hold state of the plot.
53
takes 0 or 1 as argument
70
def flush_queue(self):
75
def scatter(self,x,y,labels=[],title='',style='points', jitter=True, multiplot=0):
77
Makes scatter plots from numpy arrays.
78
if x and are multidimensional(lists of lists), multiple scatter plots will be generated, pairing rows.
81
-`x`: list of numbers or list of lists
82
-`y`: list of numbers or list of lists
83
-`labels`: list of strings (variable names)
84
-`title`: Title of the plot
88
sq = numpy.sqrt(len(x))
89
ad = 1 if sq%1 >0.5 else 0
90
r= numpy.floor(sq);c=numpy.ceil(sq)+ad
93
self.gp('set multiplot layout %s,%s title "%s"'%(r, c, title))
95
self.gp('set title "%s"'%title)
97
jt = numpy.random.normal(1, 1e-4,1)[0]
103
if x.shape != y.shape:
104
raise ValueError("x, %s and y, %s arrays must have the same shape."%(x.shape,y.shape))
108
raise ValueError("Labels list should contain exactly 2 elements, but has %s"%len(labels))
110
if len(labels) != x.shape[0]:
111
raise ValueError("labels list must have exactly %s items, but has %s."%(x.shape[0],len(labels)))
114
self.gp('set title "%s"'%title)
116
labels = ['s%s'%i for i in range(x.shape[0])]
117
if len(x.shape) > 1 and len(x.shape) <= 2:
119
for n in range(x.shape[0]):
120
self.plots.append(Gnuplot.PlotItems.Data(x[n]*jt,y[n]*jt,title=labels[i],with_=style))
123
[self.gp.plot(pl) for pl in self.plots]
124
self.gp('unset multiplot')
126
self.gp.plot(*tuple(self.plots))
127
elif len(x.shape) >2:
131
self.plots.append(Gnuplot.PlotItems.Data(x*jt,y*jt,title=labels[0],with_=style))
133
[self.gp.plot(pl) for pl in self.plots]
134
self.gp('unset multiplot')
136
self.gp.plot(*tuple(self.plots))
142
def lines(self, data, x=[], labels=[],title='',style='lines', multiplot=0):
144
Create a single/multiple line plot from a numpy array or record array.
147
- `data`: must be a list of lists.
148
- `x`: x values for the series: list
149
- `labels`: is a list of strings to serve as legend labels
150
- `style`: plot styles from gnuplot: lines, boxes, points, linespoints, etc.
151
- `multiplot`: Whether to make multiple subplots
153
#self.gp('set style %s 1'%style)
155
sq = numpy.sqrt(len(data))
156
ad = 1 if sq%1 >0.5 else 0
157
r= numpy.floor(sq);c=numpy.ceil(sq)+ad
160
self.gp('set multiplot layout %s,%s title "%s"'%(r, c, title))
162
self.gp('set title "%s"'%title)
164
assert isinstance (data, list)
165
data = numpy.array(data)
167
if len(data.shape) > 1 and len(data.shape) <= 2:
171
x = numpy.arange(len(row))
173
self.plots.append(Gnuplot.PlotItems.Data(x, row,title=labels[i], with_=style))
175
self.plots.append(Gnuplot.PlotItems.Data(x, row, with_=style))
178
[self.gp.plot(pl) for pl in self.plots]
179
self.gp('unset multiplot')
181
self.gp.plot(*tuple(self.plots))
182
elif len(data.shape) >2:
187
x = numpy.arange(len(data))
188
self.plots.append(Gnuplot.PlotItems.Data(x,data,title=labels[0],with_=style))
189
self.gp.plot(*tuple(self.plots))
191
self.gp('unset multiplot')
197
#~ def histogram(self,data,labels=[],title='',multiplot=0):
198
#~ self.Queue.put((self._histogram,(data,labels,title,multiplot)))
200
def histogram(self,data,labels=[],title='',multiplot=0,**kwargs):
202
Create a single/multiple Histogram plot from a numpy array or record array.
205
- `data`: must be a list of lists.
206
- `labels`: is a list of strings to serve as legend labels
207
- `multiplot`: Whether to make multiple subplots
210
sq = numpy.sqrt(len(data))
211
ad = 1 if sq%1 >0.5 else 0
212
r= numpy.floor(sq);c=numpy.ceil(sq)+ad
215
self.gp('set multiplot layout %s,%s title "%s"'%(r, c, title))
217
self.gp('set title "%s"'%title)
218
self.gp('set style data boxes')
220
assert isinstance (data, list)
221
data = numpy.array(data)
223
labels = ['Var_%s'%i for i in range(data.shape[0])]
224
if len(data.shape) == 2:
225
for n,row in enumerate(data):
226
m,bins = numpy.histogram(row,normed=True,bins=50)
228
self.plots.append(Gnuplot.PlotItems.Data(d,title=labels[n]))
231
[self.gp.plot(pl) for pl in self.plots]
232
self.gp('unset multiplot')
234
self.gp.plot(*tuple(self.plots))
237
elif len(data.shape) >2:
239
elif len(data.shape) == 1:
240
m,bins = numpy.histogram(data,normed=True,bins=50)
242
self.plots.append(Gnuplot.PlotItems.Data(d,title=labels[0]))
243
self.gp.plot(*tuple(self.plots))
245
self.gp('unset multiplot')
253
def _start_server(server, persist,hold):
254
server.register_instance(RTplot(persist=persist, hold=hold))
255
server.register_introspection_functions()
256
server.serve_forever()
260
def rpc_plot(port=0, persist=0, hold=0):
262
XML RPC plot server factory function
263
returns port if server successfully started or 0
268
if port in __ports_used:
272
server = SimpleXMLRPCServer(("localhost", port),logRequests=False, allow_none=True)
273
server.register_introspection_functions()
274
p = Process(target=_start_server, args=(server, persist, hold))
276
#p = Process(target=_start_twisted_server, args=(port, persist))
283
__ports_used.append(port)
287
if __name__ == "__main__":