def busmap_by_spectral_clustering(network, n_clusters, **kwds): if find_spec('sklearn') is None: raise ModuleNotFoundError( "Optional dependency 'sklearn' not found." "Install via 'conda install -c conda-forge scikit-learn' " "or 'pip install scikit-learn'") from sklearn.cluster import spectral_clustering as sk_spectral_clustering weight = { "Line": network.lines.s_max_pu * network.lines.s_nom.clip(.1) / abs(network.lines.r + 1j * network.lines.x), "Link": network.links.p_max_pu * network.links.p_nom.clip(.1) } A = network.adjacency_matrix(branch_components=["Line", "Link"], weights=weight) # input arg for spectral clustering must be symmetric, but A is directed. use A+A.T: return pd.Series(sk_spectral_clustering(A + A.T, n_clusters=n_clusters).astype(str), index=network.buses.index)
def busmap_by_spectral_clustering(network, n_clusters, **kwds): lines = network.lines.loc[:,['bus0', 'bus1']].assign(weight=1./network.lines.x).set_index(['bus0','bus1']) G = OrderedGraph() G.add_nodes_from(network.buses.index) G.add_edges_from((u,v,dict(weight=w)) for (u,v),w in lines.itertuples()) return pd.Series(sk_spectral_clustering(nx.adjacency_matrix(G), n_clusters, **kwds) + 1, index=network.buses.index)
def busmap_by_spectral_clustering(network, n_clusters, **kwds): lines = network.lines.loc[:,['bus0', 'bus1']].assign(weight=network.lines.num_parallel).set_index(['bus0','bus1']) lines.weight+=0.1 G = nx.Graph() G.add_nodes_from(network.buses.index) G.add_edges_from((u,v,dict(weight=w)) for (u,v),w in lines.itertuples()) return pd.Series(list(map(str,sk_spectral_clustering(nx.adjacency_matrix(G), n_clusters, **kwds) + 1)), index=network.buses.index)
def busmap_by_spectral_clustering(network, n_clusters, **kwds): if find_spec('sklearn') is None: raise ModuleNotFoundError( "Optional dependency 'sklearn' not found." "Install via 'conda install -c conda-forge scikit-learn' " "or 'pip install scikit-learn'") from sklearn.cluster import spectral_clustering as sk_spectral_clustering lines = network.lines.loc[:, ['bus0', 'bus1']].assign( weight=network.lines.num_parallel).set_index(['bus0', 'bus1']) lines.weight += 0.1 G = nx.Graph() G.add_nodes_from(network.buses.index) G.add_edges_from( (u, v, dict(weight=w)) for (u, v), w in lines.itertuples()) return pd.Series(list( map( str, sk_spectral_clustering(nx.adjacency_matrix(G), n_clusters, **kwds) + 1)), index=network.buses.index)