|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +from __future__ import print_function |
| 4 | +import sys, os, glob |
| 5 | +import matplotlib |
| 6 | +import IPython.Shell |
| 7 | +#matplotlib.rcdefaults() |
| 8 | +matplotlib.use('Agg') |
| 9 | + |
| 10 | +mplshell = IPython.Shell.MatplotlibShell('mpl') |
| 11 | + |
| 12 | +formats = [('png', 100), |
| 13 | + ('hires.png', 200), |
| 14 | + ('pdf', 72)] |
| 15 | + |
| 16 | +def figs(): |
| 17 | + print('making figs') |
| 18 | + import matplotlib.pyplot as plt |
| 19 | + for fname in glob.glob('*.py'): |
| 20 | + if fname.split('/')[-1] == __file__.split('/')[-1]: continue |
| 21 | + basename, ext = os.path.splitext(fname) |
| 22 | + imagefiles = dict([('%s.%s'%(basename, format), dpi) |
| 23 | + for format, dpi in formats]) |
| 24 | + all_exists = True |
| 25 | + for imagefile in imagefiles: |
| 26 | + if not os.path.exists(imagefile): |
| 27 | + all_exists = False |
| 28 | + break |
| 29 | + |
| 30 | + if all_exists: |
| 31 | + print(' already have %s'%fname) |
| 32 | + else: |
| 33 | + print(' building %s'%fname) |
| 34 | + plt.close('all') # we need to clear between runs |
| 35 | + mplshell.magic_run(basename) |
| 36 | + for imagefile, dpi in imagefiles.iteritems(): |
| 37 | + # todo: this will get called even if the run script |
| 38 | + # fails and exits, thus creating a stub pdf and png |
| 39 | + # iles preventing them from getting built successfully |
| 40 | + # later |
| 41 | + plt.savefig(imagefile, dpi=dpi) |
| 42 | + print('all figures made') |
| 43 | + |
| 44 | + |
| 45 | +def clean(): |
| 46 | + patterns = (['#*', '*~', '*pyc'] + |
| 47 | + ['*.%s' % format for format, dpi in formats]) |
| 48 | + for pattern in patterns: |
| 49 | + for fname in glob.glob(pattern): |
| 50 | + os.remove(fname) |
| 51 | + print('all clean') |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +def all(): |
| 56 | + figs() |
| 57 | + |
| 58 | +funcd = {'figs':figs, |
| 59 | + 'clean':clean, |
| 60 | + 'all':all, |
| 61 | + } |
| 62 | + |
| 63 | +if len(sys.argv)>1: |
| 64 | + for arg in sys.argv[1:]: |
| 65 | + func = funcd.get(arg) |
| 66 | + if func is None: |
| 67 | + raise SystemExit('Do not know how to handle %s; valid args are'%( |
| 68 | + arg, funcd.keys())) |
| 69 | + func() |
| 70 | +else: |
| 71 | + all() |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + |
0 commit comments