sktime / pytorch-forecasting

Time series forecasting with PyTorch
https://pytorch-forecasting.readthedocs.io/
MIT License
3.98k stars 629 forks source link

Pytorch-Forecasting imports deprecated property from transient dependency on numpy #1207

Open JeroenPeterBos opened 1 year ago

JeroenPeterBos commented 1 year ago

Expected behavior

I created a simple TimeSeriesDataset without specifying an explicit target_normalizer. I expected it to simply create a default normalizer deduced from the other arguments as explained in the documentation.

Actual behavior

An exception was raised of the type AttributeError by the numpy package. The cause is that aliases like numpy.float and numpy.int have been deprecated as of numpy 1.20 which has been out for almost two years. The deprecation is explained here. This dependency on numpy<=1.19 is not specified by pytorch-forecasting as described in #1130 .

Code to reproduce the problem

Create an environment with the above versions and install numpy >= 1.20. Then run the first few cell's of the tutorial in the documentation for TFT: https://pytorch-forecasting.readthedocs.io/en/stable/tutorials/stallion.html

STACKTRACE

#### The stacktrace from a simple `TimeSeriesDataSet` creation. ```python --------------------------------------------------------------------------- NotFittedError Traceback (most recent call last) File /opt/conda/envs/leap-dsr-rd/lib/python3.8/site-packages/pytorch_forecasting/data/timeseries.py:753, in TimeSeriesDataSet._preprocess_data(self, data) 752 try: --> 753 check_is_fitted(self.target_normalizer) 754 except NotFittedError: File /opt/conda/envs/leap-dsr-rd/lib/python3.8/site-packages/sklearn/utils/validation.py:1380, in check_is_fitted(estimator, attributes, msg, all_or_any) 1379 if not fitted: -> 1380 raise NotFittedError(msg % {"name": type(estimator).__name__}) NotFittedError: This GroupNormalizer instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator. During handling of the above exception, another exception occurred: AttributeError Traceback (most recent call last) Cell In[10], line 1 ----> 1 training = TimeSeriesDataSet( 2 df_h[df_h["local_hour_start"].dt.year == 2021], 3 group_ids=["meter_id"], 4 time_idx="local_hour_idx", 5 target="energy_kwh", 6 target_normalizer=GroupNormalizer(groups=["meter_id"]), 7 max_encoder_length=24 * 7, 8 min_prediction_length=3, # One hour plus 2 buffer hours 9 max_prediction_length=7, # Five hours plus 2 buffer hours 10 time_varying_unknown_categoricals=[], 11 time_varying_unknown_reals=["energy_kwh"], 12 time_varying_known_categoricals=["is_event_hour"], 13 time_varying_known_reals=[], 14 ) 16 # validation = TimeSeriesDataSet.from_dataset(training, df_h, predict=True, stop_randomization=True) File /opt/conda/envs/leap-dsr-rd/lib/python3.8/site-packages/pytorch_forecasting/data/timeseries.py:476, in TimeSeriesDataSet.__init__(self, data, time_idx, target, group_ids, weight, max_encoder_length, min_encoder_length, min_prediction_idx, min_prediction_length, max_prediction_length, static_categoricals, static_reals, time_varying_known_categoricals, time_varying_known_reals, time_varying_unknown_categoricals, time_varying_unknown_reals, variable_groups, constant_fill_strategy, allow_missing_timesteps, lags, add_relative_time_idx, add_target_scales, add_encoder_length, target_normalizer, categorical_encoders, scalers, randomize_length, predict_mode) 473 data = data.sort_values(self.group_ids + [self.time_idx]) 475 # preprocess data --> 476 data = self._preprocess_data(data) 477 for target in self.target_names: 478 assert target not in self.scalers, "Target normalizer is separate and not in scalers." File /opt/conda/envs/leap-dsr-rd/lib/python3.8/site-packages/pytorch_forecasting/data/timeseries.py:758, in TimeSeriesDataSet._preprocess_data(self, data) 756 self.target_normalizer.fit(data[self.target]) 757 elif isinstance(self.target_normalizer, (GroupNormalizer, MultiNormalizer)): --> 758 self.target_normalizer.fit(data[self.target], data) 759 else: 760 self.target_normalizer.fit(data[self.target]) File /opt/conda/envs/leap-dsr-rd/lib/python3.8/site-packages/pytorch_forecasting/data/encoders.py:771, in GroupNormalizer.fit(self, y, X) 760 """ 761 Determine scales for each group 762 (...) 768 self 769 """ 770 y = self.preprocess(y) --> 771 eps = np.finfo(np.float).eps 772 if len(self.groups) == 0: 773 assert not self.scale_by_group, "No groups are defined, i.e. `scale_by_group=[]`" File /opt/conda/envs/leap-dsr-rd/lib/python3.8/site-packages/numpy/__init__.py:284, in __getattr__(attr) 281 from .testing import Tester 282 return Tester --> 284 raise AttributeError("module {!r} has no attribute " 285 "{!r}".format(__name__, attr)) AttributeError: module 'numpy' has no attribute 'float' ```

borab96 commented 1 year ago

numpy<1.24 will only raise warnings. Probably preferable to <=1.19