def hyper(self, dimnames, replacedim, indexed=False, all=True): if isinstance(replacedim, string_types): replacedim = self.ivardata[replacedim] out = DataBlock() out.blockname = self.blockname dims = [] sortdata = [] for x in dimnames: if indexed: newname = "%s_index" % x else: newname = x dims.append( DimSweep(newname, stable_uniq(self[x]), unit=self[x].unit, outputformat=self[x].outputformat)) sortdata.append(self[x]) dims = tuple(dims) dims_shape = tuple(len(x.data) for x in dims) sortdata.append(range(len(sortdata[0]))) sortorder = sorted(zip(*sortdata)) sortorderidx = [x[-1] for x in sortorder] for dim in dims: out.ivardata[dim.name] = dim for k, v in self.vardata.items(): if replacedim not in v.dims: if all: out[k] = v 1 + 0 # coverage.py bug necessary to get coverage for # continue on next line continue if k in out.ivardata: continue i = v.dims_index(replacedim) v = hfarray(v.take(sortorderidx, axis=i), copy=False, order="C") new_shape = v.shape[:i] + dims_shape + v.shape[i + 1:] v.shape = new_shape v.dims = v.dims[:i] + dims + v.dims[i + 1:] out[k] = v for k, v in self.ivardata.items(): if k not in out and k != replacedim.name: out[k] = v return out
def test_4(self): a = DimSweep(self.dim, name="P") self.assertEqual(a.name, "P") self.assertEqual(a.unit, "Hz") self.assertAllclose(a.data, range(10))
def test_3(self): a = DimSweep(self.dim, unit="m") self.assertEqual(a.name, "a") self.assertEqual(a.unit, "m") self.assertAllclose(a.data, range(10))
def test_str(self): d = DimSweep("a", ["a"]) self.assertEqual(d.outputformat, "%s")
def test_num(self): d = DimSweep("a", [1., 2., 3]) self.assertEqual(d.outputformat, "%.16e")
def test_7(self): a = DimSweep("P", data=np.array([0, 1, 2]), unit="W") self.assertEqual(a.name, "P") self.assertEqual(a.unit, "W") self.assertAllclose(a.data, range(3))
outputformat = dims[0].outputformat return _hfarray.__new__(subtype, data, dims=dims, dtype=dtype, copy=copy, order=order, subok=subok, ndmin=ndmin, unit=unit, outputformat=outputformat, info=info) class ValueArray(hfarray): """This class is included, for compatibility reasons. Do not use in new code.""" pass def make_matrix(data, dims, iname="i", jname="j"): dims = dims + (DimMatrix_i(iname, arange(data.shape[-2])), DimMatrix_j(jname, arange(data.shape[-1]))) return hfarray(data, dims, copy=False) def make_vector(data, dims, jname="j"): dims = dims + (DimMatrix_j(jname, arange(data.shape[-1])),) return hfarray(data, dims, copy=False) if __name__ == '__main__': fi = DimSweep("freq", linspace(0, 10e9, 11)) ri = DimRep("rep", range(10)) a = hfarray(zeros((11, 10)), (fi, ri)) b = hfarray(zeros((11, )), (fi, )) c = hfarray(zeros((10, )), (ri, ))
def test_date_1(self): res = np.array(["2012-05-30 12:12:31", "2012-05-31 12:12:31"], np.dtype("datetime64[us]")) d = DimSweep("a", res) self.assertEqual(d.data.dtype, np.dtype("datetime64[us]"))
def test_obj(self): d = DimSweep("a", np.array([Object(), Object()])) self.assertEqual(d.data.dtype, np.object)
def test_5(self): self._helper((DimSweep("d", 3), ComplexIndepAxis("cplx", 2), ComplexDerivAxis("cplx", 2)))
def test_1(self): a = DimSweep(self.dim) self.assertEqual(a, self.dim) self.assertEqual(a.name, "a") self.assertEqual(a.unit, "Hz") self.assertAllclose(a.data, range(10))
def test_2(self): self._helper((DimSweep("d", 3), ComplexDiagAxis("cplx", 2)))
def setUp(self): self.dim = DimSweep("a", 10, unit="Hz")
def test_1(self): self._helper_false((DimSweep("d", 3), ))
def test_5(self): a = DimSweep(self.dim, name="P", unit="W", data=range(3)) self.assertEqual(a.name, "P") self.assertEqual(a.unit, "W") self.assertAllclose(a.data, range(3))
def test_2(self): a = DimSweep(self.dim, data=range(5)) self.assertEqual(a.name, "a") self.assertEqual(a.unit, "Hz") self.assertAllclose(a.data, range(5))
def test_6(self): a = DimSweep("P", data=set([0, 1, 2]), unit="W") self.assertEqual(a.name, "P") self.assertEqual(a.unit, "W") self.assertAllclose(sorted(a.data), range(3))
def test_empty(self): d = DimSweep("a", []) self.assertEqual(d.data.dtype, np.float64)
def test_hfarray(self): a = hfarray(self.dim) self.assertAllclose(a, range(10)) self.assertEqual(a.dims, (DimSweep("a", 10, unit="Hz"), ))
def test_int(self): d = DimSweep("a", [1, 2, 3]) self.assertEqual(d.outputformat, "%d")
class _hfarray(ndarray): u"""Basklass som ej skall anvandas direkt """ default_dim = (DimSweep("freq", array(0)), DimRep("rep", array(0))) def __new__(subtype, data, dims=None, dtype=None, copy=True, order=None, subok=False, ndmin=0, unit=None, outputformat=None, info=None): if info is not None: deprecate("hfarray, use dims not info") if dims is not None: raise ValueError("Can not specify both info and dims") dims = info # Make sure we are working with an array, and copy the data # if requested subarr = np.array(data, dtype=dtype, copy=copy, order=order, subok=subok, ndmin=ndmin) # Transform 'subarr' from an ndarray to our new subclass. subarr = subarr.view(subtype) # Use the specified 'dims' parameter if given if dims is None: if hasattr(data, 'dims'): dims = tuple(data.dims) elif subarr.ndim == 0: dims = tuple() elif len(subarr.shape) <= len(subtype.default_dim): dims = tuple(x.__class__(x.name, range(size)) for (x, size) in zip(subtype.default_dim, subarr.shape)) else: msg = ("On creation of %s *dims* " "must be specified" % subtype.__name__) raise DimensionMismatchError(msg) subarr._dims = Dims(dims) # Check to see that dims matches shape subarr.verify_dimension() if outputformat is not None: subarr.outputformat = outputformat elif hasattr(data, "outputformat"): subarr.outputformat = data.outputformat else: if is_integer(subarr): subarr.outputformat = "%d" elif is_numlike(subarr): subarr.outputformat = "%.16e" elif np.issubdtype(subarr.dtype, np.datetime64): subarr.outputformat = "%s" else: subarr.outputformat = "%s" # Finally, we must return the newly created object: if unit is None and hasattr(data, "unit"): subarr.__dict__["unit"] = data.unit else: subarr.__dict__["unit"] = unit return subarr @property def dims(self): return self._dims @dims.setter def dims(self, value): self._dims = Dims(value) @property def info(self): deprecate("hfarray, use dims not info") return self.dims @info.setter def info(self, value): deprecate("hfarray, use dims not info") self.dims = value def __repr__(self): prefix = " " * (len(self.__class__.__name__) - 5) r = np.asarray(self).__repr__() rlist = r.split("\n") out = [rlist[0].replace("array", self.__class__.__name__)] for rad in rlist[1:]: out.append(prefix + rad) return "\n".join(out) def __array_finalize__(self, obj): self.__dict__["_dims"] = Dims(getattr(obj, "_dims", Dims())) self.__dict__["outputformat"] = getattr(obj, "outputformat", "%.16e") self.__dict__["unit"] = getattr(obj, "unit", None) def verify_dimension(self): u"""Internal function that checks to see if the arrays dimensions match those of the *dims* specification. """ if len(self.dims) != self.ndim: raise HFArrayShapeDimsMismatchError def dims_index(self, name, cls=None): u"""Leta upp index for axisobjekt med *name* """ for idx, ax in enumerate(self.dims): if ax.name == name: if cls is None: return idx elif isinstance(ax, cls): return idx msg = "Can not find AxisObject with name:%r and cls:%s" % (name, cls) raise IndexError(msg) def info_index(self, name, cls=None): deprecate("info_index deprecated") return self.dims_index(name, cls) def replace_dim(self, olddim, newdim): if isinstance(olddim, string_types): olddim = self.dims_index(olddim) olddim = self.dims[olddim] if np.issubclass_(newdim, DimBase): newdim = newdim(olddim) self.dims = replace_dim(self.dims, olddim, newdim) return self.dims def view(self, dtype=None, type=None): u"""Return view of *data* i.e. new *hfarray* object but pointing to same data. """ if type is None: return self.__class__(ndarray.view(self, dtype=self.dtype), dims=self.dims, copy=False) else: return ndarray.view(self, dtype=self.dtype, type=type) def reorder_dimensions(self, *order): u"""Omorganiserar ordningen pa Axis i *dims*. Genom att flytta Axis objekten som raknas upp i *order* till borjan. """ infos = list(self.dims[:]) neworder = [] for dim in order: neworder.append(self.dims.index(dim)) del infos[infos.index(dim)] for dim in infos: neworder.append(self.dims.index(dim)) return self.transpose(*neworder) def transpose(self, *order): u"""Returnerar hfarray med dimensionerna omorganiserade i ordning som ges av *order*. *order* anger index i *dims* listan. .. todo:: Ta aven emot en lista med Axis objekt. """ if not order: order = range(self.ndim)[::-1] return self.__class__(ndarray.transpose(self, *order), dims=[self.dims[i] for i in order], copy=False) @property def T(self): return self.transpose() @property def t(self): """transpose dimensions indicated by DimMatrix_i and DimMatrix_j The position of the dimensions will be kept but the data will be transposed. The dimensions will be transformed like: (..., DimMatrix_i("i", [1, 2, 3]), DimMatrix_j("j", [1, 2]),) => (..., DimMatrix_i("i", [1, 2]), DimMatrix_j("j", [1, 2, 3]),) => """ di = None dj = None for dim in self.dims: if isinstance(dim, DimMatrix_i): di = dim elif isinstance(dim, DimMatrix_j): dj = dim if di is None or dj is None: return self i = self.dims_index(di) j = self.dims_index(dj) order = list(range(self.ndim)) order[i], order[j] = order[j], order[i] out = self.transpose(*order) newdims = list(out.dims) newdims[i], newdims[j], = (DimMatrix_i(di.name, dj.data), DimMatrix_j(dj.name, di.data)) out.dims = Dims(newdims) return out def take(self, indices, axis=None, out=None, mode="raise"): if axis is None: data = np.ndarray.take(self, indices, axis, out, mode) return hfarray(data, dims=(DimAnonymous("anon1", indices), )) axis = self.dims_index(axis_handler(self, axis)) olddim = self.dims[axis] newdim = olddim.__class__(olddim.name, indices) data = np.ndarray.take(self, indices, axis, out, mode) newdims = list(self.dims) newdims[axis] = newdim data.dims = Dims(newdims) return data def squeeze(self, axis=None): u"""Remove single-dimensional entries from the shape of an array. """ dims, dimidxs = multiple_axis_handler(self, axis) if dims is None: newinfo = [ax for idx, ax in enumerate(self.dims) if self.shape[idx] != 1] else: newinfo = [ax for idx, ax in enumerate(self.dims) if idx not in dimidxs] return self.__class__(ndarray.squeeze(self, axis=dimidxs), dims=newinfo, copy=False) def apply_outputformat(fun): def __getitem__(self, *x, **kw): out = fun(self, *x, **kw) if hasattr(self, "outputformat") and hasattr(out, "outputformat"): out.outputformat = self.outputformat if hasattr(self, "unit") and hasattr(out, "unit"): out.unit = self.unit return out return __getitem__ @apply_outputformat def __getslice__(self, start, stop): return self.__getitem__(slice(start, stop)) @apply_outputformat def __getitem__(self, x): if x is newaxis or (isinstance(x, tuple) and newaxis in x): return self.view(type=ndarray, dtype=self.dtype)[x] if x is Ellipsis: return self.view() if isinstance(x, tuple): indices = x else: indices = (x,) ellipsis_and_ints = True orig_indices = indices for i in indices: if isinstance(i, integer_types) or i is Ellipsis: pass else: ellipsis_and_ints = False ellips_count = len([i for i in indices if isinstance(i, type(Ellipsis))]) if ellips_count == 1: i = indices.index(Ellipsis) indices = (indices[:i] + (slice(None),) * (self.ndim - (len(x) - 1)) + indices[i + 1:]) elif ellips_count > 1: raise IndexError("Can not handle more than one Ellipsis") dims = self.dims testbool = (len(indices) == 1 and isinstance(indices[0], hfarray) and indices[0].dtype == bool) if testbool: reorder = [] for dim in self.dims: if dim not in indices[0].dims: reorder.append(dim) else: break reorder2 = reorder + list(indices[0].dims) reordered = self.reorder_dimensions(*reorder2) if len(indices[0].dims) == 1: dim = indices[0].dims[0] newdim = dim.__class__(dim.name, np.array(dim.data)[indices[0]], unit=dim.unit) else: newdim = get_new_anonymous_dim(self, int(np.sum(indices[0]))) dims = (reordered.dims[:len(reorder)] + (newdim,) + reordered.dims[len(reorder) + len(indices[0].dims):]) idx = (slice(None), ) * len(reorder) + (indices[0],) return hfarray(ndarray.__getitem__(reordered, idx), dims=dims) indices = indices + (slice(None),) * (self.ndim - len(indices)) dims = [] dim_in_indices = dict((x.dims[0].name, x.dims[0]) for x in indices if isinstance(x, hfarray)) for idx, dim in zip(indices, self.dims): if isinstance(idx, integer_types): continue elif isinstance(idx, slice): dims.append(dim[idx]) else: dims.append(dim_in_indices.get(dim.name, dim)) if ellipsis_and_ints: indices = orig_indices out = ndarray.__getitem__(self, indices) if isinstance(out, ndarray): return self.__class__(out, dims=dims, copy=False) else: return out @check_instance def __and__(self, other): q = ndarray.__and__(self, other) return q @check_instance def __or__(self, other): q = ndarray.__or__(self, other) return q @check_instance def __xor__(self, other): q = ndarray.__xor__(self, other) return q @check_instance def __add__(self, other): return ndarray.__add__(self, other) @check_instance def __sub__(self, other): return ndarray.__sub__(self, other) @check_instance def __mul__(self, other): return ndarray.__mul__(self, other) @check_instance def __div__(self, other): return ndarray.__div__(self, other) @check_instance def __truediv__(self, other): return ndarray.__truediv__(self, other) @check_instance def __pow__(self, other): return ndarray.__pow__(self, other) @check_instance def __rand__(self, other): q = ndarray.__rand__(self, other) return q @check_instance def __ror__(self, other): q = ndarray.__ror__(self, other) return q @check_instance def __rxor__(self, other): q = ndarray.__rxor__(self, other) return q @check_instance def __radd__(self, other): return self.__add__(other) @check_instance def __rsub__(self, other): return (-self).__add__(other) @check_instance def __rmul__(self, other): return self.__mul__(other) @check_instance def __eq__(self, other): return ndarray.__eq__(self, other) @check_instance def __rdiv__(self, other): return np.divide(other, self) @check_instance def __rtruediv__(self, other): return np.divide(other, self) @check_instance def __rpow__(self, other): return np.power(other, self) def __abs__(self): return ndarray.__abs__(self) def __neg__(self): return ndarray.__neg__(self) def copy(self): u"""Skapa kopia av objekt """ return self.__class__(self) def rss(self, axis=None): u"""Berakna kvadratsumma over *axis*. Dar *axis* specas av index till *dims*. .. todo:: Ta aven emot en lista med Axis objekt. """ return (abs(self) ** 2).sum(axis) ** 0.5 def sum(self, axis=None, dtype=None, out=None, keepdims=False, dimerror=True): try: dims, dimidx = multiple_axis_handler(self, axis) except IndexError: if dimerror: raise else: return self r = np.asarray(self).sum(dimidx, dtype=dtype, out=out, keepdims=True) res = hfarray(r, dims=self.dims) if not keepdims: res = res.squeeze(axis=dimidx) return res def mean(self, axis=None, dtype=None, out=None, keepdims=False, dimerror=True): try: dims, dimidx = multiple_axis_handler(self, axis) except IndexError: if dimerror: raise else: return self r = np.asarray(self).mean(dimidx, dtype=dtype, out=out, keepdims=True) res = hfarray(r, dims=self.dims) if not keepdims: res = res.squeeze(axis=dimidx) return res def std(self, axis=None, dtype=None, out=None, ddof=0, keepdims=False, dimerror=True): u"""Berakna standardavvikelse over *axis*. Dar *axis* specas av index till *dims*. .. todo:: Ta aven emot en lista med Axis objekt. """ try: dims, dimidx = multiple_axis_handler(self, axis) except IndexError: if dimerror: raise else: return self r = np.asarray(self).std(dimidx, dtype=dtype, out=out, keepdims=True) res = hfarray(r, dims=self.dims) if not keepdims: res = res.squeeze(axis=dimidx) return res def var(self, axis=None, dtype=None, out=None, ddof=0, keepdims=False, dimerror=True): u"""Berakna standardavvikelse over *axis*. Dar *axis* specas av index till *dims*. .. todo:: Ta aven emot en lista med Axis objekt. """ try: dims, dimidx = multiple_axis_handler(self, axis) except IndexError: if dimerror: raise else: return self r = np.asarray(self).var(dimidx, dtype=dtype, out=out, keepdims=True) res = hfarray(r, dims=self.dims) if not keepdims: res = res.squeeze(axis=dimidx) return res def min(self, axis=None, out=None, keepdims=False, dimerror=True): u"""Berakna minsta varde over *axis*. Dar *axis* specas av index till *dims*. .. todo:: Ta aven emot en lista med Axis objekt. """ try: dims, dimidx = multiple_axis_handler(self, axis) except IndexError: if dimerror: raise else: return self r = np.asarray(self).min(dimidx, out=out, keepdims=True) res = hfarray(r, dims=self.dims) if not keepdims: res = res.squeeze(axis=dimidx) return res def max(self, axis=None, out=None, keepdims=False, dimerror=True): u"""Berakna storsta varde over *axis*. Dar *axis* specas av index till *dims*. .. todo:: Ta aven emot en lista med Axis objekt. """ try: dims, dimidx = multiple_axis_handler(self, axis) except IndexError: if dimerror: raise else: return self r = np.asarray(self).max(dimidx, out=out, keepdims=True) res = hfarray(r, dims=self.dims) if not keepdims: res = res.squeeze(axis=dimidx) return res def cumprod(self, axis=0, dtype=None, out=None): if axis is None: raise HFArrayError("Must choose axis for cumulative product") axis = axis_handler(self, axis) result = np.asarray(self).cumprod(axis=self.dims_index(axis), dtype=dtype, out=out) return self.__class__(result, dims=self.dims, copy=False) def cumsum(self, axis=0, dtype=None, out=None): if axis is None: raise HFArrayError("Must choose axis for cumulative product") axis = axis_handler(self, axis) result = np.asarray(self).cumsum(axis=self.dims_index(axis), dtype=dtype, out=out) return self.__class__(result, dims=self.dims, copy=False) def help(self): out = "\n".join(["class: %(_class)s", "dtype: %(dtype)s", "shape: %(shape)r", "dims: (%(dims)s)"]) dims = ["%r" % self.dims[0]] for i in self.dims[1:]: dims.append(" %r" % i) out = out % dict(_class=self.__class__.__name__, dtype=self.dtype, shape=self.shape, dims=",\n".join(dims), ) return out def add_dim(self, dim, axis=0): if dim is None: return self if dim in self.dims: return self axis = min(axis, self.ndim) out = self[(slice(None), ) * axis + (None, )] if len(dim.data) > 1: out = out.repeat(len(dim.data), axis) dims = list(self.dims) dims.insert(axis, dim) return self.__class__(out, dims=dims, copy=False)
vg <6x6x201> """ def complex_normal(loc=0, scale=1, dims=None): size = [x.data.shape[0] for x in dims] result = np.empty(size, np.complex128) result[:].real = rnd.normal(loc, scale, size) result[:].imag = rnd.normal(loc, scale, size) return hfarray(result, dims=dims) def normal(loc=0, scale=1, dims=None): size = [x.data.shape[0] for x in dims] return hfarray(rnd.normal(loc, scale, size), dims=dims) db = DataBlock() db.blockname = "RFmeas.LDMOS69.Spar" freq = DimSweep("freq", linspace(50e6, 20.05e9, 201)) vd = DimSweep("vd", linspace(0, 5, 6)) vg = DimSweep("vg", linspace(0, 5, 6)) sweep_dims = (vd, vg, freq) matrix_dims = (DimMatrix_i("i", 2), DimMatrix_j("j", 2)) db.freq = freq db.vd = vd db.vg = vg db.H = complex_normal(dims=sweep_dims + matrix_dims) db.Id = normal(dims=sweep_dims) db.Ig = normal(dims=sweep_dims) db.S = complex_normal(dims=sweep_dims) db.Ig = normal(dims=sweep_dims) db.Ig = normal(dims=sweep_dims)