From 703fad03660c8f4a5658bd4aecd3a18fad6ce10c Mon Sep 17 00:00:00 2001 From: Muriithi Frederick Muriuki Date: Thu, 22 Jul 2021 12:59:46 +0300 Subject: Check that distance from A to B is same as from B to A Issue: https://github.com/genenetwork/gn-gemtext-threads/blob/main/topics/gn1-migration-to-gn2/clustering.gmi * gn3/computations/slink.py: check that the distance from child A to B is the same as distance from child B to A. If not, throw an exception. * tests/unit/computations/test_slink.py: * Change the name of the test to more closely correspond to the business logic being tested. * Update the data in a separate test such that it does not error out due to failing to fulfill the expectations of separate requirement. - pass tests - Rename test - Fix errors: distances same both directions --- gn3/computations/slink.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'gn3/computations') 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 -- cgit v1.2.3