diff --git a/fedot/core/data/data.py b/fedot/core/data/data.py index 3a33a1ad33..db2761d244 100644 --- a/fedot/core/data/data.py +++ b/fedot/core/data/data.py @@ -442,6 +442,9 @@ def slice(self, start: Optional[int] = None, stop: Optional[int] = None, index = np.arange(len(self))[slice(start, stop, step)] + if len(index) == 0: + raise ValueError(f"Uncorrect slicing [{start}:{stop}:{step}] for data with length {len(self)}") + return self.slice_by_index(index, step, in_sample=in_sample) def slice_by_index(self, indexes: Iterable, step: int = 1, in_sample: bool = True): diff --git a/fedot/core/pipelines/ts_wrappers.py b/fedot/core/pipelines/ts_wrappers.py index a2a94b451b..acaf51f137 100644 --- a/fedot/core/pipelines/ts_wrappers.py +++ b/fedot/core/pipelines/ts_wrappers.py @@ -89,16 +89,19 @@ def in_sample_ts_forecast(pipeline, input_data: Union[InputData, MultiModalData] number_of_iterations = math.ceil(horizon / forecast_length) final_forecast = np.zeros((number_of_iterations, forecast_length)) + data = InputData(idx=input_data.idx, + features=input_data.features[:-forecast_length], + target=input_data.target, + data_type=input_data.data_type, + task=input_data.task) for i in range(number_of_iterations): - data = input_data.slice(-(i + 1) * forecast_length, - -i * forecast_length if i != 0 else None, - in_sample=False) - if data.features.shape[0] == 0: - raise ValueError(('Cannot make predict without features.' - 'Please, check that horizon is lower than data.features')) iter_predict = pipeline.predict(input_data=data) - iter_predict = np.ravel(iter_predict.predict) - final_forecast[-(i + 1), :] = iter_predict + final_forecast[-(i + 1), :] = np.ravel(iter_predict.predict) + data = InputData(idx=data.idx[:-forecast_length], + features=data.features[:-forecast_length], + target=data.target[:-forecast_length], + data_type=data.data_type, + task=data.task) final_forecast = np.ravel(final_forecast)[:horizon] return final_forecast diff --git a/test/unit/tasks/test_forecasting.py b/test/unit/tasks/test_forecasting.py index 0c5c51e183..314c74fa21 100644 --- a/test/unit/tasks/test_forecasting.py +++ b/test/unit/tasks/test_forecasting.py @@ -271,19 +271,29 @@ def test_multistep_out_of_sample_forecasting(): def test_multistep_in_sample_forecasting(): horizon = 12 - train_data, test_data = get_ts_data(n_steps=200, forecast_length=5) + train_data, test_data = get_ts_data(n_steps=200, forecast_length=5, validation_blocks=3) pipeline = get_multiscale_pipeline() # Fit pipeline to make forecasts 5 elements above pipeline.fit(input_data=train_data) + # manual predict + a = test_data.slice(-15, -10, in_sample=False) + manual_predict = [] + manual_predict.append(pipeline.predict(input_data=test_data.slice(-15, -10, in_sample=False))) + manual_predict.append(pipeline.predict(input_data=test_data.slice(-10, -5, in_sample=False))) + manual_predict.append(pipeline.predict(input_data=test_data.slice(-5, in_sample=False))) + manual_predict = [np.ravel(x.predict) for x in manual_predict] + manual_predict = np.concatenate(manual_predict, axis=0)[:horizon] + # Make prediction for 12 elements predicted = in_sample_ts_forecast(pipeline=pipeline, input_data=test_data, horizon=horizon) assert len(predicted) == horizon + assert np.array_equal(manual_predict, predicted) def test_ts_forecasting_with_multiple_series_in_lagged():