Skip to content

Commit 21ffc8d

Browse files
committed
doc
1 parent 2c1d715 commit 21ffc8d

8 files changed

Lines changed: 92 additions & 10 deletions

plotpy/tests/unit/test_aspect_ratio_tool.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010

1111
def test_aspect_ratio_tool():
12+
"""Test the aspect ratio tool."""
1213
with qt_app_context(exec_loop=False):
1314
win, tool = create_window(AspectRatioTool, active_item_type=IImageItemType)
1415
plot = win.manager.get_plot()

plotpy/tests/unit/test_display_coords_tool.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99

1010

1111
def test_display_coords_on_curve():
12+
"""Test display coordinates tool on a curve."""
1213
with qt_app_context(exec_loop=False) as qapp:
1314
win, tool = create_window(DisplayCoordsTool)
1415
drag_mouse(win, qapp, np.array([0.5]), np.array([0.5]), click=False)
1516
drag_mouse(win, qapp, np.array([0.5]), np.array([0.5]), click=True)
1617

1718

1819
def test_display_coords_on_image():
20+
"""Test display coordinates tool on an image."""
1921
with qt_app_context(exec_loop=False) as qapp:
2022
win, tool = create_window(DisplayCoordsTool, active_item_type=IImageItemType)
2123
drag_mouse(win, qapp, np.array([0.5]), np.array([0.5]), click=False)

plotpy/tests/unit/test_multiline_tools.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515

1616
def test_free_form_tool():
17+
"""Test the free form tool."""
1718
corners = np.array(((0.1, 0.1), (0.1, 0.8), (0.8, 0.8), (0.8, 0.1)))
1819
with qt_app_context(exec_loop=False) as qapp:
1920
win, tool = create_window(FreeFormTool)
@@ -30,6 +31,7 @@ def test_free_form_tool():
3031

3132

3233
def test_multiline_tool():
34+
"""Test the multi line tool."""
3335
n = 100
3436
t = np.linspace(0, np.pi * 10, n)
3537

plotpy/tests/unit/test_oblique_cross_section_tool.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111

1212
def test_oblique_cross_section():
13+
"""Test the oblique cross section tool."""
1314
with qt_app_context(exec_loop=False) as qapp:
1415
win, tool = create_window(
1516
ObliqueCrossSectionTool,

plotpy/tests/unit/test_point_tools.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323

2424
def test_free_select_point_tool():
25+
"""Test the free select point tool."""
2526
with qt_app_context(exec_loop=False) as qapp:
2627
win, tool = create_window(SelectPointTool)
2728
mouse_event_at_relative_plot_pos(
@@ -35,20 +36,29 @@ def test_free_select_point_tool():
3536

3637

3738
def test_contrained_select_point_tool():
39+
"""Test the constrained select point tool contrained to a CurveItem."""
3840
with qt_app_context(exec_loop=False) as qapp:
3941
win, tool = create_window(SelectPointTool)
4042
tool.on_active_item = True
43+
4144
mouse_event_at_relative_plot_pos(
4245
win,
4346
qapp,
4447
(0.5, 0.5),
4548
CLICK,
4649
)
47-
assert tool.get_coordinates() is not None
50+
coor = tool.get_coordinates()
51+
curve_item: CurveItem = win.manager.get_plot().get_active_item() # type: ignore
52+
arr_x, arr_y = curve_item.get_data()
53+
assert coor is not None
54+
55+
x, y = coor
56+
assert np.where(arr_x == x)[0] == np.where(arr_y == y)[0]
4857
exec_dialog(win)
4958

5059

5160
def test_select_points_tool():
61+
"""Test the select points tool constrained to a CurveItem."""
5262
with qt_app_context(exec_loop=False) as qapp:
5363
win, tool = create_window(tool_class=SelectPointsTool)
5464
mod = QC.Qt.KeyboardModifier.ControlModifier
@@ -69,10 +79,18 @@ def test_select_points_tool():
6979
mouse_event_at_relative_plot_pos(win, qapp, (0.1, 0.1), CLICK)
7080
assert len(tool.get_coordinates() or ()) == 1
7181

82+
coor = tool.get_coordinates()
83+
assert coor is not None
84+
x, y = coor[0]
85+
curve_item: CurveItem = win.manager.get_plot().get_active_item() # type: ignore
86+
arr_x, arr_y = curve_item.get_data()
87+
assert np.where(arr_x == x)[0] == np.where(arr_y == y)[0]
88+
7289
exec_dialog(win)
7390

7491

7592
def test_edit_point_tool():
93+
"""Test the edit point tool for a CurveItem."""
7694
with qt_app_context(exec_loop=False) as qapp:
7795
win, tool = create_window(EditPointTool)
7896
curve_item: CurveItem = win.manager.get_plot().get_active_item() # type: ignore
@@ -105,9 +123,13 @@ def test_edit_point_tool():
105123
assert x == x_arr[i]
106124
assert y == y_arr[i]
107125

108-
tool.get_arrays()
109-
tool.get_initial_arrays()
126+
x_arr, y_arr = tool.get_arrays()
127+
assert x_arr is not None and y_arr is not None
128+
129+
x_arr, y_arr = tool.get_initial_arrays()
130+
assert x_arr is not None and y_arr is not None
110131

132+
# Reset the arrays and deletes the changes
111133
keyboard_event(
112134
win, qapp, QC.Qt.Key.Key_Z, QC.Qt.KeyboardModifier.ControlModifier
113135
)

plotpy/tests/unit/test_rect_zoom.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@
1414
def zoom(
1515
x_path: np.ndarray, y_path: np.ndarray, compare: Callable[[float, float], bool]
1616
):
17+
"""Zoom in or out on a plot by sumilating a mouse drag along a defined path.
18+
19+
Args:
20+
x_path: The x relative plot coordinates of the path to simulate the mouse
21+
drag along.
22+
y_path: The y relative plot coordinates of the path to simulate the mouse
23+
drag along.
24+
compare: Comparison function to compare the initial and final axis limits to
25+
check if the zoom worked as expected.
26+
"""
1727
with qt_app_context(exec_loop=False) as qapp:
1828
win, tool = create_window(RectZoomTool, active_item_type=ICurveItemType)
1929
plot = win.manager.get_plot()
@@ -41,12 +51,14 @@ def zoom(
4151

4252

4353
def test_rect_zoom_tool():
54+
"""Test the rectangular zoom tool."""
4455
x_path = linspace(0, 0.5, 100)
4556
y_path = linspace(0, 0.5, 100)
4657
zoom(x_path, y_path, lambda og, final: final < og)
4758

4859

4960
def test_rect_unzoom_tool():
61+
"""Test the rectangular zoom tool to unzoom."""
5062
x_path = linspace(-0.5, 1.5, 100)
5163
y_path = linspace(-0.5, 1.5, 100)
5264
zoom(x_path, y_path, lambda og, final: abs(final) > abs(og))

plotpy/tests/unit/test_shape_tools.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@
5757

5858

5959
def create_window(tool_classes: tuple[type[RectangularActionTool], ...]) -> PlotWindow:
60+
"""Create a window with the given tools. The plot contains a curve, an image and a
61+
legend.
62+
63+
Args:
64+
tool_classes: tools to add to the window.
65+
66+
Returns:
67+
PlotWindow: The window containing the tools.
68+
"""
6069
items = make_curve_image_legend()
6170
win = ptv.show_items(
6271
items, wintitle="Unit tests for RectangularActionTools", auto_tools=True
@@ -66,18 +75,13 @@ def create_window(tool_classes: tuple[type[RectangularActionTool], ...]) -> Plot
6675
_ = win.manager.add_tool(toolklass)
6776

6877
assert len(tool_classes) > 0
69-
# win.manager.set_default_tool(tool)
7078

7179
return win
7280

7381

74-
def use_tool(win: PlotWindow, tool_class: type[RectangularActionTool]):
75-
filter_ = win.manager.get_plot().filter
76-
if (tool := win.manager.get_tool(tool_class)) is not None:
77-
tool.end_rect(filter_, P0, P1)
78-
79-
8082
def _test_annotation_tools(tool_classes: tuple[type[RectangularActionTool], ...]):
83+
"""Generic test for annotation tool. Simulates a mouse drag on the plot and checks
84+
that the tool is activated and deactivated correctly."""
8185
with qt_app_context(exec_loop=False) as qapp:
8286
win = create_window(tool_classes)
8387
default_tool = win.manager.get_default_tool()
@@ -94,66 +98,82 @@ def _test_annotation_tools(tool_classes: tuple[type[RectangularActionTool], ...]
9498

9599

96100
def test_annotated_circle_tool():
101+
"""Test the annotated circle tool."""
97102
_test_annotation_tools((AnnotatedCircleTool,))
98103

99104

100105
def test_annotated_ellipse_tool():
106+
"""Test the annotated ellipse tool."""
101107
_test_annotation_tools((AnnotatedEllipseTool,))
102108

103109

104110
def test_annotated_oblique_rectangle_tool():
111+
"""Test the annotated oblique rectangle tool."""
105112
_test_annotation_tools((AnnotatedObliqueRectangleTool,))
106113

107114

108115
def test_annotated_point_tool():
116+
"""Test the annotated point tool."""
109117
_test_annotation_tools((AnnotatedPointTool,))
110118

111119

112120
def test_annotated_rectangle_tool():
121+
"""Test the annotated rectangle tool."""
113122
_test_annotation_tools((AnnotatedRectangleTool,))
114123

115124

116125
def test_annotated_segment_tool():
126+
"""Test the annotated segment tool."""
117127
_test_annotation_tools((AnnotatedSegmentTool,))
118128

119129

120130
def test_avg_cross_section_tool():
131+
"""Test the average cross section tool."""
121132
_test_annotation_tools((AverageCrossSectionTool,))
122133

123134

124135
def test_cross_section_tool():
136+
"""Test the cross section tool."""
125137
_test_annotation_tools((CrossSectionTool,))
126138

127139

128140
def test_snapshot_tool():
141+
"""Test the snapshot tool."""
129142
_test_annotation_tools((SnapshotTool,))
130143

131144

132145
def test_image_stats_tool():
146+
"""Test the image stats tool."""
133147
_test_annotation_tools((ImageStatsTool,))
134148

135149

136150
def test_circle_tool():
151+
"""Test the circle tool."""
137152
_test_annotation_tools((CircleTool,))
138153

139154

140155
def test_ellipse_tool():
156+
"""Test the ellipse tool."""
141157
_test_annotation_tools((EllipseTool,))
142158

143159

144160
def test_oblique_rectangle_tool():
161+
"""Test the oblique rectangle tool."""
145162
_test_annotation_tools((ObliqueRectangleTool,))
146163

147164

148165
def test_point_tool():
166+
"""Test the point tool."""
149167
_test_annotation_tools((PointTool,))
150168

151169

152170
def test_rectangle_tool():
171+
"""Test the rectangle tool."""
153172
_test_annotation_tools((RectangleTool,))
154173

155174

156175
def test_segment_tool():
176+
"""Test the segment tool."""
157177
_test_annotation_tools((SegmentTool,))
158178

159179

plotpy/tests/unit/utils.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,17 @@ def drag_mouse(
102102
mod=QC.Qt.KeyboardModifier.NoModifier,
103103
click=True,
104104
) -> None:
105+
"""Simulates a mouse drag on the plot.
106+
107+
Args:
108+
win: window containing the plot
109+
qapp: QApplication instance
110+
x_path: relative x plot coordinates of the path to simulate the mouse drag along
111+
y_path: relative y plot coordinates of the path to simulate the mouse drag along
112+
mod: keyboard modifier. Defaults to QC.Qt.KeyboardModifier.NoModifier.
113+
click: Whether a click must be simulated (mouse hold -> drag -> mouse release).
114+
Defaults to True.
115+
"""
105116
x0, y0 = x_path[0], y_path[0]
106117
xn, yn = x_path[-1], y_path[-1]
107118
press = (QC.QEvent.Type.MouseButtonPress,)
@@ -121,7 +132,18 @@ def create_window(
121132
active_item_type: type[IItemType] = ICurveItemType,
122133
panels: list[type[PanelWidget]] | None = None,
123134
) -> tuple[PlotWindow, ToolT]:
135+
"""Create a window with the given tool. The plot contains a curve, an image and a
136+
legend.
124137
138+
Args:
139+
tool_class: tool class to add to the window.
140+
active_item_type: Type of Item to set to active (curve or image).
141+
Defaults to ICurveItemType.
142+
panels: PanelWidget classes to add. Defaults to None.
143+
144+
Returns:
145+
The window containing the activated tool and the tool instance itself.
146+
"""
125147
items: list[CurveItem | BaseImageItem] = make_curve_image_legend()
126148
win = ptv.show_items(items, wintitle="Unit test plot", auto_tools=False)
127149
plot = win.manager.get_plot()

0 commit comments

Comments
 (0)