Skip to content

Unify utility functionality for time structure iterators - #112

Merged
JulStraus merged 5 commits into
mainfrom
enhance/common_utils
Nov 4, 2026
Merged

JulStraus merged 5 commits into
mainfrom
enhance/common_utils

Conversation

@JulStraus

Copy link
Copy Markdown
Collaborator

This PR came up as I was a bit uncertain regarding which iteration utilities we included for the different time structures. Most of the work was done by me in #23 but we never really unified it. The work was done by Claude but with extensive review from my side.

Summary

  • The iterators returned by strat_periods, repr_periods, opscenarios, and strategic_scenarios are indexable: getindex with a BoundsError check, plus generic firstindex, eachindex, and keys.
    lastindex (and with it x[end] and last) exists only for iterators of strategic and representative periods.
    It is not defined for scenario iterators (scenarios are not ordered) or for the nodes of a TwoLevelTree (a tree has several last nodes).
  • All periods, including the Single* periods and StratNode, delegate getindex and eachindex to the structure they contain.
    SimpleTimes and CalendarTimes check the index against len.
  • Ordering:
    • StratReprPeriod and StratNodeReprPeriod compare by strategic period, then by representative period.
    • For StratNodes, n1 < n2 holds only if n1 is an ancestor of n2. isless is unchanged, so sort stays reliable and puts ancestors first.
  • withprev, withnext, chunk, and chunk_duration forward IteratorSize, so they also work over iterators without a length (e.g. Iterators.filter).
    isfirst is defined for partitions.
    The guards against tree nodes in chunk and chunk_duration also catch calls with keywords.
  • Docs: new sections on indexing and ordering in the iteration manual; updated docstrings of TimeStructInnerIter, TimeStructOuterIter, and isfirst.

Changes in behaviour

  • <, <=, and >= between StratNodes now consider only ancestors. This fixes start_time and end_time with a TwoLevelTree.
    It also changes the results of downstream code that compares nodes across branches. For example, the CO2Storage capacity constraint in EnergyModelsCO2 with a TwoLevelTree now sums over the ancestors only, which is correct.
    remaining(n, ::TwoLevelTree) for nodes other than the root now sums the node and its descendants. It is still not correct and is left as is.
  • sort(repr_periods(::TwoLevel)) now follows the order of iteration; before, it interleaved strategic periods.
  • getindex of SimpleTimes and CalendarTimes throws a BoundsError beyond len; before, it returned a period.
  • eachindex of all iterators is Base.OneTo(length(x)); before, some methods used the length of internal vectors, which is wrong when len is smaller.

JulStraus and others added 5 commits October 30, 2026 07:30
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…cestry

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…n trees

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@JulStraus
JulStraus requested review from hellemo and trulsf October 30, 2026 07:05
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
src/calendar.jl 94.54% <100.00%> (+3.80%) ⬆️
src/op_scenarios/opscenarios.jl 89.28% <100.00%> (+12.81%) ⬆️
src/op_scenarios/rep_periods.jl 90.19% <ø> (+18.49%) ⬆️
src/op_scenarios/strat_periods.jl 95.65% <ø> (+14.13%) ⬆️
src/representative/rep_periods.jl 90.00% <100.00%> (+2.16%) ⬆️
src/representative/strat_periods.jl 96.07% <100.00%> (+18.71%) ⬆️
src/representative/tree_periods.jl 85.71% <100.00%> (+1.33%) ⬆️
src/simple.jl 100.00% <100.00%> (ø)
src/strat_scenarios/core_types.jl 98.87% <100.00%> (+0.01%) ⬆️
src/strat_scenarios/strat_scenarios.jl 100.00% <100.00%> (+2.63%) ⬆️
... and 4 more
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@trulsf trulsf 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.

Looks fine. I think the docs update on recommended usage of indexing is a good addition. Even if users are not reading the docs, it may help in the behavior of coding agents.

@JulStraus
JulStraus merged commit 60cdc81 into main Nov 4, 2026
6 checks passed
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