Skip to content

Commit

Permalink
Merge pull request #1110 from hejung/fix_shooting_point_selector_out_…
Browse files Browse the repository at this point in the history
…of_bounds

Improved implementation of shooting point selector pick (avoid model that might introduce index error in subclasses)
  • Loading branch information
dwhswenson committed Apr 5, 2022
2 parents 9131891 + 502f0a2 commit a414001
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
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

0 comments on commit a414001

Please sign in to comment.