Nixtla / nixtla

TimeGPT-1: production ready pre-trained Time Series Foundation Model for forecasting and anomaly detection. Generative pretrained transformer for time series trained on over 100B data points. It's capable of accurately predicting various domains such as retail, electricity, finance, and IoT with just a few lines of code 🚀.
https://docs.nixtla.io
Other
1.91k stars 151 forks source link

Using X_df Parameter Causes ApiError Internal Error #402

Open worldtu opened 4 days ago

worldtu commented 4 days ago

I am adding an exogenous variable in the forecast model. And it raised "ApiError: status_code: 500, body: Internal Error" internal error. But when I delete this variable, it can predict smoothly.

And I am guessing it might be caused by "X_df" parameter. Because when I delete related exogenous variable and still using X_df ways to generate predictions, it raised the same error.

Success Forecast without exogenous variables:

success

Success Code:

nixtla = NixtlaClient(api_key = api_key)
fcst_df = nixtla.forecast(train_sales_data, 
                            h=2, freq='MS', finetune_steps=2, 
                            finetune_loss='default', level=[50])

Failed Forecast with an exogenous variable:

failed

Failed Forecast without exogenous variable but using X_df parameter:

image

Failed Code:

nixtla = NixtlaClient(api_key = api_key)
fcst_df = nixtla.forecast(df=train_sales_data, 
                              X_df=plan_data,
                              h=2, freq='MS', 
                              finetune_steps=2,
                              finetune_loss='default', level=[50])

Detailed Error:

```python INFO:nixtla.nixtla_client:Validating inputs... INFO:nixtla.nixtla_client:Preprocessing dataframes... INFO:nixtla.nixtla_client:Using the following exogenous variables: sales_target INFO:nixtla.nixtla_client:Calling Forecast Endpoint... INFO:nixtla.nixtla_client:Attempt 1 failed... INFO:nixtla.nixtla_client:Attempt 2 failed... INFO:nixtla.nixtla_client:Attempt 3 failed... INFO:nixtla.nixtla_client:Attempt 4 failed... INFO:nixtla.nixtla_client:Attempt 5 failed... INFO:nixtla.nixtla_client:Attempt 6 failed... --------------------------------------------------------------------------- JSONDecodeError Traceback (most recent call last) File ~/anaconda3/envs/python3.8torch/lib/python3.8/site-packages/nixtla/client.py:532, in Nixtla.forecast_multi_series(self, request, request_options) 531 try: --> 532 _response_json = _response.json() 533 except JSONDecodeError: File ~/anaconda3/envs/python3.8torch/lib/python3.8/site-packages/httpx/_models.py:764, in Response.json(self, **kwargs) 763 def json(self, **kwargs: typing.Any) -> typing.Any: --> 764 return jsonlib.loads(self.content, **kwargs) File ~/anaconda3/envs/python3.8torch/lib/python3.8/json/__init__.py:357, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw) 354 if (cls is None and object_hook is None and 355 parse_int is None and parse_float is None and 356 parse_constant is None and object_pairs_hook is None and not kw): --> 357 return _default_decoder.decode(s) 358 if cls is None: File ~/anaconda3/envs/python3.8torch/lib/python3.8/json/decoder.py:337, in JSONDecoder.decode(self, s, _w) 333 """Return the Python representation of ``s`` (a ``str`` instance 334 containing a JSON document). 335 336 """ --> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end()) 338 end = _w(s, end).end() File ~/anaconda3/envs/python3.8torch/lib/python3.8/json/decoder.py:355, in JSONDecoder.raw_decode(self, s, idx) 354 except StopIteration as err: --> 355 raise JSONDecodeError("Expecting value", s, err.value) from None 356 return obj, end JSONDecodeError: Expecting value: line 1 column 1 (char 0) During handling of the above exception, another exception occurred: ApiError Traceback (most recent call last) Cell In[38], line 37 10 # sales_data = sales_data_ori.copy() 11 # train_sales_data = sales_data.query(f'ds < "{fcst_ds}"') 12 # train_sales_data = train_sales_data[train_sales_data['ds'] < train_sales_data['ds'].max()] (...) 34 35 # -- 整体预测 36 nixtla = NixtlaClient(api_key = api_key) ---> 37 fcst_df = nixtla.forecast(df=train_sales_data, X_df=plan_data, 38 h=2, freq='MS', 39 finetune_steps=4, 40 finetune_loss='default', level=[50]) 41 fcst_df[['ITEM_CODE', 'SITE_CODE', 'CUSTOMER_CODE']] = fcst_df['unique_id'].str.split("-", expand=True) 43 # 选择M-1预测结果 File ~/anaconda3/envs/python3.8torch/lib/python3.8/site-packages/nixtla/nixtla_client.py:60, in deprecated_argument..decorator..wrapper(*args, **kwargs) 58 raise TypeError(f"{new_name} argument duplicated") 59 kwargs[new_name] = kwargs.pop(old_name) ---> 60 return func(*args, **kwargs) File ~/anaconda3/envs/python3.8torch/lib/python3.8/site-packages/nixtla/nixtla_client.py:60, in deprecated_argument..decorator..wrapper(*args, **kwargs) 58 raise TypeError(f"{new_name} argument duplicated") 59 kwargs[new_name] = kwargs.pop(old_name) ---> 60 return func(*args, **kwargs) File ~/anaconda3/envs/python3.8torch/lib/python3.8/site-packages/nixtla/nixtla_client.py:1304, in NixtlaClient.forecast(self, df, h, freq, id_col, time_col, target_col, X_df, level, quantiles, finetune_steps, finetune_loss, clean_ex_first, validate_api_key, add_history, date_features, date_features_to_one_hot, model, num_partitions) 1227 """Forecast your time series using TimeGPT. 1228 1229 Parameters (...) 1301 predictions (if level is not None). 1302 """ 1303 if isinstance(df, pd.DataFrame): -> 1304 return self._forecast( 1305 df=df, 1306 h=h, 1307 freq=freq, 1308 id_col=id_col, 1309 time_col=time_col, 1310 target_col=target_col, 1311 X_df=X_df, 1312 level=level, 1313 quantiles=quantiles, 1314 finetune_steps=finetune_steps, 1315 finetune_loss=finetune_loss, 1316 clean_ex_first=clean_ex_first, 1317 validate_api_key=validate_api_key, 1318 add_history=add_history, 1319 date_features=date_features, 1320 date_features_to_one_hot=date_features_to_one_hot, 1321 model=model, 1322 num_partitions=num_partitions, 1323 ) 1324 else: 1325 dist_nixtla_client = self._instantiate_distributed_nixtla_client() File ~/anaconda3/envs/python3.8torch/lib/python3.8/site-packages/nixtla/nixtla_client.py:730, in validate_model_parameter..wrapper(self, *args, **kwargs) 725 if model not in self.supported_models: 726 raise ValueError( 727 f'unsupported model: {kwargs["model"]} ' 728 f'supported models: {", ".join(self.supported_models)}' 729 ) --> 730 return func(self, *args, **kwargs) File ~/anaconda3/envs/python3.8torch/lib/python3.8/site-packages/nixtla/nixtla_client.py:748, in partition_by_uid..wrapper(self, num_partitions, **kwargs) 746 def wrapper(self, num_partitions, **kwargs): 747 if num_partitions is None or num_partitions == 1: --> 748 return func(self, **kwargs, num_partitions=1) 749 df = kwargs.pop("df") 750 X_df = kwargs.pop("X_df", None) File ~/anaconda3/envs/python3.8torch/lib/python3.8/site-packages/nixtla/nixtla_client.py:976, in _NixtlaClient._forecast(self, df, h, freq, id_col, time_col, target_col, X_df, level, quantiles, finetune_steps, finetune_loss, clean_ex_first, validate_api_key, add_history, date_features, date_features_to_one_hot, model, num_partitions) 954 nixtla_client_model = _NixtlaClientModel( 955 client=self.client, 956 h=h, (...) 971 max_wait_time=self.max_wait_time, 972 ) 973 df, X_df, uids_dtype = self._uids_to_categorical( 974 df=df, X_df=X_df, id_col=id_col 975 ) --> 976 fcst_df = nixtla_client_model.forecast( 977 df=df, X_df=X_df, add_history=add_history 978 ) 979 fcst_df = self._restore_uids(fcst_df, dtype=uids_dtype, id_col=id_col) 980 self.weights_x = nixtla_client_model.weights_x File ~/anaconda3/envs/python3.8torch/lib/python3.8/site-packages/nixtla/nixtla_client.py:589, in _NixtlaClientModel.forecast(self, df, X_df, add_history) 577 main_logger.info("Calling Forecast Endpoint...") 578 payload = MultiSeriesForecast( 579 y=y, 580 x=x, (...) 587 model=self.model, 588 ) --> 589 response_timegpt = self._call_api( 590 self.client.forecast_multi_series, 591 payload, 592 ) 593 if "weights_x" in response_timegpt: 594 self.weights_x = pd.DataFrame( 595 { 596 "features": x_cols, 597 "weights": response_timegpt["weights_x"], 598 } 599 ) File ~/anaconda3/envs/python3.8torch/lib/python3.8/site-packages/nixtla/nixtla_client.py:229, in _NixtlaClientModel._call_api(self, method, request) 228 def _call_api(self, method, request): --> 229 response = self._retry_strategy()(method)(request=request) 230 if "data" in response: 231 response = response["data"] File ~/anaconda3/envs/python3.8torch/lib/python3.8/site-packages/tenacity/__init__.py:330, in BaseRetrying.wraps..wrapped_f(*args, **kw) 326 @functools.wraps( 327 f, functools.WRAPPER_ASSIGNMENTS + ("__defaults__", "__kwdefaults__") 328 ) 329 def wrapped_f(*args: t.Any, **kw: t.Any) -> t.Any: --> 330 return self(f, *args, **kw) File ~/anaconda3/envs/python3.8torch/lib/python3.8/site-packages/tenacity/__init__.py:467, in Retrying.__call__(self, fn, *args, **kwargs) 465 retry_state = RetryCallState(retry_object=self, fn=fn, args=args, kwargs=kwargs) 466 while True: --> 467 do = self.iter(retry_state=retry_state) 468 if isinstance(do, DoAttempt): 469 try: File ~/anaconda3/envs/python3.8torch/lib/python3.8/site-packages/tenacity/__init__.py:368, in BaseRetrying.iter(self, retry_state) 366 result = None 367 for action in self.iter_state.actions: --> 368 result = action(retry_state) 369 return result File ~/anaconda3/envs/python3.8torch/lib/python3.8/site-packages/tenacity/__init__.py:410, in BaseRetrying._post_stop_check_actions..exc_check(rs) 408 retry_exc = self.retry_error_cls(fut) 409 if self.reraise: --> 410 raise retry_exc.reraise() 411 raise retry_exc from fut.exception() File ~/anaconda3/envs/python3.8torch/lib/python3.8/site-packages/tenacity/__init__.py:183, in RetryError.reraise(self) 181 def reraise(self) -> t.NoReturn: 182 if self.last_attempt.failed: --> 183 raise self.last_attempt.result() 184 raise self File ~/anaconda3/envs/python3.8torch/lib/python3.8/concurrent/futures/_base.py:437, in Future.result(self, timeout) 435 raise CancelledError() 436 elif self._state == FINISHED: --> 437 return self.__get_result() 439 self._condition.wait(timeout) 441 if self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]: File ~/anaconda3/envs/python3.8torch/lib/python3.8/concurrent/futures/_base.py:389, in Future.__get_result(self) 387 if self._exception: 388 try: --> 389 raise self._exception 390 finally: 391 # Break a reference cycle with the exception in self._exception 392 self = None File ~/anaconda3/envs/python3.8torch/lib/python3.8/site-packages/tenacity/__init__.py:470, in Retrying.__call__(self, fn, *args, **kwargs) 468 if isinstance(do, DoAttempt): 469 try: --> 470 result = fn(*args, **kwargs) 471 except BaseException: # noqa: B902 472 retry_state.set_exception(sys.exc_info()) # type: ignore[arg-type] File ~/anaconda3/envs/python3.8torch/lib/python3.8/site-packages/nixtla/client.py:534, in Nixtla.forecast_multi_series(self, request, request_options) 532 _response_json = _response.json() 533 except JSONDecodeError: --> 534 raise ApiError(status_code=_response.status_code, body=_response.text) 535 raise ApiError(status_code=_response.status_code, body=_response_json) ApiError: status_code: 500, body: Internal Error ```
jmoralez commented 3 days ago

Hey. What are the contents of X_df?