def test_cluster_additional_points_concave(self):
        """
        Test with the concave hull
        """
        hull = {
            "C1":
            np.array([[0.0, 0.0], [0.3, 0.5], [0.0, 1.0], [1.0, 1.0],
                      [0.7, 0.5], [1.0, 0.0]])
        }
        p = [
            [-1.0, -1],  # point that is totally out polygon
            [0.2, 0.5],  # point outside the polygon, but close
            [0.8, 0.5],  # point outside the polygon, but close
            [0.5, 0.5],  # point in
            [0.5, 0.7],  # point in
            [0.5, 0.3],  # point in
            [0.2, 0.9],  # point in
            [0.2, 0.1],  # point in
        ]
        points = Table(
            Domain([ContinuousVariable("x"),
                    ContinuousVariable("y")]), p)

        clusters = cluster_additional_points(points, hull)
        expected = [np.nan] * 3 + [0.0] * 5
        np.testing.assert_array_equal(clusters.X.flatten(), expected)
Exemplo n.º 2
0
 def compute_secondary_clusters(embedding: Table, result: Result, state: TaskState):
     if not result.clusters.groups or not embedding:
         result.clusters.secondary_table = None
     else:
         state.set_status("Finding secondary clusters...")
         hulls = {k: v[2] for k, v in result.clusters.groups.items()}
         clusters = result.clusters.table
         domain = clusters and clusters.domain["Clusters"]
         table = cluster_additional_points(embedding, hulls, domain)
         result.clusters.secondary_table = table
     state.set_partial_result(("secondary_clusters", result))
    def test_cluster_additional_points_more_clusters(self):
        """
        Test with more concave hulls
        """

        points_table = Table(
            Domain([ContinuousVariable("x"),
                    ContinuousVariable("y")]), self.points)

        clusters = cluster_additional_points(points_table, self.hull)
        clusters = list(
            map(clusters.domain.attributes[0].repr_val, clusters.X[:, 0]))
        expected = ["?"] * 3 + ["C1"] * 5 + ["C2"] * 2 + ["?"] * 2
        np.testing.assert_array_equal(clusters, expected)
    def test_cluster_additional_points_with_existing_domain(self):
        """
        Test with the concave hull
        """
        points = Table(
            Domain([ContinuousVariable("x"),
                    ContinuousVariable("y")]), self.points)
        attr = DiscreteVariable("Clusters", values=["C2", "C1"])

        clusters = cluster_additional_points(points,
                                             self.hull,
                                             cluster_attribute=attr)
        cluster_map = list(
            map(clusters.domain.attributes[0].repr_val, clusters.X[:, 0]))
        expected = ["?"] * 3 + ["C1"] * 5 + ["C2"] * 2 + ["?"] * 2
        np.testing.assert_array_equal(cluster_map, expected)
        self.assertEqual(attr, clusters.domain.attributes[0])
        self.assertListEqual(attr.values, clusters.domain.attributes[0].values)
    def test_cluster_additional_points_simple(self):
        """
        Test with the simple convex hull
        """
        hull = {
            "C1": np.array([[0.0, 0.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0]])
        }
        x, y = np.meshgrid(np.arange(-1.0, 2.0, 0.2), np.arange(-1, 2.0, 0.2))
        points = Table(
            Domain([ContinuousVariable("x"),
                    ContinuousVariable("y")]),
            np.concatenate((x.reshape(-1, 1), y.reshape(-1, 1)), axis=1),
        )

        clusters = cluster_additional_points(points, hull)
        expected = [
            0.0 if 0 < x < 1 and 0 < y < 1 else np.nan for x, y in points.X
        ]
        np.testing.assert_array_equal(clusters.X.flatten(), expected)