From 2f3005e55512cee79639c491c6244a24fdbc3b89 Mon Sep 17 00:00:00 2001 From: Fabian Date: Thu, 29 Jun 2023 15:37:29 +0200 Subject: [PATCH 1/3] use pypsa.clustering.spatial namespace --- scripts/_helpers.py | 2 +- scripts/cluster_network.py | 6 +++--- scripts/simplify_network.py | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/scripts/_helpers.py b/scripts/_helpers.py index 512cb87a4..0e1d23276 100644 --- a/scripts/_helpers.py +++ b/scripts/_helpers.py @@ -283,7 +283,7 @@ def get_aggregation_strategies(aggregation_strategies): # when custom values are specified in the config. import numpy as np - from pypsa.networkclustering import _make_consense + from pypsa.clustering.spatial import _make_consense bus_strategies = dict(country=_make_consense("Bus", "country")) bus_strategies.update(aggregation_strategies.get("buses", {})) diff --git a/scripts/cluster_network.py b/scripts/cluster_network.py index 52685af24..14741f9eb 100644 --- a/scripts/cluster_network.py +++ b/scripts/cluster_network.py @@ -89,7 +89,7 @@ **Is it possible to run the model without the** ``simplify_network`` **rule?** No, the network clustering methods in the PyPSA module - `pypsa.networkclustering `_ + `pypsa.clustering.spatial `_ do not work reliably with multiple voltage levels and transformers. .. tip:: @@ -134,7 +134,7 @@ import pypsa import seaborn as sns from _helpers import configure_logging, get_aggregation_strategies, update_p_nom_max -from pypsa.networkclustering import ( +from pypsa.clustering.spatial import ( busmap_by_greedy_modularity, busmap_by_hac, busmap_by_kmeans, @@ -484,7 +484,7 @@ def plot_busmap_for_n_clusters(n, n_clusters, fn=None): # Fast-path if no clustering is necessary busmap = n.buses.index.to_series() linemap = n.lines.index.to_series() - clustering = pypsa.networkclustering.Clustering( + clustering = pypsa.clustering.spatial.Clustering( n, busmap, linemap, linemap, pd.Series(dtype="O") ) else: diff --git a/scripts/simplify_network.py b/scripts/simplify_network.py index 79161760a..b4c0829b3 100644 --- a/scripts/simplify_network.py +++ b/scripts/simplify_network.py @@ -95,13 +95,13 @@ from _helpers import configure_logging, get_aggregation_strategies, update_p_nom_max from add_electricity import load_costs from cluster_network import cluster_regions, clustering_for_n_clusters -from pypsa.io import import_components_from_dataframe, import_series_from_dataframe -from pypsa.networkclustering import ( +from pypsa.clustering.spatial import ( aggregategenerators, aggregateoneport, busmap_by_stubs, get_clustering_from_busmap, ) +from pypsa.io import import_components_from_dataframe, import_series_from_dataframe from scipy.sparse.csgraph import connected_components, dijkstra logger = logging.getLogger(__name__) From 0dd1933057f3a8849114cba122f50881e791358c Mon Sep 17 00:00:00 2001 From: Fabian Date: Thu, 29 Jun 2023 16:21:45 +0200 Subject: [PATCH 2/3] solve_network: fix argument for linopy groupby --- scripts/solve_network.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/scripts/solve_network.py b/scripts/solve_network.py index d80e38a08..58f4db721 100644 --- a/scripts/solve_network.py +++ b/scripts/solve_network.py @@ -235,8 +235,7 @@ def add_CCL_constraints(n, config): p_nom = n.model["Generator-p_nom"] gens = n.generators.query("p_nom_extendable").rename_axis(index="Generator-ext") - grouper = [gens.bus.map(n.buses.country), gens.carrier] - grouper = xr.DataArray(pd.MultiIndex.from_arrays(grouper), dims=["Generator-ext"]) + grouper = pd.concat([gens.bus.map(n.buses.country), gens.carrier]) lhs = p_nom.groupby(grouper).sum().rename(bus="country") minimum = xr.DataArray(agg_p_nom_minmax["min"].dropna()).rename(dim_0="group") From 00ec36a12e43be64ef9001fbdbdb98996f77e899 Mon Sep 17 00:00:00 2001 From: Fabian Date: Thu, 29 Jun 2023 16:38:56 +0200 Subject: [PATCH 3/3] add_electricity: filter out nan geocoords in kdtree --- scripts/add_electricity.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/add_electricity.py b/scripts/add_electricity.py index 594988bdb..ecd3b70e1 100755 --- a/scripts/add_electricity.py +++ b/scripts/add_electricity.py @@ -644,7 +644,7 @@ def attach_OPSD_renewables(n, tech_map): buses = n.buses.loc[gens.bus.unique()] gens_per_bus = gens.groupby("bus").p_nom.count() - caps = map_country_bus(df.query("Fueltype == @fueltype"), buses) + caps = map_country_bus(df.query("Fueltype == @fueltype and lat == lat"), buses) caps = caps.groupby(["bus"]).Capacity.sum() caps = caps / gens_per_bus.reindex(caps.index, fill_value=1)