|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# This file as well as the whole tsfresh package are licenced under the MIT licence (see the LICENCE.txt) |
| 3 | +# Maximilian Christ (maximilianchrist.com), Blue Yonder Gmbh, 2016 |
| 4 | + |
| 5 | +from builtins import range |
| 6 | +from unittest import TestCase |
| 7 | +import pandas as pd |
| 8 | +import pandas.util.testing as pdt |
| 9 | +from sklearn.exceptions import NotFittedError |
| 10 | + |
| 11 | +import numpy as np |
| 12 | +import numpy.testing as npt |
| 13 | + |
| 14 | +from tsfresh.transformers.per_column_imputer import PerColumnImputer |
| 15 | + |
| 16 | + |
| 17 | +class PerColumnImputerTestCase(TestCase): |
| 18 | + def setUp(self): |
| 19 | + np.random.seed(0) |
| 20 | + |
| 21 | + def test_not_fitted(self): |
| 22 | + imputer = PerColumnImputer() |
| 23 | + |
| 24 | + X = pd.DataFrame() |
| 25 | + |
| 26 | + self.assertRaises(NotFittedError, imputer.transform, X) |
| 27 | + |
| 28 | + def test_only_nans_and_infs(self): |
| 29 | + imputer = PerColumnImputer() |
| 30 | + |
| 31 | + X = pd.DataFrame(index=list(range(100))) |
| 32 | + |
| 33 | + X["NaNs"] = np.nan * np.ones(100) |
| 34 | + X["PINF"] = np.PINF * np.ones(100) |
| 35 | + X["NINF"] = np.NINF * np.ones(100) |
| 36 | + |
| 37 | + imputer.fit(X) |
| 38 | + selected_X = imputer.transform(X) |
| 39 | + |
| 40 | + self.assertTrue((selected_X.values == 0).all()) |
| 41 | + |
| 42 | + def test_with_numpy_array(self): |
| 43 | + imputer = PerColumnImputer() |
| 44 | + |
| 45 | + X = pd.DataFrame(index=list(range(100))) |
| 46 | + |
| 47 | + X["NaNs"] = np.nan * np.ones(100) |
| 48 | + X["PINF"] = np.PINF * np.ones(100) |
| 49 | + X["NINF"] = np.NINF * np.ones(100) |
| 50 | + |
| 51 | + X_numpy = X.values |
| 52 | + |
| 53 | + imputer.fit(X) |
| 54 | + selected_X = imputer.transform(X) |
| 55 | + |
| 56 | + #re-initialize for new dicts |
| 57 | + imputer = PerColumnImputer() |
| 58 | + imputer.fit(X_numpy) |
| 59 | + selected_X_numpy = imputer.transform(X_numpy) |
| 60 | + |
| 61 | + npt.assert_array_equal(selected_X.values, selected_X_numpy.values) |
| 62 | + |
| 63 | + self.assertTrue(selected_X_numpy.shape, (1, 100)) |
| 64 | + |
| 65 | + def test_standard_replacement_behavior(self): |
| 66 | + imputer = PerColumnImputer() |
| 67 | + |
| 68 | + data = [np.NINF, np.PINF, np.nan, 100.0, -100.0, 1.0, 1.0] |
| 69 | + truth = [-100.0, 100.0, 1.0, 100.0, -100.0, 1.0, 1.0] |
| 70 | + X = pd.DataFrame({"a": data}) |
| 71 | + true_X = pd.DataFrame({"a": truth}) |
| 72 | + |
| 73 | + imputer.fit(X) |
| 74 | + selected_X = imputer.transform(X) |
| 75 | + |
| 76 | + pdt.assert_frame_equal(selected_X, true_X) |
| 77 | + |
| 78 | + def test_partial_preset_col_to_NINF_given(self): |
| 79 | + data = [np.NINF, np.PINF, np.nan, 100.0, -100.0, 1.0, 1.0] |
| 80 | + truth = [-100.0, 100.0, 1.0, 100.0, -100.0, 1.0, 1.0] |
| 81 | + X = pd.DataFrame({"a": data}) |
| 82 | + true_X = pd.DataFrame({"a": truth}) |
| 83 | + |
| 84 | + col_to_min = {"a": -100} |
| 85 | + imputer = PerColumnImputer(col_to_NINF_repl_preset=col_to_min) |
| 86 | + |
| 87 | + imputer.fit(X) |
| 88 | + selected_X = imputer.transform(X) |
| 89 | + |
| 90 | + pdt.assert_frame_equal(selected_X, true_X) |
| 91 | + |
| 92 | + def test_partial_preset_col_to_PINF_given(self): |
| 93 | + data = [np.NINF, np.PINF, np.nan, 100.0, -100.0, 1.0, 1.0] |
| 94 | + truth = [-100.0, 100.0, 1.0, 100.0, -100.0, 1.0, 1.0] |
| 95 | + X = pd.DataFrame({"a": data}) |
| 96 | + true_X = pd.DataFrame({"a": truth}) |
| 97 | + |
| 98 | + col_to_max = {"a": 100} |
| 99 | + imputer = PerColumnImputer(col_to_PINF_repl_preset=col_to_max) |
| 100 | + |
| 101 | + imputer.fit(X) |
| 102 | + selected_X = imputer.transform(X) |
| 103 | + |
| 104 | + pdt.assert_frame_equal(selected_X, true_X) |
| 105 | + |
| 106 | + def test_partial_preset_col_to_NAN_given(self): |
| 107 | + data = [np.NINF, np.PINF, np.nan, 100.0, -100.0, 1.0, 1.0] |
| 108 | + truth = [-100.0, 100.0, 1.0, 100.0, -100.0, 1.0, 1.0] |
| 109 | + X = pd.DataFrame({"a": data}) |
| 110 | + true_X = pd.DataFrame({"a": truth}) |
| 111 | + |
| 112 | + col_to_median = {"a": 1} |
| 113 | + imputer = PerColumnImputer(col_to_NAN_repl_preset=col_to_median) |
| 114 | + |
| 115 | + imputer.fit(X) |
| 116 | + selected_X = imputer.transform(X) |
| 117 | + |
| 118 | + pdt.assert_frame_equal(selected_X, true_X) |
| 119 | + |
| 120 | + def test_different_shapes_fitted_and_transformed(self): |
| 121 | + imputer = PerColumnImputer() |
| 122 | + |
| 123 | + X = pd.DataFrame(index=list(range(10))) |
| 124 | + X["a"] = np.ones(10) |
| 125 | + |
| 126 | + imputer.fit(X) |
| 127 | + X["b"] = np.ones(10) |
| 128 | + |
| 129 | + self.assertRaises(ValueError, imputer.transform, X) |
| 130 | + |
| 131 | + def test_preset_has_higher_priority_than_fit(self): |
| 132 | + data = [np.NINF, np.PINF, np.nan, 100.0, -100.0, 1.0, 1.0] |
| 133 | + truth = [-100.0, 100.0, 0.0, 100.0, -100.0, 1.0, 1.0] |
| 134 | + |
| 135 | + X = pd.DataFrame({"a": data}) |
| 136 | + true_X = pd.DataFrame({"a": truth}) |
| 137 | + |
| 138 | + col_to_median = {"a": 0} |
| 139 | + imputer = PerColumnImputer(col_to_NAN_repl_preset=col_to_median) |
| 140 | + imputer.fit(X) |
| 141 | + |
| 142 | + selected_X = imputer.transform(X) |
| 143 | + |
| 144 | + pdt.assert_frame_equal(selected_X, true_X) |
| 145 | + |
| 146 | + def test_only_parameters_of_last_fit_count(self): |
| 147 | + data = [np.NINF, np.PINF, np.nan, 100.0, -100.0, 1.0, 1.0] |
| 148 | + data_2 = [np.NINF, np.PINF, np.nan, 10.0, -10.0, 3.0, 3.0] |
| 149 | + truth_a = [-10.0, 10.0, 3.0, 10.0, -10.0, 3.0, 3.0] |
| 150 | + truth_b = [-10.0, 10.0, 3.0, 10.0, -10.0, 3.0, 3.0] |
| 151 | + |
| 152 | + X = pd.DataFrame({"a": data, "b": data}) |
| 153 | + X_2 = pd.DataFrame({"a": data_2, "b": data_2}) |
| 154 | + true_X = pd.DataFrame({"a": truth_a, "b": truth_b}) |
| 155 | + |
| 156 | + imputer = PerColumnImputer() |
| 157 | + |
| 158 | + imputer.fit(X) |
| 159 | + imputer.fit(X_2) |
| 160 | + |
| 161 | + selected_X = imputer.transform(X_2) |
| 162 | + |
| 163 | + pdt.assert_frame_equal(selected_X, true_X) |
| 164 | + |
| 165 | + def test_only_subset_of_columns_given(self): |
| 166 | + data = [np.NINF, np.PINF, np.nan, 100.0, -100.0, 1.0, 1.0] |
| 167 | + truth_a = [-100.0, 100.0, 0.0, 100.0, -100.0, 1.0, 1.0] |
| 168 | + truth_b = [-100.0, 100.0, 1.0, 100.0, -100.0, 1.0, 1.0] |
| 169 | + X = pd.DataFrame({"a": data, "b":data}) |
| 170 | + true_X = pd.DataFrame({"a":truth_a, "b":truth_b}) |
| 171 | + |
| 172 | + col_to_median = {"a": 0} |
| 173 | + imputer = PerColumnImputer(col_to_NAN_repl_preset=col_to_median) |
| 174 | + |
| 175 | + imputer.fit(X) |
| 176 | + selected_X = imputer.transform(X) |
| 177 | + |
| 178 | + pdt.assert_frame_equal(selected_X,true_X) |
| 179 | + |
| 180 | + def test_NINF_preset_contains_more_columns_than_dataframe_to_fit(self): |
| 181 | + X = pd.DataFrame(index=list(range(10))) |
| 182 | + X["a"] = np.ones(10) |
| 183 | + |
| 184 | + col_to_min = {"a": 0, "b":0} |
| 185 | + |
| 186 | + imputer = PerColumnImputer(col_to_NINF_repl_preset=col_to_min) |
| 187 | + |
| 188 | + self.assertRaises(ValueError, imputer.fit, X) |
| 189 | + |
| 190 | + def test_PINF_preset_contains_more_columns_than_dataframe_to_fit(self): |
| 191 | + X = pd.DataFrame(index=list(range(10))) |
| 192 | + X["a"] = np.ones(10) |
| 193 | + |
| 194 | + col_to_max = {"a": 0, "b":0} |
| 195 | + |
| 196 | + imputer = PerColumnImputer(col_to_PINF_repl_preset=col_to_max) |
| 197 | + |
| 198 | + self.assertRaises(ValueError, imputer.fit, X) |
| 199 | + |
| 200 | + def test_NAN_preset_contains_more_columns_than_dataframe_to_fit(self): |
| 201 | + X = pd.DataFrame(index=list(range(10))) |
| 202 | + X["a"] = np.ones(10) |
| 203 | + |
| 204 | + col_to_median = {"a": 0, "b":0} |
| 205 | + |
| 206 | + imputer = PerColumnImputer(col_to_NAN_repl_preset=col_to_median) |
| 207 | + |
| 208 | + self.assertRaises(ValueError, imputer.fit, X) |
0 commit comments