Skip to content

Commit

Permalink
Added exception and unittest for dist_mat_to_vec of analysis/psa.py (…
Browse files Browse the repository at this point in the history
…Part of MDAnalysis#597)
  • Loading branch information
vedantrathore committed Jan 31, 2017
1 parent cecc350 commit 3943ddc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 29 deletions.
36 changes: 8 additions & 28 deletions package/MDAnalysis/analysis/psa.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,10 +573,14 @@ def dist_mat_to_vec(N, i, j):
:Returns:
int, index (of the matrix element) in the corresponding distance vector
"""
try:
validate_dist_mat_to_vec_inputs(N, i, j)
except Exception as E:
raise E

if not (isinstance(N, int) or isinstance(i, int) or isinstance(j, int)):
raise ValueError("N, i, j all must be of type int")

if i < 0 or j < 0 or N < 2:
error_str = "Matrix indices are invalid; i and j must be greater than 0 and N must be greater the 2"
raise ValueError(error_str)

if i > N or j > N:
err_str = "Matrix indices are out of range; i and j must be less than" \
+ " N = {0:d}".format(N)
Expand All @@ -595,30 +599,6 @@ def dist_mat_to_vec(N, i, j):
raise ValueError(err_str)


def validate_dist_mat_to_vec_inputs(N, i, j):
"""
Validate the inputs of the function dist_mat_to_vec else raise an exception
:param N: int
:param i: int
:param j: int
:return: null
"""

# Validating if the inputs are integer
try:
NValue = int(N)
iValue = int(i)
jValue = int(j)
except ValueError:
raise ValueError

# Check if the inputs are not out of bounds
if N-1 > i > 0 and N >= 2 and i < j < N:
pass
else:
raise ValueError


class Path(object):
"""Pre-process a :class:`MDAnalysis.Universe` object: (1) fit the
trajectory to a reference structure, (2) convert fitted time series to a
Expand Down
17 changes: 16 additions & 1 deletion testsuite/MDAnalysisTests/analysis/test_psa.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,27 @@ def test_dist_mat_to_vec_func_out_of_bounds(self):
with self.assertRaises(ValueError):
PSA.dist_mat_to_vec(5, 6, 7)

# Check if i is negative
with self.assertRaises(ValueError):
PSA.dist_mat_to_vec(5, -1, 2)

# Check if i is negative
with self.assertRaises(ValueError):
PSA.dist_mat_to_vec(5, 1, -2)

# Check if N is less than 2
with self.assertRaises(ValueError):
PSA.dist_mat_to_vec(1, 0, 0)

def test_dist_mat_to_vec_func_i_equals_j(self):
"""Test that ValueError is raised when i == j"""
"""Test that ValueError is raised when i == j or i,j == N"""

with self.assertRaises(ValueError):
PSA.dist_mat_to_vec(5, 4, 4)

with self.assertRaises(ValueError):
PSA.dist_mat_to_vec(4, 6, 4)

def test_dist_mat_to_vec_func_bad_integers(self):
"""Test that ValueError is raised when i or j are
not Integers"""
Expand Down

0 comments on commit 3943ddc

Please sign in to comment.