Beispiel #1
0
def fuzz_network(G_orig, threshold, b, edge_frac=1.0, nonedge_mult=5.0):
    G = G_orig.copy()
    n = len(G.nodes())
    H = Graph()
    H.add_nodes_from(range(n))
    pairs = n * (n - 1) / 2
    actual_edges = len(G.edges())
    edges = int(edge_frac * actual_edges)
    nonedges = int(edges * nonedge_mult)

    a = b / nonedge_mult

    # though these distributions are normalized to one, by selecting the appropriate number of edges
    # and nonedges, we make these 'distributions' correct
    edge_probs = np.random.beta(a + 1, b, edges)
    nonedge_probs = np.random.beta(a, b + 1, nonedges)

    # picking the right number of edges from the appropriate list
    edge_list = G.edges()
    nonedge_list = list(non_edges(G))
    shuffle(edge_list)
    shuffle(nonedge_list)
    for i in range(len(edge_probs)):
        G[edge_list[i][0]][edge_list[i][1]]["weight"] = edge_probs[i]
        if edge_probs[i] > threshold:
            H.add_edge(edge_list[i][0], edge_list[i][1])
    for i in range(len(nonedge_probs)):
        G.add_edge(nonedge_list[i][0], nonedge_list[i][1], weight=nonedge_probs[i])
        if nonedge_probs[i] > threshold:
            H.add_edge(nonedge_list[i][0], nonedge_list[i][1])

    return G, H
Beispiel #2
0
def network_UKGDS(filename,header=28):
	"""
	Load Excel file with UKGDS data format and build dict array of bus coordinates
	and graph structure suitable for plotting with the networkx module.
	"""
	from numpy import array,where
	from pandas import ExcelFile
	from networkx import Graph

	data = ExcelFile(filename)
	bus = data.parse("Buses",header=header)
	branch = data.parse("Branches",header=header)
	pos = {}
	for node in range(len(bus["BNU"])):
		pos.update({node:array([bus["BXC"][node],bus["BYC"][node]])})
	net = []
	for k in range(len(branch["CFB"])):
		von = where(bus["BNU"]==branch["CFB"][k])[0][0]
		zu  = where(bus["BNU"]==branch["CTB"][k])[0][0]
		net.append([von,zu])
	nodes = set([n1 for n1,n2 in net] + [n2 for n1,n2 in net])
	G = Graph()
	for node in nodes:
		G.add_node(node)
	for edge in net:
		G.add_edge(edge[0],edge[1])
	return G,pos
Beispiel #3
0
def summarize(text, sentence_count=5, language='english'):
    processor = LanguageProcessor(language)

    sentence_list = processor.split_sentences(text)
    wordset_list = map(processor.extract_significant_words, sentence_list)
    stemsets = [
        {processor.stem(word) for word in wordset}
        for wordset in wordset_list
    ]

    graph = Graph()
    pairs = combinations(enumerate(stemsets), 2)
    for (index_a, stems_a), (index_b, stems_b) in pairs:
        if stems_a and stems_b:
            similarity = 1 - jaccard(stems_a, stems_b)
            if similarity > 0:
                graph.add_edge(index_a, index_b, weight=similarity)

    ranked_sentence_indexes = list(pagerank(graph).items())
    if ranked_sentence_indexes:
        sentences_by_rank = sorted(
            ranked_sentence_indexes, key=itemgetter(1), reverse=True)
        best_sentences = map(itemgetter(0), sentences_by_rank[:sentence_count])
        best_sentences_in_order = sorted(best_sentences)
    else:
        best_sentences_in_order = range(min(sentence_count, len(sentence_list)))

    return ' '.join(sentence_list[index] for index in best_sentences_in_order)
 def generate_small_world_graph(self):
     max_edges = self.NODE_COUNT*(self.NODE_COUNT-1)/2
     if self.EDGE_COUNT > max_edges:
         return complete_graph(self.NODE_COUNT)
     graph = Graph()
     graph.add_nodes_from(range(self.NODE_COUNT))
     edges = performer.edge_indices.flatten()
     probabilities = performer.probabilities.flatten()
     for trial in range(len(edges)-9):
         edge_index = numpy.random.choice(edges, p=probabilities)
         source, destination = self.edge_nodes(edge_index)
         graph.add_edge(source, destination, length = self.link_length(source, destination),
                        weight = self.edge_weight(source, destination))
         probabilities[edge_index] = 0
         probabilities /= sum(probabilities)
         if max(graph.degree().values()) > self.DEGREE_MAX:
             graph.remove_edge(source, destination)
         if graph.number_of_edges() > self.EDGE_COUNT:
             victim = random.choice(graph.edges())
             graph.remove_edge(victim[0], victim[1])
         if self.constraints_satisfied(graph):
             print 'performer.generate_small_world_graph:',
             print self.BENCHMARK, self.NODE_COUNT, self.EDGE_COUNT, trial
             self.process_graph(graph)
             return graph
Beispiel #5
0
def make_prim_mst(G, generator=None):
	if generator is None:
		mst = Graph()
	else:
		mst = generator()       
	#priorityQ is a list of list (the reverse of the edge tuple with the weight in the front)
	priorityQ = []
	firstNode = G.nodes()[0]
	mst.add_node(firstNode)
	for edge in G.edges_iter(firstNode, data=True):
		if len(edge) != 3 or edge[2] is None:
			raise ValueError, "make_prim_mst accepts a weighted graph only (with numerical weights)"
		heappush(priorityQ, (edge[2], edge))

	while len(mst.edges()) < (G.order()-1):
		w, minEdge = heappop(priorityQ)
		if len(minEdge) != 3 or minEdge[2] is None:
			raise ValueError, "make_prim_mst accepts a weighted graph only (with numerical weights)"
		v1, v2, w = minEdge
		if v1 not in mst:
			for edge in G.edges_iter(v1, data=True):
				if edge == minEdge:
					continue
				heappush(priorityQ, (edge[2], edge))
		elif v2 not in mst:
			for edge in G.edges_iter(v2, data=True):
				if edge == minEdge:
					continue
				heappush(priorityQ, (edge[2], edge))
		else:
			# non-crossing edge 
			continue 
		mst.add_edge(minEdge[0],minEdge[1],minEdge[2])
	return mst
Beispiel #6
0
def network(qmatrix): 
  """ Creates networkx graph object from a :class:`QMatrix` object.
  
      Vertices have an "open" attribute to indicate whether they are open or shut. Edges have a
      "k+" and "k-" attribute containing the transition rates for the node with smaller index to
      the node with larger index, and vice-versa. 

      :param qmatrix: 
        A :class:`QMatrix` instance for which to construct a graph
      :return: A `networkx.Graph` object which contains all the information held by the qmatrix
               object, but in a graph format.
  """
  from networkx import Graph

  graph = Graph()
  for i in range(qmatrix.nopen): graph.add_node(i, open=True)
  for j in range(qmatrix.nshut): graph.add_node(i+j, open=False)

  for i in range(qmatrix.matrix.shape[0]):
    for j in range(i, qmatrix.matrix.shape[1]):
      if abs(qmatrix.matrix[i,j]) > 1e-8:
        graph.add_edge(i, j)
        graph[i][j]['k+'] = qmatrix.matrix[i, j]
        graph[i][j]['k-'] = qmatrix.matrix[j, i]
  return graph
Beispiel #7
0
def draw_network(bus,branch,bus_names=None,coordinates=None,ax=None):
	"""Generate networkx Graph object and draw network diagram
	It is assumed that bus and branch ordering corresponds to PyPower format
	"""
	from networkx import Graph,draw
	from matplotlib.pyplot import show
	if isinstance(coordinates,np.ndarray):
		pos = {}
		if coordinates.shape[0]==2:
			coordinates = coordinates.T
		for node in range(len(bus)):
			pos.update({node:coordinates[node,:]})
	else:
		pos = None
	net = []
	for k in range(len(branch)):
		von = np.where(bus[:,BUS_I]==branch[k,F_BUS])[0][0]
		zu  = np.where(bus[:,BUS_I]==branch[k,T_BUS])[0][0]
		net.append([von,zu])
	nodes = set([n1 for n1,n2 in net] + [n2 for n1,n2 in net])
	G = Graph()
	for node in nodes:
		G.add_node(node)
	for edge in net:
		G.add_edge(edge[0],edge[1])
	draw(G,pos,ax=ax)
	show()
Beispiel #8
0
def build_graph(ways):
    graph = Graph()
    for way, tags in ways:
        for segment in nwise(way.coords):
            weight = length(segment) * coef(tags)
            graph.add_edge(segment[0], segment[1],
                           weight=weight)
    return graph
def make_graph(points, neighbor_max_dist=0.01):
    graph = Graph()
    graph.add_nodes_from(range(len(points)))
    for i in xrange(len(points)):
        for j in xrange(i+1, len(points)):
            if euclidian_3d_dist(points[i], points[j])<neighbor_max_dist:
                graph.add_edge(i,j)
    return graph
Beispiel #10
0
def find_rings(atoms):
    graph = Graph()
    for i, atom1 in enumerate(atoms):
        for atom2 in atoms[i+1:]:
            if is_bound(atom1.cart, atom1.element, atom2.cart, atom2.element):
                graph.add_edge(atom1.name, atom2.name)
    ring_list = cycle_basis(graph)
    return ring_list
Beispiel #11
0
def polygon_skeleton(polygon, density=10):
    """ Given a buffer polygon, return a skeleton graph.
    """
    skeleton = Graph()
    points = []
    
    for ring in polygon_rings(polygon):
        points.extend(densify_line(list(ring.coords), density))
    
    if len(points) <= 4:
        # don't bother with this one
        return skeleton
    
    print >> stderr, ' ', len(points), 'perimeter points',
    
    rbox = '\n'.join( ['2', str(len(points))] + ['%.2f %.2f' % (x, y) for (x, y) in points] + [''] )
    
    qvoronoi = Popen('qvoronoi o'.split(), stdin=PIPE, stdout=PIPE)
    output, error = qvoronoi.communicate(rbox)
    voronoi_lines = output.splitlines()
    
    if qvoronoi.returncode:
        raise _QHullFailure('Failed with code %s' % qvoronoi.returncode)
    
    vert_count, poly_count = map(int, voronoi_lines[1].split()[:2])
    
    for (index, line) in enumerate(voronoi_lines[2:2+vert_count]):
        point = Point(*map(float, line.split()[:2]))
        if point.within(polygon):
            skeleton.add_node(index, dict(point=point))
    
    for line in voronoi_lines[2+vert_count:2+vert_count+poly_count]:
        indexes = map(int, line.split()[1:])
        for (v, w) in zip(indexes, indexes[1:] + indexes[:1]):
            if v not in skeleton.node or w not in skeleton.node:
                continue
            v1, v2 = skeleton.node[v]['point'], skeleton.node[w]['point']
            line = LineString([(v1.x, v1.y), (v2.x, v2.y)])
            if line.within(polygon):
                skeleton.add_edge(v, w, dict(line=line, length=line.length))
    
    removing = True
    
    while removing:
        removing = False
    
        for index in skeleton.nodes():
            if skeleton.degree(index) == 1:
                depth = skeleton.node[index].get('depth', 0)
                if depth < 20:
                    other = skeleton.neighbors(index)[0]
                    skeleton.node[other]['depth'] = depth + skeleton.edge[index][other]['line'].length
                    skeleton.remove_node(index)
                    removing = True
    
    print >> stderr, 'contain', len(skeleton.edge), 'internal edges.'
    
    return skeleton
def main(argv):
    
    if len(argv) < 3:
        print("call: translations_spanish_graph.py data_path (bibtex_key|component)")
        sys.exit(1)

    cr = CorpusReaderDict(argv[1])

    dictdata_ids = cr.dictdata_ids_for_bibtex_key(argv[2])
    if len(dictdata_ids) == 0:
        dictdata_ids = cr.dictdata_ids_for_component(argv[2])
        if len(dictdata_ids) == 0:
            print("did not find any dictionary data for the bibtex_key or component {0}.".format(argv[2]))
            sys.exit(1)
        

    for dictdata_id in dictdata_ids:
        gr = Graph()
        src_language_iso = cr.src_languages_iso_for_dictdata_id(dictdata_id)
        tgt_language_iso = cr.tgt_languages_iso_for_dictdata_id(dictdata_id)
        if (src_language_iso != ['spa']) and (tgt_language_iso != ['spa']):
            continue
        
        if (len(src_language_iso) > 1) or (len(tgt_language_iso) > 1):
            continue
        
        language_iso = None
        if tgt_language_iso == [ 'spa' ]:
            language_iso = src_language_iso[0]
        else:
            language_iso = tgt_language_iso[0]
                        
        dictdata_string = cr.dictdata_string_id_for_dictata_id(dictdata_id)
        bibtex_key = dictdata_string.split("_")[0]

        for head, translation in cr.heads_with_translations_for_dictdata_id(dictdata_id):
            if src_language_iso == [ 'spa' ]:
                (head, translation) = (translation, head)
                
            head_with_source = escape_string("{0}|{1}".format(head, bibtex_key))
            translation = escape_string(translation)
            
            #translation_with_language = "{0}|{1}".format(translation, language_iso)
            
            #if head_with_source not in gr:
            gr.add_node(head_with_source, attr_dict={ "lang": language_iso, "source": bibtex_key })
            
            #if translation not in gr:
            gr.add_node(translation, attr_dict={ "lang": "spa" })
                
            #if not gr.has_edge((head_with_source, translation)):
            gr.add_edge(head_with_source, translation)

        output = codecs.open("{0}.dot".format(dictdata_string), "w", "utf-8")
        output.write(write(gr))
        output.close()
Beispiel #13
0
def read_rw():
	"""
	Parses the rare word similarity test collection.
	"""
	G = Graph()
	filename = get_support_data_filename('rw/rw.txt')
	with open(filename) as file:
		for line in file:
			parts = line.split()
			G.add_edge(parts[0], parts[1], weight=(float(parts[2])/10))
Beispiel #14
0
def read_mc():
	"""
	Parses the Miller and Charles word similarity test collection.
	"""
	G=Graph()
	filename = get_support_data_filename('mc/EN-MC-30.txt')
	with open(filename) as file:
		for line in file:
			parts = line.split()
			G.add_edge(parts[0], parts[1], weight=float(parts[2]))
Beispiel #15
0
def read_graphml(graph_file):
    from BeautifulSoup import BeautifulSoup as Soup
    soup = Soup(graph_file.read())
    graph = Graph()

    for edge in soup.findAll("edge"):
        source = int(edge['source'])
        target = int(edge['target'])
        graph.add_edge(source, target)
    return graph
Beispiel #16
0
def read_gml(graph_file):
    graph = Graph()

    data = read_gml_data(graph_file)
    for n in data['graph']['node']:
        graph.add_node(int(n['id']))

    for e in data['graph']['edge']:
        graph.add_edge(int(e['source']), int(e['target']))

    return graph
Beispiel #17
0
def test_two_nodes():
    test = Graph()

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

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

    communities = asyn_fluidc(test, 2)
    result = {frozenset(c) for c in communities}
    assert_equal(result, ground_truth)
Beispiel #18
0
def read_rg65():
	"""
	Parses the Rubenstein and Goodenough word similarity test collection.
	"""
	G = Graph()
	filename = get_support_data_filename('rg65/EN-RG-65.txt')
	with open(filename) as file:
		for line in file:
			parts = line.split()
			G.add_edge(parts[0], parts[1], weight=float(parts[2]))
	return G
Beispiel #19
0
def read_edgelist(graph_file):
    graph = Graph()
    for line in graph_file:
        line = line.strip()
        if line[0] == '#':
            continue
        source, target = line.split()
        s = int(source)
        t = int(target)

        graph.add_edge(s, t)

    return graph
Beispiel #20
0
 def recalculate_edges(self, nodes=[]):
     """ Recalculate edges for given nodes or for all self.nodes().
     Edge between nodes n1 and n2 are added if both are
     ChannelType.in_comm_range of each other"""
     if(not nodes):
         nodes = self.nodes()
     for n1 in nodes:
         for n2 in self.nodes():
             if (n1 != n2):
                 if (self.channelType.in_comm_range(self, n1, n2)):
                     Graph.add_edge(self, n1, n2)
                 elif (Graph.has_edge(self, n1, n2)):
                     Graph.remove_edge(self, n1, n2)
Beispiel #21
0
def read(string):

    """
    Read a graph from a string in Dot language and return it. Nodes and
    edges specified in the input will be added to the current graph.
    
    Args:
        - string: Input string in Dot format specifying a graph.
    
    Returns:
        - networkx.Graph object
    """

    lines = string.split("\n")
    first_line = lines.pop(0)
    if not re.match("graph \w+ {$", first_line):
        raise WrongDotFormatException("string contains no parseable graph")

    re_node = re.compile('^"([^"]*)"(?: \[([^]]*)\])?;?$')
    re_edge = re.compile('^"([^"]*)" -- "([^"]*)"(?: \[([^]]*)\])?;?$')

    gr = Graph()

    for line in lines:
        line = line.strip()
        match_node = re_node.search(line)
        match_edge = re_edge.search(line)
        if match_node:
            g = match_node.groups()
            gr.add_node(g[0])
            if g[1]:
                for attribute_string in g[1].split(", "):
                    key, value = attribute_string.split("=")
                    if value == "True":
                        value = True
                    elif value == "False":
                        value = False
                    gr.node[g[0]][key] = value

        elif match_edge:
            g = match_edge.groups()
            gr.add_edge(g[0], g[1])
            if g[2]:
                for attribute_string in g[2].split(", "):
                    key, value = attribute_string.split("=")
                    gr.edge[g[0]][g[1]][key] = value

        elif line != "}" and len(line) > 0:
            raise WrongDotFormatException("Could not parse line:\n\t{0}".format(line))

    return gr
Beispiel #22
0
def simplify(tree, keepers):
    """ Given a tree and a set of nodes to keep, create a new tree
    where only the nodes to keep and the branch points between them are preserved.
    WARNING: will reroot the tree at the first of the keepers.
    WARNING: keepers can't be empty. """
    # Ensure no repeats
    keepers = set(keepers)
    # Add all keeper nodes to the minified graph
    mini = Graph()
    for node in keepers:
        mini.add_node(node)
    # Pick the first to be the root node of the tree, removing it
    root = keepers.pop()
    reroot(tree, root)
    # For every keeper node, traverse towards the parent until
    # finding one that is in the minified graph, or is a branch node
    children = defaultdict(int)
    seen_branch_nodes = set(keepers) # a copy
    paths = []
    # For all keeper nodes except the root
    for node in keepers:
        path = [node]
        paths.append(path)
        parents = tree.predecessors(node)
        while parents:
            parent = parents[0]
            if parent in mini:
                # Reached one of the keeper nodes
                path.append(parent)
                break
            elif len(tree.successors(parent)) > 1:
                # Reached a branch node
                children[parent] += 1
                path.append(parent)
                if parent in seen_branch_nodes:
                    break
                seen_branch_nodes.add(parent)
            parents = tree.predecessors(parent)
    for path in paths:
        # A path starts and ends with desired nodes for the minified tree.
        # The nodes in the middle of the path are branch nodes
        # that must be added to mini only if they have been visited more than once.
        origin = path[0]
        for i in xrange(1, len(path) -1):
            if children[path[i]] > 1:
                mini.add_edge(origin, path[i])
                origin = path[i]
        mini.add_edge(origin, path[-1])

    return mini
Beispiel #23
0
def solve():
	from networkx import Graph, max_flow
	W, H, B = map(int,input().split())
	R = [[True for _ in range(H)] for _ in range(W)]
	
	for _ in range(B):
		x0, y0, x1, y1 = map(int,input().split())
		for x in range(x0,x1+1):
			for y in range(y0,y1+1):
				R[x][y] = False
	
	G = Graph()
	
	for x in range(W):
		for y in range(H):
			if R[x][y]:
				G.add_edge(('start',x,y),('end',x,y),capacity=1)
			
				if y == 0:
					G.add_edge('source',('start',x,y))
				else:
					if R[x][y-1]:
						#G.add_edge(('end',x,y),('start',x,y-1))
						pass
				
				if y == H-1:
					G.add_edge(('end',x,y),'sink')
				else:
					if R[x][y+1]:
						G.add_edge(('end',x,y),('start',x,y+1))
	
	return max_flow(G,'source','sink','capacity')
    def __init__(self, mol, eps):
        G = Graph()

        G.add_nodes_from(a.GetIdx() for a in mol.GetAtoms())

        for bond in mol.GetBonds():
            a = bond.GetBeginAtom()
            b = bond.GetEndAtom()

            w = a.GetDegree() * b.GetDegree()

            G.add_edge(a.GetIdx(), b.GetIdx(), weight=w)

        self.G = G
        self.lim = int(1.0 / (eps ** 2))
Beispiel #25
0
def connect_close_enough_edges_(g: nx.Graph, radius: float) -> None:
    """
    Simply iterate over nodes and check distance. Connect nodes if distance
    lower or equal ``radius``. Function modify ``g``!

    Args:
        g: Graph, each node must have ``pos`` attribute.
        radius: Max distance.
    """

    # iterate over nodes and connect - 0(n^2) operation
    for n1 in g.nodes:
        for n2 in range(n1 + 1, len(g.nodes)):
            if distance(g.nodes[n1]["pos"], g.nodes[n2]["pos"]) <= radius:
                g.add_edge(n1, n2)
Beispiel #26
0
def _fix_disconnected_graph(g: nx.Graph, ref_cells: List[str], fn: str,
                            dist_grp: str, sorted_dist_grp: str, name: str,
                            weight: float) -> nx.Graph:
    """
    Connect graph components based on nearest component.

    :param g: Input disconnected graph
    :param ref_cells: Name of reference cells
    :param fn: HDF5 file containing distances
    :param dist_grp: Distance group name
    :param sorted_dist_grp: Sorted distance indices
    :param name: Reference cell name
    :param weight: Weight for connecting edge
    :return: Networkx graph
    """
    h5 = h5py.File(fn, mode='r')
    cells_map_idx = {x: n for n, x in enumerate(ref_cells)}
    comps = list(nx.connected_components(g))
    n_comps = len(comps)
    comp_lens = [len(x) for x in comps]
    sorted_lens_idx = np.argsort(comp_lens)
    valid_match_cell_idx = {x: [] for x in range(n_comps)}
    for i in range(n_comps):
        idx1 = sorted_lens_idx[i]
        for j in range(i + 1, n_comps):
            idx2 = sorted_lens_idx[j]
            if comp_lens[idx2] > comp_lens[idx1]:
                valid_match_cell_idx[idx1].extend([
                    cells_map_idx[x.replace('_' + name, '')]
                    for x in comps[idx2]
                ])
    for comp_n in valid_match_cell_idx:
        target_cells = {x: None for x in valid_match_cell_idx[comp_n]}
        if len(target_cells) > 0:
            dists, names1, names2 = [], [], []
            for cell in comps[comp_n]:
                cell = cell.rsplit('_', 1)[0]
                for i in h5[sorted_dist_grp][cell][:]:
                    if i in target_cells:
                        dists.append(h5[dist_grp][cell][i])
                        names1.append(cell)
                        names2.append(ref_cells[i])
                        break
            n1 = names1[np.argsort(dists)[0]] + '_' + name
            n2 = names2[np.argsort(dists)[0]] + '_' + name
            g.add_edge(n1, n2, weight=weight)
    h5.close()
    return g
Beispiel #27
0
def group_graph_partition(
    inputlist,
    numgrp=2,
    objective_weights=None,
    objective_measures=None,
    preferences=None,
    preferences_weight=1.1,
    preferences_weight_match=1.3,
):
    """
    Form groups using recursive Kernighan-Lin algorithm by reading in students list
    and weight list and partitioning the vertices.
    """
    # Read in students list and the weight list
    students = [item[0] for item in inputlist]
    weights = [item[1:] for item in inputlist]
    # Create graph and populate with node weights
    vertex_weight_pairs = enumerate([{"weight": w} for w in weights])
    G = Graph()
    G.add_nodes_from(vertex_weight_pairs)

    # Add edges between distinct vertices, weighted by compatibility score
    for i, w1 in enumerate(weights):
        for j, w2 in enumerate(weights[:i]):
            score = compatibility(
                w1,
                w2,
                objective_weights=objective_weights,
                objective_measures=objective_measures,
            )
            if preferences:
                # Scale the compatibility score based on students' preferences for each other
                prefs_j = preferences.get(students[j])
                prefs_i = preferences.get(students[i])
                match_j = students[i] in prefs_j if prefs_j else None
                match_i = students[j] in prefs_i if prefs_i else None
                if match_i and match_j:
                    score = score * preferences_weight_match
                elif match_i or match_j:
                    score = score * preferences_weight
            G.add_edge(i, j, weight=score)

    # Partition the vertices
    partition = recursive_kl(G, numgrp=numgrp)
    groups = []
    for p in partition:
        groups.append([inputlist[i] for i in p])
    return groups
Beispiel #28
0
def polygon_dots_skeleton(polygon, points):
    '''
    '''
    skeleton = Graph()

    rbox = '\n'.join( ['2', str(len(points))] + ['%.2f %.2f' % (x, y) for (x, y) in points] + [''] )
    
    qvoronoi = Popen('qvoronoi o'.split(), stdin=PIPE, stdout=PIPE)
    output, error = qvoronoi.communicate(rbox)
    voronoi_lines = output.splitlines()
    
    if qvoronoi.returncode:
        raise _QHullFailure('Failed with code %s' % qvoronoi.returncode)
    
    vert_count, poly_count = map(int, voronoi_lines[1].split()[:2])
    
    for (index, line) in enumerate(voronoi_lines[2:2+vert_count]):
        point = Point(*map(float, line.split()[:2]))
        if point.within(polygon):
            skeleton.add_node(index, dict(point=point))
    
    for line in voronoi_lines[2+vert_count:2+vert_count+poly_count]:
        indexes = map(int, line.split()[1:])
        for (v, w) in zip(indexes, indexes[1:] + indexes[:1]):
            if v not in skeleton.node or w not in skeleton.node:
                continue
            v1, v2 = skeleton.node[v]['point'], skeleton.node[w]['point']
            line = LineString([(v1.x, v1.y), (v2.x, v2.y)])
            if line.within(polygon):
                skeleton.add_edge(v, w, dict(line=line, length=line.length))
    
    removing = True
    
    while removing:
        removing = False
    
        for index in skeleton.nodes():
            if skeleton.degree(index) == 1:
                depth = skeleton.node[index].get('depth', 0)
                if depth < 20:
                    other = skeleton.neighbors(index)[0]
                    skeleton.node[other]['depth'] = depth + skeleton.edge[index][other]['line'].length
                    skeleton.remove_node(index)
                    removing = True
    
    logging.debug('found %d skeleton edges' % len(skeleton.edge))
    
    return skeleton
Beispiel #29
0
def check_embedding(Q, A, emb, **args):
    from networkx import Graph, is_connected
    check_embedding.warning = None
    Qg = Graph()
    Ag = Graph()
    Qg.add_edges_from(Q)
    Ag.add_edges_from(A)

    qubhits = 0
    footprint = set()
    var = {}
    for x in Qg:
        try:
            embx = emb[x]
        except KeyError:
            check_embedding.errcode = "missing chain"
            return False
        for q in embx:
            var[q] = x
        footprint.update(embx)
        qubhits += len(embx)
        if not is_connected(Ag.subgraph(embx)):
            check_embedding.errcode = "broken chain for %s: (%s)" % (x, embx)
            return False
    if len(footprint) != qubhits:
        check_embedding.errcode = "overlapped chains"
        return False

    Qv = Graph()
    for p, q in Ag.edges():
        try:
            Qv.add_edge(var[p], var[q])
        except KeyError:
            continue
    for x, y in Qg.edges():
        if not Qv.has_edge(x, y):
            check_embedding.errcode = "missing edge"
            return False

    for x, chain in args.get("fixed_chains", {}).items():
        if set(chain) != set(emb[x]):
            check_embedding.errcode = "fixed chain mismatch"
            return False
    for x, domain in args.get("restrict_chains", {}).items():
        if not set(domain) >= set(emb[x]):
            check_embedding.warning = "restrict chain mismatch"

    return True
Beispiel #30
0
    def rewire_network(self,
                       network: nx.Graph,
                       rewiring_prop: float = None,
                       rewiring_exact=None,
                       **kwargs):
        """
        This method executes Maslov Sneppen rewiring [MaslovSneppen2002]_. Until a given proportion of the network
        edges is rewired, the algorithm will pick two edges at random, remove them, and construct an edge between a
        different combination of nodes that have just lost an edge. Effectively, this introduces randomization of network
        structure, while leaving the degree distribution unchanged. Because the parameter `rewiring_prop` functions as a
        threshold for number of rewiring iterations that need to be executed, it can exceed 1. Actually, to achieve a
        random network starting from a network with structure, the rewiring proportion should exceed 1.

        :param network: NetworkX Graph object
        :param rewiring_prop: A threshold for the minimum proportion of edges in the graph object that need to be rewired.
            Sampling these edges happens with replacement, so rewiring_prop may exceed 1.
        :param rewiring_exact: The exact number of edges to rewire. One of rewiring_prop or rewiring_exact must be set.
            If both are set, rewiring_exact takes precedence.
        :return: Does not return the network. The network is modified in place.
        """

        if rewiring_prop is None:
            rewiring_prop = self.rewiring_prop
        if rewiring_exact is None:
            rewiring_exact = self.rewiring_exact

        if rewiring_exact is not None and rewiring_prop is None:
            rewire_n = rewiring_exact
        elif rewiring_exact is None and rewiring_prop is not None:
            rewire_n = rewiring_prop * network.number_of_edges()
        elif rewiring_exact is not None and rewiring_prop is not None:
            rewire_n = rewiring_exact
            warnings.warn(
                "Both proportional and exact rewiring specified, using exact.")

        ticker = 0
        while rewire_n > ticker:
            agentA, agentB = random.choice(list(network.edges()))
            agentC, agentD = random.choice(list(network.edges()))
            if ((agentA != agentC) and (agentA != agentD)
                    and (agentB != agentC) and (agentB != agentD)
                    and not network.has_edge(agentA, agentC)
                    and not network.has_edge(agentB, agentD)):
                network.remove_edge(agentA, agentB)
                network.remove_edge(agentC, agentD)
                network.add_edge(agentA, agentC)
                network.add_edge(agentB, agentD)
                ticker += 1
Beispiel #31
0
def read_graph_in_dimacs_format(filename):
    with open(filename) as f:
        line = f.readline()
        while line.startswith('c '):
            line = f.readline()
        tokens = line.split()
        num_nodes = int(tokens[2])
        num_edges = int(tokens[3])
        G = Graph()
        G.add_nodes_from(range(1, num_nodes + 1))
        for i in range(num_edges):
            tokens = f.readline().split()
            n1 = int(tokens[1])
            n2 = int(tokens[2])
            G.add_edge(n1, n2)
        return G
Beispiel #32
0
    def test_bad_input_links(self):
        graph = Graph()
        e1 = gen_name()
        e2 = gen_name()
        e3 = gen_name()
        graph.add_node(e1, layer=1, position=(1.0, 2.0), label='E')
        graph.add_node(e2, layer=1, position=(1.0, 1.0), label='E')
        graph.add_node(e3, layer=1, position=(2.0, 1.0), label='E')

        graph.add_edge(e1, e2)
        graph.add_edge(e1, e3)

        i = add_interior(graph, e1, e2, e3)

        with self.assertRaises(AssertionError):
            P2().apply(graph, [i])
Beispiel #33
0
def construct_graph(path, weights=[]):
    """reads the adjacency list and construct the base graph"""
    G = Graph()
    adj_list = read_adj_list(path)
    G.add_nodes_from(set(adj_list.keys()) - set([-1]))
    for n in G.nodes:
        G.nodes[n]['boundary'] = False
    for n, adj in adj_list.items():
        if n == -1:
            continue
        for v in adj:
            if v == -1:
                G.nodes[n]['boundary'] = True
                continue
            G.add_edge(n, v, border_length=weights[n][v])
    return G
Beispiel #34
0
class TextRank(object):

    def __init__(self, text):
        self.text = text.strip()
        self.build()

    def build(self):
        self._build_sentences()
        self._build_graph()
        self.pageranks = pagerank(self.graph, weight='weight')
        self.reordered = sorted(self.pageranks, key=self.pageranks.get, reverse=True)

    def _build_sentences(self):
        dup = {}
        candidates = split(r'(?:(?<=[^0-9])\.|\n)', self.text)
        self.sentences = []
        index = 0
        for candidate in candidates:
            while len(candidate) and (candidate[-1] == '.' or candidate[-1] == ' '):
                candidate = candidate.strip(' ').strip('.')
            if len(candidate) and candidate not in dup:
                dup[candidate] = True
                self.sentences.append(Sentence(candidate + '.', index))
                index += 1
        del dup
        del candidates

    def _build_graph(self):
        self.graph = Graph()
        self.graph.add_nodes_from(self.sentences)
        for sent1, sent2 in combinations(self.sentences, 2):
            weight = self._jaccard(sent1, sent2)
            if weight:
                self.graph.add_edge(sent1, sent2, weight=weight)

    def _jaccard(self, sent1, sent2):
        p = sum((sent1.bow & sent2.bow).values())
        q = sum((sent1.bow | sent2.bow).values())
        return p / q if q else 0

    def summarize(self, count=3, verbose=True):
        results = sorted(self.reordered[:count], key=lambda sentence: sentence.index)
        results = [result.text for result in results]
        if verbose:
            return '\n'.join(results)
        else:
            return results
Beispiel #35
0
    def save(self, output_dir, save_src):
        with open(path.join(output_dir, 'commits.csv'), 'w', newline='') as commits_file:
            writer = csv.DictWriter(commits_file, ['min_fitness', 'change_size'])
            writer.writeheader()
            writer.writerows(self._revisions)

        with open(path.join(output_dir, 'methods.csv'), 'w', newline='') as methods_file:
            writer = csv.DictWriter(methods_file, ['method', 'class', 'ref_count'])
            writer.writeheader()
            for method_name, in_degree in self._method_call_graph.in_degree_iter():
                writer.writerow({
                    'method': method_name,
                    'class': self._method_call_graph.node[method_name]['class_name'],
                    'ref_count': in_degree
                })

        with open(path.join(output_dir, 'methods.json'), 'w') as methods_file:
            data = json_graph.node_link_data(self._method_call_graph)
            json.dump(data, methods_file, skipkeys=True, default=lambda d: None)

        association_graph = Graph()
        for e in self._method_call_graph.edges_iter():
            association_graph.add_edge(
                self._method_call_graph.node[e[0]]['class_name'],
                self._method_call_graph.node[e[1]]['class_name'])
        for e in self._inheritance_graph.edges_iter():
            association_graph.add_edge(*e)
        with open(path.join(output_dir, 'classes.csv'), 'w', newline='') as classes_file:
            writer = csv.DictWriter(classes_file, ['class', 'subclasses', 'lines', 'degree'])
            writer.writeheader()
            for class_name, in_degree in self._inheritance_graph.in_degree_iter():
                klass = self._inheritance_graph.node[class_name]['class']
                java_printer = JavaPrinter()
                klass.accept(java_printer)
                writer.writerow({'class': class_name,
                                 'subclasses': in_degree,
                                 'lines': java_printer.result.count('\n') + 1,
                                 'degree': association_graph.degree(class_name)
                                 if class_name in association_graph else 0
                                 })
                if save_src:
                    with open(path.join(output_dir, 'src', class_name + '.java'), 'w') as java_file:
                        java_file.write(java_printer.result)

        with open(path.join(output_dir, 'classes.json'), 'w') as classes_file:
            data = json_graph.node_link_data(association_graph)
            json.dump(data, classes_file, skipkeys=True)
Beispiel #36
0
    def generate_graph(self):
        graph = Graph()
        # populate cities:
        g = self.iter_all_strings()
        for _ in range(self.node_count):
            self.cities.append(next(g))

        # to make a connected graph, all nodes should be connected.
        connected = self.cities[:1]
        not_connected = self.cities[1:]

        for city in not_connected:
            connected_with = self.random.choice(connected)
            graph.add_edge(connected_with, city)
            # if the number of neighbors (edges) for connecting city is over max_path, remove it from connected,
            # so it doesn't overflow no more.
            if len(list(graph.neighbors(connected_with))) == self.max_paths:
                connected.remove(connected_with)
            connected.append(city)

        for city in connected:
            neighbors = list(graph.neighbors(city))
            edge_count = self.random.randint(self.min_paths - len(neighbors),
                                             self.max_paths - len(neighbors))
            if edge_count < 1:
                continue

            connectable = [
                c for c in connected if c not in neighbors and c != city
            ]
            if not connectable:
                continue
            for _ in range(edge_count):
                if not connectable:
                    break
                connected_with = self.random.choice(connectable)

                if len(list(
                        graph.neighbors(connected_with))) >= self.max_paths:
                    connectable.remove(connected_with)
                    continue
                graph.add_edge(city, connected_with)
                # same applies, so when we reach max path, remove it from the connectable pool
                connectable.remove(connected_with)
            gc.collect()
        return graph
    def predict_undirected_graph(self, data):
        """Build a skeleton using a pairwise independence criterion.

        Args:
            data (pandas.DataFrame): Raw data table

        Returns:
            networkx.Graph: Undirected graph representing the skeleton.
        """
        graph = Graph()
        for idx_i, i in enumerate(data.columns):
            for idx_j, j in enumerate(data.columns[idx_i+1:]):
                score = self.predict(data[i].values, data[j].values)
                if abs(score) > 0.001:
                    graph.add_edge(i, j, weight=score)

        return graph
Beispiel #38
0
def add_phosphodiester_bonds(G: nx.Graph) -> nx.Graph:
    """
    Adds phosphodiester bonds between adjacent nucleotides to an RNA secondary structure graph

    :param G: RNA Graph to add edges to
    :type G: nx.Graph
    :return: RNA graph with phosphodiester_bond edges added
    :rtype: nx.Graph
    """
    # Iterate over dotbracket to build connectivity
    bases = []
    for i, c in enumerate(G.graph["dotbracket"]):
        # Add adjacent edges (phosphodiester_bonds)
        if i > 0:
            G.add_edge(i, i - 1, attr="phosphodiester_bond", color="b")
    log.debug("Added phosphodiester bonds as edges")
    return G
Beispiel #39
0
def add_aromatic_interactions(G: nx.Graph,
                              pdb_df: Optional[pd.DataFrame] = None):
    """
    Find all aromatic-aromatic interaction.
    Criteria: phenyl ring centroids separated between 4.5A to 7A.
    Phenyl rings are present on PHE, TRP, HIS and TYR.
    Phenyl ring atoms on these amino acids are defined by the following
    atoms:
    - PHE: CG, CD, CE, CZ
    - TRP: CD, CE, CH, CZ
    - HIS: CG, CD, ND, NE, CE
    - TYR: CG, CD, CE, CZ
    Centroids of these atoms are taken by taking:
        (mean x), (mean y), (mean z)
    for each of the ring atoms.
    Notes for future self/developers:
    - Because of the requirement to pre-compute ring centroids, we do not
        use the functions written above (filter_dataframe, compute_distmat,
        get_interacting_atoms), as they do not return centroid atom
        euclidean coordinates.
    """
    dfs = []
    for resi in AROMATIC_RESIS:
        resi_rings_df = get_ring_atoms(pdb_df, resi)
        resi_centroid_df = get_ring_centroids(resi_rings_df)
        dfs.append(resi_centroid_df)

    aromatic_df = (pd.concat(dfs).sort_values(by="node_id").reset_index(
        drop=True))

    distmat = compute_distmat(aromatic_df)
    distmat.set_index(aromatic_df["node_id"], inplace=True)
    distmat.columns = aromatic_df["node_id"]
    distmat = distmat[(distmat >= 4.5) & (distmat <= 7)].fillna(0)
    indices = np.where(distmat > 0)

    interacting_resis = [(distmat.index[r], distmat.index[c])
                         for r, c in zip(indices[0], indices[1])]

    for n1, n2 in interacting_resis:
        assert G.nodes[n1]["residue_name"] in AROMATIC_RESIS
        assert G.nodes[n2]["residue_name"] in AROMATIC_RESIS
        if G.has_edge(n1, n2):
            G.edges[n1, n2]["kind"].add("aromatic")
        else:
            G.add_edge(n1, n2, kind={"aromatic"})
Beispiel #40
0
def read_men3000():
	"""
	Parses the MEN test collection. MEN is a collection of 3000 english word
	pairs, each with a relatedness rating between 0 and 50. The relatedness of
	a pair of words was determined by the number of times the pair was selected
	as more related compared to another randomly chosen pair.
	"""
	G = Graph()
	filename = get_support_data_filename('mensim/MEN_dataset_lemma_form.dev')
	with open(filename) as file:
		for line in file:
			parts = line.rstrip().split()
			term1 = parts[0].split('-')[0]  # remove part of speech
			term2 = parts[1].split('-')[0]  # as above
			gold_score = float(parts[2]) * 0.02
			G.add_edge(term1, term2, weight=gold_score)
	return G
Beispiel #41
0
def make(g=None, p=None):
    """Construct a g560 graph (a.k.a. GP-graph) using the supplied Gewirtz and Peterson graphs.

    The separate node numbering schemes of the Gewirtz and Peterson graphs are
    preserved in the labelling scheme of the nodes in the new graph, which have
    will be strings of the form "G-P" where G and P are the respective names from
    the supplied Gewirtz and Peterson graphs.

    Args:
        g: An optional Gewirtz graph. If not supplied the default Gewirtz graph will be used.
        p: An optional Peterson graph. If not supplied the default Peterson graph will be used.

    Returns:
        A g560 graph.
    """

    g = g or gewirtz.make()
    p = p or petersen.make()

    # Make a disconnected graph containing 56 disconnected Peterson graphs

    s = Graph()

    # Add 560 nodes by adding one Petersen graph (10 nodes) for each
    # of the 56 nodes of the gewirtz graph.
    for g_node in g.nodes:
        s = union(s, p, rename=('', '{}-'.format(g_node)))

    neighbours = {n: list(g.neighbors(n)) for n in g.nodes}

    for g_edge in g.edges:
        # This edge will be the ith outedge and jth inedge
        # Connect g_edge_from-i to g_edge_to-j

        g_from_node, g_to_node = g_edge
        i = neighbours[g_from_node].index(g_to_node)
        j = neighbours[g_to_node].index(g_from_node)

        s_from_node = '{}-{}'.format(g_from_node, i)
        s_to_node = '{}-{}'.format(g_to_node, j)

        #print("{} -- {}".format(s_from_node, s_to_node))

        s.add_edge(s_from_node, s_to_node)

    return s
def plot_link_prediction_graph(G: nx.Graph,
                               pred_edges: [],
                               pred_acc=None,
                               idx2node=None):
    G = G.copy()
    if pred_acc is None:
        pred_acc = []
    pos = nx.spring_layout(G, seed=6)  # positions for all nodes
    nx.draw_networkx_nodes(G, pos)
    # edges
    nx.draw_networkx_edges(G, pos, width=1.0, alpha=0.5)

    pred_edges_ = []
    edges_labels = {}

    for i, (u, v) in enumerate(pred_edges):
        if G.has_node(u) and G.has_node(v):
            G.add_edge(u, v)
            pred_edges_.append((u, v))
            if pred_acc:
                edges_labels[(u, v)] = round(pred_acc[i], 2)

    nx.draw_networkx_edges(
        G,
        pos,
        edgelist=pred_edges_,
        # width=1,
        # alpha=0.5,
        edge_color='r')

    if pred_acc:
        nx.draw_networkx_edge_labels(G,
                                     pos,
                                     edge_labels=edges_labels,
                                     font_color='red')

    if idx2node is not None:
        labels = {}
        for u in G.nodes:
            labels[u] = str(idx2node[u])
        nx.draw_networkx_labels(G, pos, labels=labels)
    else:
        nx.draw_networkx_labels(G, pos=pos)

    plt.axis('off')
    plt.show()
def test_topology(graph: nx.Graph, repetitions=100, k=0.95, vector_amount=20):
    how_many_times_until_stop = []
    edges = deepcopy(graph.edges())
    for _ in range(repetitions):
        N = create_random_intensity(vector_amount=vector_amount)
        calculateA(graph, N)
        setC(graph)
        alive = 0
        degrade(graph, k=k)
        while fine(graph):
            alive += 1
            calculateA(graph, N)
            degrade(graph, k=k)
        how_many_times_until_stop.append(alive)
        for edge in edges:
            graph.add_edge(edge[0], edge[1])
    return sum(how_many_times_until_stop) / len(how_many_times_until_stop)
Beispiel #44
0
def gen_reg(peak=0, root_node=0, N=extension_nodes, reg_num=3):
    graph = sym_k_normal(N, reg_num)    # Generate adjacency matrix
    net = Graph()   # Initialize new graph

    # Add nodes to graph
    net.add_nodes_from([i for i in range(peak, peak+N)])
    net.add_node(root_node)

    # Add edges to graph
    for i in range(len(graph)):
        for j in range(i+1, len(graph)):
            if graph[i][j] == 1:
                src = i + peak if i != 0 else root_node
                dst = j + peak if j != 0 else root_node
                net.add_edge(src, dst)

    return net
Beispiel #45
0
 def add_edge(self, u, v=None):
     if v is None:
         (u, v) = u  # no v given, assume u is an edge tuple
     if self.has_edge(u, v):
         return  # no parallel edges allowed
     elif u in self and v in self:
         raise NetworkXError("adding edge %s-%s not allowed in tree" %
                             (u, v))
     elif u in self or v in self:
         Graph.add_edge(self, u, v)
         return
     elif len(self.adj) == 0:  # first leaf
         Graph.add_edge(self, u, v)
         return
     else:
         raise NetworkXError("adding edge %s-%s not allowed in tree" %
                             (u, v))
Beispiel #46
0
def visualise_plan(plan, filename):
    graph = Graph()

    for i, step in enumerate(plan.steps):
        name = repr_action(step, i)
        graph.add_node(name)

    if len(plan.steps) > 1:
        for i, (step, next_step) in enumerate(look_ahead(plan.steps)):
            name = repr_action(step, i)
            next_name = repr_action(next_step, i + 1)
            graph.add_edge(name, next_name)

    plt.axis("off")
    draw_networkx(graph)
    plt.savefig(filename)
    plt.close()
Beispiel #47
0
    def predict_undirected_graph(self, data):
        """Build a skeleton using a pairwise independence criterion.

        :param data: Raw data table
        :type data: pandas.DataFrame
        :return: Undirected graph
        :rtype: UndirectedGraph
        """
        graph = Graph()

        for idx_i, i in enumerate(data.columns):
            for idx_j, j in enumerate(data.columns[idx_i+1:]):
                score = self.predict(data[i].values, data[j].values)
                if abs(score) > 0.001:
                    graph.add_edge(i, j, weight=score)

        return graph
def most_between(graph: Graph):
    """
    Returns the tree by selecting edges of increasing betweeness centrality which do not create cycles (Kruskal).
    :param graph: graph on which to build tree
    :return: tree with betweeness nodes
    """

    # def add_neighbors(U):
    #     """
    #     Adds all neighbors of node in graph to q.
    #     :param U: node whose neighbors we add
    #     """
    #     for V in graph[U]:
    #         if (U, V) in betweenness:
    #             if not nx.has_path(new, U, V):
    #                 q.put((-betweenness[U, V], U, V))
    #             continue
    #         if (V, U) in betweenness:
    #             if not nx.has_path(new, U, V):
    #                 q.put((-betweenness[V, U], U, V))
    #             continue

    new = Graph()
    new.add_nodes_from(graph.nodes)

    betweenness = edge_betweenness_centrality(graph)
    q = PriorityQueue()
    for u, v in graph.edges:
        if (u, v) in betweenness:
            q.put((-betweenness[u, v], u, v))
            continue
        if (v, u) in betweenness:
            q.put((-betweenness[v, u], v, u))
            continue

    # for u in nodes:
    #     add_neighbors(u)

    while not nx.is_connected(new):
        bet, u, v = q.get()
        if not nx.has_path(new, u, v):
            new.add_edge(u, v, weight=graph[u][v]['weight'])
            # add_neighbors(u)

    return new
def de_augment(nodes: set, graph: Graph):
    """
    Takes the nodes selected from an augmented graph and builds a corresponding graph with original edges and nodes.
    :param nodes: selected nodes from augmented graph
    :param graph: base graph
    :return: graph with original edges and selected nodes
    """
    new = Graph()
    # add all nodes and all possible edges
    new.add_nodes_from(nodes)
    for node1 in nodes:
        for node2 in nodes:
            if graph.has_edge(node1, node2):
                new.add_edge(node1,
                             node2,
                             weight=graph[node1][node2]['weight'])

    return new
Beispiel #50
0
def create_graph(sentences):
    """Creates a complete NetworkX graph with the text_place of every sentence
    as the nodes. The edge weights are values indicating the similarity
    between each sentence.
    """
    graph = Graph()

    for sentence in sentences:
        graph.add_node(sentence.text_place)

    # Turn graph into a complete graph
    for (s1, s2) in combinations(sentences, 2):
        # The weight is derived from the textrank sentence similarity equation
        # applied to the reduced forms of the sentences.
        similarity_value = similarity(s1.reduced,s2.reduced)
        graph.add_edge(s1.text_place, s2.text_place, weight=similarity_value)

    return graph
Beispiel #51
0
def add_edges_naive(graph: Graph, max_distance: float):
    """
    Add edges between each pair of nodes in the graph if their distance along both axis (x, y) is less than max_distance.
    It compares all the possible pairs of nodes
    Complexity: O(n^2) with n = number of nodes
    """

    nodes = list(graph.nodes(data=True))
    n_nodes = len(nodes)

    for i in range(n_nodes):
        v_id, v_attr = nodes[i]
        for j in range(i + 1, n_nodes):
            u_id, u_attr = nodes[j]
            # There is an edge between two nodes if their coordinates differ for less than max_distance
            if abs(v_attr["x"] - u_attr["x"]) <= max_distance and abs(
                    v_attr["y"] - u_attr["y"]) <= max_distance:
                graph.add_edge(v_id, u_id)
Beispiel #52
0
def positive_edges(G: nx.Graph) -> Iterable[Tuple[Any, Any]]:
    """
    Given a graph, yield edges which are positive samples; these can be removed from the
    graph without disconnecting the graph.

    :param G: input NetworkX graph object
    :return: iterate tuples, each representing an edge
    """
    # TODO this needs more tests
    G = nx.Graph(G)
    edges = list(G.edges())
    random.shuffle(edges)
    for choice in edges:
        G.remove_edge(*choice)
        if nx.is_connected(G):
            yield choice
        else:
            G.add_edge(*choice)
Beispiel #53
0
def add_newick_node_and_edge(nx_graph: nx.Graph, current_node,
                             none_counter: int):
    """
    Adding the descendants of the current node to the graph and adding the according edge.
    Rename a node if the name is None.
    :param nx_graph: the network x graph to which node and edge should be added
    :param current_node: the current node from the newick graph
    :param none_counter: an integer representing the amount of None node
    :return: (graph, descendants, none_counter)
    """
    descendants = current_node.descendants
    for child_pos in range(len(descendants)):
        descendants[child_pos], none_counter = rename_none_node(
            descendants[child_pos], none_counter)
        nx_graph.add_node(descendants[child_pos], child_position=child_pos)
        nx_graph.add_edge(current_node, descendants[child_pos])

    return nx_graph, descendants, none_counter
Beispiel #54
0
def engine_picker(G: Graph, engine: str, node_mode: bool = False):
    if not node_mode:
        G_ = Graph()
        nodes = {i: edge for i, edge in edges_view(G)}
        for i, (l1, l2) in nodes.items():
            for j, edge in nodes.items():
                if i == j:
                    continue
                if (l1 in edge) or (l2 in edge):
                    G_.add_edge(i, j)
        H = G_
    else:
        H = G
    if type(engine) == str:
        if engine == "random":
            E = {
                k: (x * 200, y * 200)
                for k, (x, y) in random_layout(H).items()
            }
        elif engine == "shell":
            E = shell_layout(H, scale=100)
        elif engine == "circular":
            E = circular_layout(H, scale=100)
        elif engine == "spring":
            E = spring_layout(H, scale=100)
        elif engine == "spectral":
            E = spectral_layout(H, scale=100)
        else:
            try:
                E = nx_pydot.graphviz_layout(H, prog=engine)
            except:
                raise EngineError("No Graphviz")
        pos = {
            k: (round(float(x), 4), round(float(y), 4))
            for k, (x, y) in E.items()
        }
        x_cen = (max(x for x, y in pos.values()) +
                 min(x for x, y in pos.values())) / 2
        y_cen = (max(y for x, y in pos.values()) +
                 min(y for x, y in pos.values())) / 2
        pos = {k: (x - x_cen, y - y_cen) for k, (x, y) in pos.items()}
    else:
        pos = engine
    return pos
Beispiel #55
0
def chimera_block_quotient(G, blocks):
    """
    Extract the blocks from a graph, and returns a
    block-quotient graph according to the acceptability
    functions block_good and eblock_good

    Inputs:
        G: a networkx graph
        blocks: a tuple of tuples

    """
    from networkx import Graph
    from itertools import product

    BG = Graph()
    blockid = {}
    for i, b in enumerate(blocks):
        BG.add_node(i)
        if not b or not all(G.has_node(x) for x in b):
            continue
        for q in b:
            if q in blockid:
                raise (RuntimeError, "two blocks overlap")
            blockid[q] = i

    for q, u in blockid.items():
        ublock = blocks[u]
        for p in G[q]:
            if p not in blockid:
                continue
            v = blockid[p]
            if BG.has_edge(u, v) or u == v:
                continue
            vblock = blocks[v]

            if ublock[0][2] == vblock[0][2]:
                block_edges = zip(ublock, vblock)
            else:
                block_edges = product(ublock, vblock)

            if all(G.has_edge(x, y) for x, y in block_edges):
                BG.add_edge(u, v)

    return BG
Beispiel #56
0
def add_base_pairing_interactions(G: nx.Graph) -> nx.Graph:
    """
    Adds base_pairing interactions between nucleotides to an RNA secondary structure graph

    :param G: RNA Graph to add edges to
    :type G: nx.Graph
    :return: RNA graph with base_pairing edges added
    :rtype: nx.Graph
    """
    # Check sequence is used
    if "sequence" in G.graph.keys():
        check_base_pairing = True
    else:
        check_base_pairing = False

    # Iterate over dotbracket to build connectivity
    bases = []
    for i, c in enumerate(G.graph["dotbracket"]):
        # Add base_pairing interactions
        if c == "(":
            bases.append(i)
        elif c == ")":
            neighbor = bases.pop()

            if check_base_pairing:
                pairing_type = check_base_pairing_type(
                    G.nodes[i]["nucleotide"], G.nodes[neighbor]["nucleotide"]
                )
            else:
                pairing_type = "unknown"

            G.add_edge(
                i,
                neighbor,
                attr="base_pairing",
                pairing_type=pairing_type,
                color="r",
            )
        elif c in [".", "[", "]", "{", "}", "<", ">"]:
            continue
        else:
            raise ValueError("Input is not in dot-bracket notation!")
        log.debug("Added base_pairing interactions as edges")
    return G
Beispiel #57
0
def summarize(text, sentence_count=5, language='english'):
    stopwords = get_stopwords(language)
    sentence_list = tokenize.sent_tokenize(text, language)
    wordsets = [get_words(sentence, stopwords) for sentence in sentence_list]

    graph = Graph()
    pairs = combinations(enumerate(filter(None, wordsets)), 2)
    for (index_a, words_a), (index_b, words_b) in pairs:
        similarity = 1 - jaccard(words_a, words_b)
        if similarity > 0:
            graph.add_edge(index_a, index_b, weight=similarity)

    ranked_sentence_indexes = pagerank(graph).items()
    sentences_by_rank = sorted(
        ranked_sentence_indexes, key=itemgetter(1), reverse=True)
    best_sentences = map(itemgetter(0), sentences_by_rank[:sentence_count])
    best_sentences_in_order = sorted(best_sentences)

    return ' '.join(sentence_list[index] for index in best_sentences_in_order)
Beispiel #58
0
def load_to_networkx(j_graph: nx.Graph, file_name: str):
    """
    load to network x graph from a json
    """
    try:
        with open(file_name, "r") as file:
            my_dict = json.load(file)
    except IOError as e:
        print(e)

    for nodes in my_dict["Nodes"]:
        if ["pos"] in my_dict["Nodes"]:
            pos = tuple(nodes["pos"].split(","))
            j_graph.add_node(nodes["id"], pos)
        else:
            j_graph.add_node(nodes["id"])

    for edges in my_dict["Edges"]:
        j_graph.add_edge(edges["src"], edges["dest"], weight=edges["w"])
Beispiel #59
0
    def to_networkx(self):
        """Return a NetworkX Graph object representing the minimum spanning tree.

        Edge weights in the graph are the distance between the nodes they connect.

        Nodes have a `data` attribute attached giving the data vector of the
        associated point.
        """
        try:
            from networkx import Graph, set_node_attributes
        except ImportError:
            raise ImportError('You must have networkx installed to export networkx graphs')

        result = Graph()
        for row in self._mst:
            result.add_edge(row[0], row[1], weight=row[2])

        data_dict = {index:tuple(row) for index, row in enumerate(self._data)}
        set_node_attributes(result, 'data', data_dict)

        return result