Exemplo n.º 1
0
 def delete_node(self, n):
     if n in self:
         MultiGraph.delete_node(self, n)
         self.ubigraph.remove_vertex(self.nodeid[n])
         id = self.nodeid[n]
         del self.nodeid[n]
         del self.idnode[id]
Exemplo n.º 2
0
 def delete_node(self,n):
     if n in self:
         MultiGraph.delete_node(self,n)
         self.ubigraph.remove_vertex(self.nodeid[n])
         id=self.nodeid[n]
         del self.nodeid[n]
         del self.idnode[id]
Exemplo n.º 3
0
    def to_undirected(self, reciprocal=False):
        """Return an undirected representation of the digraph.

        Parameters
        ----------
        reciprocal : bool (optional)
          If True only keep edges that appear in both directions
          in the original digraph.

        Returns
        -------
        G : MultiGraph
            An undirected graph with the same name and nodes and
            with edge (u,v,data) if either (u,v,data) or (v,u,data)
            is in the digraph.  If both edges exist in digraph and
            their edge data is different, only one edge is created
            with an arbitrary choice of which edge data to use.
            You must check and correct for this manually if desired.

        Notes
        -----
        This returns a "deepcopy" of the edge, node, and
        graph attributes which attempts to completely copy
        all of the data and references.

        This is in contrast to the similar D=DiGraph(G) which returns a
        shallow copy of the data.

        See the Python copy module for more information on shallow
        and deep copies, http://docs.python.org/library/copy.html.

        Warning: If you have subclassed MultiGraph to use dict-like objects 
        in the data structure, those changes do not transfer to the MultiDiGraph
        created by this method.

        """
        H = MultiGraph()
        H.name = self.name
        H.add_nodes_from(self)
        if reciprocal is True:
            H.add_edges_from(
                (u, v, key, deepcopy(data))
                for u, nbrs in self.adjacency()
                for v, keydict in nbrs.items()
                for key, data in keydict.items()
                if self.has_edge(v, u, key)
            )
        else:
            H.add_edges_from(
                (u, v, key, deepcopy(data))
                for u, nbrs in self.adjacency()
                for v, keydict in nbrs.items()
                for key, data in keydict.items()
            )
        H.graph = deepcopy(self.graph)
        H.node = deepcopy(self.node)
        return H
Exemplo n.º 4
0
    def to_undirected(self):
        """Return an undirected representation of the digraph.

        Returns
        -------
        G : MultiGraph
            An undirected graph with the same name and nodes and
            with edge (u,v,data) if either (u,v,data) or (v,u,data)
            is in the digraph.  If both edges exist in digraph and
            their edge data is different, only one edge is created
            with an arbitrary choice of which edge data to use.
            You must check and correct for this manually if desired.

        Notes
        -----
        This returns a "deepcopy" of the edge, node, and
        graph attributes which attempts to completely copy
        all of the data and references.

        This is in contrast to the similar D=DiGraph(G) which returns a
        shallow copy of the data.

        See the Python copy module for more information on shallow
        and deep copies, http://docs.python.org/library/copy.html.
        """
        H = MultiGraph()
        H.name = self.name
        H.add_nodes_from(self)
        H.add_edges_from((u, v, key, deepcopy(data))
                         for u, nbrs in self.adjacency_iter()
                         for v, keydict in nbrs.items()
                         for key, data in list(keydict.items()))
        H.graph = deepcopy(self.graph)
        H.node = deepcopy(self.node)
        return H
Exemplo n.º 5
0
    def to_undirected(self):
        """Return an undirected representation of the digraph.
 
        Returns
        -------
        G : MultiGraph
            An undirected graph with the same name and nodes and 
            with edge (u,v,data) if either (u,v,data) or (v,u,data)
            is in the digraph.  If both edges exist in digraph and
            their edge data is different, only one edge is created
            with an arbitrary choice of which edge data to use.
            You must check and correct for this manually if desired.

        Notes
        -----
        This is similar to MultiGraph(self) which returns a shallow copy.  
        self.to_undirected() returns a deepcopy of edge, node and
        graph attributes.
        """
        H=MultiGraph()
        H.name=self.name
        H.add_nodes_from(self)
        H.add_edges_from( ((u,v,key,deepcopy(data))
                           for u,nbrs in self.adjacency_iter()
                           for v,keydict in nbrs.iteritems()
                           for key,data in keydict.items()))
        H.graph=deepcopy(self.graph)
        H.node=deepcopy(self.node)
        return H
Exemplo n.º 6
0
 def add_node(self, n,**kwds):
     if n not in self:
         MultiGraph.add_node(self,n)
         self.nodeid[n]=self.nextid
         self.idnode[self.nextid]=n
         self.nextid+=1
         self.ubigraph.new_vertex_w_id(self.nodeid[n])
         # add ubigraph attributes
         for (k,v) in kwds.items():
             ret=self.ubigraph.set_vertex_attribute(self.nodeid[n],k,v) 
         # support toggling node labels
         if self.use_node_labels:
             self.ubigraph.set_vertex_attribute(self.nodeid[n],'label',str(n))
Exemplo n.º 7
0
    def to_undirected(self):
        """Return an undirected representation of the digraph.

        Returns
        -------
        G : MultiGraph
            An undirected graph with the same name and nodes and
            with edge (u,v,data) if either (u,v,data) or (v,u,data)
            is in the digraph.  If both edges exist in digraph and
            their edge data is different, only one edge is created
            with an arbitrary choice of which edge data to use.
            You must check and correct for this manually if desired.

        Notes
        -----
        This returns a "deepcopy" of the edge, node, and
        graph attributes which attempts to completely copy
        all of the data and references.

        This is in contrast to the similar D=DiGraph(G) which returns a
        shallow copy of the data.

        See the Python copy module for more information on shallow
        and deep copies, http://docs.python.org/library/copy.html.
        """
        H=MultiGraph()
        H.name=self.name
        H.add_nodes_from(self)
        H.add_edges_from( (u,v,key,deepcopy(data))
                           for u,nbrs in self.adjacency_iter()
                           for v,keydict in nbrs.items()
                           for key,data in list(keydict.items()))
        H.graph=deepcopy(self.graph)
        H.node=deepcopy(self.node)
        return H
Exemplo n.º 8
0
 def add_node(self, n, **kwds):
     if n not in self:
         MultiGraph.add_node(self, n)
         self.nodeid[n] = self.nextid
         self.idnode[self.nextid] = n
         self.nextid += 1
         self.ubigraph.new_vertex_w_id(self.nodeid[n])
         # add ubigraph attributes
         for (k, v) in kwds.items():
             ret = self.ubigraph.set_vertex_attribute(self.nodeid[n], k, v)
         # support toggling node labels
         if self.use_node_labels:
             self.ubigraph.set_vertex_attribute(self.nodeid[n], 'label',
                                                str(n))
Exemplo n.º 9
0
 def to_undirected(self):
     """Return an undirected representation of the digraph.
 
     A new graph is returned with the same name and nodes and
     with edge (u,v,data) if either (u,v,data) or (v,u,data) 
     is in the digraph.  If both edges exist in digraph they 
     appear as a double edge in the new multigraph.
     
     """
     H = MultiGraph()
     H.name = self.name
     H.add_nodes_from(self)
     H.add_edges_from([(v, u, d)
                       for (u, v, d) in self.edges_iter(data=True)])
     return H
Exemplo n.º 10
0
 def to_undirected(self):
     """Return an undirected representation of the digraph.
 
     A new graph is returned with the same name and nodes and
     with edge (u,v,data) if either (u,v,data) or (v,u,data) 
     is in the digraph.  If both edges exist in digraph they 
     appear as a double edge in the new multigraph.
     
     """
     H=MultiGraph()
     H.name=self.name
     H.add_nodes_from(self)
     H.add_edges_from([(v,u,d) for (u,v,d) in self.edges_iter(data=True)])
     return H
Exemplo n.º 11
0
    def to_undirected(self, reciprocal=False, as_view=False):
        """Return an undirected representation of the digraph.

        Parameters
        ----------
        reciprocal : bool (optional)
          If True only keep edges that appear in both directions
          in the original digraph.
        as_view : bool (optional, default=False)
          If True return an undirected view of the original directed graph.

        Returns
        -------
        G : MultiGraph
            An undirected graph with the same name and nodes and
            with edge (u, v, data) if either (u, v, data) or (v, u, data)
            is in the digraph.  If both edges exist in digraph and
            their edge data is different, only one edge is created
            with an arbitrary choice of which edge data to use.
            You must check and correct for this manually if desired.

        See Also
        --------
        MultiGraph, copy, add_edge, add_edges_from

        Notes
        -----
        This returns a "deepcopy" of the edge, node, and
        graph attributes which attempts to completely copy
        all of the data and references.

        This is in contrast to the similar D=MultiiGraph(G) which
        returns a shallow copy of the data.

        See the Python copy module for more information on shallow
        and deep copies, https://docs.python.org/2/library/copy.html.

        Warning: If you have subclassed MultiDiGraph to use dict-like
        objects in the data structure, those changes do not transfer
        to the MultiGraph created by this method.

        Examples
        --------
        >>> G = nx.path_graph(2)   # or MultiGraph, etc
        >>> H = G.to_directed()
        >>> list(H.edges)
        [(0, 1), (1, 0)]
        >>> G2 = H.to_undirected()
        >>> list(G2.edges)
        [(0, 1)]
        """
        if as_view is True:
            return nx.graphviews.MultiGraphView(self)
        # deepcopy when not a view
        G = MultiGraph()
        G.graph.update(deepcopy(self.graph))
        G.add_nodes_from((n, deepcopy(d)) for n, d in self._node.items())
        if reciprocal is True:
            G.add_edges_from((u, v, key, deepcopy(data))
                             for u, nbrs in self.adj.items()
                             for v, keydict in nbrs.items()
                             for key, data in keydict.items()
                             if v in self.pred[u] and key in self.pred[u][v])
        else:
            G.add_edges_from((u, v, key, deepcopy(data))
                             for u, nbrs in self.adj.items()
                             for v, keydict in nbrs.items()
                             for key, data in keydict.items())
        return G
Exemplo n.º 12
0
    def to_undirected(self, reciprocal=False):
        """Return an undirected representation of the digraph.

        Parameters
        ----------
        reciprocal : bool (optional)
          If True only keep edges that appear in both directions
          in the original digraph.

        Returns
        -------
        G : MultiGraph
            An undirected graph with the same name and nodes and
            with edge (u,v,data) if either (u,v,data) or (v,u,data)
            is in the digraph.  If both edges exist in digraph and
            their edge data is different, only one edge is created
            with an arbitrary choice of which edge data to use.
            You must check and correct for this manually if desired.

        Notes
        -----
        This returns a "deepcopy" of the edge, node, and
        graph attributes which attempts to completely copy
        all of the data and references.

        This is in contrast to the similar D=DiGraph(G) which returns a
        shallow copy of the data.

        See the Python copy module for more information on shallow
        and deep copies, http://docs.python.org/library/copy.html.

        Warning: If you have subclassed MultiGraph to use dict-like objects
        in the data structure, those changes do not transfer to the MultiDiGraph
        created by this method.

        """
        H = MultiGraph()
        H.name = self.name
        H.add_nodes_from(self)
        if reciprocal is True:
            H.add_edges_from((u, v, key, deepcopy(data))
                             for u, nbrs in self.adjacency()
                             for v, keydict in nbrs.items()
                             for key, data in keydict.items()
                             if self.has_edge(v, u, key))
        else:
            H.add_edges_from((u, v, key, deepcopy(data))
                             for u, nbrs in self.adjacency()
                             for v, keydict in nbrs.items()
                             for key, data in keydict.items())
        H.graph = deepcopy(self.graph)
        H.node = deepcopy(self.node)
        return H
Exemplo n.º 13
0
    def to_subgraph(self,
                    begin,
                    end,
                    multigraph=False,
                    edge_data=False,
                    edge_interval_data=False,
                    node_data=False):
        """Return a networkx Graph or MultiGraph which includes all the nodes and
        edges which have overlapping intervals with the given interval.

        Parameters
        ----------
        begin: integer
            Inclusive beginning time of the edge appearing in the interval graph.
            Must be bigger than begin.
        end: integer
            Non-inclusive ending time of the edge appearing in the interval graph.
        multigraph: bool, optional (default= False)
            If True, a networkx MultiGraph will be returned. If False, networkx Graph.
        edge_data: bool, optional (default= False)
            If True, edges will keep their attributes.
        edge_interval_data: bool, optional (default= False)
            If True, each edge's attribute will also include its begin and end interval data.
            If `edge_data= True` and there already exist edge attributes with names begin and end,
            they will be overwritten.
        node_data : bool, optional (default= False)
            if True, each node's attributes will be included.

        See Also
        --------
        to_snapshots : divide the interval graph to snapshots

        Notes
        -----
        If multigraph= False, and edge_data=True or edge_interval_data=True,
        in case there are multiple edges, only one will show with one of the edge's attributes.

        Note: nodes with no edges will not appear in any subgraph.

        Examples
        --------
        >>> G = dnx.IntervalGraph()
        >>> G.add_edges_from([(1, 2, 3, 10), (2, 4, 1, 11), (6, 4, 12, 19), (2, 4, 8, 15)])
        >>> H = G.to_subgraph(4, 12)
        >>> type(H)
        <class 'networkx.classes.graph.Graph'>
        >>> list(H.edges(data=True))
        [(1, 2, {}), (2, 4, {})]

        >>> H = G.to_subgraph(4, 12, edge_interval_data=True)
        >>> type(H)
        <class 'networkx.classes.graph.Graph'>
        >>> list(H.edges(data=True))
        [(1, 2, {'end': 10, 'begin': 3}), (2, 4, {'end': 15, 'begin': 8})]

        >>> M = G.to_subgraph(4, 12, multigraph=True, edge_interval_data=True)
        >>> type(M)
        <class 'networkx.classes.multigraph.MultiGraph'>
        >>> list(M.edges(data=True))
        [(1, 2, {'end': 10, 'begin': 3}), (2, 4, {'end': 11, 'begin': 1}), (2, 4, {'end': 15, 'begin': 8})]
        """

        if end <= begin:
            raise NetworkXError(
                "IntervalGraph: subgraph duration must be strictly bigger than zero: "
                "begin: {}, end: {}.".format(begin, end))

        iedges = self.tree[begin:end]

        if multigraph:
            G = MultiGraph()
        else:
            G = Graph()

        if edge_data and edge_interval_data:
            G.add_edges_from((iedge.data[0], iedge.data[1],
                              dict(self._adj[iedge.data[0]][iedge],
                                   begin=iedge.begin,
                                   end=iedge.end)) for iedge in iedges)
        elif edge_data:
            G.add_edges_from((iedge.data[0], iedge.data[1],
                              self._adj[iedge.data[0]][iedge].copy())
                             for iedge in iedges)
        elif edge_interval_data:
            G.add_edges_from((iedge.data[0], iedge.data[1], {
                'begin': iedge.begin,
                'end': iedge.end
            }) for iedge in iedges)
        else:
            G.add_edges_from(
                (iedge.data[0], iedge.data[1]) for iedge in iedges)

        # include node attributes
        if node_data:
            G.add_nodes_from((n, self._node[n].copy()) for n in G.nodes)

        return G
Exemplo n.º 14
0
    def to_undirected(self, reciprocal=False, as_view=False):
        """Return an undirected representation of the digraph.

        Parameters
        ----------
        reciprocal : bool (optional)
          If True only keep edges that appear in both directions
          in the original digraph.
        as_view : bool (optional, default=False)
          If True return an undirected view of the original directed graph.

        Returns
        -------
        G : MultiGraph
            An undirected graph with the same name and nodes and
            with edge (u, v, data) if either (u, v, data) or (v, u, data)
            is in the digraph.  If both edges exist in digraph and
            their edge data is different, only one edge is created
            with an arbitrary choice of which edge data to use.
            You must check and correct for this manually if desired.

        See Also
        --------
        MultiGraph, copy, add_edge, add_edges_from

        Notes
        -----
        This returns a "deepcopy" of the edge, node, and
        graph attributes which attempts to completely copy
        all of the data and references.

        This is in contrast to the similar D=MultiiGraph(G) which
        returns a shallow copy of the data.

        See the Python copy module for more information on shallow
        and deep copies, https://docs.python.org/2/library/copy.html.

        Warning: If you have subclassed MultiDiGraph to use dict-like
        objects in the data structure, those changes do not transfer
        to the MultiGraph created by this method.

        Examples
        --------
        >>> G = nx.path_graph(2)   # or MultiGraph, etc
        >>> H = G.to_directed()
        >>> list(H.edges)
        [(0, 1), (1, 0)]
        >>> G2 = H.to_undirected()
        >>> list(G2.edges)
        [(0, 1)]
        """
        if as_view is True:
            return nx.graphviews.MultiGraphView(self)
        # deepcopy when not a view
        G = MultiGraph()
        G.graph.update(deepcopy(self.graph))
        G.add_nodes_from((n, deepcopy(d)) for n, d in self._node.items())
        if reciprocal is True:
            G.add_edges_from((u, v, key, deepcopy(data))
                             for u, nbrs in self._adj.items()
                             for v, keydict in nbrs.items()
                             for key, data in keydict.items()
                             if v in self._pred[u] and key in self._pred[u][v])
        else:
            G.add_edges_from((u, v, key, deepcopy(data))
                             for u, nbrs in self._adj.items()
                             for v, keydict in nbrs.items()
                             for key, data in keydict.items())
        return G
Exemplo n.º 15
0
 def clear(self):
     if len(self) > 0:
         MultiGraph.clear(self)
         self.ubigraph.clear()
         self.nodeid = {}
         self.nextid = 0
Exemplo n.º 16
0
 def clear(self):
     if len(self)>0:
         MultiGraph.clear(self)
         self.ubigraph.clear()
         self.nodeid={}
         self.nextid=0
Exemplo n.º 17
0
    def to_subgraph(self,
                    begin,
                    end,
                    inclusive=(True, True),
                    multigraph=False,
                    edge_data=False,
                    edge_timestamp_data=False,
                    node_data=False):
        """Return a networkx Graph or MultiGraph which includes all the nodes and
        edges which have timestamps within the given interval.

        Parameters
        ----------
        begin: int or float
        end: int or float
            Must be bigger than or equal to begin.
        inclusive: 2-tuple boolean that determines inclusivity of begin and end
        multigraph: bool, optional (default= False)
            If True, a networkx MultiGraph will be returned. If False, networkx Graph.
        edge_data: bool, optional (default= False)
            If True, edges will keep their attributes.
        edge_timestamp_data: bool, optional (default= False)
            If True, each edge's attribute will also include its timestamp data.
            If `edge_data= True` and there already exist edge attributes named timestamp
            it will be overwritten.
        node_data : bool, optional (default= False)
            if True, each node's attributes will be included.

        See Also
        --------
        to_snapshots : divide the impulse graph to snapshots

        Notes
        -----
        If multigraph= False, and edge_data=True or edge_interval_data=True,
        in case there are multiple edges, only one will show with one of the edge's attributes.

        Note: nodes with no edges will not appear in any subgraph.

        Examples
        --------
        >>> G = dnx.ImpulseGraph()
        >>> G.add_edges_from([(1, 2, 10), (2, 4, 11), (6, 4, 19), (2, 4, 15)])
        >>> H = G.to_subgraph(4, 12)
        >>> type(H)
        <class 'networkx.classes.graph.Graph'>
        >>> list(H.edges(data=True))
        [(1, 2, {}), (2, 4, {})]

        >>> H = G.to_subgraph(10, 12, edge_timestamp_data=True)
        >>> type(H)
        <class 'networkx.classes.graph.Graph'>
        >>> list(H.edges(data=True))
        [(1, 2, {'timestamp': 10}), (2, 4, {'timestamp': 11})]

        >>> M = G.to_subgraph(4, 12, multigraph=True, edge_timestamp_data=True)
        >>> type(M)
        <class 'networkx.classes.multigraph.MultiGraph'>
        >>> list(M.edges(data=True))
        [(1, 2, {'timestamp': 10}), (2, 4, {'timestamp': 11})]
        """
        iedges = self.__search_tree(begin, end, inclusive=inclusive)

        if multigraph:
            G = MultiGraph()
        else:
            G = Graph()

        if edge_data and edge_timestamp_data:
            G.add_edges_from(
                (iedge[0], iedge[1],
                 dict(self._adj[iedge[0]][iedge], timestamp=iedge[3]))
                for iedge in iedges)
        elif edge_data:
            G.add_edges_from(
                (iedge[0], iedge[1], self._adj[iedge.data[0]][iedge].copy())
                for iedge in iedges)
        elif edge_timestamp_data:
            G.add_edges_from((iedge[0], iedge[1], {
                'timestamp': iedge[3]
            }) for iedge in iedges)
        else:

            G.add_edges_from((iedge[0], iedge[1]) for iedge in iedges)

        if node_data:
            G.add_nodes_from((n, self._node[n].copy()) for n in G.nodes)

        return G