Repository: mne-tools/mne-python

Issue:
DeprecationWarning in pandas after importing mne
Whenever I

```
import mne
import pandas as pd
```

I get the following warning:

```
/Users/test/anaconda/lib/python3.5/site-packages/pandas/__init__.py:7: DeprecationWarning: bad escape \s
  from pandas import hashtable, tslib, lib
```

Importing `pandas` in a clean session works. This is a fresh Anaconda install where I added MNE via `pip install mne`.



Discussion:
not an MNE issue ... maybe different pandas versions on your system?

On Thu, Feb 11, 2016 at 1:36 PM, Clemens Brunner notifications@github.com
wrote:

> Whenever I
> 
> import mne
> import pandas as pd
> 
> I get the following warning:
> 
> /Users/test/anaconda/lib/python3.5/site-packages/pandas/**init**.py:7: DeprecationWarning: bad escape \s
>   from pandas import hashtable, tslib, lib
> 
> Importing pandas in a clean session works. This is a fresh Anaconda
> install where I added MNE via pip install mne.
> 
> —
> Reply to this email directly or view it on GitHub
> https://github.com/mne-tools/mne-python/issues/2870.

I just installed a fresh Anaconda on a different Windows machine and I get the same warning. I only see this message if I first import `mne`. So MNE is at least part of the problem.

mne is maybe affecting a global setting on warnings ...

is this recent on mne master?

Oh right. Yes we do:

https://github.com/mne-tools/mne-python/blob/master/mne/utils.py#L463

(and we've had that line for a _long_ time, so it's more likely that Pandas has some recent change that now elicits a DeprecationWarning than it is that we changed something)

So - is changing the warning state necessary? And I don't quite understand the warning anyway, what's wrong with the Pandas code? Should they change anything?

It's necessary if we want users to see Deprecation messages, which are pretty useful. So I think we should keep it. The warning is probably due to some change in Pandas code, but they might not see they are using some deprecated behavior because they don't have them enabled by default.

...but before bothering them about it, you might want to verify that replacing `import mne` with some suitable `warnings` call before the `import pandas` does indeed elicit the same warning.

Yes. The following minimal example triggers the warning:

```
import warnings
warnings.simplefilter("default")
import pandas as pd
```

I'll let the Pandas devs know.

However, I'm still not convinced that changing the warnings filter is a good idea. As we saw, it might trigger a whole lot of warnings in other packages. Since DeprecationWarnings are ignored by default, there must be a way to show these to the user other than changing the global warnings filter (I think scikit-learn is showing DeprecationWarnings to users, but without changing the global warnings state).

Indeed:

```
warnings.filterwarnings('always', category=DeprecationWarning,
                        module='^{0}\.'.format(re.escape(__name__)))
```

I'll open a PR


Resolving patch:
diff --git a/mne/utils.py b/mne/utils.py
--- a/mne/utils.py
+++ b/mne/utils.py
@@ -6,27 +6,26 @@
 #
 # License: BSD (3-clause)
 
-import warnings
-import logging
-import time
-import platform
+import atexit
 from distutils.version import LooseVersion
-import os
-import os.path as op
 from functools import wraps
+import hashlib
 import inspect
+import json
+import logging
+from math import log, ceil
+import os
+import os.path as op
+import platform
+import shutil
+from shutil import rmtree
 from string import Formatter
 import subprocess
 import sys
 import tempfile
-import shutil
-from shutil import rmtree
-from math import log, ceil
-import json
+import time
+import warnings
 import ftplib
-import hashlib
-from functools import partial
-import atexit
 
 import numpy as np
 from scipy import linalg, sparse
@@ -35,7 +34,7 @@
 from .externals.six import string_types, StringIO, BytesIO
 from .externals.decorator import decorator
 
-from .fixes import _get_args
+from .fixes import _get_args, partial
 
 logger = logging.getLogger('mne')  # one selection here used across mne-python
 logger.propagate = False  # don't propagate (in case of multiple imports)
@@ -468,7 +467,7 @@ def trait_wraith(*args, **kwargs):
 # Following deprecated class copied from scikit-learn
 
 # force show of DeprecationWarning even on python 2.7
-warnings.simplefilter('default')
+warnings.filterwarnings('always', category=DeprecationWarning, module='mne')
 
 
 class deprecated(object):


Test patch:
diff --git a/mne/tests/test_dipole.py b/mne/tests/test_dipole.py
--- a/mne/tests/test_dipole.py
+++ b/mne/tests/test_dipole.py
@@ -121,7 +121,7 @@ def test_dipole_fitting():
     # XXX weird, inexplicable differenc for 3.5 build we'll assume is due to
     # Anaconda bug for now...
     if os.getenv('TRAVIS', 'false') == 'true' and \
-            sys.version.startswith('3.5'):
+            sys.version[:3] in ('3.5', '2.7'):
         factor = 0.8
     assert_true((data_rms > factor * resi_rms).all(),
                 msg='%s (factor: %s)' % ((data_rms / resi_rms).min(), factor))