AmpersandTV / pymc3-hmm

Hidden Markov models in PyMC3
Other
94 stars 13 forks source link

Example notebook `poisson_zero_example.ipynb` does not complete #108

Closed chopley closed 2 years ago

chopley commented 2 years ago

Description of your problem or feature request

When running https://github.com/AmpersandTV/pymc3-hmm/blob/main/examples/poisson_zero_example.ipynb the notebook fails on plot_split_timeseries_histograms

Please provide a minimal, self-contained, and reproducible example. see https://github.com/AmpersandTV/pymc3-hmm/blob/main/examples/poisson_zero_example.ipynb

axes_split_data = plot_split_timeseries_histograms(
    plot_data,
    sample_col="Y_t",
    plot_fn=plot_fn,
    split_max=10,
    figsize=(15, 30),
    twin_column_name="E[S_t]",
    twin_plot_kwargs={
        "color": "green",
        "drawstyle": "steps",
        "linestyle": "--",
        "alpha": 0.4,
    },
)

**Please provide the full traceback of any errors.**
```python
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File ~/anaconda3/envs/pymc-v3.11.4/lib/python3.9/site-packages/pandas/core/indexes/base.py:3621, in Index.get_loc(self, key, method, tolerance)
   3620 try:
-> 3621     return self._engine.get_loc(casted_key)
   3622 except KeyError as err:

File ~/anaconda3/envs/pymc-v3.11.4/lib/python3.9/site-packages/pandas/_libs/index.pyx:136, in pandas._libs.index.IndexEngine.get_loc()

File ~/anaconda3/envs/pymc-v3.11.4/lib/python3.9/site-packages/pandas/_libs/index.pyx:163, in pandas._libs.index.IndexEngine.get_loc()

File pandas/_libs/hashtable_class_helper.pxi:5198, in pandas._libs.hashtable.PyObjectHashTable.get_item()

File pandas/_libs/hashtable_class_helper.pxi:5206, in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'E[S_t]'

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
Input In [45], in <cell line: 20>()
      2     ax.plot(
      3         data["y_t"],
      4         label="y_t",
   (...)
      8         drawstyle="steps",
      9     )
     11     ax.plot(
     12         data["E[Y_t]"],
     13         label="E[Y_t]",
   (...)
     17         drawstyle="steps",
     18     )
---> 20 axes_split_data = plot_split_timeseries_histograms(
     21     plot_data,
     22     sample_col="Y_t",
     23     plot_fn=plot_fn,
     24     split_max=10,
     25     figsize=(15, 30),
     26     twin_column_name="E[S_t]",
     27     twin_plot_kwargs={
     28         "color": "green",
     29         "drawstyle": "steps",
     30         "linestyle": "--",
     31         "alpha": 0.4,
     32     },
     33 )
     35 # for (_, twin_ax), _ in axes_split_data:
     36 #     _ = twin_ax.set_ylabel(
     37 #         "E[S_t]", color=twin_ax.get_lines()[0].get_color()
     38 #     )
     39 #     _ = twin_ax.yaxis.set_major_locator(MaxNLocator(integer=True))
     41 plt.tight_layout()

File ~/anaconda3/envs/pymc-v3.11.4/lib/python3.9/site-packages/pymc3_hmm/utils.py:475, in plot_split_timeseries_histograms(plot_data, sample_col, plot_fn, **split_ts_kwargs)
    452 """A wrapper function for `plot_split_timeseries` and `plot_timeseries_histograms`
    453 
    454 Parameters
   (...)
    467 
    468 """
    469 axes_split_data = plot_split_timeseries(
    470     plot_data,
    471     plot_fn=plot_fn,
    472     **split_ts_kwargs,
    473 )
--> 475 _ = plot_split_timeseries(
    476     plot_data[sample_col],
    477     axes_split_data=axes_split_data,
    478     plot_fn=plot_timeseries_histograms,
    479     **split_ts_kwargs,
    480 )
    482 return axes_split_data

File ~/anaconda3/envs/pymc-v3.11.4/lib/python3.9/site-packages/pymc3_hmm/utils.py:317, in plot_split_timeseries(data, axes_split_data, split_freq, split_max, twin_column_name, twin_plot_kwargs, figsize, title, plot_fn, **plot_kwds)
    314 split_data = obs_splits[i]
    316 if twin_column_name:
--> 317     alt_data = split_data[twin_column_name].to_frame()
    318     split_data = split_data.drop(columns=[twin_column_name])
    320 plot_fn(ax, split_data, **plot_kwds)

File ~/anaconda3/envs/pymc-v3.11.4/lib/python3.9/site-packages/pandas/core/frame.py:3505, in DataFrame.__getitem__(self, key)
   3503 if self.columns.nlevels > 1:
   3504     return self._getitem_multilevel(key)
-> 3505 indexer = self.columns.get_loc(key)
   3506 if is_integer(indexer):
   3507     indexer = [indexer]

File ~/anaconda3/envs/pymc-v3.11.4/lib/python3.9/site-packages/pandas/core/indexes/base.py:3623, in Index.get_loc(self, key, method, tolerance)
   3621     return self._engine.get_loc(casted_key)
   3622 except KeyError as err:
-> 3623     raise KeyError(key) from err
   3624 except TypeError:
   3625     # If we have a listlike key, _check_indexing_error will raise
   3626     #  InvalidIndexError. Otherwise we fall through and re-raise
   3627     #  the TypeError.
   3628     self._check_indexing_error(key)

KeyError: 'E[S_t]'

Versions and main components