From 60f4d059f3fab0714669a92c8e8a1ff435d59200 Mon Sep 17 00:00:00 2001 From: rwedge Date: Fri, 15 Sep 2023 15:21:54 -0400 Subject: [PATCH] remove psutil use in tests --- setup.py | 1 - .../transformers/test_categorical.py | 44 ++--------------- tests/unit/transformers/test_categorical.py | 49 +++++-------------- 3 files changed, 16 insertions(+), 78 deletions(-) diff --git a/setup.py b/setup.py index fec4275e2..d94dc7e3f 100644 --- a/setup.py +++ b/setup.py @@ -41,7 +41,6 @@ 'jupyter>=1.0.0,<2', 'rundoc>=0.4.3,<0.5', 'pytest-subtests>=0.5,<1.0', - 'psutil>=5.7,<6', ] addons_require = [] diff --git a/tests/integration/transformers/test_categorical.py b/tests/integration/transformers/test_categorical.py index 35cefba65..a1499534a 100644 --- a/tests/integration/transformers/test_categorical.py +++ b/tests/integration/transformers/test_categorical.py @@ -1,6 +1,5 @@ import pickle from io import BytesIO -from unittest.mock import Mock, patch import numpy as np import pandas as pd @@ -360,43 +359,12 @@ def test_frequency_encoder_mixed(): pd.testing.assert_frame_equal(data, reverse) -@patch('psutil.virtual_memory') -def test_frequency_encoder_mixed_low_virtual_memory(psutil_mock): - """Test the FrequencyEncoder on mixed type data with low virtual memory. - - Ensure that the FrequencyEncoder can fit, transform, and reverse - transform on mixed type data, when there is low virtual memory. Expect that the - reverse transformed data is the same as the input. - - Input: - - 4 rows of mixed data - Output: - - The reverse transformed data - """ - # setup - data = pd.DataFrame([True, 'a', 1, None], columns=['column_name']) - column = 'column_name' - transformer = FrequencyEncoder() - - virtual_memory = Mock() - virtual_memory.available = 1 - psutil_mock.return_value = virtual_memory - - # run - transformer.fit(data, column) - reverse = transformer.reverse_transform(transformer.transform(data)) - - # assert - pd.testing.assert_frame_equal(data, reverse) - - -@patch('psutil.virtual_memory') -def test_frequency_encoder_mixed_more_rows(psutil_mock): - """Test the FrequencyEncoder on mixed type data with low virtual memory. +def test_frequency_encoder_mixed_more_rows(): + """Test the FrequencyEncoder on mixed type data. Ensure that the FrequencyEncoder can fit, transform, and reverse - transform on mixed type data, when there is low virtual memory and a larger - number of rows. Expect that the reverse transformed data is the same as the input. + transform on mixed type data, when there is a larger number of rows. + Expect that the reverse transformed data is the same as the input. Input: - 4 rows of mixed data @@ -409,10 +377,6 @@ def test_frequency_encoder_mixed_more_rows(psutil_mock): transform_data = pd.DataFrame(['a', 1, None, 'a', True, 1], columns=['column_name']) transformer = FrequencyEncoder() - virtual_memory = Mock() - virtual_memory.available = 1 - psutil_mock.return_value = virtual_memory - # run transformer.fit(data, column) transformed = transformer.transform(transform_data) diff --git a/tests/unit/transformers/test_categorical.py b/tests/unit/transformers/test_categorical.py index ffdcabf9c..6021801f4 100644 --- a/tests/unit/transformers/test_categorical.py +++ b/tests/unit/transformers/test_categorical.py @@ -954,16 +954,14 @@ def test__transform_by_row(self): expected = np.array([0.875, 0.625, 0.375, 0.125]) assert (transformed == expected).all() - @patch('psutil.virtual_memory') - def test__reverse_transform_by_category_called(self, psutil_mock): + def test__reverse_transform_by_category_called(self): """Test that the `_reverse_transform_by_category` method is called. - When there is not enough virtual memory and the number of rows is greater than the - number of categories, expect that the `_reverse_transform_by_category` method is called. + When the number of rows is greater than the number of categories, expect + that the `_reverse_transform_by_category` method is called. Setup: - The categorical transformer is instantiated with 4 categories. Also patch the - `psutil.virtual_memory` function to return an `available_memory` of 1. + The categorical transformer is instantiated with 4 categories. Input: - numerical data with 5 rows Output: @@ -977,10 +975,6 @@ def test__reverse_transform_by_category_called(self, psutil_mock): categorical_transformer_mock = Mock() categorical_transformer_mock.means = pd.Series([0.125, 0.375, 0.625, 0.875]) - virtual_memory = Mock() - virtual_memory.available = 1 - psutil_mock.return_value = virtual_memory - # Run reverse = FrequencyEncoder._reverse_transform( categorical_transformer_mock, transform_data) @@ -990,16 +984,14 @@ def test__reverse_transform_by_category_called(self, psutil_mock): np.testing.assert_array_equal(reverse_arg, transform_data.clip(0, 1)) assert reverse == categorical_transformer_mock._reverse_transform_by_category.return_value - @patch('psutil.virtual_memory') - def test__reverse_transform_by_category(self, psutil_mock): + def test__reverse_transform_by_category(self): """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. + and intervals are set for those categories. Input: - transformed data with 5 rows Ouptut: @@ -1018,10 +1010,6 @@ def test__reverse_transform_by_category(self, psutil_mock): } 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) @@ -1052,17 +1040,14 @@ def test__get_category_from_start(self): # Assert assert category == 'c' - @patch('psutil.virtual_memory') - def test__reverse_transform_by_row_called(self, psutil_mock): + def test__reverse_transform_by_row_called(self): """Test that the `_reverse_transform_by_row` method is called. - When there is not enough virtual memory and the number of rows is less than or equal - to the number of categories, expect that the `_reverse_transform_by_row` method - is called. + When the number of rows is less than or equal to the number of categories, + expect that the `_reverse_transform_by_row` method is called. Setup: - The categorical transformer is instantiated with 4 categories. Also patch the - `psutil.virtual_memory` function to return an `available_memory` of 1. + The categorical transformer is instantiated with 4 categories. Input: - numerical data with 4 rows Output: @@ -1079,10 +1064,6 @@ def test__reverse_transform_by_row_called(self, psutil_mock): [0., 0.25, 0.5, 0.75], index=[4, 3, 2, 1], columns=['category']) categorical_transformer_mock._normalize.return_value = data - virtual_memory = Mock() - virtual_memory.available = 1 - psutil_mock.return_value = virtual_memory - # Run reverse = FrequencyEncoder._reverse_transform(categorical_transformer_mock, data) @@ -1091,16 +1072,14 @@ def test__reverse_transform_by_row_called(self, psutil_mock): np.testing.assert_array_equal(reverse_arg, data.clip(0, 1)) assert reverse == categorical_transformer_mock._reverse_transform_by_row.return_value - @patch('psutil.virtual_memory') - def test__reverse_transform_by_row(self, psutil_mock): + def test__reverse_transform_by_row(self): """Test the _reverse_transform_by_row 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, starts, - and intervals are set for those categories. Also patch the `psutil.virtual_memory` - function to return an `available_memory` of 1. + and intervals are set for those categories. Input: - transformed data with 4 rows Ouptut: @@ -1122,10 +1101,6 @@ def test__reverse_transform_by_row(self, psutil_mock): } transformer.dtype = data.dtype - virtual_memory = Mock() - virtual_memory.available = 1 - psutil_mock.return_value = virtual_memory - # Run reverse = transformer._reverse_transform(transformed)