From cef998d962d0f8eafd770874f7fd22fcb1ca9f05 Mon Sep 17 00:00:00 2001 From: Per A Brodtkorb Date: Sun, 14 Feb 2016 01:46:11 +0100 Subject: [PATCH] made code python 3 compatible: Replaced xrange with range Replaced map with list comprehension --- wafo/fig.py | 29 +++++++++++++++-------------- wafo/integrate.py | 34 +++++++++++++++++----------------- wafo/interpolate.py | 6 +++--- wafo/kdetools.py | 4 ++-- wafo/misc.py | 6 +++--- wafo/objects.py | 10 +++++----- wafo/polynomial.py | 12 ++++++------ wafo/stats/estimation.py | 4 ++-- wafo/transform/estimation.py | 2 +- wafo/win32_utils.py | 2 +- 10 files changed, 55 insertions(+), 54 deletions(-) diff --git a/wafo/fig.py b/wafo/fig.py index d6ef8a8..70284b1 100644 --- a/wafo/fig.py +++ b/wafo/fig.py @@ -1,11 +1,11 @@ -''' -Module FIG ------------- -Module for manipulating windows/figures created using -pylab or enthought.mayavi.mlab on the windows platform. +''' +Module FIG +------------ +Module for manipulating windows/figures created using +pylab or enthought.mayavi.mlab on the windows platform. -Figure manipulation involves -maximization, minimization, hiding, closing, stacking or tiling. +Figure manipulation involves +maximization, minimization, hiding, closing, stacking or tiling. This module assumes that the figures are uniquely numbered in the following way: Figure 1 @@ -21,7 +21,7 @@ Example ------- >>> import pylab as p >>> import wafo.fig as fig ->>> for ix in range(6): +>>> for ix in range(6): ... f = p.figure(ix) >>> fig.stack('all') >>> fig.stack(1,2) @@ -538,7 +538,7 @@ def stack(*figs): #% Location (1,1) is at bottom left corner # #print('Screensz = ',screenpos) - for iy in xrange(numfigs): + for iy in range(numfigs): pos = list(GetWindowRect(wnds[iy])) pos[3] -= pos[1] pos[2] -= pos[0] @@ -621,7 +621,7 @@ def tile(*figs, **kwds): #% 3 - Window horizontal size #% 4 - Window vertical size #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - + hspc = 10 # Horisontal space. topspc = 20; # Space above top figure. medspc = 10; # Space between figures. @@ -639,10 +639,10 @@ def tile(*figs, **kwds): #% Put the figures where they belong. #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% idx = 0 - for unused_ix in xrange(nlayers): - for row in xrange(nv): - for col in xrange(nh): - if (row) * nh + col < nfigspertile : + for unused_ix in range(nlayers): + for row in range(nv): + for col in range(nh): + if (row) * nh + col < nfigspertile : if idx < nfigs: figlft = int(screenpos[0] + (col + 1) * hspc + col * figwid) figtop = int(screenpos[1] + topspc + row * (fighgt + medspc)) @@ -654,6 +654,7 @@ def tile(*figs, **kwds): #figure(figs[idx]); # Raise figure. idx += 1 + def cycle(*figs, **kwds): ''' Cycle through figure windows. diff --git a/wafo/integrate.py b/wafo/integrate.py index ce21a72..5783bab 100644 --- a/wafo/integrate.py +++ b/wafo/integrate.py @@ -255,7 +255,7 @@ def romberg(fun, a, b, releps=1e-3, abseps=1e-3): # [res,abserr,epstab,newflg] = dea(newflg,Ih4,abserr,epstab) two = 1 one = 0 - for i in xrange(1, tableLimit): + for i in range(1, tableLimit): h *= 0.5 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] # Richardson extrapolation - for k in xrange(i): + for k in range(i): rom[two, k + 1] = rom[two, k] + \ (rom[two, k] - rom[one, k]) / (fp[k] - 1) @@ -354,12 +354,12 @@ def h_roots(n, method='newton'): L = zeros((3, len(z))) k0 = 0 kp1 = 1 - for _its in xrange(max_iter): + for _its in range(max_iter): # Newtons method carried out simultaneously on the roots. L[k0, :] = 0 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 # polynomials evaluated at z. km1 = k0 @@ -454,13 +454,13 @@ def j_roots(n, alpha, beta, method='newton'): L = zeros((3, len(z))) k0 = 0 kp1 = 1 - for _its in xrange(max_iter): + for _its in range(max_iter): # Newton's method carried out simultaneously on the roots. tmp = 2 + alfbet L[k0, :] = 1 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 # polynomials evaluated at z. km1 = k0 @@ -565,12 +565,12 @@ def la_roots(n, alpha=0, method='newton'): k0 = 0 kp1 = 1 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. L[k0, k] = 0. 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 # polynomials evaluated at z. km1 = k0 @@ -683,11 +683,11 @@ def p_roots(n, method='newton', a=-1, b=1): k = slice(m) k0 = 0 kp1 = 1 - for _ix in xrange(max_iter): + for _ix in range(max_iter): L[k0, k] = 1 L[kp1, k] = xo[k] - for jj in xrange(2, n + 1): + for jj in range(2, n + 1): km1 = k0 k0 = kp1 kp1 = np.mod(k0 + 1, 3) @@ -712,10 +712,10 @@ def p_roots(n, method='newton', a=-1, b=1): e1 = n * (n + 1) - for _j in xrange(2): + for _j in range(2): pkm1 = 1 pk = xo - for k in xrange(2, n + 1): + for k in range(2, n + 1): t1 = xo * pk pkp1 = t1 - pkm1 - (t1 - pkm1) / k + t1 pkm1 = pk @@ -1008,7 +1008,7 @@ class _Gaussq(object): def _initialize(self, wfun, a, b, args): args = np.broadcast_arrays(*np.atleast_1d(a, b, *args)) 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] args = args[2:] if wfun in [2, 3]: @@ -1036,7 +1036,7 @@ class _Gaussq(object): k = np.arange(nk) opts = (nk, dtype) 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) 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) # Successive bisection of intervals - for k in xrange(1, max_iter): + for k in range(1, max_iter): # Interval bisection hh = hh / 2 @@ -1312,7 +1312,7 @@ def qdemo(f, a, b, kmax=9, plot_error=False): # try various approximations methods = [trapz, simps, boole, ] - for k in xrange(kmax): + for k in range(kmax): n = 2 ** (k + 1) + 1 neval[k] = 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(err_dic[name]) data = np.vstack(tuple(data)).T - for k in xrange(kmax): + for k in range(kmax): tmp = data[k].tolist() print(''.join(fi % t for fi, t in zip(formats, tmp))) if plot_error: diff --git a/wafo/interpolate.py b/wafo/interpolate.py index c37da7a..9f4eed2 100644 --- a/wafo/interpolate.py +++ b/wafo/interpolate.py @@ -322,7 +322,7 @@ class PPform(object): dx = xx - self.breaks.take(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] values = v @@ -409,7 +409,7 @@ class PPform(object): vv = xs * cof[0, index] k = self.order - for i in xrange(1, k): + for i in range(1, k): vv = xs * (vv + cof[i, index]) cof[-1] = np.hstack((0, vv)).cumsum() @@ -419,7 +419,7 @@ class PPform(object): # def fromspline(self, xk, cvals, order, fill=0.0): # N = len(xk) - 1 # 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) # res = _fitpack._bspleval(xk[:-1], xk, cvals, order, m) # res /= fact diff --git a/wafo/kdetools.py b/wafo/kdetools.py index 19bbe6b..cb9695c 100644 --- a/wafo/kdetools.py +++ b/wafo/kdetools.py @@ -3172,10 +3172,10 @@ def gridcount(data, X, y=1): # fact1 = fact1(ones(n,1),:); bt0 = [0, 0] 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[1] = 1 - bt0[0] - for ix in xrange(2): + for ix in range(2): one = np.mod(ix, 2) two = np.mod(ix + 1, 2) # Convert to linear index diff --git a/wafo/misc.py b/wafo/misc.py index 238ead5..f7fefad 100644 --- a/wafo/misc.py +++ b/wafo/misc.py @@ -1069,7 +1069,7 @@ def findrfc(tp, h=0.0, method='clib'): if clib is None or method not in ('clib'): ind = zeros(n, dtype=np.int) NC = np.int(NC) - for i in xrange(NC): + for i in range(NC): Tmi = Tstart + 2 * i Tpl = Tstart + 2 * i + 2 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]) if first_is_down_crossing: - for i in xrange(n_tc): + for i in range(n_tc): # trough j = 2 * i 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() else: # the first is a up-crossing - for i in xrange(n_tc): + for i in range(n_tc): # crest j = 2 * i ind[j] = x[v_ind[j] + 1:v_ind[j + 1] + 1].argmax() diff --git a/wafo/objects.py b/wafo/objects.py index 232cd20..51cf4c5 100644 --- a/wafo/objects.py +++ b/wafo/objects.py @@ -318,7 +318,7 @@ class LevelCrossings(PlotData): ff = [f[0], ] tt = [t[0], ] - for i in xrange(1, n): + for i in range(1, n): if f[i] > ff[-1]: ff.append(f[i]) tt.append(t[i]) @@ -741,7 +741,7 @@ class CyclePairs(PlotData): n = extremes.shape[1] extr = zeros((4, n)) extr[:, 0] = extremes[:, 0] - for i in xrange(1, n): + for i in range(1, n): if extremes[0, i] == extr[0, ii]: extr[1:4, ii] = extr[1:4, ii] + extremes[1:4, i] else: @@ -1563,7 +1563,7 @@ class TimeSeries(PlotData): >>> import wafo.objects as wo >>> x = wd.sea() >>> ts = wo.mat2timeseries(x) - >>> for i in xrange(-3,4): + >>> for i in range(-3,4): ... S, H = ts.wave_height_steepness(method=i) ... print(S[:2],H[:2]) (array([ 0.01186982, 0.04852534]), array([ 0.69, 0.86])) @@ -2207,10 +2207,10 @@ class TimeSeries(PlotData): plot = plotbackend.plot subplot = plotbackend.subplot figs = [] - for unused_iz in xrange(nfig): + for unused_iz in range(nfig): figs.append(plotbackend.figure()) plotbackend.title('Surface elevation from mean water level (MWL).') - for ix in xrange(nsub): + for ix in range(nsub): if nsub > 1: subplot(nsub, 1, ix) h_scale = array([tn[ind[0]], tn[ind[-1]]]) diff --git a/wafo/polynomial.py b/wafo/polynomial.py index 9630d7d..eb45614 100644 --- a/wafo/polynomial.py +++ b/wafo/polynomial.py @@ -961,7 +961,7 @@ def cheb2poly(ck, a=-1, b=1): y2 = 2. * y # Clenshaw recurence - for ix in xrange(n - 1): + for ix in range(n - 1): tmp = b_Nmi b_Nmi = polymul(y2, b_Nmi) # polynomial multiplication nb = len(b_Nmip1) @@ -1327,7 +1327,7 @@ def _chebval(x, ck, kind=1): b_Nmip1 = b_Nmi.copy() # b_(N-i+1) x2 = 2 * x # Clenshaw reccurence - for ix in xrange(n - 1): + for ix in range(n - 1): tmp = b_Nmi b_Nmi = x2 * b_Nmi - b_Nmip1 + ck[ix] b_Nmip1 = tmp @@ -1444,7 +1444,7 @@ def chebder(ck, a=-1, b=1): cder = zeros(n, dtype=asarray(ck).dtype) cder[0] = 2 * n * ck[0] 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] 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 u = zeros((npt, ncof)) - for ix in xrange(MAXIT): + for ix in range(MAXIT): # Set up design matrix for least squares fit. pow1 = wt bb = pow1 * (fs + abs(mad) * sign(ee)) - for jx in xrange(m + 1): + for jx in range(m + 1): u[:, jx] = pow1 pow1 = pow1 * x pow1 = -bb - for jx in xrange(m + 1, ncof): + for jx in range(m + 1, ncof): pow1 = pow1 * x u[:, jx] = pow1 diff --git a/wafo/stats/estimation.py b/wafo/stats/estimation.py index 5455ff3..23028bc 100644 --- a/wafo/stats/estimation.py +++ b/wafo/stats/estimation.py @@ -253,7 +253,7 @@ class Profile(object): for size, step in ((-1, -1), (pvec.size, 1)): 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]) self.data[ix] = Lmax if self.data[ix] < self.alpha_cross_level: @@ -728,7 +728,7 @@ class FitDistribution(rv_frozen): LL = nnlf(theta, data) H = zeros((num_par, num_par)) # Hessian matrix theta = tuple(theta) - for ix in xrange(num_par): + for ix in range(num_par): sparam = list(theta) sparam[ix] = theta[ix] + delta fp = nnlf(sparam, data) diff --git a/wafo/transform/estimation.py b/wafo/transform/estimation.py index 184ee96..b282d5c 100644 --- a/wafo/transform/estimation.py +++ b/wafo/transform/estimation.py @@ -104,7 +104,7 @@ class TransformEstimator(object): x = tr.args mean = tr.mean sigma = tr.sigma - for ix in xrange(5): + for ix in range(5): dy = np.diff(tr.data) if (dy <= 0).any(): dy[dy > 0] = eps diff --git a/wafo/win32_utils.py b/wafo/win32_utils.py index 44cf945..ace64e4 100644 --- a/wafo/win32_utils.py +++ b/wafo/win32_utils.py @@ -250,7 +250,7 @@ if __name__ == '__main__': ErrorDlg('This is an example of an error message') wb = Waitbar('Waitbar example') # wb2 = Waitbar2('Waitbar example') - for i in xrange(20): + for i in range(20): print(wb.update(i * 5)) # wb2.update(i) sleep(0.1)