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