def setUp(self): self.a = aobj.hfarray([1, 2, 3], dims=(ds.DiagAxis("f", 3),)) bdims = (ds.DiagAxis("f", 3), ds.DiagAxis("p", 3),) self.b = aobj.hfarray([[1, 2, 3], [10, 20, 30], [100, 200, 300]], dims=bdims)
def test_info_deprecation_2(self): reset_hftools_warnings() self.assertHFToolsDeprecationWarning(aobj.hfarray, 1, info=()) with warnings.catch_warnings(): warnings.resetwarnings() warnings.simplefilter("ignore", HFToolsDeprecationWarning) aobj.hfarray(1, info=())
def test_get_bool_1(self): a = aobj.DimSweep("a", [1, 2, 3]) A = aobj.hfarray([10, 20, 30], dims=(a, )) ar = aobj.DimSweep("a", [2, 3]) Ar = aobj.hfarray([20, 30], dims=(ar, )) bools = A > 10 self.assertAllclose(A[bools], Ar)
def interpolate(newx, y, defaultmode=None): mode = getattr(y, "interpolationmode", defaultmode) if isinstance(newx, (_hfarray,)): if len(newx.dims) != 1: raise ValueError("hfarray must have one dimension") else: newx = newx.dims[0] if newx not in y.dims: return y oldx = y.dims.get_matching_dim(newx) olddims = y.dims dimidx = olddims.matching_index(newx) newdims = olddims[:dimidx] + (newx,) + olddims[dimidx + 1:] if mode in [None, "none"]: bools = np.array(newx.data)[:, None] == np.array(oldx.data)[None, :] boolarray = hfarray(bools.any(axis=0), dims=(oldx,), unit=y.unit, outputformat=y.outputformat) if boolarray.sum() != len(newx.data): raise ValueError("Missing x-values") data = np.array(y, copy=False)[boolarray] elif mode in ["linear"]: data = interp1d(y.dims[dimidx].data, y, axis=dimidx)(newx.data) else: raise ValueError("Interpolation mode %r unknown" % mode) return hfarray(data, dims=newdims, unit=y.unit, outputformat=y.outputformat)
def interpolate(newx, y, defaultmode=None): mode = getattr(y, "interpolationmode", defaultmode) if isinstance(newx, (_hfarray, )): if len(newx.dims) != 1: raise ValueError("hfarray must have one dimension") else: newx = newx.dims[0] if newx not in y.dims: return y oldx = y.dims.get_matching_dim(newx) olddims = y.dims dimidx = olddims.matching_index(newx) newdims = olddims[:dimidx] + (newx, ) + olddims[dimidx + 1:] if mode in [None, "none"]: bools = np.array(newx.data)[:, None] == np.array(oldx.data)[None, :] boolarray = hfarray(bools.any(axis=0), dims=(oldx, ), unit=y.unit, outputformat=y.outputformat) if boolarray.sum() != len(newx.data): raise ValueError("Missing x-values") data = np.array(y, copy=False)[boolarray] elif mode in ["linear"]: data = interp1d(y.dims[dimidx].data, y, axis=dimidx)(newx.data) else: raise ValueError("Interpolation mode %r unknown" % mode) return hfarray(data, dims=newdims, unit=y.unit, outputformat=y.outputformat)
def setUp(self): adims = (aobj.DimSweep("f", 1), aobj.DimMatrix_i("i", 2), aobj.DimMatrix_j("j", 2)) self.a = aobj.hfarray([[[1, 2 + 0j], [3, 4]]], dims=adims) bdims = (aobj.DimSweep("f", 1), aobj.DimMatrix_i("i", 3), aobj.DimMatrix_j("j", 3)) self.b = aobj.hfarray([[[1, 2 + 0j, 3], [4, 5, 6], [7, 8, 9]]], dims=bdims) self.adims = adims self.bdims = bdims
def test_2(self): ai = aobj.DimSweep("a", [1, 2, 3]) bi = aobj.DimSweep("b", [1, 2]) aj = aobj.DimRep("a", [1, 2, 3]) A = aobj.hfarray(ai) B = aobj.hfarray(bi) AB = A * B AB.replace_dim("a", aobj.DimRep) self.assertEqual(AB.dims, (aj, bi))
def setUp(self): (ai, bi, ci) = (dim.DimSweep("ai", 2), dim.DimRep("bi", 3), dim.DimSweep("ci", 4)) self.ai = ai self.bi = bi self.ci = ci self.a = aobj.hfarray(ai) self.b = aobj.hfarray(bi) * 10 self.c = aobj.hfarray(ci) * 100 self.abc = self.a + self.b + self.c
def setUp(self): self.I = I = DimSweep("I", [1, 2]) self.J = J = DimSweep("J", [10, 20, 30]) self.K = K = DimSweep("K", [100, 200, 300, 400]) self.i, self.j, self.k = (i, j, k) = list(map(hfarray, (I, J, K))) self.a = i * j * k self.b = hfarray(i * j * k, unit="Hz", outputformat="%.3f") cinfo = (DimMatrix_i("i", 2), DimMatrix_j("j", 2)) self.c = hfarray([[11, 12], [21, 22]], dims=cinfo)
def setUp(self): self.a = aobj.hfarray([1, 2, 3], dims=(ds.DiagAxis("f", 3),)) dims = (ds.DiagAxis("f", 3),) self.ca = aobj.hfarray([1 + 10j, 2 + 20j, 3 + 30j], dims=dims) dims = (ds.DiagAxis("f", 3), ds.DiagAxis("p", 2),) self.b = aobj.hfarray([[1, 2], [10, 20], [100, 200]], dims=dims) self.d = aobj.hfarray([[1, -2], [2, 1]], dims=ds.CPLX)
def setUp(self): self.a = aobj.hfarray([1, 2, 3], dims=(ds.DiagAxis("f", 3),)) dims = (ds.DiagAxis("f", 3), ds.DiagAxis("p", 3),) self.b = aobj.hfarray([[1, 2, 3], [10, 20, 30], [100, 200, 300]], dims=dims) dims = (ds.DimSweep("f", 2), ds.DimSweep("Vg", 3), ds.DimRep("rep", 4)) self.c = aobj.hfarray(np.zeros((2, 3, 4)), dims=dims) dims = (ds.DimSweep("f", 2), ds.DimSweep("Vg", 3), ds.DimRep("rep", 4), ds.DimMatrix_i("i", 5), ds.DimMatrix_j("j", 5),) self.d = aobj.hfarray(np.zeros((2, 3, 4, 5, 5)), dims=dims)
def test_get_bool_2(self): a = aobj.DimSweep("a", [1, 2]) b = aobj.DimSweep("b", [1, 2, 3]) c = aobj.DimSweep("c", [1, 2, 3, 4]) A = aobj.hfarray([1, 2], dims=(a, )) B = aobj.hfarray([10, 20, 30], dims=(b, )) C = aobj.hfarray([100, 200, 300, 400], dims=(c, )) ABC = A + B + C self.assertAllclose(ABC[A == 1].squeeze(), 1 + B + C) self.assertAllclose(ABC[B == 20].squeeze(), A + 20 + C) self.assertAllclose(ABC[C == 300].squeeze(), A + B + 300) self.assertRaises(ValueError, C.__getitem__, A == 1)
def test_take(self): a = aobj.DimSweep("a", [1, 2]) b = aobj.DimSweep("b", [1, 2, 3]) c = aobj.DimSweep("c", [1, 2, 3, 4]) A = aobj.hfarray([1, 2], dims=(a, )) B = aobj.hfarray([10, 20, 30], dims=(b, )) C = aobj.hfarray([100, 200, 300, 400], dims=(c, )) ABC = A + B + C anon = aobj.DimAnonymous("anon1", [0, 1, 2]) self.assertAllclose(ABC.take([0, 1, 2]), aobj.hfarray([111, 211, 311], dims=(anon, )))
def test_1(self): ai = aobj.DimSweep("a", [1, 2, 3]) bi = aobj.DimSweep("b", [1, 2]) A = aobj.hfarray(ai) B = aobj.hfarray(bi) AB = A * B newdim = aobj.DimSweep("c", [1, 2, 3]) AB.replace_dim(ai, newdim) self.assertEqual(AB.dims, (newdim, bi)) AB = A * B AB.replace_dim("a", newdim) self.assertEqual(AB.dims, (newdim, bi))
def values_from_property(self): """Convert numeric properties to values""" if self.comments: for k, v in self.comments.property.items(): if hasattr(v, "dtype") or isinstance(v, (int, float)): if (k not in self.vardata) and (k not in self.ivardata): self[k] = hfarray(v)
def test_init_6(self): fi = aobj.DimSweep("f", [12, 13, 14], unit="m", outputformat="%.3f") a = aobj.hfarray(fi, unit="Hz", outputformat="%.5f") self.assertEqual(a.unit, "Hz") self.assertEqual(a.dims, (fi,)) self.assertEqual(a.outputformat, "%.5f") self.assertAllclose(a, [12, 13, 14])
def make_matrix(A): a = np.asanyarray(A) dims = ( DimMatrix_i("i", range(a.shape[-2])), DimMatrix_j("j", range(a.shape[-1])), ) return hfarray(a, dims=dims)
def test_info_deprecation_1(self): a = aobj.hfarray(1) reset_hftools_warnings() self.assertHFToolsDeprecationWarning(lambda x: x.info, a) with warnings.catch_warnings(): warnings.resetwarnings() warnings.simplefilter("ignore", HFToolsDeprecationWarning) a.info
def test_set_info_deprecation_1(self): a = aobj.hfarray(1) reset_hftools_warnings() self.assertHFToolsDeprecationWarning(setattr, a, "info", ()) with warnings.catch_warnings(): warnings.resetwarnings() warnings.simplefilter("ignore", HFToolsDeprecationWarning) a.info = ()
def test_info_index_deprecated(self): reset_hftools_warnings() x = aobj.hfarray([1,2], dims=(aobj.DimSweep("a", 2),)) self.assertHFToolsDeprecationWarning(x.info_index, "a", aobj.DimSweep) with warnings.catch_warnings(): warnings.resetwarnings() warnings.simplefilter("ignore", HFToolsDeprecationWarning) x.info_index("a", aobj.DimSweep)
def setUp(self): self.adims = (aobj.DimSweep("f", 1), aobj.DimMatrix_i("i", 2), aobj.DimMatrix_j("j", 2)) self.a = aobj.hfarray([[[0, 0j], [0, 0]]], dims=self.adims) self.bdims = (aobj.DimSweep("f", 10), aobj.DimMatrix_i("i", 2), aobj.DimMatrix_j("j", 2)) self.b = aobj.hfarray(np.array([[[1, 2 + 0j], [3, 4]]]) + np.arange(10)[:, newaxis, newaxis], dims=self.bdims) self.c, = aobj.make_same_dims_list([random_value_array(4, 5)]) self.cdims = self.c.dims self.ddims = self.bdims self.d = aobj.hfarray(np.array([[[1, 2], [3, 4]]]) + np.arange(10)[:, newaxis, newaxis], dims=self.ddims) self.i1 = i1 = aobj.DimSweep("i1", 1) self.i2 = i2 = aobj.DimSweep("i2", 1) self.i3 = i3 = aobj.DimSweep("i3", 1) self.i4 = i4 = aobj.DimSweep("i4", 1) self.i5 = i5 = aobj.DimSweep("i5", 1) self.fi = fi = aobj.DimSweep("f", 2), self.gi = gi = aobj.DimSweep("g", 3), self.e = aobj.hfarray(np.zeros((1, 2, 3)), dims=(i1, fi, gi)) self.f = aobj.hfarray(np.zeros((2, 3, 1)), dims=(fi, gi, i1)) self.g = aobj.hfarray(np.zeros((1, 2, 3, 1)), dims=(i2, fi, gi, i1)) dims = (i2, fi, i3, gi, i1) self.h = aobj.hfarray(np.zeros((1, 2, 1, 3, 1)), dims=dims) dims = (i4, i5)
def remove_rep(self, newdimname="AllReps"): out = DataBlock() out.comments = self.comments dims = [x for x in self.ivardata.values() if isinstance(x, DimRep)] fullshape = tuple(x.data.shape[0] for x in dims) newdim = DimRep(newdimname, np.arange(np.multiply.reduce(fullshape))) for idx, dim in enumerate(dims): data = zeros(fullshape, dtype=dims[idx].data.dtype) a = hfarray(data, dims=dims) sliceidx = ((None,) * idx + (slice(None),) + (None,) * (len(fullshape) - 1 - idx)) a[...] = dim.data[sliceidx] a = np.array(a, copy=False) a.shape = (newdim.data.shape[0],) out[dim.name] = hfarray(a, dims=(newdim,)) for x in self.vardata: out[x] = remove_rep(self[x], newdimname) return out
def remove_rep(self, newdimname="AllReps"): out = DataBlock() out.comments = self.comments dims = [x for x in self.ivardata.values() if isinstance(x, DimRep)] fullshape = tuple(x.data.shape[0] for x in dims) newdim = DimRep(newdimname, np.arange(np.multiply.reduce(fullshape))) for idx, dim in enumerate(dims): data = zeros(fullshape, dtype=dims[idx].data.dtype) a = hfarray(data, dims=dims) sliceidx = ((None, ) * idx + (slice(None), ) + (None, ) * (len(fullshape) - 1 - idx)) a[...] = dim.data[sliceidx] a = np.array(a, copy=False) a.shape = (newdim.data.shape[0], ) out[dim.name] = hfarray(a, dims=(newdim, )) for x in self.vardata: out[x] = remove_rep(self[x], newdimname) return out
def test_t_3(self): a = aobj.DimSweep("freq", [1, 2, 3]) i = aobj.DimMatrix_i("i", 2) j = aobj.DimMatrix_j("j", 2) A = aobj.hfarray(np.zeros((3, 2,), dtype=np.complex128), dims=(a, j,)) A[:, 0] = [11, 110, 1100] A[:, 1] = [22, 220, 2200] At = A.t self.assertAllclose(A, At)
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 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 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 __getitem__(self, key): try: return hfarray(self.ivardata[key]) except KeyError: try: return self.vardata[key] except KeyError: pass mname, elem = is_matrix_name(key) if mname: data = self.vardata[mname] if ismatrix(data): idx = (Ellipsis, ) + elem return data[idx] else: msg = "No matrix with name %r is available" msg = msg % mname raise KeyError(msg) else: raise KeyError("No data named %r is available" % (key, ))
def __getitem__(self, key): try: return hfarray(self.ivardata[key]) except KeyError: try: return self.vardata[key] except KeyError: pass mname, elem = is_matrix_name(key) if mname: data = self.vardata[mname] if ismatrix(data): idx = (Ellipsis,) + elem return data[idx] else: msg = "No matrix with name %r is available" msg = msg % mname raise KeyError(msg) else: raise KeyError("No data named %r is available" % (key,))
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 accessor(x): """Matrix accessor function for zero indexed element (%d,%d)""" return hfarray(x, copy=False)[..., i, j]
def unit_matrix(size=2, cls=SArray): i = DimMatrix_i("i", range(size)) j = DimMatrix_j("j", range(size)) m = hfarray(np.identity(size, dtype=np.complex128), dims=(i, j)) return SArray(m)
def test_b_5(self): b = hfarray([1, 2, 3], unit="Hz", outputformat="%.3f") res = b[0] self.assertEqual(res, 1) self.assertFalse(hasattr(res, "unit")) self.assertFalse(hasattr(res, "outputformat"))
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)
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 unit_smatrix(size=2, cls=SArray): i = DimMatrix_i("i", range(size)) j = DimMatrix_j("j", range(size)) m = hfarray(np.zeros((size, size), dtype=np.complex128), dims=(i, j)) m[0, 1] = m[1, 0] = 1 return SArray(m)