mal_core.habitat.potential

Static breeding-habitat potential H[i,g] per proxy habitat type.

The only knowledge this module has is the plan-defined mapping from the contract v3.0 per-cell feature columns to the six H values (plan docs/plans/in-process/redesign/02-oferta-criaderos-proxies-download-ingest-abm.md §6.2-§6.4). It is an INDEX: a relative offer potential in [0, 1]; it is not a measured pool count, depth or water volume, and the plan explicitly rejects interpreting it as such.

Mapping (per cell i; features are column numpy arrays of the env NC statics):

P := jrc_persistent          (JRC occurrence >= 80 %, post-coast-filter fraction)
V := lc_wetland              (Dynamic World flooded-vegetation area fraction)
U := lc_built                (Dynamic World built-up area fraction)
O := lc_open_plus_water      (grass + shrub + bare + water area fraction)
C := lc_crop                 (Dynamic World crops area fraction)

H_surface_persistent = P
H_wetland_persistent = V
H_urban_persistent   = urban.beta_urban * U
H_wetland_pluvial    = V
H_open_pluvial       = O + urban.rain_pools_built_damage * U   (aligned with the engine)
H_crop_pluvial       = C

Binding rules (plan §6.4 + project rule #167):

  • The urban persistent contribution is NOT gated by JRC or rainfall: a cell with built cover and zero JRC water keeps a small persistent offer for the whole dry season.
  • H is clipped to [0, 1]; nan features are treated as 0 (absence of land-cover evidence, not a wet signal). NaN counts of valid Dynamic World pixels are NOT used to rescale here — quality lives in the writer.
  • No alpha/theta matrix: the plan dropped the phase-1 linear-weight design; if a future weighting needs fitting, it must NOT resurrect the alpha matrix without a new user decision.
  1"""Static breeding-habitat potential H[i,g] per proxy habitat type.
  2
  3The only knowledge this module has is the plan-defined mapping from the
  4contract v3.0 per-cell feature columns to the six H values (plan
  5docs/plans/in-process/redesign/02-oferta-criaderos-proxies-download-ingest-abm.md
  6§6.2-§6.4). It is an INDEX: a relative offer potential in [0, 1]; it is not
  7a measured pool count, depth or water volume, and the plan explicitly
  8rejects interpreting it as such.
  9
 10Mapping (per cell i; features are column numpy arrays of the env NC statics):
 11
 12    P := jrc_persistent          (JRC occurrence >= 80 %, post-coast-filter fraction)
 13    V := lc_wetland              (Dynamic World flooded-vegetation area fraction)
 14    U := lc_built                (Dynamic World built-up area fraction)
 15    O := lc_open_plus_water      (grass + shrub + bare + water area fraction)
 16    C := lc_crop                 (Dynamic World crops area fraction)
 17
 18    H_surface_persistent = P
 19    H_wetland_persistent = V
 20    H_urban_persistent   = urban.beta_urban * U
 21    H_wetland_pluvial    = V
 22    H_open_pluvial       = O + urban.rain_pools_built_damage * U   (aligned with the engine)
 23    H_crop_pluvial       = C
 24
 25Binding rules (plan §6.4 + project rule #167):
 26* The urban persistent contribution is NOT gated by JRC or rainfall: a cell
 27  with built cover and zero JRC water keeps a small persistent offer for the
 28  whole dry season.
 29* H is clipped to [0, 1]; nan features are treated as 0 (absence of
 30  land-cover evidence, not a wet signal). NaN counts of valid Dynamic World
 31  pixels are NOT used to rescale here — quality lives in the writer.
 32* No alpha/theta matrix: the plan dropped the phase-1 linear-weight design;
 33  if a future weighting needs fitting, it must NOT resurrect the alpha
 34  matrix without a new user decision.
 35"""
 36from __future__ import annotations
 37
 38from typing import Mapping
 39
 40import numpy as np
 41
 42from .config import CONFIG, habitat_names
 43
 44HABITAT_TYPES = habitat_names()
 45_BETA_URBAN = float(CONFIG["urban"]["beta_urban"])
 46_RAIN_POOLS_BUILT_DAMAGE = float(
 47    CONFIG["urban"]["rain_pools_built_damage"])
 48
 49
 50def static_potential(
 51    features: Mapping[str, np.ndarray | float],
 52) -> dict[str, np.ndarray | float]:
 53    """Compute the six H[i,g] values from contract v3.0 feature columns.
 54
 55    Args:
 56        features: mapping ``feature_column`` -> per-cell array/scalar.
 57            Missing or non-finite values are treated as 0. Extra keys are
 58            ignored; missing required keys raise KeyError (the ingest
 59            builder is contract-strict).
 60
 61    Returns:
 62        mapping habitat type -> per-cell H array/scalar clipped [0, 1].
 63    """
 64    p = _feature(features, "jrc_persistent")
 65    v = _feature(features, "lc_wetland")
 66    u = _feature(features, "lc_built")
 67    o = _feature(features, "lc_open_plus_water")
 68    c = _feature(features, "lc_crop")
 69
 70    out: dict[str, np.ndarray | float] = {
 71        "surface_persistent": p,
 72        "wetland_persistent": v,
 73        "urban_persistent": _clip(_BETA_URBAN * u),
 74        "wetland_pluvial": v,
 75        "open_pluvial": _clip(o + _RAIN_POOLS_BUILT_DAMAGE * u),
 76        "crop_pluvial": c,
 77    }
 78    assert set(out) == set(HABITAT_TYPES)
 79    return out
 80
 81
 82def total_potential(h: Mapping[str, np.ndarray | float]) -> np.ndarray | float:
 83    """Sum of the six H values (used as the candidate filter and as the
 84    oviposition-attraction amplitude). Clipped to [0, 1]."""
 85    total = None
 86    for g in HABITAT_TYPES:
 87        val = _clip(h[g])
 88        total = val if total is None else total + val
 89    return _clip(total)
 90
 91
 92def _feature(
 93    features: Mapping[str, np.ndarray | float], name: str
 94) -> np.ndarray | float:
 95    """Return one contract v3.0 feature column, clipped to [0, 1].
 96
 97    Raises KeyError when the column is absent from the mapping.
 98    """
 99    if name not in features:
100        raise KeyError(
101            f"missing contract v3.0 feature column '{name}' "
102            f"(present: {sorted(features)})"
103        )
104    return _clip(features[name])
105
106
107def _clip(x):
108    """Return ``x`` as float64 with NaN replaced by 0.0 and values clipped to [0, 1]."""
109    import numpy as _np
110
111    arr = _np.asarray(x, dtype=_np.float64)
112    arr = _np.nan_to_num(arr, nan=0.0)
113    return _np.clip(arr, 0.0, 1.0)
114
115
116__all__ = ["static_potential", "total_potential"]
def static_potential( features: Mapping[str, numpy.ndarray | float]) -> dict[str, numpy.ndarray | float]:
51def static_potential(
52    features: Mapping[str, np.ndarray | float],
53) -> dict[str, np.ndarray | float]:
54    """Compute the six H[i,g] values from contract v3.0 feature columns.
55
56    Args:
57        features: mapping ``feature_column`` -> per-cell array/scalar.
58            Missing or non-finite values are treated as 0. Extra keys are
59            ignored; missing required keys raise KeyError (the ingest
60            builder is contract-strict).
61
62    Returns:
63        mapping habitat type -> per-cell H array/scalar clipped [0, 1].
64    """
65    p = _feature(features, "jrc_persistent")
66    v = _feature(features, "lc_wetland")
67    u = _feature(features, "lc_built")
68    o = _feature(features, "lc_open_plus_water")
69    c = _feature(features, "lc_crop")
70
71    out: dict[str, np.ndarray | float] = {
72        "surface_persistent": p,
73        "wetland_persistent": v,
74        "urban_persistent": _clip(_BETA_URBAN * u),
75        "wetland_pluvial": v,
76        "open_pluvial": _clip(o + _RAIN_POOLS_BUILT_DAMAGE * u),
77        "crop_pluvial": c,
78    }
79    assert set(out) == set(HABITAT_TYPES)
80    return out

Compute the six H[i,g] values from contract v3.0 feature columns.

Args: features: mapping feature_column -> per-cell array/scalar. Missing or non-finite values are treated as 0. Extra keys are ignored; missing required keys raise KeyError (the ingest builder is contract-strict).

Returns: mapping habitat type -> per-cell H array/scalar clipped [0, 1].

def total_potential(h: Mapping[str, numpy.ndarray | float]) -> numpy.ndarray | float:
83def total_potential(h: Mapping[str, np.ndarray | float]) -> np.ndarray | float:
84    """Sum of the six H values (used as the candidate filter and as the
85    oviposition-attraction amplitude). Clipped to [0, 1]."""
86    total = None
87    for g in HABITAT_TYPES:
88        val = _clip(h[g])
89        total = val if total is None else total + val
90    return _clip(total)

Sum of the six H values (used as the candidate filter and as the oviposition-attraction amplitude). Clipped to [0, 1].