aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gn3/computations/slink.py11
-rw-r--r--tests/unit/computations/test_slink.py5
2 files changed, 13 insertions, 3 deletions
diff --git a/gn3/computations/slink.py b/gn3/computations/slink.py
index 928330d..7d13d91 100644
--- a/gn3/computations/slink.py
+++ b/gn3/computations/slink.py
@@ -46,6 +46,16 @@ If the two distances are different, throw an exception."""
"should be the same in both directions.").format(
lists[i][j], lists[j][i]))
+def raise_valueerror_on_negative_distances(lists):
+ """Check that distances between 'somethings' are all positive, otherwise,
+raise an exception."""
+ def zero_or_positive(val):
+ return val >= 0;
+ # flatten lists
+ flattened = [distance for child in lists for distance in child]
+ if not all(map(zero_or_positive, flattened)):
+ raise ValueError("Distances should be positive.")
+
def nearest(lists, i, j):
"""Computes some form of distance.
This is 'copied' over from genenetwork1, from https://github.com/genenetwork/genenetwork1/blob/master/web/webqtl/heatmap/slink.py#L42-L64.
@@ -58,5 +68,6 @@ This description should be updated once the form/type of 'distance' identified."
raise_lengtherror_if_child_lists_are_not_same_as_parent(lists)
raise_valueerror_if_child_list_distance_from_itself_is_not_zero(lists)
raise_mirrorerror_of_distances_one_way_are_not_same_other_way(lists)
+ raise_valueerror_on_negative_distances(lists)
#### END: Guard Functions ####
return None
diff --git a/tests/unit/computations/test_slink.py b/tests/unit/computations/test_slink.py
index 207debf..6be3f33 100644
--- a/tests/unit/computations/test_slink.py
+++ b/tests/unit/computations/test_slink.py
@@ -55,15 +55,14 @@ class TestSlink(TestCase):
def test_nearest_expects_zero_or_positive_distances(self):
# Based on:
# https://github.com/genenetwork/genenetwork1/blob/master/web/webqtl/heatmap/slink.py#L87-L89
- for lst in [[[0,1,2,3],[1,0,3,4],[2,3,0,5],[3,4,5,0]],
- [[0,-1,2,3],[-1,0,3,4],[2,3,0,5],[3,4,5,0]],
+ for lst in [[[0,-1,2,3],[-1,0,3,4],[2,3,0,5],[3,4,5,0]],
[[0,1,-2,3],[1,0,3,4],[-2,3,0,5],[3,4,5,0]],
[[0,1,2,3],[1,0,-3,4],[2,-3,0,5],[3,4,5,0]],
[[0,1,2,-3],[1,0,3,4],[2,3,0,5],[-3,4,5,0]],
[[0,1,2,3],[1,0,3,-4],[2,3,0,5],[3,-4,5,0]],
[[0,1,2,3],[1,0,3,4],[2,3,0,-5],[3,4,-5,0]]]:
with self.subTest(lst=lst):
- with self.assertRaises(ValueError, msg="Got an unexpected negative value!"):
+ with self.assertRaises(ValueError, msg="Distances should be positive."):
nearest(lst, 1, 1)
def test_nearest_with_expected(self):