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
5 changes: 3 additions & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ uv run python -m benchmarks.report results.json
```

Test markers:
- `integration` tests build uv environments with real anndata releases (0.8 to
0.13), scanpy and PyTables, so they need the network on the first run.
- `integration` tests build uv environments with real anndata releases (0.7.8
to 0.13) and the oracles (scanpy, PyTables and others), all pinned exactly
in `tests/reference_stores.py`, so they need the network on the first run.
- `perf` tests are slow, but CI runs them in the main job.

CI also installs the package from a fresh resolution, without the lock,
Expand Down
6 changes: 4 additions & 2 deletions docs/developers.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,10 @@ before changing anything that writes:
parametrised over `h5ad`, `zarr2` and `zarr3`.
- **An oracle, not self-consistency.** anndata reads back what is written,
scipy checks `recast`, scanpy, loompy and mudata check the conversions.
`test_anndata_versions.py` builds environments with anndata 0.8 to 0.13 and
checks both directions.
`test_anndata_versions.py` builds environments with anndata 0.7.8 to 0.13,
pinned exactly, and checks both directions: each release's store is read,
and from 0.8 the release reads back `subset`, `convert`, `split` and
`concat` outputs.
- **Complexity guards that count, not time.** `tests/test_performance.py`
counts the elements and bytes read through hooks on h5py and zarr. A change
that adds reads per row or per group fails it on any machine.
Expand Down
5 changes: 3 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ Status goes to stderr and data to stdout, so every command pipes cleanly.

- **Every AnnData on-disk layout**, from anndata 0.7 to the current spec, in
HDF5 and in Zarr v2 and v3; everything written is in the current spec. CI
checks this against stores written by anndata 0.8 to 0.13, and checks that
each of those releases reads what `adata` writes.
checks this against stores written by anndata 0.7.8 to 0.13, and checks
that each release from 0.8 reads the output of `subset`, `convert`,
`split` and `concat` on the store it wrote.
- **Other formats, through `convert`**: 10x Genomics HDF5 and MTX folders
(Cell Ranger, STARsolo, velocyto), Visium and Xenium spatial outputs with
their coordinates and images, CellBender output, Parse Biosciences runs,
Expand Down
66 changes: 54 additions & 12 deletions docs/reference/spec-h5ad.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ It is intended to be GitHub-renderable Markdown (no Sphinx/MyST directives).
- [DataFrame v0.1.0 (legacy: anndata 0.7.x)](#dataframe-v010-legacy-anndata-07x)
- [Legacy categorical columns (Series-level)](#legacy-categorical-columns-series-level)
- [Mappings](#mappings)
- [Legacy mapping encoding (`dict` v0.1.0)](#legacy-mapping-encoding-dict-v010)
- [The `dict` encoding (v0.1.0)](#the-dict-encoding-v010)
- [Scalars](#scalars)
- [Categorical arrays](#categorical-arrays)
- [String arrays](#string-arrays)
Expand Down Expand Up @@ -161,6 +161,15 @@ In v0.1.0 DataFrames, a categorical column dataset (e.g. `obs/cell_type`) can be

- `categories`: an **HDF5 object reference** pointing to the corresponding `__categories/<colname>` dataset.

Whether the categories are ordered is not on the column. It is the
`ordered` attribute of the labels dataset, `__categories/<colname>`.

A reference holds an address in its own file. Copied into another file by
HDF5 without help, it becomes a null reference, so a copy of a 0.1.0
dataframe either rewrites it as 0.2.0 (`obs` and `var`, and every copy to
Zarr) or has HDF5 repoint the references at the copies (a 0.1.0 dataframe
nested in `raw`, `obsm` or `uns`, copied between HDF5 files).

## Mappings

Mappings are stored as HDF5 **groups** on disk.
Expand All @@ -169,20 +178,16 @@ Mappings are stored as HDF5 **groups** on disk.
- Mappings are distinct from DataFrames and sparse arrays and do not require special mapping-specific attributes.
- Mapping semantics are recursive: entries in `uns` can themselves be groups containing additional encoded elements.

> **Legacy compatibility note**
>
> In earlier conventions (commonly seen in older docs and some files), mappings could carry
> `encoding-type: "dict"` and `encoding-version: "0.1.0"`.
> Readers should still accept this legacy metadata when encountered.

### Legacy mapping encoding (`dict` v0.1.0)
### The `dict` encoding (v0.1.0)

For backward compatibility, older files may encode mappings with explicit mapping metadata:
anndata 0.8 and later tag a mapping group with:

- `encoding-type: "dict"`
- `encoding-version: "0.1.0"`

This historical convention existed in earlier AnnData docs and files and should still be accepted by readers.
This is the current encoding, not a legacy one: anndata 0.13 writes it, and
so does this tool, on every mapping group. Files from anndata 0.7.x leave
mappings untagged, and readers accept those too.

## Scalars

Expand Down Expand Up @@ -269,6 +274,20 @@ For elements supporting a `na-value` attribute:

Readers should preserve semantics when the runtime model supports it.

## Record arrays

### `encoding-type: rec-array`, `encoding-version: 0.2.0`

A numpy record array, as scanpy's `rank_genes_groups` keeps its results in
`uns` (`names`, `scores`, `pvals` and the rest: one field per group). It is a
single dataset with a compound dtype:

- a text field is variable-length UTF-8, as anndata writes it to HDF5 (Zarr
holds the same field as fixed-width `<U`);
- every other field keeps its dtype.

anndata 0.7.x wrote the same datasets untagged.

## Awkward arrays (experimental)

### `encoding-type: awkward-array`, `encoding-version: 0.1.0`
Expand Down Expand Up @@ -307,6 +326,7 @@ and always writes the current spec version shown in each section.
| `numeric-scalar` | yes | yes (0.2.0) |
| `string` | yes | yes (0.2.0), as a 0-d dataset |
| `null` | yes | yes (0.1.0) |
| `rec-array` | yes, tagged or not | copied field by field: text as variable-length UTF-8 to HDF5, as `<U` to Zarr |
| `awkward-array` | reported by `view` and `ls` | not written |

### `null` (`encoding-version: 0.1.0`)
Expand All @@ -315,10 +335,32 @@ Not in the upstream prose spec, but written by anndata 0.12+ for a `None`
value in `uns`. In HDF5 it is a dataset with a null dataspace (`h5py.Empty`);
in Zarr it is a 0-d boolean array. Both carry `encoding-type: null`.

anndata 0.8 to 0.11 cannot read a `null`, so this tool writes one only where
the source had one. An empty dataspace *without* the tag (anndata never
writes one; other HDF5 writers do) is copied between HDF5 files as it is;
Zarr has no equivalent, so a copy to Zarr leaves it out, with a warning.

### Data held outside the file

HDF5 lets a dataset keep its data in other files (external storage, or a
virtual dataset mapping datasets of other files), and lets a link point
into another file. anndata writes none of these. This tool reads only data
held in the file itself: a store holding any of them is refused when it is
opened, naming the element, and so is an HDF5 source of `convert` (with the
HDF5 files of a folder source). So is an object reference to an object the
file gives no name, which no walk of its links would meet.

A reference is copied to another HDF5 file only inside an element HDF5
copies whole, pointed at the copy of its target; one leading out of the
element is refused, as is a dataset of references copied on its own.

### Elements with no `encoding-type`

Files written by anndata 0.7.x carry no encoding attributes at all. These are
classified structurally: a group with `codes` and `categories` is a
Files written by anndata 0.7.x tag only their dataframes, sparse matrices and
`raw`; the root, mappings, dense arrays, scalars and record arrays carry no
encoding attributes. A store whose root is untagged while its `obs` and `var`
are 0.1.0 dataframes is taken for one of these, and opens without a warning
about its root. Untagged elements are classified structurally: a group with `codes` and `categories` is a
categorical, one with `values` and `mask` is a nullable array, one with
`_index` in its attributes is a dataframe, and anything else is a mapping.
Structural inference is only ever a fallback -- a declared `encoding-type`
Expand Down
76 changes: 58 additions & 18 deletions docs/reference/spec-zarr.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ It is intended to be GitHub-renderable Markdown (no Sphinx/MyST directives).
- [DataFrame v0.1.0 (legacy: anndata 0.7.x)](#dataframe-v010-legacy-anndata-07x)
- [Legacy categorical columns (Series-level)](#legacy-categorical-columns-series-level)
- [Mappings](#mappings)
- [Legacy mapping encoding (`dict` v0.1.0)](#legacy-mapping-encoding-dict-v010)
- [The `dict` encoding (v0.1.0)](#the-dict-encoding-v010)
- [Scalars](#scalars)
- [Categorical arrays](#categorical-arrays)
- [String arrays](#string-arrays)
Expand Down Expand Up @@ -159,9 +159,12 @@ A legacy dataframe is stored as a Zarr **group** where:

In v0.1.0 DataFrames, a categorical column array (e.g. `obs/cell_type`) can be identified by the presence of an attribute:

- `categories`: an **absolute path string** to the corresponding `__categories/<colname>` array.
- `categories`: a **path string, relative to the dataframe group**, to the corresponding labels: `__categories/<colname>`, as anndata 0.7.8 writes it.

(This differs from HDF5, which can store an object reference.)
(This differs from HDF5, which stores an object reference.)

Whether the categories are ordered is not on the column. It is the
`ordered` attribute of the labels array, `__categories/<colname>`.

## Mappings

Expand All @@ -171,20 +174,16 @@ Mappings are stored as Zarr **groups** on disk.
- Mappings are distinct from DataFrames and sparse arrays and do not require special mapping-specific attributes.
- Mapping semantics are recursive: entries in `uns` can themselves be groups containing additional encoded elements.

> **Legacy compatibility note**
>
> In earlier conventions (commonly seen in older docs and some files), mappings could carry
> `encoding-type: "dict"` and `encoding-version: "0.1.0"`.
> Readers should still accept this legacy metadata when encountered.

### Legacy mapping encoding (`dict` v0.1.0)
### The `dict` encoding (v0.1.0)

For backward compatibility, older files may encode mappings with explicit mapping metadata:
anndata 0.8 and later tag a mapping group with:

- `encoding-type: "dict"`
- `encoding-version: "0.1.0"`

This historical convention existed in earlier AnnData docs and files and should still be accepted by readers.
This is the current encoding, not a legacy one: anndata 0.13 writes it, and
so does this tool, on every mapping group. Files from anndata 0.7.x leave
mappings untagged, and readers accept those too.

## Scalars

Expand All @@ -201,7 +200,7 @@ These should typically only occur inside `uns` and are commonly used for saved p
- String scalars:
- `encoding-type: "string"`
- `encoding-version: "0.2.0"`
- **Zarr requirement:** fixed-length unicode dtype (e.g. `<U9`)
- a 0-d array of variable-length UTF-8 text (`VariableLengthUTF8`, `VLenUTF8` on v2), as anndata 0.13 writes it; anndata 0.8 to 0.11 wrote a fixed-width `<U`, and readers accept both

> **Legacy note**
>
Expand Down Expand Up @@ -271,6 +270,24 @@ For elements supporting a `na-value` attribute:

Readers should preserve semantics when the runtime model supports it.

## Record arrays

### `encoding-type: rec-array`, `encoding-version: 0.2.0`

A numpy record array, as scanpy's `rank_genes_groups` keeps its results in
`uns` (`names`, `scores`, `pvals` and the rest: one field per group). It is a
single Zarr array with a structured dtype:

- a text field is fixed-width unicode, `<U<n>` for the longest value, as
anndata writes it to Zarr (HDF5 holds the same field as variable-length
UTF-8);
- every other field keeps its dtype.

anndata 0.7.x wrote the same arrays untagged.

zarr-python warns that a structured dtype has no Zarr v3 specification yet;
anndata writes rec-arrays to v3 regardless, and this tool does too.

## Awkward arrays (experimental)

### `encoding-type: awkward-array`, `encoding-version: 0.1.0`
Expand Down Expand Up @@ -309,6 +326,7 @@ and always writes the current spec version shown in each section.
| `numeric-scalar` | yes | yes (0.2.0) |
| `string` | yes | yes (0.2.0), as a 0-d dataset |
| `null` | yes | yes (0.1.0) |
| `rec-array` | yes, tagged or not | copied field by field: text as `<U` to Zarr, as variable-length UTF-8 to HDF5 |
| `awkward-array` | reported by `view` and `ls` | not written |

### `null` (`encoding-version: 0.1.0`)
Expand All @@ -317,10 +335,30 @@ Not in the upstream prose spec, but written by anndata 0.12+ for a `None`
value in `uns`. In HDF5 it is a dataset with a null dataspace (`h5py.Empty`);
in Zarr it is a 0-d boolean array. Both carry `encoding-type: null`.

anndata 0.8 to 0.11 cannot read a `null`, so this tool writes one only where
the source had one. An HDF5 empty dataspace *without* the tag (anndata never
writes one; other HDF5 writers do) has no Zarr equivalent: copied to Zarr it
is left out, with a warning, and between HDF5 files it is copied as it is.

### What cannot be copied to Zarr

Two HDF5 dtypes have no Zarr equivalent, and a copy to Zarr refuses them
before writing anything, naming the element: variable-length sequences of
numbers (`h5py.vlen_dtype(int)`) and object references, whether a dataset of
them or an attribute holding one. Neither is anything anndata writes.
Between HDF5 files, variable-length numbers are copied. A reference is
copied only inside an element HDF5 copies whole, pointing at the copy of
its target (as anndata 0.7's categoricals in a nested dataframe are); one
that leads out of the element, or is copied on its own (`--compression`
copies dataset by dataset), is refused, naming it.

### Elements with no `encoding-type`

Files written by anndata 0.7.x carry no encoding attributes at all. These are
classified structurally: a group with `codes` and `categories` is a
Files written by anndata 0.7.x tag only their dataframes, sparse matrices and
`raw`; the root, mappings, dense arrays, scalars and record arrays carry no
encoding attributes. A store whose root is untagged while its `obs` and `var`
are 0.1.0 dataframes is taken for one of these, and opens without a warning
about its root. Untagged elements are classified structurally: a group with `codes` and `categories` is a
categorical, one with `values` and `mask` is a nullable array, one with
`_index` in its attributes is a dataframe, and anything else is a mapping.
Structural inference is only ever a fallback -- a declared `encoding-type`
Expand Down Expand Up @@ -348,9 +386,11 @@ raises `Expected an ArrayArrayCodec`, because the v3 string data type encodes
variable length itself. This tool drops the filter and resolves the dtype for
the target rather than reusing the source's.

Codecs travel only between stores of the same Zarr version. Across backends
none do: an HDF5 dataset written to Zarr gets zarr-python's default codec, and
a compressed Zarr array written to HDF5 becomes a gzip dataset at level 4
Codecs travel only between stores of the same Zarr version, and with them
the absence of one: an uncompressed array stays uncompressed, on v2 as on v3
(read through zarr's `compressors`, on both). Across backends none do: an
HDF5 dataset written to Zarr gets zarr-python's default codec, and a
compressed Zarr array written to HDF5 becomes a gzip dataset at level 4
(h5py's default). An array that was not compressed stays uncompressed.

Note also that `zarr-python` flags both `NullTerminatedBytes` (what `dtype="S"`
Expand Down
Loading
Loading