From 6fbb6701dfbd61ce0adae46994b0d5eadad458a1 Mon Sep 17 00:00:00 2001 From: Dennis Bader Date: Sat, 24 Feb 2024 13:07:13 +0100 Subject: [PATCH] Reformat / lint repository with new dev dependency versions (#2248) * update dev requirements with new pre commit hook lint dependency versions * black reformatting * fix flake8 checks --- darts/ad/aggregators/or_aggregator.py | 1 - darts/ad/anomaly_model/forecasting_am.py | 4 +- darts/ad/detectors/quantile_detector.py | 4 +- darts/ad/detectors/threshold_detector.py | 4 +- darts/ad/scorers/scorers.py | 2 +- darts/dataprocessing/pipeline.py | 1 + darts/dataprocessing/transformers/midas.py | 1 + .../transformers/reconciliation.py | 1 - .../static_covariates_transformer.py | 1 + darts/datasets/__init__.py | 10 +- darts/explainability/explainability.py | 1 + darts/explainability/shap_explainer.py | 13 +- darts/explainability/utils.py | 19 +- darts/metrics/metrics.py | 5 +- darts/models/forecasting/arima.py | 28 +-- darts/models/forecasting/baselines.py | 20 +- darts/models/forecasting/block_rnn_model.py | 2 - darts/models/forecasting/catboost_model.py | 8 +- darts/models/forecasting/croston.py | 16 +- darts/models/forecasting/ensemble_model.py | 12 +- .../forecasting/exponential_smoothing.py | 1 - darts/models/forecasting/fft.py | 6 +- darts/models/forecasting/forecasting_model.py | 39 ++-- darts/models/forecasting/lgbm.py | 8 +- .../forecasting/linear_regression_model.py | 1 + .../forecasting/pl_forecasting_module.py | 54 +++--- darts/models/forecasting/random_forest.py | 1 + .../forecasting/regression_ensemble_model.py | 37 ++-- darts/models/forecasting/regression_model.py | 25 ++- darts/models/forecasting/rnn_model.py | 9 +- darts/models/forecasting/tbats_model.py | 1 - darts/models/forecasting/tcn_model.py | 2 - darts/models/forecasting/tft_model.py | 9 +- darts/models/forecasting/tide_model.py | 8 +- .../forecasting/torch_forecasting_model.py | 2 +- darts/models/forecasting/transformer_model.py | 1 - darts/models/forecasting/varima.py | 29 +-- darts/models/forecasting/xgboost.py | 8 +- .../dataprocessing/transformers/test_diff.py | 7 +- darts/tests/datasets/test_datasets.py | 2 +- .../forecasting/test_dlinear_nlinear.py | 34 ++-- .../test_global_forecasting_models.py | 6 +- .../forecasting/test_historical_forecasts.py | 134 +++++++------ .../test_regression_ensemble_model.py | 4 +- .../forecasting/test_regression_models.py | 177 +++++++++++------- darts/tests/test_timeseries.py | 2 +- .../test_create_lagged_prediction_data.py | 12 +- .../test_create_lagged_training_data.py | 12 +- .../tabularization/test_get_feature_times.py | 8 +- .../tabularization/test_get_shared_times.py | 1 - .../test_strided_moving_window.py | 3 +- darts/tests/utils/test_likelihood_models.py | 2 +- darts/timeseries.py | 40 ++-- darts/utils/__init__.py | 1 + darts/utils/data/inference_dataset.py | 24 +-- darts/utils/data/sequential_dataset.py | 12 +- darts/utils/data/shifted_dataset.py | 12 +- darts/utils/data/training_dataset.py | 12 +- ...timized_historical_forecasts_regression.py | 64 ++++--- darts/utils/likelihood_models.py | 8 +- darts/utils/losses.py | 1 + darts/utils/multioutput.py | 8 +- darts/utils/statistics.py | 21 ++- darts/utils/timeseries_generation.py | 6 +- darts/utils/utils.py | 3 +- requirements/dev.txt | 8 +- 66 files changed, 555 insertions(+), 463 deletions(-) diff --git a/darts/ad/aggregators/or_aggregator.py b/darts/ad/aggregators/or_aggregator.py index 5737839630..a5417a294e 100644 --- a/darts/ad/aggregators/or_aggregator.py +++ b/darts/ad/aggregators/or_aggregator.py @@ -6,7 +6,6 @@ is flagged as anomalous (logical OR). """ - from typing import Sequence from darts import TimeSeries diff --git a/darts/ad/anomaly_model/forecasting_am.py b/darts/ad/anomaly_model/forecasting_am.py index cab0eaf683..0e1f574ca9 100644 --- a/darts/ad/anomaly_model/forecasting_am.py +++ b/darts/ad/anomaly_model/forecasting_am.py @@ -221,7 +221,8 @@ def _prepare_covariates( series: Sequence[TimeSeries], name_covariates: str, ) -> Sequence[TimeSeries]: - """Convert `covariates` into Sequence, if not already, and checks if their length is equal to the one of `series`. + """Convert `covariates` into Sequence, if not already, and checks if their length is equal to the one of + `series`. Parameters ---------- @@ -515,7 +516,6 @@ def _predict_with_forecasting( start: Union[pd.Timestamp, float, int] = None, num_samples: int = 1, ) -> TimeSeries: - """Compute the historical forecasts that would have been obtained by this model on the `series`. `retrain` is set to False if possible (this is not supported by all models). If set to True, it will always diff --git a/darts/ad/detectors/quantile_detector.py b/darts/ad/detectors/quantile_detector.py index 471850990b..4496d8f294 100644 --- a/darts/ad/detectors/quantile_detector.py +++ b/darts/ad/detectors/quantile_detector.py @@ -69,9 +69,7 @@ def _prep_quantile(q): return ( q.tolist() if isinstance(q, np.ndarray) - else [q] - if not isinstance(q, Sequence) - else q + else [q] if not isinstance(q, Sequence) else q ) low = _prep_quantile(low_quantile) diff --git a/darts/ad/detectors/threshold_detector.py b/darts/ad/detectors/threshold_detector.py index 6643c72f37..56c01a026a 100644 --- a/darts/ad/detectors/threshold_detector.py +++ b/darts/ad/detectors/threshold_detector.py @@ -62,9 +62,7 @@ def _prep_thresholds(q): return ( q.tolist() if isinstance(q, np.ndarray) - else [q] - if not isinstance(q, Sequence) - else q + else [q] if not isinstance(q, Sequence) else q ) low = _prep_thresholds(low_threshold) diff --git a/darts/ad/scorers/scorers.py b/darts/ad/scorers/scorers.py index de2f878aab..b9e41cb680 100644 --- a/darts/ad/scorers/scorers.py +++ b/darts/ad/scorers/scorers.py @@ -527,7 +527,7 @@ def score_from_prediction( _assert_same_length(list_actual_series, list_pred_series) anomaly_scores = [] - for (s1, s2) in zip(list_actual_series, list_pred_series): + for s1, s2 in zip(list_actual_series, list_pred_series): _sanity_check_two_series(s1, s2) s1 = self._assert_deterministic(s1, "actual_series") s2 = self._assert_deterministic(s2, "pred_series") diff --git a/darts/dataprocessing/pipeline.py b/darts/dataprocessing/pipeline.py index d43899eb13..f4c9849cd1 100644 --- a/darts/dataprocessing/pipeline.py +++ b/darts/dataprocessing/pipeline.py @@ -2,6 +2,7 @@ Pipeline -------- """ + from copy import deepcopy from typing import Iterator, Sequence, Union diff --git a/darts/dataprocessing/transformers/midas.py b/darts/dataprocessing/transformers/midas.py index bba870a31e..7e06a6dcd5 100644 --- a/darts/dataprocessing/transformers/midas.py +++ b/darts/dataprocessing/transformers/midas.py @@ -2,6 +2,7 @@ Mixed-data sampling (MIDAS) Transformer --------------------------------------- """ + from typing import Any, Dict, List, Mapping, Optional, Sequence, Union import numpy as np diff --git a/darts/dataprocessing/transformers/reconciliation.py b/darts/dataprocessing/transformers/reconciliation.py index bcba40ecc1..e20fde490c 100644 --- a/darts/dataprocessing/transformers/reconciliation.py +++ b/darts/dataprocessing/transformers/reconciliation.py @@ -9,7 +9,6 @@ It can be added to a ``TimeSeries`` using e.g., the :meth:`TimeSeries.with_hierarchy` method. """ - from typing import Any, Mapping, Optional import numpy as np diff --git a/darts/dataprocessing/transformers/static_covariates_transformer.py b/darts/dataprocessing/transformers/static_covariates_transformer.py index 76a2f0373f..3000794092 100644 --- a/darts/dataprocessing/transformers/static_covariates_transformer.py +++ b/darts/dataprocessing/transformers/static_covariates_transformer.py @@ -2,6 +2,7 @@ Static Covariates Transformer ------ """ + from collections import OrderedDict from typing import Any, Dict, List, Optional, Sequence, Tuple diff --git a/darts/datasets/__init__.py b/darts/datasets/__init__.py index eb5dd9a6a2..5074af4c97 100644 --- a/darts/datasets/__init__.py +++ b/darts/datasets/__init__.py @@ -520,7 +520,7 @@ def __init__(self, multivariate: bool = True): def pre_proces_fn(extracted_dir, dataset_path): with open(Path(extracted_dir, "LD2011_2014.txt")) as fin: - with open(dataset_path, "wt", newline="\n") as fout: + with open(dataset_path, "w", newline="\n") as fout: for line in fin: fout.write(line.replace(",", ".").replace(";", ",")) @@ -622,9 +622,11 @@ def pre_proces_fn(extracted_dir, dataset_path): uri="https://github.com/fivethirtyeight/uber-tlc-foil-response/raw/" "63bb878b76f47f69b4527d50af57aac26dead983/" "uber-trip-data/uber-raw-data-janjune-15.csv.zip", - hash="9ed84ebe0df4bc664748724b633b3fe6" - if sample_freq == "hourly" - else "24f9fd67e4b9e53f0214a90268cd9bee", + hash=( + "9ed84ebe0df4bc664748724b633b3fe6" + if sample_freq == "hourly" + else "24f9fd67e4b9e53f0214a90268cd9bee" + ), header_time="Pickup_date", format_time="%Y-%m-%d %H:%M:%S", pre_process_zipped_csv_fn=pre_proces_fn, diff --git a/darts/explainability/explainability.py b/darts/explainability/explainability.py index d1287d749a..c40d226561 100644 --- a/darts/explainability/explainability.py +++ b/darts/explainability/explainability.py @@ -3,6 +3,7 @@ A `_ForecastingModelExplainer` takes a fitted forecasting model as input and generates explanations for it. """ + from abc import ABC, abstractmethod from typing import Optional, Sequence, Tuple, Union diff --git a/darts/explainability/shap_explainer.py b/darts/explainability/shap_explainer.py index 26978bb00a..a31a844ca4 100644 --- a/darts/explainability/shap_explainer.py +++ b/darts/explainability/shap_explainer.py @@ -624,7 +624,6 @@ def shap_explanations( horizons: Optional[Sequence[int]] = None, target_components: Optional[Sequence[str]] = None, ) -> Dict[int, Dict[str, shap.Explanation]]: - """ Return a dictionary of dictionaries of shap.Explanation instances: - the first dimension corresponds to the n forecasts ahead we want to explain (Horizon). @@ -760,14 +759,14 @@ def _create_regression_model_shap_X( X, indexes = create_lagged_prediction_data( target_series=target_series if lags_list else None, past_covariates=past_covariates if lags_past_covariates_list else None, - future_covariates=future_covariates - if lags_future_covariates_list - else None, + future_covariates=( + future_covariates if lags_future_covariates_list else None + ), lags=lags_list, lags_past_covariates=lags_past_covariates_list if past_covariates else None, - lags_future_covariates=lags_future_covariates_list - if future_covariates - else None, + lags_future_covariates=( + lags_future_covariates_list if future_covariates else None + ), uses_static_covariates=self.model.uses_static_covariates, last_static_covariates_shape=self.model._static_covariates_shape, ) diff --git a/darts/explainability/utils.py b/darts/explainability/utils.py index 682c8d6a10..854dff786e 100644 --- a/darts/explainability/utils.py +++ b/darts/explainability/utils.py @@ -345,13 +345,18 @@ def _check_valid_input( all( [ series[idx].columns.to_list() == target_components, - past_covariates[idx].columns.to_list() == past_covariates_components - if past_covariates is not None - else True, - future_covariates[idx].columns.to_list() - == future_covariates_components - if future_covariates is not None - else True, + ( + past_covariates[idx].columns.to_list() + == past_covariates_components + if past_covariates is not None + else True + ), + ( + future_covariates[idx].columns.to_list() + == future_covariates_components + if future_covariates is not None + else True + ), ] ), "Columns names must be identical between TimeSeries list components (multi-TimeSeries).", diff --git a/darts/metrics/metrics.py b/darts/metrics/metrics.py index d016c4ea51..478752e590 100644 --- a/darts/metrics/metrics.py +++ b/darts/metrics/metrics.py @@ -46,9 +46,7 @@ def wrapper_multi_ts_support(*args, **kwargs): pred_series = ( kwargs["pred_series"] if "pred_series" in kwargs - else args[0] - if "actual_series" in kwargs - else args[1] + else args[0] if "actual_series" in kwargs else args[1] ) n_jobs = kwargs.pop("n_jobs", signature(func).parameters["n_jobs"].default) @@ -1134,7 +1132,6 @@ def rho_risk( n_jobs: int = 1, verbose: bool = False ) -> float: - """:math:`\\rho`-risk (rho-risk or quantile risk). Given a time series of actual values :math:`y_t` of length :math:`T` and a time series of stochastic predictions diff --git a/darts/models/forecasting/arima.py b/darts/models/forecasting/arima.py index f057b556ef..489cf338e4 100644 --- a/darts/models/forecasting/arima.py +++ b/darts/models/forecasting/arima.py @@ -193,17 +193,19 @@ def _predict( if series is not None: self.model = self.model.apply( series.values(copy=False), - exog=historic_future_covariates.values(copy=False) - if historic_future_covariates - else None, + exog=( + historic_future_covariates.values(copy=False) + if historic_future_covariates + else None + ), ) if num_samples == 1: forecast = self.model.forecast( steps=n, - exog=future_covariates.values(copy=False) - if future_covariates - else None, + exog=( + future_covariates.values(copy=False) if future_covariates else None + ), ) else: forecast = self.model.simulate( @@ -212,18 +214,20 @@ def _predict( initial_state=self.model.states.predicted[-1, :], random_state=self._random_state, anchor="end", - exog=future_covariates.values(copy=False) - if future_covariates - else None, + exog=( + future_covariates.values(copy=False) if future_covariates else None + ), ) # restoring statsmodels results object state if series is not None: self.model = self.model.apply( self._orig_training_series.values(copy=False), - exog=self.training_historic_future_covariates.values(copy=False) - if self.training_historic_future_covariates - else None, + exog=( + self.training_historic_future_covariates.values(copy=False) + if self.training_historic_future_covariates + else None + ), ) return self._build_forecast_series(forecast) diff --git a/darts/models/forecasting/baselines.py b/darts/models/forecasting/baselines.py index 2b370aad00..f210b4945e 100644 --- a/darts/models/forecasting/baselines.py +++ b/darts/models/forecasting/baselines.py @@ -336,12 +336,12 @@ def fit( for model in self.forecasting_models: model._fit_wrapper( series=series, - past_covariates=past_covariates - if model.supports_past_covariates - else None, - future_covariates=future_covariates - if model.supports_future_covariates - else None, + past_covariates=( + past_covariates if model.supports_past_covariates else None + ), + future_covariates=( + future_covariates if model.supports_future_covariates else None + ), ) return self @@ -364,9 +364,11 @@ def ensemble( if isinstance(predictions, Sequence): return [ - self._target_average(p, ts) - if not predict_likelihood_parameters - else self._params_average(p, ts) + ( + self._target_average(p, ts) + if not predict_likelihood_parameters + else self._params_average(p, ts) + ) for p, ts in zip(predictions, series) ] else: diff --git a/darts/models/forecasting/block_rnn_model.py b/darts/models/forecasting/block_rnn_model.py index 36cf6e210d..8de8efd3f1 100644 --- a/darts/models/forecasting/block_rnn_model.py +++ b/darts/models/forecasting/block_rnn_model.py @@ -106,7 +106,6 @@ def __init__( name: str, **kwargs, ): - """PyTorch module implementing a block RNN to be used in `BlockRNNModel`. PyTorch module implementing a simple block RNN with the specified `name` layer. @@ -196,7 +195,6 @@ def __init__( dropout: float = 0.0, **kwargs, ): - """Block Recurrent Neural Network Model (RNNs). This is a neural network model that uses an RNN encoder to encode fixed-length input chunks, and diff --git a/darts/models/forecasting/catboost_model.py b/darts/models/forecasting/catboost_model.py index fbb8e3df7d..5adc8d0bc7 100644 --- a/darts/models/forecasting/catboost_model.py +++ b/darts/models/forecasting/catboost_model.py @@ -305,7 +305,9 @@ def min_train_series_length(self) -> int: # for other regression models return max( 3, - -self.lags["target"][0] + self.output_chunk_length + 1 - if "target" in self.lags - else self.output_chunk_length, + ( + -self.lags["target"][0] + self.output_chunk_length + 1 + if "target" in self.lags + else self.output_chunk_length + ), ) diff --git a/darts/models/forecasting/croston.py b/darts/models/forecasting/croston.py index d71aaf2b29..4737aec4b7 100644 --- a/darts/models/forecasting/croston.py +++ b/darts/models/forecasting/croston.py @@ -130,9 +130,11 @@ def _fit(self, series: TimeSeries, future_covariates: Optional[TimeSeries] = Non self.model.fit( y=series.values(copy=False).flatten(), - X=future_covariates.values(copy=False).flatten() - if future_covariates is not None - else None, + X=( + future_covariates.values(copy=False).flatten() + if future_covariates is not None + else None + ), ) return self @@ -147,9 +149,11 @@ def _predict( super()._predict(n, future_covariates, num_samples) values = self.model.predict( h=n, - X=future_covariates.values(copy=False).flatten() - if future_covariates is not None - else None, + X=( + future_covariates.values(copy=False).flatten() + if future_covariates is not None + else None + ), )["mean"] return self._build_forecast_series(values) diff --git a/darts/models/forecasting/ensemble_model.py b/darts/models/forecasting/ensemble_model.py index b772594d1e..e9f6e87175 100644 --- a/darts/models/forecasting/ensemble_model.py +++ b/darts/models/forecasting/ensemble_model.py @@ -255,12 +255,12 @@ def _make_multiple_predictions( model._predict_wrapper( n=n, series=series, - past_covariates=past_covariates - if model.supports_past_covariates - else None, - future_covariates=future_covariates - if model.supports_future_covariates - else None, + past_covariates=( + past_covariates if model.supports_past_covariates else None + ), + future_covariates=( + future_covariates if model.supports_future_covariates else None + ), num_samples=num_samples if model._is_probabilistic else 1, predict_likelihood_parameters=predict_likelihood_parameters, ) diff --git a/darts/models/forecasting/exponential_smoothing.py b/darts/models/forecasting/exponential_smoothing.py index f535eb0d03..217e5485d7 100644 --- a/darts/models/forecasting/exponential_smoothing.py +++ b/darts/models/forecasting/exponential_smoothing.py @@ -27,7 +27,6 @@ def __init__( kwargs: Optional[Dict[str, Any]] = None, **fit_kwargs ): - """Exponential Smoothing This is a wrapper around diff --git a/darts/models/forecasting/fft.py b/darts/models/forecasting/fft.py index 490210ac69..6dada42a66 100644 --- a/darts/models/forecasting/fft.py +++ b/darts/models/forecasting/fft.py @@ -105,7 +105,7 @@ def _find_relevant_timestamp_attributes(series: TimeSeries) -> set: # check for yearly seasonality if _check_approximate_seasonality(series, 12, 1, 0): relevant_attributes.add("month") - elif type(series.freq) == pd.tseries.offsets.Day: + elif type(series.freq) is pd.tseries.offsets.Day: # check for yearly seasonality if _check_approximate_seasonality(series, 365, 5, 20): relevant_attributes.update({"month", "day"}) @@ -115,7 +115,7 @@ def _find_relevant_timestamp_attributes(series: TimeSeries) -> set: # check for weekly seasonality elif _check_approximate_seasonality(series, 7, 0, 0): relevant_attributes.add("weekday") - elif type(series.freq) == pd.tseries.offsets.Hour: + elif type(series.freq) is pd.tseries.offsets.Hour: # check for yearly seasonality if _check_approximate_seasonality(series, 8760, 100, 100): relevant_attributes.update({"month", "day", "hour"}) @@ -128,7 +128,7 @@ def _find_relevant_timestamp_attributes(series: TimeSeries) -> set: # check for daily seasonality elif _check_approximate_seasonality(series, 24, 1, 1): relevant_attributes.add("hour") - elif type(series.freq) == pd.tseries.offsets.Minute: + elif type(series.freq) is pd.tseries.offsets.Minute: # check for daily seasonality if _check_approximate_seasonality(series, 1440, 20, 50): relevant_attributes.update({"hour", "minute"}) diff --git a/darts/models/forecasting/forecasting_model.py b/darts/models/forecasting/forecasting_model.py index f1ab933b05..8d05e5bbce 100644 --- a/darts/models/forecasting/forecasting_model.py +++ b/darts/models/forecasting/forecasting_model.py @@ -11,6 +11,7 @@ one or several time series. The function `predict()` applies `f()` on one or several time series in order to obtain forecasts for a desired number of time stamps into the future. """ + import copy import datetime import inspect @@ -1115,15 +1116,21 @@ def retrain_func( freq=series_.freq * stride, ), np.array(last_points_values), - columns=forecast_components - if forecast_components is not None - else series_.columns, - static_covariates=series_.static_covariates - if not predict_likelihood_parameters - else None, - hierarchy=series_.hierarchy - if not predict_likelihood_parameters - else None, + columns=( + forecast_components + if forecast_components is not None + else series_.columns + ), + static_covariates=( + series_.static_covariates + if not predict_likelihood_parameters + else None + ), + hierarchy=( + series_.hierarchy + if not predict_likelihood_parameters + else None + ), ) ) else: @@ -1313,9 +1320,11 @@ def backtest( backtest_list.append(errors) else: errors = [ - [metric_f(target_ts, f) for metric_f in metric] - if len(metric) > 1 - else metric[0](target_ts, f) + ( + [metric_f(target_ts, f) for metric_f in metric] + if len(metric) > 1 + else metric[0](target_ts, f) + ) for f in forecasts[idx] ] @@ -1522,9 +1531,9 @@ def _evaluate_combination(param_combination) -> float: ) if param_combination_dict.get("model_name", None): current_time = time.strftime("%Y-%m-%d_%H.%M.%S.%f", time.localtime()) - param_combination_dict[ - "model_name" - ] = f"{current_time}_{param_combination_dict['model_name']}" + param_combination_dict["model_name"] = ( + f"{current_time}_{param_combination_dict['model_name']}" + ) model = model_class(**param_combination_dict) if use_fitted_values: # fitted value mode diff --git a/darts/models/forecasting/lgbm.py b/darts/models/forecasting/lgbm.py index 4a3d748719..2a320686ce 100644 --- a/darts/models/forecasting/lgbm.py +++ b/darts/models/forecasting/lgbm.py @@ -305,7 +305,9 @@ def min_train_series_length(self) -> int: # for other regression models return max( 3, - -self.lags["target"][0] + self.output_chunk_length + 1 - if "target" in self.lags - else self.output_chunk_length, + ( + -self.lags["target"][0] + self.output_chunk_length + 1 + if "target" in self.lags + else self.output_chunk_length + ), ) diff --git a/darts/models/forecasting/linear_regression_model.py b/darts/models/forecasting/linear_regression_model.py index e599dd017b..e09487e192 100644 --- a/darts/models/forecasting/linear_regression_model.py +++ b/darts/models/forecasting/linear_regression_model.py @@ -5,6 +5,7 @@ A forecasting model using a linear regression of some of the target series' lags, as well as optionally some covariate series lags in order to obtain a forecast. """ + from typing import List, Optional, Sequence, Union import numpy as np diff --git a/darts/models/forecasting/pl_forecasting_module.py b/darts/models/forecasting/pl_forecasting_module.py index 5610a2d3df..53ebf62da6 100644 --- a/darts/models/forecasting/pl_forecasting_module.py +++ b/darts/models/forecasting/pl_forecasting_module.py @@ -313,9 +313,11 @@ def predict_step( delayed(_build_forecast_series)( [batch_prediction[batch_idx] for batch_prediction in batch_predictions], input_series, - custom_columns=self.likelihood.likelihood_components_names(input_series) - if self.predict_likelihood_parameters - else None, + custom_columns=( + self.likelihood.likelihood_components_names(input_series) + if self.predict_likelihood_parameters + else None + ), with_static_covs=False if self.predict_likelihood_parameters else True, with_hierarchy=False if self.predict_likelihood_parameters else True, pred_start=pred_start, @@ -577,9 +579,11 @@ def _produce_train_output(self, input_batch: Tuple): past_target, past_covariates, static_covariates = input_batch # Currently all our PastCovariates models require past target and covariates concatenated inpt = ( - torch.cat([past_target, past_covariates], dim=2) - if past_covariates is not None - else past_target, + ( + torch.cat([past_target, past_covariates], dim=2) + if past_covariates is not None + else past_target + ), static_covariates, ) return self(inpt) @@ -659,13 +663,13 @@ def _get_batch_prediction( # update past covariates to include next `roll_size` future past covariates elements if n_past_covs and self.input_chunk_length >= roll_size: - input_past[ - :, -roll_size:, n_targets : n_targets + n_past_covs - ] = future_past_covariates[:, left_past:right_past, :] + input_past[:, -roll_size:, n_targets : n_targets + n_past_covs] = ( + future_past_covariates[:, left_past:right_past, :] + ) elif n_past_covs: - input_past[ - :, :, n_targets : n_targets + n_past_covs - ] = future_past_covariates[:, left_past:right_past, :] + input_past[:, :, n_targets : n_targets + n_past_covs] = ( + future_past_covariates[:, left_past:right_past, :] + ) # take only last part of the output sequence where needed out = self._produce_predict_output(x=(input_past, static_covariates))[ @@ -795,9 +799,11 @@ def _get_batch_prediction( past_target, past_covariates, historic_future_covariates, - future_covariates[:, :roll_size, :] - if future_covariates is not None - else None, + ( + future_covariates[:, :roll_size, :] + if future_covariates is not None + else None + ), static_covariates, ) ) @@ -842,19 +848,19 @@ def _get_batch_prediction( # update past covariates to include next `roll_size` future past covariates elements if n_past_covs and self.input_chunk_length >= roll_size: - input_past[ - :, -roll_size:, n_targets : n_targets + n_past_covs - ] = future_past_covariates[:, left_past:right_past, :] + input_past[:, -roll_size:, n_targets : n_targets + n_past_covs] = ( + future_past_covariates[:, left_past:right_past, :] + ) elif n_past_covs: - input_past[ - :, :, n_targets : n_targets + n_past_covs - ] = future_past_covariates[:, left_past:right_past, :] + input_past[:, :, n_targets : n_targets + n_past_covs] = ( + future_past_covariates[:, left_past:right_past, :] + ) # update historic future covariates to include next `roll_size` future covariates elements if n_future_covs and self.input_chunk_length >= roll_size: - input_past[ - :, -roll_size:, n_targets + n_past_covs : - ] = future_covariates[:, left_past:right_past, :] + input_past[:, -roll_size:, n_targets + n_past_covs :] = ( + future_covariates[:, left_past:right_past, :] + ) elif n_future_covs: input_past[:, :, n_targets + n_past_covs :] = future_covariates[ :, left_past:right_past, : diff --git a/darts/models/forecasting/random_forest.py b/darts/models/forecasting/random_forest.py index 34cee5f38f..0f1def2a64 100644 --- a/darts/models/forecasting/random_forest.py +++ b/darts/models/forecasting/random_forest.py @@ -14,6 +14,7 @@ ---------- .. [1] https://en.wikipedia.org/wiki/Random_forest """ + from typing import Optional from sklearn.ensemble import RandomForestRegressor diff --git a/darts/models/forecasting/regression_ensemble_model.py b/darts/models/forecasting/regression_ensemble_model.py index b55170aede..eee2f50770 100644 --- a/darts/models/forecasting/regression_ensemble_model.py +++ b/darts/models/forecasting/regression_ensemble_model.py @@ -4,6 +4,7 @@ An ensemble model which uses a regression model to compute the ensemble forecast. """ + from typing import List, Optional, Sequence, Tuple, Union from darts.logging import get_logger, raise_if, raise_if_not @@ -213,12 +214,12 @@ def _make_multiple_historical_forecasts( tmp_pred = model.historical_forecasts( series=series, - past_covariates=past_covariates - if model.supports_past_covariates - else None, - future_covariates=future_covariates - if model.supports_future_covariates - else None, + past_covariates=( + past_covariates if model.supports_past_covariates else None + ), + future_covariates=( + future_covariates if model.supports_future_covariates else None + ), forecast_horizon=model.output_chunk_length, stride=model.output_chunk_length, num_samples=num_samples if model._is_probabilistic else 1, @@ -374,12 +375,12 @@ def fit( # maximize covariate usage model._fit_wrapper( series=forecast_training, - past_covariates=past_covariates - if model.supports_past_covariates - else None, - future_covariates=future_covariates - if model.supports_future_covariates - else None, + past_covariates=( + past_covariates if model.supports_past_covariates else None + ), + future_covariates=( + future_covariates if model.supports_future_covariates else None + ), ) # we can call direct prediction in any case. Even if we overwrite with historical @@ -416,12 +417,12 @@ def fit( for model in self.forecasting_models: model._fit_wrapper( series=series, - past_covariates=past_covariates - if model.supports_past_covariates - else None, - future_covariates=future_covariates - if model.supports_future_covariates - else None, + past_covariates=( + past_covariates if model.supports_past_covariates else None + ), + future_covariates=( + future_covariates if model.supports_future_covariates else None + ), ) return self diff --git a/darts/models/forecasting/regression_model.py b/darts/models/forecasting/regression_model.py index 47fd5d2b92..54f0eb9a1b 100644 --- a/darts/models/forecasting/regression_model.py +++ b/darts/models/forecasting/regression_model.py @@ -26,6 +26,7 @@ When static covariates are present, they are appended to the lagged features. When multiple time series are passed, if their static covariates do not have the same size, the shorter ones are padded with 0 valued features. """ + from collections import OrderedDict from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Union @@ -448,9 +449,11 @@ def supports_multivariate(self) -> bool: def min_train_series_length(self) -> int: return max( 3, - -self.lags["target"][0] + self.output_chunk_length - if "target" in self.lags - else self.output_chunk_length, + ( + -self.lags["target"][0] + self.output_chunk_length + if "target" in self.lags + else self.output_chunk_length + ), ) @property @@ -716,9 +719,11 @@ def fit( else: # reorder the components based on the input series, insert the default when necessary self.component_lags[variate_type] = { - comp_name: self.component_lags[variate_type][comp_name] - if comp_name in self.component_lags[variate_type] - else self.component_lags[variate_type]["default_lags"] + comp_name: ( + self.component_lags[variate_type][comp_name] + if comp_name in self.component_lags[variate_type] + else self.component_lags[variate_type]["default_lags"] + ) for comp_name in variate[0].components } @@ -1022,9 +1027,11 @@ def predict( self._build_forecast_series( points_preds=row, input_series=input_tgt, - custom_components=self._likelihood_components_names(input_tgt) - if predict_likelihood_parameters - else None, + custom_components=( + self._likelihood_components_names(input_tgt) + if predict_likelihood_parameters + else None + ), with_static_covs=False if predict_likelihood_parameters else True, with_hierarchy=False if predict_likelihood_parameters else True, ) diff --git a/darts/models/forecasting/rnn_model.py b/darts/models/forecasting/rnn_model.py index 16ef18015e..7e13231772 100644 --- a/darts/models/forecasting/rnn_model.py +++ b/darts/models/forecasting/rnn_model.py @@ -111,9 +111,11 @@ def _produce_train_output(self, input_batch: Tuple) -> torch.Tensor: # For the RNN we concatenate the past_target with the future_covariates # (they have the same length because we enforce a Shift dataset for RNNs) model_input = ( - torch.cat([past_target, future_covariates], dim=2) - if future_covariates is not None - else past_target, + ( + torch.cat([past_target, future_covariates], dim=2) + if future_covariates is not None + else past_target + ), static_covariates, ) return self(model_input)[0] @@ -278,7 +280,6 @@ def __init__( training_length: int = 24, **kwargs, ): - """Recurrent Neural Network Model (RNNs). This class provides three variants of RNNs: diff --git a/darts/models/forecasting/tbats_model.py b/darts/models/forecasting/tbats_model.py index eb251726d3..ec1cc62cfd 100644 --- a/darts/models/forecasting/tbats_model.py +++ b/darts/models/forecasting/tbats_model.py @@ -125,7 +125,6 @@ def __init__( multiprocessing_start_method: Optional[str] = "spawn", random_state: int = 0, ): - """ This is a wrapper around `tbats diff --git a/darts/models/forecasting/tcn_model.py b/darts/models/forecasting/tcn_model.py index e93f5b86cf..7f4bbdc9a0 100644 --- a/darts/models/forecasting/tcn_model.py +++ b/darts/models/forecasting/tcn_model.py @@ -143,7 +143,6 @@ def __init__( dropout: float, **kwargs ): - """PyTorch module implementing a dilated TCN module used in `TCNModel`. @@ -269,7 +268,6 @@ def __init__( dropout: float = 0.2, **kwargs ): - """Temporal Convolutional Network Model (TCN). This is an implementation of a dilated TCN used for forecasting, inspired from [1]_. diff --git a/darts/models/forecasting/tft_model.py b/darts/models/forecasting/tft_model.py index baca30f71c..555621f280 100644 --- a/darts/models/forecasting/tft_model.py +++ b/darts/models/forecasting/tft_model.py @@ -60,7 +60,6 @@ def __init__( norm_type: Union[str, nn.Module], **kwargs, ): - """PyTorch module implementing the TFT architecture from `this paper `_ The implementation is built upon `pytorch-forecasting's TemporalFusionTransformer `_. @@ -158,9 +157,11 @@ def __init__( # continuous variable processing self.prescalers_linear = { name: nn.Linear( - 1 - if name not in self.numeric_static_variables - else self.num_static_components, + ( + 1 + if name not in self.numeric_static_variables + else self.num_static_components + ), self.hidden_continuous_size, ) for name in self.reals diff --git a/darts/models/forecasting/tide_model.py b/darts/models/forecasting/tide_model.py index 14b942c76b..460a81a8ab 100644 --- a/darts/models/forecasting/tide_model.py +++ b/darts/models/forecasting/tide_model.py @@ -337,9 +337,11 @@ def forward( # stack and temporally decode with future covariate last output steps temporal_decoder_input = [ decoded, - x_dynamic_future_covariates[:, -self.output_chunk_length :, :] - if self.future_cov_dim > 0 - else None, + ( + x_dynamic_future_covariates[:, -self.output_chunk_length :, :] + if self.future_cov_dim > 0 + else None + ), ] temporal_decoder_input = [t for t in temporal_decoder_input if t is not None] diff --git a/darts/models/forecasting/torch_forecasting_model.py b/darts/models/forecasting/torch_forecasting_model.py index fe0c67c364..a54cecfb01 100644 --- a/darts/models/forecasting/torch_forecasting_model.py +++ b/darts/models/forecasting/torch_forecasting_model.py @@ -2098,7 +2098,7 @@ def _load_encoders( # transformers are equal if they are instances of the same class self_transformer = self.add_encoders.get("transformer", None) tfm_transformer = tfm_save.add_encoders.get("transformer", None) - same_transformer = type(self_transformer) == type(tfm_transformer) + same_transformer = type(self_transformer) is type(tfm_transformer) # encoders are equal if they have the same entries (transformer excluded) self_encoders = { diff --git a/darts/models/forecasting/transformer_model.py b/darts/models/forecasting/transformer_model.py index d68f30e2fa..3ec83dfb36 100644 --- a/darts/models/forecasting/transformer_model.py +++ b/darts/models/forecasting/transformer_model.py @@ -339,7 +339,6 @@ def __init__( custom_decoder: Optional[nn.Module] = None, **kwargs, ): - """Transformer model Transformer is a state-of-the-art deep learning model introduced in 2017. It is an encoder-decoder diff --git a/darts/models/forecasting/varima.py b/darts/models/forecasting/varima.py index 7e49df4fa7..3b6e4e9c05 100644 --- a/darts/models/forecasting/varima.py +++ b/darts/models/forecasting/varima.py @@ -9,6 +9,7 @@ ---------- .. [1] https://en.wikipedia.org/wiki/Vector_autoregression """ + from typing import Optional import numpy as np @@ -191,27 +192,29 @@ def _predict( self.model = self.model.apply( series.values(copy=False), - exog=historic_future_covariates.values(copy=False) - if historic_future_covariates - else None, + exog=( + historic_future_covariates.values(copy=False) + if historic_future_covariates + else None + ), ) # forecast before restoring the training state if num_samples == 1: forecast = self.model.forecast( steps=n, - exog=future_covariates.values(copy=False) - if future_covariates - else None, + exog=( + future_covariates.values(copy=False) if future_covariates else None + ), ) else: forecast = self.model.simulate( nsimulations=n, repetitions=num_samples, initial_state=self.model.states.predicted[-1, :], - exog=future_covariates.values(copy=False) - if future_covariates - else None, + exog=( + future_covariates.values(copy=False) if future_covariates else None + ), ) forecast = self._invert_transformation(forecast) @@ -220,9 +223,11 @@ def _predict( if series is not None: self.model = self.model.apply( self._orig_training_series.values(copy=False), - exog=self.training_historic_future_covariates.values(copy=False) - if self.training_historic_future_covariates - else None, + exog=( + self.training_historic_future_covariates.values(copy=False) + if self.training_historic_future_covariates + else None + ), ) self._last_values = self._training_last_values diff --git a/darts/models/forecasting/xgboost.py b/darts/models/forecasting/xgboost.py index 246e68c17a..2e484e0af4 100644 --- a/darts/models/forecasting/xgboost.py +++ b/darts/models/forecasting/xgboost.py @@ -324,7 +324,9 @@ def min_train_series_length(self) -> int: # more than for other regression models return max( 3, - -self.lags["target"][0] + self.output_chunk_length + 1 - if "target" in self.lags - else self.output_chunk_length, + ( + -self.lags["target"][0] + self.output_chunk_length + 1 + if "target" in self.lags + else self.output_chunk_length + ), ) diff --git a/darts/tests/dataprocessing/transformers/test_diff.py b/darts/tests/dataprocessing/transformers/test_diff.py index 83634ab511..3fb9aae609 100644 --- a/darts/tests/dataprocessing/transformers/test_diff.py +++ b/darts/tests/dataprocessing/transformers/test_diff.py @@ -27,7 +27,6 @@ def assert_series_equal( equal_nan: bool, to_compare: Optional[np.ndarray] = None, ): - """ Helper to compare series differenced by `Diff`. @@ -97,7 +96,7 @@ def test_diff_inverse_transform_beyond_fit_data(self): # Artifically truncate series: short_sine = self.sine_series.copy().drop_after(10) - for (lags, dropna) in test_cases: + for lags, dropna in test_cases: # Fit Diff to truncated series: diff = Diff(lags=lags, dropna=dropna) diff.fit(short_sine) @@ -133,7 +132,7 @@ def test_diff_multi_ts(self): (1, False, component_mask), ([1, 2, 3, 2, 1], False, component_mask), ] - for (lags, dropna, mask) in test_cases: + for lags, dropna, mask in test_cases: diff = Diff(lags=lags, dropna=dropna) transformed = diff.fit_transform( [self.sine_series, self.sine_series], component_mask=mask @@ -172,7 +171,7 @@ def test_diff_stochastic_series(self): vals = np.random.rand(10, 5, 10) series = TimeSeries.from_values(vals) - for (lags, dropna) in test_cases: + for lags, dropna in test_cases: transformer = Diff(lags=lags, dropna=dropna) new_series = transformer.fit_transform(series) series_back = transformer.inverse_transform(new_series) diff --git a/darts/tests/datasets/test_datasets.py b/darts/tests/datasets/test_datasets.py index 24260f337d..45ed8bef39 100644 --- a/darts/tests/datasets/test_datasets.py +++ b/darts/tests/datasets/test_datasets.py @@ -52,7 +52,7 @@ def _assert_eq(self, lefts: tuple, rights: tuple): for left, right in zip(lefts, rights): left = left.values() if isinstance(left, TimeSeries) else left right = right.values() if isinstance(right, TimeSeries) else right - assert type(left) == type(right) + assert type(left) is type(right) assert ( isinstance( left, (TimeSeries, pd.Series, pd.DataFrame, np.ndarray, list) diff --git a/darts/tests/models/forecasting/test_dlinear_nlinear.py b/darts/tests/models/forecasting/test_dlinear_nlinear.py index 7348603626..ebac942bab 100644 --- a/darts/tests/models/forecasting/test_dlinear_nlinear.py +++ b/darts/tests/models/forecasting/test_dlinear_nlinear.py @@ -52,7 +52,7 @@ def test_fit(self): large_ts = tg.constant_timeseries(length=100, value=1000) small_ts = tg.constant_timeseries(length=100, value=10) - for (model_cls, kwargs) in [ + for model_cls, kwargs in [ (DLinearModel, {"kernel_size": 5}), (DLinearModel, {"kernel_size": 6}), (NLinearModel, {}), @@ -194,26 +194,28 @@ def _eval_model( model.fit( [train1, train2], - past_covariates=[past_cov1, past_cov2] - if past_cov1 is not None - else None, - val_past_covariates=[val_past_cov1, val_past_cov2] - if val_past_cov1 is not None - else None, - future_covariates=[fut_cov1, fut_cov2] - if fut_cov1 is not None - else None, + past_covariates=( + [past_cov1, past_cov2] if past_cov1 is not None else None + ), + val_past_covariates=( + [val_past_cov1, val_past_cov2] + if val_past_cov1 is not None + else None + ), + future_covariates=( + [fut_cov1, fut_cov2] if fut_cov1 is not None else None + ), epochs=10, ) pred1, pred2 = model.predict( series=[train1, train2], - future_covariates=[fut_cov1, fut_cov2] - if fut_cov1 is not None - else None, - past_covariates=[fut_cov1, fut_cov2] - if past_cov1 is not None - else None, + future_covariates=( + [fut_cov1, fut_cov2] if fut_cov1 is not None else None + ), + past_covariates=( + [fut_cov1, fut_cov2] if past_cov1 is not None else None + ), n=len(val1), num_samples=500 if lkl is not None else 1, ) diff --git a/darts/tests/models/forecasting/test_global_forecasting_models.py b/darts/tests/models/forecasting/test_global_forecasting_models.py index cec70efb4e..1fcb2d45ca 100644 --- a/darts/tests/models/forecasting/test_global_forecasting_models.py +++ b/darts/tests/models/forecasting/test_global_forecasting_models.py @@ -484,9 +484,9 @@ def test_use_static_covariates(self, model_cls, ts): # must provide mandatory future_covariates to TFTModel model.fit( series=ts, - future_covariates=self.sine_1_ts - if model.supports_future_covariates - else None, + future_covariates=( + self.sine_1_ts if model.supports_future_covariates else None + ), ) pred = model.predict(OUT_LEN) assert pred.static_covariates.equals(ts.static_covariates) diff --git a/darts/tests/models/forecasting/test_historical_forecasts.py b/darts/tests/models/forecasting/test_historical_forecasts.py index 4f2fd6eac5..38a48156ba 100644 --- a/darts/tests/models/forecasting/test_historical_forecasts.py +++ b/darts/tests/models/forecasting/test_historical_forecasts.py @@ -348,9 +348,9 @@ def create_model(ocl, use_ll=True, model_type="regression"): return None return NLinearModel( input_chunk_length=3, - likelihood=QuantileRegression([0.05, 0.4, 0.5, 0.6, 0.95]) - if use_ll - else None, + likelihood=( + QuantileRegression([0.05, 0.4, 0.5, 0.6, 0.95]) if use_ll else None + ), output_chunk_length=ocl, n_epochs=1, random_state=42, @@ -843,9 +843,9 @@ def test_optimized_historical_forecasts_regression(self, config): model_same.fit( series=ts[:start], past_covariates=ts_covs if model_same.supports_past_covariates else None, - future_covariates=ts_covs - if model_same.supports_future_covariates - else None, + future_covariates=( + ts_covs if model_same.supports_future_covariates else None + ), ) # ocl >= forecast horizon model_kwargs_diff = model_kwargs.copy() @@ -855,9 +855,9 @@ def test_optimized_historical_forecasts_regression(self, config): model_diff.fit( series=ts[:start], past_covariates=ts_covs if model_diff.supports_past_covariates else None, - future_covariates=ts_covs - if model_diff.supports_future_covariates - else None, + future_covariates=( + ts_covs if model_diff.supports_future_covariates else None + ), ) # no parametrization to save time on model training at the cost of test granularity for model in [model_same, model_diff]: @@ -865,12 +865,12 @@ def test_optimized_historical_forecasts_regression(self, config): for stride in [1, 2]: hist_fct = model.historical_forecasts( series=ts, - past_covariates=ts_covs - if model.supports_past_covariates - else None, - future_covariates=ts_covs - if model.supports_future_covariates - else None, + past_covariates=( + ts_covs if model.supports_past_covariates else None + ), + future_covariates=( + ts_covs if model.supports_future_covariates else None + ), start=start, retrain=False, last_points_only=last_points_only, @@ -882,12 +882,12 @@ def test_optimized_historical_forecasts_regression(self, config): # manually packing the series in list to match expected inputs opti_hist_fct = model._optimized_historical_forecasts( series=[ts], - past_covariates=[ts_covs] - if model.supports_past_covariates - else None, - future_covariates=[ts_covs] - if model.supports_future_covariates - else None, + past_covariates=( + [ts_covs] if model.supports_past_covariates else None + ), + future_covariates=( + [ts_covs] if model.supports_future_covariates else None + ), start=start, last_points_only=last_points_only, stride=stride, @@ -1429,18 +1429,22 @@ def test_regression_auto_start_multiple_with_cov_retrain(self, model_config): forecasts_retrain = model.historical_forecasts( series=[self.ts_pass_val, self.ts_pass_val], - past_covariates=[ - self.ts_past_cov_valid_same_start, - self.ts_past_cov_valid_same_start, - ] - if "lags_past_covariates" in kwargs - else None, - future_covariates=[ - self.ts_past_cov_valid_same_start, - self.ts_past_cov_valid_same_start, - ] - if "lags_future_covariates" in kwargs - else None, + past_covariates=( + [ + self.ts_past_cov_valid_same_start, + self.ts_past_cov_valid_same_start, + ] + if "lags_past_covariates" in kwargs + else None + ), + future_covariates=( + [ + self.ts_past_cov_valid_same_start, + self.ts_past_cov_valid_same_start, + ] + if "lags_future_covariates" in kwargs + else None + ), last_points_only=True, forecast_horizon=forecast_hrz, stride=1, @@ -1464,9 +1468,11 @@ def test_regression_auto_start_multiple_with_cov_retrain(self, model_config): past_lag = min( min_target_lag if min_target_lag else 0, min_past_cov_lag if min_past_cov_lag else 0, - min_future_cov_lag - if min_future_cov_lag is not None and min_future_cov_lag < 0 - else 0, + ( + min_future_cov_lag + if min_future_cov_lag is not None and min_future_cov_lag < 0 + else 0 + ), ) future_lag = ( @@ -1519,33 +1525,41 @@ def test_regression_auto_start_multiple_with_cov_no_retrain(self, model_config): model.fit( series=[self.ts_pass_val, self.ts_pass_val], - past_covariates=[ - self.ts_past_cov_valid_same_start, - self.ts_past_cov_valid_same_start, - ] - if "lags_past_covariates" in kwargs - else None, - future_covariates=[ - self.ts_past_cov_valid_same_start, - self.ts_past_cov_valid_same_start, - ] - if "lags_future_covariates" in kwargs - else None, + past_covariates=( + [ + self.ts_past_cov_valid_same_start, + self.ts_past_cov_valid_same_start, + ] + if "lags_past_covariates" in kwargs + else None + ), + future_covariates=( + [ + self.ts_past_cov_valid_same_start, + self.ts_past_cov_valid_same_start, + ] + if "lags_future_covariates" in kwargs + else None + ), ) forecasts_no_retrain = model.historical_forecasts( series=[self.ts_pass_val, self.ts_pass_val], - past_covariates=[ - self.ts_past_cov_valid_same_start, - self.ts_past_cov_valid_same_start, - ] - if "lags_past_covariates" in kwargs - else None, - future_covariates=[ - self.ts_past_cov_valid_same_start, - self.ts_past_cov_valid_same_start, - ] - if "lags_future_covariates" in kwargs - else None, + past_covariates=( + [ + self.ts_past_cov_valid_same_start, + self.ts_past_cov_valid_same_start, + ] + if "lags_past_covariates" in kwargs + else None + ), + future_covariates=( + [ + self.ts_past_cov_valid_same_start, + self.ts_past_cov_valid_same_start, + ] + if "lags_future_covariates" in kwargs + else None + ), last_points_only=True, forecast_horizon=forecast_hrz, stride=1, diff --git a/darts/tests/models/forecasting/test_regression_ensemble_model.py b/darts/tests/models/forecasting/test_regression_ensemble_model.py index 258b1a1507..979e767bb7 100644 --- a/darts/tests/models/forecasting/test_regression_ensemble_model.py +++ b/darts/tests/models/forecasting/test_regression_ensemble_model.py @@ -885,8 +885,8 @@ def test_predict_likelihood_parameters_multivariate_regression_ensemble(self): ) and all(pred_ens["linear_q0.50"].values() < pred_ens["linear_q0.95"].values()) def test_wrong_model_creation_params(self): - """Since `multi_models=False` requires to shift the regression model lags in the past (outside of the forecasting - model predictions), it is not supported.""" + """Since `multi_models=False` requires to shift the regression model lags in the past (outside of the + forecasting model predictions), it is not supported.""" forcasting_models = [ self.get_deterministic_global_model(2), self.get_deterministic_global_model([-5, -7]), diff --git a/darts/tests/models/forecasting/test_regression_models.py b/darts/tests/models/forecasting/test_regression_models.py index 9d5c369526..85cff2aca6 100644 --- a/darts/tests/models/forecasting/test_regression_models.py +++ b/darts/tests/models/forecasting/test_regression_models.py @@ -1580,9 +1580,11 @@ def test_not_enough_covariates(self, config): @pytest.mark.skipif(not lgbm_available, reason="requires lightgbm") @patch.object( - darts.models.forecasting.lgbm.lgb.LGBMRegressor - if lgbm_available - else darts.models.utils.NotImportedModule, + ( + darts.models.forecasting.lgbm.lgb.LGBMRegressor + if lgbm_available + else darts.models.utils.NotImportedModule + ), "fit", ) def test_gradient_boosted_model_with_eval_set(self, lgb_fit_patch): @@ -1745,22 +1747,30 @@ def test_component_specific_lags_forecasts(self, config): pred = model.predict( 1, series=series[0] if multiple_series else None, - past_covariates=past_cov[0] - if multiple_series and model.supports_past_covariates - else None, - future_covariates=future_cov[0] - if multiple_series and model.supports_future_covariates - else None, + past_covariates=( + past_cov[0] + if multiple_series and model.supports_past_covariates + else None + ), + future_covariates=( + future_cov[0] + if multiple_series and model.supports_future_covariates + else None + ), ) pred2 = model2.predict( 1, series=series[0] if multiple_series else None, - past_covariates=past_cov[0] - if multiple_series and model2.supports_past_covariates - else None, - future_covariates=future_cov[0] - if multiple_series and model2.supports_future_covariates - else None, + past_covariates=( + past_cov[0] + if multiple_series and model2.supports_past_covariates + else None + ), + future_covariates=( + future_cov[0] + if multiple_series and model2.supports_future_covariates + else None + ), ) np.testing.assert_array_almost_equal(pred.values(), pred2.values()) assert pred.time_index.equals(pred2.time_index) @@ -1769,22 +1779,30 @@ def test_component_specific_lags_forecasts(self, config): pred = model.predict( 3, series=series[0] if multiple_series else None, - past_covariates=past_cov[0] - if multiple_series and model.supports_past_covariates - else None, - future_covariates=future_cov[0] - if multiple_series and model.supports_future_covariates - else None, + past_covariates=( + past_cov[0] + if multiple_series and model.supports_past_covariates + else None + ), + future_covariates=( + future_cov[0] + if multiple_series and model.supports_future_covariates + else None + ), ) pred2 = model2.predict( 3, series=series[0] if multiple_series else None, - past_covariates=past_cov[0] - if multiple_series and model2.supports_past_covariates - else None, - future_covariates=future_cov[0] - if multiple_series and model2.supports_future_covariates - else None, + past_covariates=( + past_cov[0] + if multiple_series and model2.supports_past_covariates + else None + ), + future_covariates=( + future_cov[0] + if multiple_series and model2.supports_future_covariates + else None + ), ) np.testing.assert_array_almost_equal(pred.values(), pred2.values()) assert pred.time_index.equals(pred2.time_index) @@ -1863,24 +1881,32 @@ def test_component_specific_lags(self, config): model.predict( 1, series=series[0] if multiple_series else None, - past_covariates=past_cov[0] - if multiple_series and model.supports_past_covariates - else None, - future_covariates=future_cov[0] - if multiple_series and model.supports_future_covariates - else None, + past_covariates=( + past_cov[0] + if multiple_series and model.supports_past_covariates + else None + ), + future_covariates=( + future_cov[0] + if multiple_series and model.supports_future_covariates + else None + ), ) # n > output_chunk_length model.predict( 7, series=series[0] if multiple_series else None, - past_covariates=past_cov[0] - if multiple_series and model.supports_past_covariates - else None, - future_covariates=future_cov[0] - if multiple_series and model.supports_future_covariates - else None, + past_covariates=( + past_cov[0] + if multiple_series and model.supports_past_covariates + else None + ), + future_covariates=( + future_cov[0] + if multiple_series and model.supports_future_covariates + else None + ), ) @pytest.mark.parametrize( @@ -2250,9 +2276,11 @@ def helper_test_encoders_settings(model, example: str): @pytest.mark.skipif(not cb_available, reason="requires catboost") @patch.object( - darts.models.forecasting.catboost_model.CatBoostRegressor - if cb_available - else darts.models.utils.NotImportedModule, + ( + darts.models.forecasting.catboost_model.CatBoostRegressor + if cb_available + else darts.models.utils.NotImportedModule + ), "fit", ) def test_catboost_model_with_eval_set(self, lgb_fit_patch): @@ -2350,32 +2378,37 @@ def get_model_params(): @pytest.mark.skipif(not lgbm_available, reason="requires lightgbm") @pytest.mark.parametrize( "model", - [ - LightGBMModel( - lags=1, - lags_past_covariates=1, - output_chunk_length=1, - categorical_past_covariates=["does_not_exist", "past_cov_cat_dummy"], - categorical_static_covariates=["product_id"], - ), - LightGBMModel( - lags=1, - lags_past_covariates=1, - output_chunk_length=1, - categorical_past_covariates=[ - "past_cov_cat_dummy", - ], - categorical_static_covariates=["does_not_exist"], - ), - LightGBMModel( - lags=1, - lags_past_covariates=1, - output_chunk_length=1, - categorical_future_covariates=["does_not_exist"], - ), - ] - if lgbm_available - else [], + ( + [ + LightGBMModel( + lags=1, + lags_past_covariates=1, + output_chunk_length=1, + categorical_past_covariates=[ + "does_not_exist", + "past_cov_cat_dummy", + ], + categorical_static_covariates=["product_id"], + ), + LightGBMModel( + lags=1, + lags_past_covariates=1, + output_chunk_length=1, + categorical_past_covariates=[ + "past_cov_cat_dummy", + ], + categorical_static_covariates=["does_not_exist"], + ), + LightGBMModel( + lags=1, + lags_past_covariates=1, + output_chunk_length=1, + categorical_future_covariates=["does_not_exist"], + ), + ] + if lgbm_available + else [] + ), ) def test_fit_with_categorical_features_raises_error(self, model): ( @@ -2415,9 +2448,11 @@ def test_get_categorical_features_helper(self): @pytest.mark.skipif(not lgbm_available, reason="requires lightgbm") @patch.object( - darts.models.forecasting.lgbm.lgb.LGBMRegressor - if lgbm_available - else darts.models.utils.NotImportedModule, + ( + darts.models.forecasting.lgbm.lgb.LGBMRegressor + if lgbm_available + else darts.models.utils.NotImportedModule + ), "fit", ) def test_lgbm_categorical_features_passed_to_fit_correctly(self, lgb_fit_patch): diff --git a/darts/tests/test_timeseries.py b/darts/tests/test_timeseries.py index 31f2a5fa02..edefc2fe9d 100644 --- a/darts/tests/test_timeseries.py +++ b/darts/tests/test_timeseries.py @@ -2103,7 +2103,7 @@ def test_time_col_convert_rangeindex(self): ts = TimeSeries.from_dataframe(df=df, time_col="Time") # check type (should convert to RangeIndex): - assert type(ts.time_index) == pd.RangeIndex + assert type(ts.time_index) is pd.RangeIndex # check values inside the index (should be sorted correctly): assert list(ts.time_index) == sorted(expected) diff --git a/darts/tests/utils/tabularization/test_create_lagged_prediction_data.py b/darts/tests/utils/tabularization/test_create_lagged_prediction_data.py index 4bff71fbe9..e00b76bebf 100644 --- a/darts/tests/utils/tabularization/test_create_lagged_prediction_data.py +++ b/darts/tests/utils/tabularization/test_create_lagged_prediction_data.py @@ -356,7 +356,7 @@ def test_lagged_prediction_data_equal_freq_range_index(self): n_components=4, start_value=20, end_value=30, start=6, end=26, freq=3 ) # Conduct test for each input parameter combo: - for (lags, lags_past, lags_future, max_samples_per_ts) in product( + for lags, lags_past, lags_future, max_samples_per_ts in product( self.target_lag_combos, self.past_lag_combos, self.future_lag_combos, @@ -444,7 +444,7 @@ def test_lagged_prediction_data_equal_freq_datetime_index(self): freq="2d", ) # Conduct test for each input parameter combo: - for (lags, lags_past, lags_future, max_samples_per_ts) in product( + for lags, lags_past, lags_future, max_samples_per_ts in product( self.target_lag_combos, self.past_lag_combos, self.future_lag_combos, @@ -517,7 +517,7 @@ def test_lagged_prediction_data_unequal_freq_range_index(self): n_components=4, start_value=20, end_value=30, start=6, end=26, freq=3 ) # Conduct test for each input parameter combo: - for (lags, lags_past, lags_future, max_samples_per_ts) in product( + for lags, lags_past, lags_future, max_samples_per_ts in product( self.target_lag_combos, self.past_lag_combos, self.future_lag_combos, @@ -590,7 +590,7 @@ def test_lagged_prediction_data_unequal_freq_datetime_index(self): n_components=4, start_value=20, end_value=30, start=6, end=26, freq=3 ) # Conduct test for each input parameter combo: - for (lags, lags_past, lags_future, max_samples_per_ts) in product( + for lags, lags_past, lags_future, max_samples_per_ts in product( self.target_lag_combos, self.past_lag_combos, self.future_lag_combos, @@ -678,7 +678,7 @@ def test_lagged_prediction_data_method_consistency_range_index(self): freq="2d", ) # Conduct test for each input parameter combo: - for (lags, lags_past, lags_future, max_samples_per_ts) in product( + for lags, lags_past, lags_future, max_samples_per_ts in product( self.target_lag_combos, self.past_lag_combos, self.future_lag_combos, @@ -758,7 +758,7 @@ def test_lagged_prediction_data_method_consistency_datetime_index(self): freq="2d", ) # Conduct test for each input parameter combo: - for (lags, lags_past, lags_future, max_samples_per_ts) in product( + for lags, lags_past, lags_future, max_samples_per_ts in product( self.target_lag_combos, self.past_lag_combos, self.future_lag_combos, diff --git a/darts/tests/utils/tabularization/test_create_lagged_training_data.py b/darts/tests/utils/tabularization/test_create_lagged_training_data.py index 9afe53d3f1..ff4d32444d 100644 --- a/darts/tests/utils/tabularization/test_create_lagged_training_data.py +++ b/darts/tests/utils/tabularization/test_create_lagged_training_data.py @@ -1220,7 +1220,7 @@ def test_lagged_training_data_single_point_range_idx(self): expected_y = np.ones((1, 1, 1)) # Test correctness for 'moving window' and for 'time intersection' methods, as well # as for different `multi_models` values: - for (use_moving_windows, multi_models) in product([False, True], [False, True]): + for use_moving_windows, multi_models in product([False, True], [False, True]): X, y, times, _ = create_lagged_training_data( target, output_chunk_length, @@ -1252,7 +1252,7 @@ def test_lagged_training_data_single_point_datetime_idx(self): expected_y = np.ones((1, 1, 1)) # Test correctness for 'moving window' and for 'time intersection' methods, as well # as for different `multi_models` values: - for (use_moving_windows, multi_models) in product([False, True], [False, True]): + for use_moving_windows, multi_models in product([False, True], [False, True]): X, y, times, _ = create_lagged_training_data( target, output_chunk_length, @@ -1289,7 +1289,7 @@ def test_lagged_training_data_zero_lags_range_idx(self): expected_y = np.ones((1, 1, 1)) # Check correctness for 'moving windows' and 'time intersection' methods, as # well as for different `multi_models` values: - for (use_moving_windows, multi_models) in product([False, True], [False, True]): + for use_moving_windows, multi_models in product([False, True], [False, True]): X, y, times, _ = create_lagged_training_data( target, output_chunk_length=1, @@ -1329,7 +1329,7 @@ def test_lagged_training_data_zero_lags_datetime_idx(self): expected_y = np.ones((1, 1, 1)) # Check correctness for 'moving windows' and 'time intersection' methods, as # well as for different `multi_models` values: - for (use_moving_windows, multi_models) in product([False, True], [False, True]): + for use_moving_windows, multi_models in product([False, True], [False, True]): X, y, times, _ = create_lagged_training_data( target, output_chunk_length=1, @@ -1367,7 +1367,7 @@ def test_lagged_training_data_positive_lags_range_idx(self): expected_y = np.ones((1, 1, 1)) # Check correctness for 'moving windows' and 'time intersection' methods, as # well as for different `multi_models` values: - for (use_moving_windows, multi_models) in product([False, True], [False, True]): + for use_moving_windows, multi_models in product([False, True], [False, True]): X, y, times, _ = create_lagged_training_data( target, output_chunk_length=1, @@ -1407,7 +1407,7 @@ def test_lagged_training_data_positive_lags_datetime_idx(self): expected_y = np.ones((1, 1, 1)) # Check correctness for 'moving windows' and 'time intersection' methods, as # well as for different `multi_models` values: - for (use_moving_windows, multi_models) in product([False, True], [False, True]): + for use_moving_windows, multi_models in product([False, True], [False, True]): X, y, times, _ = create_lagged_training_data( target, output_chunk_length=1, diff --git a/darts/tests/utils/tabularization/test_get_feature_times.py b/darts/tests/utils/tabularization/test_get_feature_times.py index e63a8e4057..457b419c02 100644 --- a/darts/tests/utils/tabularization/test_get_feature_times.py +++ b/darts/tests/utils/tabularization/test_get_feature_times.py @@ -215,7 +215,7 @@ def test_feature_times_training_range_idx(self): target = linear_timeseries(start=1, length=20, freq=1) past = linear_timeseries(start=2, length=25, freq=2) future = linear_timeseries(start=3, length=30, freq=3) - for (lags, lags_past, lags_future, ocl) in product( + for lags, lags_past, lags_future, ocl in product( self.target_lag_combos, self.lags_past_combos, self.lags_future_combos, @@ -252,7 +252,7 @@ def test_feature_times_training_datetime_idx(self): target = linear_timeseries(start=pd.Timestamp("1/1/2000"), length=20, freq="1d") past = linear_timeseries(start=pd.Timestamp("1/2/2000"), length=25, freq="2d") future = linear_timeseries(start=pd.Timestamp("1/3/2000"), length=30, freq="3d") - for (lags, lags_past, lags_future, ocl) in product( + for lags, lags_past, lags_future, ocl in product( self.target_lag_combos, self.lags_past_combos, self.lags_future_combos, @@ -289,7 +289,7 @@ def test_feature_times_prediction_range_idx(self): target = linear_timeseries(start=1, length=20, freq=1) past = linear_timeseries(start=2, length=25, freq=2) future = linear_timeseries(start=3, length=30, freq=3) - for (lags, lags_past, lags_future) in product( + for lags, lags_past, lags_future in product( self.target_lag_combos, self.lags_past_combos, self.lags_future_combos ): feature_times = _get_feature_times( @@ -322,7 +322,7 @@ def test_feature_times_prediction_datetime_idx(self): target = linear_timeseries(start=pd.Timestamp("1/1/2000"), length=20, freq="1d") past = linear_timeseries(start=pd.Timestamp("1/2/2000"), length=25, freq="2d") future = linear_timeseries(start=pd.Timestamp("1/3/2000"), length=30, freq="3d") - for (lags, lags_past, lags_future) in product( + for lags, lags_past, lags_future in product( self.target_lag_combos, self.lags_past_combos, self.lags_future_combos ): feature_times = _get_feature_times( diff --git a/darts/tests/utils/tabularization/test_get_shared_times.py b/darts/tests/utils/tabularization/test_get_shared_times.py index 3f2b399734..0b5dc6cee8 100644 --- a/darts/tests/utils/tabularization/test_get_shared_times.py +++ b/darts/tests/utils/tabularization/test_get_shared_times.py @@ -16,7 +16,6 @@ def lcm(*integers): class TestGetSharedTimes: - """ Tests `get_shared_times` function defined in `darts.utils.data.tabularization`. """ diff --git a/darts/tests/utils/tabularization/test_strided_moving_window.py b/darts/tests/utils/tabularization/test_strided_moving_window.py index 164e9bea94..0fbad5026d 100644 --- a/darts/tests/utils/tabularization/test_strided_moving_window.py +++ b/darts/tests/utils/tabularization/test_strided_moving_window.py @@ -7,7 +7,6 @@ class TestStridedMovingWindow: - """ Tests `strided_moving_window` function defined in `darts.utils.data.tabularization`. """ @@ -28,7 +27,7 @@ def test_strided_moving_windows_extracted_windows(self): # Create a 'dummy input' with linearly increasing values: x_shape = (10, 8, 12) x = np.arange(np.prod(x_shape)).reshape(*x_shape) - for (axis, stride, window_len) in product( + for axis, stride, window_len in product( axis_combos, stride_combos, window_len_combos ): windows = strided_moving_window(x, window_len, stride, axis) diff --git a/darts/tests/utils/test_likelihood_models.py b/darts/tests/utils/test_likelihood_models.py index 3c0dd67bc9..a7ccce76f9 100644 --- a/darts/tests/utils/test_likelihood_models.py +++ b/darts/tests/utils/test_likelihood_models.py @@ -59,7 +59,7 @@ def test_intra_class_equality(self): def test_inter_class_equality(self): model_combinations = combinations(likelihood_models.keys(), 2) - for (first_model_name, second_model_name) in model_combinations: + for first_model_name, second_model_name in model_combinations: assert ( likelihood_models[first_model_name][0] != likelihood_models[second_model_name][0] diff --git a/darts/timeseries.py b/darts/timeseries.py index 30d5aac716..7a9ad9dfba 100644 --- a/darts/timeseries.py +++ b/darts/timeseries.py @@ -911,9 +911,11 @@ def from_group_dataframe( # store static covariate Series and group DataFrame (without static cov columns) splits.append( ( - pd.DataFrame([static_cov_vals], columns=extract_static_cov_cols) - if extract_static_cov_cols - else None, + ( + pd.DataFrame([static_cov_vals], columns=extract_static_cov_cols) + if extract_static_cov_cols + else None + ), group[extract_value_cols], ) ) @@ -2338,7 +2340,7 @@ def slice( A new series, with indices greater or equal than `start_ts` and smaller or equal than `end_ts`. """ raise_if_not( - type(start_ts) == type(end_ts), + type(start_ts) is type(end_ts), "The two timestamps provided to slice() have to be of the same type.", logger, ) @@ -4443,9 +4445,9 @@ def _fill_missing_dates( time_dim = xa.dims[0] sorted_xa = cls._sort_index(xa, copy=False) - time_index: Union[ - pd.Index, pd.RangeIndex, pd.DatetimeIndex - ] = sorted_xa.get_index(time_dim) + time_index: Union[pd.Index, pd.RangeIndex, pd.DatetimeIndex] = ( + sorted_xa.get_index(time_dim) + ) if isinstance(time_index, pd.DatetimeIndex): has_datetime_index = True @@ -5022,9 +5024,11 @@ def _get_freq(xa_in: xr.DataArray): # selecting components discards the hierarchy, if any xa_ = _xarray_with_attrs( xa_, - xa_.attrs[STATIC_COV_TAG][key.start : key.stop] - if adapt_covs_on_component - else xa_.attrs[STATIC_COV_TAG], + ( + xa_.attrs[STATIC_COV_TAG][key.start : key.stop] + if adapt_covs_on_component + else xa_.attrs[STATIC_COV_TAG] + ), None, ) return self.__class__(xa_) @@ -5055,9 +5059,11 @@ def _get_freq(xa_in: xr.DataArray): # selecting components discards the hierarchy, if any xa_ = _xarray_with_attrs( xa_, - xa_.attrs[STATIC_COV_TAG].loc[[key]] - if adapt_covs_on_component - else xa_.attrs[STATIC_COV_TAG], + ( + xa_.attrs[STATIC_COV_TAG].loc[[key]] + if adapt_covs_on_component + else xa_.attrs[STATIC_COV_TAG] + ), None, ) return self.__class__(xa_) @@ -5096,9 +5102,11 @@ def _get_freq(xa_in: xr.DataArray): xa_ = self._xa.sel({DIMS[1]: key}) xa_ = _xarray_with_attrs( xa_, - xa_.attrs[STATIC_COV_TAG].loc[key] - if adapt_covs_on_component - else xa_.attrs[STATIC_COV_TAG], + ( + xa_.attrs[STATIC_COV_TAG].loc[key] + if adapt_covs_on_component + else xa_.attrs[STATIC_COV_TAG] + ), None, ) return self.__class__(xa_) diff --git a/darts/utils/__init__.py b/darts/utils/__init__.py index a13d1d8b69..be17f2204c 100644 --- a/darts/utils/__init__.py +++ b/darts/utils/__init__.py @@ -2,6 +2,7 @@ Utils ----- """ + from .utils import ( _build_tqdm_iterator, _parallel_apply, diff --git a/darts/utils/data/inference_dataset.py b/darts/utils/data/inference_dataset.py index c60f1f22c0..e914696fbd 100644 --- a/darts/utils/data/inference_dataset.py +++ b/darts/utils/data/inference_dataset.py @@ -219,9 +219,7 @@ def find_list_index(index, cumulative_lengths, bounds, stride): stride_idx = (index - cumulative_lengths[list_index - 1]) * stride return list_index, bound_left + stride_idx - def __getitem__( - self, idx: int - ) -> Tuple[ + def __getitem__(self, idx: int) -> Tuple[ np.ndarray, Optional[np.ndarray], Optional[np.ndarray], @@ -380,9 +378,7 @@ def __init__( def __len__(self): return len(self.ds) - def __getitem__( - self, idx: int - ) -> Tuple[ + def __getitem__(self, idx: int) -> Tuple[ np.ndarray, Optional[np.ndarray], Optional[np.ndarray], @@ -446,9 +442,7 @@ def __init__( def __len__(self): return len(self.ds) - def __getitem__( - self, idx: int - ) -> Tuple[ + def __getitem__(self, idx: int) -> Tuple[ np.ndarray, Optional[np.ndarray], Optional[np.ndarray], @@ -540,9 +534,7 @@ def __init__( def __len__(self): return len(self.ds_past) - def __getitem__( - self, idx - ) -> Tuple[ + def __getitem__(self, idx) -> Tuple[ np.ndarray, Optional[np.ndarray], Optional[np.ndarray], @@ -644,9 +636,7 @@ def __init__( def __len__(self): return len(self.ds_past) - def __getitem__( - self, idx - ) -> Tuple[ + def __getitem__(self, idx) -> Tuple[ np.ndarray, Optional[np.ndarray], Optional[np.ndarray], @@ -752,9 +742,7 @@ def __init__( def __len__(self): return len(self.ds_past) - def __getitem__( - self, idx - ) -> Tuple[ + def __getitem__(self, idx) -> Tuple[ np.ndarray, Optional[np.ndarray], Optional[np.ndarray], diff --git a/darts/utils/data/sequential_dataset.py b/darts/utils/data/sequential_dataset.py index 881dc344fe..3b7a713f38 100644 --- a/darts/utils/data/sequential_dataset.py +++ b/darts/utils/data/sequential_dataset.py @@ -248,9 +248,7 @@ def __init__( def __len__(self): return len(self.ds_past) - def __getitem__( - self, idx - ) -> Tuple[ + def __getitem__(self, idx) -> Tuple[ np.ndarray, Optional[np.ndarray], Optional[np.ndarray], @@ -351,9 +349,7 @@ def __init__( def __len__(self): return len(self.ds_past) - def __getitem__( - self, idx - ) -> Tuple[ + def __getitem__(self, idx) -> Tuple[ np.ndarray, Optional[np.ndarray], Optional[np.ndarray], @@ -459,9 +455,7 @@ def __init__( def __len__(self): return len(self.ds_past) - def __getitem__( - self, idx - ) -> Tuple[ + def __getitem__(self, idx) -> Tuple[ np.ndarray, Optional[np.ndarray], Optional[np.ndarray], diff --git a/darts/utils/data/shifted_dataset.py b/darts/utils/data/shifted_dataset.py index 1a82ff5583..9bd4d4acb3 100644 --- a/darts/utils/data/shifted_dataset.py +++ b/darts/utils/data/shifted_dataset.py @@ -253,9 +253,7 @@ def __init__( def __len__(self): return len(self.ds_past) - def __getitem__( - self, idx - ) -> Tuple[ + def __getitem__(self, idx) -> Tuple[ np.ndarray, Optional[np.ndarray], Optional[np.ndarray], @@ -356,9 +354,7 @@ def __init__( def __len__(self): return len(self.ds_past) - def __getitem__( - self, idx - ) -> Tuple[ + def __getitem__(self, idx) -> Tuple[ np.ndarray, Optional[np.ndarray], Optional[np.ndarray], @@ -466,9 +462,7 @@ def __init__( def __len__(self): return len(self.ds_past) - def __getitem__( - self, idx - ) -> Tuple[ + def __getitem__(self, idx) -> Tuple[ np.ndarray, Optional[np.ndarray], Optional[np.ndarray], diff --git a/darts/utils/data/training_dataset.py b/darts/utils/data/training_dataset.py index d485ee6159..2735e614f0 100644 --- a/darts/utils/data/training_dataset.py +++ b/darts/utils/data/training_dataset.py @@ -241,9 +241,7 @@ def __init__(self): super().__init__() @abstractmethod - def __getitem__( - self, idx: int - ) -> Tuple[ + def __getitem__(self, idx: int) -> Tuple[ np.ndarray, Optional[np.ndarray], Optional[np.ndarray], @@ -264,9 +262,7 @@ def __init__(self): super().__init__() @abstractmethod - def __getitem__( - self, idx: int - ) -> Tuple[ + def __getitem__(self, idx: int) -> Tuple[ np.ndarray, Optional[np.ndarray], Optional[np.ndarray], @@ -287,9 +283,7 @@ def __init__(self): super().__init__() @abstractmethod - def __getitem__( - self, idx: int - ) -> Tuple[ + def __getitem__(self, idx: int) -> Tuple[ np.ndarray, Optional[np.ndarray], Optional[np.ndarray], diff --git a/darts/utils/historical_forecasts/optimized_historical_forecasts_regression.py b/darts/utils/historical_forecasts/optimized_historical_forecasts_regression.py index 2876d716eb..a6cca7d77e 100644 --- a/darts/utils/historical_forecasts/optimized_historical_forecasts_regression.py +++ b/darts/utils/historical_forecasts/optimized_historical_forecasts_regression.py @@ -101,15 +101,21 @@ def _optimized_historical_forecasts_last_points_only( ) X, times = create_lagged_prediction_data( - target_series=None - if model._get_lags("target") is None - else series_[hist_fct_tgt_start:hist_fct_tgt_end], - past_covariates=None - if past_covariates_ is None - else past_covariates_[hist_fct_pc_start:hist_fct_pc_end], - future_covariates=None - if future_covariates_ is None - else future_covariates_[hist_fct_fc_start:hist_fct_fc_end], + target_series=( + None + if model._get_lags("target") is None + else series_[hist_fct_tgt_start:hist_fct_tgt_end] + ), + past_covariates=( + None + if past_covariates_ is None + else past_covariates_[hist_fct_pc_start:hist_fct_pc_end] + ), + future_covariates=( + None + if future_covariates_ is None + else future_covariates_[hist_fct_fc_start:hist_fct_fc_end] + ), lags=model._get_lags("target"), lags_past_covariates=model._get_lags("past"), lags_future_covariates=model._get_lags("future"), @@ -149,13 +155,15 @@ def _optimized_historical_forecasts_last_points_only( forecasts_list.append( TimeSeries.from_times_and_values( - times=times[0] - if stride == 1 and model.output_chunk_length == 1 - else generate_index( - start=hist_fct_start + (forecast_horizon - 1) * freq, - length=forecast.shape[0], - freq=freq * stride, - name=series_.time_index.name, + times=( + times[0] + if stride == 1 and model.output_chunk_length == 1 + else generate_index( + start=hist_fct_start + (forecast_horizon - 1) * freq, + length=forecast.shape[0], + freq=freq * stride, + name=series_.time_index.name, + ) ), values=forecast, columns=forecast_components, @@ -248,15 +256,21 @@ def _optimized_historical_forecasts_all_points( ) X, _ = create_lagged_prediction_data( - target_series=None - if model._get_lags("target") is None - else series_[hist_fct_tgt_start:hist_fct_tgt_end], - past_covariates=None - if past_covariates_ is None - else past_covariates_[hist_fct_pc_start:hist_fct_pc_end], - future_covariates=None - if future_covariates_ is None - else future_covariates_[hist_fct_fc_start:hist_fct_fc_end], + target_series=( + None + if model._get_lags("target") is None + else series_[hist_fct_tgt_start:hist_fct_tgt_end] + ), + past_covariates=( + None + if past_covariates_ is None + else past_covariates_[hist_fct_pc_start:hist_fct_pc_end] + ), + future_covariates=( + None + if future_covariates_ is None + else future_covariates_[hist_fct_fc_start:hist_fct_fc_end] + ), lags=model._get_lags("target"), lags_past_covariates=model._get_lags("past"), lags_future_covariates=model._get_lags("future"), diff --git a/darts/utils/likelihood_models.py b/darts/utils/likelihood_models.py index 900c47687d..7701b7a960 100644 --- a/darts/utils/likelihood_models.py +++ b/darts/utils/likelihood_models.py @@ -114,9 +114,11 @@ def compute_loss(self, model_output: torch.Tensor, target: torch.Tensor): device = params_out[0].device prior_params = tuple( # use model output as "prior" for parameters not specified as prior - torch.tensor(prior_params[i]).to(device) - if prior_params[i] is not None - else params_out[i] + ( + torch.tensor(prior_params[i]).to(device) + if prior_params[i] is not None + else params_out[i] + ) for i in range(len(prior_params)) ) prior_distr = self._distr_from_params(prior_params) diff --git a/darts/utils/losses.py b/darts/utils/losses.py index a2eb251337..2c51e71145 100644 --- a/darts/utils/losses.py +++ b/darts/utils/losses.py @@ -2,6 +2,7 @@ PyTorch Loss Functions ---------------------- """ + # Inspiration: https://github.com/ElementAI/N-BEATS/blob/master/common/torch/losses.py import numpy as np diff --git a/darts/utils/multioutput.py b/darts/utils/multioutput.py index 84e4f04523..5c93ed95a4 100644 --- a/darts/utils/multioutput.py +++ b/darts/utils/multioutput.py @@ -84,9 +84,11 @@ def fit(self, X, y, sample_weight=None, **fit_params): y[:, i], sample_weight, # eval set may be a list (for XGBRegressor), in which case we have to keep it as a list - eval_set=[(eval_set[0][0], eval_set[0][1][:, i])] - if isinstance(eval_set, list) - else (eval_set[0], eval_set[1][:, i]), + eval_set=( + [(eval_set[0][0], eval_set[0][1][:, i])] + if isinstance(eval_set, list) + else (eval_set[0], eval_set[1][:, i]) + ), **fit_params_validated ) for i in range(y.shape[1]) diff --git a/darts/utils/statistics.py b/darts/utils/statistics.py index faf4d1304c..46383e95d5 100644 --- a/darts/utils/statistics.py +++ b/darts/utils/statistics.py @@ -390,7 +390,6 @@ def stationarity_tests( p_value_threshold_adfuller: float = 0.05, p_value_threshold_kpss: float = 0.05, ) -> bool: - """ Double test on stationarity using both Kwiatkowski-Phillips-Schmidt-Shin and Augmented Dickey-Fuller statistical tests. @@ -668,9 +667,11 @@ def plot_acf( axis.plot( (i, i), (0, r[i]), - color=("#b512b8" if m is not None and i == m else "black") - if default_formatting - else None, + color=( + ("#b512b8" if m is not None and i == m else "black") + if default_formatting + else None + ), lw=(1 if m is not None and i == m else 0.5), ) @@ -769,9 +770,7 @@ def plot_pacf( color=( "#b512b8" if m is not None and i == m - else "black" - if default_formatting - else None + else "black" if default_formatting else None ), lw=(1 if m is not None and i == m else 0.5), ) @@ -887,9 +886,11 @@ def plot_ccf( axis.plot( (i, i), (0, ccf[i]), - color=("#b512b8" if m is not None and i == m else "black") - if default_formatting - else None, + color=( + ("#b512b8" if m is not None and i == m else "black") + if default_formatting + else None + ), lw=(1 if m is not None and i == m else 0.5), ) diff --git a/darts/utils/timeseries_generation.py b/darts/utils/timeseries_generation.py index da1d2a524c..0e38747d7a 100644 --- a/darts/utils/timeseries_generation.py +++ b/darts/utils/timeseries_generation.py @@ -60,7 +60,7 @@ def generate_index( logger, ) raise_if( - end is not None and start is not None and type(start) != type(end), + end is not None and start is not None and type(start) is not type(end), "index generation with `start` and `end` requires equal object types of `start` and `end`", logger, ) @@ -311,14 +311,14 @@ def gaussian_timeseries( A white noise TimeSeries created as indicated above. """ - if type(mean) == np.ndarray: + if type(mean) is np.ndarray: raise_if_not( mean.shape == (length,), "If a vector of means is provided, " "it requires the same length as the TimeSeries.", logger, ) - if type(std) == np.ndarray: + if type(std) is np.ndarray: raise_if_not( std.shape == (length, length), "If a matrix of standard deviations is provided, " diff --git a/darts/utils/utils.py b/darts/utils/utils.py index 7a7adc7c59..12a1400fd2 100644 --- a/darts/utils/utils.py +++ b/darts/utils/utils.py @@ -2,6 +2,7 @@ Additional util functions ------------------------- """ + from enum import Enum from functools import wraps from inspect import Parameter, getcallargs, signature @@ -300,7 +301,7 @@ def slice_index( included. """ - if type(start) != type(end): + if type(start) is not type(end): raise_log( ValueError( "start and end values must be of the same type (either both integers or both pd.Timestamps)" diff --git a/requirements/dev.txt b/requirements/dev.txt index 1894e4f4f8..988ecf746f 100644 --- a/requirements/dev.txt +++ b/requirements/dev.txt @@ -1,7 +1,7 @@ -black[jupyter]==22.3.0 -flake8==4.0.1 -isort==5.11.5 +black[jupyter]==24.1.1 +flake8==7.0.0 +isort==5.13.2 pre-commit pytest-cov -pyupgrade==2.31.0 +pyupgrade==v3.15.0 testfixtures