Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/comp lags feat order #2272

Merged
merged 27 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
ddfb341
fix: reorder lagged features per lags when they are provided componen…
madtoinou Mar 6, 2024
3be57e9
fix: parametrize lagged_features_names test
madtoinou Mar 6, 2024
7a9c8cf
feat: added tests for lagged_features_names when lags are component-s…
madtoinou Mar 6, 2024
ec2f577
fix: create_lagged_name is not affected by lags order different than …
madtoinou Mar 6, 2024
838de67
fix: improve comment
madtoinou Mar 6, 2024
5a7f829
feat: tests verify that list and dict lags yield the same result
madtoinou Mar 6, 2024
80ef121
fix: remove staticmethod for the tests to pass on python 3.9
madtoinou Mar 11, 2024
a2b867d
feat: properly reorder features during autoregression, added correspo…
madtoinou Mar 12, 2024
38096b8
Merge branch 'master' into fix/comp_lags_feat_order
madtoinou Mar 12, 2024
80e4ed6
Merge branch 'master' into fix/comp_lags_feat_order
madtoinou Apr 4, 2024
d15c970
update changelog
madtoinou Apr 4, 2024
00ec6a1
fix: adressing review comments
madtoinou Apr 4, 2024
a27cd0d
Merge branch 'master' into fix/comp_lags_feat_order
madtoinou Apr 4, 2024
617b6ed
fix: moved autoregression lags extraction to tabularization
madtoinou Apr 4, 2024
dd2bee1
Merge branch 'fix/comp_lags_feat_order' of https://github.com/unit8co…
madtoinou Apr 4, 2024
2717183
fix: refactor tests to reduce code duplication
madtoinou Apr 5, 2024
73f4f64
fix: adress review comment
madtoinou Apr 5, 2024
e461b4b
Merge branch 'master' into fix/comp_lags_feat_order
dennisbader Apr 8, 2024
8465b5e
Merge branch 'master' into fix/comp_lags_feat_order
dennisbader Apr 8, 2024
ca50e1f
Merge branch 'master' into fix/comp_lags_feat_order
dennisbader Apr 9, 2024
c9e61d0
fix: remove usage of strict argument in zip, not support in python 3.9
madtoinou Apr 10, 2024
c6a6c74
further refactor lagged data extraction for autoregression
dennisbader Apr 10, 2024
9467190
allow coverage diffs for codecov upload
dennisbader Apr 11, 2024
677a606
use codecov v3
dennisbader Apr 11, 2024
bedf5a8
precompute lagged and ordered feature indices
dennisbader Apr 11, 2024
071372f
Merge branch 'master' into fix/comp_lags_feat_order
dennisbader Apr 11, 2024
be8c706
Merge branch 'master' into fix/comp_lags_feat_order
dennisbader Apr 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ but cannot always guarantee backwards compatibility. Changes that may **break co
**Fixed**
- Fixed a bug in `quantile_loss`, where the loss was computed on all samples rather than only on the predicted quantiles. [#2284](https://github.com/unit8co/darts/pull/2284) by [Dennis Bader](https://github.com/dennisbader).
- Fixed type hint warning "Unexpected argument" when calling `historical_forecasts()` caused by the `_with_sanity_checks` decorator. The type hinting is now properly configured to expect any input arguments and return the output type of the method for which the sanity checks are performed for. [#2286](https://github.com/unit8co/darts/pull/2286) by [Dennis Bader](https://github.com/dennisbader).
- Fixed the order of the features when using component-wise lags so that they are grouped by values, then by components (before, were grouped by components, then by values). [#2272](https://github.com/unit8co/darts/pull/2272) by [Antoine Madrona](https://github.com/madtoinou).
- Fixed a segmentation fault that some users were facing when importing a `LightGBMModel`. [#2304](https://github.com/unit8co/darts/pull/2304) by [Dennis Bader](https://github.com/dennisbader).
- Fixed a bug when using a dropout with a `TorchForecasting` and pytorch lightning versions >= 2.2.0, where the dropout was not properly activated during training. [#2312](https://github.com/unit8co/darts/pull/2312) by [Dennis Bader](https://github.com/dennisbader).

Expand Down
94 changes: 18 additions & 76 deletions darts/models/forecasting/regression_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
from darts.models.forecasting.forecasting_model import GlobalForecastingModel
from darts.timeseries import TimeSeries
from darts.utils.data.tabularization import (
add_static_covariates_to_lagged_data,
_create_lagged_data_autoregression,
create_lagged_component_names,
create_lagged_training_data,
)
Expand Down Expand Up @@ -1019,83 +1019,25 @@ def predict(
last_step_shift = t_pred - (n - step)
t_pred = n - step

np_X = []
# retrieve target lags
if "target" in self.lags:
if predictions:
series_matrix = np.concatenate(
[series_matrix, predictions[-1]], axis=1
)
# component-wise lags
if "target" in self.component_lags:
tmp_X = [
series_matrix[
:,
[lag - (shift + last_step_shift) for lag in comp_lags],
comp_i,
]
for comp_i, (comp, comp_lags) in enumerate(
self.component_lags["target"].items()
)
]
# values are grouped by component
np_X.append(
np.concatenate(tmp_X, axis=1).reshape(
len(series) * num_samples, -1
)
)
else:
# values are grouped by lags
np_X.append(
series_matrix[
:,
[
lag - (shift + last_step_shift)
for lag in self.lags["target"]
],
].reshape(len(series) * num_samples, -1)
)
# retrieve covariate lags, enforce order (dict only preserves insertion order for python 3.6+)
for cov_type in ["past", "future"]:
if cov_type in covariate_matrices:
# component-wise lags
if cov_type in self.component_lags:
tmp_X = [
covariate_matrices[cov_type][
:,
np.array(comp_lags) - self.lags[cov_type][0] + t_pred,
comp_i,
]
for comp_i, (comp, comp_lags) in enumerate(
self.component_lags[cov_type].items()
)
]
np_X.append(
np.concatenate(tmp_X, axis=1).reshape(
len(series) * num_samples, -1
)
)
else:
np_X.append(
covariate_matrices[cov_type][
:, relative_cov_lags[cov_type] + t_pred
].reshape(len(series) * num_samples, -1)
)

# concatenate retrieved lags
X = np.concatenate(np_X, axis=1)
# Need to split up `X` into three equally-sized sub-blocks
# corresponding to each timeseries in `series`, so that
# static covariates can be added to each block; valid since
# each block contains same number of observations:
X_blocks = np.split(X, len(series), axis=0)
X_blocks, _ = add_static_covariates_to_lagged_data(
X_blocks,
series,
# concatenate previous iteration forecasts
if "target" in self.lags and predictions:
series_matrix = np.concatenate([series_matrix, predictions[-1]], axis=1)

# extract and concatenate lags from target and covariates series
X = _create_lagged_data_autoregression(
target_series=series,
t_pred=t_pred,
shift=shift,
last_step_shift=last_step_shift,
series_matrix=series_matrix,
covariate_matrices=covariate_matrices,
lags=self.lags,
component_lags=self.component_lags,
relative_cov_lags=relative_cov_lags,
num_samples=num_samples,
uses_static_covariates=self.uses_static_covariates,
last_shape=self._static_covariates_shape,
last_static_covariates_shape=self._static_covariates_shape,
)
X = np.concatenate(X_blocks, axis=0)

# X has shape (n_series * n_samples, n_regression_features)
prediction = self._predict_and_sample(
Expand Down
7 changes: 6 additions & 1 deletion darts/tests/models/forecasting/test_regression_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1991,7 +1991,7 @@ def test_component_specific_lags(self, config):
)

# n > output_chunk_length
model.predict(
pred = model.predict(
7,
series=series[0] if multiple_series else None,
past_covariates=(
Expand All @@ -2005,6 +2005,11 @@ def test_component_specific_lags(self, config):
else None
),
)
# check that lagged features are properly extracted during auto-regression
if multivar_target:
np.testing.assert_array_almost_equal(
tg.sine_timeseries(length=27)[-7:].values(), pred["sine"].values()
)

@pytest.mark.parametrize(
"config",
Expand Down