Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] OWScatterOWScatterPlotGraph: Match group colors with marker colors #3053

Merged
merged 1 commit into from
Jun 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions Orange/widgets/utils/annotated_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,16 @@ def create_groups_table(data, selection,
var_name=ANNOTATED_DATA_FEATURE_NAME):
if data is None:
return None
values = ["G{}".format(i + 1) for i in range(np.max(selection))]
max_sel = np.max(selection)
values = ["G{}".format(i + 1) for i in range(max_sel)]
if include_unselected:
values.insert(0, "Unselected")
# Place Unselected instances in the "last group", so that the group
# colors and scatter diagram marker colors will match
values.append("Unselected")
mask = (selection != 0)
selection = selection.copy()
selection[mask] = selection[mask] - 1
selection[~mask] = selection[~mask] = max_sel
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just subtract 1 from everything and then replace -1 with something else?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because in some cases array's dtype is "uint8". In that case subtracting 1 from 0 results in 255.
I was thinking about changing dtype, but I think the above solution is more robust.

else:
mask = np.flatnonzero(selection)
data = data[mask]
Expand Down
15 changes: 14 additions & 1 deletion Orange/widgets/utils/tests/test_annotated_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import numpy as np

from Orange.data import Table, Variable
from Orange.data.filter import SameValue
from Orange.widgets.utils.annotated_data import (
create_annotated_table, get_next_name, get_unique_names,
ANNOTATED_DATA_FEATURE_NAME)
create_groups_table, ANNOTATED_DATA_FEATURE_NAME
)


class TestGetNextName(unittest.TestCase):
Expand Down Expand Up @@ -115,3 +117,14 @@ def test_get_unique_names(self):
"bravo (3)"]
self.assertEqual(get_unique_names(names, ["bravo", "charlie"]),
["bravo (5)", "charlie (5)"])

def test_create_groups_table_include_unselected(self):
group_indices = random.sample(range(0, len(self.zoo)), 20)
selection = np.zeros(len(self.zoo), dtype=np.uint8)
selection[group_indices[:10]] = 1
selection[group_indices[10:]] = 2
table = create_groups_table(self.zoo, selection)
self.assertEqual(
len(SameValue(table.domain["Selected"], "Unselected")(table)),
len(self.zoo) - len(group_indices)
)
5 changes: 1 addition & 4 deletions Orange/widgets/visualize/owscatterplotgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,12 +851,9 @@ def compute_colors_sel(self, keep_colors=False):
_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.)
[_make_pen(palette[i], SELECTION_WIDTH + 1.)
for i in range(sels)]
pen = [pens[a] for a in self.selection[self.valid_data]]
else:
Expand Down
8 changes: 6 additions & 2 deletions Orange/widgets/visualize/tests/test_owscatterplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ def annotations():
graph.select(points[5:10])
np.testing.assert_equal(selectedx(), x[:10])
np.testing.assert_equal(selected_groups(), np.array([0] * 5 + [1] * 5))
sel_column[5:10] = 2
sel_column[:5] = 0
sel_column[5:10] = 1
sel_column[10:] = 2
np.testing.assert_equal(annotated(), sel_column)
self.assertEqual(len(annotations()), 3)

Expand Down Expand Up @@ -232,7 +234,9 @@ def annotations():
# ... then Ctrl-Shift-select (add-to-last) 10:17; we have 17:25, 30:40
with self.modifiers(Qt.ShiftModifier | Qt.ControlModifier):
graph.select(points[35:40])
sel_column[30:40] = 2
sel_column[:] = 2
sel_column[17:25] = 0
sel_column[30:40] = 1
np.testing.assert_equal(selected_groups(), np.array([0] * 8 + [1] * 10))
np.testing.assert_equal(annotated(), sel_column)
self.assertEqual(len(annotations()), 3)
Expand Down