-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix_default_axis.py
291 lines (259 loc) · 7.85 KB
/
fix_default_axis.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
"""
This module adds the default axis argument to code which did not specify it
for the functions where the default was changed in NumPy.
The functions changed are
add -1 ( all second argument)
======
nansum
nanmax
nanmin
nanargmax
nanargmin
argmax
argmin
compress 3
add 0
======
take 3
repeat 3
sum # might cause problems with builtin.
product
sometrue
alltrue
cumsum
cumproduct
average
ptp
cumprod
prod
std
mean
"""
__all__ = ['convertfile', 'convertall', 'converttree']
import sys
import os
import re
import glob
_args3 = ['compress', 'take', 'repeat']
_funcm1 = ['nansum', 'nanmax', 'nanmin', 'nanargmax', 'nanargmin',
'argmax', 'argmin', 'compress']
_func0 = ['take', 'repeat', 'sum', 'product', 'sometrue', 'alltrue',
'cumsum', 'cumproduct', 'average', 'ptp', 'cumprod', 'prod',
'std', 'mean']
_all = _func0 + _funcm1
func_re = {}
for name in _all:
_astr = r"""%s\s*[(]"""%name
func_re[name] = re.compile(_astr)
import string
disallowed = '_' + string.uppercase + string.lowercase + string.digits
def _add_axis(fstr, name, repl):
alter = 0
if name in _args3:
allowed_comma = 1
else:
allowed_comma = 0
newcode = ""
last = 0
for obj in func_re[name].finditer(fstr):
nochange = 0
start, end = obj.span()
if fstr[start-1] in disallowed:
continue
if fstr[start-1] == '.' \
and fstr[start-6:start-1] != 'numpy' \
and fstr[start-2:start-1] != 'N' \
and fstr[start-9:start-1] != 'numarray' \
and fstr[start-8:start-1] != 'numerix' \
and fstr[start-8:start-1] != 'Numeric':
continue
if fstr[start-1] in ['\t',' ']:
k = start-2
while fstr[k] in ['\t',' ']:
k -= 1
if fstr[k-2:k+1] == 'def' or \
fstr[k-4:k+1] == 'class':
continue
k = end
stack = 1
ncommas = 0
N = len(fstr)
while stack:
if k>=N:
nochange =1
break
if fstr[k] == ')':
stack -= 1
elif fstr[k] == '(':
stack += 1
elif stack == 1 and fstr[k] == ',':
ncommas += 1
if ncommas > allowed_comma:
nochange = 1
break
k += 1
if nochange:
continue
alter += 1
newcode = "%s%s,%s)" % (newcode, fstr[last:k-1], repl)
last = k
if not alter:
newcode = fstr
else:
newcode = "%s%s" % (newcode, fstr[last:])
return newcode, alter
def _import_change(fstr, names):
# Four possibilities
# 1.) import numpy with subsequent use of numpy.<name>
# change this to import numpy.oldnumeric as numpy
# 2.) import numpy as XXXX with subsequent use of
# XXXX.<name> ==> import numpy.oldnumeric as XXXX
# 3.) from numpy import *
# with subsequent use of one of the names
# 4.) from numpy import ..., <name>, ... (could span multiple
# lines. ==> remove all names from list and
# add from numpy.oldnumeric import <name>
num = 0
# case 1
importstr = "import numpy"
ind = fstr.find(importstr)
if (ind > 0):
found = 0
for name in names:
ind2 = fstr.find("numpy.%s" % name, ind)
if (ind2 > 0):
found = 1
break
if found:
fstr = "%s%s%s" % (fstr[:ind], "import numpy.oldnumeric as numpy",
fstr[ind+len(importstr):])
num += 1
# case 2
importre = re.compile("""import numpy as ([A-Za-z0-9_]+)""")
modules = importre.findall(fstr)
if len(modules) > 0:
for module in modules:
found = 0
for name in names:
ind2 = fstr.find("%s.%s" % (module, name))
if (ind2 > 0):
found = 1
break
if found:
importstr = "import numpy as %s" % module
ind = fstr.find(importstr)
fstr = "%s%s%s" % (fstr[:ind],
"import numpy.oldnumeric as %s" % module,
fstr[ind+len(importstr):])
num += 1
# case 3
importstr = "from numpy import *"
ind = fstr.find(importstr)
if (ind > 0):
found = 0
for name in names:
ind2 = fstr.find(name, ind)
if (ind2 > 0) and fstr[ind2-1] not in disallowed:
found = 1
break
if found:
fstr = "%s%s%s" % (fstr[:ind],
"from numpy.oldnumeric import *",
fstr[ind+len(importstr):])
num += 1
# case 4
ind = 0
importstr = "from numpy import"
N = len(importstr)
while 1:
ind = fstr.find(importstr, ind)
if (ind < 0):
break
ind += N
ptr = ind+1
stack = 1
while stack:
if fstr[ptr] == '\\':
stack += 1
elif fstr[ptr] == '\n':
stack -= 1
ptr += 1
substr = fstr[ind:ptr]
found = 0
substr = substr.replace('\n',' ')
substr = substr.replace('\\','')
importnames = [x.strip() for x in substr.split(',')]
# determine if any of names are in importnames
addnames = []
for name in names:
if name in importnames:
importnames.remove(name)
addnames.append(name)
if len(addnames) > 0:
fstr = "%s%s\n%s\n%s" % \
(fstr[:ind],
"from numpy import %s" % \
", ".join(importnames),
"from numpy.oldnumeric import %s" % \
", ".join(addnames),
fstr[ptr:])
num += 1
return fstr, num
def add_axis(fstr, import_change=False):
total = 0
if not import_change:
for name in _funcm1:
fstr, num = _add_axis(fstr, name, 'axis=-1')
total += num
for name in _func0:
fstr, num = _add_axis(fstr, name, 'axis=0')
total += num
return fstr, total
else:
fstr, num = _import_change(fstr, _funcm1+_func0)
return fstr, num
def makenewfile(name, filestr):
fid = file(name, 'w')
fid.write(filestr)
fid.close()
def getfile(name):
fid = file(name)
filestr = fid.read()
fid.close()
return filestr
def copyfile(name, fstr):
base, ext = os.path.splitext(name)
makenewfile(base+'.orig', fstr)
return
def convertfile(filename, import_change=False):
"""Convert the filename given from using Numeric to using NumPy
Copies the file to filename.orig and then over-writes the file
with the updated code
"""
filestr = getfile(filename)
newstr, total = add_axis(filestr, import_change)
if total > 0:
print "Changing ", filename
copyfile(filename, filestr)
makenewfile(filename, newstr)
sys.stdout.flush()
def fromargs(args):
filename = args[1]
convertfile(filename)
def convertall(direc=os.path.curdir, import_change=False):
"""Convert all .py files in the directory given
For each file, a backup of <usesnumeric>.py is made as
<usesnumeric>.py.orig. A new file named <usesnumeric>.py
is then written with the updated code.
"""
files = glob.glob(os.path.join(direc,'*.py'))
for afile in files:
convertfile(afile, import_change)
def _func(arg, dirname, fnames):
convertall(dirname, import_change=arg)
def converttree(direc=os.path.curdir, import_change=False):
"""Convert all .py files in the tree given
"""
os.path.walk(direc, _func, import_change)
if __name__ == '__main__':
fromargs(sys.argv)