Skip to content

fix: align reconstructed coordinate order with input (+ comparison recipe) - #97

Merged
FBumann merged 5 commits into
mainfrom
fix/reconstructed-coord-align
Jul 21, 2026
Merged

fix: align reconstructed coordinate order with input (+ comparison recipe)#97
FBumann merged 5 commits into
mainfrom
fix/reconstructed-coord-align

Conversation

@FBumann

@FBumann FBumann commented Jul 20, 2026

Copy link
Copy Markdown
Owner

Follow-up to #93 (now merged): fixes an incomplete part of the dim-order work and adds the comparison-plot recipe we opted for over a first-class plotting method.

Fix — coordinate order (not just dim order)

#93 transposed reconstructed to the input's dim order, but tsam still returns it with alphabetically sorted columns, so the variable (etc.) coordinate order diverged from original:

agg.original["variable"].values        # ['wind', 'solar', 'demand']  (input order)
agg.reconstructed["variable"].values   # ['demand', 'solar', 'wind']  (tsam sorted)

Consequences: original.to_dataframe() and reconstructed.to_dataframe() had different row orders, and compare()'s concat emitted a FutureWarning (join='outer''exact') that will become an error in a future xarray.

reindex_like(da) after the transpose realigns coordinate order too. The column set is identical, so it only reorders — no NaNs. original, reconstructed, and residuals now line up exactly in values and row order. Regression test added (test_non_alphabetical_coord_order_preserved).

Docs — comparison recipe (resolves #95 without a plotting method)

Per discussion on #95/#96, we decided not to add a plot_compare() method (keeps the library lightweight; compare()/to_dataframe() already return plot-ready data). Instead, the getting-started notebook now shows a load-duration-curve comparison built directly from the tidy to_dataframe() output (Original dotted / Reconstructed solid) — the one thing the proposed method offered beyond the existing compare().plotly.line(...) example.

Validation

529 passed / 4 skipped; mypy + ruff clean; notebook executes end-to-end. Closes #95.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Preserved input coordinate/dimension order in reconstructed results to prevent mismatches.
    • Improved original/reconstructed alignment so missing values are avoided.
  • Documentation
    • Added a getting-started example demonstrating result.to_dataframe() for comparison workflows.
    • Included an example for generating and visualizing load-duration style curve comparisons.

FBumann and others added 2 commits July 20, 2026 21:57
The #92 dim-order fix only transposed dims; tsam still returns reconstructed
with alphabetically sorted columns, so its coordinate order (e.g. `variable`)
differed from `original`'s input order. That left `original.to_dataframe()`
and `reconstructed.to_dataframe()` with divergent row orders and made
compare()'s concat emit a FutureWarning (join='outer' -> 'exact').

reindex_like(da) after the transpose realigns coordinate order too; the column
set is identical, so it only reorders (no NaNs). original, reconstructed, and
residuals now line up exactly in values and row order.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Shows how to build richer original-vs-reconstructed comparisons directly from
the tidy to_dataframe() output (duration curves, Original dotted / Reconstructed
solid) — no dedicated plotting helper in the library, keeping it lightweight.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jul 20, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 687d898d-a2a2-4ea7-897b-2dc068ff4e82

📥 Commits

Reviewing files that changed from the base of the PR and between 678869d and 44a2523.

📒 Files selected for processing (4)
  • docs/examples/getting-started.ipynb
  • src/tsam_xarray/_core.py
  • src/tsam_xarray/_result.py
  • test/test_aggregate.py
💤 Files with no reviewable changes (1)
  • src/tsam_xarray/_result.py
🚧 Files skipped from review as they are similar to previous changes (2)
  • docs/examples/getting-started.ipynb
  • test/test_aggregate.py

📝 Walkthrough

Walkthrough

Aggregation output now aligns reconstructed coordinates with the input order and labels, suppresses a targeted upstream warning, and adds regression coverage. The getting-started notebook demonstrates dataframe-based original-versus-reconstructed load-duration curves.

Changes

Aggregation alignment and comparison

Layer / File(s) Summary
Realign reconstructed output
src/tsam_xarray/_core.py, test/test_aggregate.py
reconstructed is transposed and reindexed to match input dimensions and coordinate labels; regression coverage verifies non-alphabetical coordinate preservation without NaNs.
Filter aggregation warning
src/tsam_xarray/_core.py
Aggregation calls run inside a warning filter targeting the specified FutureWarning, while existing aggregation branches remain unchanged.
Document load-duration comparison
docs/examples/getting-started.ipynb, src/tsam_xarray/_result.py
The notebook ranks tidy dataframe values per variant and variable and plots original-versus-reconstructed load-duration curves; dataframe behavior is unchanged apart from comment cleanup.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

  • FBumann/tsam_xarray#93: Both changes address reconstructed coordinate alignment and dataframe-based comparison workflows.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning Issue #95 asked for a built-in compare plot on AggregationResult, but this PR only adds a notebook recipe and no plot_compare helper. Add AggregationResult.plot_compare() with timeseries/duration_curve modes, variable selection, faceting, and a clear plotly fallback error.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title matches the main change: fixing coordinate order alignment, while the comparison recipe is a secondary part.
Out of Scope Changes check ✅ Passed The warning suppression, tests, and notebook recipe all support the stated fix and comparison documentation; nothing clearly unrelated stands out.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/reconstructed-coord-align

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

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 current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@docs/examples/getting-started.ipynb`:
- Around line 172-188: Update the load-duration curve plotting block after the
`tidy` transformation to use the notebook’s `xarray_plotly` plotting accessor
instead of `plotly.express`, while preserving the existing rank, energy,
variant, variable, line-style, and title configuration.
🪄 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: defaults

Review profile: CHILL

Plan: Pro

Run ID: 50531d44-1c3e-440c-837f-82636e342aea

📥 Commits

Reviewing files that changed from the base of the PR and between b3fd174 and 678869d.

📒 Files selected for processing (3)
  • docs/examples/getting-started.ipynb
  • src/tsam_xarray/_core.py
  • test/test_aggregate.py

Comment thread docs/examples/getting-started.ipynb Outdated
FBumann and others added 3 commits July 20, 2026 22:28
tsam warns that v3 sorts result columns alphabetically while v4 will follow
the input order. tsam_xarray reindexes every output back to the input's
coordinate order, so that change is invisible to our users — the warning is
noise they can't act on. Filter it narrowly (by message + FutureWarning)
around the tsam.aggregate/apply calls. Also drops now-redundant inline
comments and consolidates the warnings import.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Plot via the notebook's `.plotly.line` accessor instead of plotly.express,
matching the rest of the notebook and dropping the extra import.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@FBumann
FBumann merged commit afd6818 into main Jul 21, 2026
10 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.

Add an original-vs-reconstructed compare plot to AggregationResult

1 participant