コード例 #1
0
 def new_edge(self, source, target, attributes=None):
     '''
     Adding a connection to the graph, with optional properties.
     
     Parameters
     ----------
     source : :class:`int/node`
         Source node.
     target : :class:`int/node`
         Target node.
     attributes : :class:`dict`, optional (default: ``{}``)
         Dictionary containing optional edge properties. If the graph is
         weighted, defaults to ``{"weight": 1.}``, the unit weight for the
         connection (synaptic strength in NEST).
         
     Returns
     -------
     The new connection.
     '''
     enum = self.ecount()
     if attributes is None:
         attributes = {}
     super(_IGraph, self).add_edge(source, target)
     _set_edge_attr(self, [(source, target)], attributes)
     for key, val in attributes.items():
         self.es[enum][key] = val[0]
     if not self._directed:
         super(_IGraph, self).add_edge(target, source)
         for key, val in attributes.items():
             self.es[enum][key] = val[0]
     return (source, target)
コード例 #2
0
 def new_edges(self, edge_list, attributes=None):
     '''
     Add a list of edges to the graph.
     
     Parameters
     ----------
     edge_list : list of 2-tuples or np.array of shape (edge_nb, 2)
         List of the edges that should be added as tuples (source, target)
     attributes : dict, optional (default: ``None``)
         Dictionary of the form ``{ "name": [], "values": [],
         "type": [] }``, containing the attributes of the new edges.
     
     warning ::
         For now attributes works only when adding edges for the first time
         (i.e. adding edges to an empty graph).
         
     @todo: add example, check the edges for self-loops and multiple edges
     
     Returns
     -------
     Returns new edges only.
     '''
     if attributes is None:
         attributes = {}
     initial_ecount = self.ecount()
     if not self._directed:
         edge_list = np.concatenate((edge_list, edge_list[:, ::-1]))
         for key, val in attributes.items():
             attributes[key] = np.concatenate((val, val))
     first_eid = self.ecount()
     super(_IGraph, self).add_edges(edge_list)
     _set_edge_attr(self, edge_list, attributes)
     num_edges = self.ecount()
     # attributes
     if self._weighted and "weight" not in attributes:
         attributes["weight"] = np.repeat(1., num_edges)
     if attributes:
         elist0 = None  #@todo: make elist supported and remove this
         # take care of classic attributes
         if "weight" in attributes:
             self.set_weights(weight=attributes["weight"], elist=elist0)
         if "delay" in attributes:
             self.set_delays(delay=attributes["delay"], elist=elist0)
         if "distance" in attributes:
             raise NotImplementedError("distance not implemented yet")
             #~ self.set_distances(elist=edge_list,
             #~ dlist=attributes["distance"])
         # take care of potential additional attributes
         if "names" in attributes:
             num_attr = len(attributes["names"])
             for i in range(num_attr):
                 v = attributes["values"]
                 if not nonstring_container(v):
                     v = np.repeat(v, self.ecount())
                 self._eattr.new_ea(attributes["names"][i],
                                    attributes["types"][i],
                                    values=v)
     return edge_list
コード例 #3
0
ファイル: nx_graph.py プロジェクト: cocconat/NNGT
 def new_edges(self, edge_list, attributes=None):
     '''
     Add a list of edges to the graph.
     
     Parameters
     ----------
     edge_list : list of 2-tuples or np.array of shape (edge_nb, 2)
         List of the edges that should be added as tuples (source, target)
     attributes : :class:`dict`, optional (default: ``{}``)
         Dictionary containing optional edge properties. If the graph is
         weighted, defaults to ``{"weight": ones}``, where ``ones`` is an
         array the same length as the `edge_list` containing a unit weight
         for each connection (synaptic strength in NEST).
     
     warning ::
         For now attributes works only when adding edges for the first time
         (i.e. adding edges to an empty graph).
         
     @todo: add example, check the edges for self-loops and multiple edges
     '''
     if attributes is None:
         attributes = {}
     _set_edge_attr(self, edge_list, attributes)
     for attr in attributes:
         if "_corr" in attr:
             raise NotImplementedError("Correlated attributes are not "
                                       "available with networkx.")
     initial_edges = self.number_of_edges()
     if not self._directed:
         elist_tmp = np.zeros((2 * len(edge_list), 2), dtype=np.uint)
         i = 0
         for e, e_reversed in zip(edge_list, edge_list[:, ::-1]):
             elist_tmp[i:i + 2, :] = (e, e_reversed)
             i += 1
         edge_list = elist_tmp
         for key, val in attributes.items():
             attributes[key] = np.repeat(val, 2)
     edge_generator = (e for e in edge_list)
     edge_list = np.array(edge_list)
     if self._weighted and "weight" not in attributes:
         attributes["weight"] = np.repeat(1., edge_list.shape[0])
     attributes["eid"] = np.arange(initial_edges,
                                   initial_edges + len(edge_list))
     for i, (u, v) in enumerate(edge_list):
         if u not in self.succ:
             self.succ[u] = self.adjlist_dict_factory()
             self.pred[u] = self.adjlist_dict_factory()
             self.node[u] = {}
         if v not in self.succ:
             self.succ[v] = self.adjlist_dict_factory()
             self.pred[v] = self.adjlist_dict_factory()
             self.node[v] = {}
         datadict = self.adj[u].get(v, self.edge_attr_dict_factory())
         datadict.update({key: val[i] for key, val in attributes.items()})
         self.succ[u][v] = datadict
         self.pred[v][u] = datadict
     return edge_generator
コード例 #4
0
 def new_edges(self, edge_list, attributes=None):
     '''
     Add a list of edges to the graph.
     
     Parameters
     ----------
     edge_list : list of 2-tuples or np.array of shape (edge_nb, 2)
         List of the edges that should be added as tuples (source, target)
     attributes : :class:`dict`, optional (default: ``{}``)
         Dictionary containing optional edge properties. If the graph is
         weighted, defaults to ``{"weight": ones}``, where ``ones`` is an
         array the same length as the `edge_list` containing a unit weight
         for each connection (synaptic strength in NEST).
     
     warning ::
         For now attributes works only when adding edges for the first time
         (i.e. adding edges to an empty graph).
         
     @todo: add example, check the edges for self-loops and multiple edges
     '''
     if attributes is None:
         attributes = {}
     initial_edges = self.num_edges()
     edge_generator = (e for e in edge_list)
     if not isinstance(edge_list, np.ndarray):
         edge_list = np.array(edge_list)
     super(_GtGraph, self).add_edge_list(edge_list)
     _set_edge_attr(self, edge_list, attributes)
     if not self._directed:
         edge_list = np.concatenate((edge_list, edge_list[:, ::-1]))
         for key, val in attributes.items():
             attributes[key] = np.concatenate((val, val))
     if attributes:
         elist0 = None  #@todo: make elist supported and remove this
         # take care of classic attributes
         if "weight" in attributes:
             self.set_weights(weight=attributes["weight"], elist=elist0)
         if "delay" in attributes:
             self.set_delays(delay=attributes["delay"], elist=elist0)
         if "distance" in attributes:
             raise NotImplementedError("distance not implemented yet")
             #~ self.set_distances(elist=edge_list,
             #~ dlist=attributes["distance"])
         # take care of potential additional attributes
         if "names" in attributes:
             num_attr = len(attributes["names"])
             for i in range(num_attr):
                 v = attributes["values"]
                 if not nonstring_container(v):
                     v = np.repeat(v, self.ecount())
                 self._eattr.new_ea(attributes["names"][i],
                                    attributes["types"][i],
                                    values=v)
     return edge_generator
コード例 #5
0
 def new_edge(self, source, target, attributes=None):
     '''
     Adding a connection to the graph, with optional properties.
     
     Parameters
     ----------
     source : :class:`int/node`
         Source node.
     target : :class:`int/node`
         Target node.
     attributes : :class:`dict`, optional (default: ``{}``)
         Dictionary containing optional edge properties. If the graph is
         weighted, defaults to ``{"weight": 1.}``, the unit weight for the
         connection (synaptic strength in NEST).
         
     Returns
     -------
     The new connection.
     '''
     if attributes is None:
         attributes = {}
     connection = super(_GtGraph, self).add_edge(source,
                                                 target,
                                                 add_missing=True)
     _set_edge_attr(self, [(source, target)], attributes)
     for key, val in attributes:
         if key in self.edge_properties:
             self.edge_properties[key][connection] = val[0]
         else:
             raise InvalidArgument("Unknown edge property `" + key + "'.")
     if not self._directed:
         c2 = super(_GtGraph, self).add_edge(target, source)
         for key, val in attributes:
             if key in self.edge_properties:
                 self.edge_properties[key][c2] = val[0]
             else:
                 raise InvalidArgument("Unknown edge property `" + key +
                                       "'.")
     return connection
コード例 #6
0
ファイル: nx_graph.py プロジェクト: cocconat/NNGT
 def new_edge(self, source, target, attributes=None):
     '''
     Adding a connection to the graph, with optional properties.
     
     Parameters
     ----------
     source : :class:`int/node`
         Source node.
     target : :class:`int/node`
         Target node.
     attributes : :class:`dict`, optional (default: ``{}``)
         Dictionary containing optional edge properties. If the graph is
         weighted, defaults to ``{"weight": 1.}``, the unit weight for the
         connection (synaptic strength in NEST).
         
     Returns
     -------
     The new connection.
     '''
     if attributes is None:
         attributes = {}
     _set_edge_attr(self, [(source, target)], attributes)
     for attr in attributes:
         if "_corr" in attr:
             raise NotImplementedError("Correlated attributes are not "
                                       "available with networkx.")
     if self._weighted and "weight" not in attributes:
         attributes["weight"] = 1.
     self.add_edge(source, target)
     self[source][target]["eid"] = self.number_of_edges()
     for key, val in attributes.items:
         self[source][target][key] = val
     if not self._directed:
         self.add_edge(target, source)
         self[source][target]["eid"] = self.number_of_edges()
         for key, val in attributes.items:
             self[target][source][key] = val
     return (source, target)