TrakEM2 Scripting

Revision as of 12:51, 29 October 2009 by Albertcardona (talk | contribs) (Access the Layer and Selection of a Display)

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

The 'front' is the last activated display window. If there's only one display window, then that is 'front'. To access the front display, we call static function getFront() in namespace 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

The most interesting data members of a Display, as seen above, are mainly the Layer and the Selection.

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