Beispiel #1
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 #2
0
    def root_tree(self, tree: nx.Graph, root_sample: str,
                  remaining_samples: List[str]):
        """Roots a tree produced by UPGMA.

        Adds the root at the top of the UPGMA reconstructed tree. By the
        ultrametric assumption, the root is placed as the parent to the last
        two unjoined nodes.

        Args:
            tree: Networkx object representing the tree topology
            root_sample: Ignored in this case, the root is known in this case
            remaining_samples: The last two unjoined nodes in the tree

        Returns:
            A rooted tree.
        """

        tree.add_node("root")
        tree.add_edges_from([("root", remaining_samples[0]),
                             ("root", remaining_samples[1])])

        rooted_tree = nx.DiGraph()
        for e in nx.dfs_edges(tree, source="root"):
            rooted_tree.add_edge(e[0], e[1])

        return rooted_tree
Beispiel #3
0
def upsert_node(slug: str, g: nx.Graph):
    if g.has_node(slug):
        return g.nodes[slug]

    g.add_node(slug, slug=slug, size=0)

    return g.nodes[slug]
Beispiel #4
0
def simple_graph():
    """Return a basic graph for testing input/output."""
    g = Graph()
    g.add_node("034", bipartite=0, name="comp", data={"some": "data"})
    g.add_node("1a", bipartite=1, name="jule", data={"mode": "data-y"})
    g.add_edge("034", "1a", weight=2, data={"edgey": "data"})
    return g
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 build_graph(metadata):
    """Get a NetworkX Graph object populated from a SQLAlchemy MetaData object.

    :param metadata: Reflected SQLAlchemy MetaData object.
    :type metadata: MetaData
    :returns: Populated and structured NetworkX Graph object.
    :rtype: Column
    """
    if not isinstance(metadata, MetaData):
        raise TypeError("Graph requires MetaData object, "
                        f"object passed was of type {type(metadata)}.")

    graph = Graph(metadata=metadata)  # Stores metadata in Graph object.
    for table in metadata.tables.keys():  # Creates nodes from all Tables.
        table_object = metadata.tables[table]
        graph.add_node(table_object.name, table=table_object)

    # Stores Table objects in Nodes
    for target_table in metadata.tables.keys():
        target_table_obj = metadata.tables[target_table]

        # Uses the Table object foreign keys to create Graph edges.
        for foreign_key in target_table_obj.foreign_keys:
            referent_column = foreign_key.column  # Retreives source Column
            referent_table_obj = referent_column.table
            referent_table = referent_table_obj.name

            # Stores the foreign key as a table name key pair [Target][Source]
            graph.add_edge(target_table, referent_table, key=foreign_key)

    return graph
Beispiel #7
0
def get_every_station_and_time():
    try:
        # Ожидание прогрузки элементов страницы.
        some_elem_on_page = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable(
                (By.XPATH, "//button[@class='decor_button_button']")))
    finally:
        all_elem = driver.find_elements_by_xpath("//td[@class='flag']/a")

        # Создание графа
        G = Graph()
        temp = None

        # Пройти по всем значениям станция/время
        for i in range(len(all_elem)):
            current_city = driver.find_element_by_xpath(
                "//tr[" + str(i + 1) + "]/td[@class='flag']/a").text
            current_time = driver.find_element_by_xpath(
                "//table[ @ id = 'schedule_table'] / tbody / tr[" +
                str(i + 1) + "] / td[3]").text

            # Наполнение графа(в разработке)
            if temp is None:
                G.add_node(current_city, Time=5)
            else:
                G.add_edge(temp, current_city, Time=6)
            temp = current_city

            print("Город - ", current_city, ";\t Время - ", current_time)

        driver.close()
    def _hypernymy_dfs(self, G: nx.Graph, node_cui: str, visited: set, leaf: Dict[str, ConceptNode]) -> int:
        """ Depth first search helper function """
        visited.add(node_cui)
        parents = self._get_parents(node_cui)
        parents = [p for p in parents if p not in visited]
        parent_depth = []

        if parents:
            for parent in parents:
                parent_depth.append(self._hypernymy_dfs(G, parent, visited, leaf))

            current_depth = np.max(parent_depth) + 1

            # Add node to graph
            if current_depth > 0 or node_cui in leaf:
                if node_cui in leaf:
                    attr = "leaf"
                else:
                    attr = "hypernymy"

                G.add_node(node_cui, type=attr)

                # Create edge to hypernymy
                for p_depth, parent in zip(parent_depth, parents):
                    if parent in G and p_depth > 0:
                        G.add_edge(node_cui, parent, weight=p_depth/(p_depth+1), relation="hypernymy")

            return current_depth
        else:
            return -1  # define root depth as -1, ignore the top-2 node
def run(location_geotable):
    graph = Graph()
    for index, row in location_geotable.iterrows():
        graph.add_node(index, **{
            'lat': row['Latitude'],
            'lon': row['Longitude']
        })
    for node1_id in range(min(graph), max(graph)):
        for node2_id in range(node1_id + 1, max(graph) + 1):
            node1_d = graph.node[node1_id]
            node2_d = graph.node[node2_id]
            node1_ll = node1_d['lat'], node1_d['lon']
            node2_ll = node2_d['lat'], node2_d['lon']
            distance = get_distance(node1_ll, node2_ll).m
            graph.add_edge(node1_id, node2_id, weight=distance)
    tree = minimum_spanning_tree(graph)
    total_distance = sum(
        edge_d['weight']
        for node1_id, node2_id, edge_d in tree.edges(data=True))
    location_count = len(graph)
    average_distance = divide_safely(total_distance, location_count, 0)
    return [
        ('total_distance_between_locations_in_meters', total_distance),
        ('location_count', location_count),
        ('average_distance_between_locations_in_meters', average_distance),
    ]
Beispiel #10
0
    def generatePairings(self):
        for scoreGroup in Pairing.getOrdering(self.round):
            playersInGroup = self.groups.get(scoreGroup / 2)
            if (playersInGroup == None):
                continue
            self.K = scoreGroup
            self.M = len(playersInGroup)
            self.m = Pairing.numberOfFloaters(playersInGroup)
            allPossibleEdges = Pairing.pairwise(list(range(1, self.M + 1)))
            finalEdges = self.filterOutCollisions(allPossibleEdges)
            if (len(finalEdges) == 0):
                self.transfer(playersInGroup, scoreGroup)

            # Create graph
            g = Graph()
            for i in range(0, self.M):
                g.add_node(playersInGroup[i]['player_id'])
            for edge in finalEdges:
                weight = 5000 - self.penaltyPoints(edge[0], edge[1])
                g.add_edge(playersInGroup[edge[0] - 1]['player_id'], playersInGroup[edge[1] - 1]['player_id'],
                           weight=weight)
            result = max_weight_matching(g, maxcardinality=True)  # Edmonds Blossom algorithm
            unpaired_players = self.get_unpaired_players(result, scoreGroup / 2)  # Get any unpaired players
            if len(unpaired_players) > 0: self.transfer(unpaired_players, scoreGroup)  # Transfer them to next group
            # Might need to do some more processing of the result here
            self.pairings = self.pairings + list(result)
        return self.pairings
Beispiel #11
0
def triangle_graph(regions: List[Polygon], graph: Graph) -> List[Triangle]:
    """
    Triangulate regions, connecting into a tree (acyclic digraph) for lookups from triangles to
    the original polygon tiling.
    
    :param regions:  list of regions to be triangulated
    
    :param graph: graph to be populated with edge from the original polygons to the triangles in their triangulations.
    
    :return: list of all triangles in all regions
    """
    logging.debug("Triangulating subdivision of %d regions" % len(regions))
    triangles = []
    for i, region in enumerate(regions):
        logging.debug("Triangulating region %d" % i)
        logging.getLogger().handlers[0].flush()
        graph.add_node(region, original=True)

        if isinstance(region, Triangle):
            triangles.append(region)
        elif region.n == 3:
            region.__class__ = Triangle
            triangles.append(region)
        else:
            # triangulation = region.triangulate()
            triangulation = triangulate(region)
            for triangle in triangulation:
                graph.add_node(triangle, original=False)
                graph.add_edge(triangle, region)
                triangles.append(triangle)

    return triangles
Beispiel #12
0
    def add_node(self, node=None, pos=None, ori=None, commRange=None):
        """
        Add node to network.

        Attributes:
          `node` -- node to add, default: new node is created
          `pos` -- position (x,y), default: random free position in environment
          `ori` -- orientation from 0 to 2*pi, default: random orientation

        """
        if (not node):
            node = Node(commRange=commRange)
        assert(isinstance(node, Node))
        if not node.network:
            node.network = self
        else:
            logger.warning('Node is already in another network, can\'t add.')
            return None

        pos = pos if pos is not None else self.find_random_pos(n=100)
        ori = ori if ori is not None else rand() * 2 * pi
        ori = ori % (2 * pi)

        if (self._environment.is_space(pos)):
            Graph.add_node(self, node)
            self.pos[node] = array(pos)
            self.ori[node] = ori
            self.labels[node] = str(node.id)
            logger.debug('Node %d is placed on position %s.' % (node.id, pos))
            self.recalculate_edges([node])
        else:
            logger.error('Given position is not free space.')
        return node
Beispiel #13
0
def dpg_maximum(g: nx.Graph):
    mm = nx.max_weight_matching(g, maxcardinality=True)
    g.remove_edges_from(mm.items())
    g.add_node(nx.number_of_nodes(g))
    for v in mm.keys():
        g.add_edge(v, nx.number_of_nodes(g) - 1)
    return g
Beispiel #14
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 #15
0
    def add_node(self, node=None, pos=None, ori=None, commRange=None):
        """
        Add node to network.

        Attributes:
          `node` -- node to add, default: new node is created
          `pos` -- position (x,y), default: random free position in environment
          `ori` -- orientation from 0 to 2*pi, default: random orientation

        """
        if (not node):
            node = Node(commRange=commRange)
        assert(isinstance(node, Node))
        if not node.network:
            node.network = self
        else:
            logger.warning('Node is already in another network, can\'t add.')
            return None

        pos = pos if pos is not None else self.find_random_pos(n=100)
        ori = ori if ori is not None else rand() * 2 * pi
        ori = ori % (2 * pi)

        if (self._environment.is_space(pos)):
            Graph.add_node(self, node)
            self.pos[node] = array(pos)
            self.ori[node] = ori
            self.labels[node] = str(node.id)
            logger.debug('Node %d is placed on position %s.' % (node.id, pos))
            self.recalculate_edges([node])
        else:
            logger.error('Given position is not free space.')
        return node
def add_break(graph: Graph, segment_ids: [(str, str)]) -> str:
    """
    Adds a node that breaks proper segment.
    Proper segment is a segment with the smallest angle with positive x-axis.

    `segment_ids` - list of tuples. Each tuple consists of two vertexes ids that represents a segment. This means
    that there has to be an edge between these vertexes.

    Returns id of newly created vertex.
    """
    (v1, v2) = get_segment_with_smallest_angle(graph, segment_ids)

    layer = graph.nodes[v1]['layer']
    v1_pos = graph.nodes[v1]['position']
    v2_pos = graph.nodes[v2]['position']

    v_x = (v1_pos[0] + v2_pos[0]) / 2
    v_y = (v1_pos[1] + v2_pos[1]) / 2

    v_pos = (v_x, v_y)
    v = gen_name()

    graph.add_node(v, layer=layer, position=v_pos, label='E')

    graph.remove_edge(v1, v2)
    graph.add_edge(v1, v)
    graph.add_edge(v2, v)

    return v
Beispiel #17
0
    def to_networkx(self, graph: nx.Graph = None) -> nx.Graph:
        """
        Return networkx graph of entities.

        Parameters
        ----------
        graph : nx.Graph, optional
            Graph to add entities to. If not supplied the function
            creates and returns a new graph.
            By default None

        Returns
        -------
        nx.Graph
            Graph with entity and any connected entities.

        """
        graph = graph or nx.Graph()

        if not graph.has_node(self):
            graph.add_node(self, **self.node_properties)
        for edge in self.edges:
            if graph.has_edge(edge.source, edge.target):
                continue
            graph.add_edge(edge.source, edge.target, **edge.attrs)

            for node in (edge.source, edge.target):
                # If this node has edges that are not in our graph
                # call to_networkx recursively on that node.
                if any(edge for edge in node.edges
                       if not graph.has_edge(edge.source, edge.target)):
                    ent_node = typing.cast(Entity, node)
                    ent_node.to_networkx(graph)
        return graph
def shortest_paths(graph: Graph):
    """
    Returns the shortest paths tree from the node with the highest betweenness centrality among trees.
    :param graph: graph to consider
    :return: shortest path tree
    """

    betweenness = nx.betweenness_centrality(graph)
    center = max(betweenness, key=betweenness.get)
    distances, paths = nx.algorithms.shortest_paths.single_source_dijkstra(
        graph, center)

    edges = set()
    for path in paths.values():
        prev = None
        for node in path:
            if prev is not None:
                edges.add((prev, node, graph[prev][node]['weight']))
            prev = node
    new = Graph()
    if edges:
        for edge in edges:
            new.add_edge(edge[0], edge[1], weight=edge[2])
    else:
        new.add_node(center)
    return new
Beispiel #19
0
def find_connected():
    for stemmer in [
            'Lancaster', 'WordNetLemmatizer', 'PorterStemmer',
            'SnowballStemmer'
    ]:
        with open('output_files/window50/%s_dice_filtered.txt' % stemmer, 'r') as sin, \
                open('output_files/window50/%s_dice_connected.txt' % stemmer, 'w') as out, \
                open('output_files/window50/%s_dice_sconnected.txt' % stemmer, 'w') as out2:
            print('building clusters for %s' % stemmer)
            g = DiGraph()
            g2 = Graph()
            nodes = set()
            edges = set()
            for line in sin:
                stemC1C2, dice = line.rstrip().split(' ')
                stem, c1, c2 = stemC1C2.split(',')
                nodes.add(stem)
                nodes.add(c1)
                nodes.add(c2)
                edges.add((stem, c1, float(dice)))
                edges.add((stem, c2, float(dice)))
                edges.add((c1, stem, float(dice)))
                edges.add((c2, stem, float(dice)))
            for n in nodes:
                g.add_node(n)
                g2.add_node(n)
            for stem, term, dice in edges:
                g.add_edge(stem, term, weight=dice)
                g2.add_edge(stem, term, weight=dice)
            for connected in sorted(nx.connected_components(g2),
                                    key=lambda s: list(s)[0]):
                out.write('%s\n' % ' '.join(connected))
            for connected in sorted(nx.strongly_connected_components(g),
                                    key=lambda s: list(s)[0]):
                out2.write('%s\n' % ' '.join(connected))
Beispiel #20
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 #21
0
def make_pairings(plrs):
    graph = Graph()
    plr_ids = [plr["id"] for plr in plrs]
    for pid in plr_ids:
        graph.add_node(pid)
    for pair in combinations(plr_ids, 2):
        p1 = get_player(pair[0])
        p2 = get_player(pair[1])
        corp_player_id, runner_player_id, side_bias_cost = get_side_tuple(
            p1, p2)
        if side_bias_cost is None:
            # print(f"{p1.p_name} v. {p2.p_name} cannot play")
            continue
        score_cost = calc_score_cost(p1["score"], p2["score"])
        # print(f"{p1.p_name} v. {p2.p_name}: {side_bias_cost+score_cost}")
        graph.add_edge(
            p1,
            p2,
            weight=1000 - (side_bias_cost + score_cost),
            corp_player=corp_player_id,
            runner_player=runner_player_id,
        )
    pairings = max_weight_matching(graph, maxcardinality=True)
    aug_pairings = {}
    for i, pair in enumerate(pairings):
        aug_pairings[i] = {
            "corp": graph[pair[0]][pair[1]]["corp_player"],
            "runner": graph[pair[0]][pair[1]]["runner_player"],
        }
    return aug_pairings
Beispiel #22
0
    def plot_partial_graph(self, action='save'):
        """ Plot graphs for edge unveiling. """
        uid = 200
        while os.path.isfile(f'{uid}_{self.D.name}.png'):
            uid += 1

        r = lambda: np.random.randint(low=0, high=256)
        ng = Graph()
        for i in range(self.D.N):
            ng.add_node(i + 1)
        for u, v in self.D.E:
            ng.add_edge(u, v, entropy='')
        for w in range(len(self.D.UK)):
            ng.add_edge(self.D.UK[w][0],
                        self.D.UK[w][1],
                        entropy='{:.3f}'.format(self.action_entropys[w]))
        colormap = []
        for ee in ng.edges:
            if ee in self.D.UK:
                colormap.append('red')
            else:
                colormap.append('black')
        plt.figure()
        plt.title('Edge Unveiling')
        pos = spring_layout(ng)
        draw_networkx(ng, pos, node_color='#D3D3D3', edge_color=colormap)
        edge_labels = get_edge_attributes(ng, 'entropy')
        draw_networkx_edge_labels(ng, pos, edge_labels=edge_labels)
        if action == 'save':
            plt.savefig(f'{uid}_{self.D.name}.png')
            plt.close()
        elif action == 'show':
            plt.show()
Beispiel #23
0
def create_src_tokengraph(dataset,
                          vocab,
                          G: nx.Graph = None,
                          window_size: int = 2):
    """ Given a corpus create a token Graph.

    Append to graph G if provided.

    :param edge_attr: Name of the edge attribute, should match with param name
     when calling add_edge().
    :param window_size: Sliding window size
    :param G:
    :param dataset: TorchText dataset
    :param vocab: TorchText field containing vocab.
    :return:
    """
    ## Create graph if not exist:
    if G is None:
        G = nx.Graph()

    ## Add token's id as node to the graph
    for token_txt, token_id in vocab['str2idx_map'].items():
        # try:
        #     token_emb = glove_embs[token_txt]
        # except KeyError:
        #     emb_shape = glove_embs[list(glove_embs.keys())[0]].shape
        #     glove_embs['<UNK>'] = np.random.uniform(low=0.5, high=0.5,
        #                                             size=emb_shape)
        #     token_emb = glove_embs['<UNK>']
        # G.add_node(token_id, node_txt=token_txt, s_co=field.vocab.freqs[
        #     token_txt], t_co=0, emb=token_emb)
        G.add_node(token_id,
                   node_txt=token_txt,
                   s_co=vocab['freqs'][token_txt],
                   t_co=0)

    ## Add edges based on token co-occurrence within a sliding window:
    for txt_toks in dataset:
        j = 0
        txt_len = len(txt_toks)
        if window_size is None or window_size > txt_len:
            window_size = txt_len

        slide = txt_len - window_size + 1

        for k in range(slide):
            txt_window = txt_toks[j:j + window_size]
            ## Co-occurrence in tweet:
            occurrences = find_cooccurrences(txt_window)

            ## Add edges with attribute:
            for token_pair, wt in occurrences.items():
                node1 = vocab['str2idx_map'][token_pair[0]]
                node2 = vocab['str2idx_map'][token_pair[1]]
                if G.has_edge(node1, node2):
                    wt = G.get_edge_data(node1, node2)['s_pair'] + wt
                G.add_edge(node1, node2, s_pair=wt, t_pair=0)
            j = j + 1

    return G
Beispiel #24
0
def build_graph_from_tree(G: nx.Graph, level, image_pattern):
    for node in level:
        G.add_node(node[0], image=image_pattern.format(node[0]))
        if node[1] is not None:
            build_graph_from_tree(G, node[1], image_pattern)
            for child in node[1]:
                G.add_edge(node[0], child[0])
Beispiel #25
0
    def test_integrity(self):
        graph = Graph()
        initial_node_name = gen_name()
        graph.add_node(initial_node_name,
                       layer=0,
                       position=(0.5, 0.5),
                       label='E')

        [i1, i2] = P1().apply(graph, [initial_node_name])
        [i1_1, i1_2] = P2().apply(graph, [i1])
        [i2_1, i2_2] = P2().apply(graph, [i2])
        [i3_1, i3_2] = P2().apply(graph, [i1_1])

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()

        [i4_1, i4_2] = P2().apply(graph, [i1_2])

        self.check_graph_integrity(graph, i1, 'i')
        self.check_graph_integrity(graph, i2, 'i')
        self.check_graph_integrity(graph, i1_1, 'i')
        self.check_graph_integrity(graph, i1_2, 'i')
        self.check_graph_integrity(graph, i2_1, 'I')
        self.check_graph_integrity(graph, i2_2, 'I')
        self.check_graph_integrity(graph, i3_1, 'I')
        self.check_graph_integrity(graph, i3_2, 'I')
        self.check_graph_integrity(graph, i4_1, 'I')
        self.check_graph_integrity(graph, i4_2, 'I')

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()
def derive_b():
    graph = Graph()
    initial_node_name = gen_name()
    graph.add_node(initial_node_name, layer=0, position=(0.5, 0.5), label='E')

    visualize_graph_3d(graph)
    pyplot.show()

    [i1, i2] = P1().apply(graph, [initial_node_name])

    visualize_graph_3d(graph)
    pyplot.show()

    [i1_1, i1_2] = P2().apply(graph, [i1], orientation=1)
    [i2_1] = P9().apply(graph, [i2])

    visualize_graph_3d(graph)
    pyplot.show()

    P10().apply(graph, [i1_1, i1_2, i2_1])

    visualize_graph_3d(graph)
    pyplot.show()

    return graph
Beispiel #27
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 #28
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 #29
0
    def test_bad_input_i_label(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)
        graph.add_edge(e2, e3)

        i = add_interior(graph, e1, e2, e3)
        graph.nodes[i]['label'] = 'i'

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()

        with self.assertRaises(AssertionError):
            P2().apply(graph, [i])

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()
Beispiel #30
0
    def test_happy_path(self):
        graph = Graph()
        initial_node = gen_name()
        graph.add_node(initial_node, layer=0, position=(0.5, 0.5), label='E')

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()

        P1().apply(graph, [initial_node])

        nodes_data = graph.nodes(data=True)

        self.assertEqual(len(graph.nodes()), 7)
        self.assertEqual(len(graph.edges()), 13)

        # check the initial node
        initial_node_data = nodes_data[initial_node]
        self.assertEqual(initial_node_data['layer'], 0)
        self.assertEqual(initial_node_data['position'], (0.5, 0.5))
        self.assertEqual(initial_node_data['label'], 'e')

        # check other nodes
        vx_bl = get_node_at(graph, 1, (0, 0))
        vx_br = get_node_at(graph, 1, (1, 0))
        vx_tl = get_node_at(graph, 1, (0, 1))
        vx_tr = get_node_at(graph, 1, (1, 1))
        self.assertIsNotNone(vx_bl)
        self.assertIsNotNone(vx_br)
        self.assertIsNotNone(vx_tl)
        self.assertIsNotNone(vx_tr)
        self.assertEqual(nodes_data[vx_bl]['label'], 'E')
        self.assertEqual(nodes_data[vx_br]['label'], 'E')
        self.assertEqual(nodes_data[vx_tl]['label'], 'E')
        self.assertEqual(nodes_data[vx_tr]['label'], 'E')

        vx_i1 = get_node_at(graph, 1, (2 / 3, 1 / 3))
        vx_i2 = get_node_at(graph, 1, (1 / 3, 2 / 3))
        self.assertIsNotNone(vx_i1)
        self.assertIsNotNone(vx_i2)
        self.assertEqual(nodes_data[vx_i1]['label'], 'I')
        self.assertEqual(nodes_data[vx_i2]['label'], 'I')

        self.assertTrue(graph.has_edge(initial_node, vx_i1))
        self.assertTrue(graph.has_edge(initial_node, vx_i2))
        self.assertTrue(graph.has_edge(vx_tl, vx_tr))
        self.assertTrue(graph.has_edge(vx_tr, vx_br))
        self.assertTrue(graph.has_edge(vx_br, vx_bl))
        self.assertTrue(graph.has_edge(vx_bl, vx_tl))
        self.assertTrue(graph.has_edge(vx_bl, vx_tr))
        self.assertTrue(graph.has_edge(vx_i1, vx_bl))
        self.assertTrue(graph.has_edge(vx_i1, vx_br))
        self.assertTrue(graph.has_edge(vx_i1, vx_tr))
        self.assertTrue(graph.has_edge(vx_i2, vx_bl))
        self.assertTrue(graph.has_edge(vx_i2, vx_tl))
        self.assertTrue(graph.has_edge(vx_i2, vx_tr))

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()
def create_render(state):  # Should be done once.
    g = Graph()

    pos = {}
    edges = []
    index = 0

    target = state
    dim = len(target)
    y_start = dim
    x_start = 0
    for i in range(dim):
        row_dim = len(target[i])
        for j in range(row_dim):
            g.add_node(index)

            # Add Edges
            if j > 0 and i + 1 < dim:  # Next column, previous row.
                edges.append((index, index + row_dim - 1))
            if j + 1 < row_dim:  # Next in row.
                edges.append((index, index + 1))
            if i + 1 < dim and j < len(
                    target[i + 1]):  # Next column, next row.
                edges.append((index, index + row_dim))

            pos[index] = (j + x_start, y_start - j)
            index += 1
        x_start -= 1
        y_start -= 1

    g.add_edges_from(edges)

    return g, pos
Beispiel #32
0
    def visualize(self,
                  g: nx.Graph,
                  maps: Tuple[Dict[str, str], Dict[int, List[str]]],
                  path: str = '',
                  depth: int = 0) -> None:
        """
        Helper for creating a visualization of this ExprTree using networkx
        and matplotlib.

        You do not need to understand this code.
        """
        label_map, at_depth = maps
        # record all nodes at each depth and create
        # nodes (with unique names) and edges in the networkx graph g.
        if depth not in at_depth:
            at_depth[depth] = []
        node_label = path + str(depth) + str(self._root)
        at_depth[depth].append(node_label)
        label_map[node_label] = str(self._root)
        g.add_node(node_label)
        i = 0
        for c in self._subtrees:
            # recursively call visualize on each subtree
            c.visualize(g, (label_map, at_depth), path + str(i), depth + 1)
            # connect self to each of its children in the graph
            g.add_edge(node_label,
                       path + str(i) + str(depth + 1) + str(c._root))
            i += 1
Beispiel #33
0
    def test_different_position(self):
        graph = Graph()
        initial_node = gen_name()
        graph.add_node(initial_node, layer=0, position=(0, 0), label='E')

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()

        P1().apply(graph, [initial_node],
                   positions=[
                       (0, 0),
                       (2, 1.5),
                       (1.5, 2),
                       (-0.5, 1.5),
                   ])

        # check other nodes
        vx_bl = get_node_at(graph, 1, (0, 0))
        vx_br = get_node_at(graph, 1, (2, 1.5))
        vx_tl = get_node_at(graph, 1, (1.5, 2))
        vx_tr = get_node_at(graph, 1, (-0.5, 1.5))
        self.assertIsNotNone(vx_bl)
        self.assertIsNotNone(vx_br)
        self.assertIsNotNone(vx_tl)
        self.assertIsNotNone(vx_tr)

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()
Beispiel #34
0
def test_exceptions():
    test = Graph()
    test.add_node('a')
    assert_raises(NetworkXError, asyn_fluidc, test, 'hi')
    assert_raises(NetworkXError, asyn_fluidc, test, -1)
    assert_raises(NetworkXError, asyn_fluidc, test, 3)
    test.add_node('b')
    assert_raises(NetworkXError, asyn_fluidc, test, 1)
Beispiel #35
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 #37
0
def test_single_node():
    test = Graph()

    test.add_node('a')

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

    communities = asyn_fluidc(test, 1)
    result = {frozenset(c) for c in communities}
    assert_equal(result, ground_truth)
Beispiel #38
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 #39
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 #40
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 #41
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 #42
0
    def add_node(self, node=None, pos=None, ori=None, commRange=None, find_random=False):
        """
        Add node to network.

        Attributes:
          `node` -- node to add, default: new node is created
          `pos` -- position (x,y), default: random free position in environment
          `ori` -- orientation from 0 to 2*pi, default: random orientation

        """
        if (not node):
            node = Node(commRange=commRange or self.comm_range)
        if not node.commRange:
            node.commRange = commRange or self.comm_range

        assert(isinstance(node, Node))
        if not node.network:
            node.network = self
        else:
            logger.warning('Node is already in another network, can\'t add.')
            return None

        pos = pos if (pos is not None and not isnan(pos[0])) else self.find_random_pos(n=100)
        ori = ori if ori is not None else rand() * 2 * pi
        ori = ori % (2 * pi)

        got_random = False
        if find_random and not self._environment.is_space(pos):
            pos = self.find_random_pos(n=100)
            got_random = True

        if (self._environment.is_space(pos)):
            Graph.add_node(self, node)
            self.pos[node] = array(pos)
            self.ori[node] = ori
            self.labels[node] = ('C' if node.type == 'C' else "") + str(node.id)
            logger.debug('Node %d is placed on position %s %s %s'
                         % (node.id, pos,
                            '[energy=%5.3f]' %node.power.energy
                                    if node.power.energy != EnergyModel.E_INIT  else '',
                            'Random' if got_random else ''))
            self.recalculate_edges([node])
        else:
            Node.cid -= 1
            logger.error('Given position is not free space. [%s] %s' % (Node.cid, pos))
            node = None
        return node
    def get_coauthor_graph_by_author_name(self, name):
        coauthors = set()
        for p in self.publications:
            for a in p.authors:
                if a == self.author_idx[name]:
                    for a2 in p.authors:
                        if a != a2:
                            coauthors.add(a2)

        graph = Graph()
        # the nodes format will be {"id":int, "name":str}
        graph.add_node(self.author_idx[name], name = name)
        # graph.add_nodes_from([(i, {"name": all_data[0][i][0]}) for i in range(len(all_data[0]))])
        graph.add_nodes_from([(ca, {"name": self.authors[ca].name}) for ca in coauthors])
        graph.add_edges_from([(self.author_idx[name], ca) for ca in coauthors])

        return graph
Beispiel #44
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
    def post_process(self):
        trackings = self.parents['irit_harmo_tracking'].results['irit_harmo_tracking'].data_object.value

        graph = Graph()

        for t, h in [(track, track.harmo_link(trackings)) for track in trackings]:

            graph.add_node(t)

            if len(h) > 0:

                graph.add_edges_from([(t, o) for o in h])

        res = self.new_result(time_mode='global')
        res.data_object.value = [c2 for c in connected_components(graph) for c2 in Cluster(c).harmo_sub()]
        self.add_result(res)

        return
Beispiel #46
0
def fast_sbm(c_cc, c_cp, c_pp, n):
  mav_cc = c_cc * n / 4
  mav_cp = c_cp * n / 2
  mav_pp = c_pp * n / 4

  m_in1 = poisson.rvs(mav_cc)
  m_in2 = poisson.rvs(mav_pp)
  m_out = poisson.rvs(mav_cp)

  G = Graph()
  for i in range(n):
    G.add_node(i)

  # Generate first comm edges
  counter = 0
  while counter < m_in1:
    u = randrange(0,n//2)
    v = randrange(0,n//2)
    if u != v:
      G.add_edge(u,v)
      counter += 1

  # Generate second comm edges
  counter = 0
  while counter < m_in2:
    u = randrange(n//2,n)
    v = randrange(n//2,n)
    if u != v:
      G.add_edge(u,v)
      counter += 1

  # Generate between comm edges
  counter = 0
  while counter < m_out:
    u = randrange(0,n//2)
    v = randrange(n//2,n)
    if u != v:
      G.add_edge(u,v)
      counter += 1

  # Create sparse adjacency matrix
  return G
Beispiel #47
0
def fast_sbm(c_in, c_out, n):
  mav_in = c_in * n / 4.
  mav_out = c_out * n / 2.

  m_in1 = poisson.rvs(mav_in)
  m_in2 = poisson.rvs(mav_in)
  m_out = poisson.rvs(mav_out)

  G = Graph()
  for i in range(n):
    G.add_node(i)

  # Generate first comm edges
  counter = 0
  while counter < m_in1:
    u = randrange(0,n//2)
    v = randrange(0,n//2)
    if u != v:
      G.add_edge(u,v)
      counter += 1

  # Generate second comm edges
  counter = 0
  while counter < m_in2:
    u = randrange(n//2,n)
    v = randrange(n//2,n)
    if u != v:
      G.add_edge(u,v)
      counter += 1

  # Generate between comm edges
  counter = 0
  while counter < m_out:
    u = randrange(0,n//2)
    v = randrange(n//2,n)
    if u != v:
      G.add_edge(u,v)
      counter += 1

  # Create sparse adjacency matrix
  return G
def run(location_geotable):
    graph = Graph()
    for index, row in location_geotable.iterrows():
        graph.add_node(index, {
            'lat': row['Latitude'], 'lon': row['Longitude']})
    for node1_id in xrange(min(graph), max(graph)):
        for node2_id in xrange(node1_id + 1, max(graph) + 1):
            node1_d = graph.node[node1_id]
            node2_d = graph.node[node2_id]
            node1_ll = node1_d['lat'], node1_d['lon']
            node2_ll = node2_d['lat'], node2_d['lon']
            distance = get_distance(node1_ll, node2_ll).m
            graph.add_edge(node1_id, node2_id, weight=distance)
    tree = minimum_spanning_tree(graph)
    total_distance = sum(edge_d[
        'weight'] for node1_id, node2_id, edge_d in tree.edges(data=True))
    location_count = len(graph)
    average_distance = divide_safely(total_distance, location_count, 0)
    return [
        ('total_distance_between_locations_in_meters', total_distance),
        ('location_count', location_count),
        ('average_distance_between_locations_in_meters', average_distance),
    ]
     def generate_dictdata_graph():
         gr = Graph()
         src_language_iso = cr.src_language_iso_for_dictdata_id(dictdata_id)
         tgt_language_iso = cr.tgt_language_iso_for_dictdata_id(dictdata_id)
         if src_language_iso != 'spa' and tgt_language_iso != 'spa':
             raise(NoSpanishException)
         
         language_iso = None
         if tgt_language_iso == 'spa':
             language_iso = src_language_iso
         else:
             language_iso = tgt_language_iso
                         
         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(target_file, "w", "utf-8")
         output.write(write(gr))
         output.close()
class SimpleSectorNetworkCase(unittest.TestCase):

	def setUp(self):
		self.G = Graph()
		self.G.add_node(0, coord=(0., 0.))
		self.G.add_node(1, coord=(1., 0.))
		self.G.add_node(2, coord=(-0.5, np.sqrt(3.)/2.))
		self.G.add_node(3, coord=(0.5, np.sqrt(3.)/2.))
		self.G.add_node(4, coord=(1.5, np.sqrt(3.)/2.))
		self.G.add_node(5, coord=(0., np.sqrt(3.)))
		self.G.add_node(6, coord=(1., np.sqrt(3.)))

	def show_network(self, show=True):
		plt.scatter(*zip(*[self.G.node[n]['coord'] for n in self.G.nodes()]), marker='s', color='r', s=50)

		if show:
			plt.show()

	def show_polygons(self, show=True):
		for pol in self.G.polygons.values():
			plt.fill(*zip(*list(pol.exterior.coords)), alpha=0.4)

		if show:
			plt.show()
Beispiel #51
0
def solve():
	from networkx import Graph, number_connected_components
	from heap.heap import heap
	
	def up(p):
		r, c = divmod(p,C)
		return (R-1 if r==0 else r-1)*C + c
	
	def down(p):
		r, c = divmod(p,C)
		return (0 if r==R-1 else r+1)*C + c
	
	def left(p):
		r, c = divmod(p,C)
		return r*C + (C-1 if c==0 else c-1)
	
	def right(p):
		r, c = divmod(p,C)
		return r*C + (0 if c==C-1 else c+1)
	
	def link(a,b):
		G.add_edge(('start',a),('end',b))
		H[b] += 1
	
	R, C = map(int,input().split())
	N = R*C
	G = Graph()
	H = heap({n:0 for n in range(N)})
	
	for p in range(N):
		G.add_node(('start',p))
		G.add_node(('end',p))
	
	for p, x in enumerate(x for _ in range(R) for x in input().strip()):
		if x == '|':
			link(p,up(p))
			link(p,down(p))
		elif x == '-':
			link(p,left(p))
			link(p,right(p))
		elif x == '/':
			link(p,down(left(p)))
			link(p,up(right(p)))
		elif x == '\\':
			link(p,up(left(p)))
			link(p,down(right(p)))
	
	while H[H.peek()] <= 1:
		p, ins = H.popitem()
		
		if ins == 0:
			# There is a sqaure such that no other square points to it.
			# It's impossible for a lemming to get here.
			# So there are no proper ways to arrange this.
			return 0
		
		else:
			# There is exactly one edge leading into p.
			# Remove the relevant nodes.
			_, q = next(iter(G['end',p]))
			r = next(r for _, r in G['start',q] if r != p)
			
			G.remove_node(('end',p))
			G.remove_node(('start',q))
			
			# If q now points to p, the other square that q points to
			# has one fewer square pointing to it.
			H[r] -= 1
	
	# Now all remaining nodes are cycles, and there are exatly two ways
	# to choose the directions for each cycle.
	return pow(2,number_connected_components(G),1000003)
Beispiel #52
0
class Topo(object):
    '''Data center network representation for structured multi-trees.'''

    def __init__(self):
        '''Create Topo object.

        '''
        self.g = Graph()
        self.node_info = {}  # dpids hash to Node objects
        self.edge_info = {}  # (src_dpid, dst_dpid) tuples hash to Edge objects
        self.ports = {}  # ports[src][dst] is port on src that connects to dst
        self.id_gen = NodeID  # class used to generate dpid

    def add_node(self, dpid, node):
        '''Add Node to graph.

        @param dpid dpid
        @param node Node object
        '''
        self.g.add_node(dpid)
        self.node_info[dpid] = node

    def add_edge(self, src, dst, edge = None):
        '''Add edge (Node, Node) to graph.

        @param src src dpid
        @param dst dst dpid
        @param edge Edge object
        '''
        src, dst = tuple(sorted([src, dst]))
        self.g.add_edge(src, dst)
        if not edge:
            edge = Edge()
        self.edge_info[(src, dst)] = edge
        self.add_port(src, dst)

    def add_port(self, src, dst):
        '''Generate port mapping for new edge.

        @param src source switch DPID
        @param dst destination switch DPID
        '''
        src_base = SWITCH_PORT_BASE if self.is_switch(src) else 0
        dst_base = SWITCH_PORT_BASE if self.is_switch(dst) else 0
        if src not in self.ports:
            self.ports[src] = {}
        if dst not in self.ports[src]:
            # num outlinks
            self.ports[src][dst] = len(self.ports[src]) + src_base
        if dst not in self.ports:
            self.ports[dst] = {}
        if src not in self.ports[dst]:
            # num outlinks
            self.ports[dst][src] = len(self.ports[dst]) + dst_base

    def node_enabled(self, dpid):
        '''Is node connected, admin on, powered on, and fault-free?

        @param dpid dpid

        @return bool node is enabled
        '''
        ni = self.node_info[dpid]
        return ni.connected and ni.admin_on and ni.power_on and not ni.fault

    def nodes_enabled(self, dpids, enabled = True):
        '''Return subset of enabled nodes

        @param dpids list of dpids
        @param enabled only return enabled nodes?

        @return dpids filtered list of dpids
        '''
        if enabled:
            return [n for n in dpids if self.node_enabled(n)]
        else:
            return dpids

    def nodes(self, enabled = True):
        '''Return graph nodes.

        @param enabled only return enabled nodes?

        @return dpids list of dpids
        '''
        return self.nodes_enabled(self.g.nodes(), enabled)

    def nodes_str(self, dpids):
        '''Return string of custom-encoded nodes.

        @param dpids list of dpids

        @return str string
        '''
        return [str(self.id_gen(dpid = dpid)) for dpid in dpids]

    def is_switch(self, n):
        '''Returns true if node is a switch.'''
        return self.node_info[n].is_switch

    def switches(self, enabled = True):
        '''Return switches.

        @param enabled only return enabled nodes?

        @return dpids list of dpids
        '''
        nodes = [n for n in self.g.nodes() if self.is_switch(n)]
        return self.nodes_enabled(nodes, enabled)

    def hosts(self, enabled = True):
        '''Return hosts.

        @param enabled only return enabled nodes?

        @return dpids list of dpids
        '''

        def is_host(n):
            '''Returns true if node is a host.'''
            return not self.node_info[n].is_switch

        nodes = [n for n in self.g.nodes() if is_host(n)]
        return self.nodes_enabled(nodes, enabled)

    def edge_enabled(self, edge):
        '''Is edge admin on, powered on, and fault-free?

        @param edge (src, dst) dpid tuple

        @return bool edge is enabled
        '''
        src, dst = edge
        src, dst = tuple(sorted([src, dst]))
        ei = self.edge_info[tuple(sorted([src, dst]))]
        return ei.admin_on and ei.power_on and not ei.fault

    def edges_enabled(self, edges, enabled = True):
        '''Return subset of enabled edges

        @param edges list of edges
        @param enabled only return enabled edges?

        @return edges filtered list of edges
        '''
        if enabled:
            return [e for e in edges if self.edge_enabled(e)]
        else:
            return edges

    def edges(self, enabled = True):
        '''Return edges.

        @param enabled only return enabled edges?

        @return edges list of dpid pairs
        '''
        return self.edges_enabled(self.g.edges(), enabled)

    def edges_str(self, dpid_pairs):
        '''Return string of custom-encoded node pairs.

        @param dpid_pairs list of dpid pairs (src, dst)

        @return str string
        '''
        edges = []
        for pair in dpid_pairs:
            src, dst = pair
            src = str(self.id_gen(dpid = src))
            dst = str(self.id_gen(dpid = dst))
            edges.append((src, dst))
        return edges

    def port(self, src, dst):
        '''Get port number.

        @param src source switch DPID
        @param dst destination switch DPID
        @return tuple (src_port, dst_port):
            src_port: port on source switch leading to the destination switch
            dst_port: port on destination switch leading to the source switch
        '''
        if src in self.ports and dst in self.ports[src]:
            assert dst in self.ports and src in self.ports[dst]
            return (self.ports[src][dst], self.ports[dst][src])

    def enable_edges(self):
        '''Enable all edges in the network graph.

        Set admin on, power on, and fault off.
        '''
        for e in self.g.edges():
            src, dst = e
            ei = self.edge_info[tuple(sorted([src, dst]))]
            ei.admin_on = True
            ei.power_on = True
            ei.fault = False

    def enable_nodes(self):
        '''Enable all nodes in the network graph.

        Set connected on, admin on, power on, and fault off.
        '''
        for node in self.g.nodes():
            ni = self.node_info[node]
            ni.connected = True
            ni.admin_on = True
            ni.power_on = True
            ni.fault = False

    def enable_all(self):
        '''Enable all nodes and edges in the network graph.'''
        self.enable_nodes()
        self.enable_edges()

    def name(self, dpid):
        '''Get string name of node ID.

        @param dpid DPID of host or switch
        @return name_str string name with no dashes
        '''
        return self.id_gen(dpid = dpid).name_str()

    def ip(self, dpid):
        '''Get IP dotted-decimal string of node ID.

        @param dpid DPID of host or switch
        @return ip_str
        '''
        return self.id_gen(dpid = dpid).ip_str()
Beispiel #53
0
 def add_node(self, n, attr_dict=None, **attr):
     Graph.add_node(self,n,attr_dict=attr_dict,**attr)
     self.fh.write("Add node: %s\n"%n)
Beispiel #54
0
class Topo(object):
    "Data center network representation for structured multi-trees."

    def __init__(self, hopts=None, sopts=None, lopts=None, ropts=None):
        """Topo object:
           hinfo: default host options
           sopts: default switch options
           lopts: default link options"""
        self.g = Graph()
        self.node_info = {}
        self.link_info = {}  # (src, dst) tuples hash to EdgeInfo objects
        self.hopts = {} if hopts is None else hopts
	self.ropts = {} if ropts is None else ropts
        self.sopts = {} if sopts is None else sopts
        self.lopts = {} if lopts is None else lopts
        self.ports = {}  # ports[src][dst] is port on src that connects to dst

    def addNode(self, name, **opts):
        """Add Node to graph.
           name: name
           opts: node options
           returns: node name"""
        self.g.add_node(name)
        self.node_info[name] = opts
        return name

    def addHost(self, name, **opts):
        """Convenience method: Add host to graph.
           name: host name
           opts: host options
           returns: host name"""

        if not opts:
            if self.hopts:
                opts = self.hopts
            elif self.ropts:
                opts = self.ropts

        return self.addNode(name, **opts)

    def addSwitch(self, name, **opts):
        """Convenience method: Add switch to graph.
           name: switch name
           opts: switch options
           returns: switch name"""
        if not opts and self.sopts:
            opts = self.sopts
        result = self.addNode(name, isSwitch=True, **opts)
        return result

    def addLink(self, node1, node2, port1=None, port2=None,
                **opts):
        """node1, node2: nodes to link together
           port1, port2: ports (optional)
           opts: link options (optional)
           returns: link info key"""
        if not opts and self.lopts:
            opts = self.lopts
        self.addPort(node1, node2, port1, port2)
        key = tuple(self.sorted([node1, node2]))
        self.link_info[key] = opts
        self.g.add_edge(*key)
        return key

    def addPort(self, src, dst, sport=None, dport=None):
        '''Generate port mapping for new edge.
        @param src source switch name
        @param dst destination switch name
        '''
        self.ports.setdefault(src, {})
        self.ports.setdefault(dst, {})
        # New port: number of outlinks + base
        src_base = 1 if self.isSwitch(src) else 0
        dst_base = 1 if self.isSwitch(dst) else 0
        if sport is None:
            sport = len(self.ports[src]) + src_base
        if dport is None:
            dport = len(self.ports[dst]) + dst_base
        self.ports[src][dst] = sport
        self.ports[dst][src] = dport

    def nodes(self, sort=True):
        "Return nodes in graph"
        if sort:
            return self.sorted( self.g.nodes() )
        else:
            return self.g.nodes()

    def isSwitch(self, n):
        '''Returns true if node is a switch.'''
        info = self.node_info[n]
        return info and info.get('isSwitch', False)

    def switches(self, sort=True):
        '''Return switches.
        sort: sort switches alphabetically
        @return dpids list of dpids
        '''
        return [n for n in self.nodes(sort) if self.isSwitch(n)]

    def hosts(self, sort=True):
        '''Return hosts.
        sort: sort hosts alphabetically
        @return dpids list of dpids
        '''
        return [n for n in self.nodes(sort) if not self.isSwitch(n)]

    def links(self, sort=True):
        '''Return links.
        sort: sort links alphabetically
        @return links list of name pairs
        '''
        if not sort:
            return self.g.edges()
        else:
            links = [tuple(self.sorted(e)) for e in self.g.edges()]
            return sorted( links, key=naturalSeq )

    def port(self, src, dst):
        '''Get port number.

        @param src source switch name
        @param dst destination switch name
        @return tuple (src_port, dst_port):
            src_port: port on source switch leading to the destination switch
            dst_port: port on destination switch leading to the source switch
        '''
        if src in self.ports and dst in self.ports[src]:
            assert dst in self.ports and src in self.ports[dst]
            return (self.ports[src][dst], self.ports[dst][src])

    def linkInfo( self, src, dst ):
        "Return link metadata"
        src, dst = self.sorted([src, dst])
        return self.link_info[(src, dst)]

    def setlinkInfo( self, src, dst, info ):
        "Set link metadata"
        src, dst = self.sorted([src, dst])
        self.link_info[(src, dst)] = info

    def nodeInfo( self, name ):
        "Return metadata (dict) for node"
        info = self.node_info[ name ]
        return info if info is not None else {}

    def setNodeInfo( self, name, info ):
        "Set metadata (dict) for node"
        self.node_info[ name ] = info

    @staticmethod
    def sorted( items ):
        "Items sorted in natural (i.e. alphabetical) order"
        return sorted(items, key=natural)
class HybridNetworkCase(unittest.TestCase):
	def setUp(self):
		#super(HybridNetworkCase, self).__init__()
		self.G = Graph()
		self.G.add_node(0, coord=(0., 0.))
		self.G.add_node(1, coord=(1., 0.))
		self.G.add_node(2, coord=(-0.5, np.sqrt(3.)/2.))
		self.G.add_node(3, coord=(0.5, np.sqrt(3.)/2.))
		self.G.add_node(4, coord=(1.5, np.sqrt(3.)/2.))
		self.G.add_node(5, coord=(0., np.sqrt(3.)))
		self.G.add_node(6, coord=(1., np.sqrt(3.)))

		self.G.G_nav = Graph()
		l = np.sqrt(3.)
		#pouet = []

		# Put six points inside the central sector
		center = np.array([0.5, l/2.])
		for i in range(6):
			angle = float(i)/6.*2.*np.pi
			point = center + 0.25*np.array([np.cos(angle), np.sin(angle)])
			#pouet.append(list(point))
			self.G.G_nav.add_node(i, coord=point)

		# Put two points in the 6 outer sectors
		for i in range(12):
			angle = 1./24.*(2.*np.pi) + float(i)/12.*(2.*np.pi)
			point = center + 1.25*np.array([np.cos(angle), np.sin(angle)])
			self.G.G_nav.add_node(6 + i, coord=point)

		self.G.G_nav.add_node(18, coord=center)

		self.G.airports = []
		self.G.G_nav.airports = []

		def get_airports(G):
			return G.airports

		self.G.get_airports = types.MethodType(get_airports, self.G)
		self.G.G_nav.get_airports = types.MethodType(get_airports, self.G.G_nav)

		self.G.G_nav.navpoints_borders = []

	def give_nodes_to_secs(self):
		self.G.node[3]['navs'] = list(range(6)) + [18]
		self.G.node[0]['navs'] = [13, 14]
		self.G.node[1]['navs'] = [15, 16]
		self.G.node[2]['navs'] = [11, 12]
		self.G.node[4]['navs'] = [6, 17]
		self.G.node[5]['navs'] = [9, 10]
		self.G.node[6]['navs'] = [7, 8]
		
		for i in range(6):
			self.G.G_nav.node[i]['sec'] = 3
		self.G.G_nav.node[18]['sec'] = 3
		self.G.G_nav.node[6]['sec'] = self.G.G_nav.node[17]['sec'] = 4
		self.G.G_nav.node[7]['sec'] = self.G.G_nav.node[8]['sec'] = 6
		self.G.G_nav.node[9]['sec'] = self.G.G_nav.node[10]['sec'] = 5
		self.G.G_nav.node[11]['sec'] = self.G.G_nav.node[12]['sec'] = 2
		self.G.G_nav.node[13]['sec'] = self.G.G_nav.node[14]['sec'] = 0
		self.G.G_nav.node[15]['sec'] = self.G.G_nav.node[16]['sec'] = 1

	def give_full_connections(self):
		for i in range(7):
			if i!=3:
				self.G.add_edge(3, i)

		periph_secs = [0, 1, 4, 6, 5, 2]
		for idx, n in enumerate(periph_secs):
			self.G.add_edge(n, periph_secs[(idx+1)%len(periph_secs)])

		for i in range(6):
			self.G.G_nav.add_edge(18, i)
			self.G.G_nav.add_edge(i, (i+1)%6)
			self.G.G_nav.add_edge(i, 2*i+6)
			coin = (2*i+5) if (2*i+5)!=5 else 17
			self.G.G_nav.add_edge(i, coin)

		for i in range(6, 18):
			coin = i+1 if i+1 != 18 else 6
		 	self.G.G_nav.add_edge(i, coin)

		# print "TEST: Creating the following edges:"
		# print self.G.G_nav.edges()

	def do_voronoi(self):
		self.G, vor = compute_voronoi(self.G, xlims=(-1., 2.), ylims=(-0.5, 2.5))
		self.G.polygons = {i:pol for i, pol in self.G.polygons.items() if type(pol)==type(Polygon())}
		self.G.global_shape = cascaded_union(self.G.polygons.values())

	def show_network(self, secs=True, navs=True, show=True):
		if secs:
			plt.scatter(*zip(*[self.G.node[n]['coord'] for n in self.G.nodes()]), marker='s', color='r', s=50)

		if navs:
			plt.scatter(*zip(*[self.G.G_nav.node[n]['coord'] for n in self.G.G_nav.nodes()]), marker='o')
	
		if show:
			plt.show()

	def show_polygons(self, show=True):
		for pol in self.G.polygons.values():
			plt.fill(*zip(*list(pol.exterior.coords)), alpha=0.4)

		if show:
			plt.show()
Beispiel #56
0
class GapGraph():
    """
    Holds Nodes and Edges of Contig Ends and their Connections.
    """
    def __init__(self, gapInfo=None):
        self.gapInfo = gapInfo
        self.graph = Graph()
        if gapInfo is not None:
            self.__createGraph()
        
    def __createGraph(self):
        """
        Create a graph with 
            Nodes = Contig Ends (5 and 3) and an arbitrary gap name
            Edges = inScaffolding A super heavy PointTo 
                    And reads that map across
            Taking these notes to testing/alignTestNotes.txt

        Create a graph with nodes == contigNames, Edges == 5' or 3'
        """
        for key in self.gapInfo:
            gap = self.gapInfo[key]
            
            if gap.endsFlag == (Gap.BEGIN + Gap.END):
                if gap.start == 'na': # singleton
                    prevNode = gap.scaffoldId + "e5"
                    nextNode = gap.scaffoldId + "e3"
                    self.graph.add_node(prevNode, extenders=[])
                    self.graph.add_node(nextNode, extenders=[])
                    self.graph.add_edge(prevNode, nextNode,  evidence=["Contig"])
                else:#one gap weirdo
                    startNode = gap.scaffoldId + "e5"
                    prevNode = gap.leftContig + "e3"
                    nextNode = gap.rightContig + "e5"
                    endNode = gap.scaffoldId + "e3"
                    
                    self.graph.add_node(startNode, extenders=[])
                    self.graph.add_node(prevNode,  extenders=[])
                    self.graph.add_node(nextNode,  extenders=[])
                    self.graph.add_node(endNode,   extenders=[])

                    self.graph.add_edge(startNode, prevNode, evidence=["Contig"])
                    self.graph.add_edge(prevNode,  nextNode, evidence=["Scaffold"])
                    self.graph.add_edge(nextNode,  endNode,  evidence=["Contig"])
                    
                continue
                
            prevNode = gap.leftContig + "e3"
            if gap.endsFlag & Gap.BEGIN:#is first gap - get that first contig   
                startNode = gap.scaffoldId + "e5"
                self.graph.add_node(startNode, extenders=[])
                self.graph.add_node(prevNode,  extenders=[])
                self.graph.add_edge(startNode, prevNode, evidence=["Contig"])
                
            nextNode = gap.rightContig + "e5"
            if gap.endsFlag & Gap.END:#is last gap
                endNode = gap.scaffoldId + "e3"
            else:
                endNode = gap.rightContig + "e3"
            
            self.graph.add_node(nextNode, extenders=[])
            self.graph.add_node(endNode,  extenders=[])
            
            self.graph.add_edge(prevNode, nextNode, evidence=["Scaffold"])
            self.graph.add_edge(nextNode, endNode,  evidence=["Contig"])
            
    def saveBML(self, fileName="briefGraph.bml"):
        """
        Only saves the evidence and extenders for the graph.
        This is for programatically building a graph quickly.
        evidence  start   end name,name,name
        extend  start   name,name,name
        """
        fout = open(fileName,'w')
        newGraph = copy.deepcopy(self.graph)
        for node in newGraph.nodes_iter():
            if len(newGraph.node[node]['extenders']) > 0:
                if newGraph.node[node].has_key('extenders'):
                    fout.write("extend\t%s\t%s\n" % (node, \
                        "::".join(newGraph.node[node]['extenders'])))
        for edgeA, edgeB in newGraph.edges_iter():
            ev = filter(lambda x: x != "Scaffold" and x != "Contig", \
                    newGraph[edgeA][edgeB]['evidence'])
            if len(ev) > 0:
                fout.write("evidence\t%s\t%s\t%s\n" % (edgeA, edgeB, "::".join(ev)))
        
        fout.close()
        
    def loadBML(self, fileName):
        """
        load the evidence and extenders from the BML file
        """
        fh = open(fileName,'r')
        for line in fh.readlines():
            data = line.strip().split('\t')
            if data[0] == 'extend':
                self.add_extend(data[1].replace('/','.'), data[2].split('::'))
            if data[0] == 'evidence':
                self.add_evidence(data[1].replace('/','.'), data[2].replace('/','.'), data[3].split('::'))

        fh.close()
        
    def saveGraph(self, fileName="graph.gml", spanOnly=False): 
        """
        Save a gml (default fileName is graph.gml). 
        if spanOnly, we won't write extenders evidence
        """
        newGraph = copy.deepcopy(self.graph)
        for node in newGraph.nodes_iter():
            if spanOnly:
                newGraph.node[node]['extenders'] = ""
            else:
                newGraph.node[node]['extenders'] = ":".join(newGraph.node[node]['extenders'])
                
        for edge in newGraph.edges_iter():
            newGraph[edge[0]][edge[1]]['evidence'] = ":".join(newGraph.get_edge_data(*edge)['evidence'])
        
        write_gml(newGraph, fileName)
      
    def add_extend(self, nodeName, extName):
        """
        Takes a string or a list
        """
        logging.debug("%s extends %s" % (extName, nodeName))
        if not isinstance(extName, list):
            extName = [str(extName)]
        
        logging.debug(nodeName)
        if nodeName not in self.graph.node:
            self.graph.add_node(nodeName, extenders=extName)
        else:
            self.graph.node[nodeName]['extenders'].extend(extName)
         
    def add_evidence(self, source, target, readName):
        """
        Add linkage support to the graph
        for span, we add left and then right
        same for contains
        readName can be a string or a list
        """
        if not isinstance(readName, list):
            readName = [str(readName)]
        
        if source == target:
            logging.warning(("Read %s self-extends Node %s " \
                             "Possible Evidence of Tandem Repeat on Singleton")\
                             % (readName, source))
        
            self.add_extend(source, readName)
            return
        
        logging.debug("%s gives evidence %s -> %s" % (readName, source, target))
        
        if source not in self.graph.node:
            self.graph.add_node(source, extenders=[])
        if target not in self.graph.node:
            self.graph.add_node(target, extenders=[])
        
        try:
            l = [source, target]; l.sort(); source, target = l
            self.graph.edge[source][target]['evidence'].extend(readName)
        except KeyError:#New edge
            self.graph.add_edge(source, target, evidence=readName)
Beispiel #57
0
def make_steiner_tree(G, voi, generator=None):
        mst = Graph()
        for v in voi:
                if not v in G:
                        raise ValueError, "make_steiner_tree(): Some vertice not in original graph"
        if len(voi) == 0:
                return mst
        if len(voi) == 1:
                mst.add_node(voi[0])
                return mst

        # Initially, use (a version of) Kruskal's algorithm to extract a minimal spanning tree
        # from a weighted graph.  This algorithm differs in that only a subset of vertices are
        # going to be present in the final subgraph (which is not truely a MST - must use Prim's
        # algorithm later.

        # extract all shortest paths among the voi
        heapq = []
        paths = {}

        # load all the paths bwteen the Steiner vertices. Store them in a heap queue
        # and reconstruct the MST of the complete graph using Kruskal's algorithm
        for i in range(len(voi) - 1):
                v1 = voi[i]
                for v2  in voi[i+1:]:
                        result = bidirectional_dijkstra(G, v1, v2)
                        if result == False:
                                raise RuntimeError, "The two vertices given (%s, %s) don't exist on the same connected graph" % (v1, v2)
                                #print "The two vertices given (%s, %s) don't exist on the same connected graph" % (v1, v2)
                        distance, vertList = result
                        keys = [v1, v2]
                        keys.sort()
                        key = "%s:%s" % tuple(keys)
                        paths[key] = (vertList)
                        heappush(heapq, (distance, v1, v2))

                                
        # construct the minimum spanning tree of the complete graph
        while heapq:
                w, v1, v2 = heappop(heapq)
                # if no path exists yet between v1 and v2, add this one
                if v1 not in mst or v2 not in mst or not has_path(mst, v1, v2):
                        mst.add_edge(v1, v2,weight=w)

        # check if the graph is tree and correct
        sTree = set(mst.nodes())
        sSteiner = set(voi)
        if sTree ^ sSteiner:
                raise RuntimeError, 'Failed to construct MST spanning tree'
        
        # reconstruct subgraph of origGraph using the paths
        if generator is None:
                subgraph  = Graph()
        else:
                subgraph = generator()
        for edge in mst.edges_iter(data=True):
                keys = [edge[0],edge[1]]
                keys.sort()
                key = "%s:%s" % tuple(keys)
                vList = paths[key]
                for i in range(len(vList) - 1):
                        v1 = vList[i]
                        v2 = vList[i+1]
                        w = G[v1][v2]
                        subgraph.add_edge(v1, v2, w)
        # get rid of possible loops - result will be a true MST
        subgraph = make_prim_mst(subgraph, generator)

        # remove intermediate nodes in paths that are not in list of voi
        return _trimTree(subgraph, voi)
class SocialNetwork(object):
    """Object oriented interface for conducting social network analysis."""
    valid_indexes = []
    for index_name in conf.INDEXES.keys():
            valid_indexes.append(index_name)
            
    cache = None
    
    def __init__(self):
        logging.info("libSNA object created.")
        self.graph = Graph()
        self.measures = {}
        self.nodesmeasures = {}
        self.edgesmeasures = {}
        for index in self.valid_indexes: self.measures[index] = None
        
    def getNodes(self):
        nodes = self.graph.nodes()
        nodes.sort()
        return nodes
        
    def getEdges(self):
        edges = self.graph.edges()
        return edges
        
    def loadGraph(self, nodes, edges):
        logging.info("Loading network from input variables")
        for node in nodes:
            self.graph.add_node(node)
        for edge in edges:
            self.graph.add_edge(edge[0], edge[1])
        self.graph.name = "Social Network"
        logging.info("Finished loading network.")

    def runMeasure(self, measure_name, backend):
        if measure_name in self.valid_indexes:
            eval('self.calculate_' + measure_name.replace(' ', '_') + '(backend)')
        else:
            logging.error("Unable to calculate the measure (%s)"%(measure_name))
            
    def returnResults(self, measure_name, value = 'value'):
        if measure_name in self.valid_indexes:
            if value == 'value': return self.measures[measure_name]
            elif value == 'nodes': return self.nodesmeasures[measure_name]
            elif value == 'edges': return self.edgesmeasures[measure_name]
        else:
            return None
            
    def displayResults(self, measure_name, value = 'value'):
        if measure_name in self.valid_indexes:
            if value == 'value': logging.info((conf.INDEX_TYPES[measure_name] + '.') % self.measures[measure_name])
            elif value == 'nodes': logging.info(str(self.nodesmeasures[measure_name] or '<null>'))
            elif value == 'edges': logging.info(str(self.edgesmeasures[measure_name] or '<null>'))
        else:
            logging.error("Unable to calculate the measure (%s)"%(measure_name))
            
    def calculateMeasures(self, backend=False):
        for measure_name in self.valid_indexes:
            self.runMeasure(measure_name, backend=False)
    
    def calculate_density(self, backend=False):        
        logging.info("Calculating density.")
        
        nodes = self.graph.nodes()
        edges = self.graph.edges()
        tot_edges = float(len(nodes) * (len(nodes)-1))
        tot_edges = tot_edges / 2
        num_edges = float(len(edges))
        
        w = {}
        for n1 in nodes:
            for n2 in nodes:
                w[n1,n2] = 0.0
        
        for n1,n2 in edges:
            w[n1,n2] = 1.0
            
        
        self.measures['density'] = num_edges / tot_edges * 100
        self.nodesmeasures['density'] = None
        self.edgesmeasures['density'] = w 
        
    def calculate_geodesic(self, backend=False):        
        logging.info("Calculating geodesic.")
        
        path = self.floyd_warshall(backend)
        nodes = self.graph.nodes()
        dividend = 0
        geodesic = float(0)
        geodesic_edges = {}
        
        for i in nodes:
            for j in nodes:
                try:
                    geodesic_edges[i,j] = path[i,j]
                    geodesic += path[i,j]
                    dividend += 1
                except KeyError:
                    pass
        
        geodesic /= dividend
        
        self.measures['geodesic'] = geodesic
        self.nodesmeasures['geodesic'] = None
        self.edgesmeasures['geodesic'] = geodesic_edges
        
    def calculate_fragmentation(self, backend=False):        
        logging.info("Calculating fragmentation.")
        
        nodes = self.graph.nodes()
        w = self.floyd_warshall(backend)
        fragmentation = float(0)
        
        for i in nodes:
            for j in nodes:
                try:
                    w[i,j]
                except KeyError:
                    fragmentation += 1
                    pass
        
        fragmentation /= len(nodes)*(len(nodes)-1)
        self.measures['fragmentation'] = fragmentation * 100
        self.nodesmeasures['fragmentation'] = None
        self.edgesmeasures['fragmentation'] = w
        
    def calculate_diameter(self, backend=False):        
        logging.info("Calculating diameter.")
        
        path = self.floyd_warshall(backend)
        nodes = self.graph.nodes()
        diameter = float(0)
        
        for i in nodes:
            for j in nodes:
                try:
                    diameter = max(diameter, path[i,j])
                except KeyError:
                    pass
    
        self.measures['diameter'] = diameter
        self.nodesmeasures['diameter'] = None
        self.edgesmeasures['diameter'] = path
        
    def calculate_degree(self, backend=False):
        logging.info("Calculating degree.")
        
        degrees = degree_centrality(self.graph)
        degree = float(sum(degrees.values())/len(degrees.values()))
        
        self.measures['degree'] = degree * 100
        self.nodesmeasures['degree'] = degrees
        self.edgesmeasures['degree'] = None
        
    def calculate_centralization(self, backend=False):
        logging.info("Calculating centralization.")
        
        degrees = degree_centrality(self.graph)
        centralization = float(0)
        maxdegree = max(degrees.values())
        for degree in degrees.values():
            centralization += maxdegree-degree
        centralization /= len(degrees.values())-1
        
        self.measures['centralization'] = centralization * 100
        self.nodesmeasures['centralization'] = degrees
        self.edgesmeasures['centralization'] = None
        
    def calculate_closeness(self, backend=False):        
        logging.info("Calculating closeness.")

        closenesses = closeness_centrality(self.graph)
        closeness = float(sum(closenesses.values())/len(closenesses.values()))
        
        self.measures['closeness'] = closeness * 100
        self.nodesmeasures['closeness'] = closenesses
        self.edgesmeasures['closeness'] = None
        
    def calculate_eigenvector(self, backend=False):        
        logging.info("Calculating eigenvector.")
        
        eigenvectors = eigenvector_centrality(self.graph)
        eigenvector = float(sum(eigenvectors.values())/len(eigenvectors.values()))
        
        self.measures['eigenvector'] = eigenvector * 100
        self.nodesmeasures['eigenvector'] = eigenvectors
        self.edgesmeasures['eigenvector'] = None
        
    def calculate_betweenness(self, backend=False):
        logging.info("Calculating betweenness.")
        
        betweennesses = betweenness_centrality(self.graph)
        betweenness = float(sum(betweennesses.values())/len(betweennesses.values()))
        
        self.measures['betweenness'] = betweenness * 100
        self.nodesmeasures['betweenness'] = betweennesses
        self.edgesmeasures['betweenness'] = None

    def calculate_cliques(self, backend=False):
        logging.info("Calculating cliques.")
        cliques = list(find_cliques(self.graph))
        
        w = {}
        nodes = self.graph.nodes()
        for node in nodes:
            w[node] = 0.0
            for clique in cliques:
                if node in clique:
                    w[node] += 1
        
        self.measures['cliques'] = len(cliques)
        self.nodesmeasures['cliques'] = w
        self.edgesmeasures['cliques'] = None
                
    def calculate_comembership(self, backend=False):
        logging.info("Calculating comembership.")
        
        nodes = self.graph.nodes()
        n = len(nodes)
        if not backend and n > 500:
            raise network_big.NetworkTooBigException(n)
        
        cliques = list(find_cliques(self.graph))
        
        w = {}
        for clique in cliques:
            for node1 in clique:
                for node2 in clique:
                    try:
                        w[node1,node2] += 1
                    except KeyError:
                        w[node1,node2] = 1
                        
        nodes = w.keys()
        comembership = float(0)
        for node1, node2 in nodes:
            if node1 != node2: comembership += w[node1,node2]
            
        num_nodes = len(self.graph.nodes())
        comembership /= num_nodes*(num_nodes-1)
        
        self.measures['comembership'] = comembership
        self.nodesmeasures['comembership'] = None
        self.edgesmeasures['comembership'] = w
        
    def calculate_components(self, backend=False):
        logging.info("Calculating components.")
        components = nx.connected_component_subgraphs(self.graph)
        
        w = {}
        nodes = self.graph.nodes()
        for node in nodes:
            w[node] = 0.0
        
        for component in components:
            if len(component) > 1:
                for node in component:
                    w[node] += 1
        
        self.measures['components'] = len(components)
        self.nodesmeasures['components'] = w
        self.edgesmeasures['components'] = None
    
    def floyd_warshall(self, backend):
        nodes = self.graph.nodes()
        
        if not backend and len(nodes) > 400:
            raise network_big.NetworkTooBigException(len(nodes))
        
        logging.info("Computing Floyd-Warshall.")
        
        infvalue = 127 #sys.maxint
        F = nx.floyd_warshall_numpy(self.graph, dtype=np.int8, infvalue=infvalue)
       
        w = {}
        for i in range(0, len(nodes)):
            for j in range(0, len(nodes)):
                if not F[i,j] == infvalue:
                    w[nodes[i],nodes[j]] = F[i,j]
        
        return w