Skip to content, Skip to search


TrakEM2 Scripting

6,928 bytes added, 11:30, 1 November 2016
update java3d package names to org.scijava.vecmath
for d in selection.get(Patch):
print d.title
=== Find the file path of images that lay under a specific floating text label ===
The idea is to add floating text labels over images (using the Text Tool), and then to search for all the images that are under the X,Y coordinate of each label. Then we print the
<source lang="python">
regularExpression = ".*fold.*"
for layer in Display.getFront().getLayerSet().getLayers():
for label in layer.getDisplayables(DLabel):
if label.getTitle().matches(regularExpression):
tx = label.getAffineTransform().getTranslationX()
ty = label.getAffineTransform().getTranslationY()
patches = layer.find(Patch, tx, ty)
for patch in patches:
print patch.getImageFilePath()
If you change the affine transform of a Displayable directly (by calling <i>getAffineTransform()</i> 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 <i>updateBucket()</i> 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 <i><b>folder</b></i> where images are to be found.
2. The <i><b>pattern</b></i> 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 <i>getLayer</i> method on the <i>layerset</i> 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.
Documentation you may want to look at:
[,%20ini.trakem2.tree.TemplateThing,%20java.lang.String) Project.newFSProject], [,%20java.lang.String) Patch.createPatch], [ Layer.add], [ Align], [ AlignTask],
<source lang="python">
# Albert Cardona 2011-06-05
# Script for Colenso Speer
import os, re
#folder = "/path/to/folder/with/all/images/"
folder = "/home/albert/Desktop/t2/example-data/images/2043_5_6_7"
# 1. Create a TrakEM2 project
project = Project.newFSProject("blank", None, folder)
# OR: get the first open project
# project = Project.getProjects().get(0)
layerset = project.getRootLayerSet()
# 2. Create 10 layers (or as many as you need)
for i in range(10):
layerset.getLayer(i, 1, True)
# ... and update the LayerTree:
# ... and the display slider
# 3. To each layer, add images that have "_zN_" in the name
# where N is the index of the layer
# and also end with ".tif"
filenames = os.listdir(folder)
for i,layer in enumerate(layerset.getLayers()):
# EDIT the following pattern to match the filename of the images
# that must be inserted into section at index i:
pattern = re.compile(".*_z" + str(i) + "_.*\.tif")
for filename in filter(pattern.match, filenames):
filepath = os.path.join(folder, filename)
patch = Patch.createPatch(project, filepath)
# Update internal quadtree of the layer
# 4. Montage each layer independently
from mpicbg.trakem2.align import Align, AlignTask
param = Align.ParamOptimize() # which extends Align.Param
param.sift.maxOctaveSize = 512
# ... above, adjust other parameters as necessary
# See:
# features:
# transformation models:
# sift:
AlignTask.montageLayers(param, layerset.getLayers(), False, False, False, False)
# 5. Resize width and height of the world to fit the montages
# 6. Blend images of each layer
Blending.blendLayerWise(layerset.getLayers(), True, None)
# 7. Save the project
project.saveAs(os.path.join(folder, "montages.xml"), False)
print "Done!"
= Manipulating Displayable objects =
=== Extract areas from an arealist and put them as ROIs in ImageJ's ROI Manager ===
<source lang="python">
# 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:
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)
def run():
front = Display.getFront()
layers = front.getLayerSet().getLayers()
arealists = front.getSelection().getSelected(AreaList)
if arealists.isEmpty():
IJ.log("No arealists selected!")
# Extract areas as ROIs for the first one:
Notice that python (and jython) lets you use object instance methods as first-class functions, and constructors 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:
<source lang="python">
def putAreas(arealist):
""" Take all areas of an AreaList and put them in the ROI Manager."""
def put(arealist):
filter(lambda area: not area.isEmpty(),
map(arealist.getAreaAt, arealist.getLayerRange())))))
=== Calibrating and setting the Z dimension ===
Each [http://pacificfiji.mpi-cbg.desc/javadoc/ini/trakem2/display/Layer.html Layer] stores a Z coordinate and a thickness value with <i>double</i> precision. The Z coordinate is in pixels.
How to compute the Z coordinate of a [http://pacificfiji.mpi-cbg.desc/javadoc/ini/trakem2/display/Layer.html 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:
1. Right-click on the "Top Level [Layer Set]" node of the <i>Layer Tree</i>.
Then choose "Reset layer Z and thickness".
2. Click on the first layer node, then shift{{key|Shift}}+{{key|click }} on the last layer node.
All nodes will be selected.
3. Right-click on the selected nodes and choose "Scale...".
= Interacting with Treeline, AreaTree and Connector =
All three types: "treeline", "areatree", and "connector" are expressed by homonimous classes that inherit from the abstract class [http://pacificfiji.mpi-cbg.desc/javadoc/ini/trakem2/display/Tree.html ini.trakem2.display.Tree].
A [http://pacificfiji.mpi-cbg.desc/javadoc/ini/trakem2/display/Tree.html Tree] is a [http://pacific.mpi-cbgfiji.desc/javadoc/ini/trakem2/display/Displayable.html Displayable] and hence presents properties such as title, alpha, color, locked, visible ... which are accessible with their homonimous set and get methods (e.g. <I>setAlpha(0.8f);</i>, <i>getAlpha();</i> etc.)
The [http://pacificfiji.mpi-cbg.desc/javadoc/ini/trakem2/display/Tree.html Tree] consists of a root [http://pacific.mpi-cbgfiji.desc/javadoc/ini/trakem2/display/Node.html Node] and public methods to access it and modify it.
The root [http://pacificfiji.mpi-cbg.desc/javadoc/ini/trakem2/display/Node.html Node] gives access to the rest of the nodes of the [http://pacific.mpi-cbgfiji.desc/javadoc/ini/trakem2/display/Tree.html 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:
<source lang="python">
Now that we have a reference to the root [http://pacificfiji.mpi-cbg.desc/javadoc/ini/trakem2/display/Node.html Node], we'll ask it to give us the entire collection of subtree nodes: all nodes in the [http://pacific.mpi-cbgfiji.desc/javadoc/ini/trakem2/display/Tree.html Tree]:
<source lang="python">
The [http://pacificfiji.mpi-cbg.desc/javadoc/ini.trakem2/display/Node.NodeCollection.html 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:
<source lang="python">
Each [http://pacificfiji.mpi-cbg.desc/javadoc/ini/trakem2/display/Node.html Node] has:
<li>X, Y coordinates, relative to the local coordinate system of the Tree that contains the [http://pacificfiji.mpi-cbg.desc/javadoc/ini/trakem2/display/Node.html Node].</li> <li>A reference to a layer (get it with nd.getLayer()). The [http://pacific.mpi-cbgfiji.desc/javadoc/ini/trakem2/display/Layer.html Layer] has a getZ() method to get the Z coordinate (in pixels).</li>
<li>A data field, which can be a radius or a java.awt.geom.Area (see below).</li>
Each [http://pacificfiji.mpi-cbg.desc/javadoc/ini/trakem2/display/Node.html Node] contains a [http://pacific.mpi-cbgfiji.desc/javadoc/ini/trakem2/display/Node.html#getData() getData()] public method to acquire whatever it is that it has:
<li>Treeline and Connector: its nodes [http://pacificfiji.mpi-cbg.desc/javadoc/ini/trakem2/display/Node.html#getData() getData()] return a radius. The default value is zero.</li> <li>AreaTree: its nodes [http://pacificfiji.mpi-cbg.desc/javadoc/ini/trakem2/display/Node.html#getData() getData()] return a [http://pacific.mpi-cbgfiji.desc/javadoc/java/awt/geom/Area.html java.awt.geom.Area] instance, or null if none yet assigned to it.</li>
The method we use is [ Ulrik Brande]'s fast algorithm for computing betweenness centrality (see the [ paper]).
The method [http://pacificfiji.mpi-cbg.desc/javadoc/ini/trakem2/display/Tree.html#computeCentrality() computeCentrality()] of class [http://pacific.mpi-cbgfiji.desc/javadoc/ini/trakem2/display/Tree.html Tree] returns as a [http://pacific.mpi-cbgfiji.desc/javadoc/java/util/Map.html Map] of [http://pacific.mpi-cbgfiji.desc/javadoc/ini/trakem2/display/Node.html Node] instance vs. its centrality value:
<source lang="python">
== Compute the degree of every node ==
The degree of a node is the number of parent nodes that separate it from the root node. It's a built-in function in [http://pacificfiji.mpi-cbg.desc/javadoc/ini/trakem2/display/Tree.html Tree] (and also in [http://pacific.mpi-cbgfiji.desc/javadoc/ini/trakem2/display/Tree.html Node]):
In the following example, we colorize the tree based on the degree of the node: the closer to the root, the hotest:
== Find branch nodes or end nodes ==
The [http://pacificfiji.mpi-cbg.desc/javadoc/ini/trakem2/display/Tree.html Tree] class offers methods to obtain the list of all branch points, end points, or both:
<source lang="python">
Similarly, we could compute the incomming connections. There is a convenience method [http://pacificfiji.mpi-cbg.desc/javadoc/ini/trakem2/display/Tree.html#findConnectors() findConnectors()] in class [http://pacific.mpi-cbgfiji.desc/javadoc/ini/trakem2/display/Tree.html 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 [http://pacificfiji.mpi-cbg.desc/javadoc/ini/trakem2/display/Connector.html 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.
<source lang="python">
ballOb.repaint(True, None)
== Export all Ball objects as a CSV file ==
<source lang="python">
# Open a text window containing all Ball objects as a CSV file,
# in calibrated coordinates.
# The text window has a "File - Save" menu for saving to a file.
# Albert Cardona 2015-07-02 for Jemima Burden at UCL.
# See also the API of the Ball class:
from ini.trakem2.display import Display, Ball
from ij.text import TextWindow
ball_obs = Display.getFront().getLayerSet().getZDisplayables(Ball)
# One entry for each id,x,y,z,r
rows = []
# Iterate every Ball instance, which contains one or more x,y,z,r balls
for ball_ob in ball_obs:
id = ball_ob.getId()
# Iterate every x,y,z,r ball of a Ball instance, calibrated
wbs = ball_ob.getWorldBalls()
for ball_coords in wbs:
# Store every ball as a row with id, x, y, z, r
rows.append(str(id) + "," + ",".join(str(c) for c in ball_coords))
csv = "\n".join(rows)
t = TextWindow("Balls CSV", csv, 400, 400)
= 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 [http://pacificfiji.mpi-cbg.desc/javadoc/customnode/CustomMesh.html CustomMesh] of the 3D Viewer library, such as a [http://pacificfiji.mpi-cbg.desc/javadoc/customnode/CustomTriangleMesh.html CustomTriangleMesh] or a [http://pacificfiji.mpi-cbg.desc/javadoc/customnode/CustomLineMesh.html CustomLineMesh]. Then these mesh objects are encapsulated into a [http://pacificfiji.mpi-cbg.desc/javadoc/ij3d/Content.html Content] object and added to an instance of the [http://pacific.mpi-cbgfiji.desc/javadoc/ij3d/Image3DUniverse.html 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.
<source lang="python">
from ini.trakem2.display import Display
from javaxorg.scijava.vecmath import Color3f
from customnode import WavefrontExporter, CustomTriangleMesh
from import StringWriter
The [http://pacificfiji.mpi-cbg.desc/javadoc/ini/trakem2/display/AreaTree.html AreaTree]'s generateMesh returns a [http://pacificfiji.mpi-cbg.desc/javadoc/ini/trakem2/display/Tree.MeshData.html MeshData] object with the list of vertices and the list of colors of each vertex. The <i>generateTriangles</i> method of an [http://pacificfiji.mpi-cbg.desc/javadoc/ini/trakem2/display/AreaTree.html AreaTree] returns a list of [ Point3f] that are ready for creating a [http://pacific.mpi-cbgfiji.desc/javadoc/customnode/CustomLineMesh.html CustomLineMesh] (in PAIRWISE mode) to represent the skeleton.
= Save the project while running a task =
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 [http://pacificfiji.mpi-cbg.desc/javadoc/ini/trakem2/display/Patch.html#makeFlatImage(int,%20ini.trakem2.display.Layer,%20java.awt.Rectangle,%20double,%20java.util.Collection,%20java.awt.Color,%20boolean) 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 [http://pacificfiji.mpi-cbg.desc/javadoc/ij/ImagePlus.html ImagePlus] class.
= Enrich the GUI of TrakEM =
*[[Jython Scripting]] in fiji.
*[ Jython webpage].
*[Fiji scripting tutorial Fiji scripting tutorial] in Jython.
== Jython scripts for TrakEM2 ==
All the following are included in Fiji's plugins/Examples/TrakEM2_Example_Scripts/ folder:
*[{{GitHub|repo=fiji.git;a=blob;f|path=plugins/Examples/TrakEM2_Example_Scripts/;hb|label=HEAD Extract stack under AreaList] }} in TrakEM2.*[{{GitHub|repo=fiji.git;a=blob;f|path=plugins/Examples/TrakEM2_Example_Scripts/;hb|label=HEAD Set all transforms to identity] }} for TrakEM2 objects.*[http:{{GitHub|repo=fiji|path=plugins/Examples/pacific.mpi-cbg.deTrakEM2_Example_Scripts/cgi-bin/gitwebT2_Select_All.cgi?ppy|label=fijiSelect All}} objects in TrakEM2.git;a*{{GitHub|repo=blob;ffiji|path=plugins/Examples/TrakEM2_Example_Scripts/;hb|label=HEAD Select All] objects Measure AreaList}} in TrakEM2.*A [ collection of scripts for TrakEM2], hosted by github. Mostly related to inspecting and analyzing [http://pacificfiji.mpi-cbgsc/javadoc/ini/trakem2/display/Treeline.html Treeline], [http://fiji.desc/javadoc/ini/trakem2/cgi-bindisplay/gitwebAreaTree.cgi?p=html AreaTree] and [http://fiji.git;a=blob;f=pluginssc/javadoc/ini/Examplestrakem2/TrakEM2_Example_Scriptsdisplay/;hb=HEAD Measure AreaListhtml Connector] in TrakEM2instances, when used for neural circuit reconstruction.
Bureaucrat, emailconfirmed, incoming, administrator, uploaders