Context

Context object

class graphscope.framework.context.BaseContextDAGNode(bound_app, graph, *args, **kwargs)[source]

Base class of concrete context DAG node.

In GraphScope, it will return a instance of concrete class ContextDAGNode after evaluating an app, that will be automatically executed by :method:`sess.run` in eager mode and return a instance of graphscope.framework.context.Context

We can further use the handle to retrieve data:
  • as a numpy.ndarray( to_numpy() ),

  • as a pandas.DataFrame( to_dataframe() ),

  • as a vineyard tensor ( to_vineyard_tensor() ),

  • or as a vineyard dataframe ( to_vineyard_dataframe() ).

The following example demonstrates its usage:

>>> # lazy mode
>>> import graphscope as gs
>>> sess = gs.session(mode="lazy")
>>> g = arrow_property_graph(sess)
>>> c = property_sssp(g, 20)
>>> print(c) # <graphscope.framework.context.LabeledVertexDataContextDAGNode>
>>> r1 = c.to_numpy("r:v0.dist_0")
>>> print(r1) # <graphscope.ramework.context.ResultDAGNode>
>>> r2 = c.to_dataframe({"id": "v:v0.id", "result": "r:v0.dist_0"})
>>> r3 = c.to_vineyard_tensor("v:v0.id")
>>> r4 = c.to_vineyard_dataframe({"id": "v:v0.id", "data": "v:v0.dist", "result": "r:v0.dist_0"})
>>> r = sess.run([r1, r2, r3, r4])
>>> r[0].shape
(40521,)
>>> r[1].shape
(40521, 2)
>>> r[2] # return an object id
>>> r[3] # return an object id

>>> # eager mode
>>> import graphscope as gs
>>> sess = gs.session(mode="eager")
>>> g = arrow_property_graph(sess)
>>> sg = g.project(vertices={'person': ['id']}, edges={'knows': ['weight']})
>>> c = sssp(sg, 20)
>>> print(c) # <graphscope.framework.context.Context>
>>> r1 = c.to_numpy('r')
>>> r1.shape
(20345,)
>>> r2 = c.to_dataframe({'id': 'v.id', 'result': 'r'})
>>> r2.shape
(20345, 2)
>>> r3 = c.to_vineyard_tensor() # return an object id
>>> r4 = c.to_vineyard_dataframe() # return an object id
to_dataframe(selector, vertex_range=None)[source]

Get the context data as a pandas DataFrame.

Parameters
  • selector – dict The key is column name in dataframe, and the value describes how to select values of context. See more details in derived context DAG node class.

  • vertex_range – dict, optional, default to None. Works as slicing. The expression {‘begin’: m, ‘end’: n} select a portion of vertices from m to, but not including n. Type of m, n must be identical with vertices’ oid type. Only the sub-ranges of vertices data will be retrieved. Note the comparision is not based on numeric order, but on alphabetic order.

Returns

A result holds the pandas.DataFrame, evaluated in eager mode.

Return type

graphscope.framework.context.ResultDAGNode

to_numpy(selector, vertex_range=None, axis=0)[source]

Get the context data as a numpy array.

Parameters
  • selector (str) – Describes how to select values of context. See more details in derived context DAG node class.

  • vertex_range (dict) – optional, default to None. Works as slicing. The expression {‘begin’: m, ‘end’: n} select a portion of vertices from m to, but not including n. Type of m, n must be identical with vertices’ oid type. Omitting the first index starts the slice at the beginning of the vertices, and omitting the second index extends the slice to the end of the vertices. Note the comparision is not based on numeric order, but on alphabetic order.

  • axis (int) – optional, default to 0.

Returns

A result holds the numpy.ndarray, evaluated in eager mode.

Return type

graphscope.framework.context.ResultDAGNode

to_vineyard_dataframe(selector=None, vertex_range=None)[source]

Get the context data as a vineyard dataframe and return the vineyard object id.

Parameters
  • selector – dict Key is used as column name of the dataframe, and the value describes how to select values of context. See more details in derived context DAG node class.

  • vertex_range – dict, optional, default to None Works as slicing. The expression {‘begin’: m, ‘end’: n} select a portion of vertices from m to, but not including n. Type of m, n must be identical with vertices’ oid type. Only the sub-ranges of vertices data will be retrieved.

Returns

A result hold the object id of vineyard dataframe, evaluated in eager mode.

Return type

graphscope.framework.context.ResultDAGNode

to_vineyard_tensor(selector=None, vertex_range=None, axis=0)[source]

Get the context data as a vineyard tensor and return the vineyard object id.

Returns

A result hold the object id of vineyard tensor, evaluated in eager mode.

Return type

graphscope.framework.context.ResultDAGNode

class graphscope.framework.context.TensorContextDAGNode(bound_app, graph, *args, **kwargs)[source]

Tensor context DAG node holds a tensor. Only axis is meaningful when considering a TensorContext.

class graphscope.framework.context.VertexDataContextDAGNode(bound_app, graph, *args, **kwargs)[source]

The most simple kind of context. A vertex has a single value as results.

  • The syntax of selector on vertex is:
    • v.id: Get the Id of vertices

    • v.data: Get the data of vertices

      If there is any, means origin data on the graph, not results.

  • The syntax of selector of edge is (not supported yet):
    • e.src: Get the source Id of edges

    • e.dst: Get the destination Id of edges

    • e.data: Get the edge data on the edges

      If there is any, means origin data on the graph

  • The syntax of selector of results is:
    • r: Get quering results of algorithms. e.g. Rankings of vertices after doing PageRank.

class graphscope.framework.context.LabeledVertexDataContextDAGNode(bound_app, graph, *args, **kwargs)[source]

The labeled kind of context. This context has several vertex labels and edge labels, and each label has several properties. Selection are performed on labels first, then on properties.

We use : to filter labels, and . to select properties. And the results has no property, only have labels.

  • The syntax of selector of vertex is:
    • v:label_name.id: Get Id that belongs to a specific vertex label.

    • v:label_name.property_name: Get data that on a specific property of a specific vertex label.

  • The syntax of selector of edge is (not supported yet):
    • e:label_name.src: Get source Id of a specific edge label.

    • e:label_name.dst: Get destination Id of a specific edge label.

    • e:label_name.property_name: Get data on a specific property of a specific edge label.

  • The syntax of selector of results is:
    • r:label_name: Get results data of a vertex label.

class graphscope.framework.context.VertexPropertyContextDAGNode(bound_app, graph, *args, **kwargs)[source]

The simple kind of context with property. A vertex can have multiple values (a.k.a. properties) as results.

  • The syntax of selector on vertex is:
    • v.id: Get the Id of vertices

    • v.data: Get the data of vertices

      If there is any, means origin data on the graph, not results

  • The syntax of selector of edge is (not supported yet):
    • e.src: Get the source Id of edges

    • e.dst: Get the destination Id of edges

    • e.data: Get the edge data on the edges

      If there is any, means origin data on the graph

  • The syntax of selector of results is:
    • r.column_name: Get the property named column_name in results.

      e.g. r.hub in graphscope.hits().

class graphscope.framework.context.LabeledVertexPropertyContextDAGNode(bound_app, graph, *args, **kwargs)[source]

The labeld kind of context with properties. This context has several vertex labels and edge labels, And each label has several properties. Selection are performed on labels first, then on properties.

We use : to filter labels, and . to select properties. And the results can have several properties.

  • The syntax of selector of vertex is:
    • v:label_name.id: Get Id that belongs to a specific vertex label.

    • v:label_name.property_name: Get data that on a specific property of a specific vertex label.

  • The syntax of selector of edge is (not supported yet):
    • e:label_name.src: Get source Id of a specific edge label.

    • e:label_name.dst: Get destination Id of a specific edge label.

    • e:label_name.property_name: Get data on a specific property of a specific edge label.

  • The syntax of selector of results is:
    • r:label_name.column_name: Get the property named column_name of label_name.

class graphscope.framework.context.Context(context_node, key, result_schema)[source]

Hold a handle of app querying context.

After evaluating an app, the context (vertex data, partial results, etc.) are preserved, and can be referenced through a handle.

class graphscope.framework.context.DynamicVertexDataContext(context_node, key)[source]

Vertex data context for complicated result store. A vertex has a single value as results.

class graphscope.framework.context.ResultDAGNode(dag_node, op)[source]

A class represents a result node in a DAG.

In GraphScope, result node is always a leaf node in a DAG.