🐍 Setting up Python for scientific image analysis#

Python can be used to process, analyze, and visualize your images. There are several ways of installing and setting up Python on your computer. If you have never used Python before, we recommend that you follow the steps below to get started.

Install Python using conda#

If you haven’t yet installed Python, Anaconda, or Miniconda on your machine, we recommend you install Miniconda which is based on the conda package manager. Click on the link below to download the installer:

Miniconda: https://docs.conda.io/en/latest/miniconda.html

Once you have downloaded the installer, run it to install conda.

  1. Run the executable file you just downloaded (Miniforge3-Windows-x86_64.exe) and follow the instructions.

  2. Launch the Anaconda Prompt terminal from the start menu.

../../../_images/anaconda_prompt.jpeg
  1. Open your Terminal (you can search for it in spotlight - cmd + space)

  2. Navigate to the folder you downloaded the installer to using cd. For example:

cd ~/Downloads
  1. Execute the installer with the command below. You can use your arrow keys to scroll up and down to read it/agree to it.

bash Miniforge3-MacOSX-x86_64.sh -b
  1. To verify that your installation worked, close your terminal window and open a new one. You should see (base) to the left of your prompt.

  2. Finally, initialize miniforge with the command below. This ensures that your terminal is set up correctly for your Python installation.

conda init

Verify your installation

Verify that you have conda installed by typing conda -V in your terminal. This should print out a version number.

Create a Python virtual environment#

A virtual environments lets you isolate the packages and dependencies that you need for a specific project. Using virtual environments will ensure that these dependencies do not create conflicts between the different Python projects you may be working on.

Type the following commands in your terminal to create a virtual environment (named project-env) using conda:

conda create -n project-env python=3.9

The -n parameter specifies the name of the virtual environment (here, project-env). We also specify the Python version to be 3.9. Python is constantly evolving and new versions are regularly released. At the time of writing, modern versions include 3.8 to 3.11.

Tip

Print a list of the virtual environments available on your machine by typing conda env list.

Activate your environment#

Let’s install a few Python packages into your project-env environment. To do that, you first have to activate the environment. Use the following command:

conda activate project-env

If you successfully activated the environment, you should see (project-env) to the left of your command prompt. To deactivate your environment and switch back to the (base) environment, you can use the conda deactivate command.

Install packages#

Once you have activated your environment, you can install packages in it. Many packages are available for scientific computing, image processing and analysis in Python. For example, take a look at the projects below:

You can install packages using your terminal and a package manager program. By default, Python includes a package manager called pip which lets you install packages from the official Python Package Index, also known as PyPI.

Let’s install Scikit-image in your project-env virtual environment. To do so, type the following command in your terminal:

pip install scikit-image

To check that your installation was successful, you can type pip list in your terminal to list all the packages installed in your environment. If you search for it, you should see scikit-image in the list!

Install Jupyter lab#

Jupyter lab is a powerful web appliation that you can use to edit and execute Python code. You can install it in your environment just like a regular Python package, using pip. Type the following command in your terminal:

pip install jupyterlab

Check your installation

Type jupyter lab in your terminal. This should start the Jupyter lab application in your web browser. To stop Jupyter lab, press Ctrl+C in your terminal window.

../../../_images/jlab-3.gif

One of the main features of Jupyter Lab is to enable viewing and editing Jupyter notebooks, which are interactive documents that combine code, visualizations, and narrative text, and are used by scientists to experiment with code and and demonstrate workflows.

Install Napari#

Napari is a multi-dimensional image viewer for Python. It is used to visualize scientific images and the data associated with them, including segmentation masks, bounding boxes, and keypoints.

With Napari, you can

  • visualize timeseries, 2D, 3D, and multi-channel images.

  • create interactive visualizations tailored to your needs.

  • set up visualizations in a Python script or a Jupyter notebook.

  • annotate data (draw masks, polygons, etc.).

  • use plugins from the community or develop and share your own plugin.

We recommend that you install Napari by typing the following command:

pip install "napari[all]"

Check your installation

With your virtual environment activated, type napari in your terminal. The Napari viewer should open in a separate window.

../../../_images/napari_terminal.gif

You can also find the official Napari installation instructions here.

Install a code editor#

Code editors provide many useful features, including syntax highlighting, a file system manager, integrated terminals, and code auto-completion. You can also interact with Jupyter notebooks directly in your code editor instead of using your web browser.

We recommend that you try and pick one of the code editors below.

Summary#

Setting up Python for working on your image analysis project typically involves the following steps.

  • Installing Python, which you can do (for example) via conda, which you can obtain by installing Miniconda.

  • Creating a virtual environment for your project.

  • Installing packages in your virtual environment using pip.

  • Installing a program to develop code, such as Jupyter lab or a code editor.