コード例 #1
0
    def test_impute_values_set_to_attribute(self):
        """Test that the value passed for impute_value is saved in an attribute of the same name."""

        x = LogTransformer(
            columns=["a", "b"],
            base=1,
            add_1=True,
            drop=False,
            suffix="new",
            verbose=True,
            copy=True,
        )

        expected_attributes = {
            "base": 1,
            "add_1": True,
            "drop": False,
            "suffix": "new"
        }

        h.test_object_attributes(
            obj=x,
            expected_attributes=expected_attributes,
            msg="Attributes for LogTransformer set in init",
        )
コード例 #2
0
def test_assert_equal_dispatch_calls():
    """Test the call arguments to assert_equal_dispatch."""

    x = DummyClass()

    expected_attributes = {"a": 1, "b": 2, "c": 3}

    call_n = 0

    with mock.patch(target="tubular.testing.helpers.assert_equal_dispatch"
                    ) as mocked_method:

        h.test_object_attributes(obj=x,
                                 expected_attributes=expected_attributes,
                                 msg="test_msg")

        call_n = 0

        for k, v in expected_attributes.items():

            call_n_args = mocked_method.call_args_list[call_n]
            call_n_pos_args = call_n_args[0]
            call_n_kwargs = call_n_args[1]

            expected_pos_args = ()

            expected_kwargs = {
                "expected": v,
                "actual": getattr(x, k),
                "msg": f"{k} test_msg",
            }

            assert (
                call_n_pos_args == expected_pos_args
            ), f"Unexpected call positional args in call {call_n} -\n  Expected: None\n  Actual: {call_n_pos_args}"

            assert len(call_n_kwargs.keys()) == len(
                expected_kwargs.keys()
            ), f"Difference in number of positional arguments in call {call_n} -\n  Expected: {len(call_n_kwargs.keys())}\n  Actual: {len(expected_kwargs.keys())}"

            keys_diff_e_a = set(expected_kwargs.keys()) - set(
                call_n_kwargs.keys())

            keys_diff_a_e = set(call_n_kwargs.keys()) - set(
                expected_kwargs.keys())

            assert (
                keys_diff_e_a == set()
            ), f"Keys in expected not in actual: {keys_diff_e_a}\nKeys in actual not in expected: {keys_diff_a_e}"

            for k in expected_kwargs.keys():

                e = expected_kwargs[k]
                a = call_n_kwargs[k]

                assert (
                    e == a
                ), f"Difference in keyword arg {k} -\n Expected: {e}\n  Actual: {a}"

            call_n += 1
コード例 #3
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",
        )
コード例 #4
0
def test_expected_attributes_non_dict_error():
    """Test that a TypeError is raised if expected_attributes passed as not a dict."""

    x = DummyClass()

    with pytest.raises(TypeError):

        h.test_object_attributes(obj=x,
                                 expected_attributes=(1, 2),
                                 msg="test_msg")
コード例 #5
0
    def test_values_passed_in_init_set_to_attribute(self):
        """Test that the values passed in init are saved in an attribute of the same name."""

        x = OrdinalEncoderTransformer(response_column="aaa")

        h.test_object_attributes(
            obj=x,
            expected_attributes={"response_column": "aaa"},
            msg="Attributes for OrdinalEncoderTransformer set in init",
        )
コード例 #6
0
    def test_columns_str_to_list(self):
        """Test columns is converted to list if passed as string."""

        x = BaseTransformer(columns="a")

        expected_attributes = {"columns": ["a"]}

        h.test_object_attributes(
            obj=x,
            expected_attributes=expected_attributes,
            msg="String put in list for columns",
        )
コード例 #7
0
def test_expected_attribute_missing_error():
    """Test that an AssertionError is raised if an excpeted attribute is missing from object."""

    x = DummyClass()

    expected_attributes = {"a": 1, "b": 2, "c": 3, "d": 4}

    with pytest.raises(AssertionError, match="obj has not attribute d"):

        h.test_object_attributes(obj=x,
                                 expected_attributes=expected_attributes,
                                 msg="test_msg")
コード例 #8
0
def test_expected_attribute_wrong_error():
    """Test that an AssertionError is raised if an excpeted attribute value is wrong."""

    x = DummyClass()

    expected_attributes = {"a": 1, "b": 2, "c": 10}

    with pytest.raises(AssertionError, match=f"""{'c'} {'test_msg'}"""):

        h.test_object_attributes(obj=x,
                                 expected_attributes=expected_attributes,
                                 msg="test_msg")
コード例 #9
0
    def test_mappings_set_to_attribute(self):
        """Test that the value passed for mappings is saved in an attribute of the same name."""

        value = {"a": {"a": 1}, "b": {"a": 1}}

        x = BaseMappingTransformer(mappings=value)

        h.test_object_attributes(
            obj=x,
            expected_attributes={"mappings": value},
            msg="Attributes for BaseMappingTransformer set in init",
        )
コード例 #10
0
    def test_adjust_column_set_to_attribute(self):
        """Test that the value passed for adjust_column is saved in an attribute of the same name."""

        value = "b"

        x = CrossColumnMappingTransformer(mappings={"a": {"a": 1}}, adjust_column=value)

        h.test_object_attributes(
            obj=x,
            expected_attributes={"adjust_column": value},
            msg="Attributes for CrossColumnMappingTransformer set in init",
        )
コード例 #11
0
    def test_start_encoding_set_to_attribute(self):
        """Test that the value passed for start_encoding is saved in an attribute of the same name."""

        value = 1

        x = NominalToIntegerTransformer(start_encoding=value)

        h.test_object_attributes(
            obj=x,
            expected_attributes={"start_encoding": value},
            msg="Attributes for NominalToIntegerTransformer set in init",
        )
コード例 #12
0
    def test_values_passed_in_init_set_to_attribute(self):
        """Test that the values passed in init are saved in an attribute of the same name."""

        x = OneHotEncodingTransformer(
            columns=None, verbose=True, copy=True, separator="x", drop_original=True
        )

        h.test_object_attributes(
            obj=x,
            expected_attributes={"separator": "x", "drop_original": True},
            msg="Attributes for OneHotEncodingTransformer set in init",
        )
コード例 #13
0
    def test_attributes_set(self):
        """Test that the values passed for new_column_name, pd_method_name are saved to attributes on the object."""

        x = DataFrameMethodTransformer(new_column_name="a",
                                       pd_method_name="sum",
                                       columns=["b", "c"])

        h.test_object_attributes(
            obj=x,
            expected_attributes={
                "new_column_name": "a",
                "pd_method_name": "sum"
            },
            msg="Attributes for DataFrameMethodTransformer set in init",
        )
コード例 #14
0
    def test_impute_values_set_to_attribute(self):
        """Test that the value passed for impute_value is saved in an attribute of the same name."""

        value = 1

        x = ArbitraryImputer(impute_value=value, columns="a")

        h.test_object_attributes(
            obj=x,
            expected_attributes={
                "impute_value": value,
                "impute_values_": {}
            },
            msg="Attributes for ArbitraryImputer set in init",
        )
コード例 #15
0
    def test_callable_attribute_set(self, pd_method_name, callable_attr):
        """Test the _callable attribute is set to True if pd.Series.dt.pd_method_name is callable."""

        x = SeriesDtMethodTransformer(
            new_column_name="a",
            pd_method_name=pd_method_name,
            column="b",
            pd_method_kwargs={"d": 1},
        )

        h.test_object_attributes(
            obj=x,
            expected_attributes={"_callable": callable_attr},
            msg="_callable attribute for SeriesDtMethodTransformer set in init",
        )
コード例 #16
0
    def test_learnt_values_not_modified(self):
        """Test that the replacements from fit are not changed in transform."""

        capping_values_dict = {"a": [2, 5], "b": [-1, 8]}

        df = d.create_df_3()

        x = CappingTransformer(capping_values_dict)

        x.transform(df)

        h.test_object_attributes(
            obj=x,
            expected_attributes={"capping_values": capping_values_dict},
            msg="Attributes for CappingTransformer set in init",
        )
コード例 #17
0
    def test_attributes_set_from_passed_values(self):
        """Test attributes set from values passed in init have the correct values."""

        expected_attributes = {
            "columns": ["a", "b", "c"],
            "copy": False,
            "verbose": True,
        }

        x = BaseTransformer(**expected_attributes)

        h.test_object_attributes(
            obj=x,
            expected_attributes=expected_attributes,
            msg="Attributes set in init from passed values",
        )
コード例 #18
0
    def test_impute_value_unchanged(self):
        """Test that self.impute_value is unchanged after transform."""

        df = d.create_df_1()

        value = 1

        x = ArbitraryImputer(impute_value=value, columns="a")

        x.transform(df)

        h.test_object_attributes(
            obj=x,
            expected_attributes={"impute_value": value},
            msg="impute_value changed in transform",
        )
コード例 #19
0
def test_getattr_calls():
    """Test the call arguments to getattr."""

    x = DummyClass()

    expected_attributes = {"a": 1, "b": 2, "c": 3}

    call_n = 0

    with mock.patch(target="tubular.testing.helpers.getattr") as mocked_method:

        # again mock assert_equal_dispatch (called by test_object_attributes) so it does not error from
        # getattr not returning the right things - as it is mocked
        with mock.patch(
                target="tubular.testing.helpers.assert_equal_dispatch"):

            h.test_object_attributes(obj=x,
                                     expected_attributes=expected_attributes,
                                     msg="msg")

            call_n = 0

            for k, v in expected_attributes.items():

                call_n_args = mocked_method.call_args_list[call_n]
                call_n_pos_args = call_n_args[0]
                call_n_kwargs = call_n_args[1]

                expected_pos_args = (x, k)

                assert (
                    call_n_kwargs == {}
                ), f"Unexpected call keyword args in call {call_n} -\n  Expected: None\n  Actual: {call_n_kwargs}"

                assert len(call_n_pos_args) == len(
                    expected_pos_args
                ), f"Difference in number of positional arguments in call {call_n} -\n  Expected: {len(expected_pos_args)}\n  Actual: {len(call_n_pos_args)}"

                for i, (e,
                        a) in enumerate(zip(expected_pos_args,
                                            call_n_pos_args)):

                    assert (
                        e == a
                    ), f"Difference in positional args at index {i} in call {call_n} -\n Expected: {e}\n  Actual: {a}"

                call_n += 1
コード例 #20
0
    def test_default_attributes_set_in_init(self):
        """Test correct default values for attributes set in init."""

        x = BaseTransformer()

        expected_attributes = {
            "version_": tubular._version.__version__,
            "verbose": False,
            "columns": None,
            "copy": True,
        }

        h.test_object_attributes(
            obj=x,
            expected_attributes=expected_attributes,
            msg="Default attributes set in init",
        )
コード例 #21
0
def test_n_assert_equal_dispatch_calls():
    """Test the number of calls to tubular.testing.helpers.assert_equal_dispatch."""

    x = DummyClass()

    expected_attributes = {"a": 1, "b": 2, "c": 3}

    with mock.patch(target="tubular.testing.helpers.assert_equal_dispatch"
                    ) as mocked_method:

        h.test_object_attributes(obj=x,
                                 expected_attributes=expected_attributes,
                                 msg="msg")

        assert mocked_method.call_count == len(
            expected_attributes.keys()
        ), f"Unexpected number of calls to hasattr -\n  Expected: {len(expected_attributes.keys())}\n  Actual: {mocked_method.call_count}"
コード例 #22
0
    def test_values_passed_in_init_set_to_attribute_quantiles(self):
        """Test that the capping_values passed in init are saved in an attribute of the same name."""

        quantiles_dict = {"a": [0.2, 1], "b": [None, 0.9]}

        x = CappingTransformer(quantiles=quantiles_dict)

        h.test_object_attributes(
            obj=x,
            expected_attributes={
                "quantiles": quantiles_dict,
                "weights_column": None,
                "capping_values": {},
                "_replacement_values": {},
            },
            msg="quantiles attribute for CappingTransformer set in init",
        )
コード例 #23
0
    def test_learnt_values_weight_2(self):
        """Test that the impute values learnt during fit, using a weight, are expected."""

        df = d.create_df_6()

        x = GroupRareLevelsTransformer(columns=["c"],
                                       cut_off_percent=0.2,
                                       weights="a")

        x.fit(df)

        h.test_object_attributes(
            obj=x,
            expected_attributes={"mapping_": {
                "c": ["f", "g"]
            }},
            msg="mapping_ attribute",
        )
コード例 #24
0
    def test_learnt_values_no_weight(self):
        """Test that the impute values learnt during fit, without using a weight, are expected."""

        df = d.create_df_5()

        x = GroupRareLevelsTransformer(columns=["b", "c"], cut_off_percent=0.2)

        x.fit(df)

        h.test_object_attributes(
            obj=x,
            expected_attributes={
                "mapping_": {
                    "b": ["a", np.NaN],
                    "c": ["a", "c", "e"]
                }
            },
            msg="mapping_ attribute",
        )
コード例 #25
0
    def test_values_passed_in_init_set_to_attribute(self):
        """Test that the values passed in init are saved in an attribute of the same name."""

        x = GroupRareLevelsTransformer(
            cut_off_percent=0.05,
            weight="aaa",
            rare_level_name="bbb",
            record_rare_levels=False,
        )

        h.test_object_attributes(
            obj=x,
            expected_attributes={
                "cut_off_percent": 0.05,
                "weight": "aaa",
                "rare_level_name": "bbb",
                "record_rare_levels": False,
            },
            msg="Attributes for GroupRareLevelsTransformer set in init",
        )
コード例 #26
0
    def test_learnt_values(self):
        """Test that the impute values learnt during fit are expected."""

        df = d.create_df_3()

        x = ModeImputer(columns=["a", "b", "c"])

        x.fit(df)

        h.test_object_attributes(
            obj=x,
            expected_attributes={
                "impute_values_": {
                    "a": df["a"].mode()[0],
                    "b": df["b"].mode()[0],
                    "c": df["c"].mode()[0],
                }
            },
            msg="impute_values_ attribute",
        )
コード例 #27
0
    def test_attributes_set(self):
        """Test that the values passed for new_column_name, pd_method_name are saved to attributes on the object."""

        x = SeriesStrMethodTransformer(
            new_column_name="a",
            pd_method_name="find",
            columns=["b"],
            pd_method_kwargs={"d": 1},
        )

        h.test_object_attributes(
            obj=x,
            expected_attributes={
                "new_column_name": "a",
                "pd_method_name": "find",
                "pd_method_kwargs": {
                    "d": 1
                },
            },
            msg="Attributes for SeriesStrMethodTransformer set in init",
        )
コード例 #28
0
def test_n_getattr_calls():
    """Test the number of calls to getattr."""

    x = DummyClass()

    expected_attributes = {"a": 1, "b": 2, "c": 3}

    with mock.patch(target="tubular.testing.helpers.getattr") as mocked_method:

        # mock assert_equal_dispatch (called by test_object_attributes) so it does not error from
        # getattr not returning the right things - as it is mocked
        with mock.patch(
                target="tubular.testing.helpers.assert_equal_dispatch"):

            h.test_object_attributes(obj=x,
                                     expected_attributes=expected_attributes,
                                     msg="msg")

            assert mocked_method.call_count == len(
                expected_attributes.keys()
            ), f"Unexpected number of calls to hasattr -\n  Expected: {len(expected_attributes.keys())}\n  Actual: {mocked_method.call_count}"
コード例 #29
0
    def test_learnt_values(self):
        """Test that the impute values learnt during fit are expected."""

        df = d.create_df_1()

        x = NominalToIntegerTransformer(columns=["a", "b"], start_encoding=1)

        x.fit(df)

        h.test_object_attributes(
            obj=x,
            expected_attributes={
                "mappings": {
                    "a": {k: i
                          for i, k in enumerate(df["a"].unique(), 1)},
                    "b": {k: i
                          for i, k in enumerate(df["b"].unique(), 1)},
                }
            },
            msg="mappings attribute",
        )
コード例 #30
0
    def test_values_passed_in_init_set_to_attribute(self):
        """Test that attributes are set by init."""

        x = BetweenDatesTransformer(
            column_lower="a",
            column_between="b",
            column_upper="c",
            new_column_name="d",
            lower_inclusive=False,
            upper_inclusive=False,
        )

        h.test_object_attributes(
            obj=x,
            expected_attributes={
                "columns": ["a", "b", "c"],
                "new_column_name": "d",
                "lower_inclusive": False,
                "upper_inclusive": False,
            },
            msg="Attributes for BetweenDatesTransformer set in init",
        )