Context

Context object

class graphscope.framework.context.BaseContext(session_id, context_key, graph)[source]

Base class of concrete contexts. 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.

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() ).

Examples:

>>> g = get_test_property_graph()
>>> sg = g.project(vertices={'person': ['id']}, edges={'knows': ['weight']})
>>> ret = sssp(sg, 20)
>>> out = ret.to_numpy('r')
>>> out.shape
(20345,)
>>> out = ret.to_dataframe({'id': 'v.id', 'result': 'r'})
>>> out.shape
(20345, 2)
>>> out = ret.to_vineyard_tensor()  # return an object id
>>> out = ret.to_vineyard_dataframe()  # return an object id
output(fd, selector, vertex_range=None, **kwargs)[source]

Dump results to fd. Support dumps data to local (respect to pod) files, hdfs or oss. It first write results to a vineyard dataframe, and let vineyard do the data dumping job. fd must meet specific formats, with auth information if needed. As follows:

  • local

    file:///tmp/result_path

  • oss

    oss:///bucket/object

  • hdfs

    hdfs:///tmp/result_path

Parameters
  • fd (str) – Output location.

  • selector (dict) – Similar to to_dataframe.

  • vertex_range (dict, optional) – Similar to to_dataframe. Defaults to None.

  • kwargs (dict, optional) – Storage options with respect to output storage type. for example: key, secret, endpoint for oss, key, secret, client_kwargs for s3, host, port for hdfs, None for local.

to_dataframe(selector, vertex_range=None)[source]

Return results 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 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

pandas.DataFrame

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

Return context data as numpy array

Parameters
  • selector (str) – Describes how to select values of context. See more details in derived context 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

numpy.ndarray.

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

Return results as a vineyard dataframe. Only object id is returned.

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 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

object id of vineyard tensor

Return type

str

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

Return results as a vineyard tensor. Only object id is returned.

Returns

object id of vineyard tensor

Return type

str

class graphscope.framework.context.TensorContext(session_id, context_key, graph)[source]

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

class graphscope.framework.context.VertexDataContext(session_id, context_key, graph)[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:
    • 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.LabeledVertexDataContext(session_id, context_key, graph)[source]

The labeld 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:
    • 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.VertexPropertyContext(session_id, context_key, graph)[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:
    • 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.LabeledVertexPropertyContext(session_id, context_key, graph)[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:
    • 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.