Skip to content

feat(config): add hapi.config and Catchment.from_yaml for YAML-driven runs #215

Description

@MAfarrag

Context

PR #214 replaced the hardcoded-Python-script workflow for configuring a Hapi run with a YAML
file plus a builder. This issue documents that change.

What Changed

  • New module src/hapi/config.py: a pure pydantic schema (RunConfig and its nested blocks)
    that validates a parsed YAML mapping for both lumped and distributed runs. Imports nothing
    from hapi, so it stays a leaf of the import graph.
  • New Catchment.from_yaml classmethod (src/hapi/catchment.py): reads the file, validates it,
    resolves every relative path against the file's own directory, and makes the read_* calls
    in the order the build-then-mutate pattern requires. Calibration.from_yaml returns its own
    type; Run.from_yaml is overridden to refuse, since Run holds entry points rather than
    being a catchment.
  • New MeteoInputs.from_config (src/hapi/inputs.py): dispatches over meteo.source to the
    three existing loaders (from_rasters, from_netcdf, from_netcdf_files).
  • Validation rules: required blocks per spatial_resolution and meteo.source, refusal of
    fields the chosen run shape will never read, routing_method derived from
    parameters.maxbas when unstated (and required to agree with it otherwise), the resolved
    meteorological window checked rather than two independent pairs, and every missing input
    path reported together before any reader runs.
  • All four Coello example scripts (examples/hydrological-model/coello/run/) ported to
    YAML-driven configurations, each script paired with a .yaml file beside it.
  • New docs page docs/examples/run-configuration.md, linked from the two existing run pages,
    plus the generated API reference docs/api/config.md.

Affected locations

File Symbol
src/hapi/config.py RunConfig, CatchmentConfig, MeteoConfig, FlowNetworkConfig, ParametersConfig, ConceptualModelConfig, GaugesConfig, OutputsConfig
src/hapi/catchment.py Catchment.from_yaml
src/hapi/inputs.py MeteoInputs.from_config
src/hapi/run.py Run.from_yaml (override, refuses)

Usage Example

from hapi.catchment import Catchment
from hapi.run import Run

model = Catchment.from_yaml("coello-distributed-model-run-netcdf.yaml")
Run.RunHapi(model)

Verification

  • hapi.config reaches 100% line and branch coverage
  • Each YAML-built model verified field-for-field identical to the hardcoded assembly it
    replaces
  • Two full /review-rounds passes (22 + 25 findings, all resolved) and a SonarCloud sweep
  • docs/examples/run-configuration.md walks through a configuration end to end

Implemented in #214.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions