TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/sdarionicolas-boop/AgroIA-RAG/llms.txt
Use this file to discover all available pages before exploring further.
src/pipeline/agro_math.py module contains the core scoring mathematics for AgroIA. It defines the CONFIG dict that parameterises every crop, the calcular_score() function that translates satellite and climate measurements into a 0–100 score, and two NDVI validation helpers that guard against implausible Sentinel-2 readings before they reach the scoring layer. All functions in this module are pure Python and have no external side effects, making them easy to test in isolation.
CONFIG
TheCONFIG dict holds the agronomic parameters for each supported crop. Every key used by the pipeline (calcular_score, validar_ndvi, get_gee_ndvi_validado) is read from here, so modifying a value automatically propagates to all downstream calculations.
| Field | Type | Description |
|---|---|---|
tbase | int | Base temperature (°C) for heat-degree accumulation. |
umbral_calor | int | Temperature threshold (°C) above which heat stress is counted. |
umbral_clima | int | Maximum expected heat-stress hours; used to normalise the Climate score component. |
mes_critico | int | Month (1–12) of peak biomass. Sentinel-2 NDVI is retrieved for this month. |
pesos | dict[int, float] | Monthly weight multipliers for phenological relevance (informational; not used in calcular_score directly). |
color | str | Hex color for the crop in report visualisations. |
biblio | str | Bibliographic source for the agronomic parameters. |
ndvi_min | float | Minimum plausible NDVI for this crop. Values below this are flagged as sospechoso_bajo. |
ndvi_max | float | Maximum plausible NDVI. Values above this are flagged as sospechoso_alto. |
calcular_score
Calculates the AgroIA Score (0–100) from a single NDVI observation, a climate stress value, and the full NDVI historical series.Parameters
NDVI value for the current year’s critical month (0.0–1.0 range). Normalised against 0.9 to compute the Vigor component.
Accumulated heat-stress hours for the current year from NASA POWER. Used to compute the Climate component, normalised against
umbral_clima.List of NDVI values for the historical series including the current year. Used to compute Stability (requires ≥ 3 values) and Cleanliness via Isolation Forest (requires ≥ 4 values). Shorter lists receive partial scores: 15.0 for Stability and 10.0 for Cleanliness.
Maximum expected heat-stress hours for the crop. Taken from
CONFIG[cultivo]["umbral_clima"]. A value of 40 means that 40 or more hours of heat stress yields a Climate component of 0.Return value
A dict with five keys. All float values are rounded to one decimal place;
total is an integer.Full source
The Stability component caps the CV denominator at 0.45. This means a series with CV ≥ 0.45 (highly variable) scores 0 on Stability, while a perfectly stable series (CV = 0) scores the maximum 30 points.
validar_ndvi
Checks whether an NDVI value is within the agronomically plausible range for a given crop, year, and month. Called byget_gee_ndvi_validado before any value is used in scoring.
Parameters
The NDVI value to validate (expected range 0.0–1.0). Pass
None or float('nan') for null readings.Crop key used to look up
ndvi_min and ndvi_max from CONFIG. Falls back to CONFIG["maiz"] if the key is not found.Year of the observation. Used only for the human-readable message.
Month of the observation (1–12). Used only for the human-readable message.
Return value
One of four string values:
"ok"— value is within[ndvi_min, ndvi_max]for the crop."sospechoso_bajo"— value is belowndvi_min; excluded from scoring byget_gee_ndvi_validado."sospechoso_alto"— value exceedsndvi_max; returned but treated with caution."nulo"— value isNoneorNaN.
Human-readable diagnostic string including the year, month, and NDVI value.
get_gee_ndvi_validado
Fetches NDVI from Google Earth Engine for a polygon and immediately validates it. Returns a three-tuple so the pipeline can branch on the status without additional checks.Parameters
A Google Earth Engine
Geometry.Polygon representing the field boundary in WGS-84.Year of the requested observation.
Month (1–12) of the requested observation. Should be
CONFIG[cultivo]["mes_critico"] in normal usage.Crop key passed to
validar_ndvi to apply the correct plausibility bounds.Return value
Atuple[float | None, str, str]:
The validated NDVI value, or
None when the status is "sospechoso_bajo", "nulo", or "error".One of
"ok", "sospechoso_bajo", "sospechoso_alto", "nulo", or "error".Human-readable diagnostic from
validar_ndvi, or an error description if the GEE call raised an exception.Usage example: calculating score for a lot
Related pages
Score AgroIA concepts
Conceptual explanation of the four score components.
Pipeline module
How agro_math integrates into the full analysis pipeline.