feat: accept a tsam_xarray ClusteringResult in apply_clustering() - #777
Merged
FBumann merged 1 commit intoSep 8, 2026
Merged
Conversation
Reusing a clustering computed outside flixopt meant importing `Clustering` and
constructing the wrapper by hand, guessing that `original_timesteps` should be
the target FlowSystem's own timesteps:
Clustering(clustering_result=cr, original_timesteps=fs.timesteps)
`apply_clustering()` now takes a tsam_xarray `ClusteringResult` — or its
`to_dict()` form, which is what lands in a JSON file — directly, and defaults
`original_timesteps` to the FlowSystem's own grid. That default is always correct
for the case the method already requires, since it validates that the target has
`n_original_clusters * timesteps_per_cluster` timesteps.
`original_timesteps` is rejected when combined with a `Clustering`, which already
carries its own timesteps; silently ignoring it would hide the mistake.
Passing a `Clustering` is unchanged.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SkyekJFFtk63wUj8W6v93E
Contributor
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthrough
ChangesClustering reuse
Estimated code review effort: 2 (Simple) | ~10 minutes Sequence Diagram(s)sequenceDiagram
participant FlowSystem
participant TransformAccessor
participant Clustering
FlowSystem->>TransformAccessor: apply_clustering(clustering, original_timesteps)
TransformAccessor->>Clustering: normalize ClusteringResult or dict
Clustering-->>TransformAccessor: reusable clustering
TransformAccessor-->>FlowSystem: apply cluster assignments
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
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.
Note
Stacked on #766 — base is
dependabot/pip/tsam-4.0.0. Review/merge that first; this diff shows only the changes on top of it.Reusing a clustering computed outside flixopt worked, but only by importing
Clusteringand building the wrapper by hand — and guessing whatoriginal_timestepsshould be:Change
transform.apply_clustering()now accepts a tsam_xarrayClusteringResult, or itsto_dict()form (what lands in a JSON file), in addition to aClustering:original_timestepsdefaults to the FlowSystem's own grid. That default is always correct for the case the method already requires, since it validates the target hasn_original_clusters × timesteps_per_clustertimesteps — so the two can't disagree without the existing check firing first.Passing
original_timestepsalongside aClusteringraisesValueErrorrather than being ignored: aClusteringalready carries its own timesteps, so accepting it silently would hide the mistake.Passing a
Clusteringis unchanged — this is purely additive.Tests
7 new tests in
TestApplyClusteringFromClusteringResultcovering each accepted input form, equivalence with theClusteringpath, theoriginal_timestepsdefault and override, the rejection case, and the cross-tooltsam_xarray.load_clustering()round-trip.Docs
New "Reusing a Clustering" section in the clustering guide covering all three input forms, JSON persistence, and the time-grid constraint.
🤖 Generated with Claude Code
https://claude.ai/code/session_01SkyekJFFtk63wUj8W6v93E
Summary by CodeRabbit
New Features
Documentation