Skip to content

Refactor disagg calls into cornerstone_disagg_pipeline.py#453

Merged
jvendries merged 8 commits into
mainfrom
move_disagg_code_from_cornerstone_derived_functions
Jun 8, 2026
Merged

Refactor disagg calls into cornerstone_disagg_pipeline.py#453
jvendries merged 8 commits into
mainfrom
move_disagg_code_from_cornerstone_derived_functions

Conversation

@jvendries

@jvendries jvendries commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

cc:
Closes:

What changed? Why?

Extracts the cornerstone sector-disaggregation orchestration logic from derived_cornerstone.py into a new dedicated module, cornerstone_disagg_pipeline.py.

The new module owns:

  • cornerstone_sector_disagg_active — single gate replacing the previous electricity_reallocation_enabled checks scattered across public routers
  • get_waste_disagg_weights — moved from derived_cornerstone
  • electricity_reallocation_enabled — moved from derived_cornerstone
  • derive_disagg_io_bundle — replaces _derive_cornerstone_io_after_electricity_reallocation
  • derive_disagg_Ytot_with_trade — replaces _derive_cornerstone_Ytot_with_trade
  • distribute_waste_parent_x_using_v_row_shares — extracted from _distribute_waste_parent_x_using_v_row_shares

derived_cornerstone.py public routers (derive_cornerstone_V, derive_cornerstone_U_with_negatives, derive_cornerstone_VA, derive_cornerstone_Ytot_full_cs_matrix, derive_cornerstone_Aq) now gate on cornerstone_sector_disagg_active() instead of electricity_reallocation_enabled(). Baseline (no-disagg) IO helpers are renamed _derive_cornerstone_V_baseline, _derive_cornerstone_U_baseline, and _derive_cornerstone_VA_baseline to make the routing intent explicit. The _normalize_E_for_waste helper is removed as it was no longer used. Import aliases were removed from derived_cornerstone.py.

inflate_cornerstone_V_with_industry_pi is extracted into inflation_helpers_cornerstone.py to replace inline inflation logic in derive_cornerstone_V.

Monkeypatches in test_waste_disagg_pipeline_integration.py are updated to target disagg_weights_module.load_disagg_weights directly rather than patching through derived_cornerstone, reflecting the moved import site.

Testing

Existing test suite passes. Cache-clearing lists in test_electricity_reallocation.py and test_waste_disagg_pipeline_integration.py are updated to reference the relocated cached functions (derive_disagg_io_bundle, derive_disagg_Ytot_with_trade, cornerstone_sector_disagg_active) in cornerstone_disagg_pipeline.

jvendries commented Jun 4, 2026

Copy link
Copy Markdown
Contributor Author

@jvendries jvendries force-pushed the move_disagg_code_from_cornerstone_derived_functions branch 2 times, most recently from e412ad3 to 2ad75e6 Compare June 4, 2026 15:58
@jvendries jvendries marked this pull request as ready for review June 4, 2026 16:02
@jvendries

Copy link
Copy Markdown
Contributor Author

@WesIngwersen there is some duplication in this PR still, for example, between _derive_cornerstone_V_baseline in derived_cornerstone.py and derive_cornerstone_V_after_waste in cornerstone_disagg_pipeline.py. The reason for this is to avoid circular imports. However, let me know if you would prefer to avoid duplication and route everything through a single function, in which case we will have to add the circular imports.

for code in present:
x.loc[code] = parent_go * float(shares.loc[code])
return x
return distribute_waste_parent_x_using_v_row_shares(x_cs)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noting this still calling a specific waste disagg function

Comment thread bedrock/transform/eeio/cornerstone_disagg_pipeline.py Outdated
return Udom_cs, Uimp_cs


def derive_cornerstone_VA_after_waste() -> pd.DataFrame:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be made generic

Ytot_orig = load_2017_Ytot_usa()
Ytot = commodity_corresp() @ Ytot_orig
Ytot.index.name = 'sector'
weights = get_waste_disagg_weights()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be made more generic

return Ytot


def distribute_waste_parent_x_using_v_row_shares(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be made more generic

Comment thread bedrock/transform/eeio/derived_cornerstone.py Outdated

@WesIngwersen WesIngwersen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useful and needed refactoring to move disaggregation functionality largely into a separate module. Can use further work to make it more generic but this is good progress and I tested code to generate various models and it does not break it.

@bl-young bl-young left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see my inline comments

Comment thread bedrock/transform/eeio/derived_cornerstone.py Outdated
Comment thread docs/apply_inflation_to_V_pipeline.mmd Outdated
Comment thread bedrock/transform/eeio/__tests__/test_cornerstone_disagg_refactor_parity.py Outdated
@bl-young

bl-young commented Jun 8, 2026

Copy link
Copy Markdown
Member

btw I'm running the integration tests on this branch to make sure they pass

@jvendries jvendries merged commit d384f8b into main Jun 8, 2026
5 checks passed
@jvendries jvendries deleted the move_disagg_code_from_cornerstone_derived_functions branch June 8, 2026 20:08
bl-young added a commit that referenced this pull request Jun 10, 2026
bl-young added a commit that referenced this pull request Jun 10, 2026
* refactor(publish): extract shared model getters from excel writer

Move cached getters, location suffix helpers, and cache clearing into
model_objects and cache_reset so XLSX and other publishers share one path.

* feat(publish): emit cornerstone CO2e supply-chain factors CSV

Add purchaser-price CO2e tables from N with year rebase via cornerstone
industry PI, placeholder Phi/margins, and emission_factors CLI.

* move files inside `emission_factors`

* fix runtime typing error

* fix application of price ratio

* use commodity PI instead of industry PI

* resolve remaining conflicts with #453
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants