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

Implemented H5DOappend #1891

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions h5py/api_functions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ hdf5:
1.10.5 herr_t H5Dget_chunk_info(hid_t dset_id, hid_t fspace_id, hsize_t chk_idx, hsize_t *offset, unsigned *filter_mask, haddr_t *addr, hsize_t *size)
1.10.5 herr_t H5Dget_chunk_info_by_coord(hid_t dset_id, const hsize_t *offset, unsigned *filter_mask, haddr_t *addr, hsize_t *size)

# HDF5 Append
1.10.0 herr_t H5DOappend(hid_t dset_id, hid_t dxpl_id, unsigned index, size_t num_elem, hid_t memtype, const void *buffer)


# === H5E - Minimal error-handling interface ================================

Expand Down
37 changes: 37 additions & 0 deletions h5py/h5d.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,43 @@ cdef class DatasetID(ObjectID):

dset_rw(self_id, mtype_id, mspace_id, fspace_id, plist_id, data, 0)

IF HDF5_VERSION >= (1, 10, 2):
@with_phil
def append(self, ndarray arr_obj not None, uint32_t index, size_t num_elem,
TypeID mtype=None, PropID dxpl=None):
""" (SpaceID mspace, SpaceID fspace, NDARRAY arr_obj,
TypeID mtype=None, PropDXID dxpl=None)

Write data from a Numpy array to an HDF5 dataset. Keyword dxpl may
be a dataset transfer property list.

It is your responsibility to ensure that the memory dataspace
provided is compatible with the shape of the Numpy array. Since a
wide variety of dataspace configurations are possible, this is not
checked. You can easily crash Python by writing data from too
large a dataspace.

If a memory datatype is not specified, one will be auto-created
based on the array's dtype.

The provided Numpy array must be C-contiguous. If this is not the
case, ValueError will be raised and the read will fail.
"""
cdef hid_t self_id, mtype_id, plist_id
cdef void* data
cdef int oldflags

if mtype is None:
mtype = py_create(arr_obj.dtype)
check_numpy_read(arr_obj, -1)

self_id = self.id
mtype_id = mtype.id
plist_id = pdefault(dxpl)
data = PyArray_DATA(arr_obj)

H5DOappend(self_id, plist_id, index, num_elem, mtype_id, data)


@with_phil
def extend(self, tuple shape):
Expand Down
29 changes: 29 additions & 0 deletions h5py/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,35 @@ def test_len_big(self):
self.assertEqual(dset.len(), 2 ** 33)


class TestAppend(BaseDataset):
"""
Feature: H5DOappend testing
"""
@ut.skipIf(
h5py.version.hdf5_version_tuple < (1, 10, 0),
"Reading non-existent label segfaults"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this comment correct?

)
def test_append_1(self):
dset = self.f.create_dataset('foo', (0, 10), maxshape=(None, 10))
for i in range(10):
data = np.full(10, i)
dset.id.append(data, 0, 1)
np.testing.assert_array_equal(dset[-1], data)
self.assertEqual(len(dset), 10)

@ut.skipIf(
h5py.version.hdf5_version_tuple < (1, 10, 0),
"Reading non-existent label segfaults"
)
def test_append_2(self):
dset = self.f.create_dataset('foo', (0, 10), maxshape=(None, 10))
for i in range(5):
data = np.full((2, 10), i)
dset.id.append(data, 0, 2)
np.testing.assert_array_equal(dset[-2:], data)
self.assertEqual(len(dset), 10)


class TestIter(BaseDataset):

"""
Expand Down
5 changes: 5 additions & 0 deletions news/implement-1890.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Exposing HDF5 functions
-----------------------

* Added H5DOappend to exposed C functions
* Added append to h5d.pyx