{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "PyImageJ Tutorial\n",
    "===\n",
    "\n",
    "This notebook covers how to use ImageJ as a library from Python. A major advantage of this approach is the ability to combine ImageJ with other tools available from the Python software ecosystem, including NumPy, SciPy, scikit-image, CellProfiler, OpenCV, ITK and more.\n",
    "\n",
    "This notebook assumes familiarity with the ImageJ API. Detailed tutorials in that regard can be found in the other notebooks."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5 Convenience methods of PyImageJ\n",
    "\n",
    "PyImageJ is built to provide easy access to key ImageJ resources. We call these collective methods \"conveience methods\". These methods are attached to`ij.py` after initializing ImageJ. Here's a quick list of some of the more useful methods and additional information."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "| `ij.py.` | function | more information |\n",
    "| :---: | :---: | :---: |\n",
    "| `show` | Show an image | [2-Opening-and-Displaying-Images](../doc/2-Opening-and-Displaying-Images.ipynb)\n",
    "| `to_java` | Convert data from Python to Java | [3-Sending-Data-to-Java](../doc/3-Sending-Data-to-Java.ipynb) |\n",
    "| `from_java` | Convert data from Java to Python | [4-Retrieving-Data-from-Java](../doc/4-Retrieving-Data-from-Java.ipynb) |\n",
    "| `run_macro` | Run an original ImageJ macro | [7-Running-Macros-Scripts-and-Plugins](../doc/7-Running-Macros-Scripts-and-Plugins.ipynb) |\n",
    "| `run_script` | Run an ImageJ script (supported languages) | [7-Running-Macros-Scripts-and-Plugins](../doc/7-Running-Macros-Scripts-and-Plugins.ipynb) |\n",
    "| `run_plugin` | Run a plugin | [7-Running-Macros-Scripts-and-Plugins](../doc/7-Running-Macros-Scripts-and-Plugins.ipynb) |\n",
    "| `initialize_numpy_image` | Create a new numpy image in the same shape as input image | [6-Working-with-Images](../doc/6-Working-with-Images.ipynb) |\n",
    "| `sync_image` | Syncronize data between ImageJ and ImageJ2 data structures | -- |\n",
    "| `active_image_plus` | Get the `ImagePlus` from the current window | -- |\n",
    "| `active_xarray` | Convert current window into an `xarray.DataArray` | -- |\n",
    "| `active_dataset` | Get the active `Dataset` from the Dataset service | -- |"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are other convenience methods that are attached to `ij.py`. After initializing ImageJ you can explore `ij.py`'s methods with `dir`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.1 Other convenient access to ImageJ functions\n",
    "\n",
    "When the original ImageJ is available (_i.e._ the legacy layer is active) `IJ`, `WindowManager`, `ResultsTable` and `RoiManager` are accessible directly from the initialized `ij` object."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "log4j:WARN No appenders could be found for logger (org.bushe.swing.event.EventService).\n",
      "log4j:WARN Please initialize the log4j system properly.\n",
      "log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.\n",
      "WARNING:root:Operating in headless mode - the original ImageJ will have limited functionality.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ImageJ version: 2.3.0/1.53f\n",
      "Legacy layer active: True\n"
     ]
    }
   ],
   "source": [
    "import imagej\n",
    "\n",
    "# initialize imagej\n",
    "ij = imagej.init()\n",
    "print(f\"ImageJ version: {ij.getVersion()}\")\n",
    "\n",
    "# first check if the legacy layer is active\n",
    "print(f\"Legacy layer active: {ij.legacy.isActive()}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:root:Operating in headless mode - the IJ class will not be fully functional.\n",
      "WARNING:root:Operating in headless mode - the ResultsTable class will not be fully functional.\n",
      "WARNING:root:Operating in headless mode - the RoiManager class will not be fully functional.\n",
      "WARNING:root:Operating in headless mode - the WindowManager class will not be fully functional.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<java class 'ij.IJ'>\n",
      "<java class 'ij.measure.ResultsTable'>\n",
      "<java class 'ij.plugin.frame.RoiManager'>\n",
      "<java class 'ij.WindowManager'>\n"
     ]
    }
   ],
   "source": [
    "# demonstrate access to classes\n",
    "print(ij.IJ)\n",
    "print(ij.ResultsTable)\n",
    "print(ij.RoiManager)\n",
    "print(ij.WindowManager)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note the warnings! We're currently in headless mode. The many legacy ImageJ functions operate limitedly or not at all in headless mode. For example the `RoiManager` is not functional in a true headless enviornment."
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "ffb7cfab4b775ee54ab00d6ea209daa40cc054b7bb58fbad3b74bb1492b78b3c"
  },
  "kernelspec": {
   "display_name": "Python 3.8.10 ('pyijdev')",
   "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.8.10"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}