def betweenness(network, normalized=False): assert isinstance(network, Network), \ "network must be an instance of Network" Log.add('Calculating betweenness centralities ...', Severity.INFO) all_paths = shortest_paths(network) node_centralities = defaultdict(lambda: 0) for s in all_paths: for d in all_paths[s]: for p in all_paths[s][d]: for x in p[1:-1]: if s != d != x: node_centralities[x] += 1.0 / len(all_paths[s][d]) if normalized: max_centr = max(node_centralities.values()) for v in node_centralities: node_centralities[v] /= max_centr # assign zero values to nodes not occurring on shortest paths for v in network.nodes: node_centralities[v] += 0 return node_centralities
def test_get_shortest_paths(path_from_ngram_file): paths_dict = shortest_paths(path_from_ngram_file) expected_paths = { ('d', 'a'): {('d', 'a')}, ('b', 'd'): {('b', 'c', 'd')}, ('d', 'e'): {('d', 'e')}, ('a', 'c'): {('a', 'b', 'c')}, ('a', 'a'): {('a', )}, ('e', 'a'): {('e', 'd', 'a')}, ('e', 'b'): {('e', 'd', 'a', 'b')}, ('e', 'e'): {('e', )}, ('a', 'b'): {('a', 'b')}, ('b', 'b'): {('b', )}, ('c', 'd'): {('c', 'd')}, ('d', 'b'): {('d', 'a', 'b')}, ('c', 'a'): {('c', 'd', 'a')}, ('b', 'a'): {('b', 'c', 'd', 'a')}, ('c', 'b'): {('c', 'd', 'a', 'b')}, ('e', 'd'): {('e', 'd')}, ('a', 'd'): {('a', 'b', 'c', 'd')}, ('d', 'd'): {('d', )}, ('c', 'c'): {('c', )}, ('b', 'c'): {('b', 'c')} } paths_to_check = dict() for k in paths_dict: for p in paths_dict[k]: paths_to_check[(k, p)] = paths_dict[k][p] assert paths_to_check == expected_paths
def _bw(paths, normalized=False): """Calculates the betweenness of nodes based on observed shortest paths between all pairs of nodes Parameters ---------- paths: Paths object normalized: bool normalize such that largest value is 1.0 Returns ------- dict """ assert isinstance(paths, Paths), "argument must be an instance of pathpy.Paths" node_centralities = defaultdict(lambda: 0) Log.add('Calculating betweenness in paths ...', Severity.INFO) all_paths = shortest_paths(paths) for s in all_paths: for d in all_paths[s]: for p in all_paths[s][d]: for x in p[1:-1]: if s != d != x: node_centralities[x] += 1.0 / len(all_paths[s][d]) if normalized: max_centr = max(node_centralities.values()) for v in node_centralities: node_centralities[v] /= max_centr # assign zero values to nodes not occurring on shortest paths nodes = paths.nodes for v in nodes: node_centralities[v] += 0 Log.add('finished.') return node_centralities
def _bw(higher_order_net, normalized=False): """Calculates the betweenness of all nodes. If the order of the higher-order network is larger than one centralities calculated based on the higher-order topology will automatically be projected back to first-order nodes. Parameters ---------- network: HigherOrderNetwork an instance of a pathpy HigherOrderNetwork normalized: If set to True, betweenness centralities of nodes will be scaled by the maximum value (default False) Returns ------- dict Dictionary containing as the keys the higher order node and as values their centralities """ assert isinstance(higher_order_net, HigherOrderNetwork), \ "arguments must be an instance of HigherOrderNetwork" Log.add('Calculating betweenness (order k = %s) ...' % \ higher_order_net.order, Severity.INFO) all_paths = shortest_paths(higher_order_net) node_centralities = defaultdict(lambda: 0) shortest_paths_first_order = defaultdict(lambda: defaultdict(set)) shortest_paths_first_order_lengths = defaultdict( lambda: defaultdict(lambda: _np.inf)) for path_1_ord_k in all_paths: for path_2_ord_k in all_paths: source_k1 = higher_order_net.higher_order_node_to_path( path_1_ord_k)[0] dest_k1 = higher_order_net.higher_order_node_to_path( path_2_ord_k)[-1] # we consider a path in a k-th order network # connecting first-order node s1 to d1 for path_ord_k in all_paths[path_1_ord_k][path_2_ord_k]: # convert k-th order path to first-order path and add #shortest_paths_first_order[source_k1][dest_k1].add( # higher_order_net.higher_order_path_to_first_order(path_ord_k)) p1 = higher_order_net.higher_order_path_to_first_order( path_ord_k) # obtain start node and end node s1 = p1[0] d1 = p1[-1] # compute the length of the first-order path l = len(p1) - 1 # if path is a shortest path add it to dictionary if l < shortest_paths_first_order_lengths[s1][d1]: shortest_paths_first_order_lengths[s1][d1] = l shortest_paths_first_order[s1][d1] = set() shortest_paths_first_order[s1][d1].add(p1) elif l == shortest_paths_first_order_lengths[s1][d1]: shortest_paths_first_order[s1][d1].add(p1) for source_k1 in shortest_paths_first_order: for dest_k1 in shortest_paths_first_order[source_k1]: for path_k1 in shortest_paths_first_order[source_k1][dest_k1]: # increase betweenness of all intermediary nodes # on path from s1 to d1 for v in path_k1[1:-1]: if source_k1 != v != dest_k1: l_p = len( shortest_paths_first_order[source_k1][dest_k1]) node_centralities[v] += 1.0 / l_p if normalized: max_centr = max(node_centralities.values()) for v in node_centralities: node_centralities[v] /= max_centr # assign centrality zero to nodes not occurring on higher-order shortest paths nodes = higher_order_net.paths.nodes for v in nodes: node_centralities[v] += 0 Log.add('finished.', Severity.INFO) return node_centralities