From 3943ddc4480ae6d9bb9fc15f0042832e6bef93f1 Mon Sep 17 00:00:00 2001 From: Vedant Rathore Date: Wed, 1 Feb 2017 02:33:43 +0530 Subject: [PATCH] Added exception and unittest for dist_mat_to_vec of analysis/psa.py (Part of #597) --- package/MDAnalysis/analysis/psa.py | 36 +++++-------------- .../MDAnalysisTests/analysis/test_psa.py | 17 ++++++++- 2 files changed, 24 insertions(+), 29 deletions(-) diff --git a/package/MDAnalysis/analysis/psa.py b/package/MDAnalysis/analysis/psa.py index 6db5120b4b3..baf6756b701 100644 --- a/package/MDAnalysis/analysis/psa.py +++ b/package/MDAnalysis/analysis/psa.py @@ -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) @@ -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 diff --git a/testsuite/MDAnalysisTests/analysis/test_psa.py b/testsuite/MDAnalysisTests/analysis/test_psa.py index 64443a797ba..9c6e03d3a2f 100644 --- a/testsuite/MDAnalysisTests/analysis/test_psa.py +++ b/testsuite/MDAnalysisTests/analysis/test_psa.py @@ -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"""