forked from profjsb/python-bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workshop_checklist.py
180 lines (142 loc) · 5.24 KB
/
workshop_checklist.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Minimal test script to check for modules needed in python workshop.
Execute this code at the command line by typing:
python workshop_checklist.py
If it does NOT say 'OK' at the end, copy the *entire* output of the run and
send it to the course instructor for help.
"""
# Standard library imports
import glob
import os
import platform
import sys
from io import StringIO
# Third-party imports
import nose
import nose.tools as nt
##############################################################################
# Code begins
# Global list of modules to check for imporability. Simply comment out any
# that we don't want to check, or add any package (by its actual import name)
# that you want to include in the checks.
MODULES_TO_CHECK = [ # The basic packages we always need
'IPython',
'numpy',
'scipy', 'scipy.io',
'matplotlib',
'nose',
'pandas',
'sympy',
# 'networkx',
# Potentially more delicate packages
# 'Cython',
#'mayavi.mlab',
# Data analysis packages
#'pandas',
#'statsmodels',
#'sklearn',
#'scikits.image',
]
#-----------------------------------------------------------------------------
# Generic utility functions
def sys_info():
"""Summarize some info about the system"""
print('==================')
print('System information')
print('==================')
print('os.name :', os.name)
#print 'os.uname :', os.uname()
print('platform :', sys.platform)
print('platform+ :', platform.platform())
print('prefix :', sys.prefix)
print('exec_prefix :', sys.exec_prefix)
print('executable :', sys.executable)
print('version_info :', sys.version_info)
print('version :', sys.version)
print('==================')
#-----------------------------------------------------------------------------
# Tests
def check_import(mname):
"Check that the given name imports correctly"
namespace = {}
exec("import %s as m" % mname, namespace)
m = namespace["m"]
## python2: exec "import %s as m" % mname
if mname == 'matplotlib':
m.use('Agg')
m.rcParams['figure.subplot.top']= 0.85
try:
vinfo = m.__version__
except AttributeError:
vinfo = '*no info*'
print('MOD: {mname}, version: {vinfo}'.format(mname=mname,vinfo=vinfo))
# Test generators are best written without docstrings, because nose can then
# show the parameters being used.
def test_imports():
for mname in MODULES_TO_CHECK:
yield check_import,mname
# Test generator, don't put a docstring in it
def test_loadtxt():
import numpy as np
import numpy.testing as npt
# Examples taken from the loadtxt docstring
array = np.array
c = StringIO("0 1\n2 3")
a1 = np.loadtxt(c)
a2 = np.array([[ 0., 1.],
[ 2., 3.]])
yield npt.assert_array_equal,a1,a2
d = StringIO("M 21 72\nF 35 58")
a1 = np.loadtxt(d, dtype={'names': ('gender', 'age', 'weight'),
'formats': ('S1', 'i4', 'f4')})
a2 = np.array([('M', 21, 72.0), ('F', 35, 58.0)],
dtype=[('gender', '|S1'), ('age', '<i4'), ('weight', '<f4')])
yield npt.assert_array_equal,a1,a2
c = StringIO("1,0,2\n3,0,4")
x,y = np.loadtxt(c, delimiter=',', usecols=(0,2), unpack=True)
yield npt.assert_array_equal,x,np.array([ 1., 3.])
yield npt.assert_array_equal,y,np.array([ 2., 4.])
def test_plot():
"Simple plot generation."
from matplotlib import pyplot as plt
plt.figure()
plt.plot([1,2,3])
plt.xlabel('some numbers')
plt.savefig('tmp_test_plot.png')
def test_plot_math():
"Plots with math"
from matplotlib import pyplot as plt
plt.figure()
plt.plot([1,2,3],label='data')
t=(r'And X is $\sum_{i=0}^\infty \gamma_i + \frac{\alpha^{i^2}}{\gamma}'
r'+ \cos(2 \theta^2)$')
plt.title(t)
plt.legend()
plt.grid()
plt.savefig('tmp_test_plot_math.png')
def cleanup_pngs():
"""Remove temporary pngs made by our plotting tests"""
for f in glob.glob('tmp_test_plot*.png'):
try:
if os.path.isfile(f):
os.remove(f)
except OSError:
print('*** Error: could not remove this file: {}'.format(f))
#-----------------------------------------------------------------------------
# Main routine, executed when this file is run as a script
#
if __name__ == '__main__':
print("Running tests:")
# This call form is ipython-friendly
nose.runmodule(argv=[__file__, '-vvs'], exit=False)
print("""
***************************************************************************
TESTS FINISHED
***************************************************************************
If the printout above did not finish in 'OK' but instead says 'FAILED', copy
and send the *entire* output (all info above and summary below) to the
instructor for help.
""")
sys_info()
cleanup_pngs()