Skip to content

terraflow.model

Suitability scoring is a normalized weighted composite of vegetation index, mean temperature, and total rainfall. Scores in [0, 1] are mapped to ordinal labels.

Public functions

  • suitability_score(v, t, r, params) — single-cell scalar score.
  • suitability_score_array(v, t, r, params) — vectorised NumPy version used by the pipeline.
  • suitability_label(score) — score → ordinal class.

API Reference

model

suitability_label(score)

Bucket suitability score into qualitative labels.

Parameters:

Name Type Description Default
score float

Suitability score in [0, 1] range.

required

Returns:

Name Type Description
str str

One of 'low', 'medium', or 'high' based on score thresholds.

Notes
  • 'low': score < 0.33
  • 'medium': 0.33 <= score < 0.66
  • 'high': score >= 0.66
Source code in terraflow/model.py
def suitability_label(score: float) -> str:
    """
    Bucket suitability score into qualitative labels.

    Parameters
    ----------
    score:
        Suitability score in [0, 1] range.

    Returns
    -------
    str:
        One of 'low', 'medium', or 'high' based on score thresholds.

    Notes
    -----
    - 'low': score < 0.33
    - 'medium': 0.33 <= score < 0.66
    - 'high': score >= 0.66
    """
    if score < 0.33:
        return "low"
    if score < 0.66:
        return "medium"
    return "high"

suitability_score(v_index, mean_temp, total_rain, params)

Compute a simple suitability score in [0, 1].

Combines normalized vegetation index, temperature, and rainfall using weighted linear combination. All inputs are normalized to [0, 1] range based on the parameter min/max bounds, then combined using the weights.

Parameters:

Name Type Description Default
v_index float

Vegetation index value (typically 0-1, but can be outside range).

required
mean_temp float

Mean temperature in degrees Celsius.

required
total_rain float

Total rainfall in millimeters.

required
params ModelParams

Model parameters containing min/max bounds and weights.

required

Returns:

Name Type Description
float float

Suitability score in [0, 1] range.

Notes
  • Out-of-range inputs are clipped to [0, 1] during normalization
  • Weights should sum to 1.0 for proper combination
  • Final result is clipped to [0, 1] range
Source code in terraflow/model.py
def suitability_score(
    v_index: float,
    mean_temp: float,
    total_rain: float,
    params: ModelParams,
) -> float:
    """
    Compute a simple suitability score in [0, 1].

    Combines normalized vegetation index, temperature, and rainfall using
    weighted linear combination. All inputs are normalized to [0, 1] range
    based on the parameter min/max bounds, then combined using the weights.

    Parameters
    ----------
    v_index:
        Vegetation index value (typically 0-1, but can be outside range).
    mean_temp:
        Mean temperature in degrees Celsius.
    total_rain:
        Total rainfall in millimeters.
    params:
        Model parameters containing min/max bounds and weights.

    Returns
    -------
    float:
        Suitability score in [0, 1] range.

    Notes
    -----
    - Out-of-range inputs are clipped to [0, 1] during normalization
    - Weights should sum to 1.0 for proper combination
    - Final result is clipped to [0, 1] range
    """
    v_n = normalize(v_index, params.v_min, params.v_max)
    t_n = normalize(mean_temp, params.t_min, params.t_max)
    r_n = normalize(total_rain, params.r_min, params.r_max)

    score = params.w_v * v_n + params.w_t * t_n + params.w_r * r_n
    return max(0.0, min(1.0, score))

suitability_score_array(v_index, mean_temp, total_rain, params)

Vectorized suitability scoring over numpy arrays.

Equivalent to calling :func:suitability_score element-wise but operates on arbitrary-shape numpy arrays. Used internally for Monte Carlo uncertainty propagation.

Parameters:

Name Type Description Default
v_index ndarray

Vegetation index values (any shape).

required
mean_temp ndarray

Mean temperature values in °C (same shape as v_index).

required
total_rain ndarray

Total rainfall values in mm (same shape as v_index).

required
params ModelParams

Model parameters containing min/max bounds and weights.

required

Returns:

Type Description
np.ndarray:

Suitability scores clipped to [0, 1], same shape as inputs.

Source code in terraflow/model.py
def suitability_score_array(
    v_index: "np.ndarray",
    mean_temp: "np.ndarray",
    total_rain: "np.ndarray",
    params: ModelParams,
) -> "np.ndarray":
    """Vectorized suitability scoring over numpy arrays.

    Equivalent to calling :func:`suitability_score` element-wise but operates
    on arbitrary-shape numpy arrays.  Used internally for Monte Carlo
    uncertainty propagation.

    Parameters
    ----------
    v_index:
        Vegetation index values (any shape).
    mean_temp:
        Mean temperature values in °C (same shape as *v_index*).
    total_rain:
        Total rainfall values in mm (same shape as *v_index*).
    params:
        Model parameters containing min/max bounds and weights.

    Returns
    -------
    np.ndarray:
        Suitability scores clipped to [0, 1], same shape as inputs.
    """

    def _norm(x: "np.ndarray", lo: float, hi: float) -> "np.ndarray":
        if hi == lo:
            return np.zeros_like(x, dtype=float)
        return np.clip((x - lo) / (hi - lo), 0.0, 1.0)

    score = (
        params.w_v * _norm(v_index, params.v_min, params.v_max)
        + params.w_t * _norm(mean_temp, params.t_min, params.t_max)
        + params.w_r * _norm(total_rain, params.r_min, params.r_max)
    )
    return np.clip(score, 0.0, 1.0)