From a6a40cf54fab3c035d42bd4fe682e1c1a91ca3d1 Mon Sep 17 00:00:00 2001 From: Jonas Hoersch Date: Fri, 1 Dec 2017 15:23:48 +0100 Subject: [PATCH] pf, descriptors: Add degree function for compatibility with NX2.0 --- pypsa/descriptors.py | 7 +++++++ pypsa/pf.py | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/pypsa/descriptors.py b/pypsa/descriptors.py index d6f603ae9..739ae44f5 100644 --- a/pypsa/descriptors.py +++ b/pypsa/descriptors.py @@ -89,6 +89,13 @@ def __init__(self, data=None, **attr): else: raise ImportError("NetworkX version {} is too old. At least 1.10 is needed.".format(nx.__version__)) +if _nx_version >= '2.0': + def degree(G): + return G.degree() +else: + def degree(G): + return G.degree_iter() + class Dict(dict): """ Dict is a subclass of dict, which allows you to get AND SET diff --git a/pypsa/pf.py b/pypsa/pf.py index 9a87db76f..6ff83da9c 100644 --- a/pypsa/pf.py +++ b/pypsa/pf.py @@ -43,7 +43,7 @@ from itertools import chain import time -from .descriptors import get_switchable_as_dense, allocate_series_dataframes, Dict, zsum +from .descriptors import get_switchable_as_dense, allocate_series_dataframes, Dict, zsum, degree pd.Series.zsum = zsum @@ -836,7 +836,7 @@ def find_tree(sub_network, weight='x_pu'): sub_network.tree = nx.minimum_spanning_tree(graph) #find bus with highest degree to use as slack - tree_slack_bus, slack_degree = max(sub_network.tree.degree_iter(), key=itemgetter(1)) + tree_slack_bus, slack_degree = max(degree(sub_network.tree), key=itemgetter(1)) logger.info("Tree slack bus is %s with degree %d.", tree_slack_bus, slack_degree) #determine which buses are supplied in tree through branch from slack