Repository: nilearn/nilearn

Issue:
Nilearn seems to need nose in certain (non-dev) cases
I created 2 new environments to run Nilearn code from pbellec's branch https://github.com/SIMEXP/nilearn
Env1:
```
conda install python=2 cython matplotlib jupyterlab scikit-learn
pip install -e .
```
Env2:
```
conda install python=2.7 scikit-learn=0.18
 pip install nibabel==2.0.2
conda install jupyterlab
 pip install -e .
```
Then I ran this code in Jupyter lab notebooks in both environment:
```
# one motor contrast map from NeuroVault
from nilearn import datasets
from nilearn.plotting import view_stat_map
motor_images = datasets.fetch_neurovault_motor_task()
view_stat_map(motor_images.images[0], cut_coords=[-4,-7,49], draw_cross=True,dim=0, opacity=0.5,
              colorbar=True, annotate=True, title="Motor Activation", resampling_interpolation='nearest', 
              threshold=2)
```
In Env2 but not Env1, I get the error:
![image](https://user-images.githubusercontent.com/8203204/47651554-d6752400-db83-11e8-8669-faa40ee54f07.png)

I will test this in Nilearn master as well. Nilearn should not need to install nose libraries when using (not developing) Nilearn.
Neither of the envs have nose installed, but I am getting the error in only Env2, not 1.


Discussion:
Don't tihnk i can replicate this and I have not seen this since. Closing.
I have just run into this. I a clean environment with nilearn and nibabel installed I'm getting:

```
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
/srv/conda/lib/python3.6/site-packages/nilearn/plotting/__init__.py in _set_mpl_backend()
     11         # We are doing local imports here to avoid poluting our namespace
---> 12         import matplotlib
     13         import os

ModuleNotFoundError: No module named 'matplotlib'

During handling of the above exception, another exception occurred:

ModuleNotFoundError                       Traceback (most recent call last)
/srv/conda/lib/python3.6/site-packages/nilearn/_utils/testing.py in <module>()
     20 try:
---> 21     from nose.tools import assert_raises_regex
     22 except ImportError:

ModuleNotFoundError: No module named 'nose'

During handling of the above exception, another exception occurred:

ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-0715ca1b6298> in <module>()
      8 from nimare.meta.ibma import stouffers, weighted_stouffers
      9 from nilearn.masking import apply_mask
---> 10 from nilearn.plotting import plot_roi, plot_stat_map, plot_glass_brain
     11 
     12 template_nii = load_mni152_template()

/srv/conda/lib/python3.6/site-packages/nilearn/plotting/__init__.py in <module>()
     32                                           OPTIONAL_MATPLOTLIB_MIN_VERSION)
     33 
---> 34 _set_mpl_backend()
     35 
     36 ###############################################################################

/srv/conda/lib/python3.6/site-packages/nilearn/plotting/__init__.py in _set_mpl_backend()
     20             matplotlib.use('Agg')
     21     except ImportError:
---> 22         from .._utils.testing import skip_if_running_nose
     23         # No need to fail when running tests
     24         skip_if_running_nose('matplotlib not installed')

/srv/conda/lib/python3.6/site-packages/nilearn/_utils/testing.py in <module>()
     22 except ImportError:
     23     # For Py 2.7
---> 24     from nose.tools import assert_raises_regexp as assert_raises_regex
     25 
     26 

ModuleNotFoundError: No module named 'nose'
```
Thanks. Time to go a-tinkering!
Thx for posting. @kchawla-pi can you take care of that ?
Looking into this. Does it only happen when Matplotlib is not installed?
Not sure, I installed both nose and matplotlib to resolve the issue.

On Tue, Dec 4, 2018, 1:20 AM Kshitij Chawla <notifications@github.com wrote:

> Looking into this. Does it only happen when Matplotlib is not installed?
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <https://github.com/nilearn/nilearn/issues/1846#issuecomment-444028783>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/AAOkp95BWAZPkpvTRdM4K1PzCHirbUzOks5u1j55gaJpZM4X_Inc>
> .
>

I believe this happens because nose is being imported/used in the `__init__.py` file for the `plotting` & obliquely, the `testing` module.
```Python traceback
git grep -n nose | grep __init__
nilearn/__init__.py:88:        from ._utils.testing import is_nose_running
nilearn/__init__.py:89:        if is_nose_running():
nilearn/plotting/__init__.py:22:        from .._utils.testing import skip_if_running_nose
nilearn/plotting/__init__.py:24:        skip_if_running_nose('matplotlib not installed')
```

If we remove the imports referring to test runners, `nose` or `pytest` in future, this will be fixed.
I will open a PR for this.

Resolving patch:
diff --git a/nilearn/plotting/__init__.py b/nilearn/plotting/__init__.py
--- a/nilearn/plotting/__init__.py
+++ b/nilearn/plotting/__init__.py
@@ -1,23 +1,18 @@
 """
 Plotting code for nilearn
 """
-# Authors: Chris Filo Gorgolewski, Gael Varoquaux
+# Original Authors: Chris Filo Gorgolewski, Gael Varoquaux
+import os
+import sys
+
 
 ###############################################################################
 # Make sure that we don't get DISPLAY problems when running without X on
 # unices
 def _set_mpl_backend():
+    # We are doing local imports here to avoid poluting our namespace
     try:
-        # We are doing local imports here to avoid poluting our namespace
         import matplotlib
-        import os
-        import sys
-        # Set the backend to a non-interactive one for unices without X
-        if (os.name == 'posix' and 'DISPLAY' not in os.environ
-            and not (sys.platform == 'darwin'
-                     and matplotlib.get_backend() == 'MacOSX'
-                     )):
-            matplotlib.use('Agg')
     except ImportError:
         from .._utils.testing import skip_if_running_nose
         # No need to fail when running tests
@@ -30,6 +25,12 @@ def _set_mpl_backend():
         # that the version is greater that the minimum required one
         _import_module_with_version_check('matplotlib',
                                           OPTIONAL_MATPLOTLIB_MIN_VERSION)
+        # Set the backend to a non-interactive one for unices without X
+        if (os.name == 'posix' and 'DISPLAY' not in os.environ
+                and not (sys.platform == 'darwin'
+                         and matplotlib.get_backend() == 'MacOSX')
+        ):
+            matplotlib.use('Agg')
 
 _set_mpl_backend()
 


Test patch:
diff --git a/nilearn/_utils/testing.py b/nilearn/_utils/testing.py
--- a/nilearn/_utils/testing.py
+++ b/nilearn/_utils/testing.py
@@ -11,19 +11,12 @@
 import gc
 
 import numpy as np
-from numpy.testing import assert_warns  # noqa: F401
+from numpy.testing import assert_raises_regex, assert_warns  # noqa: F401
 
 from .compat import _basestring, _urllib
 from ..datasets.utils import _fetch_files
 
 
-try:
-    from nose.tools import assert_raises_regex
-except ImportError:
-    # For Py 2.7
-    from nose.tools import assert_raises_regexp as assert_raises_regex
-
-
 # we use memory_profiler library for memory consumption checks
 try:
     from memory_profiler import memory_usage