def update_coordinates(self): """ Trigger the update of coordinates while keeping other features intact. The method gets the coordinates by calling `self.get_coordinates`, which in turn calls the widget's `get_coordinate_data`. The number of coordinate pairs returned by the latter must match the current number of points. If this is not the case, the widget should trigger the complete update by calling `reset_graph` instead of this method. """ x, y = self.get_coordinates() if x is None or not len(x): return if self.scatterplot_item is None: if self.sample_indices is None: indices = np.arange(self.n_valid) else: indices = self.sample_indices kwargs = dict(x=x, y=y, data=indices) self.scatterplot_item = ScatterPlotItem(**kwargs) self.scatterplot_item.sigClicked.connect(self.select_by_click) self.scatterplot_item_sel = ScatterPlotItem(**kwargs) self.plot_widget.addItem(self.scatterplot_item_sel) self.plot_widget.addItem(self.scatterplot_item) else: self._update_plot_coordinates(self.scatterplot_item, x, y) self._update_plot_coordinates(self.scatterplot_item_sel, x, y) self.update_labels() self.update_density() # Todo: doesn't work: try MDS with density on self._reset_view(x, y)
def setDataPointsVisibility(self, b): if self.dataPoints is None: if self.flags == 'peak': chrom = self.ref.sample.massExtraction(self.ref.mass(), self.ref.sample.ppm, asChromatogram=True) self.dataPoints = ScatterPlotItem(x=chrom.x_data, y=chrom.y_data) else: self.dataPoints = ScatterPlotItem(x=self.ref.x_data, y=self.ref.y_data) if self.flags != 'spectra': self.dataPoints.sigClicked.connect(self.requestSpectra) self.pw.addDataItem(self.dataPoints) self.dataPoints.setVisible(b)
def make_color_legend(self): color_index = self.get_color_index() if color_index == -1: return color_var = self.domain[color_index] use_shape = self.get_shape_index() == color_index if color_var.is_discrete: if not self.legend: self.create_legend() palette = self.discrete_palette for i, value in enumerate(color_var.values): color = QColor(*palette.getRGB(i)) brush = color.lighter(self.DarkerValue) self.legend.addItem( ScatterPlotItem( pen=color, brush=brush, size=10, symbol=self.CurveSymbols[i] if use_shape else "o"), escape(value)) else: legend = self.color_legend = LegendItem() legend.setParentItem(self.plot_widget.getViewBox()) legend.restoreAnchor(self.__color_legend_anchor) label = PaletteItemSample(self.continuous_palette, self.scale) legend.addItem(label, "") legend.setGeometry(label.boundingRect())
def make_color_legend(self): color_index = self.get_color_index() if color_index == -1: return color_var = self.data_domain[color_index] use_shape = self.get_shape_index() == color_index if isinstance(color_var, DiscreteVariable): if not self.legend: self.create_legend() palette = self.discrete_palette for i, value in enumerate(color_var.values): color = QColor(*palette.getRGB(i)) brush = color.lighter(self.DarkerValue) self.legend.addItem( ScatterPlotItem( pen=color, brush=brush, size=10, symbol=self.CurveSymbols[i] if use_shape else "o"), value) else: legend = self.color_legend = PositionedLegendItem( self.plot_widget.plotItem, self, legend_id="colors", at_bottom=True) label = PaletteItemSample(self.continuous_palette, self.scale) legend.addItem(label, "") legend.setGeometry(label.boundingRect())
def make_color_legend(self): if self.attr_color is None: return use_shape = self.attr_shape == self.get_color() if self.attr_color.is_discrete: if not self.legend: self.create_legend() palette = self.discrete_palette for i, value in enumerate(self._get_values(self.attr_color)): color = QColor(*palette.getRGB(i)) pen = _make_pen(color.darker(self.DarkerValue), 1.5) color.setAlpha( self.alpha_value if self.subset_indices is None else 255) brush = QBrush(color) self.legend.addItem( ScatterPlotItem( pen=pen, brush=brush, size=10, symbol=self.CurveSymbols[i] if use_shape else "o"), escape(value)) else: legend = self.color_legend = LegendItem() legend.setParentItem(self.plot_widget.getViewBox()) legend.restoreAnchor(self.__color_legend_anchor) label = PaletteItemSample(self.continuous_palette, self.scale) legend.addItem(label, "") legend.setGeometry(label.boundingRect())
def _setup_plot(self): self.__replot_requested = False self.clear_plot() variables = list(self.varmodel_selected) if not variables: return coords = [self._get_data(var) for var in variables] coords = numpy.vstack(coords) p, N = coords.shape assert N == len(self.data), p == len(variables) axes = linproj.defaultaxes(len(variables)) assert axes.shape == (2, p) mask = ~numpy.logical_or.reduce(numpy.isnan(coords), axis=0) coords = coords[:, mask] X, Y = numpy.dot(axes, coords) X = plotutils.normalized(X) Y = plotutils.normalized(Y) pen_data, brush_data = self._color_data(mask) size_data = self._size_data(mask) shape_data = self._shape_data(mask) if self.jitter_value > 0: value = [0, 0.01, 0.1, 0.5, 1, 2][self.jitter_value] rstate = numpy.random.RandomState(0) jitter_x = (rstate.random_sample(X.shape) * 2 - 1) * value / 100 rstate = numpy.random.RandomState(1) jitter_y = (rstate.random_sample(Y.shape) * 2 - 1) * value / 100 X += jitter_x Y += jitter_y self._item = ScatterPlotItem( X, Y, pen=pen_data, brush=brush_data, size=size_data, shape=shape_data, antialias=True, data=numpy.arange(len(self.data))[mask] ) self._item._mask = mask self.viewbox.addItem(self._item) for i, axis in enumerate(axes.T): axis_item = AxisItem(line=QLineF(0, 0, axis[0], axis[1]), label=variables[i].name) self.viewbox.addItem(axis_item) self.viewbox.setRange(QtCore.QRectF(-1.05, -1.05, 2.1, 2.1))
def _update_shape_legend(self, labels): self.shape_legend.clear() if labels is None or self.scatterplot_item is None: return color = QColor(0, 0, 0) color.setAlpha(self.alpha_value) for label, symbol in zip(labels, self.CurveSymbols): self.shape_legend.addItem( ScatterPlotItem(pen=color, brush=color, size=10, symbol=symbol), escape(label))
def update_data(self, attr_x, attr_y): self.shown_x = attr_x self.shown_y = attr_y self.remove_legend() if self.scatterplot_item: self.plot_widget.removeItem(self.scatterplot_item) for label in self.labels: self.plot_widget.removeItem(label) self.labels = [] self.set_axis_title("bottom", "") self.set_axis_title("left", "") if self.scaled_data is None or not len(self.scaled_data): self.valid_data = None self.n_points = 0 return index_x = self.attribute_name_index[attr_x] index_y = self.attribute_name_index[attr_y] self.valid_data = self.get_valid_list([index_x, index_y]) x_data, y_data = self.get_xy_data_positions(attr_x, attr_y, self.valid_data) x_data = x_data[self.valid_data] y_data = y_data[self.valid_data] self.n_points = len(x_data) for axis, name, index in (("bottom", attr_x, index_x), ("left", attr_y, index_y)): self.set_axis_title(axis, name) var = self.data_domain[index] if isinstance(var, DiscreteVariable): self.set_labels(axis, get_variable_values_sorted(var)) else: self.set_labels(axis, None) color_data, brush_data = self.compute_colors() size_data = self.compute_sizes() shape_data = self.compute_symbols() self.scatterplot_item = ScatterPlotItem(x=x_data, y=y_data, data=np.arange(self.n_points), symbol=shape_data, size=size_data, pen=color_data, brush=brush_data) self.plot_widget.addItem(self.scatterplot_item) self.scatterplot_item.selected_points = [] self.scatterplot_item.sigClicked.connect(self.select_by_click) self.update_labels() self.make_legend() self.plot_widget.replot()
def make_shape_legend(self): shape = self.get_shape() if shape is None or shape == self.get_color(): return if not self.legend: self.create_legend() color = QColor(0, 0, 0) color.setAlpha(self.alpha_value) for i, value in enumerate(self.attr_shape.values): self.legend.addItem( ScatterPlotItem(pen=color, brush=color, size=10, symbol=self.CurveSymbols[i]), escape(value))
def make_shape_legend(self): shape_index = self.get_shape_index() if shape_index == -1 or shape_index == self.get_color_index(): return if not self.legend: self.create_legend() shape_var = self.domain[shape_index] color = QColor(0, 0, 0) color.setAlpha(self.alpha_value) for i, value in enumerate(shape_var.values): self.legend.addItem( ScatterPlotItem(pen=color, brush=color, size=10, symbol=self.CurveSymbols[i]), escape(value))
def _update_colored_legend(self, legend, labels, symbols): if self.scatterplot_item is None or not self.palette: return if isinstance(symbols, str): symbols = itertools.repeat(symbols, times=len(labels)) for i, (label, symbol) in enumerate(zip(labels, symbols)): color = QColor(*self.palette.getRGB(i)) pen = _make_pen(color.darker(self.DarkerValue), 1.5) color.setAlpha(255 if self.subset_is_shown else self.alpha_value) brush = QBrush(color) legend.addItem( ScatterPlotItem(pen=pen, brush=brush, size=10, symbol=symbol), escape(label))
def make_shape_legend(self): shape_index = self.get_shape_index() if shape_index == -1 or shape_index == self.get_color_index(): return if not self.legend: self.create_legend() shape_var = self.data_domain[shape_index] color = self.plot_widget.palette().color(OWPalette.Data) pen = QPen(color.darker(self.DarkerValue)) color.setAlpha(self.alpha_value) for i, value in enumerate(shape_var.values): self.legend.addItem( ScatterPlotItem(pen=pen, brush=color, size=10, symbol=self.CurveSymbols[i]), escape(value))
def update_data(self, attr_x, attr_y, reset_view=True): self.master.Warning.missing_coords.clear() self.master.Information.missing_coords.clear() self._clear_plot_widget() if self.shown_y != attr_y: # 'reset' the axis text width estimation. Without this the left # axis tick labels space only ever expands yaxis = self.plot_widget.getAxis("left") yaxis.textWidth = 30 self.shown_x, self.shown_y = attr_x, attr_y if attr_x not in self.data.domain or attr_y not in self.data.domain: data = self.sparse_to_dense() self.set_data(data) if self.jittered_data is None or not len(self.jittered_data): self.valid_data = None else: self.valid_data = self.get_valid_list([attr_x, attr_y]) if not np.any(self.valid_data): self.valid_data = None if self.valid_data is None: self.selection = None self.n_points = 0 self.master.Warning.missing_coords(self.shown_x.name, self.shown_y.name) return x_data, y_data = self.get_xy_data_positions(attr_x, attr_y, self.valid_data) self.n_points = len(x_data) if reset_view: min_x, max_x = np.nanmin(x_data), np.nanmax(x_data) min_y, max_y = np.nanmin(y_data), np.nanmax(y_data) self.view_box.setRange(QRectF(min_x, min_y, max_x - min_x, max_y - min_y), padding=0.025) self.view_box.init_history() self.view_box.tag_history() [min_x, max_x], [min_y, max_y] = self.view_box.viewRange() for axis, var in (("bottom", attr_x), ("left", attr_y)): self.set_axis_title(axis, var) if var.is_discrete: self.set_labels(axis, get_variable_values_sorted(var)) else: self.set_labels(axis, None) color_data, brush_data = self.compute_colors() color_data_sel, brush_data_sel = self.compute_colors_sel() size_data = self.compute_sizes() shape_data = self.compute_symbols() if self.should_draw_density(): rgb_data = [pen.color().getRgb()[:3] for pen in color_data] self.density_img = classdensity.class_density_image( min_x, max_x, min_y, max_y, self.resolution, x_data, y_data, rgb_data) self.plot_widget.addItem(self.density_img) self.data_indices = np.flatnonzero(self.valid_data) if len(self.data_indices) != len(self.data): self.master.Information.missing_coords(self.shown_x.name, self.shown_y.name) self.scatterplot_item = ScatterPlotItem(x=x_data, y=y_data, data=self.data_indices, symbol=shape_data, size=size_data, pen=color_data, brush=brush_data) self.scatterplot_item_sel = ScatterPlotItem(x=x_data, y=y_data, data=self.data_indices, symbol=shape_data, size=size_data + SELECTION_WIDTH, pen=color_data_sel, brush=brush_data_sel) self.plot_widget.addItem(self.scatterplot_item_sel) self.plot_widget.addItem(self.scatterplot_item) self.scatterplot_item.selected_points = [] self.scatterplot_item.sigClicked.connect(self.select_by_click) if self.show_reg_line: _x_data = self.data.get_column_view(self.shown_x)[0] _y_data = self.data.get_column_view(self.shown_y)[0] _x_data = _x_data[self.valid_data] _y_data = _y_data[self.valid_data] assert _x_data.size assert _y_data.size self.draw_regression_line(_x_data, _y_data, np.min(_x_data), np.max(_y_data)) self.update_labels() self.make_legend() self.plot_widget.replot()
def _setup_plot(self): have_data = self.data is not None have_matrix_transposed = self.matrix is not None and not self.matrix.axis plotstyle = mdsplotutils.plotstyle size = self._effective_matrix.shape[0] def column(data, variable): a, _ = data.get_column_view(variable) return a.ravel() def attributes(matrix): return matrix.row_items.domain.attributes def scale(a): dmin, dmax = numpy.nanmin(a), numpy.nanmax(a) if dmax - dmin > 0: return (a - dmin) / (dmax - dmin) else: return numpy.zeros_like(a) def jitter(x, factor=1, rstate=None): if rstate is None: rstate = numpy.random.RandomState() elif not isinstance(rstate, numpy.random.RandomState): rstate = numpy.random.RandomState(rstate) span = numpy.nanmax(x) - numpy.nanmin(x) if span < numpy.finfo(x.dtype).eps * 100: span = 1 a = factor * span / 100. return x + (rstate.random_sample(x.shape) - 0.5) * a if self._pen_data is None: if self._selection_mask is not None: pointflags = numpy.where( self._selection_mask, mdsplotutils.Selected, mdsplotutils.NoFlags) else: pointflags = None color_index = self.cb_color_value.currentIndex() if have_data and color_index > 0: color_var = self.colorvar_model[color_index] if color_var.is_discrete: palette = colorpalette.ColorPaletteGenerator( len(color_var.values) ) plotstyle = plotstyle.updated(discrete_palette=palette) else: palette = None color_data = mdsplotutils.color_data( self.data, color_var, plotstyle=plotstyle) color_data = numpy.hstack( (color_data, numpy.full((len(color_data), 1), self.symbol_opacity, dtype=float)) ) pen_data = mdsplotutils.pen_data(color_data * 0.8, pointflags) brush_data = mdsplotutils.brush_data(color_data) elif have_matrix_transposed and \ self.colorvar_model[color_index] == 'Attribute names': attr = attributes(self.matrix) palette = colorpalette.ColorPaletteGenerator(len(attr)) color_data = [palette.getRGB(i) for i in range(len(attr))] color_data = numpy.hstack(( color_data, numpy.full((len(color_data), 1), self.symbol_opacity, dtype=float)) ) pen_data = mdsplotutils.pen_data(color_data * 0.8, pointflags) brush_data = mdsplotutils.brush_data(color_data) else: pen_data = make_pen(QtGui.QColor(Qt.darkGray), cosmetic=True) if self._selection_mask is not None: pen_data = numpy.array( [pen_data, plotstyle.selected_pen]) pen_data = pen_data[self._selection_mask.astype(int)] else: pen_data = numpy.full(self._effective_matrix.dim, pen_data, dtype=object) brush_data = numpy.full( size, pg.mkColor((192, 192, 192, self.symbol_opacity)), dtype=object) if self._subset_mask is not None and have_data and \ self._subset_mask.shape == (size, ): # clear brush fill for non subset data brush_data[~self._subset_mask] = QtGui.QBrush(Qt.NoBrush) self._pen_data = pen_data self._brush_data = brush_data if self._shape_data is None: shape_index = self.cb_shape_value.currentIndex() if have_data and shape_index > 0: Symbols = ScatterPlotItem.Symbols symbols = numpy.array(list(Symbols.keys())) shape_var = self.shapevar_model[shape_index] data = column(self.data, shape_var).astype(numpy.float) data = data % (len(Symbols) - 1) data[numpy.isnan(data)] = len(Symbols) - 1 shape_data = symbols[data.astype(int)] elif have_matrix_transposed and \ self.shapevar_model[shape_index] == 'Attribute names': Symbols = ScatterPlotItem.Symbols symbols = numpy.array(list(Symbols.keys())) attr = [i % (len(Symbols) - 1) for i, _ in enumerate(attributes(self.matrix))] shape_data = symbols[attr] else: shape_data = "o" self._shape_data = shape_data if self._size_data is None: MinPointSize = 3 point_size = self.symbol_size + MinPointSize size_index = self.cb_size_value.currentIndex() if have_data and size_index == 1: # size by stress size_data = stress(self.embedding, self._effective_matrix) size_data = scale(size_data) size_data = MinPointSize + size_data * point_size elif have_data and size_index > 0: size_var = self.sizevar_model[size_index] size_data = column(self.data, size_var) size_data = scale(size_data) size_data = MinPointSize + size_data * point_size else: size_data = point_size self._size_data = size_data if self._label_data is None: label_index = self.cb_label_value.currentIndex() if have_data and label_index > 0: label_var = self.labelvar_model[label_index] label_data = column(self.data, label_var) label_data = [label_var.str_val(val) for val in label_data] label_items = [pg.TextItem(text, anchor=(0.5, 0), color=0.0) for text in label_data] elif have_matrix_transposed and \ self.labelvar_model[label_index] == 'Attribute names': attr = attributes(self.matrix) label_items = [pg.TextItem(str(text), anchor=(0.5, 0)) for text in attr] else: label_items = None self._label_data = label_items emb_x, emb_y = self.embedding[:, 0], self.embedding[:, 1] if self.jitter > 0: _, jitter_factor = self.JitterAmount[self.jitter] emb_x = jitter(emb_x, jitter_factor, rstate=42) emb_y = jitter(emb_y, jitter_factor, rstate=667) if self.connected_pairs and self.__draw_similar_pairs: if self._similar_pairs is None: # This code requires storing lower triangle of X (n x n / 2 # doubles), n x n / 2 * 2 indices to X, n x n / 2 indices for # argsort result. If this becomes an issue, it can be reduced to # n x n argsort indices by argsorting the entire X. Then we # take the first n + 2 * p indices. We compute their coordinates # i, j in the original matrix. We keep those for which i < j. # n + 2 * p will suffice to exclude the diagonal (i = j). If the # number of those for which i < j is smaller than p, we instead # take i > j. Among those that remain, we take the first p. # Assuming that MDS can't show so many points that memory could # become an issue, I preferred using simpler code. m = self._effective_matrix n = len(m) p = (n * (n - 1) // 2 * self.connected_pairs) // 100 indcs = numpy.triu_indices(n, 1) sorted = numpy.argsort(m[indcs])[:p] self._similar_pairs = fpairs = numpy.empty(2 * p, dtype=int) fpairs[::2] = indcs[0][sorted] fpairs[1::2] = indcs[1][sorted] for i in range(int(len(emb_x[self._similar_pairs]) / 2)): item = QtGui.QGraphicsLineItem( emb_x[self._similar_pairs][i * 2], emb_y[self._similar_pairs][i * 2], emb_x[self._similar_pairs][i * 2 + 1], emb_y[self._similar_pairs][i * 2 + 1] ) pen = QtGui.QPen(QtGui.QBrush(QtGui.QColor(204, 204, 204)), 2) pen.setCosmetic(True) item.setPen(pen) self.plot.addItem(item) data = numpy.arange(size) self._scatter_item = item = ScatterPlotItem( x=emb_x, y=emb_y, pen=self._pen_data, brush=self._brush_data, symbol=self._shape_data, size=self._size_data, data=data, antialias=True ) self.plot.addItem(item) if self._label_data is not None: if self.label_only_selected: if self._selection_mask is not None: for (x, y), text_item, selected \ in zip(self.embedding, self._label_data, self._selection_mask): if selected: self.plot.addItem(text_item) text_item.setPos(x, y) else: for (x, y), text_item in zip(self.embedding, self._label_data): self.plot.addItem(text_item) text_item.setPos(x, y) self._legend_item = LegendItem() viewbox = self.plot.getViewBox() self._legend_item.setParentItem(self.plot.getViewBox()) self._legend_item.setZValue(viewbox.zValue() + 10) self._legend_item.restoreAnchor(self.legend_anchor) color_var = shape_var = None color_index = self.cb_color_value.currentIndex() if have_data and 1 <= color_index < len(self.colorvar_model): color_var = self.colorvar_model[color_index] assert isinstance(color_var, Orange.data.Variable) shape_index = self.cb_shape_value.currentIndex() if have_data and 1 <= shape_index < len(self.shapevar_model): shape_var = self.shapevar_model[shape_index] assert isinstance(shape_var, Orange.data.Variable) if shape_var is not None or \ (color_var is not None and color_var.is_discrete): legend_data = mdsplotutils.legend_data( color_var, shape_var, plotstyle=plotstyle) for color, symbol, text in legend_data: self._legend_item.addItem( ScatterPlotItem(pen=color, brush=color, symbol=symbol, size=10), escape(text) ) else: self._legend_item.hide()
def update_data(self, attr_x, attr_y, reset_view=True): self.master.Warning.missing_coords.clear() self.master.Information.missing_coords.clear() self._clear_plot_widget() self.shown_x, self.shown_y = attr_x, attr_y if self.jittered_data is None or not len(self.jittered_data): self.valid_data = None else: index_x = self.domain.index(attr_x) index_y = self.domain.index(attr_y) self.valid_data = self.get_valid_list([index_x, index_y]) if not np.any(self.valid_data): self.valid_data = None if self.valid_data is None: self.selection = None self.n_points = 0 self.master.Warning.missing_coords(self.shown_x.name, self.shown_y.name) return x_data, y_data = self.get_xy_data_positions(attr_x, attr_y, self.valid_data) self.n_points = len(x_data) if reset_view: min_x, max_x = np.nanmin(x_data), np.nanmax(x_data) min_y, max_y = np.nanmin(y_data), np.nanmax(y_data) self.view_box.setRange(QRectF(min_x, min_y, max_x - min_x, max_y - min_y), padding=0.025) self.view_box.init_history() self.view_box.tag_history() [min_x, max_x], [min_y, max_y] = self.view_box.viewRange() for axis, name, index in (("bottom", attr_x, index_x), ("left", attr_y, index_y)): self.set_axis_title(axis, name) var = self.domain[index] if var.is_discrete: self.set_labels(axis, get_variable_values_sorted(var)) else: self.set_labels(axis, None) color_data, brush_data = self.compute_colors() color_data_sel, brush_data_sel = self.compute_colors_sel() size_data = self.compute_sizes() shape_data = self.compute_symbols() if self.should_draw_density(): rgb_data = [pen.color().getRgb()[:3] for pen in color_data] self.density_img = classdensity.class_density_image( min_x, max_x, min_y, max_y, self.resolution, x_data, y_data, rgb_data) self.plot_widget.addItem(self.density_img) data_indices = np.flatnonzero(self.valid_data) if len(data_indices) != self.original_data.shape[1]: self.master.Information.missing_coords(self.shown_x.name, self.shown_y.name) self.scatterplot_item = ScatterPlotItem(x=x_data, y=y_data, data=data_indices, symbol=shape_data, size=size_data, pen=color_data, brush=brush_data) self.scatterplot_item_sel = ScatterPlotItem(x=x_data, y=y_data, data=data_indices, symbol=shape_data, size=size_data + SELECTION_WIDTH, pen=color_data_sel, brush=brush_data_sel) self.plot_widget.addItem(self.scatterplot_item_sel) self.plot_widget.addItem(self.scatterplot_item) self.scatterplot_item.selected_points = [] self.scatterplot_item.sigClicked.connect(self.select_by_click) self.update_labels() self.make_legend() self.plot_widget.replot()
def _setup_plot(self): have_data = self.data is not None have_matrix_transposed = self.matrix is not None and not self.matrix.axis def column(data, variable): a, _ = data.get_column_view(variable) return a.ravel() def attributes(matrix): return matrix.row_items.domain.attributes def scale(a): dmin, dmax = numpy.nanmin(a), numpy.nanmax(a) if dmax - dmin > 0: return (a - dmin) / (dmax - dmin) else: return numpy.zeros_like(a) if self._pen_data is None: if self._selection_mask is not None: pointflags = numpy.where(self._selection_mask, mdsplotutils.Selected, mdsplotutils.NoFlags) else: pointflags = None if have_data and self.color_index > 0: color_var = self.colorvar_model[self.color_index] if color_var.is_discrete: palette = colorpalette.ColorPaletteGenerator( len(color_var.values)) else: palette = None color_data = mdsplotutils.color_data( self.data, color_var, plotstyle=mdsplotutils.plotstyle) color_data = numpy.hstack((color_data, numpy.full((len(color_data), 1), self.symbol_opacity))) pen_data = mdsplotutils.pen_data(color_data, pointflags) elif have_matrix_transposed and self.colorvar_model[ self.color_index] == 'Attribute names': attr = attributes(self.matrix) palette = colorpalette.ColorPaletteGenerator(len(attr)) color_data = [palette.getRGB(i) for i in range(len(attr))] color_data = numpy.hstack( color_data, numpy.full((len(color_data), 1), self.symbol_opacity)) pen_data = mdsplotutils.pen_data(color_data, pointflags) else: pen_data = make_pen(QtGui.QColor(Qt.darkGray), cosmetic=True) pen_data = numpy.full(len(self.data), pen_data, dtype=object) self._pen_data = pen_data if self._shape_data is None: if have_data and self.shape_index > 0: Symbols = ScatterPlotItem.Symbols symbols = numpy.array(list(Symbols.keys())) shape_var = self.shapevar_model[self.shape_index] data = column(self.data, shape_var) data = data % (len(Symbols) - 1) data[numpy.isnan(data)] = len(Symbols) - 1 shape_data = symbols[data.astype(int)] elif have_matrix_transposed and self.shapevar_model[ self.shape_index] == 'Attribute names': Symbols = ScatterPlotItem.Symbols symbols = numpy.array(list(Symbols.keys())) attr = [ i % (len(Symbols) - 1) for i, _ in enumerate(attributes(self.matrix)) ] shape_data = symbols[attr] else: shape_data = "o" self._shape_data = shape_data if self._size_data is None: MinPointSize = 3 point_size = self.symbol_size + MinPointSize if have_data and self.size_index == 1: # size by stress size_data = stress(self.embedding, self._effective_matrix.X) size_data = scale(size_data) size_data = MinPointSize + size_data * point_size elif have_data and self.size_index > 0: size_var = self.sizevar_model[self.size_index] size_data = column(self.data, size_var) size_data = scale(size_data) size_data = MinPointSize + size_data * point_size else: size_data = point_size if self._label_data is None: if have_data and self.label_index > 0: label_var = self.labelvar_model[self.label_index] label_data = column(self.data, label_var) label_data = [label_var.repr_val(val) for val in label_data] label_items = [ pg.TextItem(text, anchor=(0.5, 0)) for text in label_data ] elif have_matrix_transposed and self.labelvar_model[ self.label_index] == 'Attribute names': attr = attributes(self.matrix) label_items = [ pg.TextItem(str(text), anchor=(0.5, 0)) for text in attr ] else: label_items = None self._label_data = label_items self._scatter_item = item = ScatterPlotItem( x=self.embedding[:, 0], y=self.embedding[:, 1], pen=self._pen_data, symbol=self._shape_data, brush=QtGui.QBrush(Qt.transparent), size=size_data, data=numpy.arange(len(self.data)), antialias=True) self.plot.addItem(item) if self._label_data is not None: for (x, y), text_item in zip(self.embedding, self._label_data): self.plot.addItem(text_item) text_item.setPos(x, y)
def update_data(self, attr_x, attr_y): self.shown_x = attr_x self.shown_y = attr_y self.remove_legend() if self.scatterplot_item: self.plot_widget.removeItem(self.scatterplot_item) if self.scatterplot_item_sel: self.plot_widget.removeItem(self.scatterplot_item_sel) for label in self.labels: self.plot_widget.removeItem(label) self.labels = [] self.set_axis_title("bottom", "") self.set_axis_title("left", "") if self.scaled_data is None or not len(self.scaled_data): self.valid_data = None self.n_points = 0 return index_x = self.attribute_name_index[attr_x] index_y = self.attribute_name_index[attr_y] self.valid_data = self.get_valid_list([index_x, index_y]) x_data, y_data = self.get_xy_data_positions(attr_x, attr_y, self.valid_data) x_data = x_data[self.valid_data] y_data = y_data[self.valid_data] self.n_points = len(x_data) for axis, name, index in (("bottom", attr_x, index_x), ("left", attr_y, index_y)): self.set_axis_title(axis, name) var = self.data_domain[index] if var.is_discrete: self.set_labels(axis, get_variable_values_sorted(var)) else: self.set_labels(axis, None) color_data, brush_data = self.compute_colors() color_data_sel, brush_data_sel = self.compute_colors_sel() size_data = self.compute_sizes() shape_data = self.compute_symbols() self.scatterplot_item = ScatterPlotItem(x=x_data, y=y_data, data=np.arange(self.n_points), symbol=shape_data, size=size_data, pen=color_data, brush=brush_data) self.scatterplot_item_sel = ScatterPlotItem( x=x_data, y=y_data, data=np.arange(self.n_points), symbol=shape_data, size=size_data + SELECTION_WIDTH, pen=color_data_sel, brush=brush_data_sel) self.plot_widget.addItem(self.scatterplot_item) self.plot_widget.addItem(self.scatterplot_item_sel) self.scatterplot_item.selected_points = [] self.scatterplot_item.sigClicked.connect(self.select_by_click) self.update_labels() self.make_legend() self.view_box.init_history() self.plot_widget.replot() min_x, max_x = np.nanmin(x_data), np.nanmax(x_data) min_y, max_y = np.nanmin(y_data), np.nanmax(y_data) self.view_box.setRange(QRectF(min_x, min_y, max_x - min_x, max_y - min_y), padding=0.025) self.view_box.tag_history()
def update_data(self, attr_x, attr_y, reset_view=True): self.shown_x = attr_x self.shown_y = attr_y self.remove_legend() if self.density_img: self.plot_widget.removeItem(self.density_img) self.density_img = None if self.scatterplot_item: self.plot_widget.removeItem(self.scatterplot_item) self.scatterplot_item = None if self.scatterplot_item_sel: self.plot_widget.removeItem(self.scatterplot_item_sel) self.scatterplot_item_sel = None for label in self.labels: self.plot_widget.removeItem(label) self.labels = [] self.set_axis_title("bottom", "") self.set_axis_title("left", "") if self.scaled_data is None or not len(self.scaled_data): self.valid_data = None self.selection = None self.n_points = 0 return index_x = self.attribute_name_index[attr_x] index_y = self.attribute_name_index[attr_y] self.valid_data = self.get_valid_list([index_x, index_y], also_class_if_exists=False) x_data, y_data = self.get_xy_data_positions(attr_x, attr_y, self.valid_data) self.n_points = len(x_data) if reset_view: min_x, max_x = np.nanmin(x_data), np.nanmax(x_data) min_y, max_y = np.nanmin(y_data), np.nanmax(y_data) self.view_box.setRange(QRectF(min_x, min_y, max_x - min_x, max_y - min_y), padding=0.025) self.view_box.init_history() self.view_box.tag_history() [min_x, max_x], [min_y, max_y] = self.view_box.viewRange() for axis, name, index in (("bottom", attr_x, index_x), ("left", attr_y, index_y)): self.set_axis_title(axis, name) var = self.data_domain[index] if var.is_discrete: self.set_labels(axis, get_variable_values_sorted(var)) else: self.set_labels(axis, None) color_data, brush_data = self.compute_colors() color_data_sel, brush_data_sel = self.compute_colors_sel() size_data = self.compute_sizes() shape_data = self.compute_symbols() if self.should_draw_density(): rgb_data = [pen.color().getRgb()[:3] for pen in color_data] self.density_img = classdensity.class_density_image( min_x, max_x, min_y, max_y, self.resolution, x_data, y_data, rgb_data) self.plot_widget.addItem(self.density_img) data_indices = np.flatnonzero(self.valid_data) self.scatterplot_item = ScatterPlotItem(x=x_data, y=y_data, data=data_indices, symbol=shape_data, size=size_data, pen=color_data, brush=brush_data) self.scatterplot_item_sel = ScatterPlotItem(x=x_data, y=y_data, data=data_indices, symbol=shape_data, size=size_data + SELECTION_WIDTH, pen=color_data_sel, brush=brush_data_sel) self.plot_widget.addItem(self.scatterplot_item_sel) self.plot_widget.addItem(self.scatterplot_item) self.scatterplot_item.selected_points = [] self.scatterplot_item.sigClicked.connect(self.select_by_click) self.update_labels() self.make_legend() self.plot_widget.replot()