/
spatial_knn.py
208 lines (166 loc) · 6.84 KB
/
spatial_knn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
from pyspark.sql import SparkSession, DataFrame, SQLContext
from mosaic.utils import scala_utils
class SpatialKNN:
"""
SpatialKNN is a distributed KNN model that uses a spatial index to reduce the number of candidate records to
consider for each query record. The model is built on top of the Spark DataFrame API and is designed to be
used in a distributed fashion. The model uses hex rings as the means to iterate through the spatial grid and
find the nearest neighbours. Delta tables are used to checkpoint the intermediate results of the model.
The number of iterations is controlled using max number of iterations and early stopping parameters.
"""
def __init__(self):
"""
Initialize the SpatialKNN model.
"""
self.spark = SparkSession.builder.getOrCreate()
self.model = getattr(
self.spark._jvm.com.databricks.labs.mosaic.models.knn, "SpatialKNN"
)()
def setUseTableCheckpoint(self, useTableCheckpoint):
"""
Set whether to use a Delta table to checkpoint the intermediate results of the model.
:param useTableCheckpoint:
True to use a Delta table (provided as a table name) to checkpoint the intermediate results of the model.
False to use a Delta table (provided as a file system path) to checkpoint the intermediate results of the model.
:return: SpatialKNN model with the param set to provided value.
"""
self.model.setUseTableCheckpoint(useTableCheckpoint)
return self
def setApproximate(self, approximate):
"""
Set whether to use approximate nearest neighbours or to use exact nearest neighbours finalisation.
:param approximate:
True to use approximate nearest neighbours.
False to use exact nearest neighbours finalisation.
:return: SpatialKNN model with the param set to provided value.
"""
self.model.setApproximate(approximate)
return self
def setLandmarksFeatureCol(self, feature):
"""
Set the feature column name for the landmarks.
:param feature: Feature column name for the landmarks.
:return: SpatialKNN model with the param set to provided value.
"""
self.model.setLandmarksFeatureCol(feature)
return self
def setLandmarksRowID(self, rowID):
"""
Set the row ID column name for the landmarks.
:param rowID: Row ID column name for the landmarks.
:return: SpatialKNN model with the param set to provided value.
"""
self.model.setLandmarksRowID(rowID)
return self
def setCandidatesFeatureCol(self, feature):
"""
Ser the feature column name for the candidates.
:param feature: Feature column name for the candidates.
:return: SpatialKNN model with the param set to provided value.
"""
self.model.setCandidatesFeatureCol(feature)
return self
def setCandidatesRowID(self, rowID):
"""
Set the row ID column name for the candidates.
:param rowID: Row ID column name for the candidates.
:return: SpatialKNN model with the param set to provided value.
"""
self.model.setCandidatesRowID(rowID)
return self
def setDistanceThreshold(self, d):
"""
Set the distance threshold for the nearest neighbours.
:param n: Distance threshold for the nearest neighbours.
:return: SpatialKNN model with the param set to provided value.
"""
self.model.setDistanceThreshold(d)
return self
def setIndexResolution(self, resolution):
"""
Set the index resolution for the spatial index.
:param resolution: Index resolution for the spatial index.
:return: SpatialKNN model with the param set to provided value.
"""
self.model.setIndexResolution(resolution)
return self
def setKNeighbours(self, k):
"""
Set the number of nearest neighbours to find.
:param k: Number of nearest neighbours to find.
:return: SpatialKNN model with the param set to provided value.
"""
self.model.setKNeighbours(k)
return self
def setMaxIterations(self, n):
"""
Set the maximum number of iterations to run the model.
:param n: Maximum number of iterations to run the model.
:return: SpatialKNN model with the param set to provided value.
"""
self.model.setMaxIterations(n)
return self
def setEarlyStopIterations(self, n):
"""
Set the number of iterations to run before early stopping.
:param n: Number of iterations to run before early stopping.
:return: SpatialKNN model with the param set to provided value.
"""
self.model.setEarlyStopIterations(n)
return self
def setCheckpointTablePrefix(self, prefix):
"""
Set the checkpoint table prefix for the model.
:param prefix: Checkpoint table prefix for the model.
:return: SpatialKNN model with the param set to provided value.
"""
self.model.setCheckpointTablePrefix(prefix)
return self
def setCandidatesDf(self, df):
"""
Set candidates DataFrame.
:param df: Candidates DataFrame.
:return: SpatialKNN model with the param set to provided value.
"""
self.model.setCandidatesDf(df._jdf)
return self
def transform(self, df):
"""
Transform the provided DataFrame using the model.
:param df: DataFrame to transform.
:return: Transformed DataFrame.
"""
result = self.model.transform(df._jdf)
return DataFrame(result, SQLContext(self.spark.sparkContext))
def getParams(self):
"""
Get the parameters of the model.
:return: Parameters of the model as a dict.
"""
params = self.model.getParams()
return scala_utils.scala_map_to_dict(params)
def getMetrics(self):
"""
Get the metrics of the model.
:return: Metrics of the model as a dict.
"""
metrics = self.model.getMetrics()
return scala_utils.scala_map_to_dict(metrics)
def write(self):
"""
return: a SpatialKNNWriter instance for this SpatialKNN instance.
To write out this SpatialKNN instance to persistent storage, use the save method
on the returned instance.
"""
return self.model.write
@staticmethod
def read(self):
"""
return: a SpatialKNNReader instance for this SpatialKNN instance.
To load this SpatialKNN instance from persistent storage, use the load method
on the returned instance.
"""
return self.model.read
def load(self, path):
""" """
return self.model.load(path)