AnnData Data Model
Use this sub-skill when a task asks how to build, inspect, validate, subset, copy, convert, or mutate an in-memory anndata.AnnData object. Typical prompts mention AnnData(...), X, obs, var, layers, obsm, varm, obsp, varp, uns, .raw, obs_names, var_names, views, copy-on-modify warnings, .to_df(), .copy(), .to_memory(), or shape/name validation.
Route First
- Continue here for single-object construction, aligned slot contracts, index/name checks, view semantics,
.raw, to_df, copy, to_memory, and safe in-memory mutation patterns.
- Route
.h5ad, Zarr, backed reads/writes, lazy reads, element I/O, compression, and file lifecycle to ../storage-io/SKILL.md.
- Route
anndata.concat, experimental.concat_on_disk, AnnCollection, batch labels, joins, and multi-object merging to ../combining-data/SKILL.md.
- Route
anndata.acc references and register_anndata_namespace extension namespaces to ../accessors-extensions/SKILL.md.
Use The Bundled Files
references/api-reference.md lists verified public signatures, construction forms, and method/property notes.
references/object-model.md explains axis alignment, slot shape contracts, slicing, copy-on-modify views, .raw, and mutation rules.
references/troubleshooting.md maps common construction and mutation failures to concrete recovery steps.
scripts/inspect_anndata_structure.py prints a deterministic shape/key/name summary for a demo object, an .h5ad, or a Zarr store.
Default Workflow
- Establish the intended shape
(n_obs, n_vars) from X, from shape=..., or from the aligned annotations being supplied.
- Build
obs with exactly n_obs rows and var with exactly n_vars rows; treat their indexes as obs_names and var_names.
- Add aligned containers only after checking their axes:
layers match (n_obs, n_vars), obsm rows match n_obs, varm rows match n_vars, obsp match (n_obs, n_obs), and varp match (n_vars, n_vars).
- Keep free-form metadata in
uns; do not put axis-aligned arrays in uns when an aligned slot exists.
- When subsetting, assume
adata[...] returns a view until copied or mutated; use .copy() before intentional independent edits.
- For normalized/counts snapshots, set
adata.raw = adata.copy() before replacing X or filtering variables; remember .raw follows observation slicing but keeps its own variable axis.
- Validate final objects with
.shape, .obs_names.is_unique, .var_names.is_unique, slot keys, and the inspection script before handing them to storage, concat, or downstream analysis.
Quick Checks
python sub-skills/data-model/scripts/inspect_anndata_structure.py
python sub-skills/data-model/scripts/inspect_anndata_structure.py --h5ad example.h5ad
python sub-skills/data-model/scripts/inspect_anndata_structure.py --zarr example.zarr
When running from outside the skill directory, use the script's full path. The script does not write outputs, access the network, or mutate inputs.
1---2name: data-model3description: "Construct, inspect, validate, slice, copy, and mutate in-memory4 AnnData objects and their aligned annotations."5disable-model-invocation: true6metadata:7 source: https://skillmd.com/skills/vectorspacelab/data-model8 disco-role: operating9license: BSD 3-Clause10---1112# AnnData Data Model1314Use this sub-skill when a task asks how to build, inspect, validate, subset, copy, convert, or mutate an in-memory `anndata.AnnData` object. Typical prompts mention `AnnData(...)`, `X`, `obs`, `var`, `layers`, `obsm`, `varm`, `obsp`, `varp`, `uns`, `.raw`, `obs_names`, `var_names`, views, copy-on-modify warnings, `.to_df()`, `.copy()`, `.to_memory()`, or shape/name validation.1516## Route First1718- Continue here for single-object construction, aligned slot contracts, index/name checks, view semantics, `.raw`, `to_df`, `copy`, `to_memory`, and safe in-memory mutation patterns.19- Route `.h5ad`, Zarr, backed reads/writes, lazy reads, element I/O, compression, and file lifecycle to [`../storage-io/SKILL.md`](../storage-io/SKILL.md).20- Route `anndata.concat`, `experimental.concat_on_disk`, `AnnCollection`, batch labels, joins, and multi-object merging to [`../combining-data/SKILL.md`](../combining-data/SKILL.md).21- Route `anndata.acc` references and `register_anndata_namespace` extension namespaces to [`../accessors-extensions/SKILL.md`](../accessors-extensions/SKILL.md).2223## Use The Bundled Files2425- [`references/api-reference.md`](references/api-reference.md) lists verified public signatures, construction forms, and method/property notes.26- [`references/object-model.md`](references/object-model.md) explains axis alignment, slot shape contracts, slicing, copy-on-modify views, `.raw`, and mutation rules.27- [`references/troubleshooting.md`](references/troubleshooting.md) maps common construction and mutation failures to concrete recovery steps.28- [`scripts/inspect_anndata_structure.py`](scripts/inspect_anndata_structure.py) prints a deterministic shape/key/name summary for a demo object, an `.h5ad`, or a Zarr store.2930## Default Workflow31321. Establish the intended shape `(n_obs, n_vars)` from `X`, from `shape=...`, or from the aligned annotations being supplied.332. Build `obs` with exactly `n_obs` rows and `var` with exactly `n_vars` rows; treat their indexes as `obs_names` and `var_names`.343. Add aligned containers only after checking their axes: `layers` match `(n_obs, n_vars)`, `obsm` rows match `n_obs`, `varm` rows match `n_vars`, `obsp` match `(n_obs, n_obs)`, and `varp` match `(n_vars, n_vars)`.354. Keep free-form metadata in `uns`; do not put axis-aligned arrays in `uns` when an aligned slot exists.365. When subsetting, assume `adata[...]` returns a view until copied or mutated; use `.copy()` before intentional independent edits.376. For normalized/counts snapshots, set `adata.raw = adata.copy()` before replacing `X` or filtering variables; remember `.raw` follows observation slicing but keeps its own variable axis.387. Validate final objects with `.shape`, `.obs_names.is_unique`, `.var_names.is_unique`, slot keys, and the inspection script before handing them to storage, concat, or downstream analysis.3940## Quick Checks4142```bash43python sub-skills/data-model/scripts/inspect_anndata_structure.py44python sub-skills/data-model/scripts/inspect_anndata_structure.py --h5ad example.h5ad45python sub-skills/data-model/scripts/inspect_anndata_structure.py --zarr example.zarr46```4748When running from outside the skill directory, use the script's full path. The script does not write outputs, access the network, or mutate inputs.