-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #728 from frheault/multi_centro_labels_map
Multi centro labels map
- Loading branch information
Showing
3 changed files
with
122 additions
and
177 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 |
---|---|---|
@@ -1,70 +1,25 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import numpy as np | ||
import tempfile | ||
import os | ||
from scipy.spatial import KDTree | ||
|
||
|
||
def min_dist_to_centroid(bundle_pts, centroid_pts): | ||
nb_bundle_points = len(bundle_pts) | ||
nb_centroid_points = len(centroid_pts) | ||
total_len = nb_bundle_points*nb_centroid_points | ||
def min_dist_to_centroid(bundle_pts, centroid_pts, nb_pts): | ||
tree = KDTree(centroid_pts, copy_data=True) | ||
dists, labels = tree.query(bundle_pts, k=1) | ||
dists, labels = np.expand_dims( | ||
dists, axis=1), np.expand_dims(labels, axis=1) | ||
|
||
# bundle_points will be shaped like | ||
# [[bundle_pt1], ⸣ | ||
# [bundle_pt1], ⸠ → Repeated # of centroid points time | ||
# [bundle_pt1], ⸥ | ||
# ... | ||
# [bundle_ptN], | ||
# [bundle_ptN], | ||
# [bundle_ptN]] | ||
with tempfile.TemporaryDirectory() as tmp_path: | ||
bundle_points = np.memmap(os.path.join(tmp_path, 'bundle_points'), | ||
dtype='float16', mode='w+', | ||
shape=(total_len, 3)) | ||
bundle_points[:] = np.repeat(bundle_pts, | ||
nb_centroid_points, | ||
axis=0) | ||
labels = np.mod(labels, nb_pts) | ||
|
||
# centroid_points will be shaped like | ||
# [[centroid_pt1], ⸣ | ||
# [centroid_pt2], | | ||
# ... ⸠ → Repeated # of points in bundle times | ||
# [centroid_pt20], ⸥ | ||
# [centroid_pt1], | ||
# [centroid_pt2], | ||
# ... | ||
# [centroid_pt20]] | ||
centroid_points = np.memmap(os.path.join(tmp_path, 'centroid_points'), | ||
dtype='float16', mode='w+', | ||
shape=(total_len, 3)) | ||
centroid_points[:] = np.tile(centroid_pts, (nb_bundle_points, 1)) | ||
sum_dist = np.expand_dims(np.sum(dists, axis=1), axis=1) | ||
weights = np.exp(-dists / sum_dist) | ||
|
||
# norm will be shaped like | ||
# [[bundle_pt1 - centroid_pt1], | ||
# [bundle_pt1 - centroid_pt2], | ||
# [bundle_pt1 - centroid_pt3], | ||
# ... | ||
# [bundle_ptN - centroid_pt1]] | ||
# [bundle_ptN - centroid_pt2]] | ||
# ... | ||
# [bundle_ptN - centroid_pt20]] | ||
norm = np.memmap(os.path.join(tmp_path, 'norm'), | ||
dtype='float16', mode='w+', | ||
shape=(total_len,)) | ||
norm[:] = np.linalg.norm(bundle_points - centroid_points, axis=1) | ||
votes = [] | ||
for i in range(len(bundle_pts)): | ||
vote = np.bincount(labels[i], weights=weights[i]) | ||
total = np.arange(np.amax(labels[i])+1) | ||
winner = total[np.argmax(vote)] | ||
votes.append(winner) | ||
|
||
# Reshape so we have the distance to each centroid for each | ||
# bundle point | ||
dist_to_centroid = np.memmap(os.path.join(tmp_path, 'dist_to_centroid'), | ||
dtype='float16', mode='w+', | ||
shape=(nb_bundle_points, nb_centroid_points)) | ||
dist_to_centroid[:] = norm.reshape(nb_bundle_points, | ||
nb_centroid_points) | ||
|
||
# Find the closest centroid (label and distance) for each point of the | ||
# bundle | ||
min_dist_label = np.argmin(dist_to_centroid, axis=1) | ||
min_dist = np.amin(dist_to_centroid, axis=1) | ||
|
||
return min_dist_label, min_dist | ||
return np.array(votes, dtype=np.uint16), np.average(dists, axis=1) |
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
Oops, something went wrong.