Skip to content, Skip to search


TrakEM2 Scripting

52 bytes added, 24 January
Use HTTPS links where feasible
Documentation you may want to look at:
[http,%20ini.trakem2.tree.TemplateThing,%20java.lang.String) Project.newFSProject], [http,%20java.lang.String) Patch.createPatch], [http Layer.add], [http Align], [http AlignTask],
# ... above, adjust other parameters as necessary
# See:
# features: http transformation models: http sift: http
AlignTask.montageLayers(param, layerset.getLayers(), False, False, False, False)
=== Calibrating and setting the Z dimension ===
Each [http 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 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:
= 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 ini.trakem2.display.Tree].
A [http Tree] is a [http 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 Tree] consists of a root [http Node] and public methods to access it and modify it.
The root [http Node] gives access to the rest of the nodes of the [http 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 Node], we'll ask it to give us the entire collection of subtree nodes: all nodes in the [http Tree]:
<source lang="python">
The [http 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 Node] has:
<li>X, Y coordinates, relative to the local coordinate system of the Tree that contains the [http Node].</li> <li>A reference to a layer (get it with nd.getLayer()). The [http 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 Node] contains a [http getData()] public method to acquire whatever it is that it has:
<li>Treeline and Connector: its nodes [http getData()] return a radius. The default value is zero.</li> <li>AreaTree: its nodes [http getData()] return a [http 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 computeCentrality()] of class [http Tree] returns as a [http Map] of [http 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 Tree] (and also in [http 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 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 findConnectors()] in class [http 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 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">
= 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 CustomMesh] of the 3D Viewer library, such as a [http CustomTriangleMesh] or a [http CustomLineMesh]. Then these mesh objects are encapsulated into a [http Content] object and added to an instance of the [http 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.
The [http AreaTree]'s generateMesh returns a [http MeshData] object with the list of vertices and the list of colors of each vertex. The <i>generateTriangles</i> method of an [http AreaTree] returns a list of [ Point3f] that are ready for creating a [http 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,%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 ImagePlus] class.
= Enrich the GUI of TrakEM =
*{{GitHub|repo=fiji|path=plugins/Examples/TrakEM2_Example_Scripts/|label=Select All}} objects in TrakEM2.
*{{GitHub|repo=fiji|path=plugins/Examples/TrakEM2_Example_Scripts/|label=Measure AreaList}} in TrakEM2.
* A [ collection of scripts for TrakEM2], hosted by github. Mostly related to inspecting and analyzing [http Treeline], [http AreaTree] and [http Connector] instances, when used for neural circuit reconstruction.
Bureaucrat, emailconfirmed, incoming, administrator, uploaders