diff options
-rw-r--r-- | gn3/computations/slink.py | 5 | ||||
-rw-r--r-- | tests/unit/computations/test_slink.py | 12 |
2 files changed, 16 insertions, 1 deletions
diff --git a/gn3/computations/slink.py b/gn3/computations/slink.py index 7b4016a..3156b31 100644 --- a/gn3/computations/slink.py +++ b/gn3/computations/slink.py @@ -70,6 +70,9 @@ This description should be updated once the form/type of 'distance' identified." raise_mirrorerror_of_distances_one_way_are_not_same_other_way(lists) raise_valueerror_on_negative_distances(lists) #### END: Guard Functions #### - return None if type(i) == int and type(j) == int: # From member i to member j return lists[i][j] + elif type(i) == int and (type(j) in [list, tuple]): + return min(map(lambda j_new: nearest(lists, i, j_new), j)) + elif type(j) == int and (type(i) in [list, tuple]): + return min(map(lambda i_new: nearest(lists, i_new, j), i)) diff --git a/tests/unit/computations/test_slink.py b/tests/unit/computations/test_slink.py index 166eff9..64d123c 100644 --- a/tests/unit/computations/test_slink.py +++ b/tests/unit/computations/test_slink.py @@ -172,3 +172,15 @@ class TestSlink(TestCase): 4,4,0]]: with self.subTest(lst=lst): self.assertEqual(nearest(lst, i, j), expected) + + def test_given_a_list_or_tuple_of_members_distances_and_a_coordinate_find_closest_member_to_member_at_coordinate(self): + for md, ml, mc, ed in [ + [[[0,9,3,6,11],[9,0,7,5,10],[3,7,0,9,2],[6,5,9,0,8],[11,10,2,8,0]],[0,1,2,3,4],3,0], + [[[0,9,3,6,11],[9,0,7,5,10],[3,7,0,9,2],[6,5,9,0,8],[11,10,2,8,0]],[0,1,2,4],3,5], + [[[0,9,3,6,11],[9,0,7,5,10],[3,7,0,9,2],[6,5,9,0,8],[11,10,2,8,0]],[0,2,4],3,6], + [[[0,9,3,6,11],[9,0,7,5,10],[3,7,0,9,2],[6,5,9,0,8],[11,10,2,8,0]],[2,4],3,8]]: + with self.subTest( + members_distances=md, members_list=ml, member_coordinate=mc, + expected_distance=ed): + self.assertEqual(nearest(md, ml, mc), ed) + self.assertEqual(nearest(md, mc, ml), ed) |