Exemple #1
0
    def _setup_plot(self):
        have_data = self.data is not None

        if self._pen_data is None:
            if have_data and self.color_var > 0:
                color_var = self.colorvar_model[self.color_var]
                if is_discrete(color_var):
                    palette = colorpalette.ColorPaletteGenerator(
                        len(color_var.values))
                else:
                    palette = None

                color_data = colors(self.data, color_var, palette)
                pen_data = [
                    QtGui.QPen(QtGui.QColor(r, g, b)) for r, g, b in color_data
                ]
            else:
                pen_data = QtGui.QPen(Qt.black)
            self._pen_data = pen_data

        if self._shape_data is None:
            if have_data and self.shape_var > 0:
                Symbols = pg.graphicsItems.ScatterPlotItem.Symbols
                symbols = numpy.array(list(Symbols.keys()))

                shape_var = self.shapevar_model[self.shape_var]
                data = numpy.array(self.data[:, shape_var]).ravel()
                data = data % (len(Symbols) - 1)
                data[numpy.isnan(data)] = len(Symbols) - 1
                shape_data = symbols[data.astype(int)]
            else:
                shape_data = "o"
            self._shape_data = shape_data

        if self._size_data is None:
            MinPointSize = 1
            point_size = 8 + MinPointSize
            if have_data and self.size_var > 0:
                size_var = self.sizevar_model[self.size_var]
                size_data = numpy.array(self.data[:, size_var]).ravel()
                dmin, dmax = numpy.nanmin(size_data), numpy.nanmax(size_data)
                if dmax - dmin > 0:
                    size_data = (size_data - dmin) / (dmax - dmin)

                size_data = MinPointSize + size_data * point_size
            else:
                size_data = point_size

        item = pg.ScatterPlotItem(x=self.embeding[:, 0],
                                  y=self.embeding[:, 1],
                                  pen=self._pen_data,
                                  symbol=self._shape_data,
                                  brush=QtGui.QBrush(Qt.transparent),
                                  size=size_data,
                                  antialias=True)
        # plot(x, y, colors=plot.colors(data[:, color_var]),
        #      point_size=data[:, size_var],
        #      symbol=data[:, symbol_var])

        self.plot.addItem(item)
Exemple #2
0
    def _setup_plot(self):
        have_data = self.data is not None

        if self._pen_data is None:
            if have_data and self.color_var > 0:
                color_var = self.colorvar_model[self.color_var]
                if is_discrete(color_var):
                    palette = colorpalette.ColorPaletteGenerator(
                        len(color_var.values)
                    )
                else:
                    palette = None

                color_data = colors(self.data, color_var, palette)
                pen_data = [QtGui.QPen(QtGui.QColor(r, g, b))
                            for r, g, b in color_data]
            else:
                pen_data = QtGui.QPen(Qt.black)
            self._pen_data = pen_data

        if self._shape_data is None:
            if have_data and self.shape_var > 0:
                Symbols = pg.graphicsItems.ScatterPlotItem.Symbols
                symbols = numpy.array(list(Symbols.keys()))

                shape_var = self.shapevar_model[self.shape_var]
                data = numpy.array(self.data[:, shape_var]).ravel()
                data = data % (len(Symbols) - 1)
                data[numpy.isnan(data)] = len(Symbols) - 1
                shape_data = symbols[data.astype(int)]
            else:
                shape_data = "o"
            self._shape_data = shape_data

        if self._size_data is None:
            MinPointSize = 1
            point_size = 8 + MinPointSize
            if have_data and self.size_var > 0:
                size_var = self.sizevar_model[self.size_var]
                size_data = numpy.array(self.data[:, size_var]).ravel()
                dmin, dmax = numpy.nanmin(size_data), numpy.nanmax(size_data)
                if dmax - dmin > 0:
                    size_data = (size_data - dmin) / (dmax - dmin)

                size_data = MinPointSize + size_data * point_size
            else:
                size_data = point_size

        item = pg.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,
            antialias=True
        )
        # plot(x, y, colors=plot.colors(data[:, color_var]),
        #      point_size=data[:, size_var],
        #      symbol=data[:, symbol_var])

        self.plot.addItem(item)
Exemple #3
0
    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()
Exemple #4
0
    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 have_data and self.color_index > 0:
                color_var = self.colorvar_model[self.color_index]
                if is_discrete(color_var):
                    palette = colorpalette.ColorPaletteGenerator(
                        len(color_var.values)
                    )
                else:
                    palette = None

                color_data = colors(self.data, color_var, palette)
                pen_data = [make_pen(QtGui.QColor(r, g, b, self.symbol_opacity),
                                     cosmetic=True)
                            for r, g, b in color_data]
            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))]
                pen_data = [make_pen(QtGui.QColor(r, g, b, self.symbol_opacity),
                                     cosmetic=True)
                            for r, g, b in color_data]
            else:
                pen_data = make_pen(QtGui.QColor(Qt.darkGray), cosmetic=True)
            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

        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,
            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)
Exemple #5
0
    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)
Exemple #6
0
    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)

        self._legend_item = LegendItem()
        self._legend_item.setParentItem(self.plot.getViewBox())
        self._legend_item.anchor(*self.legend_anchor)

        color_var = shape_var = None
        if have_data and 1 <= self.color_index < len(self.colorvar_model):
            color_var = self.colorvar_model[self.color_index]
            assert isinstance(color_var, Orange.data.Variable)
        if have_data and 1 <= self.shape_index < len(self.shapevar_model):
            shape_var = self.shapevar_model[self.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=mdsplotutils.plotstyle)

            for color, symbol, text in legend_data:
                self._legend_item.addItem(
                    ScatterPlotItem(pen=color, brush=color, symbol=symbol,
                                    size=10),
                    text
                )
        else:
            self._legend_item.hide()
Exemple #7
0
    def call_filter(
            self,
            filter_type,
            filter_band,
            first_cutoff,
            second_cutoff,
            order,
            max_ripple,
            min_attenuation):
        """
        Call specified filter function on all audio clips

        :param filter_type: type of filter
        :param filter_band:  band of filter
        :param first_cutoff: first cutoff frequency
        :param second_cutoff: second cutoff frequency
        :param order: filter order
        :param max_ripple: the maximum ripple
        :param min_attenuation: the minimum attenuatio
        :return: Void
        """

        if self.data is None:
            return

        filterBand = (''.join(c for c in filter_band if c not in "-")).lower()
        filterType = self.convertTypeToStr(filter_type)

        error = None

        self.X = []
        self.metas = []

        try:
            for i in range(len(self.data.metas)):
                if self.data.X != []:
                    input_data = self.data.X[i]
                else:
                    input_data = read(self.data.metas[i][1])[1]
                    if len(input_data.shape) > 1:
                        input_data = input_data[:, 0]

                if filterType == "FIR" or filterType == "butter" or filterType == "bessel":
                    if filterBand == "lowpass" or filterBand == "highpass":
                        filtered = st.filter_signal(input_data,
                                                    ftype=filterType,
                                                    band=filterBand,
                                                    order=order,
                                                    frequency=first_cutoff,
                                                    sampling_rate=self.data.metas[i][-1])
                    else:
                        filtered = st.filter_signal(input_data, ftype=filterType, band=filterBand, order=order,
                                                    frequency=[first_cutoff, second_cutoff],
                                                    sampling_rate=self.data.metas[i][-1])
                elif filterType == "cheby1":
                    if filterBand == "lowpass" or filterBand == "highpass":
                        filtered = st.filter_signal(input_data,
                                                    ftype=filterType,
                                                    band=filterBand,
                                                    order=order,
                                                    frequency=first_cutoff,
                                                    sampling_rate=self.data.metas[i][-1],
                                                    rp=max_ripple)
                    else:
                        filtered = st.filter_signal(input_data, ftype=filterType, band=filterBand, order=order,
                                                    frequency=[first_cutoff, second_cutoff], sampling_rate=self.data.metas[i][-1],
                                                    rp=max_ripple)
                elif filterType == "cheby2":
                    if filterBand == "lowpass" or filterBand == "highpass":
                        filtered = st.filter_signal(input_data,
                                                    ftype=filterType,
                                                    band=filterBand,
                                                    order=order,
                                                    frequency=first_cutoff,
                                                    sampling_rate=self.data.metas[i][-1],
                                                    rs=min_attenuation)
                    else:
                        filtered = st.filter_signal(input_data, ftype=filterType, band=filterBand, order=order,
                                                    frequency=[first_cutoff, second_cutoff], sampling_rate=self.data.metas[i][-1],
                                                    rs=min_attenuation)

                else:
                    if filterBand == "lowpass" or filterBand == "highpass":
                        filtered = st.filter_signal(input_data,
                                                    ftype=filterType,
                                                    band=filterBand,
                                                    order=order,
                                                    frequency=first_cutoff,
                                                    sampling_rate=self.data.metas[i][-1],
                                                    rp=max_ripple,
                                                    rs=min_attenuation)
                    else:
                        filtered = st.filter_signal(input_data,
                                                    ftype=filterType,
                                                    band=filterBand,
                                                    order=order,
                                                    frequency=[first_cutoff,
                                                               second_cutoff],
                                                    sampling_rate=self.data.metas[i][-1],
                                                    rp=max_ripple,
                                                    rs=min_attenuation)

                self.new_tmp_dir = os.path.dirname(
                    self.data.metas[i][1]) + os.sep + "filtered-" + self.tmp_dir_id + os.sep

                if not os.path.exists(self.new_tmp_dir):
                    os.makedirs(self.new_tmp_dir)
                    self.new_tmp_dirs.append(self.new_tmp_dir)

                filename = self.new_tmp_dir + self.data.metas[i][0] + ".wav"
                self.metas.append([self.data.metas[i][0],
                                   filename,
                                   self.data.metas[i][2],
                                   self.data.metas[i][3],
                                   self.data.metas[i][4]])

                data = filtered["signal"]
                data = data / data.max()
                data = data * (2 ** 15 - 1)
                data = data.astype(numpy.int16)
                write(filename, self.data.metas[i][-1], data)

        except Exception as ex:
            error = ex

        if not error:
            self.info.setStyleSheet(success_green)
            self.info.setText(
                filter_type +
                " " +
                filter_band +
                " " +
                "filter successful!")
            orange_table = Orange.data.Table.from_numpy(
                self.data.domain, numpy.empty((len(self.data.Y), 0), dtype=float),
                self.data.Y, self.metas
            )

            self.send("Filtered data", orange_table)
        if error:
            self.info.setStyleSheet(error_red)
            self.info.setText("An error occurred:\n{}".format(error))
            return