def __getitem__(self, spec): _get = super(Dimension, self).__getitem__ if spec is None: return _get(slice(None)) try: if iss.issequence(spec): return tuple(map(_get, spec)) if not callable(spec): return _get(spec) except: raise IndexError, 'invalid index for dimension "%s" (%r)' \ % (self.name, spec) try: return tuple(v for i, v in enumerate(self) if spec(i, v)) except Exception, e: raise type(e), 'predicate error for dimension "%s" (%s)' \ % (self.name, e)
def mu_sigma(hbrick, axes, newdim=(u'stat', (u'mean', u'stddev'))): """Compute mean and stddev over all the specified axes. hbrick: an (n-dimensional) HyperBrick object; axes: either a k-tuple of integers or strings, or a string describing a space-separated list of k dimension names or dimension indices (integers); newdim: a pair whose first element should be a string and second element a 2-tuple of strings; this pair serves as the specification (name and levels) for the last dimension of the returned HyperBrick; default: (u'stat', (u'mean', u'stddev')) returns: a HyperBrick object, whose _data attribute contains the ndarray computed by _mu_sigma(hbrick._data, indices) (where indices holds the integer equivalents of the axes specified in the axes parameter); the first n-k dimensions of the returned HyperBrick are the same as the dimensions of hbrick that were not specified in the axes parameter, and whose last dimension is a 2-level dimension; the hyperslab corresponding to the first and second levels of this last dimension are, respectively, the computed means and stddevs; the ordering of the first n-k (i.e. the kept) dimensions is the same as it was in hbrick. """ # the next line allows us to pass several named dimensions as a # single space-separated string. if not iss.issequence(axes): axes = axes.split() # we convert (as necessary) the dimensions specified in axes to # numerical indices; axes = tuple(map(hbrick._toaxis, axes)) data = _mu_sigma(hbrick._data, axes) newdims = [hbrick._fromaxis(i) for i in range(hbrick.ndim) if i not in set(axes)] + [newdim] return hb.HyperBrick(data, newdims)