Graph Transformation
In GraphScope, nx.Graph
or nx.DiGraph
can be created from GraphScope graph object.
We implement a copy-on-write transformation strategy for creating from GraphScope graph.
That is, when init a nx.Graph
or nx.DiGraph
object with GraphScope graph object,
we do not convert arrow property graph to dynamic property graph immediately，
but just host the arrow property graph in the nx.Graph
or nx.DiGraph
object.
And the graph can report graph info and run algorithms as expected.
When the graph comes to modification operation, the arrow property graph would convert
to dynamic property graph, that is the copy-on-write transformation strategy.
Here is an example to show the copy-on-write transformation.
Note: nx.Graph
and nx.DiGraph
not support to be created from a GraphScope multigraph
(which contains parallel edge). If input graph is multigraph, it would raise a
NetworkXError.
>>> # first create a labeled graphscope graph
>>> graph = graphscope.g()
>>> graph.add_vertices("v_0.csv", label="v0")
>>> graph.add_vertices("v_1.csv", label="v1")
>>> graph.add_edge("e.csv", label="e", src_label="v0", dst_label="v1")
>>> # then we init a nx.Graph with graph, here we set v0 as default vertex label
>>> G = nx.Graph(graph, default_label="v0") # G actually hosts a arrow property graph
>>> # we can report info of G or run algorithm without converting to dynamic property graph
>>> print(G.nodes)
[0, ("v1", 1)]
>>> nx.builtin.pagerank(G)
>>> # when comes to the modification, hosted arrow property graph convert to dynamic property graph
>>> G.add_node(1)
>>> print(G.nodes)
[0, 1, ("v1", 1)]
- graphscope.nx.Graph.__init__(self, incoming_graph_data=None, default_label=None, **attr)
Initialize a graph with graph, edges, name, or graph attributes
- Parameters
incoming_graph_data (input graph (optional, default: None)) – Data to initialize graph. If None (default) an empty graph is created. The data can be any format that is supported by the to_networkx_graph() function, currently including edge list, dict of dicts, dict of lists, NetworkX graph, NumPy matrix or 2d ndarray, Pandas DataFrame, SciPy sparse matrix, or a GraphScope graph object.
default_label (default node label (optional, default: "_")) – if incoming_graph_data is a GraphScope graph object, default label means the nodes of the label can be accessed by id directly, other label nodes need to use (label, id) to access.
attr (keyword arguments, optional (default= no attributes)) – Attributes to add to graph as key=value pairs.
Examples
>>> G = nx.Graph() # or DiGraph >>> G = nx.Graph(name='my graph') >>> e = [(1, 2), (2, 3), (3, 4)] # list of edges >>> G = nx.Graph(e)
Arbitrary graph attribute pairs (key=value) may be assigned
>>> G = nx.Graph(e, day="Friday") >>> G.graph {'day': 'Friday'}
Created from a GraphScope graph object
>>> g = graphscope.g(directed=False) # if transform to DiGraph, directed=True >>> g.add_vertices("person.csv", label="person").add_vertices("comment.csv", label="comment").add_edges(...) >>> G = nx.Graph(g, default_label="person") # or DiGraph
- graphscope.nx.Graph._init_with_arrow_property_graph(self, arrow_property_graph)
Init graph with arrow property graph
- graphscope.nx.Graph._convert_arrow_to_dynamic(self)
Try to convert the hosted graph from arrow_property to dynamic_property.
Notes
the method is implicit called by modification and graph view methods.