[h5py] 181/455: Start new selections module

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Thu Jul 2 18:19:30 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 b7150e60881d331bcaeb4722a5398af0efa76dcd
Author: andrewcollette <andrew.collette at gmail.com>
Date:   Sun Dec 14 00:39:11 2008 +0000

    Start new selections module
---
 h5py/selections.py | 234 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 234 insertions(+)

diff --git a/h5py/selections.py b/h5py/selections.py
new file mode 100644
index 0000000..e55b22e
--- /dev/null
+++ b/h5py/selections.py
@@ -0,0 +1,234 @@
+
+"""
+    High-level access to HDF5 dataspace selections
+"""
+import numpy as np
+
+from h5py import h5s
+
+# Selection types for hyperslabs
+from h5py.h5s import SELECT_SET  as SET
+from h5py.h5s import SELECT_OR   as OR
+from h5py.h5s import SELECT_AND  as AND
+from h5py.h5s import SELECT_XOR  as XOR
+from h5py.h5s import SELECT_NOTB as NOTB
+from h5py.h5s import SELECT_NOTA as NOTA
+
+
+class Selection(object):
+
+    """
+        Base class for HDF5 dataspace selections
+    """
+
+    def __init__(self, shape):
+        shape = tuple(shape)
+        self._id = h5s.create_simple(shape, (None,)*len(shape))
+        self._shape = shape
+
+    @property
+    def shape(self):
+        return self._shape
+
+    @property
+    def nselect(self):
+        seltype = self._id.get_select_type()
+
+        if seltype == h5s.SEL_POINTS:
+            return self._id.get_select_npoints()
+
+        elif seltype == h5s.SEL_HYPERSLABS:
+            return self._id.get_select_hyper_nblocks()
+
+        elif seltype == h5s.SEL_ALL:
+            return np.product(self.shape)
+
+        elif seltype == h5s.SEL_NONE:
+            return 0
+
+        raise TypeError("Selection invalid")
+
+
+class PointSelection(Selection):
+
+    """
+        Represents a point-wise selection.
+    """
+
+    def _perform_selection(self, points, op):
+
+        points = np.asarray(points, order='C')
+        if len(points.shape) == 1:
+            points.shape = (1,points.shape[0])
+
+        if self._id.get_select_type() != h5s.SEL_POINTS:
+            op = h5s.SELECT_SET
+
+        self._id.select_elements(points, op)
+
+    def append(self, points):
+        self._perform_selection(points, h5s.SELECT_APPEND)
+
+    def prepend(self, points):
+        self._perform_selection(points, h5s.SELECT_PREPEND)
+
+
+class HyperSelection(Selection):
+
+    """
+        Represents multiple overlapping rectangular selections, combined
+        with set-like operators.
+
+        When created, the entire dataspace is selected.  To make
+        adjustments to the selection, use the standard NumPy slicing
+        syntax::
+
+            >>> sel = HyperSelection((10,20,20))  # Initially 10 x 20 x 20
+            >>> sel[:,5:15,:] = SET               # Now 10 x 10 x 20
+            >>> sel[0:5,:,:] = AND                # Now  5 x 10 x 10
+
+        Legal operators (in the h5py.selections module) are:
+            
+        SET
+            New selection, wiping out any old one
+        
+        OR (or True), AND, XOR
+            Logical OR/AND/XOR between new and old selection
+
+        NOTA
+            Select only regions in new selection which don't intersect the old
+
+        NOTB (or False)
+            Select only regions in old selection which don't intersect the new
+  
+    """
+
+    def __setitem__(self, args, op):
+
+        if not isinstance(args, tuple):
+            args = (args,)
+  
+        start, count, step = _handle_simple(args, self.shape)
+
+        if not op in (SET, OR, AND, XOR, NOTB, NOTA, True, False):
+            raise ValueError("Illegal selection operator")
+
+        if op is True:
+            op = OR
+        elif op is False:
+            op = NOTB
+
+        seltype == self._id.get_select_type()
+
+        if seltype == h5s.SEL_ALL:
+            self._id.select_hyperslab((0,)*len(self.shape), self.shape, op=h5s.SELECT_SET)
+        
+        elif seltype == h5s.SEL_NONE:
+            if op in (SET, OR, XOR, NOTA):
+                op = SET
+            else:
+                return
+
+        self._id.select_hyperslab(start, count, step, op=op)
+
+class FancySelection(HyperSelection):
+
+    """
+        Implements advanced, NumPy-style selection operations.
+
+        Indexing arguments may be ints, slices, lists of indecies, or
+        boolean arrays (1-D).  The only permitted operation is SET.
+    """
+    
+    pass
+
+def _handle_simple(args, shape):
+    """ Process a "simple" selection tuple, containing only slices and
+        integer objects.  Return is a 3-tuple with start, count, step tuples.
+
+        If "args" is shorter than "shape", the remaining axes are fully
+        selected.
+    """
+    if len(args) > len(shape):
+        raise TypeError("Argument sequence too long")
+    elif len(args) < len(shape):
+        args = args + (slice(None,None,None),)*(len(shape)-len(args))
+
+    def handle_arg(arg, length):
+        if isinstance(arg, slice):
+            return _translate_slice(arg, length)
+        try:
+            return _translate_int(int(arg), length)
+        except TypeError:
+            raise TypeError("Illegal index (must be a slice or number)")
+
+    start = []
+    count = []
+    step  = []
+
+    for a, length in zip(args, shape):
+        x,y,z = handle_arg(a, length)
+        start.append(x)
+        count.append(y)
+        step.append(z)
+
+    return tuple(start), tuple(count), tuple(step)
+
+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-1):
+        raise ValueError("Index out of range")
+
+    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.start, exp.stop, exp.step
+    start = 0 if start is None else int(start)
+    stop = length if stop is None else int(stop)
+    step = 1 if step is None else int(step)
+
+    if step < 1:
+        raise ValueError("Step must be >= 1 (got %d)" % step)
+    if stop == start:
+        raise ValueError("Zero-length selections are not allowed")
+    if stop < start:
+        raise ValueError("Reverse-order selections are not allowed")
+    if start < 0:
+        start = length+start
+    if stop < 0:
+        stop = length+stop
+
+    if not 0 < start < (length-1):
+        raise ValueError("Start index out of range (0-%d)" % length-1)
+    if not 1 < stop < length:
+        raise ValueError("Stop index out of range (1-%d)" % length)
+
+    count = (stop-start)//step
+    if (stop-start) % step != 0:
+        count += 1
+
+    if start+count > length:
+        raise ValueError("Selection out of bounds (%d; axis has %d)" % (start+count,length))
+
+    return start, count, step
+
+
+
+
+
+
+
+
+
+
+

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