|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +This is used to drive many of the examples across the backends, for |
| 4 | +regression testing, and comparing backend efficiency. |
| 5 | +
|
| 6 | +The script takes one or more arguments specifying backends |
| 7 | +to be tested, e.g. |
| 8 | +
|
| 9 | + python backend_driver.py agg ps cairo.png cairo.ps |
| 10 | +
|
| 11 | +would test the agg and ps backends, and the cairo backend with |
| 12 | +output to png and ps files. |
| 13 | +
|
| 14 | +If no arguments are given, a default list of backends will be |
| 15 | +tested. |
| 16 | +""" |
| 17 | + |
| 18 | +from __future__ import division |
| 19 | +import os, time, sys |
| 20 | +import matplotlib.backends as mplbe |
| 21 | + |
| 22 | +pylab_dir = os.path.join('..', 'pylab') |
| 23 | +pylab_files = [ |
| 24 | + 'alignment_test.py', |
| 25 | + 'arctest.py', |
| 26 | + 'arrow_demo.py', |
| 27 | + 'auto_layout.py', |
| 28 | + 'axes_demo.py', |
| 29 | + 'axhspan_demo.py', |
| 30 | + 'bar_stacked.py', |
| 31 | + 'barchart_demo.py', |
| 32 | + 'boxplot_demo.py', |
| 33 | + 'broken_barh.py', |
| 34 | + 'barh_demo.py', |
| 35 | + 'color_demo.py', |
| 36 | + 'cohere_demo.py', |
| 37 | + 'contour_demo.py', |
| 38 | + 'contourf_demo.py', |
| 39 | + 'csd_demo.py', |
| 40 | + 'custom_ticker1.py', |
| 41 | + 'customize_rc.py', |
| 42 | + 'date_demo1.py', |
| 43 | + 'date_demo2.py', |
| 44 | + 'equal_aspect_ratio.py', |
| 45 | + 'errorbar_limits.py', |
| 46 | + 'figimage_demo.py', |
| 47 | + 'figlegend_demo.py', |
| 48 | + 'figtext.py', |
| 49 | + 'fill_demo.py', |
| 50 | + 'finance_demo.py', |
| 51 | + 'fonts_demo_kw.py', |
| 52 | + 'hexbin_demo.py', |
| 53 | + 'histogram_demo.py', |
| 54 | + 'hline_demo.py', |
| 55 | + 'image_demo.py', |
| 56 | + 'image_demo2.py', |
| 57 | + 'image_masked.py', |
| 58 | + 'image_origin.py', |
| 59 | + 'invert_axes.py', |
| 60 | + 'layer_images.py', |
| 61 | + 'legend_auto.py', |
| 62 | + 'legend_demo.py', |
| 63 | + 'legend_demo2.py', |
| 64 | + 'line_collection.py', |
| 65 | + 'line_collection2.py', |
| 66 | + 'line_styles.py', |
| 67 | + 'log_demo.py', |
| 68 | + 'log_test.py', |
| 69 | + 'major_minor_demo1.py', |
| 70 | + 'major_minor_demo2.py', |
| 71 | + 'masked_demo.py', |
| 72 | + 'mathtext_demo.py', |
| 73 | + 'mri_with_eeg.py', |
| 74 | + 'multiple_figs_demo.py', |
| 75 | + 'nan_test.py', |
| 76 | + 'pcolor_demo.py', |
| 77 | + 'pcolor_demo2.py', |
| 78 | + 'pcolor_small.py', |
| 79 | + 'pie_demo.py', |
| 80 | + 'polar_demo.py', |
| 81 | + 'polar_scatter.py', |
| 82 | + 'psd_demo.py', |
| 83 | + 'quadmesh_demo.py', |
| 84 | + 'quiver_demo.py', |
| 85 | + 'scatter_demo.py', |
| 86 | + 'scatter_demo2.py', |
| 87 | + 'scatter_star_poly.py', |
| 88 | + 'shared_axis_demo.py', |
| 89 | + 'shared_axis_across_figures.py', |
| 90 | + 'simple_plot.py', |
| 91 | + 'specgram_demo.py', |
| 92 | + 'spy_demos.py', |
| 93 | + 'stem_plot.py', |
| 94 | + 'step_demo.py', |
| 95 | + 'stock_demo.py', |
| 96 | + 'subplot_demo.py', |
| 97 | +# 'set_and_get.py', |
| 98 | + 'table_demo.py', |
| 99 | + 'text_handles.py', |
| 100 | + 'text_rotation.py', |
| 101 | + 'text_themes.py', |
| 102 | +# 'tex_demo.py', |
| 103 | + 'two_scales.py', |
| 104 | + 'unicode_demo.py', |
| 105 | + 'vline_demo.py', |
| 106 | + 'xcorr_demo.py', |
| 107 | + 'zorder_demo.py', |
| 108 | + ] |
| 109 | + |
| 110 | + |
| 111 | +api_dir = os.path.join('..', 'api') |
| 112 | +api_files = [ |
| 113 | + 'colorbar_only.py', |
| 114 | +] |
| 115 | + |
| 116 | + |
| 117 | +files = [os.path.join(pylab_dir, fname) for fname in pylab_files] +\ |
| 118 | + [os.path.join(api_dir, fname) for fname in api_files] |
| 119 | +# tests known to fail on a given backend |
| 120 | + |
| 121 | + |
| 122 | +failbackend = dict( |
| 123 | + SVG = ('tex_demo.py,'), |
| 124 | + ) |
| 125 | + |
| 126 | +try: |
| 127 | + import subprocess |
| 128 | + def run(arglist): |
| 129 | + try: |
| 130 | + subprocess.call(arglist) |
| 131 | + except KeyboardInterrupt: |
| 132 | + sys.exit() |
| 133 | +except ImportError: |
| 134 | + def run(arglist): |
| 135 | + os.system(' '.join(arglist)) |
| 136 | + |
| 137 | +def drive(backend, python=['python'], switches = []): |
| 138 | + exclude = failbackend.get(backend, []) |
| 139 | + |
| 140 | + # Clear the destination directory for the examples |
| 141 | + path = backend |
| 142 | + if os.path.exists(path): |
| 143 | + import glob |
| 144 | + for fname in os.listdir(path): |
| 145 | + os.unlink(os.path.join(path,fname)) |
| 146 | + else: |
| 147 | + os.mkdir(backend) |
| 148 | + |
| 149 | + for fullpath in files: |
| 150 | + |
| 151 | + print ('\tdriving %-40s' % (fullpath)), |
| 152 | + sys.stdout.flush() |
| 153 | + |
| 154 | + fpath, fname = os.path.split(fullpath) |
| 155 | + |
| 156 | + if fname in exclude: |
| 157 | + print '\tSkipping %s, known to fail on backend: %s'%backend |
| 158 | + continue |
| 159 | + |
| 160 | + basename, ext = os.path.splitext(fname) |
| 161 | + outfile = os.path.join(path,basename) |
| 162 | + tmpfile_name = '_tmp_%s.py' % basename |
| 163 | + tmpfile = file(tmpfile_name, 'w') |
| 164 | + |
| 165 | + for line in file(fullpath): |
| 166 | + line_lstrip = line.lstrip() |
| 167 | + if line_lstrip.startswith("#"): |
| 168 | + tmpfile.write(line) |
| 169 | + else: |
| 170 | + break |
| 171 | + |
| 172 | + tmpfile.writelines(( |
| 173 | + 'from __future__ import division\n', |
| 174 | + 'import sys\n', |
| 175 | + 'sys.path.append("%s")\n'%fpath, |
| 176 | + 'import matplotlib\n', |
| 177 | + 'matplotlib.use("%s")\n' % backend, |
| 178 | + 'from pylab import savefig\n', |
| 179 | + )) |
| 180 | + for line in file(fullpath): |
| 181 | + line_lstrip = line.lstrip() |
| 182 | + if (line_lstrip.startswith('from __future__ import division') or |
| 183 | + line_lstrip.startswith('matplotlib.use') or |
| 184 | + line_lstrip.startswith('savefig') or |
| 185 | + line_lstrip.startswith('show')): |
| 186 | + continue |
| 187 | + tmpfile.write(line) |
| 188 | + if backend in mplbe.interactive_bk: |
| 189 | + tmpfile.write('show()') |
| 190 | + else: |
| 191 | + tmpfile.write('savefig("%s", dpi=150)' % outfile) |
| 192 | + |
| 193 | + tmpfile.close() |
| 194 | + start_time = time.time() |
| 195 | + program = [x % {'name': basename} for x in python] |
| 196 | + run(program + [tmpfile_name, switchstring]) |
| 197 | + end_time = time.time() |
| 198 | + print (end_time - start_time) |
| 199 | + #os.system('%s %s %s' % (python, tmpfile_name, switchstring)) |
| 200 | + os.remove(tmpfile_name) |
| 201 | + |
| 202 | +if __name__ == '__main__': |
| 203 | + times = {} |
| 204 | + default_backends = ['Agg', 'PS', 'SVG', 'PDF', 'Template'] |
| 205 | + if '--coverage' in sys.argv: |
| 206 | + python = ['coverage.py', '-x'] |
| 207 | + sys.argv.remove('--coverage') |
| 208 | + elif '--valgrind' in sys.argv: |
| 209 | + python = ['valgrind', '--tool=memcheck', '--leak-check=yes', |
| 210 | + '--log-file=%(name)s', 'python'] |
| 211 | + sys.argv.remove('--valgrind') |
| 212 | + elif sys.platform == 'win32': |
| 213 | + python = [r'c:\Python24\python.exe'] |
| 214 | + else: |
| 215 | + python = ['python'] |
| 216 | + all_backends = [b.lower() for b in mplbe.all_backends] |
| 217 | + all_backends.extend(['cairo.png', 'cairo.ps', 'cairo.pdf', 'cairo.svg']) |
| 218 | + backends = [] |
| 219 | + switches = [] |
| 220 | + if sys.argv[1:]: |
| 221 | + backends = [b.lower() for b in sys.argv[1:] if b.lower() in all_backends] |
| 222 | + switches = [s for s in sys.argv[1:] if s.startswith('--')] |
| 223 | + if not backends: |
| 224 | + backends = default_backends |
| 225 | + for backend in backends: |
| 226 | + switchstring = ' '.join(switches) |
| 227 | + print 'testing %s %s' % (backend, switchstring) |
| 228 | + t0 = time.time() |
| 229 | + drive(backend, python, switches) |
| 230 | + t1 = time.time() |
| 231 | + times[backend] = (t1-t0)/60.0 |
| 232 | + |
| 233 | + # print times |
| 234 | + for backend, elapsed in times.items(): |
| 235 | + print 'Backend %s took %1.2f minutes to complete' % ( backend, elapsed) |
| 236 | + if 'Template' in times: |
| 237 | + print '\ttemplate ratio %1.3f, template residual %1.3f' % ( |
| 238 | + elapsed/times['Template'], elapsed-times['Template']) |
0 commit comments