1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
# ----------------------------------------------------------------------------
# Copyright (c) 2013--, scikit-bio development team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE.txt, distributed with this software.
# ----------------------------------------------------------------------------
import sys
import unittest
try:
import matplotlib
except ImportError:
has_matplotlib = False
else:
has_matplotlib = True
from skbio.util._plotting import PlottableMixin
@unittest.skipUnless(has_matplotlib, "Matplotlib not available.")
class TestPlottableMixin(unittest.TestCase):
def setUp(self):
def _plot(self):
fig, ax = self.plt.subplots()
ax.plot(1, 1, color='k', marker='o')
return fig
PlottableMixin.plot = _plot
def test_get_mpl_plt(self):
obj = PlottableMixin()
# hasn't imported yet
self.assertFalse(hasattr(obj, 'mpl'))
# import Matplotlib if available
obj._get_mpl_plt()
self.assertEqual(obj.mpl.__name__, 'matplotlib')
self.assertEqual(obj.plt.__name__, 'matplotlib.pyplot')
# make Matplotlib unimportable
delattr(obj, 'mpl')
backup = sys.modules['matplotlib']
sys.modules['matplotlib'] = None
with self.assertRaises(ImportError):
obj._get_mpl_plt()
# won't try again if already failed
sys.modules['matplotlib'] = backup
self.assertIsNone(obj.mpl)
with self.assertRaises(ImportError):
obj._get_mpl_plt()
def test_figure_data(self):
obj = PlottableMixin()
obj._get_mpl_plt()
# PNG data are bytes
obs = obj._figure_data('png')
self.assertIsInstance(obs, bytes)
self.assertTrue(len(obs) > 0)
# SVG data are string
obs = obj._figure_data('svg')
self.assertIsInstance(obs, str)
self.assertTrue(len(obs) > 0)
# plotting backend not available
delattr(obj, 'mpl')
backup = sys.modules['matplotlib']
sys.modules['matplotlib'] = None
self.assertIsNone(obj._figure_data())
sys.modules['matplotlib'] = backup
def test_repr_png(self):
obj = PlottableMixin()
obj._get_mpl_plt()
obs = obj._repr_png_()
self.assertIsInstance(obs, bytes)
self.assertTrue(len(obs) > 0)
def test_repr_svg(self):
obj = PlottableMixin()
obj._get_mpl_plt()
obs = obj._repr_svg_()
self.assertIsInstance(obs, str)
self.assertTrue(len(obs) > 0)
def test_png(self):
obj = PlottableMixin()
obj._get_mpl_plt()
obs = obj.png
self.assertIsInstance(obs, bytes)
self.assertTrue(len(obs) > 0)
def test_svg(self):
obj = PlottableMixin()
obj._get_mpl_plt()
obs = obj.svg
self.assertIsInstance(obs, str)
self.assertTrue(len(obs) > 0)
if __name__ == '__main__':
unittest.main()
|