Skip to content

Commit 87fdc66

Browse files
committed
Monkey patch until tskit v2 released
1 parent 596e588 commit 87fdc66

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

tsinfer/__init__.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,36 @@
2323
"""
2424

2525
import sys
26+
# Start temporary monkey patch to allow use of functions / constants in tskit v2.0.
27+
# The following lines can be deleted once the master tskit version has been updated
28+
import numpy as np
29+
import tskit
30+
tskit.MISSING_DATA = -1
31+
32+
33+
class util():
34+
@staticmethod
35+
def safe_np_int_cast(int_array, dtype, copy=False): # Copied from v2.0 tskit/util.py
36+
if not isinstance(int_array, np.ndarray):
37+
int_array = np.array(int_array)
38+
copy = False
39+
if int_array.size == 0:
40+
return int_array.astype(dtype, copy=copy)
41+
try:
42+
return int_array.astype(dtype, casting='safe', copy=copy)
43+
except TypeError:
44+
bounds = np.iinfo(dtype)
45+
if np.any(int_array < bounds.min) or np.any(int_array > bounds.max):
46+
raise OverflowError("Cannot convert safely to {} type".format(dtype))
47+
if int_array.dtype.kind == 'i' and np.dtype(dtype).kind == 'u':
48+
casting = 'unsafe'
49+
else:
50+
casting = 'same_kind'
51+
return int_array.astype(dtype, casting=casting, copy=copy)
52+
53+
54+
tskit.util = util
55+
# End temporary monkey patch
2656

2757
if sys.version_info[0] < 3:
2858
raise Exception("Python 3 only")

0 commit comments

Comments
 (0)