|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Licensed under the terms of the BSD 3-Clause |
| 4 | +# (see plotpy/LICENSE for details) |
| 5 | + |
| 6 | +"""Curve and Image Statistics tools tests""" |
| 7 | + |
| 8 | +# guitest: show |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +from typing import TYPE_CHECKING |
| 13 | + |
| 14 | +import numpy as np |
| 15 | +from guidata.qthelpers import create_toolbutton, execenv, qt_app_context |
| 16 | + |
| 17 | +from plotpy.builder import make |
| 18 | +from plotpy.config import _ |
| 19 | +from plotpy.constants import PlotType |
| 20 | +from plotpy.plot import PlotDialog, PlotOptions |
| 21 | +from plotpy.tests.data import gen_image4 |
| 22 | +from plotpy.tests.unit.utils import drag_mouse |
| 23 | +from plotpy.tools import CurveStatsTool, ImageStatsTool |
| 24 | + |
| 25 | +if TYPE_CHECKING: |
| 26 | + from plotpy.items.image.base import BaseImageItem |
| 27 | + from plotpy.styles import BaseImageParam |
| 28 | + |
| 29 | + |
| 30 | +class MyPlotDialog(PlotDialog): |
| 31 | + def __init__(self, type: PlotType) -> None: |
| 32 | + """Reimplement PlotDialog method""" |
| 33 | + super().__init__( |
| 34 | + title=f"{type.name.lower().capitalize()} Statistics tools test", |
| 35 | + toolbar=True, |
| 36 | + options=PlotOptions(type=type), |
| 37 | + ) |
| 38 | + # No need to add the tools to the manager, they are automatically added |
| 39 | + # when the `register_curve_tools` or `register_image_tools` method is called |
| 40 | + self.setup_items() |
| 41 | + if execenv.unattended: |
| 42 | + self.simulate_stats_tool() |
| 43 | + |
| 44 | + def populate_plot_layout(self) -> None: |
| 45 | + """Populate the plot layout""" |
| 46 | + super().populate_plot_layout() |
| 47 | + options = self.plot_widget.options |
| 48 | + btn = create_toolbutton( |
| 49 | + self, |
| 50 | + None, |
| 51 | + f"Simulate {options.type.name.lower()} statistics tool", |
| 52 | + self.simulate_stats_tool, |
| 53 | + ) |
| 54 | + self.add_widget(btn) |
| 55 | + |
| 56 | + def simulate_stats_tool(self) -> None: |
| 57 | + """Simulate the statistics tool""" |
| 58 | + options = self.plot_widget.options |
| 59 | + klass = CurveStatsTool if options.type is PlotType.CURVE else ImageStatsTool |
| 60 | + tool = self.manager.get_tool(klass) |
| 61 | + tool.activate() |
| 62 | + drag_mouse(self, np.array([0.4, 0.8]), np.array([0.2, 0.7])) |
| 63 | + |
| 64 | + def setup_items(self) -> None: |
| 65 | + """Setup items""" |
| 66 | + plot = self.get_plot() |
| 67 | + if self.plot_widget.options.type is PlotType.CURVE: |
| 68 | + x = np.linspace(0, 10, 100) |
| 69 | + y = np.sin(x) |
| 70 | + item = make.curve(x, y, title="Curve") |
| 71 | + else: |
| 72 | + item = make.image(gen_image4(500, 500), title="Image", colormap="cool") |
| 73 | + plot.add_item(item) |
| 74 | + plot.select_item(item) |
| 75 | + |
| 76 | + |
| 77 | +def test_curve_stats_tools() -> None: |
| 78 | + """Test""" |
| 79 | + with qt_app_context(exec_loop=True): |
| 80 | + win = MyPlotDialog(type=PlotType.CURVE) |
| 81 | + tool = win.manager.get_tool(CurveStatsTool) |
| 82 | + labelfuncs = ( |
| 83 | + ("Mean = %g", lambda *args: np.mean(args[1])), |
| 84 | + ("Std = %g", lambda *args: np.std(args[1])), |
| 85 | + ("Max = %g", lambda *args: np.max(args[1])), |
| 86 | + ("Min = %g", lambda *args: np.min(args[1])), |
| 87 | + ) |
| 88 | + tool.set_labelfuncs(labelfuncs) |
| 89 | + win.show() |
| 90 | + |
| 91 | + |
| 92 | +# Note: |
| 93 | +# ----- |
| 94 | +# |
| 95 | +# Using the following `get_more_stats` function, the `ImageStatsTool` will display |
| 96 | +# the surface, the sum and the density of the selected area - as additional stats |
| 97 | +# if the `replace` parameter is set to `False` (default value). |
| 98 | +# |
| 99 | +# This is the way of reimplementing the old `show_surface` and `show_integral` |
| 100 | +# arguments of the `ImageStatsTool` class in previous versions of PlotPy. |
| 101 | + |
| 102 | + |
| 103 | +def get_more_stats( |
| 104 | + item: BaseImageItem, |
| 105 | + x0: float, |
| 106 | + y0: float, |
| 107 | + x1: float, |
| 108 | + y1: float, |
| 109 | +) -> str: |
| 110 | + """Return formatted string with stats on image rectangular area |
| 111 | + (output should be compatible with AnnotatedShape.get_infos) |
| 112 | +
|
| 113 | + Args: |
| 114 | + item: image item |
| 115 | + x0: X0 |
| 116 | + y0: Y0 |
| 117 | + x1: X1 |
| 118 | + y1: Y1 |
| 119 | + """ |
| 120 | + ix0, iy0, ix1, iy1 = item.get_closest_index_rect(x0, y0, x1, y1) |
| 121 | + data = item.data[iy0:iy1, ix0:ix1] |
| 122 | + p: BaseImageParam = item.param |
| 123 | + xunit, yunit, zunit = p.get_units() |
| 124 | + infos = "" |
| 125 | + if xunit == yunit: |
| 126 | + surfacefmt = p.xformat.split()[0] + " " + xunit |
| 127 | + if xunit != "": |
| 128 | + surfacefmt = surfacefmt + "²" |
| 129 | + surface = abs((x1 - x0) * (y1 - y0)) |
| 130 | + infos += _("surface = %s") % (surfacefmt % surface) |
| 131 | + integral = data.sum() |
| 132 | + integral_fmt = r"%.3e " + zunit |
| 133 | + infos = infos + "<br>" + _("sum = %s") % (integral_fmt % integral) |
| 134 | + if xunit == yunit and xunit is not None and zunit is not None: |
| 135 | + if surface != 0: |
| 136 | + density = integral / surface |
| 137 | + densityfmt = r"%.3e" |
| 138 | + if xunit and zunit: |
| 139 | + densityfmt += " " + zunit + "/" + xunit + "²" |
| 140 | + infos = infos + "<br>" + _("density = %s") % (densityfmt % density) |
| 141 | + else: |
| 142 | + infos = infos + "<br>" + _("density not computed : surface is null !") |
| 143 | + return infos |
| 144 | + |
| 145 | + |
| 146 | +def test_image_stats_tools() -> None: |
| 147 | + """Test""" |
| 148 | + with qt_app_context(exec_loop=True): |
| 149 | + win = MyPlotDialog(type=PlotType.IMAGE) |
| 150 | + tool = win.manager.get_tool(ImageStatsTool) |
| 151 | + tool.set_stats_func(get_more_stats, replace=False) |
| 152 | + win.show() |
| 153 | + |
| 154 | + |
| 155 | +if __name__ == "__main__": |
| 156 | + test_curve_stats_tools() |
| 157 | + test_image_stats_tools() |
0 commit comments