Summary and Setup
This image analysis course provides an introduction to basic image analysis techniques in two environments: Python and Napari.
These two environments have different interfaces for user interaction, but both offer powerful tools for image processing and analysis. Both are discussed here in order to highlight the relative strengths of each one, and to present the analysis techniques in this course as generic and independent of one particular platform.
The first section on Python consists of working in a Jupyter notebook setting, but can equally be done in the terminal. This way of working also lends itself quite well to adapting your analysis process into a workflow that can be applied to a batch of many images.
Napari is a point-and-click graphical user interface, which is well-suited to exploring and previewing images, and is particularly useful for viewing multi-dimensional data such as 3D Z-stacks and time series. Incidentally, Napari is built on top of Python, using many of the same tools available in a Jupyter or terminal setting. The interactive nature of Napari makes it less easy to adapt to batch processing of many images, but it does have ways of exporting the processing steps performed in the GUI to a text file or even a Jupyter notebook, that can then be adapted for your own purposes.
This course will cover a range of basic filtering and segmentation techniques in Jupyter, and then the final chapter will explore equivalents of these in Napari.
Two main sources were used for building this course:
Building and publishing was done with the Carpentries Workbench and GitHub Pages.
Part one of this course requires access to some kind of Python environment, which may be any of:
- A plain Python interpreter on the command line
- An IDE, e.g. PyCharm, Visual Studio Code
- A managed JupyterHub instance at your organisation
- For courses taught at the University of Edinburgh, staff and students have access to Noteable at https://noteable.edina.ac.uk/login. If you wish to use this, log in here and proceed to GUI setup or command line setup below.
- A JupyterLab instance on your local machine
For part 2 on Napari, you’ll need access to a machine with Napari installed. Installation of Napari is covered below.
Datasets
Discussion
Test data is available in the GitHub repo. These can be downloaded either by visiting each image and selecting ‘Download raw file’, or using a command line utility such as wget, as below.
The test images can be downloaded in fewer clicks with the command:
SH
wget https://raw.githubusercontent.com/IGC-Advanced-Imaging-Resource/image-analysis-python-napari/main/data/FluorescentCells_3channel.tif \
https://raw.githubusercontent.com/IGC-Advanced-Imaging-Resource/image-analysis-python-napari/main/data/confocal-series_zstack.tif \
https://raw.githubusercontent.com/IGC-Advanced-Imaging-Resource/image-analysis-python-napari/main/data/hela-cells_rgb.tif \
https://raw.githubusercontent.com/IGC-Advanced-Imaging-Resource/image-analysis-python-napari/main/data/Ersi_organoid_WT2.nd2
Python Setup
Details
We recommend Jupyter as a powerful way of saving your progress and displaying your work in Python. All setup done here is based on either Conda or pip with Python virtual environments.
Once logged in, select ‘New -> Folder’ in the top right menu. This will create a new folder called ‘Untitled’. To rename it, check the box next to it, select ‘Rename’ in the top left and enter a suitable name for the folder. Navigate into it by selecting its name in the navigator.
Now do the same steps again to create a folder called ‘test_data’ and navigate into it.
Now that you’re in a sensible location, you can use the ‘Upload’ button in the top right to select and upload your downloaded image files from GitHub.
In the folder you’ve created, select ‘New’ -> ‘Python 3 (ipykernel)’. This will open a Jupyter notebook in a new browser tab.
Once logged in, select ‘New -> Terminal’ in the top right. This will open a command line shell in a new tab. Once here:
- Use
mkdir
to create a folder for this course andcd
into it - Inside here, use
mkdir
to create a ‘test_data’ folder andcd
into it - Use
wget
to download the test data
Now that the folders and test data are set up, navigate to your new course folder in the GUI and select ‘New -> ’Python 3 (ipykernel)’. This will open a Jupyter notebook in a new browser tab.
If you don’t have access to a JupyterHub instance but still want to use Jupyter, you can install it locally either with Conda:
Or with pip:
Once installed, start Jupyter with:
This will start the server process and open JupyterLab in a new browser window. Follow the same GUI or graphical folder setup as for JupyterHub above.
You may need to install some extra packages, such as skimage. To do this, exit Jupyter and in the same terminal window, run:
Or in Conda:
If you wish to use Python on its own, then that is also possible. You will also need the packages skimage, imageio, numpy and matplotlib.
When working with a local Python instance, it’s almost always a good
idea to install packages into a virtual
environment such as those created by Conda or Python’s
venv
module:
SH
$ python -m venv ./python_environment
$ source ./python_environment/bin/activate
$ pip install skimage imageio numpy matplotlib
Then to start up Python:
Napari setup
Details
While Napari has developed enormously in recent years, installing it still requires the use of Pip and/or Conda. See below for installation on Unix-like systems. The process should take a few minutes to run.
Pip is Python’s package manager. To install Napari into a Python virtual environment:
$ python -m venv path/to/new/virtualenv
$ source path/to/new/virtualenv/bin/activate
$ pip install 'napari[all]'
A Conda environment containing Napari can be created with:
$ conda create -p path/to/new/env -c conda-forge -c pytorch devbio-napari pyqt
You may find that solving the environment is extrememly slow or fails altogether. Up-to-date Conda setups are able to use a much faster solver, Mamba:
$ conda list -n base mamba # make sure that conda-libmamba-solver is present in the base environment
$ conda install -n base conda-libmamba-solver # install the Mamba solver if needed
$ conda config --show solver # check the configured solver
$ config config --set solver libmamba # if the confgured solver is 'classic'
This assumes that you already have Anaconda installed on your system. There should be an ‘Anaconda Prompt’ option in the start menu. Select this to open a terminal session with Conda loaded, and run:
SH
> conda create --name devbio-napari napari=0.4.19 python=3.9 devbio-napari pyqt -c conda-forge -c pytorch
You can then activate the environment and run Napari from the terminal window: