def test_graph(self): g = nx.cycle_graph(10) G = nx.Graph() G.add_nodes_from(g) G.add_weighted_edges_from((u, v, u) for u, v in g.edges()) # Dict of dicts dod = to_dict_of_dicts(G) GG = from_dict_of_dicts(dod, create_using=nx.Graph) assert nodes_equal(sorted(G.nodes()), sorted(GG.nodes())) assert edges_equal(sorted(G.edges()), sorted(GG.edges())) GW = to_networkx_graph(dod, create_using=nx.Graph) assert nodes_equal(sorted(G.nodes()), sorted(GW.nodes())) assert edges_equal(sorted(G.edges()), sorted(GW.edges())) GI = nx.Graph(dod) assert nodes_equal(sorted(G.nodes()), sorted(GI.nodes())) assert edges_equal(sorted(G.edges()), sorted(GI.edges())) # Dict of lists dol = to_dict_of_lists(G) GG = from_dict_of_lists(dol, create_using=nx.Graph) # dict of lists throws away edge data so set it to none enone = [(u, v, {}) for (u, v, d) in G.edges(data=True)] assert nodes_equal(sorted(G.nodes()), sorted(GG.nodes())) assert edges_equal(enone, sorted(GG.edges(data=True))) GW = to_networkx_graph(dol, create_using=nx.Graph) assert nodes_equal(sorted(G.nodes()), sorted(GW.nodes())) assert edges_equal(enone, sorted(GW.edges(data=True))) GI = nx.Graph(dol) assert nodes_equal(sorted(G.nodes()), sorted(GI.nodes())) assert edges_equal(enone, sorted(GI.edges(data=True)))
def __init__(self, incoming_graph_data=None, default_label=None, **attr): self.graph_attr_dict_factory = self.graph_attr_dict_factory self.node_dict_factory = self.node_dict_factory self.adjlist_outer_dict_factory = self.adjlist_outer_dict_factory self.cache = self.graph_cache_factory(self) # init node and adj (must be after cache) self.graph = self.graph_attr_dict_factory() self._node = self.node_dict_factory(self) self._adj = self.adjlist_outer_dict_factory(self) self._succ = self._adj self._pred = self.adjlist_outer_dict_factory(self, pred=True) self._key = None self._op = None self._graph_type = self._graph_type self._schema = GraphSchema() # cache for add_node and add_edge self._add_node_cache = [] self._add_edge_cache = [] self._remove_node_cache = [] self._remove_edge_cache = [] create_empty_in_engine = attr.pop( "create_empty_in_engine", True ) # a hidden parameter self._distributed = attr.pop("dist", False) if incoming_graph_data is not None and self._is_gs_graph(incoming_graph_data): # convert from gs graph always use distributed mode self._distributed = True if self._session is None: self._session = get_session_by_id(incoming_graph_data.session_id) self._default_label = default_label self._default_label_id = -1 if self._session is None: self._session = get_default_session() if not self._is_gs_graph(incoming_graph_data) and create_empty_in_engine: graph_def = init_empty_graph_in_engine( self, self.is_directed(), self._distributed ) self._key = graph_def.key # attempt to load graph with data if incoming_graph_data is not None: to_networkx_graph(incoming_graph_data, create_using=self) self.cache.warmup() # load graph attributes (must be after to_networkx_graph) self.graph.update(attr) self._saved_signature = self.signature self._is_client_view = False
def test_attribute_dict_integrity(self): # we must not replace dict-like graph data structures with dicts G = nx.Graph() G.add_nodes_from("abc") H = to_networkx_graph(G, create_using=nx.Graph) assert list(H.nodes) == list(G.nodes) H = nx.Graph(G) assert list(H.nodes) == list(G.nodes)