marimo
In this unit you set up marimo in your Codespace. marimo is a notebook tool for Python. We used it in the CLIP unit, but only in your browser. Now you run it yourself.
I walk you through the setup in class. Then we try marimo together.
You need your fork of the class repo and the pixi environment from the Codespaces unit.
1. Install npm
We will use the skills CLI to add marimo skills for your AI assistant. The skills CLI needs npm. npm is a tool that installs JavaScript programs.
Open your Codespace from github.com/codespaces. In the terminal, run:
sudo apt-get update && sudo apt-get install npmIf it asks Do you want to continue? [Y/n], type Y and press Enter. Wait for the prompt to come back.
Check that it worked:
npm --versionYou should see a version number.
2. Install the marimo skills
A skill is a set of notes that teaches your AI assistant a new job. These skills teach it how to work inside a marimo notebook with you.
In the terminal, run:
npx skills add marimo-team/marimo-pairIf it asks you to pick things, press Enter to take the default.
3. Install marimo
Add marimo to your pixi environment:
pixi add marimomarimo installs the other packages it needs when you run the notebook.
Wait for the prompt to come back. If it fails, run pixi install, then try again. If it still fails, tell the instructor.
4. Start marimo
Run this in the terminal:
pixi run marimo edit --no-token marimo-demo.py--no-token turns off the access token, so you do not need to type one.
Wait for marimo to print a link. I show you in class how to open it.
5. Add the starting code
marimo opens marimo-demo.py in your browser. We build it together in class. Start with two cells.
Add a new cell. Paste this in, and run it. It loads the packages:
import altair as alt
import pandas as pd
import umap
from datasets import load_datasetAdd another cell. Paste this in, and run it. It loads blood cell photos and their CLIP numbers (the same ones from the CLIP unit):
# Embeddings (CLIP) and the matching test images
emb = pd.read_csv(
"https://raw.githubusercontent.com/UNC-BIOS-512/jupyterlite/main/data/bloodmnist-emb.csv.gz"
)
images = load_dataset("danjacobellis/bloodmnist_224", split="test")
cell_types = [
"basophil",
"eosinophil",
"erythroblast",
"immature granulocyte",
"lymphocyte",
"monocyte",
"neutrophil",
"platelet",
]
assert len(emb) == len(images)
assert emb["label"].tolist() == [x[0] for x in images["label"]]The first run downloads the photos, so it can take a minute. Then stop and wait. We do the rest together.
If the agent gets stuck
These are the finished cells. Open one only if your AI assistant is not getting it right.
UMAP cell
UMAP squashes the 512 numbers for each photo down to 2. It is slow, so it gets its own cell.
_xy = umap.UMAP(n_components=2, random_state=512).fit_transform(
emb.filter(like="dim_")
)
umap_df = pd.DataFrame(
{
"x": _xy[:, 0],
"y": _xy[:, 1],
"cell type": emb["label"].map(dict(enumerate(cell_types))),
"row": range(len(emb)),
}
)Chart cell
Drag a box on the chart to select cells. This cell needs import marimo as mo in a cell above it.
overview = mo.ui.altair_chart(
alt.Chart(umap_df)
.mark_circle(size=30, opacity=0.7)
.encode(
x=alt.X("x", title="UMAP 1"),
y=alt.Y("y", title="UMAP 2"),
color="cell type:N",
)
.properties(width=550, height=450, title="Drag a box to select cells"),
chart_selection="interval",
)
overviewPhoto grid cell
This shows the photos for the cells you selected. It shows at most 48, picked at random.
MAX_SHOWN = 48
_sel = overview.value
if len(_sel) == 0:
_out = mo.md("Drag a box on the chart to see the cells.")
else:
_shown = _sel.sample(min(MAX_SHOWN, len(_sel)), random_state=512)
_tiles = [
mo.vstack(
[
mo.image(images[int(r)]["image"], width=96),
mo.md(f"<small>{t}</small>"),
],
align="center",
gap=0.25,
)
for r, t in zip(_shown["row"], _shown["cell type"])
]
_out = mo.vstack(
[
mo.md(
f"Selected **{len(_sel)}** cells. Showing {len(_shown)}."
),
mo.hstack(_tiles, wrap=True, justify="start", gap=0.75),
]
)
_out