Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved implementation of shooting point selector pick (avoid model that might introduce index error in subclasses) #1110

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion openpathsampling/shooting.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def pick(self, trajectory):
rand = self._rng.random() * sum_bias
idx = 0
prob = prob_list[0]
while prob <= rand and idx < len(prob_list):
while prob <= rand and idx < len(prob_list) - 1:
idx += 1
prob += prob_list[idx]

Expand Down
17 changes: 17 additions & 0 deletions openpathsampling/tests/test_shooting.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ def test_probability(self):
assert sel.probability(frame, self.mytraj) == \
uniform.probability(frame, self.mytraj)

def test_pick(self):
sel = ShootingPointSelector()
test_traj_len = 22 # must be a multiple of 2 for this test
test_traj = [1 for _ in range(test_traj_len)]
# overwrite _biases to make sure we can only pick the last frame
sel._biases = lambda traj: [0 for _ in traj[:-1]] + [1]
assert sel.pick(test_traj) == test_traj_len - 1
# test pick first frame
sel._biases = lambda traj: [1] + [0 for _ in traj[1:]]
assert sel.pick(test_traj) == 0
# and test middle frame (only works if test_traj_len is a multiple of 2)
sel._biases = lambda traj: ([0 for _ in traj[:test_traj_len // 2]]
+ [1]
+ [0 for _ in traj[test_traj_len // 2 + 1:]]
)
assert sel.pick(test_traj) == test_traj_len // 2


class TestGaussianBiasSelector(SelectorTest):
def setup(self):
Expand Down