Rules to Directives

This commit is contained in:
rowanc1
2014-05-16 12:10:15 -07:00
parent 399678e496
commit 7b22d0aa7a
4 changed files with 19 additions and 19 deletions
+12 -12
View File
@@ -1,7 +1,7 @@
import Utils, numpy as np
class InversionRule(object):
"""InversionRule"""
class InversionDirective(object):
"""InversionDirective"""
debug = False #: Print debugging information
@@ -12,12 +12,12 @@ class InversionRule(object):
@property
def inversion(self):
"""This is the inversion of the InversionRule instance."""
"""This is the inversion of the InversionDirective instance."""
return getattr(self,'_inversion',None)
@inversion.setter
def inversion(self, i):
if getattr(self,'_inversion',None) is not None:
print 'Warning: InversionRule %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
@@ -40,14 +40,14 @@ class InversionRule(object):
def finish(self):
pass
class RuleList(object):
class DirectiveList(object):
rList = None #: The list of Rules
rList = None #: The list of Directives
def __init__(self, *rules, **kwargs):
self.rList = []
for r in rules:
assert isinstance(r, InversionRule), 'All rules must be InversionRules not %s' % r.__name__
assert isinstance(r, InversionDirective), 'All rules must be InversionDirectives not %s' % r.__name__
self.rList.append(r)
Utils.setKwargs(self, **kwargs)
@@ -62,7 +62,7 @@ class RuleList(object):
@property
def inversion(self):
"""This is the inversion of the InversionRule instance."""
"""This is the inversion of the InversionDirective instance."""
return getattr(self,'_inversion',None)
@inversion.setter
def inversion(self, i):
@@ -75,16 +75,16 @@ class RuleList(object):
def call(self, ruleType):
if self.rList is None:
if self.debug: 'RuleList is None, no rules to call!'
if self.debug: 'DirectiveList is None, no rules to call!'
return
rules = ['initialize', 'endIter', 'finish']
assert ruleType in rules, 'Rule type must be in ["%s"]' % '", "'.join(rules)
assert ruleType in rules, 'Directive type must be in ["%s"]' % '", "'.join(rules)
for r in self.rList:
getattr(r, ruleType)()
class BetaEstimate_ByEig(InversionRule):
class BetaEstimate_ByEig(InversionDirective):
"""BetaEstimate"""
beta0 = None #: The initial Beta (regularization parameter)
@@ -133,7 +133,7 @@ class BetaEstimate_ByEig(InversionRule):
self.objFunc.beta = self.beta0
class BetaSchedule(InversionRule):
class BetaSchedule(InversionDirective):
"""BetaSchedule"""
coolingFactor = 2.
+3 -3
View File
@@ -1,7 +1,7 @@
import SimPEG
from SimPEG import Utils, sp, np
from Optimization import Remember, IterationPrinters, StoppingCriteria
import Rules
import Directives
class BaseInversion(object):
@@ -19,12 +19,12 @@ class BaseInversion(object):
@property
def ruleList(self):
if getattr(self,'_ruleList', None) is None:
self._ruleList = Rules.RuleList(inversion=self)
self._ruleList = Directives.DirectiveList(inversion=self)
return self._ruleList
@ruleList.setter
def ruleList(self, value):
assert isinstance(value, Rules.RuleList), 'Must be a RuleList'
assert isinstance(value, Directives.DirectiveList), 'Must be a RuleList'
self._ruleList = value
self._ruleList.inversion = self
+1 -1
View File
@@ -9,7 +9,7 @@ import Survey
import Regularization
import ObjFunction
import Optimization
import Rules
import Directives
import Inversion
import Tests
+3 -3
View File
@@ -60,9 +60,9 @@ if __name__ == '__main__':
objFunc = ObjFunction.BaseObjFunction(survey, reg)
opt = Optimization.InexactGaussNewton(maxIter=20)
inv = Inversion.BaseInversion(objFunc, opt)
beta = Rules.BetaSchedule()
betaest = Rules.BetaEstimate_ByEig()
inv.ruleList = Rules.RuleList(betaest, beta)
beta = Directives.BetaSchedule()
betaest = Directives.BetaEstimate_ByEig()
inv.ruleList = Directives.DirectiveList(betaest, beta)
m0 = np.zeros_like(survey.mtrue)
mrec = inv.run(m0)