{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook is part of the PyImageJ [Tutorial Series](./notebooks.rst), and assumes familiarity with the ImageJ API. Dedicated tutorials for ImageJ can be found [here](https://imagej.net/tutorials/)."
   ]
  },
  {
   "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 \"convenience 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 | [06-Working-with-Images](06-Working-with-Images.ipynb)\n",
    "| `to_java` | Convert data from Python to Java | [03-Sending-Data-to-Java](03-Sending-Data-to-Java.ipynb) |\n",
    "| `from_java` | Convert data from Java to Python | [04-Retrieving-Data-from-Java](04-Retrieving-Data-from-Java.ipynb) |\n",
    "| `run_macro` | Run an original ImageJ macro | [07-Running-Macros-Scripts-and-Plugins](07-Running-Macros-Scripts-and-Plugins.ipynb) |\n",
    "| `run_script` | Run an ImageJ script (supported languages) | [07-Running-Macros-Scripts-and-Plugins](07-Running-Macros-Scripts-and-Plugins.ipynb) |\n",
    "| `run_plugin` | Run a plugin | [07-Running-Macros-Scripts-and-Plugins](07-Running-Macros-Scripts-and-Plugins.ipynb) |\n",
    "| `initialize_numpy_image` | Create a new numpy image in the same shape as input image | [06-Working-with-Images](06-Working-with-Images.ipynb) |\n",
    "| `sync_image` | Synchronize data between ImageJ and ImageJ2 data structures | -- |\n",
    "| `active_dataset` | Get the active image as a `Dataset` | -- |\n",
    "| `active_xarray` | Get a copy of the active image as an `xarray.DataArray` | -- |\n",
    "| `active_imageplus` | Get the `ImagePlus` from the current window | -- |"
   ]
  },
  {
   "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": [
      "Operating in headless mode - the original ImageJ will have limited functionality.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ImageJ2 version: 2.9.0/1.53t\n",
      "Legacy layer active: True\n"
     ]
    }
   ],
   "source": [
    "import imagej\n",
    "\n",
    "# initialize imagej\n",
    "ij = imagej.init()\n",
    "print(f\"ImageJ2 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": [
      "Operating in headless mode - the IJ class will not be fully functional.\n",
      "Operating in headless mode - the ResultsTable class will not be fully functional.\n",
      "Operating in headless mode - the RoiManager class will not be fully functional.\n",
      "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": {
  "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.10.6"
  },
  "vscode": {
   "interpreter": {
    "hash": "fd4de699765e9fab70e2644720b91b55c1a435ebb41ccdac66a2b7a412168f61"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}