def test_error_on_operation_on_graph(graphscope_session): g = Graph(graphscope_session) with pytest.raises(RuntimeError, match="Empty graph"): g.project_to_simple(v_label=0, v_prop=0, e_label=0, e_prop=0) with pytest.raises(RuntimeError): property_sssp(g, src=6)
def test_properties_omitted_loader(graphscope_session, student_group_e, student_v): graph = Graph(graphscope_session, generate_eid=False) graph = graph.add_vertices(student_v, "student", [], "student_id") graph = graph.add_edges(student_group_e, "group", []) assert len(graph.schema.vertex_properties[0]) == 4 assert len(graph.schema.edge_properties[0]) == 2
def test_error_on_ambigious_default_label(graphscope_session, student_group_e, student_v, teacher_v): graph = Graph(graphscope_session) graph = graph.add_vertices(student_v, "student") graph = graph.add_vertices(teacher_v, "teacher") with pytest.raises(AssertionError, match="ambiguous vertex label"): graph = graph.add_edges(student_group_e, "group")
def test_v_property_omitted_form_loader(graphscope_session, student_group_e, student_v): # properties for v can be omit, all columns will be load, # the first one used as vid by # default. default vlabel would be '_'; graph = Graph(graphscope_session) graph = graph.add_vertices(student_v, "student") graph = graph.add_edges( student_group_e, "group", ["group_id", "member_size"], src_field="leader_student_id", dst_field="member_student_id", ) assert graph.loaded()
def test_error_on_non_default_and_non_existing_v_label(graphscope_session, student_group_e, student_v): graph = Graph(graphscope_session) graph = graph.add_vertices(student_v, "student") with pytest.raises(ValueError, match="src label or dst_label not existed in graph"): graph = graph.add_edges(student_group_e, "group", src_label="v", dst_label="v") with pytest.raises(ValueError, match="must be both specified or either unspecified"): graph = graph.add_edges(student_group_e, src_label="v")
def test_error_on_src_dst_refer_to_same_col(graphscope_session, student_group_e): graph = Graph(graphscope_session) with pytest.raises(AssertionError, match="cannot refer to the same field"): graph = graph.add_edges( student_group_e, "group", src_field="leader_student_id", dst_field="leader_student_id", ) with pytest.raises(AssertionError, match="cannot refer to the same field"): graph = graph.add_edges(student_group_e, "group", src_field=0, dst_field=0)
def test_multiple_add_vertices_edges(graphscope_session): prefix = os.path.expandvars("${GS_TEST_DIR}/modern_graph") graph = Graph(graphscope_session) graph = graph.add_vertices(Loader(f"{prefix}/person.csv", delimiter="|"), "person") graph = graph.add_edges(Loader(f"{prefix}/knows.csv", delimiter="|"), "knows") graph = graph.add_vertices(Loader(f"{prefix}/software.csv", delimiter="|"), "software") graph = graph.add_edges( Loader(f"{prefix}/created.csv", delimiter="|"), "created", src_label="person", dst_label="software", ) assert graph.schema.vertex_labels == ["person", "software"] assert graph.schema.edge_labels == ["created", "knows"] graph = graph.add_vertices(Loader(f"{prefix}/person.csv", delimiter="|"), "person2") graph = graph.add_edges( Loader(f"{prefix}/knows.csv", delimiter="|"), "knows2", src_label="person2", dst_label="person2", ) graph = graph.add_vertices(Loader(f"{prefix}/software.csv", delimiter="|"), "software2") graph = graph.add_edges( Loader(f"{prefix}/created.csv", delimiter="|"), "created2", src_label="person2", dst_label="software2", ) assert sorted(graph.schema.vertex_labels) == [ "person", "person2", "software", "software2", ] assert sorted(graph.schema.edge_labels) == [ "created", "created2", "knows", "knows2", ]
def test_load_graph_copy(graphscope_session, arrow_property_graph): g = arrow_property_graph g2 = Graph(graphscope_session, g) assert g.key != g2.key assert g.vineyard_id != g2.vineyard_id assert str(g.schema) == str(g2.schema) assert np.all(g.to_numpy("v:v0.id") == g2.to_numpy("v:v0.id")) g2.unload() assert not g2.loaded() # test load from vineyard's graph g3 = Graph(graphscope_session, vineyard.ObjectID(g.vineyard_id)) assert g3.loaded()
def test_vid_omitted_form_loader(graphscope_session, student_group_e, student_v): # vid can be omit, the first column will be used as vid; graph = Graph(graphscope_session) graph = graph.add_vertices(student_v, "student", ["name", "lesson_nums", "avg_score"]) graph = graph.add_edges( student_group_e, "group", ["group_id", "member_size"], src_label="student", dst_label="student", src_field="leader_student_id", dst_field="member_student_id", ) assert graph.loaded()
def test_serialize_roundtrip(gs_session_distributed, p2p_property_dir): graph = gs_session_distributed.load_from( edges={ "knows": ( Loader("{}/p2p-31_property_e_0".format(p2p_property_dir), header_row=True), ["src_label_id", "dst_label_id", "dist"], ("src_id", "person"), ("dst_id", "person"), ), }, vertices={ "person": Loader("{}/p2p-31_property_v_0".format(p2p_property_dir), header_row=True), }, generate_eid=False, ) graph.serialize("/tmp/serialize") new_graph = Graph.deserialize("/tmp/serialize", gs_session_distributed) pg = new_graph.project_to_simple(0, 0, 0, 2) ctx = graphscope.sssp(pg, src=6) ret = (ctx.to_dataframe({ "node": "v.id", "r": "r" }, vertex_range={ "end": 6 }).sort_values(by=["node"]).to_numpy(dtype=float)) expect = np.array([[1.0, 260.0], [2.0, 229.0], [3.0, 310.0], [4.0, 256.0], [5.0, 303.0]]) assert np.all(ret == expect)
def test_serialize_roundtrip(gs_session_distributed, p2p_property_dir): graph = gs_session_distributed.g(generate_eid=False) graph = graph.add_vertices(f"{p2p_property_dir}/p2p-31_property_v_0", "person") graph = graph.add_edges( f"{p2p_property_dir}/p2p-31_property_e_0", label="knows", src_label="person", dst_label="person", ) graph.save_to("/tmp/serialize") new_graph = Graph.load_from("/tmp/serialize", gs_session_distributed) pg = new_graph.project_to_simple(0, 0, 0, 2) ctx = graphscope.sssp(pg, src=6) ret = (ctx.to_dataframe({ "node": "v.id", "r": "r" }, vertex_range={ "end": 6 }).sort_values(by=["node"]).to_numpy(dtype=float)) expect = np.array([[1.0, 260.0], [2.0, 229.0], [3.0, 310.0], [4.0, 256.0], [5.0, 303.0]]) assert np.all(ret == expect)
def load_ogbn_mag(sess, prefix): """Load ogbn_mag graph. The ogbn-mag dataset is a heterogeneous network composed of a subset of the Microsoft Academic Graph (MAG). See more details here: https://ogb.stanford.edu/docs/nodeprop/#ogbn-mag Args: sess (:class:`graphscope.Session`): Load graph within the session. prefix (str): Data directory. directed (bool, optional): Determine to load a directed or undirected graph. Defaults to True. Returns: :class:`graphscope.Graph`: A Graph object which graph type is ArrowProperty """ graph = Graph(sess) graph = (graph.add_vertices(os.path.join( prefix, "paper.csv"), "paper").add_vertices( os.path.join(prefix, "author.csv"), "author").add_vertices( os.path.join(prefix, "institution.csv"), "institution").add_vertices( os.path.join(prefix, "field_of_study.csv"), "field_of_study").add_edges( os.path.join(prefix, "author_affiliated_with_institution.csv"), "affiliated", src_label="author", dst_label="institution", ).add_edges( os.path.join(prefix, "paper_has_topic_field_of_study.csv"), "hasTopic", src_label="paper", dst_label="field_of_study", ).add_edges( os.path.join(prefix, "paper_cites_paper.csv"), "cites", src_label="paper", dst_label="paper", ).add_edges( os.path.join(prefix, "author_writes_paper.csv"), "writes", src_label="author", dst_label="paper", )) return graph
def test_complete_form_loader(graphscope_session, student_group_e, student_v): # a complete form for loading from ev files. # types are inferred from Loader. graph = Graph(graphscope_session) graph = graph.add_vertices(student_v, "student", ["name", "lesson_nums", "avg_score"], "student_id") graph = graph.add_edges( student_group_e, "group", ["group_id", "member_size"], src_label="student", dst_label="student", src_field="leader_student_id", dst_field="member_student_id", ) assert graph.loaded()
def test_Load_complex_graph_variants( graphscope_session, score_e, student_group_e_df, student_v_array, teacher_v_oss, lesson_v_mars, ): graph = Graph(graphscope_session) graph = graph.add_vertices(student_v_array, "student", ["name", "lesson_nums", "avg_score"], "student_id") graph = graph.add_vertices(teacher_v_oss, "teacher", ["student_num", "score", "email", "tel"]) graph = graph.add_vertices(lesson_v_mars, "lesson") graph = graph.add_edges( score_e, "score", ["score", "score_id"], src_label="student", dst_label="lesson", src_field="studnet_id", dst_field="subject", ) graph = graph.add_edges( student_group_e_df, "group", ["member_size"], src_label="student", dst_label="student", src_field="leader_studnet_id", dst_field="member_student_id", ) assert graph.schema is not None
def test_multi_src_dst_edge_loader(graphscope_session, student_group_e, teacher_group_e, student_v, teacher_v): graph = Graph(graphscope_session) graph = graph.add_vertices(student_v, "student", ["name", "lesson_nums", "avg_score"], "student_id") graph = graph.add_vertices(teacher_v, "teacher", ["student_num", "score", "email", "tel"], "teacher_id") graph = graph.add_edges( student_group_e, "group", ["group_id", "member_size"], src_label="student", dst_label="student", src_field="leader_student_id", dst_field="member_student_id", ) graph = graph.add_edges( teacher_group_e, "group", ["group_id", "member_size"], src_label="teacher", dst_label="teacher", src_field="leader_teacher_id", dst_field="member_teacher_id", ) assert graph.loaded()
def test_error_on_transform_graph(): g = Graph() ag = gs.to_appendable(g) with pytest.raises(AssertionError, match="expect source graph is immutable"): agg = gs.to_appendable(ag) g2 = gs.to_immutable(ag) with pytest.raises(AssertionError, match="expect source graph is appendable"): g3 = gs.to_immutable(g2)
def __call__(self, graph: Graph, *args, **kwargs): kwargs_extend = dict(app_class=self.java_app_class, **kwargs) if not hasattr(graph, "graph_type"): raise InvalidArgumentError("Missing graph_type attribute in graph object.") if ( self.java_app_type.find("simple") != -1 and graph.graph_type == graph_def_pb2.ARROW_PROPERTY ): graph = graph._project_to_simple() app_ = graph.session._wrapper(JavaAppDagNode(graph, self)) return app_(*args, **kwargs_extend)
def test_loader_with_specified_data_type(graphscope_session, student_group_e, student_v): graph = Graph(graphscope_session, oid_type="string", generate_eid=False) graph = graph.add_vertices( student_v, "student", ["name", ("lesson_nums", "int"), ("avg_score", "float")], "student_id", ) graph = graph.add_edges(student_group_e, "group", ["group_id", ("member_size", "int")]) assert graph.schema.vertex_properties == [{ "name": 21, "lesson_nums": 11, "avg_score": 18, "student_id": 21 }] assert graph.schema.edge_properties == [{ "group_id": 21, "member_size": 11 }]
def test_add_vertices_edges(graphscope_session): prefix = os.path.expandvars("${GS_TEST_DIR}/modern_graph") graph = Graph(graphscope_session) graph = graph.add_vertices(Loader(f"{prefix}/person.csv", delimiter="|"), "person") graph = graph.add_edges(Loader(f"{prefix}/knows.csv", delimiter="|"), "knows") assert graph.schema.vertex_labels == ["person"] assert graph.schema.edge_labels == ["knows"] with pytest.raises(ValueError, match="src label and dst label cannot be None"): graph = graph.add_edges(Loader(f"{prefix}/knows.csv", delimiter="|"), "created") with pytest.raises(ValueError, match="src label or dst_label not existed in graph"): graph = graph.add_edges( Loader(f"{prefix}/created.csv", delimiter="|"), "created", src_label="person", dst_label="software", ) graph = graph.add_vertices(Loader(f"{prefix}/software.csv", delimiter="|"), "software") with pytest.raises(ValueError, match="Cannot add new relation to existed graph"): graph = graph.add_edges( Loader(f"{prefix}/knows.csv", delimiter="|"), "knows", src_label="software", dst_label="software", ) graph = graph.add_edges( Loader(f"{prefix}/created.csv", delimiter="|"), "created", src_label="person", dst_label="software", ) assert graph.schema.vertex_labels == ["person", "software"] assert graph.schema.edge_labels == ["knows", "created"]
def arrow_property_graph_lpa(graphscope_session): g = Graph(graphscope_session, generate_eid=False) g = g.add_vertices(f"{property_dir}/lpa_dataset/lpa_3000_v_0", "v0") g = g.add_vertices(f"{property_dir}/lpa_dataset/lpa_3000_v_1", "v1") g = g.add_edges(f"{property_dir}/lpa_dataset/lpa_3000_e_0", "e0", ["weight"], "v0", "v1") yield g g.unload()
def test_error_on_duplicate_labels(graphscope_session, student_group_e, student_v): graph = Graph(graphscope_session) graph = graph.add_vertices(student_v, "student") with pytest.raises(ValueError, match="Label student already existed in graph"): graph = graph.add_vertices(student_v, "student") graph = graph.add_edges(student_group_e, "group") with pytest.raises(ValueError, match="already existed in graph"): graph = graph.add_edges(student_group_e, "group")
def test_serialize_roundtrip(p2p_property_dir): gs_image, gie_manager_image = get_gs_image_on_ci_env() sess = graphscope.session( num_workers=2, k8s_gs_image=gs_image, k8s_gie_graph_manager_image=gie_manager_image, k8s_coordinator_cpu=0.5, k8s_coordinator_mem="2500Mi", k8s_vineyard_cpu=0.1, k8s_vineyard_mem="512Mi", k8s_engine_cpu=0.1, k8s_engine_mem="1500Mi", k8s_vineyard_shared_mem="2Gi", k8s_volumes=get_k8s_volumes(), ) graph = sess.load_from( edges={ "knows": ( Loader("{}/p2p-31_property_e_0".format(p2p_property_dir), header_row=True), ["src_label_id", "dst_label_id", "dist"], ("src_id", "person"), ("dst_id", "person"), ), }, vertices={ "person": Loader("{}/p2p-31_property_v_0".format(p2p_property_dir), header_row=True), }, generate_eid=False, ) graph.serialize("/tmp/serialize") graph.unload() new_graph = Graph.deserialize("/tmp/serialize", sess) pg = new_graph.project_to_simple(0, 0, 0, 2) ctx = graphscope.sssp(pg, src=6) ret = (ctx.to_dataframe({ "node": "v.id", "r": "r" }, vertex_range={ "end": 6 }).sort_values(by=["node"]).to_numpy(dtype=float)) expect = np.array([[1.0, 260.0], [2.0, 229.0], [3.0, 310.0], [4.0, 256.0], [5.0, 303.0]]) assert np.all(ret == expect)
def test_load_complex_graph( graphscope_session, score_e, student_teacher_e, teacher_lesson_e, student_v, teacher_v, lesson_v, ): graph = Graph(graphscope_session, oid_type="string") graph = graph.add_vertices(student_v, "student", ["name", "lesson_nums", "avg_score"], "student_id") graph = graph.add_vertices(teacher_v, "teacher", ["student_num", "score", "email", "tel"]) graph = graph.add_vertices(lesson_v, "lesson") graph = graph.add_edges( score_e, "score", ["score", "score_id"], src_label="student", dst_label="lesson", src_field="student_id", dst_field="subject", ) graph = graph.add_edges( student_teacher_e, "student_teacher", ["teaching_score"], src_label="student", dst_label="teacher", src_field="student_id", dst_field="teacher_id", ) graph = graph.add_edges( teacher_lesson_e, "teacher_lesson", ["times"], src_label="teacher", dst_label="lesson", src_field="teacher_id", dst_field="lesson", ) assert graph.schema is not None
def p2p_property_graph_undirected(graphscope_session): g = Graph(graphscope_session, directed=False, generate_eid=False) g = g.add_vertices(f"{property_dir}/p2p-31_property_v_0", "person") g = g.add_edges( f"{property_dir}/p2p-31_property_e_0", label="knows", src_label="person", dst_label="person", ) yield g g.unload()
def test_error_on_operation_on_graph(graphscope_session): g = Graph(graphscope_session.session_id) with pytest.raises(RuntimeError, match="The graph is not registered in remote"): g.project_to_simple(v_label=0, v_prop=0, e_label=0, e_prop=0) with pytest.raises(RuntimeError, match="The graph is not registered in remote"): g.unload() with pytest.raises(AssertionError): property_sssp(g, src=6)
def test_errors_on_files(graphscope_session, one_column_file, double_type_id_file, empty_file): with pytest.raises(AnalyticalEngineInternalError, match="Object not exists"): Graph(graphscope_session, vineyard.ObjectName("non_exist_vy_name"))._ensure_loaded() return graph = graphscope_session.g() with pytest.raises(AnalyticalEngineInternalError, match="IOError"): non_existing_file = "file:///abc" graph.add_edges(Loader(non_existing_file))._ensure_loaded() with pytest.raises(AnalyticalEngineInternalError, match="Index out of range: 1"): graph.add_edges(one_column_file)._ensure_loaded() with pytest.raises(AnalyticalEngineInternalError, match="CSV conversion error to int64"): graph.add_edges(double_type_id_file)._ensure_loaded() with pytest.raises(AnalyticalEngineInternalError, match="End Of File"): graph.add_edges(empty_file)._ensure_loaded()
def test_remove_vertices_edges(graphscope_session): prefix = os.path.expandvars("${GS_TEST_DIR}/modern_graph") graph = (Graph(graphscope_session).add_vertices( Loader(f"{prefix}/person.csv", delimiter="|"), "person").add_edges(Loader(f"{prefix}/knows.csv", delimiter="|"), "knows")) another_graph = graph.add_vertices( Loader(f"{prefix}/software.csv", delimiter="|"), "software").add_edges( Loader("{prefix}/created.csv", delimiter="|"), "created", src_label="person", dst_label="software", ) another_graph = another_graph.remove_edges("created") another_graph = another_graph.remove_vertices("software") assert graph.schema.vertex_labels == another_graph.schema.vertex_labels assert graph.schema.edge_labels == another_graph.schema.edge_labels
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)
def test_load_from_pandas(graphscope_session, student_group_e_df, student_v_df): graph = Graph(graphscope_session) graph = graph.add_vertices(student_v_df) graph = graph.add_edges(student_group_e_df) assert graph.loaded()
def test_load_from_numpy(graphscope_session, student_group_e_array, student_v_array): graph = Graph(graphscope_session) graph = graph.add_vertices(student_v_array) graph = graph.add_edges(student_group_e_array) assert graph.loaded()