mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-06-02 14:45:15 +02:00
getstate > _getstate and setstate > _setstate
This commit is contained in:
parent
e128059377
commit
cec766b723
17 changed files with 73 additions and 71 deletions
|
|
@ -403,7 +403,7 @@ class Param(ObservableArray, Constrainable):
|
|||
x=self.name_hirarchical)
|
||||
return name + super(Param, self).__repr__(*args,**kwargs)
|
||||
def _ties_for(self, rav_index):
|
||||
size = sum(p.size for p in self._tied_to_)
|
||||
#size = sum(p.size for p in self._tied_to_)
|
||||
ties = numpy.empty(shape=(len(self._tied_to_), numpy.size(rav_index)), dtype=Param)
|
||||
for i, tied_to in enumerate(self._tied_to_):
|
||||
for t, ind in tied_to._tied_to_me_.iteritems():
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ class Observable(object):
|
|||
[callble(self) for callble in self._observers_.itervalues()]
|
||||
|
||||
class Pickleable(object):
|
||||
def getstate(self):
|
||||
def _getstate(self):
|
||||
"""
|
||||
Returns the state of this class in a memento pattern.
|
||||
The state must be a list-like structure of all the fields
|
||||
|
|
@ -30,13 +30,13 @@ class Pickleable(object):
|
|||
See python doc "pickling" (`__getstate__` and `__setstate__`) for details.
|
||||
"""
|
||||
raise NotImplementedError, "To be able to use pickling you need to implement this method"
|
||||
def setstate(self, state):
|
||||
def _setstate(self, state):
|
||||
"""
|
||||
Set the state (memento pattern) of this class to the given state.
|
||||
Usually this is just the counterpart to getstate, such that
|
||||
Usually this is just the counterpart to _getstate, such that
|
||||
an object is a copy of another when calling
|
||||
|
||||
copy = <classname>.__new__(*args,**kw).setstate(<to_be_copied>.getstate())
|
||||
copy = <classname>.__new__(*args,**kw)._setstate(<to_be_copied>._getstate())
|
||||
|
||||
See python doc "pickling" (`__getstate__` and `__setstate__`) for details.
|
||||
"""
|
||||
|
|
@ -54,7 +54,7 @@ class Parentable(object):
|
|||
self._highest_parent_ = highest_parent
|
||||
|
||||
def has_parent(self):
|
||||
return self._direct_parent_ is not None
|
||||
return self._direct_parent_ is not None and self._highest_parent_ is not None
|
||||
|
||||
class Nameable(Parentable):
|
||||
_name = None
|
||||
|
|
@ -96,6 +96,7 @@ class Constrainable(Nameable):
|
|||
self._add_constrain(p, transform, warning)
|
||||
if update:
|
||||
self.parameters_changed()
|
||||
|
||||
def constrain_positive(self, warning=True):
|
||||
"""
|
||||
:param warning: print a warning if re-constraining parameters.
|
||||
|
|
|
|||
|
|
@ -218,7 +218,7 @@ class Parameterized(Constrainable, Pickleable, Observable):
|
|||
p._direct_parent_ = self
|
||||
p._parent_index_ = i
|
||||
i += 1
|
||||
for pi in p.flattened_parameters:
|
||||
for pi in p._parameters_:
|
||||
pi._highest_parent_ = self
|
||||
not_unique = []
|
||||
sizes.append(p.size+sizes[-1])
|
||||
|
|
@ -233,6 +233,7 @@ class Parameterized(Constrainable, Pickleable, Observable):
|
|||
elif not (pname in not_unique):
|
||||
self.__dict__[pname] = p
|
||||
self._added_names_.add(pname)
|
||||
|
||||
#===========================================================================
|
||||
# Pickling operations
|
||||
#===========================================================================
|
||||
|
|
@ -253,24 +254,24 @@ class Parameterized(Constrainable, Pickleable, Observable):
|
|||
return copy.deepcopy(self)
|
||||
def __getstate__(self):
|
||||
if self._has_get_set_state():
|
||||
return self.getstate()
|
||||
return self._getstate()
|
||||
return self.__dict__
|
||||
def __setstate__(self, state):
|
||||
if self._has_get_set_state():
|
||||
self.setstate(state) # set state
|
||||
self._setstate(state) # set state
|
||||
#self._set_params(self._get_params()) # restore all values
|
||||
return
|
||||
self.__dict__ = state
|
||||
def _has_get_set_state(self):
|
||||
return 'getstate' in vars(self.__class__) and 'setstate' in vars(self.__class__)
|
||||
def getstate(self):
|
||||
return '_getstate' in vars(self.__class__) and '_setstate' in vars(self.__class__)
|
||||
def _getstate(self):
|
||||
"""
|
||||
Get the current state of the class,
|
||||
here just all the indices, rest can get recomputed
|
||||
For inheriting from Parameterized:
|
||||
|
||||
Allways append the state of the inherited object
|
||||
and call down to the inherited object in setstate!!
|
||||
and call down to the inherited object in _setstate!!
|
||||
"""
|
||||
return [
|
||||
self._fixes_,
|
||||
|
|
@ -280,7 +281,7 @@ class Parameterized(Constrainable, Pickleable, Observable):
|
|||
self._added_names_,
|
||||
]
|
||||
|
||||
def setstate(self, state):
|
||||
def _setstate(self, state):
|
||||
self._added_names_ = state.pop()
|
||||
self._name = state.pop()
|
||||
self._parameters_ = state.pop()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue