Individual-Based Model (IBM) API Reference¶
Super-individual tracking with bioenergetics, predation, reproduction, behavior, and spatial integration.
Base Classes¶
pypath.ibm.base ¶
Base data structures for the IBM (Individual-Based Model) module.
Provides the foundational dataclasses and abstract base class used by all IBM group implementations in PyPath. These structures define how individual organisms (super-individuals) are represented and how IBM groups interface with the Ecosim population dynamics engine.
Classes:
| Name | Description |
|---|---|
SuperIndividual |
Represents a cohort of biologically identical organisms. |
IBMStepResult |
Return type for a single IBM integration step. |
IBMGroup |
Abstract base class that all concrete IBM implementations must subclass. |
IBMGroup ¶
Bases: ABC
Abstract base class for IBM group implementations.
Every IBM-managed functional group must subclass IBMGroup and
implement all four abstract methods. The Ecosim integration loop
calls these methods to delegate dynamics to the IBM engine while
keeping the rest of the food-web coupled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group_index
|
int
|
One-based index of this group in the Ecopath/Ecosim model (0 is reserved for the "Outside" placeholder). |
required |
n_groups
|
int
|
Total number of functional groups in the model (used to size consumption arrays). |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
group_index |
int
|
One-based index of this group. |
n_groups |
int
|
Total number of groups in the model. |
individuals |
List[SuperIndividual]
|
Population of super-individuals managed by this group. |
Source code in pypath/ibm/base.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | |
compute_step
abstractmethod
¶
compute_step(prey_available: ndarray, predation_pressure: float, env_forcing: Dict[str, Any], dt: float, spatial_context: Optional['SpatialContext'] = None) -> IBMStepResult
Advance the IBM population by one time step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prey_available
|
ndarray
|
1-D array of shape |
required |
predation_pressure
|
float
|
Total predation mortality pressure on this group from other predators in the Ecosim food web. |
required |
env_forcing
|
Dict[str, Any]
|
Dictionary of environmental forcing values (temperature, etc.). |
required |
dt
|
float
|
Time step size (years). |
required |
spatial_context
|
SpatialContext
|
Spatial patch data for Ecospace simulations. When |
None
|
Returns:
| Type | Description |
|---|---|
IBMStepResult
|
Aggregated results of this time step. |
Source code in pypath/ibm/base.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | |
get_aggregate_biomass
abstractmethod
¶
get_aggregate_biomass() -> float
Return the total biomass (tonnes) of all super-individuals.
Returns:
| Type | Description |
|---|---|
float
|
Sum of biomass across all super-individuals. |
Source code in pypath/ibm/base.py
209 210 211 212 213 214 215 216 217 | |
get_consumption_by_prey
abstractmethod
¶
get_consumption_by_prey() -> np.ndarray
Return the consumption vector by prey group.
Returns:
| Type | Description |
|---|---|
ndarray
|
1-D array of shape |
Source code in pypath/ibm/base.py
219 220 221 222 223 224 225 226 227 228 | |
initialize_from_ecosim
abstractmethod
¶
initialize_from_ecosim(biomass: float, params: Dict[str, Any], n_super_individuals: int = 500) -> None
Initialize the IBM population from Ecosim equilibrium state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
biomass
|
float
|
Initial total biomass (tonnes) from Ecopath. |
required |
params
|
Dict[str, Any]
|
Species-specific biological parameters. |
required |
n_super_individuals
|
int
|
Number of super-individuals to create (default 500). |
500
|
Source code in pypath/ibm/base.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | |
IBMStepResult
dataclass
¶
Result of a single IBM integration step.
Returned by :meth:IBMGroup.compute_step to communicate the IBM
state back to the Ecosim integration loop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
biomass
|
float
|
Total group biomass (tonnes) after this step. |
required |
production
|
float
|
Net production during this step (tonnes). |
required |
consumption_by_prey
|
ndarray
|
1-D array of shape |
required |
mortality_count
|
float
|
Number of individuals that died during this step. |
required |
recruitment_count
|
float
|
Number of new individuals recruited during this step. |
required |
Source code in pypath/ibm/base.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | |
SpatialContext
dataclass
¶
Spatial data passed to IBM groups during Ecospace simulations.
When an IBM group is part of a spatial simulation, this context provides the patch-level environmental information needed for movement decisions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adjacency
|
Any
|
Sparse adjacency matrix of shape |
required |
habitat_quality
|
ndarray
|
Per-patch habitat quality for this group, shape |
required |
food_density
|
ndarray
|
Per-patch total prey biomass, shape |
required |
predator_density
|
ndarray
|
Per-patch total predator biomass, shape |
required |
n_patches
|
int
|
Number of spatial patches. |
required |
Source code in pypath/ibm/base.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
SuperIndividual
dataclass
¶
A super-individual representing a cohort of identical organisms.
Each super-individual tracks the state of n_represented biologically identical fish (or other organisms). This is the fundamental unit of the IBM module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
int
|
Unique identifier for this super-individual. |
required |
n_represented
|
float
|
Number of real individuals this super-individual represents. |
required |
weight
|
float
|
Individual body weight (grams). |
required |
length
|
float
|
Individual body length (cm). |
required |
age
|
float
|
Age (years). |
required |
energy_reserve
|
float
|
Dimensionless energy reserve index (0-1 typical range). |
required |
patch_idx
|
int
|
Spatial patch index where this super-individual currently resides. |
required |
is_mature
|
bool
|
Whether this super-individual has reached sexual maturity. |
required |
sex
|
int
|
Sex code (0 = female, 1 = male, or other coding as needed). |
required |
Source code in pypath/ibm/base.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | |
total_biomass_tonnes ¶
total_biomass_tonnes() -> float
Return total biomass represented by this super-individual in tonnes.
Computed as n_represented * weight / 1e6, assuming weight is in
grams and 1 tonne = 1e6 grams.
Returns:
| Type | Description |
|---|---|
float
|
Total biomass in metric tonnes. |
Source code in pypath/ibm/base.py
71 72 73 74 75 76 77 78 79 80 81 82 | |
Bioenergetics¶
pypath.ibm.bioenergetics ¶
Wisconsin bioenergetics model for the IBM integration.
Implements the energy-budget approach used to drive individual fish growth in the Individual-Based Model. Each super-individual's weight and energy reserve are updated each timestep based on consumption, metabolism, specific dynamic action (SDA), and (for mature fish) reproduction costs.
The core equation follows the Wisconsin model framework:
net_energy = assimilated_consumption - metabolism - SDA - reproduction_cost
Temperature dependence is modelled with a Q10 formulation, and allometric scaling converts weight to length.
Functions:
| Name | Description |
|---|---|
q10_temperature_factor |
Compute Q10 temperature scaling factor. |
allometric_length |
Convert weight to length using an allometric power law. |
metabolism |
Compute standard metabolic rate. |
assimilation |
Compute assimilated consumption. |
growth_step |
Advance weight and energy reserve by one timestep. |
Classes:
| Name | Description |
|---|---|
BioenergParams |
Dataclass holding all bioenergetics parameters for a species. |
BioenergParams
dataclass
¶
Parameters for the Wisconsin bioenergetics model.
Holds species-specific constants that govern metabolism, assimilation, growth, and reproduction in the IBM.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ra
|
float
|
Metabolic rate intercept (g O2 / g fish / day at reference temperature). |
required |
rb
|
float
|
Metabolic rate weight exponent (typically negative, indicating per-gram metabolic rate decreases with body size). |
required |
q10
|
float
|
Q10 temperature coefficient -- the factor by which metabolic rate increases for every 10 degree C rise in temperature. |
required |
t_ref
|
float
|
Reference temperature (degrees C) at which |
required |
sda_fraction
|
float
|
Specific dynamic action fraction (0-1). The proportion of consumption allocated to the energetic cost of digestion. |
required |
unassimilated_fraction
|
float
|
Fraction of consumption that is not assimilated (0-1), i.e. lost as faeces and excretion. |
required |
a_length
|
float
|
Allometric coefficient for the weight-to-length conversion (length = a_length * weight ** b_length). |
required |
b_length
|
float
|
Allometric exponent for the weight-to-length conversion. |
required |
energy_density
|
float
|
Energy content per gram of fish tissue (kJ/g). Used to convert net energy (kJ) to weight change (g). Default is 5.0. |
5.0
|
reproduction_fraction
|
float
|
Fraction of net surplus energy allocated to reproduction when the individual is mature. Default is 0.3. |
0.3
|
Source code in pypath/ibm/bioenergetics.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | |
allometric_length ¶
allometric_length(weight: float, a: float, b: float) -> float
Convert body weight to body length using an allometric power law.
Computes length = a * weight ** b. Returns 0.0 if weight is zero
or negative.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weight
|
float
|
Individual body weight (grams). |
required |
a
|
float
|
Allometric coefficient. |
required |
b
|
float
|
Allometric exponent. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Body length (cm), or 0.0 if weight <= 0. |
Source code in pypath/ibm/bioenergetics.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | |
assimilation ¶
assimilation(consumption: float, params: BioenergParams) -> float
Compute the assimilated portion of consumption.
Removes the unassimilated fraction (faeces + excretion) from total consumption:
assimilated = consumption * (1 - unassimilated_fraction)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
consumption
|
float
|
Total consumption (energy or mass units). |
required |
params
|
BioenergParams
|
Bioenergetics parameters. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Assimilated consumption. |
Source code in pypath/ibm/bioenergetics.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |
growth_step ¶
growth_step(weight: float, energy_reserve: float, consumption: float, temperature: float, is_mature: bool, dt: float, params: BioenergParams) -> tuple[float, float]
Advance an individual's weight and energy reserve by one timestep.
Implements the Wisconsin bioenergetics budget for a single integration step. The energy budget is:
assim = assimilation(consumption)
sda = consumption * sda_fraction
met = metabolism(weight, temperature) * dt * 365
net = assim - met - sda
reproduction_cost = net * reproduction_fraction (if mature and net > 0)
weight_change = net / energy_density
The dt * 365 factor converts the daily metabolic rate (ra) to
the appropriate fraction of a year represented by dt.
Surplus energy is added to the energy reserve; deficits drain the reserve first before reducing body weight. Weight is clamped to a minimum of 0.1 grams.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weight
|
float
|
Current individual body weight (grams). |
required |
energy_reserve
|
float
|
Current energy reserve (dimensionless index). |
required |
consumption
|
float
|
Total consumption during this timestep. |
required |
temperature
|
float
|
Current water temperature (degrees C). |
required |
is_mature
|
bool
|
Whether the individual has reached sexual maturity. |
required |
dt
|
float
|
Timestep size (fraction of a year). |
required |
params
|
BioenergParams
|
Bioenergetics parameters. |
required |
Returns:
| Type | Description |
|---|---|
tuple[float, float]
|
|
Source code in pypath/ibm/bioenergetics.py
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | |
metabolism ¶
metabolism(weight: float, temperature: float, params: BioenergParams) -> float
Compute the standard metabolic rate for an individual.
Uses the allometric form with Q10 temperature dependence:
rate = ra * weight^rb * q10_factor
The result is in the same units as ra (g O2 / g fish / day) and
represents the per-gram daily metabolic cost scaled for temperature.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weight
|
float
|
Individual body weight (grams). |
required |
temperature
|
float
|
Current water temperature (degrees C). |
required |
params
|
BioenergParams
|
Bioenergetics parameters. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Metabolic rate (g O2 / g fish / day, temperature-adjusted). |
Source code in pypath/ibm/bioenergetics.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
q10_temperature_factor ¶
q10_temperature_factor(temp: float, t_ref: float, q10: float) -> float
Compute the Q10 temperature scaling factor.
The Q10 model scales a rate measured at t_ref to a new temperature
temp using the formula:
factor = q10 ** ((temp - t_ref) / 10.0)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
temp
|
float
|
Current water temperature (degrees C). |
required |
t_ref
|
float
|
Reference temperature (degrees C). |
required |
q10
|
float
|
Q10 coefficient (dimensionless, typically 1.5 -- 3.0). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Multiplicative scaling factor (1.0 at |
Source code in pypath/ibm/bioenergetics.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | |
Predation¶
pypath.ibm.predation ¶
Size-structured predation module for the IBM.
Distributes Ecosim group-level predation mortality across IBM super-individuals based on body size using a log-normal selectivity curve. Larger or smaller fish relative to the optimal prey length experience proportionally less predation pressure, reflecting size-dependent vulnerability to predators.
Functions:
| Name | Description |
|---|---|
size_selectivity |
Log-normal selectivity based on prey body length. |
distribute_mortality |
Allocate group-level mortality across super-individuals. |
apply_predation_mortality |
Apply predation mortality and return surviving individuals. |
Classes:
| Name | Description |
|---|---|
PredationParams |
Dataclass holding size-selectivity parameters. |
PredationParams
dataclass
¶
Parameters for size-structured predation selectivity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
optimal_prey_length
|
float
|
Prey body length (cm) at which predation selectivity is maximised. |
required |
selectivity_sd
|
float
|
Standard deviation of the log-normal selectivity curve (in log-space units). Larger values yield a flatter curve. |
required |
Source code in pypath/ibm/predation.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
apply_predation_mortality ¶
apply_predation_mortality(individuals: List[SuperIndividual], total_mortality_rate: float, dt: float, params: PredationParams) -> List[SuperIndividual]
Apply predation mortality and return surviving super-individuals.
Creates shallow copies of the input individuals, reduces their
n_represented by the allocated deaths, and removes any
individual whose n_represented drops to zero or below.
The original individuals list is not modified.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
individuals
|
List[SuperIndividual]
|
Current population of super-individuals (not modified). |
required |
total_mortality_rate
|
float
|
Annual mortality rate for the functional group (yr^-1). |
required |
dt
|
float
|
Time-step size (years). |
required |
params
|
PredationParams
|
Size-selectivity parameters. |
required |
Returns:
| Type | Description |
|---|---|
List[SuperIndividual]
|
Surviving super-individuals with updated |
Source code in pypath/ibm/predation.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | |
distribute_mortality ¶
distribute_mortality(individuals: List[SuperIndividual], total_mortality_rate: float, dt: float, params: PredationParams) -> List[float]
Distribute group-level mortality across super-individuals by size.
Deaths are allocated proportionally to each individual's
selectivity-weighted abundance (n_represented * selectivity).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
individuals
|
List[SuperIndividual]
|
Current population of super-individuals. |
required |
total_mortality_rate
|
float
|
Annual mortality rate for the functional group (yr^-1). |
required |
dt
|
float
|
Time-step size (years). |
required |
params
|
PredationParams
|
Size-selectivity parameters. |
required |
Returns:
| Type | Description |
|---|---|
List[float]
|
Number of deaths for each super-individual. Each entry is
capped at the individual's |
Source code in pypath/ibm/predation.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
size_selectivity ¶
size_selectivity(length: float, params: PredationParams) -> float
Compute log-normal size selectivity for a given prey length.
The selectivity peaks at 1.0 when length equals
params.optimal_prey_length and decays symmetrically in log-space
as the prey deviates from the optimal size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
length
|
float
|
Body length of the prey (cm). |
required |
params
|
PredationParams
|
Predation selectivity parameters. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Selectivity value in [0.0, 1.0]. Returns 0.0 if length <= 0. |
Source code in pypath/ibm/predation.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
Reproduction¶
pypath.ibm.reproduction ¶
Stochastic reproduction module for the IBM.
Implements spawning, fecundity calculation, and the Cushing match/mismatch hypothesis for larval survival in Baltic smelt. Mature females produce eggs proportional to body weight, and larval survival depends on the temporal overlap between hatching and zooplankton peak abundance.
Functions:
| Name | Description |
|---|---|
calculate_fecundity |
Weight-dependent egg production per female. |
larval_survival_probability |
Gaussian match/mismatch survival for larvae. |
spawn |
Determine total egg production for a super-individual. |
create_recruits |
Create new super-individual recruits from surviving larvae. |
Classes:
| Name | Description |
|---|---|
ReproductionParams |
Dataclass holding reproduction and larval survival parameters. |
ReproductionParams
dataclass
¶
Parameters for stochastic reproduction and larval survival.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fecundity_coefficient
|
float
|
Coefficient in the fecundity-weight relationship (eggs = coefficient * weight ^ exponent). |
required |
fecundity_exponent
|
float
|
Exponent in the fecundity-weight relationship. |
required |
larval_base_survival
|
float
|
Base survival probability at perfect match (0-1). |
required |
zooplankton_match_window
|
float
|
Width of the Gaussian match/mismatch window (days). |
required |
maturity_energy_threshold
|
float
|
Minimum energy reserve (kJ) required for spawning. |
required |
spawning_temp_threshold
|
float
|
Minimum water temperature (C) for spawning to occur. |
required |
larval_duration_days
|
int
|
Duration of the larval phase (days). |
required |
recruit_weight
|
float
|
Body weight (g) of a newly recruited individual. |
required |
recruit_length
|
float
|
Body length (cm) of a newly recruited individual. |
required |
Source code in pypath/ibm/reproduction.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
calculate_fecundity ¶
calculate_fecundity(weight: float, params: ReproductionParams) -> float
Calculate egg production for a single female of a given weight.
Uses a power-law relationship: eggs = coefficient * weight ^ exponent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weight
|
float
|
Individual body weight (g). |
required |
params
|
ReproductionParams
|
Reproduction parameters containing fecundity coefficient and exponent. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Number of eggs produced. Returns 0.0 if weight <= 0. |
Source code in pypath/ibm/reproduction.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
create_recruits ¶
create_recruits(total_eggs: float, spawn_day: float, zoo_peak_day: float, patch_idx: int, next_id: int, params: ReproductionParams, n_super_individuals: int = 1) -> List[SuperIndividual]
Create new super-individual recruits from surviving larvae.
Calculates the number of surviving larvae using
:func:larval_survival_probability, then distributes the survivors
evenly across n_super_individuals new :class:SuperIndividual
objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
total_eggs
|
float
|
Total number of eggs produced. |
required |
spawn_day
|
float
|
Day of year when spawning occurred. |
required |
zoo_peak_day
|
float
|
Day of year of the zooplankton abundance peak. |
required |
patch_idx
|
int
|
Spatial patch index where recruits are placed. |
required |
next_id
|
int
|
Starting ID for the new super-individuals. |
required |
params
|
ReproductionParams
|
Reproduction parameters. |
required |
n_super_individuals
|
int
|
Number of super-individuals to create (default 1). |
1
|
Returns:
| Type | Description |
|---|---|
List[SuperIndividual]
|
New recruit super-individuals. Empty list if total survivors < 1. |
Source code in pypath/ibm/reproduction.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | |
larval_survival_probability ¶
larval_survival_probability(spawn_day: float, zoo_peak_day: float, params: ReproductionParams) -> float
Compute larval survival probability using the Cushing match/mismatch hypothesis.
Survival follows a Gaussian function of the temporal mismatch between spawning and the zooplankton peak:
survival = base_survival * exp(-0.5 * (mismatch / match_window)^2)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spawn_day
|
float
|
Day of year when spawning occurs. |
required |
zoo_peak_day
|
float
|
Day of year of the zooplankton abundance peak. |
required |
params
|
ReproductionParams
|
Reproduction parameters containing base survival and match window. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Survival probability in (0, base_survival]. |
Source code in pypath/ibm/reproduction.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
spawn ¶
spawn(individual: SuperIndividual, temperature: float, params: ReproductionParams) -> float
Determine total egg production for a super-individual.
Only mature females (is_mature=True, sex=0) spawn, and only
when temperature and energy reserves meet the required thresholds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
individual
|
SuperIndividual
|
The super-individual attempting to spawn. |
required |
temperature
|
float
|
Current water temperature (C). |
required |
params
|
ReproductionParams
|
Reproduction parameters. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Total number of eggs produced (n_represented * fecundity_per_female). Returns 0.0 if spawning conditions are not met. |
Source code in pypath/ibm/reproduction.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
Behavior (Spatial Movement)¶
pypath.ibm.behavior ¶
Behavior module for the IBM: spatial movement and adaptive foraging.
Handles two key behaviors for IBM super-individuals:
-
Spatial movement between ECOSPACE patches, using a weighted score of habitat quality, food density, and predator avoidance to compute movement probabilities across a sparse adjacency graph.
-
Adaptive foraging (prey selection), where super-individuals allocate consumption proportionally to prey profitability while respecting availability constraints.
Functions:
| Name | Description |
|---|---|
calculate_movement_probabilities |
Compute per-patch movement probabilities for a super-individual. |
move_individual |
Move a super-individual to a new patch based on movement probabilities. |
should_migrate |
Determine whether migration conditions are met. |
adaptive_forage |
Allocate consumption across prey groups by profitability. |
Classes:
| Name | Description |
|---|---|
MovementParams |
Dataclass holding spatial movement parameters. |
ForagingParams |
Dataclass holding adaptive foraging parameters. |
ForagingParams
dataclass
¶
Parameters controlling adaptive prey selection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
energy_content
|
ndarray
|
Energy per gram of each prey group (kJ/g). Shape |
required |
handling_time
|
ndarray
|
Handling time per gram of each prey group. Shape |
required |
Source code in pypath/ibm/behavior.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 | |
MovementParams
dataclass
¶
Parameters controlling spatial movement between patches.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_speed
|
float
|
Base movement probability scaling (0-1). Low values increase the inertia bonus, making the individual more likely to stay. |
required |
habitat_weight
|
float
|
Weight for habitat quality in the movement score (0-1). |
required |
food_weight
|
float
|
Weight for food density in the movement score (0-1). |
required |
predator_weight
|
float
|
Weight for predator avoidance in the movement score (0-1). |
required |
migration_temp_threshold
|
float
|
Temperature (degrees C) above which spring migration can occur. |
required |
migration_months
|
tuple
|
Months (1-12) during which migration can occur. |
(3, 4, 5)
|
Source code in pypath/ibm/behavior.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
adaptive_forage ¶
adaptive_forage(prey_available: Dict[int, float], max_consumption: float, individual_length: float, params: ForagingParams) -> Dict[int, float]
Allocate consumption across prey groups by profitability.
Profitability of each prey group is defined as:
profitability = (energy_content / handling_time) * availability
Consumption is allocated proportionally to profitability, subject to availability constraints (cannot eat more than available for any group). When a group is availability-constrained, the surplus is redistributed to the remaining groups in a second pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prey_available
|
Dict[int, float]
|
Available biomass per prey group index. |
required |
max_consumption
|
float
|
Maximum total consumption for this individual. |
required |
individual_length
|
float
|
Body length of the individual (cm). Currently unused but reserved for future size-dependent foraging selectivity. |
required |
params
|
ForagingParams
|
Foraging parameters. |
required |
Returns:
| Type | Description |
|---|---|
Dict[int, float]
|
Consumption by prey group index. |
Source code in pypath/ibm/behavior.py
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | |
calculate_movement_probabilities ¶
calculate_movement_probabilities(current_patch: int, adjacency: csr_matrix, habitat_quality: ndarray, food_density: ndarray, predator_density: ndarray, params: MovementParams) -> np.ndarray
Compute per-patch movement probabilities for a super-individual.
For each reachable patch (current patch plus its neighbors in the sparse adjacency matrix), a weighted score is computed as:
score = habitat_weight * habitat_quality
+ food_weight * food_density
+ predator_weight * (1 / (1 + predator_density))
The current patch receives an inertia bonus proportional to
(1 - base_speed), making the individual more sedentary when
base_speed is low.
Scores are normalized to probabilities summing to 1.0. If all scores are zero, the individual stays in its current patch with probability 1.0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_patch
|
int
|
Index of the patch currently occupied. |
required |
adjacency
|
csr_matrix
|
Sparse adjacency matrix (n_patches x n_patches). |
required |
habitat_quality
|
ndarray
|
Per-patch habitat quality, shape |
required |
food_density
|
ndarray
|
Per-patch food density, shape |
required |
predator_density
|
ndarray
|
Per-patch predator density, shape |
required |
params
|
MovementParams
|
Movement parameters. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Probability array of shape |
Source code in pypath/ibm/behavior.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
move_individual ¶
move_individual(individual: SuperIndividual, adjacency: csr_matrix, habitat_quality: ndarray, food_density: ndarray, predator_density: ndarray, params: MovementParams, rng: Optional[Generator] = None) -> SuperIndividual
Move a super-individual to a new patch based on movement probabilities.
Computes movement probabilities via :func:calculate_movement_probabilities
and uses np.random.choice (or the supplied RNG) to select a destination
patch. Returns a copy of the individual with updated patch_idx; the
original is not modified.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
individual
|
SuperIndividual
|
The super-individual to move. |
required |
adjacency
|
csr_matrix
|
Sparse adjacency matrix. |
required |
habitat_quality
|
ndarray
|
Per-patch habitat quality. |
required |
food_density
|
ndarray
|
Per-patch food density. |
required |
predator_density
|
ndarray
|
Per-patch predator density. |
required |
params
|
MovementParams
|
Movement parameters. |
required |
rng
|
Generator
|
Random number generator. If |
None
|
Returns:
| Type | Description |
|---|---|
SuperIndividual
|
A copy with potentially updated |
Source code in pypath/ibm/behavior.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |
should_migrate ¶
should_migrate(temperature: float, month: int, params: MovementParams) -> bool
Determine whether migration conditions are met.
Migration occurs when the temperature is strictly above the threshold and the current month is in the configured migration months.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
temperature
|
float
|
Current water temperature (degrees C). |
required |
month
|
int
|
Current month (1-12). |
required |
params
|
MovementParams
|
Movement parameters containing threshold and migration months. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if migration conditions are met. |
Source code in pypath/ibm/behavior.py
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | |
EwE Integration¶
pypath.ibm.integration ¶
IBM-Ecosim derivative override integration.
Provides the bridge functions that allow IBM-managed functional groups to override the standard Ecosim derivative calculation. When an IBM group is active, these functions extract the relevant food-web data from the Ecosim consumption matrix, delegate the dynamics to the IBM engine, and write the results back into the derivative vector.
Functions:
| Name | Description |
|---|---|
extract_prey_availability |
Extract non-zero prey consumption rates for a given predator. |
extract_predation_pressure |
Sum total predation on a given prey from all living predators. |
check_ibm_mass_balance |
Validate that an IBM step result is physically plausible. |
apply_ibm_to_derivative |
Override the Ecosim derivative for an IBM group in-place. |
apply_ibm_to_derivative ¶
apply_ibm_to_derivative(deriv: ndarray, QQ: ndarray, BB: ndarray, ibm_group: 'IBMGroup', forcing: dict, dt: float, spatial_context: Optional['SpatialContext'] = None) -> None
Override the Ecosim derivative for an IBM-managed group in-place.
Calls the IBM group's compute_step method with prey availability
and predation pressure extracted from the current Ecosim state, then
writes the resulting biomass change into the derivative vector and
subtracts IBM consumption from prey derivatives.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
deriv
|
ndarray
|
Derivative vector (modified in-place). Shape |
required |
QQ
|
ndarray
|
Consumption matrix |
required |
BB
|
ndarray
|
Current biomass vector |
required |
ibm_group
|
IBMGroup
|
The IBM group instance that will compute the step. |
required |
forcing
|
dict
|
Environmental forcing dictionary passed through to |
required |
dt
|
float
|
Time step size in years. |
required |
spatial_context
|
SpatialContext or None
|
Spatial context for multi-patch IBM movement. When provided, it is
forwarded to |
None
|
Source code in pypath/ibm/integration.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | |
check_ibm_mass_balance ¶
check_ibm_mass_balance(result: 'IBMStepResult', tolerance: float = 0.05) -> Tuple[bool, float]
Validate that an IBM step result is physically plausible.
Checks that biomass is non-negative and that no consumption entry is negative. The relative error returned is the magnitude of the largest violation (or 0.0 if none).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
IBMStepResult
|
The result from an IBM integration step. |
required |
tolerance
|
float
|
Maximum acceptable relative error (default 0.05). |
0.05
|
Returns:
| Type | Description |
|---|---|
Tuple[bool, float]
|
|
Source code in pypath/ibm/integration.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | |
extract_predation_pressure ¶
extract_predation_pressure(QQ: ndarray, prey_idx: int, n_living: int) -> float
Sum total predation on a prey group from all living predators.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
QQ
|
ndarray
|
Consumption matrix of shape |
required |
prey_idx
|
int
|
1-based index of the prey group. |
required |
n_living
|
int
|
Number of living groups in the model. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Sum of |
Source code in pypath/ibm/integration.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | |
extract_prey_availability ¶
extract_prey_availability(QQ: ndarray, predator_idx: int, n_groups: int) -> Dict[int, float]
Extract non-zero prey consumption rates for a predator.
Reads column predator_idx of the consumption matrix QQ and
returns a dictionary mapping each prey index to its consumption rate,
excluding entries that are exactly zero.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
QQ
|
ndarray
|
Consumption matrix of shape |
required |
predator_idx
|
int
|
1-based index of the predator group. |
required |
n_groups
|
int
|
Total number of functional groups (excluding the 0-index padding). |
required |
Returns:
| Type | Description |
|---|---|
Dict[int, float]
|
Mapping |
Source code in pypath/ibm/integration.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | |
Smelt Implementation¶
pypath.ibm.smelt ¶
SmeltIBM concrete implementation for Baltic smelt (Osmerus eperlanus).
Orchestrates all IBM behavior modules -- bioenergetics, predation, foraging, reproduction, and growth -- into a single cohesive IBM group that can be injected into the Ecosim derivative loop.
The SmeltIBM is initialized from Ecopath equilibrium biomass and creates an age-structured population of super-individuals using Von Bertalanffy growth curves. Each time step, it runs up to five phases:
- Forage + Grow: adaptive foraging followed by Wisconsin bioenergetics.
- Reproduce: mature females spawn; surviving larvae become recruits.
- Predation mortality: size-structured mortality from Ecosim predators.
- Bookkeeping: add recruits, age individuals, remove senescent fish.
- Spatial movement (optional): move individuals between patches.
Classes:
| Name | Description |
|---|---|
SmeltParams |
Composite parameter dataclass combining all IBM sub-module parameters. |
SmeltIBM |
Concrete IBMGroup implementation for Baltic smelt. |
SmeltIBM ¶
Bases: IBMGroup
Concrete IBM group implementation for Baltic smelt.
Orchestrates bioenergetics, predation, foraging, and reproduction modules for an age-structured population of super-individuals representing Osmerus eperlanus.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group_index
|
int
|
One-based index of this group in the Ecopath/Ecosim model (0 is reserved for the "Outside" placeholder). |
required |
n_groups
|
int
|
Total number of functional groups in the model. |
required |
params
|
SmeltParams
|
Species-specific parameters for all IBM sub-modules. |
required |
Source code in pypath/ibm/smelt.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 | |
compute_step ¶
compute_step(prey_available: ndarray, predation_pressure: float, env_forcing: Dict[str, Any], dt: float, spatial_context: Optional[SpatialContext] = None) -> IBMStepResult
Advance the SmeltIBM population by one time step.
Executes up to five phases:
- Forage + Grow: For each individual, compute adaptive foraging allocation, then update weight and energy via bioenergetics.
- Reproduce: Mature females spawn; surviving larvae create recruits.
- Predation mortality: Apply size-structured predation.
- Bookkeeping: Age individuals, add recruits, remove senescent fish.
- Spatial movement (optional): Move individuals between patches when a spatial context is provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prey_available
|
ndarray
|
1-D array of shape |
required |
predation_pressure
|
float
|
Total predation mortality rate on this group (yr^-1). |
required |
env_forcing
|
Dict[str, Any]
|
Environmental forcing with keys like |
required |
dt
|
float
|
Time step size (fraction of a year). |
required |
spatial_context
|
SpatialContext
|
Spatial patch data for Ecospace simulations. When |
None
|
Returns:
| Type | Description |
|---|---|
IBMStepResult
|
Aggregated results of this time step. |
Source code in pypath/ibm/smelt.py
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 | |
get_aggregate_biomass ¶
get_aggregate_biomass() -> float
Return total biomass (tonnes) across all super-individuals.
Returns:
| Type | Description |
|---|---|
float
|
Sum of |
Source code in pypath/ibm/smelt.py
298 299 300 301 302 303 304 305 306 | |
get_consumption_by_prey ¶
get_consumption_by_prey() -> np.ndarray
Return the consumption vector from the last time step.
Returns:
| Type | Description |
|---|---|
ndarray
|
1-D array of shape |
Source code in pypath/ibm/smelt.py
308 309 310 311 312 313 314 315 316 317 | |
initialize_from_ecosim ¶
initialize_from_ecosim(biomass: float, params: Dict[str, Any], n_super_individuals: int = 500) -> None
Initialize an age-structured population from Ecosim equilibrium.
Creates n_super_individuals super-individuals with ages
distributed from 0.5 to max_age. Lengths are computed from the
Von Bertalanffy growth function, weights from inverse allometry,
and n_represented from an exponential survival curve scaled
so that total biomass matches the input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
biomass
|
float
|
Initial total biomass (tonnes) from Ecopath. |
required |
params
|
Dict[str, Any]
|
Additional species-specific parameters (currently unused;
all parameters come from |
required |
n_super_individuals
|
int
|
Number of super-individuals to create (default 500). |
500
|
Source code in pypath/ibm/smelt.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | |
SmeltParams
dataclass
¶
Composite parameters for the SmeltIBM.
Combines all sub-module parameter sets plus species-specific Von Bertalanffy growth parameters for Baltic smelt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bioenerg
|
BioenergParams
|
Wisconsin bioenergetics model parameters. |
required |
predation
|
PredationParams
|
Size-structured predation selectivity parameters. |
required |
foraging
|
ForagingParams
|
Adaptive prey-selection parameters. |
required |
movement
|
MovementParams
|
Spatial movement parameters. |
required |
reproduction
|
ReproductionParams
|
Spawning and larval survival parameters. |
required |
vbgf_k_mean
|
float
|
Mean Von Bertalanffy growth coefficient K (yr^-1). |
0.3
|
vbgf_k_sd
|
float
|
Standard deviation of K across individuals. |
0.05
|
vbgf_linf_mean
|
float
|
Mean asymptotic body length Linf (cm). |
25.0
|
vbgf_linf_sd
|
float
|
Standard deviation of Linf across individuals. |
3.0
|
max_age
|
float
|
Maximum age (years) before natural senescence removal. |
10.0
|
Source code in pypath/ibm/smelt.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | |
baltic_defaults
classmethod
¶
baltic_defaults() -> 'SmeltParams'
Return default parameters for Baltic smelt (Osmerus eperlanus).
These are literature-based defaults suitable for the Baltic Sea ecosystem. All sub-parameter sets are populated with species- specific values.
Returns:
| Type | Description |
|---|---|
SmeltParams
|
Fully populated parameter object. |
Source code in pypath/ibm/smelt.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | |