aimclub / Fedot.Industrial

Python framework for automated time series classification, regression and forecasting
https://fedotindustrial.readthedocs.io
BSD 3-Clause "New" or "Revised" License
84 stars 7 forks source link

Sweep: Create unit-tests for forecasting ssa model #152

Closed technocreep closed 3 months ago

technocreep commented 3 months ago

Details

Create unit-tests for SSAForecasterImplementation class located in fedot_ind.core.models.ts_forecasting.ssa_forecaster module. It must cover most of edge cases.

corresponding test file must be located in tests/unit/core/models directory.

Branch

No response

sweep-ai[bot] commented 3 months ago

🚀 Here's the PR! #155

💎 Sweep Pro: You have unlimited Sweep issues

Actions

Step 1: 🔎 Searching

(Click to expand) Here are the code search results. I'm now analyzing these search results to write the PR. https://github.com/aimclub/Fedot.Industrial/blob/e66a1234d7c4f1c67d6085c2a9adfa690985395c/fedot_ind/core/models/ts_forecasting/ssa_forecaster.py#L1-L197 https://github.com/aimclub/Fedot.Industrial/blob/e66a1234d7c4f1c67d6085c2a9adfa690985395c/fedot_ind/core/repository/constanst_repository.py#L1-L715 https://github.com/aimclub/Fedot.Industrial/blob/e66a1234d7c4f1c67d6085c2a9adfa690985395c/fedot_ind/core/repository/data/default_operation_params.json#L1-L387 https://github.com/aimclub/Fedot.Industrial/blob/e66a1234d7c4f1c67d6085c2a9adfa690985395c/fedot_ind/core/repository/data/industrial_model_repository.json#L1-L650 https://github.com/aimclub/Fedot.Industrial/blob/e66a1234d7c4f1c67d6085c2a9adfa690985395c/fedot_ind/core/tuning/search_space.py#L1-L724

Step 2: ⌨️ Coding

I'm going to follow the following steps to help you solve the GitHub issue:

  1. Create a new test file named test_ssa_forecaster.py in the tests/unit/core/models directory.
  2. Import the necessary modules in the test file, including the SSAForecasterImplementation class from fedot_ind.core.models.ts_forecasting.ssa_forecaster.
  3. Define a test class named TestSSAForecasterImplementation in the test file.
  4. Implement a test method to verify the initialization of the SSAForecasterImplementation class with default parameters.
  5. Implement a test method to verify the initialization of the SSAForecasterImplementation class with custom parameters.
  6. Implement test methods to cover the predict method of the SSAForecasterImplementation class with different input data scenarios.
  7. Implement test methods to cover the edge cases and potential error scenarios in the SSAForecasterImplementation class.

Here are the changes we decided to make. I'm done making edits and now I'm just validating the changes using a linter to catch any mistakes like syntax errors or undefined variables:

tests/unit/core/models/test_ssa_forecaster.py

Create a new file named test_ssa_forecaster.py in the tests/unit/core/models directory. This file will contain the unit tests for the SSAForecasterImplementation class.
import unittest
import numpy as np
from fedot.core.data.data import InputData
from fedot_ind.core.models.ts_forecasting.ssa_forecaster import SSAForecasterImplementation

class TestSSAForecasterImplementation(unittest.TestCase):
    def test_default_initialization(self):
        forecaster = SSAForecasterImplementation()
        self.assertIsInstance(forecaster, SSAForecasterImplementation)
        self.assertEqual(forecaster.window_size_method, None)
        self.assertEqual(forecaster.history_lookback, 100)
        self.assertEqual(forecaster.low_rank_approximation, False)
        self.assertEqual(forecaster.tuning_params, {'tuning_iterations': 100, 'tuning_timeout': 20, 'tuning_early_stop': 20, 'tuner': 'SimultaneousTuner'})
        self.assertEqual(forecaster.component_model, 'topological')
        self.assertEqual(forecaster.mode, 'channel_independent')

    def test_custom_initialization(self):
        params = {
            'window_size_method': 'hac',
            'history_lookback': 50,
            'low_rank_approximation': True,
            'tuning_params': {'tuning_iterations': 50, 'tuning_timeout': 10, 'tuning_early_stop': 10, 'tuner': 'OptunaTuner'},
            'component_model': 'ar',
            'mode': 'one_dimensional'
        }
        forecaster = SSAForecasterImplementation(params)
        self.assertIsInstance(forecaster, SSAForecasterImplementation)
        self.assertEqual(forecaster.window_size_method, 'hac')
        self.assertEqual(forecaster.history_lookback, 50)
        self.assertEqual(forecaster.low_rank_approximation, True)
        self.assertEqual(forecaster.tuning_params, {'tuning_iterations': 50, 'tuning_timeout': 10, 'tuning_early_stop': 10, 'tuner': 'OptunaTuner'})
        self.assertEqual(forecaster.component_model, 'ar')
        self.assertEqual(forecaster.mode, 'one_dimensional')

    def test_predict_one_dimensional(self):
        forecaster = SSAForecasterImplementation({'mode': 'one_dimensional'})
        time_series = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
        input_data = InputData(features=time_series, target=time_series)
        forecast = forecaster.predict(input_data)
        self.assertIsInstance(forecast.predict, np.ndarray)
        self.assertEqual(forecast.predict.shape, (forecaster.horizon,))

    def test_predict_channel_independent(self):
        forecaster = SSAForecasterImplementation({'mode': 'channel_independent'})
        time_series = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
        input_data = InputData(features=time_series, target=time_series)
        forecast = forecaster.predict(input_data)
        self.assertIsInstance(forecast.predict, np.ndarray)
        self.assertEqual(forecast.predict.shape, (forecaster.horizon,))

    def test_predict_missing_values(self):
        forecaster = SSAForecasterImplementation()
        time_series = np.array([1, 2, 3, np.nan, 5, 6, 7, 8, 9, 10])
        input_data = InputData(features=time_series, target=time_series)
        forecast = forecaster.predict(input_data)
        self.assertIsInstance(forecast.predict, np.ndarray)
        self.assertEqual(forecast.predict.shape, (forecaster.horizon,))

    def test_predict_invalid_input(self):
        forecaster = SSAForecasterImplementation()
        input_data = InputData(features=None, target=None)
        with self.assertRaises(ValueError):
            forecaster.predict(input_data)

        input_data = InputData(features=np.array([]), target=np.array([]))
        with self.assertRaises(ValueError):
            forecaster.predict(input_data)

        input_data = InputData(features=np.array([1, 2, 3]), target=np.array([1, 2]))
        with self.assertRaises(ValueError):
            forecaster.predict(input_data)

if __name__ == '__main__':
    unittest.main()

Step 3: 🔄️ Validating

Your changes have been successfully made to the branch sweep/create_unittests_for_forecasting_ssa_mod_efe26. I have validated these changes using a syntax checker and a linter.


[!TIP] To recreate the pull request, edit the issue title or description.

This is an automated message generated by Sweep AI.

technocreep commented 3 months ago

Sweep:

FAILED tests/unit/core/models/test_ssa_forecaster.py::TestSSAForecasterImplementation::test_fit - TypeError: init() missing 1 required positional argument: 'data_type' FAILED tests/unit/core/models/test_ssa_forecaster.py::TestSSAForecasterImplementation::test_init_custom_params - AssertionError: 100 != 50 FAILED tests/unit/core/models/test_ssa_forecaster.py::TestSSAForecasterImplementation::test_init_default_params - AttributeError: 'NoneType' object has no attribute 'get' FAILED tests/unit/core/models/test_ssa_forecaster.py::TestSSAForecasterImplementation::test_predict_complex_ts - TypeError: init() missing 1 required positional argument: 'data_type' FAILED tests/unit/core/models/test_ssa_forecaster.py::TestSSAForecasterImplementation::test_predict_edge_cases - TypeError: init() missing 1 required positional argument: 'data_type' FAILED tests/unit/core/models/test_ssa_forecaster.py::TestSSAForecasterImplementation::test_predict_for_fit_complex_ts - TypeError: init() missing 1 required positional argument: 'data_type' FAILED tests/unit/core/models/test_ssa_forecaster.py::TestSSAForecasterImplementation::test_predict_for_fit_edge_cases - TypeError: init() missing 1 required positional argument: 'data_type' FAILED tests/unit/core/models/test_ssa_forecaster.py::TestSSAForecasterImplementation::test_predict_for_fit_simple_ts - TypeError: init() missing 1 required positional argument: 'data_type' FAILED tests/unit/core/models/test_ssa_forecaster.py::TestSSAForecasterImplementation::test_predict_simple_ts - TypeError: init() missing 1 required positional argument: 'data_type'