[h5py] 401/455: Add __array__ method to improve conversion speed

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Thu Jul 2 18:19:55 UTC 2015


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to annotated tag 1.3.0
in repository h5py.

commit 1bd99fe083ad97495cfa6b197d861f832d62cbdc
Author: andrewcollette <andrew.collette at gmail.com>
Date:   Wed Feb 10 06:21:15 2010 +0000

    Add __array__ method to improve conversion speed
---
 h5py/highlevel.py               |  6 ++++++
 h5py/tests/high/test_dataset.py | 43 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)

diff --git a/h5py/highlevel.py b/h5py/highlevel.py
index 2c9de78..fe932d3 100644
--- a/h5py/highlevel.py
+++ b/h5py/highlevel.py
@@ -1281,6 +1281,12 @@ class Dataset(HLObject):
         for fspace in dest_sel.broadcast(source_sel.mshape):
             self.id.write(mspace, fspace, source)
 
+    def __array__(self, dtype=None):
+        with phil:
+            arr = numpy.empty(self.shape, dtype=self.dtype if dtype is None else dtype)
+            self.read_direct(arr)
+            return arr
+
     def __repr__(self):
         if not self:
             return "<Closed HDF5 dataset>"
diff --git a/h5py/tests/high/test_dataset.py b/h5py/tests/high/test_dataset.py
new file mode 100644
index 0000000..94882b9
--- /dev/null
+++ b/h5py/tests/high/test_dataset.py
@@ -0,0 +1,43 @@
+
+from tempfile import mktemp
+from h5py import tests
+import h5py
+
+import numpy as np
+
+class Base(tests.HTest):
+
+    def setUp(self):
+        self.name = mktemp()
+        self.f = h5py.File(self.name, 'w')
+
+    def tearDown(self):
+        import os
+        self.f.close()
+        os.unlink(self.name)
+
+class TestArray(Base):
+
+    def test_array(self):
+        """ (Dataset) Auto-conversion (2D) """
+        data = np.arange(100).reshape((10,10))
+        ds = self.f.create_dataset('foo', data=data)
+        arr = np.array(ds)
+        self.assertArrayEqual(arr, data)
+
+    def test_scalar(self):
+        """ (Dataset) Auto-conversion (scalar) """
+        data = np.array(42)
+        ds = self.f.create_dataset('foo', data=data)
+        arr = np.array(ds)
+        self.assertEqual(arr, data)
+
+    def test_dtype(self):
+        """ (Dataset) Auto-conversion (type) """
+        data = np.arange(100).reshape((10,10)).astype('u8')
+        ds = self.f.create_dataset('foo', data=data)
+        arr = ds.__array__(np.dtype('i1'))
+        self.assertEqual(arr.dtype, np.dtype('i1'))
+        self.assertArrayEqual(arr, data.astype(arr.dtype))
+
+        

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/h5py.git



More information about the debian-science-commits mailing list