Skip to content

Commit

Permalink
remove psutil use in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rwedge committed Sep 15, 2023
1 parent 26e0c68 commit 60f4d05
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 78 deletions.
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
44 changes: 4 additions & 40 deletions tests/integration/transformers/test_categorical.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pickle
from io import BytesIO
from unittest.mock import Mock, patch

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
49 changes: 12 additions & 37 deletions tests/unit/transformers/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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)

Expand All @@ -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:
Expand All @@ -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)

Expand Down

0 comments on commit 60f4d05

Please sign in to comment.