Added 'xy polygon plot' as loop / plot sample.
[l3full.git] / l3gui / experimental.py
blobd9c51801499ce184b9eab4d14cd13b42e43dd03c
2 # Author: Michael H. Hohn (mhhohn@lbl.gov)
4 # Copyright (c) 2006, The Regents of the University of California
5 #
6 # See legal.txt and license.txt
9 import l3gui.main
11 #* Experimenting:
12 from l3lang.ast import *
13 from l3lang.interp import *
14 from l3lang.repl import *
15 from l3gui.main import *
16 from l3gui.cruft.pylab import *
17 from l3gui.widgets import *
18 from l3gui.l3canvas import *
19 from l3gui.misc import *
20 from l3gui.misc import _get_props, _set_props, _safed
21 from l3lang.globals import *
23 from pickle import dumps
24 import pdb, os
25 from pdb import pm
26 from pprint import pprint
28 import sys
29 import pdb
30 def enter_debug(type, value, traceback):
31 pdb.post_mortem(traceback)
33 if 0:
34 sys.excepthook = enter_debug
36 # reset via
37 if 0:
38 sys.excepthook = sys.__excepthook__
40 def inline(st):
41 exec(st, globals())
43 # NO: gtk.threads_init()
44 gobject.threads_init()
45 gtk.main()
48 import profile, pstats
49 prof = profile.Profile()
50 prof.run('gtk.main()')
52 ps = pstats.Stats(prof)
53 ps.strip_dirs()
54 ps.sort_stats('cumulative').print_stats(40)
55 ps.sort_stats('time', 'stdname').print_stats(40)
57 # Function ... was called by
58 ps.print_callers()
60 # Function ... called
61 ps.print_callees()
63 #* Misc. tests:
64 fail_here
66 in running gnome-terminal inside the phenix setup:
68 (gnome-terminal:979): Pango-WARNING **: No builtin or dynamically
69 loaded modules were found. Pango will not work correctly. This
70 probably means there was an error in the creation of:
71 '/net/ribbon/scratch1/phenix/phenix-1.22a/build/intel-linux-2.4/python/etc/pango/pango.modules'
72 You may be able to recreate this file by running
73 pango-querymodules.
75 Trying this gives
77 # Pango Modules file
78 # Automatically generated file, do not edit
80 # ModulesPath = /net/ribbon/scratch1/phenix/phenix-1.22a/build/intel-linux-2.4/python/lib/pango/1.4.0/modules
83 which is a nonexistent file..
86 #* state handling
87 #** save
88 foo = save_state(w_, '/tmp/foo.state')
91 #** view
92 # VERY useful for debugging: tree structure, plain text!
93 pprint(utils.as_list(get_current_state(w_)))
96 #** load
97 bar = load_state(w_, '/tmp/foo.state')
98 gtk.main()
100 pprint(utils.file_unpickle('/tmp/bar.1'))
104 #* Iterators
105 #** find all instances of l3If
106 lc = w_.lib_canvas.view
107 print lc._nodes
108 print filter(lambda x: isinstance(x, l3If), lc._nodes)
110 t_if = filter(lambda x: isinstance(x, l3If), lc._nodes)[0]
111 print t_if._d_cond_marker.get_property("points")
113 t_set = filter(lambda x: isinstance(x, l3Set), lc._nodes)
114 for set in t_set:
115 print set._d_lhs_marker.get_property("points")
121 #* Persistence
122 utils.file_pickle(tree)
124 #** iterators
125 itr = iter([1,2,3])
126 for ii in itr:
127 print ii
129 itr = iter([1,2,3])
130 fn = utils.file_pickle(itr)
133 #* tree focus
134 w_.node_tree.view.widget.expand_to_path( w_.node_tree.view.start ) # yes
135 w_.node_tree.view.widget.row_activated(w_.node_tree.view.start, label_col )
136 # the cursor must be released somehow...
137 w_.node_tree.view.widget.set_cursor( w_.node_tree.view.start ) # no
138 w_.node_tree.view.widget.scroll_to_cell( w_.node_tree.view.start ) # no
142 #* canvas-embedded text, running program
143 raw = list(w_.canvas.view._nodes)[1]
145 print raw._textview.get_events()
147 # Inert widget.
148 print raw._textview.get_property("sensitive")
149 raw._textview.set_property("sensitive", False)
152 print raw._ltext.get_bounds()
153 buff = raw._textview.get_buffer()
154 print buff.get_text(*buff.get_bounds())
155 raw._loutline.hide()
160 #* canvas-embedded text widget
161 #** setup
162 view = w_.canvas.view
163 ra = w_.canvas.view.root().add
165 c_root = ra(canvas.CanvasGroup)
166 c_text = gtk.TextView()
167 c_text.get_buffer().set_text("short text")
169 if 0:
170 c_label = ra(canvas.CanvasWidget,
171 anchor = gtk.ANCHOR_NORTH_WEST,
172 # size_pixels = False,
173 # height = 2,
174 # width = 6,
176 size_pixels = True,
177 height = 20,
178 width = 200,
180 widget = c_text,
181 x = 15,
182 y = 15,
184 else:
185 c_label = c_root.add(canvas.CanvasWidget,
186 anchor = gtk.ANCHOR_NORTH_WEST,
187 # size_pixels = False,
188 # height = 2,
189 # width = 6,
191 size_pixels = True,
192 height = 20,
193 width = 200,
195 widget = c_text,
196 x = 15,
197 y = 15,
200 c_label.show()
201 c_text.show()
202 if 0:
203 c_root.hide()
204 c_label.hide()
205 c_text.hide()
206 print c_label.get_bounds()
208 #** tags
209 #*** setup
210 buff = c_text.get_buffer()
211 print [ii.get_offset() for ii in buff.get_bounds()]
213 tag_call = buff.create_tag()
214 tag_size = buff.create_tag( family = "Monospace")
216 buff.apply_tag(tag_size, *buff.get_bounds() )
217 buff.apply_tag(tag_call, *buff.get_bounds() )
219 print tag_call.get_priority()
220 print tag_size.get_priority()
223 #*** events
224 def tag_event(texttag, widget, event, iter):
225 print event.type
226 if event.type == gtk.gdk.BUTTON_PRESS:
227 if event.button == 1:
228 print "at: ", iter.get_offset()
229 texttag.set_property("weight", pango.WEIGHT_HEAVY)
231 elif event.type == gtk.gdk.BUTTON_RELEASE:
232 if event.button == 1:
233 texttag.set_property("weight", pango.WEIGHT_NORMAL)
235 return False
237 tag_call.connect("event", lambda *a: tag_event(*a))
240 #*** dynamic property adjust
241 print tag_size.get_property("size-points")
242 tag_size.set_property("size-points", 12)
245 #** buffer events
246 def re_tag_inserted_text(buffer, iter, text, length):
247 iter_to = iter.copy()
248 if not iter.backward_chars(length):
249 return False
251 if iter.has_tag(tag_size):
252 print "no tagging needed"
253 return False
255 buff.apply_tag(tag_size, iter, iter_to )
256 return False
258 buff.connect_after("insert-text", lambda *a: re_tag_inserted_text(*a))
262 #** pango font adjust -- whole widget
263 # font_desc = c_text.get_pango_context().get_font_description()
264 # print font_desc.get_size() # 10240 -- 10 pt font
265 # font_desc.set_size(fs_default / 10)
267 #* canvas work
269 #** canvas content
270 nds = w_.canvas.view._nodes
272 pprint(nds)
273 gtk.main()
277 #** canvas text
278 view = w_.canvas.view
279 ra = w_.canvas.view.root().add
280 c_label = ra(canvas.CanvasText,
281 x = 12, y = 12,
282 size_points = 12 * 1,
283 scale = 1.0,
284 scale_set = True,
285 text = "the test",
287 # c_label.set_property("scale", 0.5)
288 print c_label.get_property("size-set")
289 flush_events()
291 #*** scale test
292 # The item only moves; no font size change.
293 def affine_scale(sx, sy):
294 return (sx, 0.0, 0.0, sy, 0.0, 0.0)
296 c_label.affine_relative(affine_scale(0.2, 0.2))
297 flush_events()
300 #** canvas text
301 #*** init
302 view = w_.canvas.view
303 ra = w_.canvas.view.root().add
304 c_label = ra(canvas.CanvasText,
305 x = 12, y = 15,
306 size = 10 * pango.SCALE,
307 scale = 1.0,
308 scale_set = True,
309 size_set = True,
310 text = "the test",
313 #*** Resize test.
314 c_label.set_property("size", 15 * pango.SCALE) # Resize. Good.
315 flush_events()
317 # c_label.affine_relative( (0.5, 0.0, 0.0, 0.5, 0.0, 0.0) ) # Only moves...
318 # flush_events()
321 #*** Moving item test.
322 # Drag region is the bounding box. Easy to select.
323 class foo:
324 pass
326 self = foo()
328 def drag_event(item, event):
329 if event.type == gtk.gdk.BUTTON_PRESS:
330 if event.button == 1:
331 # sm: move group start
332 self.remember_x = event.x
333 self.remember_y = event.y
334 return True
336 elif event.type == gtk.gdk.MOTION_NOTIFY:
337 if event.state & gtk.gdk.BUTTON1_MASK:
339 # Get the new position and move by the difference.
341 # sm: move group
342 new_x = event.x
343 new_y = event.y
344 dx = new_x - self.remember_x
345 dy = new_y - self.remember_y
346 item.move(dx, dy)
347 self.remember_x = new_x
348 self.remember_y = new_y
349 return True
351 return False
352 print c_label.connect("event", drag_event)
355 #*** Font change test.
356 c_label.set_property("font", "monospace") # Font style name
357 c_label.set_property("size", 12 * pango.SCALE) #
358 flush_events()
361 #*** Color change test.
362 c_label.set_property("fill-color", "red")
363 flush_events()
367 #** CanvasText speed test
368 #*** create
369 view = w_.canvas.view
370 rg = w_.canvas.view.root().add(canvas.CanvasGroup)
371 ra = rg.add
372 def add(ii):
373 c_label = ra(canvas.CanvasText,
374 x = 2 + (ii % 10) * 7,
375 y = 5 + ii / 10,
376 size = 10 * pango.SCALE,
377 scale_set = True,
378 scale = 1.8, # ignored...
379 font = "monospace",
380 size_set = True,
381 text = "txt%d" % ii,
383 return c_label
384 text_l = [add(ii) for ii in xrange(0,1000)]
385 flush_events()
388 # Zoom does change position, but not size.
391 #*** change font and size
392 # Slow!:
393 for ii in text_l:
394 ii.set_property("font", "monospace")
395 ii.set_property("size", 12 * pango.SCALE)
396 flush_events()
398 # Fast:
399 for ii in text_l:
400 ii.set_property("font", "monospace")
401 ii.set_property("size", 8 * pango.SCALE)
402 flush_events()
404 # test
405 # "scale" is ignored (maybe try cairo backend?)
406 for ii in text_l:
407 ii.set_property("scale", 1.8)
408 ii.set_property("scale_set", True)
409 flush_events()
412 #*** remove
413 # Fast:
414 rg.destroy()
415 flush_events()
417 #** canvas text tags
418 view = w_.canvas.view
420 raw = l3Rawtext(view.w_, view)
421 ## raw.destroy()
423 buff = raw._ltext.get_buffer()
424 tag_call = buff.create_tag()
425 print tag_call.get_priority()
426 tag_call.set_priority(0)
428 def tag_event(texttag, widget, event, iter):
429 print event.type
430 # useless: iter.get_offset() is always 0 for mouse buttons
431 # never signalled: gtk.gdk.ENTER_NOTIFY, LEAVE_NOTIFY
432 if event.type == gtk.gdk.BUTTON_PRESS:
433 if event.button == 1:
434 print "at: ", iter.get_offset()
435 texttag.set_property("weight", pango.WEIGHT_HEAVY)
437 elif event.type == gtk.gdk.BUTTON_RELEASE:
438 if event.button == 1:
439 texttag.set_property("weight", pango.WEIGHT_NORMAL)
441 return False
443 print tag_call.get_property("size-set")
444 tag_call.set_property("size", view._pixpu_base * 10)
445 # tag_call.set_property("size-set", False)
447 tag_call.connect("event", lambda *a: tag_event(*a))
449 # To avoid:
450 # Text reverts to old size after apply_tag...
451 # buff.apply_tag(raw._tag_size, *buff.get_bounds() )
452 # The sequence create, edit, esc, apply_tag works...
454 raw._focus_dummy.grab_focus()
456 # raw._text_orig = buff.get_text(*buff.get_bounds())
457 buff.set_text( raw._text_orig )
459 buff.apply_tag(tag_call, *buff.get_bounds() )
460 # beg, _ = buff.get_bounds()
461 # end = beg.copy()
462 # end.forward_chars(4)
463 # beg.forward_chars(1)
464 # buff.apply_tag(tag_call, beg, end )
466 buff.apply_tag(raw._tag_size, *buff.get_bounds() )
468 # buff.get_tag_table().foreach(lambda tag, _: pprint(tag))
471 #** text handling, raw
476 #** pango font adjustments
477 # Using l3Rawtext
478 view = w_.canvas.view
479 ra = w_.canvas.view.root().add
481 font_desc = view.get_pango_context().get_font_description()
482 font_desc.set_stretch(pango.STRETCH_NORMAL)
483 fs_default = font_desc.get_size() # 10240 -- 10 pt font
484 font_desc.set_size(fs_default / 10)
486 print font_desc.get_size()
488 # Any effect?
489 c_label = ra(canvas.CanvasRichText,
490 x = 10,
491 y = 10,
492 width = 10,
493 height = 5,
494 text = "NEW SAMPLE TEXT",
496 c_label.show()
497 ## c_label.hide()
498 c_label.get_buffer()
502 #** edges and nodes
503 w_.canvas.view
504 pprint(w_.canvas.view._nodes)
505 pprint(w_.canvas.view._edges)
507 pprint(w_.canvas.view._pgraph._nodes)
508 pprint(w_.canvas.view._pgraph._edges)
510 (w_.canvas.view._edges[0]).lower_to_bottom()
513 w_.canvas.view._nodes[(2, 'root')].get_bounds()
516 #** canvas anti-aliasing
517 view = w_.canvas.view
518 view.get_property('aa')
519 view.set_property('aa', True) # only at init time.
521 # canvas color
522 view = canvas.Canvas(aa = True)
523 view.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse("grey5") )
526 #** canvas item zoom, affine
527 view = w_.canvas.view
528 rt = list(view._nodes)[0]._ltext
532 #** canvas zoom
533 view = w_.canvas.view
535 # n.a.
536 # view.set_center_scroll_region(True)
537 view.set_pixels_per_unit(view._pixpu * 1.6)
538 gtk.main()
540 # pixel units:
541 print view.get_scroll_offsets() # pixels
542 print view.get_scroll_region() # world
543 view.scroll_to
544 view.set_scroll_region
546 # Get widget size
547 wc = view.get_allocation().width # pixels
548 hc = view.get_allocation().height
550 # Get the display size.
551 cxw, cyw = view.c2w(wc/2, hc/2)
553 view.world_to_window(0, 0)
554 view.window_to_world(400, 400)
556 # world units
557 view.get_size()
558 view.get_size_request()
560 view.parent.children()
561 view.parent.get_child_visible
563 # No good:
564 # >>> view.parent
565 # <gtk.HBox object (GtkHBox) at 0x40d6e8c4>
566 # >>> view.parent.get_parent_window().get_size()
567 # (774, 785)
572 # Don't:
573 # c_label.destroy()
575 # GnomeCanvas-ERROR **: file gnome-canvas-rich-text.c: line 1596
576 # (blink_cb): assertion failed: (text->_priv->layout)
578 # Huge font...
579 c_label = ra(canvas.CanvasRichText,
580 x = 2, y = 2,
581 width = 10 * cp_.font_size, height = 1 * cp_.font_size,
582 text = "sample text",
586 #** line drawing
587 ra = w_.canvas.view.root().add
588 edge = ra(
589 canvas.CanvasLine,
590 fill_color = 'black',
591 first_arrowhead = 0,
592 last_arrowhead = 0,
593 line_style = gtk.gdk.LINE_SOLID,
594 width_pixels = cp_.outline_width_normal,
595 points = [ 0,0, 20,20 ], # scaled units.
598 point_l = (edge).get_property('points') # by value..
601 #** motion via properties:
602 (edge).set_property('points', [ 0, 0, 20, 10])
604 (edge).set_property('points', [ 0, 0, 20, 5])
608 #* canvas rich text
609 canv = w_.canvas.view
612 #** focus dummy
613 focus_dummy = w_.canvas.view.root().add(canvas.CanvasItem)
614 focus_dummy.hide()
615 focus_dummy.grab_focus()
618 #** the group
619 label_root = w_.canvas.view.root().add(canvas.CanvasGroup)
622 #** the text
623 label_text = label_root.add(canvas.CanvasRichText,
624 x = 20,
625 y = 10,
626 width = 50,
627 height = 20,
628 grow_height = False,
629 ### grow_height = True,
630 editable = True,
631 cursor_visible = False,
632 cursor_blink = False,
636 #** the outline.
637 print label_text.get_bounds()
638 x1, y1, x2, y2 = label_text.get_bounds()
639 x1, y1 = canv.c2w(int(x1), int(y1))
640 x2, y2 = canv.c2w(int(x2), int(y2))
642 label_rect = label_root.add(canvas.CanvasRect,
643 x1 = x1, y1 = y1,
644 x2 = x2, y2 = y2,
645 fill_color = 'green',
646 outline_color = 'blue',
647 width_pixels = cp_.outline_width_normal,
650 label_rect.lower(1)
651 label_rect.show()
654 #** event handlers
655 def tag_event_1(texttag, widget, event, iter):
656 if event.type == gtk.gdk.BUTTON_PRESS:
657 if event.button == 1:
658 print "tag_event_1: ", texttag
659 return False
661 def tag_event(texttag, widget, event, iter):
662 # useless: iter.get_offset() is always 0 for mouse buttons
663 # never signalled: gtk.gdk.ENTER_NOTIFY, LEAVE_NOTIFY
664 if event.type == gtk.gdk.BUTTON_PRESS:
665 if event.button == 1:
666 print "at: ", iter.get_offset()
667 texttag.set_property("weight", pango.WEIGHT_HEAVY)
669 elif event.type == gtk.gdk.BUTTON_RELEASE:
670 if event.button == 1:
671 texttag.set_property("weight", pango.WEIGHT_NORMAL)
673 return False
675 def select_event(item, event, data = None):
676 if event.type == gtk.gdk.BUTTON_PRESS:
677 print 'button!'
678 ### item.grab_focus()
679 if event.button == 3:
680 item.hide()
681 return False
682 return False
685 #** tags
686 # Creation order has no effect on size override.
688 buff = label_text.get_buffer()
689 buff_text = "sample text"
690 buff.set_text(buff_text)
692 # Highlight.
693 gi = buff.get_iter_at_offset
694 ct = lambda color: buff.create_tag( background = color,
695 size = int(10 * canv._pixpu_base),
696 family = "Monospace",
698 tag_call = map(ct, ["blue", "grey90"])
700 ii = buff_text.find(" ", 0)
702 # Size
703 tag_size = buff.create_tag( size = int(10 * canv._pixpu_base),
704 family = "Monospace",
706 ### No text, serious delays:
707 ### tag_size = buff.create_tag( size = 10 * pango.SCALE,
708 ### family = "Monospace")
710 # Apply them.
711 # Application order has no effect on override.
712 ### buff.apply_tag(tag_size, *buff.get_bounds() )
713 ### buff.apply_tag(tag_call[0], gi(2), gi(ii))
715 ### # The overlapping region has NO tag's text size.
716 ### buff.apply_tag(tag_size, gi(0), gi(4) )
717 ### buff.apply_tag(tag_call[0], gi(2), buff.get_bounds()[1])
719 buff.apply_tag(tag_size, gi(0), gi(4) )
720 buff.apply_tag(tag_call[0], gi(4), gi(7))
721 buff.apply_tag(tag_call[1], gi(7), buff.get_bounds()[1])
724 # jj = buff_text.find(" ", ii + 1)
725 # if jj == -1:
726 # jj = len(buff_text)
728 # buff.apply_tag(tag_call[1],
729 # gi(ii), gi(jj))
731 print tag_size.get_priority()
732 print tag_call[0].get_priority()
734 # no effect on size...
735 # tag_size.set_priority(buff.get_tag_table().get_size() - 1)
737 if 0:
738 tag_size.set_property("size", canv._pixpu_base * 1.2)
741 #** Event connection.
742 tag_size.connect("event", lambda *a: tag_event(*a))
743 tag_call[0].connect("event", lambda *a: tag_event_1(*a))
744 tag_call[1].connect("event", lambda *a: tag_event_1(*a))
746 label_root.connect("event", select_event)
751 # No effect:
752 ### canv.set_pixels_per_unit(canv._pixpu)
753 # ugly hack:
754 canv._pixpu = (1.00001 * canv._pixpu)
755 canv.set_pixels_per_unit(canv._pixpu)
758 #** affine adjustment
759 # The affine transform changes the box bounds, but not the font size...
760 # label_text.affine_relative( (0.1, 0.0,
761 # 0.0, 0.1,
762 # 0.0, 0.0) )
764 # print label_text.get_bounds()
765 # print label_text.i2c_affine(tuple(range(0,6)))
766 # print label_text.i2w_affine(tuple(range(0,6)))
768 # label_text = label_root.add(canvas.CanvasRichText,
769 # x = 40, y = 40,
770 # width = 10 * cp_.font_size, height = 1 * cp_.font_size,
771 # text = "sample text",
774 #** tried and discarded
776 ### No effect (events not received?):
777 ### label_root.connect("event", select_event)
778 ### label_root.connect("event", focus_event)
781 ### massive delays when zooming:
782 ### tag_size = buff.create_tag(# size = 12 * w_.canvas.view._pixpu,
783 ### size_points = 12,
784 ### )
786 ### core dump after zoom:
787 ### tag_size = buff.create_tag(# size = 12 * w_.canvas.view._pixpu,
788 ### scale = 0.1,
789 ### )
791 gtk.main()
794 #* minimal window sample
795 def zoom_spin_button():
796 spinner_adj = gtk.Adjustment(2.500,
797 0.1, 5.0,
798 0.1,
799 1.0, 1.0)
801 spinner = gtk.SpinButton(spinner_adj, 0.001, 3)
803 # Minimal gtk window
804 l_window = gtk.Window()
805 l_window.connect("delete-event", lambda *args: l_window.destroy())
806 l_window.set_border_width(0)
807 l_window.add(spinner)
808 l_window.show_all()
810 zoom_spin_button()
811 gtk.main()
816 #* AST details
817 aa = String("""
818 first
819 second
820 """)
821 aa.source_string() # no_source...
823 aa = reader.parse("""
825 first
826 second
828 """).body()
829 print aa.source_string() # no_source...
830 print aa.l3_string()
834 #* program formation from gui; evaluation test.
835 def info(prog):
836 print
837 view.print_info(storage, prog, show_macros = True)
839 pgraph = w_.canvas.view._pgraph
840 chains, free_nodes = pgraph.identify_programs()
841 print "Free nodes found: ", free_nodes # modeline / popup?
843 print "Chains found: ",
844 pprint(chains)
846 print "============================================"
847 print "Chain links:"
848 [info(storage.load(foo)) for foo in chains[0] ]
851 programs = [pgraph.chain_to_program(ch,
852 w_.state_.def_env,
853 w_.state_.storage,
854 ) for ch in chains]
855 print "============================================"
856 print " Program:"
857 info(programs[0])
860 print "============================================"
861 print " Evaluation:"
862 value = programs[0].interpret(def_env, storage)
864 #* bpath testing
865 #** lines
866 cnvs = w_.canvas.view
867 from canvas import MOVETO_OPEN, MOVETO, LINETO, CURVETO, END
869 ls_path = canvas.path_def_new([(MOVETO, 10, 10),
870 (LINETO, 20, 30),
871 (LINETO, 30, 40),
872 (LINETO, 40, 45),
873 (END,)
875 ls_item = cnvs.root().add(canvas.CanvasBpath,
876 width_units = 0.3,
877 outline_color = "black",
878 fill_color = "green",
880 ls_item.set_bpath(ls_path)
881 #*** Display
882 flush_events()
883 gtk.main()
884 #*** Scale
885 def affine_scale(sx, sy):
886 return (sx, 0.0, 0.0, sy, 0.0, 0.0)
888 ls_item.affine_relative(affine_scale(0.7, 0.3))
889 flush_events()
891 ls_item.affine_relative(affine_scale(1/0.7, 1/0.3))
892 flush_events()
895 # cleanup
896 ls_item.destroy()
899 #** curves
900 cnvs = w_.canvas.view
901 from canvas import MOVETO_OPEN, MOVETO, LINETO, CURVETO, END
903 def test_path(path):
904 ls_path = canvas.path_def_new(path)
905 ls_item = cnvs.root().add(canvas.CanvasBpath,
906 width_units = 1.0/w_.cp_.font_size * 2,
907 outline_color = "black",
908 fill_color = "green",
909 cap_style = gtk.gdk.CAP_ROUND,
910 join_style = gtk.gdk.JOIN_ROUND,
912 ls_item.set_bpath(ls_path)
914 ll, tt, rr, bb = canvas_item_get_bounds_world(ls_item)
915 ls_item.move( 10 + w_.cp_.off_x - ll,
916 10 + w_.cp_.off_y - tt,
918 gtk.main()
919 ls_item.destroy()
923 #*** corner
925 # Rounded corner, leaving the bezier control points on the tangent lines.
927 # p1 X
931 # p2 O
933 # +---O---------X
934 # p3 p4
935 path = [(MOVETO, 0, 0), # p1
936 (CURVETO,
937 0, 8,
938 2, 10,
939 10,10,
941 # (END,) # close
943 test_path(path)
946 #*** rounded corner rectangle
947 # rectangle w/ rounded corners, leaving the bezier control points on
948 # the tangent lines.
949 # Note: using one definition, say on [0,1]x[0,1] and scaling will also
950 # scale the rounded edges, but these must have absolute size. [ie. the
951 # rounding size must be independent of the rectangle size.]
954 def path_rectangle_rounded(el, er,
955 et, eb, # edge positions
956 cv, ch, # corner point indentation
957 sv, sh, # spline control point indentation
959 assert (el < er)
960 assert (et < eb)
962 assert ((er - el) > 2*ch)
963 assert ((eb - et) > 2*cv)
965 path = [(MOVETO, el, et + cv),
967 (LINETO, el, eb - cv),
968 (CURVETO , # bottom left
969 el , eb - sv,
970 el + sh , eb,
971 el + ch , eb,
974 (LINETO, er - ch, eb), # bottom right
975 (CURVETO ,
976 er - sh , eb,
977 er , eb - sv,
978 er , eb - cv,
981 (LINETO, er, et + cv),
982 (CURVETO , # top right
983 er , et + sv ,
984 er - sh , et,
985 er - ch , et,
988 (LINETO, el + ch, et),
989 (CURVETO , # top left
990 el + sh , et,
991 el , et + sv,
992 el , et + cv,
995 return path
997 el = 0.0 + 20
998 er = 10 + 20
999 eb = 10 + 20
1000 et = 0 + 20
1002 cv = 1
1003 ch = 1
1005 sh = 0.2
1006 sv = 0.2
1008 test_path(path_rectangle_rounded(el, er, et, eb,
1009 cv, ch, sv, sh))
1011 test_path(path_rectangle_rounded(5, 10, 5, 20,
1012 1, 1, 0.3, 0.3))
1015 #** path group
1017 cnvs = w_.canvas.view
1018 from canvas import MOVETO_OPEN, MOVETO, LINETO, CURVETO, END
1020 class AttrDict:
1021 def __init__(self, dct):
1022 self.__dict__.update(dct)
1024 #*** no good
1026 # Scaling is expected to widen or heighten the object, retaining the
1027 # left upper corner. For this to happen with a simple call to
1028 # affine_relative(), the upper left of the original must be at 0,0,
1029 # and the drawing must be in the positive x and y directions.
1031 # The problem here is insufficient use of CanvasGroups.
1033 def draw_path(path, width_w, **props):
1034 # This drawing order shows nothing.
1036 # Draw the path.
1037 ls_path = canvas.path_def_new(path)
1038 ls_rg = cnvs.root().add(canvas.CanvasGroup)
1039 ls_item = ls_rg.add(canvas.CanvasBpath, **props)
1040 ls_item.set_bpath(ls_path)
1042 # Reposition.
1043 flush_events()
1044 ll, tt, rr, bb = canvas_item_get_bounds_world(ls_item)
1045 ls_item.move( -ll, -tt)
1047 # not this order.
1048 # Scale to size.
1049 flush_events()
1050 sx = sy = width_w / (rr - ll)
1051 print "scale: ", sx,sy
1052 ## ls_item.affine_relative( (sx, 0.0, 0.0, sy, 0.0, 0.0) )
1054 flush_events()
1055 ll, tt, rr, bb = canvas_item_get_bounds_world(ls_item)
1056 ls_item.move( 20 + w_.cp_.off_x - ll,
1057 10 + w_.cp_.off_y - tt,
1059 return AttrDict(locals())
1062 def draw_path(path, width_w, **props):
1063 # Draw the path.
1064 ls_path = canvas.path_def_new(path)
1065 ls_rg = cnvs.root().add(canvas.CanvasGroup)
1066 ls_item = ls_rg.add(canvas.CanvasBpath, **props)
1067 ls_item.set_bpath(ls_path)
1069 # Reposition.
1070 flush_events()
1071 ll, tt, rr, bb = canvas_item_get_bounds_world(ls_item)
1072 ls_item.move( 20 + w_.cp_.off_x - ll,
1073 10 + w_.cp_.off_y - tt,
1076 # Scale using the local coordinates (the group is unchanged).
1077 sx = sy = width_w / (rr - ll)
1078 ls_item.affine_relative( (sx, 0.0, 0.0, sy, 0.0, 0.0) )
1080 return AttrDict(locals())
1083 #*** good: proper affine nesting.
1085 # @@ name the groups.
1086 # distinct nested groups for group, center, scale and
1087 # translation.
1089 def draw_path(path, width_w, **props):
1090 # Get groups.
1091 grp_trans = cnvs.root().add(canvas.CanvasGroup)
1092 grp_scale = grp_trans.add(canvas.CanvasGroup)
1093 grp_obj = grp_scale.add(canvas.CanvasGroup)
1095 # Draw the path.
1096 ls_path = canvas.path_def_new(path)
1097 ls_item = grp_obj.add(canvas.CanvasBpath, **props)
1098 ls_item.set_bpath(ls_path)
1100 # Reposition for scaling around center.
1101 # flush_events()
1102 ll, tt, rr, bb = canvas_item_get_bounds_world(grp_obj)
1103 grp_obj.move( -(ll + rr)/2, -(tt + bb)/2 )
1105 # Scale.
1106 # flush_events()
1107 ll, tt, rr, bb = canvas_item_get_bounds_world(grp_scale)
1108 sx = sy = width_w / (rr - ll)
1109 grp_scale.affine_relative( (sx, 0.0, 0.0, sy, 0.0, 0.0) )
1111 # Translate for visibility.
1112 # flush_events()
1113 ll, tt, rr, bb = canvas_item_get_bounds_world(grp_trans)
1114 grp_trans.move( 20 + w_.cp_.off_x - ll,
1115 10 + w_.cp_.off_y - tt,
1118 return AttrDict(locals())
1122 #*** triangle right
1123 path = [
1124 (MOVETO , 0, 0 ),
1125 (LINETO , 20, 20 ),
1126 (LINETO , 0, 40 ),
1127 # (LINETO , 0, 0 ),
1128 (END,)
1131 #*** triangle down
1132 path = [
1133 (MOVETO, 0,0),
1134 (LINETO, 41,0),
1135 (LINETO, 21,20),
1136 # (LINETO, 0,0), # use end to close (avoid irregularities)
1137 (END,)
1140 #*** draw
1141 dct = draw_path(path,
1143 width_units = 1.0/w_.cp_.font_size * 2,
1144 outline_color = "black",
1145 fill_color = "black",
1146 cap_style = gtk.gdk.CAP_ROUND,
1147 join_style = gtk.gdk.JOIN_ROUND,
1150 w_.dct = dct
1151 flush_events()
1153 # Transforms.
1154 print w_.dct.ls_item.i2w_affine(tuple(range(0,6)))
1155 print w_.dct.grp_trans.i2w_affine(tuple(range(0,6)))
1157 # Group tests.
1158 w_.dct.grp_scale.affine_relative( (3, 0.0, 0.0, 3, 0.0, 0.0) ) # scale
1159 w_.dct.grp_trans.move(2,2)
1161 w_.dct.grp_trans.destroy()
1164 #* l3List test
1165 cnvs = w_.canvas.view
1166 list_ = l3List(w_, cnvs)
1168 cnvs.display_bounding_box( list_._head.get_bounds())
1169 list_.hide()
1171 # list_._root_group.connect("event", lambda *a: list_.drag_event(*a))
1172 # list_._head._root_group.connect("event", lambda *a: list_.drag_event(*a))
1173 # list_._alist._root_group.connect("event", lambda *a: list_.drag_event(*a))
1175 #* bounding box testing
1176 def bounds_world(item):
1177 x, y, u, v = item.get_bounds()
1178 return item.i2w(x,y) + item.i2w(u,v)
1180 # Outline bounds != outline corners. Why?
1181 print "bounds"
1182 print rawt._loutline.get_bounds()
1183 print [rawt._loutline.get_property(prop) for prop in ['x1', 'y1', 'x2', 'y2']]
1185 def bbox_test():
1186 self__lgroup = cnvs.root().add(canvas.CanvasGroup)
1188 self__textview = gtk.TextView()
1189 self__ltext = self__lgroup.add(canvas.CanvasWidget,
1190 size_pixels = False,
1191 height = 1.5, ### params
1192 width = 20,
1194 widget = self__textview,
1195 x = 10,
1196 y = 10,
1198 x1, y1, x2, y2 = self__ltext.get_bounds() # world units
1199 x1 -= 5 / cnvs._pixpu
1200 y1 -= 5 / cnvs._pixpu
1201 x2 += 5 / cnvs._pixpu
1202 y2 += 5 / cnvs._pixpu
1204 self__loutline = self__lgroup.add(
1205 canvas.CanvasRect,
1206 x1 = x1, y1 = y1,
1207 x2 = x2, y2 = y2,
1208 fill_color = 'white',
1209 outline_color = 'black',
1210 width_pixels = cp_.outline_width_normal,
1212 st = utils.Shared()
1213 st.__dict__.update( locals())
1214 return st
1215 st = bbox_test()
1217 print "st bounds"
1218 print st.self__loutline.get_bounds()
1219 print [st.self__loutline.get_property(prop)
1220 for prop in ['x1', 'y1', 'x2', 'y2']]
1221 # Off already.
1223 #################################
1224 def bbox_test():
1225 self__lgroup = cnvs.root().add(canvas.CanvasGroup)
1227 self__loutline = self__lgroup.add(
1228 canvas.CanvasRect,
1229 x1 = 10, y1 = 11,
1230 x2 = 20, y2 = 21,
1231 fill_color = 'white',
1232 outline_color = 'black',
1233 width_pixels = cp_.outline_width_normal,
1235 st = utils.Shared()
1236 st.__dict__.update( locals())
1237 return st
1238 st = bbox_test()
1240 print "st bounds"
1241 print st.self__loutline.get_bounds()
1242 print [st.self__loutline.get_property(prop)
1243 for prop in ['x1', 'y1', 'x2', 'y2']]
1244 # Off already.
1246 #################################
1247 def bbox_test():
1248 self__lgroup = cnvs.root().add(canvas.CanvasGroup)
1250 self__loutline = self__lgroup.add(
1251 canvas.CanvasRect,
1252 x1 = 10, y1 = 11,
1253 x2 = 20, y2 = 21,
1254 fill_color = 'white',
1255 outline_color = 'black',
1256 width_pixels = 1,
1258 return utils.Shared(**locals())
1259 st = bbox_test()
1261 print "st bounds"
1262 print st.self__loutline.get_bounds()
1263 print [st.self__loutline.get_property(prop)
1264 for prop in ['x1', 'y1', 'x2', 'y2']]
1265 # Off already.
1267 print st.self__ltext.get_bounds()
1269 #################################
1271 x, y, u, v = bounds_world(rawt._loutline)
1272 print u-x, v-y
1274 print "group bounds"
1275 print rawt._root_group.get_bounds()
1276 x, y, u, v = bounds_world(rawt._root_group)
1277 print u-x, v-y
1278 print bounds_world(rawt._root_group)
1280 print "text bounds"
1281 print rawt._ltext.get_bounds()
1282 x, y, u, v = bounds_world(rawt._ltext)
1283 print u-x, v-y
1284 print bounds_world(rawt._ltext)
1286 print "world"
1287 print rawt._loutline.i2w_affine(tuple(range(0,6)))
1288 print rawt._root_group.i2w_affine(tuple(range(0,6)))
1290 print "canvas"
1291 print rawt._loutline.i2c_affine(tuple(range(0,6)))
1292 print rawt._root_group.i2c_affine(tuple(range(0,6)))
1295 #* line testing
1296 cnvs = w_.canvas.view
1297 l_line = cnvs.root().add(canvas.CanvasLine,
1298 fill_color = "black",
1299 points = [10,1,
1300 20,2,
1301 10,3,
1302 20,5,
1304 width_units = 0.2,
1305 line_style = gtk.gdk.SOLID,
1306 cap_style = gtk.gdk.CAP_ROUND,
1307 join_style = gtk.gdk.JOIN_ROUND,
1308 smooth = False,
1309 spline_steps = 12,
1312 l_line = cnvs.root().add(canvas.CanvasLine,
1313 fill_color = "blue",
1314 points = [14,1,
1315 24,2,
1316 14,3,
1317 24,5,
1319 width_units = 0.2,
1320 line_style = gtk.gdk.SOLID,
1321 cap_style = gtk.gdk.CAP_ROUND,
1322 join_style = gtk.gdk.JOIN_ROUND,
1323 smooth = True,
1324 spline_steps = 2,
1326 print l_line.get_property('points')
1327 flush_events()
1329 #* polygon testing
1330 cnvs = w_.canvas.view
1331 l_line = cnvs.root().add(canvas.CanvasPolygon,
1332 fill_color = "black",
1333 points = [10,1,
1334 20,2,
1335 10,3,
1336 20,5,
1338 width_units = 0.2,
1339 cap_style = gtk.gdk.CAP_ROUND,
1340 join_style = gtk.gdk.JOIN_ROUND,
1343 l_line = cnvs.root().add(canvas.CanvasPolygon,
1344 fill_color = "black",
1345 points = [20, 20,
1346 40, 20,
1347 40, 40,
1348 20, 40,
1349 20, 20,
1351 width_units = 0.2,
1352 cap_style = gtk.gdk.CAP_ROUND,
1353 join_style = gtk.gdk.JOIN_ROUND,
1356 print l_line.get_bounds()
1358 print l_line.get_property('points')
1359 flush_events()
1362 #* canvas ast insertion / display
1363 tree = reader.parse("[d,e]")
1364 tree = reader.parse("function = { |x,y| x - y }")
1366 tree = reader.parse("""
1367 [ function = not_yet({ |x,y| x - y }),
1368 macro = not_yet([ |x,y| z = x - y ]),
1369 raw_expression = a - b,
1370 list = [],
1371 inline_python = '''
1372 from operator import *
1373 from l3lang.test.functions import axpy
1374 ''',
1375 program = [],
1377 """)
1378 tree.setup(empty_parent(), def_env, storage)
1379 view.print_info(storage, tree)
1380 w_.lib_canvas.view.start_add_l3tree(tree)
1381 flush_events()
1383 #* pango font work
1384 pcon = w_.lib_canvas.view.get_pango_context()
1385 pcon.list_families()
1386 pcon.list_families()[0].get_name()
1387 pcon.get_font_description()
1388 print pcon.get_font_description().get_size() / pango.SCALE
1390 # Select font.
1391 sz = 20
1392 sz_orig = 10
1393 font_desc = pcon.get_font_description().copy()
1394 font_desc.set_family("monospace")
1395 font_desc.set_size(sz_orig * pango.SCALE)
1397 # Set new canvas default.
1398 pcon.set_font_description(font_desc)
1400 # Get metrics.
1401 def dump_metrics():
1402 font = pcon.load_font(font_desc)
1403 metrics = font.get_metrics()
1404 # Font sizes are in pango_units.
1405 # 1 point == PANGO_SCALE pango_units
1406 # 1 pango_point = 1/72 inch
1407 fheight = (metrics.get_ascent() + metrics.get_descent())
1408 fwidth = metrics.get_approximate_digit_width()
1409 # print fwidth / pango.SCALE
1410 # print fheight / pango.SCALE
1411 return fwidth, fheight
1412 ## logi, ink = font.get_glyph_extents("m")
1414 def lines(st):
1415 l1 = st.count('\n')
1416 if st[-1] == '\n':
1417 # No content in last line.
1418 l1 -= 1
1419 if len(st) > 0:
1420 l1 += 1
1421 return max(0, l1)
1423 def width(st):
1424 return reduce(max, [len(line) for line in st.split('\n')], 0)
1427 #** Sizing using test string.
1428 view = w_.canvas.view
1429 ra = w_.canvas.view.root().add
1430 c_string = "01234\n" * 8
1431 c_label = ra(canvas.CanvasText,
1432 x = 12, y = 25,
1433 text = c_string,
1434 font_desc = font_desc,
1436 ## c_label.destroy()
1437 flush_events()
1440 #** Set 1 cu = 1 ex
1441 l1, t1, r1, b1 = c_label.get_bounds()
1442 print "x: cu / char", (r1 - l1) / width(c_string)
1443 print "y: cu / char", (b1 - t1) / lines(c_string)
1444 print view._pixpu_base
1445 print view._pixpu
1446 cu_p_char = (r1 - l1) / width(c_string)
1447 view._pixpu = view._pixpu * cu_p_char
1448 view.set_pixels_per_unit(view._pixpu )
1451 xoff = 30
1452 yoff = 70
1454 #*** Draw long string.
1455 lng_str = "the test string,\nusing two lines"
1456 lng_label = ra(canvas.CanvasText,
1457 x = xoff, y = yoff,
1458 anchor = gtk.ANCHOR_NORTH_WEST,
1459 text = lng_str,
1460 font_desc = font_desc,
1462 ## lng_label.destroy()
1463 # Verify canvas size ratio...
1464 l1, t1, r1, b1 = lng_label.get_bounds()
1465 print "x: cu / char", (r1 - l1) / width(lng_str)
1466 print "y: cu / char", (b1 - t1) / lines(lng_str)
1468 # Compare to font metric ratio...
1469 wid, hei = dump_metrics()
1470 print "metric h / w", 1.0*hei/wid
1473 #*** Draw substring at proper position.
1474 # sub_str = "test"
1475 # sub_start = lng_str.find(sub_str)
1476 sub_top = 1
1477 sub_lef = 6
1478 sub_str = lng_str.split('\n')[sub_top][sub_lef:]
1479 sub_label = ra(canvas.CanvasText,
1480 anchor = gtk.ANCHOR_NORTH_WEST,
1481 fill_color = "blue",
1482 x = xoff, y = yoff,
1483 text = sub_str,
1484 font_desc = font_desc,
1486 sub_label.move(sub_lef, 1.0 * sub_top / wid * hei)
1487 ## sub_label.destroy()
1488 # Verify size ratio...
1489 l1, t1, r1, b1 = sub_label.get_bounds()
1490 print "x: cu / char", (r1 - l1) / width(sub_str)
1491 print "y: cu / char", (b1 - t1) / lines(sub_str)
1493 # Compare to font metric ratio...
1494 wid, hei = dump_metrics()
1495 print "metric h / w", 1.0*hei/wid
1499 #** resizing tests
1500 #*** Set new font sizes.
1501 fsize = sz * pango.SCALE
1502 font_desc.set_size(fsize)
1503 c_label.set_property('size', fsize)
1504 lng_label.set_property('size', fsize)
1505 sub_label.set_property('size', fsize)
1506 flush_events()
1507 #*** Set 1 cu = 1 ex (horizontal)
1508 l1, t1, r1, b1 = c_label.get_bounds()
1509 print "x: cu / char", (r1 - l1) / width(c_string)
1510 print "y: cu / char", (b1 - t1) / lines(c_string)
1511 print view._pixpu
1512 cu_p_char = (r1 - l1) / width(c_string)
1513 view._pixpu = view._pixpu * cu_p_char
1514 view.set_pixels_per_unit(view._pixpu )
1515 flush_events()
1516 #*** Check vertical [cu]
1517 l1, t1, r1, b1 = c_label.get_bounds()
1518 print "x: cu / char", (r1 - l1) / width(c_string)
1519 print "y: cu / char", (b1 - t1) / lines(c_string)
1520 #*** Compare font metrics (points) at new sizes
1521 wid, hei = dump_metrics()
1522 print "metric h / w", 1.0*hei/wid
1525 #** Find matching font metric.
1526 wid, hei = dump_metrics()
1527 h_o_w = 1.0 * hei / wid
1528 print "metric h / w", h_o_w
1530 sz_bnds = range(4, 41)
1531 print "pt. height/ "
1532 print "size widtht "
1533 print "-------------------"
1534 for sz in sz_bnds:
1535 fsize = sz * pango.SCALE
1536 font_desc.set_size(fsize)
1537 wid, hei = dump_metrics()
1538 sz_hw = 1.0 * hei / wid
1539 print "%-6d %.5f" % (sz, sz_hw)
1543 #* tree & clone structure
1544 tw = TreeWork(storage)
1545 loads = storage.load
1546 printi = lambda xx: print_info(storage, xx)
1547 printai = lambda xx: print_all_info(storage, xx)
1549 # id from first Symbol('abserr') inside If
1550 mid = 30221
1551 printai(loads(mid))
1552 printai(tw.find_root(loads(mid)))
1554 gtk.main()
1555 # add header, run program, compare again
1556 printai(loads(mid))
1557 printai(tw.find_root(loads(mid)))
1559 gtk.main()
1560 # change `if abserrr...` to `if prints(abserrr...`
1561 # run again, compare
1562 # no longer connected:
1563 # printai(loads(mid))
1564 # printai(tw.find_root(loads(mid)))
1565 printai(loads(30150)) # main program
1567 # search for prints...
1568 # The subtree was replaced and is re-run, including abserr()
1571 # stats for printing all info:
1574 wrt return 1.0 / 2 * (xk + x / xk)
1575 single clone prefix dump, 20 lines
1576 infix, 1 line
1577 ratio 20
1579 full sqrt.l3:
1580 prefix dump, no clones, 135 lines
1581 prefix dump, all clones, 770 lines
1582 infix, 30 lines - 10 comment = 20 lines
1583 ratio (/ 135.0 20) 6.75
1585 def try(x, xn) body
1586 infix 4
1587 prefix 24 lines
1588 ratio 6
1591 show only clone starting points: Function, Program
1595 #** print_dot
1596 def print_dot(storage, tree):
1597 # Print every expression and its clones.
1598 # This is far too much detail; only l3 Function clones are needed.
1600 iter = tree.prefix_grpd(storage)
1601 # Outermost.
1602 nd, clone_lev, indent = iter.next()
1603 print nd.source_string()
1605 try:
1606 while 1:
1607 nd, clone_lev, indent = iter.next()
1608 if clone_lev == '(':
1609 print '(clone '
1610 nd, clone_lev, indent = iter.next()
1611 print nd._id, nd.source_string()
1612 if clone_lev == ')':
1613 print ')'
1615 except StopIteration:
1616 pass
1619 #** print_dot
1620 def print_dot(storage, tree):
1621 # Print expressions once, including clones.
1622 # For every expression, include the id and link to clone
1623 # parents/sources.
1625 # Problems:
1626 # The if expressions are missing their lead indent.
1627 # The clone sources may not appear explicitly (with id); e.g., in
1629 # 1 30920
1630 # if abserr(x, xn) > 0.0001:
1631 # return loop(x, xkp1(x, xn))
1632 # else:
1633 # return xn
1635 # 30254 -> 30920 /* clone of */
1636 # 30867 -> 30920 /* cloned by */
1638 # the Function 30254 is referenced, but only shows up inside other
1639 # text.
1642 iter = tree.prefix_grpd(storage)
1643 gaa = lambda id: storage.get_attributes(
1644 id, ["clone_of", "cloned_by", "interp_env"])
1646 try:
1647 while 1:
1648 nd, clone_lev, indent = iter.next()
1649 if isinstance(nd, Program):
1650 nid = nd._id
1651 print clone_lev, nid
1652 print nd.source_string()
1654 of, by, env = gaa(nid)
1655 if of:
1656 print "%(of)d -> %(nid)d /* clone of */" % locals()
1657 if by:
1658 print "%(by)d -> %(nid)d /* cloned by */" % locals()
1659 print
1660 except StopIteration:
1661 pass
1664 #** print_dot
1665 def print_dot(storage, tree):
1666 # Print expressions once, including clones.
1667 # For every expression, include the id and link to clone
1668 # parents/sources.
1670 # Problems:
1671 # - The if expressions are missing their lead indent.
1672 # - The clone sources may not appear explicitly (with id);
1673 # - Displaying ALL clones takes too much room;
1675 iter = tree.prefix_grpd(storage)
1676 gaa = lambda id: storage.get_attributes(
1677 id, ["clone_of", "cloned_by", "interp_env"])
1679 try:
1680 while 1:
1681 nd, clone_lev, indent = iter.next()
1682 if isinstance(nd, Program):
1683 nid = nd._id
1684 print nid, '[label="%s"]; /* clone level %d */' % \
1685 (repr(nd.source_string())[1:-1].replace(r"\n",
1686 "\l"),
1687 clone_lev)
1689 of, by, env = gaa(nid)
1690 if of:
1691 print "%(of)d -> %(nid)d [color=green]; /* clone of */" % locals()
1692 if by:
1693 print "%(by)d -> %(nid)d [color=blue]; /* cloned by */" % locals()
1694 print
1695 except StopIteration:
1696 pass
1698 #** use
1699 print """
1700 digraph foo {
1701 page = "8.5,11.0"; /* size of single physical page */
1702 size="7.5,10.0"; /* graph size */
1703 /* rotate=90; */
1704 /* ratio=fill; */
1705 /* rankdir=LR; */
1706 node [shape=plaintext];
1708 print_dot(storage, t_tree)
1709 print "}"
1711 # Program P is a clone of a Function F
1712 # cloned by a Call C
1716 sload = storage.load
1718 #* l3 paper
1719 #** program
1720 t_string = open('../l3lang/test/sqrt.l3').readlines()
1721 t_tree = reader.parse("".join(t_string))
1722 t_tree.setup(empty_parent(), def_env, storage)[0].set_outl_edges(self.w_, None)
1723 # print_info(storage, t_tree)
1724 print_all_info(storage, t_tree)
1725 w_.canvas.view.clear_state( )
1726 w_.canvas.view.start_add_l3tree(t_tree)
1727 flush_events()
1728 gtk.main()
1730 #* error tracking
1731 #** filtered
1732 # put tree on canvas.
1733 # use B-3 -> hide.
1734 # zoom.
1735 import linecache, sys
1736 def traceit(frame, event, arg):
1737 if event == "line":
1738 file = frame.f_code.co_filename
1739 # SET NAME:
1740 if "usr/tmp/python-" in file:
1741 lineno = frame.f_lineno
1742 line = linecache.getline(file, lineno)
1743 print "line %d: %s" % (lineno, line.rstrip())
1744 sys.stdout.flush()
1746 return traceit
1747 sys.settrace(None)
1748 sys.settrace(traceit)
1749 #** all
1750 import linecache, sys
1751 def traceit(frame, event, arg):
1752 if event == "line":
1753 file = frame.f_code.co_filename
1754 # SET NAME:
1755 if 1:
1756 ## if "usr/tmp/python-" in file:
1757 lineno = frame.f_lineno
1758 line = linecache.getline(file, lineno)
1759 print "%s:%d: %s" % (file, lineno, line.rstrip())
1760 sys.stdout.flush()
1762 return traceit
1763 sys.settrace(None)
1764 sys.settrace(traceit)
1767 pdb.run('w_.canvas.view.centered_zoom(0.1)')
1769 # The warning is printed through
1770 # def warn...
1771 # in
1772 # intel-linux-2.4/python/lib/python2.4/warnings.py
1773 # Maybe trap these?
1776 #* local evaluation
1777 self._canvas.display_bounding_box(canvas_item_get_bounds_world(self._blank))
1778 self._canvas.display_bounding_box(self.get_bounds_world())
1779 self._canvas.display_bounding_box(canvas_item_get_bounds_world(self._d_lhs_marker))
1780 self._canvas.display_bounding_box(canvas_item_get_bounds_world(self._root_group))
1781 self._canvas.display_bounding_box(canvas_item_get_bounds_world(self._blank))
1783 # bbox checks
1784 # self._canvas.display_bounding_box(self.get_bounds_world())
1785 self._canvas.display_bounding_box(canvas_item_get_bounds_world(self._root_group))
1787 self._canvas.display_bounding_box(self._expander.get_bounds_world())
1789 #* phenix refine display
1790 #** program
1791 t_tree = reader.parse_file(
1792 os.path.expandvars('$SPXROOT/l3lang/test/refine.l3'))
1793 t_tree.setup(empty_parent(), def_env, storage).set_outl_edges(self.w_, None)
1794 ## print_info(storage, t_tree)
1795 ## view.print_all_info(storage, t_tree)
1796 w_.canvas.view.clear_state()
1797 # SLOW! But interesting to watch.
1798 w_.canvas.view.start_add_l3tree(t_tree)
1799 flush_events()
1800 gtk.main()
1801 #** program, limited depth
1802 t_tree = reader.parse_file(
1803 os.path.expandvars('$SPXROOT/l3lang/test/refine.l3'))
1804 t_tree.setup(empty_parent(), def_env, storage).set_outl_edges(self.w_, None)
1806 # hide nodes at level N
1807 vista = w_.canvas.view._vis_tab = VisibilityTable() # Reset.
1808 N = 7
1809 vista.hide_at(t_tree, N)
1811 # Check
1812 # ld = w_.state_.storage.load
1813 # for id in vista.keys():
1814 # print ld(id)
1816 # Render
1817 w_.canvas.view.clear_state()
1818 w_.canvas.view.start_add_l3tree(t_tree)
1819 flush_events()
1820 gtk.main()
1821 #** nested Map
1822 t_tree = reader.parse("""
1823 input = {
1824 pdb = {
1825 file_name = 'None',},
1826 xray_data = {
1827 file_name = 'None',
1828 labels = 'None',},
1829 r_free_flags = {
1830 file_name = 'None',
1831 label = 'None',
1832 test_flag_value = 'None',
1833 disable_suitability_test = 'False',},
1834 experimental_phases = {
1835 file_name = 'None',
1836 labels = 'None',},
1837 monomers = {
1838 file_name = 'None',},}
1839 """)
1840 t_tree.setup(empty_parent(), def_env, storage).set_outl_edges(self.w_, None)
1841 ## print_info(storage, t_tree)
1842 ## view.print_all_info(storage, t_tree)
1843 w_.canvas.view.clear_state()
1844 w_.canvas.view.start_add_l3tree(t_tree)
1845 flush_events()
1846 gtk.main()
1847 #** various file's layout
1848 def layout_file(name, level = 7):
1850 t_tree = reader.parse_file(os.path.expandvars(name))
1851 t_tree.setup(empty_parent(), def_env, storage).set_outl_edges(self.w_, None)
1853 # hide nodes at LEVEL.
1854 vista = w_.canvas.view._vis_tab = VisibilityTable() # Reset.
1855 vista.hide_at(t_tree, level)
1857 # Render
1858 w_.canvas.view.clear_state()
1859 w_.canvas.view.start_add_l3tree(t_tree)
1860 flush_events()
1861 gtk.main()
1863 layout_file('$SPXROOT/l3lang/test/refine.l3')
1864 layout_file('$SPXROOT/l3lang/test/ctf.l3')
1865 layout_file('$SPXROOT/l3lang/test/golem_1.l3')
1867 #* legible data dumping via PyYAML (yaml)
1868 import yaml
1869 print yaml.load("""
1870 - foo
1871 - bar
1872 - baz
1873 """)
1874 print yaml.dump(['foo', 'bar', 'baz'])
1876 # full state
1877 # 1. Slow
1878 # 2. yaml.representer.RepresenterError: recursive objects are not
1879 # allowed: Env(30288, Env-30269-skel.blck, [Program at 0x40d79a8c, id
1880 # 30290])
1881 yaml.dump(get_current_state(w_), stream = sys.stdout)
1882 ## yaml.dump(py_obj, stream = None)
1884 yaml.load(ya_str)
1886 #* legible data dumping via syck (yaml)
1887 #** Trivial test.
1888 import syck
1889 print syck.load("""
1890 - foo
1891 - bar
1892 - baz
1893 """)
1894 print syck.dump(['foo', 'bar', 'baz'])
1897 #** Full state.
1899 # [Mon Apr 30 10:28:42 2007]
1900 # Fails on <weakproxy> instances
1901 # with error
1902 # AttributeError: viewList instance has no attribute '__reduce__'
1903 # and there is no customization hook.
1904 syck.dump(get_current_state(w_), sys.stdout)
1907 # [Mon Apr 30 10:35:19 2007]
1908 # Try a detour via pickle. This fails too:
1909 # (.:16125): GLib-GObject-WARNING **: cannot retrieve class for
1910 # invalid (unclassed) type `<invalid>' ** ERROR **: file pygenum.c:
1911 # line 60 (pyg_enum_repr): assertion failed:
1912 # (G_IS_ENUM_CLASS(enum_class)) aborting...
1913 # /net/hooknose/scratch1/hohn/t2/sparx-bin/bin/sparx.wrap: line 36:
1914 # 16125 Aborted python -i
1915 # Process Python exited abnormally with code 134
1917 # Works after removing some more unnecessary gtk information.
1918 import pickle, syck
1919 n_tree = pickle.loads( pickle.dumps(get_current_state(w_), protocol=2))
1920 syck.dump(n_tree, sys.stdout)
1923 # l3 graphical node (local eval)
1924 import syck
1925 print syck.dump(self)
1927 # l3 tree (local eval)
1928 print syck.dump(self._l3tree)
1931 #** misc
1932 print syck.load("""
1933 connections:
1934 horizontal layout
1935 -requires
1936 -display choice
1938 connections:
1939 [horizontal layout, requires, >
1940 display choice display choice display choice
1941 display choice display choice display choice
1944 connections:
1945 horizontal layout:
1946 requires: display choice (direction)
1947 afd: display
1950 2d layout -- requires -- comment parsing
1952 """)
1954 #** Compare state files via their yaml representations.
1955 import pickle, syck
1956 from l3lang.utils import *
1958 n_tree_1 = file_unpickle('st.1')
1959 syck.dump(n_tree_1, open('st.1.syck', 'w'))
1961 n_tree_2 = file_unpickle('st.2')
1962 syck.dump(n_tree_2, open('st.2.syck', 'w'))
1967 #* local evaluation
1968 #** code & clones
1969 ==== local console; exit with end-of-file (ctrl-d) ====
1971 import l3lang.view as view
1972 view.print_all_info(self.w_.state_.storage, self._l3tree)
1974 #** code & values
1976 #* canvasgroup
1977 lcan = w_.lib_canvas.view
1978 pprint(lcan.root().item_list)
1979 # local eval
1980 pprint(self._root_group.item_list)
1982 #*visibility tables testing.
1983 lcan = w_.lib_canvas.view
1984 (lcan._vis_tab)
1985 pprint(lcan._vis_tab)
1987 # Assuming constant tree and node offset:
1988 manually_chosen = {30098: 'vis_hide',
1989 30136: 'vis_hide',
1990 30143: 'vis_hide',
1991 30148: 'vis_hide',
1992 30158: 'vis_hide',
1993 30166: 'vis_hide'}
1995 lcan._vis_tab.replace_table( manually_chosen)
1996 # use "apply mark" on topmost element.
1998 #* state files
1999 /tmp/st.lib - compacted library only.
2000 /tmp/st.for-pre-run - loop sample
2003 #* l3 loop templates
2004 #** range iteration, function version.
2005 # for i in range(n,m):
2006 # body
2007 def _for(_n, _m, body):
2008 if (_n < _m):
2009 body(_n)
2010 return _for(_n + 1, _m, body)
2011 else:
2012 return 0
2014 _for(1, 7, {|i| print_(i) })
2015 _for(30, 31, {|i| foo })
2016 # _for(1, 5, lambda i: print_(i) )
2017 # Nesting is no problem:
2018 _for(1, 4, {|i|
2019 _for(-5, -1,
2020 {|j| print_("%d %d" % (i,j)) })})
2021 #** range, macro version
2022 # [Thu Feb 9 16:29:39 2006]
2023 # Better not to use this one...
2025 # for i in range(n,m):
2026 # body
2027 def _form(_n, _m):
2028 if (_n < _m):
2029 i = _n
2030 body
2031 return _form(_n + 1, _m)
2032 else:
2033 return 0
2035 _form(1, 5)
2037 # Paste, insert body, change "i" to new name, run.
2038 # Nesting requires unique _for names; the following has a problem::
2039 def _for(_n, _m):
2040 if (_n < _m):
2041 ii = _n
2042 print_(ii)
2043 ########
2044 def _for(_n, _m):
2045 if (_n < _m):
2046 jj = _n
2047 ########
2048 print_("%d %d" % (ii, jj))
2049 ########
2050 return _for(_n + 1, _m)
2051 else:
2052 return 0
2053 _for(1, 5)
2054 ########
2055 return _for(_n + 1, _m) # wrong _for
2056 else:
2057 return 0
2058 _for(1, 5)
2061 #* context for values
2063 # Given multiple values from a single expression and its clones, find
2064 # the dynamic context -- the changed arguments to
2065 # enclosing Functions, or (sigh) the changed values of the stacked
2066 # bindings. Complicated.
2068 import l3lang.view
2069 st = w_.state_.storage
2070 load = w_.state_.storage.load
2071 tw = ast.TreeWork(w_.state_.storage)
2075 #* value extraction
2077 # LOADING RESETS THE STORAGE TABLE AND OTHERS!
2079 # U s e t h e w _ a c c e s s p a t h !
2081 import l3lang.view
2082 st = w_.state_.storage
2083 load = w_.state_.storage.load
2084 tw = ast.TreeWork(w_.state_.storage)
2086 # Toplevel id (static).
2087 l3nd = w_.selector.get_selection()
2088 c_id = l3nd._l3tree._id
2089 print load(c_id)
2090 print load(c_id).source_string()
2092 # Dynamic id(s).
2093 clone_l = st.get_attribute(c_id, "interp_clone")
2094 print len(clone_l)
2096 # If no values: is the correct storage instance used?
2097 for id in clone_l:
2098 print id, st.get_attribute(id, 'interp_result')
2100 # For deeper nesting, follow the clone trail.
2101 st.get_attribute(id, "interp_clone")
2103 # Get the context
2104 cctxt = tw.get_call_ctxt(c_id)
2105 # Raw.
2106 pprint( cctxt)
2107 # With text.
2108 tw.print_call_ctxt(cctxt)
2110 # If clones were cloned, these may be non-empty?
2111 # for id in clone_l:
2112 # print tw.get_call_ctxt(id)
2114 # symbol('i') evaluated, has time stamp, but no value, no env? --
2115 # Use correct storage!
2116 id = clone_l[1]
2117 print id, st.get_attribute(id, 'interp_result')
2118 print id, st.get_attribute(id, 'interp_env')
2119 pprint( [id, st.get_attribute_table(id)])
2121 # enclosing program
2122 parent = tw.find_first_parent(load(id), (ast.Function, ast.Program))
2123 view.print_info(st, parent)
2124 print parent._eval_env.all_bindings() # empty
2125 print parent._eval_env.lookup('i') # value, at last...
2126 global L3_TRACE
2127 L3_TRACE = 1
2128 print parent._eval_env.ie_lookup('i')
2131 #* environment viewing
2132 # local eval for program
2133 (self._l3tree)._block_env.all_bindings()
2134 (self._l3tree)._eval_env.all_bindings()
2135 (self._l3tree)._eval_env.ls()
2136 # has print_'s time stamp, but no print.
2137 # [after loading /tmp/st.post-loop ]
2139 #* state examination
2140 # Failed to complete:
2141 print syck.dump(get_current_state(w_))
2143 import pickle, pickletools
2144 st = get_current_state(w_)
2145 pickletools.dis(pickle.dumps(st, protocol=2))
2148 # local eval
2149 import pickle
2151 print (self._l3tree)._eval_env.lookup('print_')
2152 # <function print_ at 0x40402ed4>
2154 n_tree = pickle.loads( pickle.dumps(self._l3tree, protocol=2))
2155 print (n_tree)._eval_env.lookup('print_')
2157 #* voea etc. sample problem
2158 #** python support functions
2159 from l3lang.external.golem import \
2160 create_write_gridprojections, \
2161 anglist2doc, \
2162 spidbprg, \
2163 Golem
2166 #** voea
2168 # caption = 'Delta theta (degrees)',
2169 if 1:
2170 deltheta = 10
2171 anglist = Golem.voea(deltheta)
2172 else:
2175 #** gridproj
2177 if 1:
2178 # caption = 'Filename of volume to project',
2179 volfile = "volext.spi"
2181 # caption = 'Kaiser-Bessel K parameter',
2182 K = 6
2184 # caption = 'Kaiser-Bessel alpha parameter',
2185 alpha = 1.75
2187 # caption = 'Projection file pattern',
2188 filepattern = 'proj{****}.tst'
2190 # print vol.get_xsize()
2191 create_write_gridprojections(
2192 Golem.getImage(volfile),
2193 anglist, K, alpha, filepattern)
2194 else:
2197 #** anglist2doc
2198 # caption = 'Spider doc filename',
2199 if 1:
2200 docfname = 'foo-doc.tst'
2201 anglist2doc(anglist, docfname)
2202 else:
2205 #** spidbprg
2207 if 1:
2208 # caption = 'Projection file pattern',
2209 filepattern = 'proj{****}.tst' # above!
2211 # caption = 'Projection start index',
2212 minindx = 1
2214 # caption = 'Projection stop index',
2215 maxindx = 10
2217 # caption = 'Name of angle Spider doc file',
2218 angdocfile = docfname # above!
2220 # caption = 'Name of reconstructed volume',
2221 newvolname = 'newvol.spi'
2222 spidbprg(filepattern,
2223 minindx,
2224 maxindx,
2225 angdocfile,
2226 newvolname,
2228 else:
2231 # comparison in v4 shows newvolname to be poor...
2233 #* voea/bp as single program
2234 #** program
2235 tree = reader.parse("""
2236 inline '''
2237 from l3lang.external.golem import \
2238 create_write_gridprojections, \
2239 anglist2doc, \
2240 spidbprg, \
2241 Golem
2244 def _for(_n, _m, body):
2245 if (_n < _m):
2246 body(_n)
2247 return _for(_n + 1, _m, body)
2248 else:
2249 return 0
2251 def body(deltheta):
2252 if 1:
2253 anglist = Golem.voea(deltheta)
2254 else:
2257 # gridproj
2259 if 1:
2260 # caption = 'Filename of volume to project',
2261 volfile = "volext.spi"
2263 # caption = 'Kaiser-Bessel K parameter',
2264 K = 6
2266 # caption = 'Kaiser-Bessel alpha parameter',
2267 alpha = 1.75
2269 # caption = 'Projection file pattern',
2270 filepattern = 'proj{****}.tst'
2272 # print vol.get_xsize()
2273 create_write_gridprojections(
2274 Golem.getImage(volfile),
2275 anglist, K, alpha, filepattern)
2276 else:
2279 # anglist2doc
2280 # caption = 'Spider doc filename',
2281 if 1:
2282 docfname = 'foo-doc.tst'
2283 anglist2doc(anglist, docfname)
2284 else:
2287 # spidbprg
2289 if 1:
2290 # caption = 'Projection file pattern',
2291 filepattern = 'proj{****}.tst' # above!
2293 # caption = 'Projection start index',
2294 minindx = 1
2296 # caption = 'Projection stop index',
2297 maxindx = 10
2299 # caption = 'Name of angle Spider doc file',
2300 angdocfile = docfname # above!
2302 # caption = 'Name of reconstructed volume',
2303 newvolname = 'newvol.spi'
2304 spidbprg(filepattern,
2305 minindx,
2306 maxindx,
2307 angdocfile,
2308 newvolname,
2310 else:
2313 _for(40, 42, body)
2314 """)
2316 #** Directory setup.
2317 import shutil, os
2318 ev = os.path.expandvars
2319 os.chdir(ev('$SPXROOT/l3lang/test'))
2320 print os.getcwd()
2321 shutil.rmtree('bp-test')
2322 os.mkdir('bp-test')
2323 os.chdir(ev('$SPXROOT/l3lang/test/bp-test'))
2324 print os.getcwd()
2325 shutil.copyfile(ev('$SPXROOT/l3lang/test/volext.spi'), 'volext.spi')
2328 #** setup and execute
2329 if 1: # graphical
2330 tree.setup(empty_parent(), def_env, w_.state_.storage).set_outl_edges(self.w_, None)
2331 # view.print_info(storage, tree)
2332 w_.canvas.view.start_add_l3tree(tree)
2333 flush_events()
2335 if 2: # textual
2336 from l3lang import utils, reader, ast, view, interp
2337 play_env = interp.get_child_env(def_env, storage)
2338 tree.setup(empty_parent(), def_env, storage).set_outl_edges(self.w_, None)
2339 tree.interpret(def_env, storage)
2340 pprint(os.listdir(os.getcwd()))
2344 #* voea/bp using docstrings
2345 # /net/cci/hohn/w/tmp/eman2/libpyEM/Golem
2346 #** program
2347 tree = reader.parse("""
2348 inline '''
2349 from l3lang.external.golemwrap import *
2352 def _for(_n, _m, body):
2353 if (_n < _m):
2354 body(_n)
2355 return _for(_n + 1, _m, body)
2356 else:
2357 return 0
2359 def body(deltheta):
2360 anglist = voea(deltheta)
2363 # Param setting.
2365 filepattern = 'proj{****}.tst'
2368 # gridproj
2370 gridproj(getImage("volext.spi"), # nesting...
2371 anglist,
2373 1.75,
2374 {filepattern = filepattern})
2376 # Param setting.
2377 docfname = 'foo-doc.tst'
2379 # anglist2doc
2381 anglist2doc(anglist, docfname)
2384 # spidbprg
2386 spidbprg(filepattern,
2389 docfname,
2390 'newvol.spi',
2393 _for(40, 42, body)
2394 """)
2396 #** Directory setup.
2397 import shutil, os
2398 ev = os.path.expandvars
2399 os.chdir(ev('$SPXROOT/l3lang/test'))
2400 print os.getcwd()
2401 shutil.rmtree('bp-test')
2402 os.mkdir('bp-test')
2403 os.chdir(ev('$SPXROOT/l3lang/test/bp-test'))
2404 print os.getcwd()
2405 shutil.copyfile(ev('$SPXROOT/l3lang/test/volext.spi'), 'volext.spi')
2408 #** setup and execute
2409 if 1: # graphical
2410 tree.setup(empty_parent(), def_env, w_.state_.storage).set_outl_edges(self.w_, None)
2411 # view.print_info(storage, tree)
2412 w_.canvas.view.start_add_l3tree(tree)
2413 flush_events()
2415 if 2: # textual
2416 from l3lang.external import golemwrap
2417 from l3lang import utils, reader, ast, view, interp
2418 play_env = interp.get_child_env(def_env, storage)
2419 tree.setup(empty_parent(), play_env, storage)
2420 tree.interpret(play_env, storage)
2421 pprint(os.listdir(os.getcwd()))
2423 #* voea/bp w/o spider
2424 #** program
2425 tree = reader.parse("""
2426 """)
2428 #** Directory setup.
2429 import shutil, os
2430 ev = os.path.expandvars
2431 os.chdir(ev('$SPXROOT/l3lang/test'))
2432 print os.getcwd()
2433 shutil.rmtree('bp-test')
2434 os.mkdir('bp-test')
2435 os.chdir(ev('$SPXROOT/l3lang/test/bp-test'))
2436 print os.getcwd()
2437 shutil.copyfile(ev('$SPXROOT/l3lang/test/volext.spi'), 'volext.spi')
2440 #** setup and execute
2441 if 1: # graphical
2442 tree.setup(empty_parent(), def_env, w_.state_.storage)
2443 # view.print_info(storage, tree)
2444 w_.canvas.view.start_add_l3tree(tree)
2445 flush_events()
2447 if 2: # textual
2448 from l3lang import utils, reader, ast, view, interp
2449 play_env = interp.get_child_env(def_env, storage)
2450 tree.setup(empty_parent(), def_env, storage)
2451 tree.interpret(def_env, storage)
2452 pprint(os.listdir(os.getcwd()))
2455 #* beautification.
2456 def pri(tree):
2457 view.print_info(storage, tree)
2459 pri( reader.parse("aa = ff(xx)"))
2460 pri( reader.parse("aa = ff(xx, yy)"))
2461 pri( reader.parse("! aa = ! ff( !xx)"))
2463 pri( reader.parse("aa = ff(xx)"))
2464 pri( reader.parse("(aa, bb) = ff(xx)"))
2466 reader.parse("!! aa symbol")
2469 # ---------------------------------
2470 from l3lang.ast import *
2471 l3tree = reader.parse("a = f(x)")[0][0]
2472 l3tree = reader.parse("(a,b) = f(x)")[0][0]
2473 l3tree = reader.parse("(a,b) = f(x,y)")[0][0]
2474 l3tree = reader.parse("a = f(x,y)")[0][0]
2476 # ---------------------------------
2477 # Test for the special case `a = f(x)` and its
2478 # variations.
2480 # def add_l3tree_set_tuple()
2481 ma = ast.Matcher()
2482 if ma.match(l3tree,
2483 Set(Marker('aa'),
2484 Call(Marker('ff'), Marker('xx')))):
2485 # Convert to a list of args and list of return values.
2486 _function = ma['ff'] # Symbol or expr.
2487 _in = ma['xx'] # aList of args.
2488 out = ma['aa']
2489 if ma.match(out, # a,b,... =
2490 Tuple(Marker('aa'))):
2491 _out = ma['aa'] # aList
2492 elif ma.match(out, # a =
2493 MarkerTyped(String('aa'), Symbol('symbol'))):
2494 _out = aList([ma['aa']]) # aList
2495 else:
2496 no_luck
2498 # Get documentation for _in, _out, and _function.
2499 print _out, _function, _in
2501 # Get paths for editables.
2502 # Using the unique id.
2504 # Assemble table.
2507 #* ExprMarker
2508 # The initial try at an expression marker. This version is too buggy;
2509 # the new one is in widgets.py, based on a merge of l3If and
2510 # Placeholder.
2513 class daExprMarker:
2514 pass
2516 def __init__(self, w_, cnvs, l3_parent, l3_pidx, d_expr):
2518 :Arguments:
2519 - `l3_parent` The raw l3 ast parent of d_expr._l3tree.
2520 Without propagation of events from the l3 ast to its
2521 display (the current case), l3_parent should not be
2522 drawn.
2523 - `l3_pidx` The index of d_expr._l3tree in l3_parent.
2524 - `d_expr` The displayed expression to be handled here.
2526 assert isinstance(d_expr, (wdgt.l3aList, wdgt.l3Base))
2527 assert isinstance(l3_parent, (ast.astType,))
2529 self.w_ = w_
2530 self._canvas = cnvs
2532 self._l3_parent = l3_parent
2533 self._l3_pidx = l3_pidx
2534 self._d_expr = d_expr
2536 # Display.
2537 self._root_group = cnvs.root().add(canvas.CanvasGroup)
2538 self._d_parent = None
2539 self._destroy_hook = [] # (func -> None) list
2540 self._vis_indic = None # visibility status indicator
2542 # Marker(s).
2543 self._marker_points = {}
2544 self._d_marker = self.draw_marker()
2545 self._d_marker.lower_to_bottom()
2547 # Inform obj of marker.
2548 # This directs physical detachment of d_expr from daExprMarker to
2549 # daExprMarker.detach, which forwards the tree detachment.
2550 d_expr.setup_marker(self._d_marker, self)
2553 # Align with respect to d_expr.
2555 flush_events()
2556 ## _, y, x, _ = d_if.get_bounds()
2557 ## u, v, _, _ = d_cond.get_bounds()
2558 ## d_cond.move(x - u, y - v)
2561 # Event handling.
2562 self._d_marker.connect("event", lambda *a: self.insert_event(*a))
2563 # Forwarding to parent is implicit.
2564 # # self._root_group.connect("event", lambda *a:
2565 # # self.drag_event(*a))
2566 daExprMarker.__init__ = __init__
2569 #** destroy subtree and display
2570 def destroy(self):
2571 self._d_expr
2572 self._destroy_hook
2573 self._root_group.destroy()
2574 daExprMarker.destroy = destroy
2576 l3Base.add_destroy_hook = add_destroy_hook
2577 l3Base._run_destroy_hook = _run_destroy_hook
2580 #** dragging
2581 l3Nested.drag_event = drag_event
2584 def detach(self, d_expr):
2585 if d_expr != self._d_expr:
2586 raise DisplayError("Detaching invalid child.")
2588 # Reparent graphical parts.
2589 d_expr.reparent_to_root()
2591 # Update reference.
2592 self._d_expr = None
2594 # Shift body parts.
2595 pass
2597 # Refresh decoration.
2598 pass
2600 # Move following items in parent.
2601 if self._d_parent:
2602 self._d_parent.new_size_for(self)
2604 # Update tree data.
2605 if self.w_.fluid_ref(detach_l3_tree = True):
2606 # Remove the old.
2607 self._l3_parent.detach_child(self.d_expr._l3tree._id,
2608 self.w_.state_.storage)
2609 # Insert a placeholder, to keep indices valid.
2610 dummy = ast.placeholder(self._l3_parent, self.w_.state_.storage)
2611 self._l3_parent.insert_child(self._l3_pidx, dummy)
2612 daExprMarker.detach = detach
2614 def draw_marker(self):
2615 cp = self.w_.cp_
2616 points = [0 , 0,
2617 cp.exp_marker_width , 0,
2618 cp.exp_marker_width , cp.exp_marker_height,
2619 0 , cp.exp_marker_height,
2620 0 , 0,
2622 _marker = self._root_group.add(
2623 canvas.CanvasPolygon,
2624 fill_color = "black",
2625 points = points,
2626 width_units = 0.2,
2627 cap_style = gtk.gdk.CAP_ROUND,
2628 join_style = gtk.gdk.JOIN_ROUND,
2630 self._marker_points[_marker] = points
2631 return _marker
2632 daExprMarker.draw_marker = draw_marker
2634 def insert_event(self, item, event):
2635 if event.type == gtk.gdk.BUTTON_PRESS:
2636 if event.button == 2:
2637 print "insert_event"
2638 if not self.w_.selector.have_selection():
2639 ten_second_message("No node selected.")
2640 return False
2641 else:
2642 self.insert(item, self.w_.selector.get_selection())
2643 return False
2644 daExprMarker.insert_event = insert_event
2646 def insert(self, marker, newobj):
2647 assert isinstance(newobj, l3Base)
2649 if marker != self._d_marker:
2650 raise DisplayError("Insertion from wrong marker.")
2652 # Avoid special cases:
2653 if newobj.contains_recursive(self._d_expr):
2654 ten_second_message("Cannot insert object into itself.")
2655 return
2657 if newobj._canvas != self._canvas:
2658 ten_second_message("Cannot move objects across displays. "
2659 "Use copy instead.")
2660 return
2662 # Validate slot.
2663 old_entry = self._l3_parent[self._l3_pidx]
2664 slot_available(self.w_, old_entry)
2666 # Update reference.
2667 self._d_expr = d_expr = newobj
2669 # Reparent object display.
2670 newobj.reparent(self)
2672 # Position new child.
2673 flush_events()
2674 l1, t1, _, _ = self._d_marker.get_bounds()
2675 l2, t2, _, _ = self._d_expr.get_bounds()
2676 d_expr.move(l1 - l2, t1 - t2)
2678 # Reposition rest.
2680 # Refresh decoration.
2682 # Move following items in parent.
2683 if self._d_parent:
2684 self._d_parent.new_size_for(self)
2686 # Update tree data.
2687 if self.w_.fluid_ref(insert_l3_tree = True):
2688 self._l3_parent.replace_child(old_entry._id, newobj._l3tree)
2690 # Inform obj of marker.
2691 newobj.setup_marker(self._d_marker, self)
2692 pass
2693 daExprMarker.insert = insert
2696 def get_bounds(self):
2697 """ Marker interface used in Nested.drag_event.
2699 return marker_get_bounds(self.w_, self._d_marker)
2700 daExprMarker.get_bounds = get_bounds
2704 #** exprmarker test
2705 l3_parent = reader.parse("a = f(x,y)")[0][0]
2706 play_env = interp.get_child_env(def_env, storage)
2707 l3_parent.setup(empty_parent(), play_env, storage)
2709 l3_pidx = 0
2710 d_expr = w_.canvas.view.start_add_l3tree(l3_parent[l3_pidx])
2711 d_expr.move(100, 100)
2713 the_marker = daExprMarker(w_, w_.canvas.view, l3_parent, l3_pidx, d_expr)
2715 find_child_index
2717 #* stuff
2718 other = ...
2719 [ future(operation, i, other) for i in list]
2721 # operation is a pure function, no envs. Future need not provide
2722 # environment.
2724 def print_(st):
2725 print st
2727 def compute(_n, _m):
2728 if (_n < _m):
2729 print_( (_n + 1) * _m)
2730 return compute(_n + 1, _m)
2731 else:
2732 return _n
2734 compute(10,20)
2736 tree = reader.parse("""
2737 inline '''
2738 from l3lang.l3_utils import print_, pprint
2740 def compute(_n, _m):
2741 if (_n < _m):
2742 print_( (_n + 1) * _m)
2743 return compute(_n + 1, _m)
2744 else:
2745 return _n
2746 """)
2747 tree.setup(empty_parent(), def_env, w_.state_.storage)
2748 w_.canvas.view.start_add_l3tree(tree)
2750 # use functions,
2751 # use tail calls,
2752 # do not:'
2753 j = 0
2754 for i in range(0,10):
2755 j = j + i
2756 pass
2758 #* eman2 cruft
2759 from pprint import pprint
2760 from pdb import pm
2761 import atexit, pdb, sys, os
2762 from EMAN2 import EMData
2763 import EMAN2
2765 ev = os.path.expandvars
2766 print os.getcwd()
2767 os.chdir(ev('$SPXROOT/l3lang/test'))
2768 def load( file_name ):
2769 img = EMData()
2770 img.read_image( file_name )
2771 return img
2772 #** List available functionality.
2773 pprint(EMAN2.Processors.get_list())
2774 pprint(EMAN2.Projectors.get_list())
2775 pprint(EMAN2.Reconstructors.get_list())
2776 pprint(EMAN2.Cmps.get_list())
2777 #** Dump ALL get_list containing members.
2778 for nm in dir(EMAN2):
2779 try:
2780 memb = eval("EMAN2.%s.get_list" % nm)
2781 except AttributeError:
2782 continue
2783 print ("EMAN2.%s.get_list" % nm)
2784 print memb()
2785 #** simple mirror test
2786 from EMAN2 import *
2787 e = EMData()
2788 e.set_size(3,3)
2789 e.set_value_at(0,1, 1.0)
2790 print "original at start"
2791 e.print_image()
2793 # Use the processors listed by .get_list() above.
2794 f1 = e.process("mirror",{"axis":'x'})
2795 print "new mirrored image"
2796 f1.print_image()
2798 print "original after mirror (unchanged)"
2799 e.print_image()
2801 e.process_inplace("mirror",{"axis":'x'})
2802 print "original after in-place mirror"
2803 e.print_image()
2805 # Another possibility:
2806 e = EMData()
2807 e.set_size(3,3)
2808 e.set_value_at(0,0, 1.0)
2809 print "original at start"
2810 e.print_image()
2812 e = e.process("mirror",{"axis":'x'})
2813 print "new mirrored image"
2814 e.print_image()
2817 #* sample from andres' paper
2818 from EMAN2 import *
2820 #** build 3d model
2821 inline """
2822 from EMAN2 import EMNumPy
2823 def img_sphere(size, radius, axis, center, fill):
2824 from EMAN2 import EMData
2825 e = EMData()
2826 e.set_size(size, size, size)
2827 e.process_inplace('testimage.circlesphere',
2828 {'radius':radius, 'axis':axis,
2829 'c':center, 'fill':fill})
2830 return e
2834 sphere = img_sphere(32, 20, 'z', 15, 1)
2835 # Not useful.
2836 # sphere.print_image()
2838 # No slicing on emdata, use numpy array.
2839 t1 = EMNumPy.em2numpy(sphere)
2841 # View all 2d slices
2842 for ix in range(0,32):
2843 t1[ix, 0:None, 0:None]
2845 t1[0, 0]
2847 #** Try circle.
2848 inline """
2849 from EMAN2 import EMNumPy
2850 def img_circle(size, radius, center, fill):
2851 from EMAN2 import EMData
2852 e = EMData()
2853 e.set_size(size, size)
2854 e.process_inplace('testimage.circlesphere',
2855 {'radius':radius, 'c':center, 'fill':fill})
2856 return e
2858 circle = img_circle(32, 20, 15, 1)
2859 t1 = EMNumPy.em2numpy(circle)
2863 #** dither / diffuse
2864 #** form projections
2865 #** apply CTF
2866 #** add noise
2867 #** calculate SSNR
2868 #** and much more...
2869 # See "http://www.sciencedirect.com/science?_ob=ArticleURL&_udi=B6WM5-4HVMFBS-1&_user=635986&_coverDate=03%2F31%2F2006&_fmt=full&_orig=search&_cdi=6925&view=c&_acct=C000059612&_version=1&_urlVersion=0&_userid=635986&md5=0e4b1d3ea658eb6330884fef78c4b035&ref=full#SECX2"
2870 # 2D alignment and classification. Particles were normalized, low- and
2871 #high-pass filtered, soft-masked, and then centered. The low- and
2872 #high-pass filters were centered at 1/5 pixels1
2875 #* large list display
2876 xx = range(0,20000)
2877 # Insert as list list returns but keeps using 100% cpu
2879 xx = range(0,2000)
2880 # insert as list:
2881 # RuntimeError: maximum recursion depth exceeded in cmp
2883 # The number of bounds calculations (marker_get_bounds() and others)
2884 # is ~ N, and bounds calculations are VERY slow...
2886 #* Starting volume
2887 volume = load( "model.tcp" )
2889 proj = volume.project("pawel",
2890 {"angletype" : "SPIDER",
2891 "anglelist" : [10, 20, 30], # phi, theta, psi
2892 "radius" : 35 })
2896 #* visual program representation
2897 #** calc
2898 # Very convoluted example, heavily using higher-order functions.
2899 # Ugly in graphical view.
2900 t_tree = reader.parse_file(
2901 os.path.expandvars('$SPXROOT/l3lang/test/calc.l3'))
2902 t_tree.setup(empty_parent(), def_env, w_.state_.storage)
2903 w_.canvas.view.clear_state()
2904 w_.canvas.view.start_add_l3tree(t_tree)
2905 flush_events()
2908 #** ctf
2909 #*** full program
2910 # Very clean top-to-bottom script. Graphical view is still tedious.
2911 # Good reference for prototyping 'nice' graphical view w/o loops.
2912 tm_tree = reader.parse_file(
2913 os.path.expandvars('$SPXROOT/l3lang/test/ctf.l3'))
2914 tm_tree.setup(empty_parent(), def_env, w_.state_.storage)
2915 w_.canvas.view.clear_state()
2916 tm_tree_l3pic = w_.canvas.view.start_add_l3tree(tm_tree)
2917 flush_events()
2920 #*** concise view, program level, static
2921 # manually written
2922 # no program editing (static)
2923 t0_tree = reader.parse("""
2924 "project from volume"
2925 "convolve image and ctf"
2926 "produce noisy image"
2927 "apply ctf correction"
2928 "filter"
2929 """)
2930 t0_tree.setup(empty_parent(), def_env, w_.state_.storage)
2931 t0_tree_pic = w_.canvas.view.start_add_l3tree(t0_tree)
2932 flush_events()
2933 t0_tree_pic.move(+8, +5.5) # Relative movement.
2936 #*** concise view, program + calls, static
2937 # manually written
2938 # no program editing (static)
2939 t1_tree = reader.parse("""
2940 project_from_volume(spi.pj3)
2942 convolve_image_and_ctf(spi.ft, spi.tfc, spi.mu, spi.ft)
2944 produce_noisy_image(spi.mo, spi.adf)
2946 apply_ctf_correction(spi.tfcts)
2948 filter(spi.fq)
2949 """)
2950 t1_tree.setup(empty_parent(), def_env, w_.state_.storage)
2951 t1_tree_pic = w_.canvas.view.start_add_l3tree(t1_tree)
2952 flush_events()
2953 t1_tree_pic.move(+35, +7.5) # Relative movement.
2957 #*** concise view, program + manually selected details
2958 # manually written.
2959 # For ctf.l3, selected full subtrees are the desirable level of detail, so
2960 # no reordered node display is needed.
2961 # To keep the connection to the (function name based) outline view,
2962 # the function names can appear explicitly around the expansion.
2963 # For the present sample, this is redundant. Maybe not for others?
2964 t3_tree = reader.parse("""
2965 project_from_volume(
2966 spi.pj3(
2967 testimg = spi.pj3("/home/hohn/w/phenix/l3lang/test/volext.spi",
2968 [64, 64],
2969 [45.5, 27.2, -30.0])))
2971 convolve_image_and_ctf(
2972 spi.ft,
2973 spi.tfc(
2974 ctf = spi.tfc((2), # cs[mm]
2975 (10000, 0.025), # defocus(a), lambda(a)
2976 (64, 64), # dimension of the 2d array
2977 (0.17), # maximum spatial frequency
2978 (0.005, 0), # source size[a-1], defocus spread[a]
2979 (0, 0), # astigmatism[a], azimuth[deg]
2980 (0.1, 10000), # amplitude contrast ratio, gaussian envelope halfwidth
2981 (-1), # sign (+1 or -1)
2983 spi.mu,
2984 spi.ft)
2986 produce_noisy_image(
2987 spi.mo(
2988 noise = call_kwd_func(spi.mo, [64, 64], "random",
2989 { use_gaussian = 'y', mean = 1,
2990 std_deviation = 200} )),
2991 spi.adf)
2993 apply_ctf_correction(
2994 spi.tfcts(
2995 restored_img = spi.tfcts(spi.img_to_stack(noisyimg),
2996 [1, 1],
2997 spi.img_to_stack(ctf),
2998 100))
3001 filter(spi.fq)
3002 """)
3003 t3_tree.setup(empty_parent(), def_env, w_.state_.storage)
3004 t3_tree_pic = w_.canvas.view.start_add_l3tree(t3_tree)
3005 flush_events()
3006 t3_tree_pic.move(+70, +7.5) # Relative movement.
3009 #*** concise view, program + some full details
3010 # manually written
3011 # full program editing in the details
3012 # This version is disjoint from the others; it's missing the
3013 # "outline headers"
3014 t2_tree = reader.parse("""
3015 project_from_volume(
3016 testimg = spi.pj3("/home/hohn/w/phenix/l3lang/test/volext.spi",[64, 64],
3017 [45.5, 27.2, -30.0]))
3019 convolve_image_and_ctf(spi.ft,
3020 ctf = spi.tfc(
3021 (2), # cs[mm]
3022 (10000, 0.025), # defocus(a), lambda(a)
3023 (64, 64), # dimension of the 2d array
3024 (0.17), # maximum spatial frequency
3025 (0.005, 0), # source size[a-1], defocus spread[a]
3026 (0, 0), # astigmatism[a], azimuth[deg]
3027 (0.1, 10000), # amplitude contrast ratio, gaussian envelope halfwidth
3028 (-1), # sign (+1 or -1)
3029 ), spi.mu, spi.ft)
3031 produce_noisy_image(
3032 noise = call_kwd_func(spi.mo, [64, 64], "random",
3033 { use_gaussian = 'y', mean = 1,
3034 std_deviation = 200} ),
3035 spi.adf)
3037 apply_ctf_correction(restored_img = spi.tfcts(spi.img_to_stack(noisyimg),
3038 [1, 1],
3039 spi.img_to_stack(ctf),
3040 100)
3043 filter(spi.fq)
3044 """)
3045 t2_tree.setup(empty_parent(), def_env, w_.state_.storage)
3046 t2_tree_pic = w_.canvas.view.start_add_l3tree(t2_tree)
3047 flush_events()
3048 t2_tree_pic.move(+135, +7.5) # Relative movement.
3051 #* LOOP language addition.
3052 import pdb
3053 import l3lang
3054 from l3lang import utils, reader, ast, view, interp
3056 t2_tree = reader.parse("""
3057 inline "from pprint import pprint"
3058 loop dtheta(iteration, deltheta) from (1, 40):
3059 if iteration <= 10:
3060 pprint([iteration, deltheta])
3061 return dtheta(iteration + 1, deltheta / 2.0)
3062 else:
3063 return deltheta / 2.0
3064 """)
3066 play_env = interp.get_child_env(def_env, w_.state_.storage)
3068 if 2: # textual
3069 t2_tree.setup(empty_parent(), play_env, w_.state_.storage)
3070 t2_tree.interpret(play_env, w_.state_.storage)
3072 if 1: # graphical
3073 t2_tree.setup(empty_parent(), play_env, w_.state_.storage)
3074 # view.print_info(storage, tree)
3075 # pdb.run('w_.canvas.view.start_add_l3tree(t2_tree)')
3076 # b l3Loop.__init__
3077 ## fix:
3078 ##l3gui.misc.DisplayError: internal _container / _parent inconsistency.
3079 ##w_.canvas.view.clear_state()
3080 w_.canvas.view.start_add_l3tree(t2_tree)
3081 flush_events()
3082 gtk.main()
3085 #* LIST language addition.
3086 import pdb
3087 import l3lang
3088 from l3lang import utils, reader, ast, view, interp
3090 # w/o label.
3091 t2_tree = reader.parse("""
3092 inline "from pprint import pprint"
3093 list:
3095 iteration = 5
3096 if iteration <= 10:
3097 pprint([iteration])
3098 else:
3099 pprint((iteration, "small"))
3101 """)
3103 # with label.
3104 t2_tree = reader.parse("""
3105 inline "from pprint import pprint"
3106 list "A short block":
3108 iteration = 5
3109 if (iteration <= 10):
3110 pprint([iteration])
3111 else:
3112 pprint((iteration, "small"))
3114 """)
3116 play_env = interp.get_child_env(def_env, w_.state_.storage)
3118 if 1: # graphical
3119 t2_tree.setup(empty_parent(), play_env, w_.state_.storage)
3120 w_.canvas.view.start_add_l3tree(t2_tree)
3121 flush_events()
3122 gtk.main()
3124 if 2: # textual
3125 t2_tree.setup(empty_parent(), play_env, w_.state_.storage)
3126 t2_tree.interpret(play_env, w_.state_.storage)
3132 #* jsb paper
3134 The reference files for these fragments are now
3136 l3lang/test/voea-bp.py, v1.7 and beyond
3138 voea-bp-var.l3, v1.5 and beyond
3140 #** utils (evaluate this!)
3141 def canvas_add_program(text, xx = 0, yy = 0):
3142 t0_tree = reader.parse(text)
3143 t0_play_env = interp.get_child_env(def_env, w_.state_.storage)
3144 t0_tree.setup(empty_parent(), t0_play_env, w_.state_.storage)
3145 t0_tree_l3pic = w_.canvas.view.start_add_l3tree(t0_tree)
3146 t0_tree_l3pic.move(xx, yy) # Relative movement.
3148 def canvas_add_1st_expr(text, xx = 0, yy = 0):
3149 t0_tree = reader.parse(text)[0][0]
3150 t0_tree.setup(empty_parent(), def_env, w_.state_.storage)
3151 t0_tree_l3pic = w_.canvas.view.start_add_l3tree(t0_tree)
3152 t0_tree_l3pic.move(xx, yy) # Relative movement.
3154 def canvas_add_ast(self, t0_tree, xx = 0, yy = 0):
3155 from l3lang import utils, reader, ast, view, interp
3156 st = self.w_.state_
3157 t0_play_env = interp.get_child_env(st.def_env, st.storage)
3158 t0_tree.setup(ast.empty_parent(), t0_play_env, st.storage)
3159 t0_tree_l3pic = self.start_add_l3tree(t0_tree)
3160 t0_tree_l3pic.move(xx, yy) # Relative movement.
3161 return t0_tree_l3pic
3162 l3Canvas.canvas_add_ast = canvas_add_ast
3165 # print "\n".join(map(lambda (x,y): "%g %g" % (x,y),
3166 # (zip([1,2], [3,4]))))
3169 #*** plot
3170 def plot(x_l, y_l):
3171 data = "\n".join(map(lambda (x,y): "%g %g" % (x,y),
3172 zip(x_l, y_l)))
3173 os.system("""
3174 gnuplot -persist <<EOF
3176 set term post 24
3177 set out "/tmp/foo.ps"
3179 set grid
3180 set nokey
3181 ## set title "comparison"
3183 plot '-' w lines lw 6
3187 ps2epsi /tmp/foo.ps /tmp/foo.eps
3188 gv /tmp/foo.eps &
3189 """ % data)
3190 pass
3192 ## plot([1,2], [3,4])
3195 #*** directory of program run
3196 # See Program.directory
3199 #** voea-bp
3200 # #*** concise view, program level, static
3201 # # manually written
3202 # # no program editing (static)
3203 # t0_tree = reader.parse("""
3204 # "get reference volume"
3205 # "iterate delta theta"(
3206 # "calculate angles",
3207 # "projections",
3208 # "backproject",
3209 # "compare volumes",
3210 # "repeat if necessary")
3211 # """)
3212 # t0_play_env = interp.get_child_env(def_env, w_.state_.storage)
3213 # t0_tree.setup(empty_parent(), t0_play_env, w_.state_.storage)
3214 # ## w_.canvas.view.clear_state()
3215 # t0_tree_l3pic = w_.canvas.view.start_add_l3tree(t0_tree)
3216 # t0_tree_l3pic.move(+8.5, +7.5) # Relative movement.
3217 # flush_events()
3220 # #*** concise view, program + calls, static
3221 # # manually written
3222 # # no program editing (static)
3223 # t1_tree = reader.parse("""
3224 # "get reference volume"
3225 # "iterate delta theta"(
3226 # "calculate angles"(anglist = voea(deltheta)),
3227 # "projections"(proj_list = gridproj(ref_vol,
3228 # anglist,
3229 # 6,
3230 # 1.75)),
3231 # "backproject"(back_projection = backproj(proj_list,
3232 # anglist,
3233 # 0.4),
3234 # ),
3235 # "compare volumes"(ccc = EMAN2.Cmps.get("ccc").cmp(back_projection,
3236 # ref_vol)),
3237 # "repeat if necessary"(if_branch))
3238 # """)
3239 # t1_play_env = interp.get_child_env(def_env, w_.state_.storage)
3240 # t1_tree.setup(empty_parent(), t1_play_env, w_.state_.storage)
3241 # ## w_.canvas.view.clear_state()
3242 # t1_tree_l3pic = w_.canvas.view.start_add_l3tree(t1_tree)
3243 # t1_tree_l3pic.move(+42, +7.5) # Relative movement.
3244 # flush_events()
3247 #*** full program
3248 # Very clean top-to-bottom script
3249 tm_tree = reader.parse_file(
3250 os.path.expandvars('$SPXROOT/l3lang/test/voea-bp.l3'))
3251 tm_play_env = interp.get_child_env(def_env, w_.state_.storage)
3252 tm_tree.setup(empty_parent(), tm_play_env, w_.state_.storage)
3253 # w_.canvas.view.clear_state()
3254 tm_tree_l3pic = w_.canvas.view.start_add_l3tree(tm_tree)
3255 # tm_tree_l3pic.move(+77, +7.5) # Relative movement.
3256 gtk.main()
3259 #** Add text tree to canvas
3261 # num_angles
3262 # [12L, 13L, 27L, 59L, 195L]
3263 # Figure
3264 canvas_add_1st_expr("""
3265 # ccc
3267 0.8645,
3268 0.3051,
3269 0.1503,
3270 0.2821,
3271 0.5498,
3273 """)
3274 canvas_add_1st_expr("""
3275 # deltheta
3283 """)
3285 canvas_add_program("""
3286 from l3lang.test.voea_bp_support import *
3287 plot(
3288 # deltheta
3296 # ccc
3298 0.8645,
3299 0.3051,
3300 0.1503,
3301 0.2821,
3302 0.5498,
3305 """, xx=30, yy=50)
3307 # Figure from gnuplot
3310 #** get the diretory structure
3311 #*** in full
3312 ## use program -> eval locally
3313 # Static structure
3314 (self._l3tree)._block_env.print_tree()
3316 # After execution.
3317 (self._l3tree)._eval_env.print_tree()
3319 # As lists of names.
3320 # Too much content.
3321 from pprint import pprint
3322 pprint((self._l3tree)._block_env.get_tree())
3323 pprint((self._l3tree)._eval_env.get_tree())
3326 #*** More filtered.
3327 ## use program -> eval locally
3328 from pprint import pprint
3329 listing = (self._l3tree)._eval_env.get_dynamic_subtrees()
3330 pprint(listing)
3332 dir_struct = self._l3tree.directory(listing)
3333 dir_view = self._canvas.canvas_add_ast(dir_struct)
3334 dir_view.move(10, 10)
3337 #*** More^2 filtered.
3338 ## use program -> eval locally
3339 dir_struct = self._l3tree.directory_l3()
3340 dir_view = self._canvas.canvas_add_ast(dir_struct)
3341 dir_view.move(20, 20)
3343 # Figure
3344 dir_struct = List(aList([List(aList([List(aList([Symbol('volume'), Symbol('projections'), Symbol('errs'), Symbol('anglist'), Symbol('proj_list'), Symbol('back_projection'), Symbol('cross_corr'), Symbol('volfile'), Symbol('freqs'), Symbol('num_angles'), Symbol('angle_file'), Symbol('fsc_vals')])), Symbol('iteration'), Symbol('deltheta')])), List(aList([List(aList([Symbol('volume'), Symbol('projections'), Symbol('errs'), Symbol('anglist'), Symbol('proj_list'), Symbol('back_projection'), Symbol('cross_corr'), Symbol('volfile'), Symbol('freqs'), Symbol('num_angles'), Symbol('angle_file'), Symbol('fsc_vals')])), Symbol('iteration'), Symbol('deltheta')])), List(aList([List(aList([Symbol('volume'), Symbol('projections'), Symbol('errs'), Symbol('anglist'), Symbol('proj_list'), Symbol('back_projection'), Symbol('cross_corr'), Symbol('volfile'), Symbol('freqs'), Symbol('num_angles'), Symbol('angle_file'), Symbol('fsc_vals')])), Symbol('iteration'), Symbol('deltheta')])), List(aList([List(aList([Symbol('volume'), Symbol('projections'), Symbol('errs'), Symbol('anglist'), Symbol('proj_list'), Symbol('back_projection'), Symbol('cross_corr'), Symbol('volfile'), Symbol('freqs'), Symbol('num_angles'), Symbol('angle_file'), Symbol('fsc_vals')])), Symbol('iteration'), Symbol('deltheta')])), List(aList([List(aList([Symbol('volume'), Symbol('projections'), Symbol('errs'), Symbol('anglist'), Symbol('proj_list'), Symbol('back_projection'), Symbol('cross_corr'), Symbol('volfile'), Symbol('freqs'), Symbol('num_angles'), Symbol('angle_file'), Symbol('fsc_vals')])), Symbol('iteration'), Symbol('deltheta')])), Symbol('dtheta'), Symbol('spi'), Symbol('ref_vol')]))
3346 dir_view = w_.canvas.view.canvas_add_ast(dir_struct)
3347 dir_view.move(20, 20)
3350 import gtk
3351 gtk.main() # use menu->exit
3353 #* presentation-may-2006
3354 #** info
3355 # xterm reference size 120x50
3356 # plot:
3357 # convert /tmp/foo.ps -rotate 90 -resize 400x400 /tmp/foo.png
3358 #** library setup
3359 tm_tree = reader.parse("""
3360 list "library":
3361 list "em tasks":
3362 anglist2doc
3365 getImage
3366 gridproj
3367 imglist2stack
3368 spidbprg
3369 voea
3371 list "eman2 members":
3372 volume.save
3374 list "spider members":
3375 spi.bprg
3377 list "tools":
3378 init_spider
3379 setup_directory
3380 plot
3382 list "programming":
3383 PROGRAM
3384 { |x,y| x - y }
3388 (a = 2)
3389 call_function(a, b)
3391 {a = 1, b = 2}
3392 if condition:
3394 else:
3397 inline "from l3lang.test.voea_bp_support import *"
3399 loop name(arg) from (start_val):
3400 body
3401 """)
3403 tm_play_env = interp.get_child_env(def_env, w_.state_.storage)
3404 tm_tree.setup(empty_parent(), tm_play_env, w_.state_.storage)
3406 tm_tree_l3pic = w_.canvas.view.start_add_l3tree(tm_tree)
3408 gtk.main()
3411 #** value insertion preview
3412 # Figure
3413 canvas_add_1st_expr("""
3414 # ccc
3416 0.8645,
3417 0.3051,
3418 0.1503,
3419 0.2821,
3420 0.5498,
3422 """)
3423 canvas_add_1st_expr("""
3424 # deltheta
3432 """)
3434 canvas_add_program("""
3435 inline "from l3lang.test.voea_bp_support import *"
3436 plot(
3437 # deltheta
3445 # ccc
3447 0.8645,
3448 0.3051,
3449 0.1503,
3450 0.2821,
3451 0.5498,
3454 """, xx=30, yy=50)
3457 #* sparx functions as of [Jun-13-2006]
3459 #** Changed sparx imports to fix this.
3460 #** Trying to get an overview of the sparx functions
3461 # as of [Jun-13-2006]
3462 import sparx
3463 pprint(dir(sparx))
3465 # For this, the module structure SHOULD be useful. But it isn't
3466 # available interactively.
3468 # The following doesn't do much good. There are no comments, and
3469 # e.g. do_alignment (visible via dir(sparx)) is not shown at all.
3470 help(sparx)
3472 # This fails ...
3473 from sparx import libpy
3474 # with ImportError: cannot import name libpy.
3475 # Checking,
3476 sparx
3477 # is <module 'sparx' from '/net/hooknose/scratch1/hohn/rh9/ia32/installer-0.5/sparx-bin/opt/sparx/libpy/sparx.pyc'>
3479 # All because of the
3480 # from utilities import *
3481 # from filter import *
3482 # from projection import *
3483 # from fundamentals import *
3484 # from statistics import *
3485 # from alignment import *
3486 # from morphology import *
3487 # from reconstruction import *
3488 # in libpy/sparx.py!
3489 help(sparx.do_alignment)
3491 # So, I manually get the structure.
3492 # sparx
3493 # utilities
3494 # filter
3495 # projection
3496 # fundamentals
3497 # statistics
3498 # alignment
3499 # morphology
3500 # reconstruction
3502 # For nested modules, this should give information
3503 prfx, lst = ("sparx",
3504 ["utilities",
3505 "filter",
3506 "projection",
3507 "fundamentals",
3508 "statistics",
3509 "alignment",
3510 "morphology",
3511 "reconstruction",
3514 [help("%s.%s" % (prfx, itm)) for itm in lst]
3516 # But here it fails with
3517 # no Python documentation found for 'sparx.utilities'
3518 # no Python documentation found for 'sparx.filter'
3519 # no Python documentation found for 'sparx.projection'
3520 # ...
3521 # again because of the import *
3523 #** Overview of the sparx functions as of [Tue Jun 13 13:05:25 2006]
3524 import sparx.libpy as spx
3525 pprint(dir(spx))
3527 # module/function listing
3528 for mod in dir(spx):
3529 if not is_module(mod): continue
3530 for func in dir(mod):
3531 if not is_function(func): continue
3532 print "module: %s\t\tfunction: %s" % (mod, func)
3535 help(sparx)
3536 #** sparx.gui console use:
3537 import os
3538 tm_tree = reader.parse_file(
3539 os.path.expandvars('$SPXROOT/l3gui/test/iref01.py'))
3540 tm_play_env = interp.get_child_env(def_env, w_.state_.storage)
3541 tm_tree.setup(empty_parent(), tm_play_env, w_.state_.storage)
3542 tm_tree_l3pic = w_.canvas.view.start_add_l3tree(tm_tree)
3543 gtk.main()
3545 #* sparx persistence
3546 #** eman 'native' I/O
3547 import sparx as sx
3548 volume = sx.getImage( "model.tcp" )
3550 raw = sx.project(volume, [10.0, 20.0, 30.0, 0.0, 0.0], 35)
3551 raw.set(sx = 0)
3552 sx.info(raw)
3553 print raw.get_attr_dict()
3555 # Looks like spider format doesn't keep the attributes:
3556 sx.dropImage(raw, "raw.spi", itype = 's')
3557 raw_s = sx.getImage("raw.spi")
3558 sx.info(raw_s)
3559 print raw_s.get_attr_dict()
3560 print raw_s.sx
3562 # hdf works:
3563 sx.dropImage(raw, "raw.hdf")
3564 raw_s = sx.getImage("raw.hdf")
3565 sx.info(raw_s)
3566 print raw_s.get_attr_dict()
3567 print raw_s.sx
3569 #** pickling
3570 # Make sure there is no EMData.__getattr__ to avoid '__getinitargs__':
3571 # 'Not exist' caught crap.
3572 import l3lang.utils as ut
3573 ut.file_pickle(raw)
3574 raw_p = ut.file_unpickle("/tmp/foo")
3575 sx.info(raw)
3576 sx.info(raw_p)
3577 print raw.get_attr_dict()
3578 print raw_p.get_attr_dict()
3581 #** l3 state table pickling
3582 w_.state_.storage
3583 import l3lang.utils as ut
3584 ut.file_pickle(w_.state_.storage, "st.post-run-table")
3585 # takes forever; remove
3587 #* comment attachment
3588 # Similar to reader.raw_lexer(code_s)
3590 import sys, token, tokenize
3591 code_s = open(os.path.expandvars('$SPXROOT/l3gui/test/iref01.py')).read()
3594 #** comment parsing
3595 # Comment type identifier.
3596 COMMENT, _ = filter(lambda (k,v): v == 'COMMENT', token.tok_name.items())[0]
3597 NEWLINE, _ = filter(lambda (k,v): v == 'NEWLINE', token.tok_name.items())[0]
3598 NL, _ = filter(lambda (k,v): v == 'NL', token.tok_name.items())[0]
3602 #*** Cluster connected successive comment lines.
3603 # When connecting to the parse tree, this style of clustering has a
3604 # problem. Given
3606 # # comment 1
3608 # # comment 2
3610 # code 1
3611 # code 2
3613 # `comment 1` will attach to `code 1`, leaving `comment 2` to attach to
3614 # `code 2`, which is not good.
3616 # Choices:
3617 # - drop one of the comments, attach other to `code 1`
3618 # - combine comments and attach to `code 1`
3620 gen = tokenize.generate_tokens(reader.string_readline(code_s))
3621 st_accum = 0
3622 va_cluster = [] # (string, begin, end) list
3623 va_accum = [] # va_cluster list
3624 for ii in gen:
3625 (type, string, begin, end, line) = ii
3627 # Dump every comment.
3628 if type == COMMENT:
3629 print "(%r, %s, %s)" % (string, begin, end)
3631 # Cluster comment lines.
3632 if type in [COMMENT]:
3633 st_accum = 1
3634 va_cluster.append( (string, begin, end) )
3636 else:
3637 if st_accum:
3638 va_accum.append(va_cluster)
3639 st_accum = 0
3640 va_cluster = []
3642 # Dump data.
3643 pprint(va_accum, sys.stdout, 4, 2)
3646 #*** Cluster successive (incl.separated) comment lines
3647 # Combine blank-separated comment blocks.
3648 gen = tokenize.generate_tokens(reader.string_readline(code_s))
3649 st_accum = 0
3650 va_cluster = [] # (string, begin, end) list
3651 va_accum = [] # va_cluster list
3652 for ii in gen:
3653 (type, string, begin, end, line) = ii
3655 # # Dump every comment.
3656 # if type == COMMENT:
3657 # print "(%r, %s, %s)" % (string, begin, end)
3659 # Cluster successive comment lines, even those separated by blanks.
3660 if type in [COMMENT]:
3661 st_accum = 1
3662 va_cluster.append( (string, begin, end) )
3664 elif type in [NEWLINE, NL]:
3665 if st_accum:
3666 va_cluster.append( (string, begin, end) )
3667 else:
3668 if st_accum:
3669 # Remove trailing empty newlines.
3670 tl = ['\n']
3671 while tl[0] == '\n':
3672 tl = va_cluster.pop()
3673 va_cluster.append(tl)
3674 # Keep cluster.
3675 va_accum.append(va_cluster)
3676 st_accum = 0
3677 va_cluster = []
3679 pprint(va_accum)
3681 # Print cluster.
3682 for clus in va_accum:
3683 print clus[0][1] # begin
3684 for line in clus:
3685 print line[0], # body
3686 print clus[-1][2] # end
3687 print
3690 #*** Simplify comment structure
3691 # Adjust lexeme row indexing, compact clusters, and remove unused data.
3692 va_comments = [] # ((last_row, first_col), string) list
3693 va_comments = [
3694 ( (clus[-1][2][0] - 1, clus[0][1][1]), # (last_row, first_col)
3695 "".join([line[0] for line in clus]))
3696 for clus in va_accum ]
3697 pprint(va_comments)
3700 #** Tree work
3701 tree = reader.parse(code_s)
3704 #*** extract a list of leading subnodes from the parse tree
3705 lrow, lcol = -1, -1
3706 tr_lines = [] # [(start_row, col), node] list
3707 for nd in tree.subtrees():
3708 (row, col), _ = nd.get_char_range()
3709 # Prune all but first expression on a line.
3710 if row != lrow:
3711 tr_lines.append( [(row,col), nd] )
3712 ## tr_lines.append( [(row,col), nd] )
3713 lrow = row
3714 pprint(tr_lines)
3718 #** Find expression to attach to.
3719 # Consider only the first expression in given line.
3720 # Find the first expression following a comment end.
3722 def align_and_show():
3723 com_it = iter(va_comments)
3724 node_it = iter(tr_lines)
3725 com_row = lambda com: com[0][0]
3726 while 1:
3727 try: com = com_it.next()
3728 except StopIteration: break
3729 try: nd = node_it.next()
3730 except StopIteration: break
3731 com_line = com[0][0]
3732 # Find first line after current comment.
3733 while nd[0][0] <= com_line:
3734 nd = node_it.next()
3735 if com[0][1] != nd[0][1]:
3736 print "==== column mismatch"
3737 print com[1]
3738 print nd[1].source_string()
3739 align_and_show()
3741 ## import pdb; pdb.set_trace()
3744 #* selection handling
3745 # See pygtk-2.4.1/examples/ide/edit.py as example.
3747 #* gtk assertions
3748 import warnings
3749 warnings.filterwarnings("error", "assertion") # , GtkWarning
3751 # But the C code just continues on.
3753 # ... the bigger problem is that a fair amount of C code doesn't
3754 # handle the case where the assertion fails ...
3758 # Cause a sigsev
3759 view = w_.canvas.view
3760 ra = w_.canvas.view.root().add
3761 c_label = ra(canvas.CanvasText,
3762 x = 12, y = 12,
3763 size_points = 12 * 1,
3764 scale = 1.0,
3765 scale_set = True,
3766 text = "the test",
3768 flush_events()
3769 print c_label.get_bounds()
3770 print c_label.__grefcount__
3771 c_label.destroy()
3773 #* gtk proxy test
3774 view = w_.canvas.view
3775 ra = GtkObjProxy(w_.canvas.view.root())
3776 c_label = GtkObjProxy(ra.add(canvas.CanvasText,
3777 x = 12, y = 12,
3778 size_points = 12 * 1,
3779 scale = 1.0,
3780 scale_set = True,
3781 text = "the test",
3783 flush_events()
3784 print c_label.get_bounds()
3785 c_label.destroy()
3786 print c_label._live
3787 print c_label.get_bounds()
3790 #* python compile tests
3791 # Accepted as complete input...
3792 print compile('''
3793 if 1:
3798 else:
3800 ''', '<string>', 'single')
3803 #* l3 tests
3805 # test if
3806 if a > 0:
3808 else:
3811 # generic if (cond)
3814 # if
3815 x < 0,
3817 # elif
3818 x = 0,
3820 # elif
3821 x > 0,
3823 # else
3824 True,
3828 #* gtk object pickling
3829 # local eval of l3Rawtext
3830 import l3lang.utils as ut
3831 # Runs (works? Protocol 1 fails.).
3832 ut.file_pickle( self._ltext.get_property("anchor"))
3833 val = ut.file_unpickle("/tmp/foo")
3835 # oops:
3836 # (.:19911): GLib-GObject-WARNING **: cannot retrieve class for
3837 # invalid (unclassed) type `<invalid>'
3839 # ** ERROR **: file pygenum.c: line 60 (pyg_enum_repr): assertion
3840 # failed: (G_IS_ENUM_CLASS(enum_class))
3841 # aborting...
3844 #* l3IfLoop addition
3845 #** test single loop
3846 test_s = """
3847 if "loop":
3848 def lname(largs):
3849 lbody
3850 lname(cargs)
3851 """ # "
3854 #** test expression
3855 test_s = """
3856 if "loop":
3857 def _loop(bar):
3858 # calc 3d reconstruction
3859 foo = bar + 10
3860 if "loop":
3861 def _lname(foo):
3862 if done:
3863 return bar
3864 # calc 3d reconstruction
3865 # filter
3867 # repeat from gen. proj
3868 if 1:
3869 return _lname(2 - foo)
3870 _lname(foo)
3872 # filter
3874 # repeat from gen. proj
3875 if good:
3876 return _loop(bar + 1)
3877 _loop(1)
3878 """ # "
3879 l3tree = reader.parse(test_s)
3880 l3tree
3883 #** l3ifloopbreak pattern 1
3884 code_s = """
3885 if COND:
3886 return LNAME(LARGS)
3887 """ # "
3888 template = reader.parse(code_s)
3891 #** l3ifloopbreak pattern 2
3892 code_s = """
3893 if ! COND:
3894 return ARGS
3895 """ # "
3896 template = reader.parse(code_s)
3900 #** test single loop w/ break
3901 test_s = """
3902 if "loop":
3903 def lname(largs):
3904 first
3905 if 1:
3906 return 2
3907 last
3908 lname(cargs)
3909 """ # "
3912 #** test single loop w/ continue
3913 test_s = """
3914 if "loop":
3915 def lname(largs):
3916 first
3917 if 1:
3918 return lname(new_args)
3919 lname(cargs)
3920 """ # "
3924 #** l3ifloop pattern
3925 code_s = """
3926 if "loop":
3927 def LNAME(LARGS):
3928 LBODY
3929 LNAME2(CARGS)
3930 """ # "
3931 template = reader.parse(code_s)
3932 template.body()
3935 #** matcher
3936 # From template.body().
3937 ma = ast.Matcher()
3938 body = l3tree.body()
3939 if ma.match(body,
3940 If(String('loop'),
3941 aList([Set(MarkerTyped(Symbol('LNAME'), Symbol('symbol')),
3942 Function(MarkerTyped(Symbol('LARGS'), aList([])),
3943 MarkerTyped(Symbol('LBODY'), aList([])))),
3944 Call(MarkerTyped(Symbol('LNAME2'), Symbol('symbol')),
3945 MarkerTyped(Symbol('CARGS'), aList([])))]),
3946 Marker('_'))):
3947 if ma['LNAME'] != ma['LNAME2']:
3948 warn_("loop name mismatch; no special display")
3949 elif len(ma['LARGS']) != len(ma['CARGS']):
3950 warn_("arguments mismatched between loop definition and call")
3951 else:
3952 print_(ma['LNAME'])
3953 print_(ma['LARGS'])
3954 print_(ma['CARGS'])
3955 print_(ma['LBODY'])
3958 #* l3 tests
3959 #** load l3
3960 from l3lang.l3 import *
3961 run()
3962 #** interpretation order
3963 def foo():
3964 'foo'
3965 foo()
3967 #** args
3968 def foo(a,b, args):
3969 return args
3970 foo(1,2, { a = 1})
3972 #** print
3973 print 1, "hello", 2
3974 print "hello", "world"
3976 #** Function
3977 inline "from math import *"
3978 def f(a,b, c = cos(22), d = 10):
3980 def g(a,b):
3982 # errors:
3983 f(1) # too few
3984 f(1,2,3) # too many
3985 f(1,2, e = 1) # unknown keyword
3987 # ok
3988 p = g(1,2)
3989 p = f(1,2, c = cos(22), d = 1)
3990 p = f(1,2, c = 22, d = 1)
3991 p = f(1,2, c = 'c')
3992 p = f(1,2)
3993 #** Call to python
3994 inline '''
3995 def pyf(a,b, c=1):
3996 return a,b,c
3998 pyf(10,20)
3999 pyf(10,20, c = 30)
4000 #** If
4001 last_ring = 100
4002 if(last_ring==-1):
4003 last_ring=nx-2
4005 (last_ring == -1)
4007 # Ok:
4008 last_ring == -1
4009 last_ring <= -1
4012 # old errors (pre-r460)
4013 print parse('a = b')
4014 print parse('a == b')
4015 # error (r439):
4016 print parse('(a == b)')
4017 print parse('(a <= b)')
4018 # ok:
4019 print parse('(a = b)')
4020 print parse('(av1*(int(nima/2)+(nima%2)) + av2*int(nima/2))/nima')
4023 #* l3 tests, in gui (repeated interpretation checks)
4024 program:
4025 filter(lambda x: x > 2, [1,2,3,4])
4027 if 1:
4028 inline "from l3lang.l3 import *"
4029 def f(a,b, c = cos(22), d = 10):
4031 f(1,2)
4032 f(1, 2, c = 22)
4034 if ("prog", "filter test"):
4035 filter(lambda x: x > 2, [1,2,3,4])
4037 program:
4038 # subtract
4039 a - b
4041 program:
4042 6* (3 - 2)
4044 # using > "eval locally"
4045 # >>> self._l3tree
4046 # Program(aList([Call(Symbol('*'), aList([Int(6L), Call(Symbol('-'), aList([Int(3L), Int(2L)]))]))]))
4047 # >>> self._l3tree._first_char
4048 # (0, 0)
4049 # >>> self._l3tree._last_char
4050 # (2, 0)
4054 #* statement additions
4055 #** is, is not, not in
4056 code_s = """
4057 a is not None
4058 b not in [c,d]
4059 print hello, "world"
4061 reader.raw_lexer(code_s)
4064 #* l3 internals tests (from python)
4065 #** Call
4066 tree = reader.parse('f(1,2, c = sin(22), d = 10)')
4068 play_env = interp.get_child_env(def_env, storage)
4069 tree.setup(empty_parent(), play_env, storage)
4070 view.print_info(storage, tree, show_macros = True)
4072 # Internals.
4073 print tree.body().positional_args()
4074 print tree.body().nargs()
4075 print tree.body().named_args()
4076 # Interpret.
4077 tree.interpret(def_env, storage)
4078 #** Function
4079 tree = reader.parse('''
4080 def f(a,b, c = cos(22), d = 10):
4082 ''') # '''
4084 play_env = interp.get_child_env(def_env, storage)
4085 tree.setup(empty_parent(), play_env, storage)
4086 view.print_info(storage, tree, show_macros = True)
4088 func_ = tree.body()[1]
4089 print func_.positional_block_args()
4090 print func_.named_block_args()
4091 print func_.nargs()
4093 # Interpret.
4094 tree.interpret(def_env, storage)
4096 #** macro
4097 #*** parse
4098 from l3lang.view import print_ast
4099 def prnt(str):
4100 print_ast(reader.parse(str))
4101 prnt('''
4102 def "foo"():
4104 ''')
4105 #*** eval 1
4106 tree = reader.parse('''
4107 aa = 1
4108 def "foo"():
4109 print aa
4110 foo()
4111 ''')
4113 # Setup.
4114 play_env = interp.get_child_env(def_env, storage)
4115 tree.setup(empty_parent(), play_env, storage)
4116 view.print_info(storage, tree, show_macros = True)
4118 # Interpret.
4119 tree.interpret(def_env, storage)
4120 view.print_info(storage, tree)
4121 view.print_all_info(storage, tree)
4122 # ok: repeated .interpret() reuses first clone.
4124 #*** parse 2
4125 code_s = ('''
4127 print aa ;
4129 ''')
4130 reader.py_lexer(code_s)
4131 print reader.parse(code_s)
4133 #*** eval 3 - single call
4134 code_s = ('''
4135 aa = 1
4136 def "foo"():
4137 print aa
4139 foo()
4140 ''')
4141 # reader.raw_lexer(code_s)
4142 # reader.py_lexer(code_s)
4143 tree = reader.parse(code_s)
4145 # Setup.
4146 play_env = interp.get_child_env(def_env, storage)
4147 tree.setup(empty_parent(), play_env, storage)
4148 view.print_info(storage, tree, show_macros = True)
4150 # Interpret.
4151 tree.interpret(play_env, storage)
4152 view.print_info(storage, tree)
4153 view.print_all_info(storage, tree)
4154 # repeated .interpret() reuses first clone ?
4157 #*** eval 4 - variable value change
4158 code_s = ('''
4159 aa = 1
4160 def "foo"():
4161 print aa
4162 aa = aa + 1
4163 print aa
4164 foo()
4165 ''')
4166 # reader.raw_lexer(code_s)
4167 # reader.py_lexer(code_s)
4168 tree = reader.parse(code_s)
4170 # Setup.
4171 play_env = interp.get_child_env(def_env, storage)
4172 tree.setup(empty_parent(), play_env, storage)
4173 view.print_info(storage, tree, show_macros = True)
4175 # Interpret.
4176 tree.interpret(play_env, storage)
4177 view.print_env(play_env)
4178 view.print_info(storage, tree)
4179 view.print_all_info(storage, tree)
4180 # repeated .interpret() reuses first clone ???
4183 #*** eval 5 -- loop
4184 code_s = ('''
4185 aa = 1
4186 def "foo"():
4187 print aa
4188 aa = aa + 1
4189 if aa < 5:
4190 foo()
4191 foo()
4192 ''')
4193 # reader.raw_lexer(code_s)
4194 # reader.py_lexer(code_s)
4195 tree = reader.parse(code_s)
4197 # Setup.
4198 play_env = interp.get_child_env(def_env, storage)
4199 tree.setup(empty_parent(), play_env, storage)
4200 view.print_info(storage, tree, show_macros = True)
4202 # Interpret.
4203 tree.interpret(play_env, storage)
4204 view.print_all_info(storage, tree)
4205 tree.interpret(play_env, storage)
4206 view.print_all_info(storage, tree)
4208 view.print_info(storage, tree)
4209 view.print_env(play_env)
4210 # repeated .interpret() reuses first clone ???
4214 #* while loop
4216 #** eval 6 -- `while` expansion
4217 # '''
4218 # while C:
4221 # Python
4222 a = 1
4223 while a < 3:
4224 print a
4225 a = a + 1
4227 # l3 expanded
4228 code_s = ('''
4229 a = 1
4230 if "while":
4231 def "_while_123"():
4232 # Condition
4233 if not (a < 3):
4234 return
4235 else:
4236 # Body
4237 print a
4238 a = a + 1
4239 # end Body
4240 return _while_123()
4241 _while_123()
4242 ''')
4243 tree = reader.parse(code_s)
4245 # Setup.
4246 play_env = interp.get_child_env(def_env, storage)
4247 tree.setup(empty_parent(), play_env, storage)
4249 # Interpret.
4250 tree.interpret(play_env, storage)
4251 view.print_all_info(storage, tree)
4253 tree.interpret(play_env, storage)
4254 view.print_all_info(storage, tree)
4256 view.print_env(play_env)
4259 #** eval 7 -- `while` template
4260 # '''
4261 # while C:
4264 # l3 expanded
4265 code_s = ('''
4266 if "while":
4267 def "_while_ID"():
4268 if not C:
4269 return
4270 else:
4272 return _while_ID()
4273 _while_ID()
4274 ''')
4275 tree = reader.parse(code_s)
4277 # Setup.
4278 play_env = interp.get_child_env(def_env, storage)
4279 tree.setup(empty_parent(), play_env, storage)
4280 view.print_all_info(storage, tree)
4284 #** eval 8 -- `while` tests
4285 # '''
4286 #*** execution
4287 code_s = ('''
4288 a = 1
4289 while a < 3:
4290 print a
4291 a = a + 1
4292 ''')
4293 tree = reader.parse(code_s)
4295 # Setup.
4296 play_env = interp.get_child_env(def_env, storage)
4297 tree.setup(empty_parent(), play_env, storage)
4298 view.print_info(storage, tree)
4300 # Interpret.
4301 tree.interpret(play_env, storage)
4302 view.print_all_info(storage, tree)
4304 tree.interpret(play_env, storage)
4305 view.print_all_info(storage, tree)
4307 view.print_env(play_env)
4309 #*** reference comparison
4310 # Remove id / timestamp columns, then compare per-character.
4311 ref_s = ('''
4312 a = 1
4313 if "while":
4314 def "_while_ID"():
4315 # Condition
4316 if not (a < 3):
4317 return
4318 else:
4319 # Body
4320 print a
4321 a = a + 1
4322 # end Body
4323 return _while_ID()
4324 _while_ID()
4325 ''')
4326 ref_t = reader.parse(ref_s)
4327 # Setup.
4328 ref_env = interp.get_child_env(def_env, storage)
4329 ref_t.setup(empty_parent(), ref_env, storage)
4330 view.print_info(storage, ref_t)
4333 #** eval 9 -- `while` pattern
4334 # '''
4335 # l3 expanded
4336 code_s = ('''
4337 if "while":
4338 def "_while_ID"():
4339 if not COND:
4340 return
4341 else:
4342 BODY
4343 return _while_ID()
4344 _while_ID()
4345 ''')
4346 tree = reader.parse(code_s)
4349 #** while tests
4350 program:
4351 a = 1
4352 while a < 3:
4353 print a
4354 a = a + 1
4356 # Nesting, check identifiers.
4357 program:
4358 a = 1
4359 while a < 3:
4360 print a
4361 a = a + 1
4362 bb = 1
4363 while bb < 3:
4364 print a, bb
4365 print a * bb
4366 bb = bb + 1
4371 #* for loop language addition
4372 # for loop using while
4374 #** iterator wrapper
4375 class l3_iter:
4376 def __init__(self, seq):
4377 self._iter = iter(seq)
4378 self._next = None
4379 self._next_ready = False
4381 def more(self):
4382 if self._next_ready:
4383 return True
4384 try:
4385 self._next = self._iter.next()
4386 self._next_ready = True
4387 return True
4388 except StopIteration:
4389 self._next_ready = False
4390 return False
4392 def next(self):
4393 if self.more():
4394 self._next_ready = False
4395 return self._next
4396 raise InterpreterError("Request for elements from finished iterator.")
4399 #** original for
4401 # for V in I:
4404 for (x,y) in [(1,2), (3,4)]:
4405 print x, y
4408 #** expand to while
4409 program:
4410 I = l3iter([(1,2), (3,4)])
4411 while I.more():
4412 (x,y) = I.next()
4413 print x, y
4415 # To avoid pickling [(1,2), (3,4)] or I, use
4417 # inline 'I = l3iter([(1,2), (3,4)])'
4419 program:
4420 inline 'from l3lang.interp import l3iter; I = l3iter([(1,2), (3,4)])'
4421 while I.more():
4422 (x,y) = I.next()
4423 print x, y
4427 #** embed l3 'tail call' form for while:
4428 # while C:
4431 # if "while":
4432 # def "_while_ID"():
4433 # if not C: # force boolean evaluation via not
4434 # return
4435 # else:
4437 # return _while_ID()
4438 # _while_ID()
4440 program:
4441 if "for":
4442 inline 'from l3lang.interp import l3iter; I = l3iter([(1,2), (3,4)])'
4443 # orig. for
4444 def "_for_ID"():
4445 if not I.more():
4446 return
4447 else:
4448 (x,y) = I.next()
4449 print x, y
4450 return _for_ID()
4451 _for_ID()
4454 #** iterator problems
4455 # Problems:
4456 # 1. The sequence cannot be edited (it's a string)
4457 # 2. The indirection
4458 # - (value list VL) -> external (l3iter) -> (value sequence VS)
4459 # loses the time stamps between VL and VS, so that updates to VL
4460 # re-evaluate everything.
4462 # Thus, iterators are USELESS in l3: no persistence and no
4463 # incremental evaluation.
4465 # 3. Are I.more and I.next called again on repeated interpretation?
4467 # 4. External iterator assignments will cause failure:
4469 for V in I:
4472 # is ok, but
4474 I1 = get_iter()
4475 for V in I1:
4478 # will fail.
4480 # MAJOR PROBLEM:
4481 # Repeat execution of UNORDERED iterators will always fail.
4482 # l3 execution imparts unique ids, IN ORDER of execution.
4484 # Retrieving all iterator values and storing them is not an option
4485 # because iterators need not be finite.
4487 # A prime example: a true rand() MUST NOT be used in l3. Unless it's
4488 # a pseudo-random, FIXED sequence.
4491 # [Thu Jul 12 11:57:28 2007]
4492 # A possible adjustment is to let Call.interpret return all iterators
4493 # as externals, and evaluating every time as done for Inline(). If
4494 # done that way, the for loop also simplifies.
4496 program:
4497 if "for":
4498 I = l3iter([(1,2), (3,4)])
4499 # orig. for
4500 def "_for_ID"():
4501 if not I.more():
4502 return
4503 else:
4504 (x,y) = I.next()
4505 print x, y
4506 return _for_ID()
4507 _for_ID()
4510 #** iterator problems, part 2
4511 # ---------------------------------
4512 # Re-examining iterators in l3 is slow and makes interpretation yet
4513 # more complicated. Instead,
4515 # 1. make sure that l3iter instances pickle,
4517 # 2. assume (and test that) the iterator given to l3iter produces a
4518 # constant sequence, and
4520 # 3. let l3iter re-run the iterators (after unpickling, or better at
4521 # the first call to I.more) to get to the correct position in the
4522 # stream.
4523 # If the iterator itself performs a lot of work, nothing is gained
4524 # here...
4526 # Retrieving the iterator is a problem.
4527 # It cannot be saved with the l3iter instance, but re-running
4528 # l3iter(iterator) will create a new instance (losing the
4529 # timestamp/position info).
4530 # This requires some internal magic...
4533 #** simplify to sequences
4534 # ----------------------
4535 # Backtrack to Python < 2.2, without the __iterator__ mess. There,
4536 # ...
4537 # The for statement is used to iterate over the elements of a
4538 # sequence (string, tuple or list):
4539 # ...
4540 # The
4541 # sequence types support the len() and [] operators (__len__ and
4542 # __getitem__). Restricting to known-size sequences is a minor
4543 # restriction for tracked, DISPLAYED items.
4545 # For general iterators and simple uses, use Python directly. The
4546 # sample
4548 for k in edsummary.iterkeys():
4549 print k + ": %.3f" % edsummary[k]
4551 # uses an unordered iterator, so it cannot work in l3. It also performs
4552 # a trivial task not worth tracking, so it could be in python.
4554 # For this example, using
4555 for k in edsummary.keys():
4556 print k + ": %.3f" % edsummary[k]
4557 # in l3 would be better.
4559 # For sequences providing len() and getitem(), there are two choices:
4560 # 1. another wrapper ( l3traverse() ) can be added, and the
4561 # following should work:
4562 program:
4563 if "for":
4564 I = l3traverse([(1,2), (3,4)])
4565 # orig. for
4566 def "_for_ID"():
4567 if not I.more():
4568 return
4569 else:
4570 (x,y) = I.next()
4571 print x, y
4572 return _for_ID()
4573 _for_ID()
4574 # However, this wrapper would require access to l3 internals, so an
4575 # l3 version is preferable:
4577 # 2. Use a tail call form to handle indexing
4579 # for V in S:
4582 # In the following, ITEMS, IDX, LEN and LOOP are leaf nodes, to be
4583 # replaced UNIQUELY in if.setup().
4585 # See also ref[Wed Jul 18 11:38:10 2007]
4587 program:
4588 if "for":
4589 ITEMS = [(1,2), (3,4)]
4590 IDX = 0
4591 LEN = len(ITEMS)
4592 # orig. for
4593 def "LOOP"():
4594 if IDX < LEN:
4595 IDX = IDX + 1
4596 # V in S.
4597 (x,y) = ITEMS[ IDX - 1 ]
4598 # Body B
4599 print x, y
4600 # Iterate.
4601 return LOOP()
4602 LOOP()
4605 #** for tests
4606 # List as text (no insertions)
4607 program:
4608 for i in [1,2,3]:
4609 print i
4611 # List as structure.
4612 program:
4613 for i in [1,
4614 2,3]:
4615 print i
4617 program:
4618 for (i, x) in [(1, 1), (2, 4), (3,9)]:
4619 print i, x
4621 # Nesting, check identifiers.
4622 program:
4623 for i in [1,2,3]:
4624 print i
4625 for jj in [1,2,3]:
4626 print i, jj, i*jj
4630 #* scitbx flex array
4631 from scitbx.array_family import flex
4632 data = flex.random_double(size = 4)
4634 data.reshape(2,2)
4635 # Traceback (most recent call last):
4636 # File "<console>", line 1, in ?
4637 # ArgumentError: Python argument types in
4638 # double.reshape(double, int, int)
4639 # did not match C++ signature:
4640 # reshape(scitbx::af::versa<double, scitbx::af::flex_grid<scitbx::af::small<long, 10u> > > {lvalue}, scitbx::af::flex_grid<scitbx::af::small<long, 10u> >)
4643 #* slice and getitem
4644 #** python slice
4645 import numpy as N
4646 import operator
4647 na = N.arange(28).reshape((7,4))
4649 print na[1,2]
4650 # DIFFERENT:
4651 print na.__getitem__( [1,2] )
4652 print na.__getitem__( (1,2) ) # correct
4654 print na[1:4,2]
4655 print na.__getitem__( (slice(1,4), 2) )
4657 print na[1:4]
4658 print na.__getitem__( (slice(1,4), ) )
4659 print na.__getitem__( [slice(1,4)] )
4661 print na[1:3, 3:6]
4662 print na.__getitem__( (slice(1,3), slice(3,6)) )
4663 print operator.getitem(na, [slice(1,3), slice(3,6)] )
4664 print operator.getitem(na, (slice(1,3), slice(3,6)) )
4666 # Python reference values:
4667 print na[1,2]
4668 print na[1:4,2]
4669 print na[1:4]
4670 print na[1:3, 3:6]
4672 #** l3 slice
4673 # Compare with # Python reference values:
4674 inline '''
4675 import numpy as N
4677 na = N.arange(28).reshape((7,4))
4678 print na
4679 print na[1,2]
4680 print na[1:4,2]
4681 print na[1:4]
4682 print na[1:3, 3:6]
4687 #* gtk images, numeric, eman
4688 #'''
4689 import EMAN2 as em
4690 from EMAN2 import EMNumPy
4691 import Numeric as N
4692 import MLab as M
4694 #** numeric -> eman
4695 ema_arr = EMAN2.EMData()
4696 EMNumPy.numpy2em(num_arr, ema_arr)
4698 #** eman -> numeric
4699 foo = em.EMData()
4700 num_arr = EMNumPy.em2numpy(foo)
4702 EMNumPy.em2numpy(None)
4704 #** numeric -> gtk
4705 #*** 1. get numeric array
4706 num_arr = M.rand(4,4)
4708 #*** 2.b. numeric -> gtk
4709 # Convert float array to 8-bit array.
4710 rows, cols = num_arr.shape
4711 bottom = M.min(M.min(num_arr))
4712 top = M.max(M.max(num_arr))
4713 scale = (top - bottom)
4714 norm_arr = ((num_arr - bottom) / scale * 255).astype('b')
4716 #*** Use 3 8-bit arrays as (rgb) values.
4717 t3 = N.outerproduct(norm_arr, N.array([1,1,1], 'b'))
4718 t4 = N.reshape(t3, (rows, cols, 3))
4719 pix_arr = N.array(t4, 'b')
4721 #*** Get the pixbuf.
4722 # array input indexing is row, pixel, [r,g,b {,a} ]
4723 pixbuf = gtk.gdk.pixbuf_new_from_array(pix_arr,
4724 gtk.gdk.COLORSPACE_RGB, 8)
4726 #*** 3. display pixbuf as item
4727 d_canv = w_.canvas.view
4728 d_rt = d_canv.root().add(canvas.CanvasGroup)
4729 d_rt.add(canvas.CanvasPixbuf,
4730 x = 20, # world units
4731 y = 20,
4732 height_set = True,
4733 width_set = True,
4734 width = 15, # world units
4735 height = 15,
4736 pixbuf = pixbuf,
4738 d_rt.destroy()
4740 # Image orientation is consistent with `norm_arr` printed form.
4741 # Indexing is (row, col) from upper left.
4742 # Storage is row-major?
4744 # This is the first view of a VALUE -- the others are astTypes.
4745 # Useful value information is the program source.
4748 # "anchor" GtkAnchorType : Read / Write
4749 # "height" gdouble : Read / Write
4750 # "height-in-pixels" gboolean : Read / Write
4751 # "height-set" gboolean : Read / Write
4752 # "pixbuf" GdkPixbuf : Read / Write
4753 # "width" gdouble : Read / Write
4754 # "width-in-pixels" gboolean : Read / Write
4755 # "width-set" gboolean : Read / Write
4756 # "x" gdouble : Read / Write
4757 # "x-in-pixels" gboolean : Read / Write
4758 # "y" gdouble : Read / Write
4759 # "y-in-pixels" gboolean : Read / Write
4762 # # Other pixmap sources:
4763 # image = gtk.Image()
4764 # # use the current directory for the file
4765 # try:
4766 # pixbuf = gtk.gdk.pixbuf_new_from_file(GTKLOGO_IMAGE)
4767 # image.set_from_pixbuf(pixbuf)
4768 # #
4769 # image = gtk.Image()
4770 # image.set_from_file(BUDDY_IMAGE);
4771 # # image -> pixbuf
4772 # image.get_pixbuf()
4773 # # pixbuf -> canvas
4775 # gtk.gdk.pixbuf_new_from_array
4776 # gtk.gdk.Pixbuf.get_pixels_array
4777 # gtk.gdk.Pixbuf.save
4778 # call gtk.Image.queue_draw() after you change the Numeric array.
4781 # # from EMAN2.EMNumPy import numpy2em, em2numpy
4784 #* gtk images, numpy, eman
4785 #'''
4786 import EMAN2 as em
4787 from EMAN2 import EMNumPy
4788 import numpy as N
4790 #** numpy -> eman
4791 ema_arr = em.EMData()
4792 EMNumPy.numpy2em(num_arr, ema_arr)
4795 #** eman -> numpy
4796 import EMAN2
4797 ti = EMAN2.test_image(type = 1, size = (128,64))
4798 num_arr = EMNumPy.em2numpy(ti)
4799 print num_arr.shape
4800 # [Fri Mar 16 16:25:40 2007]: transposed: (64, 128)
4801 # [Mon Mar 19 11:23:08 2007]: eman2 fix by Grant Tang.
4802 # [Thu Mar 22 10:36:14 2007]: order confusion between eman2/hdf/numpy...
4805 #** numpy -> gtk
4806 #*** 1. get numpy array
4807 num_arr = N.random.ranf( (5,4) )
4809 #*** 2.b. numpy -> gtk
4810 # Convert float array to 8-bit array.
4811 rows, cols = num_arr.shape
4812 bottom = N.min(num_arr)
4813 top = N.max(num_arr)
4814 scale = (top - bottom)
4815 norm_arr = ((num_arr - bottom) / scale * 255).astype(N.uint8).reshape(rows*cols)
4818 #*** Use 3 8-bit arrays as (rgb) values.
4819 t3 = N.outer(norm_arr, N.array([1,1,1], 'b'))
4820 t4 = N.reshape(t3, (rows, cols, 3))
4821 pix_arr = N.array(t4, dtype=N.uint8)
4823 #*** Get the pixbuf.
4824 # array input indexing is row, pixel, [r,g,b {,a} ]
4825 # TypeError: pixbuf_new_from_array() argument 1 must be array, not
4826 # numpy.ndarray
4827 # pixbuf = gtk.gdk.pixbuf_new_from_array(pix_arr,
4828 # gtk.gdk.COLORSPACE_RGB, 8)
4829 pixbuf = gtk.gdk.pixbuf_new_from_data(
4830 pix_arr.tostring(order='a'),
4831 gtk.gdk.COLORSPACE_RGB,
4832 False,
4834 cols,
4835 rows,
4836 3*cols)
4838 #*** compare to input
4839 pixbuf.get_pixels_array()
4840 print pixbuf.get_pixels_array().shape
4841 print pixbuf.get_height()
4842 print pixbuf.get_width()
4845 #*** 3. display pixbuf as item
4846 d_canv = w_.canvas.view
4847 d_rt = d_canv.root().add(canvas.CanvasGroup)
4848 d_rt.add(canvas.CanvasPixbuf,
4849 x = 20, # world units
4850 y = 20,
4851 height_set = True,
4852 width_set = True,
4853 width = 15, # world units
4854 height = 15,
4855 pixbuf = pixbuf,
4857 d_rt.destroy()
4859 #* minimal EMData display test
4860 #** gui
4861 # paste:
4862 program:
4863 inline("import EMAN2 ")
4864 ti = EMAN2.test_image(type = 1, size = (128,64))
4865 # program > menu > run code
4866 # ti > menu > insert values
4869 #** console
4870 tm_tree = reader.parse('''
4871 inline("import EMAN2 ")
4872 ti = EMAN2.test_image(type = 0, size = (64,64))
4873 ''')
4874 tm_play_env = interp.get_child_env(def_env, w_.state_.storage)
4875 tm_tree.setup(empty_parent(), tm_play_env, w_.state_.storage)
4876 tm_l3pic = w_.canvas.view.start_add_l3tree(tm_tree)
4878 # run
4879 tm_l3pic._l3tree.interpret(w_.state_.def_env, w_.state_.storage)
4880 # insert list
4881 val, val_id = ast.val2ast(tm_l3pic.get_values_list())\
4882 .setup(empty_parent(),
4883 Env('dummy_env', None, None, storage),
4884 storage)
4885 w_.canvas.view.add_l3tree(val, RenderTable())
4887 gtk.main()
4889 #* l3IfOutline addition
4890 # High-level interface via outlining.
4891 #** original tree (simple)
4892 # Body from demo/proj-align/s1a.py, truncated for simplicity.
4893 ref_t = reader.parse_file("./demo/proj-align/s1a-in.py")
4894 ref_env = interp.get_child_env(def_env, storage)
4895 ref_t.setup(empty_parent(), ref_env, storage)
4896 view.print_info(storage, ref_t)
4898 # outline-regexp
4899 # (set-variable (quote outline-regexp) "[ ]*\\#+\\*+")
4900 # (set-variable (quote outline-regexp) "\\W+\" +" nil)
4902 #** OR: original tree (full)
4903 ref_t = reader.parse_file("./demo/proj-align/s1a.py")
4904 ref_env = interp.get_child_env(def_env, storage)
4905 ref_t.setup(empty_parent(), ref_env, storage)
4906 # view.print_info(storage, ref_t)
4907 #** add outline cross-refs
4908 ref_t.set_outl_edges(w_, None)
4909 #** examine embedded outline (text)
4910 view.print_info(storage, ref_t, custom = "nd._outl_parent._id")
4911 #** display outline
4912 ref_pic = w_.canvas.view.start_add_l3tree(ref_t)
4915 #** deprecated
4917 #*** outline tree, flat
4918 ref_o = ref_t.get_outline(w_)
4919 print ref_o
4921 ref_prun = ref_o.prune(w_)
4922 print ref_prun
4923 view.print_info(storage, ref_prun, custom = "nd._real_tree._id")
4926 #*** OR: outline tree, nested
4927 ref_o = ref_t.get_outline(w_, outl_type = 'nested')
4928 print ref_o
4929 view.print_info(storage, ref_o)
4931 ref_prun = ref_o.prune(w_)
4932 print ref_prun
4933 view.print_info(storage, ref_prun)
4935 #*** OR: outline tree, subtree
4936 # (nested outline to level N, then content)
4937 # Pick a nested outline manually.
4938 # here, 'Compute proj...'
4939 storage.load(40886).set_outline('subtree')
4941 ref_o = ref_t.get_outline(w_, outl_type = 'nested')
4942 # print ref_o
4943 # view.print_info(storage, ref_o)
4945 ref_prun = ref_o.prune(w_)
4946 # print ref_prun
4947 # view.print_info(storage, ref_prun)
4950 #*** display main
4951 ref_t_pic = w_.canvas.view.start_add_l3tree(ref_t)
4954 #*** display outline
4955 ref_pic = w_.canvas.view.start_add_l3tree(ref_prun)
4958 #** pattern
4959 tm_tree = reader.parse('''
4960 if ("outline", !! TITLE "sample"):
4961 BODY
4962 ''')
4963 print tm_tree
4964 #** single expression
4965 ref_s = '''
4966 ''' # "
4967 l3tree = reader.parse(ref_s)
4968 l3tree
4971 #** single nesting level
4972 code_s = """
4973 """ # "
4974 template = reader.parse(code_s)
4977 #** double nesting
4978 code_s = """
4979 """ # "
4980 template = reader.parse(code_s)
4984 #** viewlist tests
4985 vl, _ = viewList(aList([])).setup(empty_parent(), def_env, storage)
4986 sl, _ = List(aList([])).setup(empty_parent(), def_env, storage)
4988 vls = viewList().setup_viewlist(w_, ref_t)
4989 print vls
4992 #* list display tests
4993 #** no comments
5004 #** comments
5005 # Outer comment.
5010 # Inner comment.
5018 #* viewlist display tests
5019 program:
5020 # Very important
5021 # things are to be done...
5022 if ("outline", "Load volume."):
5023 vol = getImage(ev("$SPXROOT/sparx/test/model001.tcp"))
5024 # See http://www.macro-em.org/sparxwiki/prep_vol
5025 (volft, kb) = prep_vol(vol)
5027 # Like these wonderful ones...
5028 if ("outline", "Compute projection angles"):
5029 angles = even_angles(10.0, 90.0, 90.1, 0.0, 179.9,'P')
5030 nangles = len(angles)
5031 stack = "proj.hdf"
5032 sx = 0
5033 sy = 0
5035 #* system identification
5036 import sys
5037 sys.platform
5040 #* l3-system interface
5041 #** ShellCmds
5042 sc = shell_cmds
5043 ## sc = ShellCmds()
5044 sc.system('echo', ['a'], [])
5045 sc.system_wrap('echo', 'a', n='')
5046 #** subdir
5047 from l3lang.l3 import *
5048 run()
5049 # l3 input
5050 program:
5051 sd = subdir:
5052 a = 1
5053 touch('foo')
5054 pwd()
5056 #* system interface via shell wrapper
5057 #** prepare sample data directory, python
5058 import os
5059 ev = os.path.expandvars
5060 def ignore(cmd, *args, **kwd):
5061 try:
5062 cmd(*args, **kwd)
5063 except:
5064 pass
5066 ignore(os.mkdir, 'foo-sh-wrap')
5067 os.chdir('foo-sh-wrap')
5068 ignore(os.symlink,
5069 ev('$HOME/w/sparx/xmipp/Basic_Demo/walkthrough/taguA.tot'),
5070 'taguA.tot')
5071 ignore(os.symlink,
5072 ev('$HOME/w/sparx/xmipp/Basic_Demo/walkthrough/taguA.tot.inf'),
5073 'taguA.tot.inf')
5074 ignore(os.symlink,
5075 ev('$HOME/w/sparx/xmipp/Basic_Demo/walkthrough/tagtA.tot'),
5076 'tagtA.tot')
5077 ignore(os.symlink,
5078 ev('$HOME/w/sparx/xmipp/Basic_Demo/walkthrough/tagtA.tot.inf'),
5079 'tagtA.tot.inf')
5082 #** call using assumed return value structure
5083 # This is completely impractical; see ref[Mon Apr 16 14:35:34 2007]
5085 os.system('pwd; ls')
5086 (angles, coords, images, sel_tilt, sel_untilt) = xmipp_mark("taguA.tot")
5089 #** call in directory
5090 # This is a minimalistic approach, letting the user select directory
5091 # entries after execution.
5093 #*** using xmipp_mark found on system path
5094 os.system('pwd; ls -l')
5095 xmipp_mark(w_, "taguA.tot", tilted = "tagtA.tot")
5096 # In xmipp_mark:
5097 # menu -> save coords > tag.pos
5098 # menu -> save angles > tag.ang
5099 # menu -> generate images > images.xmp
5100 # > tilted.sel
5101 # > untilted.sel
5103 os.system('pwd; ls -l')
5106 #*** using l3lang.external module
5107 from l3lang.external import xmipp_wrap
5108 xmipp_wrap.xmipp_mark("/net/cci/hohn/w/sparx/xmipp/Basic_Demo/walkthrough/taguA.tot",
5109 tilted =
5110 "/net/cci/hohn/w/sparx/xmipp/Basic_Demo/walkthrough/tagtA.tot")
5112 #** with l3lang.external module
5113 #*** using subdirectory, l3
5114 # For batch programs, the toplevel should suffice:
5115 from l3lang.l3 import *
5116 run()
5118 # graphically:
5119 # l3 input, using manually specified full paths.
5120 program:
5121 inline "from l3lang.external.xmipp_wrap import *"
5122 sd = subdir:
5123 touch("test_file")
5124 touch("foobar")
5125 (status, log) = xmipp_mark(
5126 "/net/cci/hohn/w/sparx/xmipp/Basic_Demo/walkthrough/taguA.tot",
5127 tilted = "/net/cci/hohn/w/sparx/xmipp/Basic_Demo/walkthrough/tagtA.tot")
5129 print sd.foobar
5131 # Next, try commands using the file(s).
5132 sd1 = subdir:
5133 cp(sd.foobar, "foobar_copy")
5136 # value inspection, via local eval:
5137 # self.w_.state_.storage.load(self._l3tree._id)
5140 #*** directory only tests, l3
5141 # [Fri Apr 20 16:23:21 2007]
5142 program:
5143 inline "from l3lang.external.xmipp_wrap import *"
5144 sd = subdir:
5145 touch("test_file")
5146 touch("foobar")
5148 print sd.foobar
5150 # Next, try commands using the file(s).
5151 sd1 = subdir:
5152 cp(sd.foobar, "foobar_copy")
5154 print sd1.foobar_copy
5156 # [Thu Apr 19 15:32:15 2007]
5157 # state save sequence
5158 # (paste script)
5159 # (save state st.no-exec.1)
5160 # file size: 63992
5161 # (run script)
5162 # (save state st.1)
5163 # file size: 66351
5166 #* rpy test
5167 program:
5168 inline 'from rpy import *'
5170 d = r.capabilities()
5171 if d['png']:
5172 device = r.png # Robj, won't pickle
5173 ext='png'
5174 else:
5175 device = r.postscript
5176 ext='ps'
5178 faithful_data = r.faithful # short form:
5179 ed = faithful_data["eruptions"]
5182 #* numeric tests
5183 program:
5184 inline "import Numeric as N; import MLab as M"
5186 num_arr = M.rand(4,4) # Get numeric array.
5188 num_arr = M.rand(4,4)
5189 (rows, cols) = num_arr.shape
5190 bottom = M.min(M.min(num_arr))
5191 top = M.max(M.max(num_arr))
5192 scale = (top - bottom)
5193 norm_arr = ((num_arr - bottom) / scale * 255).astype('b') # Convert float array to 8-bit array.
5195 t3 = N.outerproduct(norm_arr, N.array([1,1,1], 'b'))
5196 t4 = N.reshape(t3, (rows, cols, 3))
5197 pix_arr = N.array(t4, 'b') # Use 3 8-bit arrays as (rgb) values.
5200 #* external test collection
5201 from EMAN2 import *
5202 e = EMData()
5203 e.set_size(3,3)
5204 e.set_value_at(0,1, 1.0)
5205 print "original at start"
5206 e.print_image()
5208 f1 = e.process("mirror",{"axis":'x'})
5209 print "new mirrored image"
5210 f1.print_image()
5213 #** image display test
5214 # [Fri Apr 20 16:48:25 2007]
5215 program:
5216 inline("import EMAN2 ")
5217 ti = EMAN2.test_image(type = 1, size = (128,64))
5218 # program > menu > run code
5219 # ti > menu > insert list of values l3
5222 #** Micrograph and particle selection
5223 # Using the xmipp demo and data.
5224 program:
5225 inline '''from l3lang.external.xmipp_wrap import \
5226 xmipp_mark, xmipp_do_selfile'''
5227 orig = subdir:
5228 cp('$HOME/w/sparx/xmipp/Basic_Demo/walkthrough/taguA.tot',
5229 'taguA.tot')
5230 cp('$HOME/w/sparx/xmipp/Basic_Demo/walkthrough/taguA.tot.Common.pos',
5231 'taguA.tot.Common.pos')
5232 cp('$HOME/w/sparx/xmipp/Basic_Demo/walkthrough/taguA.tot.inf',
5233 'taguA.tot.inf')
5234 cp('$HOME/w/sparx/xmipp/Basic_Demo/walkthrough/tagtA.tot',
5235 'tagtA.tot')
5236 cp('$HOME/w/sparx/xmipp/Basic_Demo/walkthrough/tagtA.tot.Common.pos',
5237 'tagtA.tot.Common.pos')
5238 cp('$HOME/w/sparx/xmipp/Basic_Demo/walkthrough/tagtA.tot.inf',
5239 'tagtA.tot.inf')
5241 mark = subdir:
5242 # Do the following in xmipp_mark:
5244 # file -> load coords
5245 # The file paths must be retrieved from l3, via
5246 # orig subdir > menu > pwd
5248 # file -> save angles
5250 # file -> generate images
5251 # 110 110 img_t
5252 # and
5253 # 110 110 img_u
5254 # Ignore the
5255 # 1551:SelFile::read: File
5256 # ..../Subdir-30214/taguA.tot.tot.Common.sel
5257 # not found
5258 # warning.
5259 (status, log) = xmipp_mark( orig.taguA_tot,
5260 tilted = orig.tagtA_tot )
5262 # Selection file listing images in current directory.
5263 xmipp_do_selfile("img_t*.xmp", "tilted.sel")
5264 xmipp_do_selfile("img_u*.xmp", "untilted.sel")
5266 # Selection file listing images relative to main directory.
5267 # A full xmipp wrapper would hide selection files completely.
5268 print (mark.l3_dirname() + "/" + "img_t*.xmp", "tilted.sel")
5269 xmipp_do_selfile(mark.l3_dirname() + "/" + "img_t*.xmp", "tilted.sel")
5270 xmipp_do_selfile(mark.l3_dirname() + "/" + "img_u*.xmp", "untilted.sel")
5272 # Note:
5273 # Even without any matching input files,
5274 # xmipp_do_selfile("img_u*.xmp", "untilted.sel")
5275 # returns status zero ...
5277 normalize
5278 xmipp_normalize
5280 mask
5281 xmipp_mask
5283 >> Need xmipp_show to view (intermediate) images.
5286 #** generate phantom
5287 inline "import l3lang.external.xmipp_wrap as xw"
5288 vol, fourpr = xw.pdbphantom("input-data/1P30.pdb",
5289 sampling_rate = 1.6,
5290 high_sampling_rate = 0.8,
5295 #** data alignment and classification
5296 align2d
5297 xmipp_align2d
5299 produce .doc file -- view as plain text.
5303 #** reconstruction
5304 reconstruct
5305 xmipp_art
5308 #** nested loop data selection
5309 # [Wed May 16 12:01:45 2007]
5310 program:
5311 if "loop":
5312 def lname(i):
5313 subdir:
5314 touch("value-file")
5315 if i > 3:
5316 return i
5317 if True:
5318 return lname(i + 1)
5319 lname(1)
5322 #* pretty-printer
5323 from l3lang.pretty import *
5325 #** manual construct / convert
5326 doc = DocText("hello") | DocBreak(" ") | DocText("world")
5328 doc.toString(40)
5329 doc.toString(4)
5332 #** construct / convert
5333 B = lambda : DocBreak(" ")
5334 T = lambda t : DocText(t)
5335 G = lambda x : DocGroupAuto(x)
5336 doc = G(T("begin") | B() |
5337 G(T("stmt1;") | B() |
5338 T("stmt2;") | B() |
5339 T("stmt3;")) | B() |
5340 T("end;"))
5342 print doc.toString(40)
5343 print doc.toString(8)
5344 print doc.toString(4)
5347 #** manual construct / convert
5348 B = lambda : DocBreak(" ")
5349 T = lambda t : DocText(t)
5350 G = lambda x : DocGroupAuto(x)
5351 GB = lambda x : DocGroupBreak(x)
5352 GF = lambda x : DocGroupFill(x)
5353 doc = G(T("begin") | B() |
5354 G(T("stmt1;") | B() |
5355 T("stmt3;"))
5356 | B() |
5357 T("end;"))
5359 print doc.toString(40) # no breaks
5360 print doc.toString(20) # break outer group
5361 print doc.toString(4) # break all
5364 #** manual construct w/ indentation
5365 B = lambda : DocBreak(" ")
5366 C = lambda x,y: DocCons(x, y)
5367 T = lambda t : DocText(t)
5368 G = lambda x : DocGroupAuto(x)
5369 GB = lambda x : DocGroupBreak(x)
5370 GF = lambda x : DocGroupFill(x)
5371 I = lambda r, x : DocNest(r, x)
5372 doc = G(T("begin") |
5373 I(4,
5374 B() |
5375 G( C(C(T("stmt1;"), B()), T("stmt3;"))))
5376 | B() |
5377 T("end;"))
5379 print doc.toString(40) # no breaks
5380 print doc.toString(20) # break outer group
5381 print doc.toString(4) # break all
5383 #* full-tree pretty printing
5384 #** manual comparison
5385 code_s = ('''
5386 a = 1
5387 def loop(a):
5388 if a < 3:
5389 print a
5390 loop(a + 1)
5391 loop(1)
5392 ''')
5393 tree = reader.parse(code_s)
5395 # Setup.
5396 play_env = interp.get_child_env(def_env, storage)
5397 tree.setup(empty_parent(), play_env, storage)
5398 # # view.print_info(storage, tree)
5399 print tree.get_infix_string(40)
5400 print tree.get_infix_string(10)
5403 #** print-parse-compare cycle function
5404 def comp_trees(otree, otree_tbl, pptree, pptree_tbl):
5405 # Compare trees and associated comments
5407 # Compare trees.
5408 print
5409 print "Check for differences..."
5410 print "--------------------------------------------"
5411 o_it = otree.top_down()
5412 pp_it = pptree.top_down()
5413 while 1:
5414 try: o_nd = o_it.next()
5415 except StopIteration: break
5416 pp_nd = pp_it.next()
5418 # Trees equal?
5419 if o_nd.eql_1(pp_nd):
5420 if otree_tbl.has_key(o_nd._id):
5421 if otree_tbl[o_nd._id].eql_1(pptree_tbl[pp_nd._id]):
5422 continue
5423 else:
5424 print ("Comment difference from line %d, col %d\n"
5425 " to line %d, col %d\n"
5426 % (o_nd._first_char[0], o_nd._first_char[1],
5427 pp_nd._first_char[0], pp_nd._first_char[1]))
5428 print otree_tbl[o_nd._id]
5429 print pptree_tbl[pp_nd._id]
5430 else:
5431 continue
5432 else:
5433 print ("Tree difference: line %d, col %d\n original:\n%s\n"
5434 " new:\n%s\n"
5436 (pp_nd._first_char[0],
5437 pp_nd._first_char[1],
5438 str(o_nd),
5439 str(pp_nd),
5442 def ppc(code_s, width):
5443 # Original tree.
5444 tree = reader.parse(code_s).single_program()
5445 tree_env = interp.get_child_env(def_env, storage)
5446 tree.setup(empty_parent(), tree_env, storage)
5447 associate_comments(w_, tree, code_s)
5449 # Tree from printed output.
5450 pp_string = tree.get_infix_string(width, w_.deco_table_)
5452 print "Pretty-printed string, width %d: " % width
5453 print "--------------------------------------------"
5454 print pp_string
5456 pp_tree = reader.parse(pp_string).single_program()
5457 pp_env = interp.get_child_env(def_env, storage)
5458 pp_tree.setup(empty_parent(), pp_env, storage)
5459 associate_comments(w_, pp_tree, pp_string)
5461 comp_trees(tree, w_.deco_table_, pp_tree, w_.deco_table_)
5463 def ppcm(code_s, width_list):
5464 for width in width_list:
5465 ppc(code_s, width)
5467 def chars(st):
5468 for ch in st:
5469 print '-%c-' % ch,
5473 #** string special case
5474 #*** repr / str behavior
5475 chars('a\nb')
5476 chars(r'a\nb')
5477 aa = reader.parse('"""a\nb"""')
5478 chars(aa.body()._primary[0])
5480 aa = reader.parse('"""Outer radius : %i\n"""')
5481 chars(aa.body()._primary[0])
5484 str(aa.body()._primary[0])
5485 # str(aa) converts unprintables into ascii
5486 # repr(aa) converts unprintables into ascii and escapes when needed
5488 # Other cases:
5489 repr('"""a"""')
5490 # '\'"""a"""\''
5492 repr("'''a'''")
5493 # '"\'\'\'a\'\'\'"'
5495 #*** tests
5496 ppc("""
5497 '''if 1:
5498 from rpy import *
5499 d = r.capabilities()
5500 if d['png']:
5501 device = r.png # Robj, won't pickle
5502 ext='png'
5503 else:
5504 device = r.postscript
5505 ext='ps'
5507 """, 40)
5512 #** def special case
5513 ppc('''
5514 def func(arg):
5515 body
5516 ''', 40)
5518 ppc('''
5519 def func(arg):
5520 body
5521 ''', 10)
5524 #** loop (set, function, operators)
5525 ppcm('''
5526 # Now for something else...
5527 # ... like a comment
5528 # ... across lines
5529 a = 1
5530 def loop(a):
5531 if a < 3:
5532 print a
5533 loop(a + 1)
5534 loop(1)
5535 ''', [40, 30, 10])
5539 #** program (program, subdir, tuple, comment)
5540 ppcm('''
5541 program:
5542 # with comment
5543 orig = subdir:
5544 add(a,b)
5545 ''', [50])
5548 ppcm('''
5549 program:
5550 orig = subdir:
5551 cp('$HOME/w/sparx/xmipp/Basic_Demo/walkthrough/taguA.tot',
5552 'taguA.tot')
5554 mark = subdir:
5555 inline "from l3lang.external.xmipp_wrap import xmipp_mark"
5556 # file -> load coords
5557 # The file paths must be retrieved from l3, via an Env
5558 # listing from the original subdir.
5560 (status, log) = xmipp_mark( orig.taguA_tot,
5561 tilted = orig.tagtA_tot )
5562 ''', [80, 70, 20])
5566 #** comment in expression
5567 ppcm('''
5568 [a,b,
5569 # the width
5572 ''', [70])
5575 #** outline
5576 ppcm('''
5577 "hello world"
5578 ''', [40])
5580 ppcm('''
5581 if ("outline", "outer 1"):
5582 if ("outline", "tools"):
5583 init_spider
5584 setup_directory
5585 plot
5587 if ("outline", "inner 2"):
5588 'nothing here'
5589 ''', [60])
5592 #* Local node examination.
5593 # From a widget with _l3tree
5594 st = self.w_.state_.storage
5595 st.load(self._l3tree._id)
5596 res = st.get_attribute(self._l3tree._id, "interp_result")
5598 #* ctf, emdata and numpy
5599 from sparx import *
5600 from EMAN2 import EMNumPy
5601 import numpy
5602 from numpy import flipud, fliplr
5603 import numpy as N
5605 #** conversion check (indep.)
5606 e1 = EMData()
5607 e1.set_size(3,4)
5608 e1.process_inplace('testimage.noise.uniform.rand')
5609 e1.get_data_pickle() # emdata -> std::vector -> python list
5614 #** conversion check (indep.)
5615 from sparx import *
5616 from EMAN2 import EMNumPy
5617 import numpy as N
5619 n1 = N.random.ranf( (5,4) )
5620 e1 = EMData(); EMNumPy.numpy2em(n1, e1)
5621 n2 = EMNumPy.em2numpy(e1)
5622 assert N.max(N.max(n2 - n1)) == 0, \
5623 "Array conversion failed (error = %g)." % N.max(N.max(n2 - n1))
5625 #** Get spider format image.
5626 image = getImage('foo.spi')
5628 #** Flip it to get correct layout.
5629 img_num = flipud(fliplr(EMNumPy.em2numpy(image)))
5630 img_em = EMData()
5631 EMNumPy.numpy2em(img_num, img_em)
5633 #*** Verify the conversion.
5634 num_2 = EMNumPy.em2numpy(img_em)
5635 assert N.max(N.max(num_2 - img_num)) == 0, "Array conversion failed."
5637 # Check a single element
5638 # Lots of matches:
5639 N.where( (img_num - img_em.get_value_at(0,0)) < 1e-14)
5640 # No matches. This means that the (0,1) element must be wrong,
5641 # not merely misplaced.
5642 N.where( (img_num - img_em.get_value_at(0,1)) < 1e-14)
5645 #** Get the ctf.
5646 ctf_img = filt_ctf(image, 2000, 2.0, 200,
5647 1.6, 0.1, -1, 100)
5649 #** Save the image with ctf applied.
5650 dropImage(ctf_img, "ctf.spi", itype = 's')
5652 #* numpy and pickle
5653 import numpy as N, pickle, types
5654 from pdb import pm
5655 print N.version.version
5656 if 1:
5657 # fails:
5658 aa = N.array([ 1, 2, 3, 4 ]);
5659 aa1 = aa.reshape
5660 print type(aa1) == types.BuiltinFunctionType
5661 pickle.dumps(aa1)
5662 if 2:
5663 # works:
5664 pickle.dumps(N.reshape)
5667 #* Numeric and pickle
5668 import Numeric as N, pickle
5669 from pdb import pm
5670 if 1:
5671 # fails:
5672 aa = N.array([ 1, 2, 3, 4 ]);
5673 aa1 = aa.resize
5674 print type(aa1) == types.BuiltinFunctionType
5675 pickle.dumps(aa1)
5676 if 2:
5677 # works
5678 pickle.dumps(N.reshape)
5680 #* outline traversal
5681 # print the outline
5682 for (nd, level) in w_.lib_tree.body().outl_top_down():
5683 print " " * level, level, nd.l_label
5685 #* timed run
5686 def time_func(fun, *args, **kwd):
5687 from time import time
5688 start = time()
5689 fun(*args, **kwd)
5690 print time() - start
5692 #* eman2 pickle tests
5693 # - Works when run in one session.
5694 # - Works across sessions.
5695 import sparx as sx, l3lang.utils as ut
5696 volume = sx.getImage( "model.tcp" )
5697 print volume.get_xsize() * volume.get_ysize() * volume.get_zsize()
5699 image = sx.project(volume, [10.0, 20.0, 30.0, 0.0, 0.0], 35)
5700 print image.get_xsize()
5702 # Image.
5703 time_func(cPickle.dumps, image, protocol = 2)
5705 ut.file_pickle(image, 'st.1')
5706 image_ld = ut.file_unpickle('st.1')
5709 # Volume.
5710 import cPickle
5711 # st = cPickle.dumps(volume, protocol=2)
5712 time_func(cPickle.dumps, volume, protocol=2)
5714 time_func(sx.dropImage, volume, "st.spi", itype = 's')
5716 ut.file_pickle(volume, 'st.2')
5717 # Load in separate session.
5718 import sparx as sx, l3lang.utils as ut
5719 volume_ld = ut.file_unpickle('st.2')
5721 vol_st = volume_ld.__getstate__()
5723 #* Set() special form: aa[ii] = rhs
5724 #** single-index pattern derivation
5725 aa = reader.parse("aa[i] = foo").body()
5727 # Set(Call(Member(Symbol('operator'),
5728 # Symbol('getitem')),
5729 # aList([Symbol('a'),
5730 # Symbol('i')])),
5731 # Symbol('foo'))
5733 aa = reader.parse("! AA[ ! II ] = ! RHS").body()
5735 Set(Call(Member(Symbol('operator'),
5736 Symbol('getitem')),
5737 aList([Marker('AA'),
5738 Marker('II')])),
5739 Marker('RHS'))
5741 #** transform to `operator.setitem(aa, ii, rhs)`
5742 print reader.parse('operator.setitem(!AA, !II, !RHS)').body()
5744 Call(Member(Symbol('operator'),
5745 Symbol('setitem')),
5746 aList([Marker('AA'),
5747 Marker('II'),
5748 Marker('RHS')]))
5751 #** numeric: single-bracket, multi-index
5752 import Numeric as N
5753 aa = N.reshape(N.array([ 1, 2, 3, 4 ]), (2,2))
5754 print aa[0,1]
5756 # Syntax error...
5757 print reader.parse("aa[0,1]").body()
5759 # Parses:
5760 print reader.parse("aa[(0,1)]").body()
5761 # long form works:
5762 print operator.getitem(aa, (0,1))
5764 # Call(Member(Symbol('operator'),
5765 # Symbol('getitem')),
5766 # aList([Symbol('aa'),
5767 # Tuple(aList([Int(0),
5768 # Int(1)]))]))
5770 # So the single-index pattern will work.
5772 #** tests
5773 print reader.parse("aa[ii] = foo").body()
5774 program:
5775 # Circumvents l3 value tracking.
5776 aa = [0,1,2]
5777 aa[0] = 4
5781 #* Set() special form: aa[ii][jj] = rhs
5782 #** Original in
5783 aa = reader.parse("! AA[ ! II ] [ !JJ ] = ! RHS").body()
5785 Set(Call(Member(Symbol('operator'),
5786 Symbol('getitem')),
5787 aList([Call(Member(Symbol('operator'),
5788 Symbol('getitem')),
5789 aList([Marker('AA'),
5790 Marker('II')])),
5791 Marker('JJ')])),
5792 Marker('RHS'))
5795 #** Transformed out.
5796 print reader.parse('''
5797 operator.setitem(
5798 operator.getitem(!AA, !II),
5799 !JJ,
5800 !RHS)''').body()
5801 Call(Member(Symbol('operator'),
5802 Symbol('setitem')),
5803 aList([Call(Member(Symbol('operator'),
5804 Symbol('getitem')),
5805 aList([Marker('AA'),
5806 Marker('II')])),
5807 Marker('JJ'),
5808 Marker('RHS')]))
5810 #** tests
5811 print reader.parse("aa[ii][jj] = foo").body()
5812 program:
5813 # Circumvents l3 value tracking.
5814 aa = [[0,1], [2,3]]
5815 aa[0]
5816 aa[1][0] = 10
5820 #* Set special form: a.foo = 2
5821 # Still uses setitem.
5822 aa = utils.Shared()
5823 aa.foo = 1
5824 operator.setitem(aa, 'foo', 2)
5825 print aa.foo
5827 #* string parsing
5828 #** n behavior, manual eval
5829 # This lets python convert the \n to an escape:
5830 aa = reader.parse('"""Outer radius : %i\n""" % 123')
5831 chars(aa.body()._primary[1][0]._primary[0])
5833 # Setup.
5834 play_env = interp.get_child_env(def_env, storage)
5835 aa.setup(empty_parent(), play_env, storage)
5836 view.print_info(storage, aa)
5838 # Interpret.
5839 news = aa.interpret(play_env, storage)
5840 chars(news[0]) # has actual newline
5841 #** n behavior, pasting
5842 """Outer radius : %i\n""" % 123
5843 # run code => ('Outer radius : 123\\n', 5496)
5844 #** compare
5845 manual eval:
5846 45065 -22 45065 String('Outer radius : %i\n')
5848 l3.py:
5849 >>> print """Outer radius : %i\n""" % 123
5850 Outer radius : 123\n
5852 pasting:
5853 45072 5494 45072 String('Outer radius : %i\\n')
5855 #** difference, commit c2c6cff9496972d40011002de24d20abca1a6bab and before
5857 # This lets python convert the \n to an escape:
5858 aa = reader.parse('"""Outer radius : %i\n""" % 123')
5860 # This won't, and that's the behavior observed from the gui / toplevel.
5861 aa = reader.parse(r'"""Outer radius : %i\n""" % 123')
5863 # This also contains \\n instead of the control code.
5864 pprint.pprint([ii for ii in tokenize.generate_tokens(string_readline(r'"Outer radius : %i\n"'))])
5867 #** difference removed
5868 # both contain \n control code.
5869 print reader.parse('"""Outer radius : %i\n""" % 123')
5871 # This won't, and that's the behavior observed from the gui / toplevel.
5872 print reader.parse(r'"""Outer radius : %i\n""" % 123')
5875 #* pytables for reading hdf
5876 #** raw experiments
5877 import tables
5878 # File is available after ali2d.l3 run
5879 fi = tables.openFile("_K-8-133679/average.hdf")
5880 # method listNodes in module tables.file:
5881 fi.listNodes # show all
5882 [li for li in fi.listNodes('/MDF/images')]
5884 # RootGroup in module tables.group object:
5885 fi.getNode('/')
5887 fi.root == fi.getNode('/') # true
5889 for node in fi.root:
5890 print node
5892 list(fi.root) # list from iterator
5893 list(fi.root)[0] # first child
5894 list(list(fi.root)[0]) # first child's children
5898 fi.listNodes('/') # get list
5899 (fi.listNodes('/')[0])._v_name
5901 for sub in fi.listNodes('/'):
5902 print sub._v_name
5904 # caution:
5905 # >>> fi.getNode('/')
5906 # / (RootGroup) ''
5907 # children := ['MDF' (Group)]
5908 # >>> fi.getNode('/').getNode('MDF')
5909 # Segmentation fault
5911 fi.getNode('/MDF/images')
5912 fi.getNode('/MDF/images')._v_attrs # print
5913 fi.getNode('/MDF/images')._v_attrs._g_listAttr() # get list
5915 fi.getNode('/MDF/images')._f_getChild('1')
5917 fi.getNode('/MDF/images/1')
5918 # tables.attributeset.AttributeSet'>
5919 fi.getNode('/MDF/images/1')._v_attrs # print info
5920 fi.getNode('/MDF/images/1')._v_attrs._g_listAttr() # get list
5921 (fi.getNode('/MDF/images/1')._v_attrs )._f_list() # get list (better)
5922 fi.getNode('/MDF/images/1')._v_attrs._g_getAttr('EMAN.Class_average')
5923 fi.getNode('/MDF/images/1')._v_attrs._g_getAttr('EMAN.nobjects')
5924 fi.getNode('/MDF/images/1')._v_attrs._g_getAttr('EMAN.members')
5927 # Array in module tables.array
5928 arr = fi.getNode('/MDF/images')._f_getChild('1')._f_getChild('image')
5929 arr = fi.getNode('/MDF/images/1/image')
5930 arr.shape
5931 a00 = arr[0,0] # Returns a 0-D array
5932 a00 = arr[0:1,0:1] # Returns a slice copy.
5935 #** relevant for eman2 use
5937 # 1. The pytable classes:
5939 # / <class 'tables.group.RootGroup'>
5940 # MDF <class 'tables.group.Group'>
5941 # images <class 'tables.group.Group'>
5942 # 0 <class 'tables.group.Group'>
5943 # image <class 'tables.array.Array'>
5944 # 1 <class 'tables.group.Group'>
5945 # image <class 'tables.array.Array'>
5947 # 2. The attributes of a Group
5948 # ... members in a Group start with some reserved prefix, like _f_
5949 # (for public methods), _g_ (for private ones), _v_ (for instance
5950 # variables) or _c_ (for class variables).
5952 # tables.attributeset.AttributeSet'>
5953 # fi.getNode('/MDF/images/1')._v_attrs # print info
5955 # Get list of attributes.
5956 (fi.getNode('/MDF/images/1')._v_attrs)._f_list()
5958 # Get attribute with arbitrary name
5959 (fi.getNode('/MDF/images/1')._v_attrs).__getattr__('EMAN.Class_average')
5960 (fi.getNode('/MDF/images/1')._v_attrs).__getattr__('EMAN.members')
5962 # Set attribute:
5963 (fi.getNode('/MDF/images/1')._v_attrs ).new_stuff = 1
5965 # got tables.exceptions.FileModeError: the file is not writable
5968 #** tools
5969 #*** Traverse the object tree
5970 def nshow(fi, indent = 0):
5971 if hasattr(fi, '_v_name'):
5972 print " "*indent, fi._v_name, type(fi)
5973 if isinstance(fi, tables.array.Array):
5974 # For tables.array, iteration is over the rows; this shows
5975 # 75 1-d numpy arrays per image.
5976 return
5977 for sub in fi:
5978 nshow(sub, indent = indent + 1)
5979 else:
5980 print " "*indent, type(fi)
5981 nshow(fi.root)
5984 #*** simplified access interface for l3 use
5985 # Inject l3_* methods.
5986 def l3_len(self):
5987 'Return the number of images.'
5988 return self.getNode('/MDF/images')._v_nchildren
5989 tables.file.File.l3_len = l3_len
5992 def l3_getimg(self, index):
5993 'Return the image at `index`.'
5994 return self.getNode('/MDF/images')._f_getChild(str(index))._f_getChild('image')
5995 tables.file.File.l3_getimg = l3_getimg
5998 def l3_getatts(self, index):
5999 ''' Return a (name, value) list holding all the attributes of
6000 image `index`.
6002 ind = str(index)
6003 attset = self.root.MDF.images._f_getChild(ind)._v_attrs
6004 return [ (att, attset.__getattr__(att)) for att in attset._f_list()]
6005 tables.file.File.l3_getatts = l3_getatts
6008 def l3_getatt(self, index, name):
6009 ''' Return the `name` attribute of the image at `index`.'''
6010 ind = str(index)
6011 attset = self.root.MDF.images._f_getChild(ind)._v_attrs
6012 return attset.__getattr__(str(name))
6013 tables.file.File.l3_getatt = l3_getatt
6017 #*** tables access tests
6018 fi.l3_len()
6019 fi.l3_getimg(0)
6020 fi.l3_getatts(0)
6021 fi.l3_getatt(0, 'EMAN.Class_average')
6022 fi.l3_getatt(0, 'EMAN.nobjects')
6023 fi.l3_getatt(0, 'EMAN.members')
6027 #* nested main loop
6028 # Using the simple function
6029 if 1:
6030 def flush_events():
6031 # Updates bounding boxes, among others.
6032 while gtk.events_pending():
6033 gtk.main_iteration()
6035 # a loop could be
6036 if 1:
6037 while True:
6038 draw()
6039 flush_events()
6041 # as long as draw() does not block.
6043 import thread
6044 from threading import Lock
6045 import time
6046 lck = Lock()
6047 value = []
6048 def _do():
6049 try:
6050 value.append(raw_input(prompt))
6051 except EOFError:
6052 value.append(EOFError())
6053 lck.release()
6054 lck.acquire()
6055 thread.start_new_thread(_do, ())
6056 # Manual main loop until input is ready.
6057 while not lck.acquire(False):
6058 self._do_event()
6059 time.sleep(0.01)
6060 if isinstance(value[-1], Exception):
6061 raise value[-1]
6062 return value[-1]
6064 #* gui/console hybrid use
6065 #** highly irregular data, not used
6066 inline('from numpy import *; from pylab import *')
6067 for ii in range(12*3):
6068 a1 = (ii, array([-2100, -200, -100 / 2.0, -1200]))
6069 # Added to loop body.
6070 a1[0:1]
6071 a1[0]
6072 a2 = (ii, sum(a1[1]))
6073 (ii % 2 == 0) and -200 or 0
6075 # This is just too painful through the gui; there is too much
6076 # heterogeneous data.
6077 #** highly irregular data, python
6078 # Just defining and editing the textual data structure is FAR
6079 # superior. E.g.
6080 # (month, amount, what)
6081 specials = [(1, [(-200, "this")]),
6082 (22, [(300, "that")]),
6084 regular = [(mon, [(-100, "one"),
6085 (-200, "two"),
6086 ]) for mon in range(0, 12*3)]
6087 spec_d = dict(specials)
6088 # for (month, mon_dat) in regular:
6089 # print (month, mon_dat + spec_d.get(month, []))
6090 # Shorter:
6091 [ (month, mon_dat + spec_d.get(month, []))
6092 for (month, mon_dat) in regular]
6095 #** highly irregular data, l3
6096 inline('''
6097 def combine(specials, regular, r_from, r_to):
6098 reg_list = [(mon, regular) for mon in xrange(r_from, r_to)]
6099 spec_d = dict(specials)
6100 return [ (month, mon_dat + spec_d.get(month, []))
6101 for (month, mon_dat) in reg_list]
6102 ''')
6104 specials = [(1, [(-200, "this")]),
6105 (22, [(300, "that")]),
6107 regular = [(-100, "one"),
6108 (-200, "two"),
6110 combined = combine(specials, regular, 0, 3*12)
6113 #** embed in l3?
6114 # if Env's keys include any python hashables, an Env can accumulate
6115 # (int, val) bindings in addition to (name, val) bindings
6116 stuff = if ("map", "embedded"):
6117 for ii in range(0, 3*12):
6118 l3.env().set(ii, [(-100, "one"), (-200, "two")])
6120 l3set(1, [(-200, "this")])
6121 l3set(22, [(300, "that")])
6125 #* list comprehension
6126 [(mon, [(-100, "one"),
6127 (-200, "two"),
6128 ]) for mon in range(0, 12*3)]
6129 # As tail call, roughly:
6130 def _foo(arg, seq, accum):
6131 accum.append()
6132 if more(seq):
6133 _foo(arg, seq[1:None], accum)
6134 _foo(mon, range(0, 12*3), [])
6135 # this is already FAR to tedious, and a tracked (l3) expansion is not
6136 # desirable anyway.
6138 #* l3 script pieces
6139 # From matplotlib-0.90.1/examples/date_demo1.py
6141 # This example requires an active internet connection since it uses
6142 # yahoo finance to get the data for plotting
6144 # The scripts are independently runnable, and must be edited to form a
6145 # single program.
6148 #** Collect finance data using yahoo's service.
6149 if ("outline", "Collect finance data"):
6150 inline """if 1:
6151 from matplotlib.finance import quotes_historical_yahoo
6152 import datetime
6154 # <span background="#ffdddd">Retrieve data for stock symbol.</span>
6155 quotes = quotes_historical_yahoo(
6156 'INTC',
6157 datetime.date( 1995, 1, 1 ),
6158 datetime.date( 2004, 4, 12 ),
6159 asobject = True)
6160 if not quotes:
6161 error("Unable to get data")
6162 else:
6163 # <span background="#ffdddd">The data attributes of quotes.</span>
6164 quotes.date
6165 quotes.open
6166 quotes.close
6167 quotes.high
6168 quotes.low
6169 quotes.volume
6174 #** Date plot.
6175 if ("outline", "Date plot"):
6176 # <span background="#ffdddd">Plot details.</span>
6178 inline """if 1:
6179 import datetime
6181 def plot_date(fname, dates, values):
6182 from pylab import figure, show
6183 import datetime
6184 from matplotlib.dates import YearLocator, MonthLocator, DateFormatter
6186 fig = figure(figsize=(7,4), dpi=100)
6187 ax = fig.add_subplot(111)
6188 ax.plot_date(dates, values, '-')
6190 # Plot details -- ticks.
6191 years = YearLocator() # every year
6192 months = MonthLocator() # every month
6193 yearsFmt = DateFormatter('%Y')
6195 ax.xaxis.set_major_locator(years)
6196 ax.xaxis.set_major_formatter(yearsFmt)
6197 ax.xaxis.set_minor_locator(months)
6198 ax.autoscale_view()
6200 for label in ax.get_xticklabels() + ax.get_yticklabels():
6201 label.set_fontsize('8')
6203 # Plot details -- coords. message box.
6204 def price(x):
6205 return '$%1.2f' % x
6206 ax.fmt_xdata = DateFormatter('%Y-%m-%d')
6207 ax.fmt_ydata = price
6208 ax.grid(True)
6210 fig.savefig(fname)
6211 return fname
6213 # <span background="#ffdddd">Plot data.</span>
6214 filename = plot_date('_plot-%d.png' % new_id(),
6215 [datetime.date(1995,1,1).toordinal(),
6216 datetime.date(1997,1,1).toordinal()],
6217 [1, 2])
6219 #** Moving average
6220 if ("outline", "Moving average."):
6221 inline """if 1:
6222 import numpy
6223 def movavg(vec, n):
6224 # A[t] = (V[t] + V[t-1] + ... + V[ t-n+1 ]) / n
6225 # t = N .. N-n
6226 import numpy
6227 assert len(vec.shape) == 1, 'Expecting vector argument.'
6228 assert n > 1, 'Averaging over less than 2 elements.'
6229 (N, ) = vec.shape
6230 assert N > n, 'Not enough samples for averaging.'
6232 ma = copy(vec) * numpy.nan
6233 for t in xrange(n - 1, N):
6234 ma[t] = 1.0 * sum(vec[t-n+1: t + 1]) / n
6235 return ma
6237 mov_avg = movavg(numpy.array([1, 2, 3, 4.0]),
6240 #** Moving average via convolution
6241 # n = 2
6242 data = numpy.array([1.0, 2,3,4])
6243 weight = numpy.array([1.0/2, 1.0/2])
6244 mov_avg = numpy.convolve(data, weight, mode='same')
6245 mov_avg[0:weight.shape[0] - 1] = numpy.nan
6248 #** Moving average, simple plot
6249 import matplotlib
6250 matplotlib.use("gtk")
6252 from numpy import *
6253 from pylab import *
6255 # n = 2
6256 data = [1.0, 2,3,4]
6257 weight = [1.0/2, 1.0/2]
6258 mov_avg = convolve(data, weight, mode='same')
6259 mov_avg[0:len(weight) - 1] = nan
6261 t1 = plot(mov_avg) # t1 won't pickle.
6262 show(mainloop=False)
6263 show()
6266 #** Date plot as widget
6267 if ("outline", "Date plot"):
6268 # <span background="#ffdddd">Plot details.</span>
6270 inline """if 1:
6271 import datetime
6273 def plot_date(dates, values):
6274 from pylab import figure, show
6275 import datetime
6276 from matplotlib.dates import YearLocator, MonthLocator, DateFormatter
6277 from matplotlib.backends.backend_gtk import FigureCanvasGTK \
6278 as FigureCanvas
6280 fig = figure(figsize=(7,4), dpi=100)
6281 ax = fig.add_subplot(111)
6282 ax.plot_date(dates, values, '-')
6284 # Plot details -- ticks.
6285 years = YearLocator() # every year
6286 months = MonthLocator() # every month
6287 yearsFmt = DateFormatter('%Y')
6289 ax.xaxis.set_major_locator(years)
6290 ax.xaxis.set_major_formatter(yearsFmt)
6291 ax.xaxis.set_minor_locator(months)
6292 ax.autoscale_view()
6294 for label in ax.get_xticklabels() + ax.get_yticklabels():
6295 label.set_fontsize('8')
6297 # Plot details -- coords. message box.
6298 def price(x):
6299 return '$%1.2f' % x
6300 ax.fmt_xdata = DateFormatter('%Y-%m-%d')
6301 ax.fmt_ydata = price
6302 ax.grid(True)
6303 return fig
6305 # <span background="#ffdddd">Plot data.</span>
6306 plot_date([datetime.date(1995,1,1).toordinal(),
6307 datetime.date(1997,1,1).toordinal()],
6308 [1, 2])
6310 # The display widget is FigureCanvas(fig). But canvas operations will
6311 # destroy this occasionally so the raw figure handle is returned and
6312 # L3 can display Native( fig : <matplotlib.figure.Figure> )
6313 # via Widget(FigureCanvas(fig)).
6316 #* single-run mode
6317 def inline(str):
6318 exec(str, globals())
6320 inline("import matplotlib as M")
6321 inline( "import pylab as P")
6322 M.use('Agg')
6323 fig = P.figure(figsize=(5,4), dpi=75)
6326 #** doc strings as help entries
6327 from types import InstanceType, MethodType
6328 import pydoc
6329 if isinstance(fig, InstanceType):
6330 helpfor = fig.__class__
6331 # All member functions of this class.
6332 for (name, memb) in pydoc.allmethods(helpfor).iteritems():
6333 _docstr = pydoc.getdoc(memb)
6334 head, body = pydoc.splitdoc(_docstr)
6335 print "\f=================================\n", name
6336 print head
6337 print "--------------------------------------------\n", body
6338 else:
6339 helpfor = fig
6345 # # Useless:
6346 help(fig)
6348 # # Help on instance of Figure in module matplotlib.figure object:
6349 # # class instance(object)
6350 # # ...
6351 # # Good, but a LOT of content:
6352 help( fig.__class__)
6356 #* matplotlib figures on l3 canvas (XY Plot script)
6359 #** Another numpy stupidity
6360 import matplotlib as M
6361 import pylab as P
6362 N = 5
6363 nn = P.arange(0, N)
6364 # Gives TypeError: only length-1 arrays can be converted to Python scalars
6365 xx = sin(2* nn* pi/N)
6366 # Works
6367 xx = P.sin(2* nn* pi/N)
6370 #** l3 polygon plots
6371 if ("outline", "XY Polygon Plot"):
6372 # <span background="#ffcccc">Plot details.</span>
6373 inline '''if 1:
6374 def plot_(fname, x, y):
6375 import matplotlib as M
6376 import pylab as P
6377 M.use('Agg')
6379 fig = P.figure(figsize=(5,4), dpi=75)
6380 ax = fig.add_subplot(111)
6381 ax.plot(x, y, '-', lw=2)
6383 ax.set_xlabel('')
6384 ax.set_ylabel('')
6385 ax.set_title('Polygon plot')
6386 ax.grid(True)
6388 fig.savefig(fname)
6389 return fname
6391 inline 'from pylab import *'
6392 for N in range(3, 12):
6393 nn = arange(0, N + 1)
6394 xx = sin(2* nn* pi/N)
6395 yy = cos(2* nn* pi/N)
6396 plot_(new_name('poly', '.png'), xx, yy)
6401 #** python
6402 import matplotlib as M
6403 import pylab as P
6405 def plot_xy(fname, x, y):
6406 M.use('Agg')
6407 plot = P.plot(x, y, '-', lw=2)
6409 # P.xlabel('time (s)')
6410 # P.ylabel('voltage (mV)')
6411 # P.title('About as simple as it gets, folks')
6412 # P.grid(True)
6414 P.savefig(fname)
6416 plot_xy('simple_plot.png',
6417 P.arange(0.0, 1.0+0.01, 0.01),
6418 P.cos(2*2*pi*t)
6421 import cPickle
6422 st = cPickle.dumps(plot, protocol=2)
6423 # UnpickleableError: Cannot pickle <type 'Affine'> objects
6426 #** l3 calling python
6427 inline '''
6428 def plot_(fname, x, y):
6429 import matplotlib as M
6430 import pylab as P
6431 M.use('Agg')
6433 plot = P.plot(x, y, '-', lw=2)
6435 # P.xlabel('time (s)')
6436 # P.ylabel('voltage (mV)')
6437 # P.title('About as simple as it gets, folks')
6438 # P.grid(True)
6440 P.savefig(fname)
6442 inline 'from math import pi; import matplotlib as M; import pylab as P'
6443 t = P.arange(0.0, 1.0+0.01, 0.01)
6444 s = P.cos(2 * pi * t)
6445 plot_('simple_plot.png', s, t)
6448 # Python image read (load) test
6449 import matplotlib.image as mi
6450 import numpy as N
6451 img_arr = mi.imread('simple_plot.png')
6454 #** l3 only
6455 def plot_(fname, x, y):
6456 inline('import matplotlib as M; import pylab as P')
6457 M.use('Agg')
6458 plot = P.plot(x, y, '-', lw=2)
6460 # P.xlabel('time (s)')
6461 # P.ylabel('voltage (mV)')
6462 # P.title('About as simple as it gets, folks')
6463 # P.grid(True)
6465 P.savefig(fname)
6466 inline 'from math import pi; import matplotlib as M; import pylab as P'
6467 t = P.arange(0.0, 1.0+0.01, 0.01)
6468 s = P.cos(2 * pi * t)
6469 plot_('simple_plot.png', s, t)
6473 #** l3 only, with axis
6474 # <span background="#ffcccc">Plot details.</span>
6475 def plot_(fname, x, y):
6476 inline('import matplotlib as M; import pylab as P')
6477 M.use('Agg')
6479 fig = P.figure(figsize=(5,4), dpi=75)
6480 ax = fig.add_subplot(111)
6481 ax.plot(x, y, '-', lw=2)
6483 ax.set_xlabel('time (s)')
6484 ax.set_ylabel('voltage (mV)')
6485 ax.set_title('About as simple as it gets, folks')
6486 ax.grid(True)
6488 fig.savefig(fname)
6489 return fname
6490 inline 'from math import pi; import pylab as P'
6491 # <span background="#ffdddd">Plot data.</span>
6492 x = P.arange(0.0, 1.0+0.01, 0.01)
6493 y = P.cos(2 * pi * x)
6494 # <span background="#ffdddd">Plot file.</span>
6495 filename = plot_('_plot-%d.png' % new_id(), x, y)
6499 #** l3 with explicit figure in subplot
6500 # Plot details can be adjusted here.
6501 inline '''
6502 def plot_(fname, x, y):
6503 import matplotlib as M
6504 import pylab as P
6505 M.use('Agg')
6507 fig = P.figure(figsize=(5,4), dpi=75)
6508 ax = fig.add_subplot(111)
6509 ax.plot(x, y, '-', lw=2)
6511 ax.set_xlabel('time (s)')
6512 ax.set_ylabel('voltage (mV)')
6513 ax.set_title('About as simple as it gets, folks')
6514 ax.grid(True)
6516 fig.savefig(fname)
6518 inline 'from math import pi; import matplotlib as M; import pylab as P'
6519 x = t = P.arange(0.0, 1.0+0.01, 0.01)
6520 y = s = P.cos(2 * pi * t)
6521 # Insert plot data here.
6522 plot_('simple_plot.png', s, t)
6526 #** Plot without intermediate data file
6527 # from .../matplotlib-0.90.1/examples/to_numeric.py
6530 #** Interactive matplotlib window combined with l3gui
6531 import matplotlib as M
6532 import pylab as P
6534 from matplotlib.axes import Subplot
6535 from matplotlib.figure import Figure
6536 from matplotlib.numerix import arange, sin, pi
6538 antialias = False
6539 if antialias:
6540 from matplotlib.backends.backend_gtkagg import \
6541 FigureCanvasGTKAgg as FigureCanvas
6542 from matplotlib.backends.backend_gtkagg import \
6543 NavigationToolbar2GTKAgg as NavigationToolbar
6544 else:
6545 from matplotlib.backends.backend_gtk import \
6546 FigureCanvasGTK as FigureCanvas
6548 from matplotlib.backends.backend_gtk import \
6549 NavigationToolbar2GTK as NavigationToolbar
6551 def plot_figure():
6552 fig = Figure(figsize=(5,4), dpi=100)
6553 ax = fig.add_subplot(111)
6554 t = arange(0.0,3.0,0.01)
6555 s = sin(2*pi*t)
6556 ax.plot(t,s)
6557 ax.set_xlabel('time (s)')
6558 ax.set_ylabel('voltage (mV)')
6559 ax.set_title('About as simple as it gets, folks')
6560 ax.grid(True)
6561 return (ax, fig)
6564 def plot_win(figure):
6565 win = gtk.Window()
6566 # win.connect("destroy", lambda x: gtk.main_quit())
6567 win.set_default_size(400,300)
6568 win.set_title("Interactive plot")
6570 vbox = gtk.VBox()
6571 win.add(vbox)
6573 canvas = FigureCanvas(figure)
6574 toolbar = NavigationToolbar(canvas, win)
6575 vbox.pack_start(canvas)
6576 vbox.pack_start(toolbar, False, False)
6578 win.show_all()
6579 # gtk.main()
6580 return win
6582 # Everything is done through the figure and axis (good), but these
6583 # don't pickle.
6584 (axis, figure) = plot_figure()
6585 figure.savefig('testimage.png') # Also available via plot window.
6586 plot_win(figure)
6588 gtk.main() # test only.
6592 xx = P.arange(0.0, 1.0+0.01, 0.01)
6593 yy = P.cos(2*2*pi*xx)
6594 fname = 'simple_plot.png'
6595 if 1:
6596 M.use('GTKAgg')
6597 plot = P.plot(xx, yy, '-', lw=2)
6598 P.xlabel('time (s)')
6599 P.ylabel('voltage (mV)')
6600 P.title('About as simple as it gets, folks')
6601 P.grid(True)
6602 P.savefig(fname)
6606 #* empy macro tests
6607 #** simplest expansion
6608 from l3lang.cruft import em
6609 glo = {}
6610 em.expand(
6611 '@[def emph(str)]<span background="#ffcccc"> @str </span>@[end def]', glo)
6612 print em.expand("@emph('the comment')", glo)
6614 #** Single em interpreter, faster.
6615 from l3lang.cruft import em
6616 em_inter = em.Interpreter()
6617 em_glo = {}
6618 em_inter.expand(
6619 '@[def emph(str)]<span background="#ffcccc"> @str </span>@[end def]'
6620 '@[def strong(str)]<b> @str </b>@[end def]'
6621 , em_glo)
6622 def emx(str):
6623 print em_inter.expand(str, em_glo)
6625 #*** Expand one time:
6626 emx("@emph('the comment')")
6627 emx("@strong('bold')")
6628 emx("""@emph('the @strong("bold") comment')""")
6631 #*** Nested expansion:
6632 emx("""@emph{the @strong("bold") comment}""")
6634 #*** Ordering in nested expansions.
6635 # In this, em_l inside def gives
6636 # NameError: global name 'em_l' is not defined
6637 emx('''
6638 @{em_l = 0}
6639 @[def emph(str)]
6640 @{global em_l;
6641 em_l += 1}
6642 @[if em_l % 2]
6643 <span background="EVEN">
6644 @str
6645 </span>
6646 @[else]
6647 <span background="ODD">
6648 @str
6649 </span>
6650 @[end if]
6651 @{em_l -= 1}
6652 @[end def]
6653 ''')
6655 emx("@em_l")
6656 emx("No emphasis @emph{some emphasis @emph{nested emphasis} some emphasis}")
6659 #*** Ordering in nested expansions, 2
6660 # The following cannot work; the em_l state has to be decreased at some
6661 # future time after the macro is expanded .
6662 class emph:
6663 em_l = 0
6664 def __call__(self, stri):
6665 self.__class__.em_l += 1
6667 if self.__class__.em_l % 2:
6669 <span background="EVEN">
6670 @stri
6671 </span>
6673 else:
6675 <span background="ODD">
6676 @stri
6677 </span>
6679 self.__class__.em_l -= 1
6681 emph = _foo()
6685 #*** Ordering in nested expansions, 3
6686 # The nested expansion order is inside-out (which is wrong).
6687 emx('''
6689 def emph(stri):
6690 emph_level = empy.getGlobals().get('emph_level', -1) + 1
6691 empy.getGlobals()['emph_level'] = emph_level
6692 if (emph_level % 2) == 0:
6693 rv = empy.expand('<span background="EVEN"> @stri </span>', locals())
6694 else:
6695 rv = empy.expand('<span background="ODD"> @stri </span>', locals())
6696 empy.getGlobals()['emph_level'] = emph_level - 1
6697 return rv
6699 ''')
6700 emx("@emph('foo')")
6701 emx("@emph('hello @emph(this) world')")
6702 emx("@emph{hello @emph{this} world}")
6705 #*** Ordering in nested expansions, 4
6707 emx('''
6709 def emph(stri):
6710 emph_level = empy.getGlobals().get('emph_level', -1) + 1
6711 empy.getGlobals()['emph_level'] = emph_level
6713 exp = lambda strng: empy.expand(strng, locals())
6714 if (emph_level % 2) == 0:
6715 rv = empy.expand(
6716 empy.expand(
6717 '<span background="EVEN"> @stri </span>',
6718 locals()),
6719 locals())
6720 else:
6721 rv = empy.expand(
6722 empy.expand(
6723 '<span background="ODD"> @stri </span>',
6724 locals()),
6725 locals())
6726 empy.getGlobals()['emph_level'] = emph_level - 1
6727 return rv
6729 ''')
6730 # Works
6731 emx("@emph('foo')")
6732 # Gives NameError: name 'emph' is not defined
6733 emx("@emph('hello @emph(this) world')")
6734 emx("@emph{hello @emph{this} world}")
6738 #* User-guiding emphasis
6739 #** highlight, raw pango markup
6740 # <span background="#ffcccc">Plot details can be adjusted here.</span>
6741 inline '''if 1:
6742 def plot_(fname, x, y):
6743 import matplotlib as M
6744 import pylab as P
6745 M.use('Agg')
6747 fig = P.figure(figsize=(5,4), dpi=75)
6748 ax = fig.add_subplot(111)
6749 ax.plot(x, y, '-', lw=2)
6751 ax.set_xlabel('time (s)')
6752 ax.set_ylabel('voltage (mV)')
6753 ax.set_title('About as simple as it gets, folks')
6754 ax.grid(True)
6756 fig.savefig(fname)
6759 inline 'from math import pi; import matplotlib as M; import pylab as P'
6760 x = t = P.arange(0.0, 1.0+0.01, 0.01)
6761 y = s = P.cos(2 * pi * t)
6762 # <span background="#ffdddd">Insert plot data here.</span>
6763 plot_(
6764 # <span background="#ffdddd">Output file name.</span>
6765 'simple_plot.png',
6766 # <span background="#ffdddd">Your X.</span>
6768 # <span background="#ffdddd">Your Y.</span>
6771 #** highlight, m4 syntax.
6772 # guide(Insert plot data here.)
6773 plot_(
6774 # guide(Output file name.)
6775 'simple_plot.png',
6776 # guide(Your X.)
6778 # guide(Your Y.)
6781 #** highlight, per-line prefix syntax.
6782 #g* Insert plot data here.
6783 plot_(
6784 #g* Output file name.
6785 'simple_plot.png',
6786 #g* Your X.
6788 #g* Your Y.
6791 #** highlight, long syntax w/ line separation
6792 # This leaves blank lines.
6793 #<span background="#ffdddd">
6794 # Insert plot data here.
6795 #</span>
6796 plot_(
6797 # <span background="#ffdddd">Output file name.</span>
6798 'simple_plot.png',
6799 # <span background="#ffdddd">Your X.</span>
6801 # <span background="#ffdddd">Your Y.</span>
6807 #** Greyscale other text.
6808 # From any root node:
6809 from l3gui.widgets import *
6810 for nd in self.iter_visibles():
6811 if isinstance(nd, l3Rawtext):
6812 nd._ltext.set_property("fill-color", "#" + "d0" * 3)
6814 #** Collect user guiding info from comments.
6815 # Starting from 'eval locally',
6816 # >>> self
6817 # <l3gui.widgets.l3Program instance at 0xb780a70c>
6818 #*** Find all comments.
6819 dt = self.w_.deco_table_
6820 for nd in self._l3tree.top_down():
6821 print nd, dt.get(nd._id, None)
6823 #*** Traverse comments.
6824 def iter_comments(self):
6825 dt = self.w_.deco_table_
6826 for nd in self._l3tree.top_down():
6827 comm = dt.get(nd._id, None)
6828 if comm != None:
6829 yield comm
6832 #*** Find comments with highlighting.
6833 for comm in iter_comments(self):
6834 if (comm.find("<span background") > 0):
6835 print comm
6838 #*** Accumulate comments.
6839 print [ comm for comm in iter_comments(self)
6840 if (comm.find("<span background") > 0)]
6841 print [ comm.py_string()
6842 for comm in iter_comments(self)
6843 if (comm.find("<span background") > 0)]
6844 print '\n'.join([ comm.py_string()
6845 for comm in iter_comments(self)
6846 if (comm.find("<span background") > 0)])
6849 #*** Add accumulation to this heading's comment.
6850 # This changes the associated comment and adds state handling details.
6851 # Viewing internal information can be a transient action (use popups)
6852 # or a special view (independent display alongside).
6854 # Use deco_change_label
6855 dt = self.w_.deco_table_
6856 comm = dt.get(self._l3tree._id, None)
6858 if comm != None:
6859 prepend = comm.py_string()
6860 self._old_label = prepend
6861 else:
6862 prepend = ""
6863 print (prepend + '\n' +
6864 '\n'.join([ comm.py_string()
6865 for comm in iter_comments(self)
6866 if (comm.find("<span background") > 0)]))
6868 self.deco_change_label(label =
6869 (prepend + '\n' +
6870 '\n'.join([ comm.py_string()
6871 for comm in iter_comments(self)
6872 if (comm.find("<span background") > 0)])))
6875 #*** Display accumulation as separate node
6876 # Loosely connected to independent
6877 TODO: add as menu entry
6878 done: view_as -> full member
6879 self.view_as('flat')
6884 #* matplotlib and tools
6885 from numerix import absolute, arange, array, asarray, ones, divide,\
6886 transpose, log, log10, Float, Float32, ravel, zeros,\
6887 Int16, Int32, Int, Float64, ceil, indices, \
6888 shape, which, where, sqrt, asum, compress, maximum, minimum, \
6889 typecode, concatenate, newaxis, reshape, resize, repeat, \
6890 cross_correlate, nonzero
6892 from matplotlib.numerix import absolute, arange, array, asarray, ones, divide,\
6893 transpose, log, log10, Float, Float32, ravel, zeros,\
6894 Int16, Int32, Int, Float64, ceil, indices, \
6895 shape, which, where, sqrt, asum, compress, maximum, minimum, \
6896 typecode, concatenate, newaxis, reshape, resize, repeat, \
6897 cross_correlate, nonzero
6899 inline 'import MLab as M; from matplotlib.numerix import transpose'
6900 A = M.rand(4,4)
6901 At = transpose(A)
6904 # run from l3, gets stuck in
6905 # #0 dlamc3_ (a=0xb7a10030, b=0xb7a0ffe0) at Src/dlamch.c:685
6906 # when run from l3 AND when run in l3's python:
6907 # l3 -> python -> run -> wait.
6908 M.eig(A)
6910 #* screenshots via gtk
6911 # For capturing images of selected items it's much better to use known
6912 # coordinates and directly produce a file than using the capture ->
6913 # edit -> convert -> save sequence via the gimp or others.
6915 view = self.w_.canvas.view
6916 selection = self.w_.selector.get_selection()
6918 (sl, st, sr, sb) = selection.get_bounds()
6920 # Get selection corners.
6921 (wl, wt) = map(int, view.world_to_window(sl, st))
6922 (wr, wb) = map(int, view.world_to_window(sr, sb))
6924 # Compute selection dimensions.
6925 width = wr - wl
6926 height = wb - wt
6928 # Prepare pixbuf.
6929 pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,
6930 False, # has_alpha
6931 8, # bits_per_sample
6932 width,
6933 height)
6935 # get_from_drawable(src, cmap, src_x, src_y, dest_x, dest_y, width, height)
6936 status = pixbuf.get_from_drawable(
6937 view.get_parent_window(), # gtk.gdk.Window(gtk.gdk.Drawable)
6938 gtk.gdk.colormap_get_system(),
6939 wl, wt,
6940 0, 0,
6941 width, height
6944 if status == None:
6945 print "capture failed"
6947 pixbuf.save("/tmp/foo.png", "png")
6949 #* system state work
6950 # good start:
6951 for (mname, memb) in self.__dict__.iteritems():
6952 print "%-20s %40s" % (mname, memb)
6954 # Check completeness.
6955 set([1,2]) - set([1])
6956 set(self.__dict__.keys()) - set(dir(self)) # Empty, good.
6959 #** Print an ordered table of member DATA.
6960 def sort(list):
6961 list.sort()
6962 return list
6964 print self
6965 for mname in sort(self.__dict__.keys()):
6966 memb = self.__dict__[mname]
6967 print " %-20s %-40s" % (mname, memb)
6970 #** Print an ordered table of member DATA.
6971 def pot1(datum):
6972 print "%s.%s instance at %x" % (datum.__module__,
6973 datum.__class__.__name__,
6974 (id(self)))
6976 for mname in sort(datum.__dict__.keys()):
6977 memb = datum.__dict__[mname]
6978 print " %-20s %-40.39s" % (mname, memb)
6979 pot1(self._l3tree)
6981 l3lang.ast.Program instance at -488123d4
6982 _attributes {}
6983 _block_env Env-30005-anonymous
6984 _first_char (28, 8)
6985 _id 30021
6989 #** ordered table of member data 2
6990 # Lines numbered,
6991 # first type occurence listed as subtree
6992 from types import *
6993 def pot2(datum, line = 1, handled = set()):
6994 # Filter built-in and already printed types
6995 if not (type(datum) == InstanceType):
6996 return line
6998 classname = "%s.%s" % (datum.__module__, datum.__class__.__name__)
6999 if classname in handled:
7000 return line
7001 handled.add(classname)
7003 # Print info
7004 print "%s.%s instance at %x" % (datum.__module__,
7005 datum.__class__.__name__,
7006 (id(self)))
7007 for mname in sort(datum.__dict__.keys()):
7008 memb = datum.__dict__[mname]
7009 print "%4d %-20s %-40.39s" % (line, mname, memb)
7010 line = pot2(memb, line = line + 1, handled = handled)
7011 return line
7012 pot2(self._l3tree, line = 1, handled = set())
7014 l3lang.ast.Program instance at -488123d4
7015 1 _attributes {}
7016 2 _block_env Env-30005-anonymous
7017 l3lang.ast.Env instance at -488123d4
7018 3 _bindings {('active', 'df'): None, ('pixel', 'df'
7019 4 _bindings_ids {('active', 'df'): 30509, ('pixel', 'df
7020 5 _bindings_memory {}
7021 6 _children [Env(30536, Env-30005-anonymous, [Progr
7022 7 _def_env_id 30001
7027 #** print ordered table of member data 3
7028 # Lines numbered,
7029 # first type occurence listed as subtree,
7030 # indentation for levels,
7031 # all later occurrences of a type refer to line number
7032 from types import *
7033 def sort(list):
7034 list.sort()
7035 return list
7036 def pot3(datum, line = 1, handled = {}, indent = 1):
7037 # Filter built-in and already printed types
7038 if not (type(datum) == InstanceType):
7039 return line
7041 space = " " * indent
7042 classname = "%s.%s" % (datum.__module__, datum.__class__.__name__)
7043 if handled.has_key(classname):
7044 print "%4d%s%s.%s instance -- see line %d" % (line,
7045 space,
7046 datum.__module__,
7047 datum.__class__.__name__,
7048 handled[classname])
7049 return line + 1
7050 else:
7051 handled[classname] = line
7053 # Print info
7054 print "%4d%s%s.%s instance at %x" % (line,
7055 space,
7056 datum.__module__,
7057 datum.__class__.__name__,
7058 (id(self)))
7059 line += 1
7060 for mname in sort(datum.__dict__.keys()):
7061 memb = datum.__dict__[mname]
7062 print "%4d%s%-20s %-40.39s" % (line, space, mname, memb)
7063 line = pot3(memb, line = line + 1, handled = handled,
7064 indent = indent + 1)
7065 return line
7066 # 210 lines
7067 pot3(self, line = 1, handled = {}, indent = 1)
7068 # 46 lines
7069 pot3(self._l3tree, line = 1, handled = {}, indent = 1)
7073 #** print ordered table of member data 4
7074 # Lines numbered,
7075 # first type occurence listed as subtree,
7076 # indentation for levels,
7077 # repeated instances of a fixed-member type refer to first's line number,
7078 # distinct instances of container types (Shared) produce new listing.
7079 from types import *
7080 import l3lang.ast
7081 def sort(list):
7082 list.sort()
7083 return list
7085 truncate_lines = "%-120.119s"
7086 truncate_lines = "%-80.79s"
7087 def pot4(datum, line = 1, handled = {}, indent = 1):
7088 # Filter built-in types.
7089 # inheriting from ListType has subtle problems.
7090 if isinstance(datum, l3lang.ast.aList):
7091 pass
7092 else:
7093 if not (type(datum) == InstanceType):
7094 return line
7096 space = " " * indent
7097 classname = "%s.%s" % (datum.__module__, datum.__class__.__name__)
7099 # Only cross-reference already visited data.
7100 if classname not in ['l3lang.utils.Shared']:
7101 if handled.has_key(classname):
7102 print "%4d%s%s.%s instance -- see line %d" % (line,
7103 space,
7104 datum.__module__,
7105 datum.__class__.__name__,
7106 handled[classname])
7107 return line + 1
7108 else:
7109 handled[classname] = line
7111 else:
7112 if handled.has_key(id(datum)):
7113 print "%4d%s%s.%s visited instance -- see line %d" % (
7114 line,
7115 space,
7116 datum.__module__,
7117 datum.__class__.__name__,
7118 handled[id(datum)])
7119 return line + 1
7121 else:
7122 handled[id(datum)] = line
7124 # Print info for this datum.
7125 if indent < 2:
7126 # Only show headers for objects not already described.
7127 print "%4d%s%s.%s instance at 0x%x" % (line,
7128 space,
7129 datum.__module__,
7130 datum.__class__.__name__,
7131 (id(datum) & 0xFFFFFFFFL))
7132 line += 1
7134 # Visit datum's children.
7135 for mname in sort(datum.__dict__.keys()):
7136 memb = datum.__dict__[mname]
7137 print truncate_lines % ("%4d%s%-20s %-60s" % (line, space, mname, memb))
7138 line = pot4(memb, line = line + 1, handled = handled,
7139 indent = indent + 1)
7140 return line
7141 # self is noise_add.l3
7142 pot4(self, line = 1, handled = {}, indent = 1)
7143 # 327 lines
7144 # 325 remember_y 60.25
7145 # 326 w_ <l3lang.utils.Shared instance at 0xb777512c>
7146 # 327 l3lang.utils.Shared visited instance -- see line 21
7148 # 1352 lines without back-reference to already-visited Shareds, e.g.
7150 # 1146 w_ <l3lang.utils.Shared instance at 0xb777512c>
7151 # 1147 __fluids {}
7152 # 1148 _non_pickling_keys []
7156 Include traversal of lists, dicts to cover this:
7158 This is missing _l3tree contents because
7159 type(self._alist.__dict__['_l3tree'])
7160 <class 'l3lang.ast.aList'>
7162 But this is not true; type returns
7163 >>> type(self._l3tree[0])
7164 <class 'l3lang.ast.aList'>
7165 while the instance members are all present:
7166 >>> dir(self._l3tree[0])
7167 ... '_first_char', '_id', '_last_char', '_parent',...
7168 And:
7169 >>> isinstance(self._l3tree[0], l3lang.ast.aList)
7170 True
7172 Other NESTED types inheriting from python built-ins:
7173 class aList(ListType):
7174 class vaList(aList):
7181 #** print ordered table of member data 5
7182 # - Lines numbered,
7183 # - first type occurence listed as subtree,
7184 # - indentation for levels,
7185 # - repeated instances of a fixed-member type refer to first's line number,
7186 # - distinct instances of container types (Shared) produce new listing.
7187 # - make line numbers org-indexable, via [[line]] and <<line>>
7189 # Tried
7190 # - including indentation level for text reading, didn't help
7191 from types import *
7192 import l3lang.ast
7193 def sort(list):
7194 list.sort()
7195 return list
7197 truncate_lines = "%-120.119s"
7198 truncate_lines = "%-80.79s"
7199 def pot5(datum, line = 1, handled = {}, indent = 1):
7201 This produces a reasonable state dump of the l3 widget provided as
7202 `datum`. The contained l3 program is included but not traversed
7203 recursively, so associated data and clones are completely ignored.
7204 '''
7205 # Filter built-in types.
7206 # inheriting from ListType has subtle problems.
7207 if isinstance(datum, l3lang.ast.aList):
7208 pass
7209 else:
7210 if not (type(datum) == InstanceType):
7211 return line
7213 space = " " * indent
7214 classname = "%s.%s" % (datum.__module__, datum.__class__.__name__)
7216 # Only cross-reference already visited data.
7217 if classname not in ['l3lang.utils.Shared']:
7218 if handled.has_key(classname):
7219 print "<<%03d>>%s%s.%s instance -- see line [[%03d]]" % (
7220 line,
7221 space,
7222 datum.__module__,
7223 datum.__class__.__name__,
7224 handled[classname])
7225 return line + 1
7226 else:
7227 handled[classname] = line
7229 else:
7230 if handled.has_key(id(datum)):
7231 print "<<%03d>>%s%s.%s visited instance -- see line [[%03d]]" % (
7232 line,
7233 space,
7234 datum.__module__,
7235 datum.__class__.__name__,
7236 handled[id(datum)])
7237 return line + 1
7239 else:
7240 handled[id(datum)] = line
7242 # Print info for this datum.
7243 if indent < 999:
7244 # Only show headers for objects not already described.
7245 print "<<%03d>>%s%s.%s instance at 0x%x" % (
7246 line,
7247 space,
7248 datum.__module__,
7249 datum.__class__.__name__,
7250 (id(datum) & 0xFFFFFFFFL))
7251 line += 1
7253 # Visit datum's children.
7254 for mname in sort(datum.__dict__.keys()):
7255 memb = datum.__dict__[mname]
7256 print truncate_lines % ("<<%03d>>%s%-20s %-60s" % (line, space, mname, memb))
7257 line = pot5(memb, line = line + 1, handled = handled,
7258 indent = indent + 1)
7259 return line
7260 # self is noise_add.l3
7261 pot5(self, line = 1, handled = {}, indent = 1)
7264 #** print ordered table of member data, org version 1
7265 # - use hex id as address, via org
7266 # - first type occurence listed as subtree,
7267 # - indentation for levels,
7268 # - repeated instances of a fixed-member type refer to first's id
7269 # - distinct instances of container types (Shared) produce new listing.
7270 # - no line numbers
7271 # - include emacs outline decoration (org-mode)
7272 from types import *
7273 import l3lang.ast
7274 import l3lang.ast as A
7276 def sort(list):
7277 list.sort()
7278 return list
7280 def hexid(datum):
7281 return (id(datum) & 0xFFFFFFFFL)
7284 #*** special printers
7285 from EMAN2 import EMData
7286 from numpy import ndarray
7288 accum_types = (EMData, ndarray)
7290 special_printers = {}
7291 accumulate_interp_res = {} # Accumulate stuff as side-effect.
7292 limit_output_lines = 100
7295 #**** Accumulate by EXCLUDING types.
7296 def _special(datum, line = 1, handled = {}, indent = 1):
7297 '''Print additional information for RamMem._attr_tables.'''
7298 first_line = line
7299 space = " " * indent
7300 for (id_, dic1) in datum.iteritems():
7301 # 3002 {...}
7302 # mem_str = max_str_len % str(dic1)
7303 mem_str = "{...}"
7304 if (line - first_line) < limit_output_lines:
7305 print truncate_lines % (
7306 "%s%s%-20s %s" % (
7307 "*" * indent,
7308 space[0: -indent],
7309 id_,
7310 mem_str))
7311 line += 1
7312 # clone_of {...}
7313 for key in sort(dic1.keys()):
7314 # Side effect: accumulation
7315 if key == 'interp_result':
7316 se_val = dic1[key]
7317 # These checks are superficial; real code must use
7318 # pickle w/o memo dict for verification.
7319 if not isinstance(se_val, (A.astType, A.aNone, A.aList)):
7320 if (isinstance(se_val, ListType) and
7321 len(se_val) > 0 and
7322 isinstance(se_val[0], (A.astType, A.aNone, A.aList))):
7323 pass
7324 else:
7325 accumulate_interp_res[id_] = dic1[key]
7326 # Print SOME output.
7327 if (line - first_line) < limit_output_lines:
7328 mem_str = max_str_len % str(dic1[key])
7329 print truncate_lines % (
7330 "%s%s%-20s %s" % (
7331 "*" * (indent + 1),
7332 space[0: -(indent + 1)] + " ",
7333 key,
7334 mem_str))
7335 line += 1
7336 return line
7337 special_printers[('RamMem', '_attr_tables')] = _special
7340 #**** Accumulate by INCLUDING types.
7341 def _special(datum, line = 1, handled = {}, indent = 1):
7342 '''Print additional information for RamMem._attr_tables.'''
7343 first_line = line
7344 space = " " * indent
7345 for (id_, dic1) in datum.iteritems():
7346 # 3002 {...}
7347 # mem_str = max_str_len % str(dic1)
7348 mem_str = "{...}"
7349 if (line - first_line) < limit_output_lines:
7350 print truncate_lines % (
7351 "%s%s%-20s %s" % (
7352 "*" * indent,
7353 space[0: -indent],
7354 id_,
7355 mem_str))
7356 line += 1
7357 # clone_of {...}
7358 for key in sort(dic1.keys()):
7359 # Side effect: accumulation
7360 if key == 'interp_result':
7361 se_val = dic1[key]
7362 if isinstance(se_val, accum_types):
7363 accumulate_interp_res[id_] = se_val
7365 # Print SOME output.
7366 if (line - first_line) < limit_output_lines:
7367 mem_str = max_str_len % str(dic1[key])
7368 print truncate_lines % (
7369 "%s%s%-20s %s" % (
7370 "*" * (indent + 1),
7371 space[0: -(indent + 1)] + " ",
7372 key,
7373 mem_str))
7374 line += 1
7375 return line
7376 special_printers[('RamMem', '_attr_tables')] = _special
7379 #**** Accumulate by INCLUDING types and lists/tuples of types
7380 def _special(datum, line = 1, handled = {}, indent = 1):
7381 '''Print additional information for RamMem._attr_tables.'''
7382 first_line = line
7383 space = " " * indent
7384 for (id_, dic1) in datum.iteritems():
7385 # 3002 {...}
7386 # mem_str = max_str_len % str(dic1)
7387 mem_str = "{...}"
7388 if (line - first_line) < limit_output_lines:
7389 print truncate_lines % (
7390 "%s%s%-20s %s" % (
7391 "*" * indent,
7392 space[0: -indent],
7393 id_,
7394 mem_str))
7395 line += 1
7396 # clone_of {...}
7397 for key in sort(dic1.keys()):
7398 # Side effect: accumulation
7399 if key == 'interp_result':
7400 se_val = dic1[key]
7401 if isinstance(se_val, accum_types):
7402 accumulate_interp_res[id_] = se_val
7403 if isinstance(se_val, (ListType, TupleType)) and \
7404 (len(se_val) > 0) and \
7405 (len(
7406 [ ob for ob in se_val
7407 if isinstance(ob, accum_types)]) > 0):
7408 accumulate_interp_res[id_] = se_val
7411 # Print SOME output.
7412 if ((line - first_line) < limit_output_lines):
7413 mem_str = max_str_len % str(dic1[key])
7414 print truncate_lines % (
7415 "%s%s%-20s %s" % (
7416 "*" * (indent + 1),
7417 space[0: -(indent + 1)] + " ",
7418 key,
7419 mem_str))
7420 line += 1
7421 return line
7422 special_printers[('RamMem', '_attr_tables')] = _special
7425 #*** main printer
7426 truncate_lines = "%-80.79s"
7427 max_str_len = "%-60.59s"
7429 truncate_lines = "%-160.159s"
7430 max_str_len = "%-.90s"
7431 from time import time
7432 def poto1(datum, line = 1, handled = {}, indent = 1):
7433 # Filter built-in types.
7434 # inheriting from ListType has subtle problems.
7435 if isinstance(datum, l3lang.ast.aList):
7436 pass
7437 else:
7438 if not (type(datum) == InstanceType):
7439 return line
7441 space = " " * indent
7442 classname = "%s.%s" % (datum.__module__, datum.__class__.__name__)
7444 # Only cross-reference already visited data.
7445 if classname not in ['l3lang.utils.Shared']:
7446 if handled.has_key(classname):
7447 print "%s%s.%s instance -- see id [[0x%x]]" % (
7448 space,
7449 datum.__module__,
7450 datum.__class__.__name__,
7451 handled[classname])
7452 return line + 1
7453 else:
7454 handled[classname] = hexid(datum)
7456 else:
7457 if handled.has_key(id(datum)):
7458 print "%s%s.%s visited instance -- see id [[0x%x]]" % (
7459 space,
7460 datum.__module__,
7461 datum.__class__.__name__,
7462 handled[id(datum)])
7463 return line + 1
7465 else:
7466 handled[id(datum)] = hexid(datum)
7468 # Print info for this datum.
7469 if indent < 2:
7470 print "%s%s%s.%s instance at <<0x%x>>" % (
7471 "*" * indent,
7472 space[0: -indent],
7473 datum.__module__,
7474 datum.__class__.__name__,
7475 hexid(datum))
7476 line += 1
7478 # Visit datum's children.
7479 for mname in sort(datum.__dict__.keys()):
7480 memb = datum.__dict__[mname]
7481 # Include timing information with lines.
7482 start = time()
7484 # For instances, produce org-mode <<label>>
7485 # mem_str = max_str_len % str(memb)
7486 mem_str = max_str_len % str(memb)
7487 if "instance at 0x" in mem_str:
7488 mem_str = "%s at <<0x%x>>" % (mem_str[0:-15], hexid(memb))
7489 elif isinstance(memb, (InstanceType, ClassType, ListType, DictType)):
7490 mem_str = "%s at <<0x%x>>" % (mem_str, hexid(memb))
7491 print truncate_lines % (
7492 "%s%s%-20s %s [%.4fs]" % (
7493 "*" * indent,
7494 space[0: -indent],
7495 mname,
7496 mem_str,
7497 time() - start))
7498 # Default printout for child.
7499 line = poto1(memb, line = line + 1, handled = handled,
7500 indent = indent + 1)
7501 # Additional custom printout.
7502 try:
7503 line = special_printers[(datum.__class__.__name__,
7504 mname)](memb, line = line + 1,
7505 handled = handled,
7506 indent = indent + 1)
7507 except KeyError:
7508 pass
7509 return line
7511 #** Get internals dump
7512 # Run gui
7513 # insert program
7514 # run program
7515 # use 'evaluate locally` on datum
7517 # Then:
7518 self
7519 poto1(self, line = 1, handled = {}, indent = 1)
7521 poto1(w_.state_.storage, line = 1, handled = {}, indent = 1)
7524 #** Compute disk / ram ratio
7525 # Given
7526 # Sr = Ram state
7527 # Sd = disk state (from accumulate_interp_res)
7528 # St = total state (from save_state)
7529 # S0 = initial state (overhead, from save_state at time 0)
7530 # R = ratio
7532 # we want
7533 # Sr(t) = R * Sd(t)
7534 # and
7535 # St(t) = Sr(t) + Sd(t) + S0
7537 # Using known values,
7538 # Sr = St - Sd - S0
7539 # so
7540 # R = (St - Sd - S0) / Sd
7541 # should (approximately) hold.
7543 # Run gui
7544 # insert program
7545 # save state to st.t0
7546 # run program
7547 # exit to toplevel
7549 # Then:
7550 poto1(w_.state_.storage, line = 1, handled = {}, indent = 1)
7551 print "items collected: ", len(accumulate_interp_res)
7553 from l3lang import utils
7554 utils.file_cPickle(get_current_state(w_), "st.total")
7555 utils.file_cPickle(accumulate_interp_res, "st.disk")
7557 S0 = os.path.getsize("st.t0")
7558 St = os.path.getsize("st.total")
7559 Sd = os.path.getsize("st.disk")
7561 print "ram / disk ratio", 1.0 * (St - Sd - S0) / Sd
7564 #** view interp_result
7565 for (id_, val) in accumulate_interp_res.iteritems():
7566 print "%10d %-.50s" % (id_, val)
7568 # What is accumulate_interp_res[33122]?
7569 type(accumulate_interp_res[33122])
7570 print w_.state_.storage.get_attribute_names(33122)
7571 print w_.state_.storage.get_attribute(33122, 'interp_env')
7575 #** object sizes
7576 try:
7577 from cStringIO import StringIO
7578 except ImportError:
7579 from StringIO import StringIO
7581 pckl_file = StringIO()
7582 pcklr = pickle.Pickler(pckl_file, protocol = 2)
7583 pcklr.memo
7584 pcklr.dump(accumulate_interp_res)
7585 print "pickle size", pckl_file.tell()
7587 # memo[id(obj)] = memo_len, obj
7588 # too many values with no back-link information to identify source.
7589 for (id_, (index, obj)) in pcklr.memo.iteritems():
7590 print "%-11x %-6d %-.50s" % (id_, index, obj)
7594 #* Custom pickle with printout.
7595 # Collecting through storage is unreliable; modify pickle itself.
7596 #** params
7597 import pickle
7598 from EMAN2 import EMData
7599 from numpy import ndarray
7601 try:
7602 from cStringIO import StringIO
7603 except ImportError:
7604 from StringIO import StringIO
7606 accum_types = (EMData, ndarray)
7607 pckl_file = StringIO()
7608 g_display_size = 10000
7609 truncate_lines = "%-60.59s"
7612 #** pickle modifications.
7613 from types import *
7614 from copy_reg import dispatch_table
7615 from copy_reg import _extension_registry, _inverted_registry, _extension_cache
7616 import marshal
7617 import sys
7618 import struct
7620 def _real_save(self, obj):
7621 # Info dump
7622 me_start = pckl_file.tell()
7623 def dump_info():
7624 obj_size = pckl_file.tell() - me_start
7625 if isinstance(obj, accum_types) or (obj_size > g_display_size):
7626 print "| %8d | \"%s\" | " % (obj_size,
7627 truncate_lines % repr(obj))
7628 # -- Info dump
7630 # Check for persistent id (defined by a subclass)
7631 pid = self.persistent_id(obj)
7632 if pid:
7633 self.save_pers(pid)
7634 dump_info()
7635 return
7637 # Check the memo
7638 x = self.memo.get(id(obj))
7639 if x:
7640 self.write(self.get(x[0]))
7641 dump_info()
7642 return
7644 # Check the type dispatch table
7645 t = type(obj)
7646 f = self.dispatch.get(t)
7647 if f:
7648 f(self, obj) # Call unbound method with explicit self
7649 dump_info()
7650 return
7652 # Check for a class with a custom metaclass; treat as regular class
7653 try:
7654 issc = issubclass(t, TypeType)
7655 except TypeError: # t is not a class (old Boost; see SF #502085)
7656 issc = 0
7657 if issc:
7658 self.save_global(obj)
7659 dump_info()
7660 return
7662 # Check copy_reg.dispatch_table
7663 reduce = dispatch_table.get(t)
7664 if reduce:
7665 rv = reduce(obj)
7666 else:
7667 # Check for a __reduce_ex__ method, fall back to __reduce__
7668 reduce = getattr(obj, "__reduce_ex__", None)
7669 if reduce:
7670 rv = reduce(self.proto)
7671 else:
7672 reduce = getattr(obj, "__reduce__", None)
7673 if reduce:
7674 rv = reduce()
7675 else:
7676 raise PicklingError("Can't pickle %r object: %r" %
7677 (t.__name__, obj))
7679 # Check for string returned by reduce(), meaning "save as global"
7680 if type(rv) is StringType:
7681 self.save_global(obj, rv)
7682 dump_info()
7683 return
7685 # Assert that reduce() returned a tuple
7686 if type(rv) is not TupleType:
7687 raise PicklingError("%s must return string or tuple" % reduce)
7689 # Assert that it returned an appropriately sized tuple
7690 l = len(rv)
7691 if not (2 <= l <= 5):
7692 raise PicklingError("Tuple returned by %s must have "
7693 "two to five elements" % reduce)
7695 # Save the reduce() output and finally memoize the object
7696 self.save_reduce(obj=obj, *rv)
7697 dump_info()
7699 # Overwrite method.
7700 def save(self, obj):
7701 # Produces too many empty () pairs.
7702 print "(",
7703 try:
7704 _real_save(self, obj)
7705 finally:
7706 print ")"
7708 def save(self, obj):
7709 _real_save(self, obj)
7711 pickle.Pickler.save = save
7714 #** Compute.
7715 accum_types = (EMData, ndarray)
7716 pckl_file = StringIO()
7717 g_display_size = 100000
7718 truncate_lines = "%-60.59s"
7719 pcklr = pickle.Pickler(pckl_file, protocol = 2)
7720 pcklr.memo
7722 # First get state_.storage,
7723 pcklr.dump(w_.state_.storage)
7725 # ... then get the REST of state_
7726 pcklr.dump(w_.state_)
7728 #* Custom pickle, collect info, get prefix dump
7729 # Collecting through storage is unreliable; modify pickle itself.
7730 #** params
7731 import pickle
7732 from EMAN2 import EMData
7733 from numpy import ndarray
7735 try:
7736 from cStringIO import StringIO
7737 except ImportError:
7738 from StringIO import StringIO
7740 g_accum_types = (EMData, ndarray)
7741 g_pckl_file = StringIO()
7742 g_display_size = 10000
7743 g_truncate_lines = "%-60.59s"
7744 g_info_stack = []
7748 #** pickle modifications.
7749 from types import *
7750 from copy_reg import dispatch_table
7751 from copy_reg import _extension_registry, _inverted_registry, _extension_cache
7752 import marshal
7753 import sys
7754 import struct
7756 def _real_save(self, obj):
7757 # Info dump
7758 me_start = g_pckl_file.tell()
7759 def dump_info():
7760 obj_size = g_pckl_file.tell() - me_start
7761 if isinstance(obj, g_accum_types) or (obj_size > g_display_size):
7762 # Forming the full repr() takes a LOT of time; maybe
7763 # settle for the type() info...
7764 g_info_stack.append( (obj_size, g_truncate_lines % repr(obj), obj) )
7765 # -- Info dump
7767 # Check for persistent id (defined by a subclass)
7768 pid = self.persistent_id(obj)
7769 if pid:
7770 self.save_pers(pid)
7771 dump_info()
7772 return
7774 # Check the memo
7775 x = self.memo.get(id(obj))
7776 if x:
7777 self.write(self.get(x[0]))
7778 dump_info()
7779 return
7781 # Check the type dispatch table
7782 t = type(obj)
7783 f = self.dispatch.get(t)
7784 if f:
7785 f(self, obj) # Call unbound method with explicit self
7786 dump_info()
7787 return
7789 # Check for a class with a custom metaclass; treat as regular class
7790 try:
7791 issc = issubclass(t, TypeType)
7792 except TypeError: # t is not a class (old Boost; see SF #502085)
7793 issc = 0
7794 if issc:
7795 self.save_global(obj)
7796 dump_info()
7797 return
7799 # Check copy_reg.dispatch_table
7800 reduce = dispatch_table.get(t)
7801 if reduce:
7802 rv = reduce(obj)
7803 else:
7804 # Check for a __reduce_ex__ method, fall back to __reduce__
7805 reduce = getattr(obj, "__reduce_ex__", None)
7806 if reduce:
7807 rv = reduce(self.proto)
7808 else:
7809 reduce = getattr(obj, "__reduce__", None)
7810 if reduce:
7811 rv = reduce()
7812 else:
7813 raise PicklingError("Can't pickle %r object: %r" %
7814 (t.__name__, obj))
7816 # Check for string returned by reduce(), meaning "save as global"
7817 if type(rv) is StringType:
7818 self.save_global(obj, rv)
7819 dump_info()
7820 return
7822 # Assert that reduce() returned a tuple
7823 if type(rv) is not TupleType:
7824 raise PicklingError("%s must return string or tuple" % reduce)
7826 # Assert that it returned an appropriately sized tuple
7827 l = len(rv)
7828 if not (2 <= l <= 5):
7829 raise PicklingError("Tuple returned by %s must have "
7830 "two to five elements" % reduce)
7832 # Save the reduce() output and finally memoize the object
7833 self.save_reduce(obj=obj, *rv)
7834 dump_info()
7836 # Overwrite method.
7837 def save(self, obj):
7838 global g_info_stack
7839 _tmp = g_info_stack
7840 g_info_stack = []
7841 try:
7842 _real_save(self, obj)
7843 finally:
7844 if len(g_info_stack) > 0:
7845 _tmp.append(g_info_stack)
7846 g_info_stack = _tmp
7848 pickle.Pickler.save = save
7851 #** view info
7852 def dump_stack(stack, indent):
7853 # Unwrap outer lists
7854 while isinstance(stack[-1], ListType):
7855 stack = stack[-1]
7856 # Get the main object head and print info.
7857 (size, rep, obj) = stack[-1]
7858 print "%s %-8d %s" % ("*" * indent, size, rep)
7859 # Get head's content, if any
7860 [dump_stack(ii, indent + 1) for ii in stack[0:-1]]
7863 #** Use.
7864 g_accum_types = (EMData, ndarray)
7865 g_pckl_file = StringIO()
7866 g_display_size = 1
7867 g_display_size = 1000
7868 g_truncate_lines = "%-60.59s"
7869 g_info_stack = []
7870 pcklr = pickle.Pickler(g_pckl_file, protocol = 2)
7871 pcklr.memo
7873 # Collect data.
7874 pcklr.dump(w_.state_)
7875 # OR
7876 pcklr.dump(w_.cp_)
7877 # Inspect
7878 dump_stack(g_info_stack, 0)
7881 g_display_size = 1
7882 pcklr.dump(self._l3tree)
7883 dump_stack(g_info_stack, 0)
7885 #* dbm interface
7886 import anydbm as any, whichdb
7887 #** Prepare.
7888 db = any.open('l3db.st', 'c')
7889 db.close()
7890 print whichdb.whichdb('l3db.st')
7891 #** Save
7892 db = any.open('l3db.st', 'c')
7893 # db[key] = val
7894 storage.sweep(db)
7895 db.close()
7897 #* Proxy for transparent persistence
7898 class Proxy:
7899 pass
7901 def __init__(self, obj):
7902 self._obj = obj
7903 Proxy.__init__ = __init__
7905 def to_cache(self, id):
7906 # TODO
7907 self._obj = None
7908 self._id = id
7909 Proxy.to_cache = to_cache
7911 def __getattr__(self, m_name):
7912 m_unbnd = getattr(self._obj.__class__, m_name)
7914 def _unwrap(*args):
7915 nargs = []
7916 for arg in args:
7917 if isinstance(arg, Proxy):
7918 nargs.append(arg._obj)
7919 else:
7920 nargs.append(arg)
7921 return (m_unbnd(self._obj, *nargs))
7923 return _unwrap
7924 Proxy.__getattr__ = __getattr__
7927 #** proxy numeric tests
7928 import numpy as N
7929 from cPickle import dumps
7930 n1 = N.random.ranf( (5,4) )
7931 n1_prox = Proxy(n1)
7932 print n1_prox + n1_prox
7933 # Size comparison.
7934 # TODO: getstate / setstate
7935 print len(dumps(n1))
7936 print len(dumps(n1_prox))
7937 n1_prox.to_cache(1234)
7938 print len(dumps(n1_prox))
7940 # Useless in-memory size guesses (spread too much)
7941 plist = [Proxy(n1) for ii in xrange(10)]
7942 id(plist[-1]) - id(plist[-2])
7943 ppos = [id(foo) for foo in plist]
7944 ppos.sort()
7945 print map(lambda (x,y): x-y, zip(ppos[1:None], ppos[0:-2]))
7948 #** proxy emdata tests
7949 from EMAN2 import EMData
7950 e1 = EMData()
7951 arr_size = (3,4)
7952 arr_size = (64, 64)
7953 print "array size", arr_size
7954 e1.set_size(*arr_size)
7955 e1.process_inplace('testimage.noise.uniform.rand')
7956 print sys.getrefcount(e1)
7957 e1_prox = Proxy(e1)
7958 print sys.getrefcount(e1)
7959 # Operators.
7960 print e1_prox + e1_prox
7962 # Sizes.
7963 print len(dumps(e1))
7964 print len(dumps(e1_prox))
7965 e1_prox.to_cache(1234)
7966 print len(dumps(e1_prox))
7971 #** create test cases
7972 import numpy as N
7973 import weakref
7974 from cPickle import dumps
7976 n1 = N.random.ranf( (5,4) )
7977 n1_prox = Proxy(n1)
7978 n1_weak = weakref.proxy(n1)
7980 # Initial refcount.
7981 print sys.getrefcount(n1)
7984 # Refcount with member ref.
7985 n1t = n1.tofile
7986 print sys.getrefcount(n1)
7989 # weakref's proxy adds no new fields:
7990 set(dir(n1_weak)) - set(dir(n1_prox._obj))
7992 from EMAN2 import EMData
7993 e1 = EMData()
7994 e1.set_size(3,4)
7995 e1.process_inplace('testimage.noise.uniform.rand')
7996 print sys.getrefcount(e1)
7997 e1prox = Proxy(e1)
7998 e1weak = weakref.proxy(e1)
8001 #** Try operators
8002 #*** numeric array works
8003 print n1_prox + n1_prox
8005 n1.__add__(n1)
8009 #*** emdata fails
8010 # TypeError: unsupported operand type(s) for +: 'instance' and 'instance'
8011 print e1prox + e1prox
8012 import operator
8013 print operator.add(e1prox, e1prox)
8015 # This runs,
8016 print e1weak + e1weak
8017 print operator.add(e1weak, e1weak)
8018 # but produces the equal? result:
8019 # <libpyEMData2.EMData object at 0x156239cc>
8020 # <libpyEMData2.EMData object at 0x156239cc>
8022 # Something is missing causing the discrepancy:
8023 print e1weak.__add__(e1weak)
8024 print e1.__add__(e1)
8025 # NotImplemented
8026 # <libpyEMData2.EMData object at 0x15623cdc>
8028 print n1.__add__
8029 print e1.__add__
8030 # <method-wrapper object at 0x1955d3cc>
8031 # <bound method EMData.__add__ of <libpyEMData2.EMData object at 0x1613da54>>
8033 print EMData.__add__(e1,e1)
8034 print EMData.__add__(e1prox,e1prox)
8035 print EMData.__add__(e1weak,e1weak)
8036 # <libpyEMData2.EMData object at 0x15623ca4>
8037 # NotImplemented
8039 e1prox._make_member_proxy('__add__')
8041 # Now this runs (work -- who knows):
8042 print e1prox + e1prox
8043 print e1prox.__add__
8044 print e1weak.__add__
8045 # <bound method EMData.__add__ of <libpyEMData2.EMData object at 0x1613da54>>
8046 # <bound method EMData.__add__ of <libpyEMData2.EMData object at 0x1613da54>>
8048 # TypeError: unsupported operand type(s) for +: 'EMData' and 'instance'
8049 print 1.0 * e1prox + e1prox
8051 # works
8052 print e1weak + e1weak
8055 #* Replace original class with proxy wrapper
8056 # Looks like there is no need for metaclasses, as this will do
8057 # everything needed. However, this will not intercept C code creating
8058 # instances, so it is too limited in practice.
8060 # Retain original
8061 import EMAN2
8062 eman2_emdata = EMAN2.EMData
8064 # Make replacement.
8065 class EMData(Proxy):
8066 def __init__(self, *args):
8067 self._obj = eman2_emdata(*args)
8069 # Bind replacement.
8070 # This binding remains valid for all FUTURE references, so it must be
8071 # done before other scripts import the module.
8072 EMAN2.EMData = EMData
8075 # Test
8076 e1 = EMAN2.EMData()
8077 e1.set_size(3,4)
8078 e1.process_inplace('testimage.noise.uniform.rand')
8079 e1.print_image()
8081 # The (C) member functions directly create the new object, so the
8082 # indirection above fails.
8083 tst_add = (e1 + e1)
8084 tst_add.print_image()
8086 #* Pickling uniquely
8087 # To get accurate memory stats, avoid repeated pickling given nested
8088 # references, e.g.
8089 # A, B, [A,B]
8091 import pickle
8092 try:
8093 from cStringIO import StringIO
8094 except ImportError:
8095 from StringIO import StringIO
8097 pckl_file = StringIO()
8098 pcklr = pickle.Pickler(pckl_file, protocol = 2)
8099 print pcklr.memo
8100 def pickle_size():
8101 return pckl_file.tell()
8104 #* string deepcopy test
8105 from copy import deepcopy, copy
8106 orig = 'the original string'
8107 cp = deepcopy(orig)
8108 print id(orig) - id(cp)
8109 # so strings 'copy' as reference.