Coverage for tests/test_plotter.py: 96%

49 statements  

« prev     ^ index     » next       coverage.py v7.4.3, created at 2024-04-21 11:16 +0000

1"""Tests for the opinionated plotter.""" 

2 

3import os 

4 

5import numpy as np 

6import numpy.typing as npt 

7import pytest 

8 

9from signal_edges.plotter import Cursor, Mode, Plotter, Subplot, Units 

10 

11from .conftest import env_plots 

12 

13 

14class TestPlotter: 

15 """A collection of tests for the opinionated plotter.""" 

16 

17 ## Private API ##################################################################################################### 

18 

19 ## Protected API ################################################################################################### 

20 @staticmethod 

21 def _gen_axis(start: float, step: float, count: int) -> npt.NDArray[np.float_]: 

22 """Generates the values of an axis for plotting. 

23 

24 :param start: The start value for the axis. 

25 :param step: The step between the values. 

26 :param count: The number of values. 

27 :return: Array with the values.""" 

28 return np.arange(start, start + step * count, step) 

29 

30 ## Public API ###################################################################################################### 

31 @pytest.mark.parametrize("rows", [1, 2]) 

32 @pytest.mark.parametrize("columns", [1, 2]) 

33 @pytest.mark.parametrize("count", [10000, 10000000]) 

34 @pytest.mark.parametrize("plots_num", [1, 2, 4]) 

35 @pytest.mark.parametrize("adir", ["linear_plot"], indirect=True) 

36 def test_linear_plot(self, rows: int, columns: int, count: int, plots_num: int, adir: str) -> None: 

37 """Tests several combinations of linear plots, saving the results to file for visual inspection. 

38 

39 :param rows: The number of rows. 

40 :param columns: The number of columns. 

41 :param count: The number of values in each axis of each row and column. 

42 :param plots_num: The number of plots in each row and column. 

43 :param adir: The path where the plots will be stored.""" 

44 # pylint: disable=too-many-arguments,too-many-locals 

45 

46 # Create plotter and name from parameters. 

47 plot_path = os.path.join(adir, f"rw{rows}_cl{columns}_cn{count}_pn{plots_num}.png") 

48 plotter = Plotter(Mode.LINEAR, rows=rows, columns=columns) 

49 

50 # Add plots to each slot. 

51 for row_i in range(rows): 

52 for column_i in range(columns): 

53 # Get a slot index from the rows and columns.. 

54 slot_i = row_i * columns + column_i 

55 

56 # Create plots and add them to the plotter. 

57 for plot_i in range(plots_num): 

58 # Generate arrays. 

59 hvalues = self._gen_axis(0.0, 1.0, count) # Note for simplicity indices match values. 

60 vvalues = self._gen_axis(0.0, 1.0 + slot_i * plot_i, count) 

61 

62 # Create plot and add it. 

63 plot = Subplot( 

64 name=f"pl{plot_i}", 

65 hvalues=hvalues, 

66 hunits=Units("N/A", "N/A", "N/A"), 

67 vvalues=vvalues, 

68 vunits=Units("N/A", "N/A", "N/A"), 

69 begin=(count / 10) * 2, 

70 end=(count / 10) * 8, 

71 munits=1, 

72 color=["red", "blue", "green", "violet"][plot_i], 

73 ) 

74 plotter.add_plot(row_i, column_i, plot) 

75 

76 # Create cursors and add them to the plotter. 

77 for cursor_i in range(7): 

78 # Create cursor and add it. 

79 cursor = Cursor( 

80 name=["A", "B", "C", "D", "E", "F", "G"][cursor_i], 

81 hindex=int((count / 10) * {0: 2, 1: 3, 2: 4, 3: 5, 4: 6, 5: 7, 6: 8}[cursor_i]), 

82 row=row_i, 

83 column=column_i, 

84 subplot_ids=[f"pl{i}" for i in range(plots_num)], 

85 ) 

86 plotter.add_cursor(cursor) 

87 

88 # Perform plot and save to file. 

89 if env_plots(): 89 ↛ 90line 89 didn't jump to line 90, because the condition on line 89 was never true

90 plotter.plot(plot_path) 

91 

92 @pytest.mark.parametrize("rows", [1, 2]) 

93 @pytest.mark.parametrize("count", [10000, 10000000]) 

94 @pytest.mark.parametrize("plots_num", [1, 2, 4]) 

95 @pytest.mark.parametrize("adir", ["shared_x_axis_plot"], indirect=True) 

96 def test_shared_x_axis_plot(self, rows: int, count: int, plots_num: int, adir: str) -> None: 

97 """Tests several combinations of shared horizontal axis plots, saving the results to file for visual inspection. 

98 

99 :param rows: The number of rows. 

100 :param count: The number of values in each axis of each row and column. 

101 :param plots_num: The number of plots in each row and column. 

102 :param adir: The path where the plots will be stored.""" 

103 # Create plotter and name from parameters. 

104 plot_path = os.path.join(adir, f"rw{rows}_cn{count}_pn{plots_num}.png") 

105 plotter = Plotter(Mode.SHARED_H_AXIS, rows=rows, columns=1) 

106 

107 # Add plots to each slot. 

108 for row_i in range(rows): 

109 # Create plots and add them to the plotter. 

110 for plot_i in range(plots_num): 

111 # Generate arrays. 

112 hvalues = self._gen_axis(0.0, 1.0, count) # Note for simplicity indices match values. 

113 vvalues = self._gen_axis(0.0, 1.0 + row_i * plot_i, count) 

114 

115 # Create plot and add it. 

116 plot = Subplot( 

117 name=f"pl{plot_i}", 

118 hvalues=hvalues, 

119 hunits=Units("N/A", "N/A", "N/A"), 

120 vvalues=vvalues, 

121 vunits=Units("N/A", "N/A", "N/A"), 

122 begin=(count / 10) * 2, 

123 end=(count / 10) * 8, 

124 munits=1, 

125 color=["red", "blue", "green", "violet"][plot_i], 

126 ) 

127 plotter.add_plot(row_i, 0, plot) 

128 

129 # Create cursors and add them to the plotter. 

130 for cursor_i in range(7): 

131 # Create cursor and add it. 

132 cursor = Cursor( 

133 name=["A", "B", "C", "D", "E", "F", "G"][cursor_i], 

134 hindex=int((count / 10) * {0: 2, 1: 3, 2: 4, 3: 5, 4: 6, 5: 7, 6: 8}[cursor_i]), 

135 row=row_i, 

136 column=0, 

137 subplot_ids=[f"pl{i}" for i in range(plots_num)], 

138 ) 

139 plotter.add_cursor(cursor) 

140 

141 # Perform plot and save to file. 

142 if env_plots(): 142 ↛ 143line 142 didn't jump to line 143, because the condition on line 142 was never true

143 plotter.plot(plot_path)