Debian Codegraph @

skrf/io/tests/test_touchstone.py

raw symbols SCIP
1 import os
2 import unittest
3 from pathlib import Path
4 from zipfile import ZipFile
5
6 import numpy as np
7 import pytest
8
9 from skrf import Network
10 from skrf.io.touchstone import Touchstone, read_zipped_touchstones
11
12
13 class TouchstoneTestCase(unittest.TestCase):
14 """
15 TouchstoneTestCase tests the IO of Touchstone files
16 """
17 def setUp(self):
18 """
19 Sets up the test directory
20 """
21 self.test_dir = os.path.dirname(os.path.abspath(__file__))+'/'
22
23 def test_read_data(self):
24 """
25 This test reads data from simple_touchstone.s2p and compares with known
26 true values.
27 """
28 filename = os.path.join(self.test_dir, 'simple_touchstone.s2p')
29 touch = Touchstone(filename)
30 f, s = touch.get_sparameter_arrays()
31 z0 = complex(touch.resistance)
32 f_true = np.array([1.00000000e+09, 1.10000000e+09])
33 s_true = np.array([[[1.+2.j, 5.+6.j], [3.+4.j, 7.+8.j]],
34 [[9.+10.j, 13.+14.j], [11.+12.j, 15.+16.j]]])
35 z0_true = 50+50j
36
37 comments_after_option_line = "freq ReS11 ImS11 ReS21 ImS21 ReS12 ImS12 ReS22 ImS22"
38
39 self.assertTrue((f == f_true).all())
40 self.assertTrue((s == s_true).all())
41 self.assertTrue(z0 == z0_true)
42 self.assertTrue(touch.comments_after_option_line == comments_after_option_line)
43
44 def test_double_option_line(self):
45 filename = os.path.join(self.test_dir, 'double_option_line.s2p')
46 touch = Touchstone(filename)
47
48 self.assertTrue(touch.resistance == 10+10j)
49
50 def test_read_with_special_encoding(self):
51 """
52 Read Touchstone files with various file encoding
53 """
54 filename_utf8_sig = os.path.join(self.test_dir, 'test_encoding_UTF-8-SIG.s2p')
55 filename_latin1 = os.path.join(self.test_dir, 'test_encoding_ISO-8859-1.s2p')
56 filename_unknown = os.path.join(self.test_dir, 'test_encoding_unknown.s2p')
57
58 # most common situation: try and error guessing the encoding
59 Touchstone(filename_utf8_sig)
60 Touchstone(filename_latin1)
61 Touchstone(filename_unknown)
62
63 # specify the encoding
64 Touchstone(filename_latin1, encoding='ISO-8859-1')
65 Touchstone(filename_utf8_sig, encoding='utf_8_sig')
66
67 def test_read_from_fid(self):
68 """
69 This tests reading touch stone data from a file object as compared with
70 a string path and name of the file.
71 """
72 with open(os.path.join(self.test_dir, 'simple_touchstone.s2p')) as fid:
73 touch = Touchstone(fid)
74 f, s = touch.get_sparameter_arrays()
75 z0 = complex(touch.resistance)
76 f_true = np.array([1.00000000e+09, 1.10000000e+09])
77 s_true = np.array([[[1.+2.j, 5.+6.j], [3.+4.j, 7.+8.j]],
78 [[9.+10.j, 13.+14.j], [11.+12.j, 15.+16.j]]])
79 z0_true = 50+50j
80
81 self.assertTrue((f == f_true).all())
82 self.assertTrue((s == s_true).all())
83 self.assertTrue(z0 == z0_true)
84
85 def test_get_sparameter_data(self):
86 """
87 This tests the get_sparameter_data function.
88
89 """
90 with open(os.path.join(self.test_dir, 'simple_touchstone.s2p')) as fid:
91 touch = Touchstone(fid)
92
93 expected_keys = ["frequency", "S11R", "S11I", "S12R", "S12I",
94 "S21R", "S21I", "S22R", "S22I", ]
95
96 unexpected_keys = ['S11DB', 'S11M', ]
97
98
99 with pytest.warns(DeprecationWarning):
100 # get dict data structure
101 sp_ri = touch.get_sparameter_data(format="ri")
102 # Get dict data in db to check ri -> db/angle conversion
103 sp_db = touch.get_sparameter_data(format="db")
104
105 # test data structure
106 for ek in expected_keys:
107 self.assertTrue(ek in sp_ri)
108
109 for uk in unexpected_keys:
110 self.assertFalse(uk in sp_ri)
111
112 # test data contents
113 expected_sp_ri = {
114 'frequency': np.array([1.0e+09, 1.1e+09]),
115 'S11R': np.array([1., 9.]),
116 'S11I': np.array([ 2., 10.]),
117 'S21R': np.array([ 3., 11.]),
118 'S21I': np.array([ 4., 12.]),
119 'S12R': np.array([ 5., 13.]),
120 'S12I': np.array([ 6., 14.]),
121 'S22R': np.array([ 7., 15.]),
122 'S22I': np.array([ 8., 16.]),
123 }
124
125 S11 = np.array([1., 9.]) + 1j*np.array([ 2., 10.])
126 S21 = np.array([ 3., 11.]) + 1j*np.array([ 4., 12.])
127 S12 = np.array([ 5., 13.]) + 1j*np.array([ 6., 14.])
128 S22 = np.array([ 7., 15.]) + 1j*np.array([ 8., 16.])
129 expected_sp_db = {
130 'frequency': np.array([1.0e+09, 1.1e+09]),
131 'S11DB': 20*np.log10(np.abs(S11)),
132 'S11A': np.angle(S11, deg=True),
133 'S21DB': 20*np.log10(np.abs(S21)),
134 'S21A': np.angle(S21, deg=True),
135 'S12DB': 20*np.log10(np.abs(S12)),
136 'S12A': np.angle(S12, deg=True),
137 'S22DB': 20*np.log10(np.abs(S22)),
138 'S22A': np.angle(S22, deg=True),
139 }
140
141 for k in sp_ri:
142 self.assertTrue(k in expected_sp_ri)
143
144 self.assertTrue( (expected_sp_ri[k] == sp_ri[k]).all(),
145 msg=f'Field {k} does not match. Expected "{expected_sp_ri[k]}", got "{sp_ri[k]}"')
146
147 for k in sp_db:
148 self.assertTrue(k in expected_sp_db)
149
150 self.assertTrue( (expected_sp_db[k] == sp_db[k]).all(),
151 msg=f'Field {k} does not match. Expected "{expected_sp_db[k]}", got "{sp_db[k]}"')
152
153
154 with pytest.warns(DeprecationWarning):
155 for k, v in zip(touch.get_sparameter_names(), touch.sparameters.T):
156 if k[0] != 'S':
157 # frequency doesn't match because of Hz vs GHz.
158 continue
159 self.assertTrue(np.all(expected_sp_ri[k] == v))
160
161
162 def test_HFSS_touchstone_files(self):
163 """
164 HFSS can export additional information in the Touchstone file
165 such as gamma and z0 for each port. However, the way there are stored
166 depend of the HFSS version...
167
168 In versions before 2020 R2, data were stored as following:
169
170 ! Gamma ! re1 im2 re2 im2 re3 im3 re4 im4
171 ! re5 im5 re6 im6 re7 im7 re8 im8
172 ! re9 im9 [etc]
173 ! Port Impedancere1 im2 re2 im2 re3 im3 re4 im4
174 ! re5 im5 re6 im6 re7 im7 re8 im8
175 ! re9 im9 [etc]
176 [NB: there is an extra ! before re1 for Gamma]
177 [NB: re1 value is stuck to the 'e' of Impedance]
178
179 Since version 2020 R2n the data are stored in a single line:
180
181 ! Gamma re1 im2 re2 im2 re3 im3 re4 im4 re5 im5 re6 im6 re7 im7 re8 im8 [etc]
182 ! Port Impedance re1 im2 re2 im2 re3 im3 re4 im4 re5 im5 re6 im6 re7 im7 re8 im8 [etc]
183 [NB: re1 value is no more stuck to the 'e' of Impedance]
184
185 This test checks that the shape of gamma and z0 matche the rank of the Network
186 for Touchstone files of various port obtained from different HFSS version
187 """
188 HFSS_RELEASES= ['HFSS_2019R2', 'HFSS_2020R2']
189
190 p = Path(self.test_dir)
191 for hfss_release in HFSS_RELEASES:
192 for sNp_file in p.glob(hfss_release+'/*.s*'):
193 touchst = Touchstone(sNp_file.as_posix())
194 gamma, z0 = touchst.get_gamma_z0()
195 print(z0)
196
197 assert(gamma.shape[-1] == touchst.rank)
198 assert(z0.shape[-1] == touchst.rank)
199
200
201 def test_touchstone_2(self):
202 net = Touchstone(os.path.join(self.test_dir, "ts/ansys.ts"))
203
204 assert net.port_names[0] == "U29_B6_1024G_EAS3QB_A_DBI.37.FD_0-1"
205 assert net.port_names[1] == "U29_B6_1024G_EAS3QB_A_DBI.38.GND"
206 assert net.port_names[2] == "U40_178BGA.E10.FD_0-1"
207
208 def test_ansys_modal_data(self):
209 net = Touchstone(os.path.join(self.test_dir, "ansys_modal_data.s2p"))
210 z0 = np.array([
211 [51. +1.j, 52. +2.j],
212 [61.+11.j, 62.+12.j]
213 ])
214 assert np.allclose(net.z0, z0)
215
216 @pytest.mark.skip
217 def test_ansys_terminal_data(self):
218 net = Touchstone(os.path.join(self.test_dir, "ansys_terminal_data.s4p"))
219
220 z0 = np.array([
221 [51. +1.j, 52. +2.j, 53. +3.j, 54. +4.j],
222 [61.+11.j, 62.+12.j, 63.+13.j, 64.+14.j]
223 ])
224 assert np.allclose(net.z0, z0)
225
226 def test_read_zipped_touchstones(self):
227 file = ZipFile(os.path.join(self.test_dir, "ntwk_zip.zip"))
228 ntwk1 = Network(os.path.join(self.test_dir, "ntwk1.s2p"))
229 ntwk2 = Network(os.path.join(self.test_dir, "ntwk2.s2p"))
230 ntwk3 = Network(os.path.join(self.test_dir, "ntwk3.s2p"))
231
232 read1 = read_zipped_touchstones(file, "Folder1")
233 read2 = read_zipped_touchstones(file, "Folder2")
234 read3 = read_zipped_touchstones(file)
235
236 assert read1 == {"ntwk1": ntwk1}
237 assert read2 == {"ntwk1": ntwk1, "ntwk2": ntwk2}
238 assert read3 == {"ntwk3": ntwk3}
239
240
241 suite = unittest.TestLoader().loadTestsFromTestCase(TouchstoneTestCase)
242 unittest.TextTestRunner(verbosity=2).run(suite)