Recreate a Prophet model from a dictionary. Recreates models that were converted with model_to_dict. Parameters ---------- model_dict: Dictionary containing model, created with model_to_dict. Returns ------- Prophet model.
(model_dict)
| 141 | model_dict['holidays_mode'] = model_dict['seasonality_mode'] |
| 142 | |
| 143 | def model_from_dict(model_dict): |
| 144 | """Recreate a Prophet model from a dictionary. |
| 145 | |
| 146 | Recreates models that were converted with model_to_dict. |
| 147 | |
| 148 | Parameters |
| 149 | ---------- |
| 150 | model_dict: Dictionary containing model, created with model_to_dict. |
| 151 | |
| 152 | Returns |
| 153 | ------- |
| 154 | Prophet model. |
| 155 | """ |
| 156 | model = Prophet() # We will overwrite all attributes set in init anyway |
| 157 | # Simple types |
| 158 | _handle_simple_attributes_backwards_compat(model_dict) |
| 159 | for attribute in SIMPLE_ATTRIBUTES: |
| 160 | setattr(model, attribute, model_dict[attribute]) |
| 161 | for attribute in PD_SERIES: |
| 162 | if model_dict[attribute] is None: |
| 163 | setattr(model, attribute, None) |
| 164 | else: |
| 165 | s = pd.read_json(StringIO(model_dict[attribute]), typ='series', orient='split') |
| 166 | if s.name == 'ds': |
| 167 | if len(s) == 0: |
| 168 | s = pd.to_datetime(s) |
| 169 | s = s.dt.tz_localize(None) |
| 170 | setattr(model, attribute, s) |
| 171 | for attribute in PD_TIMESTAMP: |
| 172 | pd_ts = pd.Timestamp.fromtimestamp(model_dict[attribute], tz="UTC").tz_localize(None) |
| 173 | setattr(model, attribute, pd_ts) |
| 174 | for attribute in PD_TIMEDELTA: |
| 175 | setattr(model, attribute, pd.Timedelta(seconds=model_dict[attribute])) |
| 176 | for attribute in PD_DATAFRAME: |
| 177 | if model_dict[attribute] is None: |
| 178 | setattr(model, attribute, None) |
| 179 | else: |
| 180 | df = pd.read_json(StringIO(model_dict[attribute]), typ='frame', orient='table', convert_dates=['ds']) |
| 181 | if attribute == 'train_component_cols': |
| 182 | # Special handling because of named index column |
| 183 | df.columns.name = 'component' |
| 184 | df.index.name = 'col' |
| 185 | setattr(model, attribute, df) |
| 186 | for attribute in NP_ARRAY: |
| 187 | setattr(model, attribute, np.array(model_dict[attribute])) |
| 188 | for attribute in ORDEREDDICT: |
| 189 | key_list, unordered_dict = model_dict[attribute] |
| 190 | od = OrderedDict() |
| 191 | for key in key_list: |
| 192 | od[key] = unordered_dict[key] |
| 193 | setattr(model, attribute, od) |
| 194 | # Other attributes with special handling |
| 195 | # fit_kwargs |
| 196 | model.fit_kwargs = model_dict['fit_kwargs'] |
| 197 | # Params (Dict[str, np.ndarray]) |
| 198 | model.params = {k: np.array(v) for k, v in model_dict['params'].items()} |
| 199 | # Skipped attributes |
| 200 | model.stan_backend = None |
no test coverage detected
searching dependent graphs…