From 82a0338e9f3f8b68eb7d73aad0ceb3b26862d028 Mon Sep 17 00:00:00 2001 From: martacki Date: Fri, 4 Feb 2022 20:27:18 +0100 Subject: [PATCH] treatment of outliers and small feature-bugfix --- scripts/cluster_network.py | 17 +++++++++-------- scripts/simplify_network.py | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/scripts/cluster_network.py b/scripts/cluster_network.py index bd49556f..2cc406eb 100644 --- a/scripts/cluster_network.py +++ b/scripts/cluster_network.py @@ -272,6 +272,8 @@ def busmap_for_n_clusters(n, n_clusters, solver_name, focus_weights=None, algori algorithm_kwds.setdefault('tol', 1e-6) def fix_country_assignment_for_hac(n): + from scipy.sparse import csgraph + # overwrite country of nodes that are disconnected from their country-topology for country in n.buses.country.unique(): m = n.copy() @@ -288,19 +290,18 @@ def busmap_for_n_clusters(n, n_clusters, solver_name, focus_weights=None, algori neighbor_bus = n.lines.query("bus0 in @disconnected_bus or bus1 in @disconnected_bus").iloc[0][['bus0','bus1']] new_country = list(set(n.buses.loc[neighbor_bus].country)-set([country]))[0] - logger.info(f"overwriting country ``{country}`` of bus ``{disconnected_bus}`` to new country ``{new_country}``, " + logger.info(f"overwriting country `{country}` of bus `{disconnected_bus}` to new country `{new_country}`, " "because it is disconnected from its inital inter-country transmission grid.") n.buses.at[disconnected_bus, "country"] = new_country return n if algorithm == "hac": - from scipy.sparse import csgraph - feature = get_feature_for_hac(n, buses_i=n.buses.index, feature=feature) n = fix_country_assignment_for_hac(n) - elif feature is not None: - logger.warning(f"Keyword argument feature is only valid for algorithm 'hac'." - f"given feature ``{feature}`` will be ignored.") + + if (algorithm != "hac") and (feature is not None): + logger.warning(f"Keyword argument feature is only valid for algorithm `hac`. " + f"Given feature `{feature}` will be ignored.") n.determine_network_topology() @@ -338,7 +339,7 @@ def clustering_for_n_clusters(n, n_clusters, custom_busmap=False, aggregate_carr line_length_factor=1.25, potential_mode='simple', solver_name="cbc", algorithm="hac", feature=None, extended_link_costs=0, focus_weights=None): - logger.info(f"Clustering network using algorithm ``{algorithm}`` and feature ``{feature}``...") + logger.info(f"Clustering network using algorithm `{algorithm}` and feature `{feature}`...") if potential_mode == 'simple': p_nom_max_strategy = np.sum @@ -348,7 +349,7 @@ def clustering_for_n_clusters(n, n_clusters, custom_busmap=False, aggregate_carr raise AttributeError(f"potential_mode should be one of 'simple' or 'conservative' but is '{potential_mode}'") if not custom_busmap: - busmap = busmap_for_n_clusters(n, n_clusters, solver_name, focus_weights, algorithm) + busmap = busmap_for_n_clusters(n, n_clusters, solver_name, focus_weights, algorithm, feature) else: busmap = custom_busmap diff --git a/scripts/simplify_network.py b/scripts/simplify_network.py index 23facc79..b28bc4dc 100644 --- a/scripts/simplify_network.py +++ b/scripts/simplify_network.py @@ -407,7 +407,21 @@ if __name__ == "__main__": n, substation_map = aggregate_to_substations(n) busmaps.append(substation_map) + # treatment of outliers (nodes without a profile for considered carrier) for "cluster_network" + if snakemake.config.get("clustering", {}).get("cluster_network", {}).get("algorithm", "hac") == "hac": + carriers = cluster_config.get("feature", "solar+onwind-time").split('-')[0].split('+') + buses_i = list(set(n.buses.index)-set(n.generators.query("carrier in @carriers").bus)) + n, busmap_hac = aggregate_to_substations(n, buses_i) + busmaps.append(busmap_hac) + if snakemake.wildcards.simpl: + # treatment of outliers (nodes without a profile for a considered carrier) for "simplify" + if cluster_config.get("algorithm", "hac") == "hac": + carriers = cluster_config.get("feature", "solar+onwind-time").split('-')[0].split('+') + buses_i = list(set(n.buses.index)-set(n.generators.query("carrier in @carriers").bus)) + n, busmap_hac = aggregate_to_substations(n, buses_i) + busmaps.append(busmap_hac) + # conduct clustering n, cluster_map = cluster(n, int(snakemake.wildcards.simpl), snakemake.config, algorithm=cluster_config.get('algorithm', 'hac'), feature=cluster_config.get('feature', None))