forked from biolab/orange3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathowscatterplotgraph.py
More file actions
1609 lines (1356 loc) · 57.8 KB
/
owscatterplotgraph.py
File metadata and controls
1609 lines (1356 loc) · 57.8 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
import warnings
from xml.sax.saxutils import escape
from math import log10, floor, ceil
from datetime import datetime, timezone
import numpy as np
from AnyQt.QtCore import Qt, QRectF, QSize, QTimer, pyqtSignal as Signal, \
QObject
from AnyQt.QtGui import QColor, QPen, QBrush, QPainterPath, QTransform, \
QPainter
from AnyQt.QtWidgets import QApplication, QToolTip, QGraphicsTextItem, \
QGraphicsRectItem, QGraphicsItemGroup
import pyqtgraph as pg
from pyqtgraph.graphicsItems.ScatterPlotItem import Symbols
from pyqtgraph.graphicsItems.LegendItem import LegendItem as PgLegendItem
from pyqtgraph.graphicsItems.TextItem import TextItem
from Orange.preprocess.discretize import _time_binnings
from Orange.util import utc_from_timestamp
from Orange.widgets import gui
from Orange.widgets.settings import Setting
from Orange.widgets.utils import classdensity, colorpalettes
from Orange.widgets.utils.plot import OWPalette
from Orange.widgets.visualize.utils.customizableplot import Updater, \
CommonParameterSetter
from Orange.widgets.visualize.utils.plotutils import (
HelpEventDelegate as EventDelegate, InteractiveViewBox as ViewBox,
PaletteItemSample, SymbolItemSample, AxisItem
)
SELECTION_WIDTH = 5
MAX_N_VALID_SIZE_ANIMATE = 1000
# maximum number of colors (including Other)
MAX_COLORS = 11
class LegendItem(PgLegendItem):
def __init__(self, size=None, offset=None, pen=None, brush=None):
super().__init__(size, offset)
self.layout.setContentsMargins(5, 5, 5, 5)
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 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))
# pylint: disable=arguments-differ
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()
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)
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 get_bins(self):
return self.offset + self.width * np.arange(self.bins + 1)
class ScatterPlotItem(pg.ScatterPlotItem):
"""
Modifies the behaviour of ScatterPlotItem as follows:
- Add z-index. ScatterPlotItem paints points in order of appearance in
self.data. Plotting by z-index is achieved by sorting before calling
super().paint() and re-sorting afterwards. Re-sorting (instead of
storing the original data) is needed because the inherited paint
may modify the data.
- Prevent multiple calls to updateSpots. ScatterPlotItem calls updateSpots
at any change of sizes/colors/symbols, which then rebuilds the stored
pixmaps for each symbol. Orange calls set* functions in succession,
so we postpone updateSpots() to paint()."""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._update_spots_in_paint = False
self._z_mapping = None
self._inv_mapping = None
def setZ(self, z):
"""
Set z values for all points.
Points with higher values are plotted on top of those with lower.
Args:
z (np.ndarray or None): a vector of z values
"""
if z is None:
self._z_mapping = self._inv_mapping = None
else:
assert len(z) == len(self.data)
self._z_mapping = np.argsort(z)
self._inv_mapping = np.argsort(self._z_mapping)
def setCoordinates(self, x, y):
"""
Change the coordinates of points while keeping other properties.
Asserts that the number of points stays the same.
Note. Pyqtgraph does not offer a method for this: setting coordinates
invalidates other data. We therefore retrieve the data to set it
together with the coordinates. Pyqtgraph also does not offer a
(documented) method for retrieving the data, yet using
data[prop]` looks reasonably safe.
The alternative, updating the whole scatterplot from the Orange Table,
is too slow.
"""
assert len(self.data) == len(x) == len(y)
data = dict(x=x, y=y)
for prop in ('pen', 'brush', 'size', 'symbol', 'data'):
data[prop] = self.data[prop]
self.setData(**data)
def updateSpots(self, dataSet=None): # pylint: disable=unused-argument
self._update_spots_in_paint = True
self.update()
# pylint: disable=arguments-differ
def paint(self, painter, option, widget=None):
try:
if self._z_mapping is not None:
assert len(self._z_mapping) == len(self.data)
self.data = self.data[self._z_mapping]
if self._update_spots_in_paint:
self._update_spots_in_paint = False
super().updateSpots()
painter.setRenderHint(QPainter.SmoothPixmapTransform, True)
super().paint(painter, option, widget)
finally:
if self._inv_mapping is not None:
self.data = self.data[self._inv_mapping]
def _define_symbols():
"""
Add symbol ? to ScatterPlotItemSymbols,
reflect the triangle to point upwards
"""
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
path = QPainterPath()
plusCoords = [
(-0.5, -0.1), (-0.5, 0.1), (-0.1, 0.1), (-0.1, 0.5),
(0.1, 0.5), (0.1, 0.1), (0.5, 0.1), (0.5, -0.1),
(0.1, -0.1), (0.1, -0.5), (-0.1, -0.5), (-0.1, -0.1)
]
path.moveTo(*plusCoords[0])
for x, y in plusCoords[1:]:
path.lineTo(x, y)
path.closeSubpath()
Symbols["+"] = path
tr = QTransform()
tr.rotate(180)
Symbols['t'] = tr.map(Symbols['t'])
tr = QTransform()
tr.rotate(45)
Symbols['x'] = tr.map(Symbols["+"])
_define_symbols()
def _make_pen(color, width):
p = QPen(color, width)
p.setCosmetic(True)
return p
class AxisItem(AxisItem):
"""
Axis that if needed displays ticks appropriate for time data.
"""
_label_width = 80
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._use_time = False
def use_time(self, enable):
"""Enables axes to display ticks for time data."""
self._use_time = enable
self.enableAutoSIPrefix(not enable)
def tickValues(self, minVal, maxVal, size):
"""Find appropriate tick locations."""
if not self._use_time:
return super().tickValues(minVal, maxVal, size)
# if timezone is not set, then local is used which cause exceptions
minVal = max(minVal,
datetime.min.replace(tzinfo=timezone.utc).timestamp() + 1)
maxVal = min(maxVal,
datetime.max.replace(tzinfo=timezone.utc).timestamp() - 1)
mn = utc_from_timestamp(minVal).timetuple()
mx = utc_from_timestamp(maxVal).timetuple()
try:
bins = _time_binnings(mn, mx, 6, 30)[-1]
except (IndexError, ValueError):
# cannot handle very large and very small time intervals
return super().tickValues(minVal, maxVal, size)
ticks = bins.thresholds
max_steps = max(int(size / self._label_width), 1)
if len(ticks) > max_steps:
# remove some of ticks so that they don't overlap
step = int(np.ceil(float(len(ticks)) / max_steps))
ticks = ticks[::step]
spacing = min(b - a for a, b in zip(ticks[:-1], ticks[1:]))
return [(spacing, ticks)]
def tickStrings(self, values, scale, spacing):
"""Format tick values according to space between them."""
if not self._use_time:
return super().tickStrings(values, scale, spacing)
if spacing >= 3600 * 24 * 365:
fmt = "%Y"
elif spacing >= 3600 * 24 * 28:
fmt = "%Y %b"
elif spacing >= 3600 * 24:
fmt = "%Y %b %d"
elif spacing >= 3600:
fmt = "%d %Hh"
elif spacing >= 60:
fmt = "%H:%M"
elif spacing >= 1:
fmt = "%H:%M:%S"
else:
fmt = '%S.%f'
return [utc_from_timestamp(x).strftime(fmt) for x in values]
class ScatterBaseParameterSetter(CommonParameterSetter):
CAT_LEGEND_LABEL = "Categorical legend"
NUM_LEGEND_LABEL = "Numerical legend"
NUM_LEGEND_SETTING = {
Updater.SIZE_LABEL: (range(4, 50), 11),
Updater.IS_ITALIC_LABEL: (None, False),
}
def __init__(self, master):
super().__init__()
self.master = master
self.cat_legend_settings = {}
self.num_legend_settings = {}
def update_setters(self):
self.initial_settings = {
self.LABELS_BOX: {
self.FONT_FAMILY_LABEL: self.FONT_FAMILY_SETTING,
self.TITLE_LABEL: self.FONT_SETTING,
self.LABEL_LABEL: self.FONT_SETTING,
self.CAT_LEGEND_LABEL: self.FONT_SETTING,
self.NUM_LEGEND_LABEL: self.NUM_LEGEND_SETTING,
},
self.ANNOT_BOX: {
self.TITLE_LABEL: {self.TITLE_LABEL: ("", "")},
}
}
def update_cat_legend(**settings):
self.cat_legend_settings.update(**settings)
Updater.update_legend_font(self.cat_legend_items, **settings)
def update_num_legend(**settings):
self.num_legend_settings.update(**settings)
Updater.update_num_legend_font(self.num_legend, **settings)
labels = self.LABELS_BOX
self._setters[labels][self.CAT_LEGEND_LABEL] = update_cat_legend
self._setters[labels][self.NUM_LEGEND_LABEL] = update_num_legend
@property
def title_item(self):
return self.master.plot_widget.getPlotItem().titleLabel
@property
def cat_legend_items(self):
items = self.master.color_legend.items
if items and items[0] and isinstance(items[0][0], PaletteItemSample):
items = []
return itertools.chain(self.master.shape_legend.items, items)
@property
def num_legend(self):
items = self.master.color_legend.items
if items and items[0] and isinstance(items[0][0], PaletteItemSample):
return self.master.color_legend
return None
@property
def labels(self):
return self.master.labels
class OWScatterPlotBase(gui.OWComponent, QObject):
"""
Provide a graph component for widgets that show any kind of point plot
The component plots a set of points with given coordinates, shapes,
sizes and colors. Its function is similar to that of a *view*, whereas
the widget represents a *model* and a *controler*.
The model (widget) needs to provide methods:
- `get_coordinates_data`, `get_size_data`, `get_color_data`,
`get_shape_data`, `get_label_data`, which return a 1d array (or two
arrays, for `get_coordinates_data`) of `dtype` `float64`, except for
`get_label_data`, which returns formatted labels;
- `get_shape_labels` returns a list of strings for shape legend
- `get_color_labels` returns strings for color legend, or a function for
formatting numbers if the legend is continuous, or None for default
formatting
- `get_tooltip`, which gives a tooltip for a single data point
- (optional) `impute_sizes`, `impute_shapes` get final coordinates and
shapes, and replace nans;
- `get_subset_mask` returns a bool array indicating whether a
data point is in the subset or not (e.g. in the 'Data Subset' signal
in the Scatter plot and similar widgets);
- `get_palette` returns a palette appropriate for visualizing the
current color data;
- `is_continuous_color` decides the type of the color legend;
The widget (in a role of controller) must also provide methods
- `selection_changed`
If `get_coordinates_data` returns `(None, None)`, the plot is cleared. If
`get_size_data`, `get_color_data` or `get_shape_data` return `None`,
all points will have the same size, color or shape, respectively.
If `get_label_data` returns `None`, there are no labels.
The view (this compomnent) provides methods `update_coordinates`,
`update_sizes`, `update_colors`, `update_shapes` and `update_labels`
that the widget (in a role of a controler) should call when any of
these properties are changed. If the widget calls, for instance, the
plot's `update_colors`, the plot will react by calling the widget's
`get_color_data` as well as the widget's methods needed to construct the
legend.
The view also provides a method `reset_graph`, which should be called only
when
- the widget gets entirely new data
- the number of points may have changed, for instance when selecting
a different attribute for x or y in the scatter plot, where the points
with missing x or y coordinates are hidden.
Every `update_something` calls the plot's `get_something`, which
calls the model's `get_something_data`, then it transforms this data
into whatever is needed (colors, shapes, scaled sizes) and changes the
plot. For the simplest example, here is `update_shapes`:
```
def update_shapes(self):
if self.scatterplot_item:
shape_data = self.get_shapes()
self.scatterplot_item.setSymbol(shape_data)
self.update_legends()
def get_shapes(self):
shape_data = self.master.get_shape_data()
shape_data = self.master.impute_shapes(
shape_data, len(self.CurveSymbols) - 1)
return self.CurveSymbols[shape_data]
```
On the widget's side, `get_something_data` is essentially just:
```
def get_size_data(self):
return self.get_column(self.attr_size)
```
where `get_column` retrieves a column while also filtering out the
points with missing x and y and so forth. (Here we present the simplest
two cases, "shapes" for the view and "sizes" for the model. The colors
for the view are more complicated since they deal with discrete and
continuous palettes, and the shapes for the view merge infrequent shapes.)
The plot can also show just a random sample of the data. The sample size is
set by `set_sample_size`, and the rest is taken care by the plot: the
widget keeps providing the data for all points, selection indices refer
to the entire set etc. Internally, sampling happens as early as possible
(in methods `get_<something>`).
"""
too_many_labels = Signal(bool)
begin_resizing = Signal()
step_resizing = Signal()
end_resizing = Signal()
label_only_selected = Setting(False)
point_width = Setting(10)
alpha_value = Setting(128)
show_grid = Setting(False)
show_legend = Setting(True)
class_density = Setting(False)
jitter_size = Setting(0)
resolution = 256
CurveSymbols = np.array("o x t + d star ?".split())
MinShapeSize = 6
DarkerValue = 120
UnknownColor = (168, 50, 168)
COLOR_NOT_SUBSET = (128, 128, 128, 0)
COLOR_SUBSET = (128, 128, 128, 255)
COLOR_DEFAULT = (128, 128, 128, 255)
MAX_VISIBLE_LABELS = 500
def __init__(self, scatter_widget, parent=None, view_box=ViewBox):
QObject.__init__(self)
gui.OWComponent.__init__(self, scatter_widget)
self.subset_is_shown = False
self.jittering_suspended = False
self.view_box = view_box(self)
_axis = {"left": AxisItem("left"), "bottom": AxisItem("bottom")}
self.plot_widget = pg.PlotWidget(viewBox=self.view_box, parent=parent,
background="w", axisItems=_axis)
self.plot_widget.hideAxis("left")
self.plot_widget.hideAxis("bottom")
self.plot_widget.getPlotItem().buttonsHidden = True
self.plot_widget.setAntialiasing(True)
self.plot_widget.sizeHint = lambda: QSize(500, 500)
self.density_img = None
self.scatterplot_item = None
self.scatterplot_item_sel = None
self.labels = []
self.master = scatter_widget
tooltip = self._create_drag_tooltip()
self.view_box.setDragTooltip(tooltip)
self.selection = None # np.ndarray
self.n_valid = 0
self.n_shown = 0
self.sample_size = None
self.sample_indices = None
self.palette = None
self.shape_legend = self._create_legend(((1, 0), (1, 0)))
self.color_legend = self._create_legend(((1, 1), (1, 1)))
self.update_legend_visibility()
self.scale = None # DiscretizedScale
self._too_many_labels = False
# self.setMouseTracking(True)
# self.grabGesture(QPinchGesture)
# self.grabGesture(QPanGesture)
self.update_grid_visibility()
self._tooltip_delegate = EventDelegate(self.help_event)
self.plot_widget.scene().installEventFilter(self._tooltip_delegate)
self.view_box.sigTransformChanged.connect(self.update_density)
self.view_box.sigRangeChangedManually.connect(self.update_labels)
self.timer = None
self.parameter_setter = ScatterBaseParameterSetter(self)
def _create_legend(self, anchor):
legend = LegendItem()
legend.setParentItem(self.plot_widget.getViewBox())
legend.restoreAnchor(anchor)
return legend
def _create_drag_tooltip(self):
tip_parts = [
(Qt.ControlModifier,
"{}: Append to group".
format("Cmd" if sys.platform == "darwin" else "Ctrl")),
(Qt.ShiftModifier, "Shift: Add group"),
(Qt.AltModifier, "Alt: Remove")
]
all_parts = "<center>" + \
", ".join(part for _, part in tip_parts) + \
"</center>"
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.ControlModifier])
text.setPos(4, 2)
r = text.boundingRect()
text.setTextWidth(r.width())
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()
tooltip_group = QGraphicsItemGroup()
tooltip_group.addToGroup(rect)
tooltip_group.addToGroup(text)
return tooltip_group
def update_tooltip(self, modifiers=Qt.NoModifier):
text = self.tiptexts[0]
for mod in [Qt.ControlModifier,
Qt.ShiftModifier,
Qt.AltModifier]:
if modifiers & mod:
text = self.tiptexts.get(int(mod))
break
self.tip_textitem.setHtml(text)
def suspend_jittering(self):
if self.jittering_suspended:
return
self.jittering_suspended = True
if self.jitter_size != 0:
self.update_jittering()
def unsuspend_jittering(self):
if not self.jittering_suspended:
return
self.jittering_suspended = False
if self.jitter_size != 0:
self.update_jittering()
def update_jittering(self):
x, y = self.get_coordinates()
if x is None or len(x) == 0 or self.scatterplot_item is None:
return
self.scatterplot_item.setCoordinates(x, y)
self.scatterplot_item_sel.setCoordinates(x, y)
self.update_labels()
# TODO: Rename to remove_plot_items
def clear(self):
"""
Remove all graphical elements from the plot
Calls the pyqtgraph's plot widget's clear, sets all handles to `None`,
removes labels and selections.
This method should generally not be called by the widget. If the data
is gone (*e.g.* upon receiving `None` as an input data signal), this
should be handler by calling `reset_graph`, which will in turn call
`clear`.
Derived classes should override this method if they add more graphical
elements. For instance, the regression line in the scatterplot adds
`self.reg_line_item = None` (the line in the plot is already removed
in this method).
"""
self.plot_widget.clear()
self.density_img = None
if self.timer is not None and self.timer.isActive():
self.timer.stop()
self.timer = None
self.scatterplot_item = None
self.scatterplot_item_sel = None
self.labels = []
self._signal_too_many_labels(False)
self.view_box.init_history()
self.view_box.tag_history()
# TODO: I hate `keep_something` and `reset_something` arguments
# __keep_selection is used exclusively be set_sample size which would
# otherwise just repeat the code from reset_graph except for resetting
# the selection. I'm uncomfortable with this; we may prefer to have a
# method _reset_graph which does everything except resetting the selection,
# and reset_graph would call it.
def reset_graph(self, __keep_selection=False):
"""
Reset the graph to new data (or no data)
The method must be called when the plot receives new data, in
particular when the number of points change. If only their properties
- like coordinates or shapes - change, an update method
(`update_coordinates`, `update_shapes`...) should be called instead.
The method must also be called when the data is gone.
The method calls `clear`, followed by calls of all update methods.
NB. Argument `__keep_selection` is for internal use only
"""
self.clear()
if not __keep_selection:
self.selection = None
self.sample_indices = None
self.update_coordinates()
self.update_point_props()
def set_sample_size(self, sample_size):
"""
Set the sample size
Args:
sample_size (int or None): sample size or `None` to show all points
"""
if self.sample_size != sample_size:
self.sample_size = sample_size
self.reset_graph(True)
def update_point_props(self):
"""
Update the sizes, colors, shapes and labels
The method calls the appropriate update methods for individual
properties.
"""
self.update_sizes()
self.update_colors()
self.update_selection_colors()
self.update_shapes()
self.update_labels()
# Coordinates
# TODO: It could be nice if this method was run on entire data, not just
# a sample. For this, however, it would need to either be called from
# `get_coordinates` before sampling (very ugly) or call
# `self.master.get_coordinates_data` (beyond ugly) or the widget would
# have to store the ranges of unsampled data (ugly).
# Maybe we leave it as it is.
def _reset_view(self, x_data, y_data):
"""
Set the range of the view box
Args:
x_data (np.ndarray): x coordinates
y_data (np.ndarray) y coordinates
"""
min_x, max_x = np.min(x_data), np.max(x_data)
min_y, max_y = np.min(y_data), np.max(y_data)
self.view_box.setRange(
QRectF(min_x, min_y, max_x - min_x or 1, max_y - min_y or 1),
padding=0.025)
def _filter_visible(self, data):
"""Return the sample from the data using the stored sample_indices"""
if data is None or self.sample_indices is None:
return data
else:
return np.asarray(data[self.sample_indices])
def get_coordinates(self):
"""
Prepare coordinates of the points in the plot
The method is called by `update_coordinates`. It gets the coordinates
from the widget, jitters them and return them.
The methods also initializes the sample indices if neededd and stores
the original and sampled number of points.
Returns:
(tuple): a pair of numpy arrays containing (sampled) coordinates,
or `(None, None)`.
"""
x, y = self.master.get_coordinates_data()
if x is None:
self.n_valid = self.n_shown = 0
return None, None
self.n_valid = len(x)
self._create_sample()
x = self._filter_visible(x)
y = self._filter_visible(y)
# Jittering after sampling is OK if widgets do not change the sample
# semi-permanently, e.g. take a sample for the duration of some
# animation. If the sample size changes dynamically (like by adding
# a "sample size" slider), points would move around when the sample
# size changes. To prevent this, jittering should be done before
# sampling (i.e. two lines earlier). This would slow it down somewhat.
x, y = self.jitter_coordinates(x, y)
return x, y
def _create_sample(self):
"""
Create a random sample if the data is larger than the set sample size
"""
self.n_shown = min(self.n_valid, self.sample_size or self.n_valid)
if self.sample_size is not None \
and self.sample_indices is None \
and self.n_valid != self.n_shown:
random = np.random.RandomState(seed=0)
self.sample_indices = random.choice(
self.n_valid, self.n_shown, replace=False)
# TODO: Is this really needed?
np.sort(self.sample_indices)
def jitter_coordinates(self, x, y):
"""
Display coordinates to random positions within ellipses with
radiuses of `self.jittter_size` percents of spans
"""
if self.jitter_size == 0 or self.jittering_suspended:
return x, y
return self._jitter_data(x, y)
def _jitter_data(self, x, y, span_x=None, span_y=None):
if span_x is None:
span_x = np.max(x) - np.min(x)
if span_y is None:
span_y = np.max(y) - np.min(y)
random = np.random.RandomState(seed=0)
rs = random.uniform(0, 1, len(x))
phis = random.uniform(0, 2 * np.pi, len(x))
magnitude = self.jitter_size / 100
return (x + magnitude * span_x * rs * np.cos(phis),
y + magnitude * span_y * rs * np.sin(phis))
def update_coordinates(self):
"""
Trigger the update of coordinates while keeping other features intact.
The method gets the coordinates by calling `self.get_coordinates`,
which in turn calls the widget's `get_coordinate_data`. The number of
coordinate pairs returned by the latter must match the current number
of points. If this is not the case, the widget should trigger
the complete update by calling `reset_graph` instead of this method.
"""
x, y = self.get_coordinates()
if x is None or len(x) == 0:
return
self._reset_view(x, y)
if self.scatterplot_item is None:
if self.sample_indices is None:
indices = np.arange(self.n_valid)
else:
indices = self.sample_indices
kwargs = dict(x=x, y=y, data=indices)
self.scatterplot_item = ScatterPlotItem(**kwargs)
self.scatterplot_item.sigClicked.connect(self.select_by_click)
self.scatterplot_item_sel = ScatterPlotItem(**kwargs)
self.plot_widget.addItem(self.scatterplot_item_sel)
self.plot_widget.addItem(self.scatterplot_item)
else:
self.scatterplot_item.setCoordinates(x, y)
self.scatterplot_item_sel.setCoordinates(x, y)
self.update_labels()
self.update_density() # Todo: doesn't work: try MDS with density on
# Sizes
def get_sizes(self):
"""
Prepare data for sizes of points in the plot
The method is called by `update_sizes`. It gets the sizes
from the widget and performs the necessary scaling and sizing.
The output is rounded to half a pixel for faster drawing.
Returns:
(np.ndarray): sizes
"""
size_column = self.master.get_size_data()
if size_column is None:
return np.full((self.n_shown,),
self.MinShapeSize + (5 + self.point_width) * 0.5)
size_column = self._filter_visible(size_column)
size_column = size_column.copy()
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=RuntimeWarning)
size_column -= np.nanmin(size_column)
mx = np.nanmax(size_column)
if mx > 0:
size_column /= mx
else:
size_column[:] = 0.5
sizes = self.MinShapeSize + (5 + self.point_width) * size_column
# round sizes to half pixel for smaller pyqtgraph's symbol pixmap atlas
sizes = (sizes * 2).round() / 2
return sizes
def update_sizes(self):
"""
Trigger an update of point sizes
The method calls `self.get_sizes`, which in turn calls the widget's
`get_size_data`. The result are properly scaled and then passed
back to widget for imputing (`master.impute_sizes`).
"""
if self.scatterplot_item:
size_data = self.get_sizes()
size_imputer = getattr(
self.master, "impute_sizes", self.default_impute_sizes)
size_imputer(size_data)
if self.timer is not None and self.timer.isActive():
self.timer.stop()
self.timer = None
current_size_data = self.scatterplot_item.data["size"].copy()
diff = size_data - current_size_data
widget = self
class Timeout:
# 0.5 - np.cos(np.arange(0.17, 1, 0.17) * np.pi) / 2
factors = [0.07, 0.26, 0.52, 0.77, 0.95, 1]
def __init__(self):
self._counter = 0
def __call__(self):
factor = self.factors[self._counter]
self._counter += 1
size = current_size_data + diff * factor
if len(self.factors) == self._counter:
widget.timer.stop()
widget.timer = None
size = size_data
widget.scatterplot_item.setSize(size)
widget.scatterplot_item_sel.setSize(size + SELECTION_WIDTH)
if widget.timer is None:
widget.end_resizing.emit()
else:
widget.step_resizing.emit()
if self.n_valid <= MAX_N_VALID_SIZE_ANIMATE and \
np.all(current_size_data > 0) and np.any(diff != 0):
# If encountered any strange behaviour when updating sizes,
# implement it with threads
self.begin_resizing.emit()
self.timer = QTimer(self.scatterplot_item, interval=50)
self.timer.timeout.connect(Timeout())
self.timer.start()
else:
self.begin_resizing.emit()
self.scatterplot_item.setSize(size_data)
self.scatterplot_item_sel.setSize(size_data + SELECTION_WIDTH)
self.end_resizing.emit()
update_point_size = update_sizes # backward compatibility (needed?!)
update_size = update_sizes
@classmethod
def default_impute_sizes(cls, size_data):
"""
Fallback imputation for sizes.
Set the size to two pixels smaller than the minimal size
Returns:
(bool): True if there was any missing data
"""
nans = np.isnan(size_data)
if np.any(nans):
size_data[nans] = cls.MinShapeSize - 2
return True
else:
return False
# Colors
def get_colors(self):
"""
Prepare data for colors of the points in the plot
The method is called by `update_colors`. It gets the colors and the
indices of the data subset from the widget (`get_color_data`,
`get_subset_mask`), and constructs lists of pens and brushes for
each data point.
The method uses different palettes for discrete and continuous data,
as determined by calling the widget's method `is_continuous_color`.
If also marks the points that are in the subset as defined by, for
instance the 'Data Subset' signal in the Scatter plot and similar
widgets. (Do not confuse this with *selected points*, which are
marked by circles around the points, which are colored by groups
and thus independent of this method.)