Merge pull request #2058 from vighneshbirodkar/hmerge_boundary

[WIP] Hierarchical Merging of Region Boundary RAGs
This commit is contained in:
Juan Nunez-Iglesias
2016-05-16 13:09:54 +10:00
5 changed files with 113 additions and 27 deletions
+16 -14
View File
@@ -50,8 +50,9 @@ def _edge_generator_from_csr(csr_matrix):
def min_weight(graph, src, dst, n):
"""Callback to handle merging nodes by choosing minimum weight.
Returns either the weight between (`src`, `n`) or (`dst`, `n`)
in `graph` or the minimum of the two when both exist.
Returns a dictionary with `"weight"` set as either the weight between
(`src`, `n`) or (`dst`, `n`) in `graph` or the minimum of the two when
both exist.
Parameters
----------
@@ -64,9 +65,10 @@ def min_weight(graph, src, dst, n):
Returns
-------
weight : float
The weight between (`src`, `n`) or (`dst`, `n`) in `graph` or the
minimum of the two when both exist.
data : dict
A dict with the `"weight"` attribute set the weight between
(`src`, `n`) or (`dst`, `n`) in `graph` or the minimum of the two when
both exist.
"""
@@ -74,7 +76,7 @@ def min_weight(graph, src, dst, n):
default = {'weight': np.inf}
w1 = graph[n].get(src, default)['weight']
w2 = graph[n].get(dst, default)['weight']
return min(w1, w2)
return {'weight': min(w1, w2)}
def _add_edge_filter(values, graph):
@@ -171,12 +173,12 @@ class RAG(nx.Graph):
src, dst : int
Nodes to be merged.
weight_func : callable, optional
Function to decide edge weight of edges incident on the new node.
For each neighbor `n` for `src and `dst`, `weight_func` will be
called as follows: `weight_func(src, dst, n, *extra_arguments,
Function to decide the attributes of edges incident on the new
node. For each neighbor `n` for `src and `dst`, `weight_func` will
be called as follows: `weight_func(src, dst, n, *extra_arguments,
**extra_keywords)`. `src`, `dst` and `n` are IDs of vertices in the
RAG object which is in turn a subclass of
`networkx.Graph`.
RAG object which is in turn a subclass of `networkx.Graph`. It is
expected to return a dict of attributes of the resulting edge.
in_place : bool, optional
If set to `True`, the merged node has the id `dst`, else merged
node has a new id which is returned.
@@ -207,9 +209,9 @@ class RAG(nx.Graph):
self.add_node(new)
for neighbor in neighbors:
w = weight_func(self, src, new, neighbor, *extra_arguments,
**extra_keywords)
self.add_edge(neighbor, new, weight=w)
data = weight_func(self, src, new, neighbor, *extra_arguments,
**extra_keywords)
self.add_edge(neighbor, new, attr_dict=data)
self.node[new]['labels'] = (self.node[src]['labels'] +
self.node[dst]['labels'])
+2 -2
View File
@@ -10,7 +10,7 @@ def max_edge(g, src, dst, n):
default = {'weight': -np.inf}
w1 = g[n].get(src, default)['weight']
w2 = g[n].get(dst, default)['weight']
return max(w1, w2)
return {'weight': max(w1, w2)}
@skipif(not is_installed('networkx'))
@@ -113,7 +113,7 @@ def test_rag_error():
def _weight_mean_color(graph, src, dst, n):
diff = graph.node[dst]['mean color'] - graph.node[n]['mean color']
diff = np.linalg.norm(diff)
return diff
return {'weight': diff}
def _pre_merge_mean_color(graph, src, dst):