-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathimage.py
More file actions
1392 lines (1188 loc) · 46.9 KB
/
image.py
File metadata and controls
1392 lines (1188 loc) · 46.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""Image tools"""
from __future__ import annotations
import weakref
from typing import TYPE_CHECKING, Any, Callable, Literal
from guidata.configtools import get_icon
from guidata.dataset import BoolItem, DataSet, FloatItem
from guidata.qthelpers import add_actions, exec_dialog
from guidata.widgets.arrayeditor import ArrayEditor
from qtpy import QtCore as QC
from qtpy import QtWidgets as QW
from plotpy import io
from plotpy.config import _
from plotpy.constants import AXIS_IDS, ID_CONTRAST, X_BOTTOM, Y_LEFT, PlotType
from plotpy.coords import axes_to_canvas
from plotpy.events import QtDragHandler, setup_standard_tool_filter
from plotpy.interfaces import (
IColormapImageItemType,
IImageItemType,
IVoiImageItemType,
)
from plotpy.items import (
AnnotatedRectangle,
EllipseShape,
ImageItem,
MaskedImageItem,
MaskedXYImageItem,
RectangleShape,
TrImageItem,
get_items_in_rectangle,
)
from plotpy.mathutils.colormap import ALL_COLORMAPS, build_icon_from_cmap_name, get_cmap
from plotpy.tools.base import (
CommandTool,
DefaultToolbarID,
GuiTool,
InteractiveTool,
PanelTool,
ToggleTool,
)
from plotpy.tools.misc import OpenFileTool
from plotpy.tools.shape import CircleTool, RectangleTool, RectangularShapeTool
from plotpy.widgets.colormap.manager import ColorMapManager
from plotpy.widgets.colormap.widget import EditableColormap
from plotpy.widgets.imagefile import exec_image_save_dialog
if TYPE_CHECKING:
from qtpy.QtCore import QEvent
from qtpy.QtWidgets import QMenu
from plotpy.events import StatefulEventFilter
from plotpy.interfaces.items import IBasePlotItem
from plotpy.items.image.base import BaseImageItem
from plotpy.items.shape.base import AbstractShape
from plotpy.items.shape.polygon import PolygonShape
from plotpy.plot import BasePlot
from plotpy.plot.manager import PlotManager
from plotpy.plot.plotwidget import PlotOptions
from plotpy.styles.image import BaseImageParam
from plotpy.styles.shape import AnnotationParam
def get_stats(
item: BaseImageItem,
x0: float,
y0: float,
x1: float,
y1: float,
) -> str:
"""Return formatted string with stats on image rectangular area
(output should be compatible with AnnotatedShape.get_infos)
Args:
item: image item
x0: X0
y0: Y0
x1: X1
y1: Y1
"""
ix0, iy0, ix1, iy1 = item.get_closest_index_rect(x0, y0, x1, y1)
data = item.data[iy0:iy1, ix0:ix1]
p: BaseImageParam = item.param
return "<br>".join(
[
"%sx%s %s" % (item.data.shape[1], item.data.shape[0], str(item.data.dtype)),
"",
"%s ≤ x ≤ %s" % (p.xformat % x0, p.xformat % x1),
"%s ≤ y ≤ %s" % (p.yformat % y0, p.yformat % y1),
"%s ≤ z ≤ %s" % (p.zformat % data.min(), p.zformat % data.max()),
"‹z› = " + p.zformat % data.mean(),
"σ(z) = " + p.zformat % data.std(),
]
)
class ImageStatsRectangle(AnnotatedRectangle):
"""Rectangle used to display image statistics
Args:
x1: X position of the first rectangle corner. Defaults to 0.
y1: Y position of the first rectangle corner. Defaults to 0.
x2: X position of the second rectangle corner. Defaults to 0.
y2: Y position of the second rectangle corner. Defaults to 0.
annotationparam: _description_. Defaults to None.
stats_func: function to get statistics. Defaults to None.
(see :py:func:`get_stats` for signature and default implementation)
replace: True to replace stats (statistics are not added to the
base infos but replace them). Defaults to False.
"""
shape: PolygonShape
def __init__(
self,
x1: float = 0.0,
y1: float = 0.0,
x2: float = 0.0,
y2: float = 0.0,
annotationparam: AnnotationParam | None = None,
stats_func: Callable[[BaseImageItem, float, float, float, float]] | None = None,
replace: bool = False,
):
"""_summary_"""
super().__init__(x1, y1, x2, y2, annotationparam)
self.image_item: BaseImageItem | None = None
self.setIcon(get_icon("imagestats.png"))
self.stats_func = stats_func
self.replace_stats = replace
def set_image_item(self, image_item: BaseImageItem) -> None:
"""Set image item to be used for statistics
Args:
image_item: image item to be used for statistics
"""
self.image_item = image_item
self.setTitle(self.image_item.title())
# ----AnnotatedShape API-----------------------------------------------------
def get_infos(self) -> str | None:
"""Get informations on current shape
Returns:
Formatted string with informations on current shape or None.
"""
if self.image_item is None:
return None
plot = self.image_item.plot()
if plot is None:
return None
x0, y0, x1, y1 = self.shape.get_rect()
p0x, p0y = axes_to_canvas(self, x0, y0)
p1x, p1y = axes_to_canvas(self, x1, y1)
items = get_items_in_rectangle(plot, QC.QPointF(p0x, p0y), QC.QPointF(p1x, p1y))
if len(items) >= 1:
sorted_items = [
it for it in sorted(items, key=lambda obj: obj.z()) if it.isVisible()
]
if len(sorted_items) >= 1:
self.image_item = sorted_items[-1]
else:
return _("No available data")
else:
return _("No available data")
x0, y0, x1, y1 = self.get_rect()
if self.replace_stats:
base_infos = ""
else:
base_infos = get_stats(self.image_item, x0, y0, x1, y1)
if self.stats_func is not None:
if base_infos:
base_infos += "<br>"
base_infos += self.stats_func(self.image_item, x0, y0, x1, y1)
return base_infos
class ImageStatsTool(RectangularShapeTool):
"""Tool to display image statistics in a rectangle
Args:
manager: PlotManager instance
setup_shape_cb: Callback called after shape setup. Defaults to None.
handle_final_shape_cb: Callback called when handling final shape.
Defaults to None.
shape_style: tuple of string to set the shape style. Defaults to None.
toolbar_id: toolbar id to use. Defaults to DefaultToolbarID. Defaults to
DefaultToolbarID.
title: tool title. Defaults to None.
icon: tool icon filename. Defaults to None.
tip: user tip to be displayed. Defaults to None.
stats_func: function to get statistics. Defaults to None.
(see :py:func:`get_stats` for signature and default implementation)
replace: True to replace stats (statistics are not added to the
base infos but replace them). Defaults to False.
.. note:: The stats_func function should return a formatted string with
statistics on the image rectangular area. The function signature should
be::
def stats_func(item, x0, y0, x1, y1):
return formatted_string
where item is the image item, x0, y0, x1, y1 are the rectangle coordinates
and formatted_string is the formatted string with statistics on the image
rectangular area.
Default implementation is the following:
.. literalinclude:: ../../../plotpy/tools/image.py
:pyobject: get_stats
"""
SWITCH_TO_DEFAULT_TOOL = True
TITLE = _("Image statistics")
ICON = "imagestats.png"
SHAPE_STYLE_KEY = "shape/image_stats"
def __init__(
self,
manager,
setup_shape_cb: Callable[[AbstractShape], None] | None = None,
handle_final_shape_cb: Callable[[AbstractShape], None] | None = None,
shape_style: tuple[str, str] | None = None,
toolbar_id: Any | type[DefaultToolbarID] = DefaultToolbarID,
title: str | None = None,
icon: str | None = None,
tip: str | None = None,
stats_func: Callable[[BaseImageItem, float, float, float, float]] | None = None,
replace: bool = False,
) -> None:
super().__init__(
manager,
setup_shape_cb,
handle_final_shape_cb,
shape_style,
toolbar_id,
title,
icon,
tip,
)
self._last_item = None
self.stats_func = stats_func
self.replace_stats = replace
def set_stats_func(
self,
stats_func: Callable[[BaseImageItem, float, float, float, float]],
replace: bool = False,
) -> None:
"""Set the function to get statistics
Args:
stats_func: function to get statistics
(see :py:func:`get_stats` for signature and default implementation)
replace: True to replace stats (statistics are not added to the base infos
but replace them). Defaults to False.
"""
self.stats_func = stats_func
self.replace_stats = replace
def get_last_item(self) -> BaseImageItem | None:
"""Last image item getter
Returns:
Returns last image item or None
"""
if self._last_item is not None:
return self._last_item()
return None
def create_shape(self) -> tuple[ImageStatsRectangle, Literal[0], Literal[2]]:
"""Returns a new ImageStatsRectangle instance and the index of handles to
display.
Returns:
New ImageStatsRectangle instance
"""
return (
ImageStatsRectangle(
0,
0,
1,
1,
stats_func=self.stats_func,
replace=self.replace_stats,
),
0,
2,
)
def setup_shape(self, shape: ImageStatsRectangle) -> None:
"""Setup and registers given shape.
Parameters:
shape: Shape to setup
"""
super().setup_shape(shape)
self.set_shape_style(shape)
self.register_shape(shape, final=False)
def register_shape(self, shape: ImageStatsRectangle, final=False) -> None:
"""Register given shape
Args:
shape: Shape to register
final: unused argument. Defaults to False.
"""
plot = shape.plot()
image = self.get_last_item()
if plot is not None and image is not None:
plot.unselect_all()
plot.set_active_item(shape)
shape.set_image_item(image)
def handle_final_shape(self, shape: ImageStatsRectangle) -> None:
"""Handle final shape
Args:
shape: Shape to handled and register
"""
super().handle_final_shape(shape)
self.register_shape(shape, final=True)
def get_associated_item(self, plot: BasePlot) -> BaseImageItem | None:
"""Return a reference to the last image item associated with the tool
Args:
plot: Plot instance
Returns:
Reference to the last image item associated with the tool
"""
items = plot.get_selected_items(item_type=IImageItemType)
if len(items) == 1:
self._last_item = weakref.ref(items[0])
return self.get_last_item()
def update_status(self, plot: BasePlot) -> None:
"""Update tool status if the plot type is not PlotType.CURVE.
Args:
plot: Plot instance
"""
if update_image_tool_status(self, plot):
item = self.get_associated_item(plot)
self.action.setEnabled(item is not None)
class BaseReverseAxisTool(ToggleTool):
"""Base class for tools to reverse axes"""
TITLE = "" # To be defined in subclasses
AXIS_ID = -1 # To be defined in subclasses
def __init__(self, manager: PlotManager) -> None:
assert self.TITLE, "TITLE must be defined in subclasses"
assert self.AXIS_ID in AXIS_IDS, "Invalid AXIS_ID"
super().__init__(manager, self.TITLE)
def activate_command(self, plot: BasePlot, checked: bool) -> None:
"""Triggers tool action.
Args:
plot: Plot instance
checked: True if tool is checked, False otherwise
"""
plot.set_axis_direction(self.AXIS_ID, checked)
plot.replot()
plot.SIG_AXIS_PARAMETERS_CHANGED.emit(self.AXIS_ID)
def update_status(self, plot: BasePlot) -> None:
"""Update tool status if the plot type is not PlotType.CURVE.
Args:
plot: Plot instance
"""
if update_image_tool_status(self, plot):
self.action.setChecked(plot.get_axis_direction(self.AXIS_ID))
class ReverseXAxisTool(BaseReverseAxisTool):
"""Togglable tool to reverse X axis
Args:
manager: PlotManager Instance
"""
TITLE = _("Reverse X axis")
AXIS_ID = X_BOTTOM
class ReverseYAxisTool(BaseReverseAxisTool):
"""Togglable tool to reverse Y axis
Args:
manager: PlotManager Instance
"""
TITLE = _("Reverse Y axis")
AXIS_ID = Y_LEFT
class ZAxisLogTool(ToggleTool):
"""Patched tools.ToggleTool"""
def __init__(self, manager: PlotManager) -> None:
title = _("Base-10 logarithmic Z axis")
super().__init__(
manager,
title=title,
toolbar_id=DefaultToolbarID,
icon="zlog.svg",
)
def activate_command(self, plot: BasePlot, checked: bool) -> None:
"""Reimplement tools.ToggleTool method"""
for item in self.get_supported_items(plot):
item.set_zaxis_log_state(not item.get_zaxis_log_state())
plot.replot()
self.update_status(plot)
def get_supported_items(self, plot: BasePlot) -> list[BaseImageItem]:
"""Reimplement tools.ToggleTool method"""
items = [
item
for item in plot.get_items()
if isinstance(item, ImageItem)
and not item.is_empty()
and hasattr(item, "get_zaxis_log_state")
]
if len(items) > 1:
items = [item for item in items if item in plot.get_selected_items()]
if items:
self.action.setChecked(items[0].get_zaxis_log_state())
return items
def update_status(self, plot: BasePlot) -> None:
"""Reimplement tools.ToggleTool method"""
self.action.setEnabled(len(self.get_supported_items(plot)) > 0)
class AspectRatioParam(DataSet):
"""Dataset containing aspect ratio parameters."""
lock = BoolItem(_("Lock aspect ratio"))
current = FloatItem(_("Current value")).set_prop("display", active=False)
ratio = FloatItem(_("Lock value"), min=1e-3)
class AspectRatioTool(CommandTool):
"""Tool to manage the aspect ratio of a plot
Args:
manager: PlotManager instance"""
def __init__(self, manager: PlotManager) -> None:
super().__init__(manager, _("Aspect ratio"), tip=None, toolbar_id=None)
self.action.setEnabled(True)
def create_action_menu(self, manager: PlotManager) -> QMenu:
"""Create and return menu for the tool's action"""
self.ar_param = AspectRatioParam(_("Aspect ratio"))
menu = QW.QMenu()
self.lock_action = manager.create_action(
_("Lock"), toggled=self.lock_aspect_ratio
)
self.ratio1_action = manager.create_action(
_("1:1"), triggered=self.set_aspect_ratio_1_1
)
self.set_action = manager.create_action(
_("Edit..."), triggered=self.edit_aspect_ratio
)
add_actions(menu, (self.lock_action, None, self.ratio1_action, self.set_action))
return menu
def set_aspect_ratio_1_1(self) -> None:
"""Reset current aspect ratio to 1:1"""
plot = self.get_active_plot()
if plot is not None:
plot.set_aspect_ratio(ratio=1)
plot.replot()
def activate_command(self, plot: BasePlot, checked: bool) -> None:
"""Triggers tool action.
Args:
plot: Plot instance
checked: True if tool is checked, False otherwise
"""
def __update_actions(self, checked: bool) -> None:
"""Update actions state according to given checked state
Args:
checked: True if actions should be enabled, False otherwise
"""
self.ar_param.lock = checked
self.lock_action.setChecked(checked)
plot = self.get_active_plot()
if plot is not None:
ratio = plot.get_aspect_ratio()
self.ratio1_action.setEnabled(checked and ratio != 1.0)
def lock_aspect_ratio(self, checked: bool) -> None:
"""Lock aspect ratio depending on given checked state.
Args:
checked: True if aspect ratio should be locked, False otherwise
"""
plot = self.get_active_plot()
if plot is not None:
plot.set_aspect_ratio(lock=checked)
self.__update_actions(checked)
plot.replot()
def edit_aspect_ratio(self) -> None:
"""Edit the aspect ratio with a dataset dialog"""
plot = self.get_active_plot()
if plot is not None:
self.ar_param.lock = plot.lock_aspect_ratio
self.ar_param.ratio = plot.get_aspect_ratio()
self.ar_param.current = plot.get_current_aspect_ratio()
if self.ar_param.edit(parent=plot):
lock, ratio = self.ar_param.lock, self.ar_param.ratio
plot.set_aspect_ratio(ratio=ratio, lock=lock)
self.__update_actions(lock)
plot.replot()
def update_status(self, plot: BasePlot) -> None:
"""Update tool status if the plot type is not PlotType.CURVE.
Args:
plot: Plot instance
"""
if update_image_tool_status(self, plot):
ratio = plot.get_aspect_ratio()
lock = plot.lock_aspect_ratio
self.ar_param.ratio, self.ar_param.lock = ratio, lock
self.__update_actions(lock)
class ContrastPanelTool(PanelTool):
"""Tools to adjust contrast using a dataset dialog"""
panel_name = _("Contrast adjustment")
panel_id = ID_CONTRAST
def update_status(self, plot: BasePlot) -> None:
"""Update tool status.
Args:
plot: Plot Instance
"""
super().update_status(plot)
update_image_tool_status(self, plot)
item = plot.get_last_active_item(IVoiImageItemType)
panel = self.manager.get_panel(self.panel_id)
for action in panel.toolbar.actions():
if isinstance(action, QW.QAction):
action.setEnabled(item is not None)
def get_selected_images(plot: BasePlot, item_type: Any) -> list[BaseImageItem]:
"""Returns the currently selected images in the given plot.
Args:
plot: Plot instance
item_type: Item type to filter (e.g. IColormapImageItemType)
Returns:
List of currently selected images in the given plot
"""
items = plot.get_selected_items(item_type=item_type)
if not items:
active_image = plot.get_last_active_item(item_type)
if active_image:
items = [active_image]
return items
class ColormapTool(CommandTool):
"""Tool used to select and manage colormaps (inculding visualization, edition
and saving).
Args:
manager: PlotManager Instance
toolbar_id: Toolbar Id to use. Defaults to DefaultToolbarID.
"""
def __init__(self, manager: PlotManager, toolbar_id=DefaultToolbarID) -> None: # noqa: F821
super().__init__(
manager,
_("Colormap"),
tip=_("Select colormap for active image"),
toolbar_id=toolbar_id,
)
self._active_colormap: EditableColormap = ALL_COLORMAPS["jet"]
self.default_icon = build_icon_from_cmap_name(self._active_colormap.name)
if self.action is not None:
self.action.setEnabled(False)
self.action.setIconText("")
self.action.setIcon(self.default_icon)
def activate_command(self, plot: BasePlot, checked: bool) -> None:
"""Triggers tool action.
Args:
plot: Plot instance
checked: True if tool is checked, False otherwise
"""
if (
plot is None
or not isinstance(self.action, QC.QObject)
or not isinstance(self.action.text(), str)
):
return
manager = ColorMapManager(
plot.parent(), active_colormap=self._active_colormap.name
)
manager.SIG_APPLY_COLORMAP.connect(self.update_plot)
if exec_dialog(manager) and (cmap := manager.get_colormap()) is not None:
self.activate_cmap(cmap)
def activate_cmap(self, cmap: str | EditableColormap) -> None:
"""Activate the given colormap. Supports mutliple input types.
Args:
cmap: Cmap to apply for currently selected images.
"""
assert isinstance(cmap, (str, EditableColormap))
if isinstance(cmap, str):
self._active_colormap = get_cmap(cmap)
else:
self._active_colormap = cmap
plot: BasePlot = self.get_active_plot()
if self._active_colormap is not None and plot is not None:
self.update_plot(self._active_colormap.name)
self.update_status(plot)
def update_plot(self, cmap: str) -> None:
"""Update the plot with the given colormap.
Args:
cmap: Colormap name
"""
plot: BasePlot = self.get_active_plot()
items = get_selected_images(plot, IColormapImageItemType)
for item in items:
param: BaseImageParam = item.param
param.colormap = cmap
param.update_item(item)
plot.SIG_ITEM_PARAMETERS_CHANGED.emit(item)
plot.invalidate()
def update_status(self, plot: BasePlot) -> None:
"""Update tool status if the plot type is not PlotType.CURVE.
Args:
plot: Plot Instance
"""
if update_image_tool_status(self, plot):
item: BaseImageItem | None = plot.get_last_active_item(
IColormapImageItemType
)
icon = self.default_icon
cmap_name = "jet"
if item:
self.action.setEnabled(True)
cmap = item.get_color_map()
if cmap is not None:
icon = build_icon_from_cmap_name(cmap.name)
self._active_colormap = get_cmap(cmap.name)
cmap_name = cmap.name
else:
self.action.setEnabled(False)
self._active_colormap = ALL_COLORMAPS["jet"]
self.action.setText(_("Colormap: %s") % cmap_name)
self.action.setIcon(icon)
class ReverseColormapTool(ToggleTool):
"""Togglable tool to reverse colormap
Args:
manager: PlotManager Instance
"""
def __init__(self, manager: PlotManager) -> None:
super().__init__(manager, _("Invert colormap"))
self._active_colormap: EditableColormap = ALL_COLORMAPS["jet"]
def activate_command(self, plot: BasePlot, checked: bool) -> None:
"""Triggers tool action.
Args:
plot: Plot instance
checked: True if tool is checked, False otherwise
"""
plot: BasePlot = self.get_active_plot()
if self._active_colormap is not None and plot is not None:
items = get_selected_images(plot, IColormapImageItemType)
for item in items:
param: BaseImageParam = item.param
param.invert_colormap = checked
param.update_item(item)
plot.SIG_ITEM_PARAMETERS_CHANGED.emit(item)
plot.invalidate()
self.update_status(plot)
def update_status(self, plot: BasePlot) -> None:
"""Update tool status if the plot type is not PlotType.CURVE.
Args:
plot: Plot instance
"""
if update_image_tool_status(self, plot):
item: BaseImageItem | None = plot.get_last_active_item(
IColormapImageItemType
)
state = False
if item:
self.action.setEnabled(True)
cmap = item.get_color_map()
if cmap is not None:
self._active_colormap = get_cmap(cmap.name)
state = cmap.invert
else:
self.action.setEnabled(False)
self._active_colormap = ALL_COLORMAPS["jet"]
self.action.setChecked(state)
class LockLUTRangeTool(ToggleTool):
"""Togglable tool to keep LUT range when updating image data
Args:
manager: PlotManager Instance
"""
def __init__(self, manager: PlotManager) -> None:
super().__init__(
manager,
_("Lock LUT range (update)"),
tip=_(
"If enabled, the LUT range is not updated when the image data changes."
"<br>This allows to keep the same color scale for different successive "
"images. <br><br>"
"<u>Note:</u> It has no effect when a new image is added to the plot."
),
)
def activate_command(self, plot: BasePlot, checked: bool) -> None:
"""Triggers tool action.
Args:
plot: Plot instance
checked: True if tool is checked, False otherwise
"""
plot: BasePlot = self.get_active_plot()
if plot is not None:
items = get_selected_images(plot, IColormapImageItemType)
for item in items:
param: BaseImageParam = item.param
param.keep_lut_range = checked
self.update_status(plot)
def update_status(self, plot: BasePlot) -> None:
"""Update tool status if the plot type is not PlotType.CURVE.
Args:
plot: Plot instance
"""
if update_image_tool_status(self, plot):
item: BaseImageItem | None = plot.get_last_active_item(
IColormapImageItemType
)
self.action.setEnabled(item is not None)
state = False
if item is not None:
param: BaseImageParam = item.param
state = param.keep_lut_range
self.action.setChecked(state)
class ImageMaskTool(CommandTool):
"""Tool to manage image masking
Args:
manager: Plot manager instance
toolbar_id: Toolbar id value
"""
#: Signal emitted by ImageMaskTool when mask was applied
SIG_APPLIED_MASK_TOOL = QC.Signal()
def __init__(self, manager: PlotManager, toolbar_id=DefaultToolbarID) -> None:
self._mask_shapes = {}
self._mask_already_restored = {}
super().__init__(
manager,
_("Mask"),
icon="mask_tool.png",
tip=_("Manage image masking areas"),
toolbar_id=toolbar_id,
)
self.masked_image = None # associated masked image item
def create_action_menu(self, manager: PlotManager) -> QMenu:
"""Create and return the tool's action menu for a given manager.
Args:
manager: PlotManager instance
"""
rect_tool = manager.add_tool(
RectangleTool,
toolbar_id=None,
handle_final_shape_cb=lambda shape: self.handle_shape(shape, inside=True),
title=_("Mask rectangular area (inside)"),
icon="mask_rectangle.png",
)
rect_out_tool = manager.add_tool(
RectangleTool,
toolbar_id=None,
handle_final_shape_cb=lambda shape: self.handle_shape(shape, inside=False),
title=_("Mask rectangular area (outside)"),
icon="mask_rectangle_outside.png",
)
ellipse_tool = manager.add_tool(
CircleTool,
toolbar_id=None,
handle_final_shape_cb=lambda shape: self.handle_shape(shape, inside=True),
title=_("Mask circular area (inside)"),
icon="mask_circle.png",
)
ellipse_out_tool = manager.add_tool(
CircleTool,
toolbar_id=None,
handle_final_shape_cb=lambda shape: self.handle_shape(shape, inside=False),
title=_("Mask circular area (outside)"),
icon="mask_circle_outside.png",
)
menu = QW.QMenu()
self.showmask_action = manager.create_action(
_("Show image mask"), toggled=self.show_mask
)
showshapes_action = manager.create_action(
_("Show masking shapes"), toggled=self.show_shapes
)
showshapes_action.setChecked(True)
applymask_a = manager.create_action(
_("Apply mask"), icon=get_icon("apply.png"), triggered=self.apply_mask
)
clearmask_a = manager.create_action(
_("Clear mask"), icon=get_icon("delete.png"), triggered=self.clear_mask
)
removeshapes_a = manager.create_action(
_("Remove all masking shapes"),
icon=get_icon("delete.png"),
triggered=self.remove_all_shapes,
)
add_actions(
menu,
(
self.showmask_action,
None,
showshapes_action,
rect_tool.action,
ellipse_tool.action,
rect_out_tool.action,
ellipse_out_tool.action,
applymask_a,
None,
clearmask_a,
removeshapes_a,
),
)
self.action.setMenu(menu)
return menu
def update_status(self, plot: BasePlot) -> None:
"""Enables tool if masked_image is set.
Args:
plot: Plot instance
"""
self.action.setEnabled(self.masked_image is not None)
def register_plot(self, baseplot: BasePlot) -> None:
"""Register plot in the tool instance and connect signals.
Args:
baseplot: Plot instance
"""
super().register_plot(baseplot)
self._mask_shapes.setdefault(baseplot, [])
baseplot.SIG_ITEMS_CHANGED.connect(self.items_changed)
baseplot.SIG_ITEM_SELECTION_CHANGED.connect(self.item_selection_changed)
def show_mask(self, state: bool):
"""Shows the image mask depending on given state and if masked_image is set
Args:
state: True to show mask, False otherwise
"""
if self.masked_image is not None:
self.masked_image.set_mask_visible(state)
def apply_mask(self):
"""Applies the mask to the image"""
mask = self.masked_image.get_mask()
plot = self.get_active_plot()
for shape, inside in self._mask_shapes[plot]:
if isinstance(shape, RectangleShape):
self.masked_image.align_rectangular_shape(shape)
x0, y0, x1, y1 = shape.get_rect()
self.masked_image.mask_rectangular_area(x0, y0, x1, y1, inside=inside)
else:
x0, y0, x1, y1 = shape.get_rect()
self.masked_image.mask_circular_area(x0, y0, x1, y1, inside=inside)
self.masked_image.set_mask(mask)
plot.replot()
self.SIG_APPLIED_MASK_TOOL.emit()
def remove_all_shapes(self) -> None:
"""Prompts the user to removes all shapes from the plot"""
message = _("Do you really want to remove all masking shapes?")
plot = self.get_active_plot()
answer = QW.QMessageBox.warning(
plot,
_("Remove all masking shapes"),
message,
QW.QMessageBox.Yes | QW.QMessageBox.No,
)
if answer == QW.QMessageBox.Yes:
self.remove_shapes()
def remove_shapes(self) -> None:
"""Removes all shapes from the plot"""
plot = self.get_active_plot()
plot.del_items(
[shape for shape, _inside in self._mask_shapes[plot]]
) # remove shapes
self._mask_shapes[plot] = []
plot.replot()
def show_shapes(self, state: bool) -> None:
"""Shows the masking shapes depending on given state
Args:
state: True to show shapes, False otherwise
"""
plot = self.get_active_plot()
if plot is not None:
for shape, _inside in self._mask_shapes[plot]:
shape.setVisible(state)
plot.replot()
def handle_shape(self, shape: AbstractShape, inside: bool) -> None:
"""Handles given shape and adds it to the plot and sets it to be the current
item"""
shape.set_style("plot", "shape/mask")
shape.set_private(True)
plot = self.get_active_plot()
plot.set_active_item(shape)
self._mask_shapes[plot] += [(shape, inside)]
def find_masked_image(
self, plot: BasePlot
) -> MaskedImageItem | MaskedXYImageItem | None:
"""Finds the masked image item in the given plot
Args:
plot: Plot instance
Returns:
MaskedImageItem or MaskedXYImageItem instance if found, None otherwise
"""
maskedtypes = (MaskedImageItem, MaskedXYImageItem)
item = plot.get_active_item()
if isinstance(item, maskedtypes):
return item
items = [item for item in plot.get_items() if isinstance(item, maskedtypes)]
if items:
return items[-1]
return None
def create_shapes_from_masked_areas(self) -> None:
"""Creates shapes from the masked areas of the masked image (rectangular or
ellipse).
"""
plot = self.get_active_plot()
self._mask_shapes[plot] = []
for area in self.masked_image.get_masked_areas():
if area is not None and area.geometry == "rectangular":