コード例 #1
0
    def test_empty_list_input(self):
        arrs = []
        dtypes = np.int8

        observed = iadt.restore_dtypes_(arrs, dtypes, clip=False, round=False)

        assert len(observed) == 0
コード例 #2
0
    def test_invalid_input(self):
        arr = False

        with self.assertRaises(Exception) as context:
            _ = iadt.restore_dtypes_(arr, np.int8)

        assert "Expected numpy array or " in str(context.exception)
コード例 #3
0
    def test_dtype_not_changed(self):
        arr = np.array([[-128, -1, 0, 1, 127]], dtype=np.int8)
        dtype = np.int8

        observed = iadt.restore_dtypes_(arr, dtype, clip=False, round=False)

        assert observed is arr
コード例 #4
0
    def test_dont_round_non_float_dtypes(self, mock_round):
        arr = np.array([[-128, -1, 0, 1, 127]], dtype=np.int8)
        dtype = np.float32

        _ = iadt.restore_dtypes_(np.copy(arr), dtype, clip=False)

        assert mock_round.call_count == 0
コード例 #5
0
    def test_no_round_if_dtype_not_changed(self, mock_round):
        arr = np.array([[-128, -1, 0, 1, 127]], dtype=np.int8)
        dtype = np.int8

        observed = iadt.restore_dtypes_(arr, dtype, clip=False)

        assert observed is arr
        assert mock_round.call_count == 0
コード例 #6
0
    def test_round_float_dtypes(self):
        arr = np.array([[-128, -1.1, 0.7, 1.1, 127]], dtype=np.float32)
        dtype = np.int8

        observed = iadt.restore_dtypes_(np.copy(arr), dtype, clip=False)

        expected = np.array([[-128, -1, 1, 1, 127]], dtype=np.int8)
        assert observed.dtype.name == "int8"
        assert np.array_equal(observed, expected)
コード例 #7
0
    def test_empty_array_input(self):
        arr = np.zeros((0, 5), dtype=np.float32)
        dtypes = np.int8

        observed = iadt.restore_dtypes_(np.copy(arr), dtypes,
                                        clip=False, round=False)

        assert observed.dtype.name == "int8"
        assert observed.shape == (0, 5)
コード例 #8
0
    def test_clip_and_round(self):
        arr = np.array([[0.0, 0.1, 0.9, 127.0 + 1.0, -128.0 - 1.0]],
                       dtype=np.float32)
        dtype = np.int8

        observed = iadt.restore_dtypes_(np.copy(arr), dtype)

        expected = np.array([[0, 0, 1, 127, -128]], dtype=np.int8)
        assert observed.dtype.name == "int8"
        assert np.array_equal(observed, expected)
コード例 #9
0
    def test_int_to_uint_with_clip(self):
        arr = np.array([[-100, -1, 0, 1, 100]], dtype=np.int8)
        dtype = np.uint8

        observed = iadt.restore_dtypes_(np.copy(arr), dtype,
                                        clip=True, round=False)

        expected = np.array([[0, 0, 0, 1, 100]], dtype=np.uint8)
        assert observed.dtype.name == "uint8"
        assert np.allclose(observed, expected)
コード例 #10
0
    def test_increase_float_resolution(self):
        arr = np.array([[-100.0, -1.0, 0.0, 1.0, 100.0]], dtype=np.float32)
        dtype = np.float64

        observed = iadt.restore_dtypes_(np.copy(arr), dtype,
                                        clip=False, round=False)

        expected = np.array([[-100.0, -1.0, 0.0, 1.0, 100.0]],
                            dtype=np.float32)
        assert observed.dtype.name == "float64"
        assert np.allclose(observed, expected)
コード例 #11
0
    def test_int_to_float(self):
        arr = np.array([[-100, -1, 0, 1, 100]], dtype=np.int8)
        dtype = np.float32

        observed = iadt.restore_dtypes_(np.copy(arr), dtype,
                                        clip=False, round=False)

        expected = np.array([[-100.0, -1.0, 0.0, 1.0, 100.0]],
                            dtype=np.float32)
        assert observed.dtype.name == "float32"
        assert np.allclose(observed, expected)
コード例 #12
0
    def test_array_input_single_dtype_no_clip_with_round(self):
        arr = np.array([[0.0, 0.1, 0.9, 127.0+1.0, -128.0-1.0]],
                       dtype=np.float32)
        dtype = np.int8

        observed = iadt.restore_dtypes_(np.copy(arr), dtype,
                                        clip=False, round=True)

        expected = np.array([[0, 0, 1, -128+1-1, 127-1+1]], dtype=np.int8)
        assert observed.dtype.name == "int8"
        assert np.array_equal(observed, expected)
コード例 #13
0
    def test_array_input_fail_if_many_different_dtypes(self):
        arr = np.array([
            [0.0, 0.1, 0.9, 127.0+1.0, -128.0-1.0],
            [0.0, 0.1, 0.9, 127.0+1.0, -128.0-1.0],
        ], dtype=np.float32)
        dtypes = [np.int8, np.int16]

        with self.assertRaises(AssertionError) as context:
            _observed = iadt.restore_dtypes_(np.copy(arr), dtypes,
                                             clip=False, round=False)

        assert (
            "or an iterable of N times the *same* dtype"
            in str(context.exception)
        )
コード例 #14
0
    def test_array_input_many_dtypes_no_clip_no_round(self):
        arr = np.array([
            [0.0, 0.1, 0.9, 127.0+0.0, -128.0-0.0],
            [0.0, 0.1, 0.9, 127.0+1.0, -128.0-1.0],
        ], dtype=np.float32)
        dtypes = [np.int8, np.int8]

        observed = iadt.restore_dtypes_(np.copy(arr), dtypes,
                                        clip=False, round=False)

        expected = np.array([
            [0, 0, 0, 127, -128],
            [0, 0, 0, -128+1-1, 127-1+1]
        ], dtype=np.int8)
        assert observed.dtype.name == "int8"
        assert np.array_equal(observed, expected)
コード例 #15
0
    def test_many_items_list_input_many_dtypes(self):
        arrs = [
            np.array([0.0, 0.1, 0.9, 127.0+1.0, -128.0-1.0], dtype=np.float32),
            np.array([0.0, 0.1, 0.9, 127.0+1.0, -128.0-1.0], dtype=np.float32)
        ]
        dtypes = [np.int8, np.int16]

        observed = iadt.restore_dtypes_(
            [np.copy(arr) for arr in arrs],
            dtypes,
            clip=False,
            round=False)

        expected = [
            np.array([0, 0, 0, -128+1-1, 127-1+1], dtype=np.int8),
            np.array([0, 0, 0, 127+1, -128-1], dtype=np.int16)
        ]
        assert len(observed) == 2
        assert observed[0].dtype.name == "int8"
        assert observed[1].dtype.name == "int16"
        assert np.array_equal(observed[0], expected[0])
        assert np.array_equal(observed[1], expected[1])