Created
July 2, 2025 14:15
-
-
Save NicoKiaru/9c8be72b8e83482297644ca5b7f30a5b to your computer and use it in GitHub Desktop.
How to use Warpy transforms in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "id": "28eed8b1-e782-4eaf-bd7a-f8025f037581", | |
| "metadata": {}, | |
| "source": [ | |
| "# Using Java RealTransforms in Python \n", | |
| "\n", | |
| "This is useful for Warpy and ABBA\n", | |
| "\n", | |
| "This notebook can be executed in the following environment:\n", | |
| "\n", | |
| "```bash\n", | |
| "name: warpy_env\n", | |
| "channels:\n", | |
| " - defaults\n", | |
| " - conda-forge\n", | |
| "dependencies:\n", | |
| " - python=3.12\n", | |
| " - pyimagej\n", | |
| " - jupyterlab\n", | |
| " - numpy\n", | |
| "```" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 7, | |
| "id": "91b7e44a-b302-44ea-af2a-b6f6c2477a15", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "\n", | |
| "# PyImageJ / Scyjava\n", | |
| "from scyjava import jimport\n", | |
| "import imagej\n", | |
| "\n", | |
| "# JPype\n", | |
| "from jpype.types import JString, JArray\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "id": "ba2e8bc8-9a84-44bd-9127-2d2807fd0254", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "def get_java_dependencies():\n", | |
| " \"\"\"\n", | |
| " Returns the jar files that need to be included into the classpath\n", | |
| " :return:\n", | |
| " \"\"\"\n", | |
| " return [# 'net.imagej:imagej:2.16.0',\n", | |
| " \t'ch.epfl.biop:bigdataviewer-biop-tools:0.11.2'\n", | |
| " # add another jar here if necessary\n", | |
| " ]\n", | |
| "\n", | |
| "# Start a Fiji instance - it's big and will take time the first time.\n", | |
| "ij = imagej.init(get_java_dependencies(), mode=\"headless\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "a83db2df-9332-4eee-bfeb-749bb6d87051", | |
| "metadata": {}, | |
| "source": [ | |
| "Warpy uses a json serialization of its real transform objects. They can be accessed and used in python through pyimagej" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "id": "0949d579-4c47-46b2-bf27-b9eada0cd7a9", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "\n", | |
| "transform_file_path = 'C:/Users/Nicolas/Downloads/warpy-demo-project/data/1/transform_1_2.json'\n", | |
| "\n", | |
| "# Importing Java classes in python\n", | |
| "RealPoint = jimport('net.imglib2.RealPoint')\n", | |
| "RealTransformHelper = jimport('net.imglib2.realtransform.RealTransformHelper')\n", | |
| "ArrayList = jimport('java.util.ArrayList')\n", | |
| "\n", | |
| "# Retrieve the transformation object\n", | |
| "transform = RealTransformHelper.fromJson(ij.context(), JString(transform_file_path))\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "03bf4398-f051-4509-9f83-16692f11cce3", | |
| "metadata": {}, | |
| "source": [ | |
| "## Transforming one point" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "id": "c04e8ec1-18d3-4cd2-8bd9-9ff4aa35667c", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "X: -6943.229206834344\n", | |
| "Y: -3729.827039698758\n", | |
| "Z: 0.0\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "\n", | |
| "# Transforming one point:\n", | |
| "\n", | |
| "# the order is X, Y, Z; is it a good idea ?\n", | |
| "\n", | |
| "pt_source = RealPoint(1.0, 80.0, 0.0) # You need to put .0 or to cast explicitely to with float(number)\n", | |
| "pt_dest = RealPoint(0.0,0.0,0.0) # The destination object needs to be initialized\n", | |
| "\n", | |
| "transform.apply(pt_source, pt_dest)\n", | |
| "\n", | |
| "print(\"X: \"+str(pt_dest.getDoublePosition(0)))\n", | |
| "print(\"Y: \"+str(pt_dest.getDoublePosition(1)))\n", | |
| "print(\"Z: \"+str(pt_dest.getDoublePosition(2)))\n", | |
| "\n", | |
| "# Note: this will be very slow if there are many points to transform, because each transformation requires a jni call. \n", | |
| "# Hence the second method below that uses a single call to transform a list of points using numpy\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "759bd911-8f79-4024-b160-429abb1c6cc5", | |
| "metadata": {}, | |
| "source": [ | |
| "## Transforming points in batch by using numpy and RealTransformHelper\n", | |
| "\n", | |
| "Because there's a single jni call for the list of points, this method will be faster" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "id": "6e340d78-ed65-481a-85d7-f6905a1dd947", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "[[-6939 -3803 0]\n", | |
| " [-6921 -3781 0]\n", | |
| " [-6921 -3767 0]\n", | |
| " [-6906 -3734 0]]\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "\n", | |
| "import numpy as np\n", | |
| "\n", | |
| "pts = np.array([ [5, 12,0], [21, 32,0], [21, 45,0], [35,76, 0]])\n", | |
| "RealTransformHelper.apply(ij.py.to_java(pts), transform)\n", | |
| "\n", | |
| "print(pts) # they've been mutated\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "c70a3d7b-0da4-4bf4-98c3-6c5669fdb675", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 3 (ipykernel)", | |
| "language": "python", | |
| "name": "python3" | |
| }, | |
| "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.12.11" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 5 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment