Debian Codegraph @

test/test_mdf4.py

raw
1 #!/usr/bin/env python
2 from pathlib import Path
3 import tempfile
4 import unittest
5
6 import numpy as np
7
8 from asammdf import MDF, Signal
9 from asammdf.blocks.mdf_v4 import MDF4
10
11 CHANNEL_LEN = 100000
12
13
14 class TestMDF4(unittest.TestCase):
15 tempdir: tempfile.TemporaryDirectory[str]
16
17 @classmethod
18 def setUpClass(cls) -> None:
19 cls.tempdir = tempfile.TemporaryDirectory()
20
21 def test_measurement(self) -> None:
22 self.assertTrue(MDF4)
23
24 def test_read_mdf4_00(self) -> None:
25 seed = np.random.randint(0, 2**31)
26
27 np.random.seed(seed)
28 print("Read 4.00 using seed =", seed)
29
30 sig_int = Signal(
31 np.random.randint(-(2**31), 2**31, CHANNEL_LEN),
32 np.arange(CHANNEL_LEN),
33 name="Integer Channel",
34 unit="unit1",
35 )
36
37 sig_float = Signal(
38 np.random.random(CHANNEL_LEN),
39 np.arange(CHANNEL_LEN),
40 name="Float Channel",
41 unit="unit2",
42 )
43
44 with MDF(version="4.00") as mdf:
45 mdf.append([sig_int, sig_float], common_timebase=True)
46 outfile = mdf.save(Path(TestMDF4.tempdir.name) / "tmp", overwrite=True)
47
48 with MDF(outfile) as mdf:
49 ret_sig_int = mdf.get(sig_int.name)
50 ret_sig_float = mdf.get(sig_float.name)
51
52 self.assertTrue(np.array_equal(ret_sig_int.samples, sig_int.samples))
53 self.assertTrue(np.array_equal(ret_sig_float.samples, sig_float.samples))
54
55 def test_read_mdf4_10(self) -> None:
56 seed = np.random.randint(0, 2**31)
57
58 np.random.seed(seed)
59 print("Read 4.10 using seed =", seed)
60
61 sig_int = Signal(
62 np.random.randint(-(2**31), 2**31, CHANNEL_LEN),
63 np.arange(CHANNEL_LEN),
64 name="Integer Channel",
65 unit="unit1",
66 )
67
68 sig_float = Signal(
69 np.random.random(CHANNEL_LEN),
70 np.arange(CHANNEL_LEN),
71 name="Float Channel",
72 unit="unit2",
73 )
74
75 with MDF(version="4.10") as mdf:
76 mdf.append([sig_int, sig_float], common_timebase=True)
77 outfile = mdf.save(Path(TestMDF4.tempdir.name) / "tmp", overwrite=True)
78
79 with MDF(outfile) as mdf:
80 ret_sig_int = mdf.get(sig_int.name)
81 ret_sig_float = mdf.get(sig_float.name)
82
83 self.assertTrue(np.array_equal(ret_sig_int.samples, sig_int.samples))
84 self.assertTrue(np.array_equal(ret_sig_float.samples, sig_float.samples))
85
86 def test_attachment_blocks_wo_filename(self) -> None:
87 original_data = b"Testing attachemnt block\nTest line 1"
88 mdf = MDF()
89 mdf.attach(
90 original_data,
91 file_name=None,
92 comment="",
93 compression=True,
94 mime=r"text/plain",
95 embedded=True,
96 )
97 outfile = mdf.save(Path(TestMDF4.tempdir.name) / "attachment.mf4", overwrite=True)
98
99 with MDF(outfile) as attachment_mdf:
100 data, filename, _md5_sum = attachment_mdf.extract_attachment(index=0)
101 self.assertEqual(data, original_data)
102 self.assertEqual(filename, Path("bin.bin"))
103
104 mdf.close()
105
106 def test_attachment_blocks_w_filename(self) -> None:
107 original_data = b"Testing attachemnt block\nTest line 1"
108 original_file_name = "file.txt"
109
110 mdf = MDF()
111 mdf.attach(
112 original_data,
113 file_name=original_file_name,
114 comment="",
115 compression=True,
116 mime=r"text/plain",
117 embedded=True,
118 )
119 outfile = mdf.save(Path(TestMDF4.tempdir.name) / "attachment.mf4", overwrite=True)
120
121 with MDF(outfile) as attachment_mdf:
122 data, filename, _md5_sum = attachment_mdf.extract_attachment(index=0)
123 self.assertEqual(data, original_data)
124 self.assertEqual(filename, Path(original_file_name))
125
126 mdf.close()
127
128 @unittest.skip("temporary skip")
129 def test_channel_with_boolean_array(self) -> None:
130 timestamps = np.array([0.1, 0.2, 0.3, 0.4, 0.5], dtype=np.float32)
131
132 samples = [np.ones((5, 2), dtype=np.uint8)]
133 types = [("boolean_array_channel", "(2, )<u1")]
134 record = np.rec.fromarrays(samples, dtype=np.dtype(types))
135 boolean_array_channel = Signal(
136 record,
137 timestamps=timestamps,
138 name="boolean_array_channel",
139 )
140
141 mdf4 = MDF(version="4.10")
142 mdf4.append(signals=[boolean_array_channel])
143 # set bit count to 1 to indicate that each uint8 value is a boolean flag in boolean_array_channel
144 mdf4.groups[0].channels[1].bit_count = 1
145 signal = mdf4.select([("boolean_array_channel", 0, 1)])[0]
146
147 self.assertTrue((record == signal.samples).all())
148
149
150 if __name__ == "__main__":
151 unittest.main()