Pipeline¶
agribound.pipeline runs the end-to-end workflow: seed → output-reuse check →
composite (+ LULC raster prefetch) → fine-tuning → engine → SAM refinement →
study-area selection → post-processing → LULC crop filter → metadata columns →
evaluation → export → provenance record. build_composite runs only the first
stage. See the Quickstart and
Reproducibility.
pipeline ¶
Main pipeline orchestrator.
:func:delineate runs the end-to-end workflow::
seed -> reuse check -> composite (+ LULC raster prefetch) -> fine-tuning
-> engine -> SAM refinement -> study-area selection -> post-processing
-> LULC crop filter -> metadata columns -> evaluation -> export
-> provenance record
Composites cover the study area's bounding box in the export CRS (they are
not masked to the study-area polygons), so the study-area selection
(config.aoi_selection, default "representative_point") removes
predictions outside an irregular study area before post-processing.
:func:build_composite runs only the first stage (composite or embedding
download), so data preparation can happen on a node with network access and
delineation later on a GPU node.
Every stage runs inside :meth:agribound.provenance.RunRecorder.step; the
record is written to <output_path>.provenance.json and decides whether an
existing output can be reused (:func:agribound.provenance.reuse_mismatch:
configuration hash, study-area fingerprint and results versions).
delineate ¶
delineate(study_area: str | None = None, source: str = 'sentinel2', year: int = 2024, engine: str = 'delineate-anything', output_path: str | None = None, gee_project: str | None = None, local_tif_path: str | None = None, reference_boundaries: str | None = None, fine_tune: bool = False, config: AgriboundConfig | None = None, **kwargs: Any) -> gpd.GeoDataFrame
Run the complete field boundary delineation pipeline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
study_area
|
str or None
|
Vector file, GEE asset ID, |
None
|
source
|
str
|
Satellite source (see :func: |
'sentinel2'
|
year
|
int
|
Target year (default 2024). |
2024
|
engine
|
str
|
Delineation engine (see :func: |
'delineate-anything'
|
output_path
|
str or None
|
Output vector file. Default |
None
|
gee_project
|
str or None
|
GEE project ID (required for GEE-based sources). |
None
|
local_tif_path
|
str or None
|
Local GeoTIFF (required when |
None
|
reference_boundaries
|
str or None
|
Reference field boundaries for fine-tuning or evaluation. |
None
|
fine_tune
|
bool
|
Fine-tune the engine on reference_boundaries before inference. |
False
|
config
|
AgriboundConfig or None
|
Pre-built configuration. Keyword arguments, and named arguments whose
value differs from both their default and the configuration, are
applied on top of it with :meth: |
None
|
**kwargs
|
Any
|
Any other :class: |
{}
|
Returns:
| Type | Description |
|---|---|
GeoDataFrame
|
Field boundary polygons with metadata columns |
Raises:
| Type | Description |
|---|---|
FileExistsError
|
If output_path exists, |
RuntimeError
|
If the LULC filter fails and |
Notes
An existing output is returned without recomputation when its
provenance record reports a successful run with the same
:func:agribound.provenance.config_hash, study-area fingerprint
(facts["aoi_fingerprint"]) and results versions
(facts["results_versions"]); see
:func:agribound.provenance.reuse_mismatch. A record of agribound <=
1.0.0 has neither fact: it counts as results version 1, and a
study-area file (or local raster) cannot be verified (logged at WARNING;
the output is reused). With provenance=False no record is
written, so a later run on the same output_path needs
overwrite=True.
Zero detections are not an early exit: an empty output file, the evaluation (if requested) and the provenance record are still written.
After delineation (and SAM refinement) the polygons are restricted to the
study-area geometry according to aoi_selection (default
"representative_point"; see :class:~agribound.config.AgriboundConfig
and :func:select_in_study_area); the provenance fact aoi_selection
records the rule and the counts before and after.
Examples:
>>> import agribound
>>> gdf = agribound.delineate(
... study_area="area.geojson",
... source="sentinel2",
... year=2024,
... engine="delineate-anything",
... gee_project="my-project",
... )
Source code in agribound/pipeline.py
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 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 | |
build_composite ¶
Build (or load from cache) the input raster for config.
Runs the composite builder of config.source (GEE composite, USGS
export, local file or embedding download). When config.lulc_filter is
enabled and config.lulc_mode == "raster", the LULC raster for the
study area is also downloaded into the cache, and a GEE-asset study area
is saved to the working directory
(:func:agribound.io.vector.read_config_study_area), so that the
delineation stage can run without network access. Builders cache their
outputs with :func:agribound._cache.cache_path, so repeated calls reuse
the files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
AgriboundConfig
|
Pipeline configuration. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Path to the composite (or embedding) GeoTIFF. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no study area is configured for a non-local source. |
RuntimeError
|
If the LULC raster prefetch fails and |
Source code in agribound/pipeline.py
study_area_in_crs ¶
Return the study area of config as one valid polygonal geometry in crs.
The features are unioned, repaired and reduced to their polygonal parts
in the study area's own CRS (EPSG:4326 when it has none, with a WARNING),
densified (:data:_AOI_MAX_SEGMENT_DEG / :data:_AOI_MAX_SEGMENT_M) and
reprojected to crs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
AgriboundConfig
|
Configuration with a |
required |
crs
|
CRS or str
|
Target CRS. |
required |
Returns:
| Type | Description |
|---|---|
Polygon or MultiPolygon
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the study area has no polygonal area. |
Source code in agribound/pipeline.py
select_in_study_area ¶
select_in_study_area(gdf: GeoDataFrame, aoi: Any, rule: str) -> tuple[gpd.GeoDataFrame, dict[str, Any]]
Restrict polygons to a study-area geometry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gdf
|
GeoDataFrame
|
Polygons (any CRS; aoi must be in the same CRS). |
required |
aoi
|
shapely geometry
|
Polygonal study area, e.g. from :func: |
required |
rule
|
str
|
|
required |
Returns:
| Type | Description |
|---|---|
tuple
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
For an unknown rule. |
Source code in agribound/pipeline.py
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 | |