about summary refs log tree commit diff
path: root/gn3
diff options
context:
space:
mode:
authorMuriithi Frederick Muriuki2021-07-22 13:16:16 +0300
committerMuriithi Frederick Muriuki2021-07-22 13:16:16 +0300
commit0b71c74992ab1bff82d14902c51f52c3c6fbe4a1 (patch)
tree8ca936c5a37f872a0153d6fa194284c3e94d6486 /gn3
parent703fad03660c8f4a5658bd4aecd3a18fad6ce10c (diff)
downloadgenenetwork3-0b71c74992ab1bff82d14902c51f52c3c6fbe4a1.tar.gz
Check that all distances are positive or zero
Issue:
https://github.com/genenetwork/gn-gemtext-threads/blob/main/topics/gn1-migration-to-gn2/clustering.gmi

* gn3/computations/slink.py: check that all distances between the 'somethings'
  are all either zero or positive.

* tests/unit/computations/test_slink.py:
  * Remove data with all distances positive or zero, since it would fail the
    test
  * Change the expected message to more closely correspond to the business
    logic
Diffstat (limited to 'gn3')
-rw-r--r--gn3/computations/slink.py11
1 files changed, 11 insertions, 0 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