Repository navigation
Get more from types: use the complete spec for ParameterData #1008
Replies: 3 comments 2 replies
|
Several good ideas and observations here!
That's a big PR. Can you point to the specific file(s)/code that you're referencing? Without (yet) thinking through what would be the best approach, some more info:
|
The On where this lives, the rationale for But the sort of broader idea I had was to have And that also informs the distinction from |
|
Upon deeper reflection, I think the composite ideal risks becoming a DSL so no |
Uh oh!
There was an error while loading. Please reload this page.
The core idea is with better leveraging of types, we can eliminate a broad category of tests and make invalid scenario parametrization invalid by construction.
Every domain module in message-ix-models produces the same artifact: a
dict[str, pd.DataFrame]mapping parameter names to DataFrames, eventually passed toadd_par_data(). More or less every module's test suite (for those with test suites) independently re-checks the same structural properties of that artifact: no NaN values, correct columns, no duplicate index rows, year_vtg ≤ year_act, node and year membership. The checks are written slightly differently each time but they are still repeated and not consistently enforced at run-time.We already have
make_df(name)which constructs a DataFrame with correct columns fromMESSAGE.items, but the result is an ordinary DataFrame and nothing prevents mutation afterward.MESSAGE.enforce(scenario)checks structural consistency at solve time, but only for three storage items and one capacity-factor mask, and only after data has been injected.So the pattern exists (use MESSAGE.items to know what's correct, then check) but nothing validates the dict[str, DataFrame] bundle that domain modules actually produce and add_par_data actually consumes.
ParameterDataalready exists but only as a thin type alias inmessage_ix_models/types.py(Mapping[str, pd.DataFrame]), without connection to the spec (MESSAGE.items, DIMS). The idea is to make ParameterData a frozen dataclass with construction time enforcement of validity, and thereafter trusted by downstream code. If it exists, its valid.message_ix_models.testing.checkhas a composable Check framework (NoneMissing,NoDuplicates,HasCoords, etc.) that validates intermediate genno quantities inline, but it requires a gennoComputerand transport is the only consumer; materials, water, and buildings don't use it for data generation.Adoption can be opt-in: A module that adopts it can delete its structural test assertions and keep only domain-specific checks (expected row counts, unit conventions, specific coordinate values). A module like buildings, which currently has zero structural validation, gets the full set for free by wrapping its return value.
Related issues
This would address or partially address several open issues across both repositories:
Invalid year_vtg/year_act combinations: iiasa/message-ix-models#237 (costs outputs all combos including invalid), #583 (incorrect
map_tec_lifetime), iiasa/message-ix-models#298 (manualfix_costyear filtering). The materials refactor in iiasa/message-ix-models#475 achieved 35–75% build time reduction partly by filtering these rows manually.Invalid set membership: #486 (
read_excelallows years not in scenario, producing corruptduration_period), #828 (type_year="cumulative"only populated at solve time with no earlier feedback).Missing validation in the injection path: iiasa/message-ix-models#45 (dimensionless unit bugs surface only at the database layer), iiasa/message-ix-models#78 (ad-hoc duplicate/unit handling added to
add_par_data).Per-module boilerplate: iiasa/message-ix-models#460 (bilateralization reinventing the workflow), iiasa/message-ix-models#194 (materials consolidating duplicate
combine_df_dictionaries), iiasa/message-ix-models#319 (water DSL attempt to reduce repeatedmake_dflogic), iiasa/message-ix-models#405 (water test fixture to reduce test boilerplate).All reactions