def test_reverse_transform_min_an_max_with_nulls(self): """Test reverse_transform with nulls and ``min_value`` and ``max_value`` set The ``reverse_transform`` method should clip any values above the ``max_value`` and any values below the ``min_value``. Null values should be replaced with ``np.nan``. Input: - 2d array where second column has some values over 0.5 representing null values Output: - Array with out of bounds values clipped and null values injected """ # Setup data = np.array([[-np.inf, 0], [-5000, 0.1], [-301, 0.8], [-250, 0.4], [0, 0], [125, 1], [401, 0.2], [np.inf, 0.5]]) clipped_data = np.array([[-300, 0], [-300, 0.1], [-300, 0.8], [-250, 0.4], [0, 0], [125, 1], [400, 0.2], [400, 0.5]]) expected_data = np.array( [-300, -300, np.nan, -250, 0, np.nan, 400, 400]) # Run transformer = NumericalTransformer(dtype=np.float, nan='nan') transformer._max_value = 400 transformer._min_value = -300 transformer.null_transformer = Mock() transformer.null_transformer.reverse_transform.return_value = expected_data result = transformer.reverse_transform(data) # Asserts null_transformer_calls = transformer.null_transformer.reverse_transform.mock_calls np.testing.assert_array_equal(null_transformer_calls[0][1][0], clipped_data) np.testing.assert_array_equal(result, expected_data)
def test_reverse_transform_min_no_max(self): """Test reverse_transform with ``min_value`` set The ``reverse_transform`` method should clip any values below the ``min_value`` if it is set. Input: - Array with values below the min and infinitely high values Output: - Array with low values clipped to min """ # Setup data = np.array([-np.inf, -5000, -301, -250, 0, 125, 400, np.inf]) # Run transformer = NumericalTransformer(dtype=np.float, nan=None) transformer._min_value = -300 result = transformer.reverse_transform(data) # Asserts expected_data = np.array([-300, -300, -300, -250, 0, 125, 400, np.inf]) np.testing.assert_array_equal(result, expected_data)
def test_reverse_transform_min_and_max(self): """Test reverse_transform with ``min_value`` and ``max_value`` set The ``reverse_transform`` method should clip any values above the ``max_value`` and any values below the ``min_value``. Input: - Array with values above the max and below the min Output: - Array with out of bound values clipped to min and max """ # Setup data = np.array([-np.inf, -5000, -301, -250, 0, 125, 401, np.inf]) # Run transformer = NumericalTransformer(dtype=np.float, nan=None) transformer._max_value = 400 transformer._min_value = -300 result = transformer.reverse_transform(data) # Asserts np.testing.assert_array_equal( result, np.array([-300, -300, -300, -250, 0, 125, 400, 400]))