ContextLab / hypertools

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

UserWarning thrown when importing hypertools in jupyter notebook #145

Closed jeremymanning closed 7 years ago

jeremymanning commented 7 years ago

When importing hypertools in a jupyter notebook (import hypertools as hyp), the following warning is thrown:

/opt/conda/lib/python3.6/site-packages/matplotlib/__init__.py:1405: UserWarning: 
This call to matplotlib.use() has no effect because the backend has already
been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.

  warnings.warn(_use_error_msg)

Proposed fix: suppress this warning within hypertools (more info here)

andrewheusser commented 7 years ago

@jeremymanning hmmm i'm having trouble replicating this warning. can you verify that you are running version 0.3.0?

andrewheusser commented 7 years ago

also, which matplotlib and jupyter version?

jeremymanning commented 7 years ago

here's the version info (all of this is from the MIND Docker Container

root@2ca632e4000c:/# pip show matplotlib
Name: matplotlib
Version: 2.0.2
Summary: Python plotting package
Home-page: http://matplotlib.org
Author: John D. Hunter, Michael Droettboom
Author-email: matplotlib-users@python.org
License: BSD
Location: /opt/conda/lib/python3.6/site-packages
Requires: numpy, six, python-dateutil, pytz, cycler, pyparsing
root@2ca632e4000c:/# pip show hypertools
Name: hypertools
Version: 0.3.0
Summary: A python package for visualizing and manipulating high-dimensional data
Home-page: https://github.com/ContextLab/hypertools
Author: Contextual Dynamics Lab
Author-email: contextualdynamics@gmail.com
License: MIT
Location: /opt/conda/lib/python3.6/site-packages
Requires: scipy, seaborn, PPCA, scikit-learn, future, requests, matplotlib, numpy, pandas
root@2ca632e4000c:/# pip show jupyter   
Name: jupyter
Version: 1.0.0
Summary: Jupyter metapackage. Install all the Jupyter components in one go.
Home-page: http://jupyter.org
Author: Jupyter Development Team
Author-email: jupyter@googlegroups.org
License: BSD
Location: /opt/conda/lib/python3.6/site-packages
Requires: 
andrewheusser commented 7 years ago

got it, thanks. I was able to replicate this by importing hypertools and seaborn in the same notebook. They both rely on matplotlib and both attempt to change the default matplotlib backend.
The issue comes when any package imports matplotlib with a particular backend. after that happens hypertools is not able to switch it.

I can suppress the warning, but it also may be informative to throw a different error that says that hypertools couldn't switch the matplotlib backend, so it may not work properly. Currently, we switch the backend from Agg to TkAgg because if you don't, any you are using matplotlib 2.0, when attempting to drag the 3D plots, its super choppy for whatever reason.

jeremymanning commented 7 years ago

But don't we import seaborn from within hypertools? And if so, what's the purpose of the warning?

andrewheusser commented 7 years ago

The purpose of the warning is to let the user know that the matplotlib backend was not switched.

hmm, yea we do import seaborn, but we import matplotlib before anything else, and I think seaborn supresses the warning. This is in our top level init.py script:

import matplotlib as mpl
mpl.use('TkAgg')

In the jupyter notebook you tried, do you import seaborn or anything else relying on matplotlib before hypertools?

jeremymanning commented 7 years ago

Ah-- it seems like this could be it. I get the warning when importing one or both of seaborn and nilearn.plotting. The warning goes away when both of those are removed.

However, if both seaborn and nilearn mess with the matplotlib backend (but don't output warnings when they are imported together), I think hypertools should follow that behavior as well (by suppressing this warning).

Basically, my thinking is that I don't see the warning being useful, but it makes hypertools look slightly messier.

Side question: was the mpl.use('TkAgg') need to get fonts to export correctly?

jeremymanning commented 7 years ago

Another note: it doesn't seem to matter if hypertools is imported first or last.

andrewheusser commented 7 years ago

Sounds good.

this is the line to get fonts to export correctly:

matplotlib.rcParams['pdf.fonttype'] = 42

andrewheusser commented 7 years ago

this is fixed on 9de0a517486e1e2acf8eb4532d546d59db423e82. i added:

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    mpl.use('TkAgg')
jeremymanning commented 7 years ago

🍾

andrewheusser commented 7 years ago

ill push this to pip as 0.3.1

jeremymanning commented 7 years ago

thanks!