def test_directed(self): G = nx.DiGraph(nx.path_graph(4)) communities = list(nx.girvan_newman(G)) assert_equal(len(communities), 3) validate_communities(communities[0], [{0, 1}, {2, 3}]) validate_possible_communities(communities[1], [{0}, {1}, {2, 3}], [{0, 1}, {2}, {3}]) validate_communities(communities[2], [{0}, {1}, {2}, {3}])
def test_most_valuable_edge(self): G = nx.Graph() G.add_weighted_edges_from([(0, 1, 3), (1, 2, 2), (2, 3, 1)]) # Let the most valuable edge be the one with the highest weight. heaviest = lambda G: max(G.edges(data='weight'), key=itemgetter(2))[:2] communities = list(nx.girvan_newman(G, heaviest)) assert_equal(len(communities), 3) validate_communities(communities[0], [{0}, {1, 2, 3}]) validate_communities(communities[1], [{0}, {1}, {2, 3}]) validate_communities(communities[2], [{0}, {1}, {2}, {3}])
def test_selfloops(self): G = nx.path_graph(4) G.add_edge(0, 0) G.add_edge(2, 2) communities = list(nx.girvan_newman(G)) assert_equal(len(communities), 3) validate_communities(communities[0], [{0, 1}, {2, 3}]) validate_possible_communities(communities[1], [{0}, {1}, {2, 3}], [{0, 1}, {2}, {3}]) validate_communities(communities[2], [{0}, {1}, {2}, {3}])
def test_girvan_newman_unweighted(self): g = nx.Graph() g.add_edges_from([(1, 3), (1, 2), (2, 3), (3, 7), (7, 6), (6, 4), (6, 5), (5, 4), (7, 8), (8, 9), (9, 11), (9, 10), (10, 11), (8, 12), (12, 13), (12, 14), (13, 14)]) result = nx.girvan_newman(g) assert_equal(len(result), 3) validate_communities(result[0], [(1, 2, 3, 4, 5, 6, 7), (8, 9, 10, 11, 12, 13, 14)]) validate_communities(result[1], [(1, 2, 3), (4, 5, 6), (9, 10, 11), (12, 13, 14), (7, ), (8, )]) validate_communities(result[2], [(1,), (2, ), (3, ), (4, ), (5, ), (6, ), (7, ), (8, ), (9, ), (10,), (11, ), (12, ), (13, ), (14, )]) dg = g.to_directed() result = nx.girvan_newman(dg) validate_communities(result[0], [(1, 2, 3, 4, 5, 6, 7), (8, 9, 10, 11, 12, 13, 14)]) validate_communities(result[1], [(1, 2, 3), (4, 5, 6), (9, 10, 11), (12, 13, 14), (7, ), (8, )]) validate_communities(result[2], [(1,), (2, ), (3, ), (4, ), (5, ), (6, ), (7, ), (8, ), (9, ), (10,), (11, ), (12, ), (13, ), (14, )])
def test_undirected(self): # Start with the graph .-.-.-. G = nx.path_graph(4) communities = list(nx.girvan_newman(G)) assert_equal(len(communities), 3) # After one removal, we get the graph .-. .-. validate_communities(communities[0], [{0, 1}, {2, 3}]) # After the next, we get the graph .-. . ., but there are two # symmetric possible verisons. validate_possible_communities(communities[1], [{0}, {1}, {2, 3}], [{0, 1}, {2}, {3}]) # After the last removal, we alway get the empty graph. validate_communities(communities[2], [{0}, {1}, {2}, {3}])
def get_girvan(g, n): girv = nx.girvan_newman(g) return list(islice(girv, 0, n))[n - 1]
lines["from"].append(index1) lines["to"].append(index2) lines["geometry"].append(LineString([room1.geometry.centroid, room2.geometry.centroid])) edges = gpd.GeoDataFrame(lines) # let's create network, not just displaying.. room_network = nx.Graph() for i, edge in edges.iterrows(): room_network.add_edge(edge["from"], edge["to"]) # calculate closeness and add the values to rooms closeness = nx.closeness_centrality(room_network) rooms['closeness'] = pd.Series(closeness) # color rooms by closeness fig, ax = plt.subplots() fig.set_size_inches(12, 12) ax.axison = False rooms.plot(ax=ax, linewidth=0.3, column='closeness', cmap='coolwarm') edges.plot(ax=ax, linewidth=0.3, color='#888888') rooms.geometry.centroid.plot(ax=ax, markersize=6, color='#DDDDDD') ax.set_aspect('equal') plt.show() comm = nx.girvan_newman(room_network) print(comm)
def test_girvan_newman_no_edges(self): g = nx.Graph() g.add_nodes_from([1, 2, 3, 4, 5]) result = nx.girvan_newman(g) assert_equal(len(result), 0) validate_communities(result, [])
def test_no_edges(self): G = nx.empty_graph(3) communities = list(nx.girvan_newman(G)) assert_equal(len(communities), 1) validate_communities(communities[0], [{0}, {1}, {2}])