| 1 |
import os |
| 2 |
import unittest |
| 3 |
|
| 4 |
import numpy as np |
| 5 |
|
| 6 |
import skrf as rf |
| 7 |
|
| 8 |
|
| 9 |
class MdifTestCase(unittest.TestCase): |
| 10 |
""" |
| 11 |
Test the IO of GMDIF files |
| 12 |
""" |
| 13 |
def setUp(self): |
| 14 |
""" |
| 15 |
Sets up the test directory |
| 16 |
""" |
| 17 |
self.test_dir = os.path.dirname(os.path.abspath(__file__))+'/MDIF_CITI_MDL/' |
| 18 |
|
| 19 |
|
| 20 |
self.oneport_example1 = rf.Mdif(self.test_dir + 'test_1p_gmdif.mdf') |
| 21 |
self.oneport_example2 = rf.Mdif(self.test_dir + 'test_1p_gmdif_2.mdf') |
| 22 |
self.twoport_example1 = rf.Mdif(self.test_dir + 'test_2p_gmdif.mdf') |
| 23 |
self.twoport_example2 = rf.Mdif(self.test_dir + 'test_2p_gmdif_2.mdf') |
| 24 |
self.twoport_example3 = rf.Mdif(self.test_dir + 'test_2p_gmdif_3.mdf') |
| 25 |
self.twoport_example_z = rf.Mdif(self.test_dir + 'test_2p_gmdif_z.mdf') |
| 26 |
self.twoport_example_yz = rf.Mdif(self.test_dir + 'test_2p_gmdif_yz.mdf') |
| 27 |
|
| 28 |
self.fourport_example1 = rf.Mdif(self.test_dir + 'test_4p_gmdif.mdf') |
| 29 |
|
| 30 |
self.examples = [self.oneport_example1, self.oneport_example2, |
| 31 |
self.twoport_example1, self.twoport_example2, |
| 32 |
self.twoport_example3, |
| 33 |
self.fourport_example1] |
| 34 |
|
| 35 |
|
| 36 |
file = open(self.test_dir + 'test_1p_gmdif.mdf') |
| 37 |
self.oneport_example1_from_fo = rf.Mdif(file) |
| 38 |
|
| 39 |
def test_equal(self): |
| 40 |
""" Test the comparison between two Mdif objects """ |
| 41 |
self.assertTrue(self.oneport_example1, self.oneport_example1) |
| 42 |
self.assertTrue(self.oneport_example1, self.oneport_example1_from_fo) |
| 43 |
self.assertTrue(self.twoport_example_z, self.twoport_example_yz) |
| 44 |
|
| 45 |
def test_to_networkset(self): |
| 46 |
""" Test if MDIF are correctly converted into NetworkSet """ |
| 47 |
for example in self.examples: |
| 48 |
self.assertIsInstance(example.to_networkset(), rf.NetworkSet) |
| 49 |
|
| 50 |
def test_params(self): |
| 51 |
""" Test if the params are correctly parsed from the MDIF files """ |
| 52 |
self.assertEqual(self.oneport_example1.params, ['Cm']) |
| 53 |
self.assertEqual(self.oneport_example2.params, ['mag', 'Phase']) |
| 54 |
self.assertEqual(self.twoport_example1.params, ['Cm']) |
| 55 |
self.assertEqual(self.twoport_example2.params, ['L1']) |
| 56 |
self.assertEqual(self.fourport_example1.params, ['Cm']) |
| 57 |
|
| 58 |
def test_to_to_networkset_params(self): |
| 59 |
""" Test if the params are correctly passed to the NetworkSet """ |
| 60 |
self.assertEqual(self.oneport_example1.to_networkset().params, ['Cm']) |
| 61 |
self.assertEqual(self.oneport_example2.to_networkset().params, ['mag', 'Phase']) |
| 62 |
self.assertEqual(self.twoport_example1.to_networkset().params, ['Cm']) |
| 63 |
self.assertEqual(self.twoport_example2.to_networkset().params, ['L1']) |
| 64 |
self.assertEqual(self.fourport_example1.to_networkset().params, ['Cm']) |
| 65 |
|
| 66 |
def test_to_networkset_values(self): |
| 67 |
""" Test if we extract correctly the numerical values """ |
| 68 |
|
| 69 |
ntwk = self.oneport_example1.to_networkset().sel({'Cm': 7e-16})[0] |
| 70 |
np.testing.assert_equal(ntwk.s[0,0], 0.999999951-0.000312274302j) |
| 71 |
np.testing.assert_equal(ntwk.f[0], 710000000) |
| 72 |
|
| 73 |
ntwk = self.oneport_example2.to_networkset().sel({'mag': 0.25, 'Phase': 180})[0] |
| 74 |
np.testing.assert_equal(ntwk.s_mag[0,0], 0.1) |
| 75 |
np.testing.assert_equal(ntwk.s_deg[0,0], 180) |
| 76 |
np.testing.assert_equal(ntwk.f[0], 1e9) |
| 77 |
|
| 78 |
ntwk = self.twoport_example2.to_networkset().sel({'L1': 10})[0] |
| 79 |
np.testing.assert_almost_equal(ntwk.s_db[0,0,0], -0.099191746) |
| 80 |
np.testing.assert_almost_equal(ntwk.s_deg[0,0,0], 64.474118) |
| 81 |
np.testing.assert_almost_equal(ntwk.s_db[0,0,1], -40.635912) |
| 82 |
np.testing.assert_almost_equal(ntwk.s_deg[0,0,1], 154.35237) |
| 83 |
np.testing.assert_almost_equal(ntwk.s_db[0,1,0], -42.635912) |
| 84 |
np.testing.assert_almost_equal(ntwk.s_deg[0,1,0], 150.35237) |
| 85 |
np.testing.assert_equal(ntwk.f[0], 1e9) |
| 86 |
|
| 87 |
def test_comment_after_BEGIN(self): |
| 88 |
"""Test reading a MDIF file which has comments after BEGIN ACDATA. """ |
| 89 |
file = self.test_dir + 'test_comment_after_BEGIN.mdf' |
| 90 |
|
| 91 |
mdif = rf.Mdif(file) |
| 92 |
|
| 93 |
ns = rf.NetworkSet.from_mdif(file) |
| 94 |
|
| 95 |
def test_read_and_write_back_noise(self): |
| 96 |
net = rf.Network("skrf/io/tests/ts/ex_18.s2p") |
| 97 |
nset1 = rf.NetworkSet([net.copy() for _i in range(4)]) |
| 98 |
|
| 99 |
|
| 100 |
nset1.write_mdif("out1.mdf") |
| 101 |
nset2 = rf.NetworkSet.from_mdif("out1.mdf") |
| 102 |
nset2.write_mdif("out2.mdf") |
| 103 |
nset3 = rf.NetworkSet.from_mdif("out2.mdf") |
| 104 |
nset3.write_mdif("out3.mdf") |
| 105 |
nset4 = rf.NetworkSet.from_mdif("out3.mdf") |
| 106 |
assert nset1 == nset4 |
| 107 |
|
| 108 |
for n1, n2 in zip(nset1, nset4): |
| 109 |
np.testing.assert_allclose(n1.noise, n2.noise) |
| 110 |
|
| 111 |
os.remove("out1.mdf") |
| 112 |
os.remove("out2.mdf") |
| 113 |
os.remove("out3.mdf") |
| 114 |
|
| 115 |
|
| 116 |
suite = unittest.TestLoader().loadTestsFromTestCase(MdifTestCase) |
| 117 |
unittest.TextTestRunner(verbosity=2).run(suite) |