mirror of
https://github.com/wassname/simpeg.git
synced 2026-08-14 12:50:10 +08:00
Futurize 1, futurize 2, pasteurize.
This commit is contained in:
+38
-26
@@ -1,4 +1,16 @@
|
||||
import Utils, numpy as np
|
||||
from __future__ import print_function
|
||||
from __future__ import division
|
||||
from __future__ import unicode_literals
|
||||
from __future__ import absolute_import
|
||||
from builtins import open
|
||||
from builtins import int
|
||||
from future import standard_library
|
||||
standard_library.install_aliases()
|
||||
from builtins import str
|
||||
from past.utils import old_div
|
||||
from builtins import object
|
||||
from . import Utils
|
||||
import numpy as np
|
||||
|
||||
class InversionDirective(object):
|
||||
"""InversionDirective"""
|
||||
@@ -15,7 +27,7 @@ class InversionDirective(object):
|
||||
@inversion.setter
|
||||
def inversion(self, i):
|
||||
if getattr(self,'_inversion',None) is not None:
|
||||
print 'Warning: InversionDirective %s has switched to a new inversion.' % self.__name__
|
||||
print('Warning: InversionDirective %s has switched to a new inversion.' % self.__name__)
|
||||
self._inversion = i
|
||||
|
||||
@property
|
||||
@@ -68,7 +80,7 @@ class DirectiveList(object):
|
||||
def inversion(self, i):
|
||||
if self.inversion is i: return
|
||||
if getattr(self,'_inversion',None) is not None:
|
||||
print 'Warning: %s has switched to a new inversion.' % self.__name__
|
||||
print('Warning: %s has switched to a new inversion.' % self.__name__)
|
||||
for d in self.dList:
|
||||
d.inversion = i
|
||||
self._inversion = i
|
||||
@@ -120,7 +132,7 @@ class BetaEstimate_ByEig(InversionDirective):
|
||||
:return: beta0
|
||||
"""
|
||||
|
||||
if self.debug: print 'Calculating the beta0 parameter.'
|
||||
if self.debug: print('Calculating the beta0 parameter.')
|
||||
|
||||
m = self.invProb.curModel
|
||||
f = self.invProb.getFields(m, store=True, deleteWarmstart=False)
|
||||
@@ -128,7 +140,7 @@ class BetaEstimate_ByEig(InversionDirective):
|
||||
x0 = np.random.rand(*m.shape)
|
||||
t = x0.dot(self.dmisfit.eval2Deriv(m,x0,f=f))
|
||||
b = x0.dot(self.reg.eval2Deriv(m, v=x0))
|
||||
self.beta0 = self.beta0_ratio*(t/b)
|
||||
self.beta0 = self.beta0_ratio*(old_div(t,b))
|
||||
|
||||
self.invProb.beta = self.beta0
|
||||
|
||||
@@ -141,7 +153,7 @@ class BetaSchedule(InversionDirective):
|
||||
|
||||
def endIter(self):
|
||||
if self.opt.iter > 0 and self.opt.iter % self.coolingRate == 0:
|
||||
if self.debug: print 'BetaSchedule is cooling Beta. Iteration: %d' % self.opt.iter
|
||||
if self.debug: print('BetaSchedule is cooling Beta. Iteration: %d' % self.opt.iter)
|
||||
self.invProb.beta /= self.coolingFactor
|
||||
|
||||
|
||||
@@ -192,7 +204,7 @@ class SaveModelEveryIteration(SaveEveryIteration):
|
||||
"""SaveModelEveryIteration"""
|
||||
|
||||
def initialize(self):
|
||||
print "SimPEG.SaveModelEveryIteration will save your models as: '###-%s.npy'"%self.fileName
|
||||
print("SimPEG.SaveModelEveryIteration will save your models as: '###-%s.npy'"%self.fileName)
|
||||
|
||||
def endIter(self):
|
||||
np.save('%03d-%s' % (self.opt.iter, self.fileName), self.opt.xc)
|
||||
@@ -202,7 +214,7 @@ class SaveOutputEveryIteration(SaveEveryIteration):
|
||||
"""SaveModelEveryIteration"""
|
||||
|
||||
def initialize(self):
|
||||
print "SimPEG.SaveOutputEveryIteration will save your inversion progress as: '###-%s.txt'"%self.fileName
|
||||
print("SimPEG.SaveOutputEveryIteration will save your inversion progress as: '###-%s.txt'"%self.fileName)
|
||||
f = open(self.fileName+'.txt', 'w')
|
||||
f.write(" # beta phi_d phi_m f\n")
|
||||
f.close()
|
||||
@@ -216,7 +228,7 @@ class SaveOutputDictEveryIteration(SaveEveryIteration):
|
||||
"""SaveOutputDictEveryIteration"""
|
||||
|
||||
def initialize(self):
|
||||
print "SimPEG.SaveOutputDictEveryIteration will save your inversion progress as dictionary: '###-%s.npz'"%self.fileName
|
||||
print("SimPEG.SaveOutputDictEveryIteration will save your inversion progress as dictionary: '###-%s.npz'"%self.fileName)
|
||||
|
||||
def endIter(self):
|
||||
# Save the data.
|
||||
@@ -294,7 +306,7 @@ class Update_IRLS(InversionDirective):
|
||||
|
||||
# After reaching target misfit with l2-norm, switch to IRLS (mode:2)
|
||||
if self.invProb.phi_d < self.target and self.mode == 1:
|
||||
print "Convergence with smooth l2-norm regularization: Start IRLS steps..."
|
||||
print("Convergence with smooth l2-norm regularization: Start IRLS steps...")
|
||||
|
||||
self.mode = 2
|
||||
|
||||
@@ -302,17 +314,17 @@ class Update_IRLS(InversionDirective):
|
||||
# model values
|
||||
if getattr(self, 'reg.eps', None) is None:
|
||||
self.reg.eps_p = np.percentile(np.abs(self.invProb.curModel),self.prctile)
|
||||
else:
|
||||
else:
|
||||
self.reg.eps_p = self.eps[0]
|
||||
|
||||
|
||||
if getattr(self, 'reg.eps', None) is None:
|
||||
self.reg.eps_q = np.percentile(np.abs(self.reg.regmesh.cellDiffxStencil*(self.reg.mapping * self.invProb.curModel)),self.prctile)
|
||||
else:
|
||||
else:
|
||||
self.reg.eps_q = self.eps[1]
|
||||
|
||||
print "L[p qx qy qz]-norm : " + str(self.reg.norms)
|
||||
print "eps_p: " + str(self.reg.eps_p) + " eps_q: " + str(self.reg.eps_q)
|
||||
|
||||
|
||||
print("L[p qx qy qz]-norm : " + str(self.reg.norms))
|
||||
print("eps_p: " + str(self.reg.eps_p) + " eps_q: " + str(self.reg.eps_q))
|
||||
|
||||
self.reg.norms = self.norms
|
||||
self.coolingFactor = 1.
|
||||
self.coolingRate = 1
|
||||
@@ -328,7 +340,7 @@ class Update_IRLS(InversionDirective):
|
||||
|
||||
# Beta Schedule
|
||||
if self.opt.iter > 0 and self.opt.iter % self.coolingRate == 0:
|
||||
if self.debug: print 'BetaSchedule is cooling Beta. Iteration: %d' % self.opt.iter
|
||||
if self.debug: print('BetaSchedule is cooling Beta. Iteration: %d' % self.opt.iter)
|
||||
self.invProb.beta /= self.coolingFactor
|
||||
|
||||
|
||||
@@ -338,19 +350,19 @@ class Update_IRLS(InversionDirective):
|
||||
self.IRLSiter += 1
|
||||
|
||||
phim_new = self.reg.eval(self.invProb.curModel)
|
||||
self.f_change = np.abs(self.f_old - phim_new) / self.f_old
|
||||
self.f_change = old_div(np.abs(self.f_old - phim_new), self.f_old)
|
||||
|
||||
print "Regularization decrease: %6.3e" % (self.f_change)
|
||||
print("Regularization decrease: %6.3e" % (self.f_change))
|
||||
|
||||
# Check for maximum number of IRLS cycles
|
||||
if self.IRLSiter == self.maxIRLSiter:
|
||||
print "Reach maximum number of IRLS cycles: %i" % self.maxIRLSiter
|
||||
print("Reach maximum number of IRLS cycles: %i" % self.maxIRLSiter)
|
||||
self.opt.stopNextIteration = True
|
||||
return
|
||||
|
||||
# Check if the function has changed enough
|
||||
if self.f_change < self.f_min_change and self.IRLSiter > 1:
|
||||
print "Minimum decrease in regularization. End of IRLS"
|
||||
print("Minimum decrease in regularization. End of IRLS")
|
||||
self.opt.stopNextIteration = True
|
||||
return
|
||||
else:
|
||||
@@ -385,7 +397,7 @@ class Update_IRLS(InversionDirective):
|
||||
phim_new = self.reg.eval(self.invProb.curModel)
|
||||
|
||||
# Update gamma to scale the regularization between IRLS iterations
|
||||
self.reg.gamma = self.phi_m_last / phim_new
|
||||
self.reg.gamma = old_div(self.phi_m_last, phim_new)
|
||||
|
||||
# Reset the regularization matrices again for new gamma
|
||||
self.reg._Wsmall = None
|
||||
@@ -394,7 +406,7 @@ class Update_IRLS(InversionDirective):
|
||||
self.reg._Wz = None
|
||||
|
||||
# Check if misfit is within the tolerance, otherwise scale beta
|
||||
val = self.invProb.phi_d / (self.survey.nD*0.5)
|
||||
val = old_div(self.invProb.phi_d, (self.survey.nD*0.5))
|
||||
|
||||
if np.abs(1.-val) > self.beta_tol:
|
||||
self.invProb.beta = self.invProb.beta * self.survey.nD*0.5 / self.invProb.phi_d
|
||||
@@ -438,7 +450,7 @@ class Update_Wj(InversionDirective):
|
||||
|
||||
m = self.invProb.curModel
|
||||
if self.k is None:
|
||||
self.k = int(self.survey.nD/10)
|
||||
self.k = int(old_div(self.survey.nD,10))
|
||||
|
||||
def JtJv(v):
|
||||
|
||||
@@ -447,6 +459,6 @@ class Update_Wj(InversionDirective):
|
||||
return self.prob.Jtvec(m,Jv)
|
||||
|
||||
JtJdiag = Utils.diagEst(JtJv,len(m),k=self.k)
|
||||
JtJdiag = JtJdiag / max(JtJdiag)
|
||||
JtJdiag = old_div(JtJdiag, max(JtJdiag))
|
||||
|
||||
self.reg.wght = JtJdiag
|
||||
|
||||
Reference in New Issue
Block a user