Debian Codegraph @

skrf/tests/test_frequency.py

raw symbols SCIP
1 import os
2 import unittest
3
4 import numpy as np
5
6 import skrf as rf
7 from skrf.frequency import InvalidFrequencyWarning
8
9
10 class FrequencyTestCase(unittest.TestCase):
11 """ """
12
13 def setUp(self):
14 """ """
15 self.test_dir = os.path.dirname(os.path.abspath(__file__)) + "/"
16
17 def test_create_linear_sweep(self):
18 freq = rf.Frequency(1, 10, 10, "ghz")
19 self.assertTrue((freq.f == np.linspace(1, 10, 10) * 1e9).all())
20 self.assertTrue((freq.f_scaled == np.linspace(1, 10, 10)).all())
21 self.assertTrue(freq.sweep_type == "lin")
22
23 def test_create_log_sweep(self):
24 freq = rf.Frequency(1, 10, 10, "ghz", sweep_type="log")
25 # Check end points
26 self.assertTrue(freq.f[0] == 1e9)
27 self.assertTrue(freq.f[-1] == 10e9)
28 spacing = [freq.f[i + 1] / freq.f[i] for i in range(len(freq.f) - 1)]
29 # Check that frequency is increasing
30 self.assertTrue(all(s > 1 for s in spacing))
31 # Check that ratio of adjacent frequency points is identical
32 self.assertTrue(all(abs(spacing[i] - spacing[0]) < 1e-10 for i in range(len(spacing))))
33 self.assertTrue(freq.sweep_type == "log")
34
35 def test_create_rando_sweep(self):
36 f = np.array([1, 5, 200])
37 freq = rf.Frequency.from_f(f, unit="khz")
38 self.assertTrue((freq.f == f * 1e3).all())
39 self.assertTrue((freq.f_scaled == f).all())
40
41 # TODO : in next release
42 # with self.assertRaises(AttributeError):
43 # # number of point is a property and can't be set
44 # freq.npoints = 10
45
46 def test_rando_sweep_from_touchstone(self):
47 """
48 this also tests the ability to read a touchstone file.
49 """
50 rando_sweep_ntwk = rf.Network(os.path.join(self.test_dir, "ntwk_arbitrary_frequency.s2p"))
51 self.assertTrue((rando_sweep_ntwk.f == np.array([1, 4, 10, 20])).all())
52
53 def test_slicer(self):
54 a = rf.Frequency.from_f([1, 2, 4, 5, 6], unit="GHz")
55
56 b = a["2-5ghz"]
57 tinyfloat = 1e-12
58 self.assertTrue((abs(b.f - [2e9, 4e9, 5e9]) < tinyfloat).all())
59
60 def test_frequency_check(self):
61 with self.assertWarns(InvalidFrequencyWarning):
62 freq = rf.Frequency.from_f([2, 1], unit="Hz")
63
64 with self.assertWarns(InvalidFrequencyWarning):
65 freq = rf.Frequency.from_f([1, 2, 2], unit="Hz")
66
67 with self.assertWarns(InvalidFrequencyWarning):
68 freq = rf.Frequency.from_f([1, 2, 2], unit="Hz")
69 inv = freq.drop_non_monotonic_increasing()
70 self.assertListEqual(inv, [2])
71 self.assertTrue(np.allclose(freq.f, [1, 2]))
72
73 def test_immutability(self):
74 """
75 To avoid corner cases, it is not be possible to change the
76 frequency points directly.
77 """
78 a = rf.Frequency.from_f([1, 2, 4, 5, 6], unit="Hz")
79 with self.assertRaises(AttributeError):
80 a.f = [1, 2]
81 with self.assertRaises(AttributeError):
82 a.npoints = 10
83 with self.assertRaises(AttributeError):
84 a.start = 2
85 with self.assertRaises(AttributeError):
86 a.stop = 10
87
88 def test_frequency_math(self):
89 from operator import add, floordiv, mod, mul, sub, truediv
90
91 # Test all the basic operators
92 for op in (add, sub, mul, floordiv, truediv, mod):
93 # Test 2 Frequency objects
94 a = rf.Frequency(1, 10, 10, "GHz")
95 b = rf.Frequency.from_f(10, "Hz")
96 self.assertTrue(np.array_equal(op(a, b).f, op(a.f, b.f)))
97
98 # Test a Frequency object and an integer
99 c = rf.Frequency(1, 10, 10, "GHz")
100 d = 5
101 self.assertTrue(np.array_equal(op(c, d).f, op(c.f, d)))
102
103 # Test a Frequency object and a float
104 e = rf.Frequency(1, 10, 10, "GHz")
105 f_ = 5.31
106 self.assertTrue(np.array_equal(op(e, f_).f, op(e.f, f_)))
107
108 # Test a Frequency object and a numpy array of the appropriate length
109 g = rf.Frequency(1, 10, 10, "GHz")
110 h = np.linspace(10, 100, g.f.size)
111 self.assertTrue(np.array_equal(op(g, h).f, op(g.f, h)))
112
113 # Test a Frequency object and a numpy array of an inappropriate length
114 i = rf.Frequency(1, 10, 10, "GHz")
115 j = np.linspace(10, 100, g.f.size * 2)
116 with self.assertRaises(ValueError) as context:
117 np.array_equal(op(i, j).f, op(i.f, j))
118
119 suite = unittest.TestLoader().loadTestsFromTestCase(FrequencyTestCase)
120 unittest.TextTestRunner(verbosity=2).run(suite)