Example #1
0
def main():
    G = initialize()
    user, business = node_initialize(G)
    user = list(set(user) & set(G.nodes()))
    business = list(set(business) & set(G.nodes()))
    G = make_bipartite(G, user, business)
    print nx.is_bipartite(G)
    G = list(nx.connected_component_subgraphs(G))[0]
    user, business = bipartite.sets(G)
    print "nodes separated"
    Gu = bipartite.projected_graph(G, user)
    print Gu.number_of_nodes()
Example #2
0
def get_graph_shape(pattern):
    if nx.is_bipartite(pattern) and nr_nodes_with_degree_bigger_than(pattern,4)==1:
        return "star"
    if nx.is_bipartite(pattern) and nr_nodes_with_degree_bigger_than(pattern,4)>1:
        return str(nr_nodes_with_degree_bigger_than(pattern,4))+"_star"
    if is_there_cycle_in_graph(pattern):
        return "cycle"
    if not is_there_cycle_in_graph(pattern) and nr_nodes_with_degree(pattern,1)==2:
        return "chain"
    if not is_there_cycle_in_graph(pattern) and nr_nodes_with_degree(pattern,3)==1:
        return "T"
    if not is_there_cycle_in_graph(pattern) and nr_nodes_with_degree(pattern,3)>1:
        return str(nr_nodes_with_degree(pattern,3))+"_T"
Example #3
0
def edge_bipartivity_measure_new(Graph, param):

    adj_matrix = adjacency_matrix(Graph)
    total_edge = nx.number_of_edges(Graph)
    mine = -1000
    while not nx.is_bipartite(Graph):
        row, col = adj_matrix.shape
        for i in range(row):
            j = 0
            while j <= i:
                if adj_matrix[i, j] == 1:
                    if param == 1:
                        e = edge_bipartivity_adjacency(Graph, i, j)
                    else:
                        e = edge_bipartivity_normalized_laplacian(Graph, i, j)
                    if e > mine:
                        mine = e
                        edgei = i
                        edgej = j
                j = j + 1

        adj_matrix[edgei, edgej] = 0
        adj_matrix[edgej, edgei] = 0
        Graph = nx.from_numpy_matrix(adj_matrix)
        mine = -1000

    return float(nx.number_of_edges(Graph)) / (total_edge)
Example #4
0
    def create_bipartite_graph(self):
        """
        Create a bipartite graph of two node types:
            Notes (stored as integer)
            Harmonic series (stores as Series() object)
            Between a note and a given harmonic series, the edge weight is
            given by n, where the note is the nth harmonic of the fundamental
        """

        note_list = list(range(88))
        series_list = [Series(i) for i in note_list]

        B = nx.Graph()
        B.add_nodes_from(note_list, bipartite=0)
        B.add_nodes_from(series_list, bipartite=1)

        for series in series_list:
            for note in note_list:

                #check if the note is in the given truncated harmonic series
                truncation = 6
                series_notes = [0, 12, 19, 24, 28, 31]
                for i in range(truncation):
                    #if so, add it to the graph with weight n,
                    #where the note is the nth harmonic of the fundamental
                    if (note == series.fundamental + series_notes[i]):
                        #add edge
                        B.add_edge(note, series, weight=i)

        self.B = B
        assert nx.is_bipartite(B)
def print_check(title, g):
    print('-- Check', title, 'Graph --')
    print('Tree graph?', nx.is_tree(g))
    print('Complete graph?', check_complete_graph(g))
    print('Bipartite graph?', nx.is_bipartite(g))
    print('Cycle graph?', False if len(nx.cycle_basis(g)) == 0 else True)
    print('')
Example #6
0
def test_is_bipartite():
    for i in range(1, 10):
        for j in range(1, i * i):
            x = nx.dense_gnm_random_graph(i, j)
            y = nqs.graph.CustomGraph(x.edges)
            # if len(x) == len(set((i for (i, j) in x.edges)) | set((j for (i, j) in x.edges))):
            assert y.is_bipartite == nx.is_bipartite(x)
Example #7
0
def project_notwork(bnetwork, list_of_items):
    if nx.is_bipartite(bipartite_network):
        # print("My network is Bipartite")
        projected_nodes = bipartite.weighted_projected_graph(
            bipartite_network, list_of_items)
    # nx.draw(projected_users, with_labels=True)
    return projected_nodes
Example #8
0
def construct_bi_graph_buyer_product(orders, name='Bipartite'):
    """ Constructs bipartite graph of buyers and products"""
    G = nx.Graph()
    G.name = name
    dprint("Constructing bipartite graph...")

    buyers = set([order['buyer'] for order in orders])
    products = set([order['product'] for order in orders])

    # Add nodes to bipartite graph
    G.add_nodes_from(buyers, bipartite=0)
    G.add_nodes_from(products, bipartite=1)

    # Loop trough orders and add edges to bipartite graph
    edges = defaultdict(int)
    for idx, order in enumerate(orders):
        edges[(order['buyer'], order['product'])] += order['quantity']
        #* (idx / len(orders))

    G.add_weighted_edges_from([(b, p, w) for ((b, p), w) in edges.items()])

    if (nx.is_bipartite(G)):
        dprint('Bipartite is constructed')
    else:
        dprint('Error not bipartite graph')
        exit(-1)

    return G
Example #9
0
def test_lattice_is_bipartite():
    for graph in graphs:
        print(graph)
        g = nx.Graph()
        for edge in graph.edges():
            g.add_edge(edge[0], edge[1])
        assert graph.is_bipartite() == nx.is_bipartite(g)
Example #10
0
    def test_kegg_link_graph_returns_bipartite_graphs(self, mocker):

        mocker.return_value = [hsa_nodes, enzyme_nodes]

        graph = kgu.kegg_link_graph("hsa", "enzyme")

        self.assertTrue(nx.is_bipartite(graph), "Graph is not bipartite")
Example #11
0
def estrada_bipartivity(Graph):
    adj_matrix = adjacency_matrix(Graph)
    total_edge = nx.number_of_edges(Graph)
    mine = 1000
    while not nx.is_bipartite(Graph):
        row, col = adj_matrix.shape
        bg = calculate_betaG(adj_matrix)
        for i in range(row):
            j = 0
            while j <= i:
                if adj_matrix[i, j] == 1:
                    bge = calculate_betaGe(Graph, i, j)
                    e = 1 - (bge - bg)
                    if e < mine:
                        mine = e
                        edgei = i
                        edgej = j
                j = j + 1

        adj_matrix[edgei, edgej] = 0
        adj_matrix[edgej, edgei] = 0
        Graph = nx.from_numpy_matrix(adj_matrix)
        mine = 1000

    return float(nx.number_of_edges(Graph)) / (total_edge)
Example #12
0
    def check_if_bipartite(self):
        """Checks if graph is bipartite."""

        if nx.is_bipartite(self.G):
            pass
        else:
            raise_exc = 1
            for i in range(len(self.structures_copy)):
                self.structures = self.structures_copy[:]
                self.structures.remove(self.structures_copy[i])
                self.G.clear()
                self.draw_graph()
                if nx.is_bipartite(self.G):
                    print "Structure number ", i + 1, " is inconsistent."
                    raise_exc = 0
                    break
            if raise_exc == 1:
                raise Exception("Inconsistent structures. Unsolvable case.")
Example #13
0
def main():
    fname = os.path.join(dir.graphs_dir, "long-all-bipartite.edges")
    full_g = nx.read_edgelist(fname, delimiter=",", nodetype=str)
    if nx.is_bipartite(full_g):
        nois, followers = bipartite.sets(full_g)
	# bipartite graph projection
        g = projection.overlap_weighted_projected_graph(full_g, nodes=nois, jaccard=False)
        fname = os.path.join(dir.graphs_dir, "projection-full-graph.edgelist")
        nx.write_edgelist(g, fname, delimiter=',', data='weight')
Example #14
0
 def test_random_digraph(self):
     n = 10
     m = 20
     G = random_graph(n, m, 0.9, directed=True)
     assert len(G) == 30
     assert nx.is_bipartite(G)
     X, Y = nx.algorithms.bipartite.sets(G)
     assert set(range(n)) == X
     assert set(range(n, n + m)) == Y
Example #15
0
def test_is_bipartite():
    for i in range(1, 10):
        for j in range(1, i * i):
            x = nx.dense_gnm_random_graph(i, j)
            y = nk.graph.Graph(nodes=list(x.nodes()), edges=list(x.edges()))
            if len(x) == len(
                    set((i for (i, j) in x.edges()))
                    | set((j for (i, j) in x.edges()))):
                assert y.is_bipartite() == nx.is_bipartite(x)
Example #16
0
def test_is_bipartite():
    for i in range(1, 10):
        for j in range(1, i * i):
            x = nx.dense_gnm_random_graph(i, j)
            y = Graph.from_networkx(x)
            if len(x) == len(
                set((i for (i, j) in x.edges())) | set((j for (i, j) in x.edges()))
            ):
                assert y.is_bipartite() == nx.is_bipartite(x)
Example #17
0
def BipartiteGraphPercentiles(dataset, percentileUp, percentileLow, usereferenceGood = False):
    """
    Construct bipartite graph B = (S, G)
    S are the poor outcome patients and G are the deregulated genes.
    For each gene the corresponding values for the upper and lower percentiles are calculated from its expression profile. 
    Then for each patient it is tested whether the gene's value (e.g. expression) lies in the top or bottom percentile.

    dataset: An ExpressionDataset
    percentileUp, percentileLow: the cut-off percentiles
    usereferenceGood: if True the percentiles are determined on the profiles of good outcome patients only.
    """

    #divide into two patient groups
    if usereferenceGood:
        GoodP = dataset.extractPatientsByIndices("Good", dataset.patientClassLabels==False, False, False)
        PoorP = dataset.extractPatientsByIndices("Poor", dataset.patientClassLabels==True, False, False)
    else:
        GoodP = dataset
        PoorP = dataset

    edges = []
    #translate index of gene to Entrez GeneID
    dict_IdxToGeneNames = dict(zip(range(dataset.expressionData.shape[1]), dataset.geneLabels.tolist()))

    print "DETERMINE edges ..."

    tic = time.time()
    for gene in range(0, GoodP.expressionData.shape[1]):
        #upper, lower: cut-off values in the tail of the distribution.
        upper = scipy.stats.scoreatpercentile(GoodP.expressionData[:, gene], percentileUp)
        lower = scipy.stats.scoreatpercentile(GoodP.expressionData[:, gene], percentileLow)
        mean = GoodP.expressionData[:, gene].mean()
        std = GoodP.expressionData[:, gene].std()
        for pat in range(0, PoorP.expressionData.shape[0]):
            try:
                patName = PoorP.patientLabels[pat].split("/")[1] #if / in patient label only take the latter part
            except:
                patName = PoorP.patientLabels[pat]
            if PoorP.expressionData[pat, gene]>upper:
                edges.append((patName, dict_IdxToGeneNames[gene], (PoorP.expressionData[pat, gene]- mean)/std, PoorP.expressionData[pat, gene]))
            elif PoorP.expressionData[pat, gene]<lower:
                edges.append((patName, dict_IdxToGeneNames[gene], (PoorP.expressionData[pat, gene]- mean)/std, PoorP.expressionData[pat, gene]))

    #bipartite graph
    print "CONSTRUCT bipartite graph"
    B = nx.Graph()
    for n1, n2, dereg, expr in edges:
        B.add_edge(n1, n2, deregulation = dereg, expression = expr)

    assert nx.is_bipartite(B)

    toc = time.time()
    print "Graph constructed in %.3f seconds." % (toc - tic)

    return B
Example #18
0
def get_quasi_bicliques():
    pre_processing_flag = False

    # graph = nx.read_graphml('datasets/toy_bipartite.graphml', node_type=int)
    # graph = nx.read_graphml('datasets/bipartite.graphml', node_type=int)
    # graph = nx.read_graphml('datasets/amazon_thousands.graphml', node_type=int)
    graph = nx.read_graphml('datasets/bipartite_diameter.graphml',
                            node_type=int)
    # graph = load_graph('datasets/bipartite_diameter.graphml')

    print(
        "Looking for quasi bicliques with gamma <%s> density; lambda <%s> density "
        "and min. size of U = <%s> and V = <%s>" %
        (gamma_min, lambda_min, u_min, v_min))

    # if not is_bipartite(graph):
    #     print("Not a bipartite graph")
    #     return
    # elapsed_time = time.time()  # Starting timer
    print("Warning! Graph pre-processing disabled\n"
          ) if not pre_processing_flag else pre_processing(graph)
    pre_processing(graph)  # Passing by reference?

    # After this point, the graph contains a cleaned version of the graph

    cc_list = list(nx.connected_component_subgraphs(
        graph))  ## list of connected components
    # Iterating through connected components

    for cc in cc_list:
        if not nx.is_bipartite(cc):
            continue
        u_vertices = set(n for n, d in cc.nodes(data=True)
                         if d['color'] == "red")
        v_vertices = set(n for n, d in cc.nodes(data=True)
                         if d['color'] == "blue")

        if len(v_vertices) < v_min or len(u_vertices) < u_min:
            print("skipping CC.  |U'| = %s  |V'| = %s" %
                  (len(u_vertices), len(v_vertices)))
            continue
        print("CC of # vertices: |U| = %s ,  |V| = %s   total nodes SET: %s" %
              (len(u_vertices), len(v_vertices),
               (2**len(u_vertices) - 1) * (2**len(v_vertices) - 1)))
        # global_tree_traverse = detect_quasi_biclique(graph, nx.subgraph(graph, vertices_in_cc))
        detect_quasi_biclique(graph, cc)
        print("End of CC")
        # break # TODO: delete after testing

    print("Quasi-bicliques in the graph: ", cluster_list)
    print("Total QBC in graph: ", len(cluster_list))
    final_time = time.time() - elapsed_time
    # print("# visits to SET: %s" % global_tree_traverse)

    print("runtime: %s" % final_time)
Example #19
0
def verify(pre_oct_set, oct_set, graph):
    """
    Returns whether the preprocessed OCT vertices (pre_oct_set) and the proposed oct_set are really an OCT set.
    """
    # Remove the OCT set
    if pre_oct_set:
        graph.remove_nodes_from(pre_oct_set)
    if oct_set:
        graph.remove_nodes_from(oct_set)

    return nx.is_bipartite(graph)
Example #20
0
 def test_gnmk_random_graph_complete(self):
     n = 10
     m = 20
     edges = 200
     G = gnmk_random_graph(n, m, edges)
     assert len(G) == n + m
     assert nx.is_bipartite(G)
     X, Y = nx.algorithms.bipartite.sets(G)
     # print(X)
     assert set(range(n)) == X
     assert set(range(n, n + m)) == Y
     assert edges == len(list(G.edges()))
Example #21
0
def BipartiteGraphTwoSided(dataset, pVal):
    """
    Construct bipartite graph B = (S, G)
    S are the poor outcome patients and G are the deregulated genes.
    For each gene a reference (gaussian) distribution is estimated from the good outcome patients. 
    Then for each poor outcome patient it is tested whether the gene's value (e.g. expression) lies in the top
    pVal/2 or bottom pVal/2 tails.

    dataset: An ExpressionDataset
    pVal:    cut off value for the tails
    """

    #divide into two patient groups
    GoodP = dataset.extractPatientsByIndices("Good", dataset.patientClassLabels==False, False, False)
    PoorP = dataset.extractPatientsByIndices("Poor", dataset.patientClassLabels==True, False, False)

    edges = []
    #translate index of gene to Entrez GeneID
    dict_IdxToGeneNames = dict(zip(range(dataset.expressionData.shape[1]), dataset.geneLabels.tolist()))

    print "DETERMINE edges ..."

    tic = time.time()
    for gene in range(0, GoodP.expressionData.shape[1]):
        dist = scipy.stats.norm.fit(GoodP.expressionData[:, gene])
        #upper, lower: cut-off values in the tail of the distribution.
        upper = scipy.stats.norm.ppf(1-pVal/2, dist[0], dist[1])
        lower = scipy.stats.norm.ppf(pVal/2, dist[0], dist[1])
        mean = GoodP.expressionData[:, gene].mean()
        std = GoodP.expressionData[:, gene].std()
        for pat in range(0, PoorP.expressionData.shape[0]):
            try:
                patName = PoorP.patientLabels[pat].split("/")[1] #if / in patient label only take the latter part
            except:
                patName = PoorP.patientLabels[pat]
            if PoorP.expressionData[pat, gene]>upper:
                edges.append((patName, dict_IdxToGeneNames[gene], (PoorP.expressionData[pat, gene]- mean)/std, PoorP.expressionData[pat, gene]))
            elif PoorP.expressionData[pat, gene]<lower:
                edges.append((patName, dict_IdxToGeneNames[gene], (PoorP.expressionData[pat, gene]- mean)/std, PoorP.expressionData[pat, gene]))

    #bipartite graph
    print "CONSTRUCT bipartite graph"
    B = nx.Graph()
    for n1, n2, dereg, expr in edges:
        B.add_edge(n1, n2, deregulation = dereg, expression = expr)

    assert nx.is_bipartite(B)

    toc = time.time()
    print "Graph constructed in %.3f seconds." % (toc - tic)

    return B
Example #22
0
def create_graph(speakerset, personae):
    """
    First creates a bipartite graph with scenes on the one hand,
    and speakers in one scene on the other.
    The graph is then projected into a unipartite graph of speakers,
    which are linked if they appear in one scene together.

    Returns a networkx weighted projected graph.
    """
    charmap = create_charmap(personae)
    if debug:
        print(charmap)

    B = nx.Graph()
    labels = {}
    for act, scenes in speakerset.items():
        for scene, speakers in scenes.items():
            try:
                source = " ".join([act, scene])
            except TypeError:
                source = " ".join([scene, scene])
            targets = speakers

            if not source in B.nodes():
                B.add_node(source, bipartite=0)
                labels[source] = source

            for target in targets:
                target = charmap.get(target)
                if not target in B.nodes():
                    B.add_node(target, bipartite=1)
                B.add_edge(source, target)

    scene_nodes = set(n for n,d in B.nodes(data=True) if d['bipartite']==0)
    person_nodes = set(B) - scene_nodes
    nx.is_bipartite(B)
    G = nx.bipartite.weighted_projected_graph(B, person_nodes)

    return G
Example #23
0
def verifier(nodes, edges, choice):
    g = nx.Graph()
    g.add_nodes_from(nodes)
    g.add_edges_from(edges)
    is_bip = nx.is_bipartite(g)
    if choice in "true":
        choice = True
    else:
        choice = False
    if not (is_bip ^ choice):
        print('Risposta esatta')
    else:
        print('Risposta errata')
Example #24
0
def create_graph(speakerset, personae):
    """
    First creates a bipartite graph with scenes on the one hand,
    and speakers in one scene on the other.
    The graph is then projected into a unipartite graph of speakers,
    which are linked if they appear in one scene together.

    Returns a networkx weighted projected graph.
    """
    charmap = create_charmap(personae)
    if args.debug:
        print(charmap)

    B = nx.Graph()
    labels = {}
    for act, scenes in speakerset.items():
        for scene, speakers in scenes.items():
            try:
                source = " ".join([act, scene])
            except TypeError:
                source = " ".join([scene, scene])
            targets = speakers

            if not source in B.nodes():
                B.add_node(source, bipartite=0)
                labels[source] = source

            for target in targets:
                target = charmap.get(target)
                if not target in B.nodes():
                    B.add_node(target, bipartite=1)
                B.add_edge(source, target)

    scene_nodes = set(n for n, d in B.nodes(data=True) if d['bipartite'] == 0)
    person_nodes = set(B) - scene_nodes
    nx.is_bipartite(B)
    G = nx.bipartite.weighted_projected_graph(B, person_nodes)

    return G
def test_pairwise_bipartite_cc_functions():
    # Test functions for different kinds of bipartite clustering coefficients
    # between pairs of nodes using 3 example graphs from figure 5 p. 40
    # Latapy et al (2008)
    G1 = nx.Graph([(0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (1, 5), (1, 6), (1, 7)])
    G2 = nx.Graph([(0, 2), (0, 3), (0, 4), (1, 3), (1, 4), (1, 5)])
    G3 = nx.Graph([(0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9)])
    result = {0: [1 / 3.0, 2 / 3.0, 2 / 5.0], 1: [1 / 2.0, 2 / 3.0, 2 / 3.0], 2: [2 / 8.0, 2 / 5.0, 2 / 5.0]}
    for i, G in enumerate([G1, G2, G3]):
        assert nx.is_bipartite(G)
        assert cc_dot(set(G[0]), set(G[1])) == result[i][0]
        assert cc_min(set(G[0]), set(G[1])) == result[i][1]
        assert cc_max(set(G[0]), set(G[1])) == result[i][2]
Example #26
0
    def create_graph(self):
        """
        First creates a bipartite graph with scenes on the one hand,
        and speakers in one scene on the other.
        The graph is then projected into a unipartite graph of speakers,
        which are linked if they appear in one scene together.

        Returns a networkx weighted projected graph.
        """
        speakerset = self.segments

        B = nx.Graph()
        labels = {}
        for i, speakers in enumerate(speakerset):
            # speakers are Character objects
            source = str(i)
            targets = speakers
            # if args.debug:
            #     print("SOURCE, TARGET:", source, targets)

            if source not in B.nodes():
                B.add_node(source, bipartite=0)
                labels[source] = source

            for target in targets:
                if target not in B.nodes():
                    B.add_node(target, bipartite=1)
                B.add_edge(source, target)

        scene_nodes = set(n
                          for n, d in B.nodes(data=True)
                          if d['bipartite'] == 0)
        person_nodes = set(B) - scene_nodes
        nx.is_bipartite(B)
        G = nx.bipartite.weighted_projected_graph(B, person_nodes)
        if self.major_only:
            G = max(nx.connected_component_subgraphs(G), key=len)
        return G
Example #27
0
def get_bipartite_proj(G,proj1_name=None,proj2_name=None):
    """docstring for get_bipartite_proj
    Returns the bipartite projection for each set
    of nodes in the bipartite graph G
    """
    if networkx.is_bipartite(G):
        set1,set2=networkx.bipartite_sets(G)
        net1=networkx.project(G,set1)
        net1.name=proj1_name
        net2=networkx.project(G,set2)
        net2.name=proj2_name
        return net1,net2
    else:
        raise networkx.NetworkXError("Network is not bipartite")
Example #28
0
 def test_gnmk_random_graph(self):
     n = 10
     m = 20
     edges = 100
     # set seed because sometimes it is not connected
     # which raises an error in bipartite.sets(G) below.
     G = gnmk_random_graph(n, m, edges, seed=1234)
     assert len(G) == n + m
     assert nx.is_bipartite(G)
     X, Y = nx.algorithms.bipartite.sets(G)
     # print(X)
     assert set(range(n)) == X
     assert set(range(n, n + m)) == Y
     assert edges == len(list(G.edges()))
Example #29
0
    def __init__(self, graph):
        if (False == nx.is_bipartite(graph)):
            print "[ERROR]Not a bipartite graph provided."
            sys.exit()

        #Copying the graph provided by the user
        self.G = graph

        #Creating a Directed graph with all bidirectional edges
        self.D = graph.to_directed()

        #Separating the variables and constraints
        self.variables, self.constraints = nx.bipartite.sets(self.G)

        #Ensuring that the bipartite == 1 is assigned to varaibles
        Y = list(self.variables)
        if (self.G.node[Y[0]]['bipartite'] == 1):
            X = self.constraints
            self.constraints = self.variables
            self.variables = X

        #Separating the known and the unknown variables
        try:
            known = []
            unknown = []
            for n in self.variables:
                if (self.G.node[n]['type'] == 'known'):
                    known.append(n)
                elif (self.G.node[n]['type'] == 'unknown'):
                    unknown.append(n)
                else:
                    print "[ERROR] Please specify for each node type as 'known' or 'unknown'"
                    sys.exit()

            self.known = set(known)
            self.unknown = set(unknown)
        except KeyError:
            print "[ERROR] Please specify for each node type as 'known' or 'unknown'"
            sys.exit()

        #Creating reduced graph by removing known variables
        self.R = self.G.copy()
        self.R.remove_nodes_from(self.known)

        #Removing edges which cannot be matched
        #Example with derivative casuality
        for x, y in self.G.edges():
            if 'derivative_casuality' in self.G[x][y]:
                self.R.remove_edge(x, y)
Example #30
0
    def __init__(self, graph):
        if (False == nx.is_bipartite(graph)):
            print "[ERROR]Not a bipartite graph provided."
            sys.exit()

        #Copying the graph provided by the user
        self.G = graph
        
        #Creating a Directed graph with all bidirectional edges
        self.D = graph.to_directed()

        #Separating the variables and constraints
        self.variables, self.constraints = nx.bipartite.sets(self.G)

        #Ensuring that the bipartite == 1 is assigned to varaibles
        Y = list(self.variables)
        if (self.G.node[Y[0]]['bipartite'] == 1):
            X = self.constraints
            self.constraints = self.variables
            self.variables = X

        #Separating the known and the unknown variables
        try:
            known = []
            unknown = []
            for n in self.variables:
                if (self.G.node[n]['type'] == 'known'):
                    known.append(n)
                elif (self.G.node[n]['type'] == 'unknown'):
                    unknown.append(n)
                else:
                    print "[ERROR] Please specify for each node type as 'known' or 'unknown'"
                    sys.exit()

            self.known = set(known)
            self.unknown = set(unknown)
        except KeyError:
                print "[ERROR] Please specify for each node type as 'known' or 'unknown'"
                sys.exit()

        #Creating reduced graph by removing known variables
        self.R = self.G.copy()
        self.R.remove_nodes_from(self.known)
        
        #Removing edges which cannot be matched
        #Example with derivative casuality
        for x,y in  self.R.edges():
            if 'derivative_casuality' in self.G[x][y] :
                self.R.remove_edge(x,y)
def build_graph(u_idx_set: set, v_idx_set: set, graph_dict: dict):
    edges = list()

    for feat_idx in graph_dict.keys():
        # order doesn't matter here
        vertex0 = feat_idx
        for vertex1 in graph_dict.get(feat_idx, []):
            if (vertex0 in u_idx_set) and (vertex1 in v_idx_set):
                edges.append((vertex0, vertex1))
            elif (vertex0 in v_idx_set) and (vertex1 in u_idx_set):
                edges.append((vertex1, vertex0))

    graph = nx.Graph()
    graph.add_nodes_from(u_idx_set, bipartite=0)
    graph.add_nodes_from(v_idx_set, bipartite=1)
    graph.add_edges_from(edges)
    assert nx.is_bipartite(graph), "Error: graph should be bipartite!"

    # remove disconnected components
    print("before: total of {} vertices".format(graph.number_of_nodes()))
    graph.remove_nodes_from(list(nx.isolates(graph)))
    print("after: total of {} vertices".format(graph.number_of_nodes()))
    assert nx.is_bipartite(graph), "Error: graph should be bipartite!"

    # A = np.asarray(nx.adjacency_matrix(graph).todense())
    # print(A.shape)
    new_u_idx_set = {
        n
        for n, d in graph.nodes(data=True) if d['bipartite'] == 0
    }
    new_v_idx_set = {
        n
        for n, d in graph.nodes(data=True) if d['bipartite'] == 1
    }

    return new_u_idx_set, new_v_idx_set, graph
Example #32
0
def main():
    test_cases = [
        nx.complete_graph(8),
        star_with_extra_edges(14, 19),
        two_touching_cycles(10, 3),
        two_touching_cycles(5, 22),
    ]

    for g in test_cases:
        assert nx.is_connected(g)
        assert not nx.is_bipartite(g)

    solved_cases = sum(evaluate_test_case(g) for g in test_cases)

    print(f"You solved {solved_cases} cases out of {len(test_cases)}.")
    def test_random_graph2(self):
        """
    Check program output for a random not bipartite graph
    """
        N = random.randint(3, 5)
        G = random_graph(N, bipartite=False)

        save(G, "input-network.txt", "weight")

        subprocess.check_call(["./lab4a.out", "input-network.txt"])

        reference = "YES" if nx.is_bipartite(G) else "NO"
        result = self.load_result("result.txt")

        self.assertEqual(reference, result)
    def test_cycle_graph1(self):
        """
    Check program output for an even cycle graph
    """
        N = random.randint(2, 5) * 2
        G = random_cycle_graph(N)

        save(G, "input-network.txt", "weight")

        subprocess.check_call(["./lab4a.out", "input-network.txt"])

        reference = "YES" if nx.is_bipartite(G) else "NO"
        result = self.load_result("result.txt")

        self.assertEqual(reference, result)
    def layout_args(layout_algorithm, network, scale):
        if layout_algorithm == 'Shell':
            if nx.is_bipartite(network):
                nodes, other_nodes = get_bipartite_node_set(network,
                                                            bipartite=0)
                args = dict(nlist=[nodes, other_nodes])

        if layout_algorithm == 'Fruchterman-Reingold':
            k = scale  #/ math.sqrt(network.number_of_nodes())
            args = dict(dim=2, k=k, iterations=20, weight='weight', scale=0.5)

        if layout_algorithm == 'Kamada-Kawai':
            args = dict(dim=2, weight='weight', scale=1.0)

        return dict()
Example #36
0
def simrank_bipartite(G, r=0.8, max_iter=100, eps=1e-4):
    """ A bipartite version in the paper.
    """
    if not nx.is_bipartite(G):
        assert ("A bipartie graph is required.")

    nodes = G.nodes()
    nodes_i = {}
    for (k, v) in [(nodes[i], i) for i in range(0, len(nodes))]:
        nodes_i[k] = v

    sim_prev = np.zeros(len(nodes))
    sim = np.identity(len(nodes))

    lns = {}
    rns = {}
    for n in nodes:
        preds = G.predecessors(n)
        succs = G.successors(n)
        if len(preds) == 0:
            lns[n] = succs
        else:
            rns[n] = preds

    def _update_partite(ns):
        for u, v in itertools.product(ns.keys(), ns.keys()):
            if u is v: continue
            u_ns, v_ns = ns[u], ns[v]
            if len(u_ns) == 0 or len(v_ns) == 0:
                sim[nodes_i[u]][nodes_i[v]] = 0
            else:
                s_uv = sum([
                    sim_prev[nodes_i[u_n]][nodes_i[v_n]]
                    for u_n, v_n in itertools.product(u_ns, v_ns)
                ])
                sim[nodes_i[u]][nodes_i[v]] = (r * s_uv) / (len(u_ns) *
                                                            len(v_ns))

    for i in range(max_iter):
        if np.allclose(sim, sim_prev, atol=eps):
            break
        sim_prev = np.copy(sim)
        _update_partite(lns)
        _update_partite(rns)

    print("Converge after %d iterations (eps=%f)." % (i, eps))

    return sim
Example #37
0
def BipartiteGraphSTD(dataset, mul = 1):
    """
    Construct bipartite graph B = (S, G)
    S are the patients and G are the deregulated genes.
    For each gene the expression profile is estimated from all patients.
    Then for each patient it is tested whether the gene's value (e.g. expression) lies outside of the standard deviation.
    If yes, an edge is drawn between the gene and the patient.

    dataset: An ExpressionDataset
    percentileUp, percentileLow: the cut-off percentiles
    usereferenceGood: if True the percentiles are determined on the profiles of good outcome patients only.
    """

    edges = []
    #translate index of gene to Entrez GeneID
    dict_IdxToGeneNames = dict(zip(range(dataset.expressionData.shape[1]), dataset.geneLabels.tolist()))

    print "DETERMINE edges ..."

    tic = time.time()
    for gene in range(0, dataset.expressionData.shape[1]):
        dist = scipy.stats.norm.fit(dataset.expressionData[:, gene])
        #upper, lower: cut-off values in the tail of the distribution.
        mean = dataset.expressionData[:, gene].mean()
        std = dataset.expressionData[:, gene].std()
        for pat in range(0, dataset.expressionData.shape[0]):
            try:
                patName = dataset.patientLabels[pat].split("/")[1] #if / in patient label only take the latter part
            except:
                patName = dataset.patientLabels[pat]
            if dataset.expressionData[pat, gene]>mean+mul*std:
                edges.append((patName, dict_IdxToGeneNames[gene], (dataset.expressionData[pat, gene]- mean)/std, dataset.expressionData[pat, gene]))
            elif dataset.expressionData[pat, gene]<mean-mul*std:
                edges.append((patName, dict_IdxToGeneNames[gene], (dataset.expressionData[pat, gene]- mean)/std, dataset.expressionData[pat, gene]))

    #bipartite graph
    print "CONSTRUCT bipartite graph"
    B = nx.Graph()
    for n1, n2, dereg, expr in edges:
        B.add_edge(n1, n2, deregulation = dereg, expression = expr)

    assert nx.is_bipartite(B)

    toc = time.time()
    print "Graph constructed in %.3f seconds." % (toc - tic)

    return B
Example #38
0
def bipartite_adj(G):
    G = G.to_undirected()
    if not nx.is_bipartite(G):
        return 'Error: the network is not bipartite'
    set1, set2 = nx.bipartite.sets(G)
    if len(set1) > len(set2):
        firms = np.array(list(set1))
        banks = np.array(list(set2))
    else:
        firms = np.array(list(set2))
        banks = np.array(list(set1))
    
    n = len(firms)
    nodelist = np.append(firms, banks)
    
    A = nx.to_scipy_sparse_matrix(G, nodelist = nodelist)
    biAdj = A[0:n, n:] 
    return biAdj
Example #39
0
def BipartiteGraphExprThreshold(dataset, thresholdUp, thresholdDown):
    """
    Construct bipartite graph B = (S, G)
    S are the patients and G are the deregulated genes.
    A threshold on the expression values determines wether the gene is deregulated.

    dataset: An ExpressionDataset
    thresholdUp, thresholdDown: the cut-off values
    """

    edges = []
    #translate index of gene to Entrez GeneID
    dict_IdxToGeneNames = dict(zip(range(dataset.expressionData.shape[1]), dataset.geneLabels.tolist()))

    print "DETERMINE edges ..."

    tic = time.time()
    for gene in range(0, dataset.expressionData.shape[1]):
        #upper, lower: cut-off values in the tail of the distribution.
        mean = dataset.expressionData[:, gene].mean()
        std = dataset.expressionData[:, gene].std()
        for pat in range(0, dataset.expressionData.shape[0]):
            try:
                patName = dataset.patientLabels[pat].split("/")[1] #if / in patient label only take the latter part
            except:
                patName = dataset.patientLabels[pat]
            print pat, gene, dataset.expressionData[pat, gene]
            if dataset.expressionData[pat, gene]>thresholdUp:
                edges.append((patName, dict_IdxToGeneNames[gene], (dataset.expressionData[pat, gene]- mean)/std, dataset.expressionData[pat, gene]))
            elif dataset.expressionData[pat, gene]<thresholdDown:
                edges.append((patName, dict_IdxToGeneNames[gene], (dataset.expressionData[pat, gene]- mean)/std, dataset.expressionData[pat, gene]))

    #bipartite graph
    print "CONSTRUCT bipartite graph"
    B = nx.Graph()
    for n1, n2, dereg, expr in edges:
        B.add_edge(n1, n2, deregulation = dereg, expression = expr)

    assert nx.is_bipartite(B)

    toc = time.time()
    print "Graph constructed in %.3f seconds." % (toc - tic)

    return B
Example #40
0
def simrank_bipartite(G, r=0.8, max_iter=100, eps=1e-4):
    """ A bipartite version in the paper.
    """
    if not nx.is_bipartite(G):
        assert("A bipartie graph is required.")

    nodes = G.nodes()
    nodes_i = {}
    for (k, v) in [(nodes[i], i) for i in range(0, len(nodes))]:
        nodes_i[k] = v

    sim_prev = np.zeros(len(nodes))
    sim = np.identity(len(nodes))

    lns = {}
    rns = {}
    for n in nodes:
        preds = G.predecessors(n)
        succs = G.successors(n)
        if len(preds) == 0:
            lns[n] = succs
        else:
            rns[n] = preds

    def _update_partite(ns):
        for u, v in itertools.product(ns.keys(), ns.keys()):
            if u is v: continue
            u_ns, v_ns = ns[u], ns[v]
            if len(u_ns) == 0 or len(v_ns) == 0:
                sim[nodes_i[u]][nodes_i[v]] = 0
            else:
                s_uv = sum([sim_prev[nodes_i[u_n]][nodes_i[v_n]] for u_n, v_n in itertools.product(u_ns, v_ns)])
                sim[nodes_i[u]][nodes_i[v]] = (r * s_uv) / (len(u_ns) * len(v_ns))

    for i in range(max_iter):
        if np.allclose(sim, sim_prev, atol=eps):
            break
        sim_prev = np.copy(sim)
        _update_partite(lns)
        _update_partite(rns)

    print("Converge after %d iterations (eps=%f)." % (i, eps))

    return sim
Example #41
0
def BipartiteGraphBottom(dataset, pVal):

    """
    As BipartiteGraphTeoSided. Here edges between the poor outcome patients
    and the genes are introduced when the value lies in the top of the distribution.
    """    
    #divide into two patient groups
    GoodP = dataset.extractPatientsByIndices("Good", dataset.patientClassLabels==False, False, False)
    PoorP = dataset.extractPatientsByIndices("Poor", dataset.patientClassLabels==True, False, False)

    edges = []
    #translate index of gene to GeneID
    dict_IdxToGeneNames = dict(zip(range(dataset.expressionData.shape[1]), dataset.geneLabels.tolist()))

    print "DETERMINE edges ..."

    tic = time.time()
    for gene in range(0, GoodP.expressionData.shape[1]):
        dist = scipy.stats.norm.fit(GoodP.expressionData[:, gene])
        #upper, lower: cut-off values in the tail of the distribution.
        lower = scipy.stats.norm.ppf(pVal/2, dist[0], dist[1])
        mean = GoodP.expressionData[:, gene].mean()
        std = GoodP.expressionData[:, gene].std()
        for pat in range(0, PoorP.expressionData.shape[0]):
            patName = PoorP.patientLabels[pat].split("/")[1] #if / in patient label only take the latter part
            if PoorP.expressionData[pat, gene]<lower:
                edges.append((patName, dict_IdxToGeneNames[gene], (PoorP.expressionData[pat, gene]- mean)/std, PoorP.expressionData[pat, gene]))

    #bipartite graph
    print "CONSTRUCT bipartite graph"
    B = nx.Graph()
    for n1, n2, dereg, expr in edges:
        B.add_edge(n1, n2, deregulation = dereg, expression = expr)

    assert nx.is_bipartite(B)

    toc = time.time()
    print "Graph constructed in %.3f seconds." % (toc - tic)

    return B
Example #42
0
def bip():
    with open("rosalind_bip.txt") as f:
        lines = f.readlines()
        # remove empty lines
        lines = [line for line in lines if line.strip()]

    # Num test cases
    t = int(lines[0])
    del lines[0]
    for i in xrange(t):
        n, e = map(int, lines[0].split())
        edge_list = map(lambda x: map(int, x.strip().split()), lines[1:e+1])
        del lines[:e+1]

        # Create the graph
        G = nx.Graph()
        G.add_nodes_from(range(1,n+1))
        G.add_edges_from(edge_list)

        if nx.is_bipartite(G):
            print 1,
        else:
            print -1,
    print ""
Example #43
0
if not (len(sys.argv) == 3 and (sys.argv[1] == 'b' or sys.argv[1] == 'p' or sys.argv[1] == 'v')):
  print "USAGE: python {} <b|p|v> <path to graph>".format(sys.argv[0])
  print "\tb - BMM"
  print "\tp - P3C"
  print "\tv - VC3"
  sys.exit(0)


graph = graphml_parse(sys.argv[2])

if sys.argv[1] == "p":
  for n in nodes:
    if n.deg > 2:
      print "P3C skipped, degree of all nodes must be less than 2"
      sys.exit(0)
  acquire_algorithm(p3c)
  run_algorithm(p3c)

if sys.argv[1] == "b":
  if nx.is_bipartite(graph):
    acquire_algorithm(bmm)
    run_algorithm(bmm)
  else:
    print "BMM skipped, not bipartite"

if sys.argv[1] == "v":
  acquire_algorithm(vc3)
  run_algorithm(vc3)

draw_graph(graph)
Example #44
0
 def test_is_bipartite(self):
     G=nx.path_graph(4)
     assert_true(nx.is_bipartite(G))
     G=nx.DiGraph([(1,0)])
     assert_true(nx.is_bipartite(G))
 def test_is_bipartite(self):
     G=networkx.path_graph(4)
     assert_equal(networkx.is_bipartite(G),True)
def get_driver_nodes(DG):
    '''Return the driver nodes number and driver nodes from a DiGraph DG

    Basic Idea:
    Given a graph DG, create a new undirected bipartite graph, BG
    suppose DG has n nodes, the the BG has 2*n nodes, the first n nodes [0, n)
    form the left parts of BG, the [n, 2*n) nodes form the right part of BG,
    for each edge form DG, say, 1-->3, add edges in BG 1---(3+n)
    then call the maximum matching algorithm find the matched nodes
    All the unmatched nodes are the driver nodes we are looking for

    Parameters
    ----------
    DG: networkx.DiGraph, directed graph, node number start from 0

    Returns
    -------
    driver node num:    the number of driver nodes
    driver nodes:       the driver nodes we are looking for

    Notes:
    -------
    The index of nodes in DG must start from 0, 1, 2, 3...

    References:
    -----------
    [1] Yang-Yu Liu, Jean-Jacques Slotine, Albert L. Barabasi. Controllability
        of complex networks. Nature, 2011.
    '''
    assert(nx.is_directed(DG))

    nodeNum = DG.number_of_nodes()
    edgeNum = DG.number_of_edges()

    # convert to a bipartite graph
    G = nx.Graph()
    left_nodes  = ['a'+str(node) for node in G.nodes()]
    right_nodes = ['b'+str(node) for node in G.nodes()]       
    G.add_nodes_from(left_nodes)
    G.add_nodes_from(right_nodes)
    for edge in DG.edges():
        da = 'a' + str(edge[0])
        db = 'b' + str(edge[1])
        G.add_edge(da, db)

    assert(nx.is_bipartite(G))
    # maximum matching algorithm
    matched_edges = nx.maximal_matching(G)

    # find all the matched and unmatched nodes
    matched_nodes = [int(edge[0][1:]) if edge[0][0] == 'b' else int(edge[1][1:]) for edge in matched_edges]
    unmatched_nodes = [node for node in DG.nodes() if node not in matched_nodes]
    unmatched_nodes_num = len(unmatched_nodes)

    # perfect matching
    isPerfect = False
    if unmatched_nodes_num == 0:
        print '>>> Perfect Match Found ! <<<'
        isPerfect = True
        unmatched_nodes_num = 1
        unmatched_nodes = [0]
        
    return (isPerfect, unmatched_nodes_num, unmatched_nodes)
Example #47
0
def CompleteWeightedBipartiteGraph(dataset, thresholdUp=None, thresholdDown=None, mul = 1, debug = False):
    """
    Constructs the complete bipartite graph.Edges are drawn between patients and genes. 
    One can give thresholds for the over expression and under expression. Edges will be scored as 'real' edges 
    if the expression in a patient lies above thresholdUp or below thresholdDown. 
    If these two parameters are not given, thresholdUp = mean(gene)+mul*std(gene) and thresholdDown = mean(gene)-mul*std(gene)

    The graph's edges are annotated with 'expression' (actual matrix entry), 'deregulation' (the z-score of the gene for this patient)
    and the 'score'.
    The 'score' is the distance to thresholdUp iff expression is higher than the mean expression or the distance to thresholdDown iff
    expression is lower than the mean expression of the gene. 
    The sign in front of 'score' indicates whether the expression value lies within [thresholdDown, thresholdUp] -- 
    indicated by a negative score; or outside of this interval -- positive score. 

    For binary data use thresholdUp = 0 and thresholdDown = -1. 
    If you work with binary data ignore the field 'deregulation' in the returned graph! 
    """

    edges = []
    #translate index of gene to Entrez GeneID
    dict_IdxToGeneNames = dict(zip(range(dataset.expressionData.shape[1]), dataset.geneLabels.tolist()))

    print "DETERMINE edges ..."

    tic = time.time()
    Tup = []
    Tdown = []
    for gene in range(0, dataset.expressionData.shape[1]):
        #upper, lower: cut-off values in the tail of the distribution.
        mean = dataset.expressionData[:, gene].mean()
        std = dataset.expressionData[:, gene].std()
        #means.append(mean)
        #stds.append(std)
        for pat in range(0, dataset.expressionData.shape[0]):
            try:
                patName = dataset.patientLabels[pat].split("/")[1] #if / in patient label only take the latter part
            except:
                patName = dataset.patientLabels[pat]
            #print pat, gene, dataset.expressionData[pat, gene]
            if thresholdUp != None and thresholdDown!=None:
                if dataset.expressionData[pat, gene]>thresholdUp:
                    edges.append((patName, dict_IdxToGeneNames[gene], (dataset.expressionData[pat, gene]- mean)/std, dataset.expressionData[pat, gene], 
                        (abs(dataset.expressionData[pat, gene]) - abs(thresholdUp))/std))
                elif dataset.expressionData[pat, gene]<thresholdDown:
                    edges.append((patName, dict_IdxToGeneNames[gene], (dataset.expressionData[pat, gene]- mean)/std, dataset.expressionData[pat, gene], 
                        (abs(dataset.expressionData[pat, gene]) - abs(thresholdDown))/std))
                elif dataset.expressionData[pat, gene] > mean:
                    #if the expr is close 0 then give a high score, the '-' indicates taht it is a negative edge
                    edges.append((patName, dict_IdxToGeneNames[gene], (dataset.expressionData[pat, gene]- mean)/std, dataset.expressionData[pat, gene], 
                        -((abs(thresholdUp) - abs(dataset.expressionData[pat, gene]))/std)))
                else:
                    edges.append((patName, dict_IdxToGeneNames[gene], (dataset.expressionData[pat, gene]- mean)/std, dataset.expressionData[pat, gene], 
                        -((abs(thresholdDown) - abs(dataset.expressionData[pat, gene]))/std)))
            else:
                Up = mean + mul*std
                Down = mean - mul*std
                Tup.append(Up)
                Tdown.append(Down)
                if dataset.expressionData[pat, gene]>Up:
                   edges.append((patName, dict_IdxToGeneNames[gene], (dataset.expressionData[pat, gene]- mean)/std, dataset.expressionData[pat, gene],
                        (abs(dataset.expressionData[pat, gene]) - abs(Up))/std))
                elif dataset.expressionData[pat, gene]<Down:
                    edges.append((patName, dict_IdxToGeneNames[gene], (dataset.expressionData[pat, gene]- mean)/std, dataset.expressionData[pat, gene],
                        (abs(dataset.expressionData[pat, gene]) - abs(Down))/std))
                elif dataset.expressionData[pat, gene] > mean:
                    #if the expr is close to the mean then give a high score, the '-' indicates taht it is a negative edge
                    edges.append((patName, dict_IdxToGeneNames[gene], (dataset.expressionData[pat, gene]- mean)/std, dataset.expressionData[pat, gene],
                        -((abs(Up) - abs(dataset.expressionData[pat, gene]))/std)))
                else:
                    edges.append((patName, dict_IdxToGeneNames[gene], (dataset.expressionData[pat, gene]- mean)/std, dataset.expressionData[pat, gene],
                        -((abs(Down) - abs(dataset.expressionData[pat, gene]))/std)))

    #bipartite graph
    print "CONSTRUCT bipartite graph"
    B = nx.Graph()
    for n1, n2, dereg, expr, score in edges:
        B.add_nodes_from([n1, n2])
        B.add_edge(n1, n2, deregulation = dereg, expression = expr, score = score)

    assert nx.is_bipartite(B)

    toc = time.time()
    print "Graph constructed in %.3f seconds." % (toc - tic)

    if debug:
        return (B, Tup, Tdown)
    else:
        return B
Example #48
0
numNodes = int(nodesAndEdges[0])
numEdges = int(nodesAndEdges[1])

# adds nodes
for i in range(1, numNodes + 1):
	G.add_node(str(i))

# adds edges
for i in range(0, numEdges):
	temp = inputFile.readline().strip().split(" ")
	node1 = temp[0]
	node2 = temp[1]
	G.add_edge(node1, node2)

# if the graph is bipartite, we can easily find the optimal soltuion
if nx.is_bipartite(G):
	top_nodes, bottom_nodes = bipartite.sets(G)
	partition1 = list(top_nodes)
	partition2 = list(bottom_nodes)
	maxEdgeCount = 0
	for i in range(0, len(partition1)):
		maxEdgeCount += len(G.neighbors(partition1[i]))

# If it's not bipartite, we must start attempting to find the optimal solution
else:
	graph_dict = dict()
	sorted_graph_dict = dict()
	for i in G.nodes():
		graph_dict[i] = G.neighbors(str(i))
		sorted_graph_dict[i] = G.neighbors(str(i))
Example #49
0
def graph_to_conn(g, conn_type=core.Connectivity):
    """
    Convert a bipartite NetworkX directed multigraph to a connectivity object.

    Parameters
    ----------
    g : networkx.MultiDiGraph
        Directed multigraph instance.
    conn_type : {base.BaseConnectivity, core.Connectivity}
        Type of output to generate.

    Examples
    --------
    >>> import networkx as nx
    >>> g = nx.MultiDiGraph()
    >>> g.add_nodes_from(['A:0', 'A:1', 'B:2', 'B:1', 'B:0'])
    >>> g.add_edges_from([('A:0', 'B:1'), ('A:1', 'B:2')])
    >>> c = graph_to_conn(g, base.BaseConnectivity)
    >>> c['A', :, 'B', :]
    array([[0, 1, 0],
           [0, 0, 1]])
        
    Notes
    -----
    Assumes that `g` is bipartite and all of its nodes are labeled 'A:X' or
    'B:X', where 'A' and 'B' are the names of the connected modules.

    When loading a graph from a GEXF file via networkx.read_gexf(),
    the relabel parameter should be set to True to prevent the actual labels in
    the file from being ignored.
    """

    if not isinstance(g, nx.MultiDiGraph):
        raise ValueError('invalid graph object')
    if not nx.is_bipartite(g):
        raise ValueError('graph must be bipartite')
    if conn_type not in [base.BaseConnectivity, core.Connectivity]:
        raise ValueError('invalid connectivity type')
    
    # Categorize nodes and determine number of nodes to support:
    A_nodes = []
    B_nodes = []

    node_dict = {}
    for label in g.nodes():
        try:
            id, n = re.search('(.+):(.+)', label).groups()
        except:
            raise ValueError('incorrectly formatted node label: %s' % label)
        if not node_dict.has_key(id):
            node_dict[id] = set()
        node_dict[id].add(int(n))
    
    # Graph must be bipartite:
    if len(node_dict.keys()) != 2:
        raise ValueError('incorrectly formatted graph')
    A_id, B_id = node_dict.keys()
    N_A = len(node_dict[A_id])
    N_B = len(node_dict[B_id])

    # Nodes must be consecutively numbered from 0 to N:
    if set(range(max(node_dict[A_id])+1)) != node_dict[A_id] or \
        set(range(max(node_dict[B_id])+1)) != node_dict[B_id]:
        raise ValueError('nodes must be numbered consecutively from 0..N')

    # If a Connectivity object must be created, count how many graded potential
    # and spiking neurons are comprised by the graph:
    if conn_type == core.Connectivity:
        N_A_gpot = 0
        N_A_spike = 0
        N_B_gpot = 0
        N_B_spike = 0
        for n in node_dict[A_id]:
            if g.node[A_id+':'+str(n)]['neuron_type'] == 'gpot':
                N_A_gpot += 1
            elif g.node[A_id+':'+str(n)]['neuron_type'] == 'spike':
                N_A_spike += 1
            else:
                raise ValueError('invalid neuron type')
        for n in node_dict[B_id]:
            if g.node[B_id+':'+str(n)]['neuron_type'] == 'gpot':
                N_B_gpot += 1
            elif g.node[B_id+':'+str(n)]['neuron_type'] == 'spike':
                N_B_spike += 1
            else:
                raise ValueError('invalid neuron type')
        
    # Find maximum number of edges between any two nodes:
    N_mult = max([1]+[len(g[u][v]) for u,v in set(g.edges())])

    # Create empty connectivity structure:
    if conn_type == base.BaseConnectivity:
        c = base.BaseConnectivity(N_A, N_B, N_mult, A_id, B_id)
    else:
        c = core.Connectivity(N_A_gpot, N_A_spike, N_B_gpot, N_B_spike,
                              N_mult, A_id, B_id)
    
    # Set the parameters in the connectivity object:
    for edge in g.edges():
        A_id, i = edge[0].split(':')
        i = int(i)
        B_id, j = edge[1].split(':')
        j = int(j)
        edge_dict = g[edge[0]][edge[1]]
        for conn, k in enumerate(edge_dict.keys()):
            if conn_type == base.BaseConnectivity:
                idx_tuple = (A_id, i, B_id, j, conn)
            else:
                idx_tuple = (A_id, 'all', i, B_id, 'all', j, conn)
            c[idx_tuple] = 1

            for param in edge_dict[k].keys():

                # The ID loaded by networkx.read_gexf() is always a string, but
                # should be interpreted as an integer:
                if param == 'id':
                    c[idx_tuple+(param,)] = int(edge_dict[k][param])
                else:
                    c[idx_tuple+(param,)] = edge_dict[k][param]
                                    
    return c                        
Example #50
0
def bipartite_clustering(G, nodes=None, mode='dot'):
    """Compute a bipartite clustering coefficient for nodes.

    The bipartie clustering coefficient is a measure of local density
    of connections defined as [1]_
    
    .. math::

       c_u = \\frac{\\sum_{v \in N(N(v))} c_{uv} }{|N(N(u))|}

    where  
    :math:`N(N(u))` are the second order neighbors of 
    :math:`u` in :math:`G` excluding :math:`u`. 
    and :math:`c_{uv}` is the pairwise clustering coefficient between nodes 
    u and v.

    The mode selects the function for :math:`c_{uv}`

    'dot': 

    .. math::

       c_{uv}=\\frac{|N(u)\cap N(v)|}{|N(u) \cup N(v)|}

    'min': 

    .. math::

       c_{uv}=\\frac{|N(u)\cap N(v)|}{min(|N(u)|,|N(v)|)}

    'max': 

    .. math::

       c_{uv}=\\frac{|N(u)\cap N(v)|}{max(|N(u)|,|N(v)|)}


    Parameters
    ----------
    G : graph
        A bipartite graph

    nodes : list or iterable (optional)
        Compute bipartite clustering for these nodes. The default 
        is all nodes in G.

    mode : string
        The pariwise bipartite clustering method to be used in the computation.
        It must be "dot", "max", or "min". 
    
    Returns
    -------
    clustering : dictionary
        A dictionary keyed by node with the clustering coefficient value.


    Examples
    --------
    >>> G=nx.path_graph(4) # path is bipartite
    >>> c=nx.bipartite_clustering(G) 
    >>> c[0]
    0.5
    >>> c=nx.bipartite_clustering(G,mode='min') 
    >>> c[0]
    1.0

    See Also
    --------
    clustering
    average_bipartite_clustering
    
    References
    ----------
    .. [1] Latapy, Matthieu, Clémence Magnien, and Nathalie Del Vecchio (2008).
       Basic notions for the analysis of large two-mode networks. 
       Social Networks 30(1), 31--48.
    """
    if not nx.is_bipartite(G):
        raise nx.NetworkXError("Graph is not bipartite")
    
    try:
        cc_func = modes[mode]
    except KeyError:
        raise nx.NetworkXError(\
                "Mode for bipartite clustering must be: dot, min or max")

    if nodes is None:
        nodes = G
    ccs = {}
    for v in nodes:
        cc = 0.0
        nbrs2=set([u for nbr in G[v] for u in G[nbr]])-set([v])
        for u in nbrs2:
            cc += cc_func(set(G[u]),set(G[v]))
        if cc > 0.0: # len(nbrs2)>0
            cc /= len(nbrs2)
        ccs[v] = cc
    return ccs
Example #51
0
#!/usr/bin/python
# coding: utf-8
# 建立二分图与建立普通的图方法比较类似,需要注意的是,
# 边只能在不同类节点之间添加,同类节点之间不要添加边就可以。
# 下面是一个简单的例子
# 本例中用1开头的编号表示项目节点,用2开头的编号表示参与者节点

import networkx as nx
B = nx.Graph()
# 添加一个项目101,它有3个参与者:201,202,203
B.add_edge(101, 201)
B.add_edge(101, 202)
B.add_edge(101, 203)
# 添加一个项目102,它有2个参与者:203,202,2034
B.add_edge(102, 203)
B.add_edge(102, 204)
# 用networkx.is_bipartite()方法可以检测一个图是否是二分图
print nx.is_bipartite(B)
Example #52
0
#plt.show() # if you want to visualize your seed graph first
#init = InitMatrix(nodes)
#init = init.makeFromNetworkxGraph(G)
#init.addSelfEdges() # if you want to ensure self edges for Kronecker

k = 5
print ("Seed Matrix Nodes:")
print (nodes)
print ("Kronecker Iterations:")
print (k)
nxgraph = KroneckerGenerator.generateStochasticKron(init, k, True)
#for line in nx.generate_edgelist(nxgraph, data=False):
 #   print(line)
print ("Done Creating Network!")

is_bipart = nx.is_bipartite(nxgraph)
print ("is_bipart:")
print (is_bipart)
is_conn = nx.is_connected(nxgraph)
print ("is_conn:")
print (is_conn) #test

#print "Exporting to GML File"
#nx.write_gml(nxgraph,"KronSeed1_75a5b.gml") #export to gml file for gephi

#nx.draw(nxgraph, pos=nx.spring_layout(nxgraph))
#plt.show()

#print "Printing Statistics..."
#stats = create_graph_stats(nxgraph)
#print "Density: "
Example #53
0
 def test_is_bipartite(self):
     G=nx.path_graph(4)
     assert_true(nx.is_bipartite(G))