def test_1hop(self): """ Test case for sample 1 hop neighbor with strategy of edge_weight. All the src_ids have neighbors. """ expand_factor = 6 ids = self._seed_node1_ids nbr_s = self.g.neighbor_sampler(self._edge1_type, expand_factor=expand_factor, strategy="edge_weight") nbrs = nbr_s.get(ids) edges = nbrs.layer_edges(1) nodes = nbrs.layer_nodes(1) utils.check_fixed_edge_dst_ids(edges, dst_range=self._node2_range, expected_src_ids=ids) utils.check_edge_type(edges, src_type=self._node1_type, dst_type=self._node2_type, edge_type=self._edge1_type) utils.check_edge_shape(edges, ids.size * expand_factor) utils.check_edge_attrs(edges) utils.check_edge_labels(edges) utils.check_equal(nodes.ids, edges.dst_ids) utils.check_node_ids(nodes, self._node2_ids) utils.check_node_type(nodes, node_type=self._node2_type) utils.check_node_shape(nodes, ids.size * expand_factor) utils.check_node_weights(nodes) utils.check_node_labels(nodes)
def test_1hop_with_neighbor_missing(self): """ Test case for sample 1 hop neighbor with strategy of edge_weight. Some of src_ids have no neighbor. """ expand_factor = 6 ids = self._seed_node1_ids_with_nbr_missing nbr_s = self.g.neighbor_sampler(self._edge1_type, expand_factor=expand_factor, strategy="edge_weight") nbrs = nbr_s.get(ids) edges = nbrs.layer_edges(1) nodes = nbrs.layer_nodes(1) utils.check_fixed_edge_dst_ids(edges, dst_range=self._node2_range, expected_src_ids=ids, default_dst_id=self._default_dst_id) utils.check_edge_type(edges, src_type=self._node1_type, dst_type=self._node2_type, edge_type=self._edge1_type) utils.check_edge_shape(edges, ids.size * expand_factor) utils.check_not_exist_edge_attrs( edges, default_int_attr=self._default_int_attr, default_float_attr=self._default_float_attr, default_string_attr=self._default_string_attr,) utils.check_not_exist_edge_labels(edges) utils.check_equal(nodes.ids, edges.dst_ids) utils.check_node_ids(nodes, [self._default_dst_id]) utils.check_node_type(nodes, node_type=self._node2_type) utils.check_node_shape(nodes, ids.size * expand_factor) utils.check_not_exist_node_weights(nodes) utils.check_not_exist_node_labels(nodes)
def test_1hop_using_gsl(self): """ Using gsl api. """ gl.set_eager_mode(True) expand_factor = 6 ids = self._seed_node1_ids nbrs = self.g.V(self._node1_type, feed=ids) \ .outE(self._edge1_type).sample(expand_factor).by("in_degree") \ .inV().emit() edges = nbrs[1] nodes = nbrs[2] utils.check_fixed_edge_dst_ids(edges, dst_range=self._node2_range, expected_src_ids=ids) utils.check_edge_type(edges, src_type=self._node1_type, dst_type=self._node2_type, edge_type=self._edge1_type) utils.check_edge_shape(edges, ids.size * expand_factor) utils.check_edge_attrs(edges) utils.check_edge_labels(edges) utils.check_equal(nodes.ids, edges.dst_ids) utils.check_node_ids(nodes, self._node2_ids) utils.check_node_type(nodes, node_type=self._node2_type) utils.check_node_shape(nodes, ids.size * expand_factor) utils.check_node_weights(nodes) utils.check_node_labels(nodes)
def test_1hop_using_gsl(self): """ Test case for sample 1 hop neighbor. hetegerous graph with edge attrs, without edge weight. """ gl.set_eager_mode(True) expand_factor = 6 ids = self._seed_node1_ids nbrs = self.g.V(self._node1_type, feed=ids) \ .outE(self._edge1_type).sample(expand_factor).by("edge_weight") \ .inV().emit() edges = nbrs[1] nodes = nbrs[2] utils.check_fixed_edge_dst_ids(edges, dst_range=self._node2_range, expected_src_ids=ids) utils.check_edge_type(edges, src_type=self._node1_type, dst_type=self._node2_type, edge_type=self._edge1_type) utils.check_edge_shape(edges, ids.size * expand_factor) utils.check_edge_attrs(edges) utils.check_edge_labels(edges) utils.check_equal(nodes.ids, edges.dst_ids) utils.check_node_ids(nodes, self._node2_ids) utils.check_node_type(nodes, node_type=self._node2_type) utils.check_node_shape(nodes, ids.size * expand_factor) utils.check_node_weights(nodes) utils.check_node_labels(nodes)
def test_1hop(self): """ Sample one hop of neighbors. """ expand_factor = 6 ids = self._seed_node1_ids nbr_s = self.g.neighbor_sampler(self._edge1_type, expand_factor=expand_factor, strategy="random") nbrs = nbr_s.get(ids) edges = nbrs.layer_edges(1) nodes = nbrs.layer_nodes(1) print(edges.src_nodes.get_out_degrees(self._edge1_type)) utils.check_fixed_edge_dst_ids(edges, dst_range=self._node2_range, expected_src_ids=ids) utils.check_edge_type(edges, src_type=self._node1_type, dst_type=self._node2_type, edge_type=self._edge1_type) utils.check_edge_shape(edges, ids.size * expand_factor) utils.check_edge_attrs(edges) utils.check_edge_labels(edges) utils.check_equal(nodes.ids, edges.dst_ids) utils.check_node_ids(nodes, self._node2_ids) utils.check_node_type(nodes, node_type=self._node2_type) utils.check_node_shape(nodes, ids.size * expand_factor) utils.check_node_weights(nodes) utils.check_node_labels(nodes)
def test_1hop_with_neighbor_missing(self): """ Sample neighbors for nodes which have no out neighbors, and get the default neighbor id. """ expand_factor = 6 ids = self._seed_node1_ids_with_nbr_missing nbr_s = self.g.neighbor_sampler(self._edge1_type, expand_factor=expand_factor, strategy="in_degree") nbrs = nbr_s.get(ids) edges = nbrs.layer_edges(1) nodes = nbrs.layer_nodes(1) utils.check_fixed_edge_dst_ids(edges, dst_range=self._node2_range, expected_src_ids=ids, default_dst_id=self._default_dst_id) utils.check_edge_type(edges, src_type=self._node1_type, dst_type=self._node2_type, edge_type=self._edge1_type) utils.check_edge_shape(edges, ids.size * expand_factor) utils.check_not_exist_edge_attrs( edges, default_int_attr=self._default_int_attr, default_float_attr=self._default_float_attr, default_string_attr=self._default_string_attr,) utils.check_not_exist_edge_labels(edges) utils.check_equal(nodes.ids, edges.dst_ids) utils.check_node_ids(nodes, [self._default_dst_id]) utils.check_node_type(nodes, node_type=self._node2_type) utils.check_node_shape(nodes, ids.size * expand_factor) utils.check_not_exist_node_weights(nodes) utils.check_not_exist_node_labels(nodes)
def test_sampling_with_mask_eager_mode(self): gl.set_eager_mode(True) bs = 8 q = self.g.E(self._edge1_type, mask=gl.Mask.VAL).batch(bs).alias('val') \ .each( lambda e: (e.outV().alias('src'), e.inV().alias('dst') \ .outV(self._edge2_type).sample(3).by('topk').alias('nbr')) ).values(lambda x: (x['src'].ids, x['val'].labels, x['dst'].ids, x['dst'].weights, x['dst'].labels, x['nbr'].ids, x['nbr'].int_attrs)) iteration = 0 for i in range(2): res = [] while True: try: sid, elb, did, dwei, dlb, nid, ni = q.next() utils.check_id_weights(did, dwei) utils.check_equal(dlb, did) iteration += 1 res += list(sid) except gl.OutOfRangeError: break whole = range(self._val_node_range[0], self._val_node_range[1]) expected = [] for elem in whole: expected += [elem] * len( utils.fixed_dst_ids(elem, self._node2_range)) utils.check_sorted_equal(res, expected)
def test_negative_sample(self): q = self.g.V(self._node1_type).batch(2).alias('a') \ .outNeg(self._edge1_type).sample(5).by("random").alias('b') \ .values(lambda x: (x['a'].ids, x['b'].weights)) dataset = gl.Dataset(q) res = dataset.next() utils.check_equal(list(res[0].shape), [2]) utils.check_equal(list(res[1].shape), [2, 5])
def test_2hop(self): """ Sample 2 hops of neighbors. """ gl.set_eager_mode(True) expand_factor = [3, 2] ids = self._seed_node1_ids nbr_s = self.g.neighbor_sampler([self._edge1_type, self._edge2_type], expand_factor=expand_factor, strategy="in_degree") nbrs = nbr_s.get(ids) edges = nbrs.layer_edges(1) nodes = nbrs.layer_nodes(1) utils.check_fixed_edge_dst_ids(edges, dst_range=self._node2_range, expected_src_ids=ids) utils.check_edge_type(edges, src_type=self._node1_type, dst_type=self._node2_type, edge_type=self._edge1_type) utils.check_edge_shape(edges, ids.size * expand_factor[0]) utils.check_edge_attrs(edges) utils.check_edge_labels(edges) utils.check_equal(nodes.ids, edges.dst_ids) utils.check_node_ids(nodes, self._node2_ids) utils.check_node_type(nodes, node_type=self._node2_type) utils.check_node_shape(nodes, ids.size * expand_factor[0]) utils.check_node_weights(nodes) utils.check_node_labels(nodes) ids = nodes.ids.reshape(-1) edges = nbrs.layer_edges(2) nodes = nbrs.layer_nodes(2) utils.check_fixed_edge_dst_ids(edges, dst_range=self._node1_range, expected_src_ids=ids) utils.check_edge_type(edges, src_type=self._node2_type, dst_type=self._node1_type, edge_type=self._edge2_type) utils.check_edge_shape(edges, ids.size * expand_factor[1]) utils.check_edge_attrs(edges) utils.check_edge_weights(edges) utils.check_equal(nodes.ids, edges.dst_ids) utils.check_node_ids(nodes, self._node1_ids) utils.check_node_type(nodes, node_type=self._node1_type) utils.check_node_shape(nodes, ids.size * expand_factor[1])
def test_sample_with_filter(self): q = self.g.E(self._edge1_type).batch(4).alias("a") \ .each(lambda e: (e.inV().alias('dst'), e.outV().alias('src') \ .outV(self._edge1_type).sample(2).by("random").filter('dst').alias("b") ) ) \ .values() dataset = gl.Dataset(q) while True: try: res = dataset.next() utils.check_equal(list(res['b'].shape), [4, 2]) filter_ids = res['dst'].ids remained_ids = res['b'].ids for fid, rid in zip(filter_ids, remained_ids): assert fid not in rid except gl.OutOfRangeError: break
def test_iterate_edge_with_1hop(self): q = self.g.E(self._edge1_type).batch(4).alias("a") \ .outV().alias("b") \ .outV(self._edge1_type).sample(2).by("random").alias("c") \ .values() dataset = gl.Dataset(q) while True: try: res = dataset.next() utils.check_equal(list(res['a'].shape), [4]) utils.check_equal(list(res['b'].shape), [4]) utils.check_equal(list(res['b'].int_attrs.shape), [4, 2]) # [batch_size, int_attr_num] utils.check_equal(list(res['c'].shape), [4, 2]) except gl.OutOfRangeError: break
def test_sample_edge(self): q = self.g.V(self._node1_type).batch(8).alias('a') \ .outE(self._edge1_type).sample(3).by("random").alias('b') \ .inV().alias('c') \ .values() dataset = gl.Dataset(q) res = dataset.next() utils.check_equal(list(res['a'].shape), [8]) utils.check_equal(list(res['b'].shape), [8, 3]) utils.check_equal(list(res['c'].shape), [8, 3])
def test_iterate_node_with_2hop(self): q = self.g.V(self._node1_type).batch(2).alias('a') \ .outV(self._edge1_type).sample(3).by('random').alias('b') \ .outV(self._edge2_type).sample(4).by('random').alias('c') \ .values() dataset = gl.Dataset(q, 10) while True: try: res = dataset.next() utils.check_equal(list(res['a'].shape), [2]) utils.check_equal(list(res['b'].shape), [2, 3]) utils.check_equal(list(res['c'].shape), [2 * 3, 4]) except gl.OutOfRangeError: break