Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-hse-repository committed Feb 27, 2023
1 parent e9a58cd commit 036c7a8
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 11 deletions.
8 changes: 4 additions & 4 deletions etna/transforms/math/differencing.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ def _get_column_name(self) -> str:

def get_regressors_info(self) -> List[str]:
"""Return the list with regressors created by the transform."""
if self.inplace:
return []
if self.in_column_regressor is None:
raise ValueError("Fit the transform to get the correct regressors info!")
if self.inplace:
return []
return [self._get_column_name()] if self.in_column_regressor else []

def fit(self, ts: TSDataset) -> "_SingleDifferencingTransform":
Expand Down Expand Up @@ -358,10 +358,10 @@ def _get_column_name(self) -> str:

def get_regressors_info(self) -> List[str]:
"""Return the list with regressors created by the transform."""
if self.inplace:
return []
if self.in_column_regressor is None:
raise ValueError("Fit the transform to get the correct regressors info!")
if self.inplace:
return []
return [self._get_column_name()] if self.in_column_regressor else []

def fit(self, ts: TSDataset) -> "DifferencingTransform":
Expand Down
4 changes: 2 additions & 2 deletions etna/transforms/math/sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,8 @@ def _inverse_reshape(self, df: pd.DataFrame, transformed: np.ndarray) -> np.ndar

def get_regressors_info(self) -> List[str]:
"""Return the list with regressors created by the transform."""
if self.inplace:
return []
if self.out_column_regressors is None:
raise ValueError("Fit the transform to get the correct regressors info!")
if self.inplace:
return []
return self.out_column_regressors
4 changes: 2 additions & 2 deletions etna/transforms/missing_values/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,10 @@ def _get_out_column(self, out_column: Optional[str]) -> str:

def get_regressors_info(self) -> List[str]:
"""Return the list with regressors created by the transform."""
if self.inplace:
return []
if self.in_column_regressor is None:
raise ValueError("Fit the transform to get the correct regressors info!")
if self.inplace:
return []
return [self.out_column] if self.in_column_regressor else []

def fit(self, ts: TSDataset) -> "ResampleWithDistributionTransform":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,11 @@ def test_save_load_ohe(dtype):
for i in range(3):
transform = OneHotEncoderTransform(in_column=f"regressor_{i}", out_column="test")
assert_transformation_equals_loaded_original(transform=transform, ts=ts)


@pytest.mark.parametrize(
"transform", (OneHotEncoderTransform(in_column="regressor_0"), LabelEncoderTransform(in_column="regressor_0"))
)
def test_get_regressors_info_not_fitted(transform):
with pytest.raises(ValueError, match="Fit the transform to get the correct regressors info!"):
_ = transform.get_regressors_info()
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,9 @@ def test_fit_transform_with_nans(ts_diff_endings):
def test_save_load(inplace, example_tsds):
transform = AddConstTransform(in_column="target", value=10, inplace=inplace)
assert_transformation_equals_loaded_original(transform=transform, ts=example_tsds)


def test_get_regressors_info_not_fitted():
transform = AddConstTransform(in_column="target", value=1, out_column="out_column")
with pytest.raises(ValueError, match="Fit the transform to get the correct regressors info!"):
_ = transform.get_regressors_info()
Original file line number Diff line number Diff line change
Expand Up @@ -474,3 +474,9 @@ def test_save_load(inplace, ts_nans):
ts = ts_nans
transform = DifferencingTransform(in_column="target", inplace=inplace)
assert_transformation_equals_loaded_original(transform=transform, ts=ts)


def test_get_regressors_info_not_fitted():
transform = DifferencingTransform(in_column="target")
with pytest.raises(ValueError, match="Fit the transform to get the correct regressors info!"):
_ = transform.get_regressors_info()
6 changes: 6 additions & 0 deletions tests/test_transforms/test_math/test_lambda_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,9 @@ def test_save_load(inplace, ts_range_const):
inverse_transform_func=example_inverse_transform_func,
)
assert_transformation_equals_loaded_original(transform=transform, ts=ts_range_const)


def test_get_regressors_info_not_fitted():
transform = LambdaTransform(in_column="target", inplace=False, transform_func=lambda x: x)
with pytest.raises(ValueError, match="Fit the transform to get the correct regressors info!"):
_ = transform.get_regressors_info()
6 changes: 6 additions & 0 deletions tests/test_transforms/test_math/test_log_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,9 @@ def test_save_load(inplace, positive_ts_):
ts = positive_ts_
transform = LogTransform(in_column="target", inplace=inplace)
assert_transformation_equals_loaded_original(transform=transform, ts=ts)


def test_get_regressors_info_not_fitted():
transform = LogTransform(in_column="target")
with pytest.raises(ValueError, match="Fit the transform to get the correct regressors info!"):
_ = transform.get_regressors_info()
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,23 @@ def test_ordering(transform_constructor, in_column, mode, multicolumn_ts):
df_multi = transformed_df.loc[:, pd.IndexSlice[segments, column_multi]]
df_single = transformed_dfs_one_column[i].loc[:, pd.IndexSlice[segments, column_single]]
assert np.all(df_multi.values == df_single.values)


@pytest.mark.parametrize(
"transform_constructor",
[
BoxCoxTransform,
YeoJohnsonTransform,
StandardScalerTransform,
RobustScalerTransform,
MinMaxScalerTransform,
MaxAbsScalerTransform,
StandardScalerTransform,
RobustScalerTransform,
MinMaxScalerTransform,
],
)
def test_get_regressors_info_not_fitted(transform_constructor):
transform = transform_constructor(in_column="target")
with pytest.raises(ValueError, match="Fit the transform to get the correct regressors info!"):
_ = transform.get_regressors_info()
4 changes: 1 addition & 3 deletions tests/test_transforms/test_math/test_statistics_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ def test_get_regressors_info(example_reg_tsds, in_column, expected_regressors):

def test_get_regressors_info_not_fitted():
transform = DummyWindowStatisticsTransform(in_column="target", out_column="out_column", window=1)
with pytest.warns(
UserWarning, match="Regressors info might be incorrect. Fit the transform to get the correct regressors info."
):
with pytest.raises(ValueError, match="Fit the transform to get the correct regressors info!"):
_ = transform.get_regressors_info()


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,9 @@ def test_save_load(inplace, out_column, daily_exog_ts):
in_column="regressor_exog", inplace=inplace, distribution_column="target", out_column=out_column
)
assert_transformation_equals_loaded_original(transform=transform, ts=daily_exog_ts)


def test_get_regressors_info_not_fitted():
transform = ResampleWithDistributionTransform(in_column="regressor_exog", distribution_column="target")
with pytest.raises(ValueError, match="Fit the transform to get the correct regressors info!"):
_ = transform.get_regressors_info()

0 comments on commit 036c7a8

Please sign in to comment.