From 0419c1e6f7d0af99d38ff0a1a02fab4dd9f51655 Mon Sep 17 00:00:00 2001 From: Vighnesh Birodkar Date: Thu, 16 Oct 2014 23:22:56 +0530 Subject: [PATCH] sequential numbering of nodes using max id --- skimage/graph/rag.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/skimage/graph/rag.py b/skimage/graph/rag.py index a300c486..1fd1ddc9 100644 --- a/skimage/graph/rag.py +++ b/skimage/graph/rag.py @@ -15,7 +15,6 @@ from scipy.ndimage import filters from scipy import ndimage as nd import math from .. import draw, measure, segmentation, util, color -import random try: from matplotlib import colors from matplotlib import cm @@ -60,6 +59,13 @@ class RAG(nx.Graph): `networx.Graph `_ """ + def __init__(self, data=None, **attr): + + nx.Graph.__init__(self, data, **attr) + self.max_id = None + for n in self.nodes_iter(): + self.max_id = max(self.max_id, n) + def merge_nodes(self, src, dst, weight_func=min_weight, in_place=True, extra_arguments=[], extra_keywords={}): """Merge node `src` and `dst`. @@ -100,10 +106,7 @@ class RAG(nx.Graph): 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) + new = self.max_id + 1 self.add_node(new) for neighbor in neighbors: @@ -118,6 +121,19 @@ class RAG(nx.Graph): self.remove_node(dst) return new + def add_node(self, n, attr_dict=None, **attr): + nx.Graph.add_node(self, n, attr_dict, **attr) + self.max_id = max(n, self.max_id) + + def add_edge(self, u, v, attr_dict=None, **attr): + nx.Graph.add_edge(self, u, v, attr_dict, **attr) + self.max_id = max(u, v, self.max_id) + + def copy(self): + g = nx.Graph.copy(self) + g.max_id = self.max_id + return g + def _add_edge_filter(values, graph): """Create edge in `g` between the first element of `values` and the rest.