diff --git a/{{cookiecutter.project_name}}/CONVENTIONS.md b/{{cookiecutter.project_name}}/CONVENTIONS.md index f347a01..c0c8d74 100644 --- a/{{cookiecutter.project_name}}/CONVENTIONS.md +++ b/{{cookiecutter.project_name}}/CONVENTIONS.md @@ -1,19 +1,23 @@ # 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///human__v1 --author --notebook 01_ +pixi run analysis-workflow new analysis///human__v1 --notebook 01_ ``` -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 ` sets another. ## Tasks @@ -21,7 +25,7 @@ All analysis lives in task directories, `analysis///_` is there even when the repo has one dataset. Topics, such as `preprocessing/`, `annotation/` or `spatial/`, can mostly be worked on in any order. -- `` is `human` or `agent`; the README's author line says who: initials, or the model. +- `` 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 @@ -30,7 +34,8 @@ All analysis lives in task directories, `analysis///_.ipynb`, numbered in run order; outputs committed, so keep them small | | `scripts/` | yes | `NN_.py`, numbered in run order; `_common.py` holds the task's paths | | `results/` | yes | small evidence tables; past ~1 MB they belong in `outputs/` | @@ -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 +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 `, or +`with writes():` in a script, where `` 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/_.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["_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 # or: trace +``` + +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 @@ -98,6 +134,5 @@ git push -u origin 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. diff --git a/{{cookiecutter.project_name}}/README.md b/{{cookiecutter.project_name}}/README.md index 3fbc56a..e625fa0 100644 --- a/{{cookiecutter.project_name}}/README.md +++ b/{{cookiecutter.project_name}}/README.md @@ -4,6 +4,9 @@ +**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 @@ -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///human__v1 --author --notebook 01_ +pixi run analysis-workflow new analysis///human__v1 --notebook 01_ ``` - **How it fits together**: [`CONVENTIONS.md`](CONVENTIONS.md): task directories, where outputs go, `data/`, and diff --git a/{{cookiecutter.project_name}}/REVIEW_GUIDE.md b/{{cookiecutter.project_name}}/REVIEW_GUIDE.md index 6e9f7a2..2da2b21 100644 --- a/{{cookiecutter.project_name}}/REVIEW_GUIDE.md +++ b/{{cookiecutter.project_name}}/REVIEW_GUIDE.md @@ -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 @@ -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//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//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//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///.../__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 diff --git a/{{cookiecutter.project_name}}/analysis/example_dataset/demo/human_scrna_workflow_v1/README.md b/{{cookiecutter.project_name}}/analysis/example_dataset/demo/human_scrna_workflow_v1/README.md index 01dac55..f1cb9e9 100644 --- a/{{cookiecutter.project_name}}/analysis/example_dataset/demo/human_scrna_workflow_v1/README.md +++ b/{{cookiecutter.project_name}}/analysis/example_dataset/demo/human_scrna_workflow_v1/README.md @@ -2,6 +2,7 @@ **Date:** 2026-01-27 **Author:** ML +**Id:** 5c1d9e42 @@ -19,17 +20,10 @@ otherwise; a later change is recorded here too. --> | UMAP overviews | `figures/` (gitignored) | | a new working object | Write-back, below | -## Inputs - - - -- scanpy's PBMC 3k dataset, downloaded to `data/example_dataset/raw/` -- CellTypist's `Immune_All_High.pkl` model - ## Write-back - + - `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 diff --git a/{{cookiecutter.project_name}}/analysis/example_dataset/demo/human_scrna_workflow_v1/inputs.yaml b/{{cookiecutter.project_name}}/analysis/example_dataset/demo/human_scrna_workflow_v1/inputs.yaml new file mode 100644 index 0000000..2ccb292 --- /dev/null +++ b/{{cookiecutter.project_name}}/analysis/example_dataset/demo/human_scrna_workflow_v1/inputs.yaml @@ -0,0 +1,2 @@ +- path: data/example_dataset/raw/pbmc3k_raw.h5ad +- path: ~/.celltypist/data/models/Immune_All_High.pkl diff --git a/{{cookiecutter.project_name}}/analysis/example_dataset/demo/human_scrna_workflow_v1/notebooks/01_scrna_workflow.ipynb b/{{cookiecutter.project_name}}/analysis/example_dataset/demo/human_scrna_workflow_v1/notebooks/01_scrna_workflow.ipynb index b2cd190..c3e913e 100644 --- a/{{cookiecutter.project_name}}/analysis/example_dataset/demo/human_scrna_workflow_v1/notebooks/01_scrna_workflow.ipynb +++ b/{{cookiecutter.project_name}}/analysis/example_dataset/demo/human_scrna_workflow_v1/notebooks/01_scrna_workflow.ipynb @@ -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." ] }, { @@ -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}\")" ] diff --git a/{{cookiecutter.project_name}}/templates/notebook.ipynb b/{{cookiecutter.project_name}}/templates/notebook.ipynb index 39b2d3c..9e31d5c 100644 --- a/{{cookiecutter.project_name}}/templates/notebook.ipynb +++ b/{{cookiecutter.project_name}}/templates/notebook.ipynb @@ -6,7 +6,7 @@ "source": [ "# \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`." ] }, { @@ -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\": [\"\"]})\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\": [\"\"]}, yes=True)\n", "```" ] }