Example #1
0
 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)
Example #2
0
    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()')
Example #3
0
    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()')
Example #4
0
 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)
Example #5
0
 def _repopulate_wordcloud(self, words, weights):
     N_BEST = 200
     words, weights = words[:N_BEST], weights[:N_BEST]
     # Repopulate table
     self.tablemodel.wrap(list(zip(weights, words)))
     self.tableview.sortByColumn(0, Qt.DescendingOrder)
     # Reset wordcloud
     weights = np.clip(weights, *np.percentile(weights, [2, 98]))
     weights = scale(weights, 8, 40)
     self.wordlist = np.c_[words, weights].tolist()
     self.on_cloud_pref_change()
Example #6
0
 def _repopulate_wordcloud(self, words, weights):
     N_BEST = 200
     words, weights = words[:N_BEST], weights[:N_BEST]
     # Repopulate table
     self.tablemodel.wrap(list(zip(weights, words)))
     self.tableview.sortByColumn(0, Qt.DescendingOrder)
     # Reset wordcloud
     weights = np.clip(weights, *np.percentile(weights, [2, 98]))
     weights = scale(weights, 8, 40)
     self.wordlist = np.c_[words, weights].tolist()
     self.on_cloud_pref_change()
Example #7
0
    def recompute_heatmap(self, points):
        if self.model is None or not self.data or not self.lat_attr or not self.lon_attr:
            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()
        predictions = scale(np.round(predictions, 7))  # Avoid small errors
        self.exposeObject('model_predictions', dict(data=predictions))
        self.evalJS('draw_heatmap()')
Example #8
0
    def _repopulate_wordcloud(
        self, words: List[str], weights: List[float]
    ) -> None:
        """
        This function prepare a word list and trigger a cloud replot.

        Parameters
        ----------
        words
            List of words to show.
        weights
            Words' weights
        """
        def is_whole(d):
            """Whether or not d is a whole number."""
            return (
                    isinstance(d, int)
                    or (isinstance(d, float) and d.is_integer())
            )

        words, weights = words[:N_BEST_PLOTTED], weights[:N_BEST_PLOTTED]
        self.shown_words, self.shown_weights = words, weights
        # Repopulate table
        self.tablemodel.set_precision(
            0 if all(is_whole(w) for w in weights) else 2
        )
        self.tablemodel.wrap(list(zip(weights, words)))
        self.tableview.sortByColumn(0, Qt.DescendingOrder)

        # Reset wordcloud
        if self.topic is not None:
            # when weights are from topic negative weights should be treated
            # as positive when calculating the font size
            weights = np.abs(weights)
        weights = np.clip(weights, *np.percentile(weights, [2, 98]))
        weights = scale(weights, 10, 40)
        self.wordlist = np.c_[words, weights].tolist()

        # sometimes words are longer in average and word sizes in pt are bigger
        # in average - with this parameter we combine this in size scaling
        self.combined_size_length = sum([
            len(word) * float(weight) for word, weight in
            self.wordlist
        ])
        self.on_cloud_pref_change()
Example #9
0
 def set_marker_size(self, attr, update=True):
     try:
         self._size_attr = variable = self.data.domain[attr]
     except Exception:
         self._size_attr = None
         self._legend_sizes = []
     else:
         assert variable.is_continuous
         self._raw_sizes = values = self.data.get_column_view(variable)[0]
         # Note, [5, 60] is also hardcoded in legend-size-indicator.svg
         self._sizes = scale(values, 5, 60).astype(np.uint8)
         min = np.nanmin(values)
         self._legend_sizes = self._legend_values(
             variable,
             [min, np.nanmax(values)]) if not np.isnan(min) else []
     finally:
         if update:
             self.redraw_markers_overlay_image(new_image=True)
Example #10
0
 def set_marker_size(self, attr, update=True):
     try:
         self._size_attr = variable = self.data.domain[attr]
         if len(self.data) == 0:
             raise Exception
     except Exception:
         self._size_attr = None
         self._legend_sizes = []
     else:
         assert variable.is_continuous
         self._raw_sizes = values = self.data.get_column_view(variable)[0].astype(float)
         # Note, [5, 60] is also hardcoded in legend-size-indicator.svg
         self._sizes = scale(values, 5, 60).astype(np.uint8)
         min = np.nanmin(values)
         self._legend_sizes = self._legend_values(variable,
                                                  [min, np.nanmax(values)]) if not np.isnan(min) else []
     finally:
         if update:
             self.redraw_markers_overlay_image(new_image=True)
Example #11
0
 def test_scale(self):
     np.testing.assert_equal(scale([0, 1, 2], -1, 1), [-1, 0, 1])
     np.testing.assert_equal(scale([3, 3, 3]), [1, 1, 1])
     np.testing.assert_equal(scale([.1, .5, np.nan]), [0, 1, np.nan])
Example #12
0
 def test_scale(self):
     np.testing.assert_equal(scale([0, 1, 2], -1, 1), [-1, 0, 1])
     np.testing.assert_equal(scale([3, 3, 3]), [1, 1, 1])
     np.testing.assert_equal(scale([.1, .5, np.nan]), [0, 1, np.nan])
 def test_scale(self):
     np.testing.assert_equal(scale([0, 1, 2], -1, 1), [-1, 0, 1])
     np.testing.assert_equal(scale([3, 3, 3]), [1, 1, 1])
     np.testing.assert_equal(scale([0.1, 0.5, np.nan]), [0, 1, np.nan])
     np.testing.assert_equal(scale(np.array([])), np.array([]))