Skip to content

Commit 8a8e7b2

Browse files
authored
nojump.py raises a valueerror when applied not at first frame (#5201)
* nojump.py raises a valueerror when applied not at 0 * Updated changelog * edited the valueError * updated test_nojump_fails_midtrajectory * updated test to a single function
1 parent 428da04 commit 8a8e7b2

3 files changed

Lines changed: 24 additions & 0 deletions

File tree

package/CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ The rules for this file:
2020
* 2.11.0
2121

2222
Fixes
23+
* NoJump shows a more informative message and fails when applied
24+
outside of the first frame (Issue #4915, PR #5201)
2325
* DSSP now explicitly checks for a minimum of 6 residues and raises a clear
2426
error message, unlike the previous behavior where it would fail with an
2527
incomprehensible broadcasting error at execution time (Issue #5046, PR #5163)

package/MDAnalysis/transformations/nojump.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ def _transform(self, ts):
118118
except np.linalg.LinAlgError:
119119
msg = f"Periodic box dimensions are not invertible at step {ts.frame}"
120120
raise NoDataError(msg)
121+
122+
if self.prev is None and ts.frame != 0:
123+
raise ValueError(
124+
"NoJump transformation must be applied starting from frame 0. "
125+
f"Currently at frame {ts.frame}. Please reset trajectory to frame 0 before adding this transformation."
126+
)
127+
121128
if ts.frame == 0:
122129
# We don't need to apply the transformation here. However, we need to
123130
# ensure we have the 0th frame coordinates in reduced form. We also need to

testsuite/MDAnalysisTests/transformations/test_nojump.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,3 +379,18 @@ def test_notinvertible(nojump_universe):
379379
]
380380
u.trajectory.add_transformations(*workflow)
381381
transformed_coordinates = u.trajectory.timeseries()[0]
382+
383+
384+
@pytest.mark.parametrize("frame_index", [-1, 5])
385+
def test_nojump_fails_when_not_at_frame_0(frame_index):
386+
"""
387+
Test that NoJump raises a clear error when applied to a trajectory
388+
that is not at frame 0.
389+
"""
390+
u = mda.Universe(data.PSF_TRICLINIC, data.DCD_TRICLINIC)
391+
u.trajectory[frame_index]
392+
393+
with pytest.raises(
394+
ValueError, match="must be applied starting from frame 0"
395+
):
396+
u.trajectory.add_transformations(NoJump())

0 commit comments

Comments
 (0)