Exemple #1
0
    def __getstate__(self):
        # We just dump the version number for comparison with the actual class.
        # Note: we do not want to set the version number in __setstate__,
        # since we obtain it from the actual definition.
        try:
            if _debug:
                logger.debug('get state of %s' % self)
            state = {'class_tree_versions': {}}
            # currently it is used to handle class renames etc.
            versions = state['class_tree_versions']
            for c in self.__class__.mro():
                name = _importable_name(c)
                try:
                    v = SerializableMixIn._get_version(c)
                except AttributeError:
                    v = -1
                versions[name] = v

            # if we want to save the chain, do this now:
            if self._save_data_producer:
                assert hasattr(self, 'data_producer')
                state['data_producer'] = self.data_producer

            classes_to_inspect = self._get_classes_to_inspect()
            if _debug:
                logger.debug("classes to inspect during setstate: \n%s" %
                             classes_to_inspect)
            for klass in classes_to_inspect:
                self._get_state_of_serializeable_fields(klass, state)

            from pyemma._base.estimator import Estimator
            if isinstance(self, Estimator):
                state.update(Estimator.__my_getstate__(self))

            from pyemma._base.model import Model
            if isinstance(self, Model):
                state.update(Model.__my_getstate__(self))

            from deeptime.base import Model as DeeptimeModel
            if isinstance(self, DeeptimeModel):
                state.update(self.__dict__)

            from pyemma import version
            state['pyemma_version'] = version

            return state
        except:
            logger.exception('exception during pickling {}'.format(self))
            raise
Exemple #2
0
 def  _get_version_for_class_from_state(state, klass):
     """ retrieves the version of the current klass from the state mapping from old locations to new ones. """
     # klass may have renamed, so we have to look this up in the class rename registry.
     names = [_importable_name(klass)]
     # lookup old names, handled by current klass.
     from .util import class_rename_registry
     names.extend(class_rename_registry.old_handled_by(klass))
     for n in names:
         try:
             return state['class_tree_versions'][n]
         except KeyError:
             continue
     # if we did not find a suitable version number return infinity.
     if _debug:
         logger.debug('unable to obtain a _serialize_version for class %s', klass)
     return float('inf')