From e4448c2f2ef6c86123a0506f367d7b341758b2b3 Mon Sep 17 00:00:00 2001 From: GudniRos Date: Fri, 14 Aug 2015 11:57:02 -0700 Subject: [PATCH] Progressing with pickling. Pickling of PropModels doesn't work which cause many classes that use it not to pickle. --- SimPEG/DataMisfit.py | 28 +++++++++++ SimPEG/Directives.py | 28 +++++++++++ SimPEG/Fields.py | 28 +++++++++++ SimPEG/Maps.py | 30 +++++++++-- SimPEG/Optimization.py | 28 +++++++++++ SimPEG/Problem.py | 32 +++++++++++- SimPEG/PropMaps.py | 78 ++++++++++++++++++++++++++++- SimPEG/Regularization.py | 30 ++++++++++- SimPEG/Survey.py | 105 ++++++++++++++++++++++++++++++++++++++- 9 files changed, 379 insertions(+), 8 deletions(-) diff --git a/SimPEG/DataMisfit.py b/SimPEG/DataMisfit.py index c1203d47..a83da4e1 100644 --- a/SimPEG/DataMisfit.py +++ b/SimPEG/DataMisfit.py @@ -14,6 +14,34 @@ class BaseDataMisfit(object): debug = False #: Print debugging information counter = None #: Set this to a SimPEG.Utils.Counter() if you want to count things + # Pickleing support methods + def __getstate__(self): + ''' + Method that makes the dictionary of the object pickleble, removes non-pickleble elements of the object. + + Used when doing: + pickle.dump(pickleFile,object) + ''' + odict = self.__dict__.copy() + # Remove fields that are not needed + del odict['hook'] + del odict['setKwargs'] + # Return the dict + return odict + + def __setstate__(self,odict): + ''' + Function that sets a pickle dictionary in to an object. + + Used when doing: + object = pickle.load(pickleFile) + ''' + # Update the dict + self.__dict__.update(odict) + # Re-hook the methods to the object + Utils.codeutils.hook(self,Utils.codeutils.hook) + Utils.codeutils.hook(self,Utils.codeutils.setKwargs) + def __init__(self, survey, **kwargs): assert survey.ispaired, 'The survey must be paired to a problem.' if isinstance(survey, Survey.BaseSurvey): diff --git a/SimPEG/Directives.py b/SimPEG/Directives.py index 9e3dafef..e7ea0485 100644 --- a/SimPEG/Directives.py +++ b/SimPEG/Directives.py @@ -8,6 +8,34 @@ class InversionDirective(object): def __init__(self, **kwargs): Utils.setKwargs(self, **kwargs) + # Pickleing support methods + def __getstate__(self): + ''' + Method that makes the dictionary of the object pickleble, removes non-pickleble elements of the object. + + Used when doing: + pickle.dump(pickleFile,object) + ''' + odict = self.__dict__.copy() + # Remove fields that are not needed + del odict['hook'] + del odict['setKwargs'] + # Return the dict + return odict + + def __setstate__(self,odict): + ''' + Function that sets a pickle dictionary in to an object. + + Used when doing: + object = pickle.load(pickleFile) + ''' + # Update the dict + self.__dict__.update(odict) + # Re-hook the methods to the object + Utils.codeutils.hook(self,Utils.codeutils.hook) + Utils.codeutils.hook(self,Utils.codeutils.setKwargs) + @property def inversion(self): """This is the inversion of the InversionDirective instance.""" diff --git a/SimPEG/Fields.py b/SimPEG/Fields.py index 9baa32f8..4767de1f 100644 --- a/SimPEG/Fields.py +++ b/SimPEG/Fields.py @@ -12,6 +12,34 @@ class Fields(object): aliasFields = None #: Aliased fields, a dict with [alias, location, function], e.g. {"b":["e","F",lambda(F,e,ind)]} dtype = float #: dtype is the type of the storage matrix. This can be a dictionary. + # Pickleing support methods + def __getstate__(self): + ''' + Method that makes the dictionary of the object pickleble, removes non-pickleble elements of the object. + + Used when doing: + pickle.dump(pickleFile,object) + ''' + odict = self.__dict__.copy() + # Remove fields that are not needed + del odict['hook'] + del odict['setKwargs'] + # Return the dict + return odict + + def __setstate__(self,odict): + ''' + Function that sets a pickle dictionary in to an object. + + Used when doing: + object = pickle.load(pickleFile) + ''' + # Update the dict + self.__dict__.update(odict) + # Re-hook the methods to the object + Utils.codeutils.hook(self,Utils.codeutils.hook) + Utils.codeutils.hook(self,Utils.codeutils.setKwargs) + def __init__(self, mesh, survey, **kwargs): self.survey = survey self.mesh = mesh diff --git a/SimPEG/Maps.py b/SimPEG/Maps.py index 6dca13dd..97a9261a 100644 --- a/SimPEG/Maps.py +++ b/SimPEG/Maps.py @@ -17,6 +17,30 @@ class IdentityMap(object): Utils.setKwargs(self, **kwargs) self.mesh = mesh + # Pickleing support methods + def __getstate__(self): + ''' + Method that makes the dictionary of the object pickleble, removes non-pickleble elements of the object. + + Used when doing: + pickle.dump(pickleFile,object) + ''' + odict = self.__dict__.copy() + # Remove fields that are not needed + # Return the dict + return odict + + def __setstate__(self,odict): + ''' + Function that sets a pickle dictionary in to an object. + + Used when doing: + object = pickle.load(pickleFile) + ''' + # Update the dict + self.__dict__.update(odict) + # Re-hook the methods to the object + @property def nP(self): """ @@ -289,7 +313,7 @@ class FullMap(IdentityMap): """ FullMap - Given a scalar, the FullMap maps the value to the + Given a scalar, the FullMap maps the value to the full model space. """ @@ -314,8 +338,8 @@ class FullMap(IdentityMap): :rtype: numpy.array :return: derivative of transformed model """ - return np.ones([self.mesh.nC,1]) - + return np.ones([self.mesh.nC,1]) + class Vertical1DMap(IdentityMap): """Vertical1DMap diff --git a/SimPEG/Optimization.py b/SimPEG/Optimization.py index 4f2cb062..c5b20d4f 100644 --- a/SimPEG/Optimization.py +++ b/SimPEG/Optimization.py @@ -115,6 +115,34 @@ class Minimize(object): Utils.setKwargs(self, **kwargs) + # Pickleing support methods + def __getstate__(self): + ''' + Method that makes the dictionary of the object pickleble, removes non-pickleble elements of the object. + + Used when doing: + pickle.dump(pickleFile,object) + ''' + odict = self.__dict__.copy() + # Remove fields that are not needed + del odict['hook'] + del odict['setKwargs'] + # Return the dict + return odict + + def __setstate__(self,odict): + ''' + Function that sets a pickle dictionary in to an object. + + Used when doing: + object = pickle.load(pickleFile) + ''' + # Update the dict + self.__dict__.update(odict) + # Re-hook the methods to the object + Utils.codeutils.hook(self,Utils.codeutils.hook) + Utils.codeutils.hook(self,Utils.codeutils.setKwargs) + @property def callback(self): return getattr(self, '_callback', None) diff --git a/SimPEG/Problem.py b/SimPEG/Problem.py index 607cff5b..720b8f36 100644 --- a/SimPEG/Problem.py +++ b/SimPEG/Problem.py @@ -22,6 +22,34 @@ class BaseProblem(object): PropMap = None #: A SimPEG PropertyMap class. + # Pickleing support methods + def __getstate__(self): + ''' + Method that makes the dictionary of the object pickleble, removes non-pickleble elements of the object. + + Used when doing: + pickle.dump(pickleFile,object) + ''' + odict = self.__dict__.copy() + # Remove fields that are not needed + del odict['hook'] + del odict['setKwargs'] + # Return the dict + return odict + + def __setstate__(self,odict): + ''' + Function that sets a pickle dictionary in to an object. + + Used when doing: + object = pickle.load(pickleFile) + ''' + # Update the dict + self.__dict__.update(odict) + # Re-hook the methods to the object + Utils.codeutils.hook(self,Utils.codeutils.hook) + Utils.codeutils.hook(self,Utils.codeutils.setKwargs) + @property def mapping(self): "A SimPEG.Map instance or a property map is PropMap is not None" @@ -32,8 +60,8 @@ class BaseProblem(object): val._assertMatchesPair(self.mapPair) self._mapping = val else: - self._mapping = self.PropMap(val) - + self._mapping = self.PropMap(val) + def __init__(self, mesh, mapping=None, **kwargs): Utils.setKwargs(self, **kwargs) assert isinstance(mesh, Mesh.BaseMesh), "mesh must be a SimPEG.Mesh object." diff --git a/SimPEG/PropMaps.py b/SimPEG/PropMaps.py index 527a6f7e..ecd15b1b 100644 --- a/SimPEG/PropMaps.py +++ b/SimPEG/PropMaps.py @@ -13,6 +13,34 @@ class Property(object): self.doc = doc Utils.setKwargs(self, **kwargs) + # Pickleing support methods + def __getstate__(self): + ''' + Method that makes the dictionary of the object pickleble, removes non-pickleble elements of the object. + + Used when doing: + pickle.dump(pickleFile,object) + ''' + odict = self.__dict__.copy() + # Remove fields that are not needed + del odict['hook'] + del odict['setKwargs'] + # Return the dict + return odict + + def __setstate__(self,odict): + ''' + Function that sets a pickle dictionary in to an object. + + Used when doing: + object = pickle.load(pickleFile) + ''' + # Update the dict + self.__dict__.update(odict) + # Re-hook the methods to the object + Utils.codeutils.hook(self,Utils.codeutils.hook) + Utils.codeutils.hook(self,Utils.codeutils.setKwargs) + @property def propertyLink(self): "Can be something like: ('sigma', Maps.ReciprocalMap)" @@ -118,6 +146,36 @@ class PropModel(object): self.vector = vector assert len(self.vector) == self.nP + # Pickleing support methods + def __reduce__(self): + return (dict,{self.propMap,self.vector}) + + # def __getstate__(self): + # ''' + # Method that makes the dictionary of the object pickleble, removes non-pickleble elements of the object. + + # Used when doing: + # pickle.dump(pickleFile,object) + # ''' + # self.__class__ = ProbModel + # odict = {} + # odict['vec'] = self.__dict__['vector'] + # odict['pMap'] = self.__dict__['propMap'] + # # Return the dict + # return odict + + # def __setstate__(self,odict): + # ''' + # Function that sets a pickle dictionary in to an object. + + # Used when doing: + # object = pickle.load(pickleFile) + # ''' + # # Update the dict + # # Re-hook the methods to the object + # self.propMap = odict['prMap'] + # self.vector = odict['vec'] + @property def nP(self): inds = [] @@ -207,6 +265,24 @@ class PropMap(object): else: raise Exception('mappings must be a dict, a mapping, or a list of tuples.') + # Pickleing support methods + def __getstate__(self): + ''' + Method that makes the dictionary of the object pickleble, removes non-pickleble elements of the object. + + Used when doing: + pickle.dump(pickleFile,object) + ''' + pass + + def __setstate__(self,odict): + ''' + Function that sets a pickle dictionary in to an object. + + Used when doing: + object = pickle.load(pickleFile) + ''' + pass def setup(self, maps, slices=None): """ @@ -239,7 +315,7 @@ class PropMap(object): setattr(self, '%sMap'%name, mapping) setattr(self, '%sIndex'%name, slices.get(name, slice(nP, nP + mapping.nP))) nP += mapping.nP - self.nP = nP + self.nP = nP @property def defaultInvProp(self): diff --git a/SimPEG/Regularization.py b/SimPEG/Regularization.py index e3506290..8534cced 100644 --- a/SimPEG/Regularization.py +++ b/SimPEG/Regularization.py @@ -27,6 +27,34 @@ class BaseRegularization(object): self.mapping = mapping or Maps.IdentityMap(mesh) self.mapping._assertMatchesPair(self.mapPair) + # Pickleing support methods + def __getstate__(self): + ''' + Method that makes the dictionary of the object pickleble, removes non-pickleble elements of the object. + + Used when doing: + pickle.dump(pickleFile,object) + ''' + odict = self.__dict__.copy() + # Remove fields that are not needed + del odict['hook'] + del odict['setKwargs'] + # Return the dict + return odict + + def __setstate__(self,odict): + ''' + Function that sets a pickle dictionary in to an object. + + Used when doing: + object = pickle.load(pickleFile) + ''' + # Update the dict + self.__dict__.update(odict) + # Re-hook the methods to the object + Utils.codeutils.hook(self,Utils.codeutils.hook) + Utils.codeutils.hook(self,Utils.codeutils.setKwargs) + @property def parent(self): """This is the parent of the regularization.""" @@ -311,7 +339,7 @@ class Tikhonov(BaseRegularization): if self.smoothModel == True: mD1 = self.mapping.deriv(m) mD2 = self.mapping.deriv(m - self.mref) - r1 = self.Wsmooth * ( self.mapping * (m)) + r1 = self.Wsmooth * ( self.mapping * (m)) r2 = self.Ws * ( self.mapping * (m - self.mref) ) out1 = mD1.T * ( self.Wsmooth.T * r1 ) out2 = mD2.T * ( self.Ws.T * r2 ) diff --git a/SimPEG/Survey.py b/SimPEG/Survey.py index 0fdb0cd1..8faf8fd1 100644 --- a/SimPEG/Survey.py +++ b/SimPEG/Survey.py @@ -19,6 +19,35 @@ class BaseRx(object): self._Ps = {} Utils.setKwargs(self, **kwargs) + # Pickleing support methods + def __getstate__(self): + ''' + Method that makes the dictionary of the object pickleble, removes non-pickleble elements of the object. + + Used when doing: + pickle.dump(pickleFile,object) + ''' + odict = self.__dict__.copy() + # Remove fields that are not needed + del odict['hook'] + del odict['setKwargs'] + # Return the dict + return odict + + def __setstate__(self,odict): + ''' + Function that sets a pickle dictionary in to an object. + + Used when doing: + object = pickle.load(pickleFile) + ''' + # Update the dict + self.__dict__.update(odict) + # Re-hook the methods to the object + Utils.codeutils.hook(self,Utils.codeutils.hook) + Utils.codeutils.hook(self,Utils.codeutils.setKwargs) + + @property def rxType(self): """Receiver Type""" @@ -129,6 +158,33 @@ class BaseSrc(object): self.rxList = rxList Utils.setKwargs(self, **kwargs) + # Pickleing support methods + def __getstate__(self): + ''' + Method that makes the dictionary of the object pickleble, removes non-pickleble elements of the object. + + Used when doing: + pickle.dump(pickleFile,object) + ''' + odict = self.__dict__.copy() + # Remove fields that are not needed + del odict['hook'] + del odict['setKwargs'] + # Return the dict + return odict + + def __setstate__(self,odict): + ''' + Function that sets a pickle dictionary in to an object. + + Used when doing: + object = pickle.load(pickleFile) + ''' + # Update the dict + self.__dict__.update(odict) + # Re-hook the methods to the object + Utils.codeutils.hook(self,Utils.codeutils.hook) + Utils.codeutils.hook(self,Utils.codeutils.setKwargs) @property def nD(self): @@ -153,6 +209,25 @@ class Data(object): if v is not None: self.fromvec(v) + # Pickleing support methods + def __getstate__(self): + ''' + Method that makes the dictionary of the object pickleble, removes non-pickleble elements of the object. + + Used when doing: + pickle.dump(pickleFile,object) + ''' + pass + + def __setstate__(self,odict): + ''' + Function that sets a pickle dictionary in to an object. + + Used when doing: + object = pickle.load(pickleFile) + ''' + pass + def _ensureCorrectKey(self, key): if type(key) is tuple: if len(key) is not 2: @@ -210,11 +285,39 @@ class BaseSurvey(object): mtrue = None #: True model, if data is synthetic counter = None #: A SimPEG.Utils.Counter object + srcPair = BaseSrc #: Source Pair def __init__(self, **kwargs): Utils.setKwargs(self, **kwargs) - srcPair = BaseSrc #: Source Pair + # Pickleing support methods + def __getstate__(self): + ''' + Method that makes the dictionary of the object pickleble, removes non-pickleble elements of the object. + + Used when doing: + pickle.dump(pickleFile,object) + ''' + odict = self.__dict__.copy() + # Remove fields that are not needed + del odict['hook'] + del odict['setKwargs'] + # Return the dict + return odict + + def __setstate__(self,odict): + ''' + Function that sets a pickle dictionary in to an object. + + Used when doing: + object = pickle.load(pickleFile) + ''' + # Update the dict + self.__dict__.update(odict) + # Re-hook the methods to the object + Utils.codeutils.hook(self,Utils.codeutils.hook) + Utils.codeutils.hook(self,Utils.codeutils.setKwargs) + @property def srcList(self):