<jupyter_start><jupyter_text>Network Clustering  In this example, we show how pypsa can deal with spatial clustering of networks.<jupyter_code>import pypsa
import re
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import pandas as pd
from pypsa.networkclustering import get_clustering_from_busmap, busmap_by_kmeans
n = pypsa.examples.scigrid_de()
n.lines["type"] = np.nan # delete the 'type' specifications to make this example easier<jupyter_output>WARNING:pypsa.io:
Importing PyPSA from older version of PyPSA than current version.
Please read the release notes at https://pypsa.org/doc/release_notes.html
carefully to prepare your network for import. 
Currently used PyPSA version [0, 18, 1], imported network file PyPSA version [0, 17, 1].

INFO:pypsa.io:Imported network scigrid-de.nc has buses, generators, lines, loads, storage_units, transformers<jupyter_text>The important information that pypsa needs for spatial clustering is in the `busmap`. It contains the mapping of which buses should be grouped together, similar to the groupby groups as we know it from pandas.You can either calculate a `busmap` from the provided clustering algorithms or you can create/use your own busmap.  Cluster by custom busmap Let's start with creating our own. In the following, we group all buses together which belong to the same operator. Buses which do not have a specific operator just stay on its own.<jupyter_code>groups = n.buses.operator.apply(lambda x: re.split(" |,|;", x)[0])
busmap = groups.where(groups != "", n.buses.index)<jupyter_output><empty_output><jupyter_text>Now we cluster the network based on the busmap.<jupyter_code>C = get_clustering_from_busmap(n, busmap)<jupyter_output><empty_output><jupyter_text>`C` is a Clustering object which contains all important information.Among others, the new network is now stored in that Clustering object.<jupyter_code>nc = C.network<jupyter_output><empty_output><jupyter_text>We have a look at the original and the clustered topology<jupyter_code>fig, (ax, ax1) = plt.subplots(1, 2, subplot_kw={"projection": ccrs.EqualEarth()}, figsize=(12,12))
plot_kwrgs = dict(bus_sizes=1e-3, line_widths=0.5)
n.plot(ax=ax, title="original", **plot_kwrgs)
nc.plot(ax=ax1, title="clustered by operator", **plot_kwrgs)
fig.tight_layout()<jupyter_output><empty_output><jupyter_text>Looks a bit messy as over 120 buses do not have assigned operators.  Clustering by busmap created from K-means Let's now make a clustering based on the kmeans algorithm.Therefore we calculate the `busmap` from a non-weighted kmeans clustering.<jupyter_code>weighting = pd.Series(1, n.buses.index)
busmap2 = busmap_by_kmeans(n, bus_weightings=weighting, n_clusters=50)<jupyter_output>/home/fabian/.miniconda3/lib/python3.8/site-packages/sklearn/base.py:434: UserWarning:

X has feature names, but KMeans was fitted without feature names<jupyter_text>We use this new kmeans-based `busmap` to create a new clustered method.<jupyter_code>C2 = get_clustering_from_busmap(n, busmap2)
nc2 = C2.network<jupyter_output><empty_output><jupyter_text>Again, let's plot the networks to compare:<jupyter_code>fig, (ax, ax1) = plt.subplots(1, 2, subplot_kw={"projection": ccrs.EqualEarth()}, figsize=(12,12))
plot_kwrgs = dict(bus_sizes=1e-3, line_widths=0.5)
n.plot(ax=ax, title="original", **plot_kwrgs)
nc2.plot(ax=ax1, title="clustered by kmeans", **plot_kwrgs)
fig.tight_layout()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09744779582366589, 'fraction_numerical': 0.014791183294663572, 'mean_word_length': 6.630530973451328, 'pattern_counts': {'":': 2, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '134464', 'license': 'MIT', 'n_tokens_mistral': 1217, 'n_tokens_neox': 1137, 'n_words': 352, 'path': 'examples/notebooks/spatial-clustering.ipynb', 'repo_name': 'readthedocs-assistant/PyPSA', 'size': '3448'}