Skip to content

Commit 0ee2cd5

Browse files
committed
Removed unnecessary IStatsImageItemType interface and BaseImage.get_stats method
1 parent b24f74a commit 0ee2cd5

File tree

4 files changed

+84
-114
lines changed

4 files changed

+84
-114
lines changed

plotpy/interfaces/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
IItemType,
1717
ISerializableType,
1818
IShapeItemType,
19-
IStatsImageItemType,
2019
ITrackableItemType,
2120
IVoiImageItemType,
2221
)

plotpy/interfaces/items.py

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -148,31 +148,6 @@ def export_roi(
148148
pass
149149

150150

151-
class IStatsImageItemType(IItemType):
152-
"""An image supporting stats computations"""
153-
154-
def get_stats(
155-
self,
156-
x0: float,
157-
y0: float,
158-
x1: float,
159-
y1: float,
160-
show_surface: bool = False,
161-
show_integral: bool = False,
162-
) -> str:
163-
"""Return formatted string with stats on image rectangular area
164-
(output should be compatible with AnnotatedShape.get_infos)
165-
166-
Args:
167-
x0: X0
168-
y0: Y0
169-
x1: X1
170-
y1: Y1
171-
show_surface: Show surface (Default value = False)
172-
show_integral: Show integral (Default value = False)
173-
"""
174-
175-
176151
class ICSImageItemType(IItemType):
177152
"""An image supporting X/Y cross sections"""
178153

plotpy/items/image/base.py

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
IHistDataSource,
3838
IImageItemType,
3939
ISerializableType,
40-
IStatsImageItemType,
4140
ITrackableItemType,
4241
IVoiImageItemType,
4342
)
@@ -75,7 +74,6 @@ class BaseImageItem(QwtPlotItem):
7574
IHistDataSource,
7675
IVoiImageItemType,
7776
ICSImageItemType,
78-
IStatsImageItemType,
7977
IExportROIImageItemType,
8078
)
8179
_can_select = True
@@ -720,8 +718,6 @@ def types(self) -> tuple[type[IItemType], ...]:
720718
ITrackableItemType,
721719
ICSImageItemType,
722720
IExportROIImageItemType,
723-
IStatsImageItemType,
724-
IStatsImageItemType,
725721
)
726722

727723
def set_readonly(self, state: bool) -> None:
@@ -998,85 +994,6 @@ def __process_cross_section(self, ydata, apply_lut):
998994
else:
999995
return ydata
1000996

1001-
def get_stats(
1002-
self,
1003-
x0: float,
1004-
y0: float,
1005-
x1: float,
1006-
y1: float,
1007-
show_surface: bool = False,
1008-
show_integral: bool = False,
1009-
) -> str:
1010-
"""Return formatted string with stats on image rectangular area
1011-
(output should be compatible with AnnotatedShape.get_infos)
1012-
1013-
Args:
1014-
x0: X0
1015-
y0: Y0
1016-
x1: X1
1017-
y1: Y1
1018-
show_surface: Show surface (Default value = False)
1019-
show_integral: Show integral (Default value = False)
1020-
"""
1021-
ix0, iy0, ix1, iy1 = self.get_closest_index_rect(x0, y0, x1, y1)
1022-
data = self.data[iy0:iy1, ix0:ix1]
1023-
xfmt = self.param.xformat
1024-
yfmt = self.param.yformat
1025-
zfmt = self.param.zformat
1026-
try:
1027-
xunit = xfmt.split()[1]
1028-
except IndexError:
1029-
xunit = ""
1030-
try:
1031-
yunit = yfmt.split()[1]
1032-
except IndexError:
1033-
yunit = ""
1034-
try:
1035-
zunit = zfmt.split()[1]
1036-
except IndexError:
1037-
zunit = ""
1038-
if show_integral:
1039-
integral = data.sum()
1040-
infos = "<br>".join(
1041-
[
1042-
"<b>%s</b>" % self.param.label,
1043-
"%sx%s %s"
1044-
% (self.data.shape[1], self.data.shape[0], str(self.data.dtype)),
1045-
"",
1046-
"%s ≤ x ≤ %s" % (xfmt % x0, xfmt % x1),
1047-
"%s ≤ y ≤ %s" % (yfmt % y0, yfmt % y1),
1048-
"%s ≤ z ≤ %s" % (zfmt % data.min(), zfmt % data.max()),
1049-
"‹z› = " + zfmt % data.mean(),
1050-
"σ(z) = " + zfmt % data.std(),
1051-
]
1052-
)
1053-
if show_surface and xunit == yunit:
1054-
surfacefmt = xfmt.split()[0] + " " + xunit
1055-
if xunit != "":
1056-
surfacefmt = surfacefmt + "²"
1057-
surface = abs((x1 - x0) * (y1 - y0))
1058-
infos = infos + "<br>" + _("surface = %s") % (surfacefmt % surface)
1059-
if show_integral:
1060-
integral = data.sum()
1061-
integral_fmt = r"%.3e " + zunit
1062-
infos = infos + "<br>" + _("sum = %s") % (integral_fmt % integral)
1063-
if (
1064-
show_surface
1065-
and xunit == yunit
1066-
and xunit is not None
1067-
and show_integral
1068-
and zunit is not None
1069-
):
1070-
if surface != 0:
1071-
density = integral / surface
1072-
densityfmt = r"%.3e " + zunit + "/" + xunit
1073-
if xunit != "":
1074-
densityfmt = densityfmt + "²"
1075-
infos = infos + "<br>" + _("density = %s") % (densityfmt % density)
1076-
else:
1077-
infos = infos + "<br>" + _("density not computed : surface is null !")
1078-
return infos
1079-
1080997
def get_xsection(self, y0: float | int, apply_lut: bool = False) -> np.ndarray:
1081998
"""Return cross section along x-axis at y=y0
1082999
@@ -1287,7 +1204,6 @@ def types(self) -> tuple[type[IItemType], ...]:
12871204
ICSImageItemType,
12881205
ISerializableType,
12891206
IExportROIImageItemType,
1290-
IStatsImageItemType,
12911207
)
12921208

12931209
def update_item_parameters(self) -> None:

plotpy/tools/image.py

Lines changed: 84 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from plotpy.events import QtDragHandler, setup_standard_tool_filter
2121
from plotpy.interfaces import (
2222
IColormapImageItemType,
23-
IStatsImageItemType,
23+
IImageItemType,
2424
IVoiImageItemType,
2525
)
2626
from plotpy.items import (
@@ -63,6 +63,86 @@
6363
from plotpy.styles.shape import AnnotationParam
6464

6565

66+
def get_stats(
67+
item: BaseImageItem,
68+
x0: float,
69+
y0: float,
70+
x1: float,
71+
y1: float,
72+
show_surface: bool = False,
73+
show_integral: bool = False,
74+
) -> str:
75+
"""Return formatted string with stats on image rectangular area
76+
(output should be compatible with AnnotatedShape.get_infos)
77+
78+
Args:
79+
x0: X0
80+
y0: Y0
81+
x1: X1
82+
y1: Y1
83+
show_surface: Show surface (Default value = False)
84+
show_integral: Show integral (Default value = False)
85+
"""
86+
ix0, iy0, ix1, iy1 = item.get_closest_index_rect(x0, y0, x1, y1)
87+
data = item.data[iy0:iy1, ix0:ix1]
88+
param: BaseImageParam = item.param
89+
xfmt = param.xformat
90+
yfmt = param.yformat
91+
zfmt = param.zformat
92+
try:
93+
xunit = xfmt.split()[1]
94+
except IndexError:
95+
xunit = ""
96+
try:
97+
yunit = yfmt.split()[1]
98+
except IndexError:
99+
yunit = ""
100+
try:
101+
zunit = zfmt.split()[1]
102+
except IndexError:
103+
zunit = ""
104+
if show_integral:
105+
integral = data.sum()
106+
infos = "<br>".join(
107+
[
108+
"<b>%s</b>" % param.label,
109+
"%sx%s %s" % (item.data.shape[1], item.data.shape[0], str(item.data.dtype)),
110+
"",
111+
"%s ≤ x ≤ %s" % (xfmt % x0, xfmt % x1),
112+
"%s ≤ y ≤ %s" % (yfmt % y0, yfmt % y1),
113+
"%s ≤ z ≤ %s" % (zfmt % data.min(), zfmt % data.max()),
114+
"‹z› = " + zfmt % data.mean(),
115+
"σ(z) = " + zfmt % data.std(),
116+
]
117+
)
118+
if show_surface and xunit == yunit:
119+
surfacefmt = xfmt.split()[0] + " " + xunit
120+
if xunit != "":
121+
surfacefmt = surfacefmt + "²"
122+
surface = abs((x1 - x0) * (y1 - y0))
123+
infos = infos + "<br>" + _("surface = %s") % (surfacefmt % surface)
124+
if show_integral:
125+
integral = data.sum()
126+
integral_fmt = r"%.3e " + zunit
127+
infos = infos + "<br>" + _("sum = %s") % (integral_fmt % integral)
128+
if (
129+
show_surface
130+
and xunit == yunit
131+
and xunit is not None
132+
and show_integral
133+
and zunit is not None
134+
):
135+
if surface != 0:
136+
density = integral / surface
137+
densityfmt = r"%.3e " + zunit + "/" + xunit
138+
if xunit != "":
139+
densityfmt = densityfmt + "²"
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+
66146
class ImageStatsRectangle(AnnotatedRectangle):
67147
"""Rectangle used to display image statistics
68148
@@ -129,8 +209,8 @@ def get_infos(self) -> str | None:
129209
return _("No available data")
130210
else:
131211
return _("No available data")
132-
return self.image_item.get_stats(
133-
*self.get_rect(), self.show_surface, self.show_integral
212+
return get_stats(
213+
self.image_item, *self.get_rect(), self.show_surface, self.show_integral
134214
)
135215

136216

@@ -258,7 +338,7 @@ def get_associated_item(self, plot: BasePlot) -> BaseImageItem | None:
258338
Returns:
259339
Reference to the last image item associated with the tool
260340
"""
261-
items = plot.get_selected_items(item_type=IStatsImageItemType)
341+
items = plot.get_selected_items(item_type=IImageItemType)
262342
if len(items) == 1:
263343
self._last_item = weakref.ref(items[0])
264344
return self.get_last_item()

0 commit comments

Comments
 (0)