Building a reference¶
Functions for building a custom reference from precomputed single-cell atlas embeddings and
labels. Reference construction projects atlas representations into the shared cell space and
records the cell_space_id used later for compatibility checks.
alhena.refbuild.build_reference
¶
build_reference(embeddings, labels, encoder, output, *, name=None, axis=DEFAULT_AXIS, class_order=None, label_column=None, dataset=DEFAULT_DATASET, unlabeled=DEFAULT_UNLABELED, source='', license='', k=DEFAULT_K, tau=DEFAULT_TAU, batch_size=DEFAULT_PROJECTION_BATCH, self_check_cells=None, seed=0, overwrite=False, progress=None)
Build a reference shard from single-cell embeddings and a user's own labels.
The embeddings are projected by the reference head and written at half precision in atlas
order, with one class-code array per axis, -1 where a row has no class. The shard is read
back and retrieved against itself for the report. A custom taxonomy raises EnvelopeWarning,
since k and tau were not selected on it. The shard is marked user_built.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embeddings
|
str | Path | ndarray
|
Path of the HDF5 file holding |
required |
labels
|
LabelSource | Mapping[str, LabelSource]
|
One label per cell, as an array, a table or the path of a table; or a mapping from axis name to any of those. |
required |
encoder
|
AlhenaEncoder
|
The encoder whose reference head defines the cell space. |
required |
output
|
str | Path
|
Path of the shard to write. |
required |
name
|
str | None
|
Name the shard carries. Defaults to the file stem. |
None
|
axis
|
str
|
Axis the self-check runs on, one of the axes coded. |
DEFAULT_AXIS
|
class_order
|
Sequence[str] | Mapping[str, Sequence[str]] | None
|
Class names of a custom taxonomy in the order fixing their codes, or a mapping from axis name to such an order. Omitted for a declared axis. |
None
|
label_column
|
str | Mapping[str, str] | None
|
Column holding the labels when a table has several columns, or a mapping from axis name to column. |
None
|
dataset
|
str
|
Dataset holding the embeddings in the HDF5 file. |
DEFAULT_DATASET
|
unlabeled
|
Sequence[str]
|
Label values read as no label. |
DEFAULT_UNLABELED
|
source
|
str
|
Where the atlas comes from, recorded in the shard. |
''
|
license
|
str
|
Terms the atlas is distributed under, recorded in the shard. |
''
|
k
|
int
|
Neighbors the self-check votes over. |
DEFAULT_K
|
tau
|
float
|
Softmax temperature the self-check votes at. |
DEFAULT_TAU
|
batch_size
|
int
|
Rows projected in one pass. |
DEFAULT_PROJECTION_BATCH
|
self_check_cells
|
int | None
|
Cells the self-check samples. Its own default when omitted. |
None
|
seed
|
int
|
Seed of the self-check sample. |
0
|
overwrite
|
bool
|
Replace a shard already at |
False
|
progress
|
bool | None
|
Whether to report progress on standard error. None follows
|
None
|
Returns:
| Type | Description |
|---|---|
ShardBuildReport
|
The path written, the shard's name, cell space and support, and its self-check. |
Raises:
| Type | Description |
|---|---|
AlhenaValueError
|
If an input is malformed, a label matches no class, or |
ReferenceError
|
If the projection cannot be stored, or if the shard does not load back. |
BundleError
|
If the encoder carries no reference head. |
alhena.refbuild.read_scconcept_embeddings
¶
read_scconcept_embeddings(path, *, dataset=DEFAULT_DATASET)
Read single-cell embeddings from the HDF5 file the embedding step writes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path of the HDF5 file. |
required |
dataset
|
str
|
Dataset holding the embeddings. |
DEFAULT_DATASET
|
Returns:
| Type | Description |
|---|---|
ndarray
|
An |
Raises:
| Type | Description |
|---|---|
AlhenaValueError
|
If the file or dataset is missing, or the array is not |
alhena.refbuild.read_labels
¶
Read one label per cell, matched to the embeddings by row position, never by identifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
LabelSource
|
An array of labels, a table, or the path of a |
required |
n_rows
|
int
|
Number of cells the labels must cover. |
required |
column
|
str | None
|
Column holding the labels, needed when a table holds several. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
An |
Raises:
| Type | Description |
|---|---|
AlhenaValueError
|
If the file, the column or the row count does not match. |
alhena.refbuild.encode_labels
¶
encode_labels(labels, class_order, unlabeled=DEFAULT_UNLABELED)
Code labels against a class order, marking rows carrying no class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
LabelSource
|
One label per cell. |
required |
class_order
|
Sequence[str]
|
Class names, in the order fixing their codes. The unknown class codes no row. |
required |
unlabeled
|
Sequence[str]
|
Label values read as no label. |
DEFAULT_UNLABELED
|
Returns:
| Type | Description |
|---|---|
tuple[ndarray, dict[str, int]]
|
An int8 array of class codes, |
Raises:
| Type | Description |
|---|---|
AlhenaValueError
|
If the class order is empty, repeated or too long, or a label is unknown. |
alhena.refbuild.shard_metadata
¶
Read the metadata of a shard without reading its tensors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path of the shard. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
The metadata fields, all of them strings. |
Raises:
| Type | Description |
|---|---|
ReferenceError
|
If the file is missing or is not a shard. |
alhena.refbuild.is_user_built
¶
Report whether a reference was built from a user's own atlas.
The mark is USER_BUILT_KEY in the metadata of a shard, and USER_BUILT_SOURCE in
ReferenceInfo.source.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reference
|
ReferenceLike
|
A loaded reference, a |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True when the reference was built from a user's own atlas. |
Raises:
| Type | Description |
|---|---|
AlhenaValueError
|
If the argument is none of those three. |
alhena.refbuild.ShardBuildReport
dataclass
¶
ShardBuildReport(path, name, axis, axes, class_orders, n_rows, n_labeled, n_invalid_embeddings, class_support, cell_space_id, user_built, info, self_check)
What a reference build wrote, and how the reference reads at the build's k and tau.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
Path
|
Path of the shard. |
name |
str
|
Name the shard carries. |
axis |
str
|
Axis the self-check was run on. |
axes |
tuple[str, ...]
|
Every axis the shard codes, in the order the labels declared them. |
class_orders |
dict[str, tuple[str, ...]]
|
Classes of each axis, in the order fixing their codes. The unknown class, stored after them and coding no row, is left out. |
n_rows |
int
|
Rows written: every row of the source atlas. |
n_labeled |
dict[str, int]
|
Rows carrying a class, per axis. |
n_invalid_embeddings |
int
|
Rows whose projection is unusable, so they carry no class on any axis. |
class_support |
dict[str, dict[str, int]]
|
Cells per class, per axis. |
cell_space_id |
str
|
Identity of the cell space the reference was projected into. |
user_built |
bool
|
Always True for a shard written here. |
info |
ReferenceInfo
|
Name, cell space and support of the shard as the loader reads it. |
self_check |
SelfCheckReport
|
Leave-one-out retrieval of the reference against itself. |
alhena.refbuild.DEFAULT_DATASET
module-attribute
¶
Dataset holding the embeddings in the HDF5 file the embedding step writes.
alhena.refbuild.UNLABELED_CODE
module-attribute
¶
Class code of a row carrying no label on an axis.
alhena.refbuild.MAX_CLASSES
module-attribute
¶
Largest class code an int8 array can hold, so the widest taxonomy a shard carries.
alhena.refbuild.DEFAULT_UNLABELED
module-attribute
¶
DEFAULT_UNLABELED = (UNKNOWN_LABEL, '')
Label values read as no label. The unknown class is always among them.
alhena.refbuild.DEFAULT_PROJECTION_BATCH
module-attribute
¶
Rows projected in one pass.
alhena.refbuild.USER_BUILT_KEY
module-attribute
¶
Metadata key marking a shard built from a user's own atlas.
alhena.refbuild.USER_BUILT_SOURCE
module-attribute
¶
Prefix of ReferenceInfo.source marking a user-built shard once loaded.
alhena.refbuild.ROW_ORDER
module-attribute
¶
Value of the metadata field recording that rows keep the order of the source atlas.
alhena.refbuild.EMBEDDINGS_TENSOR
module-attribute
¶
Name of the embedding tensor in a shard; every other tensor codes the classes of an axis.
alhena.refbuild.SHARD_SUFFIX
module-attribute
¶
Suffix of a reference shard file.
alhena.refbuild.LabelSource
module-attribute
¶
Labels given as an array, a table, or the path of a table.
alhena.refbuild.ReferenceLike
module-attribute
¶
A loaded reference, its ReferenceInfo, or the path of its shard.
Next: Scoring