Difference between revisions of "TrakEM2 Scripting"

(added category)
Line 2: Line 2:
  
 
Open the "Plugins - Scripting - Jython Interpreter" (see [[Scripting Help]]) and make sure there is a TrakEM2 project open, with a display open. Then type or paste the examples below.
 
Open the "Plugins - Scripting - Jython Interpreter" (see [[Scripting Help]]) and make sure there is a TrakEM2 project open, with a display open. Then type or paste the examples below.
 +
 +
= Introduction to scripting TrakEM2 =
  
 
Some basics:
 
Some basics:
Line 9: Line 11:
 
*The LayerSet contains 3D objects like [http://www.ini.uzh.ch/~acardona/api/ini/trakem2/display/AreaList.html AreaList], [http://www.ini.uzh.ch/~acardona/api/ini/trakem2/display/Pipe.html Pipe], [http://www.ini.uzh.ch/~acardona/api/ini/trakem2/display/Polyline.html Polyline], [http://www.ini.uzh.ch/~acardona/api/ini/trakem2/display/Ball.html Ball], [http://www.ini.uzh.ch/~acardona/api/ini/trakem2/display/Dissector.html Dissector], [http://www.ini.uzh.ch/~acardona/api/ini/trakem2/display/Treeline.html Treeline] and [http://www.ini.uzh.ch/~acardona/api/ini/trakem2/display/Stack.html Stack] (the latter wraps an ij.ImagePlus that contains an ij.ImageStack).
 
*The LayerSet contains 3D objects like [http://www.ini.uzh.ch/~acardona/api/ini/trakem2/display/AreaList.html AreaList], [http://www.ini.uzh.ch/~acardona/api/ini/trakem2/display/Pipe.html Pipe], [http://www.ini.uzh.ch/~acardona/api/ini/trakem2/display/Polyline.html Polyline], [http://www.ini.uzh.ch/~acardona/api/ini/trakem2/display/Ball.html Ball], [http://www.ini.uzh.ch/~acardona/api/ini/trakem2/display/Dissector.html Dissector], [http://www.ini.uzh.ch/~acardona/api/ini/trakem2/display/Treeline.html Treeline] and [http://www.ini.uzh.ch/~acardona/api/ini/trakem2/display/Stack.html Stack] (the latter wraps an ij.ImagePlus that contains an ij.ImageStack).
  
Both Layer and LayerSet and in a way containers. The LayerSet contains as well a list of Layer. The Display merely views the data in a LayerSet, one Layer at a time.
+
Both Layer and LayerSet are in a way containers. The LayerSet contains as well a list of Layer. The Display merely views the data in a LayerSet, one Layer at a time.
  
 
See a [http://www.ini.uzh.ch/~acardona/img/trakem2_datastructure_diagram.svg TrakEM2 class diagram] for a complete list.
 
See a [http://www.ini.uzh.ch/~acardona/img/trakem2_datastructure_diagram.svg TrakEM2 class diagram] for a complete list.
Line 131: Line 133:
  
 
More convenient are the methods <i>scale</i>, <i>translate</i>, <i>rotate</i> and particularly <i>preTransform</i>, for the manipulation of a [http://www.ini.uzh.ch/~acardona/api/ini/trakem2/display/Displayable.html Displayable]'s affine transform (see [http://java.sun.com/javase/6/docs/api/java/awt/geom/AffineTransform.html AffineTransform]) and that of its linked Displayables (any transform propagates to the linked ones).
 
More convenient are the methods <i>scale</i>, <i>translate</i>, <i>rotate</i> and particularly <i>preTransform</i>, for the manipulation of a [http://www.ini.uzh.ch/~acardona/api/ini/trakem2/display/Displayable.html Displayable]'s affine transform (see [http://java.sun.com/javase/6/docs/api/java/awt/geom/AffineTransform.html AffineTransform]) and that of its linked Displayables (any transform propagates to the linked ones).
 +
 +
 +
= See also =
 +
 +
*TrakEM2 [http://www.ini.uzh.ch/~acardona/howto.html how to], with java code examples.
 +
*TrakEM2 [[TrakEM2 tutorials|video tutorials]].
  
 
[[Category:Scripting]]
 
[[Category:Scripting]]

Revision as of 11:38, 29 October 2009

Examples in Jython.

Open the "Plugins - Scripting - Jython Interpreter" (see Scripting Help) and make sure there is a TrakEM2 project open, with a display open. Then type or paste the examples below.

Introduction to scripting TrakEM2

Some basics:

  • The canvas into which images are dragged and visualized is part of a Display object. The latter has methods to access its Selection, as well as the Layer and LayerSet that the Display is viewing.
  • The Layer contains 2D objects like Patch (each Patch wraps an image) and DLabel (floating text).
  • The LayerSet contains 3D objects like AreaList, Pipe, Polyline, Ball, Dissector, Treeline and Stack (the latter wraps an ij.ImagePlus that contains an ij.ImageStack).

Both Layer and LayerSet are in a way containers. The LayerSet contains as well a list of Layer. The Display merely views the data in a LayerSet, one Layer at a time.

See a TrakEM2 class diagram for a complete list.


Get the instance of a selected image

>>> p = Display.getFront().getActive()
>>> print p
090504_0314_ex0768.mrc z=0.0 #67398


Obtain the ImagePlus of a selected image

>>> p = Display.getFront().getActive()
>>> imp = p.getImagePlus()
>>> print imp.width, imp.height
2048 2048


Access the Layer and Selection of a Display

>>> front = Display.getFront()
>>> layer = front.getLayer()
>>> layer_set = front.getLayerSet()
>>> sel = front.getSelection()
>>> print sel.getSelected().size()
10
>>> print sel.isEmpty()
0

In Jython, 1 is True and 0 is False

Lock all selected objects

for d in Display.getFront().getSelected():
  d.setLocked(True)

Obtain a collection of selected images

The Selection object of a Display can return a number of collections with any selected objects in it, for example of type Patch (those that wrap an image). All you need to do is to call getSelected with the name of the class to filter for:

for d in Display.getFront().getSelected(Patch):
  print d.title


Setting and getting member objects in jython

In Jython as in Python, member objects have automatically get</> and <i>set functions.

For example, altough a Displayable has a private String title member, this is valid python code for getting and setting the title of a Displayable like a Patch:

>>> p = Display.getFront().getActive()
>>> print p.title
090504_0314_ex0768.mrc

Above, a Patch takes as title the name of the file containing the ImagePlus, by default. Let's change the title to something else:

>>> p = Display.getFront().getActive()
>>> p.title = "A new name for this Patch"
>>> print p.title
A new name for this Patch


The properties of a Displayable: title, color, visibility, locked, alpha, affine transform, dimensions and bounds

Let's set a few values:

>>> p = Display.getFront().getActive()
>>> p.title = "Test image"
>>> p.alpha = 0.4
>>> p.visible = True
>>> p.locked = False
>>> from java.awt import Color
>>> p.color = Color.blue

Let's read a few values:

>>> p.getAffineTransform()
AffineTransform[[1.0, 0.0, 474.0], [0.0, 1.0, 567.0]]
>>> print p.getBoundingBox()
java.awt.Rectangle[x=474,y=567,width=2048,height=2048]
>>> 
e)

The affine transform cannot be set, because it's a final member. But itself the value may be edited via setAffineTransform:

>>> from java.awt.geom import AffineTransform
>>> aff = AffineTransform()
>>> aff.scale(2.0, 2.0)
>>> p.setAffineTransform(aff)

Be careful: java's AffineTranform does concatenations and not pre-concatenations (order matters in matrix multiplication).

In most occasions, what you want can be accomplished with preTransform, such as translating an image:

>>> from java.awt.geom import AffineTransform
>>> aff = AffineTransform()
>>> aff.translate(300, -400)
>>> p.preTransform(aff, True)

More convenient are the methods scale, translate, rotate and particularly preTransform, for the manipulation of a Displayable's affine transform (see AffineTransform) and that of its linked Displayables (any transform propagates to the linked ones).


See also