def test_1(self): d = DataBlock() fi = DimSweep("Freq[Hz]", 3) gi = DimSweep("g", 4) dims = (fi,) d["a"] = hfarray([1, 2, 3], dims=dims, outputformat="%.5f", unit="V") d["b"] = hfarray([10, 20, 30], dims=dims, outputformat="%.7f", unit="A") d["c"] = hfarray([10, 20, 30, 40], dims=(gi,), outputformat="%.7f", unit="A") w = d.filter(d["Freq[Hz]"] <= 1) self.assertEqual(id(d.comments), id(w.comments)) self.assertEqual(w["Freq[Hz]"].dims, (DimSweep("Freq[Hz]", 2),)) self.assertEqual(w.a.dims, (DimSweep("Freq[Hz]", 2),)) self.assertEqual(w.b.dims, (DimSweep("Freq[Hz]", 2),)) self.assertEqual(w.c.dims, (DimSweep("g", 4),)) self.assertAllclose(w["Freq[Hz]"], [0, 1]) self.assertAllclose(w.a, [1, 2]) self.assertAllclose(w.b, [10, 20]) self.assertAllclose(w.c, [10, 20, 30, 40]) self.assertEqual(w.a.unit, "V") self.assertEqual(w.b.unit, "A") self.assertEqual(w.c.unit, "A")
class Test_replace_dim(TestCase): 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_error_1(self): self.assertRaises(KeyError, self.d.replace_dim, self.hi, self.fi) def test_error_2(self): self.assertRaises(KeyError, self.d.replace_dim, "nonexisting-dim", self.fi) def test_1(self): self.d.replace_dim("g", self.hi) self.assertTrue(self.hi not in self.d["Vds"].dims) self.assertTrue(self.hi in self.d["Id"].dims) def test_2(self): self.d.replace_dim("g", DimRep) self.assertIsInstance(self.d.Id.dims[0], DimRep) self.assertEqual(self.d.Id.dims[0].name, "g")
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_none(self): db = DataBlock() db.x = hfarray(1) fname = testpath / "testdata/hdf5/v02/slask.hdf5" savefun(db, fname) db = readfun(fname) self.assertIsNone(db.blockname) 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_normalize_names_3(self): db = DataBlock() db.b = hfarray(1) db["a1/a2 raw"] = hfarray(1) db["a12"] = hfarray(2) res = common.normalize_names(db) self.assertTrue("a1/a2 raw" in res.vardata) self.assertTrue("a12" in res.vardata)
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 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)
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 test_9(self): d = DataBlock() d.comments = Comments(["Hej=10"]) d.b = hfarray([2], dims=(DimSweep("a", 1), ), unit="V") fname = testpath / "testdata/hdf5/v01/savetest/res_9.hdf5" self.savefun[0](d, fname) d2 = readfun(fname) self.assertEqual(d2.b.unit, "V") fname.unlink()
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_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 read_hdf5_handle(filehandle, **kw): if isinstance(filehandle, h5py.File): db = DataBlock() for k in filehandle: if "dimtype" not in filehandle[k].attrs: db[k] = getvar(filehandle, k) db.comments = Comments() return db else: raise IOError("filehandle should be a h5py File, is: %r" % filehandle)
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 merge_blocks(blocks, hyper=False, indexed=False): db = DataBlock() dimpartialgroups = OrderedDict() for b in blocks: parts = get_partials(b) partgroup = dimpartialgroups.setdefault(tuple(parts.keys()), {}) for k, v in parts.items(): partgroup.setdefault(k, []).extend(v) for idx, dims in enumerate(dimpartialgroups.values(), 1): for k, v in dims.items(): dims = (DimSweep("INDEX%s" % idx, len(v)),) db[k] = hfarray([x.data[0] for x in v], dims=dims, unit=v[0].unit) varnames = set() for b in blocks: for k, v in b.ivardata.items(): if k not in db: db[k] = v for k in b.vardata.keys(): varnames.add(k) for vname in varnames: v = [] for b in blocks: if vname not in b: continue partials = get_partials(b) v.append(b[vname]) if v: k = tuple(partials.keys()) if k: ri = (db[tuple(dimpartialgroups[k].keys())[0]].dims[0],) else: ri = tuple() value = hfarray(v, dims=ri + v[0].dims, unit=v[0].unit) if v[0].dims and isinstance(v[0].dims[0], DimSweep): value = value.reorder_dimensions(v[0].dims[0]) db[vname] = value cmt = Comments() for block in blocks: if block.comments: cmt.extend(block.comments) db.comments = cmt db.blockname = blocks[0].blockname if hyper: for vnames in dimpartialgroups.keys(): if vnames: hyperindex = db[vnames[0]].dims[0] db = db.hyper(vnames, hyperindex, all=True) db = db.squeeze() return db
def test_5(self): d = DataBlock() d.comments = Comments(["Hej=10", "Svejs=11"]) dim = DimSweep("f", 3, outputformat="%.1f") d.freq = dim d.date = hfarray("2012-08-13 08:03:01", dtype="datetime64[us]") fname = testpath / "testdata/hdf5/v02/savetest/res_5.hdf5" self.savefun[0](d, fname) d2 = readfun(fname) fname.unlink() self.assertEqual(d2.date, d.date)
def test_10(self): d = DataBlock() d.comments = Comments(["Hej=10"]) # import pdb;pdb.set_trace() d.b = hfarray([2], dims=(DimSweep("a", 1), ), unit="V") d.b.outputformat = "" fname = testpath / "testdata/hdf5/v02/savetest/res_10.hdf5" self.savefun[0](d, fname) d2 = readfun(fname) self.assertEqual(d2.b.outputformat, "%d") fname.unlink()
def test_1(self): d = DataBlock() d.b = hfarray([2], dims=(DimSweep("a", 1),)) f1 = testpath / "testdata/sp-data/savetest/res_1.txt" f2 = testpath / "testdata/sp-data/savetest/facit_1.txt" hftools.file_formats.spdata.save_spdata(d, f1) with open(f1) as resfil: with open(f2) as facitfil: for idx, (rad1, rad2) in enumerate(zip(resfil, facitfil)): msg = "\nFailed on line %d\n result: %r\n facit: %r" args = (idx + 1, rad1, rad2) self.assertEqual(rad1, rad2, msg=msg % args)
class Test_DataBlock(TestCase): def setUp(self): self.a = DataBlock() def test_report(self): self.a.report() def test_str(self): self.assertEqual(self.a.report(), str(self.a)) def test_guess_1(self): self.assertIsNone(self.a.guess_units(False))
def test_1(self): db = DataBlock() Sdims = (DimRep("freq", [1, 2]) ,DimRep("r", [1]), DimMatrix_i("i", 2), DimMatrix_j("j", 2)) db.S = hfarray(np.array([[11, 12], [21, 22]])[np.newaxis, np.newaxis, :, :] * np.array([[10], [20]])[..., np.newaxis, np.newaxis], dims=Sdims) db.V = hfarray([1.23], Sdims[1:2]) db.Y = hfarray([1.23], (DimRep("k", [1]),)) out = dset.subset_datablock_by_dims(dset.convert_matrices_to_elements(db), Sdims[:-2]) self.assertTrue("V" in out) self.assertTrue("S11" in out) self.assertTrue("S12" in out) self.assertTrue("S21" in out) self.assertTrue("S22" in out) self.assertFalse("Y" in out)
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_wrong_version_2(self): ia = DimSweep("a", 1, unit="") d1 = DataBlock() d1.b = hfarray([2], dims=(ia,)) d2 = DataBlock() d2.b = hfarray([3], dims=(ia,)) fname = testpath / "testdata/hdf5/v02/savetest/res_1.hdf5" with hdf5context(fname, mode="w") as fil: save_hdf5_v01(d1, fil) self.assertRaises(Exception, append_hdf5, d2, fil) # append_hdf5(d2, fil) fname.unlink()
def test_11(self): ia = DimSweep("a", 1) d = DataBlock() d.comments = Comments(["Hej=10"]) # import pdb;pdb.set_trace() d.b = hfarray([u"kalle"], dims=(ia, ), unit="V") d.c = hfarray(u"kalle") fname = testpath / "testdata/hdf5/v02/savetest/res_11.hdf5" self.savefun[0](d, fname) d2 = readfun(fname) self.assertEqual(d2.b[0], u"kalle") self.assertEqual(d2.c, u"kalle") 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/sp-data/savetest/res_4.txt" facitfname = testpath / "testdata/sp-data/savetest/facit_4.txt" hftools.file_formats.spdata.save_spdata(d, fname) with open(fname) as resfil: with open(facitfname) as facitfil: for idx, (rad1, rad2) in enumerate(zip(resfil, facitfil)): msg = "\nFailed on line %d\n result: %r\n facit: %r" args = (idx + 1, rad1, rad2) self.assertEqual(rad1, rad2, msg=msg % args)
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 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_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[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 test_5(self): d = DataBlock() d.comments = Comments(["Hej=10", "Svejs=11"]) dim = DimSweep("f", 3, outputformat="%.1f") d.freq = dim fname = testpath / "testdata/sp-data/savetest/res_5.txt" facitfname = testpath / "testdata/sp-data/savetest/facit_5.txt" with open(fname, "w") as fil: hftools.file_formats.spdata.save_spdata(d, fil) with open(fname) as resfil: with open(facitfname) as facitfil: for idx, (rad1, rad2) in enumerate(zip(resfil, facitfil)): msg = "\nFailed on line %d\n result: %r\n facit: %r" args = (idx + 1, rad1, rad2) self.assertEqual(rad1, rad2, msg=msg % args)
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_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/v01/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 test_error(self): fi = DimSweep("freq", [ 1, ]) db = DataBlock() self.assertRaises(ValueError, dset.subset_datablock_by_dims, db, (fi, fi))
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 db_iterator(indb, fmt_block_generator): datablock = {} used_ivars = set() for vname in indb.vardata.keys(): if ismatrix(indb.vardata[vname]): dimsnames = get_dims_names(*indb[vname].dims)[:-2] names = get_dims_names(*indb[vname].dims)[-2:] used_ivars = used_ivars.union(names) else: dimsnames = get_dims_names(*indb[vname].dims) q = datablock.setdefault(dimsnames, DataBlock()) if q.comments is None: q.comments = indb.comments q[vname] = indb[vname] q[vname].outputformat = indb[vname].outputformat # #Collect dangling ivars # for names in datablock.keys(): used_ivars = used_ivars.union(names) for vname in set(indb.ivardata).difference(used_ivars): q = datablock.setdefault((vname, ), DataBlock()) if q.comments is None: q.comments = Comments() q[vname] = indb.ivardata[vname] first = True #Handle block with scalar data first dbs = [] if tuple() in datablock: dbs.append(datablock.pop(tuple())) for v in datablock.values(): dbs.append(v) for db in dbs: for block_content in make_block_iterator(db, first): if first: first = False else: yield [] for rad in fmt_block_generator(*block_content): yield rad
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 test_1(self): db = DataBlock() Sdims = (DimRep("freq", [1, 2]), DimRep("r", [1]), DimMatrix_i("i", 2), DimMatrix_j("j", 2)) db.S = hfarray( np.array([[11, 12], [21, 22]])[np.newaxis, np.newaxis, :, :] * np.array([[10], [20]])[..., np.newaxis, np.newaxis], dims=Sdims) db.V = hfarray([1.23], Sdims[1:2]) db.Y = hfarray([1.23], (DimRep("k", [1]), )) out = dset.subset_datablock_by_dims( dset.convert_matrices_to_elements(db), Sdims[:-2]) self.assertTrue("V" in out) self.assertTrue("S11" in out) self.assertTrue("S12" in out) self.assertTrue("S21" in out) self.assertTrue("S22" in out) self.assertFalse("Y" in out)
def setUp(self): self.db = db = DataBlock() Sdims = (DimRep("freq", [1, 2]), DimRep("r", [1]), DimMatrix_i("i", 2), DimMatrix_j("j", 2)) db.S = hfarray(np.array([[11, 12], [21, 22]])[None, None, :, :] * np.array([[10], [20]])[..., None, None], dims=Sdims) db.V = hfarray([1.23], Sdims[1:2]) db.Y = hfarray([1.23], (DimRep("k", [1]), ))
def parse_blocks(self, stream): for comments, header, data in stream: db = DataBlock() db.comments = Comments(comments) header = header[0].strip().split("\t") Nhead = len(header) #data = np.array(data) if Nhead != len(data[0]): msg = "Different number of header variables "\ "from data columns" raise SPDataIOError(msg) output = DataDict() for varname, column in zip(header, zip(*data)): output.setdefault(varname, []).append(column) for varname in output: data = output[varname] if len(data) > 1: output[varname] = np.array(output[varname], order="F").T else: output[varname] = np.array(output[varname][0]) freq = DimSweep(header[0], output[header[0]]) db[header[0]] = freq for x in output.keys()[1:]: if output[x].ndim == 1: db[x] = hfarray(output[x], dims=(freq, )) else: repdim = DimRep("rep", output[x].shape[1]) db[x] = hfarray(output[x], dims=(freq, repdim)).squeeze() remove = [] for vname in db.comments.property: if vname[:1] == "@": unit = db.comments.property[vname].unit data = [float(db.comments.property[vname])] db[vname[1:]] = DimPartial(vname[1:], data=data, unit=unit) remove.append(vname) for v in remove: del db.comments.property[v] db.comments.fullcomments = [ com for com in db.comments.fullcomments if not com.startswith("@") ] yield db
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 parse_blocks(self, stream): for comments, vars, blockname, header, attribs, data in stream: db = DataBlock() db.blockname = blockname = blockname[0] #self.header = header = header[0] db.comments = Comments(comments) #data = numpy.array(data) dd = self.proc_data(header, data[:]) for vname in dd: db[vname] = dd[vname] for var in vars: vi = self.proc_var(var) db[vi.name] = vi for var in attribs: name, value = self.proc_attrib(var) db[name] = value self.make_matrices(db, sum(header, [])) yield 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 setUp(self): self.db = db = DataBlock() fi = DimSweep("freq", [1e9, 2e9, 3e9, 4e9], unit="Hz") db.y = hfarray([1, 2, 3, 4], dims=(fi, ), unit="V") db.z = hfarray([1, 2, 3, 4 + 0j], dims=(fi, ), unit="Ohm") self.fig = plt.figure(1) self.ax = plt.subplot(111, projection=self.proj) self.lines = plt.plot(self.db.y) self.outpath = p = path(basepath / "savetest") if not p.exists(): # pragma: no cover p.makedirs()
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") f1 = testpath / "testdata/sp-data/savetest/res_3.txt" f2 = testpath / "testdata/sp-data/savetest/facit_3.txt" hftools.file_formats.spdata.save_spdata(d, f1) with open(f1) as resfil: with open(f2) as facitfil: for idx, (rad1, rad2) in enumerate(zip(resfil, facitfil)): msg = "\nFailed on line %d\n result: %r\n facit: %r" args = (idx + 1, rad1, rad2) self.assertEqual(rad1, rad2, msg=msg % args)
class Test_interpolate(TestCase): def setUp(self): self.d = DataBlock() f1 = DimSweep("freq", [1e9, 2e9, 3e9, 4e9, 5e9]) self.d.y = hfarray([1., 1.1, 1.2, 1.3, 1.4], dims=(f1, )) def test_none(self): fx = DimSweep("freq", [1e9, 5e9]) D = self.d.interpolate(fx) self.assertAllclose(D.y, hfarray([1., 1.4], dims=(fx, ))) def test_none_2(self): fx = DimSweep("freq", [1e9, 5e9]) D = self.d.interpolate(hfarray(fx)) self.assertAllclose(D.y, hfarray([1., 1.4], dims=(fx, ))) def test_none_raise(self): fx = DimSweep("freq", [1.5e9, 5e9]) self.assertRaises(ValueError, self.d.interpolate, fx) def test_linear(self): fx = DimSweep("freq", [1e9, 1.5e9]) D = self.d.interpolate(fx, "linear") self.assertAllclose(D.y, hfarray([1., 1.05], dims=(fx, ))) def test_shape_error(self): dims = (DimSweep("freq", 2), DimSweep("power", 3)) x = hfarray(np.zeros((2, 3), ), dims=dims) self.assertRaises(ValueError, self.d.interpolate, x) def test_no_interp(self): """No dimension to interpolate""" fx = DimSweep("Vds", [1e9, 5e9]) D = self.d.interpolate(fx) self.assertAllclose(D.y, self.d.y) def test_unknown_mode(self): x = DimSweep("freq", 2) self.assertRaises(ValueError, self.d.interpolate, x, "unknown")
def simple_merge_blocks(blocks): if len(blocks) == 1: return blocks[0] ri = DimRep("INDEX", len(blocks)) out = DataBlock() #import pdb;pdb.set_trace() for k in blocks[0].vardata: out[k] = hfarray([b[k] for b in blocks], dims=(ri, ) + blocks[0][k].dims) if blocks[0][k].dims: out[k] = out[k].reorder_dimensions(blocks[0][k].dims[0]) for k in blocks[0].ivardata: if isinstance(blocks[0].ivardata[k], DimPartial): out[k] = hfarray([b.ivardata[k].data[0] for b in blocks], dims=(ri, )) c = [] for b in blocks: if b.comments is not None: c.extend(b.comments.fullcomments) out.comments = Comments() out.comments.fullcomments = c return out
def setUp(self): d = DataBlock() fi = DimSweep("Freq[Hz]", 3) gi = DimRep("g", 2) hi = DimRep("h", 3) ji = DimRep("j", 4) dims = (fi, ) d["a"] = hfarray([3, 2, 1], dims=dims, outputformat="%.5f", unit="V") d["b"] = hfarray([30, 10], dims=(gi, ), outputformat="%.7f", unit="A") d["c"] = hfarray([[30, 20, 10, 40], [20, 30, 40, 10]], dims=(gi, hi), outputformat="%.7f", unit="A") self.d = d
def test_2(self): d = DataBlock() d.comments = Comments(["Vg=10"]) fi = DimSweep("freq", [0e9, 1e9, 2e9], outputformat="%15.2f") dims = ( fi, DimMatrix_i("i", 2), DimMatrix_j("j", 2), ) d.S = hfarray([[[1 + 1j, 1 + 2j], [2 + 1j, 2 + 2j]]] * 3, dims=dims, outputformat="%.3f") filename = testpath / "testdata/touchstone/savetest/res_2.txt" hftools.file_formats.touchstone.save_touchstone(d, filename) resfilename = testpath / "testdata/touchstone/savetest/res_2.txt" facitfilename = testpath / "testdata/touchstone/savetest/facit_2.txt" with open(resfilename) as resfil: with open(facitfilename) as facitfil: for idx, (rad1, rad2) in enumerate(zip(resfil, facitfil)): msg = ("\nFailed on line %d\n result: %r\n facit: %r" % (idx + 1, rad1, rad2)) self.assertEqual(rad1, rad2, msg=msg)
def test_view_1(self): d = DataBlock() d.comments = Comments(["Hej=10"]) fi = DimSweep("Freq[Hz]", 3) dims = (fi, ) 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.view() 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(c.a, d.a) self.assertAllclose(c.b, d.b) self.assertTrue(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, [7, 2, 3]) self.assertAllclose(d.b, [7, 20, 30])
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 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_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 merge_blocks(blocks, hyper=False, indexed=False): db = DataBlock() dimpartialgroups = OrderedDict() for b in blocks: parts = get_partials(b) partgroup = dimpartialgroups.setdefault(tuple(parts.keys()), {}) for k, v in parts.items(): partgroup.setdefault(k, []).extend(v) for idx, dims in enumerate(dimpartialgroups.values(), 1): for k, v in dims.items(): dims = (DimSweep("INDEX%s" % idx, len(v)), ) db[k] = hfarray([x.data[0] for x in v], dims=dims, unit=v[0].unit) varnames = set() for b in blocks: for k, v in b.ivardata.items(): if k not in db: db[k] = v for k in b.vardata.keys(): varnames.add(k) for vname in varnames: v = [] for b in blocks: if vname not in b: continue partials = get_partials(b) v.append(b[vname]) if v: k = tuple(partials.keys()) if k: ri = (db[tuple(dimpartialgroups[k].keys())[0]].dims[0], ) else: ri = tuple() value = hfarray(v, dims=ri + v[0].dims, unit=v[0].unit) if v[0].dims and isinstance(v[0].dims[0], DimSweep): value = value.reorder_dimensions(v[0].dims[0]) db[vname] = value cmt = Comments() for block in blocks: if block.comments: cmt.extend(block.comments) db.comments = cmt db.blockname = blocks[0].blockname if hyper: for vnames in dimpartialgroups.keys(): if vnames: hyperindex = db[vnames[0]].dims[0] db = db.hyper(vnames, hyperindex, all=True) db = db.squeeze() return db
def build_meas_state(data): state_names = data["measstate"][0, 0]["props"][0, 0][0] state_data = data["measstate"][0, 0]["values"][0, 0][0] d = DataBlock() for k, v in zip(state_names, state_data): if k[0] == "Index": d["Index"] = DimPartial("Index", v[0]) else: if v[0].ndim == 1: d[k[0]] = hfarray(make_real(v[0][0])) else: d[k[0]] = hfarray(make_real(v[0])) if d[k[0]].dtype.name.startswith("unicode"): d[k[0]] = hfarray(np.char.encode(d[k[0]], "cp1252", errors="replace"), dims=d[k[0]].dims) return d
def test_wrong_version_1(self): ia = DimSweep("a", 1, unit="") d1 = DataBlock() d1.b = hfarray([2], dims=(ia, )) d2 = DataBlock() d2.b = hfarray([3], dims=(ia, )) fname = testpath / "testdata/hdf5/v02/savetest/res_1.hdf5" with hdf5context(fname, mode="w") as fil: save_hdf5_v01(d1, fil) self.assertRaises(Exception, append_hdf5, d2, fil) # append_hdf5(d2, fil) fname.unlink()
def read_hdf5(h5file, name="datablock", **kw): if isinstance(h5file, string_types): fil = h5py.File(h5file, "r") else: fil = h5file db = DataBlock() grp = fil[name] blockname = grp.attrs["Blockname"] if blockname.lower() == "none": blockname = None db.blockname = blockname comments = grp["Comments"] if "fullcomments" in comments and len(comments["fullcomments"]): db.comments = Comments([ cast_unicode(x).strip() for x in np.array(comments["fullcomments"]) ]) else: db.comments = Comments() ivardata = grp["ivardata"] vardata = grp["vardata"] for k in ivardata: v = ivardata[k] datadtype = v.attrs[r"info\dtype"] or None dimcls = dims_dict.get(v.attrs[r"info\class"], DimRep) unit = str(v.attrs.get(r"info\unit", "none")) if unit.lower() == "none": unit = None vdata = np.array(np.array(v), dtype=datadtype) dim = dimcls(k, vdata, unit=unit) db[k] = dim for k in vardata: v = vardata[k] datadtype = v.attrs[r"data\dtype"] or None dims = tuple(db.ivardata[cast_unicode(dimname)] for dimname in v.attrs[r"info\name"]) unit = cast_unicode(v.attrs.get(r"data\unit", "none")) if unit.lower() == "none": unit = None db[k] = hfarray(np.array(v), dtype=datadtype, dims=dims, unit=unit) if isinstance(h5file, string_types): fil.close() if kw.get("property_to_vars", False): db.values_from_property() return db
def setUp(self): d = DataBlock() gi = DimSweep("Index", 6) ri = DimRep("Rep", 2) dims = (gi, ) d["a"] = hfarray([1, 1, 2, 2, 3, 3], dims=dims, outputformat="%.5f", unit="V") d["b"] = hfarray([10, 20, 10, 20, 10, 20], dims=(gi, ), outputformat="%.7f", unit="A") d["c"] = hfarray([10.1, 20.1, 10.2, 20.2, 10.3, 20.3], dims=(gi, ), outputformat="%.7f", unit="A") d["d"] = hfarray([17, 19], dims=(ri, ), outputformat="%.7f", unit="A") self.d = d
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")