Show More
@@ -12,12 +12,10 b' import matplotlib' | |||||
12 | matplotlib.use('Agg') |
|
12 | matplotlib.use('Agg') | |
13 | from matplotlib.figure import Figure |
|
13 | from matplotlib.figure import Figure | |
14 |
|
14 | |||
15 | from nose import SkipTest |
|
|||
16 | import nose.tools as nt |
|
|||
17 |
|
||||
18 | from matplotlib import pyplot as plt |
|
15 | from matplotlib import pyplot as plt | |
19 | import matplotlib_inline |
|
16 | from matplotlib_inline import backend_inline | |
20 | import numpy as np |
|
17 | import numpy as np | |
|
18 | import pytest | |||
21 |
|
19 | |||
22 | from IPython.core.getipython import get_ipython |
|
20 | from IPython.core.getipython import get_ipython | |
23 | from IPython.core.interactiveshell import InteractiveShell |
|
21 | from IPython.core.interactiveshell import InteractiveShell | |
@@ -30,7 +28,7 b' from IPython.testing import decorators as dec' | |||||
30 | def test_figure_to_svg(): |
|
28 | def test_figure_to_svg(): | |
31 | # simple empty-figure test |
|
29 | # simple empty-figure test | |
32 | fig = plt.figure() |
|
30 | fig = plt.figure() | |
33 |
|
|
31 | assert pt.print_figure(fig, "svg") is None | |
34 |
|
32 | |||
35 | plt.close('all') |
|
33 | plt.close('all') | |
36 |
|
34 | |||
@@ -39,8 +37,9 b' def test_figure_to_svg():' | |||||
39 | ax = fig.add_subplot(1,1,1) |
|
37 | ax = fig.add_subplot(1,1,1) | |
40 | ax.plot([1,2,3]) |
|
38 | ax.plot([1,2,3]) | |
41 | plt.draw() |
|
39 | plt.draw() | |
42 |
svg = pt.print_figure(fig, |
|
40 | svg = pt.print_figure(fig, "svg")[:100].lower() | |
43 |
|
|
41 | assert "doctype svg" in svg | |
|
42 | ||||
44 |
|
43 | |||
45 | def _check_pil_jpeg_bytes(): |
|
44 | def _check_pil_jpeg_bytes(): | |
46 | """Skip if PIL can't write JPEGs to BytesIO objects""" |
|
45 | """Skip if PIL can't write JPEGs to BytesIO objects""" | |
@@ -53,7 +52,7 b' def _check_pil_jpeg_bytes():' | |||||
53 | img.save(buf, 'jpeg') |
|
52 | img.save(buf, 'jpeg') | |
54 | except Exception as e: |
|
53 | except Exception as e: | |
55 | ename = e.__class__.__name__ |
|
54 | ename = e.__class__.__name__ | |
56 |
raise |
|
55 | raise pytest.skip("PIL can't write JPEG to BytesIO: %s: %s" % (ename, e)) from e | |
57 |
|
56 | |||
58 | @dec.skip_without("PIL.Image") |
|
57 | @dec.skip_without("PIL.Image") | |
59 | def test_figure_to_jpeg(): |
|
58 | def test_figure_to_jpeg(): | |
@@ -69,7 +68,7 b' def test_figure_to_jpeg():' | |||||
69 | def test_retina_figure(): |
|
68 | def test_retina_figure(): | |
70 | # simple empty-figure test |
|
69 | # simple empty-figure test | |
71 | fig = plt.figure() |
|
70 | fig = plt.figure() | |
72 |
|
|
71 | assert pt.retina_figure(fig) == None | |
73 | plt.close('all') |
|
72 | plt.close('all') | |
74 |
|
73 | |||
75 | fig = plt.figure() |
|
74 | fig = plt.figure() | |
@@ -78,8 +77,9 b' def test_retina_figure():' | |||||
78 | plt.draw() |
|
77 | plt.draw() | |
79 | png, md = pt.retina_figure(fig) |
|
78 | png, md = pt.retina_figure(fig) | |
80 | assert png.startswith(_PNG) |
|
79 | assert png.startswith(_PNG) | |
81 |
|
|
80 | assert "width" in md | |
82 |
|
|
81 | assert "height" in md | |
|
82 | ||||
83 |
|
83 | |||
84 | _fmt_mime_map = { |
|
84 | _fmt_mime_map = { | |
85 | 'png': 'image/png', |
|
85 | 'png': 'image/png', | |
@@ -95,9 +95,9 b' def test_select_figure_formats_str():' | |||||
95 | pt.select_figure_formats(ip, fmt) |
|
95 | pt.select_figure_formats(ip, fmt) | |
96 | for mime, f in ip.display_formatter.formatters.items(): |
|
96 | for mime, f in ip.display_formatter.formatters.items(): | |
97 | if mime == active_mime: |
|
97 | if mime == active_mime: | |
98 |
|
|
98 | assert Figure in f | |
99 | else: |
|
99 | else: | |
100 |
|
|
100 | assert Figure not in f | |
101 |
|
101 | |||
102 | def test_select_figure_formats_kwargs(): |
|
102 | def test_select_figure_formats_kwargs(): | |
103 | ip = get_ipython() |
|
103 | ip = get_ipython() | |
@@ -134,24 +134,25 b' def test_select_figure_formats_set():' | |||||
134 | pt.select_figure_formats(ip, fmts) |
|
134 | pt.select_figure_formats(ip, fmts) | |
135 | for mime, f in ip.display_formatter.formatters.items(): |
|
135 | for mime, f in ip.display_formatter.formatters.items(): | |
136 | if mime in active_mimes: |
|
136 | if mime in active_mimes: | |
137 |
|
|
137 | assert Figure in f | |
138 | else: |
|
138 | else: | |
139 |
|
|
139 | assert Figure not in f | |
140 |
|
140 | |||
141 | def test_select_figure_formats_bad(): |
|
141 | def test_select_figure_formats_bad(): | |
142 | ip = get_ipython() |
|
142 | ip = get_ipython() | |
143 |
with |
|
143 | with pytest.raises(ValueError): | |
144 | pt.select_figure_formats(ip, 'foo') |
|
144 | pt.select_figure_formats(ip, 'foo') | |
145 |
with |
|
145 | with pytest.raises(ValueError): | |
146 | pt.select_figure_formats(ip, {'png', 'foo'}) |
|
146 | pt.select_figure_formats(ip, {'png', 'foo'}) | |
147 |
with |
|
147 | with pytest.raises(ValueError): | |
148 | pt.select_figure_formats(ip, ['retina', 'pdf', 'bar', 'bad']) |
|
148 | pt.select_figure_formats(ip, ['retina', 'pdf', 'bar', 'bad']) | |
149 |
|
149 | |||
150 | def test_import_pylab(): |
|
150 | def test_import_pylab(): | |
151 | ns = {} |
|
151 | ns = {} | |
152 | pt.import_pylab(ns, import_all=False) |
|
152 | pt.import_pylab(ns, import_all=False) | |
153 |
|
|
153 | assert "plt" in ns | |
154 |
|
|
154 | assert ns["np"] == np | |
|
155 | ||||
155 |
|
156 | |||
156 | from traitlets.config import Config |
|
157 | from traitlets.config import Config | |
157 |
|
158 | |||
@@ -182,15 +183,13 b' class TestPylabSwitch(object):' | |||||
182 | pt.activate_matplotlib = act_mpl |
|
183 | pt.activate_matplotlib = act_mpl | |
183 | self._save_ip = pt.import_pylab |
|
184 | self._save_ip = pt.import_pylab | |
184 | pt.import_pylab = lambda *a,**kw:None |
|
185 | pt.import_pylab = lambda *a,**kw:None | |
185 |
self._save_cis = |
|
186 | self._save_cis = backend_inline.configure_inline_support | |
186 |
|
|
187 | backend_inline.configure_inline_support = lambda *a, **kw: None | |
187 | lambda *a, **kw: None |
|
|||
188 | ) |
|
|||
189 |
|
188 | |||
190 | def teardown(self): |
|
189 | def teardown(self): | |
191 | pt.activate_matplotlib = self._save_am |
|
190 | pt.activate_matplotlib = self._save_am | |
192 | pt.import_pylab = self._save_ip |
|
191 | pt.import_pylab = self._save_ip | |
193 |
|
|
192 | backend_inline.configure_inline_support = self._save_cis | |
194 | import matplotlib |
|
193 | import matplotlib | |
195 | matplotlib.rcParams = self._saved_rcParams |
|
194 | matplotlib.rcParams = self._saved_rcParams | |
196 | matplotlib.rcParamsOrig = self._saved_rcParamsOrig |
|
195 | matplotlib.rcParamsOrig = self._saved_rcParamsOrig | |
@@ -199,68 +198,68 b' class TestPylabSwitch(object):' | |||||
199 |
|
198 | |||
200 | s = self.Shell() |
|
199 | s = self.Shell() | |
201 | gui, backend = s.enable_matplotlib(None) |
|
200 | gui, backend = s.enable_matplotlib(None) | |
202 |
|
|
201 | assert gui == "qt" | |
203 |
|
|
202 | assert s.pylab_gui_select == "qt" | |
204 |
|
203 | |||
205 |
gui, backend = s.enable_matplotlib( |
|
204 | gui, backend = s.enable_matplotlib("inline") | |
206 |
|
|
205 | assert gui == "inline" | |
207 |
|
|
206 | assert s.pylab_gui_select == "qt" | |
208 |
|
207 | |||
209 |
gui, backend = s.enable_matplotlib( |
|
208 | gui, backend = s.enable_matplotlib("qt") | |
210 |
|
|
209 | assert gui == "qt" | |
211 |
|
|
210 | assert s.pylab_gui_select == "qt" | |
212 |
|
211 | |||
213 |
gui, backend = s.enable_matplotlib( |
|
212 | gui, backend = s.enable_matplotlib("inline") | |
214 |
|
|
213 | assert gui == "inline" | |
215 |
|
|
214 | assert s.pylab_gui_select == "qt" | |
216 |
|
215 | |||
217 | gui, backend = s.enable_matplotlib() |
|
216 | gui, backend = s.enable_matplotlib() | |
218 |
|
|
217 | assert gui == "qt" | |
219 |
|
|
218 | assert s.pylab_gui_select == "qt" | |
220 |
|
219 | |||
221 | def test_inline(self): |
|
220 | def test_inline(self): | |
222 | s = self.Shell() |
|
221 | s = self.Shell() | |
223 |
gui, backend = s.enable_matplotlib( |
|
222 | gui, backend = s.enable_matplotlib("inline") | |
224 |
|
|
223 | assert gui == "inline" | |
225 |
|
|
224 | assert s.pylab_gui_select == None | |
226 |
|
225 | |||
227 |
gui, backend = s.enable_matplotlib( |
|
226 | gui, backend = s.enable_matplotlib("inline") | |
228 |
|
|
227 | assert gui == "inline" | |
229 |
|
|
228 | assert s.pylab_gui_select == None | |
230 |
|
229 | |||
231 |
gui, backend = s.enable_matplotlib( |
|
230 | gui, backend = s.enable_matplotlib("qt") | |
232 |
|
|
231 | assert gui == "qt" | |
233 |
|
|
232 | assert s.pylab_gui_select == "qt" | |
234 |
|
233 | |||
235 | def test_inline_twice(self): |
|
234 | def test_inline_twice(self): | |
236 | "Using '%matplotlib inline' twice should not reset formatters" |
|
235 | "Using '%matplotlib inline' twice should not reset formatters" | |
237 |
|
236 | |||
238 | ip = self.Shell() |
|
237 | ip = self.Shell() | |
239 |
gui, backend = ip.enable_matplotlib( |
|
238 | gui, backend = ip.enable_matplotlib("inline") | |
240 |
|
|
239 | assert gui == "inline" | |
241 |
|
240 | |||
242 | fmts = {'png'} |
|
241 | fmts = {'png'} | |
243 | active_mimes = {_fmt_mime_map[fmt] for fmt in fmts} |
|
242 | active_mimes = {_fmt_mime_map[fmt] for fmt in fmts} | |
244 | pt.select_figure_formats(ip, fmts) |
|
243 | pt.select_figure_formats(ip, fmts) | |
245 |
|
244 | |||
246 |
gui, backend = ip.enable_matplotlib( |
|
245 | gui, backend = ip.enable_matplotlib("inline") | |
247 |
|
|
246 | assert gui == "inline" | |
248 |
|
247 | |||
249 | for mime, f in ip.display_formatter.formatters.items(): |
|
248 | for mime, f in ip.display_formatter.formatters.items(): | |
250 | if mime in active_mimes: |
|
249 | if mime in active_mimes: | |
251 |
|
|
250 | assert Figure in f | |
252 | else: |
|
251 | else: | |
253 |
|
|
252 | assert Figure not in f | |
254 |
|
253 | |||
255 | def test_qt_gtk(self): |
|
254 | def test_qt_gtk(self): | |
256 | s = self.Shell() |
|
255 | s = self.Shell() | |
257 |
gui, backend = s.enable_matplotlib( |
|
256 | gui, backend = s.enable_matplotlib("qt") | |
258 |
|
|
257 | assert gui == "qt" | |
259 |
|
|
258 | assert s.pylab_gui_select == "qt" | |
260 |
|
259 | |||
261 |
gui, backend = s.enable_matplotlib( |
|
260 | gui, backend = s.enable_matplotlib("gtk") | |
262 |
|
|
261 | assert gui == "qt" | |
263 |
|
|
262 | assert s.pylab_gui_select == "qt" | |
264 |
|
263 | |||
265 |
|
264 | |||
266 | def test_no_gui_backends(): |
|
265 | def test_no_gui_backends(): |
General Comments 0
You need to be logged in to leave comments.
Login now