{"action":"created","comment":{"url":"https://api.github.com/repos/networkx/networkx/pulls/comments/30942423","id":30942423,"diff_hunk":"@@ -0,0 +1,128 @@\n+from networkx.algorithms.community import louvain\n+import networkx as nx\n+\n+def convert_dictionary(dictionary):\n+\t'''\n+\tConverts dictionary with nodes as keys to modules as keys\n+\n+\t------\n+\tInputs\n+\t------\n+\tdictionary =  dictionary output from Louvain community detection\n+\n+\t------\n+\tOutput\n+\t------\n+\tDictionary with modules as keys, nodes as values\n+\n+\t'''\n+\n+\tnew_dict = {}\n+\tfor m,n in zip(dictionary.values(),dictionary.keys()):\n+\t    try:\n+\t        new_dict[m].append(n)\n+\t    except KeyError:\n+\t        new_dict[m] = [n]\n+\treturn new_dict\n+\n+def get_within_module_edges(module, graph):\n+\tmodule_edges = []\n+\tfor edge in graph.edges():\n+\t\tif edge[0] in module and edge[1] in module:\n+\t\t\tmodule_edges.append(edge)\n+\treturn module_edges\n+\n+\n+def within_module_degree(graph, partition, weighted = False):\n+    '''\n+    Computes the within-module degree for each node (Guimera et al. 2005)\n+\n+    ------\n+    Inputs\n+    ------\n+    graph = Networkx Graph\n+   \tpartition = dictionary from modularity partition of graph using Louvain method\n+    where the keys are nodes and values are module numbers\n+    weighted: Boolean\n+\n+    ------\n+    Output\n+    ------\n+    Dictionary of the within-module degree of each node.\n+\n+    '''\n+    graph = graph.to_undirected()\n+    partition = convert_dictionary(partition)\n+    wmd_dict = {}\n+    for module in partition.keys():\n+        module = partition[module]\n+        module_wd_dict = {}\n+        module_edges = get_within_module_edges(module,graph)\n+        for source in module:\n+            edge_count = 0\n+            for target in module:\n+                if (source,target) in module_edges or (target,source) in module_edges:\n+                    if weighted:\n+                        edge_count += graph.get_edge_data(source,target)['weight']\n+                        edge_count += graph.get_edge_data(target,source)['weight']\n+                    else:\n+                        edge_count += 1\n+            module_wd_dict[source] = edge_count\n+        module_avg_wmd = float(sum(module_wd_dict.values()) / len(module_wd_dict))\n+        stds = []\n+        for value in module_wd_dict.values():\n+            stds.append((module_avg_wmd - value)**2)","path":"networkx/algorithms/community/nodal_roles_nx.py","position":74,"original_position":74,"commit_id":"992a202a8fd53f75dd9da73b039795392879fdd7","original_commit_id":"992a202a8fd53f75dd9da73b039795392879fdd7","user":{"login":"jfinkels","id":121755,"avatar_url":"https://avatars.githubusercontent.com/u/121755?v=3","gravatar_id":"","url":"https://api.github.com/users/jfinkels","html_url":"https://github.com/jfinkels","followers_url":"https://api.github.com/users/jfinkels/followers","following_url":"https://api.github.com/users/jfinkels/following{/other_user}","gists_url":"https://api.github.com/users/jfinkels/gists{/gist_id}","starred_url":"https://api.github.com/users/jfinkels/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jfinkels/subscriptions","organizations_url":"https://api.github.com/users/jfinkels/orgs","repos_url":"https://api.github.com/users/jfinkels/repos","events_url":"https://api.github.com/users/jfinkels/events{/privacy}","received_events_url":"https://api.github.com/users/jfinkels/received_events","type":"User","site_admin":false},"body":"Simpler: `stds = [(module_avg_wmd - value) ** 2 for value in module_wd_dict.values()]`.","created_at":"2015-05-23T06:17:58Z","updated_at":"2015-05-23T06:17:58Z","html_url":"https://github.com/networkx/networkx/pull/1090#discussion_r30942423","pull_request_url":"https://api.github.com/repos/networkx/networkx/pulls/1090","_links":{"self":{"href":"https://api.github.com/repos/networkx/networkx/pulls/comments/30942423"},"html":{"href":"https://github.com/networkx/networkx/pull/1090#discussion_r30942423"},"pull_request":{"href":"https://api.github.com/repos/networkx/networkx/pulls/1090"}}},"pull_request":{"url":"https://api.github.com/repos/networkx/networkx/pulls/1090","id":14194631,"html_url":"https://github.com/networkx/networkx/pull/1090","diff_url":"https://github.com/networkx/networkx/pull/1090.diff","patch_url":"https://github.com/networkx/networkx/pull/1090.patch","issue_url":"https://api.github.com/repos/networkx/networkx/issues/1090","number":1090,"state":"open","locked":false,"title":"two nodal role functions, louvain","user":{"login":"mb3152","id":5448277,"avatar_url":"https://avatars.githubusercontent.com/u/5448277?v=3","gravatar_id":"","url":"https://api.github.com/users/mb3152","html_url":"https://github.com/mb3152","followers_url":"https://api.github.com/users/mb3152/followers","following_url":"https://api.github.com/users/mb3152/following{/other_user}","gists_url":"https://api.github.com/users/mb3152/gists{/gist_id}","starred_url":"https://api.github.com/users/mb3152/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mb3152/subscriptions","organizations_url":"https://api.github.com/users/mb3152/orgs","repos_url":"https://api.github.com/users/mb3152/repos","events_url":"https://api.github.com/users/mb3152/events{/privacy}","received_events_url":"https://api.github.com/users/mb3152/received_events","type":"User","site_admin":false},"body":"I built two different versions of nodal role measures (participation coefficient, and within-module-degree). The nodal_roles_nx.py works with the published Louvain that has been discussed. The nodal_roles_brainx.py works with the brainx classes of weighted_modularity (again, Louvain). The brainx version is faster. Depending on how y'all decide to output the modularity partition results, either one will work, and I can write some tests then and improve the speed a bit. ","created_at":"2014-03-31T20:03:32Z","updated_at":"2015-05-23T06:17:58Z","closed_at":null,"merged_at":null,"merge_commit_sha":"93be9fbf0257adac7280e56906a2b609005e58d0","assignee":{"login":"hagberg","id":187875,"avatar_url":"https://avatars.githubusercontent.com/u/187875?v=3","gravatar_id":"","url":"https://api.github.com/users/hagberg","html_url":"https://github.com/hagberg","followers_url":"https://api.github.com/users/hagberg/followers","following_url":"https://api.github.com/users/hagberg/following{/other_user}","gists_url":"https://api.github.com/users/hagberg/gists{/gist_id}","starred_url":"https://api.github.com/users/hagberg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hagberg/subscriptions","organizations_url":"https://api.github.com/users/hagberg/orgs","repos_url":"https://api.github.com/users/hagberg/repos","events_url":"https://api.github.com/users/hagberg/events{/privacy}","received_events_url":"https://api.github.com/users/hagberg/received_events","type":"User","site_admin":false},"milestone":{"url":"https://api.github.com/repos/networkx/networkx/milestones/4","html_url":"https://github.com/networkx/networkx/milestones/networkx-future","labels_url":"https://api.github.com/repos/networkx/networkx/milestones/4/labels","id":236125,"number":4,"title":"networkx-future","description":"Features desired in future releases (unscheduled)","creator":{"login":"hagberg","id":187875,"avatar_url":"https://avatars.githubusercontent.com/u/187875?v=3","gravatar_id":"","url":"https://api.github.com/users/hagberg","html_url":"https://github.com/hagberg","followers_url":"https://api.github.com/users/hagberg/followers","following_url":"https://api.github.com/users/hagberg/following{/other_user}","gists_url":"https://api.github.com/users/hagberg/gists{/gist_id}","starred_url":"https://api.github.com/users/hagberg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hagberg/subscriptions","organizations_url":"https://api.github.com/users/hagberg/orgs","repos_url":"https://api.github.com/users/hagberg/repos","events_url":"https://api.github.com/users/hagberg/events{/privacy}","received_events_url":"https://api.github.com/users/hagberg/received_events","type":"User","site_admin":false},"open_issues":85,"closed_issues":12,"state":"open","created_at":"2012-12-29T15:43:52Z","updated_at":"2015-05-20T22:17:41Z","due_on":null,"closed_at":null},"commits_url":"https://api.github.com/repos/networkx/networkx/pulls/1090/commits","review_comments_url":"https://api.github.com/repos/networkx/networkx/pulls/1090/comments","review_comment_url":"https://api.github.com/repos/networkx/networkx/pulls/comments{/number}","comments_url":"https://api.github.com/repos/networkx/networkx/issues/1090/comments","statuses_url":"https://api.github.com/repos/networkx/networkx/statuses/992a202a8fd53f75dd9da73b039795392879fdd7","head":{"label":"mb3152:master","ref":"master","sha":"992a202a8fd53f75dd9da73b039795392879fdd7","user":{"login":"mb3152","id":5448277,"avatar_url":"https://avatars.githubusercontent.com/u/5448277?v=3","gravatar_id":"","url":"https://api.github.com/users/mb3152","html_url":"https://github.com/mb3152","followers_url":"https://api.github.com/users/mb3152/followers","following_url":"https://api.github.com/users/mb3152/following{/other_user}","gists_url":"https://api.github.com/users/mb3152/gists{/gist_id}","starred_url":"https://api.github.com/users/mb3152/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mb3152/subscriptions","organizations_url":"https://api.github.com/users/mb3152/orgs","repos_url":"https://api.github.com/users/mb3152/repos","events_url":"https://api.github.com/users/mb3152/events{/privacy}","received_events_url":"https://api.github.com/users/mb3152/received_events","type":"User","site_admin":false},"repo":{"id":17986843,"name":"networkx","full_name":"mb3152/networkx","owner":{"login":"mb3152","id":5448277,"avatar_url":"https://avatars.githubusercontent.com/u/5448277?v=3","gravatar_id":"","url":"https://api.github.com/users/mb3152","html_url":"https://github.com/mb3152","followers_url":"https://api.github.com/users/mb3152/followers","following_url":"https://api.github.com/users/mb3152/following{/other_user}","gists_url":"https://api.github.com/users/mb3152/gists{/gist_id}","starred_url":"https://api.github.com/users/mb3152/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mb3152/subscriptions","organizations_url":"https://api.github.com/users/mb3152/orgs","repos_url":"https://api.github.com/users/mb3152/repos","events_url":"https://api.github.com/users/mb3152/events{/privacy}","received_events_url":"https://api.github.com/users/mb3152/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/mb3152/networkx","description":"Official NetworkX source code repository.  ","fork":true,"url":"https://api.github.com/repos/mb3152/networkx","forks_url":"https://api.github.com/repos/mb3152/networkx/forks","keys_url":"https://api.github.com/repos/mb3152/networkx/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mb3152/networkx/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mb3152/networkx/teams","hooks_url":"https://api.github.com/repos/mb3152/networkx/hooks","issue_events_url":"https://api.github.com/repos/mb3152/networkx/issues/events{/number}","events_url":"https://api.github.com/repos/mb3152/networkx/events","assignees_url":"https://api.github.com/repos/mb3152/networkx/assignees{/user}","branches_url":"https://api.github.com/repos/mb3152/networkx/branches{/branch}","tags_url":"https://api.github.com/repos/mb3152/networkx/tags","blobs_url":"https://api.github.com/repos/mb3152/networkx/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mb3152/networkx/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mb3152/networkx/git/refs{/sha}","trees_url":"https://api.github.com/repos/mb3152/networkx/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mb3152/networkx/statuses/{sha}","languages_url":"https://api.github.com/repos/mb3152/networkx/languages","stargazers_url":"https://api.github.com/repos/mb3152/networkx/stargazers","contributors_url":"https://api.github.com/repos/mb3152/networkx/contributors","subscribers_url":"https://api.github.com/repos/mb3152/networkx/subscribers","subscription_url":"https://api.github.com/repos/mb3152/networkx/subscription","commits_url":"https://api.github.com/repos/mb3152/networkx/commits{/sha}","git_commits_url":"https://api.github.com/repos/mb3152/networkx/git/commits{/sha}","comments_url":"https://api.github.com/repos/mb3152/networkx/comments{/number}","issue_comment_url":"https://api.github.com/repos/mb3152/networkx/issues/comments{/number}","contents_url":"https://api.github.com/repos/mb3152/networkx/contents/{+path}","compare_url":"https://api.github.com/repos/mb3152/networkx/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mb3152/networkx/merges","archive_url":"https://api.github.com/repos/mb3152/networkx/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mb3152/networkx/downloads","issues_url":"https://api.github.com/repos/mb3152/networkx/issues{/number}","pulls_url":"https://api.github.com/repos/mb3152/networkx/pulls{/number}","milestones_url":"https://api.github.com/repos/mb3152/networkx/milestones{/number}","notifications_url":"https://api.github.com/repos/mb3152/networkx/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mb3152/networkx/labels{/name}","releases_url":"https://api.github.com/repos/mb3152/networkx/releases{/id}","created_at":"2014-03-21T16:47:04Z","updated_at":"2014-04-01T15:18:32Z","pushed_at":"2014-04-01T15:18:32Z","git_url":"git://github.com/mb3152/networkx.git","ssh_url":"git@github.com:mb3152/networkx.git","clone_url":"https://github.com/mb3152/networkx.git","svn_url":"https://github.com/mb3152/networkx","homepage":"http:/networkx.lanl.gov/","size":11441,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":false,"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":"networkx:master","ref":"master","sha":"f501ab4119d4ee2d243e490c5b2cd6fe7491faca","user":{"login":"networkx","id":388785,"avatar_url":"https://avatars.githubusercontent.com/u/388785?v=3","gravatar_id":"","url":"https://api.github.com/users/networkx","html_url":"https://github.com/networkx","followers_url":"https://api.github.com/users/networkx/followers","following_url":"https://api.github.com/users/networkx/following{/other_user}","gists_url":"https://api.github.com/users/networkx/gists{/gist_id}","starred_url":"https://api.github.com/users/networkx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/networkx/subscriptions","organizations_url":"https://api.github.com/users/networkx/orgs","repos_url":"https://api.github.com/users/networkx/repos","events_url":"https://api.github.com/users/networkx/events{/privacy}","received_events_url":"https://api.github.com/users/networkx/received_events","type":"Organization","site_admin":false},"repo":{"id":890377,"name":"networkx","full_name":"networkx/networkx","owner":{"login":"networkx","id":388785,"avatar_url":"https://avatars.githubusercontent.com/u/388785?v=3","gravatar_id":"","url":"https://api.github.com/users/networkx","html_url":"https://github.com/networkx","followers_url":"https://api.github.com/users/networkx/followers","following_url":"https://api.github.com/users/networkx/following{/other_user}","gists_url":"https://api.github.com/users/networkx/gists{/gist_id}","starred_url":"https://api.github.com/users/networkx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/networkx/subscriptions","organizations_url":"https://api.github.com/users/networkx/orgs","repos_url":"https://api.github.com/users/networkx/repos","events_url":"https://api.github.com/users/networkx/events{/privacy}","received_events_url":"https://api.github.com/users/networkx/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/networkx/networkx","description":"Official NetworkX source code repository.  ","fork":false,"url":"https://api.github.com/repos/networkx/networkx","forks_url":"https://api.github.com/repos/networkx/networkx/forks","keys_url":"https://api.github.com/repos/networkx/networkx/keys{/key_id}","collaborators_url":"https://api.github.com/repos/networkx/networkx/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/networkx/networkx/teams","hooks_url":"https://api.github.com/repos/networkx/networkx/hooks","issue_events_url":"https://api.github.com/repos/networkx/networkx/issues/events{/number}","events_url":"https://api.github.com/repos/networkx/networkx/events","assignees_url":"https://api.github.com/repos/networkx/networkx/assignees{/user}","branches_url":"https://api.github.com/repos/networkx/networkx/branches{/branch}","tags_url":"https://api.github.com/repos/networkx/networkx/tags","blobs_url":"https://api.github.com/repos/networkx/networkx/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/networkx/networkx/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/networkx/networkx/git/refs{/sha}","trees_url":"https://api.github.com/repos/networkx/networkx/git/trees{/sha}","statuses_url":"https://api.github.com/repos/networkx/networkx/statuses/{sha}","languages_url":"https://api.github.com/repos/networkx/networkx/languages","stargazers_url":"https://api.github.com/repos/networkx/networkx/stargazers","contributors_url":"https://api.github.com/repos/networkx/networkx/contributors","subscribers_url":"https://api.github.com/repos/networkx/networkx/subscribers","subscription_url":"https://api.github.com/repos/networkx/networkx/subscription","commits_url":"https://api.github.com/repos/networkx/networkx/commits{/sha}","git_commits_url":"https://api.github.com/repos/networkx/networkx/git/commits{/sha}","comments_url":"https://api.github.com/repos/networkx/networkx/comments{/number}","issue_comment_url":"https://api.github.com/repos/networkx/networkx/issues/comments{/number}","contents_url":"https://api.github.com/repos/networkx/networkx/contents/{+path}","compare_url":"https://api.github.com/repos/networkx/networkx/compare/{base}...{head}","merges_url":"https://api.github.com/repos/networkx/networkx/merges","archive_url":"https://api.github.com/repos/networkx/networkx/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/networkx/networkx/downloads","issues_url":"https://api.github.com/repos/networkx/networkx/issues{/number}","pulls_url":"https://api.github.com/repos/networkx/networkx/pulls{/number}","milestones_url":"https://api.github.com/repos/networkx/networkx/milestones{/number}","notifications_url":"https://api.github.com/repos/networkx/networkx/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/networkx/networkx/labels{/name}","releases_url":"https://api.github.com/repos/networkx/networkx/releases{/id}","created_at":"2010-09-06T00:53:44Z","updated_at":"2015-05-22T19:57:30Z","pushed_at":"2015-05-23T03:56:30Z","git_url":"git://github.com/networkx/networkx.git","ssh_url":"git@github.com:networkx/networkx.git","clone_url":"https://github.com/networkx/networkx.git","svn_url":"https://github.com/networkx/networkx","homepage":"http://networkx.github.io","size":23943,"stargazers_count":1410,"watchers_count":1410,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":444,"mirror_url":null,"open_issues_count":205,"forks":444,"open_issues":205,"watchers":1410,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/networkx/networkx/pulls/1090"},"html":{"href":"https://github.com/networkx/networkx/pull/1090"},"issue":{"href":"https://api.github.com/repos/networkx/networkx/issues/1090"},"comments":{"href":"https://api.github.com/repos/networkx/networkx/issues/1090/comments"},"review_comments":{"href":"https://api.github.com/repos/networkx/networkx/pulls/1090/comments"},"review_comment":{"href":"https://api.github.com/repos/networkx/networkx/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/networkx/networkx/pulls/1090/commits"},"statuses":{"href":"https://api.github.com/repos/networkx/networkx/statuses/992a202a8fd53f75dd9da73b039795392879fdd7"}}}}