Test of CV functionality.

Most storage related tests are nosetests now. So look in testcollectivevariable.py please.


In [1]:
from __future__ import print_function
import openpathsampling as paths
import numpy as np
import openpathsampling.engines.openmm as peng

In [2]:
# force numpy print options for test comparison
np.set_printoptions(precision=6, formatter={'float_kind': lambda x: "{:.6f}".format(x)})

Load first frame from test pdb.


In [3]:
template = peng.snapshot_from_pdb('../resources/AD_initial_frame.pdb')

Create a simple CV without an underlying function.


In [4]:
cv0 = paths.CollectiveVariable('func0')

Create a complicated function.


In [5]:
center = 1
def dist(snapshot, center, np):
    return np.sum(snapshot.coordinates._value[0]) - center

Create collective variable from this function. Note that you have to specify center and np to make this work


In [6]:
cv1 = paths.FunctionCV('func1', dist, center=center, np=np, cv_time_reversible=False).with_diskcache()
cv2 = paths.FunctionCV('func2', dist, center=center, np=np, cv_wrap_numpy_array=True).with_diskcache()
cv3 = paths.FunctionCV('func3', dist, center=center, np=np, cv_wrap_numpy_array=True, cv_time_reversible=True).with_diskcache()

Create storage to test save and load.


In [7]:
#! lazy
storage = paths.Storage('can_be_deleted.nc', mode='w')
print(storage.snapshots.save(template))


283860163354172696480456782077640048652

Save CV


In [8]:
#! lazy
print(storage.save([cv0, cv1, cv2, cv3]))


[(store.attributes[PseudoAttribute] : 4 object(s), 20, 283860163354172696480456782077640048654L), (store.attributes[PseudoAttribute] : 4 object(s), 20, 283860163354172696480456782077640048656L), (store.attributes[PseudoAttribute] : 4 object(s), 20, 283860163354172696480456782077640048658L), (store.attributes[PseudoAttribute] : 4 object(s), 20, 283860163354172696480456782077640048660L)]

In [9]:
#! lazy
print(storage.cvs.index)


{283860163354172696480456782077640048656L: 1, 283860163354172696480456782077640048658L: 2, 283860163354172696480456782077640048660L: 3, 283860163354172696480456782077640048654L: 0}

Set the CV value for the storage.template


In [10]:
cv0[template] = 10.0

and create a storage for it


In [11]:
storage.cvs.add_diskcache(cv0, allow_incomplete=True)

And we should have a float store


In [12]:
storage.cvs.sync(cv0)

In [13]:
assert(cv0._store_dict.value_store.vars['value'][:] == [10.0])

Test function for reversed template.


In [14]:
dd = dist(template.reversed, center, np)
print("{:.8f}".format(dd))


3.39210033

Create another CV. This time using the from_template function.


In [15]:
print(cv0([template, template]))
print(cv1([template, template]))


[10.0, 10.0]
[3.3921003341674805, 3.3921003341674805]

In [16]:
print(type(cv0([template, template])))
print(type(cv0([template, template])[0]))
print(type(cv1([template, template])))
print(type(cv1([template, template])[0]))


<type 'list'>
<type 'float'>
<type 'list'>
<type 'numpy.float64'>

In [17]:
#! skip
print(storage.cvs.variables['json'][:])


[u'{"_cls":"CollectiveVariable","_dict":{"cv_time_reversible":false,"name":"func0"}}'
 u'{"_cls":"FunctionCV","_dict":{"name":"func1","f":{"_marshal":"YwMAAAADAAAAAwAAAEMAAQBzGwAAAHwCAGoAAHwAAGoBAGoCAGQBABmDAQB8AQAYUygCAAAATmkAAAAAKAMAAAB0AwAAAHN1bXQLAAAAY29vcmRpbmF0ZXN0BgAAAF92YWx1ZSgDAAAAdAgAAABzbmFwc2hvdHQGAAAAY2VudGVydAIAAABucCgAAAAAKAAAAABzHgAAADxpcHl0aG9uLWlucHV0LTUtNzlhNzEwNDZhYjU1PnQEAAAAZGlzdAIAAABzAgAAAAAB","_module_vars":[],"_global_vars":[]},"cv_time_reversible":false,"cv_wrap_numpy_array":false,"cv_requires_lists":false,"cv_scalarize_numpy_singletons":false,"kwargs":{"np":{"_import":"numpy"},"center":1}}}'
 u'{"_cls":"FunctionCV","_dict":{"name":"func2","f":{"_marshal":"YwMAAAADAAAAAwAAAEMAAQBzGwAAAHwCAGoAAHwAAGoBAGoCAGQBABmDAQB8AQAYUygCAAAATmkAAAAAKAMAAAB0AwAAAHN1bXQLAAAAY29vcmRpbmF0ZXN0BgAAAF92YWx1ZSgDAAAAdAgAAABzbmFwc2hvdHQGAAAAY2VudGVydAIAAABucCgAAAAAKAAAAABzHgAAADxpcHl0aG9uLWlucHV0LTUtNzlhNzEwNDZhYjU1PnQEAAAAZGlzdAIAAABzAgAAAAAB","_module_vars":[],"_global_vars":[]},"cv_time_reversible":false,"cv_wrap_numpy_array":true,"cv_requires_lists":false,"cv_scalarize_numpy_singletons":false,"kwargs":{"np":{"_import":"numpy"},"center":1}}}'
 u'{"_cls":"FunctionCV","_dict":{"name":"func3","f":{"_marshal":"YwMAAAADAAAAAwAAAEMAAQBzGwAAAHwCAGoAAHwAAGoBAGoCAGQBABmDAQB8AQAYUygCAAAATmkAAAAAKAMAAAB0AwAAAHN1bXQLAAAAY29vcmRpbmF0ZXN0BgAAAF92YWx1ZSgDAAAAdAgAAABzbmFwc2hvdHQGAAAAY2VudGVydAIAAABucCgAAAAAKAAAAABzHgAAADxpcHl0aG9uLWlucHV0LTUtNzlhNzEwNDZhYjU1PnQEAAAAZGlzdAIAAABzAgAAAAAB","_module_vars":[],"_global_vars":[]},"cv_time_reversible":true,"cv_wrap_numpy_array":true,"cv_requires_lists":false,"cv_scalarize_numpy_singletons":false,"kwargs":{"np":{"_import":"numpy"},"center":1}}}']

In [18]:
cv0j = storage.cvs.vars['json'][0]
cv1j = storage.cvs.vars['json'][1]
cv2j = storage.cvs.vars['json'][2]
cv3j = storage.cvs.vars['json'][3]

In [19]:
res = cv0j([template, template, template])
assert(res[0] == res[1] == res[2])
res = cv1j([template, template, template])
assert(res[0] == res[1] == res[2])
res = cv2j([template, template, template])
assert(res[0] == res[1] == res[2])
res = cv3j([template, template, template])
assert(res[0] == res[1] == res[2])

PseudoAttributes


In [20]:
t = paths.Trajectory([template] * 4)

In [21]:
#! lazy
print(storage.save(t))


(store.trajectories[Trajectory] : 1 object(s), 0, 283860163354172696480456782077640048728L)

In [22]:
def ff(t, cv3):
    return max(cv3(t))

In [23]:
a = paths.netcdfplus.FunctionPseudoAttribute('max_cv', paths.Trajectory, ff, cv3=cv3)

In [24]:
#! lazy
print(storage.attributes.save(a))


283860163354172696480456782077640048730

In [25]:
print("{:.8f}".format(a(t)))


3.39210033

In [26]:
p = paths.netcdfplus.LoaderProxy(storage.trajectories, t.__uuid__)

In [27]:
print("{:.8f}".format(a(p)))


3.39210033

In [28]:
print(storage.trajectories.add_attribute(paths.netcdfplus.ValueStore, a, t, allow_incomplete=False))


store.trajectories_max_cv[None/ANY] : (not created)

In [29]:
ats = storage.trajectories.attribute_list[a]

In [30]:
print(ats.vars['value'][:])


[3.392100]

In [31]:
print(a._store_dict.value_store.vars['value'][:])


[3.392100]

In [32]:
storage.trajectories.sync_attribute(a)

In [33]:
storage.attributes.has_cache(a)


Out[33]:
True

In [34]:
storage.attributes.sync(a)

In [35]:
storage.close()

In [36]:
storage = paths.Storage('can_be_deleted.nc', mode='r')

In [37]:
storage.attributes[4]


Out[37]:
<openpathsampling.netcdfplus.attribute.FunctionPseudoAttribute at 0x7f2957bf4d50>

In [38]:
storage.close()

In [ ]: