Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 56 additions & 21 deletions {{cookiecutter.project_name}}/CONVENTIONS.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
# Conventions

How analysis is organized in this repo. Humans and coding agents follow the same rules; agents add their own on top
(a worktree, a session and a PR per task, sign-off on every write-back), from the
[analysis-workflow](https://github.com/quadbio/analysis-workflow) plugin.
How analysis is organized in this repo, written for the humans working in it: everything here applies to a human's
work. Coding agents follow the same rules and add their own on top (a worktree, a session and a PR per task, a
human's sign-off on every write-back), from the [analysis-workflow](https://github.com/quadbio/analysis-workflow)
plugin. Agents are mentioned here only where a human needs to know what they do.

> Everything here comes from the analysis template; change it there and pull it with `cruft update`.

## Start a task

A human starts a task with

```bash
pixi run analysis-workflow new analysis/<dataset>/<topic>/human_<name>_v1 --author <your initials> --notebook 01_<step>
pixi run analysis-workflow new analysis/<dataset>/<topic>/human_<name>_v1 --notebook 01_<step>
```

This creates the task directory with its `README.md` and a starter notebook in `notebooks/`. Open the notebook,
fill in the README's question and inputs, and work.
which creates the task directory with its `README.md` and a starter notebook in `notebooks/`, then opens the
notebook, fills in the README's question, and works. The README's author line takes the git user name;
`--author <name>` sets another.

## Tasks

All analysis lives in task directories, `analysis/<dataset>/<topic>/<lane>_<name>_vN/`:

- `<dataset>` is there even when the repo has one dataset. Topics, such as `preprocessing/`, `annotation/` or
`spatial/`, can mostly be worked on in any order.
- `<lane>` is `human` or `agent`; the README's author line says who: initials, or the model.
- `<lane>` is `human` or `agent`; the README's author line says who: a name, or the model.
- `vN` counts the answers to one question across both lanes: `human_markers_v1` may be followed by
`agent_markers_v2`. `analysis-workflow new` refuses any other version than the next.
- A task is edited freely until its first write-back to a shared object or until other tasks read its outputs (for
Expand All @@ -30,7 +34,8 @@ All analysis lives in task directories, `analysis/<dataset>/<topic>/<lane>_<name

| in a task | tracked | holds |
| --- | --- | --- |
| `README.md` | yes | the question, inputs, deliverables, write-back; kept current |
| `README.md` | yes | the task's id, the question, deliverables, write-back; kept current |
| `inputs.yaml` | yes | what the task's code consumed, written by an agent (What an artifact rests on) |
| `notebooks/` | yes | `NN_<step>.ipynb`, numbered in run order; outputs committed, so keep them small |
| `scripts/` | yes | `NN_<step>.py`, numbered in run order; `_common.py` holds the task's paths |
| `results/` | yes | small evidence tables; past ~1 MB they belong in `outputs/` |
Expand Down Expand Up @@ -59,37 +64,68 @@ fig.savefig(PATHS.figures / "umap.pdf")
| `resources/` | curated inputs that are not data: gene lists, marker tables |
| `processed/` | objects other tasks load: working objects in `working/`, others in descriptively named subdirectories |

A task writes into its own directory. An object other tasks will load goes to `processed/` (for an agent, after
sign-off), and the task README records its path.
A task writes into its own directory, and so do the external tools and shell scripts it runs. An object other

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make sure the top level README contains a link to these conventions, so humans actually read what it says here. Also, I feel like it should be clearly marked what's human-relevant, so that humans can quickly get what matters to them, withouth having to read all the agent-specific stuff.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, let's not duplicate anything - rules that hold for agents and humans alike won't be duplicated. What I"m saying is, make sure these conventions are actually human-readable, if we want humans to follow them!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 34f2be7. The README already linked the conventions under "Start Your Analysis"; it now also points to them at the top. CONVENTIONS.md opens by saying it is written for humans and applies to their work throughout, with agents mentioned only where a human needs to know what they do, and the sentences that said "you" now name who acts.

tasks will load goes to `processed/` (an agent puts it there only after a human's sign-off), and the task README
records its path.

Every write into `processed/` sits in a `writes` block: a notebook cell that starts `%%writes <path>`, or
`with writes(<path>):` in a script, where `<path>` is the store. It records in the store which task wrote what
(`adata.uns["provenance"]`, `sdata.attrs["provenance"]`). That goes for a write-back, a new object, a new
SpatialData element and a copy out of a task's `outputs/`. Stores only grow: nothing in one is replaced or removed
in place.

**Limitation.** Only AnnData and SpatialData stores hold such a record. Any other file under `data/` has none: a
trace stops at it, and the only link to the task that made it is that task's README.

## Working objects

A working object is an AnnData zarr store, `processed/working/<name>_<YYYY-MM-DD>.zarr`, in which results from many
tasks accumulate.

- Read only what you need: `ad.io.read_elem(zarr.open_group(path)["obs"])`, or `ad.experimental.read_lazy`.
- Write back only through `commit_adata`, naming the keys you add. It refuses a key that is already there, so
- Read only what is needed: `ad.io.read_elem(zarr.open_group(path)["obs"])`, or `ad.experimental.read_lazy`.
- Write back only through `commit_adata`, naming the keys to add. It refuses a key that is already there, so
nobody's results get overwritten:

```python
from analysis_workflow import commit_adata

commit_adata(adata, path, keys={"obs": ["leiden_fine"]}) # dry run: shows what it would write
```

```python
%%writes path
commit_adata(adata, path, keys={"obs": ["leiden_fine"]}, yes=True) # writes
```

- Never write an in-memory object over a working object: it erases what others added since you read it. Changing
or removing a key means a new dated copy.
- Create one with `adata.write_zarr(path, consolidate_metadata=False)`, at a path that doesn't exist yet. Readers of
a consolidated store never see keys added later, so `commit_adata` refuses one.
- Give keys descriptive names, and list each in the task README's Write-back section.
- Never write an in-memory object over a working object: it erases what others added since it was read. Changing
or removing a key means a new dated copy. In it a key may be dropped; one whose values change gets a new name,
so a name stands for the same values in every copy.
- Create one with `adata.write_zarr(path, consolidate_metadata=False)` in a `writes` block, at a path that doesn't
exist yet. Readers of a consolidated store never see keys added later, so `commit_adata` refuses one.
- Give keys descriptive names, and say what each holds in the task README's Write-back section.
- Labels: one categorical `obs` column, colours in `uns["<key>_colors"]`.
- A result that would grow the object materially (`analyze_anndata_size`) becomes a standalone AnnData on the same
`obs_names`, never a bare `.npy`.

## What an artifact rests on

Two records answer it. A store records which task wrote each of its keys (Data, above). A task's `inputs.yaml`
lists what its code consumed: stores with the keys used, other tasks' files, data files. The
[analysis-workflow](https://github.com/quadbio/analysis-workflow) plugin's `declare-inputs` skill writes that list
from the task's scripts and notebooks. For a human's task, the human asks an agent in this repo to declare its
inputs once the task is finished.

```bash
pixi run analysis-workflow trace <store> <key> # or: trace <file>
```

then shows the tasks and artifacts a key or a file rests on, back to the raw data, and fails on a task whose
inputs are not declared. The plugin's `trace-lineage` skill writes it up as a graph and a summary.

## Git

You work in the main checkout. A pull request is optional and encouraged for anything others build on:
Humans work in the main checkout. For a human's task a pull request is optional, and encouraged for anything
others build on:

```bash
git switch -c <branch>
Expand All @@ -98,6 +134,5 @@ git push -u origin <branch>
gh pr create # its body starts from the PR template
```

It is reviewed against `REVIEW_GUIDE.md`. Switching
branches changes the main checkout that agents' sessions read, `pixi.toml` included: switch back to `main` when you
are done.
It is reviewed against `REVIEW_GUIDE.md`. Switching branches changes the main checkout that agents' sessions read,
`pixi.toml` included, so a human switches back to `main` when done.
5 changes: 4 additions & 1 deletion {{cookiecutter.project_name}}/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

<!-- This README is the project's own: `cruft update` leaves it alone, so describe the project here and edit freely. -->

**Working in this repo?** Read [`CONVENTIONS.md`](CONVENTIONS.md) before the first task: it says how analysis is
organized here, for humans and coding agents alike.

---

## 🚀 Getting Started
Expand Down Expand Up @@ -80,7 +83,7 @@ In Jupyter Lab, check that your kernel appears (look for `{{ cookiecutter.projec
Each piece of analysis gets its own task directory. Start one with a notebook in it:

```bash
pixi run analysis-workflow new analysis/<dataset>/<topic>/human_<name>_v1 --author <your initials> --notebook 01_<step>
pixi run analysis-workflow new analysis/<dataset>/<topic>/human_<name>_v1 --notebook 01_<step>
```

- **How it fits together**: [`CONVENTIONS.md`](CONVENTIONS.md): task directories, where outputs go, `data/`, and
Expand Down
24 changes: 17 additions & 7 deletions {{cookiecutter.project_name}}/REVIEW_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ code loads):
- **Accumulate by addition.** Adding keys to freshly re-read state is commutative, so concurrent sessions can't
lose each other's work. Removal isn't: that means a new dated copy.
- **Writing back to a shared object needs sign-off on that specific diff.**
- **Every write is recorded.** It sits in a `writes` block, which notes in the store which task wrote what.

## Priorities

Expand All @@ -28,25 +29,34 @@ A write to a working object goes through `commit_adata`, gated behind a dry run.

- an in-memory AnnData written back over a working object (`write_zarr`/`write_h5ad` onto its path): it erases
whatever other sessions added since it was read;
- a replaced SpatialData image, labels, points or shapes element, where a new element name would do.
- a replaced SpatialData image, labels, points or shapes element, where a new element name would do;
- a write into `data/<dataset>/processed/` outside a `writes` block, a shell `cp` or `mv` among them: the store
then has no record of the task;
- in a new dated copy, a key whose values changed under its old name.

### 2. Promotions, which leave no diff

`data/` is gitignored, so an object written into `data/<dataset>/processed/` appears nowhere in the diff; the task
README's `Write-back` section is the only trace. The right subdirectory is not guessable. Check that the README
`data/` is gitignored, so an object written into `data/<dataset>/processed/` appears nowhere in the diff. The
store records which task wrote it; where it went is in the task README's `Write-back` section. The right
subdirectory is not guessable. Check that the README
*says* where it went and, for an agent's PR, that the PR body flags it for sign-off and that it was agreed, while
moving it is still cheap.

### 3. The task contract

Work belongs in a task directory `analysis/<dataset>/<topic>/.../<lane>_<name>_vN/`. Check that its README's
content is real: the actual input files, the deliverables, a short summary of each PR. An edit to a frozen task's
directory (one written back from, or merged) is a finding: analysis moves forward, and a new version names the one
it follows. A task is one branch merged through one or more PRs, so a directory whose earlier PRs (its README lists
content is real: the deliverables, a short summary of each PR. An edit to a frozen task's directory (one written
back from, or merged) is a finding, other than to its `inputs.yaml`: analysis moves forward, and a new version
names the one it follows. A task is one branch merged through one or more PRs, so a directory whose earlier PRs (its README lists
them) came from this PR's branch is this task's own, not an earlier one. Outputs written outside the task directory,
other than to a shared object, are a finding.

Every key committed to a shared object appears in that README: an unrecorded key can't be traced back.
**Declared inputs.** Each task directory holds `inputs.yaml`: the stores, with the keys used, and the files its
code consumes. Extract a task's inputs from its scripts and notebooks yourself, before reading its `inputs.yaml`:
the keys of a shared store the code uses (not merely loads), including keys used through the defaults of functions
it calls; files of other tasks; data files. Leave out what the task writes itself. Then compare. Report each input
the code uses that the file lacks, and each entry the code does not use. A finished task without the list is a
finding.

### 4. Scientific correctness

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

**Date:** 2026-01-27
**Author:** ML

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How's the author written down here? What does this look like by default for humans and for agents? For humans, it would be nice if one could pass this already when creating the task dir, with a default if left empty.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

analysis-workflow new --author writes it: a human passed a name or initials, an agent its model. As agreed, --author becomes optional for a human's task and defaults to the git user name; an agent's task still has to pass its model (quadbio/analysis-workflow#26). The commands here drop the flag (34f2be7).

**Id:** 5c1d9e42

<!-- Every heading below stays, in this order, and no others are added; a section with nothing to say reads "None."
The guidance comments stay too. -->
Expand All @@ -19,17 +20,10 @@ otherwise; a later change is recorded here too. -->
| UMAP overviews | `figures/` (gitignored) |
| a new working object | Write-back, below |

## Inputs

<!-- The actual files, not concepts: this is what makes the task re-runnable. -->

- scanpy's PBMC 3k dataset, downloaded to `data/example_dataset/raw/`
- CellTypist's `Immune_All_High.pkl` model

## Write-back

<!-- What went into a shared object, under which keys, and anything placed in `data/<dataset>/processed/` with its
final path. -->
<!-- What each key written to a shared object holds, and the final path of anything placed in
`data/<dataset>/processed/`. Which task wrote a key is recorded in the store itself. -->

- `data/example_dataset/processed/working/pbmc3k_2026-01-27.zarr`, a new working object, holding:
- `obsm["X_scVI"]`, `obsm["X_umap"]`: scVI latent space and its UMAP
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- path: data/example_dataset/raw/pbmc3k_raw.h5ad
- path: ~/.celltypist/data/models/Immune_All_High.pkl
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@
"source": [
"### 💾 Save Results — 💻 Local\n",
"\n",
"This notebook starts a new working object: a dated zarr store in `processed/working/` that other tasks load and add keys to with `commit_adata`. It is written without consolidated metadata, so those additions stay visible."
"This notebook starts a new working object: a dated zarr store in `processed/working/` that other tasks load and add keys to with `commit_adata`. It is written without consolidated metadata, so those additions stay visible. The cell that writes it starts with `%%writes`, so the store records which task wrote its keys."
]
},
{
Expand All @@ -490,7 +490,17 @@
"source": [
"output_path = FilePaths.EXAMPLE_DATASET / \"processed\" / \"working\" / \"pbmc3k_2026-01-27.zarr\"\n",
"if output_path.exists(): # other tasks may have added to it since: never write over it\n",
" raise FileExistsError(f\"{output_path} exists; write a new dated copy instead.\")\n",
" raise FileExistsError(f\"{output_path} exists; write a new dated copy instead.\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "37a",
"metadata": {},
"outputs": [],
"source": [
"%%writes output_path\n",
"adata.write_zarr(output_path, consolidate_metadata=False)\n",
"print(f\"Saved to: {output_path}\")"
]
Expand Down
11 changes: 9 additions & 2 deletions {{cookiecutter.project_name}}/templates/notebook.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"source": [
"# <step>\n",
"\n",
"One line on what this notebook does. The task's question, inputs and write-back are in its `README.md`."
"One line on what this notebook does. The task's question and write-back are in its `README.md`; what it consumed, in its `inputs.yaml`."
]
},
{
Expand Down Expand Up @@ -127,12 +127,19 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Results for other tasks go into the working object, under keys you name; list them in the README. Run the dry run, check it, then pass `yes=True`:\n",
"Results for other tasks go into the working object, under keys you name; say what each holds in the README. Run the dry run and check it:\n",
"\n",
"```python\n",
"from analysis_workflow import commit_adata\n",
"\n",
"commit_adata(adata, WORKING_OBJECT, keys={\"obs\": [\"<key>\"]})\n",
"```\n",
"\n",
"Then write, in a cell that starts with `%%writes`, so the store records which task wrote the keys:\n",
"\n",
"```python\n",
"%%writes WORKING_OBJECT\n",
"commit_adata(adata, WORKING_OBJECT, keys={\"obs\": [\"<key>\"]}, yes=True)\n",
"```"
]
}
Expand Down
Loading