Graph¶
Graph object¶
- class graphscope.framework.graph.GraphDAGNode(session, incoming_data=None, oid_type='int64', directed=True, generate_eid=True, vertex_map='global')[source]¶
A class represents a graph node in a DAG.
In GraphScope, all operations that generate a new graph will return a instance of
GraphDAGNode
, which will be automatically executed bySession.run()
in eager mode.The following example demonstrates its usage:
>>> # lazy mode >>> import graphscope as gs >>> sess = gs.session(mode="lazy") >>> g = sess.g() >>> g1 = g.add_vertices("person.csv","person") >>> print(g1) # <graphscope.framework.graph.GraphDAGNode object> >>> g2 = sess.run(g1) >>> print(g2) # <graphscope.framework.graph.Graph object> >>> # eager mode >>> import graphscope as gs >>> sess = gs.session(mode="eager") >>> g = sess.g() >>> g1 = g.add_vertices("person.csv","person") >>> print(g1) # <graphscope.framework.graph.Graph object> >>> del g1
- __init__(session, incoming_data=None, oid_type='int64', directed=True, generate_eid=True, vertex_map='global')[source]¶
Construct a
GraphDAGNode
object.- Parameters
session (
Session
) – A graphscope session instance.incoming_data –
Graph can be initialized through various type of sources, which can be one of:
graphscope.Graph
vineyard.Object
,vineyard.ObjectId
orvineyard.ObjectName
oid_type – (str, optional): Type of vertex original id. Defaults to “int64”.
directed – (bool, optional): Directed graph or not. Defaults to True.
generate_eid – (bool, optional): Generate id for each edge when setted True. Defaults to True.
vertex_map (str, optional) – Indicate use global vertex map or local vertex map. Can be “global” or “local”. Defaults to global.
- add_column(results, selector)[source]¶
Add the results as a column to the graph. Modification rules are given by the selector.
- Parameters
results – A instance of concrete class derive from (
graphscope.framework.context.BaseContextDAGNode
): A context that created by doing an app query on a graph, and holds the corresponding results.selector (dict) – Select results to add as column. Format is similar to selectors in
graphscope.framework.context.Context
- Returns
A new graph with new columns, evaluated in eager mode.
- Return type
- add_edges(edges, label='_e', properties=None, src_label=None, dst_label=None, src_field=0, dst_field=1)[source]¶
Add edges to the graph, and return a new graph. Here the src_label and dst_label must be both specified or both unspecified,
src_label and dst_label both unspecified and current graph has no vertex label.
We deduce vertex label from edge table, and set vertex label name to ‘_’.
src_label and dst_label both unspecified and current graph has one vertex label.
We set src_label and dst label to this single vertex label.
src_label and dst_label both specified and existed in current graph’s vertex labels.
src_label and dst_label both specified and some are not existed in current graph’s vertex labels.
We deduce missing vertex labels from edge tables.
- Parameters
edges (Union[str, Loader]) – Edge data source.
label (str, optional) – Edge label name. Defaults to “_e”.
properties (list[str], optional) – List of column names loaded as properties. Defaults to None.
src_label (str, optional) – Source vertex label. Defaults to None.
dst_label (str, optional) – Destination vertex label. Defaults to None.
src_field (int, optional) – Column index or name used as src field. Defaults to 0.
dst_field (int, optional) – Column index or name used as dst field. Defaults to 1.
- Raises
ValueError – If the given value is invalid or conflict with current graph.
- Returns
A new graph with edge added, evaluated in eager mode.
- Return type
- add_vertices(vertices, label='_', properties=None, vid_field=0)[source]¶
Add vertices to the graph, and return a new graph.
- Parameters
vertices (Union[str, Loader]) – Vertex data source.
label (str, optional) – Vertex label name. Defaults to “_”.
properties (list[str], optional) – List of column names loaded as properties. Defaults to None.
vid_field (int or str, optional) – Column index or property name used as id field. Defaults to 0.
- Raises
ValueError – If the given value is invalid or conflict with current graph.
- Returns
A new graph with vertex added, evaluated in eager mode.
- Return type
- project(vertices: Mapping[str, Optional[List[str]]], edges: Mapping[str, Optional[List[str]]])[source]¶
Project a subgraph from the property graph, and return a new graph. A graph produced by project just like a normal property graph, and can be projected further.
- Parameters
vertices (dict) – key is the vertex label name, the value is a list of str, which represents the name of properties. Specifically, it will select all properties if value is None. Note that, the label of the vertex in all edges you want to project should be included.
edges (dict) – key is the edge label name, the value is a list of str, which represents the name of properties. Specifically, it will select all properties if value is None.
- Returns
A new graph projected from the property graph, evaluated in eager mode.
- Return type
- class graphscope.framework.graph.Graph(graph_node)[source]¶
A class for representing metadata of a graph in the GraphScope.
A
Graph
object holds the metadata of a graph, such as key, schema, and the graph is directed or not.It is worth noticing that the graph is stored by the backend such as Analytical Engine, Vineyard. In other words, the graph object holds nothing but metadata.
The following example demonstrates its usage:
>>> import graphscope as gs >>> sess = gs.session() >>> graph = sess.g() >>> graph = graph.add_vertices("person.csv", "person") >>> graph = graph.add_vertices("software.csv", "software") >>> graph = graph.add_edges("knows.csv", "knows", src_label="person", dst_label="person") >>> graph = graph.add_edges("created.csv", "created", src_label="person", dst_label="software") >>> print(graph) >>> print(graph.schema)
- detach()[source]¶
Detaching a graph makes it being left in vineyard even when the varaible for this
Graph
object leaves the lexical scope.The graph can be accessed using the graph’s
ObjectID
or its name later.
- property key¶
The key of the corresponding graph in engine.
- classmethod load_from(path, sess, **kwargs)[source]¶
Construct a Graph by deserialize from path. It will read all serialization files, which is dumped by Graph.serialize. If any serialize file doesn’t exists or broken, will error out.
- Parameters
path (str) – Path contains the serialization files.
sess (graphscope.Session) – The target session that the graph will be construct in
- Returns
- A new graph object. Schema and data is supposed to be
identical with the one that called serialized method.
- Return type
Graph
- save_to(path, **kwargs)[source]¶
Serialize graph to a location. The meta and data of graph is dumped to specified location, and can be restored by Graph.deserialize in other sessions.
Each worker will write a path_{worker_id}.meta file and a path_{worker_id} file to storage. :param path: supported storages are local, hdfs, oss, s3 :type path: str
- property schema¶
Schema of the graph.
- Returns
the schema of the graph
- Return type
GraphSchema
- property schema_path¶
Path that Coordinator will write interactive schema path to.
- Returns
The path contains the schema. for interactive engine.
- Return type
str
- property session_id¶
Get the currrent session_id.
- Returns
Return session id that the graph belongs to.
- Return type
str
- to_dataframe(selector, vertex_range=None)[source]¶
Select some elements of the graph and output as a pandas.DataFrame
- Parameters
selector (dict) – Select some portions of graph.
vertex_range (dict, optional) – Slice vertices. Defaults to None.
- Returns
pandas.DataFrame
- to_directed()[source]¶
Returns a directed representation of the graph.
- Returns
- A directed graph with the same name, same nodes, and
with each edge (u, v, data) replaced by two directed edges (u, v, data) and (v, u, data).
- Return type
- to_numpy(selector, vertex_range=None)[source]¶
Select some elements of the graph and output to numpy.
- Parameters
selector (str) – Select a portion of graph as a numpy.ndarray.
vertex_range (dict, optional) – Slice vertices. Defaults to None.
- Returns
numpy.ndarray
- to_undirected()[source]¶
Returns an undirected representation of the digraph.
- Returns
- 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, they will both be preserved. You must check and correct for this manually if desired.
- Return type
- property vineyard_id¶
Get the vineyard object_id of this graph.
- Returns
return vineyard id of this graph
- Return type
str
Loader object¶
- class graphscope.framework.loader.Loader(source, delimiter=',', header_row=True, **kwargs)[source]¶
Generic data source wrapper. Loader can take various data sources, and assemble necessary information into a AttrValue.
- __init__(source, delimiter=',', header_row=True, **kwargs)[source]¶
Initialize a loader with configurable options. Note: Loader cannot be reused since it may change inner state when constructing information for loading a graph.
- Parameters
source (str or value) –
The data source to be load, which could be one of the followings:
local file: specified by URL
file://...
oss file: specified by URL
oss://...
hdfs file: specified by URL
hdfs://...
s3 file: specified by URL
s3://...
numpy ndarray, in CSR format
pandas dataframe
Ordinary data sources can be loaded using vineyard stream as well, a
vineyard://
prefix can be used in the URL then the local file, oss object or HDFS file will be loaded into a vineyard stream first, then GraphScope’s fragment will be built upon those streams in vineyard.Once the stream IO in vineyard reaches a stable state, it will be the default mode to load data sources and construct fragments in GraphScope.
delimiter (char, optional) – Column delimiter. Defaults to ‘,’
header_row (bool, optional) – Whether source have a header. If true, column names will be read from the first row of source, else they are named by ‘f0’, ‘f1’, …. Defaults to True.
Notes
Data is resolved by drivers in vineyard . See more additional info in Loading Graph section of Docs, and implementations in vineyard.
Graph Functions¶
- graphscope.framework.graph_builder.load_from(edges: Union[Mapping[str, Union[Sequence, graphscope.framework.loader.Loader, str, Sequence[numpy.ndarray], pandas.core.frame.DataFrame, vineyard._C.Object, vineyard._C.ObjectID, vineyard._C.ObjectName, Mapping]], graphscope.framework.loader.Loader, str, Sequence[numpy.ndarray], pandas.core.frame.DataFrame, vineyard._C.Object, vineyard._C.ObjectID, vineyard._C.ObjectName, Sequence], vertices: Optional[Union[Mapping[str, Union[Sequence, graphscope.framework.loader.Loader, str, Sequence[numpy.ndarray], pandas.core.frame.DataFrame, vineyard._C.Object, vineyard._C.ObjectID, vineyard._C.ObjectName, Mapping]], graphscope.framework.loader.Loader, str, Sequence[numpy.ndarray], pandas.core.frame.DataFrame, vineyard._C.Object, vineyard._C.ObjectID, vineyard._C.ObjectName, Sequence]] = None, directed=True, oid_type='int64_t', generate_eid=True, vformat=None, eformat=None, vertex_map='global') → graphscope.framework.graph.Graph[source]¶
Load a Arrow property graph using a list of vertex/edge specifications.
Deprecated since version version: 0.3 Use
graphscope.Graph()
instead.- Use Dict of tuples to setup a graph.
We can use a dict to set vertex and edge configurations, which can be used to build graphs.
Examples:
g = graphscope_session.load_from( edges={ "group": [ ( "file:///home/admin/group.e", ["group_id", "member_size"], ("leader_student_id", "student"), ("member_student_id", "student"), ), ( "file:///home/admin/group_for_teacher_student.e", ["group_id", "group_name", "establish_date"], ("teacher_in_charge_id", "teacher"), ("member_student_id", "student"), ), ] }, vertices={ "student": ( "file:///home/admin/student.v", ["name", "lesson_nums", "avg_score"], "student_id", ), "teacher": ( "file:///home/admin/teacher.v", ["name", "salary", "age"], "teacher_id", ), }, )
‘e’ is the label of edges, and ‘v’ is the label for vertices, edges are stored in the ‘both_in_out’ format edges with label ‘e’ linking from ‘v’ to ‘v’.
- Use Dict of dict to setup a graph.
We can also give each element inside the tuple a meaningful name, makes it more understandable.
Examples:
g = graphscope_session.load_from( edges={ "group": [ { "loader": "file:///home/admin/group.e", "properties": ["group_id", "member_size"], "source": ("leader_student_id", "student"), "destination": ("member_student_id", "student"), }, { "loader": "file:///home/admin/group_for_teacher_student.e", "properties": ["group_id", "group_name", "establish_date"], "source": ("teacher_in_charge_id", "teacher"), "destination": ("member_student_id", "student"), }, ] }, vertices={ "student": { "loader": "file:///home/admin/student.v", "properties": ["name", "lesson_nums", "avg_score"], "vid": "student_id", }, "teacher": { "loader": "file:///home/admin/teacher.v", "properties": ["name", "salary", "age"], "vid": "teacher_id", }, }, )
- Parameters
edges – Edge configuration of the graph
vertices (optional) – Vertices configurations of the graph. Defaults to None. If None, we assume all edge’s src_label and dst_label are deduced and unambiguous.
directed (bool, optional) – Indicate whether the graph should be treated as directed or undirected.
oid_type (str, optional) – ID type of graph. Can be “int64_t” or “string”. Defaults to “int64_t”.
generate_eid (bool, optional) – Whether to generate a unique edge id for each edge. Generated eid will be placed in third column. This feature is for cooperating with interactive engine. If you only need to work with analytical engine, set it to False. Defaults to False.
vertex_map (str, optional) – Indicate use global vertex map or local vertex map. Can be “global” or “local”.