Beispiel #1
0
    def add_vertices(self, vertices):
        vertices = graph_utils.normalize_parameter_vertices(vertices)
        # Configurations inherited from input graph
        # oid_type
        # CHECK label name not in existed edge labels
        vertex_labels = self._schema.vertex_labels
        for vertex in vertices:
            check_argument(
                vertex.label not in vertex_labels,
                f"Duplicate label name with existing vertex labels: {vertex.label}",
            )

        config = graph_utils.assemble_op_config([], vertices, self._directed,
                                                self._schema.oid_type,
                                                self._generate_eid)
        op = dag_utils.add_vertices(self, attrs=config)
        graph_def = op.eval()
        return Graph(self.session_id, graph_def)
Beispiel #2
0
    def add_edges(self, edges):
        edges = graph_utils.normalize_parameter_edges(edges)
        # directed, oid_type, generate_eid
        # CHECK:
        # 1. edge's src/dst labels must existed in vertex_labels
        # 2. label name not in existed edge labels
        vertex_labels = self._schema.vertex_labels
        edge_labels = self.schema.edge_labels
        graph_utils.check_edge_validity(edges, vertex_labels)
        for edge in edges:
            check_argument(
                edge.label not in edge_labels,
                f"Duplicate label name with existing edge labels: {edge.label}",
            )

        config = graph_utils.assemble_op_config(edges, [], self._directed,
                                                self._schema.oid_type,
                                                self._generate_eid)
        op = dag_utils.add_edges(self, attrs=config)
        graph_def = op.eval()
        return Graph(self.session_id, graph_def)
Beispiel #3
0
def process_add_edges(graph, edges):
    e_labels = normalize_parameter_edges(edges)
    # Configurations inherited from input graph
    # directed, oid_type, generate_eid
    # CHECK:
    # 1. edge's src/dst labels must existed in vertex_labels
    # 2. label name not in existed edge labels
    vertex_labels = graph.schema.vertex_labels
    edge_labels = graph.schema.edge_labels
    check_edge_validity(edges, vertex_labels)
    for edge in edges:
        check_argument(
            edge.label not in edge_labels,
            f"Duplicate label name with existing edge labels: {edge.label}",
        )

    config = assemble_op_config(e_labels, [], graph._directed,
                                graph.schema.oid_type, graph._generate_eid)
    op = dag_utils.add_edges(graph, attrs=config)
    graph_def = op.eval()
    return Graph(graph.session_id, graph_def)
Beispiel #4
0
    def _construct_graph(self,
                         vertices,
                         edges,
                         v_labels,
                         e_labels,
                         e_relations,
                         mutation_func=None):
        """Construct graph.
           1. Construct a graph from scratch.
              If the vertices and edges is empty, return a empty graph.
           2. Construct a graph from existed builded graph.
              If the vertices and edges is empty, return a copied graph.

        Args:
            vertices ([type]): [description]
            edges ([type]): [description]
            v_labels ([type]): [description]
            e_labels ([type]): [description]
            e_relations ([type]): [description]
            mutation_func ([type], optional): [description]. Defaults to None.

        Returns:
            [type]: [description]
        """
        config = graph_utils.assemble_op_config(
            vertices.values(),
            edges.values(),
            self._oid_type,
            self._directed,
            self._generate_eid,
        )

        # edge case.
        if not vertices and not edges:
            if mutation_func:
                # Rely on `self._key`
                return Graph(self._session, self)
            else:
                return Graph(
                    self._session,
                    None,
                    self._oid_type,
                    self._directed,
                    self._generate_eid,
                )
        if mutation_func:
            op = mutation_func(self, attrs=config)
        else:
            op = dag_utils.create_graph(self.session_id,
                                        types_pb2.ARROW_PROPERTY,
                                        attrs=config)

        graph = Graph(self._session, op, self._oid_type, self._directed,
                      self._generate_eid)
        graph._unsealed_vertices = vertices
        graph._unsealed_edges = edges
        graph._v_labels = v_labels
        graph._e_labels = e_labels
        graph._e_relationships = e_relations
        # propage info about whether is a loaded graph.
        # graph._key = self._key
        if mutation_func:
            graph._base_graph = self._base_graph or self
        return graph
Beispiel #5
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,
) -> 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.experimental import nx

    sess = get_default_session()
    if sess is None:
        raise ValueError("No default session found.")
    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)
    e_labels = normalize_parameter_edges(edges)
    config = assemble_op_config(v_labels, e_labels, oid_type, directed,
                                generate_eid)
    op = dag_utils.create_graph(sess.session_id,
                                types_pb2.ARROW_PROPERTY,
                                attrs=config)
    graph = sess.g(op)
    return graph