Derive UK person and benunit weights when loading a dataset#479
Draft
vahid-ahmadi wants to merge 1 commit into
Draft
Derive UK person and benunit weights when loading a dataset#479vahid-ahmadi wants to merge 1 commit into
vahid-ahmadi wants to merge 1 commit into
Conversation
PolicyEngineUKDataset.load() assumed `person_weight` and `benunit_weight` were already columns, but published UK microdata carries only `household_weight` at the household level. Supplying a real dataset file directly raised `KeyError: 'person_weight'`. Factor the derivation that create_datasets performed inline into shared `derive_person_weight` / `derive_benunit_weight` helpers and call them from load() as well, so the two paths cannot drift. The helpers are no-ops when the weight columns are already present, and report missing join columns with a clear message. Fixes #478 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #478
The failure
Constructing a
PolicyEngineUKDatasetagainst any real published UK dataset file raisesKeyError: 'person_weight'at construction time:Why
PolicyEngineUKDataset.load()assumedperson_weightandbenunit_weightwere already columns in the file. Published UK microdata carries onlyhousehold_weight, at the household level — person and benunit weights are derived from it. Verified absent inenhanced_frs_2024_25.h5,enhanced_frs_2023_24.h5, and the certifiedpopulace_uk_2023.h5.create_datasetsalready did that derivation inline (merginghousehold_weightonto the person frame viaperson_household_id, and onto benunits via the person frame) and saved the rewritten file. So a manifest-resolved dataset worked while a directly-supplied filepath did not.The change
create_datasetsinto sharedderive_person_weight/derive_benunit_weighthelpers, and call them fromload()too, so the two paths cannot drift.KeyError.The manifest path is unchanged.
create_datasetsperforms exactly the same merges, in the same order, producing the same columns and values — verified below against the pre-change code.User-facing benefit
Callers can run policyengine.py on their own UK dataset — for example a newer Enhanced FRS release — without waiting for it to appear in the certified bundle manifest.
Verification
Reproduced the
KeyErroronmain, then on this branch, againstenhanced_frs_2024_25.h5:(
equalcompares against the exact merge codecreate_datasetsused before this change — the value-equivalence check, not just "it no longer raises".)End-to-end
sim.ensure()from the report now succeeds on bothenhanced_frs_2024_25.h5andenhanced_frs_2023_24.h5.Tests
New
tests/test_uk_dataset_weights.py(9 tests) covers derivation when columns are missing, no-op when present (both helper-level and throughload()), clear errors for missing join columns, and equivalence with the originalcreate_datasetsmerge.Full suite: 747 passed.