Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rewrite cache reset to allow using same traj 2x
Also skeletons for test of seq ens cache
  • Loading branch information
dwhswenson committed Apr 10, 2015
commit 56a1ffadd41a0c34073a98dd5f1278c80b95c7ce
45 changes: 29 additions & 16 deletions openpathsampling/ensemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,39 @@ def __init__(self, direction=None):
self.direction = direction
self.contents = { }

def bad_direction_error(self):
raise RuntimeError("EnsembleCache.direction = " +
str(self.direction) + " invalid.") #nocover

def check(self, trajectory=None, reset=None):
"""Checks and resets (if necessary) the ensemble cache.
"""
if trajectory is not None:
lentraj = len(trajectory)
# if the first frame has changed, we should reset
if reset is None:
reset = (
self.direction > 0 and (
trajectory[0] != self.start_frame or
lentraj - 1 != self.last_length or
(lentraj > 1 and trajectory[-2] != self.prev_last_frame)
)
) or (
self.direction < 0 and (
trajectory[-1] != self.start_frame or
lentraj - 1 != self.last_length or
(lentraj > 1 and trajectory[1] != self.prev_last_frame)
)
)
lentraj = len(trajectory)
if self.direction > 0:
if trajectory[0] != self.start_frame:
reset = True
else:
if lentraj == self.last_length:
reset = (trajectory[-1] != self.prev_last_frame)
elif lentraj == self.last_length + 1:
reset = (trajectory[-2] != self.prev_last_frame)
else:
reset = True
elif self.direction < 0:
if trajectory[-1] != self.start_frame:
reset = True
else:
if lentraj == self.last_length:
reset = (trajectory[0] != self.prev_last_frame)
elif lentraj == self.last_length + 1:
reset = (trajectory[1] != self.prev_last_frame)
else:
reset = True
else:
self.bad_direction_error()
else:
reset = True

Expand All @@ -74,8 +88,7 @@ def check(self, trajectory=None, reset=None):
self.last_length = len(trajectory)
self.contents = { }
else:
raise RuntimeWarning("EnsembleCache.direction = " +
str(self.direction) + " invalid.")
self.bad_direction_error()
# by returning reset, we allow the functions that call this to reset
# other things as well
self.last_length = len(trajectory)
Expand Down
39 changes: 37 additions & 2 deletions openpathsampling/tests/testensemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,17 @@ def test_trajectory_by_frame(self):
self.rev.check(self.traj[-2:])
assert_equal(self._was_cache_reset(self.rev), False)

def test_same_traj_twice_no_reset(self):
# tests for forward
self.fwd.check(self.traj)
assert_equal(self._was_cache_reset(self.fwd), True)
self.fwd.contents = { 'test' : 'object' }
self.fwd.check(self.traj)
#assert_equal(self._was_cache_reset(self.fwd), False)
# tests for backward
raise SkipTest


def test_trajectory_skips_frame(self):
# tests for forward
self.fwd.check(self.traj[0:1])
Expand Down Expand Up @@ -903,9 +914,8 @@ def test_trajectory_middle_frame_changes(self):
assert_equal(self._was_cache_reset(self.rev), True)


class testSequentialEnsembleCache(testEnsembleCache):
class testSequentialEnsembleCache(EnsembleCacheTest):
def setUp(self):
super(testSequentialEnsembleCache, self).setUp()
self.inX = AllInXEnsemble(vol1)
self.outX = AllOutXEnsemble(vol1)
self.length1 = LengthEnsemble(1)
Expand All @@ -920,6 +930,31 @@ def setUp(self):


def test_sequential_caching_can_append(self):
#cache = self.pseudo_minus._cache_can_append
assert_equal(self.pseudo_minus.can_append(self.traj[0:1]), True)
#assert_equal(self._was_cache_reset(cache), True)
for i in range(1, len(self.traj)-1):
assert_equal(self.pseudo_minus.can_append(self.traj[0:i+1]), True)
#assert_equal(self._was_cache_reset(cache), False)
assert_equal(self.pseudo_minus.can_append(self.traj), False)
#assert_equal(self._was_cache_reset(cache), False)

def test_sequential_caching_resets(self):
#cache = self.pseudo_minus._cache_can_append
assert_equal(self.pseudo_minus.can_append(self.traj[2:3]), True)
assert_equal(self.pseudo_minus(self.traj[2:3]), False)
#assert_equal(self._was_cache_reset(cache), True)
assert_equal(self.pseudo_minus.can_append(self.traj[2:4]), True)
assert_equal(self.pseudo_minus(self.traj[2:4]), False)
#assert_equal(self._was_cache_reset(cache), True)
for i in range(4, len(self.traj)-1):
assert_equal(self.pseudo_minus.can_append(self.traj[2:i+1]), True)
assert_equal(self.pseudo_minus(self.traj[2:i+1]), False)
#assert_equal(self._was_cache_reset(cache), False)
assert_equal(self.pseudo_minus.can_append(self.traj[2:]), False)
assert_equal(self.pseudo_minus(self.traj[2:]), False)
#assert_equal(self._was_cache_reset(cache), False)
# TODO: same story backward
raise SkipTest

def test_sequential_caching_call(self):
Expand Down