def setUp(self): self.d = DataBlock() self.fi = DimSweep("Freq", 3) self.gi = DimSweep("g", 4) self.hi = DimSweep("h", 4) self.d["Vds"] = hfarray([1, 2, 3], dims=(self.fi, )) self.d["Id"] = hfarray([1, 2, 3, 4], dims=(self.gi, ))
def test_5(self): d = DataBlock() d.comments = Comments([ "Vgs=13", "Ig=14", "Datetime=2011-10-11 20:11:02", "Vds=-13", "Calibration=SOLT" ]) fi = DimSweep("Freq", 3) gi = DimSweep("g", 4) d["Vds"] = hfarray([1, 2, 3], dims=(fi, )) d["Id"] = hfarray([1, 2, 3, 4], dims=(gi, )) d.comments.property["Ig"].unit = "mA" d.guess_units() d.values_from_property() self.assertEqual(d.Vds.unit, "V") self.assertAllclose(d.Vds, [1, 2, 3]) self.assertEqual(d.Id.unit, "A") self.assertAllclose(d.Id, [1, 2, 3, 4]) self.assertEqual(d.Freq.unit, "Hz") self.assertEqual(d.comments.property["Vgs"].unit, "V") self.assertEqual(d.comments.property["Ig"].unit, "mA") self.assertEqual(d.Ig, 14) self.assertEqual(d.Vgs, 13) self.assertEqual(d.Ig.unit, "mA") self.assertEqual(d.Vgs.unit, "V") self.assertTrue("Datetime" in d.allvarnames)
def setUp(self): Test_DataBlock.setUp(self) self.a.P = hfarray([0.5, -.3, .5]) Sdims = (DimMatrix_i("i", 2), DimMatrix_j("j", 2)) self.a.S = hfarray([[11, 12], [21, 22]], dims=Sdims) Wdims = (DimSweep("g", 1), DimMatrix_i("i", 2), DimMatrix_j("j", 2)) self.a.W = hfarray([[[11, 12], [21, 22]]], dims=Wdims) self.a.T = hfarray(["Text"], dims=(DimSweep("text_i", ["index0"]), ))
def test_2(self): d = self.d reset_hftools_warnings() self.assertHFToolsWarning(d.sort, DimSweep("NONE", [1, 2, 3])) with warnings.catch_warnings() as log: warnings.resetwarnings() warnings.simplefilter("ignore", HFToolsWarning) d.sort(DimSweep("NONE", [1, 2, 3])) reset_hftools_warnings()
def test_2(self): d = DataBlock() fi = DimSweep("Freq[Hz]", 3) gi = DimSweep("g", 4) dims = (fi, gi) d["a"] = hfarray([[1, 2, 3]] * 4, dims=dims, outputformat="%.5f", unit="V") self.assertRaises(ValueError, d.filter, d.a < 2)
def test_setitem_1(self): a = DataBlock() a.l = DimSweep("l", [.1, .2, .3]) self.assertAllclose(a.l, [.1, .2, .3]) self.assertAllclose(a.xvalue, [.1, .2, .3]) self.assertEqual(a.xname, "l") a.w = DimSweep("w", [1.1, 1.2, 1.3]) self.assertAllclose(a.w, [1.1, 1.2, 1.3]) self.assertAllclose(a.xvalue, [.1, .2, .3]) self.assertEqual(a.xname, "l")
def proc_info(info, f, data, fn, noisedata): info_list = info.upper().split() if len(info_list) == 5: info = freq_mult, twoporttype, datatype, rtype, z0 = info_list elif len(info_list) == 3: info = freq_mult, twoporttype, datatype = info_list z0 = 1. else: msg = ("# format should only have 5 values this one has %d" % len(info_list)) raise TouchstoneError(msg) z0 = float(z0) f = f * freqmultdict[freq_mult] if fn is not None: fn = fn * freqmultdict[freq_mult] convfunc = dict(RI=re_im_to_complex, DB=dB_angle_to_complex, MA=mag_angle_to_complex) if datatype not in convfunc: pars = datatype msg = "Unknown dataformat: %s, valid formats: RI, DB, MAG" % pars raise TouchstoneError(msg) out = np.empty((data.shape[0], data.shape[1] // 2), dtype=np.complex128) out[...] = convfunc[datatype](data[:, ::2], data[:, 1::2]) if out.shape[1] == 1: out.shape = (out.shape[0], 1, 1) elif out.shape[1] == 4: out.shape = (out.shape[0], 2, 2) out = out.transpose(0, 2, 1) else: N = int(sqrt(out.shape[1])) out.shape = (out.shape[0], N, N) dims = (DimSweep("freq", f, unit="Hz"), ) if fn is not None and len(f) == len(fn) and np.allclose(f, fn): noisedims = dims elif fn is None: noisedims = None else: noisedims = (DimSweep("freqn", fn, unit="Hz"), ) out = make_matrix(out, dims) f = hfarray(f, dims) db = DataBlock() db[info[1]] = out if noisedata is not None: db.Rn = hfarray(noisedata[..., -1], dims=noisedims) * z0 db.Fmin = 10**(hfarray(noisedata[..., 0], dims=noisedims) / 10) db.Gopt = hfarray(convfunc[datatype](noisedata[:, 1], noisedata[:, 2]), dims=noisedims) db.Z0 = hfarray(z0, unit="Ohm") return db
def test_2(self): d = DataBlock() fi = DimSweep("Freq[Hz]", 3) gi = DimSweep("P[W]", 3) dims = (fi, ) dims2 = (fi, gi) dims3 = (gi, ) d["f"] = hfarray([[1, 2, 3]] * 3, dims=dims2) d["P[W]"] = hfarray([10, 20, 30], dims=dims) self.assertEqual(d.allvarnames, ["Freq[Hz]", "P[W]", "f"]) self.assertAllclose(d["P[W]"], [10, 20, 30])
def test_intersection_2(self): d = DataBlock() fi = DimSweep("Freq[Hz]", [10, 20, 30, 40, 50]) gi = DimSweep("g", 4) dims = (fi, gi) d["a"] = hfarray([[1, 2, 3, 4]] * 5, dims=dims, outputformat="%.5f", unit="V") x = DimSweep("Freq[Hz]", [20, 40]) dres = d.filter(hfarray(x)) self.assertAllclose(dres["Freq[Hz]"], [20, 40])
def test_7(self): d = DataBlock() d.comments = Comments(["Hej=10", "Svejs=11"]) dim = DimSweep("f", 3, outputformat="%.1f") d.freq = dim d.date = DimSweep( "date", [hfarray("2012-08-13 08:03:01", dtype="datetime64[us]")]) fname = testpath / "testdata/hdf5/v01/savetest/res_7.hdf5" self.savefun[0](d, fname) d2 = readfun(fname) fname.unlink() self.assertEqual(d2.date, d.date)
def test_4(self): d = DataBlock() fi = DimSweep("Freq", 3) gi = DimSweep("g", 4) d["Vds"] = hfarray([1, 2, 3], dims=(fi, )) d["Id"] = hfarray([1, 2, 3, 4], dims=(gi, )) d.guess_units(["Freq", "Vds", "Ig"]) self.assertEqual(d.Vds.unit, "V") self.assertAllclose(d.Vds, [1, 2, 3]) self.assertEqual(d.Id.unit, None) self.assertAllclose(d.Id, [1, 2, 3, 4]) self.assertEqual(d.Freq.unit, "Hz")
def test_2(self): d = DataBlock() fi = DimSweep("Freq", 3, unit="m") gi = DimSweep("g", 4) d["Vds"] = hfarray([1, 2, 3], dims=(fi, ), unit="A") d["Id"] = hfarray([1, 2, 3, 4], dims=(gi, ), unit="V") d.guess_units() self.assertEqual(d.Vds.unit, "A") self.assertAllclose(d.Vds, [1, 2, 3]) self.assertEqual(d.Id.unit, "V") self.assertAllclose(d.Id, [1, 2, 3, 4]) self.assertEqual(d.Freq.unit, "m")
def test_cant_append(self): i1 = DimSweep("a", 1, unit="s", outputformat="") i2 = DimSweep("INDEX", 2, unit="s") iexpand = DimRep("INDEX", 1, unit="s", outputformat="") d1 = DataBlock() d1.b = hfarray([2], dims=(i1, ), outputformat="") d1.c = hfarray(3) d2 = DataBlock() d2.b = hfarray([3], dims=(i1, )) fname = testpath / "testdata/hdf5/v02/savetest/res_1.hdf5" with hdf5context(fname, mode="w") as fil: savefun(d1, fil, expandable=True, expanddim=iexpand) self.assertRaises(ValueError, append_hdf5, d2, fil) fname.unlink()
def test_1(self): d = DataBlock() fi = DimSweep("Freq", 3) gi = DimSweep("g", 4) d["Vds"] = hfarray([1, 2, 3], dims=(fi, )) d["Id"] = hfarray([1, 2, 3, 4], dims=(gi, )) d.guess_units() d.values_from_property() self.assertEqual(d.Vds.unit, "V") self.assertAllclose(d.Vds, [1, 2, 3]) self.assertEqual(d.Id.unit, "A") self.assertAllclose(d.Id, [1, 2, 3, 4]) self.assertEqual(d.Freq.unit, "Hz")
def parse_blocks(self, stream): # raise Exception("HANTERAR INTE KOMMENTARER INNE I MANYOPTIONAL") comments = [] comments.extend(ManyOptional("COMMENT")(stream)) citifileversion = Optional("CITIFILE")(stream) comments.extend(ManyOptional("COMMENT")(stream)) name = One("NAME", "Must have END after DATA")(stream) comments.extend(ManyOptional("COMMENT")(stream)) varnames = ManyOptional("VARDEF")(stream) comments.extend(ManyOptional("COMMENT")(stream)) datanames = ManyOptional("DATADEF")(stream) comments.extend(ManyOptional("COMMENT")(stream)) block = DataBlock() block.blockname = name[0] for name, typ, N in varnames: N = int(N) errmsg = "Missing VAR_LIST_BEGIN or SEG_LIST_BEGIN" tagname, = OneOf(["VAR_LIST_BEGIN", "SEG_LIST_BEGIN"], errmsg)(stream) if tagname.startswith("VAR_LIST"): datalist = handle_data(ManyOptional("DATA")(stream), typ) block[name] = DimSweep(name, datalist) One("VAR_LIST_END", "Missing VAR_LIST_END")(stream) elif tagname.startswith("SEG"): # pragma: no branch datalist, = One("SEG", "Missing SEG")(stream) _, start, stop, step = datalist.strip().split() block[name] = DimSweep(name, np.linspace(float(start), float(stop), int(step))) One("SEG_LIST_END", "Missing SEG_LIST_END")(stream) else: # pragma: no cover pass comments.extend(ManyOptional("COMMENT")(stream)) for idx, com in enumerate(comments): if com.startswith("CONSTANT TIME"): date = tuple(com.strip().split()[2:]) comments[idx] = "Measurement time: %s-%s-%s %s:%s:%s" % date block.comments = Comments(comments) shape = tuple(block.ivardata[i[0]].data.shape[0] for i in varnames) dims = tuple(block.ivardata[i[0]] for i in varnames) for name, typ in datanames: One("DATA_LIST_BEGIN", "Missing BEGIN")(stream) datalist = array(handle_data(ManyOptional("DATA")(stream), typ)) datalist.shape = shape block[name] = hfarray(datalist, dims=dims) One("DATA_LIST_END", "Missing END")(stream) yield block
def test_3(self): d = DataBlock() d.comments = Comments(["Hej=10", "Svejs=11"]) dims = ( DimSweep("f", 3, outputformat="%.1f"), DimSweep("i", 2, outputformat="%.0f"), DimSweep("j", 2, outputformat="%.0f"), ) d.c = hfarray( [[[1, 2], [3, 4]], [[10, 20], [30, 40]], [[10, 20], [30, 40]]], dims=dims, outputformat="%.2f") fname = testpath / "testdata/hdf5/v02/savetest/res_3.hdf5" self.savefun[0](d, fname) fname.unlink()
def test_1(self): d = DataBlock() fi = DimSweep("Freq[Hz]", 3) gi = DimSweep("P[W]", 3) dims = (fi, ) dims2 = (fi, gi) d["f"] = hfarray([[1, 2, 3]] * 3, dims=dims2, outputformat="%.5f") d["P"] = hfarray([10, 20, 30], dims=dims, outputformat="%.7f") self.assertEqual(d["f"].outputformat, "%.5f") self.assertEqual(d["P"].outputformat, "%.7f") d.outputformat = "%.9f" for v in d.ivardata.values(): self.assertEqual(v.outputformat, "%.9f") for v in d.vardata.values(): self.assertEqual(v.outputformat, "%.9f")
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.mi = mi = DimMatrix_i("i", [0, 1]) self.mj = mj = DimMatrix_j("j", [0, 1]) 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") cdims = (DimMatrix_i("i", 2), DimMatrix_j("j", 2)) self.c = hfarray([[11, 12], [21, 22]], dims=cdims) self.m = hfarray([[[1., 2], [3, 4]]] * 3, dims=(J, mi, mj)) self.m2 = hfarray([[1., 2], [3, 4]], dims=(mi, mj))
def test_error(self): fi = DimSweep("freq", [ 1, ]) db = DataBlock() self.assertRaises(ValueError, dset.subset_datablock_by_dims, db, (fi, fi))
def test_copy_2(self): d = DataBlock() fi = DimSweep("Freq[Hz]", 3) dims = (fi, ) d.comments = Comments(["Hej=10"]) d["a"] = hfarray([1, 2, 3], dims=dims, outputformat="%.5f", unit="V") d["b"] = hfarray([10, 20, 30], dims=dims, outputformat="%.7f", unit="A") c = d.copy() self.assertEqual(c.allvarnames, d.allvarnames) self.assertEqual(c.vardata.keys(), d.vardata.keys()) self.assertEqual(c.ivardata.keys(), d.ivardata.keys()) self.assertAllclose(c["Freq[Hz]"], d["Freq[Hz]"]) self.assertAllclose(d.a, [1, 2, 3]) self.assertAllclose(c.a, d.a) self.assertAllclose(c.b, d.b) self.assertFalse(id(d.comments) == id(c.comments)) c.a[0] = 7 c.b[0] = 7 self.assertAllclose(c.a, [7, 2, 3]) self.assertAllclose(c.b, [7, 20, 30]) self.assertAllclose(d.a, [1, 2, 3]) self.assertAllclose(d.b, [10, 20, 30])
def setUp(self): self.d = DataBlock() self.d.P = VA([0.5, -.3, .5]) Sdims = (DimMatrix_i("i", 2), DimMatrix_j("j", 2)) self.d.S = VA([[11, 12], [21, 22]], dims=Sdims) Wdims = (DimSweep("g", 1), DimMatrix_i("i", 2), DimMatrix_j("j", 2)) self.d.W = VA([[[11, 12], [21, 22]]], dims=Wdims)
def setUp(self): d = DataBlock() fi = DimSweep("Freq[Hz]", 3) gi = DimSweep("g", 4) dims = (fi, ) d["a"] = hfarray([3, 2, 1], dims=dims, outputformat="%.5f", unit="V") d["b"] = hfarray([30, 10, 20], dims=dims, outputformat="%.7f", unit="A") d["c"] = hfarray([30, 20, 10, 40], dims=(gi, ), outputformat="%.7f", unit="A") d["ac"] = d.a * d.c self.d = d
def test_8(self): d = DataBlock() d.b = hfarray([2], dims=(DimSweep("a", 1), )) fname = testpath / "testdata/hdf5/v02/savetest/res_8.hdf5" with h5py.File(fname, mode="w") as fil: self.savefun[0](d, fil) fname.unlink()
def test_intersection_1(self): d = DataBlock() fi = DimSweep("Freq[Hz]", [10, 20, 30, 40, 50]) gi = DimSweep("g", 4) dims = (fi, gi) d["a"] = hfarray([[1, 2, 3, 4]] * 5, dims=dims, outputformat="%.5f", unit="V") x = DimSweep("Freq", [20, 40]) reset_hftools_warnings() self.assertHFToolsWarning(d.filter, hfarray(x)) with warnings.catch_warnings() as log: warnings.resetwarnings() warnings.simplefilter("ignore", HFToolsWarning) d.filter(hfarray(x)) reset_hftools_warnings()
def test_2(self): d = DataBlock() fi = DimSweep("Freq[Hz]", 3) gi = DimSweep("P[W]", 2) dims = (fi, ) dims2 = (fi, gi) dims3 = (gi, ) d.P = hfarray([1, 2, 3], dims=dims) d.Q = hfarray([3, 2], dims=dims3) d.L = hfarray([[10, 20, 30], [11, 21, 31]], dims=dims2) self.assertEqual(d.allvarnames, ["Freq[Hz]", "P[W]", "P", "Q", "L"]) d.rename("P", "H") self.assertEqual(d.allvarnames, ["Freq[Hz]", "P[W]", "H", "Q", "L"]) d.rename("Freq[Hz]", "f") self.assertEqual(d.allvarnames, ["f", "P[W]", "H", "Q", "L"]) d.rename("foo", "bar") self.assertEqual(d.allvarnames, ["f", "P[W]", "H", "Q", "L"])
def test_1(self): i1 = DimSweep("a", 1, unit="s", outputformat="") i2 = DimSweep("INDEX", 2) d1 = DataBlock() d1.b = hfarray([2], dims=(i1, ), outputformat="") d2 = DataBlock() d2.b = hfarray([3], dims=(i1, )) fname = testpath / "testdata/hdf5/v02/savetest/res_1.hdf5" with hdf5context(fname, mode="w") as fil: savefun(d1, fil, expandable=True) append_hdf5(d2, fil) d = readfun(fname) self.assertAllclose(hfarray([[2, 3]], dims=(i1, i2)), d.b) self.assertEqual(d.ivardata["a"].unit, "s") self.assertEqual(d.ivardata["a"].outputformat, "%d") self.assertEqual(d.b.outputformat, "%d") fname.unlink()
def test_4(self): d = DataBlock() d.comments = Comments(["Hej=10", "Svejs=11"]) dim = DimSweep("f", 3, outputformat="%.1f") d.freq = dim fname = testpath / "testdata/hdf5/v02/savetest/res_4.hdf5" self.savefun[0](d, fname) fname.unlink()
def test_2(self): d = DataBlock() d.comments = Comments(["Hej=10"]) # import pdb;pdb.set_trace() d.b = hfarray([2], dims=(DimSweep("a", 1), )) fname = testpath / "testdata/hdf5/v02/savetest/res_2.hdf5" self.savefun[0](d, fname) fname.unlink()
def test_10(self): d = DataBlock() d.blockname = "Foo" d.b = hfarray([2], dims=(DimSweep("a", 1), ), unit="V") fname = testpath / "testdata/hdf5/v01/savetest/res_10.hdf5" self.savefun[0](d, fname) d2 = readfun(fname) self.assertEqual(d2.blockname, "Foo") fname.unlink()
def test1(self): a = DataBlock() a.i = DimSweep("i", [1, 2, 3]) def funk(a): dims = (DimSweep("i", [1]), DimSweep("j", [1])) a.i = hfarray([[1]], dims=dims) self.assertRaises(AttributeError, funk, a)