Skip to content, Skip to search

Changes

TrakEM2 Scripting

7,163 bytes added, 05:30, 1 November 2016
update java3d package names to org.scijava.vecmath
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.
 
Or open a new [[Script Editor]] window with "File - New - Script", then paste the example, select the "Language - Python", and push the "Run" button.
= Introduction to scripting TrakEM2 =
for d in selection.get(Patch):
print d.title
</source>
 
=== 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()
</source>
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:
[http://fiji.sc/javadoc/ini/trakem2/Project.html#newFSProject(java.lang.String,%20ini.trakem2.tree.TemplateThing,%20java.lang.String) Project.newFSProject], [http://fiji.sc/javadoc/ini/trakem2/display/Patch.html#createPatch(ini.trakem2.Project,%20java.lang.String) Patch.createPatch], [http://fiji.sc/javadoc/ini/trakem2/display/Layer.html#add(ini.trakem2.display.Displayable) Layer.add], [http://fiji.sc/javadoc/mpicbg/trakem2/align/Align.html Align], [http://fiji.sc/javadoc/mpicbg/trakem2/align/AlignTask.html 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:
project.getLayerTree().updateList(layerset)
# ... and the display slider
Display.updateLayerScroller(layerset)
 
# 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)
layer.add(patch)
# Update internal quadtree of the layer
layer.recreateBuckets()
 
# 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: http://fiji.sc/javadoc/mpicbg/trakem2/align/Align.Param.html
# transformation models: http://fiji.sc/javadoc/mpicbg/trakem2/align/Align.ParamOptimize.html
# sift: http://fiji.sc/javadoc/mpicbg/imagefeatures/FloatArray2DSIFT.Param.html
AlignTask.montageLayers(param, layerset.getLayers(), False, False, False, False)
 
# 5. Resize width and height of the world to fit the montages
layerset.setMinimumDimensions()
 
# 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!"
</source>
= Manipulating Displayable objects =
Display.repaint()
</source>
 
 
=== 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:
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()
</source>
 
 
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):
map(getRoiManager().addRoi,
map(ShapeRoi,
filter(lambda area: not area.isEmpty(),
filter(None,
map(arealist.getAreaAt, arealist.getLayerRange())))))
</source>
=== 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">
</source>
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">
</source>
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">
</source>
Each [http://pacificfiji.mpi-cbg.desc/javadoc/ini/trakem2/display/Node.html Node] has:
<ol>
<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>
</ol>
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:
<ul>
<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>
</ul>
The method we use is [http://www.informatik.uni-konstanz.de/~brandes/ Ulrik Brande]'s fast algorithm for computing betweenness centrality (see the [http://www.google.com/url?sa=t&source=web&cd=1&ved=0CBcQFjAA&url=http%3A%2F%2Fciteseerx.ist.psu.edu%2Fviewdoc%2Fdownload%3Fdoi%3D10.1.1.11.2024%26rep%3Drep1%26type%3Dpdf&ei=krO5TPKvNMXN4AbS4OHdDQ&usg=AFQjCNELHtt9Gb5KHMzRCeTsqI1uDoqxew 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)
##############
</source>
 
== 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:
# https://github.com/trakem2/TrakEM2/blob/master/TrakEM2_/src/main/java/ini/trakem2/display/Ball.java#L716
 
 
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)
</source>
= Generate 3D meshes =
In TrakEM2, 3D meshes are generated as a list of [http://downloadjavadoc.javaimagej.net/media/java3d/javadocJava3D/1index.5.2-latesthtml?org/javaxscijava/vecmath/Point3f.html 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 [http://www.martinreddy.net/gfx/3d/OBJ.spec Wavefront] format.
<source lang="python">
from ini.trakem2.display import Display
from javaxorg.scijava.vecmath import Color3f
from customnode import WavefrontExporter, CustomTriangleMesh
from java.io import StringWriter
</source>
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 [http://downloadjavadoc.javaimagej.net/media/java3d/javadocJava3D/1index.5.2-latesthtml?org/javaxscijava/vecmath/Point3f.html 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.
*[http://www.jython.org Jython webpage].
*[http://www.ini.uzh.ch/~acardona/fiji-tutorial Fiji scripting tutorial] in Jython.
== Jython scripts for TrakEM2 ==
All the following are included in Fiji's plugins/Examples/TrakEM2_Example_Scripts/ folder:
*[http://pacific.mpi-cbg.de/cgi-bin/gitweb.cgi?p{{GitHub|repo=fiji.git;a=blob;f|path=plugins/Examples/TrakEM2_Example_Scripts/extract_stack_under_arealist.py;hb|label=HEAD Extract stack under AreaList] }} in TrakEM2.*[http://pacific.mpi-cbg.de/cgi-bin/gitweb.cgi?p{{GitHub|repo=fiji.git;a=blob;f|path=plugins/Examples/TrakEM2_Example_Scripts/T2_set_all_transforms_to_identity.py;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/T2_Select_AllMeasure_AreaLists.py;hb|label=HEAD Select All] objects Measure AreaList}} in TrakEM2.*A [https://github.com/acardona/Fiji-TrakEM2-scripts/tree/master/TrakEM2 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/Measure_AreaListsConnector.py;hb=HEAD Measure AreaListhtml Connector] in TrakEM2instances, when used for neural circuit reconstruction.
[[Category:Scripting]]
[[Category:TrakEM2]]
Bureaucrat, emailconfirmed, incoming, administrator, uploaders
707
edits