Repository: tskit-dev/pyslim

Issue:
deprecate unique_labels_by_group

Discussion:

Patch:
diff --git a/pyslim/slim_tree_sequence.py b/pyslim/slim_tree_sequence.py
index b2b8975..3d50674 100644
--- a/pyslim/slim_tree_sequence.py
+++ b/pyslim/slim_tree_sequence.py
@@ -65,12 +65,12 @@ def mutation_at(ts, node, position, time=None):
     if time is None:
         time = ts.node(node).time
     tree = ts.at(position)
-    # TODO: use `position` argument to ts.site( )
-    site_pos = ts.tables.sites.position
     out = tskit.NULL
-    if position in site_pos:
-        site_index = np.where(site_pos == position)[0][0]
-        site = ts.site(site_index)
+    try:
+        site = ts.site(position=position)
+    except ValueError:
+        pass
+    else:
         mut_nodes = []
         # look for only mutations that occurred before `time`
         # not strictly necessary if time was None
diff --git a/pyslim/util.py b/pyslim/util.py
index e9e514b..c4f58d2 100644
--- a/pyslim/util.py
+++ b/pyslim/util.py
@@ -1,3 +1,4 @@
+import warnings
 import numpy as np
 
 def unique_labels_by_group(group, label, minlength=0):
@@ -11,6 +12,12 @@ def unique_labels_by_group(group, label, minlength=0):
     In other words, if the result is ``x``, then
     ``x[j]`` is ``len(set(label[group == j])) <= 1``.
     '''
+    warnings.warn(
+            "The unique_labels_by_group( ) method is no longer used in pyslim, "
+            "so is now deprecated. Please copy the source code for your own use "
+            "if you wish to keep using it.",
+            FutureWarning,
+    )
     w = label.astype("float64")
     n = np.bincount(1 + group, minlength=minlength + 1)
     x = np.bincount(1 + group, weights=w, minlength=minlength + 1)


Test patch:
diff --git a/tests/test_util.py b/tests/test_util.py
index 9879431..a78068e 100644
--- a/tests/test_util.py
+++ b/tests/test_util.py
@@ -1,7 +1,8 @@
 """
 Test cases for utility functions.
 """
-import tests
+import pytest
+import warnings
 import numpy as np
 
 import pyslim
@@ -9,7 +10,8 @@ import pyslim
 class TestUniqueLabelsByGroup():
 
     def verify_unique_labels_by_group(self, group, label, minlength):
-        x = pyslim.util.unique_labels_by_group(group, label, minlength)
+        with pytest.warns(FutureWarning):
+            x = pyslim.util.unique_labels_by_group(group, label, minlength)
         assert len(x) >= minlength
         for g in range(len(x)):
             u = set(label[group == g])
@@ -22,13 +24,15 @@ class TestUniqueLabelsByGroup():
         group = np.repeat(1, 10)
         label = np.arange(10)
         self.verify_unique_labels_by_group(group, label, 1)
-        x = pyslim.util.unique_labels_by_group(group, label, 1)
+        with pytest.warns(FutureWarning):
+            x = pyslim.util.unique_labels_by_group(group, label, 1)
         assert len(x) == 2
         assert x[0] == True
         assert x[1] == False
         label = np.repeat(5, 10)
         self.verify_unique_labels_by_group(group, label, 1)
-        x = pyslim.util.unique_labels_by_group(group, label, 1)
+        with pytest.warns(FutureWarning):
+            x = pyslim.util.unique_labels_by_group(group, label, 1)
         assert len(x) == 2
         assert x[0] == True
         assert x[1] == True
@@ -38,12 +42,14 @@ class TestUniqueLabelsByGroup():
         group = np.arange(ng)
         label = np.arange(ng)
         self.verify_unique_labels_by_group(group, label, ng)
-        x = pyslim.util.unique_labels_by_group(group, label, ng)
+        with pytest.warns(FutureWarning):
+            x = pyslim.util.unique_labels_by_group(group, label, ng)
         assert np.all(x)
         group = np.append(group, [-1, -1, -1])
         label = np.append(label, [0, 1, 2])
         self.verify_unique_labels_by_group(group, label, ng)
-        x = pyslim.util.unique_labels_by_group(group, label, ng)
+        with pytest.warns(FutureWarning):
+            x = pyslim.util.unique_labels_by_group(group, label, ng)
         assert np.all(x)
 
     def test_unique_labels_by_group(self):
@@ -63,9 +69,10 @@ class TestUniqueLabelsByGroup():
                         self.verify_unique_labels_by_group(group, label, ng)
 
     def test_unused_labels(self):
-        x = pyslim.unique_labels_by_group(
-            group=np.array([1, 1, 4], dtype='int'),
-            label=np.array([2, 2, 2], dtype='int')
-        )
+        with pytest.warns(FutureWarning):
+            x = pyslim.unique_labels_by_group(
+                group=np.array([1, 1, 4], dtype='int'),
+                label=np.array([2, 2, 2], dtype='int')
+            )
         assert np.all(x)