コード例 #1
0
ファイル: Directed.py プロジェクト: jim-bo/SINAH
	def comps(self):
		''' generator for connected components '''
		
		# loop over components.
		for comp in nx.weakly_connected_components(self):
			
			# create subgraph.
			subg = nx.DiGraph()
			
			# build node list.
			nlist = []
			for n in comp:
				nlist.append( (n, self.node[n]) )
				
			# add nodes.
			subg.add_nodes_from(nlist)
			
			# build edge list.
			elist = []
			for e in self.edges(comp):
				elist.append( (e[0], e[1], self[e[0]][e[1]]) )
				
			# add edges.
			subg.add_edges_from(elist)
			
			# yield the subgraph.
			yield subg
コード例 #2
0
ファイル: percolation.py プロジェクト: wronk/dbw
def lesion_met_largest_weak_component(G, orig_order=None):
    """
    Get largest weak component size of a graph.

    Parameters
    ----------
    G : directed networkx graph
        Graph to compute largest component for
    orig_order : int
        Define orig_order if you'd like the largest component proportion

    Returns
    -------
    largest weak component size : int
        Proportion of largest remaning component size if orig_order
        is defined. Otherwise, return number of nodes in largest component.
    """

    components = sorted(nx.weakly_connected_components(G), key=len,
                        reverse=True)
    if len(components) > 0:
        largest_component = len(components[0])
    else:
        largest_component = 0.

    # Check if original component size is defined
    if orig_order is not None:
        return largest_component / float(orig_order)
    else:
        return largest_component
コード例 #3
0
ファイル: eval.py プロジェクト: jim-bo/scafathon
def _calc_counts(G, min_size, gap, scaf_only=False):
    sizes = list()
    for comp in nx.weakly_connected_components(G):

        # skip non scaffolds.
        if scaf_only == True:
            if len(comp) < 2:
                continue

        # add contig size.
        size = 0
        for n in comp:
            size += G.node[n]['width']

        # add gap size.
        if gap != False:
            for p,q in G.edges(comp):
                size += G[p][q]['gap']

        # skip this.
        if min_size != False and size < min_size:
            continue

        # save the size.
        sizes.append(len(comp))

    return sizes
コード例 #4
0
def mark_vpn(graph, vpn_macs):
    components = map(frozenset, nx.weakly_connected_components(graph))
    components = filter(vpn_macs.intersection, components)
    nodes = reduce(lambda a, b: a | b, components, set())

    for node in nodes:
        for k, v in graph[node].items():
            v['vpn'] = True
コード例 #5
0
def test_weakly_connected_components(testgraph):
    """
    Test strongly connected components
    """

    comps0 = nx.weakly_connected_components(testgraph[0])
    comps1 = sg.components.weak(testgraph[1])

    assert_components_equal(comps0, comps1)
コード例 #6
0
    def singletons(self):
        """A singleton is a weakly connected component that has only one node.

        Returns:
            A list with the singleton nodes.
        """
        components = networkx.weakly_connected_components(self.nxgraph)
        return [component[0] for component in components
                if len(component) == 1]
コード例 #7
0
def report_stats(G, params):
    print 'Nodes: %d.  Edges: %d'%(G.number_of_nodes(), G.number_of_edges())
    sccs = nx.strongly_connected_components(G)
    wccs = nx.weakly_connected_components(G)
    print 'Strongly ccs: %d, Weakly ccs: %d'%(len(sccs), len(wccs))

    sizes_sccs, sizes_wccs = ([len(c) for c in sccs], [len(c) for c in wccs])
    print 'Singletons. Strongly: %d, Weakly: %d'%(sum(np.array(sizes_sccs)==1), sum(np.array(sizes_wccs)==1))
    print [len(c) for c in sccs[:10]]
コード例 #8
0
def find_len_2_control_kernals(deterministic_transition_graph, nodes_list, attractor_ID):
    """uses the deterministic_transition_graph and specified attractor to find 
    all pairs of control nodes if they exist

    note: these aren't "strict" control kernels because they specify the states needed to be in the
    main attractor. controlling them doesn't necessarily change what attractor you'll be in.
    """


    subgraphs = [g for g in nx.weakly_connected_components(deterministic_transition_graph)]
    # index_of_largest_subgraph = max(enumerate(all_subgraph_sets), key = lambda tup: len(tup[1]))[0]

    all_subgraph_sets = []
    all_but_attractor_subgraph = []
    for sg in subgraphs:
        if attractor_ID not in sg:
            all_state_sets = []
            for nID in sg:
                all_state_sets.append(set(time_evol.decimal_to_binary(nodes_list,nID).items()))
            all_subgraph_sets.append(all_state_sets)
            all_but_attractor_subgraph.append(all_state_sets)
        else:
            all_state_sets = []
            for nID in sg:
                all_state_sets.append(set(time_evol.decimal_to_binary(nodes_list,nID).items()))
            all_subgraph_sets.append(all_state_sets)

    state_0_list = [0 for i in range(len(nodes_list))]
    state_1_list = [1 for i in range(len(nodes_list))]

    possible_states = zip(nodes_list,state_0_list) + zip(nodes_list,state_1_list) # list of (node,state)

    possible_pairs = [combo for combo in combinations(possible_states, 2)]

    possible_pairs_pared = copy.deepcopy(possible_pairs)

    # remove pairs where both keys are the same (eg. gF and gF)
    for pair in possible_pairs:

        if pair[0][0] == pair[1][0] and (pair in possible_pairs_pared):

            possible_pairs_pared.remove(pair)

    # remove pairs when any of the networks in the non-attractor subgraph contain that pair
    # (ie. that pair can not possibly be a control kernel because it is present in the wrong attractor)
    for sg in all_but_attractor_subgraph:

        for state_set in sg:

            for pair in possible_pairs:

                if (pair[0] in state_set) and (pair[1] in state_set) and (pair in possible_pairs_pared):

                    possible_pairs_pared.remove(pair)

    return possible_pairs_pared # return a list ((node,state),(node,state)) pairs that are control kernels
コード例 #9
0
def main():
    parser = OptionParser()
    parser.add_option("-m", "--osm", dest="osm_data", help="Input open street map data (typically in gpickle format)", metavar="OSM_DATA", type="string")
    parser.add_option("-t", "--track_data", dest="track_data", help="Input GPS tracks", metavar="TRACK_DATA", type="string")
    parser.add_option("-o", "--output_osm", dest="output_osm", help="Output file name (suggested extention: gpickle)", metavar="OUTPUT", type="string")
    parser.add_option("--test_case", dest="test_case", type="int", help="Test cases: 0: region-0; 1: region-1; 2: SF-region.", default=0)
    (options, args) = parser.parse_args()
    
    if not options.osm_data:
        parser.error("Input osm_data not found!")
    if not options.track_data:
        parser.error("Input track_data not found!")
    if not options.output_osm:
        parser.error("Output image not specified!")

    R = const.R 
    if options.test_case == 0:
        LOC = const.Region_0_LOC
    elif options.test_case == 1:
        LOC = const.Region_1_LOC
    elif options.test_case == 2:
        LOC = const.SF_LOC
    else:
        parser.error("Test case indexed %d not supported!"%options.test_case)

    G = nx.read_gpickle(options.osm_data)
    components = nx.weakly_connected_components(G)
    H = G.subgraph(components[0])
    G = H

    tracks = 

    osm_for_drawing = OSM_DRAW(G)
    edge_lists = osm_for_drawing.edge_list()
    line_strings = []
    for edge_list in edge_lists:
        line = LineString(edge_list)
        line_strings.append(line)


    fig = plt.figure(figsize=(10, 10))
    ax = plt.Axes(fig, [0., 0., 1., 1.], aspect='equal')
    ax.set_axis_off()
    fig.add_axes(ax)

    ROAD_WIDTH = 7 # in meters
    for line_string in line_strings:
        polygon = line_string.buffer(ROAD_WIDTH)
        patch = PolygonPatch(polygon, facecolor='k', edgecolor='k')
        ax.add_patch(patch)
    ax.set_xlim([LOC[0]-R, LOC[0]+R])
    ax.set_ylim([LOC[1]-R, LOC[1]+R])
    fig.savefig(options.output_img, dpi=100)
    plt.close()
    
    return
コード例 #10
0
def main():
    if len(sys.argv) != 2:
        print "Error!\nCorrect usage is:\n\t"
        print "python visualize_osm_test_region.py [osm_test_region_for_draw.dat]"
        return
   
    G = nx.read_gpickle(sys.argv[1])

    components = nx.weakly_connected_components(G)
    print "There are %d connected components."%len(components)

    H = G.subgraph(components[0])
    G = H

    osm_for_drawing = OSM_DRAW(G)
    easting, northing = osm_for_drawing.node_list()
    edge_list = osm_for_drawing.edge_list()

    #map_decomposition = MapDecomposition()
    #map_decomposition.primitive_decomposition(G, 10.0)

    #return

    fig = plt.figure(figsize=const.figsize)
    ax = fig.add_subplot(111, aspect='equal')
    #print edge_list
    arrow_params = {'length_includes_head':True, 'shape':'full', 'head_starts_at_zero':False}
    for segment in edge_list:
        u = segment[1][0] - segment[0][0]
        v = segment[1][1] - segment[0][1]
        ax.arrow(segment[0][0], segment[0][1], u, v, width=0.5, head_width=5,\
                    head_length=10, overhang=0.5, **arrow_params)
    #edge_collection = LineCollection(edge_list, colors='gray', linewidths=2)
    #ax.add_collection(edge_collection)

    # Junction nodes
    for node in G.nodes():
        if G.degree(node) > 2:
            ax.plot(G.node[node]['data'].easting,
                    G.node[node]['data'].northing,
                    'ro')

    # Connected components
    #for index in range(0, len(components)):
    #    color = const.colors[index%7]
    #    print len(components[index])
    #    for node in components[index]:
    #        ax.plot(G.node[node]['data'].easting,
    #                G.node[node]['data'].northing,
    #                'o', color=color)
    #    break

    ax.set_xlim([const.RANGE_SW[0], const.RANGE_NE[0]])
    ax.set_ylim([const.RANGE_SW[1], const.RANGE_NE[1]])

    plt.show()
コード例 #11
0
def maximum_matching_all(bipartite_graph):
    matches = dict()
    if is_directed(bipartite_graph):
        parts = weakly_connected_components(bipartite_graph)
    else:
        parts = connected_components(bipartite_graph)
    for conn in parts:
        sub = bipartite_graph.subgraph(conn)
        max_matching = hopcroft_karp_matching(sub)
        matches.update(max_matching)
    return matches
コード例 #12
0
def kamada_kawaii(G):
    j = None
    G_h = G.copy()
    for s in nx.weakly_connected_components(G):
        i = s.pop()
        if j == None:
            pass
        else:
            G_h.add_edge(i, j)
        j = i
    return nx.layout.kamada_kawai_layout(G_h, weight=None)
コード例 #13
0
    def subtrees(self):

        subtree_nxs = []
        for component_nodes in nx.weakly_connected_components(self):

            # actually get the subtree from the main tree
            subtree = self.subgraph(component_nodes)

            subtree_nxs.append(subtree)

        return subtree_nxs
コード例 #14
0
ファイル: parents.py プロジェクト: gitter-badger/wepy-1
 def trees(self):
     """Returns a list of the subtrees from each root in this forest. In no particular order"""
     trees_by_size = [
         self.graph.subgraph(c)
         for c in nx.weakly_connected_components(self.graph)
     ]
     trees = []
     for root in self.roots:
         root_tree = [tree for tree in trees_by_size if root in tree][0]
         trees.append(root_tree)
     return trees
コード例 #15
0
def phi4(gold_graphs, pred_graphs):
    
    P, R = 0.0, 0.0
    
    for graph_file, gold in gold_graphs.items():
        predCCs = sorted(nx.weakly_connected_components(pred_graphs[graph_file]), key=len, reverse=True)
        goldCCs = sorted(nx.weakly_connected_components(gold), key=len, reverse=True)
        
        r, p, f = ceaf_e(goldCCs, predCCs)
        
        P += p
        R += r 
        
    
    P = P / len(gold_graphs.items())
    R = R / len(gold_graphs.items())
    
    return (round (P, 4) * 100, 
            round (R, 4) * 100, 
            round ((2 * P * R / (P + R)), 4) * 100) 
コード例 #16
0
def generate_data_by_graph(data_path):
    """
    生成全连通图数据(小涛哥编写)
    :param data_path:
    :return:
    """
    train_data = pd.read_csv(data_path)
    train_data_ = zip(list(train_data.label), list(train_data.q1),
                      list(train_data.q2))
    graph = nx.DiGraph()
    for ele in train_data_:
        if ele[0] == 1:
            graph.add_edge(ele[1], ele[2])
    sub_graph = [x for x in nx.weakly_connected_components(graph)]

    pos_edges_generated = []
    for sub in sub_graph:
        sub = list(sub)
        sub_edges = itertools.product(sub, sub)
        sub_edges = [x for x in sub_edges if x[1] != x[1]]
        pos_edges_generated.extend(sub_edges)

    df = pd.DataFrame(pos_edges_generated)
    df.to_csv('pos_edges_generated.csv')

    sub_id = [(sorted(x)[0], list(x)) for x in sub_graph]
    sub_id_ = dict(sub_id)
    node_sub_id = {}
    for item in sub_id:
        index = item[0]
        sub = item[1]
        for node in sub:
            node_sub_id[node] = index

    neg = [(y[1], y[2]) for y in train_data_ if y[0] == 0]
    neg_ids = []
    for ele in neg:
        id0 = node_sub_id.get(ele[0], ele[0])
        id1 = node_sub_id.get(ele[1], ele[1])
        neg_ids.append((id0, id1))
    neg_ids = set(neg_ids)
    neg_edges_generated = []
    for ele in neg_ids:
        graph1 = sub_id_.get(ele[0], [
            ele[0],
        ])
        graph2 = sub_id_.get(ele[1], [
            ele[1],
        ])
        pairs = itertools.product(graph1, graph2)
        neg_edges_generated.extend(pairs)

    df = pd.DataFrame(neg_edges_generated)
    df.to_csv('neg_edges_generated.csv')
コード例 #17
0
def large_compute_distances(L, Lmaxdistance):
    Ldistances = np.zeros(Lmaxdistance, dtype=int)
    #Gdistances = max(nx.weakly_connected_components(G), key=len)
    for source in max(nx.weakly_connected_components(L), key=len):
        for target in max(nx.weakly_connected_components(L), key=len):
            if source != target and nx.has_path(L, source, target):
                #Gdistances = max(nx.weakly_connected_components(G), key=len)
                Ldistances[nx.shortest_path_length(L, source, target)] += 1
    #print(Ldistances)

    # Plot distances #
    fig = plt.figure()
    plt.title("Large Network Distance Distribution")
    plt.xlabel("Distance")
    plt.xticks(np.arange(0, Lmaxdistance, 1))
    plt.ylabel("Frequency")
    plt.yscale('log')
    plt.bar(np.arange(Lmaxdistance), Ldistances, color=['red'])
    plt.show()
    fig.savefig("Large_Distance_Distribution.png")
コード例 #18
0
ファイル: chain.py プロジェクト: handanlinzhang/dtmc
 def is_absorbing(self):
     # to be absorbing all states must eventually reach an
     # absorbing state
     G = self._as_digraph()
     for cpt in nx.weakly_connected_components(G):
         Gprime = nx.subgraph(G, cpt)
         Gprime.remove_edges_from(Gprime.selfloop_edges())
         akeys = [k for k, v in Gprime.out_degree().iteritems() if v == 0]
         if len(akeys) == 0:
             return False
     return True
コード例 #19
0
def isConnected(graph):
    '''
        description:
            - given a graph, return if it is connected (weakly)
        params: 
            - graph: networkx DiGraph object
        returns:
            - boolean True if graph is planar, False otherwise
    '''
    wcc = list(nx.weakly_connected_components(graph))
    return len(wcc) == 1
コード例 #20
0
ファイル: graph.py プロジェクト: gcmshadow/pipe_base
    def subsetToConnected(self: _T) -> Tuple[_T, ...]:
        """Generate a list of subgraphs where each is connected.

        Returns
        -------
        result : list of `QuantumGraph`
            A list of graphs that are each connected
        """
        return tuple(
            self.subset(connectedSet) for connectedSet in
            nx.weakly_connected_components(self._connectedQuanta))
コード例 #21
0
    def _cut_disconnected(self, problem, graph, subtour_selector):
        """ Given a disconnected graph, perform subtour cuts """

        # print("Cutting disconnected solution...")
        if self.solver._is_symmetric:
            components = nx.connected_components(graph)
        if self.solver._is_asymmetric:
            components = nx.weakly_connected_components(graph)
        components = list(components)
        for subtour in subtour_selector.get_subtours(components):
            self._presolve_subtour(problem, subtour)
コード例 #22
0
 def __init__(self, g, restart_prob, og_prob):
     self.g = g
     self.restart_prob = restart_prob
     self.og_prob = og_prob
     self.wccs = list(nx.weakly_connected_components(g.to_directed()))
     self.num = g.number_of_nodes()
     # self.idmap = dict()
     # for idx, vid in enumerate(g.nodes()):
     #   self.idmap[vid] = idx  # Vertex ID --> Index
     # self.mat = np.zeros((num, num), dtype=float)
     self.mat = dict()
コード例 #23
0
def components_by_location(people_graph):
    """Return a representative node from each component associated with a given Location"""
    loc_components = defaultdict(list)
    components = list(nx.weakly_connected_components(people_graph.graph))
    for comp_nodes in components:
        locations = locations_in_component(
            [people_graph.people[node] for node in comp_nodes])
        for location in locations:
            loc_components[location].append(comp_nodes)

    return loc_components
コード例 #24
0
ファイル: test_algorithms.py プロジェクト: Geosyntec/nereid
def test_find_leafy_branch_larger_than_size(graph):

    with pytest.raises(nx.NetworkXUnfeasible):
        sg = find_leafy_branch_larger_than_size(graph, len(graph))

    for components in nx.weakly_connected_components(graph):
        c_graph = graph.subgraph(components)
        for size in range(1, len(c_graph) + 3):
            sg = find_leafy_branch_larger_than_size(c_graph, size)

            assert len(sg) >= size or sg == c_graph
コード例 #25
0
def simplify(collapse, blob):
    to_remove = []
    for coll in collapse:
        exclude = set(p for p in coll)
        candidates = [e for e in blob['edges'] if e['pred'] in exclude]
        for c in candidates:
            # make sure we can remove the edges later
            # if they have meta the match will fail
            if 'meta' in c:
                c.pop('meta')

        if candidates:
            edges = [Edge.fromOboGraph(c) for c in candidates]
            g = OntGraph().populate_from_triples(e.asRdf() for e in edges)
            nxg = egl.rdflib_to_networkx_multidigraph(g)
            connected = list(nx.weakly_connected_components(nxg))  # FIXME may not be minimal
            ends = [e.asRdf()[-1] for e in edges if e.p == coll[-1]]
            for c in connected:
                #log.debug('\n' + pformat(c))
                nxgt = nx.MultiDiGraph()
                nxgt.add_edges_from(nxg.edges(c, keys=True))
                ordered_nodes = list(nx.topological_sort(nxgt))
                paths = [p
                         for n in nxgt.nodes()
                         for e in ends
                         for p in list(nx.all_simple_paths(nxgt, n, e))
                         if len(p) == len(coll) + 1]

                for path in sorted(paths):
                    ordered_edges = nxgt.edges(path, keys=True)
                    oe2 = [Edge.fromNx(e) for e in ordered_edges if all([n in path for n in e[:2]])]
                    predicates = [e.p for e in oe2]
                    #log.debug('\n' + pformat(oe2))
                    if predicates == coll: #in collapse:
                        to_remove.extend(zap(path, predicates, oe2, blob))
                    else:  # have to retain this branch to handle cases where the end predicate is duplicated
                        log.error('\n' + pformat(predicates) +
                                    '\n' + pformat(coll))
                        for preds in [coll]:
                            sublist_start = listIn(predicates, preds)
                            if sublist_start is not None:
                                i = sublist_start
                                j = i + len(preds)
                                npath = path[i:j + 1]  # + 1 to include final node
                                oe2 = oe2[i:j]
                                predicates = predicates[i:j]
                                to_remove.extend(zap(npath, predicates, oe2, blob))

    for r in to_remove:
        if r in blob['edges']:
            blob['edges'].remove(r)

    #log.debug('\n' + pformat(blob['edges']))
    return blob  # note that this is in place modification so sort of supruflous
コード例 #26
0
def separate_Arg(arg_structure):
    '''
    Split an Argument dict since there can be several
    Args which are not connected to each other in one file
    '''
    graph = nx.DiGraph(arg_structure)
    subtrees = []
    for nodes in nx.weakly_connected_components(graph):
        subgraph = graph.subgraph(nodes)
        subtree = nx.to_dict_of_lists(subgraph)
        subtrees.append(subtree)
    return subtrees
コード例 #27
0
def add_self_loops(graph):
    
    # firstly add self loops to all isolated nodes
    for cc in sorted(nx.weakly_connected_components(graph), key=len, reverse=True) :
        if len(cc) == 1 :
            node = next(iter(cc)) 
            graph.add_edge(node, node)
            
    # Then add a self loop to all nodes that have an in degree of 0
    for node in graph.nodes :
        if graph.in_degree(node) == 0 :
            graph.add_edge(node, node)
コード例 #28
0
def important_characteristics_of_graph(G):
    print("Eccentricity: ", nx.eccentricity(G))
    print("Diameter: ", nx.diameter(G))
    print("Radius: ", nx.radius(G))
    print("Preiphery: ", list(nx.periphery(G)))
    print("Center: ", list(nx.center(G)))

    weakly_component = [G.subgraph(c).copy() for c in sorted(nx.weakly_connected_components(G))]

    largest_wcc = max(weakly_component)

    print(weakly_component)
コード例 #29
0
def network_component(DiG, plot=False):
    weak_component = list(nx.weakly_connected_components(DiG))
    if plot:
        threshold = 500
        sm_dist = [len(sub) for sub in weak_component if len(sub) <= threshold]
        sns.distplot(sm_dist, rug=True)
        plt.xlabel("size")
        plt.ylabel("number")
        plt.title("weak components")
        plt.savefig('./figures/weak_components.png')
        plt.show()
    return DiG.subgraph(weak_component[0])
コード例 #30
0
 def collapse_proximity(self, cntr, **_):
     pg, rg = self.proximity, self.replying
     dirty = None
     for c in nx.weakly_connected_components(pg):
         p = None
         for m in sorted(m for m in c if not pg.in_degree(m)):
             if p:
                 rg.add_edge(p, m)
                 cntr.incr('p')
                 dirty = True
             p = m
     return dirty
コード例 #31
0
 def collapse_subject(self, cntr, **_):
     sg, rg = self.subject, self.replying
     dirty = None
     for c in nx.weakly_connected_components(sg):
         p = None
         for m in sorted(m for m in c if not sg.in_degree(m) and ':' in m):
             if p:
                 rg.add_edge(p, m)
                 cntr.incr('s')
                 dirty = True
             p = m
     return dirty
コード例 #32
0
def trn_stats(genes, trn, t_factors, version):
    LOGGER.info("Computing TRN statistics")
    nodes = sorted(trn.nodes_iter())
    node2id = {n: i for (i, n) in enumerate(nodes)}
    id2node = {i: n for (i, n) in enumerate(nodes)}
    (grn, node2id) = to_simple(trn.to_grn(), return_map=True)
    nodes = sorted(grn.nodes_iter())
    regulating = {node for (node, deg) in grn.out_degree_iter() if deg > 0}
    regulated = set(nodes) - regulating
    components = sorted(nx.weakly_connected_components(grn), key=len,
            reverse=True)
    data = dict()
    for (a, b) in itertools.product(("in", "out"), repeat=2):
        data["{a}_{b}_ass".format(a=a, b=b)] = nx.degree_assortativity_coefficient(grn, x=a, y=b)
    census = triadic_census(grn)
    forward = census["030T"]
    feedback = census["030C"]
    num_cycles = sum(1 for cyc in nx.simple_cycles(grn) if len(cyc) > 2)
    in_deg = [grn.in_degree(node) for node in regulated]
    out_deg = [grn.out_degree(node) for node in regulating]
    data["version"] = version,
    data["release"] = pd.to_datetime(RELEASE[version]),
    data["num_genes"] = len(genes),
    data["num_tf"] = len(t_factors),
    data["num_nodes"] = len(nodes),
    data["num_regulating"] = len(regulating),
    data["num_regulated"] = len(regulated),
    data["num_links"] = grn.size(),
    data["density"] = nx.density(grn),
    data["num_components"] = len(components),
    data["largest_component"] = len(components[0]),
    data["feed_forward"] = forward,
    data["feedback"] = feedback,
    data["fis_out"] = trn.out_degree(TranscriptionFactor[FIS_ID, version]),
    data["hns_out"] = trn.out_degree(TranscriptionFactor[HNS_ID, version]),
    data["cycles"] = num_cycles,
    data["regulated_in_deg"] = mean(in_deg),
    data["regulating_out_deg"] = mean(out_deg),
    data["hub_out_deg"] = max(out_deg)
    stats = pd.DataFrame(data, index=[1])
    in_deg = [grn.in_degree(node) for node in nodes]
    out_deg = [grn.out_degree(node) for node in nodes]
    bc = nx.betweenness_centrality(grn)
    bc = [bc[node] for node in nodes]
    dists = pd.DataFrame({
            "version": version,
            "release": [pd.to_datetime(RELEASE[version])] * len(nodes),
            "node": [id2node[node].unique_id for node in nodes],
            "regulated_in_degree": in_deg,
            "regulating_out_degree": out_deg,
            "betweenness": bc
        })
    return (stats, dists)
コード例 #33
0
def draw_clusters(graph, data, filename):
    print('Drawing clusters...')
    save_path = './plots/analyze_streaming_alg/{}'.format(filename)
    if not os.path.isdir(save_path):
        os.makedirs(save_path)

    for i, component in enumerate(nx.weakly_connected_components(graph)):
        subgraph = graph.subgraph(component)
        plt.title('Component {}'.format(i))
        nx.draw_shell(subgraph, with_labels=True)
        plt.savefig('{}/{}'.format(save_path, i))
        plt.clf()
コード例 #34
0
    def test_zero_d_to_molecule_graph(self):
        comp_graphs = [
            self.mol_structure.graph.subgraph(c)
            for c in nx.weakly_connected_components(self.mol_structure.graph)
        ]

        mol_graph = zero_d_graph_to_molecule_graph(self.mol_structure,
                                                   comp_graphs[0])

        self.assertEqual(mol_graph.get_connected_sites(0)[0].index, 1)
        self.assertEqual(mol_graph.get_connected_sites(1)[1].index, 2)
        self.assertEqual(mol_graph.molecule.num_sites, 3)
コード例 #35
0
    def _parse_swc(self, filename: Path):
        """Read one point per line. If ``ndims`` is 0, all values in one line
        are considered as the location of the point. If positive, only the
        first ``ndims`` are used. If negative, all but the last ``-ndims`` are
        used.
        """
        if "cube" in filename.name:
            return 0

        tree = parse_swc(
            filename,
            self.transform_file,
            resolution=[self.scale[i] for i in self.transpose],
            transpose=self.transpose,
        )

        assert len(list(nx.weakly_connected_components(tree))) == 1

        points = []

        for node, attrs in tree.nodes.items():
            if not self.ignore_human_nodes or attrs["human_placed"]:
                points.append(
                    Node(
                        id=node,
                        location=attrs["location"],
                        attrs=attrs,
                    ))

        human_edges = set()
        if self.ignore_human_nodes:
            for u, v in tree.edges:
                if u not in points or v not in points:
                    human_edges.add(Edge(u, v))
        edges = set(Edge(u, v) for (u, v) in tree.edges)
        if not self.directed:
            edges = edges | set(Edge(v, u) for u, v in tree.edges())
        self._add_points_to_source(points, edges - human_edges)

        return len(list(nx.weakly_connected_components(tree)))
コード例 #36
0
ファイル: rnadecomposer.py プロジェクト: smautner/GraphLearn
    def rooted_core_interface_pairs(self, root, thickness=None,  for_base=False,
                                        hash_bitmask=None,
                                      radius_list=[],
                                      thickness_list=None,
                                      node_filter=lambda x, y: True):
        """

        Parameters
        ----------
        root:
        thickness:
        args:

        Returns
        -------

        """

        ciplist = super(self.__class__, self).rooted_core_interface_pairs(root, thickness, for_base=for_base,
                                        hash_bitmask=hash_bitmask,
                                      radius_list=radius_list,
                                      thickness_list=thickness_list,
                                      node_filter=node_filter)



        # numbering shards if cip graphs not connected
        for cip in ciplist:
            if not nx.is_weakly_connected(cip.graph):
                comps = [list(node_list) for node_list in nx.weakly_connected_components(cip.graph)]
                comps.sort()

                for i, nodes in enumerate(comps):

                    for node in nodes:
                        cip.graph.node[node]['shard'] = i

        '''
        solve problem of single-ede-nodes in the core
        this may replace the need for fix_structure thing
        this is a little hard.. may fix later

        it isnt hard if i write this code in merge_core in ubergraphlearn

        for cip in ciplist:
            for n,d in cip.graph.nodes(data=True):
                if 'edge' in d and 'interface' not in d:
                    if 'interface' in cip.graph.node[ cip.graph.successors(n)[0]]:
                        #problem found
        '''

        return ciplist
コード例 #37
0
def group_features(df,
                   db_out,
                   max_rt_diff=5.0,
                   coeff_thres=0.7,
                   pvalue_thres=1.0,
                   method="pearson",
                   block=5000,
                   ncpus=None):

    conn = sqlite3.connect(db_out)
    cursor = conn.cursor()

    cursor.execute("DROP TABLE IF EXISTS groups")

    cursor.execute("""CREATE TABLE groups (
                   group_id INTEGER DEFAULT NULL,
                   peak_id_a TEXT DEFAULT NULL,
                   peak_id_b TEXT DEFAULT NULL,
                   degree_a INTEGER DEFAULT NULL,
                   degree_b INTEGER DEFAULT NULL,
                   r_value REAL DEFAULT NULL,
                   p_value REAL DEFAULT NULL,
                   rt_diff REAL DEFAULT NULL,
                   mz_diff REAL DEFAULT NULL,                 
                   PRIMARY KEY (peak_id_a, peak_id_b));""")

    df_coeffs = statistics.correlation_coefficients(df, max_rt_diff,
                                                    coeff_thres, pvalue_thres,
                                                    method, block, ncpus)
    graph = statistics.correlation_graphs(df_coeffs, df)
    sub_graphs = list(
        graph.subgraph(c) for c in nx.weakly_connected_components(graph))
    for i in range(len(sub_graphs)):
        sub_graphs[i].graph[
            "groupid"] = i + 1  # not stored in output - place holder
        sub_graph_edges = []
        # sort edges
        edges = sorted(sub_graphs[i].edges(data=True),
                       key=lambda e: (e[0], e[1]))
        for edge in edges:
            sub_graph_edges.append(
                (i + 1, str(edge[0]), str(edge[1]),
                 sub_graphs[i].degree(edge[0]), sub_graphs[i].degree(edge[1]),
                 round(float(edge[2]["rvalue"]), 2), float(edge[2]["pvalue"]),
                 float(edge[2]["rtdiff"]), float(edge[2]["mzdiff"])))
        cursor.executemany(
            """insert into groups (group_id, peak_id_a, peak_id_b, degree_a, degree_b,
                              r_value, p_value, rt_diff, mz_diff) values (?,?,?,?,?,?,?,?,?)""",
            sub_graph_edges)
    conn.commit()
    conn.close()
    return graph
コード例 #38
0
def trim_components(graph, min_edges=2, message=True):
    """
    Remove connected components less than a certain size (in number of edges) from a graph.

    Args:
        graph (nx.Graph): the networkx graph from which to remove small components
        min_edges (int, optional, default=2): the minimum number of edges required for a component to remain in the
            network; any component with FEWER edges will be removed.
        message (bool, optional, default=True): if True, prints a message indicating the number of components removed 
            from `graph`

    Returns:
        G (nx.Graph): a modified copy of the original graph with connected components smaller than `min_edges`
            removed
    """

    # Build weakly connected components -- there must be a path from A to B,
    # but not necessarily from B to A (this accounts for directed graphs)
    conn_comps = list(nx.weakly_connected_components(graph))

    # To have at least "x" edges, we need at least "x+1" nodes. So, we can
    # set a node count from the min edges
    min_nodes = min_edges + 1

    # Loop through the connected components (represented as node sets) to
    # count edges -- if we have less than the required number of nodes for
    # the required number of edges, remove the nodes that create that
    # component (thus eliminating that component)
    for cc in conn_comps:
        if len(cc) < min_nodes:
            graph.remove_nodes_from(cc)
        else:
            pass

    # If a printout of number of components removed is requested, count and
    # print here.
    if message:
        count_removed = sum([len(x) < min_nodes for x in conn_comps])
        count_message = " ".join(
            [
                str(count_removed),
                "of",
                str(len(conn_comps)),
                "were removed from the input graph",
            ]
        )
        print(count_message)
    else:
        pass

    # The graph was updated in the loop, so we can just return here
    return graph
コード例 #39
0
ファイル: claim.py プロジェクト: JoshC8C7/AFCProj
    def extract_subclaims(self):
        # Convert graphviz to networkx.
        G = nx.nx_pydot.from_pydot(graph_from_dot_data(self.graph.source)[0])
        claims_list = []
        subtrees = []

        # Find the subclaim roots - the verbs that the conjunction of implies the documents root.
        subtree_roots = list(p[0] for p in G.in_edges(
            nbunch=self.argID(self.doc[:])))  # Store the subclaim graph roots
        H = nx.subgraph_view(
            G, filter_node=(lambda n: n != self.argID(self.doc[:]))
        )  # Create a view without the overall text/main root.

        # After removing the root, should have 1+ connected components. If a cycle is detected when trying to find them,
        # remove the last edge that caused it (this rarely happens in practice).
        cycling = True
        while cycling:
            try:
                # Create the subclaim graphs - once detaching the whole-text root these are connected components
                subtrees = [
                    H.subgraph(c).copy()
                    for c in nx.weakly_connected_components(H)
                ]
            except nx.HasACycle:
                G.remove_edge(nx.find_cycle(G)[-1])
            else:
                cycling = False

        # Create a Claim object for each component.
        for subclaim in subtrees:

            # take all roots that form this subclaim (could be multiple if they're connected - this makes the 'tree' not
            # strictly a 'tree').
            rel_roots = list(filter(lambda x: x in subclaim, subtree_roots))
            removed_edges, removed_nodes = [], []

            # Outbound edges from roots are nearly always erroneous, so they are removed as soon as possible:
            # Also remove any cases of nodes that have entirely dashed input.
            for j in rel_roots:
                for i in subclaim.out_edges(j):
                    removed_edges.append((i[0], i[1]))

                for i2 in subclaim.nodes():
                    if len(subclaim.in_edges(i2)) and all(
                            x[2].get('style', '') == 'dotted'
                            for x in subclaim.in_edges(i2, data=True)):
                        removed_nodes.append(i2)
            subclaim.remove_nodes_from(removed_nodes)
            subclaim.remove_edges_from(removed_edges)
            claims_list.append(Claim(self, subclaim, rel_roots))

        return claims_list
コード例 #40
0
def graph_stats(g: nx.DiGraph) -> GraphStats:
    strongly_connected_components: List[Set[int]] = list(
        nx.strongly_connected_components(g))
    weakly_connected_components: List[Set[int]] = list(
        nx.weakly_connected_components(g))
    return GraphStats(
        g.number_of_nodes(),
        g.number_of_edges(),
        len(strongly_connected_components),
        max([len(comp) for comp in strongly_connected_components]),
        len(weakly_connected_components),
        max([len(comp) for comp in weakly_connected_components]),
    )
コード例 #41
0
    def remove_disconnected(self):
        '''
        importing networks leads to possible data fragments and isolated nodes or graphs,
        these fragments can be due to errors or leftovers in the original file or bugs on import
        some class methods can't equate these fragments and metrics can perform diferently in their presence
        this method removes all graphs and nodes isolated from the main branch

        :return: overides self.mnetwork removing disconected nodes and subgraphs
        '''
        for subgraph in list(nx.weakly_connected_components(self.mnetwork)):
            listed_nodes = list(subgraph)
            if len(listed_nodes) < (self.mnetwork.number_of_edges() * 0.1):
                self.mnetwork.remove_nodes_from(listed_nodes)
コード例 #42
0
def split(variable_graph: nx.DiGraph) -> Generator[Tuple[List, List], None, None]:
    for factor in nx.weakly_connected_components(variable_graph):
        sources = []
        targets = []
        for var in factor:
            if variable_graph.in_degree(var) == 0:
                sources.append(var)
            else:
                targets.append(var)

        assert len(sources) >= 1, "Expected at least one source"
        assert len(targets) >= 1, "Expected at least one target"
        yield sources, targets
コード例 #43
0
    def test_zero_d_to_molecule_graph(self):
        comp_graphs = [self.mol_structure.graph.subgraph(c) for c in
                       nx.weakly_connected_components(self.mol_structure.graph)]

        mol_graph = zero_d_graph_to_molecule_graph(self.mol_structure,
                                                   comp_graphs[0])

        self.assertEqual(mol_graph.get_connected_sites(0)[0].index, 1)
        self.assertEqual(mol_graph.get_connected_sites(1)[1].index, 2)
        self.assertEqual(mol_graph.molecule.num_sites, 3)

        # test catching non zero dimensionality graphs
        comp_graphs = [self.graphite.graph.subgraph(c) for c in
                       nx.weakly_connected_components(self.graphite.graph)]
        self.assertRaises(ValueError, zero_d_graph_to_molecule_graph,
                          self.graphite, comp_graphs[0])

        # test for a troublesome structure
        s = loadfn(os.path.join(test_dir, "PH7CN3O3F.json.gz"))
        bs = CrystalNN().get_bonded_structure(s)
        comp_graphs = [bs.graph.subgraph(c) for c in
                       nx.weakly_connected_components(bs.graph)]
        mol_graph = zero_d_graph_to_molecule_graph(bs, comp_graphs[0])
        self.assertEqual(mol_graph.molecule.num_sites, 12)
コード例 #44
0
ファイル: graph_info.py プロジェクト: HongxuChen/CE7490_1
 def _connected_component(self):
     if self.graph.is_directed():
         edges_in_wcc = set()
         nodes_in_wcc = set()
         wcc_counter = 0
         for wcc in nx.weakly_connected_components(self.graph):
             wcc_counter += 1
             print(type(wcc))
             nodes_in_wcc.add(wcc.nodes())
             edges_in_wcc.add(wcc.edges())
     else:  # undirected
         scc_counter = 0
         for scc in nx.strongly_connected_components(self.graph):
             scc_counter += 0
             print(type(scc))
コード例 #45
0
 def save(self, filename):
     # output the biggest weakly connected components
     vertices = list(sorted(nx.weakly_connected_components(self.graph),
                            key=len, reverse=True))[0]
     output_graph = nx.DiGraph()
     output_graph.add_nodes_from([(node, data)
                                  for node, data
                                  in self.graph.nodes(data=True)
                                  if node in vertices])
     output_graph.add_edges_from([(src, dest, data)
                                  for src, dest, data
                                  in self.graph.edges(data=True)
                                  if src in vertices and dest in vertices])
     nx.drawing.nx_pydot.write_dot(output_graph, filename)
     return self
コード例 #46
0
ファイル: utils.py プロジェクト: gboeing/osmnx
def get_largest_component(G, strongly=False):
    """
    Return a subgraph of the largest weakly or strongly connected component
    from a directed graph.

    Parameters
    ----------
    G : networkx multidigraph
    strongly : bool
        if True, return the largest strongly instead of weakly connected
        component

    Returns
    -------
    G : networkx multidigraph
        the largest connected component subgraph from the original graph
    """

    start_time = time.time()
    original_len = len(list(G.nodes()))

    if strongly:
        # if the graph is not connected retain only the largest strongly connected component
        if not nx.is_strongly_connected(G):
            
            # get all the strongly connected components in graph then identify the largest
            sccs = nx.strongly_connected_components(G)
            largest_scc = max(sccs, key=len)
            G = induce_subgraph(G, largest_scc)
            
            msg = ('Graph was not connected, retained only the largest strongly '
                   'connected component ({:,} of {:,} total nodes) in {:.2f} seconds')
            log(msg.format(len(list(G.nodes())), original_len, time.time()-start_time))
    else:
        # if the graph is not connected retain only the largest weakly connected component
        if not nx.is_weakly_connected(G):
            
            # get all the weakly connected components in graph then identify the largest
            wccs = nx.weakly_connected_components(G)
            largest_wcc = max(wccs, key=len)
            G = induce_subgraph(G, largest_wcc)
            
            msg = ('Graph was not connected, retained only the largest weakly '
                   'connected component ({:,} of {:,} total nodes) in {:.2f} seconds')
            log(msg.format(len(list(G.nodes())), original_len, time.time()-start_time))

    return G
コード例 #47
0
ファイル: joiner.py プロジェクト: SergiosLen/TwitterMining
def create_node_conncomp_graph(G,layer1,layer2,layer3):
    # print layer1
    npartition = list(nx.weakly_connected_components(G))
    print len(npartition)
    # G=nx.Graph(G)
    layers={'layer1':layer1,'layer2':layer2,'layer3':layer3}
    broken_partition={}
    for i,v in enumerate(npartition):
        vs=set(v)
        for ii,vv in layers.items():
            papa=vs.intersection(set(vv))
            if len(papa)==len(v):
                broken_partition['a_%i_%s_s' %(i,ii)]=v
            elif len(papa)>0:
                broken_partition['b_%i_%s' %(i,ii)]=list(papa)
                vs=vs-set(vv)
    # print rbroken_partition
    broken_graph=nx.Graph()
    rbroken_partition=dict()
    colors=[name for name,hex in matplotlib.colors.cnames.iteritems()]
    colors=list(set(colors)-set(['red','blue','green']))
    cl=dict()
    for i,v in broken_partition.items():
        name=i.split('_')
        for ii in v:
            # print ii
            rbroken_partition[ii]=i
        if name[-1]=='s':
            cl[name[1]]=colors.pop()
        elif name[0]=='b' and not cl.has_key(name[1]):
            cl[name[1]]=colors.pop()
    # print rbroken_partition
    for i,v in rbroken_partition.items():
        name=v.split('_')
        broken_graph.add_node(v,color=cl[name[1]])
        edg=G[i]
        # print edg
        for j in edg:
            if j not in broken_partition[v]:
                # print j
                if not broken_graph.has_edge(v,rbroken_partition[j]):
                    broken_graph.add_edge(v,rbroken_partition[j])
    
    return broken_graph,broken_partition,npartition
コード例 #48
0
def resolve_connectivity_issue(test_graph):
    weakly_connected_components = networkx.weakly_connected_components(test_graph)
    wcc_list = list()

    # get components sorted  based on the length
    for component in weakly_connected_components:
        index = 0
        for wcc in wcc_list:
            if len(component) < len(wcc):
                break
            index += 1
        wcc_list.insert(index, component)

    print "sorted list"
    for wcc in wcc_list:
        print 'component length' + str(len(wcc))

    for i in range(len(wcc_list)-1):
        for j in range(i+1, len(wcc_list)):
            connect_weakly_connected_component(wcc_list[i], wcc_list[j], test_graph)
コード例 #49
0
ファイル: RNA.py プロジェクト: antworteffekt/GraphLearn
    def rooted_core_interface_pairs(self, root, thickness=None, **args):
        '''

        Args:
            root: int
            thickness:  
            **args:

        Returns:

        '''

        ciplist = super(self.__class__, self).rooted_core_interface_pairs(root, thickness, **args)


        #numbering shards if cip graphs not connected
        for cip in ciplist:
            if not nx.is_weakly_connected(cip.graph):
                comps = [list(node_list) for node_list in nx.weakly_connected_components(cip.graph)]
                comps.sort()

                for i, nodes in enumerate(comps):

                    for node in nodes:
                        cip.graph.node[node]['shard'] = i

        '''
        solve problem of single-ede-nodes in the core
        this may replace the need for fix_structure thing
        this is a little hard.. may fix later

        it isnt hard if i write this code in merge_core in ubergraphlearn

        for cip in ciplist:
            for n,d in cip.graph.nodes(data=True):
                if 'edge' in d and 'interface' not in d:
                    if 'interface' in cip.graph.node[ cip.graph.successors(n)[0]]:
                        #problem found
        '''

        return ciplist
コード例 #50
0
def find_len_1_control_kernals(deterministic_transition_graph, nodes_list, attractor_ID):
    """uses the deterministic_transition_graph and specified attractor to find 
    all single control nodes if they exist"""

    subgraphs = [g for g in nx.weakly_connected_components(deterministic_transition_graph)]
    # index_of_largest_subgraph = max(enumerate(all_subgraph_sets), key = lambda tup: len(tup[1]))[0]

    all_subgraph_sets = []
    all_but_attractor_subgraph = []
    for sg in subgraphs:
        if attractor_ID not in sg:
            all_state_sets = []
            for nID in sg:
                all_state_sets.append(set(time_evol.decimal_to_binary(nodes_list,nID).items()))
            all_subgraph_sets.append(all_state_sets)
            all_but_attractor_subgraph.append(all_state_sets)
        else:
            all_state_sets = []
            for nID in sg:
                all_state_sets.append(set(time_evol.decimal_to_binary(nodes_list,nID).items()))
            all_subgraph_sets.append(all_state_sets)

    state_0_list = [0 for i in range(len(nodes_list))]
    state_1_list = [1 for i in range(len(nodes_list))]

    possible_states = zip(nodes_list,state_0_list) + zip(nodes_list,state_1_list) # list of (node,state)

    possible_states_pared = copy.deepcopy(possible_states)

    for sg in all_but_attractor_subgraph:

        for state_set in sg:

            for state in possible_states:

                if (state in state_set) and (state in possible_states_pared):

                    possible_states_pared.remove(state)

    return possible_states_pared
コード例 #51
0
ファイル: time_evol.py プロジェクト: SES591/cortical_network
def main():
    print "time_evol module is the main code."
    edge_file = '../data/inputs/edges-init.dat' 
    # edge_file = '../data/inputs/edges_1-init.dat' 
    node_file = '../data/inputs/ant-nodes-init.dat'
    
    G = inet.read_network_from_file(edge_file, node_file)
    nodes_list = G.nodes() # print graph nodes without states. Could also do nx.nodes(G)

    # print G.nodes()
    initial_state = nx.get_node_attributes(G,'state')
    # print initial_state #OrderedDict(sorted(initial_state.items(), key=lambda t: t[0]))

    #####

    # timeseriesdata = time_series_all(G, nodes_list, 10)#, timesteps=20)

    # network_ID = 1 #this is the ID of the init state
    # biStates = decimal_to_binary(nodes_list, network_ID)
    # print 'initial state', biStates
    # for node in G.nodes():
    #     print node, timeseriesdata[node][1]

    G_transition_graph = net_state_transition(G, nodes_list) 
    
    ####################################################
    #### calculate the max number of steps to get to any given attractor in my system
    # target_from_source_dict = nx.single_source_shortest_path(G_transition_graph.to_undirected(), 43)
    # print max([len(target_from_source_dict[i]) for i in target_from_source_dict])
    # exit()
    ####################################################
    # nx.draw(G_transition_graph)
    # plt.show()

    attractors = find_attractor(G_transition_graph) # why does this change if I update G?

    print attractors.keys()
    # print nx.number_weakly_connected_components(G_transition_graph)
    # print [i for i in nx.weakly_connected_components(G_transition_graph)]
    print [len(i) for i in nx.weakly_connected_components(G_transition_graph)]
コード例 #52
0
ファイル: post.py プロジェクト: parthdoshi/seniordesign
def main():
    flows = load_flows(RESULTS_FILE)
    zips = [n for n in flows if isinstance(n, int)]
    nxg, dest = guido_to_nx(flows)
    comps = nx.weakly_connected_components(nxg)
    comps.sort(key=len)
    print "%d weakly connected component%s" % (len(comps),
                                               "s" if len(comps) > 1 else "")
    congestion = {}
    for z in zips:
        congs = []
        try:
            for path in get_paths(nxg, z, dest):
                congs.append(get_congestion(nxg, path))
        except nx.NetworkXNoPath:
            print z
            continue
            #raise ValueError("No path from %d to %r" % (z, dest))
        congestion[z] = sum(congs) / len(congs)
    with open(OUTPUT, 'w') as f:
        for z, c in congestion.items():
            f.write('%d,%f\n' % (z, c))
コード例 #53
0
def svnet_stats(filename,selfloops = False):
    
    
    svnet = filename + 'SVGraph.pkl'
    svnet = open(svnet,'r')
    svnet = load(svnet)
    W = filename + 'Graph.pkl'
    W = open(W,'r')
    W = load(W)
    
    try:
        W = nx.to_numpy_matrix(W)
    except AttributeError:
        W = W.todense()
    
    stats = dict()

    comps = nx.weakly_connected_components(svnet)
    stats ['svnet: largest connected component:'] = len(comps[0])
    stats ['# of nodes in svnet'] = len(svnet)
    stats ['# of valid links (sl excl.)'] = len(svnet.edges()) - len(svnet.selfloop_edges())
    stats ['volume of the valid network'] = svnet.size(weight = 'weight')
    A = W > 0.
    stats ['# of links (sl excl.)'] = A.sum() - np.trace(A)
    stats ['volume of the original network'] = W.sum()
    
    if selfloops is True:

        stats ['# of valid self-links'] = len(svnet.selfloop_edges())
        stats ['# of self-links '] = np.trace(A)
        stats ['volume in selfloops (original network)'] = np.trace(W)
        W = nx.to_numpy_matrix(svnet)
        stats['volume in selfloops (valid network)'] = np.trace(W)

    stats = stats.items()
    stats = np.array(stats,dtype = [('stat','S50'),('value',np.float32)])
    np.savetxt(filename +'.svnetstats',stats,fmt = ['%10s','%10.10f'])
    
    return stats
コード例 #54
0
ファイル: eval.py プロジェクト: jim-bo/scafathon
def _dag_flip(RG, TG):
    ''' flip to make most consistant '''

    # check it.
    _graph_check(RG)
    _graph_check(TG)

    # make set of reference.
    rset = set(RG.edges())

    # loop over each component.
    NG = nx.DiGraph()
    for comp in nx.weakly_connected_components(TG):

        # turn to subgraph.
        subg = TG.subgraph(comp)

        # make sets.
        tset1 = set(subg.edges())
        tset2 = set([(e1, e0) for e0, e1 in tset1])

        # check version.
        s1 = len(rset.intersection(tset1))
        s2 = len(rset.intersection(tset2))

        # add to nodes to new graph.
        for n in subg.nodes():
            NG.add_node(n, subg.node[n])

        # add the best matching orientation.
        for e0, e1 in subg.edges():
            if s1 >= s2:
                NG.add_edge(e0, e1, subg[e0][e1])
            else:
                NG.add_edge(e1, e0, subg[e0][e1])

    # return it.
    return NG
コード例 #55
0
def get_resilience_fraction(g, steps = 1000, mode = 'in'):
	"""docstring for get_resilience_fraction"""
	n = float(g.order())
	h = g.copy()
	step_width = max(1, n / steps)
	if mode == 'in':
		sorted_nodes = sorted(g.in_degree().items(), key = lambda x: x[1])
	else:
		sorted_nodes = sorted(g.out_degree().items(), key = lambda x: x[1])
	sorted_node_index = map(lambda n: n[0], sorted_nodes)
	fraction = [1]
	while len(sorted_node_index):
		rip = []
		while len(rip) < step_width: 
			if len(sorted_node_index):
				rip.append(sorted_node_index.pop())
			else:
				break
		h.remove_nodes_from(rip)
		ws = nx.weakly_connected_components(h)
		w = ws[0].__len__() if ws else 0
		fraction.append(w / n)
	return(dict(zip(range(len(fraction)), fraction)))
コード例 #56
0
def collect_comps(G, strongly, op, path):
    if strongly:
        cc_gen = nx.strongly_connected_components(G)
        ty = 'S'
    else:
        cc_gen = nx.weakly_connected_components(G)
        ty = 'W'
    if op == 1:
        ex.collect_alt_views(ex.gen_view(cc_gen), path + "%sCCsXCountView.txt" % ty, \
                             comments= "Vertex from %sCC; Count of vertex in %sCC" % (ty,ty))
    elif op == 2:
        # write raw trpl file of only vert in giant comp
        giantcc = cull_comps(G.copy(), cc_gen, True)
        fn = 'txTripletsCounts%sGiantOnly.txt' % ty
        print "Writing %s" % fn
        nx.write_weighted_edgelist(giantcc,path + fn)
        nx.write_weighted_edgelist(giantcc,'../' + fn + '.gz')
    elif op == 3:
        # write raw trpl file of only vert not in giant comp
        giantcc = cull_comps(G.copy(), cc_gen, False)
        fn = 'txTripletsCountsNo%sGiant.txt' % ty
        nx.write_weighted_edgelist(giantcc,path + fn)
    return None
コード例 #57
0
    def rooted_core_interface_pairs(self, root,
                                    thickness_list=None,
                                    for_base=False,
                                    radius_list=[],
                                    base_thickness_list=False):

        """
        Parameters
        ----------
        root:
        thickness:
        args:

        Returns
        -------
        """

        ciplist = super(self.__class__, self).rooted_core_interface_pairs(root,
                                        thickness_list=thickness_list,
                                        for_base=for_base,
                                        radius_list=radius_list,
                                        base_thickness_list=base_thickness_list)



        if not for_base:
            # numbering shards if cip graphs not connected
            for cip in ciplist:
                if not nx.is_weakly_connected(cip.graph):
                    comps = [list(node_list) for node_list in nx.weakly_connected_components(cip.graph)]
                    comps.sort()
                    for i, nodes in enumerate(comps):
                        for node in nodes:
                            cip.graph.node[node]['shard'] = i


        return ciplist
コード例 #58
0
def stats(grn, version, description):
    nodes = sorted(grn.nodes_iter())
    regulating = {node for (node, deg) in grn.out_degree_iter() if deg > 0}
    regulated = set(nodes) - regulating
    components = sorted(nx.weakly_connected_components(grn), key=len,
            reverse=True)
    data = dict()
    census = triadic_census(grn)
    forward = census["030T"]
    feedback = census["030C"]
    cycles = list(nx.simple_cycles(grn))
    in_deg = [grn.in_degree(node) for node in regulated]
    out_deg = [grn.out_degree(node) for node in regulating]
    data["version"] = version
    data["num_components"] = len(components)
    data["largest_component"] = len(components[0])
    data["feed_forward"] = forward
    data["feedback"] = feedback
    data["cycles"] = len(cycles)
    data["regulated_in_deg"] = np.mean(in_deg)
    data["regulating_out_deg"] = np.mean(out_deg)
    data["null_model"] = description
    stats = pd.DataFrame(data, index=[1])
    return stats
コード例 #59
0
ファイル: scaffold.py プロジェクト: bsiranosian/brown-compbio
def create_scaffold(contigs, mates):
	exact_matches1 = [[] for a in range(len(mates))]
	exact_matches2 = [[] for a in range(len(mates))]
	d= 0
	for i in range(len(mates)):
		mate = mates[i]
		mate1=mate[0]
		mate2=mate[2]
		#print d
		d+=1
		for j in range(len(contigs)):
			contig = contigs[j]
			allowed_mismatches = int(math.ceil(len(mate1)*0.02))
			#match using a regex with a certain number of allowed mismatches
			regex1 = '(?:' + mate1 + '){s<='+str(allowed_mismatches)+'}'
			regex2 = '(?:' + mate2 + '){s<='+str(allowed_mismatches)+'}'

			a = regex.search(regex1, contig)
			b = regex.search(regex2, contig)
			if not (a != None and b !=None):
				if a != None:
					exact_matches1[i] = [j, a.start()]
				if b != None:
					exact_matches2[i] = [j, b.start()]

	# find set where each mate maps to a contig
	initial_set = []
	for j in range(len(exact_matches1)):
	    if (exact_matches1[j] != [] and exact_matches2[j] != []):
	    	initial_set.append([mates[j], exact_matches1[j], exact_matches2[j]])

	#compute a matrix with the number of interactions between two contigs
	contig_mat = [[0 for l in range(len(contigs))] for l in range(len(contigs))]
	for i in range(len(initial_set)):
		y=initial_set[i][1][0]
		x=initial_set[i][2][0]
		contig_mat[x][y] += 1

	num_contigs = len(contigs)/2
	G = nx.DiGraph()
	#create a graph to represent the paths through the contigs
	for i in range(len(contigs)):
		for j in range(len(contigs)):
			num = contig_mat[i][j]
			if num >=2:
				#check to make sure we're not going to create a small loop
				if (j,i) in G.edges():
					 num1=G.edge[j][i]['num']
					 if num > num1:
					 	# if we are going to make a loop, but it can be improved, remove the old edge
					 	G.add_edge(i,j,num=num)
					 	G.remove_edge(j,i)
				else:
					G.add_edge(i, j, num=num)

	#components of graph are the paths of the assembly
	component_graphs = nx.weakly_connected_component_subgraphs(G)
	components = nx.weakly_connected_components(G)
	#each will be repeated with normal and reverse orientation. 
	component_matches = []
	for i in range(len(components)):
		comp1 = components[i]
		for j in range(len(components)):
			comp2 = components[j]
			if component_match(comp1, comp2, num_contigs):
				component_matches.append([i,j])

	#pick unique components
	unique_components = []
	for i in component_matches:
		if i[::-1] not in unique_components:
			unique_components.append(i)

	#fid paths for unique components
	unique_component_paths= []
	for i in unique_components:
		unique_component_paths.append([extract_path(component_graphs[i[0]]), extract_path(component_graphs[i[1]])])

	#report just one of the unique component paths
	paths = [i[0] for i in unique_component_paths]
	#change numbers greater than num_contigs to negative representation
	# simply for nicer output
	for i in range(len(paths)):
		for j in range(len(paths[i])):
			if paths[i][j] >= num_contigs:
				paths[i][j] = (paths[i][j]-num_contigs) * -1

	#return the paths as our supercontigs!
	return paths
コード例 #60
0
ファイル: connected.py プロジェクト: fxia22/HINGE
#print nx.is_directed_acyclic_graph(g)
#print list(nx.simple_cycles(g))
degree_sequence=sorted(nx.degree(g).values(),reverse=True)
print Counter(degree_sequence)

#print nx.diameter(g)

def rev(string):
    if string[-1] == '\'':
        return string[:-1]
    else:
        return string+'\''

#for edge in g.edges():
#    g.add_edge(rev(edge[1]), rev(edge[0]))
    #print edge
    #print rev(edge[1]), rev(edge[0])

print nx.info(g)
print [len(item) for item in nx.weakly_connected_components(g)]


nx.write_graphml(g, filename.split('.')[0]+'.graphml')

with open(sys.argv[2],'w') as f:
    for edge in nx.dfs_edges(g):
        f.write('{} {}\n'.format(edge[0],edge[1]))

f.close()