[h5py] 281/455: More tests & fixes

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Thu Jul 2 18:19:42 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 6158b9bd90791fbcd283293fb0f8a7ed71b8a9ba
Author: andrewcollette <andrew.collette at gmail.com>
Date:   Wed Jun 17 00:33:18 2009 +0000

    More tests & fixes
---
 h5py/highlevel.py            |  24 +++++----
 h5py/selections.py           |   8 ++-
 h5py/tests/common.py         | 115 +++++++++++++++++++------------------------
 h5py/tests/test_dataset.py   |   5 +-
 h5py/tests/test_h5a.py       |   4 +-
 h5py/tests/test_h5d.py       |   4 +-
 h5py/tests/test_h5f.py       |   6 +--
 h5py/tests/test_h5g.py       |   6 +--
 h5py/tests/test_h5i.py       |   4 +-
 h5py/tests/test_h5s.py       |   4 +-
 h5py/tests/test_h5t.py       |   4 +-
 h5py/tests/test_highlevel.py |  45 +++++++----------
 h5py/tests/test_slicing.py   |  88 +++++++++++++++++++++++++++------
 h5py/tests/test_utils.py     |   4 +-
 14 files changed, 182 insertions(+), 139 deletions(-)

diff --git a/h5py/highlevel.py b/h5py/highlevel.py
index 6cd5866..ec60425 100644
--- a/h5py/highlevel.py
+++ b/h5py/highlevel.py
@@ -1013,8 +1013,13 @@ class Dataset(HLObject):
             if len(names) != 0:
                 raise TypeError("Field name selections are not allowed for write.")
 
-            # Validate the input array
-            val = numpy.asarray(val, order='C')
+            val2 = numpy.asarray(val, order='C')
+
+            # Special fudge factor for weirdness with scalar compound literals
+            if self.dtype.kind == 'V' and val2.dtype.kind != 'V':
+                val = numpy.asarray(val, dtype=self.dtype, order='C')
+            else:
+                val = val2
 
             # Check for array dtype compatibility and convert
             if self.dtype.subdtype is not None:
@@ -1033,13 +1038,15 @@ class Dataset(HLObject):
             if selection.nselect == 0:
                 return
 
-            # Broadcast scalars if necessary
-            # TODO: fix scalar broadcasting for array types
-            if mshape == () and selection.mshape != () and self.dtype.subdtype is None:
+            # Broadcast scalars if necessary.
+            if (mshape == () and selection.mshape != ()):
+                if self.dtype.subdtype is not None:
+                    raise NotImplementedError("Scalar broadcasting is not supported for array dtypes")
                 val2 = numpy.empty(selection.mshape[-1], dtype=val.dtype)
                 val2[...] = val
                 val = val2
-            
+                mshape = val.shape
+
             # Perform the write, with broadcasting
             # Be careful to pad memory shape with ones to avoid HDF5 chunking
             # glitch, which kicks in for mismatched memory/file selections
@@ -1161,12 +1168,11 @@ class AttributeManager(_LockableObject, _DictCompat):
         """ Delete an attribute (which must already exist). """
         h5a.delete(self.id, name)
 
-    def create(self, name, data=None, shape=None, dtype=None):
+    def create(self, name, data, shape=None, dtype=None):
         """ Create a new attribute, overwriting any existing attribute.
 
         name:   Name of the new attribute (required)
-        data:   An array to initialize the attribute.
-                Required unless "shape" is given.
+        data:   An array to initialize the attribute (required)
         shape:  Shape of the attribute.  Overrides data.shape if both are
                 given.  The total number of points must be unchanged.
         dtype:  Data type of the attribute.  Overrides data.dtype if both
diff --git a/h5py/selections.py b/h5py/selections.py
index 6593691..ff8a216 100644
--- a/h5py/selections.py
+++ b/h5py/selections.py
@@ -322,12 +322,18 @@ class FancySelection(Selection):
 
         sequenceargs = {}
         for idx, arg in enumerate(args):
-            # TODO: Put argument verification back in and handle boolean arrays
             if not isinstance(arg, slice):
+                if hasattr(arg, 'dtype') and arg.dtype == np.dtype('bool'):
+                    if len(arg.shape) != 1:
+                        raise TypeError("Boolean indexing arrays must be 1-D")
+                    arg = arg.nonzero()[0]
                 try:
                     sequenceargs[idx] = list(arg)
                 except TypeError:
                     pass
+                else:
+                    if sorted(arg) != list(arg):
+                        raise TypeError("Indexing elements must be in increasing order")
 
         if len(sequenceargs) > 1:
             # TODO: fix this with broadcasting
diff --git a/h5py/tests/common.py b/h5py/tests/common.py
index bd9785b..4c88509 100644
--- a/h5py/tests/common.py
+++ b/h5py/tests/common.py
@@ -18,6 +18,8 @@ import shutil
 from h5py import h5f, h5p, h5
 import h5py
 
+import numpy as np
+
 DATADIR = op.join(op.dirname(h5py.__file__), 'tests/data')
 
 class ResourceManager(object):
@@ -28,7 +30,7 @@ class ResourceManager(object):
         having to manually unlink its files, and restores the library to
         a known state.
     """
-
+    
     def __init__(self):
         self.fnames = set()
 
@@ -70,6 +72,27 @@ class ResourceManager(object):
 
 res = ResourceManager()
 
+class TypeManager(object):
+
+    ints =  [np.dtype(x) for x in ('i', 'i1', '<i2', '>i2', '<i4', '>i4')]
+    uints = [np.dtype(x) for x in ('u1', '<u2', '>u2', '<u4', '>u4')]
+    floats =  [np.dtype(x) for x in ('f', '<f4', '>f4', '<f8', '>f8')]
+    complex = [np.dtype(x) for x in ('<c8', '>c8', '<c16', '>c16')]
+    strings = [np.dtype(x) for x in ('|S1', '|S2', 'S17', '|S100')]
+    voids =   [np.dtype(x) for x in ('|V1', '|V4', '|V8', '|V193')]
+
+    compounds = [np.dtype(x) for x in \
+                (   [('a', 'i'), ('b', 'f')],
+                    [('a', '=c8'), ('b', [('a', 'i'), ('b', 'f')])] ) ] 
+
+types = TypeManager()
+
+FLOATS = ('f', '<f4', '>f4', '<f8', '>f8')
+COMPLEX = ('<c8', '>c8', '<c16', '>c16')
+STRINGS = ('|S1', '|S2', 'S17', '|S100')
+VOIDS = ('|V4', '|V8')
+
+
 def getfullpath(name):
     return op.abspath(op.join(DATADIR, name))
 
@@ -90,26 +113,6 @@ def skip(func):
     skipped.append(func)
     return None
 
-test_coverage = set()
-
-def covers(*args):
-    global test_coverage
-    
-    def wrap(meth):
-        test_coverage.update(args)
-        return meth
-
-    return wrap
-
-def makehdf():
-    fname = tempfile.mktemp('.hdf5')
-    f = h5py.File(fname, 'w')
-    return f
-
-def delhdf(f):
-    fname = f.filename
-    f.close()
-    os.unlink(fname)
 
 EPSILON = 1e-5
 import numpy as np
@@ -121,58 +124,19 @@ COMPLEX = ('<c8', '>c8', '<c16', '>c16')
 STRINGS = ('|S1', '|S2', 'S17', '|S100')
 VOIDS = ('|V4', '|V8')
 
-def assert_arr_equal(dset, arr, message=None, precision=None):
-    """ Make sure dset and arr have the same shape, dtype and contents, to
-        within the given precision.
-
-        Note that dset may be a NumPy array or an HDF5 dataset.
-    """
-    if precision is None:
-        precision = EPSILON
-    if message is None:
-        message = ''
-
-    if np.isscalar(dset) or np.isscalar(arr):
-        assert np.isscalar(dset) and np.isscalar(arr), "%r %r" % (dset, arr)
-        assert dset - arr < precision, message
-        return
-
-    assert dset.shape == arr.shape, message
-    assert dset.dtype == arr.dtype, message
-    assert np.all(np.abs(dset[...] - arr[...]) < precision), "%s %s" % (dset[...], arr[...]) if not message else message
-
-class HDF5TestCase(unittest.TestCase):
-
-    """
-        Base test for unit test classes.
-    """
-
-    h5py_verbosity = 0
-
-    def output(self, ipt):
-        """Print to stdout, only if verbosity levels so requires"""
-        if self.h5py_verbosity >= 3:
-            print ipt
-
-    def setup_fid(self, hdfname):
-        """Open a copy of an HDF5 file and set its identifier as self.fid"""
-        hdfname = getfullpath(hdfname)
-        newname = tempfile.mktemp('.hdf5')
-        shutil.copy(hdfname, newname)
+class TestCasePlus(unittest.TestCase):
 
+    def setup_fid(self, name):
+        self.fname = res.get_data_copy(name)
+        
         plist = h5p.create(h5p.FILE_ACCESS)
         plist.set_fclose_degree(h5f.CLOSE_STRONG)
-        self.fid = h5f.open(newname, h5f.ACC_RDWR, fapl=plist)
-        self.fname = newname
-        self.src_fname = hdfname
+        self.fid = h5f.open(self.fname, h5f.ACC_RDWR, fapl=plist)
 
     def teardown_fid(self):
-        """Close the HDF5 file copy and delete it"""
         self.fid.close()
         os.unlink(self.fname)
 
-class TestCasePlus(unittest.TestCase):
-
     def assertRaisesMsg(self, msg, exc, clb, *args, **kwds):
         try:
             clb(*args, **kwds)
@@ -180,6 +144,27 @@ class TestCasePlus(unittest.TestCase):
             return
         raise AssertionError("%s not raised: %s" % (exc, msg))
 
+    def assertArrayEqual(self, dset, arr, message=None, precision=None):
+        """ Make sure dset and arr have the same shape, dtype and contents, to
+            within the given precision.
+
+            Note that dset may be a NumPy array or an HDF5 dataset.
+        """
+        if precision is None:
+            precision = EPSILON
+        if message is None:
+            message = ''
+        else:
+            message = ' (%s)' % message
+
+        if np.isscalar(dset) or np.isscalar(arr):
+            assert np.isscalar(dset) and np.isscalar(arr), 'Scalar/array mismatch ("%r" vs "%r")%s' % (dset, arr, message)
+            assert dset - arr < precision, "Scalars differ by more than %.3f%s" % (precision, message)
+            return
+
+        assert dset.shape == arr.shape, "Shape mismatch (%s vs %s)%s" % (dset.shape, arr.shape, message)
+        assert dset.dtype == arr.dtype, "Dtype mismatch (%s vs %s)%s" % (dset.dtype, arr.dtype, message)
+        assert np.all(np.abs(dset[...] - arr[...]) < precision), "Arrays differ by more than %.3f%s" % (precision, message)
 
 
 
diff --git a/h5py/tests/test_dataset.py b/h5py/tests/test_dataset.py
index 5354dca..913a834 100644
--- a/h5py/tests/test_dataset.py
+++ b/h5py/tests/test_dataset.py
@@ -9,10 +9,9 @@ import numpy as np
 
 import h5py
 import unittest
-from common import makehdf, delhdf, assert_arr_equal,\
-                   INTS, FLOATS, COMPLEX, STRINGS, res
+from common import TestCasePlus, INTS, FLOATS, COMPLEX, STRINGS, res
 
-class TestDataset(unittest.TestCase):
+class TestDataset(TestCasePlus):
 
     def setUp(self):
         self.f = h5py.File(res.get_name(), 'w')
diff --git a/h5py/tests/test_h5a.py b/h5py/tests/test_h5a.py
index c58357f..a2c0718 100644
--- a/h5py/tests/test_h5a.py
+++ b/h5py/tests/test_h5a.py
@@ -12,7 +12,7 @@
 
 from numpy import array, ndarray, dtype, all, ones
 
-from common import HDF5TestCase, api_18
+from common import TestCasePlus, api_18
 
 from h5py import *
 
@@ -28,7 +28,7 @@ ATTRIBUTES_ORDER = sorted(ATTRIBUTES) # ['String Attribute', 'Integer', 'Integer
 NEW_ATTRIBUTES = {'New float': ( 3.14, dtype('<f4'), ()) }
 
 
-class TestH5A(HDF5TestCase):
+class TestH5A(TestCasePlus):
 
     def setUp(self):
         self.setup_fid(HDFNAME)
diff --git a/h5py/tests/test_h5d.py b/h5py/tests/test_h5d.py
index 91f7894..31e297c 100644
--- a/h5py/tests/test_h5d.py
+++ b/h5py/tests/test_h5d.py
@@ -11,7 +11,7 @@
 #-
 
 import numpy
-from common import HDF5TestCase
+from common import TestCasePlus
 
 from h5py import *
 
@@ -33,7 +33,7 @@ for i in range(SHAPE[0]):
     basearray[i]["f_name"][:] = numpy.array((1024.9637*i,)*10)
     basearray[i]["g_name"] = 109
 
-class TestH5D(HDF5TestCase):
+class TestH5D(TestCasePlus):
 
 
     def setUp(self):
diff --git a/h5py/tests/test_h5f.py b/h5py/tests/test_h5f.py
index 1d34609..abf4cce 100644
--- a/h5py/tests/test_h5f.py
+++ b/h5py/tests/test_h5f.py
@@ -13,13 +13,13 @@
 import numpy
 import os
 import tempfile
-from common import getfullpath, HDF5TestCase
+from common import TestCasePlus, res
 
 from h5py import *
 
 HDFNAME = 'attributes.hdf5'
 
-class TestH5F(HDF5TestCase):
+class TestH5F(TestCasePlus):
 
 
     def setUp(self):
@@ -29,7 +29,7 @@ class TestH5F(HDF5TestCase):
         self.teardown_fid()
 
     def test_open_close(self):
-        fid = h5f.open(getfullpath(HDFNAME), h5f.ACC_RDONLY)
+        fid = h5f.open(res.get_data_path('attributes.hdf5'), h5f.ACC_RDONLY)
         self.assertEqual(h5i.get_type(fid), h5i.FILE)
         fid.close()
         self.assertEqual(h5i.get_type(fid), h5i.BADID)
diff --git a/h5py/tests/test_h5g.py b/h5py/tests/test_h5g.py
index 91929d8..7b28379 100644
--- a/h5py/tests/test_h5g.py
+++ b/h5py/tests/test_h5g.py
@@ -10,7 +10,7 @@
 # 
 #-
 
-from common import HDF5TestCase
+from common import TestCasePlus
 
 from h5py import *
 
@@ -19,7 +19,7 @@ OBJECTNAME = 'Group'
 TEST_GROUPS = ['Subgroup1','Subgroup2','Subgroup3']
 NEW_LINK_NAME = 'Link name'
 
-class TestH5G(HDF5TestCase):
+class TestH5G(TestCasePlus):
 
 
     def setUp(self):
@@ -41,7 +41,7 @@ class TestH5G(HDF5TestCase):
             grp._close()
             self.assert_(not self.is_grp(grp), pr+"::"+str(grp))
         
-        self.assertRaises(H5Error, h5g.open, self.obj, 'Some other group')
+        self.assertRaises(KeyError, h5g.open, self.obj, 'Some other group')
 
     def test_create(self):
 
diff --git a/h5py/tests/test_h5i.py b/h5py/tests/test_h5i.py
index 9cf4d1c..8fbc134 100644
--- a/h5py/tests/test_h5i.py
+++ b/h5py/tests/test_h5i.py
@@ -10,14 +10,14 @@
 # 
 #-
 
-from common import HDF5TestCase
+from common import TestCasePlus
 
 from h5py import *
 
 HDFNAME = 'attributes.hdf5'
 OBJECTNAME = 'Group'
 
-class TestH5I(HDF5TestCase):
+class TestH5I(TestCasePlus):
 
 
     def setUp(self):
diff --git a/h5py/tests/test_h5s.py b/h5py/tests/test_h5s.py
index 245e29c..4f8646e 100644
--- a/h5py/tests/test_h5s.py
+++ b/h5py/tests/test_h5s.py
@@ -26,8 +26,6 @@ class TestH5S(unittest.TestCase):
         sid._close()
         self.assertEqual(h5i.get_type(sid), h5i.BADID)
 
-        self.assertRaises(H5Error, h5s.create, -1)
-
     def test_copy(self):
         sid = h5s.create(h5s.SCALAR)
         sid2 = sid.copy()
@@ -43,7 +41,7 @@ class TestH5S(unittest.TestCase):
             self.assertEqual(sid.get_simple_extent_ndims(), len(space))
 
         self.assertRaises(ValueError, h5s.create_simple, None)
-        self.assertRaises(H5Error, h5s.create_simple, (10,10), (10,9))
+        self.assertRaises(ValueError, h5s.create_simple, (10,10), (10,9))
         self.assertRaises(ValueError, h5s.create_simple, (10,10), (10,))
 
     def test_is_simple(self):
diff --git a/h5py/tests/test_h5t.py b/h5py/tests/test_h5t.py
index fb2eb2f..e498541 100644
--- a/h5py/tests/test_h5t.py
+++ b/h5py/tests/test_h5t.py
@@ -16,7 +16,7 @@ import os
 from numpy import dtype
 
 from h5py import *
-from common import HDF5TestCase
+from common import TestCasePlus
 
 kind_map = {'i': h5t.TypeIntegerID, 'u': h5t.TypeIntegerID, 'f': h5t.TypeFloatID,
            'c': h5t.TypeCompoundID, 'S': h5t.TypeStringID, 'V': h5t.TypeOpaqueID}
@@ -30,7 +30,7 @@ simple_types = \
     "<f4", "<f8", ">f4", ">f8", "<c8", "<c16", ">c8", ">c16",
     "|S1", "|S2", "|S33", "|V1", "|V2", "|V33"]
 
-class TestH5T(HDF5TestCase):
+class TestH5T(TestCasePlus):
 
 
     def test_create(self):
diff --git a/h5py/tests/test_highlevel.py b/h5py/tests/test_highlevel.py
index 0a93e6e..6b3ffd5 100644
--- a/h5py/tests/test_highlevel.py
+++ b/h5py/tests/test_highlevel.py
@@ -23,7 +23,7 @@ import os.path as op
 import h5py
 from h5py.highlevel import *
 from h5py import *
-from common import getfullpath, HDF5TestCase, api_18, api_16, res, TestCasePlus
+from common import TestCasePlus, api_18, api_16, res, TestCasePlus
 import common
 import testfiles
 
@@ -34,8 +34,6 @@ class SliceFreezer(object):
 def skip(func):
     return None
 
-HDFNAME = getfullpath("smpl_compound_chunked.hdf5")
-
 TYPES1 = \
   [ "<i1", "<i2", "<i4", "<i8", ">i1", ">i2", ">i4", ">i8", "|i1", "|u1", 
     "<u1", "<u2", "<u4", "<u8", ">u1", ">u2", ">u4", ">u8",
@@ -52,7 +50,7 @@ SHAPES = [(), (1,), (10,5), (1,10), (10,1), (100,1,100), (51,2,1025)]
 class TestFile(TestCasePlus):
 
     def setUp(self):
-        self.fname = res.get_data_copy(HDFNAME)
+        self.fname = res.get_data_copy("smpl_compound_chunked.hdf5")
 
     def tearDown(self):
         res.clear()
@@ -256,7 +254,7 @@ class TestFile(TestCasePlus):
         del f.attrs['a']
         self.assert_(not 'a' in f.attrs)
 
-class TestDataset(HDF5TestCase):
+class TestDataset(TestCasePlus):
 
     def setUp(self):
 
@@ -306,8 +304,6 @@ class TestDataset(HDF5TestCase):
     def test_Dataset_resize(self):
         """ Test extending datasets """
 
-        self.output("")
-
         init_shapes = [(100,), (100,100), (150,100)]
         max_shapes = [(200,), (200,200), (None, 100)]
         chunks = [(10,), (10,10), (10,10)]
@@ -328,12 +324,12 @@ class TestDataset(HDF5TestCase):
             self.assertEqual(ds.shape, shape)
 
             for final_shape in final_shapes[shape]:
-                self.output("    Extending %s to %s" % (shape, final_shape))
+                msg = "Extending %s to %s" % (shape, final_shape)
                 newarr = numpy.arange(numpy.product(final_shape)).reshape(final_shape)
                 ds.resize(final_shape)
                 ds[...] = newarr
-                self.assertEqual(ds.shape, final_shape)
-                self.assert_(numpy.all(ds[...] == newarr))
+                self.assertEqual(ds.shape, final_shape, msg)
+                self.assertArrayEqual(ds[...], newarr, msg)
 
             for illegal_shape in illegal_shapes[shape]:
                 self.assertRaises(ValueError, ds.resize, illegal_shape)
@@ -358,8 +354,6 @@ class TestDataset(HDF5TestCase):
         self.assertRaises(TypeError, list, d2)
 
     def test_slice_big(self):
-        """ Test slices > 2**32 """
-        self.output("")
 
         s = SliceFreezer()
 
@@ -375,19 +369,19 @@ class TestDataset(HDF5TestCase):
             dset = self.f.create_dataset("dset", (2**62, 2**62), '=f4', maxshape=(None,None))
 
             for shp, slc in zip(shapes, slices):
-                self.output("    Testing base 2**%d" % numpy.log2(base))
+                msg = "Testing slice base 2**%d" % numpy.log2(base)
 
-                empty = numpy.zeros(shp)
+                empty = numpy.zeros(shp, dtype='=f4')
                 data = numpy.arange(numpy.product(shp), dtype='=f4').reshape(shp)
 
                 dset[slc] = empty
                 arr = dset[slc]
                 self.assertEqual(arr.shape, shp)
-                self.assert_(numpy.all(arr == empty), "%r \n\n %r" % (arr, empty))
+                self.assertArrayEqual(arr, empty, msg)
                 
                 dset[slc] = data
                 arr = dset[slc]
-                self.assert_(numpy.all(arr == data), "%r \n\n %r" % (arr, data))
+                self.assertArrayEqual(arr, data, msg)
 
     def test_Dataset_exceptions(self):
         """ Test exceptions """
@@ -398,7 +392,7 @@ class TestDataset(HDF5TestCase):
         self.assertRaises(TypeError, dsid.id.read, h5s.ALL, h5s.ALL, arr)
         # or it'll segfault...
 
-class TestExceptions(HDF5TestCase):
+class TestExceptions(TestCasePlus):
 
     def setUp(self):
 
@@ -458,7 +452,7 @@ class TestExceptions(HDF5TestCase):
                 pass
             os.unlink(fname)
 
-class TestGroup(HDF5TestCase):
+class TestGroup(TestCasePlus):
 
     def setUp(self):
         self.f = File(res.get_name(), 'w')
@@ -536,27 +530,26 @@ class TestGroup(HDF5TestCase):
     def test_Group_setgetitem(self):
         # Also tests named types
 
-        self.output('')
         for shape in SHAPES:
             for dt in TYPES1:
 
-                self.output("    Assigning %s %s" % (dt, shape))
+                msg = "Assign %s %s" % (dt, shape)
 
                 # test arbitrary datasets
                 dt_obj = numpy.dtype(dt)       
                 arr = numpy.ones(shape, dtype=dt_obj)
                 self.f["DS"] = arr
                 harr = self.f["DS"]
-                self.assert_(isinstance(harr, Dataset))
-                self.assertEqual(harr.shape, shape)
-                self.assertEqual(harr.dtype, dt_obj)
-                self.assert_(numpy.all(harr.value == arr))
+                self.assert_(isinstance(harr, Dataset), msg)
+                self.assertEqual(harr.shape, shape, msg)
+                self.assertEqual(harr.dtype, dt_obj, msg)
+                self.assertArrayEqual(harr.value, arr[()], msg)
 
                 # test named types
                 self.f["TYPE"] = dt_obj
                 htype = self.f["TYPE"]
-                self.assert_(isinstance(htype, Datatype))
-                self.assertEqual(htype.dtype, dt_obj)
+                self.assert_(isinstance(htype, Datatype), msg)
+                self.assertEqual(htype.dtype, dt_obj, msg)
 
                 del self.f["DS"]
                 del self.f["TYPE"]
diff --git a/h5py/tests/test_slicing.py b/h5py/tests/test_slicing.py
index b182da3..42c76fe 100644
--- a/h5py/tests/test_slicing.py
+++ b/h5py/tests/test_slicing.py
@@ -1,8 +1,32 @@
+
+"""
+    Tests slicing compatibility.  The following slicing schemes are supported:
+
+    Simple slicing
+        Uses any combination of integers, ":" and "...".  These translate
+        to hyperslab selections and are the easiest to implement
+
+    Broadcast slicing
+        Refers to simple slicing where the shape of the selection and the
+        shape of the memory data do not match.  The rules for NumPy
+        broadcasting are pathologically complex.  Therefore, broadcasting is
+        not supported for advanced indexing.
+
+    Advanced indexing
+        Equivalent to simple slicing, except that the following are allowed:
+
+        1.  A list of indices, per axis
+        2.  A boolean array, per axis
+        3.  One large boolean array
+"""
+
+
+
 import numpy as np
 import os
 import unittest
 
-from common import makehdf, delhdf, assert_arr_equal, skip, res
+from common import TestCasePlus, res
 
 import h5py
 
@@ -14,7 +38,7 @@ class SliceFreezer(object):
 
 s = SliceFreezer()
 
-class TestSlicing(unittest.TestCase):
+class TestSlicing(TestCasePlus):
 
     def setUp(self):
         self.f = h5py.File(res.get_name(), 'w')
@@ -63,18 +87,18 @@ class TestSlicing(unittest.TestCase):
 
             arr[slc] += np.random.rand()
             dset[slc] = arr[slc]
-            assert_arr_equal(dset, arr, "write"+msg)
+            self.assertArrayEqual(dset, arr, "write"+msg)
 
             out = dset[slc]
-            assert_arr_equal(out, arr[slc], "read"+msg)
+            self.assertArrayEqual(out, arr[slc], "read"+msg)
 
             arr[slc] += np.random.rand()
             dset.write_direct(arr, slc, slc)
-            assert_arr_equal(dset, arr, "write direct"+msg)
+            self.assertArrayEqual(dset, arr, "write direct"+msg)
 
             out = np.ndarray(shape, 'f')
             dset.read_direct(out, slc, slc)
-            assert_arr_equal(out[slc], arr[slc], "read direct"+msg)
+            self.assertArrayEqual(out[slc], arr[slc], "read direct"+msg)
 
     def test_slices_big(self):
         # Test slicing behavior for indices larger than 2**32
@@ -98,7 +122,7 @@ class TestSlicing(unittest.TestCase):
 
                 dset[slc] = data
 
-                assert_arr_equal(dset[slc], data, msg)
+                self.assertArrayEqual(dset[slc], data, msg)
 
     def test_scalars(self):
         # Confirm correct behavior for scalar datasets
@@ -129,22 +153,24 @@ class TestSlicing(unittest.TestCase):
 
             dset[slc] = subarr
             arr[slc] = subarr
-            assert_arr_equal(dset, arr, "broadcast %s %s" % (slc, shape))
+            self.assertArrayEqual(dset, arr, "broadcast %s %s" % (slc, shape))
 
+    def test_scalar_broadcast(self):
+        # Check scalar broadcasting for multiple types
 
-    @skip
-    def test_broadcast_big(self):
+        types = ['i', 'f', [('a', 'i'), ('b','f')]]
+        values = [np.ones((), t) for t in types]
 
-        M = 1024*1024
+        for idx, (v, t) in enumerate(zip(values, types)):
 
-        dset = self.f.create_dataset('dset', (100,0.5*M), 'i')
+            comparison = np.empty((100,100), dtype=t)
+            comparison[...] = v
 
-        dset[...] = 42
+            dset = self.f.create_dataset('ds%d' % idx, (100,100), dtype=t)
 
-        comprow = np.ones((0.5*M,),dtype='i')*42
+            dset[...] = v
 
-        for row in dset:
-            assert np.all(row == comprow)
+            self.assert_(np.all(dset[...] == comparison), "%d: %s %s" % (idx, v, t))
 
     def test_slice_names(self):
         # Test slicing in conjunction with named fields
@@ -170,6 +196,36 @@ class TestSlicing(unittest.TestCase):
             msg = "slicing %s" % (slc,)
             assert np.all(dset[slc] == result), msg
 
+    def test_fancy_index(self):
+        # Test fancy selection with list indexing. Addresses I29, I31, I32.
+
+        mydata = np.arange(4*5,dtype='i').reshape((4,5))
+        dset = self.f.create_dataset("mydata", data=mydata)
+
+        slc = s[:, [0,1,2,3,4]]
+        self.assertArrayEqual(mydata[slc], dset[slc])
+
+        self.assertRaises(TypeError, dset.__getitem__, s[0,[1,0,2]])
+        self.assertRaises(TypeError, dset.__getitem__, s[[0,1], [0,1]])
+
+        # Boolean array indexing
+        barr = np.array([True, False, True, True], 'bool')
+        self.assertArrayEqual(mydata[0,barr], dset[0,barr])
+
+        # Check that NumPy arrays can be used as lists
+        slc = s[:, np.array([0,1,3], dtype='i')]
+        self.assertArrayEqual(mydata[slc], dset[slc])
+
+    def test_compound_literal(self):
+        # I41
+        dt = np.dtype([('a', 'i'), ('b', 'f'), ('c', '|S10')])
+        val = (1, 2.0, "Hello")
+
+        dset = self.f.create_dataset("ds", (10,), dt)
+        
+        dset[0] = val
+        
+        self.assert_(np.all(dset[0]==val))
 
 
 
diff --git a/h5py/tests/test_utils.py b/h5py/tests/test_utils.py
index 1c31508..a5de208 100644
--- a/h5py/tests/test_utils.py
+++ b/h5py/tests/test_utils.py
@@ -12,12 +12,12 @@
 import sys
 import numpy
 
-from common import HDF5TestCase, api_18
+from common import TestCasePlus, api_18
 
 from h5py import *
 from h5py import utils
 
-class TestUtils(HDF5TestCase):
+class TestUtils(TestCasePlus):
 
     def test_check_read(self):
         """ Check if it's possible to read from the NumPy array """

-- 
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