Implement unit conversion#492
Draft
jvendries wants to merge 2 commits into
Draft
Conversation
This was referenced Jul 1, 2026
Contributor
Author
|
Warning This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
This stack of pull requests is managed by Graphite. Learn more about stacking. |
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.

cc:
Closes: cornerstone-data/methods#86
What changed? Why?
Introduces mixed-unit (MWh) electricity emission factors for sector
221110(generation), gated behind a newimplement_electricity_mixed_unitsconfig flag. When enabled, the generation sector's output column inA,q, andBis converted from USD to MWh using eGRID net generation as the physical anchor and EIA Table 2.4 class-differentiated retail prices to compute per-column row scaling factors (c_row). This allows emission factors for the generation sector to be expressed in CO₂e/MWh rather than CO₂e/USD.Key additions:
electricity_disaggregation.py: Core conversion functions —electricity_output_factor,electricity_class_row_factors,apply_electricity_unit_conversion_to_A/q/B— and theGENERATION_SECTORconstant.electricity_end_use_mapping.py: Production home for the Cornerstone commodity → EPA end-use mapping and EIA Table 2.4 price loader, promoted from the analysis module.classify_industry_end_usenow returns a(end_use, rule)tuple and iterates overCORNERSTONE_COMMODITIES_ELECinstead ofCORNERSTONE_INDUSTRIES_ELEC.cornerstone_disagg_pipeline.py:electricity_mixed_units_enabled,electricity_conversion_factors,build_electricity_mixed_units_aq/b, andcompute_mixed_unit_ef_vectors(non-cached, used for Track B class-vs-uniform comparison).derived_cornerstone.py: Cached derivesderive_cornerstone_Aq_mixed_units,derive_cornerstone_B_mixed_units, andderive_cornerstone_y_nab_mixed_units.model_objects.pyandcache_reset.py:get_A,get_Adom,get_B,get_D,get_qroute through mixed-unit derives when the gate is on.diagnostics_helpers.py:apply_mixed_units_ef_diff_exemptionsandapply_mixed_units_bly_diff_exemptionsNaN out the221110percent-diff columns when units are incommensurate with the monetary baseline. Newbuild_mixed_vs_monetary_comparison_dfandsectors_for_mixed_vs_monetary_tabhelpers populate amixed_vs_monetary_221110diagnostics tab.calculate_ef_diagnostics.pyandcalculate_national_accounting_balance_diagnostics.py: Route to mixed derives under the gate; write the new comparison tab; add a note column on BLy vs E_orig when drift is expected due to unit change.analysis/electricity/class_prices/: Track B analysis comparing class-priced vs uniform-price mixed EF vectors, with a CLI that writes CSV and markdown output.analysis/electricity/d_86/: Three-path toy analysis (Methods INFRA-001: Create diagnostics module eeio_diagnostics.py #86) demonstrating that applying mixed-unit conversion directly to scaledA/q(PR4 path) produces numerically identical results to rebuildingV/U/Yflow tables and rederivingA. Includestoy_tables,toy_scaling,toy_dom_imp,toy_paths,mixed_flow_conversion,write_report, and a pre-generated report.test_usa_config_waste_disagg_electricity_mixed_units.yamland2025_usa_cornerstone_v0_2_electricity_mixed_units.yaml.USAConfigvalidates thatimplement_electricity_mixed_unitsrequiresimplement_electricity_disaggregation.The
d_85analysis modules are refactored to re-export from the new production homes rather than defining their own copies oftable_2_4_prices_cents_kwh,classify_industry_end_use, andbuild_end_use_map.Testing
test_electricity_mixed_units.pycover output factor validation, class row factor monotonicity, uniform-price similarity transform, end-use map coverage, gate-off no-op behavior, MWh anchor,y_nabconsistency, D scalar bridge, and the non-cachedcompute_mixed_unit_ef_vectorspath.test_toy_paths.pyverifies that Section 2 (flow-level mixed) and Section 3 (direct matrix mixed) produce identicalAtotandq, that output identities pass, and that BLy is dimensionally consistent.test_mixed_vs_monetary_diagnostics.pycoverssectors_for_mixed_vs_monetary_tabandbuild_mixed_vs_monetary_comparison_dfcolumn structure and values.test_usa_config.pyadds parsing and validation tests forimplement_electricity_mixed_units.test_calculate_national_accounting_balance.pyadds tests confirming mixed derives are called under the gate and that thenotecolumn andexemption_reasoncolumn appear in the correct output tabs.test_end_use_mapping.pyupdated to reflect the new(end_use, rule)return type andCORNERSTONE_COMMODITIES_ELECcoverage.