mirror of
https://github.com/wassname/simpeg.git
synced 2026-07-19 11:28:00 +08:00
updates to propMap location and testing
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import Utils, numpy as np, scipy.sparse as sp
|
||||
from Tests import checkDerivative
|
||||
from PropMaps import PropMap, Property
|
||||
|
||||
|
||||
class IdentityMap(object):
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from SimPEG import Utils, Maps
|
||||
import Utils, Maps, numpy as np
|
||||
|
||||
class Property(object):
|
||||
|
||||
@@ -7,7 +7,6 @@ class Property(object):
|
||||
# mappingPair = None
|
||||
|
||||
defaultVal = None
|
||||
defaultMap = None
|
||||
defaultInvProp = False
|
||||
|
||||
def __init__(self, doc, **kwargs):
|
||||
@@ -18,7 +17,7 @@ class Property(object):
|
||||
def _getMapProperty(self):
|
||||
prop = self
|
||||
def fget(self):
|
||||
return getattr(self, '_%sMap'%prop.name, prop.defaultMap)
|
||||
return getattr(self, '_%sMap'%prop.name, None)
|
||||
def fset(self, val):
|
||||
# TODO: Check if the mapping can be correct
|
||||
setattr(self, '_%sMap'%prop.name, val)
|
||||
@@ -65,7 +64,7 @@ class Property(object):
|
||||
def _getModelMapProperty(self):
|
||||
prop = self
|
||||
def fget(self):
|
||||
return getattr(self.propMap, '_%sMap'%prop.name, prop.defaultMap)
|
||||
return getattr(self.propMap, '_%sMap'%prop.name, None)
|
||||
return property(fget=fget)
|
||||
|
||||
|
||||
@@ -128,9 +127,6 @@ class _PropMapMetaClass(type):
|
||||
return type(name.replace('PropMap', 'PropModel'), (PropModel, ), attrs)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class PropMap(object):
|
||||
__metaclass__ = _PropMapMetaClass
|
||||
|
||||
@@ -141,7 +137,7 @@ class PropMap(object):
|
||||
if type(mappings) is dict:
|
||||
assert np.all([k in ['maps', 'slices'] for k in mappings]), 'Dict must only have properties "maps" and "slices"'
|
||||
self.setup(mappings['maps'], slices=mappings['slices'])
|
||||
if type(mappings) is list:
|
||||
elif type(mappings) is list:
|
||||
self.setup(mappings)
|
||||
elif isinstance(mappings, Maps.IdentityMap):
|
||||
self.setup([(self.defaultInvProp, mappings)])
|
||||
@@ -170,7 +166,7 @@ class PropMap(object):
|
||||
else:
|
||||
assert np.all([
|
||||
s in self._properties and
|
||||
(type(s) in [slice, list] or isinstance(s, np.ndarray))
|
||||
(type(slices[s]) in [slice, list] or isinstance(slices[s], np.ndarray))
|
||||
for s in slices]), 'Slices must be for each property'
|
||||
|
||||
self.clearMaps()
|
||||
@@ -181,7 +177,6 @@ class PropMap(object):
|
||||
setattr(self, '%sIndex'%name, slices.get(name, slice(nP, nP + mapping.nP)))
|
||||
nP += mapping.nP
|
||||
|
||||
|
||||
@property
|
||||
def defaultInvProp(self):
|
||||
for name in self._properties:
|
||||
@@ -197,41 +192,3 @@ class PropMap(object):
|
||||
def __call__(self, vec):
|
||||
return self.PropModel(self, vec)
|
||||
|
||||
|
||||
class MyPropMap(PropMap):
|
||||
sigma = Property("Electrical Conductivity", defaultInvProp=True)
|
||||
mu = Property("Electrical Conductivity", defaultVal=4e-10)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
|
||||
from SimPEG import Mesh
|
||||
import numpy as np
|
||||
|
||||
expMap = Maps.ExpMap(Mesh.TensorMesh((3,)))
|
||||
print expMap.nP
|
||||
|
||||
# propMap = MyPropMap([('sigma', expMap), ('mu', IMap)], indices={'sigma':[1,2,3,4,7,8]})
|
||||
propMap = MyPropMap([('sigma',expMap)])
|
||||
print [n for n in dir(propMap) if n[0] is not '_']
|
||||
# propMap = My2PropMap()
|
||||
# print [n for n in dir(propMap) if n[0] is not '_']
|
||||
|
||||
propMap.sigmaMap = expMap
|
||||
|
||||
print propMap.defaultInvProp
|
||||
print propMap.sigmaMap
|
||||
print propMap.sigmaIndex
|
||||
|
||||
mod = propMap(np.r_[1,2,3])
|
||||
print [n for n in dir(mod) if n[0] is not '_']
|
||||
print mod.sigmaModel
|
||||
print mod.sigma
|
||||
print mod.sigmaMap
|
||||
print mod.sigmaDeriv
|
||||
|
||||
print mod.mu
|
||||
print mod.muMap
|
||||
print mod.muModel
|
||||
print mod.muDeriv
|
||||
@@ -0,0 +1,57 @@
|
||||
import unittest
|
||||
from SimPEG import *
|
||||
|
||||
|
||||
|
||||
class MyPropMap(Maps.PropMap):
|
||||
sigma = Maps.Property("Electrical Conductivity", defaultInvProp=True)
|
||||
mu = Maps.Property("Electrical Conductivity", defaultVal=4e-10)
|
||||
|
||||
|
||||
class TestPropMaps(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def test_setup(self):
|
||||
expMap = Maps.ExpMap(Mesh.TensorMesh((3,)))
|
||||
assert expMap.nP == 3
|
||||
|
||||
PM1 = MyPropMap(expMap)
|
||||
PM2 = MyPropMap([('sigma', expMap)])
|
||||
PM3 = MyPropMap({'maps':[('sigma', expMap)], 'slices':{'sigma':slice(0,3)}})
|
||||
|
||||
for PM in [PM1,PM2,PM3]:
|
||||
assert PM.defaultInvProp == 'sigma'
|
||||
assert PM.sigmaMap is not None
|
||||
assert PM.sigmaMap is expMap
|
||||
assert PM.sigmaIndex == slice(0,3)
|
||||
assert getattr(PM, 'sigma', None) is None
|
||||
assert PM.muMap is None
|
||||
assert PM.muIndex is None
|
||||
|
||||
m = PM(np.r_[1,2,3])
|
||||
assert m.mu == 4e-10
|
||||
assert m.muModel is None
|
||||
assert m.muMap is None
|
||||
assert m.muDeriv is None
|
||||
|
||||
assert np.all(m.sigmaModel == np.r_[1,2,3])
|
||||
assert m.sigmaMap is expMap
|
||||
assert np.all(m.sigma == np.exp(np.r_[1,2,3]))
|
||||
assert m.sigmaDeriv is not None
|
||||
|
||||
def test_slices(self):
|
||||
expMap = Maps.ExpMap(Mesh.TensorMesh((3,)))
|
||||
PM = MyPropMap({'maps':[('sigma', expMap)], 'slices':{'sigma':[2,1,0]}})
|
||||
assert PM.sigmaIndex == [2,1,0]
|
||||
m = PM(np.r_[1,2,3])
|
||||
assert np.all(m.sigmaModel == np.r_[3,2,1])
|
||||
assert np.all(m.sigma == np.exp(np.r_[3,2,1]))
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user