def test_scaler_fit_call(self, mocker, scaler, scaler_type_str): """Test that the call to the scaler.fit method.""" df = d.create_df_3() x = ScalingTransformer(columns=["b", "c"], scaler=scaler, scaler_kwargs={"copy": True}) mocked = mocker.patch(f"sklearn.preprocessing.{scaler_type_str}.fit", return_value=None) x.fit(df) assert mocked.call_count == 1, "unexpected number of calls to scaler fit" call_args = mocked.call_args_list[0] call_pos_args = call_args[0] call_kwargs = call_args[1] expected_positional_args = (df[["b", "c"]], ) h.assert_equal_dispatch( expected=expected_positional_args, actual=call_pos_args, msg=f"unexpected positional args in {scaler_type_str} fit call", ) assert call_kwargs == {}, f"unexpected kwargs in {scaler_type_str} fit call"
def test_output_from_scaler_transform_set_to_columns( self, mocker, scaler, scaler_type_str): """Test that the call to the scaler.transform method.""" df = d.create_df_3() x = ScalingTransformer(columns=["b", "c"], scaler=scaler, scaler_kwargs={"copy": True}) x.fit(df) scaler_transform_output = pd.DataFrame({ "b": [1, 2, 3, 4, 5, 6, 7], "c": [7, 6, 5, 4, 3, 2, 1] }) mocker.patch( f"sklearn.preprocessing.{scaler_type_str}.transform", return_value=scaler_transform_output, ) df_transformed = x.transform(df) h.assert_equal_dispatch( expected=scaler_transform_output, actual=df_transformed[["b", "c"]], msg= f"output from {scaler_type_str} transform not assigned to columns", )
def test_return_self(self): """Test that fit returns self.""" df = d.create_df_2() x = ScalingTransformer(columns=["a"], scaler="standard") x_fitted = x.fit(df) assert ( x_fitted is x ), "return value from ScalingTransformer.fit not as expected (self)."
def test_X_returned(self): """Test that the input X is returned from the method.""" df = d.create_df_2() x = ScalingTransformer(columns=["a"], scaler="standard") df_returned = x.check_numeric_columns(df) h.assert_equal_dispatch( expected=df, actual=df_returned, msg="unexepcted object returned from check_numeric_columns", )
def test_exception_raised(self): """Test an exception is raised if non numeric columns are passed in X.""" df = d.create_df_2() x = ScalingTransformer(columns=["a", "b", "c"], scaler="standard") with pytest.raises( TypeError, match= r"""The following columns are not numeric in X; \['b', 'c'\]""", ): x.check_numeric_columns(df)
def test_return_type(self, scaler, scaler_type_str, columns): """Test that transform returns a pd.DataFrame.""" df = d.create_df_3() x = ScalingTransformer(columns=columns, scaler=scaler, scaler_kwargs={"copy": True}) x.fit(df) df_transformed = x.transform(df) assert (type(df_transformed) is pd.DataFrame), "unexpected output type from transform"
def test_scaler_attribute_type(self, scaler, scaler_type): """Test that the scaler attribute is set to the correct type given what is passed when initialising the transformer.""" x = ScalingTransformer(columns="b", scaler=scaler) assert (type(x.scaler) is scaler_type), f"unexpected scaler set in init for {scaler}"
def test_check_numeric_columns_call(self, mocker): """Test the call to ScalingTransformer.check_numeric_columns.""" df = d.create_df_2() x = ScalingTransformer(columns=["a"], scaler="standard") expected_call_args = {0: {"args": (d.create_df_2(), ), "kwargs": {}}} with h.assert_function_call( mocker, tubular.numeric.ScalingTransformer, "check_numeric_columns", expected_call_args, return_value=d.create_df_2(), ): x.fit(df)
def test_super_fit_call(self, mocker): """Test the call to BaseTransformer.fit.""" df = d.create_df_2() x = ScalingTransformer(columns=["a"], scaler="standard") expected_call_args = { 0: { "args": (d.create_df_2(), None), "kwargs": {} } } with h.assert_function_call(mocker, tubular.base.BaseTransformer, "fit", expected_call_args): x.fit(df)
def test_to_scaler_kwargs_type_error(self): """Test that an exception is raised if scaler_kwargs is not a dict.""" with pytest.raises( TypeError, match= r"""scaler_kwargs should be a dict but got type \<class 'int'\>""", ): ScalingTransformer(columns="b", scaler="standard", scaler_kwargs=1)
def test_to_scaler_non_allowed_value_error(self): """Test that an exception is raised if scaler is not one of the allowed values.""" with pytest.raises( ValueError, match= r"""scaler should be one of; \['min_max', 'max_abs', 'standard'\]""", ): ScalingTransformer(columns="b", scaler="zzz", scaler_kwargs={"a": 1})
def test_scaler_kwargs_key_type_error(self): """Test that an exception is raised if scaler_kwargs has keys which are not str.""" with pytest.raises( TypeError, match= r"""unexpected type \(\<class 'int'\>\) for scaler_kwargs key in position 1, must be str""", ): ScalingTransformer( columns="b", scaler="standard", scaler_kwargs={ "a": 1, 2: "b" }, )
def test_super_init_called(self, mocker): """Test that super.__init__ called.""" expected_call_args = { 0: { "args": (), "kwargs": { "columns": ["a", "b"], "copy": True, "verbose": False }, } } with h.assert_function_call(mocker, tubular.base.BaseTransformer, "__init__", expected_call_args): ScalingTransformer(columns=["a", "b"], scaler="standard", copy=True, verbose=False)
def test_scaler_initialised_with_scaler_kwargs(self, mocker, scaler, scaler_type_str, scaler_kwargs_value): """Test that the scaler is initialised with the scaler_kwargs arguments.""" mocked = mocker.patch( f"sklearn.preprocessing.{scaler_type_str}.__init__", return_value=None) ScalingTransformer(columns="b", scaler=scaler, scaler_kwargs=scaler_kwargs_value) assert mocked.call_count == 1, "unexpected number of calls to init" call_args = mocked.call_args_list[0] call_pos_args = call_args[0] call_kwargs = call_args[1] assert (call_pos_args == () ), f"unexpected positional args in {scaler_type_str} init call" assert (call_kwargs == scaler_kwargs_value ), f"unexpected kwargs in {scaler_type_str} init call"
def test_super_transform_called(self, mocker): """Test that BaseTransformer.transform called.""" df = d.create_df_2() x = ScalingTransformer(columns=["a"], scaler="standard") x.fit(df) expected_call_args = {0: {"args": (d.create_df_2(), ), "kwargs": {}}} with h.assert_function_call( mocker, tubular.base.BaseTransformer, "transform", expected_call_args, return_value=d.create_df_2(), ): x.transform(df)
def test_inheritance(self): """Test that ScalingTransformer inherits from BaseTransformer.""" x = ScalingTransformer(columns=["a"], scaler="standard") h.assert_inheritance(x, tubular.base.BaseTransformer)