In [1]:
from __future__ import print_function
import numpy as np
import openpathsampling as paths
import openpathsampling.engines.features as features
Function to show the generated source code
In [2]:
from IPython.display import Markdown
def code_to_md(snapshot_class):
md = '```py\n'
for f, s in snapshot_class.__features__.debug.items():
if s is not None:
md += s
else:
md += 'def ' + f + '(...):\n # user defined\n pass'
md += '\n\n'
md += '```'
return md
Generate simple Snapshot without any features using factory
In [3]:
EmptySnap = paths.engines.snapshot.SnapshotFactory('no', [], 'Empty', use_lazy_reversed=False)
Generate Snapshot with overridden .copy method.
In [4]:
@features.base.attach_features([
features.velocities,
features.coordinates,
features.box_vectors,
features.topology
])
class A(paths.BaseSnapshot):
def copy(self):
return 'copy'
Check that subclassing with overridden copy needs more overriding.
In [5]:
#! lazy
# lazy because of some issue with Py3k comparing strings
try:
@features.base.attach_features([
])
class B(A):
pass
except RuntimeWarning as e:
print(e)
else:
raise RuntimeError('Should have raised a RUNTIME warning')
In [6]:
a = A()
assert(a.copy() == 'copy')
In [7]:
#! ignore
Markdown(code_to_md(A))
Out[7]:
In [8]:
#! ignore
Markdown(code_to_md(EmptySnap))
Out[8]:
In [9]:
SuperSnap = paths.engines.snapshot.SnapshotFactory(
'my', [
paths.engines.features.coordinates,
paths.engines.features.box_vectors,
paths.engines.features.velocities
], 'No desc', use_lazy_reversed=False)
In [10]:
#! ignore
Markdown(code_to_md(SuperSnap))
Out[10]:
In [11]:
MegaSnap = paths.engines.snapshot.SnapshotFactory(
'mega', [
paths.engines.features.statics,
paths.engines.features.kinetics,
paths.engines.features.engine
], 'Long desc', use_lazy_reversed=False)
In [12]:
#! ignore
Markdown(code_to_md(MegaSnap))
Out[12]:
Test subclassing
In [13]:
@features.base.attach_features([
])
class HyperSnap(MegaSnap):
pass
Test subclassing with redundant features (should work / be ignored)
In [14]:
@features.base.attach_features([
paths.engines.features.statics,
])
class HyperSnap(MegaSnap):
pass
Test subclassing with conflicting features (should not work)
In [15]:
try:
@features.base.attach_features([
paths.engines.features.statics,
paths.engines.features.coordinates
])
class HyperSnap(MegaSnap):
pass
except RuntimeWarning as e:
print(e)
else:
raise RuntimeError('Should have raised a RUNTIME warning')
In [16]:
#! ignore
Markdown(code_to_md(paths.engines.openmm.MDSnapshot))
Out[16]:
In [ ]: