def replot_series(self):
        """
        This function replot just series connected with centroids and
        uses animation for that
        """
        km = self.k_means
        k = km.k

        series = []
        # plot lines between centroids and points
        if self.lines_to_centroids:
            for i, (c, pts) in enumerate(zip(
                    km.centroids, km.centroids_belonging_points)):
                series.append(dict(
                   data=list(
                       chain.from_iterable(([p[0], p[1]], [c[0], c[1]])
                                           for p in pts)),
                   type="line",
                   showInLegend=False,
                   lineWidth=0.2,
                   enableMouseTracking=False,
                   color="#ccc"))

        # plot data points
        for i, points in enumerate(km.centroids_belonging_points):
            series.append(dict(
                data=points,
                type="scatter",
                showInLegend=False,
                color=rgb_hash_brighter(
                    self.colors[i % len(self.colors)], 0.5)))

        self.scatter.add_series(series)

        self.scatter.remove_last_series(k * 2 if self.lines_to_centroids else k)
Example #2
0
    def complete_replot(self):
        """
        This function performs complete replot of the graph without animation
        """
        try:
            attr_x = self.data.domain[self.attr_x]
            attr_y = self.data.domain[self.attr_y]
        except KeyError:
            return

        # plot centroids
        options = dict(series=[])
        n_colors = len(self.colors)
        km = self.k_means
        options['series'].append(
            dict(data=[{
                'x': p[0],
                'y': p[1],
                'marker': {
                    'fillColor': self.colors[i % n_colors]
                }
            } for i, p in enumerate(km.centroids)],
                 type="scatter",
                 draggableX=True,
                 draggableY=True,
                 cursor="move",
                 zIndex=10,
                 marker=dict(symbol='square', radius=8)))

        # plot lines between centroids and points
        if self.lines_to_centroids:
            for i, (c, pts) in enumerate(
                    zip(km.centroids, km.centroids_belonging_points)):
                options['series'].append(
                    dict(data=list(
                        chain.from_iterable(
                            ([p[0], p[1]], [c[0], c[1]]) for p in pts)),
                         type="line",
                         lineWidth=0.2,
                         enableMouseTracking=False,
                         color="#ccc"))

        # plot data points
        for i, points in enumerate(km.centroids_belonging_points):
            options['series'].append(
                dict(data=points,
                     type="scatter",
                     color=rgb_hash_brighter(self.colors[i % len(self.colors)],
                                             0.3)))

        # highcharts parameters
        kwargs = dict(
            xAxis_title_text=attr_x.name,
            yAxis_title_text=attr_y.name,
            tooltip_headerFormat="",
            tooltip_pointFormat="<strong>%s:</strong> {point.x:.2f} <br/>"
            "<strong>%s:</strong> {point.y:.2f}" % (self.attr_x, self.attr_y))

        # plot
        self.scatter.chart(options, **kwargs)
    def test_rgb_hash_brighter(self):
        # calculated manually
        matches = [["#1F7ECA", "#56a5e5"], ["#CCCC33", "#dbdb70"],
                   ["#993322", "#d55a46"], ["#663366", "#ab58ab"]]

        for hash1, hash2 in matches:
            self.assertEqual(
                rgb_hash_brighter(hash1, 0.3).lower(), hash2.lower())
    def complete_replot(self):
        """
        This function performs complete replot of the graph without animation
        """
        try:
            attr_x = self.data.domain[self.attr_x]
            attr_y = self.data.domain[self.attr_y]
        except KeyError:
            return

        # plot centroids
        options = dict(series=[])
        n_colors = len(self.colors)
        km = self.k_means
        options['series'].append(
            dict(
                data=[{'x': p[0], 'y': p[1],
                       'marker':{'fillColor': self.colors[i % n_colors]}}
                      for i, p in enumerate(km.centroids)],
                type="scatter",
                draggableX=True,
                draggableY=True,
                cursor="move",
                zIndex=10,
                marker=dict(symbol='square', radius=8)))

        # plot lines between centroids and points
        if self.lines_to_centroids:
            for i, (c, pts) in enumerate(zip(
                    km.centroids, km.centroids_belonging_points)):
                options['series'].append(dict(
                    data=list(
                        chain.from_iterable(([p[0], p[1]], [c[0], c[1]])
                                            for p in pts)),
                    type="line",
                    lineWidth=0.2,
                    enableMouseTracking=False,
                    color="#ccc"))

        # plot data points
        for i, points in enumerate(km.centroids_belonging_points):
            options['series'].append(dict(
                data=points,
                type="scatter",
                color=rgb_hash_brighter(
                    self.colors[i % len(self.colors)], 0.3)))

        # highcharts parameters
        kwargs = dict(
            xAxis_title_text=attr_x.name,
            yAxis_title_text=attr_y.name,
            tooltip_headerFormat="",
            tooltip_pointFormat="<strong>%s:</strong> {point.x:.2f} <br/>"
                                "<strong>%s:</strong> {point.y:.2f}" %
                                (self.attr_x, self.attr_y))

        # plot
        self.scatter.chart(options, **kwargs)
    def test_rgb_hash_brighter(self):
        # calculated manually
        matches = [["#1F7ECA", "#56a5e5"],
                   ["#CCCC33", "#dbdb70"],
                   ["#993322", "#d55a46"],
                   ["#663366", "#ab58ab"]]

        for hash1, hash2 in matches:
            self.assertEqual(
                rgb_hash_brighter(hash1, 0.3).lower(), hash2.lower())
Example #6
0
    def complete_replot(self):
        """
        This function performs complete replot of the graph without animation
        """
        attr_x, attr_y = self.data.domain[self.attr_x], self.data.domain[self.attr_y]

        # plot centroids
        options = dict(series=[])
        options['series'].append(dict(data=[{'x': p[0],
                                             'y': p[1],
                                             'marker':{'fillColor': self.colors[i % len(self.colors)]}}
                                            for i, p in enumerate(self.k_means.centroids)],
                                      type="scatter",
                                      draggableX=True if self.k_means.step_completed else False,
                                      draggableY=True if self.k_means.step_completed else False,
                                      showInLegend=False,
                                      zIndex=10,
                                      marker=dict(symbol='diamond',
                                                  radius=10)))

        # plot lines between centroids and points
        if self.lines_to_centroids:
            for i, c in enumerate(self.k_means.centroids):
                options['series'].append(dict(data=list(
                    chain.from_iterable(([p[0], p[1]], [c[0], c[1]])
                                        for p in self.k_means.centroids_belonging_points[i])),
                                              type="line",
                                              showInLegend=False,
                                              lineWidth=0.2,
                                              enableMouseTracking=False,
                                              color="#ccc"))

        # plot data points
        for i, points in enumerate(self.k_means.centroids_belonging_points):
            options['series'].append(dict(data=points,
                                          type="scatter",
                                          showInLegend=False,
                                          color=rgb_hash_brighter(self.colors[i % len(self.colors)], 30)))

        # highcharts parameters
        kwargs = dict(
            xAxis_title_text=attr_x.name,
            yAxis_title_text=attr_y.name,
            tooltip_headerFormat="",
            tooltip_pointFormat="<strong>%s:</strong> {point.x:.2f} <br/>"
                                "<strong>%s:</strong> {point.y:.2f}" %
                                (self.attr_x, self.attr_y))

        # plot
        self.scatter.chart(options, **kwargs)
    def replot(self):
        """
        This function performs complete replot of the graph
        """
        if self.data is None or self.selected_data is None:
            self.set_empty_plot()
            return

        attr_x = self.data.domain[self.attr_x]
        attr_y = self.data.domain[self.attr_y]
        data_x = [v[0] for v in self.data[:, attr_x] if not isnan(v[0])]
        data_y = [v[0] for v in self.data[:, attr_y] if not isnan(v[0])]
        min_x = min(data_x)
        max_x = max(data_x)
        min_y = min(data_y)
        max_y = max(data_y)
        # just in cas that diff is 0
        diff_x = (max_x - min_x) if abs(max_x - min_x) > 0.001 else 0.1
        diff_y = (max_y - min_y) if abs(max_y - min_y) > 0.001 else 0.1
        min_x, max_x = min_x - 0.03 * diff_x, max_x + 0.03 * diff_x
        min_y, max_y = min_y - 0.03 * diff_y, max_y + 0.03 * diff_y

        options = dict(series=[])

        # gradient and contour
        options['series'] += self.plot_gradient_and_contour(
            min_x, max_x, min_y, max_y)

        sd = self.selected_data
        # data points
        options['series'] += [
            dict(
                data=[list(p.attributes())
                      for p in sd
                      if (int(p.metas[0]) == _class and
                          all(v is not None for v in p.attributes()))],
                type="scatter",
                zIndex=10,
                color=rgb_to_hex(tuple(
                    sd.domain.metas[0].colors[_class].tolist())),
                showInLegend=True,
                name=sd.domain.metas[0].values[_class])
            for _class in range(len(sd.domain.metas[0].values))]

        cls_domain = sd.domain.metas[0]

        target_idx = cls_domain.values.index(self.target_class)
        target_color = tuple(cls_domain.colors[target_idx].tolist())
        other_color = (tuple(cls_domain.colors[(target_idx + 1) % 2].tolist())
                       if len(cls_domain.values) == 2 else (170, 170, 170))

        # highcharts parameters
        kwargs = dict(
            xAxis_title_text=attr_x.name,
            yAxis_title_text=attr_y.name,
            xAxis_min=min_x,
            xAxis_max=max_x,
            yAxis_min=min_y,
            yAxis_max=max_y,
            colorAxis=dict(
                labels=dict(enabled=False),
                stops=[
                    [0, rgb_hash_brighter(rgb_to_hex(other_color), 0.5)],
                    [0.5, '#ffffff'],
                    [1, rgb_hash_brighter(rgb_to_hex(target_color), 0.5)]],
                tickInterval=0.2, min=0, max=1),
            plotOptions_contour_colsize=(max_y - min_y) / 1000,
            plotOptions_contour_rowsize=(max_x - min_x) / 1000,
            legend=dict(
                enabled=self.legend_enabled,
                layout='vertical',
                align='right',
                verticalAlign='top',
                floating=True,
                backgroundColor='rgba(255, 255, 255, 0.3)',
                symbolWidth=0,
                symbolHeight=0),
            tooltip_headerFormat="",
            tooltip_pointFormat="<strong>%s:</strong> {point.x:.2f} <br/>"
                                "<strong>%s:</strong> {point.y:.2f}" %
                                (self.attr_x, self.attr_y))

        self.scatter.chart(options, **kwargs)
        self.plot_contour()
Example #8
0
    def replot(self):
        """
        This function performs complete replot of the graph
        """
        if self.data is None or self.selected_data is None:
            self.set_empty_plot()
            return

        attr_x = self.data.domain[self.attr_x]
        attr_y = self.data.domain[self.attr_y]
        data_x = [v[0] for v in self.data[:, attr_x] if not isnan(v[0])]
        data_y = [v[0] for v in self.data[:, attr_y] if not isnan(v[0])]
        min_x = min(data_x)
        max_x = max(data_x)
        min_y = min(data_y)
        max_y = max(data_y)
        # just in cas that diff is 0
        diff_x = (max_x - min_x) if abs(max_x - min_x) > 0.001 else 0.1
        diff_y = (max_y - min_y) if abs(max_y - min_y) > 0.001 else 0.1
        min_x, max_x = min_x - 0.03 * diff_x, max_x + 0.03 * diff_x
        min_y, max_y = min_y - 0.03 * diff_y, max_y + 0.03 * diff_y

        options = dict(series=[])

        # gradient and contour
        options['series'] += self.plot_gradient_and_contour(
            min_x, max_x, min_y, max_y)

        sd = self.selected_data
        # data points
        options['series'] += [
            dict(data=[
                list(p.attributes()) for p in sd
                if (p.metas[0] == _class and all(v is not None
                                                 for v in p.attributes()))
            ],
                 type="scatter",
                 zIndex=10,
                 color=rgb_to_hex(
                     tuple(sd.domain.metas[0].colors[_class].tolist())),
                 showInLegend=True,
                 name=sd.domain.metas[0].values[_class])
            for _class in range(len(sd.domain.metas[0].values))
        ]

        # add nan values as a gray dots
        options['series'] += [
            dict(data=[
                list(p.attributes()) for p in sd if np.isnan(p.metas[0])
            ],
                 type="scatter",
                 zIndex=10,
                 color=rgb_to_hex((160, 160, 160)),
                 showInLegend=False)
        ]

        cls_domain = sd.domain.metas[0]

        target_idx = cls_domain.values.index(self.target_class)
        target_color = tuple(cls_domain.colors[target_idx].tolist())
        other_color = (tuple(cls_domain.colors[(target_idx + 1) % 2].tolist())
                       if len(cls_domain.values) == 2 else (170, 170, 170))

        # highcharts parameters
        kwargs = dict(
            xAxis_title_text=attr_x.name,
            yAxis_title_text=attr_y.name,
            xAxis_min=min_x,
            xAxis_max=max_x,
            yAxis_min=min_y,
            yAxis_max=max_y,
            colorAxis=dict(
                labels=dict(enabled=False),
                stops=[[0, rgb_hash_brighter(rgb_to_hex(other_color), 0.5)],
                       [0.5, '#ffffff'],
                       [1, rgb_hash_brighter(rgb_to_hex(target_color), 0.5)]],
                tickInterval=0.2,
                min=0,
                max=1),
            plotOptions_contour_colsize=(max_y - min_y) / 1000,
            plotOptions_contour_rowsize=(max_x - min_x) / 1000,
            legend=dict(enabled=self.legend_enabled,
                        layout='vertical',
                        align='right',
                        verticalAlign='top',
                        floating=True,
                        backgroundColor='rgba(255, 255, 255, 0.3)',
                        symbolWidth=0,
                        symbolHeight=0),
            tooltip_headerFormat="",
            tooltip_pointFormat="<strong>%s:</strong> {point.x:.2f} <br/>"
            "<strong>%s:</strong> {point.y:.2f}" % (self.attr_x, self.attr_y))

        self.scatter.chart(options, **kwargs)
        self.plot_contour()