Skip to content

Commit 27d5781

Browse files
authored
Merge pull request #338 from imagej/colab
Create initial Colab notebook for learning. Supporting #333
2 parents 194dba5 + 24e548e commit 27d5781

21 files changed

+3046
-10
lines changed

.github/scripts/update_notebook.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ def scan_ruleset_files(rulesets_dir: Path) -> Dict[str, str]:
5151
for file_path in sorted(env_dir.glob("env_*.md")):
5252
env_name = file_path.stem
5353
if env_name.startswith("env_"):
54-
# Convert env_colab -> Google Colab, env_script_editor -> Fiji Script Editor, etc.
54+
# Convert env_colab -> Google Colab, env_scripting -> Fiji Script Editor, etc.
5555
display_name = format_environment_name(env_name[4:]) # Remove "env_" prefix
56-
environment_mapping[display_name] = env_name
56+
# Store the full relative path from rulesets directory
57+
relative_path = f"environments/{env_name}"
58+
environment_mapping[display_name] = relative_path
5759

5860
return environment_mapping
5961

@@ -64,7 +66,7 @@ def format_environment_name(env_key: str) -> str:
6466
"colab": "Google Colab",
6567
"interactive": "Interactive Desktop",
6668
"headless": "True Headless",
67-
"script_editor": "Fiji Script Editor"
69+
"scripteditor": "Fiji Script Editor"
6870
}
6971
return mapping.get(env_key, env_key.replace("_", " ").title())
7072

.github/templates/personalize_gemini_cell.py.j2

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ experience_mapping = {
2828
{% endfor %}
2929
}
3030

31-
# Template file mapping for different experience categories
31+
# Activity file mapping for different experience categories
3232
activity_dir = 'activities/'
3333

3434
{% for category, mapping in category_mappings.items() %}
@@ -64,12 +64,12 @@ def load_persona_file(filename):
6464
section_name = base_name.replace('_', ' ').upper()
6565
return f"\n===== START OF {section_name} =====\n{content}\n===== END OF {section_name} =====\n"
6666
else:
67-
return f"# Template not found: {filename}\n(Using basic fallback)"
67+
return f"# Persona file not found: {filename}\n(Using basic fallback)"
6868
except Exception as e:
6969
return f"# Error loading {filename}: {e}\n(Using basic fallback)"
7070

7171
# Load base persona with section markers
72-
persona_text += load_persona_file('base_persona.md')
72+
persona_text = load_persona_file('base_persona.md')
7373

7474
# Get experience levels
7575
{% for category in categories.keys() %}

.github/templates/set_coding_rules_cell.py.j2

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ def load_ruleset_template(filename):
4949
return f"# Error loading {filename}: {e}\n(Using basic fallback)"
5050

5151
# Load core PyImageJ ruleset with section markers
52-
ruleset_text += load_ruleset_template('pyimagej_core.md')
52+
ruleset_text = load_ruleset_template('pyimagej_core.md')
5353

5454
# Get environment-specific ruleset with section markers
55-
env_key = environment_mapping[environment]
56-
env_filename = f"{env_key}.md"
55+
env_val = environment_mapping[environment]
56+
# Strip any leading subdirectories from the path
57+
env_filename = f"{env_val}.md"
5758
ruleset_text += load_ruleset_template(env_filename)
5859

5960
# Register the rulesets with the LLM

.github/workflows/update-notebook.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ jobs:
9191
fi
9292
9393
git add doc/llms/pyimagej-ai-guide.ipynb
94-
git commit -m "${COMMIT_PREFIX} notebook cells based on personas/rulesets changes
94+
git commit -m "${COMMIT_PREFIX} notebook cells
9595
9696
Updated from commit: ${{ github.sha }}"
9797
git push

doc/llms/README.md

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# PyImageJ LLM Support
2+
3+
This directory contains a complete framework for learning PyImageJ with Large Language Models (LLMs) like Gemini, ChatGPT, and Claude. The system uses modular persona and ruleset files to customize AI responses for different experience levels and environments.
4+
5+
## 🚀 Quick Start with Google Colab
6+
7+
The easiest way to get started is with our interactive Colab notebook:
8+
9+
**➡️ [Open PyImageJ AI Guide in Colab](pyimagej-ai-guide.ipynb)**
10+
11+
This notebook will:
12+
1. 📦 Download PyImageJ source and AI templates
13+
2. 🤖 Configure Gemini based on your experience level
14+
3. ⚖️ Set coding rules for your target environment
15+
4. 🏝️ Set up a complete Fiji bundle for analysis
16+
5. 🚀 Guide you through personalized learning activities
17+
18+
The setup takes ~2-3 minutes on first run, then you're ready to learn with AI assistance!
19+
20+
### Secondary Colab Notebook Uses
21+
22+
* As this notebook contains robust PyImageJ initialization, coupled with extensive PyImageJ LLM rulesets, it can be used as a starting point for notebook development even by advanced developers
23+
24+
## 📁 Framework Components
25+
26+
### `pyimagej-ai-guide.ipynb`
27+
Interactive Colab notebook that orchestrates the entire learning experience. Features:
28+
- Experience-based persona selection (beginner/intermediate/advanced)
29+
- Environment-specific coding rules (Colab/Desktop/Headless/Script Editor)
30+
- Notebook-safe Fiji setup and PyImageJ integration
31+
- Easy transfer of persona text to other LLMs
32+
33+
### `personas/` Directory
34+
Modular AI personality guides which can be combined and used to establish tailored persona and training roles.
35+
36+
- **`base_persona.md`** - Core PyImageJ teaching approach
37+
- **Activity Files** - Experience-specific learning paths:
38+
- `activities/coding_*.md` - Core programming concepts
39+
- `activities/pyimagej_*.md` - Image procesing with PyImageJ
40+
- `activities/colab_*.md` - Useful Google Colab skills
41+
42+
### `rulesets/` Directory
43+
Technical guidelines for how AI should write PyImageJ code. Select the ruleset appropriate for the environment you will be running your code.
44+
45+
- **`pyimagej_core.md`** - Universal PyImageJ best practices
46+
- **Environment-specific rules** in `environments/` subdirectory:
47+
- `environments/env_colab.md` - Google Colab considerations
48+
- `environments/env_interactive.md` - Desktop GUI environments
49+
- `environments/env_headless.md` - Server/cluster environments
50+
- `environments/env_scripteditor.md` - Fiji Script Editor integration
51+
52+
## 🔧 Using with Other LLMs
53+
54+
While designed for Gemini in Colab, you can use these files with any LLM:
55+
56+
### With ChatGPT, Claude, etc.
57+
58+
1. **Run the Colab notebook** to generate your personalized content
59+
2. **Copy the output** using the provided copy buttons ("📋 Copy AI Configuration for Other LLMs" cell)
60+
3. **Paste into your preferred LLM** as context
61+
62+
Or manually combine files:
63+
64+
```bash
65+
# Example: Advanced PyImageJ user in headless environment
66+
cat personas/base_persona.md \
67+
personas/activities/pyimagej_advanced.md \
68+
rulesets/pyimagej_core.md \
69+
rulesets/environments/env_headless.md > my_context.md
70+
```
71+
72+
### Prompt Template
73+
```
74+
I'm learning PyImageJ for scientific image analysis. Please use this context to guide our conversation:
75+
76+
[PASTE PERSONA/RULESET CONTENT HERE]
77+
78+
I'm ready to start! What activities do you recommend based on my experience level?
79+
```
80+
81+
## 🤝 Contributing
82+
83+
To extend this framework:
84+
85+
- **Add new personas** in `personas/activities/` using the naming pattern `{category}_{level}.md` (e.g., `statistics_beginner.md`)
86+
- **Create environment rules** in `rulesets/environments/` using the pattern `env_{environment}.md` (e.g., `env_docker.md`)
87+
- **Update the notebook** to fix bugs, improve the documentation, or add more features
88+
89+
### 🔄 Automated Updates
90+
91+
The framework includes GitHub Actions automation to keep the notebook synchronized with persona and ruleset files:
92+
93+
1. **Auto-detection**: GitHub Action triggers on commits to `main` that modify `personas/` or `rulesets/` files
94+
2. **Smart updates**: Uses Jinja templates to regenerate the "Personalize Gemini" and "Set Coding Rules" cells
95+
3. **Commit tracking**: Updates the download cell to use the exact commit SHA that triggered the update
96+
4. **Zero maintenance**: Persona dropdowns and environment options automatically reflect new files
97+
5. **Branch safety**: Builds on development branches must be triggered manually. The commit is generated as a `WIP`, which is blocked from merging by PR
98+
99+
#### File naming requirements
100+
- **Personas**: `personas/activities/{category}_{level}.md` where level is `beginner`, `intermediate`, or `advanced`
101+
- You can create just one level (e.g., only `statistics_beginner.md`) or any combination
102+
- Missing levels are automatically skipped - no need to create all three
103+
- **Rulesets**: `rulesets/environments/env_{environment}.md` for new target environments
104+
- **Ordering**: Categories appear in order: `colab``coding``pyimagej` → (new categories alphabetically)
105+
106+
#### To add new categories
107+
1. Create activity files: `newcategory_beginner.md` (and optionally `_intermediate.md`, `_advanced.md`)
108+
2. Commit to trigger the automation
109+
3. The notebook will automatically include the new category with only the levels you created
110+
111+
#### Template locations
112+
- Automation scripts: `.github/workflows/update-notebook.yml`
113+
- Jinja templates: `.github/templates/`
114+
- Update logic: `.github/scripts/update_notebook.py`
115+
116+
### 🌿 Branch Development Workflow
117+
**Automatic updates only run on `main`** to keep the main branch history clean. For development:
118+
119+
1. Edit persona/ruleset files on your feature branch
120+
2. Manually test automation by going to **Actions****Update PyImageJ AI Guide Notebook****Run workflow** → select your branch
121+
3. This creates `WIP: Auto-update...` commits on your branch (clearly marked as temporary)
122+
4. Continue development normally
123+
124+
##### Running the automation manually
125+
You can also manually apply the `update_notebook` script from the repository root:
126+
127+
```bash
128+
COMMIT_SHA={commit} BRANCH_NAME={branch} python .github/scripts/update_notebook.py
129+
```
130+
131+
#### Merging to main
132+
1. Before creating a PR, ensure no `WIP:` commits will be merged:
133+
- Option A: Rebase/squash to remove WIP commits
134+
- Option B: Manually change commit messages to remove `WIP:` prefix
135+
2. Create PR to `main` - the **Check for WIP commits** workflow will verify no temporary commits remain
136+
3. After merge, automation runs automatically and creates a clean `Auto-update...` commit on `main`
137+
138+
### Emergency options
139+
- Add `[skip-automation]` to any commit message to prevent automation
140+
- Use `workflow_dispatch` with "force update" to regenerate the notebook anytime
141+
142+
## 📚 Learn More
143+
- [ImageJ Community Forum](https://forum.image.sc/)
144+
- [PyImageJ Documentation](https://pyimagej.readthedocs.io/)

0 commit comments

Comments
 (0)