From 6277bdf88819f8c4b96efc0df049de67a46496e9 Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Prinz Date: Fri, 22 Jun 2018 00:24:26 +0200 Subject: [PATCH] allow conversion of LoaderProxies into real objects using `.proxy()` --- openpathsampling/engines/trajectory.py | 6 +++ openpathsampling/netcdfplus/__init__.py | 2 +- openpathsampling/netcdfplus/base.py | 7 ++++ openpathsampling/netcdfplus/dictify.py | 42 ------------------- openpathsampling/netcdfplus/proxy.py | 6 +-- .../tests/test_collectivevariable.py | 4 -- 6 files changed, 17 insertions(+), 50 deletions(-) diff --git a/openpathsampling/engines/trajectory.py b/openpathsampling/engines/trajectory.py index 4e0d3c732..a06dd7ee1 100644 --- a/openpathsampling/engines/trajectory.py +++ b/openpathsampling/engines/trajectory.py @@ -611,3 +611,9 @@ def _to_list_of_trajectories(trajectories): return paths.Trajectory([trajectories]) return trajectories + + def unproxy(self): + for idx, snapshot in enumerate(self.iter_proxies()): + if issubclass(type(snapshot), LoaderProxy): + list.__setitem__(self, idx, snapshot.__subject__) + snapshot.__subject__.unproxy(); \ No newline at end of file diff --git a/openpathsampling/netcdfplus/__init__.py b/openpathsampling/netcdfplus/__init__.py index 44bcac4f5..e354eea68 100644 --- a/openpathsampling/netcdfplus/__init__.py +++ b/openpathsampling/netcdfplus/__init__.py @@ -1,7 +1,7 @@ from .base import StorableNamedObject, StorableObject, create_to_dict from .cache import WeakKeyCache, WeakLRUCache, WeakValueCache, MaxCache, \ NoCache, Cache, LRUCache, LRUChunkLoadingCache -from .dictify import ObjectJSON, StorableObjectJSON, UUIDObjectJSON +from .dictify import ObjectJSON, UUIDObjectJSON from .netcdfplus import NetCDFPlus from .stores import ObjectStore diff --git a/openpathsampling/netcdfplus/base.py b/openpathsampling/netcdfplus/base.py index e9403f636..10e8eb5cf 100644 --- a/openpathsampling/netcdfplus/base.py +++ b/openpathsampling/netcdfplus/base.py @@ -345,6 +345,13 @@ def from_dict(cls, dct): else: return cls(**dct) + def unproxy(self): + if hasattr(self, '_lazy') : + for attr, value in self._lazy.items() : + if hasattr(value, '__subject__') : + self._lazy[attr] = value.__subject__ + value.__subject__.unproxy() + class StorableNamedObject(StorableObject): """Mixin that allows an object to carry a .name property that can be saved diff --git a/openpathsampling/netcdfplus/dictify.py b/openpathsampling/netcdfplus/dictify.py index 388c0e1a8..df9d19b55 100644 --- a/openpathsampling/netcdfplus/dictify.py +++ b/openpathsampling/netcdfplus/dictify.py @@ -589,48 +589,6 @@ def unit_from_json(self, json_string): return self.unit_from_dict(self.from_json(json_string)) -class StorableObjectJSON(ObjectJSON): - def __init__(self, storage, unit_system=None): - super(StorableObjectJSON, self).__init__(unit_system) - self.excluded_keys = ['idx', 'json', 'identifier'] - self.storage = storage - - def simplify(self, obj, base_type=''): - if obj is self.storage: - return {'_storage': 'self'} - if obj.__class__.__module__ != builtin_module: - if obj.__class__ in self.storage._obj_store: - store = self.storage._obj_store[obj.__class__] - if not store.nestable or obj.base_cls_name != base_type: - # this also returns the base class name used for storage - # store objects only if they are not creatable. If so they - # will only be created in their top instance and we use - # the simplify from the super class ObjectJSON - idx = store.save(obj) - if idx is None: - raise RuntimeError( - 'cannot store idx None in store %s' % store) - return { - '_idx': idx, - '_store': store.prefix} - - return super(StorableObjectJSON, self).simplify(obj, base_type) - - def build(self, obj): - if type(obj) is dict: - if '_storage' in obj: - if obj['_storage'] == 'self': - return self.storage - - if '_idx' in obj and '_store' in obj: - store = self.storage._stores[obj['_store']] - result = store.load(obj['_idx']) - - return result - - return super(StorableObjectJSON, self).build(obj) - - class UUIDObjectJSON(ObjectJSON): def __init__(self, storage, unit_system=None): super(UUIDObjectJSON, self).__init__(unit_system) diff --git a/openpathsampling/netcdfplus/proxy.py b/openpathsampling/netcdfplus/proxy.py index 25227670a..777620d00 100644 --- a/openpathsampling/netcdfplus/proxy.py +++ b/openpathsampling/netcdfplus/proxy.py @@ -98,11 +98,11 @@ def _load_(self): if type(self.__uuid__) is int: raise RuntimeWarning( 'Index %s is not in store. This should never happen!' % - self._idx) + self.__uuid__) else: raise RuntimeWarning( 'Object %s is not in store. Attach it using fallbacks.' % - self._idx) + self.__uuid__) class DelayedLoader(object): @@ -114,7 +114,7 @@ class DelayedLoader(object): def __get__(self, instance, owner): if instance is not None: obj = instance._lazy[self] - if hasattr(obj, '_idx'): + if isinstance(obj, LoaderProxy): return obj.__subject__ else: return obj diff --git a/openpathsampling/tests/test_collectivevariable.py b/openpathsampling/tests/test_collectivevariable.py index 08cf1eaf0..0ed5637c8 100644 --- a/openpathsampling/tests/test_collectivevariable.py +++ b/openpathsampling/tests/test_collectivevariable.py @@ -174,10 +174,6 @@ def test_storage_cv_function(self): assert (cv_cache.allow_incomplete == allow_incomplete) for idx, snap in enumerate(storage_r.trajectories[1]): - # print idx, snap - # if hasattr(snap, '_idx'): - # print 'Proxy IDX', snap._idx - # print 'ITEMS', storage_r.snapshots.index.items() # print snap, type(snap), snap.__dict__