示例#1
0
def _get_config(
    edges: Sequence[EdgeLabel],
    vertices: Sequence[VertexLabel],
    directed: bool,
    oid_type: str,
    generate_eid: bool,
) -> Dict:
    config = {}
    attr = attr_value_pb2.AttrValue()

    for label in chain(edges, vertices):
        label.finish(oid_type)

    for edge in edges:
        attr.list.func.extend([process_edge(edge)])

    attr.list.func.extend([process_vertex(vertex) for vertex in vertices])

    directed_attr = utils.b_to_attr(directed)
    generate_eid_attr = utils.b_to_attr(generate_eid)
    config[types_pb2.ARROW_PROPERTY_DEFINITION] = attr
    config[types_pb2.DIRECTED] = directed_attr
    config[types_pb2.OID_TYPE] = utils.s_to_attr(oid_type)
    config[types_pb2.GENERATE_EID] = generate_eid_attr
    # vid_type is fixed
    config[types_pb2.VID_TYPE] = utils.s_to_attr("uint64_t")
    config[types_pb2.IS_FROM_VINEYARD_ID] = utils.b_to_attr(False)
    return config
示例#2
0
 def _construct_op_of_empty_graph(self):
     config = {}
     config[types_pb2.ARROW_PROPERTY_DEFINITION] = attr_value_pb2.AttrValue()
     config[types_pb2.DIRECTED] = utils.b_to_attr(self._directed)
     config[types_pb2.GENERATE_EID] = utils.b_to_attr(self._generate_eid)
     config[types_pb2.OID_TYPE] = utils.s_to_attr(self._oid_type)
     config[types_pb2.VID_TYPE] = utils.s_to_attr("uint64_t")
     config[types_pb2.IS_FROM_VINEYARD_ID] = utils.b_to_attr(False)
     return dag_utils.create_graph(
         self.session_id, graph_def_pb2.ARROW_PROPERTY, inputs=None, attrs=config
     )
示例#3
0
def create_graph(session_id, graph_type, **kwargs):
    """Create an `CREATE_GRAPH` op, add op to default dag.

    Args:
        session_id (str): Refer to session that the graph will be create on.
        graph_type (:enum:`GraphType`): GraphType defined in proto.types.proto.
        **kwargs: additional properties respect to different `graph_type`.

    Returns:
        An op to create a graph in c++ side with necessary configurations.
    """
    config = {
        types_pb2.GRAPH_TYPE: utils.graph_type_to_attr(graph_type),
    }

    if graph_type == types_pb2.ARROW_PROPERTY:
        attrs = kwargs.pop("attrs", None)
        if attrs:
            for k, v in attrs.items():
                if isinstance(v, attr_value_pb2.AttrValue):
                    config[k] = v
    elif graph_type == types_pb2.DYNAMIC_PROPERTY:
        config[types_pb2.E_FILE] = utils.s_to_attr(kwargs["efile"])
        config[types_pb2.V_FILE] = utils.s_to_attr(kwargs["vfile"])
        config[types_pb2.DIRECTED] = utils.b_to_attr(kwargs["directed"])
    else:
        raise RuntimeError("Not supported graph type {}".format(graph_type))

    op = Operation(session_id,
                   types_pb2.CREATE_GRAPH,
                   config=config,
                   output_types=types_pb2.GRAPH)
    return op
示例#4
0
def add_labels_to_graph(graph, loader_op):
    """Add new labels to existed graph.

    Args:
        graph (:class:`Graph`): A graph instance.
            May not be fully loaded. i.e. it's in a building
            procedure.
        loader_op (:class:`graphscope.framework.operation.Operation`):
            Operation of loader.

    Raises:
        NotImplementedError: When encountered not supported graph type.

    Returns:
        The operation.

    Notes:
        Since we don't want to trigger the loading, we must not use
        any api that can trigger the loading process implicitly.
    """
    from graphscope.framework.graph import GraphDAGNode

    assert isinstance(graph, GraphDAGNode)
    inputs = [graph.op, loader_op]
    # vid_type is fixed
    config = {
        types_pb2.GRAPH_TYPE: utils.graph_type_to_attr(graph._graph_type),
        types_pb2.DIRECTED: utils.b_to_attr(graph._directed),
        types_pb2.OID_TYPE: utils.s_to_attr(graph._oid_type),
        types_pb2.GENERATE_EID: utils.b_to_attr(graph._generate_eid),
        types_pb2.VID_TYPE: utils.s_to_attr("uint64_t"),
        types_pb2.IS_FROM_VINEYARD_ID: utils.b_to_attr(False),
    }
    # inferred from the context of the dag.
    config.update({types_pb2.GRAPH_NAME: utils.place_holder_to_attr()})
    if graph._graph_type != graph_def_pb2.ARROW_PROPERTY:
        raise NotImplementedError(
            f"Add vertices or edges is not supported yet on graph type {graph._graph_type}"
        )
    op = Operation(
        graph._session.session_id,
        types_pb2.ADD_LABELS,
        inputs=inputs,
        config=config,
        output_types=types_pb2.GRAPH,
    )
    return op
示例#5
0
 def _from_vineyard_name(self, vineyard_name):
     config = {}
     config[types_pb2.IS_FROM_VINEYARD_ID] = utils.b_to_attr(True)
     config[types_pb2.VINEYARD_NAME] = utils.s_to_attr(str(vineyard_name))
     # FIXME(hetao) hardcode oid/vid type for codegen, when loading from vineyard
     #
     # the metadata should be retrived from vineyard
     config[types_pb2.OID_TYPE] = utils.s_to_attr("int64_t")
     config[types_pb2.VID_TYPE] = utils.s_to_attr("uint64_t")
     return dag_utils.create_graph(self.session_id,
                                   types_pb2.ARROW_PROPERTY,
                                   attrs=config)
示例#6
0
 def _from_vineyard_id(self, vineyard_id):
     config = {}
     config[types_pb2.IS_FROM_VINEYARD_ID] = b_to_attr(True)
     config[types_pb2.VINEYARD_ID] = i_to_attr(vineyard_id)
     # FIXME(hetao) hardcode oid/vid type for codegen, when loading from vineyard
     #
     # the metadata should be retrived from vineyard
     config[types_pb2.OID_TYPE] = s_to_attr("int64_t")
     config[types_pb2.VID_TYPE] = s_to_attr("uint64_t")
     op = create_graph(self._session_id, types_pb2.ARROW_PROPERTY, attrs=config)
     graph_def = op.eval()
     return graph_def
示例#7
0
 def _construct_op_from_vineyard_id(self, vineyard_id):
     assert self._session is not None
     config = {}
     config[types_pb2.IS_FROM_VINEYARD_ID] = utils.b_to_attr(True)
     config[types_pb2.VINEYARD_ID] = utils.i_to_attr(int(vineyard_id))
     # FIXME(hetao) hardcode oid/vid type for codegen, when loading from vineyard
     #
     # the metadata should be retrived from vineyard
     config[types_pb2.OID_TYPE] = utils.s_to_attr("int64_t")
     config[types_pb2.VID_TYPE] = utils.s_to_attr("uint64_t")
     return dag_utils.create_graph(
         self.session_id, graph_def_pb2.ARROW_PROPERTY, attrs=config
     )
示例#8
0
def load_from(
    edges: Union[Mapping[str, Union[LoaderVariants, Sequence, Mapping]],
                 LoaderVariants, Sequence],
    vertices: Union[Mapping[str, Union[LoaderVariants, Sequence, Mapping]],
                    LoaderVariants, Sequence, None, ] = None,
    directed=True,
    oid_type="int64_t",
    generate_eid=True,
    vformat=None,
    eformat=None,
) -> Graph:
    """Load a Arrow property graph using a list of vertex/edge specifications.

    .. deprecated:: version 0.3
       Use :class:`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:

        .. code:: ipython

            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:

        .. code:: ipython

            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",
                    },
                },
            )

    Args:
        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.
    """

    # Don't import the :code:`nx` in top-level statments to improve the
    # performance of :code:`import graphscope`.
    from graphscope import nx

    sess = get_default_session()
    if isinstance(edges, (Graph, nx.Graph, *VineyardObjectTypes)):
        return sess.g(edges)
    oid_type = utils.normalize_data_type_str(oid_type)
    if oid_type not in ("int64_t", "std::string"):
        raise ValueError("oid_type can only be int64_t or string.")
    v_labels = normalize_parameter_vertices(vertices, oid_type, vformat)
    e_labels = normalize_parameter_edges(edges, oid_type, eformat)
    # generate and add a loader op to dag
    loader_op = dag_utils.create_loader(v_labels + e_labels)
    sess.dag.add_op(loader_op)
    # construct create graph op
    config = {
        types_pb2.DIRECTED: utils.b_to_attr(directed),
        types_pb2.OID_TYPE: utils.s_to_attr(oid_type),
        types_pb2.GENERATE_EID: utils.b_to_attr(generate_eid),
        types_pb2.VID_TYPE: utils.s_to_attr("uint64_t"),
        types_pb2.IS_FROM_VINEYARD_ID: utils.b_to_attr(False),
    }
    op = dag_utils.create_graph(sess.session_id,
                                graph_def_pb2.ARROW_PROPERTY,
                                inputs=[loader_op],
                                attrs=config)
    graph = sess.g(op)
    return graph