ContextLab / hypertools

A Python toolbox for gaining geometric insights into high-dimensional data
http://hypertools.readthedocs.io/en/latest/
MIT License
1.81k stars 161 forks source link

Plotting animations do not seem to be compatible with Jupyter Notebook 7 #261

Closed ljchang closed 3 months ago

ljchang commented 3 months ago

There was a major update to Jupyter Notebook/Lab from Version 6 to Version 7 this past year.

The hypertools helper functions that deal with backends when plotting animations do not seem to be compatible with the new version of jupyter notebook.

Screenshot 2024-03-11 at 7 17 29 PM

Everything works when I switch to using the old version (i.e., nbclassic).

Just wanted to bring this to your attention. Thanks!

paxtonfitzpatrick commented 3 months ago

hey @ljchang, thanks for the report, and apologies for the delay. Turns out there were actually 2 separate bugs here:

  1. The TypeError in your screenshot was thrown by one of the callback functions Hypertools registers with IPython's EventManger for managing the Matplotlib backend around interactive/animated plots. Basically, we defer resetting the plotting backend until just before the next cell you run is executed so that animations don't stop when switching back to inline static plotting. IPython used to be pretty lenient about these callbacks' signatures but it looks like that changed in v8.17 (ipython/ipython#14216).
  2. The reason the plot itself wasn't displaying is related to the update from Notebook v6 ➡️ v7. The notebook v7 frontend now uses the same JavaScript library as JupyterLab, which doesn't expose the global IPython and Jupyter JS objects that Matplotlib's default backend for interactive inline plots in "classic" notebooks (nbAgg) relied on. Looks like most everything (now including Hypertools) has transitioned to using ipympl for inline animations, but the various GUI-based interactive backends should still work too (Qt[4|5]Agg, GTK[3|4]Agg, TkAgg, WebAgg, WXAgg, MacOSX, etc.)

I've implemented fixes for both issues in paxtonfitzpatrick/hypertools@fa28a7901277c01ebc0c49f1a3ceaff11cbcf952. Once I merge those in (hopefully tonight), I'll take a look at #262 (thanks for that PR as well!).

paxtonfitzpatrick commented 3 months ago

@ljchang this should now be fixed on master if you'd like to try it out!

pip install git+https://github.com/ContextLab/hypertools.git

Animated & interactive plots should now automatically work in both "classic" notebooks and Notebook v7+, as well as in JupyterLab.

One quick thing to note is that if you run your Jupyter notebook/lab server from a different environment than your IPython kernel, you'll also need to pip install ipympl into the notebook server's environment, then shutdown and re-launch the server. This will provide the JS components that the notebook/lab frontend needs to display interactive plots.

I'm gonna close this for now, but if you run into any more issues please feel free to re-open! Thanks again!