def test__reverse_transform_by_category(self, psutil_mock): """Test the _reverse_transform_by_category method with numerical data. Expect that the transformed data is correctly reverse transformed. Setup: The categorical transformer is instantiated with 4 categories, and the means and intervals are set for those categories. Also patch the `psutil.virtual_memory` function to return an `available_memory` of 1. Input: - transformed data with 5 rows Ouptut: - the original data """ data = pd.Series([1, 3, 3, 2, 1]) transformed = pd.Series([0.875, 0.375, 0.375, 0.625, 0.875]) transformer = CategoricalTransformer() transformer.means = pd.Series([0.125, 0.375, 0.625, 0.875], index=[4, 3, 2, 1]) transformer.intervals = { 4: (0, 0.25, 0.125, 0.041666666666666664), 3: (0.25, 0.5, 0.375, 0.041666666666666664), 2: (0.5, 0.75, 0.625, 0.041666666666666664), 1: (0.75, 1.0, 0.875, 0.041666666666666664), } transformer.dtype = data.dtype virtual_memory = Mock() virtual_memory.available = 1 psutil_mock.return_value = virtual_memory reverse = transformer._reverse_transform_by_category(transformed) pd.testing.assert_series_equal(data, reverse)
def test__reverse_transfrom_by_matrix(self, psutil_mock): """Test the _reverse_transform_by_matrix method with numerical data Expect that the transformed data is correctly reverse transformed. Setup: The categorical transformer is instantiated with 4 categories and means. Also patch the `psutil.virtual_memory` function to return a large enough `available_memory`. Input: - transformed data with 4 rows Ouptut: - the original data """ # Setup data = pd.Series([1, 2, 3, 4]) transformed = pd.Series([0.875, 0.625, 0.375, 0.125]) transformer = CategoricalTransformer() transformer.means = pd.Series([0.125, 0.375, 0.625, 0.875], index=[4, 3, 2, 1]) transformer.dtype = data.dtype virtual_memory = Mock() virtual_memory.available = 4 * 4 * 8 * 3 + 1 psutil_mock.return_value = virtual_memory # Run reverse = transformer._reverse_transform_by_matrix(transformed) # Assert pd.testing.assert_series_equal(data, reverse)