Coverage for tests/test_state_levels.py: 89%
32 statements
« prev ^ index » next coverage.py v7.4.3, created at 2024-04-21 11:16 +0000
« prev ^ index » next coverage.py v7.4.3, created at 2024-04-21 11:16 +0000
1"""Tests for signal state levels."""
3import os
5import numpy as np
6import pytest
8from signal_edges.signal import VoltageSignal
9from signal_edges.signal.state_levels import Mode
11from .conftest import env_plots
14class TestStateLevels:
15 """A collection of tests for signal state levels."""
17 # pylint: disable=no-self-use,too-few-public-methods
19 ## Private API #####################################################################################################
21 ## Protected API ###################################################################################################
23 ## Public API ######################################################################################################
24 @pytest.mark.parametrize("adir", ["state_level/test_histogram"], indirect=True)
25 def test_histogram(self, adir: str) -> None:
26 """Tests the state levels calculated with histogram mode and mean on a signal.
28 :param adir: The path where the plots will be stored."""
29 # Create signal.
30 timestamps = np.arange(0.0, 20.0).astype(np.float_)
31 voltages = np.asarray([-10, -10, 10, 10] * 5).astype(np.float_)
32 signal = VoltageSignal(timestamps, voltages)
34 # Calculate and plot histogram mode results.
35 (levels, (hist_x, hist_y)) = signal.state_levels(Mode.HISTOGRAM_MODE)
36 if env_plots(): 36 ↛ 37line 36 didn't jump to line 37, because the condition on line 36 was never true
37 signal.state_levels_plot(os.path.join(adir, "mode.png"), levels, histogram=(hist_x, hist_y))
39 assert np.isclose(levels.highest, 9.9)
40 assert np.isclose(levels.high, -9.9 + (9.9 - -9.9) * 0.90)
41 assert np.isclose(levels.high_runt, -9.9 + (9.9 - -9.9) * 0.70)
42 assert np.isclose(levels.intermediate, -9.9 + (9.9 - -9.9) * 0.50)
43 assert np.isclose(levels.low_runt, -9.9 + (9.9 - -9.9) * 0.30)
44 assert np.isclose(levels.low, -9.9 + (9.9 - -9.9) * 0.10)
45 assert np.isclose(levels.lowest, -9.9)
47 # Calculate and plot histogram mean results.
48 (levels, (hist_x, hist_y)) = signal.state_levels(Mode.HISTOGRAM_MEAN)
49 if env_plots(): 49 ↛ 50line 49 didn't jump to line 50, because the condition on line 49 was never true
50 signal.state_levels_plot(os.path.join(adir, "mean.png"), levels, histogram=(hist_x, hist_y))
52 assert np.isclose(levels.highest, 9.9)
53 assert np.isclose(levels.high, -9.9 + (9.9 - -9.9) * 0.90)
54 assert np.isclose(levels.high_runt, -9.9 + (9.9 - -9.9) * 0.70)
55 assert np.isclose(levels.intermediate, -9.9 + (9.9 - -9.9) * 0.50)
56 assert np.isclose(levels.low_runt, -9.9 + (9.9 - -9.9) * 0.30)
57 assert np.isclose(levels.low, -9.9 + (9.9 - -9.9) * 0.10)
58 assert np.isclose(levels.lowest, -9.9)