test_mask_op.py 5.1 KB
Newer Older
H
Mask Op  
hesham 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""
Testing Mask op in DE
"""
import numpy as np
import pytest

import mindspore.common.dtype as mstype
import mindspore.dataset as ds
import mindspore.dataset.transforms.c_transforms as ops

mstype_to_np_type = {
    mstype.bool_: np.bool,
    mstype.int8: np.int8,
    mstype.uint8: np.uint8,
    mstype.int16: np.int16,
    mstype.uint16: np.uint16,
    mstype.int32: np.int32,
    mstype.uint32: np.uint32,
    mstype.int64: np.int64,
    mstype.uint64: np.uint64,
    mstype.float16: np.float16,
    mstype.float32: np.float32,
    mstype.float64: np.float64,
    mstype.string: np.str
}


def mask_compare(array, op, constant, dtype=mstype.bool_):
    data = ds.NumpySlicesDataset([array])
    array = np.array(array)
    data = data.map(operations=ops.Mask(op, constant, dtype))
    for d in data:
        if op == ops.Relational.EQ:
            array = array == np.array(constant, dtype=array.dtype)
        elif op == ops.Relational.NE:
            array = array != np.array(constant, dtype=array.dtype)
        elif op == ops.Relational.GT:
            array = array > np.array(constant, dtype=array.dtype)
        elif op == ops.Relational.GE:
            array = array >= np.array(constant, dtype=array.dtype)
        elif op == ops.Relational.LT:
            array = array < np.array(constant, dtype=array.dtype)
        elif op == ops.Relational.LE:
            array = array <= np.array(constant, dtype=array.dtype)

        array = array.astype(dtype=mstype_to_np_type[dtype])

        np.testing.assert_array_equal(array, d[0])


def test_int_comparison():
    for k in mstype_to_np_type:
        if k == mstype.string:
            continue
        mask_compare([1, 2, 3, 4, 5], ops.Relational.EQ, 3, k)
        mask_compare([1, 2, 3, 4, 5], ops.Relational.NE, 3, k)
        mask_compare([1, 2, 3, 4, 5], ops.Relational.LT, 3, k)
        mask_compare([1, 2, 3, 4, 5], ops.Relational.LE, 3, k)
        mask_compare([1, 2, 3, 4, 5], ops.Relational.GT, 3, k)
        mask_compare([1, 2, 3, 4, 5], ops.Relational.GE, 3, k)


def test_float_comparison():
    for k in mstype_to_np_type:
        if k == mstype.string:
            continue
        mask_compare([1.5, 2.5, 3., 4.5, 5.5], ops.Relational.EQ, 3, k)
        mask_compare([1.5, 2.5, 3., 4.5, 5.5], ops.Relational.NE, 3, k)
        mask_compare([1.5, 2.5, 3., 4.5, 5.5], ops.Relational.LT, 3, k)
        mask_compare([1.5, 2.5, 3., 4.5, 5.5], ops.Relational.LE, 3, k)
        mask_compare([1.5, 2.5, 3., 4.5, 5.5], ops.Relational.GT, 3, k)
        mask_compare([1.5, 2.5, 3., 4.5, 5.5], ops.Relational.GE, 3, k)


def test_float_comparison2():
    for k in mstype_to_np_type:
        if k == mstype.string:
            continue
        mask_compare([1, 2, 3, 4, 5], ops.Relational.EQ, 3.5, k)
        mask_compare([1, 2, 3, 4, 5], ops.Relational.NE, 3.5, k)
        mask_compare([1, 2, 3, 4, 5], ops.Relational.LT, 3.5, k)
        mask_compare([1, 2, 3, 4, 5], ops.Relational.LE, 3.5, k)
        mask_compare([1, 2, 3, 4, 5], ops.Relational.GT, 3.5, k)
        mask_compare([1, 2, 3, 4, 5], ops.Relational.GE, 3.5, k)


def test_string_comparison():
    for k in mstype_to_np_type:
        if k == mstype.string:
            continue
        mask_compare(["1.5", "2.5", "3.", "4.5", "5.5"], ops.Relational.EQ, "3.", k)
        mask_compare(["1.5", "2.5", "3.", "4.5", "5.5"], ops.Relational.NE, "3.", k)
        mask_compare(["1.5", "2.5", "3.", "4.5", "5.5"], ops.Relational.LT, "3.", k)
        mask_compare(["1.5", "2.5", "3.", "4.5", "5.5"], ops.Relational.LE, "3.", k)
        mask_compare(["1.5", "2.5", "3.", "4.5", "5.5"], ops.Relational.GT, "3.", k)
        mask_compare(["1.5", "2.5", "3.", "4.5", "5.5"], ops.Relational.GE, "3.", k)


def test_mask_exceptions_str():
    with pytest.raises(RuntimeError) as info:
        mask_compare([1, 2, 3, 4, 5], ops.Relational.EQ, "3.5")
    assert "Cannot convert constant value to the type of the input tensor." in str(info.value)

    with pytest.raises(RuntimeError) as info:
        mask_compare(["1", "2", "3", "4", "5"], ops.Relational.EQ, 3.5)
    assert "Cannot convert constant value to the type of the input tensor." in str(info.value)

    with pytest.raises(RuntimeError) as info:
        mask_compare(["1", "2", "3", "4", "5"], ops.Relational.EQ, "3.5", mstype.string)
    assert "Cannot generate a string mask. Type should be numeric." in str(info.value)


if __name__ == "__main__":
    test_int_comparison()
    test_float_comparison()
    test_float_comparison2()
    test_string_comparison()
    test_mask_exceptions_str()