From 3c3f46ba4250710bbb0380e973fed1b9156a469d Mon Sep 17 00:00:00 2001 From: Mathias157 Date: Tue, 15 Oct 2024 18:24:58 +0200 Subject: [PATCH] Updated connectivity based on grid, removed the cancellation of Slagelse-Nyborg line, since it makes the clustering algorithm not work which may explain weird connections, and added plotting of line capacities for clustered data --- src/Modules/aggregate_inputs.py | 53 ++++++++++++++++++++++++++++----- src/Modules/clustering.py | 6 ++-- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/src/Modules/aggregate_inputs.py b/src/Modules/aggregate_inputs.py index 6d41c1d..75f01a5 100644 --- a/src/Modules/aggregate_inputs.py +++ b/src/Modules/aggregate_inputs.py @@ -10,11 +10,13 @@ ### 0. Script Settings ### ### ------------------------------- ### +import matplotlib.pyplot as plt import os import pandas as pd import numpy as np import geopandas as gpd import click +from typing import Union from pybalmorel import Balmorel, IncFile from pybalmorel.utils import symbol_to_df import gams @@ -175,6 +177,10 @@ def aggregate_parameter(db: gams.GamsDatabase, prefix=prefix, suffix=suffix) f.body = df + # Plot connection costs in case it is a connection parameter + if 'INVCOST' in symbol: + plot_transmission_invcost(symbol, df) + # Use N-1 sets as index, and the last as columns, where N = length of columns without 'Value' column if len(symbol_columns) > 2: index = symbol_columns[:-2] @@ -223,6 +229,35 @@ def aggregate_sets(db: gams.GamsDatabase, f.save() +def plot_transmission_invcost(symbol: str, + df: pd.DataFrame, + year: int = 2050): + print('Plotting connection costs for %s'%symbol) + + # f = gpd.read_file('ClusterOutput/%s_%dcluster_geofile.gpkg'%('-'.join(cluster_params_list), cluster_size)) + geo_union = gpd.read_file('ClusterOutput/DE-DH-WNDFLH-SOLEFLH_30cluster_geofile.gpkg') + geo_union['coord'] = geo_union.centroid + + fig, ax = plt.subplots() + geo_union.plot(ax=ax) + + # lines = [] + exclusion = [] + for line, cap in df.loc[str(year)].iterrows(): + if line in exclusion or line[0] == line[1]: + continue + + coords = geo_union.query('cluster_name == @line[0] or cluster_name == @line[1]')['coord'] + x1, x2 = coords.x + y1, y2 = coords.y + + l, = ax.plot([x1,x2], [y1,y2], color = 'r', linewidth = cap['Value']*1e-5) + # lines.append(l) + + exclusion.append((line[1], line[0])) + # print(df.drop) + plt.show() + #%% ------------------------------- ### ### 2. Main ### @@ -231,13 +266,15 @@ def aggregate_sets(db: gams.GamsDatabase, @click.command() @click.option('--model-path', type=str, required=True, help='Balmorel model path') @click.option('--scenario', type=str, required=True, help='Balmorel scenario') -@click.option('--exceptions', type=str, required=False, help='.inc files that should NOT be generated') -@click.option('--mean-aggfuncs', type=str, required=False, help='Parameters that should be aggregated with an average') -@click.option('--median-aggfuncs', type=str, required=False, help='Parameters that should be aggregated using the median value') -@click.option('--zero-fillnas', type=str, required=False, help='NaN values that should be converted to zero instead of EPS') -def main(model_path: str, scenario: str, exceptions: str = '', - mean_aggfuncs: str = '', median_aggfuncs: str = '', - zero_fillnas: str = '', incfile_folder: str = 'Output'): +@click.option('--exceptions', type=str, required=False, default='', help='.inc files that should NOT be generated') +@click.option('--mean-aggfuncs', type=str, required=False, default='', help='Parameters that should be aggregated with an average') +@click.option('--median-aggfuncs', type=str, required=False, default='', help='Parameters that should be aggregated using the median value') +@click.option('--zero-fillnas', type=str, required=False, default='', help='NaN values that should be converted to zero instead of EPS') +@click.option('--only-symbols', type=str, required=False, help="Only aggregate the symbols, input as comma-separated string") +def main(model_path: str, scenario: str, exceptions: str, + mean_aggfuncs: str, median_aggfuncs: str, + zero_fillnas: str, incfile_folder: str = 'Output', + only_symbols: Union[str, None] = None): # Make configuration lists exceptions = exceptions.replace(' ', '').split(',') # Symbols not to aggregate @@ -258,6 +295,8 @@ def main(model_path: str, scenario: str, exceptions: str = '', # Get which .inc-files and how to aggregate based on folder content and configurations symbols, aggfuncs, fillnas = get_symbols_to_aggregate(incfile_folder, exceptions, mean_aggfuncs, median_aggfuncs, zero_fillnas) + if only_symbols != None: + symbols = only_symbols.replace(' ', '').split(',') # Aggregating parameters and sets print('Will attempt to aggregate..\n%s\n'%(','.join(symbols))) diff --git a/src/Modules/clustering.py b/src/Modules/clustering.py index 697ad10..8728c38 100644 --- a/src/Modules/clustering.py +++ b/src/Modules/clustering.py @@ -200,7 +200,6 @@ def cluster(collected_data: pd.DataFrame, use_connectivity: bool = True, manual_corrections: list = [ ['Roedovre', 'Frederiksberg', 1], - ['Slagelse', 'Nyborg', 0], ], linkage: str = 'Ward', connection_remark: str = 'connec. included + artifical', @@ -219,7 +218,7 @@ def cluster(collected_data: pd.DataFrame, connectivity.connection.loc[manual_connection[0], manual_connection[1]] = manual_connection[2] connectivity.connection.loc[manual_connection[1], manual_connection[0]] = manual_connection[2] - + print('Is matrix symmetric?', np.all(connectivity.connection.data == connectivity.connection.data.T)) ## Make symmetric index, so the indices fit collected_data = collected_data.assign_coords(IRRRI=collected_data.coords['IRRRE'].data) @@ -229,6 +228,7 @@ def cluster(collected_data: pd.DataFrame, ## Make symmetric connectivity graph knn_graph = X.connection.data # get numpy array + print('Is matrix still symmetric?', np.all(knn_graph == knn_graph.T)) knn_graph = csr_matrix(knn_graph) # make dense format ## Drop the connection variable again @@ -272,7 +272,7 @@ def cluster(collected_data: pd.DataFrame, clustering.plot(ax=ax, column='cluster_group', cmap=truncate_colormap(cmap, 0.2, 1)) - if knn_graph == None: + if knn_graph is None: connection_remark = 'no connectivity' plot_title = '%s, %d clusters, %s\ndata: %s'%(linkage,