Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UserWarning thrown when importing hypertools in jupyter notebook #145

Closed
jeremymanning opened this issue Aug 11, 2017 · 13 comments
Closed

Comments

@jeremymanning
Copy link
Member

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
Copy link
Collaborator

andrewheusser commented Aug 11, 2017

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

@andrewheusser
Copy link
Collaborator

andrewheusser commented Aug 11, 2017

also, which matplotlib and jupyter version?

@jeremymanning
Copy link
Member Author

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
Copy link
Collaborator

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
Copy link
Member Author

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

@andrewheusser
Copy link
Collaborator

andrewheusser commented Aug 11, 2017

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
Copy link
Member Author

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
Copy link
Member Author

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

@andrewheusser
Copy link
Collaborator

Sounds good.

this is the line to get fonts to export correctly:

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

@andrewheusser
Copy link
Collaborator

this is fixed on 9de0a51. i added:

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    mpl.use('TkAgg')

@jeremymanning
Copy link
Member Author

🍾

@andrewheusser
Copy link
Collaborator

ill push this to pip as 0.3.1

@jeremymanning
Copy link
Member Author

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants