Source code for communities_utils

"""This module allows the creating networks of communities and the interactions of each community
with users from another community
"""

import networkx as nx


[docs]def connections(g, node, list_communities, own_community): """ Given a graph a node, a list of communities and the community to which that node belongs it creates the connections of each community with each other. :param g: The general graph. :param node: The node that is being analysed. :param list_communities: A list of lists with each individual list containing the users that belong to that community. :param own_community: The number of the community to which the node belongs :return: The edges among the communities. """ edges = [] list_edges_node = g.edges(node) for c in range(0, len(list_communities)): if c != own_community: for element in list_communities[c]: for e in list_edges_node: if e[1] == element: edges.append((own_community, c)) return edges
[docs]def get_communities_representative_graph(g, l_communities): """ Given a general graph and a list of communities, it creates a graph where each node is each communitie. :param g: The general graph. :param l_communities: A list of lists with each individual list containing the users that belong to that community. :return: A graph where each node is a community. """ n_graph = nx.Graph() nodes = list(range(0, len(l_communities))) edges = [] #for each community for i in range(0, len(l_communities)): print("COMMUNITY", i) print(l_communities[i]) #for each element in community for e in l_communities[i]: edges = connections(g, e, l_communities, i) print("----------- ARE THERE EDGES --------------") print(edges) if len(edges) > 0: n_graph.add_edges_from(edges) return n_graph