2 # Author: Michael H. Hohn (mhhohn@lbl.gov)
4 # Copyright (c) 2006, The Regents of the University of California
6 # See legal.txt and license.txt
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
26 from pprint
import pprint
30 def enter_debug(type, value
, traceback
):
31 pdb
.post_mortem(traceback
)
34 sys
.excepthook
= enter_debug
38 sys
.excepthook
= sys
.__excepthook
__
43 # NO: gtk.threads_init()
44 gobject
.threads_init()
48 import profile
, pstats
49 prof
= profile
.Profile()
50 prof
.run('gtk.main()')
52 ps
= pstats
.Stats(prof
)
54 ps
.sort_stats('cumulative').print_stats(40)
55 ps
.sort_stats('time', 'stdname').print_stats(40)
57 # Function ... was called by
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
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..
88 foo
= save_state(w_
, '/tmp/foo.state')
92 # VERY useful for debugging: tree structure, plain text!
93 pprint(utils
.as_list(get_current_state(w_
)))
97 bar
= load_state(w_
, '/tmp/foo.state')
100 pprint(utils
.file_unpickle('/tmp/bar.1'))
105 #** find all instances of l3If
106 lc
= w_
.lib_canvas
.view
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
)
115 print set._d
_lhs
_marker
.get_property("points")
122 utils
.file_pickle(tree
)
129 fn
= utils
.file_pickle(itr
)
133 w_
.node_tree
.view
.widget
.expand_to_path( w_
.node_tree
.view
.start
) # yes
134 w_
.node_tree
.view
.widget
.row_activated(w_
.node_tree
.view
.start
, label_col
)
135 # the cursor must be released somehow...
136 w_
.node_tree
.view
.widget
.set_cursor( w_
.node_tree
.view
.start
) # no
137 w_
.node_tree
.view
.widget
.scroll_to_cell( w_
.node_tree
.view
.start
) # no
141 #* canvas-embedded text, running program
142 raw
= list(w_
.canvas
.view
._nodes
)[1]
144 print raw
._textview
.get_events()
147 print raw
._textview
.get_property("sensitive")
148 raw
._textview
.set_property("sensitive", False)
151 print raw
._ltext
.get_bounds()
152 buff
= raw
._textview
.get_buffer()
153 print buff
.get_text(*buff
.get_bounds())
159 #* canvas-embedded text widget
161 view
= w_
.canvas
.view
162 ra
= w_
.canvas
.view
.root().add
164 c_root
= ra(canvas
.CanvasGroup
)
165 c_text
= gtk
.TextView()
166 c_text
.get_buffer().set_text("short text")
169 c_label
= ra(canvas
.CanvasWidget
,
170 anchor
= gtk
.ANCHOR_NORTH_WEST
,
171 # size_pixels = False,
184 c_label
= c_root
.add(canvas
.CanvasWidget
,
185 anchor
= gtk
.ANCHOR_NORTH_WEST
,
186 # size_pixels = False,
205 print c_label
.get_bounds()
209 buff
= c_text
.get_buffer()
210 print [ii
.get_offset() for ii
in buff
.get_bounds()]
212 tag_call
= buff
.create_tag()
213 tag_size
= buff
.create_tag( family
= "Monospace")
215 buff
.apply_tag(tag_size
, *buff
.get_bounds() )
216 buff
.apply_tag(tag_call
, *buff
.get_bounds() )
218 print tag_call
.get_priority()
219 print tag_size
.get_priority()
223 def tag_event(texttag
, widget
, event
, iter):
225 if event
.type == gtk
.gdk
.BUTTON_PRESS
:
226 if event
.button
== 1:
227 print "at: ", iter.get_offset()
228 texttag
.set_property("weight", pango
.WEIGHT_HEAVY
)
230 elif event
.type == gtk
.gdk
.BUTTON_RELEASE
:
231 if event
.button
== 1:
232 texttag
.set_property("weight", pango
.WEIGHT_NORMAL
)
236 tag_call
.connect("event", lambda *a
: tag_event(*a
))
239 #*** dynamic property adjust
240 print tag_size
.get_property("size-points")
241 tag_size
.set_property("size-points", 12)
245 def re_tag_inserted_text(buffer, iter, text
, length
):
246 iter_to
= iter.copy()
247 if not iter.backward_chars(length
):
250 if iter.has_tag(tag_size
):
251 print "no tagging needed"
254 buff
.apply_tag(tag_size
, iter, iter_to
)
257 buff
.connect_after("insert-text", lambda *a
: re_tag_inserted_text(*a
))
261 #** pango font adjust -- whole widget
262 # font_desc = c_text.get_pango_context().get_font_description()
263 # print font_desc.get_size() # 10240 -- 10 pt font
264 # font_desc.set_size(fs_default / 10)
269 nds
= w_
.canvas
.view
._nodes
277 view
= w_
.canvas
.view
278 ra
= w_
.canvas
.view
.root().add
279 c_label
= ra(canvas
.CanvasText
,
281 size_points
= 12 * 1,
286 # c_label.set_property("scale", 0.5)
287 print c_label
.get_property("size-set")
290 # The item only moves; no font size change.
291 def affine_scale(sx
, sy
):
292 return (sx
, 0.0, 0.0, sy
, 0.0, 0.0)
294 c_label
.affine_relative(affine_scale(0.2, 0.2))
300 view
= w_
.canvas
.view
301 ra
= w_
.canvas
.view
.root().add
302 c_label
= ra(canvas
.CanvasText
,
304 size
= 10 * pango
.SCALE
,
312 c_label
.set_property("size", 15 * pango
.SCALE
) # Resize. Good.
315 # c_label.affine_relative( (0.5, 0.0, 0.0, 0.5, 0.0, 0.0) ) # Only moves...
319 #*** Moving item test.
320 # Drag region is the bounding box. Easy to select.
326 def drag_event(item
, event
):
327 if event
.type == gtk
.gdk
.BUTTON_PRESS
:
328 if event
.button
== 1:
329 # sm: move group start
330 self
.remember_x
= event
.x
331 self
.remember_y
= event
.y
334 elif event
.type == gtk
.gdk
.MOTION_NOTIFY
:
335 if event
.state
& gtk
.gdk
.BUTTON1_MASK
:
337 # Get the new position and move by the difference.
342 dx
= new_x
- self
.remember_x
343 dy
= new_y
- self
.remember_y
345 self
.remember_x
= new_x
346 self
.remember_y
= new_y
350 print c_label
.connect("event", drag_event
)
353 #*** Font change test.
354 c_label
.set_property("font", "monospace") # Font style name
355 c_label
.set_property("size", 12 * pango
.SCALE
) #
359 #*** Color change test.
360 c_label
.set_property("fill-color", "red")
365 #** CanvasText speed test
367 view
= w_
.canvas
.view
368 rg
= w_
.canvas
.view
.root().add(canvas
.CanvasGroup
)
371 c_label
= ra(canvas
.CanvasText
,
372 x
= 2 + (ii
% 10) * 7,
374 size
= 10 * pango
.SCALE
,
376 scale
= 1.8, # ignored...
382 text_l
= [add(ii
) for ii
in xrange(0,1000)]
386 # Zoom does change position, but not size.
388 #*** change font and size
391 ii
.set_property("font", "monospace")
392 ii
.set_property("size", 12 * pango
.SCALE
)
397 ii
.set_property("font", "monospace")
398 ii
.set_property("size", 8 * pango
.SCALE
)
402 # "scale" is ignored (maybe try cairo backend?)
404 ii
.set_property("scale", 1.8)
405 ii
.set_property("scale_set", True)
415 view
= w_
.canvas
.view
417 raw
= l3Rawtext(view
.w_
, view
)
420 buff
= raw
._ltext
.get_buffer()
421 tag_call
= buff
.create_tag()
422 print tag_call
.get_priority()
423 tag_call
.set_priority(0)
425 def tag_event(texttag
, widget
, event
, iter):
427 # useless: iter.get_offset() is always 0 for mouse buttons
428 # never signalled: gtk.gdk.ENTER_NOTIFY, LEAVE_NOTIFY
429 if event
.type == gtk
.gdk
.BUTTON_PRESS
:
430 if event
.button
== 1:
431 print "at: ", iter.get_offset()
432 texttag
.set_property("weight", pango
.WEIGHT_HEAVY
)
434 elif event
.type == gtk
.gdk
.BUTTON_RELEASE
:
435 if event
.button
== 1:
436 texttag
.set_property("weight", pango
.WEIGHT_NORMAL
)
440 print tag_call
.get_property("size-set")
441 tag_call
.set_property("size", view
._pixpu
_base
* 10)
442 # tag_call.set_property("size-set", False)
444 tag_call
.connect("event", lambda *a
: tag_event(*a
))
447 # Text reverts to old size after apply_tag...
448 # buff.apply_tag(raw._tag_size, *buff.get_bounds() )
449 # The sequence create, edit, esc, apply_tag works...
451 raw
._focus
_dummy
.grab_focus()
453 # raw._text_orig = buff.get_text(*buff.get_bounds())
454 buff
.set_text( raw
._text
_orig
)
456 buff
.apply_tag(tag_call
, *buff
.get_bounds() )
457 # beg, _ = buff.get_bounds()
459 # end.forward_chars(4)
460 # beg.forward_chars(1)
461 # buff.apply_tag(tag_call, beg, end )
463 buff
.apply_tag(raw
._tag
_size
, *buff
.get_bounds() )
465 # buff.get_tag_table().foreach(lambda tag, _: pprint(tag))
468 #** text handling, raw
473 #** pango font adjustments
475 view
= w_
.canvas
.view
476 ra
= w_
.canvas
.view
.root().add
478 font_desc
= view
.get_pango_context().get_font_description()
479 font_desc
.set_stretch(pango
.STRETCH_NORMAL
)
480 fs_default
= font_desc
.get_size() # 10240 -- 10 pt font
481 font_desc
.set_size(fs_default
/ 10)
483 print font_desc
.get_size()
486 c_label
= ra(canvas
.CanvasRichText
,
491 text
= "NEW SAMPLE TEXT",
501 pprint(w_
.canvas
.view
._nodes
)
502 pprint(w_
.canvas
.view
._edges
)
504 pprint(w_
.canvas
.view
._pgraph
._nodes
)
505 pprint(w_
.canvas
.view
._pgraph
._edges
)
507 (w_
.canvas
.view
._edges
[0]).lower_to_bottom()
510 w_
.canvas
.view
._nodes
[(2, 'root')].get_bounds()
513 #** canvas anti-aliasing
514 view
= w_
.canvas
.view
515 view
.get_property('aa')
516 view
.set_property('aa', True) # only at init time.
519 view
= canvas
.Canvas(aa
= True)
520 view
.modify_bg(gtk
.STATE_NORMAL
, gtk
.gdk
.color_parse("grey5") )
523 #** canvas item zoom, affine
524 view
= w_
.canvas
.view
525 rt
= list(view
._nodes
)[0]._ltext
530 view
= w_
.canvas
.view
533 # view.set_center_scroll_region(True)
534 view
.set_pixels_per_unit(view
._pixpu
* 1.6)
538 print view
.get_scroll_offsets() # pixels
539 print view
.get_scroll_region() # world
541 view
.set_scroll_region
544 wc
= view
.get_allocation().width
# pixels
545 hc
= view
.get_allocation().height
547 # Get the display size.
548 cxw
, cyw
= view
.c2w(wc
/2, hc
/2)
550 view
.world_to_window(0, 0)
551 view
.window_to_world(400, 400)
555 view
.get_size_request()
557 view
.parent
.children()
558 view
.parent
.get_child_visible
562 # <gtk.HBox object (GtkHBox) at 0x40d6e8c4>
563 # >>> view.parent.get_parent_window().get_size()
572 # GnomeCanvas-ERROR **: file gnome-canvas-rich-text.c: line 1596
573 # (blink_cb): assertion failed: (text->_priv->layout)
576 c_label
= ra(canvas
.CanvasRichText
,
578 width
= 10 * cp_
.font_size
, height
= 1 * cp_
.font_size
,
579 text
= "sample text",
584 ra
= w_
.canvas
.view
.root().add
587 fill_color
= 'black',
590 line_style
= gtk
.gdk
.LINE_SOLID
,
591 width_pixels
= cp_
.outline_width_normal
,
592 points
= [ 0,0, 20,20 ], # scaled units.
595 point_l
= (edge
).get_property('points') # by value..
598 #** motion via properties:
599 (edge
).set_property('points', [ 0, 0, 20, 10])
601 (edge
).set_property('points', [ 0, 0, 20, 5])
606 canv
= w_
.canvas
.view
610 focus_dummy
= w_
.canvas
.view
.root().add(canvas
.CanvasItem
)
612 focus_dummy
.grab_focus()
616 label_root
= w_
.canvas
.view
.root().add(canvas
.CanvasGroup
)
620 label_text
= label_root
.add(canvas
.CanvasRichText
,
626 ### grow_height = True,
628 cursor_visible
= False,
629 cursor_blink
= False,
634 print label_text
.get_bounds()
635 x1
, y1
, x2
, y2
= label_text
.get_bounds()
636 x1
, y1
= canv
.c2w(int(x1
), int(y1
))
637 x2
, y2
= canv
.c2w(int(x2
), int(y2
))
639 label_rect
= label_root
.add(canvas
.CanvasRect
,
642 fill_color
= 'green',
643 outline_color
= 'blue',
644 width_pixels
= cp_
.outline_width_normal
,
652 def tag_event_1(texttag
, widget
, event
, iter):
653 if event
.type == gtk
.gdk
.BUTTON_PRESS
:
654 if event
.button
== 1:
655 print "tag_event_1: ", texttag
658 def tag_event(texttag
, widget
, event
, iter):
659 # useless: iter.get_offset() is always 0 for mouse buttons
660 # never signalled: gtk.gdk.ENTER_NOTIFY, LEAVE_NOTIFY
661 if event
.type == gtk
.gdk
.BUTTON_PRESS
:
662 if event
.button
== 1:
663 print "at: ", iter.get_offset()
664 texttag
.set_property("weight", pango
.WEIGHT_HEAVY
)
666 elif event
.type == gtk
.gdk
.BUTTON_RELEASE
:
667 if event
.button
== 1:
668 texttag
.set_property("weight", pango
.WEIGHT_NORMAL
)
672 def select_event(item
, event
, data
= None):
673 if event
.type == gtk
.gdk
.BUTTON_PRESS
:
675 ### item.grab_focus()
676 if event
.button
== 3:
683 # Creation order has no effect on size override.
685 buff
= label_text
.get_buffer()
686 buff_text
= "sample text"
687 buff
.set_text(buff_text
)
690 gi
= buff
.get_iter_at_offset
691 ct
= lambda color
: buff
.create_tag( background
= color
,
692 size
= int(10 * canv
._pixpu
_base
),
693 family
= "Monospace",
695 tag_call
= map(ct
, ["blue", "grey90"])
697 ii
= buff_text
.find(" ", 0)
700 tag_size
= buff
.create_tag( size
= int(10 * canv
._pixpu
_base
),
701 family
= "Monospace",
703 ### No text, serious delays:
704 ### tag_size = buff.create_tag( size = 10 * pango.SCALE,
705 ### family = "Monospace")
708 # Application order has no effect on override.
709 ### buff.apply_tag(tag_size, *buff.get_bounds() )
710 ### buff.apply_tag(tag_call[0], gi(2), gi(ii))
712 ### # The overlapping region has NO tag's text size.
713 ### buff.apply_tag(tag_size, gi(0), gi(4) )
714 ### buff.apply_tag(tag_call[0], gi(2), buff.get_bounds()[1])
716 buff
.apply_tag(tag_size
, gi(0), gi(4) )
717 buff
.apply_tag(tag_call
[0], gi(4), gi(7))
718 buff
.apply_tag(tag_call
[1], gi(7), buff
.get_bounds()[1])
721 # jj = buff_text.find(" ", ii + 1)
723 # jj = len(buff_text)
725 # buff.apply_tag(tag_call[1],
728 print tag_size
.get_priority()
729 print tag_call
[0].get_priority()
731 # no effect on size...
732 # tag_size.set_priority(buff.get_tag_table().get_size() - 1)
735 tag_size
.set_property("size", canv
._pixpu
_base
* 1.2)
738 #** Event connection.
739 tag_size
.connect("event", lambda *a
: tag_event(*a
))
740 tag_call
[0].connect("event", lambda *a
: tag_event_1(*a
))
741 tag_call
[1].connect("event", lambda *a
: tag_event_1(*a
))
743 label_root
.connect("event", select_event
)
749 ### canv.set_pixels_per_unit(canv._pixpu)
751 canv
._pixpu
= (1.00001 * canv
._pixpu
)
752 canv
.set_pixels_per_unit(canv
._pixpu
)
755 #** affine adjustment
756 # The affine transform changes the box bounds, but not the font size...
757 # label_text.affine_relative( (0.1, 0.0,
761 # print label_text.get_bounds()
762 # print label_text.i2c_affine(tuple(range(0,6)))
763 # print label_text.i2w_affine(tuple(range(0,6)))
765 # label_text = label_root.add(canvas.CanvasRichText,
767 # width = 10 * cp_.font_size, height = 1 * cp_.font_size,
768 # text = "sample text",
771 #** tried and discarded
773 ### No effect (events not received?):
774 ### label_root.connect("event", select_event)
775 ### label_root.connect("event", focus_event)
778 ### massive delays when zooming:
779 ### tag_size = buff.create_tag(# size = 12 * w_.canvas.view._pixpu,
780 ### size_points = 12,
783 ### core dump after zoom:
784 ### tag_size = buff.create_tag(# size = 12 * w_.canvas.view._pixpu,
791 #* minimal window sample
792 def zoom_spin_button():
793 spinner_adj
= gtk
.Adjustment(2.500,
798 spinner
= gtk
.SpinButton(spinner_adj
, 0.001, 3)
801 l_window
= gtk
.Window()
802 l_window
.connect("delete-event", lambda *args
: l_window
.destroy())
803 l_window
.set_border_width(0)
804 l_window
.add(spinner
)
818 aa
.source_string() # no_source...
820 aa
= reader
.parse("""
826 print aa
.source_string() # no_source...
831 #* program formation from gui; evaluation test.
834 view
.print_info(storage
, prog
, show_macros
= True)
836 pgraph
= w_
.canvas
.view
._pgraph
837 chains
, free_nodes
= pgraph
.identify_programs()
838 print "Free nodes found: ", free_nodes
# modeline / popup?
840 print "Chains found: ",
843 print "============================================"
845 [info(storage
.load(foo
)) for foo
in chains
[0] ]
848 programs
= [pgraph
.chain_to_program(ch
,
852 print "============================================"
857 print "============================================"
859 value
= programs
[0].interpret(def_env
, storage
)
863 cnvs
= w_
.canvas
.view
864 from canvas
import MOVETO_OPEN
, MOVETO
, LINETO
, CURVETO
, END
866 ls_path
= canvas
.path_def_new([(MOVETO
, 10, 10),
872 ls_item
= cnvs
.root().add(canvas
.CanvasBpath
,
874 outline_color
= "black",
875 fill_color
= "green",
877 ls_item
.set_bpath(ls_path
)
882 def affine_scale(sx
, sy
):
883 return (sx
, 0.0, 0.0, sy
, 0.0, 0.0)
885 ls_item
.affine_relative(affine_scale(0.7, 0.3))
888 ls_item
.affine_relative(affine_scale(1/0.7, 1/0.3))
897 cnvs
= w_
.canvas
.view
898 from canvas
import MOVETO_OPEN
, MOVETO
, LINETO
, CURVETO
, END
901 ls_path
= canvas
.path_def_new(path
)
902 ls_item
= cnvs
.root().add(canvas
.CanvasBpath
,
903 width_units
= 1.0/w_
.cp_
.font_size
* 2,
904 outline_color
= "black",
905 fill_color
= "green",
906 cap_style
= gtk
.gdk
.CAP_ROUND
,
907 join_style
= gtk
.gdk
.JOIN_ROUND
,
909 ls_item
.set_bpath(ls_path
)
911 ll
, tt
, rr
, bb
= canvas_item_get_bounds_world(ls_item
)
912 ls_item
.move( 10 + w_
.cp_
.off_x
- ll
,
913 10 + w_
.cp_
.off_y
- tt
,
922 # Rounded corner, leaving the bezier control points on the tangent lines.
932 path
= [(MOVETO
, 0, 0), # p1
943 #*** rounded corner rectangle
944 # rectangle w/ rounded corners, leaving the bezier control points on
946 # Note: using one definition, say on [0,1]x[0,1] and scaling will also
947 # scale the rounded edges, but these must have absolute size. [ie. the
948 # rounding size must be independent of the rectangle size.]
951 def path_rectangle_rounded(el
, er
,
952 et
, eb
, # edge positions
953 cv
, ch
, # corner point indentation
954 sv
, sh
, # spline control point indentation
959 assert ((er
- el
) > 2*ch
)
960 assert ((eb
- et
) > 2*cv
)
962 path
= [(MOVETO
, el
, et
+ cv
),
964 (LINETO
, el
, eb
- cv
),
965 (CURVETO
, # bottom left
971 (LINETO
, er
- ch
, eb
), # bottom right
978 (LINETO
, er
, et
+ cv
),
979 (CURVETO
, # top right
985 (LINETO
, el
+ ch
, et
),
986 (CURVETO
, # top left
1005 test_path(path_rectangle_rounded(el
, er
, et
, eb
,
1008 test_path(path_rectangle_rounded(5, 10, 5, 20,
1014 cnvs
= w_
.canvas
.view
1015 from canvas
import MOVETO_OPEN
, MOVETO
, LINETO
, CURVETO
, END
1018 def __init__(self
, dct
):
1019 self
.__dict
__.update(dct
)
1023 # Scaling is expected to widen or heighten the object, retaining the
1024 # left upper corner. For this to happen with a simple call to
1025 # affine_relative(), the upper left of the original must be at 0,0,
1026 # and the drawing must be in the positive x and y directions.
1028 # The problem here is insufficient use of CanvasGroups.
1030 def draw_path(path
, width_w
, **props
):
1031 # This drawing order shows nothing.
1034 ls_path
= canvas
.path_def_new(path
)
1035 ls_rg
= cnvs
.root().add(canvas
.CanvasGroup
)
1036 ls_item
= ls_rg
.add(canvas
.CanvasBpath
, **props
)
1037 ls_item
.set_bpath(ls_path
)
1041 ll
, tt
, rr
, bb
= canvas_item_get_bounds_world(ls_item
)
1042 ls_item
.move( -ll
, -tt
)
1047 sx
= sy
= width_w
/ (rr
- ll
)
1048 print "scale: ", sx
,sy
1049 ## ls_item.affine_relative( (sx, 0.0, 0.0, sy, 0.0, 0.0) )
1052 ll
, tt
, rr
, bb
= canvas_item_get_bounds_world(ls_item
)
1053 ls_item
.move( 20 + w_
.cp_
.off_x
- ll
,
1054 10 + w_
.cp_
.off_y
- tt
,
1056 return AttrDict(locals())
1059 def draw_path(path
, width_w
, **props
):
1061 ls_path
= canvas
.path_def_new(path
)
1062 ls_rg
= cnvs
.root().add(canvas
.CanvasGroup
)
1063 ls_item
= ls_rg
.add(canvas
.CanvasBpath
, **props
)
1064 ls_item
.set_bpath(ls_path
)
1068 ll
, tt
, rr
, bb
= canvas_item_get_bounds_world(ls_item
)
1069 ls_item
.move( 20 + w_
.cp_
.off_x
- ll
,
1070 10 + w_
.cp_
.off_y
- tt
,
1073 # Scale using the local coordinates (the group is unchanged).
1074 sx
= sy
= width_w
/ (rr
- ll
)
1075 ls_item
.affine_relative( (sx
, 0.0, 0.0, sy
, 0.0, 0.0) )
1077 return AttrDict(locals())
1080 #*** good: proper affine nesting.
1082 # @@ name the groups.
1083 # distinct nested groups for group, center, scale and
1086 def draw_path(path
, width_w
, **props
):
1088 grp_trans
= cnvs
.root().add(canvas
.CanvasGroup
)
1089 grp_scale
= grp_trans
.add(canvas
.CanvasGroup
)
1090 grp_obj
= grp_scale
.add(canvas
.CanvasGroup
)
1093 ls_path
= canvas
.path_def_new(path
)
1094 ls_item
= grp_obj
.add(canvas
.CanvasBpath
, **props
)
1095 ls_item
.set_bpath(ls_path
)
1097 # Reposition for scaling around center.
1099 ll
, tt
, rr
, bb
= canvas_item_get_bounds_world(grp_obj
)
1100 grp_obj
.move( -(ll
+ rr
)/2, -(tt
+ bb
)/2 )
1104 ll
, tt
, rr
, bb
= canvas_item_get_bounds_world(grp_scale
)
1105 sx
= sy
= width_w
/ (rr
- ll
)
1106 grp_scale
.affine_relative( (sx
, 0.0, 0.0, sy
, 0.0, 0.0) )
1108 # Translate for visibility.
1110 ll
, tt
, rr
, bb
= canvas_item_get_bounds_world(grp_trans
)
1111 grp_trans
.move( 20 + w_
.cp_
.off_x
- ll
,
1112 10 + w_
.cp_
.off_y
- tt
,
1115 return AttrDict(locals())
1133 # (LINETO, 0,0), # use end to close (avoid irregularities)
1138 dct
= draw_path(path
,
1140 width_units
= 1.0/w_
.cp_
.font_size
* 2,
1141 outline_color
= "black",
1142 fill_color
= "black",
1143 cap_style
= gtk
.gdk
.CAP_ROUND
,
1144 join_style
= gtk
.gdk
.JOIN_ROUND
,
1151 print w_
.dct
.ls_item
.i2w_affine(tuple(range(0,6)))
1152 print w_
.dct
.grp_trans
.i2w_affine(tuple(range(0,6)))
1155 w_
.dct
.grp_scale
.affine_relative( (3, 0.0, 0.0, 3, 0.0, 0.0) ) # scale
1156 w_
.dct
.grp_trans
.move(2,2)
1158 w_
.dct
.grp_trans
.destroy()
1162 cnvs
= w_
.canvas
.view
1163 list_
= l3List(w_
, cnvs
)
1165 cnvs
.display_bounding_box( list_
._head
.get_bounds())
1168 # list_._root_group.connect("event", lambda *a: list_.drag_event(*a))
1169 # list_._head._root_group.connect("event", lambda *a: list_.drag_event(*a))
1170 # list_._alist._root_group.connect("event", lambda *a: list_.drag_event(*a))
1172 #* bounding box testing
1173 def bounds_world(item
):
1174 x
, y
, u
, v
= item
.get_bounds()
1175 return item
.i2w(x
,y
) + item
.i2w(u
,v
)
1177 # Outline bounds != outline corners. Why?
1179 print rawt
._loutline
.get_bounds()
1180 print [rawt
._loutline
.get_property(prop
) for prop
in ['x1', 'y1', 'x2', 'y2']]
1183 self__lgroup
= cnvs
.root().add(canvas
.CanvasGroup
)
1185 self__textview
= gtk
.TextView()
1186 self__ltext
= self__lgroup
.add(canvas
.CanvasWidget
,
1187 size_pixels
= False,
1188 height
= 1.5, ### params
1191 widget
= self__textview
,
1195 x1
, y1
, x2
, y2
= self__ltext
.get_bounds() # world units
1196 x1
-= 5 / cnvs
._pixpu
1197 y1
-= 5 / cnvs
._pixpu
1198 x2
+= 5 / cnvs
._pixpu
1199 y2
+= 5 / cnvs
._pixpu
1201 self__loutline
= self__lgroup
.add(
1205 fill_color
= 'white',
1206 outline_color
= 'black',
1207 width_pixels
= cp_
.outline_width_normal
,
1210 st
.__dict
__.update( locals())
1215 print st
.self__loutline
.get_bounds()
1216 print [st
.self__loutline
.get_property(prop
)
1217 for prop
in ['x1', 'y1', 'x2', 'y2']]
1220 #################################
1222 self__lgroup
= cnvs
.root().add(canvas
.CanvasGroup
)
1224 self__loutline
= self__lgroup
.add(
1228 fill_color
= 'white',
1229 outline_color
= 'black',
1230 width_pixels
= cp_
.outline_width_normal
,
1233 st
.__dict
__.update( locals())
1238 print st
.self__loutline
.get_bounds()
1239 print [st
.self__loutline
.get_property(prop
)
1240 for prop
in ['x1', 'y1', 'x2', 'y2']]
1243 #################################
1245 self__lgroup
= cnvs
.root().add(canvas
.CanvasGroup
)
1247 self__loutline
= self__lgroup
.add(
1251 fill_color
= 'white',
1252 outline_color
= 'black',
1255 return utils
.Shared(**locals())
1259 print st
.self__loutline
.get_bounds()
1260 print [st
.self__loutline
.get_property(prop
)
1261 for prop
in ['x1', 'y1', 'x2', 'y2']]
1264 print st
.self__ltext
.get_bounds()
1266 #################################
1268 x
, y
, u
, v
= bounds_world(rawt
._loutline
)
1271 print "group bounds"
1272 print rawt
._root
_group
.get_bounds()
1273 x
, y
, u
, v
= bounds_world(rawt
._root
_group
)
1275 print bounds_world(rawt
._root
_group
)
1278 print rawt
._ltext
.get_bounds()
1279 x
, y
, u
, v
= bounds_world(rawt
._ltext
)
1281 print bounds_world(rawt
._ltext
)
1284 print rawt
._loutline
.i2w_affine(tuple(range(0,6)))
1285 print rawt
._root
_group
.i2w_affine(tuple(range(0,6)))
1288 print rawt
._loutline
.i2c_affine(tuple(range(0,6)))
1289 print rawt
._root
_group
.i2c_affine(tuple(range(0,6)))
1293 cnvs
= w_
.canvas
.view
1294 l_line
= cnvs
.root().add(canvas
.CanvasLine
,
1295 fill_color
= "black",
1302 line_style
= gtk
.gdk
.SOLID
,
1303 cap_style
= gtk
.gdk
.CAP_ROUND
,
1304 join_style
= gtk
.gdk
.JOIN_ROUND
,
1309 l_line
= cnvs
.root().add(canvas
.CanvasLine
,
1310 fill_color
= "blue",
1317 line_style
= gtk
.gdk
.SOLID
,
1318 cap_style
= gtk
.gdk
.CAP_ROUND
,
1319 join_style
= gtk
.gdk
.JOIN_ROUND
,
1323 print l_line
.get_property('points')
1327 cnvs
= w_
.canvas
.view
1328 l_line
= cnvs
.root().add(canvas
.CanvasPolygon
,
1329 fill_color
= "black",
1336 cap_style
= gtk
.gdk
.CAP_ROUND
,
1337 join_style
= gtk
.gdk
.JOIN_ROUND
,
1340 l_line
= cnvs
.root().add(canvas
.CanvasPolygon
,
1341 fill_color
= "black",
1349 cap_style
= gtk
.gdk
.CAP_ROUND
,
1350 join_style
= gtk
.gdk
.JOIN_ROUND
,
1353 print l_line
.get_bounds()
1355 print l_line
.get_property('points')
1359 #* canvas ast insertion / display
1360 tree
= reader
.parse("[d,e]")
1361 tree
= reader
.parse("function = { |x,y| x - y }")
1363 tree
= reader
.parse("""
1364 [ function = not_yet({ |x,y| x - y }),
1365 macro = not_yet([ |x,y| z = x - y ]),
1366 raw_expression = a - b,
1369 from operator import *
1370 from l3lang.test.functions import axpy
1375 tree
.setup(empty_parent(), def_env
, storage
)
1376 view
.print_info(storage
, tree
)
1377 w_
.lib_canvas
.view
.start_add_l3tree(tree
)
1381 pcon
= w_
.lib_canvas
.view
.get_pango_context()
1382 pcon
.list_families()
1383 pcon
.list_families()[0].get_name()
1384 pcon
.get_font_description()
1385 print pcon
.get_font_description().get_size() / pango
.SCALE
1390 font_desc
= pcon
.get_font_description().copy()
1391 font_desc
.set_family("monospace")
1392 font_desc
.set_size(sz_orig
* pango
.SCALE
)
1394 # Set new canvas default.
1395 pcon
.set_font_description(font_desc
)
1399 font
= pcon
.load_font(font_desc
)
1400 metrics
= font
.get_metrics()
1401 # Font sizes are in pango_units.
1402 # 1 point == PANGO_SCALE pango_units
1403 # 1 pango_point = 1/72 inch
1404 fheight
= (metrics
.get_ascent() + metrics
.get_descent())
1405 fwidth
= metrics
.get_approximate_digit_width()
1406 # print fwidth / pango.SCALE
1407 # print fheight / pango.SCALE
1408 return fwidth
, fheight
1409 ## logi, ink = font.get_glyph_extents("m")
1414 # No content in last line.
1421 return reduce(max, [len(line
) for line
in st
.split('\n')], 0)
1424 #** Sizing using test string.
1425 view
= w_
.canvas
.view
1426 ra
= w_
.canvas
.view
.root().add
1427 c_string
= "01234\n" * 8
1428 c_label
= ra(canvas
.CanvasText
,
1431 font_desc
= font_desc
,
1433 ## c_label.destroy()
1438 l1
, t1
, r1
, b1
= c_label
.get_bounds()
1439 print "x: cu / char", (r1
- l1
) / width(c_string
)
1440 print "y: cu / char", (b1
- t1
) / lines(c_string
)
1441 print view
._pixpu
_base
1443 cu_p_char
= (r1
- l1
) / width(c_string
)
1444 view
._pixpu
= view
._pixpu
* cu_p_char
1445 view
.set_pixels_per_unit(view
._pixpu
)
1451 #*** Draw long string.
1452 lng_str
= "the test string,\nusing two lines"
1453 lng_label
= ra(canvas
.CanvasText
,
1455 anchor
= gtk
.ANCHOR_NORTH_WEST
,
1457 font_desc
= font_desc
,
1459 ## lng_label.destroy()
1460 # Verify canvas size ratio...
1461 l1
, t1
, r1
, b1
= lng_label
.get_bounds()
1462 print "x: cu / char", (r1
- l1
) / width(lng_str
)
1463 print "y: cu / char", (b1
- t1
) / lines(lng_str
)
1465 # Compare to font metric ratio...
1466 wid
, hei
= dump_metrics()
1467 print "metric h / w", 1.0*hei
/wid
1470 #*** Draw substring at proper position.
1472 # sub_start = lng_str.find(sub_str)
1475 sub_str
= lng_str
.split('\n')[sub_top
][sub_lef
:]
1476 sub_label
= ra(canvas
.CanvasText
,
1477 anchor
= gtk
.ANCHOR_NORTH_WEST
,
1478 fill_color
= "blue",
1481 font_desc
= font_desc
,
1483 sub_label
.move(sub_lef
, 1.0 * sub_top
/ wid
* hei
)
1484 ## sub_label.destroy()
1485 # Verify size ratio...
1486 l1
, t1
, r1
, b1
= sub_label
.get_bounds()
1487 print "x: cu / char", (r1
- l1
) / width(sub_str
)
1488 print "y: cu / char", (b1
- t1
) / lines(sub_str
)
1490 # Compare to font metric ratio...
1491 wid
, hei
= dump_metrics()
1492 print "metric h / w", 1.0*hei
/wid
1497 #*** Set new font sizes.
1498 fsize
= sz
* pango
.SCALE
1499 font_desc
.set_size(fsize
)
1500 c_label
.set_property('size', fsize
)
1501 lng_label
.set_property('size', fsize
)
1502 sub_label
.set_property('size', fsize
)
1504 #*** Set 1 cu = 1 ex (horizontal)
1505 l1
, t1
, r1
, b1
= c_label
.get_bounds()
1506 print "x: cu / char", (r1
- l1
) / width(c_string
)
1507 print "y: cu / char", (b1
- t1
) / lines(c_string
)
1509 cu_p_char
= (r1
- l1
) / width(c_string
)
1510 view
._pixpu
= view
._pixpu
* cu_p_char
1511 view
.set_pixels_per_unit(view
._pixpu
)
1513 #*** Check vertical [cu]
1514 l1
, t1
, r1
, b1
= c_label
.get_bounds()
1515 print "x: cu / char", (r1
- l1
) / width(c_string
)
1516 print "y: cu / char", (b1
- t1
) / lines(c_string
)
1517 #*** Compare font metrics (points) at new sizes
1518 wid
, hei
= dump_metrics()
1519 print "metric h / w", 1.0*hei
/wid
1522 #** Find matching font metric.
1523 wid
, hei
= dump_metrics()
1524 h_o_w
= 1.0 * hei
/ wid
1525 print "metric h / w", h_o_w
1527 sz_bnds
= range(4, 41)
1528 print "pt. height/ "
1529 print "size widtht "
1530 print "-------------------"
1532 fsize
= sz
* pango
.SCALE
1533 font_desc
.set_size(fsize
)
1534 wid
, hei
= dump_metrics()
1535 sz_hw
= 1.0 * hei
/ wid
1536 print "%-6d %.5f" % (sz
, sz_hw
)
1540 #* tree & clone structure
1541 tw
= TreeWork(storage
)
1542 loads
= storage
.load
1543 printi
= lambda xx
: print_info(storage
, xx
)
1544 printai
= lambda xx
: print_all_info(storage
, xx
)
1546 # id from first Symbol('abserr') inside If
1549 printai(tw
.find_root(loads(mid
)))
1552 # add header, run program, compare again
1554 printai(tw
.find_root(loads(mid
)))
1557 # change `if abserrr...` to `if prints(abserrr...`
1558 # run again, compare
1559 # no longer connected:
1560 # printai(loads(mid))
1561 # printai(tw.find_root(loads(mid)))
1562 printai(loads(30150)) # main program
1564 # search for prints...
1565 # The subtree was replaced and is re-run, including abserr()
1568 # stats for printing all info:
1571 wrt return 1.0 / 2 * (xk + x / xk)
1572 single clone prefix dump, 20 lines
1577 prefix dump, no clones, 135 lines
1578 prefix dump, all clones, 770 lines
1579 infix, 30 lines - 10 comment = 20 lines
1580 ratio (/ 135.0 20) 6.75
1588 show only clone starting points: Function, Program
1593 def print_dot(storage
, tree
):
1594 # Print every expression and its clones.
1595 # This is far too much detail; only l3 Function clones are needed.
1597 iter = tree
.prefix_grpd(storage
)
1599 nd
, clone_lev
, indent
= iter.next()
1600 print nd
.source_string()
1604 nd
, clone_lev
, indent
= iter.next()
1605 if clone_lev
== '(':
1607 nd
, clone_lev
, indent
= iter.next()
1608 print nd
._id
, nd
.source_string()
1609 if clone_lev
== ')':
1612 except StopIteration:
1617 def print_dot(storage
, tree
):
1618 # Print expressions once, including clones.
1619 # For every expression, include the id and link to clone
1623 # The if expressions are missing their lead indent.
1624 # The clone sources may not appear explicitly (with id); e.g., in
1627 # if abserr(x, xn) > 0.0001:
1628 # return loop(x, xkp1(x, xn))
1632 # 30254 -> 30920 /* clone of */
1633 # 30867 -> 30920 /* cloned by */
1635 # the Function 30254 is referenced, but only shows up inside other
1639 iter = tree
.prefix_grpd(storage
)
1640 gaa
= lambda id: storage
.get_attributes(
1641 id, ["clone_of", "cloned_by", "interp_env"])
1645 nd
, clone_lev
, indent
= iter.next()
1646 if isinstance(nd
, Program
):
1648 print clone_lev
, nid
1649 print nd
.source_string()
1651 of
, by
, env
= gaa(nid
)
1653 print "%(of)d -> %(nid)d /* clone of */" % locals()
1655 print "%(by)d -> %(nid)d /* cloned by */" % locals()
1657 except StopIteration:
1662 def print_dot(storage
, tree
):
1663 # Print expressions once, including clones.
1664 # For every expression, include the id and link to clone
1668 # - The if expressions are missing their lead indent.
1669 # - The clone sources may not appear explicitly (with id);
1670 # - Displaying ALL clones takes too much room;
1672 iter = tree
.prefix_grpd(storage
)
1673 gaa
= lambda id: storage
.get_attributes(
1674 id, ["clone_of", "cloned_by", "interp_env"])
1678 nd
, clone_lev
, indent
= iter.next()
1679 if isinstance(nd
, Program
):
1681 print nid
, '[label="%s"]; /* clone level %d */' % \
1682 (repr(nd
.source_string())[1:-1].replace(r
"\n",
1686 of
, by
, env
= gaa(nid
)
1688 print "%(of)d -> %(nid)d [color=green]; /* clone of */" % locals()
1690 print "%(by)d -> %(nid)d [color=blue]; /* cloned by */" % locals()
1692 except StopIteration:
1698 page = "8.5,11.0"; /* size of single physical page */
1699 size="7.5,10.0"; /* graph size */
1703 node [shape=plaintext];
1705 print_dot(storage
, t_tree
)
1708 # Program P is a clone of a Function F
1709 # cloned by a Call C
1713 sload
= storage
.load
1717 t_string
= open('../l3lang/test/sqrt.l3').readlines()
1718 t_tree
= reader
.parse("".join(t_string
))
1719 t_tree
.setup(empty_parent(), def_env
, storage
)[0].set_outl_edges(self
.w_
, None)
1720 # print_info(storage, t_tree)
1721 print_all_info(storage
, t_tree
)
1722 w_
.canvas
.view
.clear_state( )
1723 w_
.canvas
.view
.start_add_l3tree(t_tree
)
1729 # put tree on canvas.
1732 import linecache
, sys
1733 def traceit(frame
, event
, arg
):
1735 file = frame
.f_code
.co_filename
1737 if "usr/tmp/python-" in file:
1738 lineno
= frame
.f_lineno
1739 line
= linecache
.getline(file, lineno
)
1740 print "line %d: %s" % (lineno
, line
.rstrip())
1745 sys
.settrace(traceit
)
1747 import linecache
, sys
1748 def traceit(frame
, event
, arg
):
1750 file = frame
.f_code
.co_filename
1753 ## if "usr/tmp/python-" in file:
1754 lineno
= frame
.f_lineno
1755 line
= linecache
.getline(file, lineno
)
1756 print "%s:%d: %s" % (file, lineno
, line
.rstrip())
1761 sys
.settrace(traceit
)
1764 pdb
.run('w_.canvas.view.centered_zoom(0.1)')
1766 # The warning is printed through
1769 # intel-linux-2.4/python/lib/python2.4/warnings.py
1774 self
._canvas
.display_bounding_box(canvas_item_get_bounds_world(self
._blank
))
1775 self
._canvas
.display_bounding_box(self
.get_bounds_world())
1776 self
._canvas
.display_bounding_box(canvas_item_get_bounds_world(self
._d
_lhs
_marker
))
1777 self
._canvas
.display_bounding_box(canvas_item_get_bounds_world(self
._root
_group
))
1778 self
._canvas
.display_bounding_box(canvas_item_get_bounds_world(self
._blank
))
1781 # self._canvas.display_bounding_box(self.get_bounds_world())
1782 self
._canvas
.display_bounding_box(canvas_item_get_bounds_world(self
._root
_group
))
1784 self
._canvas
.display_bounding_box(self
._expander
.get_bounds_world())
1786 #* phenix refine display
1788 t_tree
= reader
.parse_file(
1789 os
.path
.expandvars('$SPXROOT/l3lang/test/refine.l3'))
1790 t_tree
.setup(empty_parent(), def_env
, storage
).set_outl_edges(self
.w_
, None)
1791 ## print_info(storage, t_tree)
1792 ## view.print_all_info(storage, t_tree)
1793 w_
.canvas
.view
.clear_state()
1794 # SLOW! But interesting to watch.
1795 w_
.canvas
.view
.start_add_l3tree(t_tree
)
1798 #** program, limited depth
1799 t_tree
= reader
.parse_file(
1800 os
.path
.expandvars('$SPXROOT/l3lang/test/refine.l3'))
1801 t_tree
.setup(empty_parent(), def_env
, storage
).set_outl_edges(self
.w_
, None)
1803 # hide nodes at level N
1804 vista
= w_
.canvas
.view
._vis
_tab
= VisibilityTable() # Reset.
1806 vista
.hide_at(t_tree
, N
)
1809 # ld = w_.state_.storage.load
1810 # for id in vista.keys():
1814 w_
.canvas
.view
.clear_state()
1815 w_
.canvas
.view
.start_add_l3tree(t_tree
)
1819 t_tree
= reader
.parse("""
1822 file_name = 'None',},
1829 test_flag_value = 'None',
1830 disable_suitability_test = 'False',},
1831 experimental_phases = {
1835 file_name = 'None',},}
1837 t_tree
.setup(empty_parent(), def_env
, storage
).set_outl_edges(self
.w_
, None)
1838 ## print_info(storage, t_tree)
1839 ## view.print_all_info(storage, t_tree)
1840 w_
.canvas
.view
.clear_state()
1841 w_
.canvas
.view
.start_add_l3tree(t_tree
)
1844 #** various file's layout
1845 def layout_file(name
, level
= 7):
1847 t_tree
= reader
.parse_file(os
.path
.expandvars(name
))
1848 t_tree
.setup(empty_parent(), def_env
, storage
).set_outl_edges(self
.w_
, None)
1850 # hide nodes at LEVEL.
1851 vista
= w_
.canvas
.view
._vis
_tab
= VisibilityTable() # Reset.
1852 vista
.hide_at(t_tree
, level
)
1855 w_
.canvas
.view
.clear_state()
1856 w_
.canvas
.view
.start_add_l3tree(t_tree
)
1860 layout_file('$SPXROOT/l3lang/test/refine.l3')
1861 layout_file('$SPXROOT/l3lang/test/ctf.l3')
1862 layout_file('$SPXROOT/l3lang/test/golem_1.l3')
1864 #* legible data dumping via PyYAML (yaml)
1871 print yaml
.dump(['foo', 'bar', 'baz'])
1875 # 2. yaml.representer.RepresenterError: recursive objects are not
1876 # allowed: Env(30288, Env-30269-skel.blck, [Program at 0x40d79a8c, id
1878 yaml
.dump(get_current_state(w_
), stream
= sys
.stdout
)
1879 ## yaml.dump(py_obj, stream = None)
1883 #* legible data dumping via syck (yaml)
1891 print syck
.dump(['foo', 'bar', 'baz'])
1896 # [Mon Apr 30 10:28:42 2007]
1897 # Fails on <weakproxy> instances
1899 # AttributeError: viewList instance has no attribute '__reduce__'
1900 # and there is no customization hook.
1901 syck
.dump(get_current_state(w_
), sys
.stdout
)
1904 # [Mon Apr 30 10:35:19 2007]
1905 # Try a detour via pickle. This fails too:
1906 # (.:16125): GLib-GObject-WARNING **: cannot retrieve class for
1907 # invalid (unclassed) type `<invalid>' ** ERROR **: file pygenum.c:
1908 # line 60 (pyg_enum_repr): assertion failed:
1909 # (G_IS_ENUM_CLASS(enum_class)) aborting...
1910 # /net/hooknose/scratch1/hohn/t2/sparx-bin/bin/sparx.wrap: line 36:
1911 # 16125 Aborted python -i
1912 # Process Python exited abnormally with code 134
1914 # Works after removing some more unnecessary gtk information.
1916 n_tree
= pickle
.loads( pickle
.dumps(get_current_state(w_
), protocol
=2))
1917 syck
.dump(n_tree
, sys
.stdout
)
1920 # l3 graphical node (local eval)
1922 print syck
.dump(self
)
1924 # l3 tree (local eval)
1925 print syck
.dump(self
._l3tree
)
1936 [horizontal layout, requires, >
1937 display choice display choice display choice
1938 display choice display choice display choice
1943 requires: display choice (direction)
1947 2d layout -- requires -- comment parsing
1951 #** Compare state files via their yaml representations.
1953 from l3lang
.utils
import *
1955 n_tree_1
= file_unpickle('st.1')
1956 syck
.dump(n_tree_1
, open('st.1.syck', 'w'))
1958 n_tree_2
= file_unpickle('st.2')
1959 syck
.dump(n_tree_2
, open('st.2.syck', 'w'))
1966 ==== local console
; exit with end
-of
-file (ctrl
-d
) ====
1968 import l3lang
.view
as view
1969 view
.print_all_info(self
.w_
.state_
.storage
, self
._l3tree
)
1974 lcan
= w_
.lib_canvas
.view
1975 pprint(lcan
.root().item_list
)
1977 pprint(self
._root
_group
.item_list
)
1979 #* visibility tables testing.
1980 lcan
= w_
.lib_canvas
.view
1982 pprint(lcan
._vis
_tab
)
1984 # Assuming constant tree and node offset:
1985 manually_chosen
= {30098: 'vis_hide',
1992 lcan
._vis
_tab
.replace_table( manually_chosen
)
1993 # use "apply mark" on topmost element.
1996 /tmp
/st
.lib
- compacted library only
.
1997 /tmp
/st
.for-pre
-run
- loop sample
2000 #* l3 loop templates
2001 #** range iteration, function version.
2002 # for i in range(n,m):
2004 def _for(_n
, _m
, body
):
2007 return _for(_n
+ 1, _m
, body
)
2011 _for(1, 7, {|i|
print_(i
) })
2012 _for(30, 31, {|i| foo
})
2013 # _for(1, 5, lambda i: print_(i) )
2014 # Nesting is no problem:
2017 {|j|
print_("%d %d" % (i
,j
)) })})
2018 #** range, macro version
2019 # [Thu Feb 9 16:29:39 2006]
2020 # Better not to use this one...
2022 # for i in range(n,m):
2028 return _form(_n
+ 1, _m
)
2034 # Paste, insert body, change "i" to new name, run.
2035 # Nesting requires unique _for names; the following has a problem::
2045 print_("%d %d" % (ii
, jj
))
2047 return _for(_n
+ 1, _m
)
2052 return _for(_n
+ 1, _m
) # wrong _for
2058 #* context for values
2060 # Given multiple values from a single expression and its clones, find
2061 # the dynamic context -- the changed arguments to
2062 # enclosing Functions, or (sigh) the changed values of the stacked
2063 # bindings. Complicated.
2066 st
= w_
.state_
.storage
2067 load
= w_
.state_
.storage
.load
2068 tw
= ast
.TreeWork(w_
.state_
.storage
)
2074 # LOADING RESETS THE STORAGE TABLE AND OTHERS!
2076 # U s e t h e w _ a c c e s s p a t h !
2079 st
= w_
.state_
.storage
2080 load
= w_
.state_
.storage
.load
2081 tw
= ast
.TreeWork(w_
.state_
.storage
)
2083 # Toplevel id (static).
2084 l3nd
= w_
.selector
.get_selection()
2085 c_id
= l3nd
._l3tree
._id
2087 print load(c_id
).source_string()
2090 clone_l
= st
.get_attribute(c_id
, "interp_clone")
2093 # If no values: is the correct storage instance used?
2095 print id, st
.get_attribute(id, 'interp_result')
2097 # For deeper nesting, follow the clone trail.
2098 st
.get_attribute(id, "interp_clone")
2101 cctxt
= tw
.get_call_ctxt(c_id
)
2105 tw
.print_call_ctxt(cctxt
)
2107 # If clones were cloned, these may be non-empty?
2108 # for id in clone_l:
2109 # print tw.get_call_ctxt(id)
2111 # symbol('i') evaluated, has time stamp, but no value, no env? --
2112 # Use correct storage!
2114 print id, st
.get_attribute(id, 'interp_result')
2115 print id, st
.get_attribute(id, 'interp_env')
2116 pprint( [id, st
.get_attribute_table(id)])
2119 parent
= tw
.find_first_parent(load(id), (ast
.Function
, ast
.Program
))
2120 view
.print_info(st
, parent
)
2121 print parent
._eval
_env
.all_bindings() # empty
2122 print parent
._eval
_env
.lookup('i') # value, at last...
2125 print parent
._eval
_env
.ie_lookup('i')
2128 #* environment viewing
2129 # local eval for program
2130 (self
._l3tree
)._block
_env
.all_bindings()
2131 (self
._l3tree
)._eval
_env
.all_bindings()
2132 (self
._l3tree
)._eval
_env
.ls()
2133 # has print_'s time stamp, but no print.
2134 # [after loading /tmp/st.post-loop ]
2136 #* state examination
2137 # Failed to complete:
2138 print syck
.dump(get_current_state(w_
))
2140 import pickle
, pickletools
2141 st
= get_current_state(w_
)
2142 pickletools
.dis(pickle
.dumps(st
, protocol
=2))
2148 print (self
._l3tree
)._eval
_env
.lookup('print_')
2149 # <function print_ at 0x40402ed4>
2151 n_tree
= pickle
.loads( pickle
.dumps(self
._l3tree
, protocol
=2))
2152 print (n_tree
)._eval
_env
.lookup('print_')
2154 #* voea etc. sample problem
2155 #** python support functions
2156 from l3lang
.external
.golem
import \
2157 create_write_gridprojections
, \
2165 # caption = 'Delta theta (degrees)',
2168 anglist
= Golem
.voea(deltheta
)
2175 # caption = 'Filename of volume to project',
2176 volfile
= "volext.spi"
2178 # caption = 'Kaiser-Bessel K parameter',
2181 # caption = 'Kaiser-Bessel alpha parameter',
2184 # caption = 'Projection file pattern',
2185 filepattern
= 'proj{****}.tst'
2187 # print vol.get_xsize()
2188 create_write_gridprojections(
2189 Golem
.getImage(volfile
),
2190 anglist
, K
, alpha
, filepattern
)
2195 # caption = 'Spider doc filename',
2197 docfname
= 'foo-doc.tst'
2198 anglist2doc(anglist
, docfname
)
2205 # caption = 'Projection file pattern',
2206 filepattern
= 'proj{****}.tst' # above!
2208 # caption = 'Projection start index',
2211 # caption = 'Projection stop index',
2214 # caption = 'Name of angle Spider doc file',
2215 angdocfile
= docfname
# above!
2217 # caption = 'Name of reconstructed volume',
2218 newvolname
= 'newvol.spi'
2219 spidbprg(filepattern
,
2228 # comparison in v4 shows newvolname to be poor...
2230 #* voea/bp as single program
2232 tree
= reader
.parse("""
2234 from l3lang.external.golem import \
2235 create_write_gridprojections, \
2241 def _for(_n, _m, body):
2244 return _for(_n + 1, _m, body)
2250 anglist = Golem.voea(deltheta)
2257 # caption = 'Filename of volume to project',
2258 volfile = "volext.spi"
2260 # caption = 'Kaiser-Bessel K parameter',
2263 # caption = 'Kaiser-Bessel alpha parameter',
2266 # caption = 'Projection file pattern',
2267 filepattern = 'proj{****}.tst'
2269 # print vol.get_xsize()
2270 create_write_gridprojections(
2271 Golem.getImage(volfile),
2272 anglist, K, alpha, filepattern)
2277 # caption = 'Spider doc filename',
2279 docfname = 'foo-doc.tst'
2280 anglist2doc(anglist, docfname)
2287 # caption = 'Projection file pattern',
2288 filepattern = 'proj{****}.tst' # above!
2290 # caption = 'Projection start index',
2293 # caption = 'Projection stop index',
2296 # caption = 'Name of angle Spider doc file',
2297 angdocfile = docfname # above!
2299 # caption = 'Name of reconstructed volume',
2300 newvolname = 'newvol.spi'
2301 spidbprg(filepattern,
2313 #** Directory setup.
2315 ev
= os
.path
.expandvars
2316 os
.chdir(ev('$SPXROOT/l3lang/test'))
2318 shutil
.rmtree('bp-test')
2320 os
.chdir(ev('$SPXROOT/l3lang/test/bp-test'))
2322 shutil
.copyfile(ev('$SPXROOT/l3lang/test/volext.spi'), 'volext.spi')
2325 #** setup and execute
2327 tree
.setup(empty_parent(), def_env
, w_
.state_
.storage
).set_outl_edges(self
.w_
, None)
2328 # view.print_info(storage, tree)
2329 w_
.canvas
.view
.start_add_l3tree(tree
)
2333 from l3lang
import utils
, reader
, ast
, view
, interp
2334 play_env
= interp
.get_child_env(def_env
, storage
)
2335 tree
.setup(empty_parent(), def_env
, storage
).set_outl_edges(self
.w_
, None)
2336 tree
.interpret(def_env
, storage
)
2337 pprint(os
.listdir(os
.getcwd()))
2341 #* voea/bp using docstrings
2342 # /net/cci/hohn/w/tmp/eman2/libpyEM/Golem
2344 tree
= reader
.parse("""
2346 from l3lang.external.golemwrap import *
2349 def _for(_n, _m, body):
2352 return _for(_n + 1, _m, body)
2357 anglist = voea(deltheta)
2362 filepattern = 'proj{****}.tst'
2367 gridproj(getImage("volext.spi"), # nesting...
2371 {filepattern = filepattern})
2374 docfname = 'foo-doc.tst'
2378 anglist2doc(anglist, docfname)
2383 spidbprg(filepattern,
2393 #** Directory setup.
2395 ev
= os
.path
.expandvars
2396 os
.chdir(ev('$SPXROOT/l3lang/test'))
2398 shutil
.rmtree('bp-test')
2400 os
.chdir(ev('$SPXROOT/l3lang/test/bp-test'))
2402 shutil
.copyfile(ev('$SPXROOT/l3lang/test/volext.spi'), 'volext.spi')
2405 #** setup and execute
2407 tree
.setup(empty_parent(), def_env
, w_
.state_
.storage
).set_outl_edges(self
.w_
, None)
2408 # view.print_info(storage, tree)
2409 w_
.canvas
.view
.start_add_l3tree(tree
)
2413 from l3lang
.external
import golemwrap
2414 from l3lang
import utils
, reader
, ast
, view
, interp
2415 play_env
= interp
.get_child_env(def_env
, storage
)
2416 tree
.setup(empty_parent(), play_env
, storage
)
2417 tree
.interpret(play_env
, storage
)
2418 pprint(os
.listdir(os
.getcwd()))
2420 #* voea/bp w/o spider
2422 tree
= reader
.parse("""
2425 #** Directory setup.
2427 ev
= os
.path
.expandvars
2428 os
.chdir(ev('$SPXROOT/l3lang/test'))
2430 shutil
.rmtree('bp-test')
2432 os
.chdir(ev('$SPXROOT/l3lang/test/bp-test'))
2434 shutil
.copyfile(ev('$SPXROOT/l3lang/test/volext.spi'), 'volext.spi')
2437 #** setup and execute
2439 tree
.setup(empty_parent(), def_env
, w_
.state_
.storage
)
2440 # view.print_info(storage, tree)
2441 w_
.canvas
.view
.start_add_l3tree(tree
)
2445 from l3lang
import utils
, reader
, ast
, view
, interp
2446 play_env
= interp
.get_child_env(def_env
, storage
)
2447 tree
.setup(empty_parent(), def_env
, storage
)
2448 tree
.interpret(def_env
, storage
)
2449 pprint(os
.listdir(os
.getcwd()))
2454 view
.print_info(storage
, tree
)
2456 pri( reader
.parse("aa = ff(xx)"))
2457 pri( reader
.parse("aa = ff(xx, yy)"))
2458 pri( reader
.parse("! aa = ! ff( !xx)"))
2460 pri( reader
.parse("aa = ff(xx)"))
2461 pri( reader
.parse("(aa, bb) = ff(xx)"))
2463 reader
.parse("!! aa symbol")
2466 # ---------------------------------
2467 from l3lang
.ast
import *
2468 l3tree
= reader
.parse("a = f(x)")[0][0]
2469 l3tree
= reader
.parse("(a,b) = f(x)")[0][0]
2470 l3tree
= reader
.parse("(a,b) = f(x,y)")[0][0]
2471 l3tree
= reader
.parse("a = f(x,y)")[0][0]
2473 # ---------------------------------
2474 # Test for the special case `a = f(x)` and its
2477 # def add_l3tree_set_tuple()
2481 Call(Marker('ff'), Marker('xx')))):
2482 # Convert to a list of args and list of return values.
2483 _function
= ma
['ff'] # Symbol or expr.
2484 _in
= ma
['xx'] # aList of args.
2486 if ma
.match(out
, # a,b,... =
2487 Tuple(Marker('aa'))):
2488 _out
= ma
['aa'] # aList
2489 elif ma
.match(out
, # a =
2490 MarkerTyped(String('aa'), Symbol('symbol'))):
2491 _out
= aList([ma
['aa']]) # aList
2495 # Get documentation for _in, _out, and _function.
2496 print _out
, _function
, _in
2498 # Get paths for editables.
2499 # Using the unique id.
2505 # The initial try at an expression marker. This version is too buggy;
2506 # the new one is in widgets.py, based on a merge of l3If and
2513 def __init__(self
, w_
, cnvs
, l3_parent
, l3_pidx
, d_expr
):
2516 - `l3_parent` The raw l3 ast parent of d_expr._l3tree.
2517 Without propagation of events from the l3 ast to its
2518 display (the current case), l3_parent should not be
2520 - `l3_pidx` The index of d_expr._l3tree in l3_parent.
2521 - `d_expr` The displayed expression to be handled here.
2523 assert isinstance(d_expr
, (wdgt
.l3aList
, wdgt
.l3Base
))
2524 assert isinstance(l3_parent
, (ast
.astType
,))
2529 self
._l3_parent
= l3_parent
2530 self
._l3_pidx
= l3_pidx
2531 self
._d
_expr
= d_expr
2534 self
._root
_group
= cnvs
.root().add(canvas
.CanvasGroup
)
2535 self
._d
_parent
= None
2536 self
._destroy
_hook
= [] # (func -> None) list
2537 self
._vis
_indic
= None # visibility status indicator
2540 self
._marker
_points
= {}
2541 self
._d
_marker
= self
.draw_marker()
2542 self
._d
_marker
.lower_to_bottom()
2544 # Inform obj of marker.
2545 # This directs physical detachment of d_expr from daExprMarker to
2546 # daExprMarker.detach, which forwards the tree detachment.
2547 d_expr
.setup_marker(self
._d
_marker
, self
)
2550 # Align with respect to d_expr.
2553 ## _, y, x, _ = d_if.get_bounds()
2554 ## u, v, _, _ = d_cond.get_bounds()
2555 ## d_cond.move(x - u, y - v)
2559 self
._d
_marker
.connect("event", lambda *a
: self
.insert_event(*a
))
2560 # Forwarding to parent is implicit.
2561 # # self._root_group.connect("event", lambda *a:
2562 # # self.drag_event(*a))
2563 daExprMarker
.__init
__ = __init__
2566 #** destroy subtree and display
2570 self
._root
_group
.destroy()
2571 daExprMarker
.destroy
= destroy
2573 l3Base
.add_destroy_hook
= add_destroy_hook
2574 l3Base
._run
_destroy
_hook
= _run_destroy_hook
2578 l3Nested
.drag_event
= drag_event
2581 def detach(self
, d_expr
):
2582 if d_expr
!= self
._d
_expr
:
2583 raise DisplayError("Detaching invalid child.")
2585 # Reparent graphical parts.
2586 d_expr
.reparent_to_root()
2594 # Refresh decoration.
2597 # Move following items in parent.
2599 self
._d
_parent
.new_size_for(self
)
2602 if self
.w_
.fluid_ref(detach_l3_tree
= True):
2604 self
._l3_parent
.detach_child(self
.d_expr
._l3tree
._id
,
2605 self
.w_
.state_
.storage
)
2606 # Insert a placeholder, to keep indices valid.
2607 dummy
= ast
.placeholder(self
._l3_parent
, self
.w_
.state_
.storage
)
2608 self
._l3_parent
.insert_child(self
._l3_pidx
, dummy
)
2609 daExprMarker
.detach
= detach
2611 def draw_marker(self
):
2614 cp
.exp_marker_width
, 0,
2615 cp
.exp_marker_width
, cp
.exp_marker_height
,
2616 0 , cp
.exp_marker_height
,
2619 _marker
= self
._root
_group
.add(
2620 canvas
.CanvasPolygon
,
2621 fill_color
= "black",
2624 cap_style
= gtk
.gdk
.CAP_ROUND
,
2625 join_style
= gtk
.gdk
.JOIN_ROUND
,
2627 self
._marker
_points
[_marker
] = points
2629 daExprMarker
.draw_marker
= draw_marker
2631 def insert_event(self
, item
, event
):
2632 if event
.type == gtk
.gdk
.BUTTON_PRESS
:
2633 if event
.button
== 2:
2634 print "insert_event"
2635 if not self
.w_
.selector
.have_selection():
2636 ten_second_message("No node selected.")
2639 self
.insert(item
, self
.w_
.selector
.get_selection())
2641 daExprMarker
.insert_event
= insert_event
2643 def insert(self
, marker
, newobj
):
2644 assert isinstance(newobj
, l3Base
)
2646 if marker
!= self
._d
_marker
:
2647 raise DisplayError("Insertion from wrong marker.")
2649 # Avoid special cases:
2650 if newobj
.contains_recursive(self
._d
_expr
):
2651 ten_second_message("Cannot insert object into itself.")
2654 if newobj
._canvas
!= self
._canvas
:
2655 ten_second_message("Cannot move objects across displays. "
2656 "Use copy instead.")
2660 old_entry
= self
._l3_parent
[self
._l3_pidx
]
2661 slot_available(self
.w_
, old_entry
)
2664 self
._d
_expr
= d_expr
= newobj
2666 # Reparent object display.
2667 newobj
.reparent(self
)
2669 # Position new child.
2671 l1
, t1
, _
, _
= self
._d
_marker
.get_bounds()
2672 l2
, t2
, _
, _
= self
._d
_expr
.get_bounds()
2673 d_expr
.move(l1
- l2
, t1
- t2
)
2677 # Refresh decoration.
2679 # Move following items in parent.
2681 self
._d
_parent
.new_size_for(self
)
2684 if self
.w_
.fluid_ref(insert_l3_tree
= True):
2685 self
._l3_parent
.replace_child(old_entry
._id
, newobj
._l3tree
)
2687 # Inform obj of marker.
2688 newobj
.setup_marker(self
._d
_marker
, self
)
2690 daExprMarker
.insert
= insert
2693 def get_bounds(self
):
2694 """ Marker interface used in Nested.drag_event.
2696 return marker_get_bounds(self
.w_
, self
._d
_marker
)
2697 daExprMarker
.get_bounds
= get_bounds
2702 l3_parent
= reader
.parse("a = f(x,y)")[0][0]
2703 play_env
= interp
.get_child_env(def_env
, storage
)
2704 l3_parent
.setup(empty_parent(), play_env
, storage
)
2707 d_expr
= w_
.canvas
.view
.start_add_l3tree(l3_parent
[l3_pidx
])
2708 d_expr
.move(100, 100)
2710 the_marker
= daExprMarker(w_
, w_
.canvas
.view
, l3_parent
, l3_pidx
, d_expr
)
2716 [ future(operation
, i
, other
) for i
in list]
2718 # operation is a pure function, no envs. Future need not provide
2724 def compute(_n
, _m
):
2726 print_( (_n
+ 1) * _m
)
2727 return compute(_n
+ 1, _m
)
2733 tree
= reader
.parse("""
2735 from l3lang.l3_utils import print_, pprint
2737 def compute(_n, _m):
2739 print_( (_n + 1) * _m)
2740 return compute(_n + 1, _m)
2744 tree
.setup(empty_parent(), def_env
, w_
.state_
.storage
)
2745 w_
.canvas
.view
.start_add_l3tree(tree
)
2751 for i
in range(0,10):
2756 from pprint
import pprint
2758 import atexit
, pdb
, sys
, os
2759 from EMAN2
import EMData
2762 ev
= os
.path
.expandvars
2764 os
.chdir(ev('$SPXROOT/l3lang/test'))
2765 def load( file_name
):
2767 img
.read_image( file_name
)
2769 #** List available functionality.
2770 pprint(EMAN2
.Processors
.get_list())
2771 pprint(EMAN2
.Projectors
.get_list())
2772 pprint(EMAN2
.Reconstructors
.get_list())
2773 pprint(EMAN2
.Cmps
.get_list())
2774 #** Dump ALL get_list containing members.
2775 for nm
in dir(EMAN2
):
2777 memb
= eval("EMAN2.%s.get_list" % nm
)
2778 except AttributeError:
2780 print ("EMAN2.%s.get_list" % nm
)
2782 #** simple mirror test
2786 e
.set_value_at(0,1, 1.0)
2787 print "original at start"
2790 # Use the processors listed by .get_list() above.
2791 f1
= e
.process("mirror",{"axis":'x'})
2792 print "new mirrored image"
2795 print "original after mirror (unchanged)"
2798 e
.process_inplace("mirror",{"axis":'x'})
2799 print "original after in-place mirror"
2802 # Another possibility:
2805 e
.set_value_at(0,0, 1.0)
2806 print "original at start"
2809 e
= e
.process("mirror",{"axis":'x'})
2810 print "new mirrored image"
2814 #* sample from andres' paper
2818 from EMAN2 import EMNumPy
2819 def img_sphere(size, radius, axis, center, fill):
2820 from EMAN2 import EMData
2822 e.set_size(size, size, size)
2823 e.process_inplace('testimage.circlesphere',
2824 {'radius':radius, 'axis':axis,
2825 'c':center, 'fill':fill})
2830 sphere
= img_sphere(32, 20, 'z', 15, 1)
2832 # sphere.print_image()
2834 # No slicing on emdata, use numpy array.
2835 t1
= EMNumPy
.em2numpy(sphere
)
2837 # View all 2d slices
2838 for ix
in range(0,32):
2839 t1
[ix
, 0:None, 0:None]
2844 from EMAN2 import EMNumPy
2845 def img_circle(size, radius, center, fill):
2846 from EMAN2 import EMData
2848 e.set_size(size, size)
2849 e.process_inplace('testimage.circlesphere',
2850 {'radius':radius, 'c':center, 'fill':fill})
2853 circle
= img_circle(32, 20, 15, 1)
2854 t1
= EMNumPy
.em2numpy(circle
)
2857 #** dither / diffuse
2858 #** form projections
2862 #** and much more...
2863 # 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"
2864 # 2D alignment and classification. Particles were normalized, low- and
2865 #high-pass filtered, soft-masked, and then centered. The low- and
2866 #high-pass filters were centered at 1/5 pixels1
2869 #* large list display
2871 # Insert as list list returns but keeps using 100% cpu
2875 # RuntimeError: maximum recursion depth exceeded in cmp
2877 # The number of bounds calculations (marker_get_bounds() and others)
2878 # is ~ N, and bounds calculations are VERY slow...
2881 volume
= load( "model.tcp" )
2883 proj
= volume
.project("pawel",
2884 {"angletype" : "SPIDER",
2885 "anglelist" : [10, 20, 30], # phi, theta, psi
2890 #* visual program representation
2892 # Very convoluted example, heavily using higher-order functions.
2893 # Ugly in graphical view.
2894 t_tree
= reader
.parse_file(
2895 os
.path
.expandvars('$SPXROOT/l3lang/test/calc.l3'))
2896 t_tree
.setup(empty_parent(), def_env
, w_
.state_
.storage
)
2897 w_
.canvas
.view
.clear_state()
2898 w_
.canvas
.view
.start_add_l3tree(t_tree
)
2904 # Very clean top-to-bottom script. Graphical view is still tedious.
2905 # Good reference for prototyping 'nice' graphical view w/o loops.
2906 tm_tree
= reader
.parse_file(
2907 os
.path
.expandvars('$SPXROOT/l3lang/test/ctf.l3'))
2908 tm_tree
.setup(empty_parent(), def_env
, w_
.state_
.storage
)
2909 w_
.canvas
.view
.clear_state()
2910 tm_tree_l3pic
= w_
.canvas
.view
.start_add_l3tree(tm_tree
)
2914 #*** concise view, program level, static
2916 # no program editing (static)
2917 t0_tree
= reader
.parse("""
2918 "project from volume"
2919 "convolve image and ctf"
2920 "produce noisy image"
2921 "apply ctf correction"
2924 t0_tree
.setup(empty_parent(), def_env
, w_
.state_
.storage
)
2925 t0_tree_pic
= w_
.canvas
.view
.start_add_l3tree(t0_tree
)
2927 t0_tree_pic
.move(+8, +5.5) # Relative movement.
2930 #*** concise view, program + calls, static
2932 # no program editing (static)
2933 t1_tree
= reader
.parse("""
2934 project_from_volume(spi.pj3)
2936 convolve_image_and_ctf(spi.ft, spi.tfc, spi.mu, spi.ft)
2938 produce_noisy_image(spi.mo, spi.adf)
2940 apply_ctf_correction(spi.tfcts)
2944 t1_tree
.setup(empty_parent(), def_env
, w_
.state_
.storage
)
2945 t1_tree_pic
= w_
.canvas
.view
.start_add_l3tree(t1_tree
)
2947 t1_tree_pic
.move(+35, +7.5) # Relative movement.
2951 #*** concise view, program + manually selected details
2953 # For ctf.l3, selected full subtrees are the desirable level of detail, so
2954 # no reordered node display is needed.
2955 # To keep the connection to the (function name based) outline view,
2956 # the function names can appear explicitly around the expansion.
2957 # For the present sample, this is redundant. Maybe not for others?
2958 t3_tree
= reader
.parse("""
2959 project_from_volume(
2961 testimg = spi.pj3("/home/hohn/w/phenix/l3lang/test/volext.spi",
2963 [45.5, 27.2, -30.0])))
2965 convolve_image_and_ctf(
2968 ctf = spi.tfc((2), # cs[mm]
2969 (10000, 0.025), # defocus(a), lambda(a)
2970 (64, 64), # dimension of the 2d array
2971 (0.17), # maximum spatial frequency
2972 (0.005, 0), # source size[a-1], defocus spread[a]
2973 (0, 0), # astigmatism[a], azimuth[deg]
2974 (0.1, 10000), # amplitude contrast ratio, gaussian envelope halfwidth
2975 (-1), # sign (+1 or -1)
2980 produce_noisy_image(
2982 noise = call_kwd_func(spi.mo, [64, 64], "random",
2983 { use_gaussian = 'y', mean = 1,
2984 std_deviation = 200} )),
2987 apply_ctf_correction(
2989 restored_img = spi.tfcts(spi.img_to_stack(noisyimg),
2991 spi.img_to_stack(ctf),
2997 t3_tree
.setup(empty_parent(), def_env
, w_
.state_
.storage
)
2998 t3_tree_pic
= w_
.canvas
.view
.start_add_l3tree(t3_tree
)
3000 t3_tree_pic
.move(+70, +7.5) # Relative movement.
3003 #*** concise view, program + some full details
3005 # full program editing in the details
3006 # This version is disjoint from the others; it's missing the
3008 t2_tree
= reader
.parse("""
3009 project_from_volume(
3010 testimg = spi.pj3("/home/hohn/w/phenix/l3lang/test/volext.spi",[64, 64],
3011 [45.5, 27.2, -30.0]))
3013 convolve_image_and_ctf(spi.ft,
3016 (10000, 0.025), # defocus(a), lambda(a)
3017 (64, 64), # dimension of the 2d array
3018 (0.17), # maximum spatial frequency
3019 (0.005, 0), # source size[a-1], defocus spread[a]
3020 (0, 0), # astigmatism[a], azimuth[deg]
3021 (0.1, 10000), # amplitude contrast ratio, gaussian envelope halfwidth
3022 (-1), # sign (+1 or -1)
3025 produce_noisy_image(
3026 noise = call_kwd_func(spi.mo, [64, 64], "random",
3027 { use_gaussian = 'y', mean = 1,
3028 std_deviation = 200} ),
3031 apply_ctf_correction(restored_img = spi.tfcts(spi.img_to_stack(noisyimg),
3033 spi.img_to_stack(ctf),
3039 t2_tree
.setup(empty_parent(), def_env
, w_
.state_
.storage
)
3040 t2_tree_pic
= w_
.canvas
.view
.start_add_l3tree(t2_tree
)
3042 t2_tree_pic
.move(+135, +7.5) # Relative movement.
3045 #* LOOP language addition.
3048 from l3lang
import utils
, reader
, ast
, view
, interp
3050 t2_tree
= reader
.parse("""
3051 inline "from pprint import pprint"
3052 loop dtheta(iteration, deltheta) from (1, 40):
3054 pprint([iteration, deltheta])
3055 return dtheta(iteration + 1, deltheta / 2.0)
3057 return deltheta / 2.0
3060 play_env
= interp
.get_child_env(def_env
, w_
.state_
.storage
)
3063 t2_tree
.setup(empty_parent(), play_env
, w_
.state_
.storage
)
3064 t2_tree
.interpret(play_env
, w_
.state_
.storage
)
3067 t2_tree
.setup(empty_parent(), play_env
, w_
.state_
.storage
)
3068 # view.print_info(storage, tree)
3069 # pdb.run('w_.canvas.view.start_add_l3tree(t2_tree)')
3072 ##l3gui.misc.DisplayError: internal _container / _parent inconsistency.
3073 ##w_.canvas.view.clear_state()
3074 w_
.canvas
.view
.start_add_l3tree(t2_tree
)
3079 #* LIST language addition.
3082 from l3lang
import utils
, reader
, ast
, view
, interp
3085 t2_tree
= reader
.parse("""
3086 inline "from pprint import pprint"
3093 pprint((iteration, "small"))
3098 t2_tree
= reader
.parse("""
3099 inline "from pprint import pprint"
3100 list "A short block":
3103 if (iteration <= 10):
3106 pprint((iteration, "small"))
3110 play_env
= interp
.get_child_env(def_env
, w_
.state_
.storage
)
3113 t2_tree
.setup(empty_parent(), play_env
, w_
.state_
.storage
)
3114 w_
.canvas
.view
.start_add_l3tree(t2_tree
)
3119 t2_tree
.setup(empty_parent(), play_env
, w_
.state_
.storage
)
3120 t2_tree
.interpret(play_env
, w_
.state_
.storage
)
3128 The reference files for these fragments are now
3130 l3lang/test/voea-bp.py, v1.7 and beyond
3132 voea-bp-var.l3, v1.5 and beyond
3134 #** utils (evaluate this!)
3135 def canvas_add_program(text
, xx
= 0, yy
= 0):
3136 t0_tree
= reader
.parse(text
)
3137 t0_play_env
= interp
.get_child_env(def_env
, w_
.state_
.storage
)
3138 t0_tree
.setup(empty_parent(), t0_play_env
, w_
.state_
.storage
)
3139 t0_tree_l3pic
= w_
.canvas
.view
.start_add_l3tree(t0_tree
)
3140 t0_tree_l3pic
.move(xx
, yy
) # Relative movement.
3142 def canvas_add_1st_expr(text
, xx
= 0, yy
= 0):
3143 t0_tree
= reader
.parse(text
)[0][0]
3144 t0_tree
.setup(empty_parent(), def_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_ast(self
, t0_tree
, xx
= 0, yy
= 0):
3149 from l3lang
import utils
, reader
, ast
, view
, interp
3151 t0_play_env
= interp
.get_child_env(st
.def_env
, st
.storage
)
3152 t0_tree
.setup(ast
.empty_parent(), t0_play_env
, st
.storage
)
3153 t0_tree_l3pic
= self
.start_add_l3tree(t0_tree
)
3154 t0_tree_l3pic
.move(xx
, yy
) # Relative movement.
3155 return t0_tree_l3pic
3156 l3Canvas
.canvas_add_ast
= canvas_add_ast
3159 # print "\n".join(map(lambda (x,y): "%g %g" % (x,y),
3160 # (zip([1,2], [3,4]))))
3165 data
= "\n".join(map(lambda (x
,y
): "%g %g" % (x
,y
),
3168 gnuplot -persist <<EOF
3171 set out "/tmp/foo.ps"
3175 ## set title "comparison"
3177 plot '-' w lines lw 6
3181 ps2epsi /tmp/foo.ps /tmp/foo.eps
3186 ## plot([1,2], [3,4])
3189 #*** directory of program run
3190 # See Program.directory
3194 # #*** concise view, program level, static
3195 # # manually written
3196 # # no program editing (static)
3197 # t0_tree = reader.parse("""
3198 # "get reference volume"
3199 # "iterate delta theta"(
3200 # "calculate angles",
3203 # "compare volumes",
3204 # "repeat if necessary")
3206 # t0_play_env = interp.get_child_env(def_env, w_.state_.storage)
3207 # t0_tree.setup(empty_parent(), t0_play_env, w_.state_.storage)
3208 # ## w_.canvas.view.clear_state()
3209 # t0_tree_l3pic = w_.canvas.view.start_add_l3tree(t0_tree)
3210 # t0_tree_l3pic.move(+8.5, +7.5) # Relative movement.
3214 # #*** concise view, program + calls, static
3215 # # manually written
3216 # # no program editing (static)
3217 # t1_tree = reader.parse("""
3218 # "get reference volume"
3219 # "iterate delta theta"(
3220 # "calculate angles"(anglist = voea(deltheta)),
3221 # "projections"(proj_list = gridproj(ref_vol,
3225 # "backproject"(back_projection = backproj(proj_list,
3229 # "compare volumes"(ccc = EMAN2.Cmps.get("ccc").cmp(back_projection,
3231 # "repeat if necessary"(if_branch))
3233 # t1_play_env = interp.get_child_env(def_env, w_.state_.storage)
3234 # t1_tree.setup(empty_parent(), t1_play_env, w_.state_.storage)
3235 # ## w_.canvas.view.clear_state()
3236 # t1_tree_l3pic = w_.canvas.view.start_add_l3tree(t1_tree)
3237 # t1_tree_l3pic.move(+42, +7.5) # Relative movement.
3242 # Very clean top-to-bottom script
3243 tm_tree
= reader
.parse_file(
3244 os
.path
.expandvars('$SPXROOT/l3lang/test/voea-bp.l3'))
3245 tm_play_env
= interp
.get_child_env(def_env
, w_
.state_
.storage
)
3246 tm_tree
.setup(empty_parent(), tm_play_env
, w_
.state_
.storage
)
3247 # w_.canvas.view.clear_state()
3248 tm_tree_l3pic
= w_
.canvas
.view
.start_add_l3tree(tm_tree
)
3249 # tm_tree_l3pic.move(+77, +7.5) # Relative movement.
3253 #** Add text tree to canvas
3256 # [12L, 13L, 27L, 59L, 195L]
3258 canvas_add_1st_expr("""
3268 canvas_add_1st_expr("""
3279 canvas_add_program("""
3280 from l3lang.test.voea_bp_support import *
3301 # Figure from gnuplot
3304 #** get the diretory structure
3306 ## use program -> eval locally
3308 (self
._l3tree
)._block
_env
.print_tree()
3311 (self
._l3tree
)._eval
_env
.print_tree()
3313 # As lists of names.
3315 from pprint
import pprint
3316 pprint((self
._l3tree
)._block
_env
.get_tree())
3317 pprint((self
._l3tree
)._eval
_env
.get_tree())
3321 ## use program -> eval locally
3322 from pprint
import pprint
3323 listing
= (self
._l3tree
)._eval
_env
.get_dynamic_subtrees()
3326 dir_struct
= self
._l3tree
.directory(listing
)
3327 dir_view
= self
._canvas
.canvas_add_ast(dir_struct
)
3328 dir_view
.move(10, 10)
3331 #*** More^2 filtered.
3332 ## use program -> eval locally
3333 dir_struct
= self
._l3tree
.directory_l3()
3334 dir_view
= self
._canvas
.canvas_add_ast(dir_struct
)
3335 dir_view
.move(20, 20)
3338 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')]))
3340 dir_view
= w_
.canvas
.view
.canvas_add_ast(dir_struct
)
3341 dir_view
.move(20, 20)
3345 gtk
.main() # use menu->exit
3347 #* presentation-may-2006
3349 # xterm reference size 120x50
3351 # convert /tmp/foo.ps -rotate 90 -resize 400x400 /tmp/foo.png
3353 tm_tree
= reader
.parse("""
3365 list "eman2 members":
3368 list "spider members":
3391 inline "from l3lang.test.voea_bp_support import *"
3393 loop name(arg) from (start_val):
3397 tm_play_env
= interp
.get_child_env(def_env
, w_
.state_
.storage
)
3398 tm_tree
.setup(empty_parent(), tm_play_env
, w_
.state_
.storage
)
3400 tm_tree_l3pic
= w_
.canvas
.view
.start_add_l3tree(tm_tree
)
3405 #** value insertion preview
3407 canvas_add_1st_expr("""
3417 canvas_add_1st_expr("""
3428 canvas_add_program("""
3429 inline "from l3lang.test.voea_bp_support import *"
3451 #* sparx functions as of [Jun-13-2006]
3453 #** Changed sparx imports to fix this.
3454 #** Trying to get an overview of the sparx functions
3455 # as of [Jun-13-2006]
3459 # For this, the module structure SHOULD be useful. But it isn't
3460 # available interactively.
3462 # The following doesn't do much good. There are no comments, and
3463 # e.g. do_alignment (visible via dir(sparx)) is not shown at all.
3467 from sparx
import libpy
3468 # with ImportError: cannot import name libpy.
3471 # is <module 'sparx' from '/net/hooknose/scratch1/hohn/rh9/ia32/installer-0.5/sparx-bin/opt/sparx/libpy/sparx.pyc'>
3473 # All because of the
3474 # from utilities import *
3475 # from filter import *
3476 # from projection import *
3477 # from fundamentals import *
3478 # from statistics import *
3479 # from alignment import *
3480 # from morphology import *
3481 # from reconstruction import *
3482 # in libpy/sparx.py!
3483 help(sparx
.do_alignment
)
3485 # So, I manually get the structure.
3496 # For nested modules, this should give information
3497 prfx
, lst
= ("sparx",
3508 [help("%s.%s" % (prfx
, itm
)) for itm
in lst
]
3510 # But here it fails with
3511 # no Python documentation found for 'sparx.utilities'
3512 # no Python documentation found for 'sparx.filter'
3513 # no Python documentation found for 'sparx.projection'
3515 # again because of the import *
3517 #** Overview of the sparx functions as of [Tue Jun 13 13:05:25 2006]
3518 import sparx
.libpy
as spx
3521 # module/function listing
3522 for mod
in dir(spx
):
3523 if not is_module(mod
): continue
3524 for func
in dir(mod
):
3525 if not is_function(func
): continue
3526 print "module: %s\t\tfunction: %s" % (mod
, func
)
3530 #** sparx.gui console use:
3532 tm_tree
= reader
.parse_file(
3533 os
.path
.expandvars('$SPXROOT/l3gui/test/iref01.py'))
3534 tm_play_env
= interp
.get_child_env(def_env
, w_
.state_
.storage
)
3535 tm_tree
.setup(empty_parent(), tm_play_env
, w_
.state_
.storage
)
3536 tm_tree_l3pic
= w_
.canvas
.view
.start_add_l3tree(tm_tree
)
3539 #* sparx persistence
3540 #** eman 'native' I/O
3542 volume
= sx
.getImage( "model.tcp" )
3544 raw
= sx
.project(volume
, [10.0, 20.0, 30.0, 0.0, 0.0], 35)
3547 print raw
.get_attr_dict()
3549 # Looks like spider format doesn't keep the attributes:
3550 sx
.dropImage(raw
, "raw.spi", itype
= 's')
3551 raw_s
= sx
.getImage("raw.spi")
3553 print raw_s
.get_attr_dict()
3557 sx
.dropImage(raw
, "raw.hdf")
3558 raw_s
= sx
.getImage("raw.hdf")
3560 print raw_s
.get_attr_dict()
3563 # Make sure there is no EMData.__getattr__ to avoid '__getinitargs__':
3564 # 'Not exist' caught crap.
3565 import l3lang
.utils
as ut
3567 raw_p
= ut
.file_unpickle("/tmp/foo")
3570 print raw
.get_attr_dict()
3571 print raw_p
.get_attr_dict()
3574 #** l3 state table pickling
3576 import l3lang
.utils
as ut
3577 ut
.file_pickle(w_
.state_
.storage
, "st.post-run-table")
3578 # takes forever; remove
3580 #* comment attachment
3581 # Similar to reader.raw_lexer(code_s)
3583 import sys
, token
, tokenize
3584 code_s
= open(os
.path
.expandvars('$SPXROOT/l3gui/test/iref01.py')).read()
3588 # Comment type identifier.
3589 COMMENT
, _
= filter(lambda (k
,v
): v
== 'COMMENT', token
.tok_name
.items())[0]
3590 NEWLINE
, _
= filter(lambda (k
,v
): v
== 'NEWLINE', token
.tok_name
.items())[0]
3591 NL
, _
= filter(lambda (k
,v
): v
== 'NL', token
.tok_name
.items())[0]
3595 #*** Cluster connected successive comment lines.
3596 # When connecting to the parse tree, this style of clustering has a
3606 # `comment 1` will attach to `code 1`, leaving `comment 2` to attach to
3607 # `code 2`, which is not good.
3610 # - drop one of the comments, attach other to `code 1`
3611 # - combine comments and attach to `code 1`
3613 gen
= tokenize
.generate_tokens(reader
.string_readline(code_s
))
3615 va_cluster
= [] # (string, begin, end) list
3616 va_accum
= [] # va_cluster list
3618 (type, string
, begin
, end
, line
) = ii
3620 # Dump every comment.
3622 print "(%r, %s, %s)" % (string
, begin
, end
)
3624 # Cluster comment lines.
3625 if type in [COMMENT
]:
3627 va_cluster
.append( (string
, begin
, end
) )
3631 va_accum
.append(va_cluster
)
3636 pprint(va_accum
, sys
.stdout
, 4, 2)
3639 #*** Cluster successive (incl.separated) comment lines
3640 # Combine blank-separated comment blocks.
3641 gen
= tokenize
.generate_tokens(reader
.string_readline(code_s
))
3643 va_cluster
= [] # (string, begin, end) list
3644 va_accum
= [] # va_cluster list
3646 (type, string
, begin
, end
, line
) = ii
3648 # # Dump every comment.
3649 # if type == COMMENT:
3650 # print "(%r, %s, %s)" % (string, begin, end)
3652 # Cluster successive comment lines, even those separated by blanks.
3653 if type in [COMMENT
]:
3655 va_cluster
.append( (string
, begin
, end
) )
3657 elif type in [NEWLINE
, NL
]:
3659 va_cluster
.append( (string
, begin
, end
) )
3662 # Remove trailing empty newlines.
3664 while tl
[0] == '\n':
3665 tl
= va_cluster
.pop()
3666 va_cluster
.append(tl
)
3668 va_accum
.append(va_cluster
)
3675 for clus
in va_accum
:
3676 print clus
[0][1] # begin
3678 print line
[0], # body
3679 print clus
[-1][2] # end
3683 #*** Simplify comment structure
3684 # Adjust lexeme row indexing, compact clusters, and remove unused data.
3685 va_comments
= [] # ((last_row, first_col), string) list
3687 ( (clus
[-1][2][0] - 1, clus
[0][1][1]), # (last_row, first_col)
3688 "".join([line
[0] for line
in clus
]))
3689 for clus
in va_accum
]
3694 tree
= reader
.parse(code_s
)
3697 #*** extract a list of leading subnodes from the parse tree
3699 tr_lines
= [] # [(start_row, col), node] list
3700 for nd
in tree
.subtrees():
3701 (row
, col
), _
= nd
.get_char_range()
3702 # Prune all but first expression on a line.
3704 tr_lines
.append( [(row
,col
), nd
] )
3705 ## tr_lines.append( [(row,col), nd] )
3711 #** Find expression to attach to.
3712 # Consider only the first expression in given line.
3713 # Find the first expression following a comment end.
3715 def align_and_show():
3716 com_it
= iter(va_comments
)
3717 node_it
= iter(tr_lines
)
3718 com_row
= lambda com
: com
[0][0]
3720 try: com
= com_it
.next()
3721 except StopIteration: break
3722 try: nd
= node_it
.next()
3723 except StopIteration: break
3724 com_line
= com
[0][0]
3725 # Find first line after current comment.
3726 while nd
[0][0] <= com_line
:
3728 if com
[0][1] != nd
[0][1]:
3729 print "==== column mismatch"
3731 print nd
[1].source_string()
3734 ## import pdb; pdb.set_trace()
3737 #* selection handling
3738 # See pygtk-2.4.1/examples/ide/edit.py as example.
3742 warnings
.filterwarnings("error", "assertion") # , GtkWarning
3744 # But the C code just continues on.
3746 # ... the bigger problem is that a fair amount of C code doesn't
3747 # handle the case where the assertion fails ...
3752 view
= w_
.canvas
.view
3753 ra
= w_
.canvas
.view
.root().add
3754 c_label
= ra(canvas
.CanvasText
,
3756 size_points
= 12 * 1,
3762 print c_label
.get_bounds()
3763 print c_label
.__grefcount
__
3767 view
= w_
.canvas
.view
3768 ra
= GtkObjProxy(w_
.canvas
.view
.root())
3769 c_label
= GtkObjProxy(ra
.add(canvas
.CanvasText
,
3771 size_points
= 12 * 1,
3777 print c_label
.get_bounds()
3780 print c_label
.get_bounds()
3783 #* python compile tests
3784 # Accepted as complete input...
3793 ''', '<string>', 'single')
3821 #* gtk object pickling
3822 # local eval of l3Rawtext
3823 import l3lang
.utils
as ut
3824 # Runs (works? Protocol 1 fails.).
3825 ut
.file_pickle( self
._ltext
.get_property("anchor"))
3826 val
= ut
.file_unpickle("/tmp/foo")
3829 # (.:19911): GLib-GObject-WARNING **: cannot retrieve class for
3830 # invalid (unclassed) type `<invalid>'
3832 # ** ERROR **: file pygenum.c: line 60 (pyg_enum_repr): assertion
3833 # failed: (G_IS_ENUM_CLASS(enum_class))
3837 #* l3IfLoop addition
3838 #** test single loop
3851 # calc 3d reconstruction
3857 # calc 3d reconstruction
3860 # repeat from gen. proj
3862 return _lname(2 - foo)
3867 # repeat from gen. proj
3869 return _loop(bar + 1)
3872 l3tree
= reader
.parse(test_s
)
3876 #** l3ifloopbreak pattern 1
3881 template
= reader
.parse(code_s
)
3884 #** l3ifloopbreak pattern 2
3889 template
= reader
.parse(code_s
)
3893 #** test single loop w/ break
3905 #** test single loop w/ continue
3911 return lname(new_args)
3917 #** l3ifloop pattern
3924 template
= reader
.parse(code_s
)
3929 # From template.body().
3931 body
= l3tree
.body()
3934 aList([Set(MarkerTyped(Symbol('LNAME'), Symbol('symbol')),
3935 Function(MarkerTyped(Symbol('LARGS'), aList([])),
3936 MarkerTyped(Symbol('LBODY'), aList([])))),
3937 Call(MarkerTyped(Symbol('LNAME2'), Symbol('symbol')),
3938 MarkerTyped(Symbol('CARGS'), aList([])))]),
3940 if ma
['LNAME'] != ma
['LNAME2']:
3941 warn_("loop name mismatch; no special display")
3942 elif len(ma
['LARGS']) != len(ma
['CARGS']):
3943 warn_("arguments mismatched between loop definition and call")
3953 from l3lang
.l3
import *
3955 #** interpretation order
3967 print "hello", "world"
3970 inline
"from math import *"
3971 def f(a
,b
, c
= cos(22), d
= 10):
3978 f(1,2, e
= 1) # unknown keyword
3982 p
= f(1,2, c
= cos(22), d
= 1)
3983 p
= f(1,2, c
= 22, d
= 1)
4005 # old errors (pre-r460)
4006 print parse('a = b')
4007 print parse('a == b')
4009 print parse('(a == b)')
4010 print parse('(a <= b)')
4012 print parse('(a = b)')
4013 print parse('(av1*(int(nima/2)+(nima%2)) + av2*int(nima/2))/nima')
4016 #* l3 tests, in gui (repeated interpretation checks)
4018 filter(lambda x
: x
> 2, [1,2,3,4])
4021 inline
"from l3lang.l3 import *"
4022 def f(a
,b
, c
= cos(22), d
= 10):
4027 if ("prog", "filter test"):
4028 filter(lambda x
: x
> 2, [1,2,3,4])
4037 # using > "eval locally"
4039 # Program(aList([Call(Symbol('*'), aList([Int(6L), Call(Symbol('-'), aList([Int(3L), Int(2L)]))]))]))
4040 # >>> self._l3tree._first_char
4042 # >>> self._l3tree._last_char
4047 #* statement additions
4048 #** is, is not, not in
4052 print hello, "world"
4054 reader
.raw_lexer(code_s
)
4057 #* l3 internals tests (from python)
4059 tree
= reader
.parse('f(1,2, c = sin(22), d = 10)')
4061 play_env
= interp
.get_child_env(def_env
, storage
)
4062 tree
.setup(empty_parent(), play_env
, storage
)
4063 view
.print_info(storage
, tree
, show_macros
= True)
4066 print tree
.body().positional_args()
4067 print tree
.body().nargs()
4068 print tree
.body().named_args()
4070 tree
.interpret(def_env
, storage
)
4072 tree
= reader
.parse('''
4073 def f(a,b, c = cos(22), d = 10):
4077 play_env
= interp
.get_child_env(def_env
, storage
)
4078 tree
.setup(empty_parent(), play_env
, storage
)
4079 view
.print_info(storage
, tree
, show_macros
= True)
4081 func_
= tree
.body()[1]
4082 print func_
.positional_block_args()
4083 print func_
.named_block_args()
4087 tree
.interpret(def_env
, storage
)
4091 from l3lang
.view
import print_ast
4093 print_ast(reader
.parse(str))
4099 tree
= reader
.parse('''
4107 play_env
= interp
.get_child_env(def_env
, storage
)
4108 tree
.setup(empty_parent(), play_env
, storage
)
4109 view
.print_info(storage
, tree
, show_macros
= True)
4112 tree
.interpret(def_env
, storage
)
4113 view
.print_info(storage
, tree
)
4114 view
.print_all_info(storage
, tree
)
4115 # ok: repeated .interpret() reuses first clone.
4123 reader
.py_lexer(code_s
)
4124 print reader
.parse(code_s
)
4126 #*** eval 3 - single call
4134 # reader.raw_lexer(code_s)
4135 # reader.py_lexer(code_s)
4136 tree
= reader
.parse(code_s
)
4139 play_env
= interp
.get_child_env(def_env
, storage
)
4140 tree
.setup(empty_parent(), play_env
, storage
)
4141 view
.print_info(storage
, tree
, show_macros
= True)
4144 tree
.interpret(play_env
, storage
)
4145 view
.print_info(storage
, tree
)
4146 view
.print_all_info(storage
, tree
)
4147 # repeated .interpret() reuses first clone ?
4150 #*** eval 4 - variable value change
4159 # reader.raw_lexer(code_s)
4160 # reader.py_lexer(code_s)
4161 tree
= reader
.parse(code_s
)
4164 play_env
= interp
.get_child_env(def_env
, storage
)
4165 tree
.setup(empty_parent(), play_env
, storage
)
4166 view
.print_info(storage
, tree
, show_macros
= True)
4169 tree
.interpret(play_env
, storage
)
4170 view
.print_env(play_env
)
4171 view
.print_info(storage
, tree
)
4172 view
.print_all_info(storage
, tree
)
4173 # repeated .interpret() reuses first clone ???
4186 # reader.raw_lexer(code_s)
4187 # reader.py_lexer(code_s)
4188 tree
= reader
.parse(code_s
)
4191 play_env
= interp
.get_child_env(def_env
, storage
)
4192 tree
.setup(empty_parent(), play_env
, storage
)
4193 view
.print_info(storage
, tree
, show_macros
= True)
4196 tree
.interpret(play_env
, storage
)
4197 view
.print_all_info(storage
, tree
)
4198 tree
.interpret(play_env
, storage
)
4199 view
.print_all_info(storage
, tree
)
4201 view
.print_info(storage
, tree
)
4202 view
.print_env(play_env
)
4203 # repeated .interpret() reuses first clone ???
4209 #** eval 6 -- `while` expansion
4236 tree
= reader
.parse(code_s
)
4239 play_env
= interp
.get_child_env(def_env
, storage
)
4240 tree
.setup(empty_parent(), play_env
, storage
)
4243 tree
.interpret(play_env
, storage
)
4244 view
.print_all_info(storage
, tree
)
4246 tree
.interpret(play_env
, storage
)
4247 view
.print_all_info(storage
, tree
)
4249 view
.print_env(play_env
)
4252 #** eval 7 -- `while` template
4268 tree
= reader
.parse(code_s
)
4271 play_env
= interp
.get_child_env(def_env
, storage
)
4272 tree
.setup(empty_parent(), play_env
, storage
)
4273 view
.print_all_info(storage
, tree
)
4277 #** eval 8 -- `while` tests
4286 tree
= reader
.parse(code_s
)
4289 play_env
= interp
.get_child_env(def_env
, storage
)
4290 tree
.setup(empty_parent(), play_env
, storage
)
4291 view
.print_info(storage
, tree
)
4294 tree
.interpret(play_env
, storage
)
4295 view
.print_all_info(storage
, tree
)
4297 tree
.interpret(play_env
, storage
)
4298 view
.print_all_info(storage
, tree
)
4300 view
.print_env(play_env
)
4302 #*** reference comparison
4303 # Remove id / timestamp columns, then compare per-character.
4319 ref_t
= reader
.parse(ref_s
)
4321 ref_env
= interp
.get_child_env(def_env
, storage
)
4322 ref_t
.setup(empty_parent(), ref_env
, storage
)
4323 view
.print_info(storage
, ref_t
)
4326 #** eval 9 -- `while` pattern
4339 tree
= reader
.parse(code_s
)
4349 # Nesting, check identifiers.
4364 #* for loop language addition
4365 # for loop using while
4367 #** iterator wrapper
4369 def __init__(self
, seq
):
4370 self
._iter
= iter(seq
)
4372 self
._next
_ready
= False
4375 if self
._next
_ready
:
4378 self
._next
= self
._iter
.next()
4379 self
._next
_ready
= True
4381 except StopIteration:
4382 self
._next
_ready
= False
4387 self
._next
_ready
= False
4389 raise InterpreterError("Request for elements from finished iterator.")
4397 for (x
,y
) in [(1,2), (3,4)]:
4403 I
= l3iter([(1,2), (3,4)])
4408 # To avoid pickling [(1,2), (3,4)] or I, use
4410 # inline 'I = l3iter([(1,2), (3,4)])'
4413 inline
'from l3lang.interp import l3iter; I = l3iter([(1,2), (3,4)])'
4420 #** embed l3 'tail call' form for while:
4425 # def "_while_ID"():
4426 # if not C: # force boolean evaluation via not
4430 # return _while_ID()
4435 inline
'from l3lang.interp import l3iter; I = l3iter([(1,2), (3,4)])'
4447 #** iterator problems
4449 # 1. The sequence cannot be edited (it's a string)
4450 # 2. The indirection
4451 # - (value list VL) -> external (l3iter) -> (value sequence VS)
4452 # loses the time stamps between VL and VS, so that updates to VL
4453 # re-evaluate everything.
4455 # Thus, iterators are USELESS in l3: no persistence and no
4456 # incremental evaluation.
4458 # 3. Are I.more and I.next called again on repeated interpretation?
4460 # 4. External iterator assignments will cause failure:
4474 # Repeat execution of UNORDERED iterators will always fail.
4475 # l3 execution imparts unique ids, IN ORDER of execution.
4477 # Retrieving all iterator values and storing them is not an option
4478 # because iterators need not be finite.
4480 # A prime example: a true rand() MUST NOT be used in l3. Unless it's
4481 # a pseudo-random, FIXED sequence.
4484 # [Thu Jul 12 11:57:28 2007]
4485 # A possible adjustment is to let Call.interpret return all iterators
4486 # as externals, and evaluating every time as done for Inline(). If
4487 # done that way, the for loop also simplifies.
4491 I
= l3iter([(1,2), (3,4)])
4503 #** iterator problems, part 2
4504 # ---------------------------------
4505 # Re-examining iterators in l3 is slow and makes interpretation yet
4506 # more complicated. Instead,
4508 # 1. make sure that l3iter instances pickle,
4510 # 2. assume (and test that) the iterator given to l3iter produces a
4511 # constant sequence, and
4513 # 3. let l3iter re-run the iterators (after unpickling, or better at
4514 # the first call to I.more) to get to the correct position in the
4516 # If the iterator itself performs a lot of work, nothing is gained
4519 # Retrieving the iterator is a problem.
4520 # It cannot be saved with the l3iter instance, but re-running
4521 # l3iter(iterator) will create a new instance (losing the
4522 # timestamp/position info).
4523 # This requires some internal magic...
4526 #** simplify to sequences
4527 # ----------------------
4528 # Backtrack to Python < 2.2, without the __iterator__ mess. There,
4530 # The for statement is used to iterate over the elements of a
4531 # sequence (string, tuple or list):
4534 # sequence types support the len() and [] operators (__len__ and
4535 # __getitem__). Restricting to known-size sequences is a minor
4536 # restriction for tracked, DISPLAYED items.
4538 # For general iterators and simple uses, use Python directly. The
4541 for k
in edsummary
.iterkeys():
4542 print k
+ ": %.3f" % edsummary
[k
]
4544 # uses an unordered iterator, so it cannot work in l3. It also performs
4545 # a trivial task not worth tracking, so it could be in python.
4547 # For this example, using
4548 for k
in edsummary
.keys():
4549 print k
+ ": %.3f" % edsummary
[k
]
4550 # in l3 would be better.
4552 # For sequences providing len() and getitem(), there are two choices:
4553 # 1. another wrapper ( l3traverse() ) can be added, and the
4554 # following should work:
4557 I
= l3traverse([(1,2), (3,4)])
4567 # However, this wrapper would require access to l3 internals, so an
4568 # l3 version is preferable:
4570 # 2. Use a tail call form to handle indexing
4575 # In the following, ITEMS, IDX, LEN and LOOP are leaf nodes, to be
4576 # replaced UNIQUELY in if.setup().
4578 # See also ref[Wed Jul 18 11:38:10 2007]
4582 ITEMS
= [(1,2), (3,4)]
4590 (x
,y
) = ITEMS
[ IDX
- 1 ]
4599 # List as text (no insertions)
4604 # List as structure.
4611 for (i
, x
) in [(1, 1), (2, 4), (3,9)]:
4614 # Nesting, check identifiers.
4623 #* scitbx flex array
4624 from scitbx
.array_family
import flex
4625 data
= flex
.random_double(size
= 4)
4628 # Traceback (most recent call last):
4629 # File "<console>", line 1, in ?
4630 # ArgumentError: Python argument types in
4631 # double.reshape(double, int, int)
4632 # did not match C++ signature:
4633 # reshape(scitbx::af::versa<double, scitbx::af::flex_grid<scitbx::af::small<long, 10u> > > {lvalue}, scitbx::af::flex_grid<scitbx::af::small<long, 10u> >)
4636 #* slice and getitem
4640 na
= N
.arange(28).reshape((7,4))
4644 print na
.__getitem
__( [1,2] )
4645 print na
.__getitem
__( (1,2) ) # correct
4648 print na
.__getitem
__( (slice(1,4), 2) )
4651 print na
.__getitem
__( (slice(1,4), ) )
4652 print na
.__getitem
__( [slice(1,4)] )
4655 print na
.__getitem
__( (slice(1,3), slice(3,6)) )
4656 print operator
.getitem(na
, [slice(1,3), slice(3,6)] )
4657 print operator
.getitem(na
, (slice(1,3), slice(3,6)) )
4659 # Python reference values:
4667 # Compare with # Python reference values:
4671 na
= N
.arange(28).reshape((7,4))
4681 #* gtk images, numeric, eman
4684 from EMAN2
import EMNumPy
4689 ema_arr
= EMAN2
.EMData()
4690 EMNumPy
.numpy2em(num_arr
, ema_arr
)
4694 num_arr
= EMNumPy
.em2numpy(foo
)
4696 EMNumPy
.em2numpy(None)
4699 #*** 1. get numeric array
4700 num_arr
= M
.rand(4,4)
4702 #*** 2.b. numeric -> gtk
4703 # Convert float array to 8-bit array.
4704 rows
, cols
= num_arr
.shape
4705 bottom
= M
.min(M
.min(num_arr
))
4706 top
= M
.max(M
.max(num_arr
))
4707 scale
= (top
- bottom
)
4708 norm_arr
= ((num_arr
- bottom
) / scale
* 255).astype('b')
4710 #*** Use 3 8-bit arrays as (rgb) values.
4711 t3
= N
.outerproduct(norm_arr
, N
.array([1,1,1], 'b'))
4712 t4
= N
.reshape(t3
, (rows
, cols
, 3))
4713 pix_arr
= N
.array(t4
, 'b')
4715 #*** Get the pixbuf.
4716 # array input indexing is row, pixel, [r,g,b {,a} ]
4717 pixbuf
= gtk
.gdk
.pixbuf_new_from_array(pix_arr
,
4718 gtk
.gdk
.COLORSPACE_RGB
, 8)
4720 #*** 3. display pixbuf as item
4721 d_canv
= w_
.canvas
.view
4722 d_rt
= d_canv
.root().add(canvas
.CanvasGroup
)
4723 d_rt
.add(canvas
.CanvasPixbuf
,
4724 x
= 20, # world units
4728 width
= 15, # world units
4734 # Image orientation is consistent with `norm_arr` printed form.
4735 # Indexing is (row, col) from upper left.
4736 # Storage is row-major?
4738 # This is the first view of a VALUE -- the others are astTypes.
4739 # Useful value information is the program source.
4742 # "anchor" GtkAnchorType : Read / Write
4743 # "height" gdouble : Read / Write
4744 # "height-in-pixels" gboolean : Read / Write
4745 # "height-set" gboolean : Read / Write
4746 # "pixbuf" GdkPixbuf : Read / Write
4747 # "width" gdouble : Read / Write
4748 # "width-in-pixels" gboolean : Read / Write
4749 # "width-set" gboolean : Read / Write
4750 # "x" gdouble : Read / Write
4751 # "x-in-pixels" gboolean : Read / Write
4752 # "y" gdouble : Read / Write
4753 # "y-in-pixels" gboolean : Read / Write
4756 # # Other pixmap sources:
4757 # image = gtk.Image()
4758 # # use the current directory for the file
4760 # pixbuf = gtk.gdk.pixbuf_new_from_file(GTKLOGO_IMAGE)
4761 # image.set_from_pixbuf(pixbuf)
4763 # image = gtk.Image()
4764 # image.set_from_file(BUDDY_IMAGE);
4766 # image.get_pixbuf()
4767 # # pixbuf -> canvas
4769 # gtk.gdk.pixbuf_new_from_array
4770 # gtk.gdk.Pixbuf.get_pixels_array
4771 # gtk.gdk.Pixbuf.save
4772 # call gtk.Image.queue_draw() after you change the Numeric array.
4775 # # from EMAN2.EMNumPy import numpy2em, em2numpy
4778 #* gtk images, numpy, eman
4781 from EMAN2
import EMNumPy
4785 ema_arr
= em
.EMData()
4786 EMNumPy
.numpy2em(num_arr
, ema_arr
)
4791 ti
= EMAN2
.test_image(type = 1, size
= (128,64))
4792 num_arr
= EMNumPy
.em2numpy(ti
)
4794 # [Fri Mar 16 16:25:40 2007]: transposed: (64, 128)
4795 # [Mon Mar 19 11:23:08 2007]: eman2 fix by Grant Tang.
4796 # [Thu Mar 22 10:36:14 2007]: order confusion between eman2/hdf/numpy...
4800 #*** 1. get numpy array
4801 num_arr
= N
.random
.ranf( (5,4) )
4803 #*** 2.b. numpy -> gtk
4804 # Convert float array to 8-bit array.
4805 rows
, cols
= num_arr
.shape
4806 bottom
= N
.min(num_arr
)
4807 top
= N
.max(num_arr
)
4808 scale
= (top
- bottom
)
4809 norm_arr
= ((num_arr
- bottom
) / scale
* 255).astype(N
.uint8
).reshape(rows
*cols
)
4812 #*** Use 3 8-bit arrays as (rgb) values.
4813 t3
= N
.outer(norm_arr
, N
.array([1,1,1], 'b'))
4814 t4
= N
.reshape(t3
, (rows
, cols
, 3))
4815 pix_arr
= N
.array(t4
, dtype
=N
.uint8
)
4817 #*** Get the pixbuf.
4818 # array input indexing is row, pixel, [r,g,b {,a} ]
4819 # TypeError: pixbuf_new_from_array() argument 1 must be array, not
4821 # pixbuf = gtk.gdk.pixbuf_new_from_array(pix_arr,
4822 # gtk.gdk.COLORSPACE_RGB, 8)
4823 pixbuf
= gtk
.gdk
.pixbuf_new_from_data(
4824 pix_arr
.tostring(order
='a'),
4825 gtk
.gdk
.COLORSPACE_RGB
,
4832 #*** compare to input
4833 pixbuf
.get_pixels_array()
4834 print pixbuf
.get_pixels_array().shape
4835 print pixbuf
.get_height()
4836 print pixbuf
.get_width()
4839 #*** 3. display pixbuf as item
4840 d_canv
= w_
.canvas
.view
4841 d_rt
= d_canv
.root().add(canvas
.CanvasGroup
)
4842 d_rt
.add(canvas
.CanvasPixbuf
,
4843 x
= 20, # world units
4847 width
= 15, # world units
4853 #* minimal EMData display test
4857 inline("import EMAN2 ")
4858 ti
= EMAN2
.test_image(type = 1, size
= (128,64))
4859 # program > menu > run code
4860 # ti > menu > insert values
4864 tm_tree
= reader
.parse('''
4865 inline("import EMAN2 ")
4866 ti = EMAN2.test_image(type = 0, size = (64,64))
4868 tm_play_env
= interp
.get_child_env(def_env
, w_
.state_
.storage
)
4869 tm_tree
.setup(empty_parent(), tm_play_env
, w_
.state_
.storage
)
4870 tm_l3pic
= w_
.canvas
.view
.start_add_l3tree(tm_tree
)
4873 tm_l3pic
._l3tree
.interpret(w_
.state_
.def_env
, w_
.state_
.storage
)
4875 val
, val_id
= ast
.val2ast(tm_l3pic
.get_values_list())\
4876 .setup(empty_parent(),
4877 Env('dummy_env', None, None, storage
),
4879 w_
.canvas
.view
.add_l3tree(val
, RenderTable())
4883 #* l3IfOutline addition
4884 # High-level interface via outlining.
4885 #** original tree (simple)
4886 # Body from demo/proj-align/s1a.py, truncated for simplicity.
4887 ref_t
= reader
.parse_file("./demo/proj-align/s1a-in.py")
4888 ref_env
= interp
.get_child_env(def_env
, storage
)
4889 ref_t
.setup(empty_parent(), ref_env
, storage
)
4890 view
.print_info(storage
, ref_t
)
4893 # (set-variable (quote outline-regexp) "[ ]*\\#+\\*+")
4894 # (set-variable (quote outline-regexp) "\\W+\" +" nil)
4896 #** OR: original tree (full)
4897 ref_t
= reader
.parse_file("./demo/proj-align/s1a.py")
4898 ref_env
= interp
.get_child_env(def_env
, storage
)
4899 ref_t
.setup(empty_parent(), ref_env
, storage
)
4900 # view.print_info(storage, ref_t)
4901 #** add outline cross-refs
4902 ref_t
.set_outl_edges(w_
, None)
4903 #** examine embedded outline (text)
4904 view
.print_info(storage
, ref_t
, custom
= "nd._outl_parent._id")
4906 ref_pic
= w_
.canvas
.view
.start_add_l3tree(ref_t
)
4911 #*** outline tree, flat
4912 ref_o
= ref_t
.get_outline(w_
)
4915 ref_prun
= ref_o
.prune(w_
)
4917 view
.print_info(storage
, ref_prun
, custom
= "nd._real_tree._id")
4920 #*** OR: outline tree, nested
4921 ref_o
= ref_t
.get_outline(w_
, outl_type
= 'nested')
4923 view
.print_info(storage
, ref_o
)
4925 ref_prun
= ref_o
.prune(w_
)
4927 view
.print_info(storage
, ref_prun
)
4929 #*** OR: outline tree, subtree
4930 # (nested outline to level N, then content)
4931 # Pick a nested outline manually.
4932 # here, 'Compute proj...'
4933 storage
.load(40886).set_outline('subtree')
4935 ref_o
= ref_t
.get_outline(w_
, outl_type
= 'nested')
4937 # view.print_info(storage, ref_o)
4939 ref_prun
= ref_o
.prune(w_
)
4941 # view.print_info(storage, ref_prun)
4945 ref_t_pic
= w_
.canvas
.view
.start_add_l3tree(ref_t
)
4948 #*** display outline
4949 ref_pic
= w_
.canvas
.view
.start_add_l3tree(ref_prun
)
4953 tm_tree
= reader
.parse('''
4954 if ("outline", !! TITLE "sample"):
4958 #** single expression
4961 l3tree
= reader
.parse(ref_s
)
4965 #** single nesting level
4968 template
= reader
.parse(code_s
)
4974 template
= reader
.parse(code_s
)
4979 vl
, _
= viewList(aList([])).setup(empty_parent(), def_env
, storage
)
4980 sl
, _
= List(aList([])).setup(empty_parent(), def_env
, storage
)
4982 vls
= viewList().setup_viewlist(w_
, ref_t
)
4986 #* list display tests
5012 #* viewlist display tests
5015 # things are to be done...
5016 if ("outline", "Load volume."):
5017 vol
= getImage(ev("$SPXROOT/sparx/test/model001.tcp"))
5018 # See http://www.macro-em.org/sparxwiki/prep_vol
5019 (volft
, kb
) = prep_vol(vol
)
5021 # Like these wonderful ones...
5022 if ("outline", "Compute projection angles"):
5023 angles
= even_angles(10.0, 90.0, 90.1, 0.0, 179.9,'P')
5024 nangles
= len(angles
)
5029 #* system identification
5034 #* l3-system interface
5038 sc
.system('echo', ['a'], [])
5039 sc
.system_wrap('echo', 'a', n
='')
5041 from l3lang
.l3
import *
5050 #* system interface via shell wrapper
5051 #** prepare sample data directory, python
5053 ev
= os
.path
.expandvars
5054 def ignore(cmd
, *args
, **kwd
):
5060 ignore(os
.mkdir
, 'foo-sh-wrap')
5061 os
.chdir('foo-sh-wrap')
5063 ev('$HOME/w/sparx/xmipp/Basic_Demo/walkthrough/taguA.tot'),
5066 ev('$HOME/w/sparx/xmipp/Basic_Demo/walkthrough/taguA.tot.inf'),
5069 ev('$HOME/w/sparx/xmipp/Basic_Demo/walkthrough/tagtA.tot'),
5072 ev('$HOME/w/sparx/xmipp/Basic_Demo/walkthrough/tagtA.tot.inf'),
5076 #** call using assumed return value structure
5077 # This is completely impractical; see ref[Mon Apr 16 14:35:34 2007]
5079 os
.system('pwd; ls')
5080 (angles
, coords
, images
, sel_tilt
, sel_untilt
) = xmipp_mark("taguA.tot")
5083 #** call in directory
5084 # This is a minimalistic approach, letting the user select directory
5085 # entries after execution.
5087 #*** using xmipp_mark found on system path
5088 os
.system('pwd; ls -l')
5089 xmipp_mark(w_
, "taguA.tot", tilted
= "tagtA.tot")
5091 # menu -> save coords > tag.pos
5092 # menu -> save angles > tag.ang
5093 # menu -> generate images > images.xmp
5097 os
.system('pwd; ls -l')
5100 #*** using l3lang.external module
5101 from l3lang
.external
import xmipp_wrap
5102 xmipp_wrap
.xmipp_mark("/net/cci/hohn/w/sparx/xmipp/Basic_Demo/walkthrough/taguA.tot",
5104 "/net/cci/hohn/w/sparx/xmipp/Basic_Demo/walkthrough/tagtA.tot")
5106 #** with l3lang.external module
5107 #*** using subdirectory, l3
5108 # For batch programs, the toplevel should suffice:
5109 from l3lang
.l3
import *
5113 # l3 input, using manually specified full paths.
5115 inline
"from l3lang.external.xmipp_wrap import *"
5119 (status
, log
) = xmipp_mark(
5120 "/net/cci/hohn/w/sparx/xmipp/Basic_Demo/walkthrough/taguA.tot",
5121 tilted
= "/net/cci/hohn/w/sparx/xmipp/Basic_Demo/walkthrough/tagtA.tot")
5125 # Next, try commands using the file(s).
5127 cp(sd
.foobar
, "foobar_copy")
5130 # value inspection, via local eval:
5131 # self.w_.state_.storage.load(self._l3tree._id)
5134 #*** directory only tests, l3
5135 # [Fri Apr 20 16:23:21 2007]
5137 inline
"from l3lang.external.xmipp_wrap import *"
5144 # Next, try commands using the file(s).
5146 cp(sd
.foobar
, "foobar_copy")
5148 print sd1
.foobar_copy
5150 # [Thu Apr 19 15:32:15 2007]
5151 # state save sequence
5153 # (save state st.no-exec.1)
5162 inline
'from rpy import *'
5164 d
= r
.capabilities()
5166 device
= r
.png
# Robj, won't pickle
5169 device
= r
.postscript
5172 faithful_data
= r
.faithful
# short form:
5173 ed
= faithful_data
["eruptions"]
5178 inline
"import Numeric as N; import MLab as M"
5180 num_arr
= M
.rand(4,4) # Get numeric array.
5182 num_arr
= M
.rand(4,4)
5183 (rows
, cols
) = num_arr
.shape
5184 bottom
= M
.min(M
.min(num_arr
))
5185 top
= M
.max(M
.max(num_arr
))
5186 scale
= (top
- bottom
)
5187 norm_arr
= ((num_arr
- bottom
) / scale
* 255).astype('b') # Convert float array to 8-bit array.
5189 t3
= N
.outerproduct(norm_arr
, N
.array([1,1,1], 'b'))
5190 t4
= N
.reshape(t3
, (rows
, cols
, 3))
5191 pix_arr
= N
.array(t4
, 'b') # Use 3 8-bit arrays as (rgb) values.
5194 #* external test collection
5198 e
.set_value_at(0,1, 1.0)
5199 print "original at start"
5202 f1
= e
.process("mirror",{"axis":'x'})
5203 print "new mirrored image"
5207 #** image display test
5208 # [Fri Apr 20 16:48:25 2007]
5210 inline("import EMAN2 ")
5211 ti
= EMAN2
.test_image(type = 1, size
= (128,64))
5212 # program > menu > run code
5213 # ti > menu > insert list of values l3
5216 #** Micrograph and particle selection
5217 # Using the xmipp demo and data.
5219 inline
'''from l3lang.external.xmipp_wrap import \
5220 xmipp_mark, xmipp_do_selfile'''
5222 cp('$HOME/w/sparx/xmipp/Basic_Demo/walkthrough/taguA.tot',
5224 cp('$HOME/w/sparx/xmipp/Basic_Demo/walkthrough/taguA.tot.Common.pos',
5225 'taguA.tot.Common.pos')
5226 cp('$HOME/w/sparx/xmipp/Basic_Demo/walkthrough/taguA.tot.inf',
5228 cp('$HOME/w/sparx/xmipp/Basic_Demo/walkthrough/tagtA.tot',
5230 cp('$HOME/w/sparx/xmipp/Basic_Demo/walkthrough/tagtA.tot.Common.pos',
5231 'tagtA.tot.Common.pos')
5232 cp('$HOME/w/sparx/xmipp/Basic_Demo/walkthrough/tagtA.tot.inf',
5236 # Do the following in xmipp_mark:
5238 # file -> load coords
5239 # The file paths must be retrieved from l3, via
5240 # orig subdir > menu > pwd
5242 # file -> save angles
5244 # file -> generate images
5249 # 1551:SelFile::read: File
5250 # ..../Subdir-30214/taguA.tot.tot.Common.sel
5253 (status
, log
) = xmipp_mark( orig
.taguA_tot
,
5254 tilted
= orig
.tagtA_tot
)
5256 # Selection file listing images in current directory.
5257 xmipp_do_selfile("img_t*.xmp", "tilted.sel")
5258 xmipp_do_selfile("img_u*.xmp", "untilted.sel")
5260 # Selection file listing images relative to main directory.
5261 # A full xmipp wrapper would hide selection files completely.
5262 print (mark
.l3_dirname() + "/" + "img_t*.xmp", "tilted.sel")
5263 xmipp_do_selfile(mark
.l3_dirname() + "/" + "img_t*.xmp", "tilted.sel")
5264 xmipp_do_selfile(mark
.l3_dirname() + "/" + "img_u*.xmp", "untilted.sel")
5267 # Even without any matching input files,
5268 # xmipp_do_selfile("img_u*.xmp", "untilted.sel")
5269 # returns status zero ...
5277 >> Need xmipp_show to
view (intermediate
) images
.
5280 #** generate phantom
5281 inline
"import l3lang.external.xmipp_wrap as xw"
5282 vol
, fourpr
= xw
.pdbphantom("input-data/1P30.pdb",
5283 sampling_rate
= 1.6,
5284 high_sampling_rate
= 0.8,
5289 #** data alignment and classification
5293 produce
.doc
file -- view
as plain text
.
5302 #** nested loop data selection
5303 # [Wed May 16 12:01:45 2007]
5317 from l3lang
.pretty
import *
5319 #** manual construct / convert
5320 doc
= DocText("hello") |
DocBreak(" ") |
DocText("world")
5326 #** construct / convert
5327 B
= lambda : DocBreak(" ")
5328 T
= lambda t
: DocText(t
)
5329 G
= lambda x
: DocGroupAuto(x
)
5330 doc
= G(T("begin") |
B() |
5331 G(T("stmt1;") |
B() |
5333 T("stmt3;")) |
B() |
5336 print doc
.toString(40)
5337 print doc
.toString(8)
5338 print doc
.toString(4)
5341 #** manual construct / convert
5342 B
= lambda : DocBreak(" ")
5343 T
= lambda t
: DocText(t
)
5344 G
= lambda x
: DocGroupAuto(x
)
5345 GB
= lambda x
: DocGroupBreak(x
)
5346 GF
= lambda x
: DocGroupFill(x
)
5347 doc
= G(T("begin") |
B() |
5348 G(T("stmt1;") |
B() |
5353 print doc
.toString(40) # no breaks
5354 print doc
.toString(20) # break outer group
5355 print doc
.toString(4) # break all
5358 #** manual construct w/ indentation
5359 B
= lambda : DocBreak(" ")
5360 C
= lambda x
,y
: DocCons(x
, y
)
5361 T
= lambda t
: DocText(t
)
5362 G
= lambda x
: DocGroupAuto(x
)
5363 GB
= lambda x
: DocGroupBreak(x
)
5364 GF
= lambda x
: DocGroupFill(x
)
5365 I
= lambda r
, x
: DocNest(r
, x
)
5366 doc
= G(T("begin") |
5369 G( C(C(T("stmt1;"), B()), T("stmt3;"))))
5373 print doc
.toString(40) # no breaks
5374 print doc
.toString(20) # break outer group
5375 print doc
.toString(4) # break all
5377 #* full-tree pretty printing
5378 #** manual comparison
5387 tree
= reader
.parse(code_s
)
5390 play_env
= interp
.get_child_env(def_env
, storage
)
5391 tree
.setup(empty_parent(), play_env
, storage
)
5392 # # view.print_info(storage, tree)
5393 print tree
.get_infix_string(40)
5394 print tree
.get_infix_string(10)
5397 #** print-parse-compare cycle function
5398 def comp_trees(otree
, otree_tbl
, pptree
, pptree_tbl
):
5399 # Compare trees and associated comments
5403 print "Check for differences..."
5404 print "--------------------------------------------"
5405 o_it
= otree
.top_down()
5406 pp_it
= pptree
.top_down()
5408 try: o_nd
= o_it
.next()
5409 except StopIteration: break
5410 pp_nd
= pp_it
.next()
5413 if o_nd
.eql_1(pp_nd
):
5414 if otree_tbl
.has_key(o_nd
._id
):
5415 if otree_tbl
[o_nd
._id
].eql_1(pptree_tbl
[pp_nd
._id
]):
5418 print ("Comment difference from line %d, col %d\n"
5419 " to line %d, col %d\n"
5420 % (o_nd
._first
_char
[0], o_nd
._first
_char
[1],
5421 pp_nd
._first
_char
[0], pp_nd
._first
_char
[1]))
5422 print otree_tbl
[o_nd
._id
]
5423 print pptree_tbl
[pp_nd
._id
]
5427 print ("Tree difference: line %d, col %d\n original:\n%s\n"
5430 (pp_nd
._first
_char
[0],
5431 pp_nd
._first
_char
[1],
5436 def ppc(code_s
, width
):
5438 tree
= reader
.parse(code_s
).single_program()
5439 tree_env
= interp
.get_child_env(def_env
, storage
)
5440 tree
.setup(empty_parent(), tree_env
, storage
)
5441 associate_comments(w_
, tree
, code_s
)
5443 # Tree from printed output.
5444 pp_string
= tree
.get_infix_string(width
, w_
.deco_table_
)
5446 print "Pretty-printed string, width %d: " % width
5447 print "--------------------------------------------"
5450 pp_tree
= reader
.parse(pp_string
).single_program()
5451 pp_env
= interp
.get_child_env(def_env
, storage
)
5452 pp_tree
.setup(empty_parent(), pp_env
, storage
)
5453 associate_comments(w_
, pp_tree
, pp_string
)
5455 comp_trees(tree
, w_
.deco_table_
, pp_tree
, w_
.deco_table_
)
5457 def ppcm(code_s
, width_list
):
5458 for width
in width_list
:
5467 #** string special case
5468 #*** repr / str behavior
5471 aa
= reader
.parse('"""a\nb"""')
5472 chars(aa
.body()._primary
[0])
5474 aa
= reader
.parse('"""Outer radius : %i\n"""')
5475 chars(aa
.body()._primary
[0])
5478 str(aa
.body()._primary
[0])
5479 # str(aa) converts unprintables into ascii
5480 # repr(aa) converts unprintables into ascii and escapes when needed
5493 d = r.capabilities()
5495 device = r.png # Robj, won't pickle
5498 device = r.postscript
5506 #** def special case
5518 #** loop (set, function, operators)
5520 # Now for something else...
5521 # ... like a comment
5533 #** program (program, subdir, tuple, comment)
5545 cp('$HOME/w/sparx/xmipp/Basic_Demo/walkthrough/taguA.tot',
5549 inline "from l3lang.external.xmipp_wrap import xmipp_mark"
5550 # file -> load coords
5551 # The file paths must be retrieved from l3, via an Env
5552 # listing from the original subdir.
5554 (status, log) = xmipp_mark( orig.taguA_tot,
5555 tilted = orig.tagtA_tot )
5560 #** comment in expression
5575 if ("outline", "outer 1"):
5576 if ("outline", "tools"):
5581 if ("outline", "inner 2"):
5586 #* Local node examination.
5587 # From a widget with _l3tree
5588 st
= self
.w_
.state_
.storage
5589 st
.load(self
._l3tree
._id
)
5590 res
= st
.get_attribute(self
._l3tree
._id
, "interp_result")
5592 #* ctf, emdata and numpy
5594 from EMAN2
import EMNumPy
5596 from numpy
import flipud
, fliplr
5599 #** conversion check (indep.)
5602 e1
.process_inplace('testimage.noise.uniform.rand')
5603 e1
.get_data_pickle() # emdata -> std::vector -> python list
5608 #** conversion check (indep.)
5610 from EMAN2
import EMNumPy
5613 n1
= N
.random
.ranf( (5,4) )
5614 e1
= EMData(); EMNumPy
.numpy2em(n1
, e1
)
5615 n2
= EMNumPy
.em2numpy(e1
)
5616 assert N
.max(N
.max(n2
- n1
)) == 0, \
5617 "Array conversion failed (error = %g)." % N
.max(N
.max(n2
- n1
))
5619 #** Get spider format image.
5620 image
= getImage('foo.spi')
5622 #** Flip it to get correct layout.
5623 img_num
= flipud(fliplr(EMNumPy
.em2numpy(image
)))
5625 EMNumPy
.numpy2em(img_num
, img_em
)
5627 #*** Verify the conversion.
5628 num_2
= EMNumPy
.em2numpy(img_em
)
5629 assert N
.max(N
.max(num_2
- img_num
)) == 0, "Array conversion failed."
5631 # Check a single element
5633 N
.where( (img_num
- img_em
.get_value_at(0,0)) < 1e-14)
5634 # No matches. This means that the (0,1) element must be wrong,
5635 # not merely misplaced.
5636 N
.where( (img_num
- img_em
.get_value_at(0,1)) < 1e-14)
5640 ctf_img
= filt_ctf(image
, 2000, 2.0, 200,
5643 #** Save the image with ctf applied.
5644 dropImage(ctf_img
, "ctf.spi", itype
= 's')
5647 import numpy
as N
, pickle
, types
5649 print N
.version
.version
5652 aa
= N
.array([ 1, 2, 3, 4 ]);
5654 print type(aa1
) == types
.BuiltinFunctionType
5658 pickle
.dumps(N
.reshape
)
5661 #* Numeric and pickle
5662 import Numeric
as N
, pickle
5666 aa
= N
.array([ 1, 2, 3, 4 ]);
5668 print type(aa1
) == types
.BuiltinFunctionType
5672 pickle
.dumps(N
.reshape
)
5674 #* outline traversal
5676 for (nd
, level
) in w_
.lib_tree
.body().outl_top_down():
5677 print " " * level
, level
, nd
.l_label
5680 def time_func(fun
, *args
, **kwd
):
5681 from time
import time
5684 print time() - start
5686 #* eman2 pickle tests
5687 # - Works when run in one session.
5688 # - Works across sessions.
5689 import sparx
as sx
, l3lang
.utils
as ut
5690 volume
= sx
.getImage( "model.tcp" )
5691 print volume
.get_xsize() * volume
.get_ysize() * volume
.get_zsize()
5693 image
= sx
.project(volume
, [10.0, 20.0, 30.0, 0.0, 0.0], 35)
5694 print image
.get_xsize()
5697 time_func(cPickle
.dumps
, image
, protocol
= 2)
5699 ut
.file_pickle(image
, 'st.1')
5700 image_ld
= ut
.file_unpickle('st.1')
5705 # st = cPickle.dumps(volume, protocol=2)
5706 time_func(cPickle
.dumps
, volume
, protocol
=2)
5708 time_func(sx
.dropImage
, volume
, "st.spi", itype
= 's')
5710 ut
.file_pickle(volume
, 'st.2')
5711 # Load in separate session.
5712 import sparx
as sx
, l3lang
.utils
as ut
5713 volume_ld
= ut
.file_unpickle('st.2')
5715 vol_st
= volume_ld
.__getstate
__()
5717 #* Set() special form: aa[ii] = rhs
5718 #** single-index pattern derivation
5719 aa
= reader
.parse("aa[i] = foo").body()
5721 # Set(Call(Member(Symbol('operator'),
5722 # Symbol('getitem')),
5723 # aList([Symbol('a'),
5727 aa
= reader
.parse("! AA[ ! II ] = ! RHS").body()
5729 Set(Call(Member(Symbol('operator'),
5731 aList([Marker('AA'),
5735 #** transform to `operator.setitem(aa, ii, rhs)`
5736 print reader
.parse('operator.setitem(!AA, !II, !RHS)').body()
5738 Call(Member(Symbol('operator'),
5740 aList([Marker('AA'),
5745 #** numeric: single-bracket, multi-index
5747 aa
= N
.reshape(N
.array([ 1, 2, 3, 4 ]), (2,2))
5751 print reader
.parse("aa[0,1]").body()
5754 print reader
.parse("aa[(0,1)]").body()
5756 print operator
.getitem(aa
, (0,1))
5758 # Call(Member(Symbol('operator'),
5759 # Symbol('getitem')),
5760 # aList([Symbol('aa'),
5761 # Tuple(aList([Int(0),
5764 # So the single-index pattern will work.
5767 print reader
.parse("aa[ii] = foo").body()
5769 # Circumvents l3 value tracking.
5775 #* Set() special form: aa[ii][jj] = rhs
5777 aa
= reader
.parse("! AA[ ! II ] [ !JJ ] = ! RHS").body()
5779 Set(Call(Member(Symbol('operator'),
5781 aList([Call(Member(Symbol('operator'),
5783 aList([Marker('AA'),
5789 #** Transformed out.
5790 print reader
.parse('''
5792 operator.getitem(!AA, !II),
5795 Call(Member(Symbol('operator'),
5797 aList([Call(Member(Symbol('operator'),
5799 aList([Marker('AA'),
5805 print reader
.parse("aa[ii][jj] = foo").body()
5807 # Circumvents l3 value tracking.
5814 #* Set special form: a.foo = 2
5815 # Still uses setitem.
5818 operator
.setitem(aa
, 'foo', 2)
5822 #** n behavior, manual eval
5823 # This lets python convert the \n to an escape:
5824 aa
= reader
.parse('"""Outer radius : %i\n""" % 123')
5825 chars(aa
.body()._primary
[1][0]._primary
[0])
5828 play_env
= interp
.get_child_env(def_env
, storage
)
5829 aa
.setup(empty_parent(), play_env
, storage
)
5830 view
.print_info(storage
, aa
)
5833 news
= aa
.interpret(play_env
, storage
)
5834 chars(news
[0]) # has actual newline
5835 #** n behavior, pasting
5836 """Outer radius : %i\n""" % 123
5837 # run code => ('Outer radius : 123\\n', 5496)
5840 45065 -22 45065 String('Outer radius : %i\n')
5843 >>> print """Outer radius : %i\n""" % 123
5844 Outer radius
: 123\n
5847 45072 5494 45072 String('Outer radius : %i\\n')
5849 #** difference, commit c2c6cff9496972d40011002de24d20abca1a6bab and before
5851 # This lets python convert the \n to an escape:
5852 aa
= reader
.parse('"""Outer radius : %i\n""" % 123')
5854 # This won't, and that's the behavior observed from the gui / toplevel.
5855 aa
= reader
.parse(r
'"""Outer radius : %i\n""" % 123')
5857 # This also contains \\n instead of the control code.
5858 pprint
.pprint([ii
for ii
in tokenize
.generate_tokens(string_readline(r
'"Outer radius : %i\n"'))])
5861 #** difference removed
5862 # both contain \n control code.
5863 print reader
.parse('"""Outer radius : %i\n""" % 123')
5865 # This won't, and that's the behavior observed from the gui / toplevel.
5866 print reader
.parse(r
'"""Outer radius : %i\n""" % 123')
5869 #* pytables for reading hdf
5872 # File is available after ali2d.l3 run
5873 fi
= tables
.openFile("_K-8-133679/average.hdf")
5874 # method listNodes in module tables.file:
5875 fi
.listNodes
# show all
5876 [li
for li
in fi
.listNodes('/MDF/images')]
5878 # RootGroup in module tables.group object:
5881 fi
.root
== fi
.getNode('/') # true
5883 for node
in fi
.root
:
5886 list(fi
.root
) # list from iterator
5887 list(fi
.root
)[0] # first child
5888 list(list(fi
.root
)[0]) # first child's children
5892 fi
.listNodes('/') # get list
5893 (fi
.listNodes('/')[0])._v
_name
5895 for sub
in fi
.listNodes('/'):
5899 # >>> fi.getNode('/')
5901 # children := ['MDF' (Group)]
5902 # >>> fi.getNode('/').getNode('MDF')
5903 # Segmentation fault
5905 fi
.getNode('/MDF/images')
5906 fi
.getNode('/MDF/images')._v
_attrs
# print
5907 fi
.getNode('/MDF/images')._v
_attrs
._g
_listAttr
() # get list
5909 fi
.getNode('/MDF/images')._f
_getChild
('1')
5911 fi
.getNode('/MDF/images/1')
5912 # tables.attributeset.AttributeSet'>
5913 fi
.getNode('/MDF/images/1')._v
_attrs
# print info
5914 fi
.getNode('/MDF/images/1')._v
_attrs
._g
_listAttr
() # get list
5915 (fi
.getNode('/MDF/images/1')._v
_attrs
)._f
_list
() # get list (better)
5916 fi
.getNode('/MDF/images/1')._v
_attrs
._g
_getAttr
('EMAN.Class_average')
5917 fi
.getNode('/MDF/images/1')._v
_attrs
._g
_getAttr
('EMAN.nobjects')
5918 fi
.getNode('/MDF/images/1')._v
_attrs
._g
_getAttr
('EMAN.members')
5921 # Array in module tables.array
5922 arr
= fi
.getNode('/MDF/images')._f
_getChild
('1')._f
_getChild
('image')
5923 arr
= fi
.getNode('/MDF/images/1/image')
5925 a00
= arr
[0,0] # Returns a 0-D array
5926 a00
= arr
[0:1,0:1] # Returns a slice copy.
5929 #** relevant for eman2 use
5931 # 1. The pytable classes:
5933 # / <class 'tables.group.RootGroup'>
5934 # MDF <class 'tables.group.Group'>
5935 # images <class 'tables.group.Group'>
5936 # 0 <class 'tables.group.Group'>
5937 # image <class 'tables.array.Array'>
5938 # 1 <class 'tables.group.Group'>
5939 # image <class 'tables.array.Array'>
5941 # 2. The attributes of a Group
5942 # ... members in a Group start with some reserved prefix, like _f_
5943 # (for public methods), _g_ (for private ones), _v_ (for instance
5944 # variables) or _c_ (for class variables).
5946 # tables.attributeset.AttributeSet'>
5947 # fi.getNode('/MDF/images/1')._v_attrs # print info
5949 # Get list of attributes.
5950 (fi
.getNode('/MDF/images/1')._v
_attrs
)._f
_list
()
5952 # Get attribute with arbitrary name
5953 (fi
.getNode('/MDF/images/1')._v
_attrs
).__getattr
__('EMAN.Class_average')
5954 (fi
.getNode('/MDF/images/1')._v
_attrs
).__getattr
__('EMAN.members')
5957 (fi
.getNode('/MDF/images/1')._v
_attrs
).new_stuff
= 1
5959 # got tables.exceptions.FileModeError: the file is not writable
5963 #*** Traverse the object tree
5964 def nshow(fi
, indent
= 0):
5965 if hasattr(fi
, '_v_name'):
5966 print " "*indent
, fi
._v
_name
, type(fi
)
5967 if isinstance(fi
, tables
.array
.Array
):
5968 # For tables.array, iteration is over the rows; this shows
5969 # 75 1-d numpy arrays per image.
5972 nshow(sub
, indent
= indent
+ 1)
5974 print " "*indent
, type(fi
)
5978 #*** simplified access interface for l3 use
5979 # Inject l3_* methods.
5981 'Return the number of images.'
5982 return self
.getNode('/MDF/images')._v
_nchildren
5983 tables
.file.File
.l3_len
= l3_len
5986 def l3_getimg(self
, index
):
5987 'Return the image at `index`.'
5988 return self
.getNode('/MDF/images')._f
_getChild
(str(index
))._f
_getChild
('image')
5989 tables
.file.File
.l3_getimg
= l3_getimg
5992 def l3_getatts(self
, index
):
5993 ''' Return a (name, value) list holding all the attributes of
5997 attset
= self
.root
.MDF
.images
._f
_getChild
(ind
)._v
_attrs
5998 return [ (att
, attset
.__getattr
__(att
)) for att
in attset
._f
_list
()]
5999 tables
.file.File
.l3_getatts
= l3_getatts
6002 def l3_getatt(self
, index
, name
):
6003 ''' Return the `name` attribute of the image at `index`.'''
6005 attset
= self
.root
.MDF
.images
._f
_getChild
(ind
)._v
_attrs
6006 return attset
.__getattr
__(str(name
))
6007 tables
.file.File
.l3_getatt
= l3_getatt
6011 #*** tables access tests
6015 fi
.l3_getatt(0, 'EMAN.Class_average')
6016 fi
.l3_getatt(0, 'EMAN.nobjects')
6017 fi
.l3_getatt(0, 'EMAN.members')
6022 # Using the simple function
6025 # Updates bounding boxes, among others.
6026 while gtk
.events_pending():
6027 gtk
.main_iteration()
6035 # as long as draw() does not block.
6038 from threading
import Lock
6044 value
.append(raw_input(prompt
))
6046 value
.append(EOFError())
6049 thread
.start_new_thread(_do
, ())
6050 # Manual main loop until input is ready.
6051 while not lck
.acquire(False):
6054 if isinstance(value
[-1], Exception):
6058 #* gui/console hybrid use
6059 #** highly irregular data, not used
6060 inline('from numpy import *; from pylab import *')
6061 for ii
in range(12*3):
6062 a1
= (ii
, array([-2100, -200, -100 / 2.0, -1200]))
6063 # Added to loop body.
6066 a2
= (ii
, sum(a1
[1]))
6067 (ii
% 2 == 0) and -200 or 0
6069 # This is just too painful through the gui; there is too much
6070 # heterogeneous data.
6071 #** highly irregular data, python
6072 # Just defining and editing the textual data structure is FAR
6074 # (month, amount, what)
6075 specials
= [(1, [(-200, "this")]),
6076 (22, [(300, "that")]),
6078 regular
= [(mon
, [(-100, "one"),
6080 ]) for mon
in range(0, 12*3)]
6081 spec_d
= dict(specials
)
6082 # for (month, mon_dat) in regular:
6083 # print (month, mon_dat + spec_d.get(month, []))
6085 [ (month
, mon_dat
+ spec_d
.get(month
, []))
6086 for (month
, mon_dat
) in regular
]
6089 #** highly irregular data, l3
6091 def combine(specials, regular, r_from, r_to):
6092 reg_list = [(mon, regular) for mon in xrange(r_from, r_to)]
6093 spec_d = dict(specials)
6094 return [ (month, mon_dat + spec_d.get(month, []))
6095 for (month, mon_dat) in reg_list]
6098 specials
= [(1, [(-200, "this")]),
6099 (22, [(300, "that")]),
6101 regular
= [(-100, "one"),
6104 combined
= combine(specials
, regular
, 0, 3*12)
6108 # if Env's keys include any python hashables, an Env can accumulate
6109 # (int, val) bindings in addition to (name, val) bindings
6110 stuff
= if ("map", "embedded"):
6111 for ii
in range(0, 3*12):
6112 l3
.env().set(ii
, [(-100, "one"), (-200, "two")])
6114 l3set(1, [(-200, "this")])
6115 l3set(22, [(300, "that")])
6119 #* list comprehension
6120 [(mon
, [(-100, "one"),
6122 ]) for mon
in range(0, 12*3)]
6123 # As tail call, roughly:
6124 def _foo(arg
, seq
, accum
):
6127 _foo(arg
, seq
[1:None], accum
)
6128 _foo(mon
, range(0, 12*3), [])
6129 # this is already FAR to tedious, and a tracked (l3) expansion is not
6133 # From matplotlib-0.90.1/examples/date_demo1.py
6135 # This example requires an active internet connection since it uses
6136 # yahoo finance to get the data for plotting
6138 # The scripts are independently runnable, and must be edited to form a
6142 #** Collect finance data using yahoo's service.
6143 if ("outline", "Collect finance data"):
6145 from matplotlib.finance import quotes_historical_yahoo
6148 # <span background="#ffdddd">Retrieve data for stock symbol.</span>
6149 quotes
= quotes_historical_yahoo(
6151 datetime
.date( 1995, 1, 1 ),
6152 datetime
.date( 2004, 4, 12 ),
6155 error("Unable to get data")
6157 # <span background="#ffdddd">The data attributes of quotes.</span>
6169 if ("outline", "Date plot"):
6170 # <span background="#ffdddd">Plot details.</span>
6175 def plot_date(fname, dates, values):
6176 from pylab import figure, show
6178 from matplotlib.dates import YearLocator, MonthLocator, DateFormatter
6180 fig = figure(figsize=(7,4), dpi=100)
6181 ax = fig.add_subplot(111)
6182 ax.plot_date(dates, values, '-')
6184 # Plot details -- ticks.
6185 years = YearLocator() # every year
6186 months = MonthLocator() # every month
6187 yearsFmt = DateFormatter('%Y')
6189 ax.xaxis.set_major_locator(years)
6190 ax.xaxis.set_major_formatter(yearsFmt)
6191 ax.xaxis.set_minor_locator(months)
6194 for label in ax.get_xticklabels() + ax.get_yticklabels():
6195 label.set_fontsize('8')
6197 # Plot details -- coords. message box.
6200 ax.fmt_xdata = DateFormatter('%Y-%m-%d')
6201 ax.fmt_ydata = price
6207 # <span background="#ffdddd">Plot data.</span>
6208 filename
= plot_date('_plot-%d.png' % new_id(),
6209 [datetime
.date(1995,1,1).toordinal(),
6210 datetime
.date(1997,1,1).toordinal()],
6214 if ("outline", "Moving average."):
6218 # A[t] = (V[t] + V[t-1] + ... + V[ t-n+1 ]) / n
6221 assert len(vec.shape) == 1, 'Expecting vector argument.'
6222 assert n > 1, 'Averaging over less than 2 elements.'
6224 assert N > n, 'Not enough samples for averaging.'
6226 ma = copy(vec) * numpy.nan
6227 for t in xrange(n - 1, N):
6228 ma[t] = 1.0 * sum(vec[t-n+1: t + 1]) / n
6231 mov_avg
= movavg(numpy
.array([1, 2, 3, 4.0]),
6234 #** Moving average via convolution
6236 data
= numpy
.array([1.0, 2,3,4])
6237 weight
= numpy
.array([1.0/2, 1.0/2])
6238 mov_avg
= numpy
.convolve(data
, weight
, mode
='same')
6239 mov_avg
[0:weight
.shape
[0] - 1] = numpy
.nan
6242 #** Moving average, simple plot
6244 matplotlib
.use("gtk")
6251 weight
= [1.0/2, 1.0/2]
6252 mov_avg
= convolve(data
, weight
, mode
='same')
6253 mov_avg
[0:len(weight
) - 1] = nan
6255 t1
= plot(mov_avg
) # t1 won't pickle.
6256 show(mainloop
=False)
6260 #** Date plot as widget
6261 if ("outline", "Date plot"):
6262 # <span background="#ffdddd">Plot details.</span>
6267 def plot_date(dates, values):
6268 from pylab import figure, show
6270 from matplotlib.dates import YearLocator, MonthLocator, DateFormatter
6271 from matplotlib.backends.backend_gtk import FigureCanvasGTK \
6274 fig = figure(figsize=(7,4), dpi=100)
6275 ax = fig.add_subplot(111)
6276 ax.plot_date(dates, values, '-')
6278 # Plot details -- ticks.
6279 years = YearLocator() # every year
6280 months = MonthLocator() # every month
6281 yearsFmt = DateFormatter('%Y')
6283 ax.xaxis.set_major_locator(years)
6284 ax.xaxis.set_major_formatter(yearsFmt)
6285 ax.xaxis.set_minor_locator(months)
6288 for label in ax.get_xticklabels() + ax.get_yticklabels():
6289 label.set_fontsize('8')
6291 # Plot details -- coords. message box.
6294 ax.fmt_xdata = DateFormatter('%Y-%m-%d')
6295 ax.fmt_ydata = price
6299 # <span background="#ffdddd">Plot data.</span>
6300 plot_date([datetime
.date(1995,1,1).toordinal(),
6301 datetime
.date(1997,1,1).toordinal()],
6304 # The display widget is FigureCanvas(fig). But canvas operations will
6305 # destroy this occasionally so the raw figure handle is returned and
6306 # L3 can display Native( fig : <matplotlib.figure.Figure> )
6307 # via Widget(FigureCanvas(fig)).
6312 exec(str, globals())
6314 inline("import matplotlib as M")
6315 inline( "import pylab as P")
6317 fig
= P
.figure(figsize
=(5,4), dpi
=75)
6320 #** doc strings as help entries
6321 from types
import InstanceType
, MethodType
6323 if isinstance(fig
, InstanceType
):
6324 helpfor
= fig
.__class
__
6325 # All member functions of this class.
6326 for (name
, memb
) in pydoc
.allmethods(helpfor
).iteritems():
6327 _docstr
= pydoc
.getdoc(memb
)
6328 head
, body
= pydoc
.splitdoc(_docstr
)
6329 print "\f=================================\n", name
6331 print "--------------------------------------------\n", body
6342 # # Help on instance of Figure in module matplotlib.figure object:
6343 # # class instance(object)
6345 # # Good, but a LOT of content:
6346 help( fig
.__class
__)
6350 #* numpy arrays, python objects, l3 canvas grid
6351 # Table layout is very useful and implicit when displaying a list of tuples.
6352 # When editing a table, the underlying structure should BE a table to enforce
6353 # dimensions (no shape change when items are removed) and allow row / column
6356 arr
= N
.array([['desc', 'val'],
6358 ['house', 2.2]], 'O')
6362 print arr
[1, None] # 'car' row
6364 arr_1
= N
.concatenate(
6365 (arr
[0:2, 0:None], # Indexing stupidity to get rows 0 & 1
6374 print arr
[0:None, 1] # 'val' column
6376 arr_1
= N
.concatenate(
6378 arr
[0:None, 0:1], # Indexing stupidity to get rows 0 & 1
6387 #* matplotlib figures on l3 canvas (XY Plot script)
6390 #** Another numpy stupidity
6391 import matplotlib
as M
6395 # Gives TypeError: only length-1 arrays can be converted to Python scalars
6396 xx
= sin(2* nn
* pi
/N
)
6398 xx
= P
.sin(2* nn
* pi
/N
)
6401 #** l3 polygon plots
6402 if ("outline", "XY Polygon Plot"):
6403 # <span background="#ffcccc">Plot details.</span>
6405 def plot_(fname, x, y):
6406 import matplotlib as M
6410 fig = P.figure(figsize=(5,4), dpi=75)
6411 ax = fig.add_subplot(111)
6412 ax.plot(x, y, '-', lw=2)
6416 ax.set_title('Polygon plot')
6422 inline
'from pylab import *'
6423 for N
in range(3, 12):
6424 nn
= arange(0, N
+ 1)
6425 xx
= sin(2* nn
* pi
/N
)
6426 yy
= cos(2* nn
* pi
/N
)
6427 plot_(new_name('poly', '.png'), xx
, yy
)
6433 import matplotlib
as M
6436 def plot_xy(fname
, x
, y
):
6438 plot
= P
.plot(x
, y
, '-', lw
=2)
6440 # P.xlabel('time (s)')
6441 # P.ylabel('voltage (mV)')
6442 # P.title('About as simple as it gets, folks')
6447 plot_xy('simple_plot.png',
6448 P
.arange(0.0, 1.0+0.01, 0.01),
6453 st
= cPickle
.dumps(plot
, protocol
=2)
6454 # UnpickleableError: Cannot pickle <type 'Affine'> objects
6457 #** l3 calling python
6459 def plot_(fname, x, y):
6460 import matplotlib as M
6464 plot = P.plot(x, y, '-', lw=2)
6466 # P.xlabel('time (s)')
6467 # P.ylabel('voltage (mV)')
6468 # P.title('About as simple as it gets, folks')
6473 inline
'from math import pi; import matplotlib as M; import pylab as P'
6474 t
= P
.arange(0.0, 1.0+0.01, 0.01)
6475 s
= P
.cos(2 * pi
* t
)
6476 plot_('simple_plot.png', s
, t
)
6479 # Python image read (load) test
6480 import matplotlib
.image
as mi
6482 img_arr
= mi
.imread('simple_plot.png')
6486 def plot_(fname
, x
, y
):
6487 inline('import matplotlib as M; import pylab as P')
6489 plot
= P
.plot(x
, y
, '-', lw
=2)
6491 # P.xlabel('time (s)')
6492 # P.ylabel('voltage (mV)')
6493 # P.title('About as simple as it gets, folks')
6497 inline
'from math import pi; import matplotlib as M; import pylab as P'
6498 t
= P
.arange(0.0, 1.0+0.01, 0.01)
6499 s
= P
.cos(2 * pi
* t
)
6500 plot_('simple_plot.png', s
, t
)
6504 #** l3 only, with axis
6505 # <span background="#ffcccc">Plot details.</span>
6506 def plot_(fname
, x
, y
):
6507 inline('import matplotlib as M; import pylab as P')
6510 fig
= P
.figure(figsize
=(5,4), dpi
=75)
6511 ax
= fig
.add_subplot(111)
6512 ax
.plot(x
, y
, '-', lw
=2)
6514 ax
.set_xlabel('time (s)')
6515 ax
.set_ylabel('voltage (mV)')
6516 ax
.set_title('About as simple as it gets, folks')
6521 inline
'from math import pi; import pylab as P'
6522 # <span background="#ffdddd">Plot data.</span>
6523 x
= P
.arange(0.0, 1.0+0.01, 0.01)
6524 y
= P
.cos(2 * pi
* x
)
6525 # <span background="#ffdddd">Plot file.</span>
6526 filename
= plot_('_plot-%d.png' % new_id(), x
, y
)
6530 #** l3 with explicit figure in subplot
6531 # Plot details can be adjusted here.
6533 def plot_(fname, x, y):
6534 import matplotlib as M
6538 fig = P.figure(figsize=(5,4), dpi=75)
6539 ax = fig.add_subplot(111)
6540 ax.plot(x, y, '-', lw=2)
6542 ax.set_xlabel('time (s)')
6543 ax.set_ylabel('voltage (mV)')
6544 ax.set_title('About as simple as it gets, folks')
6549 inline
'from math import pi; import matplotlib as M; import pylab as P'
6550 x
= t
= P
.arange(0.0, 1.0+0.01, 0.01)
6551 y
= s
= P
.cos(2 * pi
* t
)
6552 # Insert plot data here.
6553 plot_('simple_plot.png', s
, t
)
6557 #** Plot without intermediate data file
6558 # from .../matplotlib-0.90.1/examples/to_numeric.py
6561 #** Interactive matplotlib window combined with l3gui
6562 import matplotlib
as M
6565 from matplotlib
.axes
import Subplot
6566 from matplotlib
.figure
import Figure
6567 from matplotlib
.numerix
import arange
, sin
, pi
6571 from matplotlib
.backends
.backend_gtkagg
import \
6572 FigureCanvasGTKAgg
as FigureCanvas
6573 from matplotlib
.backends
.backend_gtkagg
import \
6574 NavigationToolbar2GTKAgg
as NavigationToolbar
6576 from matplotlib
.backends
.backend_gtk
import \
6577 FigureCanvasGTK
as FigureCanvas
6579 from matplotlib
.backends
.backend_gtk
import \
6580 NavigationToolbar2GTK
as NavigationToolbar
6583 fig
= Figure(figsize
=(5,4), dpi
=100)
6584 ax
= fig
.add_subplot(111)
6585 t
= arange(0.0,3.0,0.01)
6588 ax
.set_xlabel('time (s)')
6589 ax
.set_ylabel('voltage (mV)')
6590 ax
.set_title('About as simple as it gets, folks')
6595 def plot_win(figure
):
6597 # win.connect("destroy", lambda x: gtk.main_quit())
6598 win
.set_default_size(400,300)
6599 win
.set_title("Interactive plot")
6604 canvas
= FigureCanvas(figure
)
6605 toolbar
= NavigationToolbar(canvas
, win
)
6606 vbox
.pack_start(canvas
)
6607 vbox
.pack_start(toolbar
, False, False)
6613 # Everything is done through the figure and axis (good), but these
6615 (axis
, figure
) = plot_figure()
6616 figure
.savefig('testimage.png') # Also available via plot window.
6619 gtk
.main() # test only.
6623 xx
= P
.arange(0.0, 1.0+0.01, 0.01)
6624 yy
= P
.cos(2*2*pi
*xx
)
6625 fname
= 'simple_plot.png'
6628 plot
= P
.plot(xx
, yy
, '-', lw
=2)
6629 P
.xlabel('time (s)')
6630 P
.ylabel('voltage (mV)')
6631 P
.title('About as simple as it gets, folks')
6638 #** simplest expansion
6639 from l3lang
.cruft
import em
6642 '@[def emph(str)]<span background="#ffcccc"> @str </span>@[end def]', glo
)
6643 print em
.expand("@emph('the comment')", glo
)
6645 #** Single em interpreter, faster.
6646 from l3lang
.cruft
import em
6647 em_inter
= em
.Interpreter()
6650 '@[def emph(str)]<span background="#ffcccc"> @str </span>@[end def]'
6651 '@[def strong(str)]<b> @str </b>@[end def]'
6654 print em_inter
.expand(str, em_glo
)
6656 #*** Expand one time:
6657 emx("@emph('the comment')")
6658 emx("@strong('bold')")
6659 emx("""@emph('the @strong("bold") comment')""")
6662 #*** Nested expansion:
6663 emx("""@emph{the @strong("bold") comment}""")
6665 #*** Ordering in nested expansions.
6666 # In this, em_l inside def gives
6667 # NameError: global name 'em_l' is not defined
6674 <span background="EVEN">
6678 <span background="ODD">
6687 emx("No emphasis @emph{some emphasis @emph{nested emphasis} some emphasis}")
6690 #*** Ordering in nested expansions, 2
6691 # The following cannot work; the em_l state has to be decreased at some
6692 # future time after the macro is expanded .
6695 def __call__(self
, stri
):
6696 self
.__class
__.em_l
+= 1
6698 if self
.__class
__.em_l
% 2:
6700 <span background="EVEN">
6706 <span background="ODD">
6710 self
.__class
__.em_l
-= 1
6716 #*** Ordering in nested expansions, 3
6717 # The nested expansion order is inside-out (which is wrong).
6721 emph_level = empy.getGlobals().get('emph_level', -1) + 1
6722 empy.getGlobals()['emph_level'] = emph_level
6723 if (emph_level % 2) == 0:
6724 rv = empy.expand('<span background="EVEN"> @stri </span>', locals())
6726 rv = empy.expand('<span background="ODD"> @stri </span>', locals())
6727 empy.getGlobals()['emph_level'] = emph_level - 1
6732 emx("@emph('hello @emph(this) world')")
6733 emx("@emph{hello @emph{this} world}")
6736 #*** Ordering in nested expansions, 4
6741 emph_level = empy.getGlobals().get('emph_level', -1) + 1
6742 empy.getGlobals()['emph_level'] = emph_level
6744 exp = lambda strng: empy.expand(strng, locals())
6745 if (emph_level % 2) == 0:
6748 '<span background="EVEN"> @stri </span>',
6754 '<span background="ODD"> @stri </span>',
6757 empy.getGlobals()['emph_level'] = emph_level - 1
6763 # Gives NameError: name 'emph' is not defined
6764 emx("@emph('hello @emph(this) world')")
6765 emx("@emph{hello @emph{this} world}")
6769 #* User-guiding emphasis
6770 #** highlight, raw pango markup
6771 # <span background="#ffcccc">Plot details can be adjusted here.</span>
6773 def plot_(fname, x, y):
6774 import matplotlib as M
6778 fig = P.figure(figsize=(5,4), dpi=75)
6779 ax = fig.add_subplot(111)
6780 ax.plot(x, y, '-', lw=2)
6782 ax.set_xlabel('time (s)')
6783 ax.set_ylabel('voltage (mV)')
6784 ax.set_title('About as simple as it gets, folks')
6790 inline
'from math import pi; import matplotlib as M; import pylab as P'
6791 x
= t
= P
.arange(0.0, 1.0+0.01, 0.01)
6792 y
= s
= P
.cos(2 * pi
* t
)
6793 # <span background="#ffdddd">Insert plot data here.</span>
6795 # <span background="#ffdddd">Output file name.</span>
6797 # <span background="#ffdddd">Your X.</span>
6799 # <span background="#ffdddd">Your Y.</span>
6802 #** highlight, m4 syntax.
6803 # guide(Insert plot data here.)
6805 # guide(Output file name.)
6812 #** highlight, per-line prefix syntax.
6813 #g* Insert plot data here.
6815 #g* Output file name.
6822 #** highlight, long syntax w/ line separation
6823 # This leaves blank lines.
6824 #<span background="#ffdddd">
6825 # Insert plot data here.
6828 # <span background="#ffdddd">Output file name.</span>
6830 # <span background="#ffdddd">Your X.</span>
6832 # <span background="#ffdddd">Your Y.</span>
6838 #** Greyscale other text.
6839 # From any root node:
6840 from l3gui
.widgets
import *
6841 for nd
in self
.iter_visibles():
6842 if isinstance(nd
, l3Rawtext
):
6843 nd
._ltext
.set_property("fill-color", "#" + "d0" * 3)
6845 #** Collect user guiding info from comments.
6846 # Starting from 'eval locally',
6848 # <l3gui.widgets.l3Program instance at 0xb780a70c>
6849 #*** Find all comments.
6850 dt
= self
.w_
.deco_table_
6851 for nd
in self
._l3tree
.top_down():
6852 print nd
, dt
.get(nd
._id
, None)
6854 #*** Traverse comments.
6855 def iter_comments(self
):
6856 dt
= self
.w_
.deco_table_
6857 for nd
in self
._l3tree
.top_down():
6858 comm
= dt
.get(nd
._id
, None)
6863 #*** Find comments with highlighting.
6864 for comm
in iter_comments(self
):
6865 if (comm
.find("<span background") > 0):
6869 #*** Accumulate comments.
6870 print [ comm
for comm
in iter_comments(self
)
6871 if (comm
.find("<span background") > 0)]
6872 print [ comm
.py_string()
6873 for comm
in iter_comments(self
)
6874 if (comm
.find("<span background") > 0)]
6875 print '\n'.join([ comm
.py_string()
6876 for comm
in iter_comments(self
)
6877 if (comm
.find("<span background") > 0)])
6880 #*** Add accumulation to this heading's comment.
6881 # This changes the associated comment and adds state handling details.
6882 # Viewing internal information can be a transient action (use popups)
6883 # or a special view (independent display alongside).
6885 # Use deco_change_label
6886 dt
= self
.w_
.deco_table_
6887 comm
= dt
.get(self
._l3tree
._id
, None)
6890 prepend
= comm
.py_string()
6891 self
._old
_label
= prepend
6894 print (prepend
+ '\n' +
6895 '\n'.join([ comm
.py_string()
6896 for comm
in iter_comments(self
)
6897 if (comm
.find("<span background") > 0)]))
6899 self
.deco_change_label(label
=
6901 '\n'.join([ comm
.py_string()
6902 for comm
in iter_comments(self
)
6903 if (comm
.find("<span background") > 0)])))
6906 #*** Display accumulation as separate node
6907 # Loosely connected to independent
6908 TODO
: add
as menu entry
6909 done
: view_as
-> full member
6910 self
.view_as('flat')
6915 #* matplotlib and tools
6916 from numerix
import absolute
, arange
, array
, asarray
, ones
, divide
,\
6917 transpose
, log
, log10
, Float
, Float32
, ravel
, zeros
,\
6918 Int16
, Int32
, Int
, Float64
, ceil
, indices
, \
6919 shape
, which
, where
, sqrt
, asum
, compress
, maximum
, minimum
, \
6920 typecode
, concatenate
, newaxis
, reshape
, resize
, repeat
, \
6921 cross_correlate
, nonzero
6923 from matplotlib
.numerix
import absolute
, arange
, array
, asarray
, ones
, divide
,\
6924 transpose
, log
, log10
, Float
, Float32
, ravel
, zeros
,\
6925 Int16
, Int32
, Int
, Float64
, ceil
, indices
, \
6926 shape
, which
, where
, sqrt
, asum
, compress
, maximum
, minimum
, \
6927 typecode
, concatenate
, newaxis
, reshape
, resize
, repeat
, \
6928 cross_correlate
, nonzero
6930 inline
'import MLab as M; from matplotlib.numerix import transpose'
6935 # run from l3, gets stuck in
6936 # #0 dlamc3_ (a=0xb7a10030, b=0xb7a0ffe0) at Src/dlamch.c:685
6937 # when run from l3 AND when run in l3's python:
6938 # l3 -> python -> run -> wait.
6941 #* screenshots via gtk
6942 # For capturing images of selected items it's much better to use known
6943 # coordinates and directly produce a file than using the capture ->
6944 # edit -> convert -> save sequence via the gimp or others.
6946 view
= self
.w_
.canvas
.view
6947 selection
= self
.w_
.selector
.get_selection()
6949 (sl
, st
, sr
, sb
) = selection
.get_bounds()
6951 # Get selection corners.
6952 (wl
, wt
) = map(int, view
.world_to_window(sl
, st
))
6953 (wr
, wb
) = map(int, view
.world_to_window(sr
, sb
))
6955 # Compute selection dimensions.
6960 pixbuf
= gtk
.gdk
.Pixbuf(gtk
.gdk
.COLORSPACE_RGB
,
6962 8, # bits_per_sample
6966 # get_from_drawable(src, cmap, src_x, src_y, dest_x, dest_y, width, height)
6967 status
= pixbuf
.get_from_drawable(
6968 view
.get_parent_window(), # gtk.gdk.Window(gtk.gdk.Drawable)
6969 gtk
.gdk
.colormap_get_system(),
6976 print "capture failed"
6978 pixbuf
.save("/tmp/foo.png", "png")
6980 #* system state work :l3:memory_use:
6982 for (mname
, memb
) in self
.__dict
__.iteritems():
6983 print "%-20s %40s" % (mname
, memb
)
6985 # Check completeness.
6986 set([1,2]) - set([1])
6987 set(self
.__dict
__.keys()) - set(dir(self
)) # Empty, good.
6990 #** Print an ordered table of member DATA.
6996 for mname
in sort(self
.__dict
__.keys()):
6997 memb
= self
.__dict
__[mname
]
6998 print " %-20s %-40s" % (mname
, memb
)
7001 #** Print an ordered table of member DATA.
7003 print "%s.%s instance at %x" % (datum
.__module
__,
7004 datum
.__class
__.__name
__,
7007 for mname
in sort(datum
.__dict
__.keys()):
7008 memb
= datum
.__dict
__[mname
]
7009 print " %-20s %-40.39s" % (mname
, memb
)
7012 l3lang.ast.Program instance at -488123d4
7014 _block_env Env-30005-anonymous
7020 #** ordered table of member data 2
7022 # first type occurence listed as subtree
7024 def pot2(datum
, line
= 1, handled
= set()):
7025 # Filter built-in and already printed types
7026 if not (type(datum
) == InstanceType
):
7029 classname
= "%s.%s" % (datum
.__module
__, datum
.__class
__.__name
__)
7030 if classname
in handled
:
7032 handled
.add(classname
)
7035 print "%s.%s instance at %x" % (datum
.__module
__,
7036 datum
.__class
__.__name
__,
7038 for mname
in sort(datum
.__dict
__.keys()):
7039 memb
= datum
.__dict
__[mname
]
7040 print "%4d %-20s %-40.39s" % (line
, mname
, memb
)
7041 line
= pot2(memb
, line
= line
+ 1, handled
= handled
)
7043 pot2(self
._l3tree
, line
= 1, handled
= set())
7045 l3lang.ast.Program instance at -488123d4
7047 2 _block_env Env-30005-anonymous
7048 l3lang.ast.Env instance at -488123d4
7049 3 _bindings {('active', 'df'): None, ('pixel', 'df'
7050 4 _bindings_ids {('active', 'df'): 30509, ('pixel', 'df
7051 5 _bindings_memory {}
7052 6 _children [Env(30536, Env-30005-anonymous, [Progr
7058 #** print ordered table of member data 3
7060 # first type occurence listed as subtree,
7061 # indentation for levels,
7062 # all later occurrences of a type refer to line number
7067 def pot3(datum
, line
= 1, handled
= {}, indent
= 1):
7068 # Filter built-in and already printed types
7069 if not (type(datum
) == InstanceType
):
7072 space
= " " * indent
7073 classname
= "%s.%s" % (datum
.__module
__, datum
.__class
__.__name
__)
7074 if handled
.has_key(classname
):
7075 print "%4d%s%s.%s instance -- see line %d" % (line
,
7078 datum
.__class
__.__name
__,
7082 handled
[classname
] = line
7085 print "%4d%s%s.%s instance at %x" % (line
,
7088 datum
.__class
__.__name
__,
7091 for mname
in sort(datum
.__dict
__.keys()):
7092 memb
= datum
.__dict
__[mname
]
7093 print "%4d%s%-20s %-40.39s" % (line
, space
, mname
, memb
)
7094 line
= pot3(memb
, line
= line
+ 1, handled
= handled
,
7095 indent
= indent
+ 1)
7098 pot3(self
, line
= 1, handled
= {}, indent
= 1)
7100 pot3(self
._l3tree
, line
= 1, handled
= {}, indent
= 1)
7104 #** print ordered table of member data 4
7106 # first type occurence listed as subtree,
7107 # indentation for levels,
7108 # repeated instances of a fixed-member type refer to first's line number,
7109 # distinct instances of container types (Shared) produce new listing.
7116 truncate_lines
= "%-120.119s"
7117 truncate_lines
= "%-80.79s"
7118 def pot4(datum
, line
= 1, handled
= {}, indent
= 1):
7119 # Filter built-in types.
7120 # inheriting from ListType has subtle problems.
7121 if isinstance(datum
, l3lang
.ast
.aList
):
7124 if not (type(datum
) == InstanceType
):
7127 space
= " " * indent
7128 classname
= "%s.%s" % (datum
.__module
__, datum
.__class
__.__name
__)
7130 # Only cross-reference already visited data.
7131 if classname
not in ['l3lang.utils.Shared']:
7132 if handled
.has_key(classname
):
7133 print "%4d%s%s.%s instance -- see line %d" % (line
,
7136 datum
.__class
__.__name
__,
7140 handled
[classname
] = line
7143 if handled
.has_key(id(datum
)):
7144 print "%4d%s%s.%s visited instance -- see line %d" % (
7148 datum
.__class
__.__name
__,
7153 handled
[id(datum
)] = line
7155 # Print info for this datum.
7157 # Only show headers for objects not already described.
7158 print "%4d%s%s.%s instance at 0x%x" % (line
,
7161 datum
.__class
__.__name
__,
7162 (id(datum
) & 0xFFFFFFFFL
))
7165 # Visit datum's children.
7166 for mname
in sort(datum
.__dict
__.keys()):
7167 memb
= datum
.__dict
__[mname
]
7168 print truncate_lines
% ("%4d%s%-20s %-60s" % (line
, space
, mname
, memb
))
7169 line
= pot4(memb
, line
= line
+ 1, handled
= handled
,
7170 indent
= indent
+ 1)
7172 # self is noise_add.l3
7173 pot4(self
, line
= 1, handled
= {}, indent
= 1)
7175 # 325 remember_y 60.25
7176 # 326 w_ <l3lang.utils.Shared instance at 0xb777512c>
7177 # 327 l3lang.utils.Shared visited instance -- see line 21
7179 # 1352 lines without back-reference to already-visited Shareds, e.g.
7181 # 1146 w_ <l3lang.utils.Shared instance at 0xb777512c>
7183 # 1148 _non_pickling_keys []
7187 Include traversal of lists, dicts to cover this:
7189 This is missing _l3tree contents because
7190 type(self._alist.__dict__['_l3tree'])
7191 <class 'l3lang.ast.aList'>
7193 But this is not true; type returns
7194 >>> type(self._l3tree[0])
7195 <class 'l3lang.ast.aList'>
7196 while the instance members are all present:
7197 >>> dir(self._l3tree[0])
7198 ... '_first_char', '_id', '_last_char', '_parent',...
7200 >>> isinstance(self._l3tree[0], l3lang.ast.aList)
7203 Other NESTED types inheriting from python built-ins:
7204 class aList(ListType):
7205 class vaList(aList):
7212 #** print ordered table of member data 5
7214 # - first type occurence listed as subtree,
7215 # - indentation for levels,
7216 # - repeated instances of a fixed-member type refer to first's line number,
7217 # - distinct instances of container types (Shared) produce new listing.
7218 # - make line numbers org-indexable, via [[line]] and <<line>>
7221 # - including indentation level for text reading, didn't help
7228 truncate_lines
= "%-120.119s"
7229 truncate_lines
= "%-80.79s"
7230 def pot5(datum
, line
= 1, handled
= {}, indent
= 1):
7232 This produces a reasonable state dump of the l3 widget provided as
7233 `datum`. The contained l3 program is included but not traversed
7234 recursively, so associated data and clones are completely ignored.
7236 # Filter built-in types.
7237 # inheriting from ListType has subtle problems.
7238 if isinstance(datum
, l3lang
.ast
.aList
):
7241 if not (type(datum
) == InstanceType
):
7244 space
= " " * indent
7245 classname
= "%s.%s" % (datum
.__module
__, datum
.__class
__.__name
__)
7247 # Only cross-reference already visited data.
7248 if classname
not in ['l3lang.utils.Shared']:
7249 if handled
.has_key(classname
):
7250 print "<<%03d>>%s%s.%s instance -- see line [[%03d]]" % (
7254 datum
.__class
__.__name
__,
7258 handled
[classname
] = line
7261 if handled
.has_key(id(datum
)):
7262 print "<<%03d>>%s%s.%s visited instance -- see line [[%03d]]" % (
7266 datum
.__class
__.__name
__,
7271 handled
[id(datum
)] = line
7273 # Print info for this datum.
7275 # Only show headers for objects not already described.
7276 print "<<%03d>>%s%s.%s instance at 0x%x" % (
7280 datum
.__class
__.__name
__,
7281 (id(datum
) & 0xFFFFFFFFL
))
7284 # Visit datum's children.
7285 for mname
in sort(datum
.__dict
__.keys()):
7286 memb
= datum
.__dict
__[mname
]
7287 print truncate_lines
% ("<<%03d>>%s%-20s %-60s" % (line
, space
, mname
, memb
))
7288 line
= pot5(memb
, line
= line
+ 1, handled
= handled
,
7289 indent
= indent
+ 1)
7291 # self is noise_add.l3
7292 pot5(self
, line
= 1, handled
= {}, indent
= 1)
7295 #** print ordered table of member data, org version 1
7296 # - use hex id as address, via org
7297 # - first type occurence listed as subtree,
7298 # - indentation for levels,
7299 # - repeated instances of a fixed-member type refer to first's id
7300 # - distinct instances of container types (Shared) produce new listing.
7302 # - include emacs outline decoration (org-mode)
7305 import l3lang
.ast
as A
7312 return (id(datum
) & 0xFFFFFFFFL
)
7315 #*** special printers
7316 from EMAN2
import EMData
7317 from numpy
import ndarray
7319 accum_types
= (EMData
, ndarray
)
7321 special_printers
= {}
7322 accumulate_interp_res
= {} # Accumulate stuff as side-effect.
7323 limit_output_lines
= 100
7326 #**** Accumulate by EXCLUDING types.
7327 def _special(datum
, line
= 1, handled
= {}, indent
= 1):
7328 '''Print additional information for RamMem._attr_tables.'''
7330 space
= " " * indent
7331 for (id_
, dic1
) in datum
.iteritems():
7333 # mem_str = max_str_len % str(dic1)
7335 if (line
- first_line
) < limit_output_lines
:
7336 print truncate_lines
% (
7344 for key
in sort(dic1
.keys()):
7345 # Side effect: accumulation
7346 if key
== 'interp_result':
7348 # These checks are superficial; real code must use
7349 # pickle w/o memo dict for verification.
7350 if not isinstance(se_val
, (A
.astType
, A
.aNone
, A
.aList
)):
7351 if (isinstance(se_val
, ListType
) and
7353 isinstance(se_val
[0], (A
.astType
, A
.aNone
, A
.aList
))):
7356 accumulate_interp_res
[id_
] = dic1
[key
]
7357 # Print SOME output.
7358 if (line
- first_line
) < limit_output_lines
:
7359 mem_str
= max_str_len
% str(dic1
[key
])
7360 print truncate_lines
% (
7363 space
[0: -(indent
+ 1)] + " ",
7368 special_printers
[('RamMem', '_attr_tables')] = _special
7371 #**** Accumulate by INCLUDING types.
7372 def _special(datum
, line
= 1, handled
= {}, indent
= 1):
7373 '''Print additional information for RamMem._attr_tables.'''
7375 space
= " " * indent
7376 for (id_
, dic1
) in datum
.iteritems():
7378 # mem_str = max_str_len % str(dic1)
7380 if (line
- first_line
) < limit_output_lines
:
7381 print truncate_lines
% (
7389 for key
in sort(dic1
.keys()):
7390 # Side effect: accumulation
7391 if key
== 'interp_result':
7393 if isinstance(se_val
, accum_types
):
7394 accumulate_interp_res
[id_
] = se_val
7396 # Print SOME output.
7397 if (line
- first_line
) < limit_output_lines
:
7398 mem_str
= max_str_len
% str(dic1
[key
])
7399 print truncate_lines
% (
7402 space
[0: -(indent
+ 1)] + " ",
7407 special_printers
[('RamMem', '_attr_tables')] = _special
7410 #**** Accumulate by INCLUDING types and lists/tuples of types
7411 def _special(datum
, line
= 1, handled
= {}, indent
= 1):
7412 '''Print additional information for RamMem._attr_tables.'''
7414 space
= " " * indent
7415 for (id_
, dic1
) in datum
.iteritems():
7417 # mem_str = max_str_len % str(dic1)
7419 if (line
- first_line
) < limit_output_lines
:
7420 print truncate_lines
% (
7428 for key
in sort(dic1
.keys()):
7429 # Side effect: accumulation
7430 if key
== 'interp_result':
7432 if isinstance(se_val
, accum_types
):
7433 accumulate_interp_res
[id_
] = se_val
7434 if isinstance(se_val
, (ListType
, TupleType
)) and \
7435 (len(se_val
) > 0) and \
7437 [ ob
for ob
in se_val
7438 if isinstance(ob
, accum_types
)]) > 0):
7439 accumulate_interp_res
[id_
] = se_val
7442 # Print SOME output.
7443 if ((line
- first_line
) < limit_output_lines
):
7444 mem_str
= max_str_len
% str(dic1
[key
])
7445 print truncate_lines
% (
7448 space
[0: -(indent
+ 1)] + " ",
7453 special_printers
[('RamMem', '_attr_tables')] = _special
7457 truncate_lines
= "%-80.79s"
7458 max_str_len
= "%-60.59s"
7460 truncate_lines
= "%-160.159s"
7461 max_str_len
= "%-.90s"
7462 from time
import time
7463 def poto1(datum
, line
= 1, handled
= {}, indent
= 1):
7464 # Filter built-in types.
7465 # inheriting from ListType has subtle problems.
7466 if isinstance(datum
, l3lang
.ast
.aList
):
7469 if not (type(datum
) == InstanceType
):
7472 space
= " " * indent
7473 classname
= "%s.%s" % (datum
.__module
__, datum
.__class
__.__name
__)
7475 # Only cross-reference already visited data.
7476 if classname
not in ['l3lang.utils.Shared']:
7477 if handled
.has_key(classname
):
7478 print "%s%s.%s instance -- see id [[0x%x]]" % (
7481 datum
.__class
__.__name
__,
7485 handled
[classname
] = hexid(datum
)
7488 if handled
.has_key(id(datum
)):
7489 print "%s%s.%s visited instance -- see id [[0x%x]]" % (
7492 datum
.__class
__.__name
__,
7497 handled
[id(datum
)] = hexid(datum
)
7499 # Print info for this datum.
7501 print "%s%s%s.%s instance at <<0x%x>>" % (
7505 datum
.__class
__.__name
__,
7509 # Visit datum's children.
7510 for mname
in sort(datum
.__dict
__.keys()):
7511 memb
= datum
.__dict
__[mname
]
7512 # Include timing information with lines.
7515 # For instances, produce org-mode <<label>>
7516 # mem_str = max_str_len % str(memb)
7517 mem_str
= max_str_len
% str(memb
)
7518 if "instance at 0x" in mem_str
:
7519 mem_str
= "%s at <<0x%x>>" % (mem_str
[0:-15], hexid(memb
))
7520 elif isinstance(memb
, (InstanceType
, ClassType
, ListType
, DictType
)):
7521 mem_str
= "%s at <<0x%x>>" % (mem_str
, hexid(memb
))
7522 print truncate_lines
% (
7523 "%s%s%-20s %s [%.4fs]" % (
7529 # Default printout for child.
7530 line
= poto1(memb
, line
= line
+ 1, handled
= handled
,
7531 indent
= indent
+ 1)
7532 # Additional custom printout.
7534 line
= special_printers
[(datum
.__class
__.__name
__,
7535 mname
)](memb
, line
= line
+ 1,
7537 indent
= indent
+ 1)
7542 #** Get internals dump
7546 # use 'evaluate locally` on datum
7550 poto1(self
, line
= 1, handled
= {}, indent
= 1)
7552 poto1(w_
.state_
.storage
, line
= 1, handled
= {}, indent
= 1)
7555 #** Compute disk / ram ratio
7558 # Sd = disk state (from accumulate_interp_res)
7559 # St = total state (from save_state)
7560 # S0 = initial state (overhead, from save_state at time 0)
7566 # St(t) = Sr(t) + Sd(t) + S0
7568 # Using known values,
7571 # R = (St - Sd - S0) / Sd
7572 # should (approximately) hold.
7576 # save state to st.t0
7581 poto1(w_
.state_
.storage
, line
= 1, handled
= {}, indent
= 1)
7582 print "items collected: ", len(accumulate_interp_res
)
7584 from l3lang
import utils
7585 utils
.file_cPickle(get_current_state(w_
), "st.total")
7586 utils
.file_cPickle(accumulate_interp_res
, "st.disk")
7588 S0
= os
.path
.getsize("st.t0")
7589 St
= os
.path
.getsize("st.total")
7590 Sd
= os
.path
.getsize("st.disk")
7592 print "ram / disk ratio", 1.0 * (St
- Sd
- S0
) / Sd
7595 #** view interp_result
7596 for (id_
, val
) in accumulate_interp_res
.iteritems():
7597 print "%10d %-.50s" % (id_
, val
)
7599 # What is accumulate_interp_res[33122]?
7600 type(accumulate_interp_res
[33122])
7601 print w_
.state_
.storage
.get_attribute_names(33122)
7602 print w_
.state_
.storage
.get_attribute(33122, 'interp_env')
7608 from cStringIO
import StringIO
7610 from StringIO
import StringIO
7612 pckl_file
= StringIO()
7613 pcklr
= pickle
.Pickler(pckl_file
, protocol
= 2)
7615 pcklr
.dump(accumulate_interp_res
)
7616 print "pickle size", pckl_file
.tell()
7618 # memo[id(obj)] = memo_len, obj
7619 # too many values with no back-link information to identify source.
7620 for (id_
, (index
, obj
)) in pcklr
.memo
.iteritems():
7621 print "%-11x %-6d %-.50s" % (id_
, index
, obj
)
7625 #* Custom pickle with printout.
7626 # Collecting through storage is unreliable; modify pickle itself.
7629 from EMAN2
import EMData
7630 from numpy
import ndarray
7633 from cStringIO
import StringIO
7635 from StringIO
import StringIO
7637 accum_types
= (EMData
, ndarray
)
7638 pckl_file
= StringIO()
7639 g_display_size
= 10000
7640 truncate_lines
= "%-60.59s"
7643 #** pickle modifications.
7645 from copy_reg
import dispatch_table
7646 from copy_reg
import _extension_registry
, _inverted_registry
, _extension_cache
7651 def _real_save(self
, obj
):
7653 me_start
= pckl_file
.tell()
7655 obj_size
= pckl_file
.tell() - me_start
7656 if isinstance(obj
, accum_types
) or (obj_size
> g_display_size
):
7657 print "| %8d | \"%s\" | " % (obj_size
,
7658 truncate_lines
% repr(obj
))
7661 # Check for persistent id (defined by a subclass)
7662 pid
= self
.persistent_id(obj
)
7669 x
= self
.memo
.get(id(obj
))
7671 self
.write(self
.get(x
[0]))
7675 # Check the type dispatch table
7677 f
= self
.dispatch
.get(t
)
7679 f(self
, obj
) # Call unbound method with explicit self
7683 # Check for a class with a custom metaclass; treat as regular class
7685 issc
= issubclass(t
, TypeType
)
7686 except TypeError: # t is not a class (old Boost; see SF #502085)
7689 self
.save_global(obj
)
7693 # Check copy_reg.dispatch_table
7694 reduce = dispatch_table
.get(t
)
7698 # Check for a __reduce_ex__ method, fall back to __reduce__
7699 reduce = getattr(obj
, "__reduce_ex__", None)
7701 rv
= reduce(self
.proto
)
7703 reduce = getattr(obj
, "__reduce__", None)
7707 raise PicklingError("Can't pickle %r object: %r" %
7710 # Check for string returned by reduce(), meaning "save as global"
7711 if type(rv
) is StringType
:
7712 self
.save_global(obj
, rv
)
7716 # Assert that reduce() returned a tuple
7717 if type(rv
) is not TupleType
:
7718 raise PicklingError("%s must return string or tuple" % reduce)
7720 # Assert that it returned an appropriately sized tuple
7722 if not (2 <= l
<= 5):
7723 raise PicklingError("Tuple returned by %s must have "
7724 "two to five elements" % reduce)
7726 # Save the reduce() output and finally memoize the object
7727 self
.save_reduce(obj
=obj
, *rv
)
7731 def save(self
, obj
):
7732 # Produces too many empty () pairs.
7735 _real_save(self
, obj
)
7739 def save(self
, obj
):
7740 _real_save(self
, obj
)
7742 pickle
.Pickler
.save
= save
7746 accum_types
= (EMData
, ndarray
)
7747 pckl_file
= StringIO()
7748 g_display_size
= 100000
7749 truncate_lines
= "%-60.59s"
7750 pcklr
= pickle
.Pickler(pckl_file
, protocol
= 2)
7753 # First get state_.storage,
7754 pcklr
.dump(w_
.state_
.storage
)
7756 # ... then get the REST of state_
7757 pcklr
.dump(w_
.state_
)
7759 #* Custom pickle, collect info, get prefix dump
7760 # Collecting through storage is unreliable; modify pickle itself.
7763 from EMAN2
import EMData
7764 from numpy
import ndarray
7767 from cStringIO
import StringIO
7769 from StringIO
import StringIO
7771 g_accum_types
= (EMData
, ndarray
)
7772 g_pckl_file
= StringIO()
7773 g_display_size
= 10000
7774 g_truncate_lines
= "%-60.59s"
7779 #** pickle modifications.
7781 from copy_reg
import dispatch_table
7782 from copy_reg
import _extension_registry
, _inverted_registry
, _extension_cache
7787 def _real_save(self
, obj
):
7789 me_start
= g_pckl_file
.tell()
7791 obj_size
= g_pckl_file
.tell() - me_start
7792 if isinstance(obj
, g_accum_types
) or (obj_size
> g_display_size
):
7793 # Forming the full repr() takes a LOT of time; maybe
7794 # settle for the type() info...
7795 g_info_stack
.append( (obj_size
, g_truncate_lines
% repr(obj
), obj
) )
7798 # Check for persistent id (defined by a subclass)
7799 pid
= self
.persistent_id(obj
)
7806 x
= self
.memo
.get(id(obj
))
7808 self
.write(self
.get(x
[0]))
7812 # Check the type dispatch table
7814 f
= self
.dispatch
.get(t
)
7816 f(self
, obj
) # Call unbound method with explicit self
7820 # Check for a class with a custom metaclass; treat as regular class
7822 issc
= issubclass(t
, TypeType
)
7823 except TypeError: # t is not a class (old Boost; see SF #502085)
7826 self
.save_global(obj
)
7830 # Check copy_reg.dispatch_table
7831 reduce = dispatch_table
.get(t
)
7835 # Check for a __reduce_ex__ method, fall back to __reduce__
7836 reduce = getattr(obj
, "__reduce_ex__", None)
7838 rv
= reduce(self
.proto
)
7840 reduce = getattr(obj
, "__reduce__", None)
7844 raise PicklingError("Can't pickle %r object: %r" %
7847 # Check for string returned by reduce(), meaning "save as global"
7848 if type(rv
) is StringType
:
7849 self
.save_global(obj
, rv
)
7853 # Assert that reduce() returned a tuple
7854 if type(rv
) is not TupleType
:
7855 raise PicklingError("%s must return string or tuple" % reduce)
7857 # Assert that it returned an appropriately sized tuple
7859 if not (2 <= l
<= 5):
7860 raise PicklingError("Tuple returned by %s must have "
7861 "two to five elements" % reduce)
7863 # Save the reduce() output and finally memoize the object
7864 self
.save_reduce(obj
=obj
, *rv
)
7868 def save(self
, obj
):
7873 _real_save(self
, obj
)
7875 if len(g_info_stack
) > 0:
7876 _tmp
.append(g_info_stack
)
7879 pickle
.Pickler
.save
= save
7883 def dump_stack(stack
, indent
):
7884 # Unwrap outer lists
7885 while isinstance(stack
[-1], ListType
):
7887 # Get the main object head and print info.
7888 (size
, rep
, obj
) = stack
[-1]
7889 print "%s %-8d %s" % ("*" * indent
, size
, rep
)
7890 # Get head's content, if any
7891 [dump_stack(ii
, indent
+ 1) for ii
in stack
[0:-1]]
7895 g_accum_types
= (EMData
, ndarray
)
7896 g_pckl_file
= StringIO()
7898 g_display_size
= 1000
7899 g_truncate_lines
= "%-60.59s"
7901 pcklr
= pickle
.Pickler(g_pckl_file
, protocol
= 2)
7905 pcklr
.dump(w_
.state_
)
7909 dump_stack(g_info_stack
, 0)
7913 pcklr
.dump(self
._l3tree
)
7914 dump_stack(g_info_stack
, 0)
7917 import anydbm
as any
, whichdb
7919 db
= any
.open('l3db.st', 'c')
7921 print whichdb
.whichdb('l3db.st')
7923 db
= any
.open('l3db.st', 'c')
7928 #* Proxy for transparent persistence
7932 def __init__(self
, obj
):
7934 Proxy
.__init
__ = __init__
7936 def to_cache(self
, id):
7940 Proxy
.to_cache
= to_cache
7942 def __getattr__(self
, m_name
):
7943 m_unbnd
= getattr(self
._obj
.__class
__, m_name
)
7948 if isinstance(arg
, Proxy
):
7949 nargs
.append(arg
._obj
)
7952 return (m_unbnd(self
._obj
, *nargs
))
7955 Proxy
.__getattr
__ = __getattr__
7958 #** proxy numeric tests
7960 from cPickle
import dumps
7961 n1
= N
.random
.ranf( (5,4) )
7963 print n1_prox
+ n1_prox
7965 # TODO: getstate / setstate
7966 print len(dumps(n1
))
7967 print len(dumps(n1_prox
))
7968 n1_prox
.to_cache(1234)
7969 print len(dumps(n1_prox
))
7971 # Useless in-memory size guesses (spread too much)
7972 plist
= [Proxy(n1
) for ii
in xrange(10)]
7973 id(plist
[-1]) - id(plist
[-2])
7974 ppos
= [id(foo
) for foo
in plist
]
7976 print map(lambda (x
,y
): x
-y
, zip(ppos
[1:None], ppos
[0:-2]))
7979 #** proxy emdata tests
7980 from EMAN2
import EMData
7984 print "array size", arr_size
7985 e1
.set_size(*arr_size
)
7986 e1
.process_inplace('testimage.noise.uniform.rand')
7987 print sys
.getrefcount(e1
)
7989 print sys
.getrefcount(e1
)
7991 print e1_prox
+ e1_prox
7994 print len(dumps(e1
))
7995 print len(dumps(e1_prox
))
7996 e1_prox
.to_cache(1234)
7997 print len(dumps(e1_prox
))
8002 #** create test cases
8005 from cPickle
import dumps
8007 n1
= N
.random
.ranf( (5,4) )
8009 n1_weak
= weakref
.proxy(n1
)
8012 print sys
.getrefcount(n1
)
8015 # Refcount with member ref.
8017 print sys
.getrefcount(n1
)
8020 # weakref's proxy adds no new fields:
8021 set(dir(n1_weak
)) - set(dir(n1_prox
._obj
))
8023 from EMAN2
import EMData
8026 e1
.process_inplace('testimage.noise.uniform.rand')
8027 print sys
.getrefcount(e1
)
8029 e1weak
= weakref
.proxy(e1
)
8033 #*** numeric array works
8034 print n1_prox
+ n1_prox
8041 # TypeError: unsupported operand type(s) for +: 'instance' and 'instance'
8042 print e1prox
+ e1prox
8044 print operator
.add(e1prox
, e1prox
)
8047 print e1weak
+ e1weak
8048 print operator
.add(e1weak
, e1weak
)
8049 # but produces the equal? result:
8050 # <libpyEMData2.EMData object at 0x156239cc>
8051 # <libpyEMData2.EMData object at 0x156239cc>
8053 # Something is missing causing the discrepancy:
8054 print e1weak
.__add
__(e1weak
)
8055 print e1
.__add__(e1
)
8057 # <libpyEMData2.EMData object at 0x15623cdc>
8061 # <method-wrapper object at 0x1955d3cc>
8062 # <bound method EMData.__add__ of <libpyEMData2.EMData object at 0x1613da54>>
8064 print EMData
.__add
__(e1
,e1
)
8065 print EMData
.__add
__(e1prox
,e1prox
)
8066 print EMData
.__add
__(e1weak
,e1weak
)
8067 # <libpyEMData2.EMData object at 0x15623ca4>
8070 e1prox
._make
_member
_proxy
('__add__')
8072 # Now this runs (work -- who knows):
8073 print e1prox
+ e1prox
8074 print e1prox
.__add
__
8075 print e1weak
.__add
__
8076 # <bound method EMData.__add__ of <libpyEMData2.EMData object at 0x1613da54>>
8077 # <bound method EMData.__add__ of <libpyEMData2.EMData object at 0x1613da54>>
8079 # TypeError: unsupported operand type(s) for +: 'EMData' and 'instance'
8080 print 1.0 * e1prox
+ e1prox
8083 print e1weak
+ e1weak
8086 #* Replace original class with proxy wrapper
8087 # Looks like there is no need for metaclasses, as this will do
8088 # everything needed. However, this will not intercept C code creating
8089 # instances, so it is too limited in practice.
8093 eman2_emdata
= EMAN2
.EMData
8096 class EMData(Proxy
):
8097 def __init__(self
, *args
):
8098 self
._obj
= eman2_emdata(*args
)
8101 # This binding remains valid for all FUTURE references, so it must be
8102 # done before other scripts import the module.
8103 EMAN2
.EMData
= EMData
8109 e1
.process_inplace('testimage.noise.uniform.rand')
8112 # The (C) member functions directly create the new object, so the
8113 # indirection above fails.
8115 tst_add
.print_image()
8117 #* Pickling uniquely
8118 # To get accurate memory stats, avoid repeated pickling given nested
8124 from cStringIO
import StringIO
8126 from StringIO
import StringIO
8128 pckl_file
= StringIO()
8129 pcklr
= pickle
.Pickler(pckl_file
, protocol
= 2)
8132 return pckl_file
.tell()
8135 #* string deepcopy test
8136 from copy
import deepcopy
, copy
8137 orig
= 'the original string'
8139 print id(orig
) - id(cp
)
8140 # so strings 'copy' as reference.