Skip to content

Simplify Python examples and use JupyterLab#129

Open
stellaprins wants to merge 22 commits into
mainfrom
sp/84-jupyter_nb
Open

Simplify Python examples and use JupyterLab#129
stellaprins wants to merge 22 commits into
mainfrom
sp/84-jupyter_nb

Conversation

@stellaprins

@stellaprins stellaprins commented Jun 8, 2026

Copy link
Copy Markdown
Collaborator
  • Updated the setup so the conda environment installs JupyterLab and ipywidgets dependency
  • Uses JupyterLab instead of saving a python script for instance segmentation workflow lesson. Closes Update the last episode to use jupyter notebook #84
  • Simplified the Python coding in the instance segmentation lesson by: using pandas instead of for-loops and numpy at the end, avoiding for loops except in 1 extra hard exercise, avoiding f-strings. Closes Make segmentation part friendlier for python novices #119
  • I've also updated the lesson to use erosion 5, 10, 15 (previously 1, 5, 10) so that over erosion can be demonstrated.
  • I updated some of the images, using layer screenshots instead of screenshots of the whole GUI (this will hopefully be more timeless)
  • Going through the lesson I slightly changed structure, adding a few headings and here and there slighlty altering the way exercises were framed.

I used RStudio to locally build the webpage, and fixed any unexpected issues with formatting.

@github-actions

github-actions Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Thank you!

Thank you for your pull request 😃

🤖 This automated message can help you check the rendered files in your submission for clarity. If you have any questions, please feel free to open an issue in {sandpaper}.

If you have files that automatically render output (e.g. R Markdown), then you should check for the following:

  • 🎯 correct output
  • 🖼️ correct figures
  • ❓ new warnings
  • ‼️ new errors

Rendered Changes

🔍 Inspect the changes: https://github.com/HealthBioscienceIDEAS/microscopy-novice/compare/md-outputs..md-outputs-PR-129

The following changes were observed in the rendered markdown documents:

 fig/eroded_ball_10.png (new)                       | Bin 0 -> 45857 bytes
 fig/eroded_ball_15.png (new)                       | Bin 0 -> 14523 bytes
 fig/eroded_ball_5.png (new)                        | Bin 0 -> 67873 bytes
 fig/instance_segmentation_clear_border.png         | Bin 159902 -> 70732 bytes
 fig/instance_segmentation_dilated.png (new)        | Bin 0 -> 154901 bytes
 fig/instance_segmentation_on_eroded_mask.png (new) | Bin 0 -> 48236 bytes
 fig/same_label_2d.png (new)                        | Bin 0 -> 169796 bytes
 fig/same_label_3d.png (new)                        | Bin 0 -> 850118 bytes
 instance-segmentation-and-measurements.md          | 810 +++++++++++----------
 md5sum.txt                                         |   4 +-
 setup.md                                           |   6 +
 11 files changed, 448 insertions(+), 372 deletions(-)
What does this mean?

If you have source files that require output and figures to be generated (e.g. R Markdown), then it is important to make sure the generated figures and output are reproducible.

This output provides a way for you to inspect the output in a diff-friendly manner so that it's easy to see the changes that occur due to new software versions or randomisation.

⏱️ Updated at 2026-06-12 17:13:54 +0000

github-actions Bot pushed a commit that referenced this pull request Jun 8, 2026
github-actions Bot pushed a commit that referenced this pull request Jun 8, 2026
github-actions Bot pushed a commit that referenced this pull request Jun 8, 2026
github-actions Bot pushed a commit that referenced this pull request Jun 8, 2026
github-actions Bot pushed a commit that referenced this pull request Jun 8, 2026
@stellaprins stellaprins marked this pull request as ready for review June 8, 2026 18:42
github-actions Bot pushed a commit that referenced this pull request Jun 9, 2026
github-actions Bot pushed a commit that referenced this pull request Jun 9, 2026

@thompson318 thompson318 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works really nicely, a big improvement on messing around with the Napari console. I've added a few minor suggestions (mostly typos I think).
I also think we should finish it with a note about saving/reusing/sharing the workflow.

Comment thread learners/setup.md
Comment thread episodes/instance-segmentation-and-measurements.md Outdated
Comment thread episodes/instance-segmentation-and-measurements.md Outdated
Comment thread episodes/instance-segmentation-and-measurements.md Outdated
Comment thread episodes/instance-segmentation-and-measurements.md Outdated
Comment thread episodes/instance-segmentation-and-measurements.md Outdated
Comment thread episodes/instance-segmentation-and-measurements.md Outdated
Comment thread episodes/instance-segmentation-and-measurements.md Outdated
Comment thread episodes/instance-segmentation-and-measurements.md Outdated
@K-Meech

K-Meech commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Thanks for putting this together @stellaprins - looks great! I think this will make it much easier to handle these code blocks in Jupyter + it's nice for learners to leave the course with a fully reproducible script 🎉

I'm part-way through reviewing the PR, but getting a bit side tracked with preparing notes for teaching tomorrow. Will hopefully submit a review by the end of today, but otherwise will finish reviewing on Friday after the course. It's already looking great for teaching this round 😄

@stellaprins

stellaprins commented Jun 9, 2026

Copy link
Copy Markdown
Collaborator Author

@thompson318 thanks! I've added your suggestions.

RE

I also think we should finish it with a note about saving/reusing/sharing the workflow.

Good idea. I was thinking this too, and have added the following section at the end.

Saving, reusing, and sharing your workflow

A key advantage of using a JupyterLab notebook is that your entire analysis is saved in one place. This makes your workflow reproducible, easy to adapt, and simple to share with others.

A tidy notebook is easier to understand for others (and for your future self).

Good practice includes:

  • Ensuring the notebook runs without issues from beginning to end.
  • Organising the notebook into clear sections (e.g. imports, loading data, segmentation, measurements, exporting results).
  • Removing unused cells and tidy temporary experiments.
  • Adding short notes explaining key steps and decisions.

Your notebook contains all the analysis steps, but it won’t run correctly unless the software environment is the same.

Export your conda environment so others can recreate it:

conda env export > environment.yml

To recreate the same software environment on another computer:

conda env create -f environment.yml

Read the conda documentation for more information.

Other good practices include:

  • Sharing your notebook on a collaborative platform such as GitHub or GitLab, where others can comment, discuss, or propose improvements.
  • Using version control (e.g., Git) to track changes, document improvements, and maintain a clear history of how your workflow evolves. This also gives you the ability to revert to earlier versions whenever a mistake happens, ensuring you never lose work in progress.

github-actions Bot pushed a commit that referenced this pull request Jun 9, 2026
Comment thread episodes/instance-segmentation-and-measurements.md Outdated
thompson318
github-actions Bot pushed a commit that referenced this pull request Jun 9, 2026

@thompson318 thompson318 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is good to merge if it makes teaching logistics easier, we can always make changes afterwards.

github-actions Bot pushed a commit that referenced this pull request Jun 11, 2026
github-actions Bot pushed a commit that referenced this pull request Jun 12, 2026

@K-Meech K-Meech left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for putting this together @stellaprins ! (sorry for the delay in getting back to you on this)

I've put some comments below - mostly minor suggestions on wording / typos. Take a look through and let me know what you think 😄

### 1. Activate your Napari environment
Open Miniforge Prompt and activate the environment you created for Napari:
``` bash
activate napari-env

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
activate napari-env
conda activate napari-env

activate napari-env
```
### 2. Create and navigate to your workshop folder
It is best practice to keep all your project files together in a dedicated folder.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we want something here about moving to a location they can find again (like /Documents, /Desktop) to create this folder? At least on windows miniforge prompt opens by default in: C:\Users\username which is tough to find again through the standard file browser.

Potentially, we could do this moving / folder creation via the jupyter interface to avoid multiple bash commands. E.g. if they open juptyer lab in the default location, they could then use the file browser at the left side of jupyter to move to a sensible location and create a new folder. What do you think?

Renaming your notebook immediately helps keep your workflow tidy and makes it easier to find later.

`File > Open Sample > napari builtins > Cells (3D + 2Ch)`
Right click the default name at the top of the notebook tab (e.g., *Untitled.ipynb*).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Right click the default name at the top of the notebook tab (e.g., *Untitled.ipynb*).
Right click the default name at the top of the notebook tab (e.g., *Untitled.ipynb*), and select `Rename Notebook...`.


For this workshop, we will only use **Code Cells**.

When you run a Code Cell, the output typically appears underneath it. This could be a number, text, a table, an error message.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
When you run a Code Cell, the output typically appears underneath it. This could be a number, text, a table, an error message.
When you run a Code Cell, the output typically appears underneath it. This could be a number, text, a table, or an error message.


First, let's open one of Napari's sample images with:
### 1. Activate your Napari environment
Open the Miniforge Prompt (or whichever other environment manager you used when installing Napari), and activate the environment you created for Napari:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Open the Miniforge Prompt (or whichever other environment manager you used when installing Napari), and activate the environment you created for Napari:
Open the terminal (the same one you used for Napari installation: see 'Opening a terminal' section of [the setup instructions](http://127.0.0.1:4321/index.html#install-python-packages)), and activate the environment you created for Napari:

Even when participants are using the same environment manager (miniforge), they will access it in different ways on different operating systems (miniforge prompt for windows, standard terminal for mac / linux etc). Maybe make this more generic by referring to a terminal + linking to the setup instructions?

```python
print(nucleus_pixels)
# Quick stats using pandas
props_df["volume_um3"].describe()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great shortcut for getting the summary stats! I'd forgotten about .describe

A tidy notebook is easier to understand for others (and for your future self).

```
Good practice include:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Good practice include:
Good practice includes:


- Organising the notebook into clear sections (e.g. imports, loading data, segmentation, measurements, exporting results).

- Removing unused cells and tidy temporary experiments.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Removing unused cells and tidy temporary experiments.
- Removing unused cells and tidying temporary experiments.


# Find the range of nucleus sizes (maximum - minimum).
print(f"Range of nucleus volumes = {nucleus_volume.max() - nucleus_volume.min():.2f} cubic micrometres.")
Your notebook contains all the analysis steps, but it won’t run correctly unless the software environment is the same.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good point, but I wonder if showing the conda env export commands is too much for this beginners lesson? Some points:

  • If we want to include this, we'd need to be clear about where to run it i.e. in the terminal once the notebook has been closed. (I imagine people will try running it in jupyter notebook)
  • These files often don't work for installation across different operating systems - as they may include platform-specific dependencies (e.g. if someone created it on windows, someone else probably couldn't use it to make the same env on a mac)
  • There is --from-history which can help with this; but it doesn't seem to work with our napari-env

Maybe remove the specific commands and just link to the documentation? What do you think?

@@ -1,3 +1,3 @@
---
title: 'Instance segmentation and measurements'
teaching: 45

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this adds more content, could you update the teaching time estimate here? It's in minutes

I'd probably go for at least 1hr teaching time, then maybe 20 minutes for exercises? That total of 1hr20 matches the size of the session after the lunch break which sounds about right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make segmentation part friendlier for python novices Update the last episode to use jupyter notebook

3 participants