Skip to content

Commit 6a6d699

Browse files
committed
ScatterplotGraph.update_density: Ignore points with mising value for color
1 parent 711a79b commit 6a6d699

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

Orange/widgets/visualize/owscatterplotgraph.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,16 +1101,22 @@ def update_density(self):
11011101
self.plot_widget.removeItem(self.density_img)
11021102
self.density_img = None
11031103
if self.class_density and self.scatterplot_item is not None:
1104+
c_data = self.master.get_color_data()
1105+
if c_data is None:
1106+
return
1107+
mask = np.isfinite(self._filter_visible(c_data))
1108+
pens = self.scatterplot_item.data['pen']
11041109
rgb_data = [
11051110
pen.color().getRgb()[:3] if pen is not None else (255, 255, 255)
1106-
for pen in self.scatterplot_item.data['pen']]
1111+
for known, pen in zip(mask, pens)
1112+
if known]
11071113
if len(set(rgb_data)) <= 1:
11081114
return
11091115
[min_x, max_x], [min_y, max_y] = self.view_box.viewRange()
11101116
x_data, y_data = self.scatterplot_item.getData()
11111117
self.density_img = classdensity.class_density_image(
11121118
min_x, max_x, min_y, max_y, self.resolution,
1113-
x_data, y_data, rgb_data)
1119+
x_data[mask], y_data[mask], rgb_data)
11141120
self.plot_widget.addItem(self.density_img)
11151121

11161122
def update_selection_colors(self):

Orange/widgets/visualize/tests/test_owscatterplotbase.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,41 @@ def test_density(self):
740740
self.assertIsNone(graph.density_img)
741741
self.assertIs(graph.plot_widget.removeItem.call_args[0][0], density)
742742

743+
@patch("Orange.widgets.utils.classdensity.class_density_image")
744+
def test_density_with_missing(self, class_density_image):
745+
graph = self.graph
746+
graph.reset_graph()
747+
graph.plot_widget.addItem = Mock()
748+
graph.plot_widget.removeItem = Mock()
749+
750+
graph.class_density = True
751+
d = np.arange(10, dtype=float) % 2
752+
self.master.get_color_data = lambda: d
753+
754+
# All colors known
755+
graph.update_colors()
756+
x_data0, y_data0, colors0 = class_density_image.call_args[0][5:]
757+
758+
# Some missing colors
759+
d[:3] = np.nan
760+
graph.update_colors()
761+
x_data, y_data, colors = class_density_image.call_args[0][5:]
762+
np.testing.assert_equal(x_data, x_data0[3:])
763+
np.testing.assert_equal(y_data, y_data0[3:])
764+
np.testing.assert_equal(colors, colors0[3:])
765+
766+
# Missing colors + only subsample plotted
767+
graph.set_sample_size(8)
768+
graph.reset_graph()
769+
d_known = np.isfinite(graph._filter_visible(d))
770+
x_data0 = graph._filter_visible(x_data0)[d_known]
771+
y_data0 = graph._filter_visible(y_data0)[d_known]
772+
colors0 = graph._filter_visible(np.array(colors0))[d_known]
773+
x_data, y_data, colors = class_density_image.call_args[0][5:]
774+
np.testing.assert_equal(x_data, x_data0)
775+
np.testing.assert_equal(y_data, y_data0)
776+
np.testing.assert_equal(colors, colors0)
777+
743778
def test_labels(self):
744779
graph = self.graph
745780
graph.reset_graph()

0 commit comments

Comments
 (0)