population_state Module
Population state management and data structures.
Overview
The population_state module defines the runtime state of population simulations, including individual tracking, age structure, and genetic composition.
Complete Module Reference
natal.population_state
Population state containers based on NamedTuple.
These containers keep scalar metadata immutable while allowing in-place mutation of NumPy array contents, which remains compatible with Numba kernels.
PopulationState
Bases: NamedTuple
Age-structured state container.
Scalars are immutable (use _replace to rebuild); array values remain
mutable in-place.
Attributes:
| Name | Type | Description |
|---|---|---|
n_tick |
int
|
Current simulation time step. |
individual_count |
NDArray[float64]
|
Array of shape (n_sexes, n_ages, n_genotypes) – counts of individuals per sex, age, and genotype. |
sperm_storage |
NDArray[float64]
|
Array of shape (n_ages, n_genotypes, n_genotypes) – stored sperm counts per female age, female genotype, and male genotype. |
create
classmethod
create(n_genotypes: int, n_sexes: Optional[int] = None, n_ages: int = 2, n_tick: int = 0, individual_count: Optional[NDArray[float64]] = None, sperm_storage: Optional[NDArray[float64]] = None) -> PopulationState
Create a PopulationState with optionally provided arrays.
If arrays are not provided, they are initialised to zeros.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_genotypes
|
int
|
Number of diploid genotype types. |
required |
n_sexes
|
Optional[int]
|
Number of sexes (defaults to 2 if not given). |
None
|
n_ages
|
int
|
Number of age classes (default 2). |
2
|
n_tick
|
int
|
Initial tick value (default 0). |
0
|
individual_count
|
Optional[NDArray[float64]]
|
Optional array (n_sexes, n_ages, n_genotypes). |
None
|
sperm_storage
|
Optional[NDArray[float64]]
|
Optional array (n_ages, n_genotypes, n_genotypes). |
None
|
Returns:
| Type | Description |
|---|---|
PopulationState
|
A new PopulationState instance. |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If dimensions are invalid or provided arrays have wrong shape. |
Source code in src/natal/population_state.py
get_count
Retrieve the count of individuals for a specific category.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sex
|
int
|
Sex index. |
required |
age
|
int
|
Age class index. |
required |
genotype_index
|
int
|
Diploid genotype index. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The count (float). |
Source code in src/natal/population_state.py
add_count
Add to the count of individuals for a specific category.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sex
|
int
|
Sex index. |
required |
age
|
int
|
Age class index. |
required |
genotype_index
|
int
|
Diploid genotype index. |
required |
count
|
float
|
Amount to add (can be negative). |
required |
Source code in src/natal/population_state.py
set_count
Set the count of individuals for a specific category.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sex
|
int
|
Sex index. |
required |
age
|
int
|
Age class index. |
required |
genotype_index
|
int
|
Diploid genotype index. |
required |
count
|
float
|
New count. |
required |
Source code in src/natal/population_state.py
get_stored_sperm
Retrieve stored sperm count for a given combination.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
age
|
int
|
Age class of the female. |
required |
female_genotype_index
|
int
|
Female genotype index. |
required |
male_genotype_index
|
int
|
Male genotype index. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Stored sperm count. |
Source code in src/natal/population_state.py
set_stored_sperm
set_stored_sperm(age: int, female_genotype_index: int, male_genotype_index: int, count: float) -> None
Add to stored sperm count (in‑place addition).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
age
|
int
|
Age class of the female. |
required |
female_genotype_index
|
int
|
Female genotype index. |
required |
male_genotype_index
|
int
|
Male genotype index. |
required |
count
|
float
|
Amount to add (can be negative). |
required |
Source code in src/natal/population_state.py
flatten_all
Flatten the entire state into a single 1D array.
The order is: tick, then individual_count flattened (row‑major), then sperm_storage flattened.
Returns:
| Type | Description |
|---|---|
NDArray[float64]
|
1D array of floats. |
Source code in src/natal/population_state.py
DiscretePopulationState
Bases: NamedTuple
Discrete‑generation state container (no sperm storage).
Attributes:
| Name | Type | Description |
|---|---|---|
n_tick |
int
|
Current simulation time step. |
individual_count |
NDArray[float64]
|
Array of shape (n_sexes, n_ages, n_genotypes) – counts of individuals per sex, age, and genotype. |
create
classmethod
create(n_sexes: int, n_ages: int, n_genotypes: int, n_tick: int = 0, individual_count: Optional[NDArray[float64]] = None) -> DiscretePopulationState
Create a DiscretePopulationState with optionally provided array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_sexes
|
int
|
Number of sexes. |
required |
n_ages
|
int
|
Number of age classes. |
required |
n_genotypes
|
int
|
Number of diploid genotype types. |
required |
n_tick
|
int
|
Initial tick value (default 0). |
0
|
individual_count
|
Optional[NDArray[float64]]
|
Optional array (n_sexes, n_ages, n_genotypes); if None, filled with zeros. |
None
|
Returns:
| Type | Description |
|---|---|
DiscretePopulationState
|
A new DiscretePopulationState instance. |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If dimensions are invalid or array shape mismatch. |
Source code in src/natal/population_state.py
flatten_all
Flatten the entire state into a single 1D array.
The order is: tick, then individual_count flattened (row‑major).
Returns:
| Type | Description |
|---|---|
NDArray[float64]
|
1D array of floats. |
Source code in src/natal/population_state.py
to_plain_population_state
Convert a PopulationState to a plain (copied) instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
PopulationState
|
Input PopulationState. |
required |
copy
|
bool
|
If True, arrays are deep‑copied; otherwise they are referenced. |
True
|
Returns:
| Type | Description |
|---|---|
PlainPopulationState
|
A new PopulationState (or the same arrays if copy=False). |
Source code in src/natal/population_state.py
to_plain_discrete_population_state
to_plain_discrete_population_state(state: DiscretePopulationState, copy: bool = True) -> PlainDiscretePopulationState
Convert a DiscretePopulationState to a plain (copied) instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
DiscretePopulationState
|
Input DiscretePopulationState. |
required |
copy
|
bool
|
If True, the array is deep‑copied; otherwise it is referenced. |
True
|
Returns:
| Type | Description |
|---|---|
PlainDiscretePopulationState
|
A new DiscretePopulationState (or the same array if copy=False). |
Source code in src/natal/population_state.py
from_plain_population_state
Convert a plain PopulationState back (arrays are referenced, not copied).
Source code in src/natal/population_state.py
from_plain_discrete_population_state
from_plain_discrete_population_state(plain: PlainDiscretePopulationState) -> DiscretePopulationState
Convert a plain DiscretePopulationState back (array is referenced).
Source code in src/natal/population_state.py
parse_flattened_state
parse_flattened_state(flat_array: NDArray[float64], n_sexes: Union[int, integer], n_ages: Union[int, integer], n_genotypes: Union[int, integer], copy: bool = True) -> PopulationState
Reconstruct a PopulationState from a flattened array.
The flattened array must be in the format produced by flatten_all().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flat_array
|
NDArray[float64]
|
1D array containing tick, individual_count, sperm_storage. |
required |
n_sexes
|
Union[int, integer]
|
Number of sexes. |
required |
n_ages
|
Union[int, integer]
|
Number of age classes. |
required |
n_genotypes
|
Union[int, integer]
|
Number of diploid genotype types. |
required |
copy
|
bool
|
If True, arrays are deep‑copied; otherwise they are viewed. |
True
|
Returns:
| Type | Description |
|---|---|
PopulationState
|
A PopulationState instance. |
Source code in src/natal/population_state.py
parse_flattened_discrete_state
parse_flattened_discrete_state(flat_array: NDArray[float64], n_sexes: Union[int, integer], n_ages: Union[int, integer], n_genotypes: Union[int, integer], copy: bool = True) -> DiscretePopulationState
Reconstruct a DiscretePopulationState from a flattened array.
The flattened array must be in the format produced by flatten_all().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flat_array
|
NDArray[float64]
|
1D array containing tick and individual_count. |
required |
n_sexes
|
Union[int, integer]
|
Number of sexes. |
required |
n_ages
|
Union[int, integer]
|
Number of age classes. |
required |
n_genotypes
|
Union[int, integer]
|
Number of diploid genotype types. |
required |
copy
|
bool
|
If True, the array is deep‑copied; otherwise it is viewed. |
True
|
Returns:
| Type | Description |
|---|---|
DiscretePopulationState
|
A DiscretePopulationState instance. |