/
revis.py
444 lines (367 loc) · 15.6 KB
/
revis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# Copyright 2011 Robert Schroll, rschroll@gmail.com
# http://rschroll.github.com/revis/
#
# This file is distributed under the terms of the BSD license, available
# at http://www.opensource.org/licenses/bsd-license.php
"""revis is an extension for Reinteract that embeds visvis figures in
worksheets. Syntax:
>>> with figure() as f:
... <plotting command>
... :
... <plotting command>
... f
where <plotting command> is any visvis command. The single-command
plotting functions may be used without the with block.
"""
__version__ = "0.1"
import os as _os
import tempfile as _tempfile
import threading as _threading
import cairo as _cairo
import gobject as _gobject
import gtk as _gtk
import gtk.gtkgl as _gtkgl# To keep from crashing on load.
from visvis.backends.backend_gtk import Figure as _Figure, BaseFigure as _BaseFigure, \
GlCanvas as _GlCanvas, app as _app
import visvis
if visvis.__version__.split('.') < ['1', '5']:
print "Warning: visvis %s is not supported by revis. Please upgrade to at least 1.5."%visvis.__version__
from reinteract.custom_result import CustomResult as _CustomResult
from reinteract.statement import Statement as _Statement
# From Stephen Langer (stephen.langer@nist.gov)
class IdleBlockCallback:
def __init__(self, func, args=(), kwargs={}):
self.func = func
self.args = args
self.kwargs = kwargs
self.event = _threading.Event()
self.result = None
def __call__(self):
#gtk.gdk.threads_enter()
try:
self.result = self.func(*self.args, **self.kwargs)
finally:
_gtk.gdk.flush()
# gtk.gdk.threads_leave()
self.event.set()
return False # don't repeat
def _run_in_main_loop(func, *args, **kwargs):
if _gobject.main_depth():
# In the main loop already
return func(*args, **kwargs)
callbackobj = IdleBlockCallback(func, args, kwargs)
callbackobj.event.clear()
_gobject.idle_add(callbackobj, priority=_gobject.PRIORITY_LOW)
callbackobj.event.wait()
return callbackobj.result
def _gdk2float(gdkcolor):
maxcint = 65536.
return gdkcolor.red/maxcint, gdkcolor.green/maxcint, gdkcolor.blue/maxcint
class LightsWindow(_gtk.Window):
def __init__(self, figure, openbutton):
_gtk.Window.__init__(self) #, gtk.WINDOW_POPUP)
self.set_resizable(False)
self.set_title('Lights')
self.figure = figure
self.openbutton = openbutton
table = _gtk.Table(6, 4, True)
self.chooser = _gtk.combo_box_new_text()
for i in range(8):
self.chooser.append_text(str(i))
self.chooser.connect("changed", self.on_choose_light)
table.attach(self.chooser, 0,1, 0,1)
for i, txt in enumerate(('position', 'ambient', 'diffuse', 'specular', 'color')):
lab = _gtk.Label(txt)
lab.set_alignment(1, 0.5)
table.attach(lab, 0,1, 1+i,2+i)
self.cb_on = _gtk.CheckButton('On')
self.cb_on.connect("toggled", self.on_set_bool, "isOn")
table.attach(self.cb_on, 1,2, 0,1)
self.cb_cam = _gtk.CheckButton('Camlight')
self.cb_cam.connect("toggled", self.on_set_bool, "isCamLight")
table.attach(self.cb_cam, 2,3, 0,1) # Should be able to do 2,4, but this messes up prev cb
self.sb_pos = [_gtk.SpinButton(_gtk.Adjustment(0,-10,10,0.1,1), digits=1) for i in range(4)]
hbox = _gtk.HBox()
for sb in self.sb_pos:
sb.connect("value-changed", self.on_change_position)
hbox.pack_start(sb)
table.attach(hbox, 1,4, 1,2)
self.sliders = [(val, _gtk.HScale(_gtk.Adjustment(0, 0, 1, 0.01, 0.1))) for val in ('ambient', 'diffuse', 'specular')]
for i, (val, slider) in enumerate(self.sliders):
slider.set_digits(2)
slider.set_value_pos(_gtk.POS_RIGHT)
slider.connect("value-changed", self.on_change_intensity, val)
table.attach(slider, 1,4, 2+i,3+i)
box = _gtk.HBox()
self.color = _gtk.ColorButton()
self.color.connect("color-set", self.on_change_color)
box.pack_start(self.color, False, False)
table.attach(box, 1,4, 5,6)
table.show_all()
self.add(table)
self.connect('delete-event', self.on_delete)
def open(self):
self.chooser.set_active(0)
self.show()
def on_delete(self, widget, event):
self.openbutton.set_active(False)
return True
def on_choose_light(self, widget):
self.currlight = None
currlight = self.figure.currentAxes.lights[widget.get_active()]
self.cb_on.set_active(currlight.isOn)
self.cb_cam.set_active(currlight.isCamLight)
for pos, sb in zip(currlight.position, self.sb_pos):
sb.set_value(pos)
for val, slider in self.sliders:
cval = getattr(currlight, val)
if isinstance(cval, tuple):
print "Warning - destroying color of", val
cval = (cval[0] + cval[1] + cval[2])/3.
slider.set_value(cval)
self.color.set_color(_gtk.gdk.Color(*map(float, currlight.color[:3])))
self.currlight = currlight
def on_set_bool(self, widget, attr):
if self.currlight is not None:
if attr is "isOn":
self.currlight.On(widget.get_active())
else:
setattr(self.currlight, attr, widget.get_active())
def on_change_position(self, widget):
if self.currlight is not None:
self.currlight.position = [sb.get_value() for sb in self.sb_pos]
def on_change_intensity(self, widget, attr):
if self.currlight is not None:
setattr(self.currlight, attr, widget.get_value())
def on_change_color(self, widget):
if self.currlight is not None:
self.currlight.color = _gdk2float(widget.get_color())
class Toolbar(_gtk.Toolbar):
def __init__(self, figure):
_gtk.Toolbar.__init__(self)
self.figure = figure
self.set_style(_gtk.TOOLBAR_BOTH_HORIZ)
savebutton = _gtk.ToolButton(_gtk.STOCK_SAVE_AS)
savebutton.connect("clicked", self.savefig)
self.insert(savebutton, 0)
lightbutton = _gtk.ToggleToolButton()
lightbutton.set_icon_widget(_gtk.Label('Lights'))
lightbutton.set_label('Lights')
lightbutton.connect("toggled", self.on_toggle_lights)
self.insert(lightbutton, -1)
self.lights_window = LightsWindow(self.figure, lightbutton)
sep = _gtk.SeparatorToolItem()
sep.set_expand(True)
sep.set_property('draw', False)
self.insert(sep, -1)
ti = _gtk.ToolItem()
self.view_lab = _gtk.Label()
self.view_lab.set_justify(_gtk.JUSTIFY_RIGHT)
ti.add(self.view_lab)
self.insert(ti, -1)
self.connect("destroy", self.on_destroy)
def on_destroy(self, widget):
self.lights_window.destroy()
return False
def savefig(self, widget):
chooser = _gtk.FileChooserDialog("Save As...", None, _gtk.FILE_CHOOSER_ACTION_SAVE,
(_gtk.STOCK_CANCEL, _gtk.RESPONSE_CANCEL,
_gtk.STOCK_SAVE, _gtk.RESPONSE_OK))
chooser.set_default_response(_gtk.RESPONSE_OK)
response = chooser.run()
filename = None
if response == _gtk.RESPONSE_OK:
filename = chooser.get_filename()
chooser.destroy()
if filename is not None:
visvis.screenshot(filename, self.figure, sf=1)
def update_view(self):
view = self.figure.currentAxes.GetView()
viewstr = ""
isflycam = 1
for k,v in sorted(view.items()):
if k == 'azimuth':
isflycam = 0
if k == ('fov', 'rotation')[isflycam]:
viewstr += '\n'
viewstr += k + ': '
if isinstance(v, tuple):
viewstr += '(' + ','.join(['%0.3g'%vv for vv in v]) + ') '
elif isinstance(v, visvis.Quaternion):
viewstr += '%0.2g+%0.2gi+%0.2gj+%0.2gk '%(v.w, v.x, v.y, v.z)
else:
viewstr += '%0.3g '%v
self.view_lab.set_text(viewstr)
def on_toggle_lights(self, widget):
if widget.get_active():
pos = widget.allocation
xo, yo = widget.window.get_origin()
self.lights_window.move(xo + pos.x, yo + pos.y + pos.height)
self.lights_window.set_transient_for(widget.get_toplevel())
self.lights_window.open()
else:
self.lights_window.hide()
class SuperFigure(_Figure, _CustomResult):
lock = _threading.RLock()
current_fig = None
def __init__(self, disable_output=True, figsize=(560,420), display='inline', **figkw):
self._widget = None
_BaseFigure.__init__(self, **figkw) # Skip Figure, to avoid creating _widget
self._disable_output = disable_output
self._size = figsize
self.display = display
def _GetPosition(self):
# Sometimes this is called before the widget is made, so we have
# to fake the answer
if self._widget is not None:
return _Figure._GetPosition(self)
else:
return 0, 0, self._size[0], self._size[1]
def _ProcessGuiEvents(self):
_run_in_main_loop(_Figure._ProcessGuiEvents, self)
def _RedrawGui(self):
_Figure._RedrawGui(self)
if hasattr(self, 'toolbar'):
self.toolbar.update_view()
def __enter__(self):
self.__class__.lock.acquire()
self.__class__.current_fig = self
self._disable_reinteract_output()
return self
def __exit__(self, type, value, traceback):
self.__class__.current_fig = None
self._restore_reinteract_output()
self.__class__.lock.release()
def _disable_reinteract_output(self):
self.statement = _Statement.get_current()
if self.statement is not None:
self.old_reinteract_output = self.statement.result_scope['reinteract_output']
if self._disable_output:
self.statement.result_scope['reinteract_output'] = lambda *args: None
def _restore_reinteract_output(self):
if self.statement is not None:
self.statement.result_scope['reinteract_output'] = self.old_reinteract_output
def _on_button_press(self, widget, event):
# Grab the focus and return true to keep the event from bubbling up
# to the TextView, which would grab focus right back.
widget.grab_focus()
return True
def _on_key_press(self, widget, event):
# Key presses are already handled in the GTK backend. This just
# keeps them from bubbling up to the TextView, which would insert
# a character.
return True
def create_widget(self):
_app.Create()
self._widget = _GlCanvas(self)
self._widget.set_size_request(*self._size)
self._widget.connect("realize", lambda widget:
widget.window.set_cursor(_gtk.gdk.Cursor(_gtk.gdk.LEFT_PTR)))
self._widget.connect("button_press_event", self._on_button_press)
self._widget.connect("key_press_event", self._on_key_press)
self._widget.disconnect_by_func(self._widget._on_scroll_event)
# We need a figure in order to get working glInfo
if visvis.misc._glInfo[0] is None:
self._widget.connect("realize", lambda widget: _getOpenGlInfo())
self.toolbar = Toolbar(self)
e = _gtk.EventBox() # For setting cursor
e.add(self.toolbar)
self.toolbar.connect("realize", lambda widget:
widget.window.set_cursor(_gtk.gdk.Cursor(_gtk.gdk.LEFT_PTR)))
box = _gtk.VBox()
box.pack_start(self._widget, True, True)
box.pack_start(e, False, False)
box.show_all()
return box
def print_result(self, context, render):
cr = context.get_cairo_context()
height = self._GetPosition()[-1]
if render:
sf = 2 # Scale factor for rendering. (Note that screenshot
# doesn't actually do supersampling yet.)
# PIL (used by screenshot) doesn't like pipes.
fd, fn = _tempfile.mkstemp()
_os.close(fd)
visvis.screenshot(fn, self, sf=sf, format="png")
image = _cairo.ImageSurface.create_from_png(fn)
_os.unlink(fn)
cr.scale(1./sf, 1./sf)
cr.set_source_surface(image, 0, 0)
cr.paint()
return height
# Make sure nothing calls getOpenGlInfo at a bad time. Disable the old
# name and move the function to a new name, which we call at an appropriate
# place.
_getOpenGlInfo = visvis.misc.getOpenGlInfo
visvis.misc.getOpenGlInfo = lambda: tuple(visvis.misc._glInfo)
# Import visvis.functions to current namespace, for convenience
from visvis.functions import *
# Modify some of visvis's functions
_solo_funcs = ('bar3', 'grid', 'hist', 'imshow', 'movieShow', 'plot',
'polarplot', 'surf', 'solidBox', 'solidCone',
'solidCylinder', 'solidLine', 'solidRing', 'solidSphere',
'solidTeapot', 'volshow')
_disable_funcs = ('close', 'closeAll', 'figure', 'ginput',
'processEvents', 'use')
_figure_doc = visvis.figure.__doc__
# Disable potentially harmful functions in visvis and current namespace.
# Don't worry about visvis.functions, though.
def _do_nothing(*args, **kw):
"""This function has been disabled by revis."""
return None
for _cmd in _disable_funcs:
setattr(visvis, _cmd, _do_nothing)
exec('%s = _do_nothing'%_cmd)
# Change these functions, and push the changes back into visvis
def gcf():
return SuperFigure.current_fig
gcf.__doc__ = visvis.gcf.__doc__
visvis.gcf = gcf
visvis.functions.gcf = gcf
def figure(*args, **kw):
if args and isinstance(args[0], SuperFigure):
return args[0]
return SuperFigure(*args, **kw)
figure.__doc__ = _figure_doc
# Leave visvis.function nilpotent
def getframe(ob):
fig = ob.GetFigure() # if ob is a figure, returns self.
if fig._widget is not None:
return _run_in_main_loop(visvis.functions.getframe, ob)
else:
raise RuntimeError, "Can't use getframe until the figure's widget has been created.\n" + \
"This error may have been triggered by a call of screenshot or record."
getframe.__doc__ = visvis.getframe.__doc__
visvis.getframe = getframe
def draw(figure=None, fast=False):
# Replace figure.Draw() with figure.DrawNow(), as use here is to
# update figure before screenshot and continuing on.
if figure is None:
figure = gcf()
if figure is not None:
figure.DrawNow(fast)
draw.__doc__ = visvis.draw.__doc__
visvis.draw = draw
# Allow 'solo' functions to be called outside of a with block
def _make_func(name):
try:
vfunc = getattr(visvis.functions, name)
except AttributeError:
return None
def func(*args, **kw):
SuperFigure.lock.acquire()
try:
if gcf() is None:
with figure() as f:
vfunc(*args, **kw)
return f
else:
return vfunc(*args, **kw)
finally:
SuperFigure.lock.release()
func.__doc__ = vfunc.__doc__
return func
for _cmd in _solo_funcs:
_func = _make_func(_cmd)
if _func is not None:
exec("%s = _func"%_cmd)