{"action":"created","comment":{"url":"https://api.github.com/repos/scikit-image/scikit-image/pulls/comments/39940300","id":39940300,"diff_hunk":"@@ -51,6 +51,67 @@ def min_weight(graph, src, dst, n):\n     return min(w1, w2)\n \n \n+def mean_color(graph, extra_arguments=[], extra_keywords={}):\n+    \"\"\"Callback to compute edge weights.\n+\n+    The method expects that each node has already been described.\n+\n+    Parameters\n+    ----------\n+    graph : RAG\n+        The graph under consideration.\n+    extra_arguments : sequence, optional\n+        Allows extra positional arguments passed.\n+    extra_keywords : dictionary, optional\n+        Allows extra keyword arguments passed.\n+        sigma = 255\n+        mode = 'distance'\n+    \"\"\"\n+    sigma = extra_keywords['sigma']\n+    mode = extra_keywords['mode']\n+    for x, y, d in graph.edges_iter(data=True):\n+        diff = graph.node[x]['mean color'] - graph.node[y]['mean color']\n+        diff = np.linalg.norm(diff)\n+        if mode == 'similarity':\n+            d['weight'] = math.e ** (-(diff ** 2) / sigma)\n+        elif mode == 'distance':\n+            d['weight'] = diff\n+        else:\n+            raise ValueError(\"The mode '%s' is not recognised\" % mode)\n+\n+\n+def describe_node(graph,labels,image, *args, **kwargs):\n+    \"\"\"Callback to handle describing nodes.\n+\n+    Nodes can have arbitrary Python objects assigned as attributes. This method\n+    expects a valid graph and computes the mean color of the node.\n+\n+    Parameters\n+    ----------\n+    graph : RAG\n+        The graph under consideration.\n+    extra_arguments : sequence, optional\n+        Allows extra positional arguments passed.\n+    extra_keywords : dictionary, optional\n+        Allows extra keyword arguments passed.\n+\n+    \"\"\"\n+    for n in graph:\n+        graph.node[n].update({'labels': [n],\n+                              'pixel count': 0,\n+                              'total color': np.array([0, 0, 0],\n+                                                      dtype=np.double)})\n+\n+    for index in np.ndindex(labels.shape):\n+        current = labels[index]\n+        graph.node[current]['pixel count'] += 1\n+        graph.node[current]['total color'] += image[index]\n+\n+    for n in graph:\n+        graph.node[n]['mean color'] = (graph.node[n]['total color'] /","path":"skimage/future/graph/rag.py","position":61,"original_position":61,"commit_id":"d5b2741c11523c5c3845de2d2a5b1db1d824147e","original_commit_id":"d5b2741c11523c5c3845de2d2a5b1db1d824147e","user":{"login":"JDWarner","id":2184487,"avatar_url":"https://avatars.githubusercontent.com/u/2184487?v=3","gravatar_id":"","url":"https://api.github.com/users/JDWarner","html_url":"https://github.com/JDWarner","followers_url":"https://api.github.com/users/JDWarner/followers","following_url":"https://api.github.com/users/JDWarner/following{/other_user}","gists_url":"https://api.github.com/users/JDWarner/gists{/gist_id}","starred_url":"https://api.github.com/users/JDWarner/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JDWarner/subscriptions","organizations_url":"https://api.github.com/users/JDWarner/orgs","repos_url":"https://api.github.com/users/JDWarner/repos","events_url":"https://api.github.com/users/JDWarner/events{/privacy}","received_events_url":"https://api.github.com/users/JDWarner/received_events","type":"User","site_admin":false},"body":"Unless one of these values is a float, the `/` operator will be truncating division (i.e., `//`) under Python 2. They both look like ints to me, so that's probably not what you want - though I could be wrong.\r\n\r\nIf you do want truncating division, use `//`. If you want true division, then we should add `from __future__ import division` at the very top of this file which will normalize this behavior across Python major versions.","created_at":"2015-09-21T03:41:38Z","updated_at":"2015-09-21T03:41:38Z","html_url":"https://github.com/scikit-image/scikit-image/pull/1723#discussion_r39940300","pull_request_url":"https://api.github.com/repos/scikit-image/scikit-image/pulls/1723","_links":{"self":{"href":"https://api.github.com/repos/scikit-image/scikit-image/pulls/comments/39940300"},"html":{"href":"https://github.com/scikit-image/scikit-image/pull/1723#discussion_r39940300"},"pull_request":{"href":"https://api.github.com/repos/scikit-image/scikit-image/pulls/1723"}}},"pull_request":{"url":"https://api.github.com/repos/scikit-image/scikit-image/pulls/1723","id":45511801,"html_url":"https://github.com/scikit-image/scikit-image/pull/1723","diff_url":"https://github.com/scikit-image/scikit-image/pull/1723.diff","patch_url":"https://github.com/scikit-image/scikit-image/pull/1723.patch","issue_url":"https://api.github.com/repos/scikit-image/scikit-image/issues/1723","number":1723,"state":"open","locked":false,"title":"[WIP] RAG Merging","user":{"login":"michaelborck","id":12557414,"avatar_url":"https://avatars.githubusercontent.com/u/12557414?v=3","gravatar_id":"","url":"https://api.github.com/users/michaelborck","html_url":"https://github.com/michaelborck","followers_url":"https://api.github.com/users/michaelborck/followers","following_url":"https://api.github.com/users/michaelborck/following{/other_user}","gists_url":"https://api.github.com/users/michaelborck/gists{/gist_id}","starred_url":"https://api.github.com/users/michaelborck/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/michaelborck/subscriptions","organizations_url":"https://api.github.com/users/michaelborck/orgs","repos_url":"https://api.github.com/users/michaelborck/repos","events_url":"https://api.github.com/users/michaelborck/events{/privacy}","received_events_url":"https://api.github.com/users/michaelborck/received_events","type":"User","site_admin":false},"body":"If anyone think these modification are useful, let me know I don't mind to continue working on this, or just as happy for someone else can takeover.  (My first pull-request ever, so be gentle)\r\n\r\nrag.py\r\n---------\r\nAbstracted rag creation method to take two functions, one to describe the nodes (parameters: graph, image, labels), the other to calculate the edge weights(parameters: graph).   I provide two example methods which duplicate functionality of rag_mean_color().  Since rag_mean_color() has 'sigma' and 'mode' parameters have also use *args, and **kwargs to allow the additional parameters to be provided to the callbacks.  It is a little ugly but works.  \r\n\r\nNot sure if a generic version is needed.   Assuming I understand the NetworkX graph object.  The attributes of a node are a python dictionary.  As such attributes can be added by the user, not forced/required at creation time.  Similarly, edge weights can be updated/modified by iterating over the nodes and updating the 'weight' attribute.\r\n\r\ngraph_merge.py\r\n----------------------\r\nAdded a flag to merge_hierachical so as to include a trace of each merger.  After the merger of the two most similar nodes a label_map is reconstructed.  This is appended to a list.  In addition to the final  segmentation, the list of labeled maps is returned.  \r\n\r\nCurrently at each merger, I iterate over the nodes and build a complete label map of all regions.  Although this will allow a reconstruction of the merge process (is that useful to anyone?) I only need a simplified label map that make it easy to distinguish the new merger form the rest of the image.  I sure there is a better/more efficient way to create a simpler label map (that contains only the merger/node)","created_at":"2015-09-21T03:20:07Z","updated_at":"2015-09-21T03:41:38Z","closed_at":null,"merged_at":null,"merge_commit_sha":"3ab10fe791426662acf20020d01ea307a80d1068","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/scikit-image/scikit-image/pulls/1723/commits","review_comments_url":"https://api.github.com/repos/scikit-image/scikit-image/pulls/1723/comments","review_comment_url":"https://api.github.com/repos/scikit-image/scikit-image/pulls/comments{/number}","comments_url":"https://api.github.com/repos/scikit-image/scikit-image/issues/1723/comments","statuses_url":"https://api.github.com/repos/scikit-image/scikit-image/statuses/d5b2741c11523c5c3845de2d2a5b1db1d824147e","head":{"label":"michaelborck:master","ref":"master","sha":"d5b2741c11523c5c3845de2d2a5b1db1d824147e","user":{"login":"michaelborck","id":12557414,"avatar_url":"https://avatars.githubusercontent.com/u/12557414?v=3","gravatar_id":"","url":"https://api.github.com/users/michaelborck","html_url":"https://github.com/michaelborck","followers_url":"https://api.github.com/users/michaelborck/followers","following_url":"https://api.github.com/users/michaelborck/following{/other_user}","gists_url":"https://api.github.com/users/michaelborck/gists{/gist_id}","starred_url":"https://api.github.com/users/michaelborck/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/michaelborck/subscriptions","organizations_url":"https://api.github.com/users/michaelborck/orgs","repos_url":"https://api.github.com/users/michaelborck/repos","events_url":"https://api.github.com/users/michaelborck/events{/privacy}","received_events_url":"https://api.github.com/users/michaelborck/received_events","type":"User","site_admin":false},"repo":{"id":42660841,"name":"scikit-image","full_name":"michaelborck/scikit-image","owner":{"login":"michaelborck","id":12557414,"avatar_url":"https://avatars.githubusercontent.com/u/12557414?v=3","gravatar_id":"","url":"https://api.github.com/users/michaelborck","html_url":"https://github.com/michaelborck","followers_url":"https://api.github.com/users/michaelborck/followers","following_url":"https://api.github.com/users/michaelborck/following{/other_user}","gists_url":"https://api.github.com/users/michaelborck/gists{/gist_id}","starred_url":"https://api.github.com/users/michaelborck/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/michaelborck/subscriptions","organizations_url":"https://api.github.com/users/michaelborck/orgs","repos_url":"https://api.github.com/users/michaelborck/repos","events_url":"https://api.github.com/users/michaelborck/events{/privacy}","received_events_url":"https://api.github.com/users/michaelborck/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/michaelborck/scikit-image","description":"Image Processing SciKit (Toolbox for SciPy)","fork":true,"url":"https://api.github.com/repos/michaelborck/scikit-image","forks_url":"https://api.github.com/repos/michaelborck/scikit-image/forks","keys_url":"https://api.github.com/repos/michaelborck/scikit-image/keys{/key_id}","collaborators_url":"https://api.github.com/repos/michaelborck/scikit-image/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/michaelborck/scikit-image/teams","hooks_url":"https://api.github.com/repos/michaelborck/scikit-image/hooks","issue_events_url":"https://api.github.com/repos/michaelborck/scikit-image/issues/events{/number}","events_url":"https://api.github.com/repos/michaelborck/scikit-image/events","assignees_url":"https://api.github.com/repos/michaelborck/scikit-image/assignees{/user}","branches_url":"https://api.github.com/repos/michaelborck/scikit-image/branches{/branch}","tags_url":"https://api.github.com/repos/michaelborck/scikit-image/tags","blobs_url":"https://api.github.com/repos/michaelborck/scikit-image/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/michaelborck/scikit-image/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/michaelborck/scikit-image/git/refs{/sha}","trees_url":"https://api.github.com/repos/michaelborck/scikit-image/git/trees{/sha}","statuses_url":"https://api.github.com/repos/michaelborck/scikit-image/statuses/{sha}","languages_url":"https://api.github.com/repos/michaelborck/scikit-image/languages","stargazers_url":"https://api.github.com/repos/michaelborck/scikit-image/stargazers","contributors_url":"https://api.github.com/repos/michaelborck/scikit-image/contributors","subscribers_url":"https://api.github.com/repos/michaelborck/scikit-image/subscribers","subscription_url":"https://api.github.com/repos/michaelborck/scikit-image/subscription","commits_url":"https://api.github.com/repos/michaelborck/scikit-image/commits{/sha}","git_commits_url":"https://api.github.com/repos/michaelborck/scikit-image/git/commits{/sha}","comments_url":"https://api.github.com/repos/michaelborck/scikit-image/comments{/number}","issue_comment_url":"https://api.github.com/repos/michaelborck/scikit-image/issues/comments{/number}","contents_url":"https://api.github.com/repos/michaelborck/scikit-image/contents/{+path}","compare_url":"https://api.github.com/repos/michaelborck/scikit-image/compare/{base}...{head}","merges_url":"https://api.github.com/repos/michaelborck/scikit-image/merges","archive_url":"https://api.github.com/repos/michaelborck/scikit-image/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/michaelborck/scikit-image/downloads","issues_url":"https://api.github.com/repos/michaelborck/scikit-image/issues{/number}","pulls_url":"https://api.github.com/repos/michaelborck/scikit-image/pulls{/number}","milestones_url":"https://api.github.com/repos/michaelborck/scikit-image/milestones{/number}","notifications_url":"https://api.github.com/repos/michaelborck/scikit-image/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/michaelborck/scikit-image/labels{/name}","releases_url":"https://api.github.com/repos/michaelborck/scikit-image/releases{/id}","created_at":"2015-09-17T14:27:10Z","updated_at":"2015-09-17T14:27:12Z","pushed_at":"2015-09-21T00:48:57Z","git_url":"git://github.com/michaelborck/scikit-image.git","ssh_url":"git@github.com:michaelborck/scikit-image.git","clone_url":"https://github.com/michaelborck/scikit-image.git","svn_url":"https://github.com/michaelborck/scikit-image","homepage":"http://scikit-image.org","size":73785,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"scikit-image:master","ref":"master","sha":"4e21f5dcef4e49deb6faf421c03a3764034bf1b4","user":{"login":"scikit-image","id":897180,"avatar_url":"https://avatars.githubusercontent.com/u/897180?v=3","gravatar_id":"","url":"https://api.github.com/users/scikit-image","html_url":"https://github.com/scikit-image","followers_url":"https://api.github.com/users/scikit-image/followers","following_url":"https://api.github.com/users/scikit-image/following{/other_user}","gists_url":"https://api.github.com/users/scikit-image/gists{/gist_id}","starred_url":"https://api.github.com/users/scikit-image/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/scikit-image/subscriptions","organizations_url":"https://api.github.com/users/scikit-image/orgs","repos_url":"https://api.github.com/users/scikit-image/repos","events_url":"https://api.github.com/users/scikit-image/events{/privacy}","received_events_url":"https://api.github.com/users/scikit-image/received_events","type":"Organization","site_admin":false},"repo":{"id":2014929,"name":"scikit-image","full_name":"scikit-image/scikit-image","owner":{"login":"scikit-image","id":897180,"avatar_url":"https://avatars.githubusercontent.com/u/897180?v=3","gravatar_id":"","url":"https://api.github.com/users/scikit-image","html_url":"https://github.com/scikit-image","followers_url":"https://api.github.com/users/scikit-image/followers","following_url":"https://api.github.com/users/scikit-image/following{/other_user}","gists_url":"https://api.github.com/users/scikit-image/gists{/gist_id}","starred_url":"https://api.github.com/users/scikit-image/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/scikit-image/subscriptions","organizations_url":"https://api.github.com/users/scikit-image/orgs","repos_url":"https://api.github.com/users/scikit-image/repos","events_url":"https://api.github.com/users/scikit-image/events{/privacy}","received_events_url":"https://api.github.com/users/scikit-image/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/scikit-image/scikit-image","description":"Image Processing SciKit (Toolbox for SciPy)","fork":false,"url":"https://api.github.com/repos/scikit-image/scikit-image","forks_url":"https://api.github.com/repos/scikit-image/scikit-image/forks","keys_url":"https://api.github.com/repos/scikit-image/scikit-image/keys{/key_id}","collaborators_url":"https://api.github.com/repos/scikit-image/scikit-image/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/scikit-image/scikit-image/teams","hooks_url":"https://api.github.com/repos/scikit-image/scikit-image/hooks","issue_events_url":"https://api.github.com/repos/scikit-image/scikit-image/issues/events{/number}","events_url":"https://api.github.com/repos/scikit-image/scikit-image/events","assignees_url":"https://api.github.com/repos/scikit-image/scikit-image/assignees{/user}","branches_url":"https://api.github.com/repos/scikit-image/scikit-image/branches{/branch}","tags_url":"https://api.github.com/repos/scikit-image/scikit-image/tags","blobs_url":"https://api.github.com/repos/scikit-image/scikit-image/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/scikit-image/scikit-image/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/scikit-image/scikit-image/git/refs{/sha}","trees_url":"https://api.github.com/repos/scikit-image/scikit-image/git/trees{/sha}","statuses_url":"https://api.github.com/repos/scikit-image/scikit-image/statuses/{sha}","languages_url":"https://api.github.com/repos/scikit-image/scikit-image/languages","stargazers_url":"https://api.github.com/repos/scikit-image/scikit-image/stargazers","contributors_url":"https://api.github.com/repos/scikit-image/scikit-image/contributors","subscribers_url":"https://api.github.com/repos/scikit-image/scikit-image/subscribers","subscription_url":"https://api.github.com/repos/scikit-image/scikit-image/subscription","commits_url":"https://api.github.com/repos/scikit-image/scikit-image/commits{/sha}","git_commits_url":"https://api.github.com/repos/scikit-image/scikit-image/git/commits{/sha}","comments_url":"https://api.github.com/repos/scikit-image/scikit-image/comments{/number}","issue_comment_url":"https://api.github.com/repos/scikit-image/scikit-image/issues/comments{/number}","contents_url":"https://api.github.com/repos/scikit-image/scikit-image/contents/{+path}","compare_url":"https://api.github.com/repos/scikit-image/scikit-image/compare/{base}...{head}","merges_url":"https://api.github.com/repos/scikit-image/scikit-image/merges","archive_url":"https://api.github.com/repos/scikit-image/scikit-image/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/scikit-image/scikit-image/downloads","issues_url":"https://api.github.com/repos/scikit-image/scikit-image/issues{/number}","pulls_url":"https://api.github.com/repos/scikit-image/scikit-image/pulls{/number}","milestones_url":"https://api.github.com/repos/scikit-image/scikit-image/milestones{/number}","notifications_url":"https://api.github.com/repos/scikit-image/scikit-image/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/scikit-image/scikit-image/labels{/name}","releases_url":"https://api.github.com/repos/scikit-image/scikit-image/releases{/id}","created_at":"2011-07-07T22:07:20Z","updated_at":"2015-09-20T20:12:51Z","pushed_at":"2015-09-21T03:20:08Z","git_url":"git://github.com/scikit-image/scikit-image.git","ssh_url":"git@github.com:scikit-image/scikit-image.git","clone_url":"https://github.com/scikit-image/scikit-image.git","svn_url":"https://github.com/scikit-image/scikit-image","homepage":"http://scikit-image.org","size":73785,"stargazers_count":805,"watchers_count":805,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":424,"mirror_url":null,"open_issues_count":215,"forks":424,"open_issues":215,"watchers":805,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/scikit-image/scikit-image/pulls/1723"},"html":{"href":"https://github.com/scikit-image/scikit-image/pull/1723"},"issue":{"href":"https://api.github.com/repos/scikit-image/scikit-image/issues/1723"},"comments":{"href":"https://api.github.com/repos/scikit-image/scikit-image/issues/1723/comments"},"review_comments":{"href":"https://api.github.com/repos/scikit-image/scikit-image/pulls/1723/comments"},"review_comment":{"href":"https://api.github.com/repos/scikit-image/scikit-image/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/scikit-image/scikit-image/pulls/1723/commits"},"statuses":{"href":"https://api.github.com/repos/scikit-image/scikit-image/statuses/d5b2741c11523c5c3845de2d2a5b1db1d824147e"}}}}