{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Extending ImageJ: Data I/O" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook demonstrates how to write an `IOPlugin`, which handles reading and/or writing of external data to and from Java data structures." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Added new repo: imagej.public\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "", "version_major": 2, "version_minor": 0 }, "method": "display_data" }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "24c4dd1c-8a8f-4835-8a01-cccbfdedd37a", "version_major": 2, "version_minor": 0 }, "method": "display_data" }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "ImageJ v2.0.0-rc-71 is ready to go." ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%classpath config resolver imagej.public https://maven.imagej.net/content/groups/public\n", "%classpath add mvn net.imagej imagej 2.0.0-rc-71\n", "ij = new net.imagej.ImageJ()\n", "\"ImageJ v${ij.getVersion()} is ready to go.\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating a data reader" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Suppose we have data stored in a file format, `.char-table`, which represents a table as a sequence of characters, along with metadata defining the number of rows and columns. We would like to write a plugin so that ImageJ can import these files via its _File ▶ Open_ command. \n", "\n", "The format of a `.char-table` file is key-value pairs, one per line. Valid keys are:\n", "\n", "* `cols` to define the column count\n", "* `rows` to define the row count\n", "* `data` to specify the actual character data: a sequence of $cols \\times rows$ characters enumerated in row-major order, and bracketed by `<` and `>`.\n", "\n", "Let's start by writing out an example `.char-table` file, which we will use for testing:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Wrote 78 bytes to '/Users/curtis/Desktop/example.char-table'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import java.io.File\n", "import org.scijava.util.DigestUtils\n", "import org.scijava.util.FileUtils\n", "data = \"\"\"\n", "cols = 7\n", "rows = 7\n", "data = <@@@@@@@@ @ @ @ @@ @ @ @@ @ @ @@ @ @ @@@ >\n", "\"\"\"\n", "desktop = System.getProperty(\"user.home\") + \"/Desktop/\"\n", "tablePath = desktop + \"example.char-table\"\n", "outFile = new File(tablePath)\n", "FileUtils.writeFile(outFile, DigestUtils.bytes(data))\n", "\"Wrote ${outFile.length()} bytes to '$outFile'\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Without further ado, here is the `IOPlugin` implementation:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "priority=0.0, enabled=true, pluginType=IOPlugin" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import java.io.File\n", "import org.scijava.io.AbstractIOPlugin\n", "import org.scijava.io.IOPlugin\n", "import org.scijava.plugin.Plugin\n", "import org.scijava.table.DefaultGenericTable\n", "import org.scijava.table.Table\n", "import org.scijava.util.DigestUtils\n", "import org.scijava.util.FileUtils\n", "\n", "@Plugin(type = IOPlugin.class)\n", "public class CharTableReader extends AbstractIOPlugin
@ | @ | @ | @ | @ | @ | @ |
@ | ||||||
@ | @ | @ | @ | |||
@ | @ | @ | @ | |||
@ | @ | @ | @ | |||
@ | @ | |||||
@ | @ | @ | @ |