Skip to content

Commit cb236e3

Browse files
committed
Pythagorean Forest: Fix report
1 parent 216264a commit cb236e3

File tree

2 files changed

+87
-3
lines changed

2 files changed

+87
-3
lines changed

Orange/widgets/visualize/owpythagoreanforest.py

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
from typing import Any, Callable, Optional
44

55
from AnyQt.QtCore import Qt, QRectF, QSize, QPointF, QSizeF, QModelIndex, \
6-
QItemSelection, QItemSelectionModel, QT_VERSION
6+
QItemSelection, QItemSelectionModel, QT_VERSION, QByteArray, QBuffer, \
7+
QIODevice
78
from AnyQt.QtGui import QPainter, QPen, QColor, QBrush, QMouseEvent
89
from AnyQt.QtWidgets import QSizePolicy, QGraphicsScene, QLabel, QSlider, \
910
QListView, QStyledItemDelegate, QStyleOptionViewItem, QStyle
1011

12+
from orangewidget.io import PngFormat
1113
from Orange.base import RandomForestModel, TreeModel
1214
from Orange.data import Table
1315
from Orange.widgets import gui, settings
@@ -23,6 +25,53 @@
2325
from Orange.widgets.widget import OWWidget
2426

2527

28+
REPORT_STYLE = """
29+
<style>
30+
* {
31+
box-sizing: border-box;
32+
}
33+
34+
.forest_model_row {
35+
display: flex;
36+
flex-wrap: wrap;
37+
padding: 0 4px;
38+
}
39+
40+
.forest_model_col {
41+
flex: 10%;
42+
max-width: 10%;
43+
padding: 0 4px;
44+
}
45+
46+
.forest_model_col img {
47+
margin-top: 8px;
48+
vertical-align: middle;
49+
}
50+
51+
@media screen and (max-width: 2200px) {
52+
.forest_model_col {
53+
flex: 25%;
54+
max-width: 25%;
55+
}
56+
}
57+
58+
@media screen and (max-width: 1200px) {
59+
.forest_model_col {
60+
flex: 50%;
61+
max-width: 50%;
62+
}
63+
}
64+
65+
@media screen and (max-width: 600px) {
66+
.forest_model_col {
67+
flex: 100%;
68+
max-width: 100%;
69+
}
70+
}
71+
</style>
72+
"""
73+
74+
2675
class PythagoreanForestModel(PyListModel):
2776
def __init__(self, *args, **kwargs):
2877
super().__init__(*args, **kwargs)
@@ -377,7 +426,30 @@ def commit(self, selection: QItemSelection) -> None:
377426

378427
def send_report(self):
379428
"""Send report."""
380-
self.report_plot()
429+
model = self.forest_model
430+
max_rows = 30
431+
432+
def item_html(row):
433+
img_data = model.data(model.index(row))
434+
byte_array = QByteArray()
435+
filename = QBuffer(byte_array)
436+
filename.open(QIODevice.WriteOnly)
437+
PngFormat.write(filename, img_data)
438+
img_encoded = byte_array.toBase64().data().decode("utf-8")
439+
return f'<img style="width:100%" ' \
440+
f'src="data:image/png;base64,{img_encoded}"/>'
441+
442+
html = ["<div class='forest_model_row'>"]
443+
for i in range(model.rowCount())[:max_rows]:
444+
html.append("<div class='forest_model_col'>")
445+
html.extend(item_html(i))
446+
html.append("</div>")
447+
html.append("</div>")
448+
449+
html = REPORT_STYLE + "".join(html)
450+
if model.rowCount() > max_rows:
451+
html += "<p>. . .</p>"
452+
self.report_raw(html)
381453

382454

383455
class SklRandomForestAdapter:

Orange/widgets/visualize/tests/test_owpythagoreanforest.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# pylint: disable=missing-docstring,protected-access
2-
2+
import unittest
33
from unittest.mock import Mock
44

55
from AnyQt.QtCore import Qt, QItemSelection, QItemSelectionModel
@@ -238,3 +238,15 @@ def test_context(self):
238238

239239
self.send_signal(self.widget.Inputs.random_forest, iris_tree)
240240
self.assertEqual(2, self.widget.target_class_index)
241+
242+
def test_report(self):
243+
self.widget.send_report()
244+
245+
self.widget.report_raw = Mock()
246+
self.send_signal(self.widget.Inputs.random_forest, self.titanic)
247+
self.widget.send_report()
248+
self.widget.report_raw.assert_called_once()
249+
250+
251+
if __name__ == "__main__":
252+
unittest.main()

0 commit comments

Comments
 (0)