← Lab 5

Colab has been hiding something

Every notebook this term started with import numpy and it simply worked. That was not Python being helpful. That was Google having already installed it for you.

Colab
# in Colab — this just works
import numpy as np
your machine, five minutes from now
# on your laptop — this does not
import numpy as np

ModuleNotFoundError: No module named 'numpy'
Python ships with very little

The standard library gave you pathlib, csv, random. Everything else — numpy, pandas, matplotlib — someone else wrote, and you have to go and get.

Today: get it yourself

One command builds a project with everything a data analysis needs. Then this week's notebooks, running in VS Code, on your own machine.

Colab pre-installs several hundred packages. Convenient — until you need to explain to someone else exactly what your analysis depends on.

Where packages come from — and where they go

numpy lives on PyPI, the Python Package Index. Getting it means downloading it — and the only real question is where to put it.

download → install
pypi.org                    600,000 packages, free to download
   │
   │   uv downloads numpy, pandas, matplotlib …
   ▼
my-project/.venv/           a private copy of Python
                            + exactly those packages
three things every project has
pyproject.toml   the list:  "numpy, pandas, matplotlib"
uv.lock          the exact versions:  numpy 2.5.4 …
.venv/           the packages themselves
One shared pile

Your dissertation needs numpy 1.26, a new tool needs 2.5. Install both for the whole machine and one of them breaks — usually the one you are not looking at.

One pile per project

Each project folder gets its own .venv — a virtual environment. They cannot see each other, so they cannot break each other.

pyproject.toml + uv.lock let a collaborator — or you-in-two-years — rebuild exactly your environment with one command.

One command, a whole project

uv-start is a small tool that builds a ready-made data analysis project. Install it once, then use it for every project you start.

Terminal (Mac) · PowerShell (Windows)
# 1. bring uv up to date
you@mac ~ %uv self update
success: Upgraded uv to v0.13.x
# 2. install uv-start, once per computer
you@mac ~ %uv tool install uv-start
Installed 1 executable: uv-start
# 3. build the project
you@mac ~ %uv-start lab5-neurons --data
Data project template files copied successfully.
A tool for the whole computer

uv tool install gives uv-start its own environment and puts the command where every terminal finds it. Packages like numpy are different: they go in each project's .venv.uv tool list shows your tools · uv tool upgrade uv-start updates one.

What it does for you

Makes the folder, a pyproject.toml, a .venv with numpy, pandas, matplotlib and seaborn, and VS Code settings for notebooks. By hand that is a dozen commands.

Run step 3 from your home folder — wherever a fresh terminal opens. The project appears as a new folder called lab5-neurons. Only need it once? uvx uv-start@latest lab5-neurons --data runs it without installing.

What you got

lab5-neurons
lab5-neurons/
├── pyproject.toml   the list of packages
├── uv.lock          their exact versions
├── .venv/           the packages themselves
├── data/            raw data — read it, never overwrite it
├── results/         figures and tables you make
├── sample.ipynb     a notebook to test the setup
├── README.md        how all of this works
└── .vscode/         settings + recommended extensions
data/ in, results/ out

Raw data stays untouched in data/; anything your code makes goes to results/. Delete results/ and you can always make it again. Lose data/ and you cannot.

later
uv add scipy        # need another package?
                    # → pyproject.toml, uv.lock and .venv
                    #   all updated in one go
Never pip install here

uv add writes the package into pyproject.toml, so the next person's uv sync gets it too. pip install does not.

The same layout works for your final project — and for every analysis after it.

Two ways to write Python: .ipynb and .py

analysis.ipynb · a notebook
[1]  dff = np.loadtxt(...)
[2]  dff.mean()
     0.058                ← answer under the cell
[3]  plt.plot(dff)
     [ the figure ]       ← right here, too
For data analysis

Cells you run one at a time, with the answer and the figure right underneath. Ideal for exploring data and telling the story of an analysis. This lab, and your final project.

count_trials.py · a script
# count_trials.py — runs top to bottom, every time
import numpy as np

go = np.loadtxt("data/neuron.csv", delimiter=",",
                skiprows=1, usecols=2)
starts = np.where(go == 1)[0][::9]
print(len(starts), "go trials")
For programs

Plain text, run top to bottom with uv run count_trials.py. Ideal for tools, pipelines and functions you reuse, test and share.

The notebook trap: cells run in any order, so a notebook can work today only because of a cell you ran yesterday. Before you trust a result: Restart → Run All. Curious? marimo, the new kid on the block, is a notebook saved as a .py file that reruns dependent cells for you, so it cannot get out of order.

Open it in VS Code

same terminal
you@mac ~ %cd lab5-neurons
you@mac ~ %code .
Then, in VS Code
  • 1"Do you trust the authors?" → Yes
  • 2"Install recommended extensions?" → Install (Python, Jupyter, Ruff). Missed the pop-up? Extensions (⇧⌘X / Ctrl+Shift+X) → search Jupyter → Install
  • 3Open sample.ipynb
  • 4Select Kernel (top right) → Python Environments → .venv
  • 5Run All
The kernel is the whole trick

A notebook's kernel is the Python that runs its cells. Pick .venv and you get this project's packages. Pick anything else and import numpy fails.

checkpoint — the first cell prints
Mac:      …/lab5-neurons/.venv/bin/python
Windows:  …\lab5-neurons\.venv\Scripts\python.exe

✅ Ends in .venv? Green sticky up. Anything else? Red sticky — see the last slide.

Open the folder, not a single file — that is how VS Code finds .venv by itself.

Get this week's notebooks

Same notebooks as Colab — now in your project. Run this in VS Code's terminal (Terminal → New Terminal), which already opens inside lab5-neurons.

VS Code terminal
# 1. download
you@mac ~ %curl -O https://python-for-biologists.vercel.app/data/lab5/lab5.zip
# 2. unpack — the CSVs land in data/
you@mac ~ %tar -xf lab5.zip
# 3. look
you@mac ~ %ls data

⚠️ Terminal trouble? Download lab5.zip in the browser, unzip it, and drag the three notebooks and the data folder into VS Code's Explorer.

what you should now have
lab5-neurons/
├── 01_numpy_small_data.ipynb          everyone starts here
├── 02_neuron_analysis_basics.ipynb    then this …
├── 02_neuron_analysis_advanced.ipynb  … or this
├── data/
│   ├── neuron_0588_20180828_5_lecture.csv
│   └── neuron_0406_20180620_8_lecture.csv
├── results/
└── sample.ipynb
Checkpoint

Three new notebooks in the Explorer, two CSVs inside data/.

The notebooks read from data/ — and fetch the files themselves if they are missing, so they still work in Colab.

Your turn — locally

The job
  • 1Open 01_numpy_small_data.ipynb — pick the .venv kernel again (once per notebook)
  • 2Work through it top to bottom — every ✅ should turn green
  • 3Then choose: 02_…_basics (the same tasks, on the real neuron) or 02_…_advanced (fewer hints, plus a stretch)
  • 4Save your final figure into results/
add this after your last plot
plt.savefig("results/go_vs_nogo.png", dpi=150)
numbers to check against
01            go 1.0   nogo 0.2     go ÷ nogo 5.0
02 basics     go 0.29  nogo 0.057   go ÷ nogo 5.1
02 advanced   neuron 1: 5.1   neuron 2: 1.2
Advanced: why the two neurons differ

Neuron 1 fires five times harder for go than for nogo — it carries the mouse's decision. Neuron 2 barely tells them apart. Same code, opposite biology.

Nothing here needs the internet any more. Close Colab — this is where your final project will live too.

When it breaks

uv / uv-start: command not found

Close the terminal and open a new one. uv-start still missing? Run uv tool update-shell, then a new terminal. uv missing? Re-run the installer from Lab 2.

code: command not found (Mac)

In VS Code: ⌘⇧P → Shell Command: Install 'code' command in PATH. Or skip it: File → Open Folder → lab5-neurons.

No .venv in the kernel list

Check you opened the folder. Then run uv sync in VS Code's terminal and ⌘⇧P / Ctrl+Shift+P → Developer: Reload Window.

ModuleNotFoundError: No module named 'numpy'

Wrong kernel. Top right of the notebook → Select Kernel → .venv. Never pip install.

PowerShell: parameter name 'O'

In PowerShell, curl means something else. Type curl.exe — with the .exe.

Running cells… asks to install ipykernel

You picked a Python other than .venv. Cancel, and select .venv — it already has ipykernel.

Still stuck after that? Red sticky up — this is exactly what the lab is for.

Python for Biologists