From 8475eadccebba8d95e18fbee1e8b688b86136619 Mon Sep 17 00:00:00 2001 From: Rowan Cockett Date: Mon, 1 Jun 2015 09:55:14 -0700 Subject: [PATCH] updates to propMap location and testing --- SimPEG/Maps.py | 1 + SimPEG/{Utils => }/PropMaps.py | 53 +++---------------------------- SimPEG/Tests/test_PropMaps.py | 57 ++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 48 deletions(-) rename SimPEG/{Utils => }/PropMaps.py (82%) create mode 100644 SimPEG/Tests/test_PropMaps.py diff --git a/SimPEG/Maps.py b/SimPEG/Maps.py index 0b47de66..4ab79e64 100644 --- a/SimPEG/Maps.py +++ b/SimPEG/Maps.py @@ -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): diff --git a/SimPEG/Utils/PropMaps.py b/SimPEG/PropMaps.py similarity index 82% rename from SimPEG/Utils/PropMaps.py rename to SimPEG/PropMaps.py index 7b40a0ec..7c22e149 100644 --- a/SimPEG/Utils/PropMaps.py +++ b/SimPEG/PropMaps.py @@ -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 diff --git a/SimPEG/Tests/test_PropMaps.py b/SimPEG/Tests/test_PropMaps.py new file mode 100644 index 00000000..4e7c8bb7 --- /dev/null +++ b/SimPEG/Tests/test_PropMaps.py @@ -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() + +