Repository: nipy/nipype

Issue:
tkagg display error
@mwaskom:  shouldn't this not happen with tkagg?

```
  File "/srv/software/python/EPD/virtualenvs/7.2/nipype0.5/lib/python2.7/site-packages/nipype/algorithms/rapidart.py", line 357, in _detect_outliers_core
    fig = plt.figure()
  File "/software/python/EPD/7.2/lib/python2.7/site-packages/matplotlib/pyplot.py", line 343, in figure
    **kwargs)
  File "/software/python/EPD/7.2/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 80, in new_figure_manager
    window = Tk.Tk()
  File "/software/python/EPD/7.2/lib/python2.7/lib-tk/Tkinter.py", line 1685, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
TclError: couldn't connect to display "computor:1.0"
```



Discussion:
The Tk backend still needs a display to draw, I would assume?  To draw without a display, use the Agg backend.

hmmm ... so that config option is not being executed properly .. let me take a look.

If you're running this from e.g. IPython and import pyplot before importing nipype, the nipype config option will not take effect.

in some recent changes i took out the matplotlib lines that set agg in the interface code. i'll push a fix shortly.

@satra : i don't know how to reproduce the issue and it's pretty old, but i'm guessing that `matplotlib.use('Agg')` or `matplotlib.use(config.get('execution', 'matplotlib_backend'))` solved problems in most of the cases. 
I can see a few places that import `matplotlib` and don't set backend (e.g. https://github.com/nipy/nipype/blob/master/nipype/algorithms/metrics.py#L141). i can search the code and `matplotlib.use(config.get('execution', 'matplotlib_backend'))`
@satra @djarecka What if we clear up all the matplotlib backend options and recommend users to change the matplotlib default? - https://github.com/nipy/nipype/blob/master/Dockerfile#L71
Not sure what is easier to "maintain". There are only a few files where you import `matplotlib`, so can review if you decide to keep the `matplotlib` backend options in the code.

The only thing I'm not completely sure is `matplotlib.is_interactive()`, should we ensure to be `False` regardless of backend? The default is `False`, so it's usually `False` anyway, but I'm not sure if this can also cause a problem.
@satra , @oesteban do we still want to do anything about it?

Resolving patch:
diff --git a/nipype/algorithms/confounds.py b/nipype/algorithms/confounds.py
--- a/nipype/algorithms/confounds.py
+++ b/nipype/algorithms/confounds.py
@@ -22,7 +22,7 @@
 from numpy.polynomial import Legendre
 from scipy import linalg
 
-from .. import logging
+from .. import config, logging
 from ..external.due import BibTeX
 from ..interfaces.base import (traits, TraitedSpec, BaseInterface,
                                BaseInterfaceInputSpec, File, isdefined,
@@ -816,7 +816,7 @@ def plot_confound(tseries, figsize, name, units=None,
 
     """
     import matplotlib
-    matplotlib.use('Agg')
+    matplotlib.use(config.get('execution', 'matplotlib_backend'))
     import matplotlib.pyplot as plt
     from matplotlib.gridspec import GridSpec
     from matplotlib.backends.backend_pdf import FigureCanvasPdf as FigureCanvas
diff --git a/nipype/algorithms/metrics.py b/nipype/algorithms/metrics.py
--- a/nipype/algorithms/metrics.py
+++ b/nipype/algorithms/metrics.py
@@ -24,7 +24,7 @@
 from scipy.spatial.distance import cdist, euclidean, dice, jaccard
 from scipy.ndimage.measurements import center_of_mass, label
 
-from .. import logging
+from .. import config, logging
 from ..utils.misc import package_check
 
 from ..interfaces.base import (BaseInterface, traits, TraitedSpec, File,
@@ -138,6 +138,8 @@ def _eucl_mean(self, nii1, nii2, weighted=False):
 
         dist_matrix = cdist(set1_coordinates.T, set2_coordinates.T)
         min_dist_matrix = np.amin(dist_matrix, axis=0)
+        import matplotlib
+        matplotlib.use(config.get('execution', 'matplotlib_backend'))
         import matplotlib.pyplot as plt
         plt.figure()
         plt.hist(min_dist_matrix, 50, normed=1, facecolor='green')


Test patch: