/usr/lib/python3/dist-packages/h5py/_hl/selections.py is in python3-h5py 2.7.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 | # 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.
# We use __getitem__ side effects, which pylint doesn't like.
# pylint: disable=pointless-statement
"""
High-level access to HDF5 dataspace selections
"""
from __future__ import absolute_import
import six
from six.moves import xrange # pylint: disable=redefined-builtin
import numpy as np
from .. import h5s, h5r
def select(shape, args, dsid):
""" High-level routine to generate a selection from arbitrary arguments
to __getitem__. The arguments should be the following:
shape
Shape of the "source" dataspace.
args
Either a single argument or a tuple of arguments. See below for
supported classes of argument.
dsid
A h5py.h5d.DatasetID instance representing the source dataset.
Argument classes:
Single Selection instance
Returns the argument.
numpy.ndarray
Must be a boolean mask. Returns a PointSelection instance.
RegionReference
Returns a Selection instance.
Indices, slices, ellipses only
Returns a SimpleSelection instance
Indices, slices, ellipses, lists or boolean index arrays
Returns a FancySelection instance.
"""
if not isinstance(args, tuple):
args = (args,)
# "Special" indexing objects
if len(args) == 1:
arg = args[0]
if isinstance(arg, Selection):
if arg.shape != shape:
raise TypeError("Mismatched selection shape")
return arg
elif isinstance(arg, np.ndarray):
sel = PointSelection(shape)
sel[arg]
return sel
elif isinstance(arg, h5r.RegionReference):
sid = h5r.get_region(arg, dsid)
if shape != sid.shape:
raise TypeError("Reference shape does not match dataset shape")
return Selection(shape, spaceid=sid)
for a in args:
if not isinstance(a, slice) and a is not Ellipsis:
try:
int(a)
if isinstance(a, np.ndarray) and a.shape == (1,):
raise Exception()
except Exception:
sel = FancySelection(shape)
sel[args]
return sel
sel = SimpleSelection(shape)
sel[args]
return sel
class _RegionProxy(object):
"""
Thin proxy object which takes __getitem__-style index arguments and
produces RegionReference objects. Example:
>>> dset = myfile['dataset']
>>> myref = dset.regionref[0:100,20:30]
>>> data = dset[myref]
"""
def __init__(self, dsid):
""" Supply a h5py.h5d.DatasetID instance """
self.id = dsid
def __getitem__(self, args):
""" Takes arbitrary selection terms and produces a RegionReference
object. Selection must be compatible with the dataset.
"""
selection = select(self.id.shape, args, self.id)
return h5r.create(self.id, '.', h5r.DATASET_REGION, selection.id)
class Selection(object):
"""
Base class for HDF5 dataspace selections. Subclasses support the
"selection protocol", which means they have at least the following
members:
__init__(shape) => Create a new selection on "shape"-tuple
__getitem__(args) => Perform a selection with the range specified.
What args are allowed depends on the
particular subclass in use.
id (read-only) => h5py.h5s.SpaceID instance
shape (read-only) => The shape of the dataspace.
mshape (read-only) => The shape of the selection region.
Not guaranteed to fit within "shape", although
the total number of points is less than
product(shape).
nselect (read-only) => Number of selected points. Always equal to
product(mshape).
broadcast(target_shape) => Return an iterable which yields dataspaces
for read, based on target_shape.
The base class represents "unshaped" selections (1-D).
"""
def __init__(self, shape, spaceid=None):
""" Create a selection. Shape may be None if spaceid is given. """
if spaceid is not None:
self._id = spaceid
self._shape = spaceid.shape
else:
shape = tuple(shape)
self._shape = shape
self._id = h5s.create_simple(shape, (h5s.UNLIMITED,)*len(shape))
self._id.select_all()
@property
def id(self):
""" SpaceID instance """
return self._id
@property
def shape(self):
""" Shape of whole dataspace """
return self._shape
@property
def nselect(self):
""" Number of elements currently selected """
return self._id.get_select_npoints()
@property
def mshape(self):
""" Shape of selection (always 1-D for this class) """
return (self.nselect,)
def broadcast(self, target_shape):
""" Get an iterable for broadcasting """
if np.product(target_shape) != self.nselect:
raise TypeError("Broadcasting is not supported for point-wise selections")
yield self._id
def __getitem__(self, args):
raise NotImplementedError("This class does not support indexing")
class PointSelection(Selection):
"""
Represents a point-wise selection. You can supply sequences of
points to the three methods append(), prepend() and set(), or a
single boolean array to __getitem__.
"""
def _perform_selection(self, points, op):
""" Internal method which actually performs the selection """
points = np.asarray(points, order='C', dtype='u8')
if len(points.shape) == 1:
points.shape = (1,points.shape[0])
if self._id.get_select_type() != h5s.SEL_POINTS:
op = h5s.SELECT_SET
if len(points) == 0:
self._id.select_none()
else:
self._id.select_elements(points, op)
def __getitem__(self, arg):
""" Perform point-wise selection from a NumPy boolean array """
if not (isinstance(arg, np.ndarray) and arg.dtype.kind == 'b'):
raise TypeError("PointSelection __getitem__ only works with bool arrays")
if not arg.shape == self.shape:
raise TypeError("Boolean indexing array has incompatible shape")
points = np.transpose(arg.nonzero())
self.set(points)
return self
def append(self, points):
""" Add the sequence of points to the end of the current selection """
self._perform_selection(points, h5s.SELECT_APPEND)
def prepend(self, points):
""" Add the sequence of points to the beginning of the current selection """
self._perform_selection(points, h5s.SELECT_PREPEND)
def set(self, points):
""" Replace the current selection with the given sequence of points"""
self._perform_selection(points, h5s.SELECT_SET)
class SimpleSelection(Selection):
""" A single "rectangular" (regular) selection composed of only slices
and integer arguments. Can participate in broadcasting.
"""
@property
def mshape(self):
""" Shape of current selection """
return self._mshape
def __init__(self, shape, *args, **kwds):
Selection.__init__(self, shape, *args, **kwds)
rank = len(self.shape)
self._sel = ((0,)*rank, self.shape, (1,)*rank, (False,)*rank)
self._mshape = self.shape
def __getitem__(self, args):
if not isinstance(args, tuple):
args = (args,)
if self.shape == ():
if len(args) > 0 and args[0] not in (Ellipsis, ()):
raise TypeError("Invalid index for scalar dataset (only ..., () allowed)")
self._id.select_all()
return self
start, count, step, scalar = _handle_simple(self.shape,args)
self._id.select_hyperslab(start, count, step)
self._sel = (start, count, step, scalar)
self._mshape = tuple(x for x, y in zip(count, scalar) if not y)
return self
def broadcast(self, target_shape):
""" Return an iterator over target dataspaces for broadcasting.
Follows the standard NumPy broadcasting rules against the current
selection shape (self.mshape).
"""
if self.shape == ():
if np.product(target_shape) != 1:
raise TypeError("Can't broadcast %s to scalar" % target_shape)
self._id.select_all()
yield self._id
return
start, count, step, scalar = self._sel
rank = len(count)
target = list(target_shape)
tshape = []
for idx in xrange(1,rank+1):
if len(target) == 0 or scalar[-idx]: # Skip scalar axes
tshape.append(1)
else:
t = target.pop()
if t == 1 or count[-idx] == t:
tshape.append(t)
else:
raise TypeError("Can't broadcast %s -> %s" % (target_shape, count))
tshape.reverse()
tshape = tuple(tshape)
chunks = tuple(x//y for x, y in zip(count, tshape))
nchunks = int(np.product(chunks))
if nchunks == 1:
yield self._id
else:
sid = self._id.copy()
sid.select_hyperslab((0,)*rank, tshape, step)
for idx in xrange(nchunks):
offset = tuple(x*y*z + s for x, y, z, s in zip(np.unravel_index(idx, chunks), tshape, step, start))
sid.offset_simple(offset)
yield sid
class FancySelection(Selection):
"""
Implements advanced NumPy-style selection operations in addition to
the standard slice-and-int behavior.
Indexing arguments may be ints, slices, lists of indicies, or
per-axis (1D) boolean arrays.
Broadcasting is not supported for these selections.
"""
@property
def mshape(self):
return self._mshape
def __init__(self, shape, *args, **kwds):
Selection.__init__(self, shape, *args, **kwds)
self._mshape = self.shape
def __getitem__(self, args):
if not isinstance(args, tuple):
args = (args,)
args = _expand_ellipsis(args, len(self.shape))
# First build up a dictionary of (position:sequence) pairs
sequenceargs = {}
for idx, arg in enumerate(args):
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:
list_arg = list(arg)
adjacent = zip(list_arg[:-1], list_arg[1:])
if any(fst >= snd for fst, snd in adjacent):
raise TypeError("Indexing elements must be in increasing order")
if len(sequenceargs) > 1:
raise TypeError("Only one indexing vector or array is currently allowed for advanced selection")
if len(sequenceargs) == 0:
raise TypeError("Advanced selection inappropriate")
vectorlength = len(list(sequenceargs.values())[0])
if not all(len(x) == vectorlength for x in sequenceargs.values()):
raise TypeError("All sequence arguments must have the same length %s" % sequenceargs)
# Now generate a vector of selection lists,
# consisting only of slices and ints
argvector = []
for idx in xrange(vectorlength):
entry = list(args)
for position, seq in six.iteritems(sequenceargs):
entry[position] = seq[idx]
argvector.append(entry)
# "OR" all these selection lists together to make the final selection
self._id.select_none()
for idx, vector in enumerate(argvector):
start, count, step, scalar = _handle_simple(self.shape, vector)
self._id.select_hyperslab(start, count, step, op=h5s.SELECT_OR)
# Final shape excludes scalars, except where
# they correspond to sequence entries
mshape = list(count)
for idx in xrange(len(mshape)):
if idx in sequenceargs:
mshape[idx] = len(sequenceargs[idx])
elif scalar[idx]:
mshape[idx] = 0
self._mshape = tuple(x for x in mshape if x != 0)
def broadcast(self, target_shape):
if not target_shape == self.mshape:
raise TypeError("Broadcasting is not supported for complex selections")
yield self._id
def _expand_ellipsis(args, rank):
""" Expand ellipsis objects and fill in missing axes.
"""
n_el = sum(1 for arg in args if arg is Ellipsis)
if n_el > 1:
raise ValueError("Only one ellipsis may be used.")
elif n_el == 0 and len(args) != rank:
args = args + (Ellipsis,)
final_args = []
n_args = len(args)
for arg in args:
if arg is Ellipsis:
final_args.extend( (slice(None,None,None),)*(rank-n_args+1) )
else:
final_args.append(arg)
if len(final_args) > rank:
raise TypeError("Argument sequence too long")
return final_args
def _handle_simple(shape, args):
""" Process a "simple" selection tuple, containing only slices and
integer objects. Return is a 4-tuple with tuples for start,
count, step, and a flag which tells if the axis is a "scalar"
selection (indexed by an integer).
If "args" is shorter than "shape", the remaining axes are fully
selected.
"""
args = _expand_ellipsis(args, len(shape))
start = []
count = []
step = []
scalar = []
for arg, length in zip(args, shape):
if isinstance(arg, slice):
x,y,z = _translate_slice(arg, length)
s = False
else:
try:
x,y,z = _translate_int(int(arg), length)
s = True
except TypeError:
raise TypeError('Illegal index "%s" (must be a slice or number)' % arg)
start.append(x)
count.append(y)
step.append(z)
scalar.append(s)
return tuple(start), tuple(count), tuple(step), tuple(scalar)
def _translate_int(exp, length):
""" Given an integer index, return a 3-tuple
(start, count, step)
for hyperslab selection
"""
if exp < 0:
exp = length+exp
if not 0<=exp<length:
raise ValueError("Index (%s) out of range (0-%s)" % (exp, length-1))
return exp, 1, 1
def _translate_slice(exp, length):
""" Given a slice object, return a 3-tuple
(start, count, step)
for use with the hyperslab selection routines
"""
start, stop, step = exp.indices(length)
# Now if step > 0, then start and stop are in [0, length];
# if step < 0, they are in [-1, length - 1] (Python 2.6b2 and later;
# Python issue 3004).
if step < 1:
raise ValueError("Step must be >= 1 (got %d)" % step)
if stop < start:
raise ValueError("Reverse-order selections are not allowed")
count = 1 + (stop - start - 1) // step
return start, count, step
def guess_shape(sid):
""" Given a dataspace, try to deduce the shape of the selection.
Returns one of:
* A tuple with the selection shape, same length as the dataspace
* A 1D selection shape for point-based and multiple-hyperslab selections
* None, for unselected scalars and for NULL dataspaces
"""
sel_class = sid.get_simple_extent_type() # Dataspace class
sel_type = sid.get_select_type() # Flavor of selection in use
if sel_class == h5s.NULL:
# NULL dataspaces don't support selections
return None
elif sel_class == h5s.SCALAR:
# NumPy has no way of expressing empty 0-rank selections, so we use None
if sel_type == h5s.SEL_NONE: return None
if sel_type == h5s.SEL_ALL: return tuple()
elif sel_class != h5s.SIMPLE:
raise TypeError("Unrecognized dataspace class %s" % sel_class)
# We have a "simple" (rank >= 1) dataspace
N = sid.get_select_npoints()
rank = len(sid.shape)
if sel_type == h5s.SEL_NONE:
return (0,)*rank
elif sel_type == h5s.SEL_ALL:
return sid.shape
elif sel_type == h5s.SEL_POINTS:
# Like NumPy, point-based selections yield 1D arrays regardless of
# the dataspace rank
return (N,)
elif sel_type != h5s.SEL_HYPERSLABS:
raise TypeError("Unrecognized selection method %s" % sel_type)
# We have a hyperslab-based selection
if N == 0:
return (0,)*rank
bottomcorner, topcorner = (np.array(x) for x in sid.get_select_bounds())
# Shape of full selection box
boxshape = topcorner - bottomcorner + np.ones((rank,))
def get_n_axis(sid, axis):
""" Determine the number of elements selected along a particular axis.
To do this, we "mask off" the axis by making a hyperslab selection
which leaves only the first point along the axis. For a 2D dataset
with selection box shape (X, Y), for axis 1, this would leave a
selection of shape (X, 1). We count the number of points N_leftover
remaining in the selection and compute the axis selection length by
N_axis = N/N_leftover.
"""
if(boxshape[axis]) == 1:
return 1
start = bottomcorner.copy()
start[axis] += 1
count = boxshape.copy()
count[axis] -= 1
# Throw away all points along this axis
masked_sid = sid.copy()
masked_sid.select_hyperslab(tuple(start), tuple(count), op=h5s.SELECT_NOTB)
N_leftover = masked_sid.get_select_npoints()
return N//N_leftover
shape = tuple(get_n_axis(sid, x) for x in xrange(rank))
if np.product(shape) != N:
# This means multiple hyperslab selections are in effect,
# so we fall back to a 1D shape
return (N,)
return shape
|