-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhistogramchartwidget.py
215 lines (181 loc) · 7.27 KB
/
histogramchartwidget.py
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
# Copyright(C) 2024 Advanced Micro Devices, Inc. All rights reserved.
from collections import OrderedDict
# pylint: disable=no-name-in-module
from PySide6.QtWidgets import (
QWidget,
QVBoxLayout,
QApplication,
QGraphicsSceneHoverEvent,
QMenu,
)
from PySide6.QtCore import Qt
from PySide6.QtGui import QPixmap, QColor, QFont, QBrush
import pyqtgraph as pg
class PlotWidgetCustom(pg.PlotWidget):
def wheelEvent(self, event):
if event.modifiers() == Qt.KeyboardModifier.ControlModifier:
super().wheelEvent(event) # Zoom with Control key
else:
QApplication.sendEvent(self.parent(), event) # Scroll otherwise
# pylint: disable=abstract-method
class InteractiveBarItem(pg.BarGraphItem):
def __init__(
self,
percentage: float,
x0: float,
y0: float,
width: float,
height: float,
brush: QBrush,
*args,
orientation: Qt.Orientation = Qt.Orientation.Horizontal,
**kwargs,
):
super().__init__(
x0=x0, y0=y0, width=width, height=height, brush=brush, *args, **kwargs
)
self.setAcceptHoverEvents(True)
self.default_color = brush.color()
self.hover_color = QColor(30, 160, 220)
self.orientation = orientation
angle = 0 if orientation == Qt.Orientation.Vertical else -90
if percentage < 1.5:
self.label = pg.TextItem(
text="", anchor=(0.5, 0.5), color=(0, 0, 0), angle=angle
)
else:
self.label = pg.TextItem(
text=f"{percentage}%", anchor=(0.5, 0.5), color=(0, 0, 0), angle=angle
)
self.label_font = QFont("Arial", 8, QFont.Weight.Bold)
self.label.setFont(self.label_font)
self.label.setParentItem(self)
self.label.setVisible(False)
y_center = y0 + height / 2
x_center = x0 + width / 2
self.label.setPos(x_center, y_center)
def hoverEnterEvent(self, event: QGraphicsSceneHoverEvent):
super().hoverEnterEvent(event)
self.setOpts(brush=self.hover_color)
self.label.setVisible(True)
def hoverLeaveEvent(self, event: QGraphicsSceneHoverEvent):
super().hoverLeaveEvent(event)
self.setOpts(brush=self.default_color)
self.label.setVisible(False)
class HistogramChartWidget(QWidget):
def __init__(self, *args, **kwargs):
"""
bar_spacing refers to the offset spacing between bars. A larger
spacing typically results in larger bars which has better
aesthetics for histograms with fewer items.
"""
super(HistogramChartWidget, self).__init__(*args, **kwargs)
self.plot_widget = PlotWidgetCustom()
layout = QVBoxLayout()
layout.addWidget(self.plot_widget)
self.setLayout(layout)
view_box = self.plot_widget.getViewBox()
view_box.setMouseEnabled(x=False, y=False)
view_box.setMenuEnabled(False)
self.bar_spacing = 25
def copy_chart_to_clipboard(self):
if self.plot_widget:
pixmap = QPixmap(self.plot_widget.grab())
QApplication.clipboard().setPixmap(pixmap)
def contextMenuEvent(self, event):
menu = QMenu()
copy_action = menu.addAction("Copy Image")
action = menu.exec(self.mapToGlobal(event.pos()))
if action == copy_action:
self.copy_chart_to_clipboard()
def set_data(self, data: OrderedDict, title="Op Histogram"):
self.plot_widget.setTitle(
f"<span style='color:rgb(200,200,200); font-size: 14pt'>{title}</span>"
)
op_count = list(data.values())[::-1]
op_names = list(data.keys())[::-1]
y_positions = list(range(len(op_count)))
total_count = sum(op_count)
height = 0.6
self.plot_widget.setFixedHeight(len(op_names) * self.bar_spacing)
for count, y_pos in zip(op_count, y_positions):
bar = InteractiveBarItem(
percentage=round(100.0 * count / total_count),
x0=0,
y0=y_pos - height / 2,
width=count,
height=height,
brush=pg.mkBrush(color=(80, 80, 80)),
orientation=Qt.Orientation.Vertical,
)
self.plot_widget.addItem(bar)
axis = self.plot_widget.getAxis("left")
ticks = [[(i, f"{op_names[i]} ({op_count[i]})") for i in y_positions]]
axis.setTicks(ticks)
class StackedHistogramWidget(QWidget):
def __init__(self, *args, **kwargs):
super(StackedHistogramWidget, self).__init__(*args, **kwargs)
self.plot_widget = pg.PlotWidget()
self.plot_widget.setMaximumHeight(200)
plot_item = self.plot_widget.getPlotItem()
if plot_item:
plot_item.setContentsMargins(0, 0, 0, 0)
layout = QVBoxLayout()
layout.addWidget(self.plot_widget)
self.setLayout(layout)
view_box = self.plot_widget.getViewBox()
view_box.setMouseEnabled(x=False, y=False)
view_box.setMenuEnabled(False)
self.bar_spacing = 25
def set_data(self, data: OrderedDict, model_name, y_max, title="", set_ticks=False):
title_color = "rgb(0,0,0)" if set_ticks else "rgb(200,200,200)"
self.plot_widget.setLabel(
"left",
f"<span style='color:{title_color}; font-size: 10pt'>{model_name}</span>",
)
if title:
self.plot_widget.setTitle(
f"<span style='color:{title_color}; font-size: 14pt'>{title}</span>"
)
self.plot_widget.setYRange(0, y_max)
op_count = list(data.values())[::-1]
op_names = list(data.keys())[::-1]
x_positions = list(range(len(op_count)))
total_count = sum(op_count)
width = 0.6
self.plot_widget.setFixedWidth(500)
for count, x_pos, tick in zip(op_count, x_positions, op_names):
x0 = x_pos - width / 2
y0 = 0
if set_ticks:
tick_label = pg.TextItem(
text=tick,
anchor=(0, 0.5),
color=(255, 255, 255),
angle=-90,
)
tick_font = QFont("Arial", 8, QFont.Weight.Bold)
tick_label.setFont(tick_font)
tick_label.setVisible(True)
y_center = y_max
x_center = x0 + width / 2
tick_label.setPos(x_center, y_center)
self.plot_widget.addItem(tick_label)
# Hide elements without losing their location
self.plot_widget.getAxis("bottom").setPen(color="k")
self.plot_widget.getAxis("left").setPen(color="k")
axis = self.plot_widget.getAxis("left")
ticks = [[(i, "_" * (1 + len(str(y_max)))) for i in x_positions]]
axis.setTicks(ticks)
bar = InteractiveBarItem(
percentage=round(100.0 * count / total_count),
x0=x0,
y0=y0,
width=width,
height=count,
brush=pg.mkBrush(color=(80, 80, 80)),
)
self.plot_widget.addItem(bar)
axis = self.plot_widget.getAxis("bottom")
ticks = [[(i, "") for i in x_positions]]
axis.setTicks(ticks)