-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add metrics for meshes #11
Draft
luisfpereira
wants to merge
4
commits into
main
Choose a base branch
from
metric
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0250894
Add metrics for meshes: EuclideanMetric and SingleSourceDijskstra (pr…
luisfpereira 63eea5a
Homogenize metric signatures
luisfpereira cf9fee5
Add fixed neighbors single source dijskstra
luisfpereira 02caf91
Update behavior of single source dijsktra when target is known
luisfpereira File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
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,272 @@ | ||
import abc | ||
|
||
import networkx as nx | ||
import numpy as np | ||
|
||
|
||
def to_nx_edge_graph(shape): | ||
# TODO: move to utils? circular imports | ||
vertex_a, vertex_b = shape.edges.T | ||
lengths = EuclideanMetric(shape).dist(vertex_a, vertex_b) | ||
|
||
weighted_edges = [ | ||
(int(vertex_a_), int(vertex_b_), length) | ||
for vertex_a_, vertex_b_, length in zip(vertex_a, vertex_b, lengths) | ||
] | ||
|
||
graph = nx.Graph() | ||
graph.add_weighted_edges_from(weighted_edges) | ||
|
||
return graph | ||
|
||
|
||
def _is_single_index(index): | ||
return isinstance(index, int) or index.ndim == 0 | ||
|
||
|
||
class BaseMetric(abc.ABC): | ||
# TODO: may need to do intermediate abstractions | ||
def __init__(self, shape): | ||
self._shape = shape | ||
|
||
@abc.abstractmethod | ||
def dist(self, point_a, point_b): | ||
"""Distance between points. | ||
|
||
Parameters | ||
---------- | ||
point_a : array-like, shape=[...] | ||
Point. | ||
point_b : array-like, shape=[...] | ||
Other point. | ||
|
||
Returns | ||
------- | ||
dist : array-like, shape=[...,] | ||
Distance. | ||
""" | ||
|
||
|
||
class EuclideanMetric(BaseMetric): | ||
def dist(self, point_a, point_b): | ||
"""Distance between mesh vertices. | ||
|
||
Parameters | ||
---------- | ||
point_a : array-like, shape=[...] | ||
Index of source point. | ||
point_b : array-like, shape=[...] | ||
Index of target point. | ||
|
||
Returns | ||
------- | ||
dist : array-like, shape=[...,] | ||
Distance. | ||
""" | ||
# TODO: do against all if target index is None? | ||
# TODO: add euclidean with cutoff | ||
vertices = self._shape.vertices | ||
diff = vertices[point_a] - vertices[point_b] | ||
return np.linalg.norm(diff, axis=diff.ndim - 1) | ||
|
||
|
||
class SingleSourceDijkstra(BaseMetric): | ||
"""Shortest path on edge graph of mesh with single source Dijkstra. | ||
|
||
Parameters | ||
---------- | ||
shape : Shape | ||
Shape. | ||
cutoff : float | ||
Length (sum of edge weights) at which the search is stopped. | ||
""" | ||
|
||
def __init__(self, shape, cutoff=None): | ||
self.cutoff = cutoff | ||
|
||
super().__init__(shape) | ||
self._graph = to_nx_edge_graph(shape) | ||
|
||
def _dist_no_target_single(self, source_point): | ||
"""Distance between mesh vertices. | ||
|
||
Parameters | ||
---------- | ||
source_point : array-like, shape=() | ||
Index of source point. | ||
|
||
Returns | ||
------- | ||
dist : array-like, shape=[n_targets] | ||
Distance. | ||
target_point : array-like, shape=[n_targets,] | ||
Target index. | ||
""" | ||
dist_dict = nx.single_source_dijkstra_path_length( | ||
self._graph, source_point, cutoff=self.cutoff, weight="weight" | ||
) | ||
return np.array(list(dist_dict.values())), np.array(list(dist_dict.keys())) | ||
|
||
def _dist_no_target(self, source_point): | ||
"""Distance between mesh vertices. | ||
|
||
Parameters | ||
---------- | ||
source_point : array-like, shape=[...] | ||
Index of source point. | ||
|
||
Returns | ||
------- | ||
dist : array-like, shape=[...,] or list[array-like] | ||
Distance. | ||
target_point : array-like, shape=[n_targets,] or list[array-like] | ||
Target index. | ||
""" | ||
if _is_single_index(source_point): | ||
return self._dist_no_target_single(source_point) | ||
|
||
out = [ | ||
self._dist_no_target_single(source_index_) for source_index_ in source_point | ||
] | ||
return list(zip(*out)) | ||
|
||
def _dist_target_single(self, point_a, point_b): | ||
"""Distance between mesh vertices. | ||
|
||
Parameters | ||
---------- | ||
point_a : array-like, shape=() | ||
Index of source point. | ||
point_b : array-like, shape=() | ||
Index of target point. | ||
|
||
Returns | ||
------- | ||
dist : numeric | ||
Distance. | ||
""" | ||
try: | ||
dist, _ = nx.single_source_dijkstra( | ||
self._graph, | ||
point_a, | ||
target=point_b, | ||
cutoff=None, | ||
weight="weight", | ||
) | ||
except nx.NetworkXNoPath: | ||
dist = np.inf | ||
return dist | ||
|
||
def _dist_target(self, point_a, point_b): | ||
"""Distance between mesh vertices. | ||
|
||
Parameters | ||
---------- | ||
point_a : array-like, shape=[...] | ||
Index of source point. | ||
point_b : array-like, shape=[...] | ||
Index of target point. | ||
|
||
Returns | ||
------- | ||
dist : array-like, shape=[...,] | ||
Distance. | ||
""" | ||
if _is_single_index(point_a) and _is_single_index(point_b): | ||
return self._dist_target_single(point_a, point_b) | ||
|
||
point_a, point_b = np.broadcast_arrays(point_a, point_b) | ||
return np.stack( | ||
[ | ||
self._dist_target_single(point_a_, point_b_) | ||
for point_a_, point_b_ in zip(point_a, point_b) | ||
] | ||
) | ||
|
||
def dist(self, point_a, point_b=None): | ||
"""Distance between mesh vertices. | ||
|
||
Parameters | ||
---------- | ||
point_a : array-like, shape=[...] | ||
Index of source point. | ||
point_b : array-like, shape=[...] | ||
Index of target point. | ||
|
||
Returns | ||
------- | ||
dist : array-like, shape=[...,] or list[array-like] | ||
Distance. | ||
point_b : array-like, shape=[n_targets,] or list[array-like] | ||
Target index. If `point_b` is None. | ||
""" | ||
if point_b is None: | ||
return self._dist_no_target(point_a) | ||
|
||
return self._dist_target(point_a, point_b) | ||
|
||
|
||
class FixedNeighborsSingleSourceDijkstra(SingleSourceDijkstra): | ||
"""Shortest path on edge graph of mesh with single source Dijkstra. | ||
|
||
Parameters | ||
---------- | ||
shape : Shape | ||
Shape. | ||
n_neighbors : int | ||
Number of neighbors to return when ``point_b is None``. | ||
neighbors_ratio : float | ||
Neighbors ratio to use to consider decreasing cuttoff. | ||
cutoff_decr_ratio : float | ||
Ratio to consider to proceed with cuttoff decrease. | ||
cutoff_incr_ratio : float | ||
Ratio use to update cutoff when not enough neighbors. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
shape, | ||
n_neighbors=5, | ||
neighbors_ratio=5, | ||
cutoff_decr_ratio=0.9, | ||
cutoff_incr_ratio=2.0, | ||
): | ||
super().__init__(shape, cutoff=self._initial_cuttoff(shape, n_neighbors)) | ||
|
||
self.n_neighbors = n_neighbors | ||
self.neighbors_ratio = neighbors_ratio | ||
self.cutoff_decr_ratio = cutoff_decr_ratio | ||
self.cutoff_incr_ratio = cutoff_incr_ratio | ||
|
||
@staticmethod | ||
def _initial_cuttoff(shape, n_neighbors): | ||
index_a, index_b = np.random.randint(0, high=shape.n_vertices, size=2) | ||
dist = EuclideanMetric(shape).dist(index_a, index_b) | ||
|
||
ratio = np.pow(n_neighbors / shape.n_vertices, 1 / 2.2) | ||
return ratio * dist | ||
|
||
def _dist_no_target_single(self, source_point): | ||
"""Distance between mesh vertices. | ||
|
||
Parameters | ||
---------- | ||
source_point : array-like, shape=() | ||
Index of source point. | ||
|
||
Returns | ||
------- | ||
dist : array-like, shape=[n_neighbors] | ||
Distance. | ||
target_point : array-like, shape=[n_neighors,] | ||
Target index. | ||
""" | ||
while True: | ||
dist, target = super()._dist_no_target_single(source_point) | ||
if target.size > self.n_neighbors: | ||
if target.size > round(self.n_neighbors * self.neighbors_ratio): | ||
self.cutoff *= self.cutoff_decr_ratio | ||
|
||
return dist[: self.n_neighbors], target[: self.n_neighbors] | ||
|
||
self.cutoff *= self.cutoff_incr_ratio |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
edge_graph
instead? maybe not, to keep code within class flexible.