grat.validation
¶
Checks input data for problems before it reaches build_sites or run_grat.
Meant to be run from a notebook right after loading your own data, so problems show up as a readable report instead of a stack trace (or worse, a silently wrong run) partway through the model::
from grat.loaders.get_sites import load_sites
from grat.loaders.get_wafr import load_wafr
from grat.loaders.get_ccc import get_crop_calendar
from grat.validation import validate_grat_inputs
sites = load_sites("my_sites.csv")
wafr = load_wafr("my_wafr.csv")
crop_calendar = get_crop_calendar("my_ccc.csv")
report = validate_grat_inputs(sites, wafr, crop_calendar=crop_calendar)
report # prints a summary in a notebook cell
if not report.ok:
raise SystemExit("fix the data before running the model")
Every check function here takes the same objects build_sites and run_grat take
(the loader output), not raw file paths, so it works regardless of where your data
came from or how messy the source file was - csv, shapefile, geodatabase, or something
assembled by hand.
ValidationReport
dataclass
¶
Collects issues found across one or more checks.
Attributes:
| Name | Type | Description |
|---|---|---|
issues |
list
|
every issue found, in the order the checks ran. |
check_sites
¶
check_sites(
report,
sites,
*,
known_wafr_sources=None,
known_bottleneck_ids=None,
known_precip_grids=None,
valid_crops=None,
valid_il_crops=None,
valid_soil_types=None,
crop_calendar=None,
crop_calendar_in_lieu=None,
)
Checks the site dictionaries returned by load_sites, before build_sites runs.
Flags conditions that build_sites and drop_invalid_sites would otherwise handle
by silently dropping the site, so a bad input file doesn't just quietly shrink the
site count.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
report
|
|
required | |
sites
|
list of site attribute dictionaries, as returned by |
required | |
known_wafr_sources
|
WAFR source names present in the WAFR input, to catch sites that reference a source the WAFR file doesn't have. |
None
|
|
known_bottleneck_ids
|
bottleneck ids present in the bottleneck input. |
None
|
|
known_precip_grids
|
precipitation grid ids present in the precip input. |
None
|
|
valid_crops
|
crop types present in the on-farm crop compatibility calendar. |
None
|
|
valid_il_crops
|
crop types present in the in-lieu crop compatibility calendar. |
None
|
|
valid_soil_types
|
soil types present in the crop compatibility calendar. |
None
|
|
crop_calendar
|
the on-farm crop compatibility calendar itself ( |
None
|
|
crop_calendar_in_lieu
|
the in-lieu crop compatibility calendar itself
( |
None
|
Source code in src/grat/validation.py
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 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 | |
check_wafr
¶
Checks the water-available-for-recharge dictionary returned by load_wafr.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
report
|
|
required | |
wafr
|
dictionary of |
required |
Source code in src/grat/validation.py
check_precip
¶
Checks the precipitation dictionary returned by get_precip.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
report
|
|
required | |
precip
|
dictionary of |
required |
Source code in src/grat/validation.py
check_bottlenecks
¶
Checks raw bottleneck rows, before they're turned into Bottleneck objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
report
|
|
required | |
bottleneck_rows
|
iterable of mappings with |
required |
Source code in src/grat/validation.py
check_crop_calendar
¶
Checks a crop calendar dictionary from get_crop_calendar or get_il_crop_calendar.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
report
|
|
required | |
crop_calendar
|
dictionary of |
required | |
source
|
name to tag issues with. |
'crop calendar'
|
Source code in src/grat/validation.py
validate_grat_inputs
¶
validate_grat_inputs(
sites,
wafr,
*,
bottleneck_rows=None,
crop_calendar=None,
crop_calendar_in_lieu=None,
precip=None,
)
Runs every check that applies to the inputs provided, and returns the combined report.
Call this with whatever you've already loaded - only sites and wafr are
required, matching what build_sites and run_grat need at a minimum. Nothing
passed in is modified.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sites
|
list of site attribute dictionaries, as returned by |
required | |
wafr
|
dictionary of |
required | |
bottleneck_rows
|
raw bottleneck rows (before |
None
|
|
crop_calendar
|
on-farm crop compatibility calendar, as returned by |
None
|
|
crop_calendar_in_lieu
|
in-lieu crop compatibility calendar, as returned by |
None
|
|
precip
|
dictionary of |
None
|
Returns:
| Type | Description |
|---|---|
|
|
|
|
is True when no errors were found, and |
|
|
the issues found at each level. |