uber / orbit

A Python package for Bayesian forecasting with object-oriented design and probabilistic models under the hood.
https://orbit-ml.readthedocs.io/en/stable/
Other
1.85k stars 132 forks source link

Unable to run KTR after fresh installment #845

Closed itamarfaran closed 5 months ago

itamarfaran commented 6 months ago

Describe the bug I opened a clean conda environment just for orbit-ml. when trying to run the KTR model for the first time, I'm getting a ValueError:

ValueError: Failed to get source info for Stan model '/opt/homebrew/anaconda3/envs/fido-forecast-expr/lib/python3.9/site-packages/orbit/stan/ktrlite.stan'. Console:
Syntax error in '/opt/homebrew/anaconda3/envs/fido-forecast-expr/lib/python3.9/site-packages/orbit/stan/ktrlite.stan', line 11, column 21 to column 22, parsing error:
   -------------------------------------------------
     9:    int<lower=0> DOF;
    10:    vector[NUM_OF_OBS] RESPONSE;
    11:    int WHICH_VALID_RES[N_VALID_RES];
                              ^
    12:    // trend related
    13:    int<lower=0> N_KNOTS_LEV;
   -------------------------------------------------

";" expected after variable declaration.

To Reproduce conda create --name orbit-env python=3.9 orbit-ml

import pandas as pd
import numpy as np
from orbit.models import KTR

df = pd.DataFrame({
    "ds": pd.date_range("2021-01-01", "2023-12-31", freq="D"),
    "y": np.random.normal(size=1095),
})

ktr = KTR(response_col="y", date_col="ds")
ktr.fit(df)

Environment (please complete the following information): macOS M1 Pro (Sonoma 14.2.1) Python == 3.9.18

itamarfaran commented 6 months ago

Full traceback:

2024-02-12 12:55:11 - orbit - INFO - Optimizing (PyStan) with algorithm: LBFGS.
2024-02-12 12:55:11 - orbit - INFO - First time in running stan model:ktrlite. Expect 3 - 5 minutes for compilation.
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[1], line 15
      6 df = pd.DataFrame({
      7     "ds": pd.date_range("2021-01-01", "2023-12-31", freq="D"),
      8     "y": np.random.normal(size=1095),
      9 })
     11 ktr = KTR(
     12     response_col="y",
     13     date_col="ds",
     14 )
---> 15 ktr.fit(df)

File /opt/homebrew/anaconda3/envs/fido-forecast-expr/lib/python3.9/site-packages/orbit/forecaster/svi.py:36, in SVIForecaster.fit(self, df, point_method, keep_samples, sampling_temperature, **kwargs)
     28 def fit(
     29     self,
     30     df,
   (...)
     34     **kwargs,
     35 ):
---> 36     super().fit(df, sampling_temperature=sampling_temperature, **kwargs)
     37     self._point_method = point_method
     39     if point_method is not None:

File /opt/homebrew/anaconda3/envs/fido-forecast-expr/lib/python3.9/site-packages/orbit/forecaster/forecaster.py:147, in Forecaster.fit(self, df, **kwargs)
    145 self._set_training_meta(df)
    146 # customize module
--> 147 self._model.set_dynamic_attributes(
    148     df=df, training_meta=self.get_training_meta()
    149 )
    150 # based on the model and df, set training input
    151 self.set_training_data_input()

File /opt/homebrew/anaconda3/envs/fido-forecast-expr/lib/python3.9/site-packages/orbit/template/ktr.py:1002, in KTRModel.set_dynamic_attributes(self, df, training_meta)
   1000 self._set_coefficients_kernel_matrix(df, training_meta)
   1001 self._set_knots_scale_matrix(df, training_meta)
-> 1002 self._set_levs_and_seas(df, training_meta)
   1003 self._filter_coef_prior(df)
   1004 self._set_valid_response_attributes(training_meta)

File /opt/homebrew/anaconda3/envs/fido-forecast-expr/lib/python3.9/site-packages/orbit/template/ktr.py:883, in KTRModel._set_levs_and_seas(self, df, training_meta)
    865 # use ktrlite to derive levs and seas
    866 ktrlite = KTRLite(
    867     response_col=response_col,
    868     date_col=date_col,
   (...)
    881     **self.ktrlite_optim_args,
    882 )
--> 883 ktrlite.fit(df=df)
    884 # self._ktrlite_model = ktrlite
    885 ktrlite_pt_posteriors = ktrlite.get_point_posteriors()

File /opt/homebrew/anaconda3/envs/fido-forecast-expr/lib/python3.9/site-packages/orbit/forecaster/map.py:23, in MAPForecaster.fit(self, df, **kwargs)
     22 def fit(self, df, **kwargs):
---> 23     super().fit(df, **kwargs)
     24     posterior_samples = self._posterior_samples
     25     map_posterior = {}

File /opt/homebrew/anaconda3/envs/fido-forecast-expr/lib/python3.9/site-packages/orbit/forecaster/forecaster.py:162, in Forecaster.fit(self, df, **kwargs)
    158 model_param_names = self._model.get_model_param_names()
    160 # note that estimator will search for the .stan, .pyro model file based on the
    161 # estimator type and model_name provided
--> 162 _posterior_samples, training_metrics = estimator.fit(
    163     model_name=model_name,
    164     model_param_names=model_param_names,
    165     data_input=data_input,
    166     fitter=self._model.get_fitter(),
    167     init_values=init_values,
    168     **kwargs,
    169 )
    170 self._posterior_samples = _posterior_samples
    171 self._training_metrics = training_metrics

File /opt/homebrew/anaconda3/envs/fido-forecast-expr/lib/python3.9/site-packages/orbit/estimators/stan_estimator.py:205, in StanEstimatorMAP.fit(self, model_name, model_param_names, data_input, fitter, init_values)
    197 def fit(
    198     self,
    199     model_name,
   (...)
    203     init_values=None,
    204 ):
--> 205     compiled_mod = get_compiled_stan_model(model_name)
    206     data_input.update({"T_STAR": 1.0})
    208     # passing callable from the model as seen in `initfun1()`
    209     # init_values = init_values or self.stan_init
    210 
    211     # in case optimizing fails with given algorithm fallback to `Newton`
    212     # init values interface can be referred here: https://cmdstanpy.readthedocs.io/en/stable-0.9.65/api.html
    213     # Dict [Str, np.array] where key is the param name and value array dim matches the param dim

File /opt/homebrew/anaconda3/envs/fido-forecast-expr/lib/python3.9/site-packages/orbit/utils/stan.py:64, in get_compiled_stan_model(stan_model_name)
     60 def get_compiled_stan_model(stan_model_name):
     61     """
     62     Load compiled Stan model
     63     """
---> 64     compiled_model = compile_stan_model(stan_model_name)
     65     with open(compiled_model, "rb") as f:
     66         return pickle.load(f)

File /opt/homebrew/anaconda3/envs/fido-forecast-expr/lib/python3.9/site-packages/orbit/utils/stan.py:52, in compile_stan_model(stan_model_name)
     43 if not os.path.isfile(compiled_model) or os.path.getmtime(
     44     compiled_model
     45 ) < os.path.getmtime(source_model):
     47     logger.info(
     48         "First time in running stan model:{}. Expect 3 - 5 minutes for compilation.".format(
     49             stan_model_name
     50         )
     51     )
---> 52     sm = CmdStanModel(stan_file=source_model)
     54     with open(compiled_model, "wb") as f:
     55         pickle.dump(sm, f, protocol=pickle.HIGHEST_PROTOCOL)

File /opt/homebrew/anaconda3/envs/fido-forecast-expr/lib/python3.9/site-packages/cmdstanpy/model.py:215, in CmdStanModel.__init__(self, model_name, stan_file, exe_file, force_compile, stanc_options, cpp_options, user_header, compile)
    211 if not cmdstan_version_before(
    212     2, 27
    213 ):  # unknown end of version range
    214     try:
--> 215         model_info = self.src_info()
    216         if 'parameters' in model_info:
    217             self._fixed_param |= len(model_info['parameters']) == 0

File /opt/homebrew/anaconda3/envs/fido-forecast-expr/lib/python3.9/site-packages/cmdstanpy/model.py:321, in CmdStanModel.src_info(self)
    319 if self.stan_file is None or cmdstan_version_before(2, 27):
    320     return {}
--> 321 return compilation.src_info(str(self.stan_file), self._compiler_options)

File /opt/homebrew/anaconda3/envs/fido-forecast-expr/lib/python3.9/site-packages/cmdstanpy/compilation.py:355, in src_info(stan_file, compiler_options)
    353 proc = subprocess.run(cmd, capture_output=True, text=True, check=False)
    354 if proc.returncode:
--> 355     raise ValueError(
    356         f"Failed to get source info for Stan model "
    357         f"'{stan_file}'. Console:\n{proc.stderr}"
    358     )
    359 result: Dict[str, Any] = json.loads(proc.stdout)
    360 return result

ValueError: Failed to get source info for Stan model '/opt/homebrew/anaconda3/envs/fido-forecast-expr/lib/python3.9/site-packages/orbit/stan/ktrlite.stan'. Console:
Syntax error in '/opt/homebrew/anaconda3/envs/fido-forecast-expr/lib/python3.9/site-packages/orbit/stan/ktrlite.stan', line 11, column 21 to column 22, parsing error:
   -------------------------------------------------
     9:    int<lower=0> DOF;
    10:    vector[NUM_OF_OBS] RESPONSE;
    11:    int WHICH_VALID_RES[N_VALID_RES];
                              ^
    12:    // trend related
    13:    int<lower=0> N_KNOTS_LEV;
   -------------------------------------------------

";" expected after variable declaration.