Spaces:
Running
Running
File size: 14,290 Bytes
122d3ff |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 |
# This file is part of h5py, a Python interface to the HDF5 library.
#
# http://www.h5py.org
#
# Copyright 2008-2013 Andrew Collette and contributors
#
# License: Standard 3-clause BSD; see "license.txt" for full license terms
# and contributor agreement.
"""
Dataset slicing test module.
Tests all supported slicing operations, including read/write and
broadcasting operations. Does not test type conversion except for
corner cases overlapping with slicing; for example, when selecting
specific fields of a compound type.
"""
import numpy as np
from .common import ut, TestCase
import h5py
from h5py import h5s, h5t, h5d
from h5py import File, MultiBlockSlice
class BaseSlicing(TestCase):
def setUp(self):
self.f = File(self.mktemp(), 'w')
def tearDown(self):
if self.f:
self.f.close()
class TestSingleElement(BaseSlicing):
"""
Feature: Retrieving a single element works with NumPy semantics
"""
def test_single_index(self):
""" Single-element selection with [index] yields array scalar """
dset = self.f.create_dataset('x', (1,), dtype='i1')
out = dset[0]
self.assertIsInstance(out, np.int8)
def test_single_null(self):
""" Single-element selection with [()] yields ndarray """
dset = self.f.create_dataset('x', (1,), dtype='i1')
out = dset[()]
self.assertIsInstance(out, np.ndarray)
self.assertEqual(out.shape, (1,))
def test_scalar_index(self):
""" Slicing with [...] yields scalar ndarray """
dset = self.f.create_dataset('x', shape=(), dtype='f')
out = dset[...]
self.assertIsInstance(out, np.ndarray)
self.assertEqual(out.shape, ())
def test_scalar_null(self):
""" Slicing with [()] yields array scalar """
dset = self.f.create_dataset('x', shape=(), dtype='i1')
out = dset[()]
self.assertIsInstance(out, np.int8)
def test_compound(self):
""" Compound scalar is numpy.void, not tuple (issue 135) """
dt = np.dtype([('a','i4'),('b','f8')])
v = np.ones((4,), dtype=dt)
dset = self.f.create_dataset('foo', (4,), data=v)
self.assertEqual(dset[0], v[0])
self.assertIsInstance(dset[0], np.void)
class TestObjectIndex(BaseSlicing):
"""
Feature: numpy.object_ subtypes map to real Python objects
"""
def test_reference(self):
""" Indexing a reference dataset returns a h5py.Reference instance """
dset = self.f.create_dataset('x', (1,), dtype=h5py.ref_dtype)
dset[0] = self.f.ref
self.assertEqual(type(dset[0]), h5py.Reference)
def test_regref(self):
""" Indexing a region reference dataset returns a h5py.RegionReference
"""
dset1 = self.f.create_dataset('x', (10,10))
regref = dset1.regionref[...]
dset2 = self.f.create_dataset('y', (1,), dtype=h5py.regionref_dtype)
dset2[0] = regref
self.assertEqual(type(dset2[0]), h5py.RegionReference)
def test_reference_field(self):
""" Compound types of which a reference is an element work right """
dt = np.dtype([('a', 'i'),('b', h5py.ref_dtype)])
dset = self.f.create_dataset('x', (1,), dtype=dt)
dset[0] = (42, self.f['/'].ref)
out = dset[0]
self.assertEqual(type(out[1]), h5py.Reference) # isinstance does NOT work
def test_scalar(self):
""" Indexing returns a real Python object on scalar datasets """
dset = self.f.create_dataset('x', (), dtype=h5py.ref_dtype)
dset[()] = self.f.ref
self.assertEqual(type(dset[()]), h5py.Reference)
def test_bytestr(self):
""" Indexing a byte string dataset returns a real python byte string
"""
dset = self.f.create_dataset('x', (1,), dtype=h5py.string_dtype(encoding='ascii'))
dset[0] = b"Hello there!"
self.assertEqual(type(dset[0]), bytes)
class TestSimpleSlicing(TestCase):
"""
Feature: Simple NumPy-style slices (start:stop:step) are supported.
"""
def setUp(self):
self.f = File(self.mktemp(), 'w')
self.arr = np.arange(10)
self.dset = self.f.create_dataset('x', data=self.arr)
def tearDown(self):
if self.f:
self.f.close()
def test_negative_stop(self):
""" Negative stop indexes work as they do in NumPy """
self.assertArrayEqual(self.dset[2:-2], self.arr[2:-2])
def test_write(self):
"""Assigning to a 1D slice of a 2D dataset
"""
dset = self.f.create_dataset('x2', (10, 2))
x = np.zeros((10, 1))
dset[:, 0] = x[:, 0]
with self.assertRaises(TypeError):
dset[:, 1] = x
class TestArraySlicing(BaseSlicing):
"""
Feature: Array types are handled appropriately
"""
def test_read(self):
""" Read arrays tack array dimensions onto end of shape tuple """
dt = np.dtype('(3,)f8')
dset = self.f.create_dataset('x',(10,),dtype=dt)
self.assertEqual(dset.shape, (10,))
self.assertEqual(dset.dtype, dt)
# Full read
out = dset[...]
self.assertEqual(out.dtype, np.dtype('f8'))
self.assertEqual(out.shape, (10,3))
# Single element
out = dset[0]
self.assertEqual(out.dtype, np.dtype('f8'))
self.assertEqual(out.shape, (3,))
# Range
out = dset[2:8:2]
self.assertEqual(out.dtype, np.dtype('f8'))
self.assertEqual(out.shape, (3,3))
def test_write_broadcast(self):
""" Array fill from constant is not supported (issue 211).
"""
dt = np.dtype('(3,)i')
dset = self.f.create_dataset('x', (10,), dtype=dt)
with self.assertRaises(TypeError):
dset[...] = 42
def test_write_element(self):
""" Write a single element to the array
Issue 211.
"""
dt = np.dtype('(3,)f8')
dset = self.f.create_dataset('x', (10,), dtype=dt)
data = np.array([1,2,3.0])
dset[4] = data
out = dset[4]
self.assertTrue(np.all(out == data))
def test_write_slices(self):
""" Write slices to array type """
dt = np.dtype('(3,)i')
data1 = np.ones((2,), dtype=dt)
data2 = np.ones((4,5), dtype=dt)
dset = self.f.create_dataset('x', (10,9,11), dtype=dt)
dset[0,0,2:4] = data1
self.assertArrayEqual(dset[0,0,2:4], data1)
dset[3, 1:5, 6:11] = data2
self.assertArrayEqual(dset[3, 1:5, 6:11], data2)
def test_roundtrip(self):
""" Read the contents of an array and write them back
Issue 211.
"""
dt = np.dtype('(3,)f8')
dset = self.f.create_dataset('x', (10,), dtype=dt)
out = dset[...]
dset[...] = out
self.assertTrue(np.all(dset[...] == out))
class TestZeroLengthSlicing(BaseSlicing):
"""
Slices resulting in empty arrays
"""
def test_slice_zero_length_dimension(self):
""" Slice a dataset with a zero in its shape vector
along the zero-length dimension """
for i, shape in enumerate([(0,), (0, 3), (0, 2, 1)]):
dset = self.f.create_dataset('x%d'%i, shape, dtype=int, maxshape=(None,)*len(shape))
self.assertEqual(dset.shape, shape)
out = dset[...]
self.assertIsInstance(out, np.ndarray)
self.assertEqual(out.shape, shape)
out = dset[:]
self.assertIsInstance(out, np.ndarray)
self.assertEqual(out.shape, shape)
if len(shape) > 1:
out = dset[:, :1]
self.assertIsInstance(out, np.ndarray)
self.assertEqual(out.shape[:2], (0, 1))
def test_slice_other_dimension(self):
""" Slice a dataset with a zero in its shape vector
along a non-zero-length dimension """
for i, shape in enumerate([(3, 0), (1, 2, 0), (2, 0, 1)]):
dset = self.f.create_dataset('x%d'%i, shape, dtype=int, maxshape=(None,)*len(shape))
self.assertEqual(dset.shape, shape)
out = dset[:1]
self.assertIsInstance(out, np.ndarray)
self.assertEqual(out.shape, (1,)+shape[1:])
def test_slice_of_length_zero(self):
""" Get a slice of length zero from a non-empty dataset """
for i, shape in enumerate([(3,), (2, 2,), (2, 1, 5)]):
dset = self.f.create_dataset('x%d'%i, data=np.zeros(shape, int), maxshape=(None,)*len(shape))
self.assertEqual(dset.shape, shape)
out = dset[1:1]
self.assertIsInstance(out, np.ndarray)
self.assertEqual(out.shape, (0,)+shape[1:])
class TestFieldNames(BaseSlicing):
"""
Field names for read & write
"""
dt = np.dtype([('a', 'f'), ('b', 'i'), ('c', 'f4')])
data = np.ones((100,), dtype=dt)
def setUp(self):
BaseSlicing.setUp(self)
self.dset = self.f.create_dataset('x', (100,), dtype=self.dt)
self.dset[...] = self.data
def test_read(self):
""" Test read with field selections """
self.assertArrayEqual(self.dset['a'], self.data['a'])
def test_unicode_names(self):
""" Unicode field names for for read and write """
self.assertArrayEqual(self.dset['a'], self.data['a'])
self.dset['a'] = 42
data = self.data.copy()
data['a'] = 42
self.assertArrayEqual(self.dset['a'], data['a'])
def test_write(self):
""" Test write with field selections """
data2 = self.data.copy()
data2['a'] *= 2
self.dset['a'] = data2
self.assertTrue(np.all(self.dset[...] == data2))
data2['b'] *= 4
self.dset['b'] = data2
self.assertTrue(np.all(self.dset[...] == data2))
data2['a'] *= 3
data2['c'] *= 3
self.dset['a','c'] = data2
self.assertTrue(np.all(self.dset[...] == data2))
def test_write_noncompound(self):
""" Test write with non-compound source (single-field) """
data2 = self.data.copy()
data2['b'] = 1.0
self.dset['b'] = 1.0
self.assertTrue(np.all(self.dset[...] == data2))
class TestMultiBlockSlice(BaseSlicing):
def setUp(self):
super().setUp()
self.arr = np.arange(10)
self.dset = self.f.create_dataset('x', data=self.arr)
def test_default(self):
# Default selects entire dataset as one block
mbslice = MultiBlockSlice()
self.assertEqual(mbslice.indices(10), (0, 1, 10, 1))
np.testing.assert_array_equal(self.dset[mbslice], self.arr)
def test_default_explicit(self):
mbslice = MultiBlockSlice(start=0, count=10, stride=1, block=1)
self.assertEqual(mbslice.indices(10), (0, 1, 10, 1))
np.testing.assert_array_equal(self.dset[mbslice], self.arr)
def test_start(self):
mbslice = MultiBlockSlice(start=4)
self.assertEqual(mbslice.indices(10), (4, 1, 6, 1))
np.testing.assert_array_equal(self.dset[mbslice], np.array([4, 5, 6, 7, 8, 9]))
def test_count(self):
mbslice = MultiBlockSlice(count=7)
self.assertEqual(mbslice.indices(10), (0, 1, 7, 1))
np.testing.assert_array_equal(
self.dset[mbslice], np.array([0, 1, 2, 3, 4, 5, 6])
)
def test_count_more_than_length_error(self):
mbslice = MultiBlockSlice(count=11)
with self.assertRaises(ValueError):
mbslice.indices(10)
def test_stride(self):
mbslice = MultiBlockSlice(stride=2)
self.assertEqual(mbslice.indices(10), (0, 2, 5, 1))
np.testing.assert_array_equal(self.dset[mbslice], np.array([0, 2, 4, 6, 8]))
def test_stride_zero_error(self):
with self.assertRaises(ValueError):
# This would cause a ZeroDivisionError if not caught
MultiBlockSlice(stride=0, block=0).indices(10)
def test_stride_block_equal(self):
mbslice = MultiBlockSlice(stride=2, block=2)
self.assertEqual(mbslice.indices(10), (0, 2, 5, 2))
np.testing.assert_array_equal(self.dset[mbslice], self.arr)
def test_block_more_than_stride_error(self):
with self.assertRaises(ValueError):
MultiBlockSlice(block=3)
with self.assertRaises(ValueError):
MultiBlockSlice(stride=2, block=3)
def test_stride_more_than_block(self):
mbslice = MultiBlockSlice(stride=3, block=2)
self.assertEqual(mbslice.indices(10), (0, 3, 3, 2))
np.testing.assert_array_equal(self.dset[mbslice], np.array([0, 1, 3, 4, 6, 7]))
def test_block_overruns_extent_error(self):
# If fully described then must fit within extent
mbslice = MultiBlockSlice(start=2, count=2, stride=5, block=4)
with self.assertRaises(ValueError):
mbslice.indices(10)
def test_fully_described(self):
mbslice = MultiBlockSlice(start=1, count=2, stride=5, block=4)
self.assertEqual(mbslice.indices(10), (1, 5, 2, 4))
np.testing.assert_array_equal(
self.dset[mbslice], np.array([1, 2, 3, 4, 6, 7, 8, 9])
)
def test_count_calculated(self):
# If not given, count should be calculated to select as many full blocks as possible
mbslice = MultiBlockSlice(start=1, stride=3, block=2)
self.assertEqual(mbslice.indices(10), (1, 3, 3, 2))
np.testing.assert_array_equal(self.dset[mbslice], np.array([1, 2, 4, 5, 7, 8]))
def test_zero_count_calculated_error(self):
# In this case, there is no possible count to select even one block, so error
mbslice = MultiBlockSlice(start=8, stride=4, block=3)
with self.assertRaises(ValueError):
mbslice.indices(10)
|