mirror of
https://github.com/wassname/simpeg.git
synced 2026-07-09 13:25:42 +08:00
Refactored and moved duplicate code to utils.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import numpy as np
|
||||
import scipy.sparse as sp
|
||||
from SimPEG.utils import sdiag, mkvc
|
||||
from SimPEG.utils import sdiag, mkvc, setKwargs
|
||||
|
||||
class Inversion(object):
|
||||
"""docstring for Inversion"""
|
||||
@@ -9,7 +9,7 @@ class Inversion(object):
|
||||
name = 'SimPEG Inversion'
|
||||
|
||||
def __init__(self, prob, reg, opt, **kwargs):
|
||||
self.setKwargs(**kwargs)
|
||||
setKwargs(self, **kwargs)
|
||||
self.prob = prob
|
||||
self.reg = reg
|
||||
self.opt = opt
|
||||
@@ -31,22 +31,6 @@ class Inversion(object):
|
||||
"value": lambda M: M.parent.phi_m,
|
||||
"width": 10, "format": "%1.2e"})
|
||||
|
||||
def setKwargs(self, **kwargs):
|
||||
"""Sets key word arguments (kwargs) that are present in the object, throw an error if they don't exist."""
|
||||
for attr in kwargs:
|
||||
if hasattr(self, attr):
|
||||
setattr(self, attr, kwargs[attr])
|
||||
else:
|
||||
raise Exception('%s attr is not recognized' % attr)
|
||||
|
||||
# def printInit(self):
|
||||
# print "%s %s %s" % ('='*22, self.name, '='*22)
|
||||
# print " # beta phi_d phi_m f norm(dJ) #LS"
|
||||
# print "%s" % '-'*62
|
||||
|
||||
# def printIter(self):
|
||||
# print "%3d %1.2e %1.2e %1.2e %1.2e %1.2e %3d" % (self.opt._iter, self._beta, self.phi_d, self.phi_m, self.opt.f, np.linalg.norm(self.opt.g), self.opt._iterLS)
|
||||
|
||||
@property
|
||||
def Wd(self):
|
||||
"""
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from SimPEG.utils import mkvc, sdiag
|
||||
from SimPEG.utils import mkvc, sdiag, setKwargs, printTitles, printLine, printStoppers
|
||||
norm = np.linalg.norm
|
||||
import scipy.sparse as sp
|
||||
from SimPEG import Solver
|
||||
@@ -122,15 +122,7 @@ class Minimize(object):
|
||||
"format": "%1.2e"
|
||||
}]
|
||||
|
||||
self.setKwargs(**kwargs)
|
||||
|
||||
def setKwargs(self, **kwargs):
|
||||
"""Sets key word arguments (kwargs) that are present in the object, throw an error if they don't exist."""
|
||||
for attr in kwargs:
|
||||
if hasattr(self, attr):
|
||||
setattr(self, attr, kwargs[attr])
|
||||
else:
|
||||
raise Exception('%s attr is not recognized' % attr)
|
||||
setKwargs(self, **kwargs)
|
||||
|
||||
def minimize(self, evalFunction, x0):
|
||||
"""
|
||||
@@ -276,17 +268,8 @@ class Minimize(object):
|
||||
"""
|
||||
if doPub and not inLS: pub.sendMessage('Minimize.printInit', minimize=self)
|
||||
pad = ' '*10 if inLS else ''
|
||||
|
||||
printers = self.printers if not inLS else self.printersLS
|
||||
name = self.name if not inLS else self.nameLS
|
||||
titles = ''
|
||||
widths = 0
|
||||
for printer in printers:
|
||||
titles += ('{:^%i}'%printer['width']).format(printer['title']) + ''
|
||||
widths += printer['width']
|
||||
print pad + "{0} {1} {0}".format('='*((widths-1-len(name))/2), name)
|
||||
print pad + titles
|
||||
print pad + "%s" % '-'*widths
|
||||
printTitles(self, self.printers if not inLS else self.printersLS, name, pad)
|
||||
|
||||
def printIter(self, inLS=False):
|
||||
"""
|
||||
@@ -298,13 +281,7 @@ class Minimize(object):
|
||||
"""
|
||||
if doPub and not inLS: pub.sendMessage('Minimize.printIter', minimize=self)
|
||||
pad = ' '*10 if inLS else ''
|
||||
|
||||
printers = self.printers if not inLS else self.printersLS
|
||||
values = ''
|
||||
for printer in printers:
|
||||
values += ('{:^%i}'%printer['width']).format(printer['format'] % printer['value'](self))
|
||||
print pad + values
|
||||
# print pad + "%3d\t%1.2e\t%1.2e\t%d" % (self._iter, self.f, norm(self.g), self._iterLS)
|
||||
printLine(self, self.printers if not inLS else self.printersLS, pad=pad)
|
||||
|
||||
def printDone(self, inLS=False):
|
||||
"""
|
||||
@@ -317,15 +294,8 @@ class Minimize(object):
|
||||
if doPub and not inLS: pub.sendMessage('Minimize.printDone', minimize=self)
|
||||
pad = ' '*10 if inLS else ''
|
||||
stop, done = (' STOP! ', ' DONE! ') if not inLS else ('----------------', ' End Linesearch ')
|
||||
print pad + "%s%s%s" % ('-'*25,stop,'-'*25)
|
||||
|
||||
stoppers = self.stoppers if not inLS else self.stoppersLS
|
||||
for stopper in stoppers:
|
||||
l = stopper['left'](self)
|
||||
r = stopper['right'](self)
|
||||
print pad + stopper['str'] % (l<=r,l,r)
|
||||
|
||||
print pad + "%s%s%s" % ('-'*25,done,'-'*25)
|
||||
printStoppers(self, stoppers, pad='', stop=stop, done=done)
|
||||
|
||||
|
||||
def stoppingCriteria(self, inLS=False):
|
||||
|
||||
@@ -10,3 +10,36 @@ from sputils import spzeros, kron3, speye, sdiag
|
||||
from lomutils import volTetra, faceInfo, inv2X2BlockDiagonal, inv3X3BlockDiagonal, indexCube, exampleLomGird
|
||||
from interputils import interpmat
|
||||
from ipythonUtils import easyAnimate as animate
|
||||
|
||||
|
||||
def setKwargs(obj, **kwargs):
|
||||
"""Sets key word arguments (kwargs) that are present in the object, throw an error if they don't exist."""
|
||||
for attr in kwargs:
|
||||
if hasattr(obj, attr):
|
||||
setattr(obj, attr, kwargs[attr])
|
||||
else:
|
||||
raise Exception('%s attr is not recognized' % attr)
|
||||
|
||||
def printTitles(obj, printers, name='Print Titles', pad=''):
|
||||
titles = ''
|
||||
widths = 0
|
||||
for printer in printers:
|
||||
titles += ('{:^%i}'%printer['width']).format(printer['title']) + ''
|
||||
widths += printer['width']
|
||||
print pad + "{0} {1} {0}".format('='*((widths-1-len(name))/2), name)
|
||||
print pad + titles
|
||||
print pad + "%s" % '-'*widths
|
||||
|
||||
def printLine(obj, printers, pad=''):
|
||||
values = ''
|
||||
for printer in printers:
|
||||
values += ('{:^%i}'%printer['width']).format(printer['format'] % printer['value'](obj))
|
||||
print pad + values
|
||||
|
||||
def printStoppers(obj, stoppers, pad='', stop='STOP!', done='DONE!'):
|
||||
print pad + "%s%s%s" % ('-'*25,stop,'-'*25)
|
||||
for stopper in stoppers:
|
||||
l = stopper['left'](obj)
|
||||
r = stopper['right'](obj)
|
||||
print pad + stopper['str'] % (l<=r,l,r)
|
||||
print pad + "%s%s%s" % ('-'*25,done,'-'*25)
|
||||
|
||||
Reference in New Issue
Block a user