Skip to content

Three bug fixes#292

Merged
ParticularlyPythonicBS merged 6 commits intoTemoaProject:unstablefrom
idelder:tbf
Apr 8, 2026
Merged

Three bug fixes#292
ParticularlyPythonicBS merged 6 commits intoTemoaProject:unstablefrom
idelder:tbf

Conversation

@idelder
Copy link
Copy Markdown
Collaborator

@idelder idelder commented Apr 8, 2026

  1. Add an rtv_new validation set to loading which holds valid rtv indices for newly built capacity. This is used to filter tables relating to newly built capacity: cost_invest, loan_rate, limit_new_capacity, loan_rate, loan_lifetime_process, emission_embodied, construction_input, limit_new_capacity, limit_new_capacity_share. All but these last two has custom loaders in place to do vintage filtering (for myopic mode). These custom loaders are no longer needed and so are removed.
  2. Fix the component manifest for limit_seasonal_capacity_factor to have a tech_or_group column, not a tech column, as this is now supported.
  3. Fix retirement accounting in annual_retirement_constraint. This does not affect actual retirement behaviour but affects accounting of retirment for outputs and for other features using retirements like end of life emissions and end of life material outputs. v_capacity is the adjusted available capacity of a particular process in a particular period. The code correctly accounted for survival curve and mid-period end of life adjustments via process_life_fraction but incorrectly accounted for early retirement (v_retired_capacity) which is included in v_capacity.

ALSO: finally clean up the (str, Enum) type inheritance that ruff was upset about

Summary by CodeRabbit

  • Bug Fixes

    • Corrected retirement/end-of-life capacity calculations, mid-life branching, and annualised retirement scaling.
    • Fixed annual-demand useful-activity indexing and early-retirement summation boundaries.
  • Refactor

    • Simplified retirement/capacity logic paths and consolidated survival-curve handling.
  • Chores

    • Updated data loading/validation paths and manifest declarations for improved viability filtering.
  • Style

    • Standardized several enum types for consistent string behavior.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 8, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 78938388-2b6a-4601-b9ef-947c95565838

📥 Commits

Reviewing files that changed from the base of the PR and between 5d43bf6 and babc151.

📒 Files selected for processing (7)
  • temoa/components/capacity.py
  • temoa/components/utils.py
  • temoa/data_io/component_manifest.py
  • temoa/data_io/hybrid_loader.py
  • temoa/model_checking/commodity_network_manager.py
  • temoa/types/solver_types.py
  • temoa/types/validation_types.py

Walkthrough

Centralized end-of-life calculations and adjusted retirement-capacity bookkeeping in capacity logic; corrected demand indexing; migrated several manifest validators and removed custom hybrid loader methods; added new RTV-vintage viability sets and updated viability filtering; small enum base-type changes to StrEnum.

Changes

Cohort / File(s) Summary
Capacity logic
temoa/components/capacity.py
Consolidated EOL computation (p_end, eol_year) in annual_retirement_constraint; changed EOL checks (p == eol_year, p <= eol_year < p_end), simplified mid-life stepping, added “undo” adjustments for decision-retirement with v_retired_capacity, and annualised retirement now divides by value(model.period_length[p]). Corrected demand-specific distribution indexing in capacity_constraint (p vs s) and tightened early-retirement boundary in adjusted_capacity_constraint.
Data manifest updates
temoa/data_io/component_manifest.py
Switched several LoadItem validators from viable_rtvviable_rtv_new, removed custom_loader_name for certain cost/emission inputs, kept explicit is_period_filtered, and adjusted limit_seasonal_capacity_factor columns to include tech_or_group.
Hybrid loader
temoa/data_io/hybrid_loader.py
Added viable_rtv_new and viable_rtv_eol source-trace viability sets; removed custom loader methods that previously applied base-year/myopic period filtering (_load_cost_invest, _load_loan_rate, _load_construction_input, _load_emission_embodied).
Viability filters
temoa/model_checking/commodity_network_manager.py
build_filters() now populates rtv_new by matching current period to vintage for tech and tech_group entries; returns rtv_new as a ViableSet and uses type-consistent vintage comparisons via cast('int', ...).
Enums / type-safety
temoa/components/utils.py, temoa/types/solver_types.py, temoa/types/validation_types.py
Changed enum base classes to StrEnum (Operator, SolverStatusEnum, ValidationSeverity) preserving members and string values.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • PR #180: Modifies the same capacity functions (annual_retirement_constraint, capacity_constraint, adjusted_capacity_constraint) and EOL/retirement logic.
  • PR #287: Contains the same change to demand_specific_distribution indexing (period vs season) in capacity constraints.
  • PR #161: Related enum change moving Operator implementation toward a StrEnum-based type.

Suggested labels

bugfix, Maintenance, type-safety

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Title check ❓ Inconclusive The title 'Three bug fixes' is vague and generic; it does not convey meaningful information about which bugs are fixed or what the primary changes accomplish. Consider a more specific title that describes the main bug fixes, such as 'Fix retirement accounting and add rtv_new validation set' or 'Fix capacity retirement accounting and component manifest issues'.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed Docstring coverage is 86.67% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
temoa/db_schema/temoa_schema_v4.sql (1)

211-221: ⚠️ Potential issue | 🟠 Major

Version this persisted schema change before release.

Adding period to demand_specific_distribution changes the on-disk table contract, but this schema still reports DB_MAJOR=4 / DB_MINOR=0. Older 4.0 databases without this column will now look compatible until the loader in temoa/data_io/component_manifest.py starts selecting period from this table, then fail at runtime. Please bump the schema version and mirror that bump in the shipped SQL assets, or add an explicit migration path.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@temoa/db_schema/temoa_schema_v4.sql` around lines 211 - 221, This change adds
the period column to the demand_specific_distribution table but leaves
DB_MAJOR=4/DB_MINOR=0, so bump the persisted schema version (increment DB_MINOR,
e.g., to 1) and update the shipped SQL assets to reflect the new version; ensure
the loader in component_manifest.py that now selects period matches the new
schema version (or add an explicit migration path) so older 4.0 databases
without the new period column are not treated as compatible.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@temoa/_internal/table_data_puller.py`:
- Around line 191-194: The branch that chooses between
model.demand_specific_distribution[r, p, s, d, o] and model.segment_fraction[s,
d] should be extracted into a single helper (e.g.,
get_annual_distribution(model, r, p, s, d, o)) that returns the correct
distribution value; implement this helper to check membership (o in
model.commodity_demand) and call value(...) on the appropriate expression,
replace the inline branch in table_data_puller.py (and other modules) with calls
to get_annual_distribution, and update imports/usage so future schema/index
changes are made in one place.

In `@temoa/components/capacity.py`:
- Around line 335-341: The last pre-EOL retirement period is excluded because
both guards use '<' against eol_year; update the comparisons in the
annual_retirement_constraint logic so the boundary case where p +
value(model.period_length[p_next]) == eol_year is included (i.e., use '<='
instead of '<' for the check that sets cap_end when computing p_next_end and for
the cap_begin check), ensuring v_retired_capacity[r, p_next, t, v] and
v_retired_capacity[r, p, t, v] are added back for the final pre-EOL retirement
period referenced by retired_capacity_variable_indices().

---

Outside diff comments:
In `@temoa/db_schema/temoa_schema_v4.sql`:
- Around line 211-221: This change adds the period column to the
demand_specific_distribution table but leaves DB_MAJOR=4/DB_MINOR=0, so bump the
persisted schema version (increment DB_MINOR, e.g., to 1) and update the shipped
SQL assets to reflect the new version; ensure the loader in
component_manifest.py that now selects period matches the new schema version (or
add an explicit migration path) so older 4.0 databases without the new period
column are not treated as compatible.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 6a0c3416-68fc-4531-9fb9-76b995e7521b

📥 Commits

Reviewing files that changed from the base of the PR and between 3e29e07 and 9c1296c.

📒 Files selected for processing (16)
  • temoa/_internal/table_data_puller.py
  • temoa/components/capacity.py
  • temoa/components/commodities.py
  • temoa/components/emissions.py
  • temoa/components/reserves.py
  • temoa/core/model.py
  • temoa/data_io/component_manifest.py
  • temoa/data_io/hybrid_loader.py
  • temoa/db_schema/temoa_schema_v4.sql
  • temoa/model_checking/commodity_network_manager.py
  • temoa/tutorial_assets/utopia.sql
  • tests/testing_data/mediumville.sql
  • tests/testing_data/seasonal_storage.sql
  • tests/testing_data/storageville.sql
  • tests/testing_data/test_system.sql
  • tests/testing_data/utopia_data.sql

Comment on lines 191 to 194
if o in model.commodity_demand:
distribution = value(model.demand_specific_distribution[r, s, d, o])
distribution = value(model.demand_specific_distribution[r, p, s, d, o])
else:
distribution = value(model.segment_fraction[s, d])
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick | 🔵 Trivial

Extract the annual distribution lookup into a shared helper.

This period-aware demand_specific_distribution/segment_fraction branch is now duplicated across multiple modules. Centralizing it would make the next schema/index change one-touch instead of another multi-file sweep.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@temoa/_internal/table_data_puller.py` around lines 191 - 194, The branch that
chooses between model.demand_specific_distribution[r, p, s, d, o] and
model.segment_fraction[s, d] should be extracted into a single helper (e.g.,
get_annual_distribution(model, r, p, s, d, o)) that returns the correct
distribution value; implement this helper to check membership (o in
model.commodity_demand) and call value(...) on the appropriate expression,
replace the inline branch in table_data_puller.py (and other modules) with calls
to get_annual_distribution, and update imports/usage so future schema/index
changes are made in one place.

Comment thread temoa/components/capacity.py Outdated
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@temoa/components/capacity.py`:
- Around line 316-318: Change the "else: if" pattern to an "elif" to reduce
indentation and improve readability: replace the block starting with "else:"
followed by "if p == model.time_optimize.last() or p_end == eol_year" with a
single "elif p == model.time_optimize.last() or p_end == eol_year" and adjust
the following else branch accordingly so control flow remains identical (keep
checks using p, model.time_optimize.last(), p_end, and eol_year and preserve the
logic for mid-life period and ending capacity assignment).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 44e65954-2bcd-4c9b-9cb6-6e4db9c05056

📥 Commits

Reviewing files that changed from the base of the PR and between 51e2e7c and 5d43bf6.

📒 Files selected for processing (4)
  • temoa/components/capacity.py
  • temoa/data_io/component_manifest.py
  • temoa/data_io/hybrid_loader.py
  • temoa/model_checking/commodity_network_manager.py

Comment thread temoa/components/capacity.py Outdated
@ParticularlyPythonicBS ParticularlyPythonicBS merged commit 6f3f250 into TemoaProject:unstable Apr 8, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants