• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2021 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""
16Eager Tests for Transform Tensor ops
17"""
18
19import numpy as np
20import mindspore.common.dtype as mstype
21import mindspore.dataset.transforms.c_transforms as data_trans
22
23
24def test_eager_concatenate():
25    """
26    Test Concatenate op is callable
27    """
28    prepend_tensor = np.array([1.4, 2., 3., 4., 4.5], dtype=np.float)
29    append_tensor = np.array([9., 10.3, 11., 12.], dtype=np.float)
30    concatenate_op = data_trans.Concatenate(0, prepend_tensor, append_tensor)
31    expected = np.array([1.4, 2., 3., 4., 4.5, 5., 6., 7., 8., 9., 10.3,
32                         11., 12.])
33    assert np.array_equal(concatenate_op([5., 6., 7., 8.]), expected)
34
35
36def test_eager_fill():
37    """
38    Test Fill op is callable
39    """
40    fill_op = data_trans.Fill(3)
41    expected = np.array([3, 3, 3, 3])
42    assert np.array_equal(fill_op([4, 5, 6, 7]), expected)
43
44
45def test_eager_mask():
46    """
47    Test Mask op is callable
48    """
49    mask_op = data_trans.Mask(data_trans.Relational.EQ, 3, mstype.bool_)
50    expected = np.array([False, False, True, False, False])
51    assert np.array_equal(mask_op([1, 2, 3, 4, 5]), expected)
52
53
54def test_eager_pad_end():
55    """
56    Test PadEnd op is callable
57    """
58    pad_end_op = data_trans.PadEnd([3], -1)
59    expected = np.array([1, 2, -1])
60    assert np.array_equal(pad_end_op([1, 2]), expected)
61
62
63def test_eager_slice():
64    """
65    Test Slice op is callable
66    """
67    indexing = [[0], [0, 3]]
68    slice_op = data_trans.Slice(*indexing)
69    expected = np.array([[1, 4]])
70    assert np.array_equal(slice_op([[1, 2, 3, 4, 5]]), expected)
71
72
73if __name__ == "__main__":
74    test_eager_concatenate()
75    test_eager_fill()
76    test_eager_mask()
77    test_eager_pad_end()
78    test_eager_slice()
79