diff options
-rw-r--r-- | gn3/computations/slink.py | 11 | ||||
-rw-r--r-- | tests/unit/computations/test_slink.py | 3 |
2 files changed, 12 insertions, 2 deletions
diff --git a/gn3/computations/slink.py b/gn3/computations/slink.py index 5288908..f3a6951 100644 --- a/gn3/computations/slink.py +++ b/gn3/computations/slink.py @@ -25,6 +25,16 @@ def raise_lengtherror_if_child_lists_are_not_same_as_parent(lists): if not all(map(len_is_same_as_parent, lists)): raise LengthError("All children lists should be same length as the parent.") +def raise_valueerror_if_child_list_distance_from_itself_is_not_zero(lists): + def get_child_distance(child): + idx = lists.index(child) + return lists[idx][idx] + def distance_is_zero(dist): + return dist == 0 + children_distances = map(get_child_distance, lists) + if not all(map(distance_is_zero, children_distances)): + raise ValueError("Distance of each child list/tuple from itself should be zero!") + 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. @@ -35,5 +45,6 @@ This description should be updated once the form/type of 'distance' identified." raise_valueerror_if_data_is_not_lists_or_tuples(lists) 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) #### END: Guard Functions #### return None diff --git a/tests/unit/computations/test_slink.py b/tests/unit/computations/test_slink.py index 816cd2f..1fb7bbb 100644 --- a/tests/unit/computations/test_slink.py +++ b/tests/unit/computations/test_slink.py @@ -35,8 +35,7 @@ class TestSlink(TestCase): with self.assertRaises(LengthError): nearest(lst, 1, 1) - def test_nearest_expects_exception_if_there_is_no_zero_at_childs_list_index_corresponding_to_its_index_in_parent(self): - # I don't like the name of this test. Make the name clearer + def test_nearest_expects_exception_if_distance_of_child_from_itself_is_not_zero(self): for lst in [[[1]], [[1,2],[3,4]], [1,0,0],[0,0,5],[0,3,4], |