-
Notifications
You must be signed in to change notification settings - Fork 662
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
Changes from 13 commits
9a49244
ceb1618
1b04205
3af71be
bf8765b
cda6c57
dcc6168
cecc350
3943ddc
b706493
f12458e
ece534a
36acf58
376b0b1
bd94007
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,7 +73,7 @@ Chronological list of authors | |
|
||
2017 | ||
- Utkarsh Bansal | ||
|
||
- Vedant Rathore | ||
|
||
External code | ||
------------- | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have each of these line in a different There was a problem hiding this comment. Choose a reason for hiding this commentThe 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') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if |
||
|
||
with self.assertRaises(ValueError): | ||
PSA.dist_mat_to_vec(5, float(6), 7) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should add tests as well for the numpy integers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh forgot that, my bad.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kain88-de I was thinking about how to check whether input is numpy integer or not, I think we are only checking whether the input is not an integer, How should I validate whether the input is valid integer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current check is testing if the input is an integer. In you test you should just make sure that it's ok to use numpy integers like np.int32 in the function. That is important if one wants to use integers from numpy arrays.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What @kain88-de means is that you should add a few lines in
test_psa.py
with numpy integer as inputs. The function should work of we giveN=numpy.int32(4)
orN=numpy.int16(4)
. and this has to be tested. It is likely that the input values fordist_mat_to_vec
will come from a numpy array at some point, then their type will not be a plainint
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kain88-de @jbarnoud I think this is what you meant, right?