Skip to content

Commit f5724cc

Browse files
committed
1. generate_example_systems.py - Fixed incorrect file name in error message: Zeitreihen2020.csv → tmy_dresden.csv
2. clustering/base.py clusters() - Now validates dimensions for all variables, not just the first one, and checks for consistency between variables 3. clustering/base.py cluster_structure_from_mapping() - Fixed cluster occurrence mapping to handle non-contiguous cluster IDs by using max(unique_clusters) + 1 instead of len(unique_clusters) for n_clusters
1 parent be053cb commit f5724cc

2 files changed

Lines changed: 22 additions & 12 deletions

File tree

docs/notebooks/data/generate_example_systems.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def _get_weather() -> pd.DataFrame:
6666
_weather = load_weather()
6767
except FileNotFoundError as e:
6868
raise FileNotFoundError(
69-
f'Weather data file not found. Ensure Zeitreihen2020.csv exists in {DATA_DIR}. Original error: {e}'
69+
f'Weather data file not found. Ensure tmy_dresden.csv exists in {DATA_DIR}/raw. Original error: {e}'
7070
) from e
7171
return _weather
7272

flixopt/clustering/base.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -892,16 +892,26 @@ def clusters(
892892
n_clusters = int(cs.n_clusters) if isinstance(cs.n_clusters, (int, np.integer)) else int(cs.n_clusters.values)
893893
timesteps_per_cluster = cs.timesteps_per_cluster
894894

895-
# Check dimensions of aggregated data
896-
sample_var = aggregated_data[resolved_variables[0]]
897-
has_cluster_dim = 'cluster' in sample_var.dims
898-
extra_dims = [d for d in sample_var.dims if d not in ('time', 'cluster')]
899-
if extra_dims:
900-
raise ValueError(
901-
f'clusters() requires data with only time (or cluster, time) dimensions. '
902-
f'Found extra dimensions: {extra_dims}. '
903-
f'Use select={{{extra_dims[0]!r}: <value>}} to select a specific {extra_dims[0]}.'
904-
)
895+
# Check dimensions of all variables for consistency
896+
has_cluster_dim = None
897+
for var in resolved_variables:
898+
da = aggregated_data[var]
899+
var_has_cluster = 'cluster' in da.dims
900+
extra_dims = [d for d in da.dims if d not in ('time', 'cluster')]
901+
if extra_dims:
902+
raise ValueError(
903+
f'clusters() requires data with only time (or cluster, time) dimensions. '
904+
f'Variable {var!r} has extra dimensions: {extra_dims}. '
905+
f'Use select={{{extra_dims[0]!r}: <value>}} to select a specific {extra_dims[0]}.'
906+
)
907+
if has_cluster_dim is None:
908+
has_cluster_dim = var_has_cluster
909+
elif has_cluster_dim != var_has_cluster:
910+
raise ValueError(
911+
f'All variables must have consistent dimensions. '
912+
f'Variable {var!r} has {"" if var_has_cluster else "no "}cluster dimension, '
913+
f'but previous variables {"do" if has_cluster_dim else "do not"}.'
914+
)
905915

906916
# Build Dataset with cluster dimension, using labels with occurrence counts
907917
# Check if cluster_occurrences has extra dims
@@ -1147,11 +1157,11 @@ def create_cluster_structure_from_mapping(
11471157

11481158
# Count occurrences of each cluster
11491159
unique_clusters = np.unique(cluster_order)
1160+
n_clusters = int(unique_clusters.max()) + 1 if len(unique_clusters) > 0 else 0
11501161
occurrences = {}
11511162
for c in unique_clusters:
11521163
occurrences[int(c)] = sum(1 for x in cluster_order if x == c)
11531164

1154-
n_clusters = len(unique_clusters)
11551165
cluster_occurrences_da = xr.DataArray(
11561166
[occurrences.get(c, 0) for c in range(n_clusters)],
11571167
dims=['cluster'],

0 commit comments

Comments
 (0)