Esempio n. 1
0
        def display():
            # pylint: disable=too-many-branches
            def format_zeros(str_val):
                """Zeros should be handled separately as they cannot be negative."""
                if float(str_val) == 0:
                    num_decimals = min(self.variables[row].number_of_decimals,
                                       2)
                    str_val = f"{0:.{num_decimals}f}"
                return str_val

            def render_value(value):
                if np.isnan(value):
                    return ""
                if np.isinf(value):
                    return "∞"

                str_val = attribute.str_val(value)
                if attribute.is_continuous and not attribute.is_time:
                    str_val = format_zeros(str_val)

                return str_val

            if column == self.Columns.NAME:
                return attribute.name
            elif column == self.Columns.DISTRIBUTION:
                if isinstance(attribute,
                              (DiscreteVariable, ContinuousVariable)):
                    if row not in self.__distributions_cache:
                        scene = QGraphicsScene(parent=self)
                        histogram = Histogram(
                            data=self.table,
                            variable=attribute,
                            color_attribute=self.target_var,
                            border=(0, 0, 2, 0),
                            border_color='#ccc',
                        )
                        scene.addItem(histogram)
                        self.__distributions_cache[row] = scene
                    return self.__distributions_cache[row]
            elif column == self.Columns.CENTER:
                return render_value(self._center[row])
            elif column == self.Columns.MEDIAN:
                return render_value(self._median[row])
            elif column == self.Columns.DISPERSION:
                if isinstance(attribute, TimeVariable):
                    return format_time_diff(self._min[row], self._max[row])
                elif isinstance(attribute, DiscreteVariable):
                    return "%.3g" % self._dispersion[row]
                else:
                    return render_value(self._dispersion[row])
            elif column == self.Columns.MIN:
                if not isinstance(attribute, DiscreteVariable):
                    return render_value(self._min[row])
            elif column == self.Columns.MAX:
                if not isinstance(attribute, DiscreteVariable):
                    return render_value(self._max[row])
            elif column == self.Columns.MISSING:
                return '%d (%d%%)' % (self._missing[row], 100 *
                                      self._missing[row] / self.n_instances)
            return None
        def display():
            # pylint: disable=too-many-branches
            def render_value(value):
                return "" if np.isnan(value) else attribute.str_val(value)

            if column == self.Columns.NAME:
                return attribute.name
            elif column == self.Columns.DISTRIBUTION:
                if isinstance(attribute,
                              (DiscreteVariable, ContinuousVariable)):
                    if row not in self.__distributions_cache:
                        scene = QGraphicsScene(parent=self)
                        histogram = Histogram(
                            data=self.table,
                            variable=attribute,
                            color_attribute=self.target_var,
                            border=(0, 0, 2, 0),
                            border_color='#ccc',
                        )
                        scene.addItem(histogram)
                        self.__distributions_cache[row] = scene
                    return self.__distributions_cache[row]
            elif column == self.Columns.CENTER:
                return render_value(self._center[row])
            elif column == self.Columns.DISPERSION:
                if isinstance(attribute, TimeVariable):
                    return format_time_diff(self._min[row], self._max[row])
                elif isinstance(attribute, DiscreteVariable):
                    return "%.3g" % self._dispersion[row]
                else:
                    return render_value(self._dispersion[row])
            elif column == self.Columns.MIN:
                if not isinstance(attribute, DiscreteVariable) \
                        or attribute.ordered:
                    return render_value(self._min[row])
            elif column == self.Columns.MAX:
                if not isinstance(attribute, DiscreteVariable) \
                        or attribute.ordered:
                    return render_value(self._max[row])
            elif column == self.Columns.MISSING:
                return '%d (%d%%)' % (
                    self._missing[row],
                    100 * self._missing[row] / self.n_instances
                )
            return None
Esempio n. 3
0
    def data(self, index, role):
        # type: (QModelIndex, Qt.ItemDataRole) -> Any
        # Text formatting for various data simply requires a lot of branches.
        # This is much better than overengineering various formatters...
        # pylint: disable=too-many-branches

        if not index.isValid():
            return None

        row, column = self.mapToSourceRows(index.row()), index.column()
        # Make sure we're not out of range
        if not 0 <= row <= self.n_attributes:
            return QVariant()

        attribute = self.variables[row]

        if role == Qt.BackgroundRole:
            if attribute in self.domain.attributes:
                return self.COLOR_FOR_ROLE[self.ATTRIBUTE]
            elif attribute in self.domain.metas:
                return self.COLOR_FOR_ROLE[self.META]
            elif attribute in self.domain.class_vars:
                return self.COLOR_FOR_ROLE[self.CLASS_VAR]

        elif role == Qt.TextAlignmentRole:
            if column == self.Columns.NAME:
                return Qt.AlignLeft | Qt.AlignVCenter
            return Qt.AlignRight | Qt.AlignVCenter

        output = None

        if column == self.Columns.ICON:
            if role == Qt.DecorationRole:
                return gui.attributeIconDict[attribute]
        elif column == self.Columns.NAME:
            if role == Qt.DisplayRole:
                output = attribute.name
        elif column == self.Columns.DISTRIBUTION:
            if role == Qt.DisplayRole:
                if isinstance(attribute,
                              (DiscreteVariable, ContinuousVariable)):
                    if row not in self.__distributions_cache:
                        scene = QGraphicsScene(parent=self)
                        histogram = Histogram(
                            data=self.table,
                            variable=attribute,
                            color_attribute=self.target_var,
                            border=(0, 0, 2, 0),
                            border_color='#ccc',
                        )
                        scene.addItem(histogram)
                        self.__distributions_cache[row] = scene
                    return self.__distributions_cache[row]
        elif column == self.Columns.CENTER:
            if role == Qt.DisplayRole:
                if isinstance(attribute, DiscreteVariable):
                    output = self._center[row]
                    if not np.isnan(output):
                        output = attribute.str_val(self._center[row])
                elif isinstance(attribute, TimeVariable):
                    output = attribute.str_val(self._center[row])
                else:
                    output = self._center[row]
        elif column == self.Columns.DISPERSION:
            if role == Qt.DisplayRole:
                if isinstance(attribute, TimeVariable):
                    output = format_time_diff(self._min[row], self._max[row])
                else:
                    output = self._dispersion[row]
        elif column == self.Columns.MIN:
            if role == Qt.DisplayRole:
                if isinstance(attribute, DiscreteVariable):
                    if attribute.ordered:
                        output = attribute.str_val(self._min[row])
                elif isinstance(attribute, TimeVariable):
                    output = attribute.str_val(self._min[row])
                else:
                    output = self._min[row]
        elif column == self.Columns.MAX:
            if role == Qt.DisplayRole:
                if isinstance(attribute, DiscreteVariable):
                    if attribute.ordered:
                        output = attribute.str_val(self._max[row])
                elif isinstance(attribute, TimeVariable):
                    output = attribute.str_val(self._max[row])
                else:
                    output = self._max[row]
        elif column == self.Columns.MISSING:
            if role == Qt.DisplayRole:
                output = '%d (%d%%)' % (self._missing[row], 100 *
                                        self._missing[row] / self.n_instances)

        # Consistently format the text inside the table cells
        # The easiest way to check for NaN is to compare with itself
        if output != output:  # pylint: disable=comparison-with-itself
            output = ''
        # Format ∞ properly
        elif output in (np.inf, -np.inf):
            output = '%s∞' % ['', '-'][output < 0]
        elif isinstance(output, int):
            output = locale.format_string('%d', output, grouping=True)
        elif isinstance(output, float):
            output = locale.format_string('%.2f', output, grouping=True)

        return output