diff options
-rw-r--r-- | gn3/computations/slink.py | 12 | ||||
-rw-r--r-- | tests/unit/computations/test_slink.py | 14 |
2 files changed, 19 insertions, 7 deletions
diff --git a/gn3/computations/slink.py b/gn3/computations/slink.py index f3a6951..928330d 100644 --- a/gn3/computations/slink.py +++ b/gn3/computations/slink.py @@ -35,6 +35,17 @@ def raise_valueerror_if_child_list_distance_from_itself_is_not_zero(lists): if not all(map(distance_is_zero, children_distances)): raise ValueError("Distance of each child list/tuple from itself should be zero!") +def raise_mirrorerror_of_distances_one_way_are_not_same_other_way(lists): + """Check that the distance from A to B, is the same as the distance from B to A. +If the two distances are different, throw an exception.""" + for i in range(len(lists)): + for j in range(len(lists)): + if lists[i][j] != lists[j][i]: + raise MirrorError( + ("Distance from one child({}) to the other ({}) " + "should be the same in both directions.").format( + lists[i][j], lists[j][i])) + 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. @@ -46,5 +57,6 @@ This description should be updated once the form/type of 'distance' identified." raise_valueerror_if_lists_empty(lists) 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) #### END: Guard Functions #### return None diff --git a/tests/unit/computations/test_slink.py b/tests/unit/computations/test_slink.py index 1fb7bbb..207debf 100644 --- a/tests/unit/computations/test_slink.py +++ b/tests/unit/computations/test_slink.py @@ -44,7 +44,7 @@ class TestSlink(TestCase): with self.assertRaises(ValueError): nearest(lst, 1, 1) - def test_nearest_expects_exception_if_value_at_index_j_in_list_i_does_not_equals_value_at_index_i_in_list_j(self): + def test_nearest_expects_exception_if_distance_from_child_a_to_child_b_is_not_distance_from_child_b_to_child_a(self): for lst in [[[0,1],[2,0]], [[0,1,2],[1,0,3],[9,7,0]], [[0,1,2,3],[7,0,2,3],[2,3,0,1],[8,9,5,0]]]: @@ -56,12 +56,12 @@ class TestSlink(TestCase): # 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]], - [[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]], + [[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!"): nearest(lst, 1, 1) |