made code python 3 compatible:

Replaced xrange with range
Replaced map with list comprehension
master
Per A Brodtkorb 9 years ago
parent c903426235
commit cef998d962

@ -538,7 +538,7 @@ def stack(*figs):
#% Location (1,1) is at bottom left corner #% Location (1,1) is at bottom left corner
# #
#print('Screensz = ',screenpos) #print('Screensz = ',screenpos)
for iy in xrange(numfigs): for iy in range(numfigs):
pos = list(GetWindowRect(wnds[iy])) pos = list(GetWindowRect(wnds[iy]))
pos[3] -= pos[1] pos[3] -= pos[1]
pos[2] -= pos[0] pos[2] -= pos[0]
@ -639,9 +639,9 @@ def tile(*figs, **kwds):
#% Put the figures where they belong. #% Put the figures where they belong.
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
idx = 0 idx = 0
for unused_ix in xrange(nlayers): for unused_ix in range(nlayers):
for row in xrange(nv): for row in range(nv):
for col in xrange(nh): for col in range(nh):
if (row) * nh + col < nfigspertile : if (row) * nh + col < nfigspertile :
if idx < nfigs: if idx < nfigs:
figlft = int(screenpos[0] + (col + 1) * hspc + col * figwid) figlft = int(screenpos[0] + (col + 1) * hspc + col * figwid)
@ -654,6 +654,7 @@ def tile(*figs, **kwds):
#figure(figs[idx]); # Raise figure. #figure(figs[idx]); # Raise figure.
idx += 1 idx += 1
def cycle(*figs, **kwds): def cycle(*figs, **kwds):
''' Cycle through figure windows. ''' Cycle through figure windows.

@ -255,7 +255,7 @@ def romberg(fun, a, b, releps=1e-3, abseps=1e-3):
# [res,abserr,epstab,newflg] = dea(newflg,Ih4,abserr,epstab) # [res,abserr,epstab,newflg] = dea(newflg,Ih4,abserr,epstab)
two = 1 two = 1
one = 0 one = 0
for i in xrange(1, tableLimit): for i in range(1, tableLimit):
h *= 0.5 h *= 0.5
Un5 = np.sum(fun(a + np.arange(1, 2 * ipower, 2) * h)) * h Un5 = np.sum(fun(a + np.arange(1, 2 * ipower, 2) * h)) * h
@ -265,7 +265,7 @@ def romberg(fun, a, b, releps=1e-3, abseps=1e-3):
fp[i] = 4 * fp[i - 1] fp[i] = 4 * fp[i - 1]
# Richardson extrapolation # Richardson extrapolation
for k in xrange(i): for k in range(i):
rom[two, k + 1] = rom[two, k] + \ rom[two, k + 1] = rom[two, k] + \
(rom[two, k] - rom[one, k]) / (fp[k] - 1) (rom[two, k] - rom[one, k]) / (fp[k] - 1)
@ -354,12 +354,12 @@ def h_roots(n, method='newton'):
L = zeros((3, len(z))) L = zeros((3, len(z)))
k0 = 0 k0 = 0
kp1 = 1 kp1 = 1
for _its in xrange(max_iter): for _its in range(max_iter):
# Newtons method carried out simultaneously on the roots. # Newtons method carried out simultaneously on the roots.
L[k0, :] = 0 L[k0, :] = 0
L[kp1, :] = PIM4 L[kp1, :] = PIM4
for j in xrange(1, n + 1): for j in range(1, n + 1):
# Loop up the recurrence relation to get the Hermite # Loop up the recurrence relation to get the Hermite
# polynomials evaluated at z. # polynomials evaluated at z.
km1 = k0 km1 = k0
@ -454,13 +454,13 @@ def j_roots(n, alpha, beta, method='newton'):
L = zeros((3, len(z))) L = zeros((3, len(z)))
k0 = 0 k0 = 0
kp1 = 1 kp1 = 1
for _its in xrange(max_iter): for _its in range(max_iter):
# Newton's method carried out simultaneously on the roots. # Newton's method carried out simultaneously on the roots.
tmp = 2 + alfbet tmp = 2 + alfbet
L[k0, :] = 1 L[k0, :] = 1
L[kp1, :] = (alpha - beta + tmp * z) / 2 L[kp1, :] = (alpha - beta + tmp * z) / 2
for j in xrange(2, n + 1): for j in range(2, n + 1):
# Loop up the recurrence relation to get the Jacobi # Loop up the recurrence relation to get the Jacobi
# polynomials evaluated at z. # polynomials evaluated at z.
km1 = k0 km1 = k0
@ -565,12 +565,12 @@ def la_roots(n, alpha=0, method='newton'):
k0 = 0 k0 = 0
kp1 = 1 kp1 = 1
k = slice(len(z)) k = slice(len(z))
for _its in xrange(max_iter): for _its in range(max_iter):
# Newton's method carried out simultaneously on the roots. # Newton's method carried out simultaneously on the roots.
L[k0, k] = 0. L[k0, k] = 0.
L[kp1, k] = 1. L[kp1, k] = 1.
for jj in xrange(1, n + 1): for jj in range(1, n + 1):
# Loop up the recurrence relation to get the Laguerre # Loop up the recurrence relation to get the Laguerre
# polynomials evaluated at z. # polynomials evaluated at z.
km1 = k0 km1 = k0
@ -683,11 +683,11 @@ def p_roots(n, method='newton', a=-1, b=1):
k = slice(m) k = slice(m)
k0 = 0 k0 = 0
kp1 = 1 kp1 = 1
for _ix in xrange(max_iter): for _ix in range(max_iter):
L[k0, k] = 1 L[k0, k] = 1
L[kp1, k] = xo[k] L[kp1, k] = xo[k]
for jj in xrange(2, n + 1): for jj in range(2, n + 1):
km1 = k0 km1 = k0
k0 = kp1 k0 = kp1
kp1 = np.mod(k0 + 1, 3) kp1 = np.mod(k0 + 1, 3)
@ -712,10 +712,10 @@ def p_roots(n, method='newton', a=-1, b=1):
e1 = n * (n + 1) e1 = n * (n + 1)
for _j in xrange(2): for _j in range(2):
pkm1 = 1 pkm1 = 1
pk = xo pk = xo
for k in xrange(2, n + 1): for k in range(2, n + 1):
t1 = xo * pk t1 = xo * pk
pkp1 = t1 - pkm1 - (t1 - pkm1) / k + t1 pkp1 = t1 - pkm1 - (t1 - pkm1) / k + t1
pkm1 = pk pkm1 = pk
@ -1008,7 +1008,7 @@ class _Gaussq(object):
def _initialize(self, wfun, a, b, args): def _initialize(self, wfun, a, b, args):
args = np.broadcast_arrays(*np.atleast_1d(a, b, *args)) args = np.broadcast_arrays(*np.atleast_1d(a, b, *args))
a_shape = args[0].shape a_shape = args[0].shape
args = map(lambda x: np.reshape(x, (-1, 1)), args) args = [np.reshape(x, (-1, 1)) for x in args]
A, B = args[:2] A, B = args[:2]
args = args[2:] args = args[2:]
if wfun in [2, 3]: if wfun in [2, 3]:
@ -1036,7 +1036,7 @@ class _Gaussq(object):
k = np.arange(nk) k = np.arange(nk)
opts = (nk, dtype) opts = (nk, dtype)
val, val_old, abserr = zeros(*opts), ones(*opts), zeros(*opts) val, val_old, abserr = zeros(*opts), ones(*opts), zeros(*opts)
for i in xrange(max_iter): for i in range(max_iter):
xn, w = self._points_and_weights(gn, wfun, alpha, beta) xn, w = self._points_and_weights(gn, wfun, alpha, beta)
x = (xn + shift) * jacob[k, :] + A[k, :] x = (xn + shift) * jacob[k, :] + A[k, :]
@ -1181,7 +1181,7 @@ def quadgr(fun, a, b, abseps=1e-5, max_iter=17):
Q0[0] = hh * np.sum(wq * fun(x), axis=0) Q0[0] = hh * np.sum(wq * fun(x), axis=0)
# Successive bisection of intervals # Successive bisection of intervals
for k in xrange(1, max_iter): for k in range(1, max_iter):
# Interval bisection # Interval bisection
hh = hh / 2 hh = hh / 2
@ -1312,7 +1312,7 @@ def qdemo(f, a, b, kmax=9, plot_error=False):
# try various approximations # try various approximations
methods = [trapz, simps, boole, ] methods = [trapz, simps, boole, ]
for k in xrange(kmax): for k in range(kmax):
n = 2 ** (k + 1) + 1 n = 2 ** (k + 1) + 1
neval[k] = n neval[k] = n
x = np.linspace(a, b, n) x = np.linspace(a, b, n)
@ -1365,7 +1365,7 @@ def qdemo(f, a, b, kmax=9, plot_error=False):
data.append(vals_dic[name]) data.append(vals_dic[name])
data.append(err_dic[name]) data.append(err_dic[name])
data = np.vstack(tuple(data)).T data = np.vstack(tuple(data)).T
for k in xrange(kmax): for k in range(kmax):
tmp = data[k].tolist() tmp = data[k].tolist()
print(''.join(fi % t for fi, t in zip(formats, tmp))) print(''.join(fi % t for fi, t in zip(formats, tmp)))
if plot_error: if plot_error:

@ -322,7 +322,7 @@ class PPform(object):
dx = xx - self.breaks.take(indxs) dx = xx - self.breaks.take(indxs)
v = pp[0, indxs] v = pp[0, indxs]
for i in xrange(1, self.order): for i in range(1, self.order):
v = dx * v + pp[i, indxs] v = dx * v + pp[i, indxs]
values = v values = v
@ -409,7 +409,7 @@ class PPform(object):
vv = xs * cof[0, index] vv = xs * cof[0, index]
k = self.order k = self.order
for i in xrange(1, k): for i in range(1, k):
vv = xs * (vv + cof[i, index]) vv = xs * (vv + cof[i, index])
cof[-1] = np.hstack((0, vv)).cumsum() cof[-1] = np.hstack((0, vv)).cumsum()
@ -419,7 +419,7 @@ class PPform(object):
# def fromspline(self, xk, cvals, order, fill=0.0): # def fromspline(self, xk, cvals, order, fill=0.0):
# N = len(xk) - 1 # N = len(xk) - 1
# sivals = np.empty((order + 1, N), dtype=float) # sivals = np.empty((order + 1, N), dtype=float)
# for m in xrange(order, -1, -1): # for m in range(order, -1, -1):
# fact = spec.gamma(m + 1) # fact = spec.gamma(m + 1)
# res = _fitpack._bspleval(xk[:-1], xk, cvals, order, m) # res = _fitpack._bspleval(xk[:-1], xk, cvals, order, m)
# res /= fact # res /= fact

@ -3172,10 +3172,10 @@ def gridcount(data, X, y=1):
# fact1 = fact1(ones(n,1),:); # fact1 = fact1(ones(n,1),:);
bt0 = [0, 0] bt0 = [0, 0]
X1 = X.ravel() X1 = X.ravel()
for ir in xrange(2 ** (d - 1)): for ir in range(2 ** (d - 1)):
bt0[0] = np.reshape(bitget(ir, np.arange(d)), (d, -1)) bt0[0] = np.reshape(bitget(ir, np.arange(d)), (d, -1))
bt0[1] = 1 - bt0[0] bt0[1] = 1 - bt0[0]
for ix in xrange(2): for ix in range(2):
one = np.mod(ix, 2) one = np.mod(ix, 2)
two = np.mod(ix + 1, 2) two = np.mod(ix + 1, 2)
# Convert to linear index # Convert to linear index

@ -1069,7 +1069,7 @@ def findrfc(tp, h=0.0, method='clib'):
if clib is None or method not in ('clib'): if clib is None or method not in ('clib'):
ind = zeros(n, dtype=np.int) ind = zeros(n, dtype=np.int)
NC = np.int(NC) NC = np.int(NC)
for i in xrange(NC): for i in range(NC):
Tmi = Tstart + 2 * i Tmi = Tstart + 2 * i
Tpl = Tstart + 2 * i + 2 Tpl = Tstart + 2 * i + 2
xminus = y[2 * i] xminus = y[2 * i]
@ -1557,7 +1557,7 @@ def findtc(x_in, v=None, kind=None):
first_is_down_crossing = (x[v_ind[0]] > x[v_ind[0] + 1]) first_is_down_crossing = (x[v_ind[0]] > x[v_ind[0] + 1])
if first_is_down_crossing: if first_is_down_crossing:
for i in xrange(n_tc): for i in range(n_tc):
# trough # trough
j = 2 * i j = 2 * i
ind[j] = x[v_ind[j] + 1:v_ind[j + 1] + 1].argmin() ind[j] = x[v_ind[j] + 1:v_ind[j + 1] + 1].argmin()
@ -1569,7 +1569,7 @@ def findtc(x_in, v=None, kind=None):
ind[n_c - 2] = x[v_ind[n_c - 2] + 1:v_ind[n_c - 1]].argmin() ind[n_c - 2] = x[v_ind[n_c - 2] + 1:v_ind[n_c - 1]].argmin()
else: # the first is a up-crossing else: # the first is a up-crossing
for i in xrange(n_tc): for i in range(n_tc):
# crest # crest
j = 2 * i j = 2 * i
ind[j] = x[v_ind[j] + 1:v_ind[j + 1] + 1].argmax() ind[j] = x[v_ind[j] + 1:v_ind[j + 1] + 1].argmax()

@ -318,7 +318,7 @@ class LevelCrossings(PlotData):
ff = [f[0], ] ff = [f[0], ]
tt = [t[0], ] tt = [t[0], ]
for i in xrange(1, n): for i in range(1, n):
if f[i] > ff[-1]: if f[i] > ff[-1]:
ff.append(f[i]) ff.append(f[i])
tt.append(t[i]) tt.append(t[i])
@ -741,7 +741,7 @@ class CyclePairs(PlotData):
n = extremes.shape[1] n = extremes.shape[1]
extr = zeros((4, n)) extr = zeros((4, n))
extr[:, 0] = extremes[:, 0] extr[:, 0] = extremes[:, 0]
for i in xrange(1, n): for i in range(1, n):
if extremes[0, i] == extr[0, ii]: if extremes[0, i] == extr[0, ii]:
extr[1:4, ii] = extr[1:4, ii] + extremes[1:4, i] extr[1:4, ii] = extr[1:4, ii] + extremes[1:4, i]
else: else:
@ -1563,7 +1563,7 @@ class TimeSeries(PlotData):
>>> import wafo.objects as wo >>> import wafo.objects as wo
>>> x = wd.sea() >>> x = wd.sea()
>>> ts = wo.mat2timeseries(x) >>> ts = wo.mat2timeseries(x)
>>> for i in xrange(-3,4): >>> for i in range(-3,4):
... S, H = ts.wave_height_steepness(method=i) ... S, H = ts.wave_height_steepness(method=i)
... print(S[:2],H[:2]) ... print(S[:2],H[:2])
(array([ 0.01186982, 0.04852534]), array([ 0.69, 0.86])) (array([ 0.01186982, 0.04852534]), array([ 0.69, 0.86]))
@ -2207,10 +2207,10 @@ class TimeSeries(PlotData):
plot = plotbackend.plot plot = plotbackend.plot
subplot = plotbackend.subplot subplot = plotbackend.subplot
figs = [] figs = []
for unused_iz in xrange(nfig): for unused_iz in range(nfig):
figs.append(plotbackend.figure()) figs.append(plotbackend.figure())
plotbackend.title('Surface elevation from mean water level (MWL).') plotbackend.title('Surface elevation from mean water level (MWL).')
for ix in xrange(nsub): for ix in range(nsub):
if nsub > 1: if nsub > 1:
subplot(nsub, 1, ix) subplot(nsub, 1, ix)
h_scale = array([tn[ind[0]], tn[ind[-1]]]) h_scale = array([tn[ind[0]], tn[ind[-1]]])

@ -961,7 +961,7 @@ def cheb2poly(ck, a=-1, b=1):
y2 = 2. * y y2 = 2. * y
# Clenshaw recurence # Clenshaw recurence
for ix in xrange(n - 1): for ix in range(n - 1):
tmp = b_Nmi tmp = b_Nmi
b_Nmi = polymul(y2, b_Nmi) # polynomial multiplication b_Nmi = polymul(y2, b_Nmi) # polynomial multiplication
nb = len(b_Nmip1) nb = len(b_Nmip1)
@ -1327,7 +1327,7 @@ def _chebval(x, ck, kind=1):
b_Nmip1 = b_Nmi.copy() # b_(N-i+1) b_Nmip1 = b_Nmi.copy() # b_(N-i+1)
x2 = 2 * x x2 = 2 * x
# Clenshaw reccurence # Clenshaw reccurence
for ix in xrange(n - 1): for ix in range(n - 1):
tmp = b_Nmi tmp = b_Nmi
b_Nmi = x2 * b_Nmi - b_Nmip1 + ck[ix] b_Nmi = x2 * b_Nmi - b_Nmip1 + ck[ix]
b_Nmip1 = tmp b_Nmip1 = tmp
@ -1444,7 +1444,7 @@ def chebder(ck, a=-1, b=1):
cder = zeros(n, dtype=asarray(ck).dtype) cder = zeros(n, dtype=asarray(ck).dtype)
cder[0] = 2 * n * ck[0] cder[0] = 2 * n * ck[0]
cder[1] = 2 * (n - 1) * ck[1] cder[1] = 2 * (n - 1) * ck[1]
for j in xrange(2, n): for j in range(2, n):
cder[j] = cder[j - 2] + 2 * (n - j) * ck[j] cder[j] = cder[j - 2] + 2 * (n - j) * ck[j]
return cder * 2. / (b - a) # Normalize to the interval b-a. return cder * 2. / (b - a) # Normalize to the interval b-a.
@ -1837,17 +1837,17 @@ def padefitlsq(fun, m, k, a=-1, b=1, trace=False, x=None, end_points=True):
mad = 0 mad = 0
u = zeros((npt, ncof)) u = zeros((npt, ncof))
for ix in xrange(MAXIT): for ix in range(MAXIT):
# Set up design matrix for least squares fit. # Set up design matrix for least squares fit.
pow1 = wt pow1 = wt
bb = pow1 * (fs + abs(mad) * sign(ee)) bb = pow1 * (fs + abs(mad) * sign(ee))
for jx in xrange(m + 1): for jx in range(m + 1):
u[:, jx] = pow1 u[:, jx] = pow1
pow1 = pow1 * x pow1 = pow1 * x
pow1 = -bb pow1 = -bb
for jx in xrange(m + 1, ncof): for jx in range(m + 1, ncof):
pow1 = pow1 * x pow1 = pow1 * x
u[:, jx] = pow1 u[:, jx] = pow1

@ -253,7 +253,7 @@ class Profile(object):
for size, step in ((-1, -1), (pvec.size, 1)): for size, step in ((-1, -1), (pvec.size, 1)):
phatfree = phatfree0.copy() phatfree = phatfree0.copy()
for ix in xrange(k1, size, step): for ix in range(k1, size, step):
Lmax, phatfree = self._profile_optimum(phatfree, pvec[ix]) Lmax, phatfree = self._profile_optimum(phatfree, pvec[ix])
self.data[ix] = Lmax self.data[ix] = Lmax
if self.data[ix] < self.alpha_cross_level: if self.data[ix] < self.alpha_cross_level:
@ -728,7 +728,7 @@ class FitDistribution(rv_frozen):
LL = nnlf(theta, data) LL = nnlf(theta, data)
H = zeros((num_par, num_par)) # Hessian matrix H = zeros((num_par, num_par)) # Hessian matrix
theta = tuple(theta) theta = tuple(theta)
for ix in xrange(num_par): for ix in range(num_par):
sparam = list(theta) sparam = list(theta)
sparam[ix] = theta[ix] + delta sparam[ix] = theta[ix] + delta
fp = nnlf(sparam, data) fp = nnlf(sparam, data)

@ -104,7 +104,7 @@ class TransformEstimator(object):
x = tr.args x = tr.args
mean = tr.mean mean = tr.mean
sigma = tr.sigma sigma = tr.sigma
for ix in xrange(5): for ix in range(5):
dy = np.diff(tr.data) dy = np.diff(tr.data)
if (dy <= 0).any(): if (dy <= 0).any():
dy[dy > 0] = eps dy[dy > 0] = eps

@ -250,7 +250,7 @@ if __name__ == '__main__':
ErrorDlg('This is an example of an error message') ErrorDlg('This is an example of an error message')
wb = Waitbar('Waitbar example') wb = Waitbar('Waitbar example')
# wb2 = Waitbar2('Waitbar example') # wb2 = Waitbar2('Waitbar example')
for i in xrange(20): for i in range(20):
print(wb.update(i * 5)) print(wb.update(i * 5))
# wb2.update(i) # wb2.update(i)
sleep(0.1) sleep(0.1)

Loading…
Cancel
Save