Imbruced commented on code in PR #1589:
URL: https://github.com/apache/sedona/pull/1589#discussion_r1775974765


##########
python/tests/stats/test_dbscan.py:
##########
@@ -0,0 +1,252 @@
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing,
+#  software distributed under the License is distributed on an
+#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  KIND, either express or implied.  See the License for the
+#  specific language governing permissions and limitations
+#  under the License.
+
+import pyspark.sql.functions as f
+
+from sedona.sql.st_constructors import ST_MakePoint
+from sedona.sql.st_functions import ST_Buffer
+from sklearn.cluster import DBSCAN as sklearnDBSCAN
+from sedona.stats.clustering.dbscan import dbscan
+
+from tests.test_base import TestBase
+
+
+class TestDBScan(TestBase):
+
+    def get_data(self):
+        return [
+            {"id": 1, "x": 1.0, "y": 2.0},
+            {"id": 2, "x": 3.0, "y": 4.0},
+            {"id": 3, "x": 2.5, "y": 4.0},
+            {"id": 4, "x": 1.5, "y": 2.5},
+            {"id": 5, "x": 3.0, "y": 5.0},
+            {"id": 6, "x": 12.8, "y": 4.5},
+            {"id": 7, "x": 2.5, "y": 4.5},
+            {"id": 8, "x": 1.2, "y": 2.5},
+            {"id": 9, "x": 1.0, "y": 3.0},
+            {"id": 10, "x": 1.0, "y": 5.0},
+            {"id": 11, "x": 1.0, "y": 2.5},
+            {"id": 12, "x": 5.0, "y": 6.0},
+            {"id": 13, "x": 4.0, "y": 3.0},
+        ]
+
+    def create_sample_dataframe(self):
+        return self.spark.createDataFrame(self.get_data()).select(
+            ST_MakePoint("x", "y").alias("arealandmark"), "id"
+        ).repartition(9)
+
+    def get_expected_result(self, input_data, epsilon, min_pts, 
include_outliers=True):
+        labels = (
+            sklearnDBSCAN(eps=epsilon, min_samples=min_pts)
+            .fit([[datum["x"], datum["y"]] for datum in input_data])
+            .labels_
+        )
+        expected = [(x[0] + 1, x[1]) for x in list(enumerate(labels))]
+        clusters = [x for x in set(labels) if (x != -1 or include_outliers)]
+        cluster_members = {
+            frozenset([y[0] for y in expected if y[1] == x]) for x in clusters
+        }
+        return cluster_members
+
+    def get_actual_results(
+            self,
+            input_data,
+            epsilon,
+            min_pts,
+            geometry=None,
+            id=None,
+            include_outliers=True,
+    ):
+        result = dbscan(
+            input_data, epsilon, min_pts, geometry, 
include_outliers=include_outliers
+        )
+        id = id or "id"
+        clusters_members = [
+            (x[id], x.cluster)
+            for x in result.collect()
+            if x.cluster != -1 or include_outliers
+        ]
+
+        result.unpersist()
+
+        clusters = {
+            frozenset([y[0] for y in clusters_members if y[1] == x])
+            for x in set([y[1] for y in clusters_members])
+        }
+
+        return clusters
+
+    def test_dbscan_valid_parameters(self):

Review Comment:
   I think it might be a good use case for parametrized tests 
   https://docs.pytest.org/en/7.1.x/how-to/parametrize.html
   
   I mean you can try to create cases based on those loops 
         for epsilon in [0.6, 0.7, 0.8]:
               for min_pts in [3, 4, 5]:



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to