Debian Codegraph @

skrf/tests/test_vectorfitting.py

raw symbols SCIP
1 import os
2 import sys
3 import tempfile
4 import unittest
5 from pathlib import Path
6
7 import numpy as np
8 import pytest
9
10 import skrf
11
12
13 class VectorFittingTestCase(unittest.TestCase):
14
15 @pytest.mark.filterwarnings('ignore::UserWarning')
16 def test_ringslot_with_proportional(self):
17 # perform the fit
18 nw = skrf.data.ring_slot
19 vf = skrf.vectorFitting.VectorFitting(nw)
20 vf.vector_fit(n_poles_real=2, n_poles_cmplx=0, fit_proportional=True, fit_constant=True)
21 self.assertLess(vf.get_rms_error(), 0.02)
22
23 @pytest.mark.filterwarnings('ignore::UserWarning')
24 def test_ringslot_default_log(self):
25 # perform the fit without proportional term
26 nw = skrf.data.ring_slot
27 vf = skrf.vectorFitting.VectorFitting(nw)
28 vf.vector_fit(n_poles_real=4, n_poles_cmplx=0, init_pole_spacing='log')
29 self.assertLess(vf.get_rms_error(), 0.01)
30
31 @pytest.mark.filterwarnings('ignore::UserWarning')
32 def test_ringslot_without_prop_const(self):
33 # perform the fit without proportional term
34 nw = skrf.data.ring_slot
35 vf = skrf.vectorFitting.VectorFitting(nw)
36 vf.vector_fit(n_poles_real=4, n_poles_cmplx=0, fit_proportional=False, fit_constant=False)
37 self.assertLess(vf.get_rms_error(), 0.01)
38
39 @pytest.mark.filterwarnings('ignore::UserWarning')
40 def test_ringslot_custompoles(self):
41 # perform the fit with custom initial poles
42 nw = skrf.data.ring_slot
43 vf = skrf.vectorFitting.VectorFitting(nw)
44 vf.poles = 2 * np.pi * np.array([-100e9, -10e9 + 100e9j])
45 vf.vector_fit(init_pole_spacing='custom')
46 self.assertLess(vf.get_rms_error(), 0.01)
47
48 def test_190ghz_measured(self):
49 # perform the fit without proportional term
50 s2p_file = Path(__file__).parent.parent.parent / 'doc/source/examples/vectorfitting/190ghz_tx_measured.S2P'
51 nw = skrf.network.Network(s2p_file)
52 vf = skrf.vectorFitting.VectorFitting(nw)
53 vf.vector_fit(n_poles_real=4, n_poles_cmplx=4, fit_proportional=False, fit_constant=True)
54 self.assertLess(vf.get_rms_error(), 0.02)
55
56 def test_no_convergence(self):
57 s2p_file = Path(__file__).parent.parent.parent / 'doc/source/examples/vectorfitting/190ghz_tx_measured.S2P'
58 # perform a bad fit that does not converge and check if a RuntimeWarning is given
59 nw = skrf.network.Network(s2p_file)
60 vf = skrf.vectorFitting.VectorFitting(nw)
61
62 with pytest.warns(RuntimeWarning) as record:
63 vf.vector_fit(n_poles_real=0, n_poles_cmplx=5, fit_proportional=False, fit_constant=True)
64
65 assert len(record) == 1
66
67 def test_dc_enforcement(self):
68 # perform the fit on data including a dc sample (0 Hz)
69 s4p_file = Path(__file__).parent / 'cst_example_4ports.s4p'
70 nw = skrf.Network(s4p_file)
71 vf = skrf.VectorFitting(nw)
72 vf.auto_fit()
73
74 # rough check on fit quality
75 self.assertLess(vf.get_rms_error(), 0.2)
76
77 # evaluate model error at the dc point (real part)
78 # the dc point should always be real (it still often has a tiny imaginary part)
79 vf_fit = np.empty(nw.nports ** 2, dtype=complex)
80 abs_real_errors = np.empty(nw.nports ** 2)
81
82 for i in range(nw.nports):
83 for j in range(nw.nports):
84 vf_ij = vf.get_model_response(i, j, nw.f[0])
85 vf_fit[i * nw.nports + j] = vf_ij
86 abs_real_errors[i * nw.nports + j] = np.abs(np.real(vf_ij - nw.s[0, i, j]))
87
88 self.assertTrue(np.all(abs_real_errors < 1e-10))
89
90
91 def test_read_write_npz(self):
92 # fit ring slot example network
93 nw = skrf.data.ring_slot
94 vf = skrf.vectorFitting.VectorFitting(nw)
95
96 with pytest.warns(UserWarning) as record:
97 vf.vector_fit(n_poles_real=3, n_poles_cmplx=0)
98
99 self.assertTrue(len(record) == 1)
100
101 # export (write) fitted parameters to .npz file in tmp directory
102 with tempfile.TemporaryDirectory() as name:
103 vf.write_npz(name)
104
105 # create a new vector fitting instance and import (read) those fitted parameters
106 vf2 = skrf.vectorFitting.VectorFitting(nw)
107 vf2.read_npz(os.path.join(name, f'coefficients_{nw.name}.npz'))
108
109 # compare both sets of parameters
110 self.assertTrue(np.allclose(vf.poles, vf2.poles))
111 self.assertTrue(np.allclose(vf.residues, vf2.residues))
112 self.assertTrue(np.allclose(vf.proportional_coeff, vf2.proportional_coeff))
113 self.assertTrue(np.allclose(vf.constant_coeff, vf2.constant_coeff))
114
115 @pytest.mark.skipif("matplotlib" in sys.modules, reason="Raise Error only if matplotlib is not installed.")
116 def test_matplotlib_missing(self):
117 vf = skrf.vectorFitting.VectorFitting(skrf.data.ring_slot)
118 with self.assertRaises(RuntimeError):
119 vf.plot_convergence()
120
121 def test_passivity_enforcement(self):
122 vf = skrf.VectorFitting(None)
123
124 # non-passive example parameters from Gustavsen's passivity assessment paper:
125 vf.poles = np.array([-1, -5 + 6j])
126 vf.residues = np.array([[0.3, 4 + 5j], [0.1, 2 + 3j], [0.1, 2 + 3j], [0.4, 3 + 4j]])
127 vf.constant_coeff = np.array([0.2, 0.1, 0.1, 0.3])
128 vf.proportional_coeff = np.array([0.0, 0.0, 0.0, 0.0])
129
130 # test if model is not passive
131 violation_bands = vf.passivity_test()
132 self.assertTrue(np.allclose(violation_bands, np.array([4.2472, 16.434]) / 2 / np.pi, rtol=1e-3, atol=1e-3))
133 self.assertFalse(vf.is_passive())
134
135 # enforce passivity with default settings
136 vf.passivity_enforce(f_max=2)
137
138 # check if model is now passive
139 self.assertTrue(vf.is_passive())
140
141 def test_autofit(self):
142 vf = skrf.VectorFitting(skrf.data.ring_slot)
143 vf.auto_fit()
144
145 self.assertTrue(vf.get_model_order(vf.poles) == 6)
146 self.assertTrue(np.sum(vf.poles.imag == 0.0) == 0)
147 self.assertTrue(np.sum(vf.poles.imag > 0.0) == 3)
148 self.assertLess(vf.get_rms_error(), 1e-05)
149
150
151 suite = unittest.TestLoader().loadTestsFromTestCase(VectorFittingTestCase)
152 unittest.TextTestRunner(verbosity=2).run(suite)