Show More
@@ -13,17 +13,19 b'' | |||||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | from __future__ import print_function |
|
14 | from __future__ import print_function | |
15 |
|
15 | |||
16 | # Stdlib imports |
|
16 | import matplotlib | |
|
17 | matplotlib.use('Agg') | |||
|
18 | from matplotlib.figure import Figure | |||
17 |
|
19 | |||
18 | # Third-party imports |
|
|||
19 | import matplotlib; matplotlib.use('Agg') |
|
|||
20 | import nose.tools as nt |
|
20 | import nose.tools as nt | |
21 |
|
21 | |||
22 | from matplotlib import pyplot as plt |
|
22 | from matplotlib import pyplot as plt | |
23 | import numpy as np |
|
23 | import numpy as np | |
24 |
|
24 | |||
25 | # Our own imports |
|
25 | # Our own imports | |
|
26 | from IPython.core.getipython import get_ipython | |||
26 | from IPython.core.interactiveshell import InteractiveShell |
|
27 | from IPython.core.interactiveshell import InteractiveShell | |
|
28 | from IPython.core.display import _PNG, _JPEG | |||
27 | from .. import pylabtools as pt |
|
29 | from .. import pylabtools as pt | |
28 |
|
30 | |||
29 | from IPython.testing import decorators as dec |
|
31 | from IPython.testing import decorators as dec | |
@@ -62,12 +64,81 b' def test_figure_to_jpg():' | |||||
62 | ax = fig.add_subplot(1,1,1) |
|
64 | ax = fig.add_subplot(1,1,1) | |
63 | ax.plot([1,2,3]) |
|
65 | ax.plot([1,2,3]) | |
64 | plt.draw() |
|
66 | plt.draw() | |
65 | jpg = pt.print_figure(fig, 'jpg')[:100].lower() |
|
67 | jpg = pt.print_figure(fig, 'jpg', quality=50)[:100].lower() | |
66 |
assert jpg.startswith( |
|
68 | assert jpg.startswith(_JPEG) | |
67 |
|
69 | |||
|
70 | def test_retina_figure(): | |||
|
71 | fig = plt.figure() | |||
|
72 | ax = fig.add_subplot(1,1,1) | |||
|
73 | ax.plot([1,2,3]) | |||
|
74 | plt.draw() | |||
|
75 | png, md = pt.retina_figure(fig) | |||
|
76 | assert png.startswith(_PNG) | |||
|
77 | nt.assert_in('width', md) | |||
|
78 | nt.assert_in('height', md) | |||
|
79 | ||||
|
80 | _fmt_mime_map = { | |||
|
81 | 'png': 'image/png', | |||
|
82 | 'jpeg': 'image/jpeg', | |||
|
83 | 'pdf': 'application/pdf', | |||
|
84 | 'retina': 'image/png', | |||
|
85 | 'svg': 'image/svg+xml', | |||
|
86 | } | |||
|
87 | ||||
|
88 | def test_select_figure_formats_str(): | |||
|
89 | ip = get_ipython() | |||
|
90 | for fmt, active_mime in _fmt_mime_map.items(): | |||
|
91 | pt.select_figure_formats(ip, fmt) | |||
|
92 | for mime, f in ip.display_formatter.formatters.items(): | |||
|
93 | if mime == active_mime: | |||
|
94 | nt.assert_in(Figure, f) | |||
|
95 | else: | |||
|
96 | nt.assert_not_in(Figure, f) | |||
|
97 | ||||
|
98 | def test_select_figure_formats_kwargs(): | |||
|
99 | ip = get_ipython() | |||
|
100 | kwargs = dict(quality=10, bbox_inches='tight') | |||
|
101 | pt.select_figure_formats(ip, 'png', **kwargs) | |||
|
102 | formatter = ip.display_formatter.formatters['image/png'] | |||
|
103 | f = formatter.lookup_by_type(Figure) | |||
|
104 | cell = f.__closure__[0].cell_contents | |||
|
105 | nt.assert_equal(cell, kwargs) | |||
|
106 | ||||
|
107 | # check that the formatter doesn't raise | |||
|
108 | fig = plt.figure() | |||
|
109 | ax = fig.add_subplot(1,1,1) | |||
|
110 | ax.plot([1,2,3]) | |||
|
111 | plt.draw() | |||
|
112 | formatter.enabled = True | |||
|
113 | png = formatter(fig) | |||
|
114 | assert png.startswith(_PNG) | |||
68 |
|
115 | |||
69 | def test_import_pylab(): |
|
116 | def test_select_figure_formats_set(): | |
70 | ip = get_ipython() |
|
117 | ip = get_ipython() | |
|
118 | for fmts in [ | |||
|
119 | {'png', 'svg'}, | |||
|
120 | ['png'], | |||
|
121 | ('jpeg', 'pdf', 'retina'), | |||
|
122 | {'svg'}, | |||
|
123 | ]: | |||
|
124 | active_mimes = {_fmt_mime_map[fmt] for fmt in fmts} | |||
|
125 | pt.select_figure_formats(ip, fmts) | |||
|
126 | for mime, f in ip.display_formatter.formatters.items(): | |||
|
127 | if mime in active_mimes: | |||
|
128 | nt.assert_in(Figure, f) | |||
|
129 | else: | |||
|
130 | nt.assert_not_in(Figure, f) | |||
|
131 | ||||
|
132 | def test_select_figure_formats_bad(): | |||
|
133 | ip = get_ipython() | |||
|
134 | with nt.assert_raises(ValueError): | |||
|
135 | pt.select_figure_formats(ip, 'foo') | |||
|
136 | with nt.assert_raises(ValueError): | |||
|
137 | pt.select_figure_formats(ip, {'png', 'foo'}) | |||
|
138 | with nt.assert_raises(ValueError): | |||
|
139 | pt.select_figure_formats(ip, ['retina', 'pdf', 'bar', 'bad']) | |||
|
140 | ||||
|
141 | def test_import_pylab(): | |||
71 | ns = {} |
|
142 | ns = {} | |
72 | pt.import_pylab(ns, import_all=False) |
|
143 | pt.import_pylab(ns, import_all=False) | |
73 | nt.assert_true('plt' in ns) |
|
144 | nt.assert_true('plt' in ns) |
General Comments 0
You need to be logged in to leave comments.
Login now