Ejemplo n.º 1
0
 def setUp(self):
     self.G = nx.path_graph(9)
     self.DG = nx.path_graph(9, create_using=nx.DiGraph())
     self.Gv = nx.to_undirected(self.DG)
     self.MG = nx.path_graph(9, create_using=nx.MultiGraph())
     self.DMG = nx.path_graph(9, create_using=nx.MultiDiGraph())
     self.MGv = nx.to_undirected(self.DMG)
 def _generate(self):
     if self.ALWAYS_SAME:
         np.random.seed(1)  # to make all random values same
     # skills of every person: every person has a list of s elements describing his/her skill values fall in [0-1].
     if self.DISTRIBUTION == 'Uniform':
         self.skills = np.random.rand(self.n, self.s)
     elif self.DISTRIBUTION == 'Normal':
         self.skills = np.random.randn(self.n, self.s) * 0.1666 + 0.5
     else:
         raise ValueError('ERROR in DISTRIBUTION value: ' % self.DISTRIBUTION)
     # risk taking of every person: it is also a value between [0,1].
     if self.DISTRIBUTION == 'Uniform':
         self.risk_takings = np.random.rand(self.n)
     elif self.DISTRIBUTION == 'Normal':
         self.risk_takings = np.random.randn(self.n) * 0.1666 + 0.5
     # network connectivity (or distance) is a matrix of n*n: we always can for any
     #   graph, compute the shortest path among all nodes
     #   and normalize it between [0,1]. We assume, relationships are not directed.
     if self.ALWAYS_SAME:
         # it keeps the same numbers which is helpful for debugging purposes
         G = nx.to_undirected(nx.scale_free_graph(self.n, seed=1))
     else:
         # it changes the structure every time we run
         G = nx.to_undirected(nx.scale_free_graph(self.n))
     D = nx.floyd_warshall_numpy(G)
     D /= np.max(D)
     self.network_connectivity = D
Ejemplo n.º 3
0
 def setup_class(cls):
     cls.G = nx.path_graph(9)
     cls.DG = nx.path_graph(9, create_using=nx.DiGraph())
     cls.MG = nx.path_graph(9, create_using=nx.MultiGraph())
     cls.MDG = nx.path_graph(9, create_using=nx.MultiDiGraph())
     cls.Gv = nx.to_undirected(cls.DG)
     cls.DGv = nx.to_directed(cls.G)
     cls.MGv = nx.to_undirected(cls.MDG)
     cls.MDGv = nx.to_directed(cls.MG)
     cls.Rv = cls.DG.reverse()
     cls.MRv = cls.MDG.reverse()
     cls.graphs = [
         cls.G,
         cls.DG,
         cls.MG,
         cls.MDG,
         cls.Gv,
         cls.DGv,
         cls.MGv,
         cls.MDGv,
         cls.Rv,
         cls.MRv,
     ]
     for G in cls.graphs:
         G.edges, G.nodes, G.degree
Ejemplo n.º 4
0
    def dist(self, G1, G2):
        """A scalable approach to network similarity.

        A network similarity measure based on node signature distrubtions.

        The results dictionary includes the underlying feature matrices in
        `'feature_matrices'` and the underlying signature vectors in
        `'signature_vectors'`.

        Params
        ------

        G1, G2 (nx.Graph): two undirected networkx graphs to be compared.

        Returns
        -------

        dist (float): the distance between G1 and G2.

        """

        # NOTE: the measure only works for undirected
        # graphs. For now we will silently convert a
        # directed graph to be undirected.
        directed_flag = False
        if nx.is_directed(G1):
            G1 = nx.to_undirected(G1)
            directed_flag = True
        if nx.is_directed(G2):
            G2 = nx.to_undirected(G2)
            directed_flag = True

        if directed_flag:
            warnings.warn("Coercing directed graph to undirected.",
                          RuntimeWarning)

        # find the graph node feature matrices
        G1_node_features = feature_extraction(G1)
        G2_node_features = feature_extraction(G2)

        # get the graph signature vectors
        G1_signature = graph_signature(G1_node_features)
        G2_signature = graph_signature(G2_node_features)

        # the final distance is the absolute canberra distance
        dist = abs(canberra(G1_signature, G2_signature))

        self.results['feature_matrices'] = G1_node_features, G2_node_features
        self.results['signature_vectors'] = G1_signature, G2_signature
        self.results['dist'] = dist

        return dist
Ejemplo n.º 5
0
 def setUp(self):
     self.G = nx.path_graph(9)
     self.DG = nx.path_graph(9, create_using=nx.DiGraph())
     self.MG = nx.path_graph(9, create_using=nx.MultiGraph())
     self.MDG = nx.path_graph(9, create_using=nx.MultiDiGraph())
     self.Gv = nx.to_undirected(self.DG)
     self.DGv = nx.to_directed(self.G)
     self.MGv = nx.to_undirected(self.MDG)
     self.MDGv = nx.to_directed(self.MG)
     self.Rv = self.DG.reverse()
     self.MRv = self.MDG.reverse()
     self.graphs = [self.G, self.DG, self.MG, self.MDG,
                    self.Gv, self.DGv, self.MGv, self.MDGv,
                    self.Rv, self.MRv]
     for G in self.graphs:
         G.edges, G.nodes, G.degree
Ejemplo n.º 6
0
def NeighborhoodConnectivity(G):
    H = nx.to_undirected(G).copy()  # convert to undirected
    R = list(nx.selfloop_edges(H))
    for (u, v) in R:
        H.remove_edge(u, v)  # remove self-loop edges
    conn = {v: len(H.adj[v]) for v in H}  # connectivity (=number of neighbors)
    return {v: sum(conn[n] / conn[v] for n in H.neighbors(v)) for v in G}
Ejemplo n.º 7
0
def nx_to_dgl_jacques(graph, edge_map):
    """
        Returns one training item at index `idx`.
    """
    #adding the self edges
    # graph.add_edges_from([(n, n, {'label': 'X'}) for n in graph.nodes()])
    graph = nx.to_undirected(graph)
    one_hot = {
        edge: torch.tensor(edge_map[label])
        for edge, label in (nx.get_edge_attributes(graph, 'label')).items()
    }
    nx.set_edge_attributes(graph, name='one_hot', values=one_hot)

    g_dgl = dgl.DGLGraph()
    # g_dgl.from_networkx(nx_graph=graph, edge_attrs=['one_hot'], node_attrs=['one_hot'])
    g_dgl.from_networkx(nx_graph=graph,
                        edge_attrs=['one_hot'],
                        node_attrs=['angles', 'identity'])

    #JACQUES
    # Init node embeddings with nodes features
    floatid = g_dgl.ndata['identity'].float()
    g_dgl.ndata['h'] = torch.cat([g_dgl.ndata['angles'], floatid], dim=1)

    return graph, g_dgl
Ejemplo n.º 8
0
def nx_to_dgl(graph, edge_map, nucs=False):
    #adding the self edges
    # graph.add_edges_from([(n, n, {'label': 'X'}) for n in graph.nodes()])
    graph = nx.to_undirected(graph)
    one_hot = {
        edge: torch.tensor(edge_map[label])
        for edge, label in (nx.get_edge_attributes(graph, 'label')).items()
    }
    nx.set_edge_attributes(graph, name='one_hot', values=one_hot)

    node_attrs = None
    if nucs:
        nuc_map = {n: i for i, n in enumerate(['A', 'C', 'G', 'N', 'U'])}
        one_hot_nucs = {
            node: torch.tensor(nuc_map[label], dtype=torch.float32)
            for node, label in (nx.get_node_attributes(graph, 'nt')).items()
        }
    else:
        one_hot_nucs = {
            node: torch.tensor(0, dtype=torch.float32)
            for node, label in (nx.get_node_attributes(graph, 'nt')).items()
        }

    nx.set_node_attributes(graph, name='one_hot', values=one_hot_nucs)

    g_dgl = dgl.DGLGraph()
    g_dgl.from_networkx(nx_graph=graph,
                        edge_attrs=['one_hot'],
                        node_attrs=['one_hot'])

    return graph, g_dgl
Ejemplo n.º 9
0
    def __init__(self, gexf_file, partition_resolution=0.3):
        # load network
        self.graph = nx.read_gexf(gexf_file)
        self.partition_resolution = partition_resolution
        # load all_paths_length
        self.all_paths_path = "bin/all_paths.bin"
        self.graph_copy = self.graph
        if os.path.exists(self.all_paths_path):
            self.all_paths = pickle.load(open(self.all_paths_path, mode='rb'))
        else:
            os.makedirs("bin")
            for edge in self.graph_copy.edges:
                self.graph_copy[edge[0]][edge[1]]['weight'] = 1
            self.all_paths = dict(
                nx.all_pairs_shortest_path_length(self.graph_copy))
            pickle.dump(self.all_paths, open(self.all_paths_path, mode='wb'))

        # load partition
        self.partition_path = "bin/partition_per{}.bin".format(
            partition_resolution)
        self.graph_copy = self.graph
        if os.path.exists(self.partition_path):
            self.partition = pickle.load(open(self.partition_path, mode='rb'))
        else:
            for edge in self.graph_copy.edges:
                weight = self.graph_copy[edge[0]][edge[1]]['weight']
                self.graph_copy[edge[0]][edge[1]]['weight'] = 1 / weight
            self.graph_copy = nx.to_undirected(self.graph_copy)
            self.partition = community.best_partition(
                self.graph_copy, resolution=partition_resolution)
            pickle.dump(self.partition, open(self.partition_path, mode='wb'))
Ejemplo n.º 10
0
    def is_connected(self):
        """
        Checks if the database graph is connected.

        :return: True if the graph is connected, False otherwise
        """
        return nx.is_connected(nx.to_undirected(self.graph))
Ejemplo n.º 11
0
    def extract_adjencecacy(self,
                            graph=None,
                            structure="sparse",
                            direction="undirected"):
        """
        This function returns the adjancecy matrix of a graph\n
        
        @param:\n
            graph {graph}:  graph containing the nodes and egdes either in numpy, scipy or networkx format\n
        
        @param:\n
            structure {str}:  flag defining the kind of output graph (default: {"sparse"})\n
            direction {str}:  flag defining type of graph (default: {"undirected"})\n
        \n
        @return:\n
            [numpy/scipy graph]: type of output
        """
        if graph is None:
            graph = self.Graph

        if direction == "undirected":
            graph_un = nx.to_undirected(graph)
            if structure == "sparse":
                return nx.convert_matrix.to_scipy_sparse_matrix(graph_un)
            else:
                return nx.convert_matrix.to_numpy_matrix(graph_un)
        else:
            if structure == "sparse":
                return nx.convert_matrix.to_scipy_sparse_matrix(graph)
            else:
                return nx.convert_matrix.to_numpy_matrix(graph)
Ejemplo n.º 12
0
def AdamicAdarIndex(g, edge):  
    
    if g is None or edge is None:
        return
  
    g_undirect = nx.to_undirected(g)
    
    source = edge[0]
    dest = edge[1]
    common = nx.common_neighbors(g_undirect, source, dest)
    
    index = 0.0
    number_of_neighbors = 0
    
    #Adamic-Adar:
    for neigh in common:
        index += 1/math.log(g_undirect.degree(neigh), 10)
        number_of_neighbors += 1
    
    #Maximum Adamic-Adar:
    max_adamic_adar = (1/math.log(2,10))*number_of_neighbors
    
    normalized_adamic_adar = (float(index)/float(max_adamic_adar)) if max_adamic_adar != 0 else 0
    
    return normalized_adamic_adar
Ejemplo n.º 13
0
def _complete_graph_constrain(nNode, in_nodes, out_nodes):
    G = nx.complete_graph(nNode)
    rand_seq = np.arange(0, len(G))
    for n, r in zip(G.nodes, rand_seq):
        G.nodes[n]['rank'] = r

    G = nx.to_undirected(G)
    DAG = nx.DiGraph()
    for n in G.nodes:
        DAG.add_node(n, rank=G.nodes[n]['rank'])

    for n in G.nodes:
        for m in G.neighbors(n):
            if n == 0:
                if m <= in_nodes:
                    DAG.add_edge(n, m)
            elif G.nodes[m]['rank'] > G.nodes[n]['rank'] and m != nNode - 1:
                if n < nNode - 1 - out_nodes:
                    if n <= in_nodes:
                        if m > in_nodes:
                            DAG.add_edge(n, m)
                    else:
                        DAG.add_edge(n, m)
            elif m == nNode - 1:
                if n >= nNode - 1 - out_nodes:
                    DAG.add_edge(n, m)
    return DAG
Ejemplo n.º 14
0
    def number_connected_components(self):
        """
        Finds the number of connected components of the database graph.

        :return: The number connected components of the database graph
        """
        return nx.number_connected_components(nx.to_undirected(self.graph))
Ejemplo n.º 15
0
def plot_3d_matching(tiling, filename="outputs/3dmatch.html"):
    visualize_graph_3d(nx.to_undirected(tiling),
                       tiling.nodes(), [],
                       filename,
                       title="3d",
                       coords={v: v
                               for v in tiling.nodes()})
Ejemplo n.º 16
0
def inference_on_graph(model,
                       graph,
                       edge_map=default_edge_map,
                       device='cpu',
                       nc_only=False):
    """
        Do inference on one networkx graph.
    """
    graph = nx.to_undirected(graph)
    one_hot = {
        edge: torch.tensor(edge_map[label])
        for edge, label in (nx.get_edge_attributes(graph, 'label')).items()
    }
    nx.set_edge_attributes(graph, name='one_hot', values=one_hot)

    g_dgl = dgl.DGLGraph()
    g_dgl.from_networkx(nx_graph=graph, edge_attrs=['one_hot'])
    g_dgl = send_graph_to_device(g_dgl, device)
    model = model.to(device)
    with torch.no_grad():
        embs = model(g_dgl)
        embs.cpu().numpy()
    g_nodes = list(sorted(graph.nodes()))

    keep_indices = range(len(graph.nodes()))

    if nc_only:
        keep_indices = get_nc_nodes_index(graph)

    node_map = {
        g_nodes[node_ind]: i
        for i, node_ind in enumerate(keep_indices)
    }
    embs = embs[keep_indices]
    return embs, node_map
def write_distance_info(G, report_file):
    report_file.write("===DISTANCE_INFO_STRONGLY_CONNECTED===\n")
    if nx.is_strongly_connected(G):
        report_file.write("Center: {}\n".format(nx.center(G)))
        report_file.write("Diameter: {}\n".format(nx.diameter(G)))
        report_file.write("Periphery: {}\n".format(nx.periphery(G)))
        report_file.write("Radius: {}\n".format(nx.radius(G)))
    else:
        report_file.write("Center: +INF\n")
        report_file.write("Diameter: +INF\n")
        report_file.write("Periphery: +INF\n")
        report_file.write("Radius: +INF\n")

    report_file.write("===DISTANCE_INFO_WEAKLY_CONNECTED===\n")
    if nx.is_weakly_connected(G):
        undirected_G = nx.to_undirected(G)
        report_file.write("Center: {}\n".format(nx.center(undirected_G)))
        report_file.write("Diameter: {}\n".format(nx.diameter(undirected_G)))
        report_file.write("Periphery: {}\n".format(nx.periphery(undirected_G)))
        report_file.write("Radius: {}\n".format(nx.radius(undirected_G)))
    else:
        report_file.write("Center: +INF\n")
        report_file.write("Diameter: +INF\n")
        report_file.write("Periphery: +INF\n")
        report_file.write("Radius: +INF\n")
Ejemplo n.º 18
0
    def __init__(self, G, precom):
        self.G = G
        self.undir_G = nx.to_undirected(G)
        
        self.pr = precom["pr"]
        self.mean_pr = precom["mean_pr"]
        
        self.katz = precom["katz"]
        self.katz_2 = precom["katz_2"]
        self.mean_katz_2 = precom["mean_katz_2"]

        self.hits = precom["hits"]
        
        #weight for source and destination of each link
        self.Weight_in = precom["Weight_in"]
        self.Weight_out = precom["Weight_out"]

        #for imputing with mean
        self.mean_weight_in = precom["mean_weight_in"]
        self.mean_weight_out = precom["mean_weight_out"]

        self.svd = precom['svd']
        self.node2id = precom['node2id']

        self.A_er = precom["A_er"]
Ejemplo n.º 19
0
def get_number_of_intransitive_users(conn, filters):
    with conn.cursor() as cursor:
        cursor.execute("""SELECT voters.id
               FROM voters
               WHERE 1 = 1 
               AND voters.creation_time >= make_date(2019, 4, 3)
               AND voters.creation_time <= make_date(2019, 4, 22) """ +
                       filter_statement(conn, filters))
        voters = [row[0] for row in cursor]

    count_intransitive = 0
    count_opportunity_intransitive = 0
    voters_with_no_votes = 0
    for voter in voters:
        filters["voter"] = voter
        if not get_votes(conn, filters):
            voters_with_no_votes += 1
            continue
        vg = get_victory_graph(conn, filters)
        try:
            nx.find_cycle(nx.to_undirected(vg))
            count_opportunity_intransitive += 1
        except:
            pass

        try:
            nx.find_cycle(vg)
            count_intransitive += 1
        except:
            pass

    return count_intransitive, count_opportunity_intransitive, len(
        voters), voters_with_no_votes
def find_all_subgraphs(di_graph_object, list_of_nodes_to_exclude=None):
    assert isinstance(di_graph_object, nx.DiGraph)
    di_graph_object_copy = di_graph_object.copy()
    tmp_di_graph_object = remove_list_of_nodes(di_graph_object_copy,
                                               list_of_nodes_to_exclude)
    tmp_list_of_bottleneck_nodes = look_for_bottleneck_nodes(
        tmp_di_graph_object)

    tmp_list_of_subgraph_node_pairs = []

    tmp_subgraph_dict = {}
    tmp_subgraph_dict['graph'] = nx.DiGraph()
    tmp_subgraph_dict[
        'condition'] = qmlReader.new_questionnaire_classes.ConditionObject(
            condition_string=True)
    subgraph_dict = defaultdict(tmp_subgraph_dict)

    for i in range(len(tmp_list_of_bottleneck_nodes) - 1):
        tmp_list_of_subgraph_node_pairs.append(
            (tmp_list_of_bottleneck_nodes[i],
             tmp_list_of_bottleneck_nodes[i + 1]))
        subgraph_dict.append()
    tmp_subgraph_nodes_dict = {}
    for node in tmp_list_of_bottleneck_nodes:
        di_graph_object_copy = remove_all_edges_connecting_to_node(
            di_graph_object_copy, node=node)
    for node_pair in tmp_list_of_subgraph_node_pairs:
        tmp_di_graph_object_copy = di_graph_object_copy.copy()
        # tmp_subgraph_nodes_dict[node_pair] = find_all_nodes_inbetween(di_graph_object=tmp_di_graph_object, source=node_pair[0], target=node_pair[1])
        tmp_subgraph_nodes_dict[node_pair] = nx.node_connected_component(
            nx.to_undirected(tmp_di_graph_object_copy), node_pair[0])
    return tmp_subgraph_nodes_dict
Ejemplo n.º 21
0
def scale_free_network(n_nodes):
    """
	returns the adjagency matrix of a scale free network
	the network is undirected and weighted

	"""

    m = np.zeros((n_nodes, n_nodes))

    G = nx.scale_free_graph(n_nodes)  # ritorna diretto
    G = nx.to_undirected(G)

    A = nx.adjacency_matrix(G)

    rows = A.nonzero()[0]
    columns = A.nonzero()[1]

    print(rows.shape, columns.shape)

    for i in range(rows.shape[0]):

        l, k = rows[i], columns[i]

        r = np.random.uniform(0, 1)
        m[l, k] = r
        m[k, l] = r

    return csr_matrix(m)
def statistics(graph, alpha, samples=2000):
    '''compare  statistics between uniform spanning tree and BW combined sample...
    
    alpha -- does Broder for the first alpha*len(graph) nodes, then finishes with Wilsons
    algorithm.
    
    
    a good statistic to compute is the probability that a given edge contains
    a spanning tree, because we can compute this explicitely...
    
    
    and also because if these probabilities agree, then it's evidence that the tree 
    sampling methods agree in distribution... (and if these marginals agree in every graph,
    then I think this proves that two methods produce the same distribution...)
    '''
    BW_trees = []
    W_trees = []
    for i in range(samples):
        BW_trees.append(nx.to_undirected(combined_broder_wilson(graph, alpha)))
        W_trees.append(nx.to_undirected(random_spanning_tree_wilson(graph)))

    edge = random.choice(list(graph.edges()))
    edge_probs_BW = []
    edge_probs_W = []

    for edge in graph.edges():

        e_prob_BW = np.mean([
            edge in tree.edges() or (edge[1], edge[0]) in tree.edges()
            for tree in BW_trees
        ])
        e_var_BW = np.var([
            edge in tree.edges() or (edge[1], edge[0]) in tree.edges()
            for tree in BW_trees
        ])
        e_prob_W = np.mean([
            edge in tree.edges() or (edge[1], edge[0]) in tree.edges()
            for tree in W_trees
        ])
        e_var_W = np.var([
            edge in tree.edges() or (edge[1], edge[0]) in tree.edges()
            for tree in W_trees
        ])
        edge_probs_BW.append([e_prob_BW, e_var_BW])
        edge_probs_W.append([e_prob_W, e_var_W])

    return np.asarray(edge_probs_BW) - np.asarray(edge_probs_W)
    def process(self, inputs: ValueSet, outputs: ValueSet) -> None:

        if self.get_config_value("find_largest_component"):
            input_graph: Graph = inputs.get_value_data("graph")
            undir_graph = nx.to_undirected(input_graph)
            undir_components = nx.connected_components(undir_graph)
            lg_component = max(undir_components, key=len)
            subgraph = input_graph.subgraph(lg_component)

            outputs.set_values(largest_component=subgraph)

        if self.get_config_value("number_of_components"):
            input_graph = inputs.get_value_data("graph")
            undir_graph = nx.to_undirected(input_graph)
            number_of_components = nx.number_connected_components(undir_graph)

            outputs.set_values(number_of_components=number_of_components)
Ejemplo n.º 24
0
def Connectivity(G):
    '''number of neighbors of a node (can differ from Degree which is the number of edges)'''
    H = nx.to_undirected(G).copy()  # convert to undirected
    R = list(nx.selfloop_edges(H))
    for (u, v) in R:
        H.remove_edge(u, v)  # remove self-loop edges
    conn = {v: len(H.adj[v]) for v in H}  # connectivity (=number of neighbors)
    return conn
Ejemplo n.º 25
0
 def distance_matrix(self, progress=False):
     dim = max(self.nodes) + 1
     D = dok_matrix((dim, dim))
     gen = nx.all_pairs_shortest_path_length(nx.to_undirected(self.graph))
     for source, table in tqdm(gen, total=dim, disable=not progress):
         for target, dist in table.items():
             D[source, target] = dist
     return D
Ejemplo n.º 26
0
def calculate_largest_weakly_connected_comp(g):
    """
    LWCC
    :param nx.Graph g: Graph
    :return: A view of the LWCC graph
    """
    return nx.to_undirected(
        max(nx.weakly_connected_component_subgraphs(g, copy=False), key=len))
Ejemplo n.º 27
0
 def setUp(self):
     self.G = nx.path_graph(9)
     self.DG = nx.path_graph(9, create_using=nx.DiGraph())
     self.MG = nx.path_graph(9, create_using=nx.MultiGraph())
     self.MDG = nx.path_graph(9, create_using=nx.MultiDiGraph())
     self.Gv = nx.to_undirected(self.DG)
     self.DGv = nx.to_directed(self.G)
     self.MGv = nx.to_undirected(self.MDG)
     self.MDGv = nx.to_directed(self.MG)
     self.Rv = self.DG.reverse()
     self.MRv = self.MDG.reverse()
     self.graphs = [
         self.G, self.DG, self.MG, self.MDG, self.Gv, self.DGv, self.MGv,
         self.MDGv, self.Rv, self.MRv
     ]
     for G in self.graphs:
         G.edges, G.nodes, G.degree
    def get_correct_paths(node, graph):
        """
        Functions returns correct paths of node: deg(node) = 4, based on smallest angle between vetors
        Given node v of deg(v) = 4 (in undirected graph), it has 4 neighbors {w, u, k, m} and 2 neighbors {k, m} in
        directed graph - we are exactly interested in that case.
        To form sensible paths we have find paths (v1, v, v2), v1 in {w, u}, v2 in {k, m} that angle between
        vectors (v1, v) & (v, v2) is the smallest, so tram would make only smooth turns
        :param node:    node to delete - node of deg(node) = 4
        :param graph: directed MultiDiGraph
        :return: list of correct paths to add, single path represents correct edge that will be added to graph,
        """
        # We need undirected graph to define undirected neighbors
        un_graph = nx.Graph(graph)
        un_graph = nx.to_undirected(un_graph)
        # Define undirected neighbors
        un_neighbors = set(un_graph.neighbors(node[0]))
        # There is an exception - some nodes are of deg()=4, but have less than 4 neighbors
        if len(un_neighbors) < 4:
            return None
        # Define directed neighbors
        di_neighbors = set(graph.neighbors(node[0]))
        un_neighbors = un_neighbors.difference(di_neighbors)
        di_neighbors = list(di_neighbors)
        un_neighbors = list(un_neighbors)
        # Here we check for condition described in above function description
        if len(un_neighbors) != 2 or len(di_neighbors) != 2:
            return None
        # First vector from 1 to given node
        vect_1 = tuple((graph.nodes[un_neighbors[0]]['y'] - node[1]['y'],
                        graph.nodes[un_neighbors[0]]['x'] - node[1]['x']))
        # Second vector from 2 to given node
        vect_2 = tuple((graph.nodes[un_neighbors[1]]['y'] - node[1]['y'],
                        graph.nodes[un_neighbors[1]]['x'] - node[1]['x']))
        # Third vector from given node to 3
        vect_3 = tuple((node[1]['y'] - graph.nodes[di_neighbors[0]]['y'],
                        node[1]['x'] - graph.nodes[di_neighbors[0]]['x']))

        # Fourth vector from given node to 4
        vect_4 = tuple((node[1]['y'] - graph.nodes[di_neighbors[1]]['y'],
                        node[1]['x'] - graph.nodes[di_neighbors[1]]['x']))
        # Calculate angles between vectors
        angle_13 = angle_between(vect_1, vect_3)
        angle_14 = angle_between(vect_1, vect_4)
        angle_23 = angle_between(vect_2, vect_3)
        angle_24 = angle_between(vect_2, vect_4)
        # The set of two correct paths will be chosen based on the smallest angle
        paths = {
            angle_13: [[un_neighbors[0], node[0], di_neighbors[0]],
                       [un_neighbors[1], node[0], di_neighbors[1]]],
            angle_14: [[un_neighbors[0], node[0], di_neighbors[1]],
                       [un_neighbors[1], node[0], di_neighbors[0]]],
            angle_23: [[un_neighbors[1], node[0], di_neighbors[0]],
                       [un_neighbors[0], node[0], di_neighbors[1]]],
            angle_24: [[un_neighbors[1], node[0], di_neighbors[1]],
                       [un_neighbors[0], node[0], di_neighbors[0]]]
        }
        best_angle = sorted([angle_13, angle_14, angle_24, angle_23])[0]
        return paths[best_angle]
Ejemplo n.º 29
0
def test_vice_versa_convert():
    G = nx.complete_graph(5)
    assert G.is_directed() is False
    data = from_networkx(G)
    assert data.is_directed() is False
    G = to_networkx(data)
    assert G.is_directed() is True
    G = nx.to_undirected(G)
    assert G.is_directed() is False
Ejemplo n.º 30
0
 def __init__(self, skeleton, sep_sets, indep_test_func=None, threshold=0, n_nodes=None):
     if n_nodes is None:
         n_nodes = nx.number_of_nodes(skeleton)
     self.n_nodes = n_nodes
     self.skeleton = nx.to_undirected(skeleton)
     self.sep_sets = sep_sets
     self.pag = nx.to_edgelist(self.skeleton)
     self.pag = list(map(lambda edge: PagEdge(edge[0], edge[1], ArrowType.CIRCLE, ArrowType.CIRCLE), self.pag))
     self.indep_test_func = indep_test_func
     self.threshold = threshold
Ejemplo n.º 31
0
def _fix_disconnected_components(dgraph, clusters):

    new_clusters = []
    for cluster in clusters:
        projected_graph = dgraph.project(cluster)
        undirect = nx.to_undirected(projected_graph.dgraph)
        node_grps = nx.connected_components(undirect)
        for grp in node_grps:
            new_clusters.append(list(grp))
    return new_clusters
Ejemplo n.º 32
0
 def test_already_directed(self):
     uu = nx.to_undirected(self.uv)
     Muu = nx.to_undirected(self.Muv)
     assert_edges_equal(uu.edges, self.uv.edges)
     assert_edges_equal(Muu.edges, self.Muv.edges)
Ejemplo n.º 33
0
 def test_already_directed(self):
     dd = nx.to_undirected(self.dv)
     Mdd = nx.to_undirected(self.Mdv)
Ejemplo n.º 34
0
 def setup(self):
     self.G = nx.path_graph(9)
     self.G = nx.path_graph(9, create_using=nx.DiGraph())
     self.dv = nx.to_undirected(self.G)
     self.MG = nx.path_graph(9, create_using=nx.MultiDiGraph())
     self.Mdv = nx.to_undirected(self.MG)
Ejemplo n.º 35
0
def mask_test_edges_directed(adj, test_frac=.1, val_frac=.05, 
    prevent_disconnect=True, verbose=False, false_edge_sampling='iterative'):
    if verbose == True:
        print 'preprocessing...'

    # Remove diagonal elements
    adj = adj - sp.dia_matrix((adj.diagonal()[np.newaxis, :], [0]), shape=adj.shape)
    adj.eliminate_zeros()
    # Check that diag is zero:
    assert np.diag(adj.todense()).sum() == 0

    # Convert to networkx graph to calc num. weakly connected components
    g = nx.from_scipy_sparse_matrix(adj, create_using=nx.DiGraph())
    orig_num_wcc = nx.number_weakly_connected_components(g)

    adj_tuple = sparse_to_tuple(adj) # (coords, values, shape)
    edges = adj_tuple[0] # List of ALL edges (either direction)
    edge_pairs = [(edge[0], edge[1]) for edge in edges] # store edges as list of tuples (from_node, to_node)

    num_test = int(np.floor(edges.shape[0] * test_frac)) # controls how large the test set should be
    num_val = int(np.floor(edges.shape[0] * val_frac)) # controls how alrge the validation set should be
    num_train = len(edge_pairs) - num_test - num_val # num train edges

    all_edge_set = set(edge_pairs)
    train_edges = set(edge_pairs) # init train_edges to have all edges
    test_edges = set() # init test_edges as empty set
    val_edges = set() # init val edges as empty set

    ### ---------- TRUE EDGES ---------- ###
    # Shuffle and iterate over all edges
    np.random.shuffle(edge_pairs)

    # get initial bridge edges
    bridge_edges = set(nx.bridges(nx.to_undirected(g))) 

    if verbose:
        print('creating true edges...')

    for ind, edge in enumerate(edge_pairs):
        node1, node2 = edge[0], edge[1]

        # Recalculate bridges every ____ iterations to relatively recent
        if ind % 10000 == 0:
            bridge_edges = set(nx.bridges(nx.to_undirected(g))) 

        # Don't sample bridge edges to increase likelihood of staying connected
        if (node1, node2) in bridge_edges or (node2, node1) in bridge_edges: 
            continue

        # If removing edge would disconnect the graph, backtrack and move on
        g.remove_edge(node1, node2)
        if prevent_disconnect == True:
            if not nx.is_weakly_connected(g):
                g.add_edge(node1, node2)
                continue

        # Fill test_edges first
        if len(test_edges) < num_test:
            test_edges.add(edge)
            train_edges.remove(edge)
            if len(test_edges) % 10000 == 0 and verbose == True:
                print 'Current num test edges: ', len(test_edges)

        # Then, fill val_edges
        elif len(val_edges) < num_val:
            val_edges.add(edge)
            train_edges.remove(edge)
            if len(val_edges) % 10000 == 0 and verbose == True:
                print 'Current num val edges: ', len(val_edges)

        # Both edge lists full --> break loop
        elif len(test_edges) == num_test and len(val_edges) == num_val:
            break



    # Check that enough test/val edges were found
    if (len(val_edges) < num_val or len(test_edges) < num_test):
        print "WARNING: not enough removable edges to perform full train-test split!"
        print "Num. (test, val) edges requested: (", num_test, ", ", num_val, ")"
        print "Num. (test, val) edges returned: (", len(test_edges), ", ", len(val_edges), ")"

    # Print stats for largest remaining WCC
    print 'Num WCC: ', nx.number_weakly_connected_components(g)
    largest_wcc_set = max(nx.weakly_connected_components(g), key=len)
    largest_wcc = g.subgraph(largest_wcc_set)
    print 'Largest WCC num nodes: ', largest_wcc.number_of_nodes()
    print 'Largest WCC num edges: ', largest_wcc.number_of_edges()

    if prevent_disconnect == True:
        assert nx.number_weakly_connected_components(g) == orig_num_cc

    # Fraction of edges with both endpoints in largest WCC
    def frac_edges_in_wcc(edge_set):
        num_wcc_contained_edges = 0.0
        num_total_edges = 0.0
        for edge in edge_set:
            num_total_edges += 1
            if edge[0] in largest_wcc_set and edge[1] in largest_wcc_set:
                num_wcc_contained_edges += 1
        frac_in_wcc = num_wcc_contained_edges / num_total_edges
        return frac_in_wcc

    # Check what percentage of edges have both endpoints in largest WCC
    print 'Fraction of train edges with both endpoints in L-WCC: ', frac_edges_in_wcc(train_edges)
    print 'Fraction of test edges with both endpoints in L-WCC: ', frac_edges_in_wcc(test_edges)
    print 'Fraction of val edges with both endpoints in L-WCC: ', frac_edges_in_wcc(val_edges)

    # Ignore edges with endpoint not in largest WCC
    print 'Removing edges with either endpoint not in L-WCC from train-test split...'
    train_edges = {edge for edge in train_edges if edge[0] in largest_wcc_set and edge[1] in largest_wcc_set}
    test_edges = {edge for edge in test_edges if edge[0] in largest_wcc_set and edge[1] in largest_wcc_set}
    val_edges = {edge for edge in val_edges if edge[0] in largest_wcc_set and edge[1] in largest_wcc_set}


    ### ---------- FALSE EDGES ---------- ###

    # Initialize empty sets
    train_edges_false = set()
    test_edges_false = set()
    val_edges_false = set()

    # Generate candidate false edges (from g-complement) and iterate through them
    if false_edge_sampling == 'iterative':
        if verbose == True:
            print 'preparing complement adjacency matrix...'

        # Sample false edges from G-complement, instead of randomly generating edges
        # g_complement = nx.complement(g)
        adj_complement = 1 - adj.toarray() # flip 0's, 1's in adjacency matrix
        np.fill_diagonal(adj_complement, val=0) # set diagonals to 0

        # 2 numpy arrays indicating x, y coords in adj_complement
            # WARNING: This line can use up a lot of RAM depending on 'adj' size
        idx1, idx2 = np.where(adj_complement == 1) 
            
        edges_false = np.stack((idx1, idx2), axis=-1) # stack arrays into coord pairs.
        edge_pairs_false = [(edge[0], edge[1]) for false_edge in edges_false]

        # Shuffle and iterate over false edges
        np.random.shuffle(edge_pairs_false)
        if verbose == True:
            print 'adding candidate false edges to false edge sets...'
        for false_edge in edge_pairs_false:
            # Fill train_edges_false first
            if len(train_edges_false) < len(train_edges):
                train_edges_false.add(false_edge)
                if len(train_edges_false) % 100000 == 0 and verbose == True:
                    print 'Current num false train edges: ', len(train_edges_false)

            # Fill test_edges_false next
            elif len(test_edges_false) < len(test_edges):
                test_edges_false.add(false_edge)
                if len(test_edges_false) % 100000 == 0 and verbose == True:
                    print 'Current num false test edges: ', len(test_edges_false)

            # Fill val_edges_false last
            elif len(val_edges_false) < len(val_edges):
                val_edges_false.add(false_edge)
                if len(val_edges_false) % 100000 == 0 and verbose == True:
                    print 'Current num false val edges: ', len(val_edges_false)

            # All sets filled --> break
            elif len(train_edges_false) == len(train_edges) and \
                len(test_edges_false) == len(test_edges) and \
                len(val_edges_false) == len(val_edges):
                break

    # Randomly generate false edges (idx_i, idx_j) 1 at a time to save memory
    elif false_edge_sampling == 'random':
        if verbose == True:
            print 'creating false test edges...'

        # FALSE TEST EDGES
        while len(test_edges_false) < len(test_edges):
            idx_i = np.random.randint(0, adj.shape[0])
            idx_j = np.random.randint(0, adj.shape[0])
            if idx_i == idx_j: # no self-loops
                continue

            # Ensure both endpoints are in largest WCC
            if idx_i not in largest_wcc_set or idx_j not in largest_wcc_set:
                continue

            false_edge = (idx_i, idx_j)

            # Make sure false_edge not an actual edge, and not a repeat
            if false_edge in all_edge_set:
                continue
            if false_edge in test_edges_false:
                continue

            test_edges_false.add(false_edge)

            if len(test_edges_false) % 100000 == 0 and verbose == True:
                print 'Current num false test edges: ', len(test_edges_false)

        # FALSE VAL EDGES
        if verbose == True:
            print 'creating false val edges...'

        while len(val_edges_false) < len(val_edges):
            idx_i = np.random.randint(0, adj.shape[0])
            idx_j = np.random.randint(0, adj.shape[0])
            if idx_i == idx_j:
                continue

            false_edge = (idx_i, idx_j)

            # Make sure false_edge in not an actual edge, not in test_edges_false, not a repeat
            if false_edge in all_edge_set or \
                false_edge in test_edges_false or \
                false_edge in val_edges_false:
                continue
                
            val_edges_false.add(false_edge)

            if len(val_edges_false) % 100000 == 0 and verbose == True:
                print 'Current num false val edges: ', len(val_edges_false)

        # FALSE TRAIN EDGES
        if verbose == True:
            print 'creating false train edges...'

        while len(train_edges_false) < len(train_edges):
            idx_i = np.random.randint(0, adj.shape[0])
            idx_j = np.random.randint(0, adj.shape[0])
            if idx_i == idx_j:
                continue

            false_edge = (idx_i, idx_j)

            # Make sure false_edge in not an actual edge, not in test_edges_false, 
                # not in val_edges_false, not a repeat
            if false_edge in all_edge_set or \
                false_edge in test_edges_false or \
                false_edge in val_edges_false or \
                false_edge in train_edges_false:
                continue

            train_edges_false.add(false_edge)

            if len(train_edges_false) % 100000 == 0 and verbose == True:
                print 'Current num false train edges: ', len(train_edges_false)


    ### ---------- FINAL DISJOINTNESS CHECKS ---------- ###
    if verbose == True:
        print 'final checks for disjointness...'

    # assert: false_edges are actually false (not in all_edge_tuples)
    assert test_edges_false.isdisjoint(all_edge_set)
    assert val_edges_false.isdisjoint(all_edge_set)
    assert train_edges_false.isdisjoint(all_edge_set)

    # assert: test, val, train false edges disjoint
    assert test_edges_false.isdisjoint(val_edges_false)
    assert test_edges_false.isdisjoint(train_edges_false)
    assert val_edges_false.isdisjoint(train_edges_false)

    # assert: test, val, train positive edges disjoint
    assert val_edges.isdisjoint(train_edges)
    assert test_edges.isdisjoint(train_edges)
    assert val_edges.isdisjoint(test_edges)

    if verbose == True:
        print 'creating adj_train...'

    # Re-build adj matrix using remaining graph
    adj_train = nx.adjacency_matrix(g)

    # Convert edge-lists to numpy arrays
    train_edges = np.array([list(edge_tuple) for edge_tuple in train_edges])
    train_edges_false = np.array([list(edge_tuple) for edge_tuple in train_edges_false])
    val_edges = np.array([list(edge_tuple) for edge_tuple in val_edges])
    val_edges_false = np.array([list(edge_tuple) for edge_tuple in val_edges_false])
    test_edges = np.array([list(edge_tuple) for edge_tuple in test_edges])
    test_edges_false = np.array([list(edge_tuple) for edge_tuple in test_edges_false])

    if verbose == True:
        print 'Done with train-test split!'
        print 'Num train edges (true, false): (', train_edges.shape[0], ', ', train_edges_false.shape[0], ')'
        print 'Num test edges (true, false): (', test_edges.shape[0], ', ', test_edges_false.shape[0], ')'
        print 'Num val edges (true, false): (', val_edges.shape[0], ', ', val_edges_false.shape[0], ')'
        print ''

    # Return final edge lists (edges can go either direction!)
    return adj_train, train_edges, train_edges_false, \
        val_edges, val_edges_false, test_edges, test_edges_false
Ejemplo n.º 36
0
 def setup(self):
     self.DG = nx.path_graph(9, create_using=nx.DiGraph())
     self.uv = nx.to_undirected(self.DG)
     self.MDG = nx.path_graph(9, create_using=nx.MultiDiGraph())
     self.Muv = nx.to_undirected(self.MDG)