terraflow.core¶
The core modules define the configuration schema and the end-to-end pipeline.
Quick Start¶
from terraflow.pipeline import main
from terraflow.config import load_config
# Load and validate configuration
config = load_config("config.yml")
# Run the complete pipeline
main("config.yml")
Module Organization
- config: YAML schema validation and Pydantic models
- pipeline: End-to-end orchestration and artifact generation
- model: Suitability scoring algorithms and label assignment
API Reference¶
config
¶
ClimateConfig
¶
Bases: BaseModel
Climate data configuration for spatial matching and interpolation.
Supports two strategies for aligning climate observations to raster cells: - 'spatial': Interpolate climate values using geographic coordinates (lat/lon). - 'index': Match cells to climate records by row index or explicit cell ID.
Attributes:
| Name | Type | Description |
|---|---|---|
strategy |
Literal['spatial', 'index']
|
Matching strategy: 'spatial' (interpolation) or 'index' (direct matching). |
cell_id_column |
str | None
|
Column name in climate CSV for cell ID (used with 'index' strategy). If None with 'index' strategy, uses row order matching. |
fallback_to_mean |
bool
|
If True, use global mean climate for cells outside interpolation range or when climate data is sparse (default True). |
validate_config()
¶
Validate consistency of climate configuration.
Source code in terraflow/config.py
validate_strategy(v)
classmethod
¶
Ensure strategy is valid.
ModelParams
¶
Bases: BaseModel
Parameters for normalization and weighting in the suitability model.
The weights w_v, w_t, w_r should sum to approximately 1.0 for proper normalization, though this is not strictly enforced.
Attributes:
| Name | Type | Description |
|---|---|---|
v_min, v_max |
Min/max vegetation index values for normalization. |
|
t_min, t_max |
Min/max temperature values for normalization (in °C). |
|
r_min, r_max |
Min/max rainfall values for normalization (in mm). |
|
w_v, w_t, w_r |
Weights for vegetation, temperature, and rainfall (should sum to 1.0). |
validate_max_values(v, info)
classmethod
¶
Ensure max values are reasonable numbers.
Source code in terraflow/config.py
validate_min_values(v, info)
classmethod
¶
Ensure min values are reasonable numbers.
Source code in terraflow/config.py
validate_ranges()
¶
Validate that min < max for all ranges. Call after initialization.
Source code in terraflow/config.py
PipelineConfig
¶
Bases: BaseModel
Top-level pipeline configuration.
Attributes:
| Name | Type | Description |
|---|---|---|
raster_path |
Path
|
Path to the input raster file (GeoTIFF). |
climate_csv |
Path
|
Path to the climate data CSV file. |
output_dir |
Path
|
Directory for output results. |
roi |
ROI
|
Region of interest specification. |
model_params |
ModelParams
|
Suitability model parameters. |
climate |
ClimateConfig
|
Climate data configuration (strategy, fallback behavior, etc.). |
max_cells |
int
|
Maximum number of cells to sample from the ROI (default 500). |
ROI
¶
Bases: BaseModel
Region of interest. For now we support only a bounding box.
Attributes:
| Name | Type | Description |
|---|---|---|
type |
Literal['bbox']
|
Type of ROI (currently only 'bbox' is supported). |
xmin, ymin, xmax, ymax |
Bounding box coordinates expressed in roi_crs (default WGS 84 degrees). |
|
roi_crs |
str
|
EPSG code or WKT string for the CRS of the bounding box coordinates.
Defaults to |
validate_bounds()
¶
Validate that bounds are sensible. Call after initialization.
Source code in terraflow/config.py
build_config(data)
¶
Validate a raw config dict into a PipelineConfig.
Source code in terraflow/config.py
load_config(path)
¶
Load YAML config from disk and validate with Pydantic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to the YAML configuration file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
PipelineConfig |
PipelineConfig
|
Validated configuration object. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError:
|
If the config file does not exist. |
yaml.YAMLError:
|
If the YAML is malformed. |
ValueError:
|
If the configuration is invalid. |
Source code in terraflow/config.py
load_config_dict(path)
¶
Load YAML config from disk into a raw dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to the YAML configuration file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
Parsed configuration as a Python dict. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError:
|
If the config file does not exist. |
yaml.YAMLError:
|
If the YAML is malformed. |
Source code in terraflow/config.py
pipeline
¶
run_pipeline(config_path)
¶
Run the end-to-end pipeline and return a DataFrame of results.
Uses spatially-aware climate data matching to apply per-cell climate values based on the configured strategy (spatial interpolation or index-based matching).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_path
|
str | Path
|
Path to YAML configuration file. |
required |
Returns:
| Type | Description |
|---|---|
pd.DataFrame:
|
Results table with columns: cell_id, lat, lon, v_index, mean_temp, total_rain, score, label. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError:
|
If config file, raster file, or climate CSV does not exist. |
ValueError:
|
If configuration is invalid or no valid raster cells found in ROI. |
Source code in terraflow/pipeline.py
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 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 | |
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
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