Skip to content

Commit aa9409a

Browse files
committed
[test] Increase the coverage
1 parent d82f9c0 commit aa9409a

File tree

6 files changed

+50
-7
lines changed

6 files changed

+50
-7
lines changed

autoPyTorch/pipeline/components/preprocessing/tabular_preprocessing/coalescer/MinorityCoalescer.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ def __init__(self, min_frac: float, random_state: np.random.RandomState):
1818
self.random_state = random_state
1919

2020
def fit(self, X: Dict[str, Any], y: Any = None) -> BaseCoalescer:
21-
2221
self.check_requirements(X, y)
23-
2422
self.preprocessor['categorical'] = MinorityCoalesceTransformer(min_frac=self.min_frac)
2523
return self
2624

autoPyTorch/pipeline/components/preprocessing/tabular_preprocessing/coalescer/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ def get_hyperparameter_search_space(self,
155155

156156
def _check_dataset_properties(self, dataset_properties: Dict[str, BaseDatasetPropertiesType]) -> None:
157157
"""
158-
A mechanism in code to ensure the correctness of the fit dictionary
158+
A mechanism in code to ensure the correctness of the dataset_properties
159159
It recursively makes sure that the children and parent level requirements
160-
are honored before fit.
160+
are honored.
161161
162162
Args:
163163
dataset_properties:

autoPyTorch/pipeline/components/preprocessing/tabular_preprocessing/coalescer/base_coalescer.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ def transform(self, X: Dict[str, Any]) -> Dict[str, Any]:
2525
Returns:
2626
X (Dict[str, Any]): the updated fit dictionary
2727
"""
28-
if self._processing and all(self.preprocessor[key] is None for key in ['numerical', 'categorical']):
29-
raise ValueError(f"fit() must be called before transform() on {self.__class__.__name__}")
28+
if self._processing and self.preprocessor['categorical'] is None:
29+
# If we apply minority coalescer, we must have categorical preprocessor!
30+
raise RuntimeError(f"fit() must be called before transform() on {self.__class__.__name__}")
3031

3132
X.update({'coalescer': self.preprocessor})
3233
return X

test/test_pipeline/components/preprocessing/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ def _get_pipeline_steps(self, dataset_properties: Optional[Dict[str, Any]],
3030
default_dataset_properties.update(dataset_properties)
3131

3232
steps.extend([
33-
("coalescer", CoalescerChoice())
3433
("imputer", SimpleImputer()),
3534
("variance_threshold", VarianceThreshold()),
35+
("coalescer", CoalescerChoice(default_dataset_properties)),
3636
("encoder", EncoderChoice(default_dataset_properties)),
3737
("scaler", ScalerChoice(default_dataset_properties)),
3838
("tabular_transformer", TabularColumnTransformer()),

test/test_pipeline/components/preprocessing/test_coalescer.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,45 @@
11
import copy
22
import unittest
33

4+
import numpy as np
5+
6+
import pytest
7+
48
from autoPyTorch.pipeline.components.preprocessing.tabular_preprocessing.coalescer import (
59
CoalescerChoice
610
)
11+
from autoPyTorch.pipeline.components.preprocessing.tabular_preprocessing.coalescer.MinorityCoalescer import (
12+
MinorityCoalescer
13+
)
14+
15+
16+
def test_transform_before_fit():
17+
with pytest.raises(RuntimeError):
18+
mc = MinorityCoalescer(min_frac=None, random_state=np.random.RandomState())
19+
mc.transform(np.random.random((4, 4)))
720

821

922
class TestCoalescerChoice(unittest.TestCase):
23+
def test_raise_error_in_check_update_compatiblity(self):
24+
dataset_properties = {'numerical_columns': [], 'categorical_columns': []}
25+
cc = CoalescerChoice(dataset_properties)
26+
choices = ["NoCoescer"] # component name with typo
27+
with pytest.raises(ValueError):
28+
# raise error because no categorical columns, but choices do not have no coalescer
29+
cc._check_update_compatiblity(choices_in_update=choices, dataset_properties=dataset_properties)
30+
31+
def test_raise_error_in_get_component_without_updates(self):
32+
dataset_properties = {'numerical_columns': [], 'categorical_columns': []}
33+
cc = CoalescerChoice(dataset_properties)
34+
with pytest.raises(ValueError):
35+
# raise error because no categorical columns, but choices do not have no coalescer
36+
cc._get_component_without_updates(
37+
avail_components={},
38+
dataset_properties=dataset_properties,
39+
default="",
40+
include=[]
41+
)
42+
1043
def test_get_set_config_space(self):
1144
"""Make sure that we can setup a valid choice in the Coalescer
1245
choice"""

test/test_utils/test_coalescer_transformer.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ def test_invalid_X(X1):
7575
MinorityCoalesceTransformer().fit_transform(X)
7676

7777

78+
@pytest.mark.parametrize("min_frac", [-0.1, 1.1])
79+
def test_invalid_min_frac(min_frac):
80+
with pytest.raises(ValueError):
81+
MinorityCoalesceTransformer(min_frac=min_frac)
82+
83+
84+
def test_transform_before_fit(X1):
85+
with pytest.raises(RuntimeError):
86+
MinorityCoalesceTransformer().transform(X1)
87+
88+
7889
def test_transform_after_fit(X1, X2):
7990
# On both X_fit and X_transf, the categories 3, 4, 5, 6, 7 are present.
8091
X_fit = X1 # Here categories 3, 4, 5 have ocurrence above 10%

0 commit comments

Comments
 (0)