Setup¶
Python is a popular language for scientific computing, and a frequent choice for machine learning as well.
This workshop will use a Python environment containing JupyterLab and the necessary dependencies as the primary interface for all practical sessions. You are encouraged to follow the local installation option.
Please set up your Python environment at least a day in advance of the workshop. If you encounter problems with the installation procedure, attend the pre-workshop setup session for assistance, so that you are ready to go as soon as the workshop begins.
Local installation¶
Important
You have different options:
Use pip and virtual environments: If you have
gitin your terminal and are comfortable using virtual environments and have a working Python interpreter (at least version 3.10 or above), orUse Miniforge / pixi and conda environments: Recommended for Windows users, and users who don’t have tools like a Python interpreter,
gitin their devices.
Once you have done either, proceed to getting the notebooks. Finally download the datasets
Using pip¶
Open a terminal (Mac/Linux) or Command Prompt (Windows) and run the following commands.
Create a virtual environment called
venv-xai-for-ls:
python3 -m venv venv-xai-for-ls
Activate the newly created virtual environment:
source venv-xai-for-ls/bin/activate
venv-xai-for-ls\Scripts\activate
Remember that you need to activate your environment every time you restart your terminal!
Install the required packages:
Once you have activated your environment, you can install all the required packages for this workshop using the following command:
pip install -r https://raw.githubusercontent.com/sweden-ai-factory/xai-for-life-science/refs/heads/main/requirements.txt
Using Miniforge¶
In this page, we give indications about how to install Miniforge and use conda/mamba.
The first step is to install Miniforge, which is a modified version of Miniconda using by default the community driven conda-forge channel.
Download the installer using curl or wget or your favorite program. For eg:
wget "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh"
or
curl -L -O "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh"
Execute the installer and initialize conda:
bash Miniforge3-$(uname)-$(uname -m).sh -b
$HOME/miniforge3/bin/conda init --all
On Windows, download the Windows installer and follow the instructions given in https://github.com/conda-forge/miniforge.
Open an Anaconda Prompt and run conda init --all.
When it’s done, open a new terminal (click on ctrl-alt-t) and check that the
line in the new terminal starts with (base). The indication (base) means
that you use the base “environment”.
However, working in the base environment is not a good practice and it is
better to disable the auto activation with:
conda config --set auto_activate_base false
Tip
If you have a GPU, you might want to comment pytorch and uncomment pytorch-gpu in the
environment.yaml file.
Create an environment called
xai-for-ls:
conda env create --yes -f environment.yaml
Activate the environment:
conda activate xai-for-ls
Using pixi¶
Install Pixi by following the installation instructions.
Download https://raw.githubusercontent.com/sweden-ai-factory/xai-for-life-science/refs/heads/main/pixi.toml, and use one the below commands
pixi shell # If you don't have a GPU
pixi shell -e gpu # If you have a GPU
Use either pixi shell to enter the environment,
or prefix commands with pixi run.
Getting the Notebooks¶
Once you have git installed, enter the following command into your terminal:
git clone https://github.com/sweden-ai-factory/xai-for-life-science
The repository will be cloned to a directory named xai-for-life-science
which will also contain the notebooks. If you want to get the latest copy,
you update this repository by using
cd xai-for-life-science
git pull
Launch JupyterLab¶
From within your virtual environment (using activate script) or conda
environment (using conda activate xai-for-ls / pixi shell) navigate to the
notebooks directory and launch JupyterLab
cd xai-for-life-science/notebooks
jupyter-lab
which should also open JupyterLab.
Checking Your Environment¶
Once in, you can verify your setup by running:
from importlib.metadata import version
import torch
print("Accelerator available:", torch.accelerator.is_available())
print("GPU device:", torch.accelerator.current_accelerator() if torch.accelerator.is_available() else None)
print("PyTorch version:", torch.__version__)
import pytorch_grad_cam
print("PyTorch Grad-CAM version:", version("grad-cam"))
import shap
print("SHAP version:", shap.__version__)
import captum
print("Captum version:", captum.__version__)
You should get something similar as follows
Output
Accelerator available: True
GPU device: cuda
PyTorch version: 2.13.0
PyTorch Grad-CAM version: 1.5.7
SHAP version: 0.52.0
Captum version: 0.9.0