-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcylinder_cluster.py
31 lines (28 loc) · 1.26 KB
/
cylinder_cluster.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
import numpy as np
from scipy.spatial import distance
from sklearn.cluster import DBSCAN
def cylinder_cluster(targets, eps_xy, eps_v, min_targets):
"""
Clustering targets by different metrics in space and speed.
Parameters
----------
targets: array, shape (n_samples, n_features)
eps_xy: float
The threshold in 2D ground plane distance to group targets together
eps_v: float
The threshold in speed distance to group targets together
min_targets: int
The minimum number of targets to formulate one cluster
"""
distance_xy = distance.cdist(np.reshape(targets[:,:2], (-1, 2)), np.reshape(targets[:, :2], (-1, 2)))
distance_v = distance.cdist(np.reshape(targets[:,2], (-1, 1)), np.reshape(targets[:, 2], (-1, 1)))
distance_xy_mask = distance_xy < eps_xy
if eps_v != 0:
distance_v_mask = distance_v < eps_v
else:
distance_v_mask = np.ones(distance_v.shape).astype(np.bool)
distance_mask = np.logical_and(distance_xy_mask, distance_v_mask)
distance_mask = distance_mask.astype(np.float)
distance_mask[distance_mask<1] = 1e4
clustering = DBSCAN(eps = 1.1, min_samples=min_targets, metric='precomputed').fit(distance_mask)
return clustering.labels_