-
Notifications
You must be signed in to change notification settings - Fork 533
/
Copy pathmake_examples.py
executable file
·95 lines (72 loc) · 2.82 KB
/
make_examples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python
"""Run the py->rst conversion and run all examples.
This also creates the index.rst file appropriately, makes figures, etc.
"""
import os
import sys
from glob import glob
import runpy
from toollib import sh
# We must configure the mpl backend before making any further mpl imports
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
# -----------------------------------------------------------------------------
# Globals
# -----------------------------------------------------------------------------
examples_header = """
.. _examples:
Examples
========
.. note_about_examples
"""
# -----------------------------------------------------------------------------
# Function defintions
# -----------------------------------------------------------------------------
# These global variables let show() be called by the scripts in the usual
# manner, but when generating examples, we override it to write the figures to
# files with a known name (derived from the script name) plus a counter
figure_basename = None
# We must change the show command to save instead
def show():
from matplotlib._pylab_helpers import Gcf
allfm = Gcf.get_all_fig_managers()
for fcount, fm in enumerate(allfm):
fm.canvas.figure.savefig("%s_%02i.png" % (figure_basename, fcount + 1))
_mpl_show = plt.show
plt.show = show
# -----------------------------------------------------------------------------
# Main script
# -----------------------------------------------------------------------------
exclude_files = ['-x %s' % sys.argv[i + 1] for i, arg in enumerate(sys.argv) if arg == '-x']
tools_path = os.path.abspath(os.path.dirname(__file__))
ex2rst = os.path.join(tools_path, 'ex2rst')
# Work in examples directory
os.chdir("users/examples")
if not os.getcwd().endswith("users/examples"):
raise OSError("This must be run from doc/examples directory")
# Run the conversion from .py to rst file
sh("%s %s --project Nipype --outdir . ../../../examples" % (ex2rst, ' '.join(exclude_files)))
sh("""%s --project Nipype %s --outdir . ../../../examples/frontiers_paper""" % (
ex2rst, ' '.join(exclude_files)))
# Make the index.rst file
"""
index = open('index.rst', 'w')
index.write(examples_header)
for name in [os.path.splitext(f)[0] for f in glob('*.rst')]:
#Don't add the index in there to avoid sphinx errors and don't add the
#note_about examples again (because it was added at the top):
if name not in(['index','note_about_examples']):
index.write(' %s\n' % name)
index.close()
"""
# Execute each python script in the directory.
if "--no-exec" in sys.argv:
pass
else:
if not os.path.isdir("fig"):
os.mkdir("fig")
for script in glob("*.py"):
figure_basename = os.path.join("fig", os.path.splitext(script)[0])
runpy.run_path(script)
plt.close("all")