registry Module
Index registry mapping genotypes and haplotypes to integer indices.
Overview
The registry subpackage provides IndexRegistry, the bridge between
high-level genetic objects and low-level numerical arrays in the simulation engine.
Complete Module Reference
natal.registry
Registry subpackage for stable integer indexing of population entities.
Provides :class:~natal.registry.index.IndexRegistry which assigns and
maintains stable integer indices for genotypes, haploid genotypes, and
gamete/somatic labels used by the simulation engine.
IndexRegistry
Registry providing stable integer indices for population entities.
The IndexRegistry assigns and stores stable integer indices for entities that occur in the population. Internally it uses flat dict-based lookups: ZType = (genotype, slab_label) for the diploid layer, and GType = (haplogenotype, glab_label) for the gamete layer.
Examples:
ic = IndexRegistry() gid = ic.register_genotype('g1') hid = ic.register_haplogenotype('h1') glid = ic.register_gamete_label('gl1')
Attributes:
| Name | Type | Description |
|---|---|---|
slab_labels |
List[str]
|
List of registered somatic (slab) label strings. |
glab_labels |
List[str]
|
List of registered gamete (glab) label strings. |
Initialize an empty IndexRegistry.
All internal mapping dicts and lists start empty; entries are added lazily via the registration methods.
Source code in src/natal/registry/index.py
index_to_genotype
property
Computed list of unique genotypes (in registration order) from ZType space.
index_to_ztype
property
Computed list of (genotype, slab_label) pairs from ZType space.
The index in this list is the ZType index — the same index consumed
by the engine's individual_count arrays on the last axis.
haplo_to_index
property
Computed dict of unique haplotypes from the GType space.
Derived from _index_to_gtype so that after compression only
surviving haplotypes appear.
index_to_haplo
property
Computed list of unique haplotypes (in registration order) from GType space.
index_to_gtype
property
Computed list of (haplogenotype, glab_label) pairs from GType space.
The index in this list is the GType index — the same index returned
by gtype_index() and consumed by the engine's compressed arrays.
glab_to_index
property
Computed dict mapping gamete label strings to their index in glab_labels.
slab_to_index
property
Computed dict mapping somatic label strings to their index in slab_labels.
register_ztype
Register a ZType (genotype, slab) pair and return its index.
O(1) dict lookup for duplicates; appends to flat list for new entries.
Automatically tracks slab_labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
genotype
|
Genotype
|
A |
required |
slab_label
|
str
|
Somatic label string for this ZType variant. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The assigned ZType index. Stable until compression. |
Source code in src/natal/registry/index.py
register_gtype
Register a GType (haplogenotype, glab) pair and return its index.
O(1) dict lookup for duplicates; appends to flat list for new entries.
Automatically tracks glab_labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
haplo
|
HaploidGenotype
|
A |
required |
glab_label
|
str
|
Gamete label string for this GType variant. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The assigned GType index. Stable until compression. |
Source code in src/natal/registry/index.py
register_genotype
Register a genotype and auto-cross-product with all slab labels.
If slab_labels is empty it is auto-initialised to ["default"].
Each (genotype, slab) pair becomes a ZType entry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
genotype_id
|
Genotype
|
A |
required |
Returns:
| Type | Description |
|---|---|
List[int]
|
list[int]: ZType indices for this genotype (one per slab label). |
Source code in src/natal/registry/index.py
register_haplogenotype
Register a haplogenotype and auto-cross-product with all glab labels.
If glab_labels is empty it is auto-initialised to ["default"].
Each (haplogenotype, glab) pair becomes a GType entry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
haplo_id
|
HaploidGenotype
|
A |
required |
Returns:
| Type | Description |
|---|---|
list[int]
|
list[int]: GType indices for this haplotype (one per glab label). |
Source code in src/natal/registry/index.py
register_gamete_label
Register a gamete label and return its index.
Adapted to use the new glab_labels list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gamete_label
|
str
|
String label for gamete origin. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Assigned integer index for the gamete label. |
Source code in src/natal/registry/index.py
register_somatic_label
Register a somatic label (slab) and return its index.
Adapted to use the new slab_labels list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
somatic_label
|
str
|
String label for somatic state. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Assigned integer index for the somatic label. |
Source code in src/natal/registry/index.py
num_genotypes
Return the number of unique diploid genotypes (derived from ZTypes).
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Count of unique diploid genotypes in the ZType space. |
num_haplogenotypes
Return the number of unique haploid genotypes (derived from GTypes).
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Count of unique haploid genotypes in the GType space. |
ztype_index
O(1) dict lookup for a ZType index.
Replaces the formula g * n_slabs + slab.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
genotype
|
Genotype
|
A |
required |
slab_label
|
str
|
Somatic label string. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The ZType index. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the (genotype, slab_label) pair is not registered. |
Source code in src/natal/registry/index.py
resolve_ztype_indices
Return all ZType indices matching a ZygoteTypePattern.
Iterates _index_to_ztype and tests each (genotype, slab_label)
pair against pattern. When pattern has no slab constraint
(slab is None), all slab variants of matching genotypes match.
Source code in src/natal/registry/index.py
resolve_default_ztype_index
Return the first ZType index matching a ZygoteTypePattern.
Used by initial_state which places individuals in the first
(default) slab when no @slab is specified.
Source code in src/natal/registry/index.py
ztype_indices_for
Return all ZType indices for a given Genotype object.
Scans _index_to_ztype — does NOT require species.unordered_genotype()
because both the stored and input genotypes are already canonicalized
via Genotype.__new__ cache key normalization.
Source code in src/natal/registry/index.py
gtype_indices_for
Return all GType indices for a given HaploidGenotype object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
haplo
|
HaploidGenotype
|
A HaploidGenotype instance. |
required |
Returns:
| Type | Description |
|---|---|
list[int]
|
List of GType indices for this haplotype (one per glab label). |
Source code in src/natal/registry/index.py
gtype_index
O(1) dict lookup for a GType index.
Replaces the formula compress_hg_glab(hg, glab, n_glabs).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
haplo
|
HaploidGenotype
|
A |
required |
glab_label
|
str
|
Gamete label string. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The GType index. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the (haplo, glab_label) pair is not registered. |
Source code in src/natal/registry/index.py
compress
Permanently remove pruned ZType and GType entries from the registry.
Both masks use -1 for pruned entries and >=0 for surviving entries (the value is the new compressed index).
Unlike the old formula-based compress, this operates directly on the flat ZType/GType spaces — individual (genotype, slab) ZTypes can be pruned independently.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ztype_mask
|
NDArray[int32]
|
|
required |
gtype_mask
|
NDArray[int32]
|
|
required |