From 4f91a4ded3468a2a5bd3c8f3bb9c7145c5b4ea2a Mon Sep 17 00:00:00 2001 From: Vighnesh Birodkar Date: Sun, 5 Oct 2014 17:03:56 +0530 Subject: [PATCH] cleanup of if else block --- skimage/graph/rag.py | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/skimage/graph/rag.py b/skimage/graph/rag.py index 5820250c..a300c486 100644 --- a/skimage/graph/rag.py +++ b/skimage/graph/rag.py @@ -79,7 +79,7 @@ class RAG(nx.Graph): **extra_keywords)`. `src`, `dst` and `n` are IDs of vertices in the RAG object which is in turn a subclass of `networkx.Graph`. - in_place : bool + 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. extra_arguments : sequence, optional @@ -95,30 +95,26 @@ class RAG(nx.Graph): """ src_nbrs = set(self.neighbors(src)) dst_nbrs = set(self.neighbors(dst)) - neighbors = (src_nbrs & dst_nbrs) - set([src, dst]) - n = self.number_of_nodes() - if not in_place: - # Randomly select an id + neighbors = (src_nbrs | dst_nbrs) - set([src, dst]) + + if in_place: + new = dst + else: + n = self.number_of_nodes() new = random.randint(1, 2*n) while new in self: new = random.randint(1, 2*n) self.add_node(new) for neighbor in neighbors: - w = weight_func(self, src, dst, neighbor, *extra_arguments, + w = weight_func(self, src, new, neighbor, *extra_arguments, **extra_keywords) - if in_place: - self.add_edge(neighbor, dst, weight=w) - else: - self.add_edge(neighbor, new, weight=w) + self.add_edge(neighbor, new, weight=w) - if in_place: - self.node[dst]['labels'] += self.node[src]['labels'] - self.remove_node(src) - else: - self.node[new]['labels'] = (self.node[src]['labels'] + - self.node[dst]['labels']) - self.remove_node(src) + self.node[new]['labels'] = (self.node[src]['labels'] + + self.node[dst]['labels']) + self.remove_node(src) + if not in_place: self.remove_node(dst) return new