{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook demonstrates integration of functionality from SNT with Python through pyimagej. \n", "The context is the computation and rendering of the convex hull enclosing all axonal/dendritic terminal nodes. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import sys\n", "!conda install --yes --prefix {sys.prefix} -c conda-forge pyimagej openjdk=8 scipy" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import imagej\n", "import numpy as np\n", "from scipy.spatial import ConvexHull" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Added 463 JARs to the Java classpath.\n" ] } ], "source": [ "# Initialize Fiji with GUI support.\n", "ij = imagej.init('sc.fiji:fiji', headless=False)\n", "\n", "from jnius import autoclass, cast" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# Import relevant SNT (Java) classes.\n", "HashSet = autoclass('java.util.HashSet')\n", "PointInImage = autoclass('sc.fiji.snt.util.PointInImage')\n", "MouseLightLoader = autoclass('sc.fiji.snt.io.MouseLightLoader')\n", "Tree = autoclass('sc.fiji.snt.Tree')\n", "TreeAnalyzer = autoclass('sc.fiji.snt.analysis.TreeAnalyzer')\n", "Viewer = autoclass('sc.fiji.snt.viewer.Viewer3D')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def convex_hull(tree):\n", " \"\"\"Computes the convex hull of the input Tree\n", " and return a Java ArrayList containing the vertices \n", " representing the hull.\n", " \n", " Returns:\n", " verts_java (ArrayList): The list containing the hull vertices.\n", " Args:\n", " tree (Tree): The SNT Tree object.\"\"\"\n", " \n", " analyzer = TreeAnalyzer(tree)\n", " # Extract the end points from the tree as a java HashSet.\n", " tips_java_set = analyzer.getTips()\n", " # Convert to array to maintain ordering.\n", " tips_java_array = tips_java_set.toArray()\n", "\n", " # Convert to python list.\n", " tips_list = [[t.x, t.y, t.z] for t in tips_java_array]\n", "\n", " assert len(tips_list) == tips_java_set.size()\n", "\n", " # Find the convex hull of the end points.\n", " hull = ConvexHull(tips_list)\n", " \n", " print(\"The volume of the convex hull encapsulating all tip points is {} um^3\".format(\n", " round(hull.volume, 2)))\n", " \n", " print(\"The area of the hull is {} um^2\".format(round(hull.area, 2)))\n", " print()\n", " \n", " # Find original PointInImage objects to pass to Viewer3D.\n", " # This is less costly than re-constructing a new Java object for each vertex.\n", " verts_java = [tips_java_array[i] for i in hull.vertices]\n", " verts_java = ij.py.to_java(verts_java)\n", " \n", " return verts_java" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def run():\n", " \n", " # Fetch swc from MouseLight database by ID.\n", " loader = MouseLightLoader('AA0100')\n", " if not loader.isDatabaseAvailable():\n", " print(\"Could not connect to ML database\", \"Error\")\n", " return\n", " if not loader.idExists():\n", " print(\"Somewhow the specified id was not found\", \"Error\")\n", " return\n", "\n", " tree_axon = loader.getTree('axon', None)\n", " tree_dendrites = loader.getTree('dendrite', None)\n", " \n", " verts_axon = convex_hull(tree_axon)\n", " verts_dendrites = convex_hull(tree_dendrites)\n", "\n", " # Visualize the result using SNT Viewer3D.\n", " viewer = Viewer()\n", " viewer.add(tree_axon)\n", " viewer.add(tree_dendrites)\n", " axon_hull = viewer.annotateSurface(verts_axon, \"Axon Hull\")\n", " dendrite_hull = viewer.annotateSurface(verts_dendrites, \"Dendrite Hull\")\n", " axon_hull.setColor(\"red\")\n", " dendrite_hull.setColor(\"blue\")\n", " viewer.show()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The volume of the convex hull encapsulating all tip points is 208791575907.36 um^3\n", "The area of the hull is 191199110.74 um^2\n", "\n", "The volume of the convex hull encapsulating all tip points is 62095649.03 um^3\n", "The area of the hull is 942390.38 um^2\n", "\n" ] } ], "source": [ "run()" ] } ], "metadata": { "kernelspec": { "display_name": "pyimagej jupyter env", "language": "python", "name": "pyimagej_notebook_env" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.1" } }, "nbformat": 4, "nbformat_minor": 2 }