-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcsplot.py
More file actions
502 lines (401 loc) · 16.1 KB
/
csplot.py
File metadata and controls
502 lines (401 loc) · 16.1 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
# -*- coding: utf-8 -*-
from __future__ import annotations
import weakref
from typing import TYPE_CHECKING, Any
from qtpy import QtCore as QC
from qtpy import QtWidgets as QW
from plotpy.config import CONF, _
from plotpy.constants import AXIS_IDS, LUT_MAX, X_BOTTOM, Y_LEFT, PlotType
from plotpy.interfaces import ICSImageItemType
from plotpy.panels.csection.csitem import (
LineCrossSectionItem,
ObliqueCrossSectionItem,
XCrossSectionItem,
YCrossSectionItem,
)
from plotpy.plot.base import BasePlot
from plotpy.styles.curve import CurveParam
LUT_AXIS_TITLE = _("LUT scale") + (" (0-%d)" % LUT_MAX)
if TYPE_CHECKING:
from qtpy.QtWidgets import QWidget
from plotpy.items import BaseImageItem
from plotpy.panels.csection.csitem import CrossSectionItem
class BaseCrossSectionPlot(BasePlot):
"""Cross section plot base class
Args:
parent: Parent widget. Defaults to None.
"""
CURVE_LABEL = _("Cross section")
LABEL_TEXT = "" # to be overridden in subclasses
_HEIGHT = None
_WIDTH = None
CS_AXIS = None
Z_AXIS = None
Z_MAX_MAJOR = 5
SHADE = 0.2
def __init__(self, parent: QWidget | None = None) -> None:
super().__init__(
parent=parent,
options=dict(title="", section="cross_section", type="curve"),
)
self.perimage_mode = True
self.autoscale_mode = True
self.autorefresh_mode = True
self.apply_lut = False
self.single_source = False
self.lockscales = True
self.last_obj = None
self.known_items = {}
self._shapes = {}
self.param = CurveParam(_("Curve"), icon="curve.png")
self.set_curve_style("cross_section", "curve")
if self._HEIGHT is not None:
self.setSizePolicy(QW.QSizePolicy.Expanding, QW.QSizePolicy.Minimum)
elif self._WIDTH is not None:
self.setSizePolicy(QW.QSizePolicy.Minimum, QW.QSizePolicy.Expanding)
# The following import is here to avoid circular imports
# pylint: disable=import-outside-toplevel
from plotpy.builder import make
self.label = make.label(self.LABEL_TEXT, "C", (0, 0), "C")
self.label.set_readonly(True)
self.add_item(self.label)
self.setAxisMaxMajor(self.Z_AXIS, self.Z_MAX_MAJOR)
self.setAxisMaxMinor(self.Z_AXIS, 0)
def set_curve_style(self, section: str, option: str) -> None:
"""Set curve style
Args:
section: Configuration section name
option: Configuration option name
"""
self.param.read_config(CONF, section, option)
self.param.label = self.CURVE_LABEL
def connect_plot(self, plot: BasePlot) -> None:
"""Connect plot to cross section plot
Args:
plot: Plot to connect (containing the image items to be used for)
"""
if plot.options.type == PlotType.CURVE:
# Connecting only to image plot widgets (allow mixing image and
# curve widgets for the same plot manager -- e.g. in pyplot)
return
plot.SIG_ITEMS_CHANGED.connect(self.items_changed)
plot.SIG_LUT_CHANGED.connect(self.lut_changed)
plot.SIG_MASK_CHANGED.connect(lambda item: self.update_plot())
plot.SIG_ACTIVE_ITEM_CHANGED.connect(self.active_item_changed)
plot.SIG_ANNOTATION_CHANGED.connect(self.shape_changed)
plot.SIG_PLOT_LABELS_CHANGED.connect(self.plot_labels_changed)
plot.SIG_AXIS_DIRECTION_CHANGED.connect(self.axis_dir_changed)
plot.SIG_PLOT_AXIS_CHANGED.connect(self.plot_axis_changed)
self.plot_labels_changed(plot)
for axis_id in AXIS_IDS:
self.axis_dir_changed(plot, axis_id)
self.items_changed(plot)
def register_shape(self, plot: BasePlot, shape: Any, refresh: bool = True) -> None:
"""Register shape associated to cross section plot
Args:
plot: Plot containing the shape and the associated image items
shape: Shape to register, and to be used for cross section
refresh: Whether to refresh the plot
"""
known_shapes = self._shapes.get(plot, [])
if shape in known_shapes:
return
self._shapes[plot] = known_shapes + [shape]
self.update_plot(shape, refresh=refresh and self.autorefresh_mode)
def unregister_shape(self, shape: Any) -> None:
"""Unregister shape associated to cross section plot
Args:
shape: Shape to unregister
"""
for plot in self._shapes:
shapes = self._shapes[plot]
if shape in shapes:
shapes.pop(shapes.index(shape))
if len(shapes) == 0 or shape is self.get_last_obj():
for curve in self.get_cross_section_curves():
curve.clear_data()
self.replot()
break
def create_cross_section_item(self) -> CrossSectionItem:
"""Create cross section item"""
raise NotImplementedError
def add_cross_section_item(self, source: BaseImageItem) -> None:
"""Add cross section item
Args:
source: Source image item to add cross section item for
"""
curve = self.create_cross_section_item()
curve.set_source_image(source)
curve.set_readonly(True)
self.add_item(curve, z=0)
self.known_items[source] = curve
def get_cross_section_curves(self) -> list[CrossSectionItem]:
"""Return cross section curves"""
return list(self.known_items.values())
def items_changed(self, plot: BasePlot) -> None:
"""Items have changed in the plot
Args:
plot: Plot containing the image items to be used for cross section
"""
# Del obsolete cross section items
new_sources = plot.get_items(item_type=ICSImageItemType)
for source in self.known_items.copy():
if source not in new_sources:
curve = self.known_items.pop(source)
curve.clear_data() # useful to emit SIG_CS_CURVE_CHANGED
# (eventually notify other panels that the
# cross section curve is now empty)
self.del_item(curve)
# Update plot only to show/hide cross section curves according to
# the associated image item visibility state (hence `refresh=False`)
self.update_plot(refresh=False)
self.plot_axis_changed(plot)
if not new_sources:
self.replot()
return
self.param.shade = self.SHADE / len(new_sources)
for source in new_sources:
if source not in self.known_items and source.isVisible():
if not self.single_source or not self.known_items:
self.add_cross_section_item(source=source)
def active_item_changed(self, plot: BasePlot) -> None:
"""Active item has just changed
Args:
plot: Plot containing the image items to be used for cross section
"""
self.shape_changed(plot.get_active_item())
def plot_labels_changed(self, plot: BasePlot) -> None:
"""Plot labels have changed
Args:
plot: Plot containing the image items to be used for cross section
"""
raise NotImplementedError
def axis_dir_changed(self, plot: BasePlot, axis_id: str) -> None:
"""An axis direction has changed
Args:
plot: Plot containing the image items to be used for cross section
axis_id: Axis ID
"""
pass
def plot_axis_changed(self, plot: BasePlot) -> None:
"""Plot was just zoomed/panned
Args:
plot: Plot containing the image items to be used for cross section
"""
if self.autoscale_mode:
self.do_autoscale(replot=True)
elif self.lockscales:
self.do_autoscale(replot=False, axis_id=self.Z_AXIS)
vmin, vmax = plot.get_axis_limits(self.CS_AXIS)
self.set_axis_limits(self.CS_AXIS, vmin, vmax)
self.replot()
def is_shape_known(self, shape: Any) -> bool:
"""Return whether shape is known
Args:
shape: Shape to check
Returns:
bool: Whether shape is known
"""
for shapes in list(self._shapes.values()):
if shape in shapes:
return True
else:
return False
def shape_changed(self, shape: Any) -> None:
"""Shape has changed
Args:
shape: Shape that has changed
"""
if self.autorefresh_mode:
if self.is_shape_known(shape):
self.update_plot(shape)
def get_last_obj(self) -> BaseImageItem | None:
"""Return last active object"""
if self.last_obj is not None:
return self.last_obj()
def update_plot(self, obj: Any = None, refresh: bool = True) -> None:
"""
Update cross section curves
Args:
obj: Object to update cross section curves for
(may be a marker or a rectangular shape, or None to update)
refresh: Whether to refresh the plot
"""
if obj is None:
obj = self.get_last_obj()
if obj is None:
return
else:
self.last_obj = weakref.ref(obj)
plot = obj.plot()
if plot is None:
self.unregister_shape(obj)
return
if self.label.isVisible():
self.label.hide()
items = list(self.known_items.items())
for item, curve in iter(items):
if not item.isVisible():
curve.hide()
else:
curve.show()
curve.perimage_mode = self.perimage_mode
curve.autoscale_mode = self.autoscale_mode
curve.apply_lut = self.apply_lut
if refresh:
curve.update_item(obj)
# Delegate all scaling logic to plot_axis_changed for consistency
self.plot_axis_changed(plot)
def toggle_perimage_mode(self, state: bool) -> None:
"""Toggle the per item mode
Args:
state: State to toggle to
"""
self.perimage_mode = state
self.update_plot()
def toggle_autoscale(self, state: bool) -> None:
"""Toggle the autoscale mode
Args:
state: State to toggle to
"""
self.autoscale_mode = state
self.update_plot()
def toggle_autorefresh(self, state: bool) -> None:
"""Toggle the autorefresh mode
Args:
state: State to toggle to
"""
self.autorefresh_mode = state
if state:
self.update_plot()
def toggle_apply_lut(self, state: bool) -> None:
"""Toggle the apply LUT mode
Args:
state: State to toggle to
"""
self.apply_lut = state
self.update_plot()
if self.apply_lut:
self.set_axis_title(self.Z_AXIS, LUT_AXIS_TITLE)
self.set_axis_color(self.Z_AXIS, "red")
else:
obj = self.get_last_obj()
if obj is not None and obj.plot() is not None:
self.plot_labels_changed(obj.plot())
def toggle_lockscales(self, state: bool) -> None:
"""Toggle the lock scales mode
Args:
state: State to toggle to
"""
self.lockscales = state
obj = self.get_last_obj()
if obj is not None and obj.plot() is not None:
self.plot_axis_changed(obj.plot())
def lut_changed(self, plot: BasePlot) -> None:
"""LUT has changed
Args:
plot: Plot containing the image items to be used for cross section
"""
if self.apply_lut:
self.update_plot()
class HorizontalCrossSectionPlot(BaseCrossSectionPlot):
LABEL_TEXT = _("Enable a marker")
CS_AXIS = X_BOTTOM
Z_AXIS = Y_LEFT
def plot_labels_changed(self, plot: BasePlot) -> None:
"""Plot labels have changed
Args:
plot: Plot containing the image items to be used for cross section
"""
self.set_axis_title("left", plot.get_axis_title("right"))
self.set_axis_title("bottom", plot.get_axis_title("bottom"))
self.set_axis_color("left", plot.get_axis_color("right"))
self.set_axis_color("bottom", plot.get_axis_color("bottom"))
def axis_dir_changed(self, plot: BasePlot, axis_id: str) -> None:
"""An axis direction has changed
Args:
plot: Plot containing the image items to be used for cross section
axis_id: Axis ID
"""
if axis_id == plot.X_BOTTOM:
self.set_axis_direction("bottom", plot.get_axis_direction("bottom"))
self.replot()
class VerticalCrossSectionPlot(BaseCrossSectionPlot):
LABEL_TEXT = _("Enable a marker")
CS_AXIS = Y_LEFT
Z_AXIS = X_BOTTOM
Z_MAX_MAJOR = 3
def plot_labels_changed(self, plot: BasePlot) -> None:
"""Plot labels have changed
Args:
plot: Plot containing the image items to be used for cross section
"""
self.set_axis_title("bottom", plot.get_axis_title("right"))
self.set_axis_title("left", plot.get_axis_title("left"))
self.set_axis_color("bottom", plot.get_axis_color("right"))
self.set_axis_color("left", plot.get_axis_color("left"))
def axis_dir_changed(self, plot: BasePlot, axis_id: str) -> None:
"""An axis direction has changed
Args:
plot: Plot containing the image items to be used for cross section
axis_id: Axis ID
"""
if axis_id == plot.Y_LEFT:
self.set_axis_direction("left", plot.get_axis_direction("left"))
self.replot()
class XYCrossSectionMixin:
def connect_plot(self, plot: BasePlot) -> None:
"""Connect plot to cross section plot
Args:
plot: Plot to connect (containing the image items to be used for)
"""
BaseCrossSectionPlot.connect_plot(self, plot)
plot.SIG_MARKER_CHANGED.connect(
lambda marker: BaseCrossSectionPlot.update_plot(self, marker)
)
class XCrossSectionPlot(HorizontalCrossSectionPlot, XYCrossSectionMixin):
"""X-axis cross section plot"""
_HEIGHT = 130
def sizeHint(self) -> QC.QSize:
"""Reimplemented from QWidget.sizeHint"""
return QC.QSize(self.width(), self._HEIGHT)
def create_cross_section_item(self) -> XCrossSectionItem:
"""Create cross section item"""
return XCrossSectionItem(self.param)
class YCrossSectionPlot(VerticalCrossSectionPlot, XYCrossSectionMixin):
"""Y-axis cross section plot"""
_WIDTH = 140
def sizeHint(self) -> QC.QSize:
"""Reimplemented from QWidget.sizeHint"""
return QC.QSize(self._WIDTH, self.height())
def create_cross_section_item(self) -> YCrossSectionItem:
"""Create cross section item"""
return YCrossSectionItem(self.param)
# Oblique cross section plot
class ObliqueCrossSectionPlot(HorizontalCrossSectionPlot):
"""Oblique averaged cross section plot"""
PLOT_TITLE = _("Oblique averaged cross section")
CURVE_LABEL = _("Oblique averaged cross section")
LABEL_TEXT = _("Activate the oblique cross section tool")
SHADE = 0.0
def __init__(self, parent=None):
super().__init__(parent)
self.set_title(self.PLOT_TITLE)
self.single_source = True
def create_cross_section_item(self) -> ObliqueCrossSectionItem:
"""Create cross section item"""
return ObliqueCrossSectionItem(self.param)
# Line cross section plot
class LineCrossSectionPlot(HorizontalCrossSectionPlot):
"""Line cross section plot"""
PLOT_TITLE = _("Line cross section")
CURVE_LABEL = _("Line cross section")
LABEL_TEXT = _("Activate the line cross section tool")
SHADE = 0.0
def __init__(self, parent=None):
super().__init__(parent)
self.set_title(self.PLOT_TITLE)
self.single_source = True
def create_cross_section_item(self) -> LineCrossSectionItem:
"""Create cross section item"""
return LineCrossSectionItem(self.param)