Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 39eb526

Browse files
committed
Allow fancy selection with an empty list
Closes h5pygh-1169 Closes h5pygh-473
1 parent 24dc880 commit 39eb526

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

docs/high/dataset.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,6 @@ dataset with shape (10, 10)::
280280

281281
The following restrictions exist:
282282

283-
* List selections may not be empty
284283
* Selection coordinates must be given in increasing order
285284
* Duplicate selections are ignored
286285
* Very long lists (> 1000 elements) may produce poor performance
@@ -296,6 +295,9 @@ list of points to select, so be careful when using it with large masks::
296295
>>> result.shape
297296
(49,)
298297

298+
.. versionchanged:: 2.10
299+
Selecting using an empty list is now allowed.
300+
This returns an array with length 0 in the relevant dimension.
299301

300302
.. _dataset_iter:
301303

h5py/_hl/selections.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -375,15 +375,24 @@ def __getitem__(self, args):
375375
if not all(len(x) == vectorlength for x in sequenceargs.values()):
376376
raise TypeError("All sequence arguments must have the same length %s" % sequenceargs)
377377

378-
# Now generate a vector of selection lists,
378+
# Now generate a vector of simple selection lists,
379379
# consisting only of slices and ints
380-
381-
argvector = []
382-
for idx in xrange(vectorlength):
380+
# e.g. [0:5, [1, 3]] is expanded to [[0:5, 1], [0:5, 3]]
381+
382+
if vectorlength > 0:
383+
argvector = []
384+
for idx in xrange(vectorlength):
385+
entry = list(args)
386+
for position, seq in six.iteritems(sequenceargs):
387+
entry[position] = seq[idx]
388+
argvector.append(entry)
389+
else:
390+
# Empty sequence: translate to empty slice to get the correct shape
391+
# [0:5, []] -> [0:5, 0:0]
383392
entry = list(args)
384-
for position, seq in six.iteritems(sequenceargs):
385-
entry[position] = seq[idx]
386-
argvector.append(entry)
393+
for position in sequenceargs:
394+
entry[position] = slice(0, 0)
395+
argvector = [entry]
387396

388397
# "OR" all these selection lists together to make the final selection
389398

@@ -400,9 +409,9 @@ def __getitem__(self, args):
400409
if idx in sequenceargs:
401410
mshape[idx] = len(sequenceargs[idx])
402411
elif scalar[idx]:
403-
mshape[idx] = 0
412+
mshape[idx] = -1
404413

405-
self._mshape = tuple(x for x in mshape if x != 0)
414+
self._mshape = tuple(x for x in mshape if x >= 0)
406415

407416
def broadcast(self, target_shape):
408417
if not target_shape == self.mshape:

0 commit comments

Comments
 (0)