Skip to content

Commit

Permalink
added support for numpy 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jreadey committed Sep 27, 2024
1 parent 41ff190 commit 4c2844a
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 513 deletions.
2 changes: 1 addition & 1 deletion h5pyd/_apps/utillib.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def copy_array(src_arr, ctx):

if has_reference(src_arr.dtype):
# flatten array to simplify iteration
count = np.product(src_arr.shape)
count = int(np.prod(src_arr.shape))
tgt_arr_flat = tgt_arr.reshape((count,))
src_arr_flat = src_arr.reshape((count,))
for i in range(count):
Expand Down
27 changes: 14 additions & 13 deletions h5pyd/_hl/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1350,17 +1350,16 @@ def __setitem__(self, args, val):
# Attempt to directly convert the input array of vlen data to its base class
val = numpy.asarray(val, dtype=vlen_base_class)

except ValueError as ve:
except (ValueError, TypeError):
# Failed to convert input array to vlen base class directly, instead create a new array where
# each element is an array of the Dataset's dtype
self.log.debug(f"asarray ValueError: {ve}")
try:
# Force output shape
tmp = numpy.empty(shape=val.shape, dtype=self.dtype)
tmp[:] = [numpy.array(x, dtype=self.dtype) for x in val]
val = tmp
except ValueError as e:
msg = f"ValueError converting value element by element: {e}"
except (ValueError, TypeError):
msg = "ValueError converting value element by element"
self.log.debug(msg)

if vlen_base_class == val.dtype:
Expand Down Expand Up @@ -1589,19 +1588,21 @@ def write_direct(self, source, source_sel=None, dest_sel=None):
data = source.__getitem__(slices)
self.__setitem__(dest_sel, data)

def __array__(self, dtype=None):
"""Create a Numpy array containing the whole dataset. DON'T THINK
THIS MEANS DATASETS ARE INTERCHANGABLE WITH ARRAYS. For one thing,
you have to read the whole dataset everytime this method is called.
"""
arr = numpy.empty(self._shape, dtype=self.dtype if dtype is None else dtype)
def __array__(self, dtype=None, copy=True):
if copy is False:
raise ValueError(
f"AstypeWrapper.__array__ received {copy=} "
f"but memory allocation cannot be avoided on read"
)

# Special case for (0,)*-shape datasets
if self._shape is None or numpy.prod(self._shape) == 0:
return arr
return numpy.empty(self._shape, dtype=self.dtype if dtype is None else dtype)

self.read_direct(arr)
return arr
data = self[:]
if dtype is not None:
return data.astype(dtype, copy=False)
return data

def __repr__(self):
if not self:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ requires-python = ">=3.8"
version = "0.18.0"

dependencies = [
"numpy >= 1.17.3, < 2.0.0",
"numpy >=2.0.0, <3",
"requests_unixsocket",
"pytz",
"pyjwt",
Expand Down
6 changes: 3 additions & 3 deletions test/hl/test_attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
class TestAttribute(TestCase):

def test_create(self):
filename = self.getFileName("create_attribfute")
filename = self.getFileName("create_attribute")
print("filename:", filename)
f = h5py.File(filename, 'w')

Expand Down Expand Up @@ -61,7 +61,7 @@ def test_create(self):
self.assertEqual(value, "Hello HDF")

# create attribute with as a fixed length string
g1.attrs.create('d1', np.string_("This is a numpy string"))
g1.attrs.create('d1', np.bytes_("This is a numpy string"))
value = g1.attrs['d1']
self.assertEqual(value, b"This is a numpy string")

Expand Down Expand Up @@ -89,7 +89,7 @@ def test_create(self):
self.assertEqual(arr[i], 1)

# array of strings
g1.attrs['strings'] = [np.string_("Hello"), np.string_("Good-bye")]
g1.attrs['strings'] = [np.bytes_("Hello"), np.bytes_("Good-bye")]
arr = g1.attrs['strings']
self.assertEqual(arr.shape, (2,))
self.assertEqual(arr[0], b"Hello")
Expand Down
Loading

0 comments on commit 4c2844a

Please sign in to comment.