Show More
@@ -1,224 +1,241 b'' | |||
|
1 | 1 | """Tests for pylab tools module. |
|
2 | 2 | """ |
|
3 | 3 | #----------------------------------------------------------------------------- |
|
4 | 4 | # Copyright (c) 2011, the IPython Development Team. |
|
5 | 5 | # |
|
6 | 6 | # Distributed under the terms of the Modified BSD License. |
|
7 | 7 | # |
|
8 | 8 | # The full license is in the file COPYING.txt, distributed with this software. |
|
9 | 9 | #----------------------------------------------------------------------------- |
|
10 | 10 | |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | # Imports |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | from __future__ import print_function |
|
15 | 15 | |
|
16 | from io import UnsupportedOperation, BytesIO | |
|
17 | ||
|
16 | 18 | import matplotlib |
|
17 | 19 | matplotlib.use('Agg') |
|
18 | 20 | from matplotlib.figure import Figure |
|
19 | 21 | |
|
22 | from nose import SkipTest | |
|
20 | 23 | import nose.tools as nt |
|
21 | 24 | |
|
22 | 25 | from matplotlib import pyplot as plt |
|
23 | 26 | import numpy as np |
|
24 | 27 | |
|
25 | 28 | # Our own imports |
|
26 | 29 | from IPython.core.getipython import get_ipython |
|
27 | 30 | from IPython.core.interactiveshell import InteractiveShell |
|
28 | 31 | from IPython.core.display import _PNG, _JPEG |
|
29 | 32 | from .. import pylabtools as pt |
|
30 | 33 | |
|
31 | 34 | from IPython.testing import decorators as dec |
|
32 | 35 | |
|
33 | 36 | #----------------------------------------------------------------------------- |
|
34 | 37 | # Globals and constants |
|
35 | 38 | #----------------------------------------------------------------------------- |
|
36 | 39 | |
|
37 | 40 | #----------------------------------------------------------------------------- |
|
38 | 41 | # Local utilities |
|
39 | 42 | #----------------------------------------------------------------------------- |
|
40 | 43 | |
|
41 | 44 | #----------------------------------------------------------------------------- |
|
42 | 45 | # Classes and functions |
|
43 | 46 | #----------------------------------------------------------------------------- |
|
44 | 47 | |
|
45 | 48 | def test_figure_to_svg(): |
|
46 | 49 | # simple empty-figure test |
|
47 | 50 | fig = plt.figure() |
|
48 | 51 | nt.assert_equal(pt.print_figure(fig, 'svg'), None) |
|
49 | 52 | |
|
50 | 53 | plt.close('all') |
|
51 | 54 | |
|
52 | 55 | # simple check for at least svg-looking output |
|
53 | 56 | fig = plt.figure() |
|
54 | 57 | ax = fig.add_subplot(1,1,1) |
|
55 | 58 | ax.plot([1,2,3]) |
|
56 | 59 | plt.draw() |
|
57 | 60 | svg = pt.print_figure(fig, 'svg')[:100].lower() |
|
58 | 61 | nt.assert_in(b'doctype svg', svg) |
|
59 | 62 | |
|
63 | def _check_pil_jpeg_bytes(): | |
|
64 | """Skip if PIL can't write JPEGs to BytesIO objects""" | |
|
65 | # PIL's JPEG plugin can't write to BytesIO objects | |
|
66 | # Pillow fixes this | |
|
67 | from PIL import Image | |
|
68 | buf = BytesIO() | |
|
69 | img = Image.new("RGB", (4,4)) | |
|
70 | try: | |
|
71 | img.save(buf, 'jpeg') | |
|
72 | except Exception as e: | |
|
73 | ename = e.__class__.__name__ | |
|
74 | raise SkipTest("PIL can't write JPEG to BytesIO: %s: %s" % (ename, e)) | |
|
75 | ||
|
60 | 76 | @dec.skip_without("PIL.Image") |
|
61 | def test_figure_to_jpg(): | |
|
62 | # simple check for at least jpg-looking output | |
|
77 | def test_figure_to_jpeg(): | |
|
78 | _check_pil_jpeg_bytes() | |
|
79 | # simple check for at least jpeg-looking output | |
|
63 | 80 | fig = plt.figure() |
|
64 | 81 | ax = fig.add_subplot(1,1,1) |
|
65 | 82 | ax.plot([1,2,3]) |
|
66 | 83 | plt.draw() |
|
67 | jpg = pt.print_figure(fig, 'jpg', quality=50)[:100].lower() | |
|
68 | assert jpg.startswith(_JPEG) | |
|
84 | jpeg = pt.print_figure(fig, 'jpeg', quality=50)[:100].lower() | |
|
85 | assert jpeg.startswith(_JPEG) | |
|
69 | 86 | |
|
70 | 87 | def test_retina_figure(): |
|
71 | 88 | fig = plt.figure() |
|
72 | 89 | ax = fig.add_subplot(1,1,1) |
|
73 | 90 | ax.plot([1,2,3]) |
|
74 | 91 | plt.draw() |
|
75 | 92 | png, md = pt.retina_figure(fig) |
|
76 | 93 | assert png.startswith(_PNG) |
|
77 | 94 | nt.assert_in('width', md) |
|
78 | 95 | nt.assert_in('height', md) |
|
79 | 96 | |
|
80 | 97 | _fmt_mime_map = { |
|
81 | 98 | 'png': 'image/png', |
|
82 | 99 | 'jpeg': 'image/jpeg', |
|
83 | 100 | 'pdf': 'application/pdf', |
|
84 | 101 | 'retina': 'image/png', |
|
85 | 102 | 'svg': 'image/svg+xml', |
|
86 | 103 | } |
|
87 | 104 | |
|
88 | 105 | def test_select_figure_formats_str(): |
|
89 | 106 | ip = get_ipython() |
|
90 | 107 | for fmt, active_mime in _fmt_mime_map.items(): |
|
91 | 108 | pt.select_figure_formats(ip, fmt) |
|
92 | 109 | for mime, f in ip.display_formatter.formatters.items(): |
|
93 | 110 | if mime == active_mime: |
|
94 | 111 | nt.assert_in(Figure, f) |
|
95 | 112 | else: |
|
96 | 113 | nt.assert_not_in(Figure, f) |
|
97 | 114 | |
|
98 | 115 | def test_select_figure_formats_kwargs(): |
|
99 | 116 | ip = get_ipython() |
|
100 | 117 | kwargs = dict(quality=10, bbox_inches='tight') |
|
101 | 118 | pt.select_figure_formats(ip, 'png', **kwargs) |
|
102 | 119 | formatter = ip.display_formatter.formatters['image/png'] |
|
103 | 120 | f = formatter.lookup_by_type(Figure) |
|
104 | 121 | cell = f.__closure__[0].cell_contents |
|
105 | 122 | nt.assert_equal(cell, kwargs) |
|
106 | 123 | |
|
107 | 124 | # check that the formatter doesn't raise |
|
108 | 125 | fig = plt.figure() |
|
109 | 126 | ax = fig.add_subplot(1,1,1) |
|
110 | 127 | ax.plot([1,2,3]) |
|
111 | 128 | plt.draw() |
|
112 | 129 | formatter.enabled = True |
|
113 | 130 | png = formatter(fig) |
|
114 | 131 | assert png.startswith(_PNG) |
|
115 | 132 | |
|
116 | 133 | def test_select_figure_formats_set(): |
|
117 | 134 | ip = get_ipython() |
|
118 | 135 | for fmts in [ |
|
119 | 136 | {'png', 'svg'}, |
|
120 | 137 | ['png'], |
|
121 | 138 | ('jpeg', 'pdf', 'retina'), |
|
122 | 139 | {'svg'}, |
|
123 | 140 | ]: |
|
124 | 141 | active_mimes = {_fmt_mime_map[fmt] for fmt in fmts} |
|
125 | 142 | pt.select_figure_formats(ip, fmts) |
|
126 | 143 | for mime, f in ip.display_formatter.formatters.items(): |
|
127 | 144 | if mime in active_mimes: |
|
128 | 145 | nt.assert_in(Figure, f) |
|
129 | 146 | else: |
|
130 | 147 | nt.assert_not_in(Figure, f) |
|
131 | 148 | |
|
132 | 149 | def test_select_figure_formats_bad(): |
|
133 | 150 | ip = get_ipython() |
|
134 | 151 | with nt.assert_raises(ValueError): |
|
135 | 152 | pt.select_figure_formats(ip, 'foo') |
|
136 | 153 | with nt.assert_raises(ValueError): |
|
137 | 154 | pt.select_figure_formats(ip, {'png', 'foo'}) |
|
138 | 155 | with nt.assert_raises(ValueError): |
|
139 | 156 | pt.select_figure_formats(ip, ['retina', 'pdf', 'bar', 'bad']) |
|
140 | 157 | |
|
141 | 158 | def test_import_pylab(): |
|
142 | 159 | ns = {} |
|
143 | 160 | pt.import_pylab(ns, import_all=False) |
|
144 | 161 | nt.assert_true('plt' in ns) |
|
145 | 162 | nt.assert_equal(ns['np'], np) |
|
146 | 163 | |
|
147 | 164 | class TestPylabSwitch(object): |
|
148 | 165 | class Shell(InteractiveShell): |
|
149 | 166 | def enable_gui(self, gui): |
|
150 | 167 | pass |
|
151 | 168 | |
|
152 | 169 | def setup(self): |
|
153 | 170 | import matplotlib |
|
154 | 171 | def act_mpl(backend): |
|
155 | 172 | matplotlib.rcParams['backend'] = backend |
|
156 | 173 | |
|
157 | 174 | # Save rcParams since they get modified |
|
158 | 175 | self._saved_rcParams = matplotlib.rcParams |
|
159 | 176 | self._saved_rcParamsOrig = matplotlib.rcParamsOrig |
|
160 | 177 | matplotlib.rcParams = dict(backend='Qt4Agg') |
|
161 | 178 | matplotlib.rcParamsOrig = dict(backend='Qt4Agg') |
|
162 | 179 | |
|
163 | 180 | # Mock out functions |
|
164 | 181 | self._save_am = pt.activate_matplotlib |
|
165 | 182 | pt.activate_matplotlib = act_mpl |
|
166 | 183 | self._save_ip = pt.import_pylab |
|
167 | 184 | pt.import_pylab = lambda *a,**kw:None |
|
168 | 185 | self._save_cis = pt.configure_inline_support |
|
169 | 186 | pt.configure_inline_support = lambda *a,**kw:None |
|
170 | 187 | |
|
171 | 188 | def teardown(self): |
|
172 | 189 | pt.activate_matplotlib = self._save_am |
|
173 | 190 | pt.import_pylab = self._save_ip |
|
174 | 191 | pt.configure_inline_support = self._save_cis |
|
175 | 192 | import matplotlib |
|
176 | 193 | matplotlib.rcParams = self._saved_rcParams |
|
177 | 194 | matplotlib.rcParamsOrig = self._saved_rcParamsOrig |
|
178 | 195 | |
|
179 | 196 | def test_qt(self): |
|
180 | 197 | s = self.Shell() |
|
181 | 198 | gui, backend = s.enable_matplotlib(None) |
|
182 | 199 | nt.assert_equal(gui, 'qt') |
|
183 | 200 | nt.assert_equal(s.pylab_gui_select, 'qt') |
|
184 | 201 | |
|
185 | 202 | gui, backend = s.enable_matplotlib('inline') |
|
186 | 203 | nt.assert_equal(gui, 'inline') |
|
187 | 204 | nt.assert_equal(s.pylab_gui_select, 'qt') |
|
188 | 205 | |
|
189 | 206 | gui, backend = s.enable_matplotlib('qt') |
|
190 | 207 | nt.assert_equal(gui, 'qt') |
|
191 | 208 | nt.assert_equal(s.pylab_gui_select, 'qt') |
|
192 | 209 | |
|
193 | 210 | gui, backend = s.enable_matplotlib('inline') |
|
194 | 211 | nt.assert_equal(gui, 'inline') |
|
195 | 212 | nt.assert_equal(s.pylab_gui_select, 'qt') |
|
196 | 213 | |
|
197 | 214 | gui, backend = s.enable_matplotlib() |
|
198 | 215 | nt.assert_equal(gui, 'qt') |
|
199 | 216 | nt.assert_equal(s.pylab_gui_select, 'qt') |
|
200 | 217 | |
|
201 | 218 | def test_inline(self): |
|
202 | 219 | s = self.Shell() |
|
203 | 220 | gui, backend = s.enable_matplotlib('inline') |
|
204 | 221 | nt.assert_equal(gui, 'inline') |
|
205 | 222 | nt.assert_equal(s.pylab_gui_select, None) |
|
206 | 223 | |
|
207 | 224 | gui, backend = s.enable_matplotlib('inline') |
|
208 | 225 | nt.assert_equal(gui, 'inline') |
|
209 | 226 | nt.assert_equal(s.pylab_gui_select, None) |
|
210 | 227 | |
|
211 | 228 | gui, backend = s.enable_matplotlib('qt') |
|
212 | 229 | nt.assert_equal(gui, 'qt') |
|
213 | 230 | nt.assert_equal(s.pylab_gui_select, 'qt') |
|
214 | 231 | |
|
215 | 232 | def test_qt_gtk(self): |
|
216 | 233 | s = self.Shell() |
|
217 | 234 | gui, backend = s.enable_matplotlib('qt') |
|
218 | 235 | nt.assert_equal(gui, 'qt') |
|
219 | 236 | nt.assert_equal(s.pylab_gui_select, 'qt') |
|
220 | 237 | |
|
221 | 238 | gui, backend = s.enable_matplotlib('gtk') |
|
222 | 239 | nt.assert_equal(gui, 'qt') |
|
223 | 240 | nt.assert_equal(s.pylab_gui_select, 'qt') |
|
224 | 241 |
General Comments 0
You need to be logged in to leave comments.
Login now