Futurize 1, futurize 2, pasteurize.

This commit is contained in:
Brendan Smithyman
2016-07-16 14:17:02 -05:00
parent 362975d2bd
commit ca8d8f8c2d
197 changed files with 2618 additions and 1235 deletions
+58 -47
View File
@@ -1,23 +1,34 @@
import Utils, numpy as np, scipy.sparse as sp
from __future__ import print_function
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from future import standard_library
standard_library.install_aliases()
from builtins import str
from builtins import range
from past.utils import old_div
from builtins import object
from . import Utils
import numpy as np, scipy.sparse as sp
from scipy.sparse.linalg import LinearOperator
from Tests import checkDerivative
from PropMaps import PropMap, Property
from .Tests import checkDerivative
from .PropMaps import PropMap, Property
from numpy.polynomial import polynomial
from scipy.interpolate import UnivariateSpline
import warnings
from future.utils import with_metaclass
class IdentityMap(object):
class IdentityMap(with_metaclass(Utils.SimPEGMetaClass, object)):
"""
SimPEG Map
"""
__metaclass__ = Utils.SimPEGMetaClass
def __init__(self, mesh=None, nP=None, **kwargs):
Utils.setKwargs(self, **kwargs)
if nP is not None:
assert type(nP) in [int, long], ' Number of parameters must be an integer.'
assert type(nP) in [int, int], ' Number of parameters must be an integer.'
self.mesh = mesh
self._nP = nP
@@ -101,7 +112,7 @@ class IdentityMap(object):
:return: passed the test?
"""
print 'Testing %s' % str(self)
print('Testing %s' % str(self))
if m is None:
m = abs(np.random.rand(self.nP))
if 'plotIt' not in kwargs:
@@ -248,10 +259,10 @@ class ReciprocalMap(IdentityMap):
"""
def _transform(self, m):
return 1.0 / Utils.mkvc(m)
return old_div(1.0, Utils.mkvc(m))
def inverse(self, D):
return 1.0 / Utils.mkvc(m)
return old_div(1.0, Utils.mkvc(m))
def deriv(self, m):
# TODO: if this is a tensor, you might have a problem.
@@ -291,7 +302,7 @@ class LogMap(IdentityMap):
deriv = np.zeros(mod.shape)
tol = 1e-16 # zero
ind = np.greater_equal(np.abs(mod),tol)
deriv[ind] = 1.0/mod[ind]
deriv[ind] = old_div(1.0,mod[ind])
return Utils.sdiag(deriv)
def inverse(self, m):
@@ -372,7 +383,7 @@ class SurjectVertical1D(IdentityMap):
repNum = self.mesh.vnC[:self.mesh.dim-1].prod()
repVec = sp.csr_matrix(
(np.ones(repNum),
(range(repNum), np.zeros(repNum))
(list(range(repNum)), np.zeros(repNum))
), shape=(repNum, 1))
return sp.kron(sp.identity(self.nP), repVec)
@@ -434,7 +445,7 @@ class Surject2Dto3D(IdentityMap):
nC, nP = self.mesh.nC, self.nP
P = sp.csr_matrix(
(np.ones(nC),
(range(nC), inds)
(list(range(nC)), inds)
), shape=(nC, nP))
return P
@@ -504,11 +515,11 @@ class InjectActiveCells(IdentityMap):
else:
self.valInactive = np.ones(self.nC)
self.valInactive[self.indInactive] = valInactive.copy()
self.valInactive[self.indActive] = 0
inds = np.nonzero(self.indActive)[0]
self.P = sp.csr_matrix((np.ones(inds.size),(inds, range(inds.size))), shape=(self.nC, self.nP))
self.P = sp.csr_matrix((np.ones(inds.size),(inds, list(range(inds.size)))), shape=(self.nC, self.nP))
@property
def shape(self):
@@ -595,14 +606,14 @@ class ComplexMap(IdentityMap):
@property
def shape(self):
return (self.nP/2,self.nP)
return (old_div(self.nP,2),self.nP)
def _transform(self, m):
nC = self.mesh.nC
return m[:nC] + m[nC:]*1j
def deriv(self, m):
nC = self.nP/2
nC = old_div(self.nP,2)
shp = (nC, nC*2)
def fwd(v):
return v[:nC] + v[nC:]*1j
@@ -647,7 +658,7 @@ class CircleMap(IdentityMap):
sig1, sig2 = np.exp(sig1), np.exp(sig2)
X = self.mesh.gridCC[:,0]
Y = self.mesh.gridCC[:,1]
return sig1 + (sig2 - sig1)*(np.arctan(a*(np.sqrt((X-x)**2 + (Y-y)**2) - r))/np.pi + 0.5)
return sig1 + (sig2 - sig1)*(old_div(np.arctan(a*(np.sqrt((X-x)**2 + (Y-y)**2) - r)),np.pi) + 0.5)
def deriv(self, m):
a = self.slope
@@ -657,11 +668,11 @@ class CircleMap(IdentityMap):
X = self.mesh.gridCC[:,0]
Y = self.mesh.gridCC[:,1]
if self.logSigma:
g1 = -(np.arctan(a*(-r + np.sqrt((X - x)**2 + (Y - y)**2)))/np.pi + 0.5)*sig1 + sig1
g2 = (np.arctan(a*(-r + np.sqrt((X - x)**2 + (Y - y)**2)))/np.pi + 0.5)*sig2
g1 = -(old_div(np.arctan(a*(-r + np.sqrt((X - x)**2 + (Y - y)**2))),np.pi) + 0.5)*sig1 + sig1
g2 = (old_div(np.arctan(a*(-r + np.sqrt((X - x)**2 + (Y - y)**2))),np.pi) + 0.5)*sig2
else:
g1 = -(np.arctan(a*(-r + np.sqrt((X - x)**2 + (Y - y)**2)))/np.pi + 0.5) + 1.0
g2 = (np.arctan(a*(-r + np.sqrt((X - x)**2 + (Y - y)**2)))/np.pi + 0.5)
g1 = -(old_div(np.arctan(a*(-r + np.sqrt((X - x)**2 + (Y - y)**2))),np.pi) + 0.5) + 1.0
g2 = (old_div(np.arctan(a*(-r + np.sqrt((X - x)**2 + (Y - y)**2))),np.pi) + 0.5)
g3 = a*(-X + x)*(-sig1 + sig2)/(np.pi*(a**2*(-r + np.sqrt((X - x)**2 + (Y - y)**2))**2 + 1)*np.sqrt((X - x)**2 + (Y - y)**2))
g4 = a*(-Y + y)*(-sig1 + sig2)/(np.pi*(a**2*(-r + np.sqrt((X - x)**2 + (Y - y)**2))**2 + 1)*np.sqrt((X - x)**2 + (Y - y)**2))
g5 = -a*(-sig1 + sig2)/(np.pi*(a**2*(-r + np.sqrt((X - x)**2 + (Y - y)**2))**2 + 1))
@@ -695,7 +706,7 @@ class PolyMap(IdentityMap):
self.actInd = actInd
if getattr(self, 'actInd', None) is None:
self.actInd = range(self.mesh.nC)
self.actInd = list(range(self.mesh.nC))
self.nC = self.mesh.nC
else:
@@ -731,7 +742,7 @@ class PolyMap(IdentityMap):
elif self.normal =='Y':
f = polynomial.polyval(X, c) - Y
else:
raise(Exception("Input for normal = X or Y or Z"))
raise Exception
#3D
elif self.mesh.dim == 3:
X = self.mesh.gridCC[self.actInd,0]
@@ -744,13 +755,13 @@ class PolyMap(IdentityMap):
elif self.normal =='Z':
f = polynomial.polyval2d(X, Y, c.reshape((self.order[0]+1,self.order[1]+1))) - Z
else:
raise(Exception("Input for normal = X or Y or Z"))
raise Exception
else:
raise(Exception("Only supports 2D"))
raise Exception
return sig1+(sig2-sig1)*(np.arctan(alpha*f)/np.pi+0.5)
return sig1+(sig2-sig1)*(old_div(np.arctan(alpha*f),np.pi)+0.5)
def deriv(self, m):
alpha = self.slope
@@ -769,7 +780,7 @@ class PolyMap(IdentityMap):
f = polynomial.polyval(X, c) - Y
V = polynomial.polyvander(X, len(c)-1)
else:
raise(Exception("Input for normal = X or Y or Z"))
raise Exception
#3D
elif self.mesh.dim == 3:
X = self.mesh.gridCC[self.actInd,0]
@@ -786,14 +797,14 @@ class PolyMap(IdentityMap):
f = polynomial.polyval2d(X, Y, c.reshape((self.order[0]+1,self.order[1]+1))) - Z
V = polynomial.polyvander2d(X, Y, self.order)
else:
raise(Exception("Input for normal = X or Y or Z"))
raise Exception
if self.logSigma:
g1 = -(np.arctan(alpha*f)/np.pi + 0.5)*sig1 + sig1
g2 = (np.arctan(alpha*f)/np.pi + 0.5)*sig2
g1 = -(old_div(np.arctan(alpha*f),np.pi) + 0.5)*sig1 + sig1
g2 = (old_div(np.arctan(alpha*f),np.pi) + 0.5)*sig2
else:
g1 = -(np.arctan(alpha*f)/np.pi + 0.5) + 1.0
g2 = (np.arctan(alpha*f)/np.pi + 0.5)
g1 = -(old_div(np.arctan(alpha*f),np.pi) + 0.5) + 1.0
g2 = (old_div(np.arctan(alpha*f),np.pi) + 0.5)
g3 = Utils.sdiag(alpha*(sig2-sig1)/(1.+(alpha*f)**2)/np.pi)*V
@@ -834,7 +845,7 @@ class SplineMap(IdentityMap):
elif self.mesh.dim == 3:
return np.size(self.pts)*2+2
else:
raise(Exception("Only supports 2D and 3D"))
raise Exception
def _transform(self, m):
# Set model parameters
@@ -853,7 +864,7 @@ class SplineMap(IdentityMap):
elif self.normal =='Y':
f = self.spl(X) - Y
else:
raise(Exception("Input for normal = X or Y or Z"))
raise Exception
# 3D:
# Comments:
@@ -868,7 +879,7 @@ class SplineMap(IdentityMap):
npts = np.size(self.pts)
if np.mod(c.size, 2):
raise(Exception("Put even points!"))
raise Exception
self.spl = {"splb":UnivariateSpline(self.pts, c[:npts], k=self.order, s=0),
"splt":UnivariateSpline(self.pts, c[npts:], k=self.order, s=0)}
@@ -881,12 +892,12 @@ class SplineMap(IdentityMap):
# elif self.normal =='Y':
# elif self.normal =='Z':
else:
raise(Exception("Input for normal = X or Y or Z"))
raise Exception
else:
raise(Exception("Only supports 2D and 3D"))
raise Exception
return sig1+(sig2-sig1)*(np.arctan(alpha*f)/np.pi+0.5)
return sig1+(sig2-sig1)*(old_div(np.arctan(alpha*f),np.pi)+0.5)
def deriv(self, m):
alpha = self.slope
@@ -903,7 +914,7 @@ class SplineMap(IdentityMap):
elif self.normal =='Y':
f = self.spl(X) - Y
else:
raise(Exception("Input for normal = X or Y or Z"))
raise Exception
#3D
elif self.mesh.dim == 3:
X = self.mesh.gridCC[:,0]
@@ -917,14 +928,14 @@ class SplineMap(IdentityMap):
# elif self.normal =='Y':
# elif self.normal =='Z':
else:
raise(Exception("Not Implemented for Y and Z, your turn :)"))
raise Exception
if self.logSigma:
g1 = -(np.arctan(alpha*f)/np.pi + 0.5)*sig1 + sig1
g2 = (np.arctan(alpha*f)/np.pi + 0.5)*sig2
g1 = -(old_div(np.arctan(alpha*f),np.pi) + 0.5)*sig1 + sig1
g2 = (old_div(np.arctan(alpha*f),np.pi) + 0.5)*sig2
else:
g1 = -(np.arctan(alpha*f)/np.pi + 0.5) + 1.0
g2 = (np.arctan(alpha*f)/np.pi + 0.5)
g1 = -(old_div(np.arctan(alpha*f),np.pi) + 0.5) + 1.0
g2 = (old_div(np.arctan(alpha*f),np.pi) + 0.5)
if self.mesh.dim ==2:
@@ -943,7 +954,7 @@ class SplineMap(IdentityMap):
cb[i] = ctemp-dy
spla = UnivariateSpline(self.pts, ca, k=self.order, s=0)
splb = UnivariateSpline(self.pts, cb, k=self.order, s=0)
fderiv = (spla(X)-splb(X))/(2*dy)
fderiv = old_div((spla(X)-splb(X)),(2*dy))
g3[:,i] = Utils.sdiag(alpha*(sig2-sig1)/(1.+(alpha*f)**2)/np.pi)*fderiv
elif self.mesh.dim==3:
@@ -970,10 +981,10 @@ class SplineMap(IdentityMap):
spltb = UnivariateSpline(self.pts, ca[self.npts:], k=self.order, s=0)
flinesa = (self.spl["splt"](Y)-splta(Y))*(Z-zb)/(zt-zb) + splta(Y) - X
flinesb = (self.spl["splt"](Y)-spltb(Y))*(Z-zb)/(zt-zb) + spltb(Y) - X
fderiv = (flinesa-flinesb)/(2*dy)
fderiv = old_div((flinesa-flinesb),(2*dy))
g3[:,i] = Utils.sdiag(alpha*(sig2-sig1)/(1.+(alpha*f)**2)/np.pi)*fderiv
else :
raise(Exception("Not Implemented for Y and Z, your turn :)"))
raise Exception
return sp.csr_matrix(np.c_[g1,g2,g3])