def create_g_cluster(self, word_pos):
        words = self.top_k(word_pos)[1:]

        if self.cluster_type < 4:
            pairs = self.gen_pairs(words)
            G = nx.Graph()
            G.add_weighted_edges_from(pairs)

        if self.cluster_type == 3:
            G = max(nx.connected_component_subgraphs(G), key=len)
            print('len_strip(G)', len(G))

        if self.cluster_type == 1:
            from networkx.algorithms.community import greedy_modularity_communities
            clusters = list(greedy_modularity_communities(G))
        elif self.cluster_type == 2:
            from chinese_whispers import chinese_whispers, aggregate_clusters
            chinese_whispers(G, iterations=20, weighting='log', seed=13)  # top, nolog, log
            clusters = aggregate_clusters(G).values()
        elif self.cluster_type == 3:
            from networkx.algorithms.community import asyn_fluidc
            if self.is_k_depends_g:
                clusters = list(asyn_fluidc(G, k=self.k - int((self.k - 8) * ((200 - len(G)) / 100))))
            else:
                clusters = list(asyn_fluidc(G, k=min(self.k, len(G))))
        elif self.cluster_type == 4:
            from collections import defaultdict
            from sklearn.cluster import KMeans

            X = [sg.emb(_) for _ in words[1:]]
            clusters = defaultdict(list)

            kmeans = KMeans(n_clusters=self.k, random_state=13)
            assigned_clusters = kmeans.fit_predict(X)

            for cl, w in zip(assigned_clusters, words): clusters[cl].append(w)
            clusters = list(clusters.values())
        elif self.cluster_type == 5:
            from collections import defaultdict
            from sklearn.cluster import DBSCAN

            X = [sg.emb(_) for _ in words[1:]]
            clusters = defaultdict(list)

            dbscan = DBSCAN(metric='l2', eps=self.min_dist_dbscan, min_samples=self.min_clust)
            assigned_clusters = dbscan.fit_predict(X)

            for cl, w in zip(assigned_clusters, words): clusters[cl].append(w)
            clusters = list(clusters.values())
        else:
            raise Exception('no cluster type', self.cluster_type)

        if self.debug:
            for i, cluster in enumerate(sorted(clusters, key=lambda e: len(e), reverse=True)):
                print('Cluster ID\tCluster Elements\n')
                print('{}\t{}\n'.format(i, cluster))
        print(word_pos, 'clusters', len(clusters))

        return clusters
Esempio n. 2
0
 def add_reservoirs(self, G):
     '''
     Adds number_of_reservoirs reservoirs to the graph G
     '''
     n_edges = G.number_of_edges()
     self._number_of_reservoirs = max(round(n_edges / 100),
                                      round(self._distance / 500), 2)
     n_communities = self._number_of_reservoirs
     community_generator = community.asyn_fluidc(self.main_connected(G),
                                                 n_communities)
     for i in range(n_communities):
         # Make a subgraph from the community nodes
         subG = G.subgraph(next(community_generator))
         # Get the maximum elevation node from the subgraph
         max_elevation_node = max(dict(subG.nodes).items(),
                                  key=lambda x: x[1]['elevation'])[0]
         Reservoir_attr = {
             'x':
             G.nodes[max_elevation_node]['x'] + random.randint(10, 30),
             'y':
             G.nodes[max_elevation_node]['y'] + random.randint(10, 30),
             'elevation':
             random.choice(self._reservoir_heads) +
             G.nodes[max_elevation_node]['elevation'],
             'demand':
             0
         }
         G.add_node('Reservoir{}'.format(i), **Reservoir_attr)
         G.add_edge('Reservoir{}'.format(i), max_elevation_node)
         G['Reservoir{}'.format(i)][max_elevation_node]['length'] = 50
         G['Reservoir{}'.format(
             i)][max_elevation_node]['roughness'] = random.choice(
                 self._roughness_values)
     return G
Esempio n. 3
0
    def add_node_demands(self, G, area=0.2):
        '''
        Add demand attribute to the nodes of the input graph. The graph is divided to communities and each community is assigned a demand value randomly from demand_values dictionnary

        area(float): The average area in hectare covered by each of the nodes
        '''
        G_copy = nx.Graph(G)
        n_nodes = len(G)
        n_communities = round(n_nodes / 4)
        # Using fluid communties algorithm for creating 4 nodes clusters
        community_generator = community.asyn_fluidc(G_copy, n_communities)
        demands = {}
        # Converting demand values to peak demands in m3/s
        demand_values = {
            key: 4 * area * 0.00379 * v / (3600 * 24)
            for key, v in self._demand_values.items()
        }
        for i in range(n_communities):
            # Construct a dictionary of demands for each community
            # (community nodes are the keys and demand values are the values)
            demands_list = list(demand_values.values())
            demands.update(
                dict.fromkeys(
                    next(community_generator),
                    {'demand': demands_list[i % len(demand_values)]}))
        # Add the demand as attributes to the nodes in the graph
        nx.set_node_attributes(G, demands)
Esempio n. 4
0
def data_sample(g, args):
    if args.split_method == 'b_min_cut':
        # balance min-cut
        labels = g.ndata['label']
        assign = min_cut(g, args.split, balance_ntypes=labels).tolist()
        index = [[] for i in range(args.split)]
        [index[ind].append(i) for i, ind in enumerate(assign)]
    elif args.split_method == 'random_choice':
        # random_choice
        assign = random_choice(args.split, g.number_of_nodes()).tolist()
        index = [[] for i in range(args.split)]
        [index[ind].append(i) for i, ind in enumerate(assign)]
    elif args.split_method == 'ub_min_cut':
        # unbalance min-cut
        assign = min_cut(g, args.split).tolist()
        index = [[] for i in range(args.split)]
        [index[ind].append(i) for i, ind in enumerate(assign)]
    elif args.split_method == 'community_detection':
        # community detection
        G = g.to_networkx().to_undirected()
        major_connected_graph(G)
        index = list(asyn_fluidc(G, args.split, seed=0))
        index = [list(k) for k in index]
    else:
        raise ValueError('Unknown split_method: {}'.format(args.split_method))

    # mini_batch

    return index
Esempio n. 5
0
def add_asyn_fluidc(graph, k, max_iter=100, seed=None):
    graph_communities = graph.copy().to_undirected(
    )  # asyn_fluidc algorithm only deals with undirected graphs
    communities_result = nx_community.asyn_fluidc(graph_communities, k,
                                                  max_iter, seed)
    _nx_community_data_to_graph(graph, communities_result)
    return graph
Esempio n. 6
0
def test_two_clique_communities():
    random.seed(7)
    test = Graph()

    # c1
    test.add_edge('a', 'b')
    test.add_edge('a', 'c')
    test.add_edge('b', 'c')

    # connection
    test.add_edge('c', 'd')

    # c2
    test.add_edge('d', 'e')
    test.add_edge('d', 'f')
    test.add_edge('f', 'e')

    # ground truth
    ground_truth = set(
        [frozenset(['a', 'c', 'b']),
         frozenset(['e', 'd', 'f'])])

    communities = asyn_fluidc(test, 2)
    result = {frozenset(c) for c in communities}
    assert_equal(result, ground_truth)
Esempio n. 7
0
def Asyn_fluidc_with_corrcoef(data):
    norm = (data-data.mean())/data.std()
    matrix = np.corrcoef(norm)
    matrix = matrix - np.diag(np.diag(matrix))

    matrix[matrix > 0 ] = 1
    matrix[matrix < 0 ] = 0

    graph = from_numpy_array(matrix)
    try:
        cluster = list(community.asyn_fluidc(graph, 2, max_iter=1000))
    except Exception as e:
        warn(str(e))
        return None

    if len(cluster) != 2:
        return None

    group = [None for _ in range(len(matrix))]
    for i in cluster[0]:
        group[i] = 0
    for i in cluster[1]:
        group[i] = 1

    return get_defective_cluster(data, group)
Esempio n. 8
0
def detect_communities(network: SpatioTemporalNetwork, algo, **kwargs):
    if algo == 'fluid':
        comm_iter = community.asyn_fluidc(network.to_multigraph().to_undirected(), **kwargs)
        return list(comm_iter)
    if algo == 'clm':
        comm_iter = community.greedy_modularity_communities(network.to_multigraph().to_undirected(), **kwargs)
        return list(comm_iter)
Esempio n. 9
0
def my_cdalgorithm(network):
        communities = []
        subnets = nx.connected_component_subgraphs(network)
        for subnet in subnets:
                coms_iter = nxcdalgorithm.asyn_fluidc(subnet, min([C, subnet.order()]), maxiter)
                for nodes in iter(coms_iter):
                       communities.append(list(nodes))
        return communities
Esempio n. 10
0
def async_fluid(G, k=4):
    """Runs the asynchronous fluid community detection algorithm
       G = Graph to look at
       k = number of communities to look for, default 4
    """
    bestcom = community.asyn_fluidc(G, k)

    return bestcom
Esempio n. 11
0
def get_communities(graph, modularity=False, fluid=False):
    k = len([key for key in graph.node.keys()]) / 10
    if modularity:
        return community.greedy_modularity_communities(graph)
    if fluid:
        return community.asyn_fluidc(graph, k)
    else:  # how work
        return community.girvan_newman(graph)
Esempio n. 12
0
def test_two_nodes():
    test = Graph()

    test.add_edge('a', 'b')

    # ground truth
    ground_truth = set([frozenset(['a']), frozenset(['b'])])

    communities = asyn_fluidc(test, 2)
    result = {frozenset(c) for c in communities}
    assert_equal(result, ground_truth)
Esempio n. 13
0
def test_single_node():
    test = Graph()

    test.add_node('a')

    # ground truth
    ground_truth = set([frozenset(['a'])])

    communities = asyn_fluidc(test, 1)
    result = {frozenset(c) for c in communities}
    assert_equal(result, ground_truth)
Esempio n. 14
0
def test_single_node():
    test = Graph()

    test.add_node('a')

    # ground truth
    ground_truth = set([frozenset(['a'])])

    communities = asyn_fluidc(test, 1)
    result = {frozenset(c) for c in communities}
    assert_equal(result, ground_truth)
Esempio n. 15
0
def test_two_nodes():
    test = Graph()

    test.add_edge('a', 'b')

    # ground truth
    ground_truth = set([frozenset(['a']), frozenset(['b'])])

    communities = asyn_fluidc(test, 2)
    result = {frozenset(c) for c in communities}
    assert_equal(result, ground_truth)
Esempio n. 16
0
def get_communities_fluid(G):
    connected_components = components.connected_components(G)
    modules = []
    min_size = 50
    coef = 1. / min_size
    for component in connected_components:
        if len(component) < min_size:
            modules = modules + [component]
            continue
        k = int(np.ceil(coef * len(G.node)))
        modules = modules + list(
            community.asyn_fluidc(G.subgraph(component), k, seed=123))
    return modules
Esempio n. 17
0
def fluidc_partition(G: nx.Graph,
                     num_communities: int) -> Tuple[Tuple[int, int], ...]:
    """
    Return the edges to remove in order to break G into communities.

    If the network has more components than num_communities, the empty tuple is
    immediately returned. If the network has multiple components, but fewer than
    num_communities, the function will make an informed decision about how many
    communities each component should have.
    """
    # list of all the components sorted by length with the largest coming first
    components = sorted((tuple(comp) for comp in nx.connected_components(G)),
                        key=len,
                        reverse=True)
    # It's possible that the network is already divided up
    if len(components) >= num_communities:
        return ()
    # handle the case where the network is unconnected
    if len(components) > 1:
        comp_to_num_communities = _calc_num_communities_per_component(
            components, num_communities)
        unflattened_edges_to_remove = (fluidc_partition(
            G.subgraph(comp),
            num_comms) for comp, num_comms in comp_to_num_communities.items())
        return tuple(it.chain(*unflattened_edges_to_remove))

    # handle the case where the network is connected
    # Contrary to the claims made in the paper, Fluid Communities is not
    # guaranteed to yield a certain number of communities. This loop makes sure
    # that the correct number gets returned. It usually runs quickly.
    n_connected_comps = np.nan
    iters = 0
    while n_connected_comps != num_communities:
        communities = tuple(asyn_fluidc(G, num_communities, seed=iters))
        id_to_community = dict(enumerate(communities))
        node_to_community = {
            node: comm_id
            for comm_id, community in id_to_community.items()
            for node in community
        }
        edges_to_remove = tuple(
            set((u, v) for u, v in G.edges
                if node_to_community[u] != node_to_community[v]))
        H = G.copy()
        H.remove_edges_from(edges_to_remove)
        n_connected_comps = nx.number_connected_components(H)
        iters += 1

    return edges_to_remove  # type: ignore
Esempio n. 18
0
def main(sc, file):
    start_time = time.time()
    userfile = sc.textFile(file)
    header = userfile.first()
    user_data = userfile.filter(lambda line: line != header).map(
        mapper).groupByKey().map(lambda x: [x[0], list(x[1])])
    user_list = (user_data.collect())
    edges = cal(user_list)
    G = nx.Graph()
    G.add_edges_from(edges)
    communities_generator = community.asyn_fluidc(G, 100)
    output = [sorted(i) for i in communities_generator]
    f = open('Ruotian_Jiang_Community_Bonus.txt', "w")
    f.write('\n'.join('[{}]'.format(str(x).replace('[', '').replace(']', ''))
                      for x in sorted(output)))
    f.close()
    print("Time: %s sec" % (time.time() - start_time))
Esempio n. 19
0
    def get_community(self, k):

        print("Finding communities using: {}".format(self.algorithm.upper()))

        communities: Dict[str, list] = dict()

        if self.algorithm.upper() == Algorithm.KERNIGHAN_LIN.value:
            partitions = community.kernighan_lin_bisection(self.graph)
            for p in range(len(partitions)):
                communities[str(p)] = list(partitions[p])

        elif self.algorithm.upper() == Algorithm.FLUIDC.value:
            partitions = community.asyn_fluidc(self.graph, k, seed=10)
            for p in range(k):
                communities[str(p)] = list(next(partitions))

        print("{} communities found".format(len(communities)))
        return communities
Esempio n. 20
0
def Asyn_fluidc_with_R_square(data):
    matrix = np.corrcoef(data)
    matrix = matrix ** 2
    matrix = matrix - np.diag(np.diag(matrix))

    graph = from_numpy_array(matrix)
    try:
        cluster = list(community.asyn_fluidc(graph, 2, max_iter=1000))
    except Exception as e:
        warn(str(e))
        return None

    if len(cluster) != 2:
        return None

    group = [None for _ in range(len(matrix))]
    for i in cluster[0]:
        group[i] = 0
    for i in cluster[1]:
        group[i] = 1

    return get_defective_cluster(data, group)
Esempio n. 21
0
def test_two_clique_communities():
    random.seed(7)
    test = Graph()

    # c1
    test.add_edge('a', 'b')
    test.add_edge('a', 'c')
    test.add_edge('b', 'c')

    # connection
    test.add_edge('c', 'd')

    # c2
    test.add_edge('d', 'e')
    test.add_edge('d', 'f')
    test.add_edge('f', 'e')

    # ground truth
    ground_truth = set([frozenset(['a', 'c', 'b']),
                        frozenset(['e', 'd', 'f'])])

    communities = asyn_fluidc(test, 2)
    result = {frozenset(c) for c in communities}
    assert_equal(result, ground_truth)
Esempio n. 22
0
    def get_communities(self, station_df):

        import networkx as nx
        import networkx.algorithms.community as nx_comm

        g_communities_ = []

        try:
            ''' sample the graph '''
            g_simple_ = self.get_simple_graph(station_df)

            if nx.is_empty(g_simple_):
                raise ValueError(
                    'A simple graph with %d stations was not created' %
                    station_df.shape[0])

            if self.name == 'ASYNC-LPA':  #asyn_lpa_communities
                g_communities_ = list(
                    nx_comm.asyn_lpa_communities(g_simple_,
                                                 weight=self.weight,
                                                 seed=self.seed))

            elif self.name == 'LPC':  #label_propagation_communities
                g_communities_ = list(
                    nx_comm.label_propagation_communities(g_simple_))

            elif self.name == 'GREEDY':  # greedy_modularity_communities
                g_communities_ = list(
                    nx_comm.greedy_modularity_communities(g_simple_))

            elif self.name == 'NAIVE-GREEDY':  #_naive_greedy_modularity_communities
                g_communities_ = list(
                    nx_comm._naive_greedy_modularity_communities(g_simple_))

            elif self.name == 'LUKES':  # lukes_partitioning
                # TODO: create MST of g_simple first but removing the mimum weigted edge doesn't seem right
                g_communities_ = list(
                    nx_comm.lukes_partitioning(
                        g_simple_,
                        edge_weight=self.weight,
                        max_size=self.maximum_node_weight))

            elif self.name == 'ASYNC-FLUID':  # asyn_fluidc
                # TODO: create complete graph for g_simple but a complete graph would not work
                g_communities_ = list(
                    nx_comm.asyn_fluidc(g_simple_,
                                        k=15,
                                        max_iter=300,
                                        seed=self.seed))

            elif self.name == 'GIRVAN-NEWMAN':  # girvan_newman
                #                g_communities_ = list(nx_comm.girvan_newman(g_simple_))
                #                tmp_communities = nx_comm.girvan_newman(g_simple_)
                #                g_communities_  = next(tmp_communities)
                g_communities_ = list(next(nx_comm.girvan_newman(g_simple_)))
#                print(list(g_communities_))

            else:
                raise AttributeError("something was not right")

#            g_simple_ = self.set_graph_cluster_labels(g_simple_, g_communities_)
            if isinstance(g_communities_, list):  #len(g_communities_)>0
                g_simple_ = self.set_graph_cluster_labels(
                    g_simple_, g_communities_)

#d            return g_simple_, g_communities_

        except Exception as err:
            print("Class community_detection [get_communities] Error message:",
                  err)

        return g_simple_, g_communities_
Esempio n. 23
0
N = len(g)
W = np.zeros((N, N))
for i in g:
    print(i, end="-> ")
    for j in nx.neighbors(g, i):
        print(j, end=" ")
        W[i][j] = 1
        W[j][i] = 1
    print()

# networkx community detection

gmc = list(greedy_modularity_communities(g))
alc = list(asyn_lpa_communities(g))
lpac = list(label_propagation_communities(g))
asfl = list(asyn_fluidc(g, 3))

# inititalization
anchorList = set([])
U = {}
U[N] = np.zeros(N)
randomFirstAnchor = random.randint(0, N - 1)
anchorList.add(randomFirstAnchor)
U[randomFirstAnchor] = np.zeros(N)
phi = 0.25
itermax = 15
K = 1
adjacentNodes = {}
for i in range(N):
    tmp = []
    for j in range(N):
Esempio n. 24
0
def five_clique_ring():
    """Not auto-tested (not named test_...) due to cross-version seed issues"""
    random.seed(9)
    test = Graph()

    # c1
    test.add_edge('1a', '1b')
    test.add_edge('1a', '1c')
    test.add_edge('1a', '1d')
    test.add_edge('1b', '1c')
    test.add_edge('1b', '1d')
    test.add_edge('1c', '1d')

    # c2
    test.add_edge('2a', '2b')
    test.add_edge('2a', '2c')
    test.add_edge('2a', '2d')
    test.add_edge('2b', '2c')
    test.add_edge('2b', '2d')
    test.add_edge('2c', '2d')

    # c3
    test.add_edge('3a', '3b')
    test.add_edge('3a', '3c')
    test.add_edge('3a', '3d')
    test.add_edge('3b', '3c')
    test.add_edge('3b', '3d')
    test.add_edge('3c', '3d')

    # c4
    test.add_edge('4a', '4b')
    test.add_edge('4a', '4c')
    test.add_edge('4a', '4d')
    test.add_edge('4b', '4c')
    test.add_edge('4b', '4d')
    test.add_edge('4c', '4d')

    # c5
    test.add_edge('5a', '5b')
    test.add_edge('5a', '5c')
    test.add_edge('5a', '5d')
    test.add_edge('5b', '5c')
    test.add_edge('5b', '5d')
    test.add_edge('5c', '5d')

    # connections
    test.add_edge('1a', '2c')
    test.add_edge('2a', '3c')
    test.add_edge('3a', '4c')
    test.add_edge('4a', '5c')
    test.add_edge('5a', '1c')

    # ground truth
    ground_truth = set([
        frozenset(['1a', '1b', '1c', '1d']),
        frozenset(['2a', '2b', '2c', '2d']),
        frozenset(['3a', '3b', '3c', '3d']),
        frozenset(['4a', '4b', '4c', '4d']),
        frozenset(['5a', '5b', '5c', '5d'])
    ])

    communities = asyn_fluidc(test, 5)
    result = {frozenset(c) for c in communities}
    assert_equal(result, ground_truth)
Esempio n. 25
0
# Find the modules in the graph
#%%Finds communities in a graph using the Girvan–Newman method
# This method generate modules (communities) in different levels
import networkx.algorithms.community as nxc

c = nxc.girvan_newman(G)
comPool = []
for item in c:
    comPool.append(tuple(sorted(item)))
print(len(comPool))
print(comPool[0])
print(comPool[49])
print(comPool[90])

#%% Returns communities in G as detected by Fluid Communities algorithm
c = nxc.asyn_fluidc(G, 4)  # 4 is the number of expected communities
for item in c:
    print(item)

#%% Greedy modularity
c = list(nxc.greedy_modularity_communities(G))
print(c)

#%%
# Find k-clique communities in graph using the percolation method.
# Definition of cliques: https://en.wikipedia.org/wiki/Clique_(graph_theory)
c = list(nxc.k_clique_communities(G, 3))
print(c)

#%% Louvain Community Detection
partition = community.best_partition(G)
Esempio n. 26
0
    def add_valves(self, wn, subG, n_sectors):
        '''
        Divide the network to sectors using Louvain-Algorithm, and add valves between sectors.
        '''
        G = wn.get_graph()
        G = nx.Graph(G)
        comm = community.asyn_fluidc(G, n_sectors)
        connected_sect = {}
        edges_bw_sect = {}
        sectors = {}
        k = 0
        while True:
            try:
                sectors['{}'.format(k)] = list(next(comm))
                k += 1
            except StopIteration:
                break

        self.sectors = sectors
        for sect1 in sectors:
            self._valves_per_sect[sect1] = []
            self._main_valves[sect1] = []
            edges_bw_sect[sect1] = []

        valves = []
        # Find connection between sectors (one for each pair)
        for (u, v) in list(G.edges()):
            for sect1 in sectors:
                if (u in sectors[sect1] and v not in sectors[sect1]) or (
                        v in sectors[sect1] and u not in sectors[sect1]):
                    edges_bw_sect[sect1].append((u, v))
                    for sect2 in sectors:
                        if sect2 != sect1:
                            if v in sectors[sect2]:
                                valves.append((u, v))
        # Add valves
        counter = 0
        pipes = []
        removed_pipes = []
        pipes_list = set(range(wn.num_links)) - set(self._problematic_pipes)
        for link in pipes_list:
            pipe = wn.get_link('{}'.format(link))
            node1 = pipe.start_node_name
            if node1.startswith('R'): no1 = node1
            else: no1 = int(node1)
            node2 = pipe.end_node_name
            if node2.startswith('R'): no2 = node2
            else: no2 = int(node2)
            diameter = pipe.diameter
            if (node1, node2) in valves:
                n1, n2 = None, None
                for sect in sectors:
                    if (node1, node2) in edges_bw_sect[sect] and (n1, n2) != (
                            node1, node2):
                        n1, n2 = node1, node2
                        removed_pipes.append('{}'.format(link))
                        wn.add_valve('S{}_{}'.format(sect, counter), node1,
                                     node2, diameter, 'TCV', 0)
                        if subG.has_edge(no1, no2) or subG.has_edge(no2, no1):
                            self._main_valves[sect].append('S{}_{}'.format(
                                sect, counter))
                        else:
                            self._valves_per_sect[sect].append('S{}_{}'.format(
                                sect, counter))
            counter += 1
        # Connect the valves to the closest node in the main distribution network
        min_dist = self._distance
        edges_list = []
        for sect in sectors:
            if len(self._main_valves[sect]) < 1:
                edge_sect = random.choice(edges_bw_sect[sect])
                node_sect = edge_sect[1]
                xr = G.nodes[node_sect]['pos'][0]
                yr = G.nodes[node_sect]['pos'][1]
                closest = list(subG.nodes())[0]
                for n in subG.nodes():
                    xn = subG.nodes[n]['x']
                    yn = subG.nodes[n]['y']
                    dist = self.proj_distance(yn, xn, yr, xr)
                    if dist < min_dist:
                        min_dist = dist
                        closest = n
                path = nx.algorithms.shortest_path(G, node_sect,
                                                   '{}'.format(closest),
                                                   'length')
                edges = [edge_sect]
                edges += [(path[j], path[j + 1]) for j in range(len(path) - 1)]
                edges_list += edges
        # Make the diameters of pipes of the connection to the main d=600mm
        for pipe_name, pipe in wn.pipes():
            node1 = pipe.start_node_name
            node2 = pipe.end_node_name
            if (node1, node2) in edges_list or (node2, node1) in edges_list:
                pipe.diameter = 0.6
        # save the lists of main pipes(control pipes) and pipes between sectors (isolation pipes)
        for sect in self._valves_per_sect:
            for valve_name in self._valves_per_sect[sect]:
                valve = wn.get_link(valve_name)
                node1 = valve.start_node_name
                node2 = valve.end_node_name
                if (node1, node2) in edges_list or (node2,
                                                    node1) in edges_list:
                    self._valves_per_sect[sect].remove(valve_name)
                    self._main_valves[sect].append(valve_name)
        # Remove the pipes parallel to the valves to avoid redundancy
        for pipe in removed_pipes:
            wn.remove_link(pipe)
        return wn
        G = nx.Graph()  # undirected graph
        for line in train:
            neighbour_list = [int(i) for i in line.split()]
            neighbour_tuples = [(neighbour_list[0], neighbour_list[i + 1])
                                for i in range(len(neighbour_list) - 1)]
            G.add_edges_from(neighbour_tuples)
        return G


print("Generating undirected graph......")
UG = get_undirected_graph('train.txt')

print("Generating community......")
from networkx.algorithms import community

comms = list(community.asyn_fluidc(UG, 100))
print("Size of communities:" + str(len(comms)))

print("Adding community attribute......")
count = 0
for node in UG.nodes():
    if (count % 10000 == 0):
        print(count)
    count += 1
    for i in range(len(comms)):
        if node in comms[i]:
            UG.nodes[node]['community'] = i


def generate_positive_features():
    features = []
Esempio n. 28
0
def five_clique_ring():
    """Not auto-tested (not named test_...) due to cross-version seed issues"""
    random.seed(9)
    test = Graph()

    # c1
    test.add_edge('1a', '1b')
    test.add_edge('1a', '1c')
    test.add_edge('1a', '1d')
    test.add_edge('1b', '1c')
    test.add_edge('1b', '1d')
    test.add_edge('1c', '1d')

    # c2
    test.add_edge('2a', '2b')
    test.add_edge('2a', '2c')
    test.add_edge('2a', '2d')
    test.add_edge('2b', '2c')
    test.add_edge('2b', '2d')
    test.add_edge('2c', '2d')

    # c3
    test.add_edge('3a', '3b')
    test.add_edge('3a', '3c')
    test.add_edge('3a', '3d')
    test.add_edge('3b', '3c')
    test.add_edge('3b', '3d')
    test.add_edge('3c', '3d')

    # c4
    test.add_edge('4a', '4b')
    test.add_edge('4a', '4c')
    test.add_edge('4a', '4d')
    test.add_edge('4b', '4c')
    test.add_edge('4b', '4d')
    test.add_edge('4c', '4d')

    # c5
    test.add_edge('5a', '5b')
    test.add_edge('5a', '5c')
    test.add_edge('5a', '5d')
    test.add_edge('5b', '5c')
    test.add_edge('5b', '5d')
    test.add_edge('5c', '5d')

    # connections
    test.add_edge('1a', '2c')
    test.add_edge('2a', '3c')
    test.add_edge('3a', '4c')
    test.add_edge('4a', '5c')
    test.add_edge('5a', '1c')

    # ground truth
    ground_truth = set([frozenset(['1a', '1b', '1c', '1d']),
                        frozenset(['2a', '2b', '2c', '2d']),
                        frozenset(['3a', '3b', '3c', '3d']),
                        frozenset(['4a', '4b', '4c', '4d']),
                        frozenset(['5a', '5b', '5c', '5d'])])

    communities = asyn_fluidc(test, 5)
    result = {frozenset(c) for c in communities}
    assert_equal(result, ground_truth)
Esempio n. 29
0
import networkx as nx
import numpy as np
from networkx.algorithms.community import asyn_fluidc
from networkx.algorithms.community.label_propagation import asyn_lpa_communities

# ---------------------------------- Part b ---------------------------------- #

G = nx.read_graphml("net.graphml")

communities = asyn_fluidc(G, 5)

# i = 0
# for community in communities:
#     f = open("community"+str(i)+".txt","w+")
#     group = ""
#     for node in community:
#       group+=node+"\n"
#     f.write(group)
#     f.close()
#     i+=1

# ---------------------------------- Part c --------------------------------- #

communitiesNetworkx = asyn_lpa_communities(G)

cGephi = []
for community in communities:
    a = [int(i) for i in community]
    a.sort(key=int)
    a = np.array(a)
    cGephi.append(a)
Esempio n. 30
0
# Graph=nx.karate_club_graph()
# Graph=nx.read_gml('datasets/dolphins.gml',label='id')
Graph = nx.fast_gnp_random_graph(25, 0.7)
# Graph=nx.windmill_graph(8,4)

# sizes = [5, 5, 10]
# probs = [[0.05, 0.05, 0.02],
#          [0.05, 0.15, 0.07],
#          [0.02, 0.07, 0.04]]
# Graph = nx.stochastic_block_model(sizes, probs, seed=0)

gmc = list(greedy_modularity_communities(Graph))
alc = list(asyn_lpa_communities(Graph))
lpac = list(label_propagation_communities(Graph))
asfl = list(asyn_fluidc(Graph, 3))
girvanNewmanCommunities = list(girvan_newman(Graph))
W2 = np.zeros((len(Graph), len(Graph)))
for i in Graph:
    for j in nx.neighbors(Graph, i):
        W2[i][j] = 1
        W2[j][i] = 1
for i in range(len(Graph)):
    print(i, end="->")
    for j in range(len(Graph)):
        if (W2[i][j] == 1):
            print(j, end=" ")
    print()
W = W2

# threshold value for acquiring membership of a particular community
Esempio n. 31
0
 def add_valves(self, wn, subG, n_sectors):
     '''
     Divide the network to sectors using Louvain-Algorithm, and add valves between sectors. If plot_sect = 'True' it also plots the network with different colors for the sectors
     '''
     G = wn.get_graph()
     G = nx.Graph(G)
     comm = community.asyn_fluidc(G, n_sectors)
     connected_sect = {}
     edges_bw_sect = {}
     sectors = {}
     k=0
     while True:
         try:
             sectors['{}'.format(k)] = list(next(comm))
             k+=1
         except StopIteration:
             break
     
     self.sectors = sectors
     # Visualize the created communities
     color = cm.rainbow(np.linspace(0,1,n_sectors))
     nc = []        
     for n in G.nodes():
         for k in sectors:
             if n in sectors[k]:
                 nc.append(color[int(k)])
     for sect1 in sectors:
         self.valves_per_sect[sect1] = []
         self.main_valves[sect1] = []
         edges_bw_sect[sect1] = []
         for sect2 in sectors:
             if sect1 != sect2:
                 connected_sect[sect1+'_'+sect2] = []
     valves = []
     # Find connection between sectors (one for each pair)
     for (u,v) in list(G.edges()):
         for sect1 in sectors:
             if (u in sectors[sect1] and v not in sectors[sect1]) or (v in sectors[sect1] and u not in sectors[sect1]):
                 edges_bw_sect[sect1].append((u,v))
                 for sect2 in sectors:
                     if sect2!=sect1:
                         if v in sectors[sect2]:
                             connected_sect[sect1+'_'+sect2].append(1)
                             valves.append((u,v))
     counter = 0
     pipes = []
     pipes_list = set(range(wn.num_links))-set(self.problematic_pipes)
     for link in pipes_list:
         pipe = wn.get_link('{}'.format(link))
         node1 = pipe.start_node_name
         if node1.startswith('R'): no1 = node1
         else: no1 = int(node1)
         node2 = pipe.end_node_name
         if node2.startswith('R'): no2 = node2
         else: no2 = int(node2)
         diameter = pipe.diameter         
         if (node1, node2) in valves:
             for sect in sectors:
                 if (node1, node2) in edges_bw_sect[sect]:
                     wn.add_valve('S{}_{}'.format(sect, counter), node1, node2, diameter, 'TCV', 0)
                     if subG.has_edge(no1, no2) or subG.has_edge(no2, no1):
                         self.main_valves[sect].append('S{}_{}'.format(sect, counter))
                     else:
                         self.valves_per_sect[sect].append('S{}_{}'.format(sect, counter))
                 elif (node2, node1) in edges_bw_sect[sect]:
                     wn.add_valve('S{}_{}'.format(sect, counter), node1, node2, diameter, 'TCV', 0)
                     if subG.has_edge(no1, no2) or subG.has_edge(no2, no1):
                         self.main_valves[sect].append('S{}_{}'.format(sect, counter))
                     else:
                         self.valves_per_sect[sect].append('S{}_{}'.format(sect, counter))
         counter += 1
     min_dist = self.distance
     edges_list = []
     for sect in sectors:
         if len(self.main_valves[sect]) < 1:
             edge_sect = random.choice(edges_bw_sect[sect])
             node_sect = edge_sect[1]
             xr = G.nodes[node_sect]['pos'][0]
             yr = G.nodes[node_sect]['pos'][1]
             closest = list(subG.nodes())[0]
             for n in subG.nodes():
                 xn = subG.nodes[n]['x']
                 yn = subG.nodes[n]['y']
                 dist = self.proj_distance(yn, xn, yr, xr)
                 if dist < min_dist:
                     min_dist = dist
                     closest = n
             path = nx.algorithms.shortest_path(G, node_sect, '{}'.format(closest),'length')
             edges = [edge_sect]
             edges += [(path[j], path[j+1]) for j in range(len(path)-1)]
             edges_list += edges
     for pipe_name, pipe in wn.pipes():
         node1 = pipe.start_node_name
         node2 = pipe.end_node_name
         if (node1, node2) in edges_list or (node2, node1) in edges_list:
             pipe.diameter = 0.6
     for sect in self.valves_per_sect:
         for valve_name in self.valves_per_sect[sect]:
             valve = wn.get_link(valve_name)
             node1 = valve.start_node_name
             node2 = valve.end_node_name
             if (node1, node2) in edges_list or (node2, node1) in edges_list:
                 self.valves_per_sect[sect].remove(valve_name)
                 self.main_valves[sect].append(valve_name)                 
     return wn
Esempio n. 32
0
	def community_using_async(self, *args):
		G = self.generate_graph(args[0])
		communities = community.asyn_fluidc(G,k=int(args[1]),max_iter=int(args[2]))
		return communities
Esempio n. 33
0
 def add_reservoirs(self, G):
     '''
     Adds number_of_reservoirs reservoirs to the graph G
     '''
     
     G = self.main_connected(G)
     n_edges = G.number_of_edges()
     self.number_of_reservoirs = min(round(n_edges/100) +2, round(self.distance/500) +2)        
     min_points = int(len(G)/self.number_of_reservoirs)
     zones = self.zonage(G, min_npoints=min_points, Reservoirs=True)
     # print(zones)
     reservoir_nodes =[]
     i = 0
     for zone in zones:
         edges_list = []
         # Make a subgraph from the community nodes
         points = self.find_points(self.graph, zones[zone])
         if len(points) > 0:
             highest_node = points[0]
             highest = self.graph.nodes[highest_node]['elevation']
             for point in points:
                 if self.graph.nodes[point]['elevation'] > highest : 
                     highest = self.graph.nodes[point]['elevation']
                     highest_node = point
             print(highest)
             if len(zones[zone]['points']) > 0:
                 closest = zones[zone]['points'][0]
                 dist = 2*self.distance
                 for point in zones[zone]['points']:
                     new_dist = self.proj_distance(self.graph.nodes[point]['y'], self.graph.nodes[point]['x'], self.graph.nodes[highest_node]['y'], self.graph.nodes[highest_node]['x']) 
                     if new_dist < dist: 
                         dist = new_dist
                         closest = point 
                 path = nx.shortest_path(self.graph, highest_node, closest)
                 edges = [(path[j], path[j+1]) for j in range(len(path)-1)]
                 edges_list.append(edges)
                 edges_list = [u for v in edges_list for u in v]
                 print(edges_list)
                 connexion = self.graph.edge_subgraph(edges_list).copy()
                 subG = G.subgraph(zones[zone]['points'])
                 subG = nx.compose(subG, connexion)
                 # Get the maximum elevation node from the subgraph
                 # if len(subG.nodes()) > 0:
                 G =  nx.compose(G, connexion)
                 max_elevation_node = max(dict(subG.nodes).items(), key=lambda x: x[1]['elevation'])[0]
                 Reservoir_attr = {'x': G.nodes[max_elevation_node]['x']+ random.randint(10, 30), 'y': G.nodes[max_elevation_node]['y'] + random.randint(10, 30), 'elevation': random.choice(self.reservoir_heads) + highest, 'demand': 0}
                 G.add_node('Reservoir{}' .format(i), **Reservoir_attr)
                 G.add_edge('Reservoir{}' .format(i), max_elevation_node)
                 G['Reservoir{}'.format(i)][max_elevation_node]['length'] = 50
                 G['Reservoir{}'.format(i)][max_elevation_node]['roughness'] = random.choice(self.roughness_values)
                 i+=1
     if i != 0 :
         self.number_of_reservoirs = i
         return G
     else:
         n_communities = self.number_of_reservoirs
         community_generator = community.asyn_fluidc(G, n_communities)
         for i in range(n_communities):
             # Make a subgraph from the community nodes
             subG = G.subgraph(next(community_generator))
             # Get the maximum elevation node from the subgraph
             max_elevation_node = max(dict(subG.nodes).items(), key=lambda x: x[1]['elevation'])[0]
             Reservoir_attr = {'x': G.nodes[max_elevation_node]['x']+ random.randint(10, 30), 'y': G.nodes[max_elevation_node]['y'] + random.randint(10, 30), 'elevation': random.choice(self.reservoir_heads) + G.nodes[max_elevation_node]['elevation'], 'demand': 0}
             G.add_node('Reservoir{}' .format(i), **Reservoir_attr)
             G.add_edge('Reservoir{}' .format(i), max_elevation_node)
             G['Reservoir{}'.format(i)][max_elevation_node]['length'] = 50
             G['Reservoir{}'.format(i)][max_elevation_node]['roughness'] = random.choice(self.roughness_values)
         return G
Esempio n. 34
0
dt_values_g = list(dt_g.values())
ground_labels = [0 if x == 'Mr. Hi' else 2 for x in dt_values_g]
pos = nx.spring_layout(G)

#Plot ground truth partition
plt.figure(1)
plt.title("Ground truth partition")
nx.draw(G,
        pos,
        node_color=ground_labels,
        cmap=plt.cm.get_cmap('rainbow'),
        with_labels=True,
        font_color='white')

#Set up Fluid partition
partition = list(nx_comm.asyn_fluidc(G, k=2, seed=2))
node_list = sorted(list(partition[0]) + list(partition[1]))
fluid_labels = [0 if node in list(partition[0]) else 2 for node in node_list]

#Calculate NMI
nmi = normalized_mutual_info_score(ground_labels, fluid_labels)

#Plot Fluid partition
plt.figure(2)
plt.title("Fluid partition, NMI = {}".format(nmi))
nx.draw(G,
        pos,
        nodelist=node_list,
        node_color=fluid_labels,
        cmap=plt.cm.get_cmap('rainbow'),
        with_labels=True,