Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Numpy2 compatibility fixes #416

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions allel/model/ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
'SortedIndex', 'UniqueIndex', 'SortedMultiIndex', 'VariantTable', 'FeatureTable',
'ChromPosIndex']

from .. import util

# noinspection PyTypeChecker
_total_slice = slice(None)
Expand Down Expand Up @@ -73,7 +74,7 @@ class NumpyArrayWrapper(ArrayWrapper):
"""Abstract base class that wraps a NumPy array."""

def __init__(self, data, copy=False, **kwargs):
values = np.array(data, copy=copy, **kwargs)
values = util.array(data, copy=copy, **kwargs)
super(NumpyArrayWrapper, self).__init__(values)


Expand Down Expand Up @@ -1004,7 +1005,7 @@ def to_gt(self, max_allele=None):
nchar = int(np.floor(np.log10(max_allele))) + 1

# convert to string
a = self.astype((np.string_, nchar)).view(np.chararray)
a = self.astype((np.bytes_, nchar)).view(np.chararray)

# recode missing alleles
a[self < 0] = b'.'
Expand Down Expand Up @@ -4034,7 +4035,7 @@ class SortedMultiIndex(DisplayAs1D):

def __init__(self, l1, l2, copy=False):
l1 = SortedIndex(l1, copy=copy)
l2 = np.array(l2, copy=copy)
l2 = util.array(l2, copy=copy)
check_ndim(l2, 1)
check_dim0_aligned(l1, l2)
self.l1 = l1
Expand Down
18 changes: 10 additions & 8 deletions allel/test/model/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import numpy as np
import pytest

from allel import util
from allel.test.tools import assert_array_equal as aeq


Expand Down Expand Up @@ -141,12 +143,12 @@ def test_array_like(self):

# diploid data
g = self.setup_instance(diploid_genotype_data)
a = np.array(g, copy=False)
a = util.array(g, copy=False)
aeq(diploid_genotype_data, a)

# polyploid data
g = self.setup_instance(triploid_genotype_data)
a = np.array(g, copy=False)
a = util.array(g, copy=False)
aeq(triploid_genotype_data, a)

def test_slice(self):
Expand Down Expand Up @@ -1043,7 +1045,7 @@ def test_array_like(self):
# a vanilla numpy array representation of the data.

h = self.setup_instance(haplotype_data)
a = np.array(h, copy=False)
a = util.array(h, copy=False)
aeq(haplotype_data, a)

def test_slice(self):
Expand Down Expand Up @@ -1395,7 +1397,7 @@ def test_array_like(self):
# a vanilla numpy array representation of the data.

ac = self.setup_instance(allele_counts_data)
a = np.array(ac, copy=False)
a = util.array(ac, copy=False)
aeq(allele_counts_data, a)

def test_slice(self):
Expand Down Expand Up @@ -1639,12 +1641,12 @@ def test_array_like(self):

# diploid data
g = self.setup_instance(diploid_genotype_ac_data)
a = np.array(g, copy=False)
a = util.array(g, copy=False)
aeq(diploid_genotype_ac_data, a)

# polyploid data
g = self.setup_instance(triploid_genotype_ac_data)
a = np.array(g, copy=False)
a = util.array(g, copy=False)
aeq(triploid_genotype_ac_data, a)

def test_slice(self):
Expand Down Expand Up @@ -2010,7 +2012,7 @@ def test_array_like(self):

data = [1, 4, 5, 7, 12]
pos = self.setup_instance(data)
a = np.array(pos, copy=False)
a = util.array(pos, copy=False)
aeq(data, a)

def test_slice(self):
Expand Down Expand Up @@ -2198,7 +2200,7 @@ def test_properties(self):
def test_array_like(self):
data = ['A', 'C', 'B', 'F']
lbl = self.setup_instance(data)
a = np.array(lbl, copy=False)
a = util.array(lbl, copy=False)
aeq(data, a)

def test_slice(self):
Expand Down
9 changes: 8 additions & 1 deletion allel/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
import numpy as np


def array(data, copy, **kwargs):
# Semantics of copy changed in numpy2 see https://github.com/scipy/scipy/pull/20172
if np.lib.NumpyVersion(np.__version__) >= "2.0.0":
if copy is False:
copy = None
return np.array(data, copy=copy, **kwargs)

@contextmanager
def ignore_invalid():
err = np.seterr(invalid='ignore')
Expand Down Expand Up @@ -46,7 +53,7 @@ def asarray_ndim(a, *ndims, **kwargs):
kwargs.setdefault('copy', False)
if a is None and allow_none:
return None
a = np.array(a, **kwargs)
a = array(a, **kwargs)
if a.ndim not in ndims:
if len(ndims) > 1:
expect_str = 'one of %s' % str(ndims)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ requires = [
"wheel",
"Cython>=0.28.5",
"setuptools_scm[toml]>=6.0",
"oldest-supported-numpy", # https://github.com/scipy/oldest-supported-numpy
"numpy>=2",
]
build-backend = "setuptools.build_meta"

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

LICENSE = "MIT"

INSTALL_REQUIRES = ["numpy", "dask[array]"]
INSTALL_REQUIRES = ["numpy>=1.23.5", "dask[array]"]

# full installation with all optional dependencies
EXTRAS_REQUIRE = {
Expand Down
Loading