age_structured_population Module
Age-structured population models with overlapping generations.
Overview
The age_structured_population module implements age-structured population models with support for age-dependent survival, fecundity, and optional sperm-storage mechanics.
Complete Module Reference
natal.age_structured_population
Age-structured population models.
This module implements age-structured (overlapping generation) population models and utilities for survival, reproduction, juvenile recruitment, and fitness management.
AgeStructuredPopulation
AgeStructuredPopulation(species: Species, population_config: PopulationConfig, name: Optional[str] = None, initial_individual_count: Optional[Mapping[str, Mapping[Union[Genotype, str], Union[List[int], Dict[int, int]]]]] = None, initial_sperm_storage: Optional[Mapping[Union[Genotype, str], Mapping[Union[Genotype, str], Union[Dict[int, float], List[float], float]]]] = None, hooks: Optional[HookRegistrationMap] = None)
Bases: BasePopulation[PopulationState]
Age-structured population model (overlapping generations).
An age-structured population built on BasePopulation and
PopulationState. Supports age-dependent survival and fecundity,
juvenile recruitment modes, optional sperm-storage mechanics, and a
hook/modifier system for user extensions.
Attributes:
| Name | Type | Description |
|---|---|---|
snapshots |
Dict[str, object]
|
Storage for custom state snapshots. |
Initialize an age-structured population instance using a PopulationConfig.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
species
|
Species
|
Species object describing genetic architecture. |
required |
population_config
|
PopulationConfig
|
Fully initialized PopulationConfig instance. |
required |
name
|
Optional[str]
|
Human-readable population name. If None, uses "AgeStructuredPop". |
None
|
initial_individual_count
|
Optional[Mapping[str, Mapping[Union[Genotype, str], Union[List[int], Dict[int, int]]]]]
|
Initial population distribution. Format: {sex: {genotype: counts_by_age}} |
None
|
initial_sperm_storage
|
Optional[Mapping[Union[Genotype, str], Mapping[Union[Genotype, str], Union[Dict[int, float], List[float], float]]]]
|
Initial sperm storage state (if supported). |
None
|
hooks
|
Optional[HookRegistrationMap]
|
Event hook registrations to apply. |
None
|
Examples:
>>> pop_config = PopulationConfigBuilder.build(species, ...)
>>> pop = AgeStructuredPopulation(
... species,
... pop_config,
... name="MyPop",
... initial_individual_count={...}
... )
Source code in src/natal/age_structured_population.py
state
property
PopulationState: The current state container for the population.
new_adult_age
property
int: Minimum age at which individuals are considered adults.
genotypes_present
property
Set[Genotype]: Returns the set of genotypes with count > 0.
setup
classmethod
setup(species: Species, name: str = 'AgeStructuredPop', stochastic: bool = True, use_continuous_sampling: bool = False, gamete_labels: Optional[List[str]] = None, use_fixed_egg_count: bool = False) -> AgeStructuredPopulationBuilder
Create and preconfigure an age-structured population builder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
species
|
Species
|
Species definition used to initialize the builder. |
required |
name
|
str
|
Population name. |
'AgeStructuredPop'
|
stochastic
|
bool
|
Whether to use stochastic sampling. |
True
|
use_continuous_sampling
|
bool
|
Whether to use Dirichlet sampling. |
False
|
gamete_labels
|
Optional[List[str]]
|
Optional labels for gamete tracking. |
None
|
use_fixed_egg_count
|
bool
|
Whether egg count is deterministic. |
False
|
Returns:
| Type | Description |
|---|---|
AgeStructuredPopulationBuilder
|
A configured |
Examples:
AgeStructuredPopulation.setup(species).age_structure(...).initial_state(...).build()
Source code in src/natal/age_structured_population.py
reset
Reset the population to its initial state.
Restores individual counts and sperm storage to original values.
Source code in src/natal/age_structured_population.py
get_total_count
Return the total number of individuals in the population.
Returns:
| Name | Type | Description |
|---|---|---|
float |
int
|
Grand total across all sexes, ages, and genotypes. |
get_female_count
Return the total number of female individuals.
Returns:
| Name | Type | Description |
|---|---|---|
float |
int
|
Sum of all female individual counts. |
get_male_count
Return the total number of male individuals.
Returns:
| Name | Type | Description |
|---|---|---|
float |
int
|
Sum of all male individual counts. |
get_adult_count
Return the number of adult individuals for the given sex.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sex
|
str
|
One of |
'both'
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
int
|
Total number of adults for the requested sex(es). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the sex identifier is not recognized. |
Source code in src/natal/age_structured_population.py
export_config
Export population configuration to Config jitclass.
Returns:
| Name | Type | Description |
|---|---|---|
PopulationConfig |
PopulationConfig
|
A copy of the current population configuration. |
import_config
Import configuration into the population.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
PopulationConfig
|
Config jitclass instance. |
required |
Source code in src/natal/age_structured_population.py
create_history_snapshot
Record current state to history records.
Saves the current tick and a flattened copy of state to _history.
Source code in src/natal/age_structured_population.py
get_history
Return history records as a 2D NumPy array.
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Float64 array with shape
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If no history is recorded. |
Source code in src/natal/age_structured_population.py
clear_history
export_state
Export population state as a flattened array.
Returns:
| Type | Description |
|---|---|
Tuple[NDArray[float64], Optional[NDArray[float64]]]
|
Tuple[NDArray, Optional[NDArray]]: (state_flat, history). state_flat: [n_tick, ind_count.ravel(), sperm_storage.ravel()] history: Optional array of shape (n_snapshots, flatten_size). |
Source code in src/natal/age_structured_population.py
import_state
import_state(state: Union[PopulationState, NDArray[float64], Dict[str, ndarray], Tuple[ndarray, ndarray]], history: Optional[ndarray] = None) -> None
Import state and optional history records.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
Union[PopulationState, NDArray[float64], Dict[str, ndarray], Tuple[ndarray, ndarray]]
|
Flattened array, PopulationState object, or data dictionary. |
required |
history
|
Optional[ndarray]
|
Optional history 2D array. |
None
|
Source code in src/natal/age_structured_population.py
get_history_as_objects
Convert selected flattened snapshots back to PopulationState objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indices
|
Optional[List[int]]
|
List of snapshot indices to convert. If None, converts all. |
None
|
Returns:
| Type | Description |
|---|---|
List[Tuple[int, PopulationState]]
|
List[Tuple[int, PopulationState]]: List of (tick, state) tuples. |
Raises:
| Type | Description |
|---|---|
IndexError
|
If an index is out of range. |
Source code in src/natal/age_structured_population.py
restore_checkpoint
Restore the population to a specific history tick.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tick
|
int
|
The target tick number. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no record is found for the specified tick. |
Source code in src/natal/age_structured_population.py
run
run(n_steps: int, record_every: Optional[int] = None, finish: bool = False, clear_history_on_start: bool = False) -> AgeStructuredPopulation
Run multi-step evolution using optimized simulation kernels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_steps
|
int
|
Number of steps to evolve. |
required |
record_every
|
Optional[int]
|
Interval for recording snapshots. If None, uses self.record_every. If 0, no snapshots are recorded. |
None
|
finish
|
bool
|
Whether to mark the population as finished after the run. |
False
|
clear_history_on_start
|
bool
|
Whether to clear existing history before starting. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
AgeStructuredPopulation |
AgeStructuredPopulation
|
Self for chaining. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the population is already finished and cannot continue. |
Source code in src/natal/age_structured_population.py
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 | |
run_tick
Execute a single tick of evolution.
Returns:
| Name | Type | Description |
|---|---|---|
AgeStructuredPopulation |
AgeStructuredPopulation
|
Self for chaining. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the population is already finished and cannot continue. |
Source code in src/natal/age_structured_population.py
get_age_distribution
Return the age distribution for the requested sex.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sex
|
str
|
One of |
'both'
|
Returns:
| Type | Description |
|---|---|
ndarray
|
NDArray[np.float64]: Age distribution array with shape (n_ages,). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If sex identifier is invalid. |
Source code in src/natal/age_structured_population.py
get_genotype_count
Return total counts for a genotype as (female_count, male_count).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
genotype
|
Genotype
|
Target genotype instance. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[int, int]
|
Tuple[int,int]: |