|
| 1 | +# Google Colab Notebook |
| 2 | + |
| 3 | +This document is designed to supplement your use of our PyImageJ + Large Language Model (LLM) Google Colab Notebook: |
| 4 | + |
| 5 | +<!-- UPDATE: pin to particular commit --> |
| 6 | +<a href="https://colab.research.google.com/github/imagej/pyimagej/blob/main/doc/llms/pyimagej-ai-guide.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a> |
| 7 | + |
| 8 | +## Background Knowledge |
| 9 | + |
| 10 | +If any of these topics are new to you: |
| 11 | + * Colab → check out [our overview](Google-Colab-Basics.md) |
| 12 | + * LLMs → read through [prompt engineering basics](Prompt-Engineering.md) |
| 13 | + * Python → browse an overview, like [W3 schools](https://www.w3schools.com/python/) |
| 14 | + |
| 15 | +## Learning Goals |
| 16 | + |
| 17 | +For doing science, ultimately we want to code in a **reproducible** environment (*which Colab is not*). We imagine the progression of a new learner might proceed as: |
| 18 | + |
| 19 | +- First, use our AI Notebook to experiment with PyImageJ until you are comfortable with the concepts and syntax. |
| 20 | +- From there, you can transition to the creating Python workflows in the [Fiji Script Editor](https://imagej.net/scripting/script-editor). |
| 21 | +- Alternatively, you *can* run Jupyter Notebooks locally. If this interests you, look at tools like [uv](https://docs.astral.sh/uv/) to [set up](Install.md) PyImageJ environments. |
| 22 | +- If you find that you love programming, you could also explore the [developer ecosystem](https://imagej.net/develop/)! Tools like GitHub Copilot, Claude Code and cursor bring exceptional new directions to ImageJ plugin development. |
| 23 | + |
| 24 | +💡**Tip**: LLMs can provide guidance in all of these tasks! But so can humans - please reach out on the [Image.sc forum](https://forum.image.sc/) if you need support! |
| 25 | + |
| 26 | +Also note that if you enjoy Gemini and Colab, you can certainly use the environment to *create* Python code that will run in the Script Editor (for example). Requirements and syntax may change a bit between environments though, so typically it is easiest to develop code in the environment where it will run. |
| 27 | + |
| 28 | +Fundamentally, **the goal of this Notebook** is to support you in building the skills and confidence that you can adapt to any of these environments, based on your own analysis needs. |
| 29 | + |
| 30 | +## Navigating the Notebook |
| 31 | + |
| 32 | +### Initialization |
| 33 | + |
| 34 | +All that's needed to start up the notebook is to click the `Run All` button in the toolbar: |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +This will do several things, which unfortunately can take a few minutes. You'll see a progress bar with status indicators as the set up functions are running: |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +Here's an overview of key steps: |
| 43 | + |
| 44 | +* **Downloading PyImageJ source** from the [GitHub repository](https://github.com/imagej/pyimagej) is necessary because it contains the [rules and persona files](https://github.com/imagej/pyimagej/tree/main/doc/llms) to teach Gemini. |
| 45 | +* **Customizing Gemini** this way allows the notebook to grow over time: we can revise these support files to bring new behavior to Gemini. |
| 46 | +* **Downloading a Fiji bundle** provides PyImageJ access to all the plugins in a base Fiji download. |
| 47 | +* **Initializing PyImageJ** starts up a running instance of Fiji, which you can then access through the `ij` variable. |
| 48 | +* ⚠️**Warning:** The only verified method of [initialization](Initialization.md) in Google Colab are using local bundles from [fiji-builds](https://github.com/fiji/fiji-builds/releases) ⚠️ |
| 49 | + |
| 50 | +Once the progress bar status shows `✅ Setup complete!`, your notebook is ready to go! You can start coding in the blank code cell at the bottom of the notebook, or start a conversation with Gemini and describe what you'd like to accomplish or learn. |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +### Making space |
| 55 | + |
| 56 | +The AI Notebook is big. We've tried to keep it concise, but for example the `🤖 Customize Gemini` step **must** print all of its output text (which is a lot!) so that Gemini can actually read it. |
| 57 | + |
| 58 | +However, if you want to make things cleaner, you *can* collapse the `⚙️ Notebook Configuration` and/or `🔍 Utilities` section(s): |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +Additionally, you can safely hide the output of individual cells using the `Output actions` button (which appears after running code cells): |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +### Modifying the Notebook Itself |
| 67 | + |
| 68 | +In this notebook we make extensive use of [Colab's forms](https://colab.research.google.com/notebooks/forms.ipynb), in large part to keep the code hidden. |
| 69 | + |
| 70 | +If you want to dig into the internals of the notebook itself, any cell with a `Show code` button at the bottom can be toggled with the `>` at the top-left of that cell: |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +The following cells have <span style="color: green;"># --UPDATE</span> comments marking areas with simple edits to influence Notebook reproducibility: |
| 75 | + |
| 76 | +* `📦 Download PyImageJ Source`: specify [the commit](https://github.com/imagej/pyimagej) for the LLM instruction files. |
| 77 | +* `🏝️ Setup Fiji Bundle`: specify [the bundle ID](https://github.com/fiji/fiji-builds/releases) for the Fiji download. |
| 78 | + |
| 79 | +## Troubleshooting |
| 80 | + |
| 81 | +### Failures during setup |
| 82 | + |
| 83 | +The setup generates *a lot* of output, which we hide by default to avoid overwhelming new users and cluttering the notebook. |
| 84 | + |
| 85 | +If something goes wrong, the `⚙️ Utilities` section has tools to help. |
| 86 | + |
| 87 | +First, you can check the `✅ Verify Setup` step to see if there are PyImageJ problems. |
| 88 | + |
| 89 | +If you need more information, you can use the `🛠️ Troubleshooting` cell to view a step's hidden output: |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | +### Gemini didn't pick up the personality text |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | +If you start communicating with Gemini *before loading* the the instructional + personality text, Gemini may not see it properly. |
| 99 | + |
| 100 | +The easiest thing to do is reload the page and re-run the notebook. |
| 101 | + |
| 102 | +## FAQ |
| 103 | + |
| 104 | +### Why a Colab Notebook? |
| 105 | + |
| 106 | +This came from a desire to build image analysis workflows using ImageJ in an environment where a LLM assistant has direct contextual access. |
| 107 | + |
| 108 | +Although a first-party LLM assistant in Fiji itself would be ideal, that requires significantly more development and maintenance time. |
| 109 | + |
| 110 | +With Colab + PyImageJ we take advantage of existing tools, and have the added benefit of access to the Python scientific software stack (e.g. [Cellpose](https://www.cellpose.org/)). |
| 111 | + |
| 112 | +### But this notebook looks complicated! |
| 113 | + |
| 114 | +Setting up PyImageJ previously required understanding of developer-focused tools. This notebook removes any configuration and installation barriers, allowing you to get right into image analysis. |
| 115 | + |
| 116 | +There were also some tricky setup challenges due to the fact that notebooks are running on Google's remote computers, and not your local system. Luckily, we've already solved them for you! |
| 117 | + |
| 118 | +Because of the general scope of this notebook, there is a lot of explanatory text. If something is still confusing or overwhelming or just not useful, that is important for us to know! Please [get in touch](https://forum.image.sc/tag/pyimagej) and let us know what we can do better. |
| 119 | + |
| 120 | +### Are there any other reasons to use this notebook? |
| 121 | + |
| 122 | +We are developing instruction sets for LLMs to help them produce better code. Ideally these should be tailored to the environment where the code will be running (e.g. in a notebook, or the Fiji script editor). These are targeted rules that general LLMs wouldn't have access to. |
| 123 | + |
| 124 | +Using the AI Notebook gives you access to these instruction sets. We are also relying on users like you to provide feedback when things go wrong! That is the only way for these rulesets to grow. |
| 125 | + |
| 126 | +### How do I use these instructions with other LLMs? |
| 127 | + |
| 128 | +After running the notebook, there is a `📋 Copy AI Instructions` cell in the `⚙️ Utilities` section. Just click that button! Then you can paste the instructions at the start of a conversation with any other LLM. |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | +### I thought an "AI Notebook" would use more... AI |
| 133 | + |
| 134 | +While AI-based analysis techniques are very exciting, they are massively overpowered for most image analysis needs. |
| 135 | + |
| 136 | +The goal of using an integrated "AI assistant" is precisely to **find** the appropriate technique for your needs. Maybe that will require another AI tool, but often it won't! |
| 137 | + |
| 138 | +### Aren't Colab notebooks "headless"? |
| 139 | + |
| 140 | +Technically yes - as they are running on remote (not local) computers, we don't have access to a display/monitor (and are thus "headless"). |
| 141 | + |
| 142 | +This is relevant because many ImageJ 1.x plugins were developed with an implicit assumption of an active display. |
| 143 | + |
| 144 | +However, the AI notebook is set up to "fake" a display: this allows many ImageJ 1.x functions to work correctly, when they otherwise would have failed. (you may still run into some things that simply can't work, though) |
| 145 | + |
| 146 | +Note that this requirement was specifically decoupled from ImageJ2 plugins, which should work as intended. |
0 commit comments