Notice that python (and jython) lets you use object instance methods as first-class functions, and contructors as well. This enables us to rewrite the "putAreas" function in a functional way, without using any temporary variables and without any if/else logic:

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.getSelected(Patch):
print d.title

The above is a static call that retrieves the list for whichever Display window happens to be activated, in front of all others. If you have a Display instance, perform the same operation via the Display's Selection:

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).

If you change the affine transform of a Displayable directly (by calling getAffineTransform() and then manipulating it), keep in mind that you will most likely screw up the internal cached maps for fast location of the Displayable object. To solve that, be sure to call updateBucket() on the affected Displayable object.

Import images, montage them, blend them and save as .xml

What follows is a small script that imports images from a single folder, sorting out which images go to what layer (section) by matching a regular expression pattern on the file name.

Then the images are montaged layer-wise, and blended together (the borders of the overlapping images are faded out).

Notice that, for this script to work for you, you will have to edit two lines:

1. The source folder where images are to be found.
2. The pattern to match, which dictates which image goes to which layer.

Be sure as well to create as many layers as you need. If you don't know, use the getLayer method on the layerset variable, which has the ability to create a new layer when asked to get one for a Z for which a layer doesn't exist yet.

Manipulating Displayable objects

Resetting the affine transform of all images in a Layer

Suppose you open the project and find that the images of a Layer have non-rigid affine transforms, and you'd like to remove the non-rigid part. A reasonable approach is to reset their affine transforms to identity, and then translate them to approximately where they used to be (based on their bounding box):

Save the above into a file named "reset_affine_transforms.py" under plugins directory or subdirectory to run it directly from the menus, or copy-paste it into the Jython Interpreter.

See also: the different methods for manipulating the affine transform of a Displayable object like a Patch.

And a WARNING: if you modify the AffineTransform of a Patch and don't call then any of the Displayable methods for doing so as well (like we did above: the script calls "Displayable.translate"), then you must update the bucket yourself:

patch.updateBucket()

The bucket is the region of the 2D world where the Patch lives. Picture the world as a checkerboard, where a given image, wrapped in a Patch object, belongs to each of the square that it intersects. Failing to update the bucket will result in improper canvas repaints--the Patch cannot be found.

Adding areas to an AreaList by scanning pixel values in the slices of a stack

The script below is the same as the command "Import - Import labels as arealists".

Extract areas from an arealist and put them as ROIs in ImageJ's ROI Manager

# Albert Cardona 2012-06-19
# Obtain an arealist and add all its areas as ROIs in the ROI Manager
from ini.trakem2.display import Display, AreaList
from ij.gui import ShapeRoi
from ij.plugin.frame import RoiManager
def getRoiManager():
""" Obtain a valid instance of the ROI Manager.
Notice that it could still be null if its window is closed."""
if RoiManager.getInstance() is None:
RoiManager()
return RoiManager.getInstance()
def putAreas(arealist):
""" Take all areas of an AreaList and put them in the ROI Manager."""
for layer in arealist.getLayerRange():
area = arealist.getAreaAt(layer)
if area is not None and not area.isEmpty():
roi = ShapeRoi(area)
getRoiManager().addRoi(roi)
def run():
front = Display.getFront()
layers = front.getLayerSet().getLayers()
arealists = front.getSelection().getSelected(AreaList)
if arealists.isEmpty():
IJ.log("No arealists selected!")
return
# Extract areas as ROIs for the first one:
putAreas(arealists[0])
run()

Notice that python (and jython) lets you use object instance methods as first-class functions, and contructors as well. This enables us to rewrite the "putAreas" function in a functional way, without using any temporary variables and without any if/else logic:

def putAreas(arealist):
""" Take all areas of an AreaList and put them in the ROI Manager."""
def put(arealist):
map(getRoiManager().addRoi,
map(ShapeRoi,
filter(lambda area: not area.isEmpty(),
filter(None,
map(arealist.getAreaAt, arealist.getLayerRange())))))

Copying images between two open projects

The script checks that at least two displays are open, and that they belong to two different projects. Then offers a dialog to choose the direction of copying, and finally copies all images, or all visible or selected images, from one project to the other:

To create a script with the above code, copy paste it into a file with an underscore in its name and extension ".py". Then place it in Fiji's plugins folder or subfolder thereof. Finally, restart Fiji or just call "Plugins - Scripting - Refresh Jython Scripts".

Concatenating multiple project XML files by copying all their layers

# Albert Cardona 2010-06-30 for JC Rah
# Takes a list of project XML files
# and grabs all layers in order
# and clones each and all its images
# and then adds it to a newly created project named "all_layers.xml"
from ini.trakem2 import Project
from ini.trakem2.display import Patch
from ini.trakem2.utils import Utils
from ij import IJ
source_dir = "/path/to/projects/" # MUST have ending slash
project_paths = ["project1.xml", "project2.xml", "project3.xml"]
# folder to save the target project at
target_folder = source_dir
def merge_layers():
# Create a new project target_folder as the storage folder:
target = Project.newFSProject("blank", None, target_folder)
# Save it there as "all_layers.xml" so we can call "save()" on it later
target.saveAs(target_folder + "all_layers.xml", True)
targetlayerset = target.getRootLayerSet()
z = 0
# For each project to concatenate, open it, and:
for path in project_paths:
IJ.log("Processing project " + path)
project = Project.openFSProject(source_dir + path, False)
rectangle = project.getRootLayerSet().get2DBounds()
# For each layer in the project, create a new layer "targetlayer" to host a copy of its images:
for layer in project.getRootLayerSet().getLayers():
targetlayer = targetlayerset.getLayer(z, 1, True)
z += 1
# Add to the new layer copies of each image
for ob in layer.getDisplayables():
targetlayer.add(ob.clone(target, False)) # clone in the context of the target project
project.getLoader().setChanged(False) # avoid dialog at closing
project.destroy()
targetlayerset.setMinimumDimensions()
# Regenerate all image mipmaps
futures = []
for patch in targetlayerset.getDisplayables(Patch):
futures.append(patch.updateMipMaps())
Utils.wait(futures)
target.save() # to validate mipmaps
#target.destroy() # comment out to close it
IJ.log("Done!")
# Invoke the function!
merge_layers()

Measure

Measure the minimal distance from each ball to a surface defined by a profile list

Suppose for example that, using a Ball object, you have clicked on each vesicle of a synaptic terminal. And that, using a profile list, you have traced the surface area of a synapse.

3D view of a synaptic surface and its vesicles

Synaptic vesicle measurements of the minimal distance from each vesicle to the synaptic surface

Using the following script, we generate a surface from the profile list, and then measure, for each synaptic vesicle, its minimal distance to the synaptic surface.

The results are finally listed in a results table, from which column-ordered data may be exported for further processing in a spreadsheet.

A similar measurement may be obtained like the following, if you don't mind typing in the IDs of the Ball (vesicles) and AreaList (the synaptic surface), and getting the results summarized into mean, standard deviation and median (of the distances of each vesicle to the mesh):

Interacting with Layers (Sections)

Calibrating and setting the Z dimension

Each Layer stores a Z coordinate and a thickness value with double precision. The Z coordinate is in pixels.

How to compute the Z coordinate of a Layer: suppose that the calibration specifies 4x4x50 nm. This means 4 nm/px in the X axis, 4 nm/px in the Y axis, and 50 nm/px in the Z axis. It is assumed that you set this values by right-clicking on the canvas window and choosing "Display - Calibration...", which opens the familiar ImageJ dialog for image calibration.

Then you have to compute the thickness of a section relative to X axis coordinates. To do so:

layer thickness = (Z calibrated thickness) / (X calibrated thickness)

In our example of 4x4x50 nm/px:

layer thickness = 50 / 4 = 12.5

Then we must set this thickness to every section. This consists of the following steps to be done on the Layer Tree This is the tree that lists the layers in the TrakEM2 window):

1. Right-click on the "Top Level [Layer Set]" node of the Layer Tree.
Then choose "Reset layer Z and thickness".
2. Click on the first layer node, then shift+click on the last layer node.
All nodes will be selected.
3. Right-click on the selected nodes and choose "Scale...".
4. In the dialog, type in "12.5"--the value we computed above.

Interacting with Treeline, AreaTree and Connector

All three types: "treeline", "areatree", and "connector" are expressed by homonimous classes that inherit from the abstract class ini.trakem2.display.Tree.

A Tree is a Displayable and hence presents properties such as title, alpha, color, locked, visible ... which are accessible with their homonimous set and get methods (e.g. setAlpha(0.8f);, getAlpha(); etc.)

The Tree consists of a root Node and public methods to access it and modify it.

The root Node gives access to the rest of the nodes of the Tree. From the canvas, a user would push 'r' on a selected Treeline, AreaTree or Connector to bring the field of view to where the root node is. From code, we would call:

# Acquire a reference the selected object in the Display
t = Display.getFront().getActive()
# If t is not a Tree, the following will fail:
root = t.getRoot()

Now that we have a reference to the root Node, we'll ask it to give us the entire collection of subtree nodes: all nodes in the Tree:

nodes = root.getSubtreeNodes()

The NodeCollection is lazy and doesn't do caching. If you are planning on calling size() on it, and then iterating its nodes, you would end up iterating the whole sequence twice. So let's start by duplicating it:

Find branch nodes or end nodes

The Tree class offers methods to obtain the list of all branch points, end points, or both:

from ini.trakem2.display import Display
# Obtain the currently selected treeline or areatree or connector:
tree = Display.getFront().getActive()
# A collection of all end nodes (not lazy):
endNodes = tree.getEndNodes()
# A lazy collection of all branch nodes:
branchNodes = tree.getBranchNodes()
# A lazy collection of both all end nodes and all branch nodes:
endOrBranchNodes = tree.getBranchAndEndNodes()

Remember that these lazy collections are non-caching. If you call size() on it, it will traverse the whole tree of nodes just to find out how many nodes of that kind exist.

If you want to sort out all nodes in one pass, query the number of children that each node has:

* if 0, it's an end node
* if 1, it's a slab node
* if more than 1, it's a branch node

Keep in mind that the root node will be listed among the nodes above, so it's not counted as an end node (unless it doesn't have any children, e.g. when the tree consists of only the root node).

Find out at which nodes the tree is connected to other trees, via Connector

The idea here is to iterate all nodes of a tree, and determine, for each node, whether it is enclosed by the origin point of a Connector instance. Then, we query that connector for its target objects. In the end, we obtain a table of nodes vs. lists of objects that node is connected to:

Similarly, we could compute the incomming connections. There is a convenience method findConnectors() in class Tree to return two lists: that of the outgoing and that of the incomming Connector instances. From these, one can easily get the connectivity graph, which you may also get by right-clicking on a Display and going for "Export - Connectivity graph...".

How to find out the network of all arbors, related via Connector instances

The easiest way is to iterate all connectors and find out which objects they are relating. A Connector object has an origin (the root node) and any number of targets (all children nodes of the root node). Each node has a radius; any other object in the TrakEM2 project that intersects with the world coordinates of that radius will be considered associated as an origin or a target.

Notice how about we called getOrigins(Tree) and getTargets(Tree), which filters all potential origins and targets (Patch--an image--, AreaList, etc.) so that only Tree instances will be present in the lists.

NOTE: you may also want to use the "Export - NeuroML" menu command, in the right-click popup menu.

Measure all spine necks in a neuronal arbor

UPDATE: as of version 0.8n, this functionality is included in TrakEM2. Right-click on a selected treeline or areatree and choose "Measure - Shortest distances between all pairs of nodes tagged as..."

The idea is to label the beginning of a spine neck with the tag "neck start" and the end of the spine neck with the tag "neck end". It is assumed that the "next end" will always be in the subtree of the "neck start" node; in other words, that the direction of the tree is from "neck start" to "neck end".

Then, we iterate all nodes of the arbor looking for nodes that have the "neck start" tag and measure the calibrated length of the neck. All measurements for all spine necks are printed out.

# 2011-03-13 Albert Cardona for Nuno da Costa
#
# For a given Treeline or AreaTree that represents a neuronal arbor,
# find all nodes that contain the tag "neck start"
# and for each of those find the distance to a node
# in their subtree that contains the tag "neck end".
#
# In short, measure the lengths of all spine necks
# labeled as such in the arbor.
from math import sqrt
from ini.trakem2.display import Display, AreaTree, Treeline
def findNeck(startNode):
""" Assumes necks are not branched. """
neck = []
for node in startNode.getSubtreeNodes():
tags = getTagsAsStrings(node)
if tags is None or not "neck end" in tags:
neck.append(node) # growing the neck
continue
# Else, end of neck:
neck.append(node)
return neck
print "Did not find a node with an end tag, for parent node " + startNode
return None # end tag not found!
def getTagsAsStrings(node):
found = set()
tags = node.getTags()
if tags is None or 0 == len(tags):
return found
for tag in tags:
found.add(tag.toString())
return found
def measureSpineNecks(neuron):
""" Expects an AreaTree or a Treeline for neuron.
Assumes that nodes with a tag "neck start" are parents or superparents of nodes with tags of "neck end".
"""
print "Measurements for neuron '" + str(neuron) + "':"
for node in neuron.getRoot().getSubtreeNodes():
# Check if the node has the start tag
tags = getTagsAsStrings(node)
if tags is None or not "neck start" in tags:
continue
# Find its child node that has an end tag
neck = findNeck(node)
if neck is None:
continue
distance = neuron.measurePathDistance(neck[0], neck[-1])
print " id:", neuron.getId(), "-- neck length: ", distance
def isTree(x):
return isinstance(x, Treeline) or isinstance(x, AreaTree)
# Measure in all treelines or areatrees:
#trees = filter(isTree, Display.getFront().getLayerSet().getZDisplayables())
# Measure only in the selected treelines or areatrees:
trees = filter(isTree, Display.getSelected())
if 0 == len(trees):
print "No trees found!"
else:
for neuron in trees:
measureSpineNecks(neuron)

Generate 3D meshes

In TrakEM2, 3D meshes are generated as a list of Point3f for each object. Then the list is wrapped into any of the subclasses of CustomMesh of the 3D Viewer library, such as a CustomTriangleMesh or a CustomLineMesh. Then these mesh objects are encapsulated into a Content object and added to an instance of the Image3DUniverse, which is the main window of the 3D Viewer.

Of course, via scripting many of these steps may be skipped. Below are several examples on how to generate meshes programmatically and save them in Wavefront format.

Generate a 3D mesh for an AreaList

This script illustrates how to bypass the 3D Viewer to generate meshes from an AreaList and then export the data in Wavefront format. The script exports an AreaList that has been selected in the front Display.

To export all selected objects, loop through the Display.getSelected().

To export all arealists, loop through Display.getFront().getLayerSet().getZDisplayables(AreaList).

Generate a 3D mesh for an AreaTree

Just like for an AreaList (see above), but extract the triangles with:

triangles = areatree.generateMesh(1, resample).verts

The AreaTree's generateMesh returns a MeshData object with the list of vertices and the list of colors of each vertex. The generateTriangles method of an AreaTree returns a list of Point3f that are ready for creating a CustomLineMesh (in PAIRWISE mode) to represent the skeleton.

Save the project while running a task

While a task is running, the right-click menu shows only an entry to cancel the task. To save the project while the task is running, type the following into the Jython Interpreter, and push return to execute it:

Display.getFront().getProject().save()

If you would like to edit the Project properties, the following code will open the "Project - Properties..." dialog:

Display.getFront().getProject().adjustProperties()

In the above dialog, you will be able to set the autosaving interval (see the bottom text field of the dialog that opens). The interval defaults to zero (meaning never). Set it for example to 30 (once every half hour).

Of course it may be easier to set that autosaving interval before running the long task!

Create a TrakEM2 project for fast visualization, without mipmaps

Create a TrakEM2 project that avoids generating mipmaps, then import lots of images from a text file that has four columns: the file path, the X, the Y, and the section index of each image tile. Then acquire a snapshot of the first section.

As a result of the script, a new Project tab will open in the "TrakEM2" window, and a new Display window will show. At any time, run "project.saveAs(xmlfilepath)" to store the project in an XML file, and from then on just "project.save()". Or right-click and choose "Project - Save", or push 's'.

Create a snapshot in 8-bit, 16-bit, 32-bit or RGB

From the right-click menu, one may choose "Export - Make flat image", which opens a dialog that lets one choose between 8-bit and RGB. These snapshots are created from the mipmaps, which are all 8-bit or RGB images.

On occasions, one wants to create a flattened montage of images in their original bit depth, such as 16-bit or 32-bit. For this purpose, the static function Patch.makeFlatImage exists.

Here is an example that, for a given Layer and set of selected Patch instances (image tiles) in it, it makes a 16-bit flat montage image and returns it as an ImageJ's ImageProcessor, at 50% the original scale.

For other output types, use ImagePlus.GRAY8, .GRAY16, GRAY32 or .COLOR_RGB, as listed in the documentation for the ImagePlus class.

Enrich the GUI of TrakEM

Add an extra tab to a Display

TrakEM API is accessible at all times. Here is an example that adds a new tab to the display. The new tab consists of a JPanel with a single button in it.

Notice that Jython lets you define the methods of event listeners as additional arguments to the constructor. So the JButton gets an actionPerformed method (from the ActionListener interface) just by referencing a declared method.