Debian Codegraph @

tests/instruments/lecroy/test_lecroyT3DSO1204_with_device.py

raw
1 #
2 # This file is part of the PyMeasure package.
3 #
4 # Copyright (c) 2013-2024 PyMeasure Developers
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a copy
7 # of this software and associated documentation files (the "Software"), to deal
8 # in the Software without restriction, including without limitation the rights
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the Software is
11 # furnished to do so, subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 # THE SOFTWARE.
23 #
24
25 from time import sleep
26 from unittest.mock import ANY
27
28 import numpy as np
29 import pytest
30 from pyvisa.errors import VisaIOError
31
32 from pymeasure.instruments.lecroy.lecroyT3DSO1204 import LeCroyT3DSO1204
33
34
35 class TestLeCroyT3DSO1204:
36 """
37 Unit tests for LeCroyT3DSO1204 class.
38
39 This test suite, needs the following setup to work properly:
40 - A LeCroyT3DSO1204 device should be connected to the computer;
41 - The device's address must be set in the RESOURCE constant;
42 - A probe on Channel 1 must be connected to the Demo output of the oscilloscope.
43 """
44
45 #########################
46 # PARAMETRIZATION CASES #
47 #########################
48
49 BOOLEANS = [False, True]
50 CHANNEL_COUPLINGS = ["ac 1M", "dc 1M", "ground"]
51 ACQUISITION_TYPES = ["normal", "average", "peak", "highres"]
52 TRIGGER_LEVELS = [0.125, 0.150, 0.175]
53 TRIGGER_SLOPES = ["negative", "positive", "window"]
54 ACQUISITION_AVERAGE = [4, 16, 32, 64, 128, 256]
55 WAVEFORM_POINTS = [100, 1000, 10000]
56 WAVEFORM_SOURCES = ["C1", "C2", "C3", "C4"]
57 CHANNELS = [1, 2, 3, 4]
58
59 ############
60 # FIXTURES #
61 ############
62
63 @pytest.fixture(scope="module")
64 def instrument(self, connected_device_address):
65 return LeCroyT3DSO1204(connected_device_address)
66
67 @pytest.fixture
68 def resetted_instrument(self, instrument):
69 instrument.reset()
70 sleep(7)
71 return instrument
72
73 @pytest.fixture
74 def autoscaled_instrument(self, instrument):
75 instrument.reset()
76 sleep(7)
77 instrument.autoscale()
78 sleep(7)
79 return instrument
80
81 #########
82 # TESTS #
83 #########
84
85 # noinspection PyTypeChecker
86 def test_instrument_connection(self):
87 bad_resource = "USB0::10893::45848::MY12345678::0::INSTR"
88 # The pure python VISA library (pyvisa-py) raises a ValueError while the
89 # PyVISA library raises a VisaIOError.
90 with pytest.raises((ValueError, VisaIOError)):
91 LeCroyT3DSO1204(bad_resource)
92
93 # Channel
94 def test_ch_current_configuration(self, autoscaled_instrument):
95 autoscaled_instrument.ch_1.offset = 0
96 autoscaled_instrument.ch_1.trigger_level = 0
97 autoscaled_instrument.ch_1.trigger_level2 = 0
98 expected = {
99 "channel": 1,
100 "attenuation": 1.0,
101 "bandwidth_limit": False,
102 "coupling": "dc 1M",
103 "offset": 0.0,
104 "skew_factor": 0.0,
105 "display": True,
106 "unit": "V",
107 "volts_div": 0.05,
108 "inverted": False,
109 "trigger_coupling": "dc",
110 "trigger_level": 0.0,
111 "trigger_level2": 0.0,
112 "trigger_slope": "positive",
113 }
114 actual = autoscaled_instrument.ch(1).current_configuration
115 assert actual == expected
116
117 @pytest.mark.parametrize("ch_number", CHANNELS)
118 @pytest.mark.parametrize("case", BOOLEANS)
119 def test_ch_bwlimit(self, instrument, ch_number, case):
120 instrument.ch(ch_number).bwlimit = case
121 assert instrument.ch(ch_number).bwlimit == case
122
123 @pytest.mark.parametrize("ch_number", CHANNELS)
124 @pytest.mark.parametrize("case", CHANNEL_COUPLINGS)
125 def test_ch_coupling(self, instrument, ch_number, case):
126 instrument.ch(ch_number).coupling = case
127 assert instrument.ch(ch_number).coupling == case
128
129 @pytest.mark.parametrize("ch_number", CHANNELS)
130 @pytest.mark.parametrize("case", BOOLEANS)
131 def test_ch_display(self, instrument, ch_number, case):
132 instrument.ch(ch_number).display = case
133 assert instrument.ch(ch_number).display == case
134
135 @pytest.mark.parametrize("ch_number", CHANNELS)
136 @pytest.mark.parametrize("case", BOOLEANS)
137 def test_ch_invert(self, instrument, ch_number, case):
138 instrument.ch(ch_number).invert = case
139 assert instrument.ch(ch_number).invert == case
140
141 @pytest.mark.parametrize("ch_number", CHANNELS)
142 def test_ch_offset(self, instrument, ch_number):
143 instrument.ch(ch_number).offset = 1
144 assert instrument.ch(ch_number).offset == 1
145
146 @pytest.mark.parametrize("ch_number", CHANNELS)
147 def test_ch_probe_attenuation(self, instrument, ch_number):
148 instrument.ch(ch_number).probe_attenuation = 10
149 assert instrument.ch(ch_number).probe_attenuation == 10
150
151 @pytest.mark.parametrize("ch_number", CHANNELS)
152 def test_ch_scale(self, instrument, ch_number):
153 instrument.ch(ch_number).scale = 1
154 assert instrument.ch(ch_number).scale == 1
155
156 def test_ch_trigger_level(self, autoscaled_instrument):
157 for case in self.TRIGGER_LEVELS:
158 autoscaled_instrument.ch_1.trigger_level = case
159 assert autoscaled_instrument.ch_1.trigger_level == case
160
161 def test_ch_trigger_level2(self, autoscaled_instrument):
162 for case in self.TRIGGER_LEVELS:
163 autoscaled_instrument.ch_1.trigger_level2 = case
164 assert autoscaled_instrument.ch_1.trigger_level2 == case
165
166 def test_ch_trigger_slope(self, autoscaled_instrument):
167 with pytest.raises(ValueError):
168 autoscaled_instrument.ch_1.trigger_slope = "abcd"
169 autoscaled_instrument.trigger_select = ("edge", "c1", "off")
170 for case in self.TRIGGER_SLOPES:
171 autoscaled_instrument.ch_1.trigger_slope = case
172 assert autoscaled_instrument.ch_1.trigger_slope == case
173
174 # Timebase
175 def test_timebase(self, autoscaled_instrument):
176 autoscaled_instrument.timebase_hor_magnify = 5e-6
177 autoscaled_instrument.timebase_hor_position = 0
178 expected = {
179 "timebase_scale": 5e-4,
180 "timebase_offset": 0.0,
181 "timebase_hor_magnify": 5e-6,
182 "timebase_hor_position": 0.0,
183 }
184 actual = autoscaled_instrument.timebase
185 for key, val in actual.items():
186 assert pytest.approx(val, 0.1) == expected[key]
187
188 def test_timebase_scale(self, resetted_instrument):
189 resetted_instrument.timebase_scale = 1e-3
190 assert resetted_instrument.timebase_scale == 1e-3
191
192 def test_timebase_offset(self, instrument):
193 instrument.timebase_offset = 1e-3
194 assert instrument.timebase_offset == 1e-3
195
196 def test_timebase_hor_magnify(self, instrument):
197 instrument.timebase_hor_magnify = 1e-4
198 assert instrument.timebase_hor_magnify == 1e-4
199
200 def test_timebase_hor_position(self, instrument):
201 instrument.timebase_hor_position = 5e-4
202 assert pytest.approx(instrument.timebase_hor_position, 0.1) == 5e-4
203
204 # Acquisition
205 @pytest.mark.parametrize("case", ACQUISITION_TYPES)
206 def test_acquisition_type(self, resetted_instrument, case):
207 if case == "average":
208 resetted_instrument.acquisition_type = case
209 resetted_instrument.acquisition_average = 16
210 assert resetted_instrument.acquisition_type == ["average", 16]
211 else:
212 resetted_instrument.acquisition_type = case
213 assert resetted_instrument.acquisition_type == case
214
215 @pytest.mark.parametrize("case", ACQUISITION_AVERAGE)
216 def test_acquisition_average(self, instrument, case):
217 instrument.acquisition_average = case
218 assert instrument.acquisition_average == case
219
220 def test_acquisition_status(self, autoscaled_instrument):
221 assert autoscaled_instrument.acquisition_status == "triggered"
222 autoscaled_instrument.stop()
223 assert autoscaled_instrument.acquisition_status == "stopped"
224
225 def test_acquisition_sampling_rate(self, resetted_instrument):
226 assert resetted_instrument.acquisition_sampling_rate == 1e9
227
228 @pytest.mark.parametrize("case", WAVEFORM_POINTS)
229 def test_waveform_points(self, instrument, case):
230 instrument.waveform_points = case
231 assert instrument.waveform_points == case
232
233 def test_waveform_preamble(self, autoscaled_instrument):
234 autoscaled_instrument.acquisition_type = "normal"
235 autoscaled_instrument.ch_1.offset = 0
236 autoscaled_instrument.waveform_points = 0
237 autoscaled_instrument.waveform_first_point = 0
238 autoscaled_instrument.waveform_sparsing = 1
239 autoscaled_instrument.waveform_source = "C1"
240 expected_preamble = {
241 "sparsing": 1.0,
242 "requested_points": 0.0,
243 "memory_size": 14e6,
244 "sampled_points": 7e6,
245 "transmitted_points": None,
246 "first_point": 0.0,
247 "source": autoscaled_instrument.waveform_source,
248 "type": "normal",
249 "average": None,
250 "sampling_rate": 1e9,
251 "grid_number": 14,
252 "status": ANY,
253 "xdiv": 5e-4,
254 "xoffset": -0.0,
255 "ydiv": 0.05,
256 "yoffset": 0.0,
257 "unit": "V",
258 }
259 preamble = autoscaled_instrument.waveform_preamble
260 assert preamble == expected_preamble
261
262 # Setup methods
263 @pytest.mark.parametrize("ch_number", CHANNELS)
264 def test_channel_setup(self, instrument, ch_number):
265 # Only autoscale on the first channel
266 instrument = instrument
267 if ch_number == self.CHANNELS[0]:
268 instrument.reset()
269 sleep(7)
270 instrument.autoscale()
271 sleep(7)
272
273 # Not testing the actual values assignment since different combinations of
274 # parameters can play off each other.
275 expected = instrument.ch(ch_number).current_configuration
276 instrument.ch(ch_number).setup()
277 assert instrument.ch(ch_number).current_configuration == expected
278 with pytest.raises(AttributeError):
279 instrument.ch(5)
280 instrument.ch(ch_number).setup(
281 bwlimit=False,
282 coupling="dc 1M",
283 display=True,
284 invert=False,
285 offset=0.0,
286 skew_factor=0.0,
287 probe_attenuation=1.0,
288 scale=0.05,
289 unit="V",
290 trigger_coupling="dc",
291 trigger_level=0.150,
292 trigger_level2=0.150,
293 trigger_slope="positive",
294 )
295 expected = {
296 "channel": ch_number,
297 "attenuation": 1.0,
298 "bandwidth_limit": False,
299 "coupling": "dc 1M",
300 "offset": 0.0,
301 "skew_factor": 0.0,
302 "display": True,
303 "unit": "V",
304 "volts_div": 0.05,
305 "inverted": False,
306 "trigger_coupling": "dc",
307 "trigger_level": 0.150,
308 "trigger_level2": 0.150,
309 "trigger_slope": "positive",
310 }
311 actual = instrument.ch(ch_number).current_configuration
312 assert actual == expected
313
314 def test_timebase_setup(self, resetted_instrument):
315 expected = resetted_instrument.timebase
316 resetted_instrument.timebase_setup()
317 assert resetted_instrument.timebase == expected
318
319 # Download methods
320 def test_download_image_default_arguments(self, autoscaled_instrument):
321 img = autoscaled_instrument.download_image()
322 assert type(img) is bytearray
323 assert pytest.approx(len(img), 0.1) == 768067
324
325 def test_download_data_missing_argument(self, resetted_instrument):
326 with pytest.raises(TypeError):
327 # noinspection PyArgumentList
328 resetted_instrument.download_waveform()
329
330 @pytest.mark.parametrize("case1", WAVEFORM_SOURCES)
331 @pytest.mark.parametrize("case2", WAVEFORM_POINTS)
332 def test_download_data(self, instrument, case1, case2):
333 if case1 == self.WAVEFORM_SOURCES[0] and case2 == self.WAVEFORM_POINTS[0]:
334 instrument.reset()
335 sleep(7)
336 instrument.autoscale()
337 sleep(7)
338 instrument.ch(case1).display = True
339 instrument.single()
340 sleep(1)
341 data, time, preamble = instrument.download_waveform(
342 source=case1, requested_points=case2, sparsing=0
343 )
344 assert type(data) is np.ndarray
345 assert len(data) == case2
346 assert type(time) is np.ndarray
347 assert len(time) == case2
348 assert type(preamble) is dict
349
350 def test_download_single_point(self, instrument):
351 instrument.acquisition_type = "normal"
352 instrument.ch_1.display = True
353 instrument.single()
354 sleep(1)
355 data, time, preamble = instrument.download_waveform(source="c1", requested_points=1)
356 assert type(data) is np.ndarray
357 assert len(data) == 1
358 assert type(time) is np.ndarray
359 assert len(time) == 1
360 assert type(preamble) is dict
361 assert preamble == {'average': None,
362 'first_point': 0,
363 'grid_number': 14,
364 'memory_size': 7000000.0,
365 'requested_points': 1,
366 'sampled_points': 3500000.0,
367 'sampling_rate': 500000000.0,
368 'source': 'C1',
369 'sparsing': 1.0,
370 'status': 'stopped',
371 'transmitted_points': 1,
372 'type': 'normal',
373 'unit': 'V',
374 'xdiv': 0.0005,
375 'xoffset': -0.0,
376 'ydiv': ANY,
377 'yoffset': ANY}
378
379 @pytest.mark.skip(reason="A human is needed to check the output waveform")
380 def test_download_data_all_points(self, instrument):
381 from matplotlib import pyplot as plt
382
383 instrument.ch_1.display = True
384 instrument.single()
385 sleep(3)
386 data, time, preamble = instrument.download_waveform(source="c1", requested_points=0)
387 assert type(data) is np.ndarray
388 assert type(time) is np.ndarray
389 assert type(preamble) is dict
390 print(preamble)
391 plt.scatter(x=time, y=data)
392 plt.show()
393
394 @pytest.mark.skip(reason="A human is needed to check the output waveform")
395 def test_download_data_sparsing(self, instrument):
396 from matplotlib import pyplot as plt
397
398 instrument.ch_1.display = True
399 instrument.single()
400 sleep(1)
401 data, time, preamble = instrument.download_waveform(
402 source="c1", requested_points=7e5, sparsing=10
403 )
404 assert type(data) is np.ndarray
405 assert len(data) == 7e5 or len(data) == 7e4
406 assert type(time) is np.ndarray
407 assert len(time) == 7e5 or len(time) == 7e4
408 assert type(preamble) is dict
409 assert preamble["type"] == "normal"
410 assert preamble["sparsing"] == 10
411 assert preamble["transmitted_points"] == 7e5 or preamble["transmitted_points"] == 7e4
412 print(preamble)
413 plt.scatter(x=time, y=data)
414 plt.show()
415
416 @pytest.mark.skip(reason="A human is needed to check the output waveform")
417 def test_download_data_averaging_16(self, instrument):
418 from matplotlib import pyplot as plt
419
420 instrument.ch_1.display = True
421 instrument.run()
422 instrument.acquisition_type = "average"
423 instrument.acquisition_average = 16
424 instrument.single()
425 sleep(1)
426 data, time, preamble = instrument.download_waveform(
427 source="c1", requested_points=1.75e5, sparsing=10
428 )
429 assert type(data) is np.ndarray
430 assert len(data) == 1.75e5 or len(data) == 7e4
431 assert type(time) is np.ndarray
432 assert len(time) == 1.75e5 or len(time) == 7e4
433 assert type(preamble) is dict
434 assert preamble["type"] == ["average", 16]
435 assert preamble["average"] == 16
436 assert preamble["transmitted_points"] == 1.75e5 or preamble["transmitted_points"] == 7e4
437 print(preamble)
438 plt.scatter(x=time, y=data)
439 plt.show()
440
441 @pytest.mark.skip(reason="A human is needed to check the output waveform")
442 def test_download_data_averaging_256(self, instrument):
443 from matplotlib import pyplot as plt
444
445 instrument.ch_1.display = True
446 instrument.run()
447 instrument.acquisition_type = "average"
448 instrument.acquisition_average = 256
449 instrument.single()
450 sleep(1)
451 data, time, preamble = instrument.download_waveform(
452 source="c1", requested_points=1.75e5, sparsing=10
453 )
454 assert type(data) is np.ndarray
455 assert len(data) == 1.75e5 or len(data) == 7e4
456 assert type(time) is np.ndarray
457 assert len(time) == 1.75e5 or len(time) == 7e4
458 assert type(preamble) is dict
459 assert preamble["type"] == ["average", 256]
460 assert preamble["average"] == 256
461 assert preamble["transmitted_points"] == 1.75e5 or preamble["transmitted_points"] == 7e4
462 print(preamble)
463 plt.scatter(x=time, y=data)
464 plt.show()
465
466 @pytest.mark.skip(reason="A human is needed to check the output waveform")
467 def test_download_math(self, instrument):
468 """ Be careful because there is no way to turn on and off the MATH function
469 programmatically, so the user should push on the MATH button and make sure
470 that the (white) math line is displayed before running this test. """
471 from matplotlib import pyplot as plt
472
473 instrument.single()
474 sleep(1)
475 data, time, preamble = instrument.download_waveform(
476 source="math", requested_points=0, sparsing=10
477 )
478 assert type(data) is np.ndarray
479 assert type(time) is np.ndarray
480 assert type(preamble) is dict
481 print(preamble)
482 plt.scatter(x=time, y=data)
483 plt.show()
484
485 # Trigger
486 def test_trigger_select(self, resetted_instrument):
487 with pytest.raises(ValueError):
488 resetted_instrument.trigger_select = "edge"
489 with pytest.raises(ValueError):
490 resetted_instrument.trigger_select = ("edge", "c2")
491 with pytest.raises(ValueError):
492 resetted_instrument.trigger_select = ("edge", "c2", "time")
493 with pytest.raises(ValueError):
494 resetted_instrument.trigger_select = ("ABCD", "c1", "time", 0)
495 with pytest.raises(ValueError):
496 resetted_instrument.trigger_select = ("edge", "c1", "time", 1000)
497 with pytest.raises(ValueError):
498 resetted_instrument.trigger_select = ("edge", "c1", "time", 0, 1)
499 resetted_instrument.trigger_select = ("edge", "c1", "off")
500 resetted_instrument.trigger_select = ("EDGE", "C1", "OFF")
501 assert resetted_instrument.trigger_select == ["edge", "c1", "off"]
502 resetted_instrument.trigger_select = ("glit", "c1", "p2", 1e-3, 2e-3)
503 assert resetted_instrument.trigger_select == ["glit", "c1", "p2", 1e-3, 2e-3]
504
505 def test_trigger_setup(self, resetted_instrument):
506 expected = resetted_instrument.trigger
507 resetted_instrument.trigger_setup(**expected)
508 assert resetted_instrument.trigger == expected
509
510
511 if __name__ == "__main__":
512 pytest.main()