from xml.sax.saxutils import escape
import numpy as np
from AnyQt.QtCore import QSize, Signal, Qt
from AnyQt.QtWidgets import QApplication
from orangewidget.utils.visual_settings_dlg import VisualSettingsDialog
from Orange.data import (
Table, ContinuousVariable, Domain, Variable, StringVariable
)
from Orange.data.util import get_unique_names, array_equal
from Orange.data.sql.table import SqlTable
from Orange.statistics.util import bincount
from Orange.widgets import gui, report
from Orange.widgets.settings import (
Setting, ContextSetting, DomainContextHandler, SettingProvider
)
from Orange.widgets.utils import colorpalettes
from Orange.widgets.utils.annotated_data import (
create_annotated_table, ANNOTATED_DATA_SIGNAL_NAME, create_groups_table
)
from Orange.widgets.utils.plot import OWPlotGUI
from Orange.widgets.utils.sql import check_sql_input
from Orange.widgets.visualize.owscatterplotgraph import (
OWScatterPlotBase, MAX_COLORS
)
from Orange.widgets.visualize.utils.component import OWGraphWithAnchors
from Orange.widgets.widget import OWWidget, Input, Output, Msg
# maximum number of shapes (including Other)
MAX_SHAPES = len(OWScatterPlotBase.CurveSymbols) - 1
MAX_POINTS_IN_TOOLTIP = 5
class OWProjectionWidgetBase(OWWidget, openclass=True):
"""
Base widget for widgets that use attribute data to set the colors, labels,
shapes and sizes of points.
The widgets defines settings `attr_color`, `attr_label`, `attr_shape`
and `attr_size`, but leaves defining the gui to the derived widgets.
These are expected to have controls that manipulate these settings,
and the controls are expected to use attribute models.
The widgets also defines attributes `data` and `valid_data` and expects
the derived widgets to use them to store an instances of `data.Table`
and a bool `np.ndarray` with indicators of valid (that is, shown)
data points.
"""
attr_color = ContextSetting(None, required=ContextSetting.OPTIONAL)
attr_label = ContextSetting(None, required=ContextSetting.OPTIONAL)
attr_shape = ContextSetting(None, required=ContextSetting.OPTIONAL)
attr_size = ContextSetting(None, required=ContextSetting.OPTIONAL)
class Information(OWWidget.Information):
missing_size = Msg(
"Points with undefined '{}' are shown in smaller size")
missing_shape = Msg(
"Points with undefined '{}' are shown as crossed circles")
def __init__(self):
super().__init__()
self.data = None
self.valid_data = None
def init_attr_values(self):
"""
Set the models for `attr_color`, `attr_shape`, `attr_size` and
`attr_label`. All values are set to `None`, except `attr_color`
which is set to the class variable if it exists.
"""
data = self.data
domain = data.domain if data and len(data) else None
for attr in ("attr_color", "attr_shape", "attr_size", "attr_label"):
getattr(self.controls, attr).model().set_domain(domain)
setattr(self, attr, None)
if domain is not None:
self.attr_color = domain.class_var
def get_coordinates_data(self):
"""A get coordinated method that returns no coordinates.
Derived classes must override this method.
"""
return None, None
def get_subset_mask(self):
"""
Return the bool array indicating the points in the subset
The base method does nothing and would usually be overridden by
a method that returns indicators from the subset signal.
Do not confuse the subset with selection.
Returns:
(np.ndarray or `None`): a bool array of indicators
"""
return None
def get_column(self, attr, filter_valid=True,
max_categories=None, return_labels=False):
"""
Retrieve the data from the given column in the data table
The method:
- densifies sparse data,
- converts arrays with dtype object to floats if the attribute is
actually primitive,
- filters out invalid data (if `filter_valid` is `True`),
- merges infrequent (discrete) values into a single value
(if `max_categories` is set).
Tha latter feature is used for shapes and labels, where only a
specified number of different values is shown, and others are
merged into category 'Other'. In this case, the method may return
either the data (e.g. color indices, shape indices) or the list
of retained values, followed by `['Other']`.
Args:
attr (:obj:~Orange.data.Variable): the column to extract
filter_valid (bool): filter out invalid data (default: `True`)
max_categories (int): merge infrequent values (default: `None`);
ignored for non-discrete attributes
return_labels (bool): return a list of labels instead of data
(default: `False`)
Returns:
(np.ndarray): (valid) data from the column, or a list of labels
"""
if attr is None:
return None
needs_merging = attr.is_discrete \
and max_categories is not None \
and len(attr.values) >= max_categories
if return_labels and not needs_merging:
assert attr.is_discrete
return attr.values
all_data = self.data.get_column_view(attr)[0]
if all_data.dtype == object and attr.is_primitive():
all_data = all_data.astype(float)
if filter_valid and self.valid_data is not None:
all_data = all_data[self.valid_data]
if not needs_merging:
return all_data
dist = bincount(all_data, max_val=len(attr.values) - 1)[0]
infrequent = np.zeros(len(attr.values), dtype=bool)
infrequent[np.argsort(dist)[:-(max_categories-1)]] = True
if return_labels:
return [value for value, infreq in zip(attr.values, infrequent)
if not infreq] + ["Other"]
else:
result = all_data.copy()
freq_vals = [i for i, f in enumerate(infrequent) if not f]
for i, infreq in enumerate(infrequent):
if infreq:
result[all_data == i] = max_categories - 1
else:
result[all_data == i] = freq_vals.index(i)
return result
# Sizes
def get_size_data(self):
"""Return the column corresponding to `attr_size`"""
return self.get_column(self.attr_size)
def impute_sizes(self, size_data):
"""
Default imputation for size data
Let the graph handle it, but add a warning if needed.
Args:
size_data (np.ndarray): scaled points sizes
"""
if self.graph.default_impute_sizes(size_data):
self.Information.missing_size(self.attr_size)
else:
self.Information.missing_size.clear()
def sizes_changed(self):
self.graph.update_sizes()
# Colors
def get_color_data(self):
"""Return the column corresponding to color data"""
return self.get_column(self.attr_color, max_categories=MAX_COLORS)
def get_color_labels(self):
"""
Return labels for the color legend
Returns:
(list of str): labels
"""
if self.attr_color is None:
return None
if not self.attr_color.is_discrete:
return self.attr_color.str_val
return self.get_column(self.attr_color, max_categories=MAX_COLORS,
return_labels=True)
def is_continuous_color(self):
"""
Tells whether the color is continuous
Returns:
(bool):
"""
return self.attr_color is not None and self.attr_color.is_continuous
def get_palette(self):
"""
Return a palette suitable for the current `attr_color`
This method must be overridden if the widget offers coloring that is
not based on attribute values.
"""
attr = self.attr_color
if not attr:
return None
palette = attr.palette
if attr.is_discrete and len(attr.values) >= MAX_COLORS:
values = self.get_color_labels()
colors = [palette.palette[attr.to_val(value)]
for value in values[:-1]] + [[192, 192, 192]]
palette = colorpalettes.DiscretePalette.from_colors(colors)
return palette
def can_draw_density(self):
"""
Tells whether the current data and settings are suitable for drawing
densities
Returns:
(bool):
"""
return self.data is not None and self.data.domain is not None and \
len(self.data) > 1 and self.attr_color is not None
def colors_changed(self):
self.graph.update_colors()
self._update_opacity_warning()
self.cb_class_density.setEnabled(self.can_draw_density())
# Labels
def get_label_data(self, formatter=None):
"""Return the column corresponding to label data"""
if self.attr_label:
label_data = self.get_column(self.attr_label)
if formatter is None:
formatter = self.attr_label.str_val
return np.array([formatter(x) for x in label_data])
return None
def labels_changed(self):
self.graph.update_labels()
# Shapes
def get_shape_data(self):
"""
Return labels for the shape legend
Returns:
(list of str): labels
"""
return self.get_column(self.attr_shape, max_categories=MAX_SHAPES)
def get_shape_labels(self):
return self.get_column(self.attr_shape, max_categories=MAX_SHAPES,
return_labels=True)
def impute_shapes(self, shape_data, default_symbol):
"""
Default imputation for shape data
Let the graph handle it, but add a warning if needed.
Args:
shape_data (np.ndarray): scaled points sizes
default_symbol (str): a string representing the symbol
"""
if self.graph.default_impute_shapes(shape_data, default_symbol):
self.Information.missing_shape(self.attr_shape)
else:
self.Information.missing_shape.clear()
def shapes_changed(self):
self.graph.update_shapes()
# Tooltip
def _point_tooltip(self, point_id, skip_attrs=()):
def show_part(_point_data, singular, plural, max_shown, _vars):
cols = [escape('{} = {}'.format(var.name, _point_data[var]))
for var in _vars[:max_shown + 2]
if _vars == domain.class_vars
or var not in skip_attrs][:max_shown]
if not cols:
return ""
n_vars = len(_vars)
if n_vars > max_shown:
cols[-1] = "... and {} others".format(n_vars - max_shown + 1)
return \
"{}:
".format(singular if n_vars < 2 else plural) \
+ "
".join(cols)
domain = self.data.domain
parts = (("Class", "Classes", 4, domain.class_vars),
("Meta", "Metas", 4, domain.metas),
("Feature", "Features", 10, domain.attributes))
point_data = self.data[point_id]
return "
".join(show_part(point_data, *columns)
for columns in parts)
def get_tooltip(self, point_ids):
"""
Return the tooltip string for the given points
The method is called by the plot on mouse hover
Args:
point_ids (list): indices into `data`
Returns:
(str):
"""
point_ids = \
np.flatnonzero(self.valid_data)[np.asarray(point_ids, dtype=int)]
text = "