-
Notifications
You must be signed in to change notification settings - Fork 695
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add dbscan scala * add dbscan python * add dbscan tests, pom file changes, pip changes * disable broadcast joins for all dbscan tests * disable non-sedona broadcast joins for all dbscan tests * unpersist dbscan result assuming that graphframes PR will eventually be merged. * revisions from Paweł * add documentation * styling in docs Co-authored-by: Kelly-Ann Dolor <[email protected]> * reword stats documentation Co-authored-by: Kelly-Ann Dolor <[email protected]> * clean up --------- Co-authored-by: jameswillis <[email protected]> Co-authored-by: Kelly-Ann Dolor <[email protected]>
- Loading branch information
1 parent
05245fa
commit f83915e
Showing
14 changed files
with
657 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
## Overview | ||
|
||
Sedona's stats module provides Scala and Python functions for conducting geospatial | ||
statistical analysis on dataframes with spatial columns. | ||
The stats module is built on top of the core module and provides a set of functions | ||
that can be used to perform spatial analysis on these dataframes. The stats module | ||
is designed to be used with the core module and the viz module to provide a | ||
complete set of geospatial analysis tools. | ||
|
||
## Using DBSCAN | ||
|
||
The DBSCAN function is provided at `org.apache.sedona.stats.DBSCAN.dbscan` in scala/java and `sedona.stats.dbscan.dbscan` in python. | ||
|
||
The function annotates a dataframe with a cluster label for each data record using the DBSCAN algorithm. | ||
The dataframe should contain at least one `GeometryType` column. Rows must be unique. If one | ||
geometry column is present it will be used automatically. If two are present, the one named | ||
'geometry' will be used. If more than one are present and none are named 'geometry', the | ||
column name must be provided. The new column will be named 'cluster'. | ||
|
||
### Parameters | ||
|
||
names in parentheses are python variable names | ||
|
||
- dataframe - dataframe to cluster. Must contain at least one GeometryType column | ||
- epsilon - minimum distance parameter of DBSCAN algorithm | ||
- minPts (min_pts) - minimum number of points parameter of DBSCAN algorithm | ||
- geometry - name of the geometry column | ||
- includeOutliers (include_outliers) - whether to include outliers in the output. Default is false | ||
- useSpheroid (use_spheroid) - whether to use a cartesian or spheroidal distance calculation. Default is false | ||
|
||
The output is the input DataFrame with the cluster label added to each row. Outlier will have a cluster value of -1 if included. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,8 @@ jupyter="*" | |
mkdocs="*" | ||
pytest-cov = "*" | ||
|
||
scikit-learn = "*" | ||
|
||
[packages] | ||
pandas="<=1.5.3" | ||
numpy="<2" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# 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. | ||
|
||
"""The clustering module contains spark based implementations of popular geospatial clustering algorithms. | ||
These implementations are designed to scale to larger datasets and support various geometric feature types. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# 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. | ||
|
||
"""DBSCAN is a popular clustering algorithm for spatial data. | ||
It identifies groups of data where enough records are close enough to each other. This implementation leverages spark, | ||
sedona and graphframes to support large scale datasets and various, heterogeneous geometric feature types. | ||
""" | ||
from typing import Optional | ||
|
||
from pyspark.sql import DataFrame, SparkSession | ||
|
||
ID_COLUMN_NAME = "__id" | ||
DEFAULT_MAX_SAMPLE_SIZE = 1000000 # 1 million | ||
|
||
|
||
def dbscan( | ||
dataframe: DataFrame, | ||
epsilon: float, | ||
min_pts: int, | ||
geometry: Optional[str] = None, | ||
include_outliers: bool = True, | ||
use_spheroid=False, | ||
): | ||
"""Annotates a dataframe with a cluster label for each data record using the DBSCAN algorithm. | ||
The dataframe should contain at least one GeometryType column. Rows must be unique. If one geometry column is | ||
present it will be used automatically. If two are present, the one named 'geometry' will be used. If more than one | ||
are present and neither is named 'geometry', the column name must be provided. | ||
Args: | ||
dataframe: spark dataframe containing the geometries | ||
epsilon: minimum distance parameter of DBSCAN algorithm | ||
min_pts: minimum number of points parameter of DBSCAN algorithm | ||
geometry: name of the geometry column | ||
include_outliers: whether to return outlier points. If True, outliers are returned with a cluster value of -1. | ||
Default is False | ||
use_spheroid: whether to use a cartesian or spheroidal distance calculation. Default is false | ||
Returns: | ||
A PySpark DataFrame containing the cluster label for each row | ||
""" | ||
sedona = SparkSession.getActiveSession() | ||
|
||
result_df = sedona._jvm.org.apache.sedona.stats.clustering.DBSCAN.dbscan( | ||
dataframe._jdf, | ||
float(epsilon), | ||
min_pts, | ||
geometry, | ||
include_outliers, | ||
use_spheroid, | ||
) | ||
|
||
return DataFrame(result_df, sedona) |
Empty file.
Oops, something went wrong.