From 7eacfcedab3e19d9b18a31d0357a127b3afc7fc8 Mon Sep 17 00:00:00 2001
From: David Rafferty <rafferty@strw.leidenuniv.nl>
Date: Fri, 30 Nov 2012 10:10:12 +0000
Subject: [PATCH] Task #3403: Work around for bug in matplotlib that caused
 crash when no display variable is set.

---
 CEP/PyBDSM/src/python/__init__.py       | 18 ++++--------------
 CEP/PyBDSM/src/python/cleanup.py        | 13 +++++++------
 CEP/PyBDSM/src/python/gausfit.py        | 11 ++++++-----
 CEP/PyBDSM/src/python/plotresults.py    |  6 ++----
 CEP/PyBDSM/src/python/psf_vary.py       | 11 ++++++-----
 CEP/PyBDSM/src/python/wavelet_atrous.py | 11 ++++++-----
 CEP/PyBDSM/test/tbdsm_process_image.py  |  1 +
 7 files changed, 32 insertions(+), 39 deletions(-)

diff --git a/CEP/PyBDSM/src/python/__init__.py b/CEP/PyBDSM/src/python/__init__.py
index e6b53202ead..9552ee2bb0a 100644
--- a/CEP/PyBDSM/src/python/__init__.py
+++ b/CEP/PyBDSM/src/python/__init__.py
@@ -9,20 +9,10 @@ required by 'execute').
 """
 try:
     import matplotlib.pyplot as pl
-except RuntimeError:
-    # Set use of AGG backend to avoid problems when there
-    # is no DISPLAY variable set
-    import sys
-    modules = []
-    for module in sys.modules:
-        if module.startswith('matplotlib'):
-            modules.append(module)
-    for module in modules:
-        sys.modules.pop(module)
-    import matplotlib as mpl
-    mpl.use('Agg')
-except ImportError:
-    print "\033[31;1mWARNING\033[0m: Matplotlib not found. Plotting is disabled."
+    has_pl = True
+except (RuntimeError, ImportError):
+    has_pl = False
+    print "\033[33;1mWARNING\033[0m: Matplotlib not found. Plotting is disabled."
 
 from readimage import Op_readimage
 from collapse import Op_collapse
diff --git a/CEP/PyBDSM/src/python/cleanup.py b/CEP/PyBDSM/src/python/cleanup.py
index 5042a9940fd..f73f331f4c3 100644
--- a/CEP/PyBDSM/src/python/cleanup.py
+++ b/CEP/PyBDSM/src/python/cleanup.py
@@ -6,12 +6,13 @@ import numpy as N
 import os
 from image import *
 import mylogger, os
-try:
-    import matplotlib.pyplot as pl
-    import matplotlib.cm as cm
-    has_pl = True
-except ImportError:
-    has_pl = False
+from . import has_pl
+#try:
+#    import matplotlib.pyplot as pl
+#    import matplotlib.cm as cm
+#    has_pl = True
+#except ImportError:
+#    has_pl = False
 import functions as func
 
 class Op_cleanup(Op):
diff --git a/CEP/PyBDSM/src/python/gausfit.py b/CEP/PyBDSM/src/python/gausfit.py
index 0b27e19dd01..2727c30500d 100644
--- a/CEP/PyBDSM/src/python/gausfit.py
+++ b/CEP/PyBDSM/src/python/gausfit.py
@@ -19,11 +19,12 @@ import sys
 import time
 import statusbar
 import _cbdsm
-try:
-    import matplotlib.pyplot as pl
-    has_pl = True
-except ImportError:
-    has_pl = False
+from . import has_pl
+#try:
+#    import matplotlib.pyplot as pl
+#    has_pl = True
+#except ImportError:
+#    has_pl = False
 import scipy.ndimage as nd
 import multi_proc as mp
 
diff --git a/CEP/PyBDSM/src/python/plotresults.py b/CEP/PyBDSM/src/python/plotresults.py
index f2956b98f75..8a93f4a243d 100644
--- a/CEP/PyBDSM/src/python/plotresults.py
+++ b/CEP/PyBDSM/src/python/plotresults.py
@@ -3,7 +3,8 @@
 This module is used to display fits results.
 """
 from image import *
-try:
+from . import has_pl
+if has_pl:
     import matplotlib.pyplot as pl
     import matplotlib.cm as cm
     import matplotlib.patches as mpatches
@@ -11,9 +12,6 @@ try:
     from matplotlib.patches import Ellipse
     from matplotlib.lines import Line2D
     from matplotlib import collections
-    has_pl = True
-except ImportError:
-    has_pl = False
 from math import log10
 import functions as func
 from const import fwsig
diff --git a/CEP/PyBDSM/src/python/psf_vary.py b/CEP/PyBDSM/src/python/psf_vary.py
index 252e09f3d02..19301635699 100644
--- a/CEP/PyBDSM/src/python/psf_vary.py
+++ b/CEP/PyBDSM/src/python/psf_vary.py
@@ -3,11 +3,12 @@ import numpy as N
 from image import *
 import mylogger
 from copy import deepcopy as cp
-try:
-    import matplotlib.pyplot as pl
-    has_pl = True
-except ImportError:
-    has_pl = False
+from . import has_pl
+#try:
+#    import matplotlib.pyplot as pl
+#    has_pl = True
+#except ImportError:
+#    has_pl = False
 import scipy
 import scipy.signal as S
 import _cbdsm
diff --git a/CEP/PyBDSM/src/python/wavelet_atrous.py b/CEP/PyBDSM/src/python/wavelet_atrous.py
index 3902ef21b6c..23e4a5342d2 100644
--- a/CEP/PyBDSM/src/python/wavelet_atrous.py
+++ b/CEP/PyBDSM/src/python/wavelet_atrous.py
@@ -8,11 +8,12 @@ import numpy as N
 from image import *
 import mylogger
 import os
-try:
-    import matplotlib.pyplot as pl
-    has_pl = True
-except ImportError:
-    has_pl = False
+from . import has_pl
+#try:
+#    import matplotlib.pyplot as pl
+#    has_pl = True
+#except ImportError:
+#    has_pl = False
 import _cbdsm
 from math import log, floor, sqrt
 import scipy.signal as S
diff --git a/CEP/PyBDSM/test/tbdsm_process_image.py b/CEP/PyBDSM/test/tbdsm_process_image.py
index cc17601e864..e0cb23b25c0 100644
--- a/CEP/PyBDSM/test/tbdsm_process_image.py
+++ b/CEP/PyBDSM/test/tbdsm_process_image.py
@@ -13,6 +13,7 @@ operations = [
 
 # Return exit status 0 if everything went fine, otherwise return 1.
 if img and all(oper in img.completed_Ops for oper in operations):
+    print 'here'
     sys.exit(0)
 else:
     sys.exit(1)
-- 
GitLab