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

Added exception and unittest for dist_mat_to_vec of analysis/psa.py (Part of #597) #1183

Merged
merged 15 commits into from
Feb 4, 2017
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
9a49244
Added exception and unittest for function dist_mat_to_vec of analysis…
vedantrathore Jan 22, 2017
ceb1618
Merge branch 'develop' of https://github.com/MDAnalysis/mdanalysis in…
vedantrathore Jan 22, 2017
1b04205
Merge branch 'develop' of https://github.com/MDAnalysis/mdanalysis in…
vedantrathore Jan 23, 2017
3af71be
Added exception and unittest for dist_mat_to_vec of analysis/psa.py (…
vedantrathore Jan 23, 2017
bf8765b
Added exception and unittest for dist_mat_to_vec of analysis/psa.py (…
vedantrathore Jan 23, 2017
cda6c57
Added exception and unittest for dist_mat_to_vec of analysis/psa.py (…
vedantrathore Jan 23, 2017
dcc6168
Adds validation for dist_mat_to_vec function inputs
vedantrathore Jan 28, 2017
cecc350
Merge branch 'develop' of https://github.com/MDAnalysis/mdanalysis in…
vedantrathore Jan 28, 2017
3943ddc
Added exception and unittest for dist_mat_to_vec of analysis/psa.py (…
vedantrathore Jan 31, 2017
b706493
Merge branch 'develop' of https://github.com/MDAnalysis/mdanalysis in…
vedantrathore Jan 31, 2017
f12458e
Added exception and unittest for dist_mat_to_vec of analysis/psa.py (…
vedantrathore Jan 31, 2017
ece534a
Added exception and unittest for dist_mat_to_vec of analysis/psa.py (…
vedantrathore Feb 2, 2017
36acf58
Added exception and unittest for dist_mat_to_vec of analysis/psa.py (…
vedantrathore Feb 3, 2017
376b0b1
Added exception and unittest for dist_mat_to_vec of analysis/psa.py (…
vedantrathore Feb 3, 2017
bd94007
Added exception and unittest for dist_mat_to_vec of analysis/psa.py (…
vedantrathore Feb 3, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package/AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Chronological list of authors
- Shantanu Srivastava
2017
- Utkarsh Bansal
- Vedant Rathore

External code
-------------
Expand Down
3 changes: 2 additions & 1 deletion package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ The rules for this file:
------------------------------------------------------------------------------
??/??/16 kain88-de, fiona-naughton, richardjgowers, tyler.je.reddy, jdetle
euhruska, orbeckst, rbrtdlg, jbarnoud, wouterboomsma, shanmbic,
dotsdl, manuel.nuno.melo, utkbansal
dotsdl, manuel.nuno.melo, utkbansal, vedantrathore

* 0.16.0

Expand Down Expand Up @@ -61,6 +61,7 @@ Enhancements
* Added groupby method to Group objects. (PR #1112)

Fixes
* Argument validation of dist_mat_to_vec is fixed (#597 PR #1183)
* Give correct error when the topology file format is not recognized (Issue #982)
* Give correct error when file doesn't exist/ has bad permissions (Issue #981)
* Improvement of analysis/waterdynamics module (Issue #935)
Expand Down
13 changes: 11 additions & 2 deletions package/MDAnalysis/analysis/psa.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@
from six.moves import range, cPickle

import numpy as np
import warnings

import MDAnalysis
import MDAnalysis.analysis.align
Expand Down Expand Up @@ -573,7 +574,15 @@ def dist_mat_to_vec(N, i, j):
:Returns:
int, index (of the matrix element) in the corresponding distance vector
"""
if i > N or j > N:

if not (isinstance(N, int) or isinstance(i, int) or isinstance(j, int)):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this still work with numpy integers? So you pass np.int32(3) or np.int64(3) to the function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kain88-de Okay, thanks! I'll look into it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kain88-de should I add diffrent clauses fr np.int32() and np.int64() and normal int?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One solution is to use isinstance(N, numbers.Integral).

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 (j > i and (i > N - 1 or j > N)) or (j < i and (i > N or j > N - 1)):
err_str = "Matrix indices are out of range; i and j must be less than" \
+ " N = {0:d}".format(N)
raise ValueError(err_str)
Expand All @@ -583,7 +592,7 @@ def dist_mat_to_vec(N, i, j):
warn_str = "Column index entered (j = {:d} is smaller than row index" \
+ " (i = {:d}). Using symmetric element in upper triangle of" \
+ " distance matrix instead: i --> j, j --> i"
print(warn_str)
warnings.warn(warn_str.format(j, i))
return (N*j) + i - (j+2)*(j+1)/2
else:
err_str = "Error in processing matrix indices; i and j must be integers"\
Expand Down
2 changes: 1 addition & 1 deletion testsuite/AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Chronological list of authors

2017
- Utkarsh Bansal

- Vedant Rathore

External code
-------------
Expand Down
2 changes: 1 addition & 1 deletion testsuite/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ and https://github.com/MDAnalysis/mdanalysis/wiki/UnitTests

------------------------------------------------------------------------------
??/??/16 jbarnoud, orbeckst, fiona-naughton, manuel.nuno.melo, richardjgowers
tyler.je.reddy, utkbansal
tyler.je.reddy, utkbansal, vedantrathore

* 0.16
- added two unit tests for MDAnalysis.analysis.polymer
Expand Down
62 changes: 59 additions & 3 deletions testsuite/MDAnalysisTests/analysis/test_psa.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

from numpy.testing import (TestCase, dec, assert_array_less,
assert_array_almost_equal, assert_,
assert_almost_equal)
assert_almost_equal, assert_equal)
import numpy as np

from MDAnalysisTests.datafiles import PSF, DCD, DCD2
Expand Down Expand Up @@ -93,6 +93,15 @@ def test_dendrogram_produced(self):
err_msg = "Dendrogram dictionary object was not produced"
assert_(type(self.plot_data[1]) is dict, err_msg)

def test_dist_mat_to_vec_i_less_j(self):
"""Test the index of corresponding distance vector is correct if i < j"""
err_msg = "dist_mat_to_vec function returning wrong values"
assert_equal(PSA.dist_mat_to_vec(5, 3, 4), 9, err_msg)

def test_dist_mat_to_vec_i_greater_j(self):
"""Test the index of corresponding distance vector is correct if i > j"""
err_msg = "dist_mat_to_vec function returning wrong values"
assert_equal(PSA.dist_mat_to_vec(5, 4, 3), 9, err_msg)

class TestPSAExceptions(TestCase):
'''Tests for exceptions that should be raised
Expand All @@ -108,13 +117,60 @@ def test_get_path_metric_func_bad_key(self):
self.fail('KeyError should be caught')

def test_get_coord_axes_bad_dims(self):
'''Test that ValueError is raised when
"""Test that ValueError is raised when
numpy array with incorrect dimensions
is fed to get_coord_axes().'''
is fed to get_coord_axes()."""

with self.assertRaises(ValueError):
PSA.get_coord_axes(np.zeros((5,5,5,5)))

def test_dist_mat_to_vec_func_out_of_bounds(self):
"""Test that ValueError is raised when i or j or both are
out of bounds of N"""

# Check if i is out of bounds of N
with self.assertRaises(ValueError):
PSA.dist_mat_to_vec(5, 6, 4)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have each of these line in a different with block, or in a different test. The way it is now makes it difficult to now which one failed if there is a failure.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment to tell what index is out of bonds. It is obvious when you are in the context of writing the test, but it makes you loose quite a lot of time when you have to figure it out latter on.


# Check if j is out of bounds of N
with self.assertRaises(ValueError):
PSA.dist_mat_to_vec(5, 4, 6)

# Check if both i and j are out of bounds of N
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 j 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 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"""

with self.assertRaises(ValueError):
PSA.dist_mat_to_vec(5, '6', '7')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if i, or j is a float?


with self.assertRaises(ValueError):
PSA.dist_mat_to_vec(5, float(6), 7)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above: separate the assertions.


class _BaseHausdorffDistance(TestCase):
'''Base Class setup and unit tests
for various Hausdorff distance
Expand Down