def get_colors_sel(self): """ Return pens and brushes for selection markers. A pen can is set to `Qt.NoPen` if a point is not selected. All brushes are completely transparent whites. Returns: (tuple): a list of pens and a list of brushes """ nopen = QPen(Qt.NoPen) if self.selection is None: pen = [nopen] * self.n_shown else: sels = np.max(self.selection) if sels == 1: pen = np.where( self._filter_visible(self.selection), _make_pen(QColor(255, 190, 0, 255), SELECTION_WIDTH + 1), nopen) else: palette = ColorPaletteGenerator(number_of_colors=sels + 1) pen = np.choose(self._filter_visible(self.selection), [nopen] + [ _make_pen(palette[i], SELECTION_WIDTH + 1) for i in range(sels) ]) return pen, [QBrush(QColor(255, 255, 255, 0))] * self.n_shown
def compute_colors_sel(self, keep_colors=False): if not keep_colors: self.pen_colors_sel = self.brush_colors_sel = None nopen = QPen(Qt.NoPen) if self.selection is not None: sels = np.max(self.selection) if sels == 1: pens = [ nopen, _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.) for i in range(sels)] pen = [pens[a] for a in self.selection[self.valid_data]] else: pen = [nopen] * self.n_points brush = [QBrush(QColor(255, 255, 255, 0))] * self.n_points return pen, brush
def set_marker_color(self, attr, update=True): try: self._color_attr = variable = self.data.domain[attr] if len(self.data) == 0: raise Exception except Exception: self._color_attr = None self._legend_colors = [] else: if variable.is_continuous: self._raw_color_values = values = self.data.get_column_view(variable)[0].astype(float) self._scaled_color_values = scale(values) self._colorgen = ContinuousPaletteGenerator(*variable.colors) min = np.nanmin(values) self._legend_colors = (['c', self._legend_values(variable, [min, np.nanmax(values)]), [color_to_hex(i) for i in variable.colors if i]] if not np.isnan(min) else []) elif variable.is_discrete: _values = np.asarray(self.data.domain[attr].values) __values = self.data.get_column_view(variable)[0].astype(np.uint16) self._raw_color_values = _values[__values] # The joke's on you self._scaled_color_values = __values self._colorgen = ColorPaletteGenerator(len(variable.colors), variable.colors) self._legend_colors = ['d', self._legend_values(variable, range(len(_values))), list(_values), [color_to_hex(self._colorgen.getRGB(i)) for i in range(len(_values))]] finally: if update: self.redraw_markers_overlay_image(new_image=True)
def redraw_selection(self, marks=None): if self.grid_cells is None: return sel_pen = QPen(QBrush(QColor(128, 128, 128)), 2) sel_pen.setCosmetic(True) mark_pen = QPen(QBrush(QColor(128, 128, 128)), 4) mark_pen.setCosmetic(True) pens = [self._grid_pen, sel_pen] mark_brush = QBrush(QColor(224, 255, 255)) sels = self.selection is not None and np.max(self.selection) palette = ColorPaletteGenerator(number_of_colors=sels + 1) brushes = [QBrush(Qt.NoBrush)] + \ [QBrush(palette[i].lighter(165)) for i in range(sels)] for y in range(self.size_y): for x in range(self.size_x - (y % 2) * self.hexagonal): cell = self.grid_cells[y, x] marked = marks is not None and marks[x, y] sel_group = self.selection is not None and self.selection[x, y] if marked: cell.setBrush(mark_brush) cell.setPen(mark_pen) else: cell.setBrush(brushes[sel_group]) cell.setPen(pens[bool(sel_group)]) cell.setZValue(marked or sel_group)
def get_palette(self): if not self.color_by_cluster or not self.clusters.table: return super().get_palette() colors = self.clusters.table.domain["Clusters"].colors return ColorPaletteGenerator(number_of_colors=len(colors), rgb_colors=colors)
def recompute_heatmap(self, points): if self.model is None or self.data is None: self.exposeObject('model_predictions', {}) self.evalJS('draw_heatmap()') return latlons = np.array(points) table = Table(Domain([self.lat_attr, self.lon_attr]), latlons) try: predictions = self.model(table) except Exception as e: self._owwidget.Error.model_error(e) return else: self._owwidget.Error.model_error.clear() class_var = self.model.domain.class_var is_regression = class_var.is_continuous if is_regression: predictions = scale(np.round(predictions, 7)) # Avoid small errors kwargs = dict( extrema=self._legend_values(class_var, [np.nanmin(predictions), np.nanmax(predictions)])) else: colorgen = ColorPaletteGenerator(len(class_var.values), class_var.colors) predictions = colorgen.getRGB(predictions) kwargs = dict( legend_labels=self._legend_values(class_var, range(len(class_var.values))), full_labels=list(class_var.values), colors=[color_to_hex(colorgen.getRGB(i)) for i in range(len(class_var.values))]) self.exposeObject('model_predictions', dict(data=predictions, **kwargs)) self.evalJS('draw_heatmap()')
def set_node_colors(self): if not self.graph: return self.lastColorColumn = self.colorCombo.currentText() attribute = self.colorCombo.itemData(self.colorCombo.currentIndex()) assert not attribute or isinstance(attribute, Orange.data.Variable) if not attribute: for node in self.view.nodes: node.setColor(None) return table = self.graph.items() if not table: return if attribute in table.domain.class_vars: values = table[:, attribute].Y if values.ndim > 1: values = values.T elif attribute in table.domain.metas: values = table[:, attribute].metas[:, 0] elif attribute in table.domain.attributes: values = table[:, attribute].X[:, 0] else: raise RuntimeError("Shouldn't be able to select this column") if attribute.is_continuous: colors = CONTINUOUS_PALETTE[scale(values)] elif attribute.is_discrete: DISCRETE_PALETTE = ColorPaletteGenerator(len(attribute.values)) colors = DISCRETE_PALETTE[values] for node, color in zip(self.view.nodes, colors): node.setColor(color)
def get_palette(self): if not self.color_by_cluster or not self.clusters.table: return super().get_palette() colors = self.clusters.table.domain["Clusters"].colors # the second option is to keep widget backward compatible (Orange < 3.25) return (self.clusters.table.domain["Clusters"].palette if hasattr( self.clusters.table.domain["Clusters"], "palette") else ColorPaletteGenerator(number_of_colors=len(colors), rgb_colors=colors))
def get_color(self): if self.attr_color is None: return None colors = self.attr_color.colors if self.attr_color.is_discrete: self.discrete_palette = ColorPaletteGenerator( number_of_colors=len(colors), rgb_colors=colors) else: self.continuous_palette = ContinuousPaletteGenerator(*colors) return self.attr_color
def get_color(self): if self.attr_color is None: return None colors = self.attr_color.colors if self.attr_color.is_discrete: self.discrete_palette = ColorPaletteGenerator( number_of_colors=min(len(colors), MAX), rgb_colors=colors if len(colors) <= MAX else DefaultRGBColors, ) else: self.continuous_palette = ContinuousPaletteGenerator(*colors) return self.attr_color
def compute_colors(self): no_brush = DEFAULT_SELECTION_BRUSH sels = np.max(self.selection) if sels == 1: brushes = [no_brush, no_brush] else: palette = ColorPaletteGenerator(number_of_colors=sels + 1) brushes = [no_brush] + [QBrush(palette[i]) for i in range(sels)] brush = [brushes[a] for a in self.selection] pen = [DEFAULT_SELECTION_PEN] * len(self.items) return pen, brush
def get_color_index(self): color_index = -1 attr_color = self.attr_color if attr_color != "" and attr_color != "(Same color)": color_index = self.attribute_name_index[attr_color] color_var = self.data_domain[attr_color] colors = color_var.colors if color_var.is_discrete: self.discrete_palette = ColorPaletteGenerator( number_of_colors=len(colors), rgb_colors=colors) else: self.continuous_palette = ContinuousPaletteGenerator(*colors) return color_index
def __init__(self, scatter_widget, parent=None, _="None"): gui.OWComponent.__init__(self, scatter_widget) self.view_box = InteractiveViewBox(self) self.plot_widget = pg.PlotWidget(viewBox=self.view_box, parent=parent, background="w") self.plot_widget.getPlotItem().buttonsHidden = True self.plot_widget.setAntialiasing(True) self.plot_widget.sizeHint = lambda: QtCore.QSize(500, 500) self.replot = self.plot_widget.replot ScaleScatterPlotData.__init__(self) self.density_img = None self.scatterplot_item = None self.scatterplot_item_sel = None self.labels = [] self.master = scatter_widget self.shown_attribute_indices = [] self.shown_x = "" self.shown_y = "" self.pen_colors = self.brush_colors = None self.valid_data = None # np.ndarray self.selection = None # np.ndarray self.n_points = 0 self.gui = OWPlotGUI(self) self.continuous_palette = ContinuousPaletteGenerator( QColor(255, 255, 0), QColor(0, 0, 255), True) self.discrete_palette = ColorPaletteGenerator() self.selection_behavior = 0 self.legend = self.color_legend = None self.__legend_anchor = (1, 0), (1, 0) self.__color_legend_anchor = (1, 1), (1, 1) self.scale = None # DiscretizedScale self.subset_indices = None # self.setMouseTracking(True) # self.grabGesture(QPinchGesture) # self.grabGesture(QPanGesture) self.update_grid() self._tooltip_delegate = HelpEventDelegate(self.help_event) self.plot_widget.scene().installEventFilter(self._tooltip_delegate)
def set_node_colors(self): if not self.graph: return attribute = self.attr_color assert not attribute or isinstance(attribute, Orange.data.Variable) if self.view.legend is not None: self.view.scene().removeItem(self.view.legend) self.view.legend.clear() else: self.view.legend = LegendItem() self.view.legend.set_parent(self.view) if not attribute: for node in self.view.nodes: node.setColor(None) return table = self.graph.items() if not table: return if attribute in table.domain.class_vars: values = table[:, attribute].Y if values.ndim > 1: values = values.T elif attribute in table.domain.metas: values = table[:, attribute].metas[:, 0] elif attribute in table.domain.attributes: values = table[:, attribute].X[:, 0] else: raise RuntimeError("Shouldn't be able to select this column") if attribute.is_continuous: colors = CONTINUOUS_PALETTE[scale(values)] label = PaletteItemSample( CONTINUOUS_PALETTE, DiscretizedScale(np.nanmin(values), np.nanmax(values))) self.view.legend.addItem(label, "") self.view.legend.setGeometry(label.boundingRect()) elif attribute.is_discrete: DISCRETE_PALETTE = ColorPaletteGenerator(len(attribute.values)) colors = DISCRETE_PALETTE[values] for value, color in zip(attribute.values, DISCRETE_PALETTE): self.view.legend.addItem( ScatterPlotItem(pen=Node.Pen.DEFAULT, brush=QBrush(QColor(color)), size=10, symbol="o"), escape(value)) for node, color in zip(self.view.nodes, colors): node.setColor(color) self.view.scene().addItem(self.view.legend) self.view.legend.geometry_changed()
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. """ if self.attr_color is None: return None colors = self.attr_color.colors if self.attr_color.is_discrete: return ColorPaletteGenerator( number_of_colors=min(len(colors), MAX_CATEGORIES), rgb_colors=colors if len(colors) <= MAX_CATEGORIES else DefaultRGBColors) else: return ContinuousPaletteGenerator(*colors)
def set_pen_colors(self): self.pen_normal.clear() self.pen_subset.clear() self.pen_selected.clear() color_var = self._current_color_var() if color_var != "(Same color)": colors = color_var.colors discrete_palette = ColorPaletteGenerator( number_of_colors=len(colors), rgb_colors=colors) for v in color_var.values: basecolor = discrete_palette[color_var.to_val(v)] basecolor = QColor(basecolor) basecolor.setAlphaF(0.9) self.pen_subset[v] = pg.mkPen(color=basecolor, width=1) self.pen_selected[v] = pg.mkPen(color=basecolor, width=2, style=Qt.DotLine) notselcolor = basecolor.lighter(150) notselcolor.setAlphaF(0.5) self.pen_normal[v] = pg.mkPen(color=notselcolor, width=1)
def on_changed(self): if not self.attrs or not self.all_attrs: return series = [] options = dict(series=series) plotlines = [] for i, (attr, color) in enumerate( zip(self.attrs, ColorPaletteGenerator(len(self.all_attrs))[self.attrs])): attr_name = self.all_attrs[attr][0] pac = self.acf(attr_name, self.use_pacf, False) if self.use_confint: # Confidence intervals, from: # https://www.mathworks.com/help/econ/autocorrelation-and-partial-autocorrelation.html # https://www.mathworks.com/help/signal/ug/confidence-intervals-for-sample-autocorrelation.html std = 1.96 * ( (1 + 2 * (pac[:, 1]**2).sum()) / len(self.data))**.5 # = more precise than 1.96/sqrt(N) color = '/**/ Highcharts.getOptions().colors[{}] /**/'.format( i) line = dict(color=color, width=1.5, dashStyle='dash') plotlines.append(dict(line, value=std)) plotlines.append(dict(line, value=-std)) series.append( dict( # TODO: set units to something more readable than #periods (e.g. days) data=pac, type='column', name=attr_name, zIndex=2, )) # TODO: give periods meaning (datetime names) plotlines.append(dict(value=0, color='black', width=2, zIndex=3)) if series: self.plot.chart(options, yAxis_plotLines=plotlines, xAxis_type='linear') else: self.plot.clear()
def compute_colors_sel(self, keep_colors=False): if not keep_colors: self.pen_colors_sel = self.brush_colors_sel = None nopen = QPen(Qt.NoPen) if self.selection is not None: sels = np.max(self.selection) if sels == 1: pens = [ nopen, _make_pen(QColor(255, 190, 0, 255), SELECTION_WIDTH + 1.) ] else: palette = ColorPaletteGenerator(number_of_colors=sels + 1) pens = [nopen] + \ [_make_pen(palette[i], SELECTION_WIDTH + 1.) for i in range(sels)] pen = [pens[a] for a in self.selection[self.valid_data]] else: pen = [nopen] * self.n_points brush = [QBrush(QColor(255, 255, 255, 0))] * self.n_points return pen, brush
def __init__(self, scatter_widget, parent=None, _="None", view_box=InteractiveViewBox): gui.OWComponent.__init__(self, scatter_widget) self.view_box = view_box(self) self.plot_widget = pg.PlotWidget(viewBox=self.view_box, parent=parent, background="w") self.plot_widget.getPlotItem().buttonsHidden = True self.plot_widget.setAntialiasing(True) self.plot_widget.sizeHint = lambda: QSize(500, 500) scene = self.plot_widget.scene() self._create_drag_tooltip(scene) self._data = None # Original Table as passed from widget to new_data before transformations self.replot = self.plot_widget.replot ScaleScatterPlotData.__init__(self) self.density_img = None self.scatterplot_item = None self.scatterplot_item_sel = None self.reg_line_item = None self.labels = [] self.master = scatter_widget self.master.Warning.add_message( "missing_coords", "Plot cannot be displayed because '{}' or '{}' is missing for " "all data points") self.master.Information.add_message( "missing_coords", "Points with missing '{}' or '{}' are not displayed") self.master.Information.add_message( "missing_size", "Points with undefined '{}' are shown in smaller size") self.master.Information.add_message( "missing_shape", "Points with undefined '{}' are shown as crossed circles") self.shown_attribute_indices = [] self.shown_x = self.shown_y = None self.pen_colors = self.brush_colors = None self.valid_data = None # np.ndarray self.selection = None # np.ndarray self.n_points = 0 self.gui = OWPlotGUI(self) self.continuous_palette = ContinuousPaletteGenerator( QColor(255, 255, 0), QColor(0, 0, 255), True) self.discrete_palette = ColorPaletteGenerator() self.selection_behavior = 0 self.legend = self.color_legend = None self.__legend_anchor = (1, 0), (1, 0) self.__color_legend_anchor = (1, 1), (1, 1) self.scale = None # DiscretizedScale self.subset_indices = None # self.setMouseTracking(True) # self.grabGesture(QPinchGesture) # self.grabGesture(QPanGesture) self.update_grid() self._tooltip_delegate = HelpEventDelegate(self.help_event) self.plot_widget.scene().installEventFilter(self._tooltip_delegate)
def __call__(self, n): return ColorPaletteGenerator(n)
c.createColorButton(box, CANVAS_COLOR, "Canvas color", self.graph.color(OWPalette.Canvas)) c.setColorSchemas(self.color_settings, self.selected_schema_index) return c def attributes_changed(self): if not self.__ignore_updates: self.graph.removeAllSelections() self.update_graph() self.send_shown_attributes() #test widget appearance if __name__ == "__main__": a = QApplication(sys.argv) ow = OWParallelCoordinates() ow.show() ow.graph.discrete_palette = ColorPaletteGenerator( rgb_colors=[(127, 201, 127), (190, 174, 212), (253, 192, 134)]) ow.graph.group_lines = True ow.graph.number_of_groups = 10 ow.graph.number_of_steps = 30 data = Orange.data.Table("iris") ow.set_data(data) ow.handleNewSignals() a.exec_() ow.saveSettings()
def get_palette(self): if self.is_continuous_color(): return ContinuousPaletteGenerator(Qt.white, Qt.black, False) else: return ColorPaletteGenerator(12)