-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathowscatterplotgraph.py
More file actions
1259 lines (1074 loc) · 45.2 KB
/
owscatterplotgraph.py
File metadata and controls
1259 lines (1074 loc) · 45.2 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
import sys
import itertools
from xml.sax.saxutils import escape
from math import log10, floor, ceil
import numpy as np
import scipy.sparse as sp
from scipy.stats import linregress
from AnyQt.QtCore import Qt, QObject, QEvent, QRectF, QPointF, QSize
from AnyQt.QtGui import (
QStaticText, QColor, QPen, QBrush, QPainterPath, QTransform, QPainter, QKeySequence)
from AnyQt.QtWidgets import QApplication, QToolTip, QPinchGesture, \
QGraphicsTextItem, QGraphicsRectItem, QAction
import pyqtgraph as pg
from pyqtgraph.graphicsItems.ViewBox import ViewBox
import pyqtgraph.graphicsItems.ScatterPlotItem
from pyqtgraph.graphicsItems.LegendItem import LegendItem, ItemSample
from pyqtgraph.graphicsItems.ScatterPlotItem import ScatterPlotItem
from pyqtgraph.graphicsItems.TextItem import TextItem
from pyqtgraph.graphicsItems.InfiniteLine import InfiniteLine
from pyqtgraph.Point import Point
from Orange.widgets import gui
from Orange.widgets.utils import classdensity, get_variable_values_sorted
from Orange.widgets.utils.colorpalette import (ColorPaletteGenerator,
ContinuousPaletteGenerator)
from Orange.widgets.utils.plot import \
OWPalette, OWPlotGUI, SELECT, PANNING, ZOOMING
from Orange.widgets.utils.scaling import ScaleScatterPlotData
from Orange.widgets.settings import Setting, ContextSetting
# TODO Move utility classes to another module, so they can be used elsewhere
SELECTION_WIDTH = 5
class PaletteItemSample(ItemSample):
"""A color strip to insert into legends for discretized continuous values"""
def __init__(self, palette, scale):
"""
:param palette: palette used for showing continuous values
:type palette: ContinuousPaletteGenerator
:param scale: an instance of DiscretizedScale that defines the
conversion of values into bins
:type scale: DiscretizedScale
"""
super().__init__(None)
self.palette = palette
self.scale = scale
cuts = ["{0:.{1}f}".format(scale.offset + i * scale.width, scale.decimals)
for i in range(scale.bins + 1)]
self.labels = [QStaticText("{} - {}".format(fr, to))
for fr, to in zip(cuts, cuts[1:])]
for label in self.labels:
label.prepare()
self.text_width = max(label.size().width() for label in self.labels)
def boundingRect(self):
return QRectF(0, 0, 40 + self.text_width, 20 + self.scale.bins * 15)
def paint(self, p, *args):
p.setRenderHint(p.Antialiasing)
scale = self.scale
palette = self.palette
font = p.font()
font.setPixelSize(11)
p.setFont(font)
for i, label in enumerate(self.labels):
color = QColor(*palette.getRGB((i + 0.5) / scale.bins))
p.setPen(Qt.NoPen)
p.setBrush(QBrush(color))
p.drawRect(0, i * 15, 15, 15)
p.setPen(QPen(Qt.black))
p.drawStaticText(20, i * 15 + 1, label)
class LegendItem(LegendItem):
def __init__(self, size=None, offset=None, pen=None, brush=None):
super().__init__(size, offset)
self.layout.setContentsMargins(5, 5, 5, 5)
self.layout.setVerticalSpacing(0)
self.layout.setHorizontalSpacing(15)
self.layout.setColumnAlignment(1, Qt.AlignLeft | Qt.AlignVCenter)
if pen is None:
pen = QPen(QColor(196, 197, 193, 200), 1)
pen.setCosmetic(True)
self.__pen = pen
if brush is None:
brush = QBrush(QColor(232, 232, 232, 100))
self.__brush = brush
def storeAnchor(self):
"""
Return the current relative anchor position (relative to the parent)
"""
anchor = legend_anchor_pos(self)
if anchor is None:
anchor = ((1, 0), (1, 0))
return anchor
def restoreAnchor(self, anchors):
"""
Restore (parent) relative position from stored anchors.
The restored position is within the parent bounds.
"""
anchor, parentanchor = anchors
self.anchor(*bound_anchor_pos(anchor, parentanchor))
def setPen(self, pen):
"""Set the legend frame pen."""
pen = QPen(pen)
if pen != self.__pen:
self.prepareGeometryChange()
self.__pen = pen
self.updateGeometry()
def pen(self):
"""Pen used to draw the legend frame."""
return QPen(self.__pen)
def setBrush(self, brush):
"""Set background brush"""
brush = QBrush(brush)
if brush != self.__brush:
self.__brush = brush
self.update()
def brush(self):
"""Background brush."""
return QBrush(self._brush)
def paint(self, painter, option, widget=None):
painter.setPen(self.__pen)
painter.setBrush(self.__brush)
rect = self.contentsRect()
painter.drawRoundedRect(rect, 2, 2)
def addItem(self, item, name):
super().addItem(item, name)
# Fix-up the label alignment
_, label = self.items[-1]
label.setText(name, justify="left")
def clear(self):
"""
Clear all legend items.
"""
items = list(self.items)
self.items = []
for sample, label in items:
self.layout.removeItem(sample)
self.layout.removeItem(label)
sample.hide()
label.hide()
self.updateSize()
ANCHORS = {
Qt.TopLeftCorner: (0, 0),
Qt.TopRightCorner: (1, 0),
Qt.BottomLeftCorner: (0, 1),
Qt.BottomRightCorner: (1, 1)
}
def corner_anchor(corner):
"""Return the relative corner coordinates for Qt.Corner
"""
return ANCHORS[corner]
def legend_anchor_pos(legend):
"""
Return the legend's anchor positions relative to it's parent (if defined).
Return `None` if legend does not have a parent or the parent's size
is empty.
.. seealso:: LegendItem.anchor, rect_anchor_pos
"""
parent = legend.parentItem()
if parent is None or parent.size().isEmpty():
return None
rect = legend.geometry() # in parent coordinates.
parent_rect = QRectF(QPointF(0, 0), parent.size())
# Find the closest corner of rect to parent rect
c1, _, *parentPos = rect_anchor_pos(rect, parent_rect)
return corner_anchor(c1), tuple(parentPos)
def bound_anchor_pos(corner, parentpos):
corner = np.clip(corner, 0, 1)
parentpos = np.clip(parentpos, 0, 1)
irx, iry = corner
prx, pry = parentpos
if irx > 0.9 and prx < 0.1:
irx = prx = 0.0
if iry > 0.9 and pry < 0.1:
iry = pry = 0.0
if irx < 0.1 and prx > 0.9:
irx = prx = 1.0
if iry < 0.1 and pry > 0.9:
iry = pry = 1.0
return (irx, iry), (prx, pry)
def rect_anchor_pos(rect, parent_rect):
"""
Find the 'best' anchor corners of rect within parent_rect.
Return a tuple of (rect_corner, parent_corner, rx, ry),
where rect/parent_corners are Qt.Corners which are closest and
rx, ry are the relative positions of the rect_corner within
parent_rect. If the parent_rect is empty return `None`.
"""
if parent_rect.isEmpty():
return None
# Find the closest corner of rect to parent rect
corners = (Qt.TopLeftCorner, Qt.TopRightCorner,
Qt.BottomRightCorner, Qt.BottomLeftCorner)
def rect_corner(rect, corner):
if corner == Qt.TopLeftCorner:
return rect.topLeft()
elif corner == Qt.TopRightCorner:
return rect.topRight()
elif corner == Qt.BottomLeftCorner:
return rect.bottomLeft()
elif corner == Qt.BottomRightCorner:
return rect.bottomRight()
else:
assert False
def corner_dist(c1, c2):
d = (rect_corner(rect, c1) - rect_corner(parent_rect, c2))
return d.x() ** 2 + d.y() ** 2
if parent_rect.contains(rect):
closest = min(corners,
key=lambda corner: corner_dist(corner, corner))
p = rect_corner(rect, closest)
return (closest, closest,
(p.x() - parent_rect.left()) / parent_rect.width(),
(p.y() - parent_rect.top()) / parent_rect.height())
else:
c1, c2 = min(itertools.product(corners, corners),
key=lambda pair: corner_dist(*pair))
p = rect_corner(rect, c1)
return (c1, c2,
(p.x() - parent_rect.left()) / parent_rect.width(),
(p.y() - parent_rect.top()) / parent_rect.height())
class DiscretizedScale:
"""
Compute suitable bins for continuous value from its minimal and
maximal value.
The width of the bin is a power of 10 (including negative powers).
The minimal value is rounded up and the maximal is rounded down. If this
gives less than 3 bins, the width is divided by four; if it gives
less than 6, it is halved.
.. attribute:: offset
The start of the first bin.
.. attribute:: width
The width of the bins
.. attribute:: bins
The number of bins
.. attribute:: decimals
The number of decimals used for printing out the boundaries
"""
def __init__(self, min_v, max_v):
"""
:param min_v: Minimal value
:type min_v: float
:param max_v: Maximal value
:type max_v: float
"""
super().__init__()
dif = max_v - min_v if max_v != min_v else 1
if np.isnan(dif):
min_v = 0
dif = decimals = 1
else:
decimals = -floor(log10(dif))
resolution = 10 ** -decimals
bins = ceil(dif / resolution)
if bins < 6:
decimals += 1
if bins < 3:
resolution /= 4
else:
resolution /= 2
bins = ceil(dif / resolution)
self.offset = resolution * floor(min_v // resolution)
self.bins = bins
self.decimals = max(decimals, 0)
self.width = resolution
def compute_bins(self, a):
"""
Compute bin number(s) for the given value(s).
:param a: value(s)
:type a: a number or numpy.ndarray
"""
a = (a - self.offset) / self.width
if isinstance(a, np.ndarray):
a.clip(0, self.bins - 1)
else:
a = min(self.bins - 1, max(0, a))
return a
class InteractiveViewBox(ViewBox):
def __init__(self, graph, enable_menu=False):
self.init_history()
ViewBox.__init__(self, enableMenu=enable_menu)
self.graph = graph
self.setMouseMode(self.PanMode)
self.grabGesture(Qt.PinchGesture)
def _dragtip_pos(self):
return 10, self.height()
def safe_update_scale_box(self, buttonDownPos, currentPos):
x, y = currentPos
if buttonDownPos[0] == x:
x += 1
if buttonDownPos[1] == y:
y += 1
self.updateScaleBox(buttonDownPos, Point(x, y))
# noinspection PyPep8Naming,PyMethodOverriding
def mouseDragEvent(self, ev, axis=None):
if self.graph.state == SELECT and axis is None:
ev.accept()
pos = ev.pos()
if ev.button() == Qt.LeftButton:
self.safe_update_scale_box(ev.buttonDownPos(), ev.pos())
scene = self.scene()
dragtip = scene.drag_tooltip
if ev.isFinish():
dragtip.hide()
self.rbScaleBox.hide()
pixel_rect = QRectF(ev.buttonDownPos(ev.button()), pos)
value_rect = self.childGroup.mapRectFromParent(pixel_rect)
self.graph.select_by_rectangle(value_rect)
else:
dragtip.setPos(*self._dragtip_pos())
dragtip.show() # although possibly already shown
self.safe_update_scale_box(ev.buttonDownPos(), ev.pos())
elif self.graph.state == ZOOMING or self.graph.state == PANNING:
ev.ignore()
super().mouseDragEvent(ev, axis=axis)
else:
ev.ignore()
def updateAutoRange(self):
# indirectly called by the autorange button on the graph
super().updateAutoRange()
self.tag_history()
def tag_history(self):
#add current view to history if it differs from the last view
if self.axHistory:
currentview = self.viewRect()
lastview = self.axHistory[self.axHistoryPointer]
inters = currentview & lastview
united = currentview.united(lastview)
if inters.width()*inters.height()/(united.width()*united.height()) > 0.95:
return
self.axHistoryPointer += 1
self.axHistory = self.axHistory[:self.axHistoryPointer] + \
[self.viewRect()]
def init_history(self):
self.axHistory = []
self.axHistoryPointer = -1
def autoRange(self, padding=None, items=None, item=None):
super().autoRange(padding=padding, items=items, item=item)
self.tag_history()
def suggestPadding(self, axis): #no padding so that undo works correcty
return 0.
def scaleHistory(self, d):
self.tag_history()
super().scaleHistory(d)
def mouseClickEvent(self, ev):
if ev.button() == Qt.RightButton: # undo zoom
self.scaleHistory(-1)
else:
ev.accept()
self.graph.unselect_all()
def sceneEvent(self, event):
if event.type() == QEvent.Gesture:
return self.gestureEvent(event)
return super().sceneEvent(event)
def gestureEvent(self, event):
gesture = event.gesture(Qt.PinchGesture)
if gesture.state() == Qt.GestureStarted:
event.accept(gesture)
elif gesture.changeFlags() & QPinchGesture.ScaleFactorChanged:
center = self.mapSceneToView(gesture.centerPoint())
scale_prev = gesture.lastScaleFactor()
scale = gesture.scaleFactor()
if scale_prev != 0:
scale = scale / scale_prev
if scale > 0:
self.scaleBy((1 / scale, 1 / scale), center)
elif gesture.state() == Qt.GestureFinished:
self.tag_history()
return True
class ScatterPlotItem(pg.ScatterPlotItem):
def paint(self, painter, option, widget=None):
painter.setRenderHint(QPainter.SmoothPixmapTransform, True)
super().paint(painter, option, widget)
def _define_symbols():
"""
Add symbol ? to ScatterPlotItemSymbols,
reflect the triangle to point upwards
"""
symbols = pyqtgraph.graphicsItems.ScatterPlotItem.Symbols
path = QPainterPath()
path.addEllipse(QRectF(-0.35, -0.35, 0.7, 0.7))
path.moveTo(-0.5, 0.5)
path.lineTo(0.5, -0.5)
path.moveTo(-0.5, -0.5)
path.lineTo(0.5, 0.5)
symbols["?"] = path
tr = QTransform()
tr.rotate(180)
symbols['t'] = tr.map(symbols['t'])
_define_symbols()
class OWScatterPlotGraph(gui.OWComponent, ScaleScatterPlotData):
attr_color = ContextSetting(None, required=ContextSetting.OPTIONAL)
attr_label = ContextSetting(None, required=ContextSetting.OPTIONAL)
attr_shape = ContextSetting(None, required=ContextSetting.OPTIONAL)
attr_size = ContextSetting(None, required=ContextSetting.OPTIONAL)
label_only_selected = Setting(False)
point_width = Setting(10)
alpha_value = Setting(128)
show_grid = Setting(False)
show_legend = Setting(True)
tooltip_shows_all = Setting(False)
class_density = Setting(False)
show_reg_line = Setting(False)
resolution = 256
CurveSymbols = np.array("o x t + d s ?".split())
MinShapeSize = 6
DarkerValue = 120
UnknownColor = (168, 50, 168)
def __init__(self, scatter_widget, parent=None, _="None", view_box=InteractiveViewBox):
gui.OWComponent.__init__(self, scatter_widget)
self.view_box = view_box(self)
self.plot_widget = pg.PlotWidget(viewBox=self.view_box, parent=parent,
background="w")
self.plot_widget.getPlotItem().buttonsHidden = True
self.plot_widget.setAntialiasing(True)
self.plot_widget.sizeHint = lambda: QSize(500, 500)
scene = self.plot_widget.scene()
self._create_drag_tooltip(scene)
self._data = None # Original Table as passed from widget to new_data before transformations
self.replot = self.plot_widget.replot
ScaleScatterPlotData.__init__(self)
self.density_img = None
self.scatterplot_item = None
self.scatterplot_item_sel = None
self.reg_line_item = None
self.labels = []
self.master = scatter_widget
self.master.Warning.add_message(
"missing_coords",
"Plot cannot be displayed because '{}' or '{}' is missing for "
"all data points")
self.master.Information.add_message(
"missing_coords",
"Points with missing '{}' or '{}' are not displayed")
self.master.Information.add_message(
"missing_size",
"Points with undefined '{}' are shown in smaller size")
self.master.Information.add_message(
"missing_shape",
"Points with undefined '{}' are shown as crossed circles")
self.shown_attribute_indices = []
self.shown_x = self.shown_y = None
self.pen_colors = self.brush_colors = None
self.valid_data = None # np.ndarray
self.selection = None # np.ndarray
self.n_points = 0
self.gui = OWPlotGUI(self)
self.continuous_palette = ContinuousPaletteGenerator(
QColor(255, 255, 0), QColor(0, 0, 255), True)
self.discrete_palette = ColorPaletteGenerator()
self.selection_behavior = 0
self.legend = self.color_legend = None
self.__legend_anchor = (1, 0), (1, 0)
self.__color_legend_anchor = (1, 1), (1, 1)
self.scale = None # DiscretizedScale
self.subset_indices = None
# self.setMouseTracking(True)
# self.grabGesture(QPinchGesture)
# self.grabGesture(QPanGesture)
self.update_grid()
self._tooltip_delegate = HelpEventDelegate(self.help_event)
self.plot_widget.scene().installEventFilter(self._tooltip_delegate)
def _create_drag_tooltip(self, scene):
tip_parts = [
(Qt.ShiftModifier, "Shift: Add group"),
(Qt.ShiftModifier + Qt.ControlModifier,
"Shift-{}: Append to group".
format("Cmd" if sys.platform == "darwin" else "Ctrl")),
(Qt.AltModifier, "Alt: Remove")
]
all_parts = ", ".join(part for _, part in tip_parts)
self.tiptexts = {
int(modifier): all_parts.replace(part, "<b>{}</b>".format(part))
for modifier, part in tip_parts
}
self.tiptexts[0] = all_parts
self.tip_textitem = text = QGraphicsTextItem()
# Set to the longest text
text.setHtml(self.tiptexts[Qt.ShiftModifier + Qt.ControlModifier])
text.setPos(4, 2)
r = text.boundingRect()
rect = QGraphicsRectItem(0, 0, r.width() + 8, r.height() + 4)
rect.setBrush(QColor(224, 224, 224, 212))
rect.setPen(QPen(Qt.NoPen))
self.update_tooltip(Qt.NoModifier)
scene.drag_tooltip = scene.createItemGroup([rect, text])
scene.drag_tooltip.hide()
def update_tooltip(self, modifiers):
modifiers &= Qt.ShiftModifier + Qt.ControlModifier + Qt.AltModifier
text = self.tiptexts.get(int(modifiers), self.tiptexts[0])
self.tip_textitem.setHtml(text)
def new_data(self, data, subset_data=None, new=True, **args):
if new:
self.plot_widget.clear()
self.remove_legend()
self.density_img = None
self.scatterplot_item = None
self.scatterplot_item_sel = None
self.reg_line_item = None
self.labels = []
self.selection = None
self.valid_data = None
self.subset_indices = set(e.id for e in subset_data) if subset_data else None
self._data = data
data = self.sparse_to_dense()
self.set_data(data, **args)
def sparse_to_dense(self):
data = self._data
if data is None or not data.is_sparse():
return data
attrs = {self.shown_x,
self.shown_y,
self.attr_color,
self.attr_shape,
self.attr_size,
self.attr_label}
domain = data.domain
all_attrs = domain.variables + domain.metas
attrs = list(set(all_attrs) & attrs)
selected_data = data[:, attrs].to_dense()
return selected_data
def _clear_plot_widget(self):
self.remove_legend()
if self.density_img:
self.plot_widget.removeItem(self.density_img)
self.density_img = None
if self.scatterplot_item:
self.plot_widget.removeItem(self.scatterplot_item)
self.scatterplot_item = None
if self.scatterplot_item_sel:
self.plot_widget.removeItem(self.scatterplot_item_sel)
self.scatterplot_item_sel = None
if self.reg_line_item:
self.plot_widget.removeItem(self.reg_line_item)
self.reg_line_item = None
for label in self.labels:
self.plot_widget.removeItem(label)
self.labels = []
self.set_axis_title("bottom", "")
self.set_axis_title("left", "")
def update_data(self, attr_x, attr_y, reset_view=True):
self.master.Warning.missing_coords.clear()
self.master.Information.missing_coords.clear()
self._clear_plot_widget()
if self.shown_y != attr_y:
# 'reset' the axis text width estimation. Without this the left
# axis tick labels space only ever expands
yaxis = self.plot_widget.getAxis("left")
yaxis.textWidth = 30
self.shown_x, self.shown_y = attr_x, attr_y
if attr_x not in self.data.domain or attr_y not in self.data.domain:
data = self.sparse_to_dense()
self.set_data(data)
if self.jittered_data is None or not len(self.jittered_data):
self.valid_data = None
else:
self.valid_data = self.get_valid_list([attr_x, attr_y])
if not np.any(self.valid_data):
self.valid_data = None
if self.valid_data is None:
self.selection = None
self.n_points = 0
self.master.Warning.missing_coords(
self.shown_x.name, self.shown_y.name)
return
x_data, y_data = self.get_xy_data_positions(
attr_x, attr_y, self.valid_data)
self.n_points = len(x_data)
if reset_view:
min_x, max_x = np.nanmin(x_data), np.nanmax(x_data)
min_y, max_y = np.nanmin(y_data), np.nanmax(y_data)
self.view_box.setRange(
QRectF(min_x, min_y, max_x - min_x, max_y - min_y),
padding=0.025)
self.view_box.init_history()
self.view_box.tag_history()
[min_x, max_x], [min_y, max_y] = self.view_box.viewRange()
for axis, var in (("bottom", attr_x), ("left", attr_y)):
self.set_axis_title(axis, var)
if var.is_discrete:
self.set_labels(axis, get_variable_values_sorted(var))
else:
self.set_labels(axis, None)
color_data, brush_data = self.compute_colors()
color_data_sel, brush_data_sel = self.compute_colors_sel()
size_data = self.compute_sizes()
shape_data = self.compute_symbols()
if self.should_draw_density():
rgb_data = [pen.color().getRgb()[:3] for pen in color_data]
self.density_img = classdensity.class_density_image(
min_x, max_x, min_y, max_y, self.resolution,
x_data, y_data, rgb_data)
self.plot_widget.addItem(self.density_img)
self.data_indices = np.flatnonzero(self.valid_data)
if len(self.data_indices) != len(self.data):
self.master.Information.missing_coords(
self.shown_x.name, self.shown_y.name)
self.scatterplot_item = ScatterPlotItem(
x=x_data, y=y_data, data=self.data_indices,
symbol=shape_data, size=size_data, pen=color_data, brush=brush_data
)
self.scatterplot_item_sel = ScatterPlotItem(
x=x_data, y=y_data, data=self.data_indices,
symbol=shape_data, size=size_data + SELECTION_WIDTH,
pen=color_data_sel, brush=brush_data_sel
)
self.plot_widget.addItem(self.scatterplot_item_sel)
self.plot_widget.addItem(self.scatterplot_item)
self.scatterplot_item.selected_points = []
self.scatterplot_item.sigClicked.connect(self.select_by_click)
self.draw_regression_line(x_data, y_data, min_x, max_x)
self.update_labels()
self.make_legend()
self.plot_widget.replot()
def draw_regression_line(self, x_data, y_data, min_x, max_x):
if self.show_reg_line and self.can_draw_regresssion_line():
slope, intercept, rvalue, _, _ = linregress(x_data, y_data)
start_y = min_x * slope + intercept
end_y = max_x * slope + intercept
angle = np.degrees(np.arctan((end_y - start_y) / (max_x - min_x)))
rotate = ((angle + 45) % 180) - 45 > 90
color = QColor("#505050")
l_opts = dict(color=color, position=abs(int(rotate) - 0.85),
rotateAxis=(1, 0), movable=True)
self.reg_line_item = InfiniteLine(
pos=QPointF(min_x, start_y), pen=pg.mkPen(color=color, width=1),
angle=angle, label="r = {:.2f}".format(rvalue), labelOpts=l_opts)
if rotate:
self.reg_line_item.label.angle = 180
self.reg_line_item.label.updateTransform()
self.plot_widget.addItem(self.reg_line_item)
def can_draw_density(self):
return self.domain is not None and \
self.attr_color is not None and \
self.attr_color.is_discrete and \
self.shown_x.is_continuous and \
self.shown_y.is_continuous
def should_draw_density(self):
return self.class_density and self.n_points > 1 and self.can_draw_density()
def can_draw_regresssion_line(self):
return self.domain is not None and \
self.shown_x.is_continuous and \
self.shown_y.is_continuous
def set_labels(self, axis, labels):
axis = self.plot_widget.getAxis(axis)
if labels:
ticks = [[(i, labels[i]) for i in range(len(labels))]]
axis.setTicks(ticks)
else:
axis.setTicks(None)
def set_axis_title(self, axis, title):
self.plot_widget.setLabel(axis=axis, text=title)
def compute_sizes(self):
self.master.Information.missing_size.clear()
if self.attr_size is None:
size_data = np.full((self.n_points,), self.point_width,
dtype=float)
else:
size_data = \
self.MinShapeSize + \
self.scaled_data.get_column_view(self.attr_size)[0][self.valid_data] * \
self.point_width
nans = np.isnan(size_data)
if np.any(nans):
size_data[nans] = self.MinShapeSize - 2
self.master.Information.missing_size(self.attr_size)
return size_data
def update_sizes(self):
self.set_data(self.sparse_to_dense())
self.update_point_size()
def update_point_size(self):
if self.scatterplot_item:
size_data = self.compute_sizes()
self.scatterplot_item.setSize(size_data)
self.scatterplot_item_sel.setSize(size_data + SELECTION_WIDTH)
def get_color(self):
if self.attr_color is None:
return None
colors = self.attr_color.colors
if self.attr_color.is_discrete:
self.discrete_palette = ColorPaletteGenerator(
number_of_colors=len(colors), rgb_colors=colors)
else:
self.continuous_palette = ContinuousPaletteGenerator(*colors)
return self.attr_color
def compute_colors_sel(self, keep_colors=False):
if not keep_colors:
self.pen_colors_sel = self.brush_colors_sel = None
def make_pen(color, width):
p = QPen(color, width)
p.setCosmetic(True)
return p
nopen = QPen(Qt.NoPen)
if self.selection is not None:
sels = np.max(self.selection)
if sels == 1:
pens = [nopen,
make_pen(QColor(255, 190, 0, 255),
SELECTION_WIDTH + 1.)]
else:
# Start with the first color so that the colors of the
# additional attribute in annotation (which start with 0,
# unselected) will match these colors
palette = ColorPaletteGenerator(number_of_colors=sels + 1)
pens = [nopen] + \
[make_pen(palette[i + 1], SELECTION_WIDTH + 1.)
for i in range(sels)]
pen = [pens[a] for a in self.selection[self.valid_data]]
else:
pen = [nopen] * self.n_points
brush = [QBrush(QColor(255, 255, 255, 0))] * self.n_points
return pen, brush
def compute_colors(self, keep_colors=False):
if not keep_colors:
self.pen_colors = self.brush_colors = None
self.get_color()
def make_pen(color, width):
p = QPen(color, width)
p.setCosmetic(True)
return p
subset = None
if self.subset_indices:
subset = np.array([ex.id in self.subset_indices
for ex in self.data[self.valid_data]])
if self.attr_color is None: # same color
color = self.plot_widget.palette().color(OWPalette.Data)
pen = [make_pen(color, 1.5)] * self.n_points
if subset is not None:
brush = [(QBrush(QColor(128, 128, 128, 0)),
QBrush(QColor(128, 128, 128, 255)))[s]
for s in subset]
else:
brush = [QBrush(QColor(128, 128, 128, self.alpha_value))] \
* self.n_points
return pen, brush
c_data = self.data.get_column_view(self.attr_color)[0][self.valid_data]
if self.attr_color.is_continuous:
if self.pen_colors is None:
self.scale = DiscretizedScale(np.nanmin(c_data), np.nanmax(c_data))
c_data -= self.scale.offset
c_data /= self.scale.width
c_data = np.floor(c_data) + 0.5
c_data /= self.scale.bins
c_data = np.clip(c_data, 0, 1)
palette = self.continuous_palette
self.pen_colors = palette.getRGB(c_data)
self.brush_colors = np.hstack(
[self.pen_colors,
np.full((self.n_points, 1), self.alpha_value, dtype=int)])
self.pen_colors *= 100
self.pen_colors //= self.DarkerValue
self.pen_colors = [make_pen(QColor(*col), 1.5)
for col in self.pen_colors.tolist()]
if subset is not None:
self.brush_colors[:, 3] = 0
self.brush_colors[subset, 3] = 255
else:
self.brush_colors[:, 3] = self.alpha_value
pen = self.pen_colors
brush = np.array([QBrush(QColor(*col))
for col in self.brush_colors.tolist()])
else:
if self.pen_colors is None:
palette = self.discrete_palette
n_colors = palette.number_of_colors
c_data = c_data.copy()
c_data[np.isnan(c_data)] = n_colors
c_data = c_data.astype(int)
colors = np.r_[palette.getRGB(np.arange(n_colors)),
[[128, 128, 128]]]
pens = np.array(
[make_pen(QColor(*col).darker(self.DarkerValue), 1.5)
for col in colors])
self.pen_colors = pens[c_data]
alpha = self.alpha_value if subset is None else 255
self.brush_colors = np.array([
[QBrush(QColor(0, 0, 0, 0)),
QBrush(QColor(col[0], col[1], col[2], alpha))]
for col in colors])
self.brush_colors = self.brush_colors[c_data]
if subset is not None:
brush = np.where(
subset,
self.brush_colors[:, 1], self.brush_colors[:, 0])
else:
brush = self.brush_colors[:, 1]
pen = self.pen_colors
return pen, brush
def update_colors(self, keep_colors=False):
self.master.update_colors()
self.set_data(self.sparse_to_dense())
self.update_alpha_value(keep_colors)
def update_alpha_value(self, keep_colors=False):
if self.scatterplot_item:
pen_data, brush_data = self.compute_colors(keep_colors)
pen_data_sel, brush_data_sel = self.compute_colors_sel(keep_colors)
self.scatterplot_item.setPen(pen_data, update=False, mask=None)
self.scatterplot_item.setBrush(brush_data, mask=None)
self.scatterplot_item_sel.setPen(pen_data_sel, update=False, mask=None)
self.scatterplot_item_sel.setBrush(brush_data_sel, mask=None)
if not keep_colors:
self.make_legend()
if self.should_draw_density():
self.update_data(self.shown_x, self.shown_y)
elif self.density_img:
self.plot_widget.removeItem(self.density_img)
def create_labels(self):
for x, y in zip(*self.scatterplot_item.getData()):
ti = TextItem()
self.plot_widget.addItem(ti)
ti.setPos(x, y)
self.labels.append(ti)
def _create_label_column(self):
if self.attr_label in self.data.domain:
label_column = self.data.get_column_view(self.attr_label)[0]
else:
label_column = self.master.data.get_column_view(self.attr_label)[0]
return label_column[self.data_indices]
def update_labels(self):
if self.attr_label is None or \
self.label_only_selected and self.selection is None:
for label in self.labels:
label.setText("")
return
self.assure_attribute_present(self.attr_label)
if not self.labels:
self.create_labels()
label_column = self._create_label_column()
formatter = self.attr_label.str_val
label_data = map(formatter, label_column)
black = pg.mkColor(0, 0, 0)
selection = self.selection[self.valid_data] if self.selection is not None else []
if self.label_only_selected:
for label, text, selected \
in zip(self.labels, label_data, selection):
label.setText(text if selected else "", black)
else:
for label, text in zip(self.labels, label_data):
label.setText(text, black)
def get_shape(self):
if self.attr_shape is None or \
len(self.attr_shape.values) > len(self.CurveSymbols):
return None
return self.attr_shape
def compute_symbols(self):
self.master.Information.missing_shape.clear()
if self.get_shape() is None:
shape_data = self.CurveSymbols[np.zeros(self.n_points, dtype=int)]
else:
shape_data = self.data.get_column_view(self.attr_shape)[0][self.valid_data]
nans = np.isnan(shape_data)
if np.any(nans):
shape_data[nans] = len(self.CurveSymbols) - 1