Skip to content

Commit 2af82d7

Browse files
FBumannclaude
andcommitted
fix: validate weight keys against selected clustering input, not full dataset
- Validate against ds_for_clustering (after data_vars selection) instead of ds (full dataset), so weights for excluded variables are caught - Move validation before drop_constant_arrays so constant columns remain accepted as valid keys - Fix test to use to_dataset() instead of clustering_data() so the constant-column path is actually exercised Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 626727c commit 2af82d7

2 files changed

Lines changed: 12 additions & 9 deletions

File tree

flixopt/transform_accessor.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,20 +1694,20 @@ def cluster(
16941694
else:
16951695
ds_for_clustering = ds
16961696

1697-
# Filter constant arrays once on the full dataset (not per slice)
1698-
# This ensures all slices have the same variables for consistent metrics
1699-
ds_for_clustering = drop_constant_arrays(ds_for_clustering, dim='time')
1700-
1701-
# Validate user-provided weight keys against available variables
1697+
# Validate user-provided weight keys against the selected clustering input
17021698
if cluster is not None and cluster.weights is not None:
1703-
all_vars = set(ds.data_vars)
1704-
unknown = sorted(set(cluster.weights) - all_vars)
1699+
selected_vars = set(ds_for_clustering.data_vars)
1700+
unknown = sorted(set(cluster.weights) - selected_vars)
17051701
if unknown:
17061702
raise ValueError(
17071703
f'ClusterConfig weights reference unknown variables: {unknown}. '
17081704
f'Available variables can be found via transform.clustering_data().'
17091705
)
17101706

1707+
# Filter constant arrays once on the full dataset (not per slice)
1708+
# This ensures all slices have the same variables for consistent metrics
1709+
ds_for_clustering = drop_constant_arrays(ds_for_clustering, dim='time')
1710+
17111711
# Guard against empty dataset after removing constant arrays
17121712
if not ds_for_clustering.data_vars:
17131713
filter_info = f'data_vars={data_vars}' if data_vars else 'all variables'

tests/test_clustering/test_integration.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,12 @@ def test_extra_weight_keys_filtered_with_constant_column(self):
396396
constant_sink = Sink('constant_load', inputs=[constant_flow])
397397
fs.add_elements(source, sink, constant_sink, bus)
398398

399-
# The constant column name (find it from clustering_data)
400-
all_data = fs.transform.clustering_data()
399+
# Use to_dataset() to get ALL columns including the constant one
400+
# (clustering_data() already strips constants, so it wouldn't test the path)
401+
all_data = fs.to_dataset(include_solution=False)
401402
all_columns = set(all_data.data_vars)
403+
clustering_columns = set(fs.transform.clustering_data().data_vars)
404+
assert all_columns > clustering_columns, 'Test requires at least one constant column to be meaningful'
402405

403406
# Build weights that reference ALL columns including the constant one
404407
# that will be dropped — these are valid variables, just constant over time

0 commit comments

Comments
 (0)