From dd67b3fce7eacdc8f97cb7dfa9c7fd51a2b00214 Mon Sep 17 00:00:00 2001 From: Vighnesh Birodkar Date: Sat, 28 Jun 2014 11:06:30 +0530 Subject: [PATCH] Changed max_edge in example/plot_rag.py --- doc/examples/plot_rag.py | 24 ++++++------------------ skimage/graph/rag.py | 2 +- skimage/graph/tests/test_rag.py | 4 ++++ 3 files changed, 11 insertions(+), 19 deletions(-) diff --git a/doc/examples/plot_rag.py b/doc/examples/plot_rag.py index d0ed3089..203f9454 100644 --- a/doc/examples/plot_rag.py +++ b/doc/examples/plot_rag.py @@ -9,32 +9,20 @@ weight of all the edges incident on it can be updated by a user defined function `weight_func`. The default behaviour is to use the smaller edge weight incase of a conflict. -THe example below also shows how to use a custom function to take the larger +The example below also shows how to use a custom function to take the larger weight instead. """ from skimage.graph import rag import networkx as nx from matplotlib import pyplot as plt +import numpy as np -def max_edge(g, src, dst, neighbor): - try: - w1 = g.edge[src][neighbor]['weight'] - except KeyError: - w1 = None - - try: - w2 = g.edge[dst][neighbor]['weight'] - except KeyError: - w2 = None - - if w1 is None: - return w2 - elif w2 is None: - return w1 - else: - return max(w1, w2) +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'] + return max(w1, w2) def display(g, title): diff --git a/skimage/graph/rag.py b/skimage/graph/rag.py index 9150bfc4..69eed104 100644 --- a/skimage/graph/rag.py +++ b/skimage/graph/rag.py @@ -17,7 +17,7 @@ def min_weight(g, src, dst, n): src, dst : int The verices in `g` to be merged. n : int - A neighbor of `src` or `dst` or both + A neighbor of `src` or `dst` or both. Returns ------- diff --git a/skimage/graph/tests/test_rag.py b/skimage/graph/tests/test_rag.py index 8179b92f..37c31887 100644 --- a/skimage/graph/tests/test_rag.py +++ b/skimage/graph/tests/test_rag.py @@ -23,10 +23,14 @@ def test_rag_merge(): gc = g.copy() + # We merge nodes and ensure that the minimum weight is chosen + # when there is a conflict. g.merge_nodes(0, 2) assert g.edge[1][2]['weight'] == 10 assert g.edge[2][3]['weight'] == 30 + # We specify `max_edge` as `weight_func` as ensure that maximum + # weight is chosen in case on conflict gc.merge_nodes(0, 2, weight_func=max_edge) assert gc.edge[1][2]['weight'] == 20 assert gc.edge[2][3]['weight'] == 40