diff --git a/doc/examples/plot_rag.py b/doc/examples/plot_rag.py index 9dd3ca76..c3192ca2 100644 --- a/doc/examples/plot_rag.py +++ b/doc/examples/plot_rag.py @@ -4,10 +4,10 @@ Region Adjacency Graphs ======================= This example demonstrates the use of the `merge_nodes` function of a Region -Adjacency Graph (RAG).The `RAG` class represents a undirected weighted graph -which inherits from `networkx.graph` class. When a new node is formed by merging -two nodes, the edge weight of all the edges incident on the resulting node can -be updated by a user defined function `weight_func`. +Adjacency Graph (RAG). The `RAG` class represents a undirected weighted graph +which inherits from `networkx.graph` class. When a new node is formed by +merging two nodes, the edge weight of all the edges incident on the resulting +node can be updated by a user defined function `weight_func`. The default behaviour is to use the smaller edge weight in case of a conflict. The example below also shows how to use a custom function to select the larger diff --git a/skimage/graph/rag.py b/skimage/graph/rag.py index 7aa65261..807e8cd3 100644 --- a/skimage/graph/rag.py +++ b/skimage/graph/rag.py @@ -28,8 +28,9 @@ def min_weight(graph, src, dst, n): """ # cover the cases where n only has edge to either `src` or `dst` - w1 = graph[n].get(src, {'weight': np.inf})['weight'] - w2 = graph[n].get(dst, {'weight': np.inf})['weight'] + default = {'weight': np.inf} + w1 = graph[n].get(src, default)['weight'] + w2 = graph[n].get(dst, default)['weight'] return min(w1, w2) @@ -55,7 +56,9 @@ class RAG(nx.Graph): 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, - **extra_keywords)` + **extra_keywords)`. `src`, `dst` and `n` are IDs of vertices in a + .. py:class:: RAG object which is in turn a subclass of + `networkx.Graph`. extra_arguments : sequence, optional The sequence of extra positional arguments passed to `weight_func`. @@ -138,7 +141,7 @@ def rag_mean_color(image, labels, connectivity=2): >>> from skimage import data, graph, segmentation >>> img = data.lena() >>> labels = segmentation.slic(img) - >>> rag = graph.rag_meancolor(img, labels) + >>> rag = graph.rag_mean_color(img, labels) References ---------- diff --git a/skimage/graph/tests/test_rag.py b/skimage/graph/tests/test_rag.py index 7b4551f4..af7481eb 100644 --- a/skimage/graph/tests/test_rag.py +++ b/skimage/graph/tests/test_rag.py @@ -3,8 +3,9 @@ from skimage import graph def max_edge(g, src, dst, n): - w1 = g[n].get(src, {'weight': -np.inf})['weight'] - w2 = g[n].get(dst, {'weight': -np.inf})['weight'] + default = {'weight': -np.inf} + w1 = g[n].get(src, default)['weight'] + w2 = g[n].get(dst, default)['weight'] return max(w1, w2)