def filter(self, boolarray): if boolarray.squeeze().ndim > 1: raise ValueError("filter can only use array with one dimension") if boolarray.dtype != np.dtype(bool): if boolarray.dims[0].name not in self.ivardata: out = self.copy() msg = "Filter warning: DataBlock does not contain dimension %r" msg = msg % boolarray.dims[0] warn(msg) return out localdim = self.ivardata[boolarray.dims[0].name] intersection = set(localdim.data).intersection(boolarray) barray = boolarray.astype(bool) barray[...] = False for f in intersection: barray[boolarray == f] = True boolarray = barray out = DataBlock() out.blockname = self.blockname out.comments = self.comments out.xname = self.xname olddim = boolarray.dims[0] newdim = olddim.__class__(olddim, data=olddim.data[boolarray]) for v in self.vardata.keys(): data = self.vardata[v].view() try: i = data.dims_index(boolarray.dims[0]) except IndexError: # This variable does not sweep in boolarray dim out[v] = data continue newinfo = list(data.dims) reorder = list(range(data.ndim)) del newinfo[i] del reorder[i] reorder = tuple([i] + reorder) newinfo = tuple([newdim] + newinfo) data = data.transpose(*reorder) utdata = hfarray(array(data)[boolarray], dims=newinfo, unit=data.unit, outputformat=data.outputformat) dimorder = [x.name for x in self.vardata[v].dims] utdata = utdata.reorder_dimensions(*dimorder) out[v] = utdata out.xname = self.xname return out
def __new__(subtype, data, dims=None, dtype=None, copy=True, order=None, subok=False, ndmin=0, unit=None, info=None, Z0=None): if info is not None: warn("_MultiPortArray, use dims not info") if dims is not None: raise ValueError("Can not specify both dims and info") dims = info info = None data = np.asanyarray(data) if dims is None: if hasattr(data, 'dims'): dims = tuple(data.dims) elif len(data.shape) <= len(subtype.default_dim) + 2: defaultdims = (subtype.default_dim) shape = data.shape[:-2] dims = tuple( x.__class__(x.name, range(size)) for (x, size) in zip(defaultdims, shape)) dims = dims + (DimMatrix_i( "i", data.shape[-2]), DimMatrix_j("j", data.shape[-1])) out = _hfarray.__new__(subtype, data, dims=dims, dtype=dtype, copy=copy, order=order, subok=subok, ndmin=ndmin, unit=unit) return out
def sort(self, dim): dim = hfarray(dim, copy=False).squeeze() if dim.ndim > 1: msg = "sort can only get sort direction from array with"\ " one dimension" raise ValueError(msg) else: dimname = dim.dims[0].name if dimname not in self.ivardata: msg = "Sort warning: DataBlock does not contain dimension %r" msg = msg % dimname warn(msg) out = self.copy() return out sortorder = dim.argsort() out = DataBlock() out.blockname = self.blockname out.comments = self.comments out.xname = self.xname for v in self.vardata.keys()[:]: data = self.vardata[v].view() try: i = data.dims_index(dimname) out[v] = data.take(sortorder, i) except IndexError: # This variable does not contain sort direction out[v] = data continue olddim = self.ivardata[dimname] out.xname = self.xname data = self[dimname][sortorder] out.replace_dim(olddim, olddim.__class__(olddim, data=data)) return out
def __new__(subtype, data, dims=None, dtype=None, copy=True, order=None, subok=False, ndmin=0, unit=None, info=None, Z0=None): if info is not None: warn("_MultiPortArray, use dims not info") if dims is not None: raise ValueError("Can not specify both dims and info") dims = info info = None data = np.asanyarray(data) if dims is None: if hasattr(data, 'dims'): dims = tuple(data.dims) elif len(data.shape) <= len(subtype.default_dim) + 2: defaultdims = (subtype.default_dim) shape = data.shape[:-2] dims = tuple(x.__class__(x.name, range(size)) for (x, size) in zip(defaultdims, shape)) dims = dims + (DimMatrix_i("i", data.shape[-2]), DimMatrix_j("j", data.shape[-1])) out = _hfarray.__new__(subtype, data, dims=dims, dtype=dtype, copy=copy, order=order, subok=subok, ndmin=ndmin, unit=unit) return out