コード例 #1
0
    def test_attributes_unchanged_from_transform(self):
        """Test that attributes are unchanged after transform is run."""

        df = d.create_df_9()

        x = CappingTransformer(quantiles={"a": [0.2, 1], "b": [0, 1]})

        x.fit(df)

        x2 = CappingTransformer(quantiles={"a": [0.2, 1], "b": [0, 1]})

        x2.fit(df)

        x2.transform(df)

        assert (
            x.capping_values == x2.capping_values
        ), "capping_values attribute modified in transform"
        assert (
            x._replacement_values == x2._replacement_values
        ), "_replacement_values attribute modified in transform"
        assert (
            x.weights_column == x2.weights_column
        ), "weights_column attribute modified in transform"
        assert x.quantiles == x2.quantiles, "quantiles attribute modified in transform"
コード例 #2
0
    def test_prepare_quantiles_output_set_attributes(self, mocker, weights_column):
        """Test the output of prepare_quantiles is set to capping_values and_replacement_values attributes."""

        df = d.create_df_9()

        x = CappingTransformer(
            quantiles={"a": [0.1, 1], "b": [0.5, None]}, weights_column=weights_column
        )

        mocked_return_values = [["aaaa", "bbbb"], [1234, None]]

        mocker.patch(
            "tubular.capping.CappingTransformer.prepare_quantiles",
            side_effect=mocked_return_values,
        )

        x.fit(df)

        h.test_object_attributes(
            obj=x,
            expected_attributes={
                "capping_values": {
                    "a": mocked_return_values[0],
                    "b": mocked_return_values[1],
                },
                "_replacement_values": {
                    "a": mocked_return_values[0],
                    "b": mocked_return_values[1],
                },
            },
            msg="weighted_quantile output set to capping_values, _replacement_values attributes",
        )
コード例 #3
0
    def test_prepare_quantiles_call_weight(self, mocker):
        """Test the call to prepare_quantiles if weights_column is set."""

        df = d.create_df_9()

        x = CappingTransformer(
            quantiles={"a": [0.1, 1], "b": [0.5, None]}, weights_column="c"
        )

        expected_call_args = {
            0: {
                "args": (d.create_df_9()["a"], [0.1, 1], d.create_df_9()["c"]),
                "kwargs": {},
            },
            1: {
                "args": (d.create_df_9()["b"], [0.5, None], d.create_df_9()["c"]),
                "kwargs": {},
            },
        }

        with h.assert_function_call(
            mocker,
            tubular.capping.CappingTransformer,
            "prepare_quantiles",
            expected_call_args,
        ):

            x.fit(df)
コード例 #4
0
    def test_quantiles_none_error(self):
        """Test that an exception is raised if quantiles is None when fit is run."""

        with pytest.warns(
            UserWarning,
            match="quantiles not set so no fitting done in CappingTransformer",
        ):

            df = d.create_df_3()

            x = CappingTransformer(capping_values={"a": [2, 5], "b": [-1, 8]})

            x.fit(df)
コード例 #5
0
    def test_super_fit_call(self, mocker):
        """Test the call to BaseTransformer.fit."""

        df = d.create_df_9()

        x = CappingTransformer(
            quantiles={"a": [0.1, 1], "b": [0.5, None]}, weights_column="c"
        )

        expected_call_args = {0: {"args": (d.create_df_9(), None), "kwargs": {}}}

        with h.assert_function_call(
            mocker, tubular.base.BaseTransformer, "fit", expected_call_args
        ):

            x.fit(df)
コード例 #6
0
    def test_quantile_combinations_handled(self, quantiles, weights_column):
        """Test that a given combination of None and non-None quantile values can be calculated successfully."""

        df = d.create_df_9()

        x = CappingTransformer(
            quantiles={"a": quantiles}, weights_column=weights_column
        )

        try:

            x.fit(df)

        except Exception as err:

            pytest.fail(
                f"unexpected exception when calling fit with quantiles {quantiles} - {err}"
            )