{"action":"created","comment":{"url":"https://api.github.com/repos/mne-tools/mne-python/pulls/comments/200895982","pull_request_review_id":135293591,"id":200895982,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDIwMDg5NTk4Mg==","diff_hunk":"@@ -239,3 +239,187 @@ def _prepare_beamformer_input(info, forward, label, picks, pick_ori,\n                          'values are: \"dipole\", \"vertex\" or None.')\n \n     return is_free_ori, ch_names, proj, vertno, G\n+\n+\n+def _compute_beamformer(beamformer, G, Cm, reg, n_orient, weight_norm,\n+                        pick_ori, reduce_rank, rank, is_free_ori,\n+                        inversion=None):\n+    \"\"\"Compute a spatial filter (LCMV or DICS).\"\"\"\n+    # Tikhonov regularization using reg parameter d to control for\n+    # trade-off between spatial resolution and noise sensitivity\n+    if beamformer == 'lcmv':\n+        Cm_inv, d = _reg_pinv(Cm.copy(), reg)\n+    elif beamformer == 'dics':\n+        Cm_inv, _ = _reg_pinv(Cm, reg, rcond='auto')\n+\n+    if weight_norm is not None and inversion is not 'single':\n+        # Compute square of Cm_inv used for weight normalization\n+        Cm_inv_sq = np.dot(Cm_inv, Cm_inv)\n+\n+        if weight_norm == 'nai':\n+            # estimate noise level based on covariance matrix, taking the\n+            # smallest eigenvalue that is not zero\n+            noise, _ = linalg.eigh(Cm)\n+            if rank is not None:\n+                rank_Cm = rank\n+            else:\n+                rank_Cm = estimate_rank(Cm, tol='auto', norm=False,\n+                                        return_singular=False)\n+                noise = noise[len(noise) - rank_Cm]\n+\n+                # use either noise floor or regularization parameter d\n+                noise = max(noise, d)\n+\n+    # compute spatial filter\n+    W = np.dot(G.T, Cm_inv)\n+    n_sources = G.shape[1] // n_orient\n+\n+    for k in range(n_sources):\n+        Wk = W[n_orient * k: n_orient * k + n_orient]\n+        Gk = G[:, n_orient * k: n_orient * k + n_orient]\n+        if beamformer == 'lcmv' and np.all(Gk == 0.):\n+            continue\n+        Ck = np.dot(Wk, Gk)\n+\n+        if beamformer == 'dics':\n+            # Normalize the spatial filters:\n+            if Wk.ndim == 2 and len(Wk) > 1:\n+                # Free source orientation\n+                if inversion == 'single':\n+                    # Invert for each dipole separately using plain division\n+                    Wk /= np.diag(Ck)[:, np.newaxis]\n+                elif inversion == 'matrix':\n+                    # Invert for all dipoles simultaneously using matrix\n+                    # inversion.\n+                    Wk[:] = np.dot(linalg.pinv(Ck, 0.1), Wk)\n+            else:\n+                # Fixed source orientation\n+                Wk /= Ck","path":"mne/beamformer/_compute_beamformer.py","position":80,"original_position":59,"commit_id":"163128589549c85e743e7f3e8be485a53fc920f4","original_commit_id":"e890ae00e2dfd6408a98c64df9c479db78d7a59e","user":{"login":"britta-wstnr","id":20129119,"node_id":"MDQ6VXNlcjIwMTI5MTE5","avatar_url":"https://avatars0.githubusercontent.com/u/20129119?v=4","gravatar_id":"","url":"https://api.github.com/users/britta-wstnr","html_url":"https://github.com/britta-wstnr","followers_url":"https://api.github.com/users/britta-wstnr/followers","following_url":"https://api.github.com/users/britta-wstnr/following{/other_user}","gists_url":"https://api.github.com/users/britta-wstnr/gists{/gist_id}","starred_url":"https://api.github.com/users/britta-wstnr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/britta-wstnr/subscriptions","organizations_url":"https://api.github.com/users/britta-wstnr/orgs","repos_url":"https://api.github.com/users/britta-wstnr/repos","events_url":"https://api.github.com/users/britta-wstnr/events{/privacy}","received_events_url":"https://api.github.com/users/britta-wstnr/received_events","type":"User","site_admin":false},"body":"@wmvanvliet Note that for LCMV we do the actual computation of the weights (DICS code above) only at the end, after the (potential) orientation selection etc. Can you confirm that this would work for the DICS case, too? -- As far as I see it, the unit-gain / no orientation selection case would not be affected by this anyway.\r\nWith your confirmation, I would incorporate your DICS options (``inversion``) into the LCMV code block at the end of the function. ","created_at":"2018-07-09T06:56:48Z","updated_at":"2018-07-09T06:56:48Z","html_url":"https://github.com/mne-tools/mne-python/pull/5135#discussion_r200895982","pull_request_url":"https://api.github.com/repos/mne-tools/mne-python/pulls/5135","author_association":"CONTRIBUTOR","_links":{"self":{"href":"https://api.github.com/repos/mne-tools/mne-python/pulls/comments/200895982"},"html":{"href":"https://github.com/mne-tools/mne-python/pull/5135#discussion_r200895982"},"pull_request":{"href":"https://api.github.com/repos/mne-tools/mne-python/pulls/5135"}},"in_reply_to_id":182998872},"pull_request":{"url":"https://api.github.com/repos/mne-tools/mne-python/pulls/5135","id":181915310,"node_id":"MDExOlB1bGxSZXF1ZXN0MTgxOTE1MzEw","html_url":"https://github.com/mne-tools/mne-python/pull/5135","diff_url":"https://github.com/mne-tools/mne-python/pull/5135.diff","patch_url":"https://github.com/mne-tools/mne-python/pull/5135.patch","issue_url":"https://api.github.com/repos/mne-tools/mne-python/issues/5135","number":5135,"state":"open","locked":false,"title":"[WIP] Refactoring beamformer code, part II","user":{"login":"britta-wstnr","id":20129119,"node_id":"MDQ6VXNlcjIwMTI5MTE5","avatar_url":"https://avatars0.githubusercontent.com/u/20129119?v=4","gravatar_id":"","url":"https://api.github.com/users/britta-wstnr","html_url":"https://github.com/britta-wstnr","followers_url":"https://api.github.com/users/britta-wstnr/followers","following_url":"https://api.github.com/users/britta-wstnr/following{/other_user}","gists_url":"https://api.github.com/users/britta-wstnr/gists{/gist_id}","starred_url":"https://api.github.com/users/britta-wstnr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/britta-wstnr/subscriptions","organizations_url":"https://api.github.com/users/britta-wstnr/orgs","repos_url":"https://api.github.com/users/britta-wstnr/repos","events_url":"https://api.github.com/users/britta-wstnr/events{/privacy}","received_events_url":"https://api.github.com/users/britta-wstnr/received_events","type":"User","site_admin":false},"body":"Second iteration for https://github.com/mne-tools/mne-python/issues/5109 to unify ``_lcmv`` and ``_dics``, also compare https://github.com/mne-tools/mne-python/pull/5134.\r\nThis PR will unify the shared code of ``_lcmv`` and ``_dics`` in a ``_compute_beamformer`` function.\r\n\r\nThis is still very much work in progress.\r\n\r\nOnce the new code is set up and generates identical results as the original implementations, we can start discussing further enhancements/unification of the code. I will ping people when I got so far.\r\n\r\ncc @larsoner ","created_at":"2018-04-16T16:05:40Z","updated_at":"2018-07-09T06:56:48Z","closed_at":null,"merged_at":null,"merge_commit_sha":"3af1df6e777f1d14f6a20ddaf554a95441b6244a","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/mne-tools/mne-python/pulls/5135/commits","review_comments_url":"https://api.github.com/repos/mne-tools/mne-python/pulls/5135/comments","review_comment_url":"https://api.github.com/repos/mne-tools/mne-python/pulls/comments{/number}","comments_url":"https://api.github.com/repos/mne-tools/mne-python/issues/5135/comments","statuses_url":"https://api.github.com/repos/mne-tools/mne-python/statuses/163128589549c85e743e7f3e8be485a53fc920f4","head":{"label":"britta-wstnr:bf_refact_II","ref":"bf_refact_II","sha":"163128589549c85e743e7f3e8be485a53fc920f4","user":{"login":"britta-wstnr","id":20129119,"node_id":"MDQ6VXNlcjIwMTI5MTE5","avatar_url":"https://avatars0.githubusercontent.com/u/20129119?v=4","gravatar_id":"","url":"https://api.github.com/users/britta-wstnr","html_url":"https://github.com/britta-wstnr","followers_url":"https://api.github.com/users/britta-wstnr/followers","following_url":"https://api.github.com/users/britta-wstnr/following{/other_user}","gists_url":"https://api.github.com/users/britta-wstnr/gists{/gist_id}","starred_url":"https://api.github.com/users/britta-wstnr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/britta-wstnr/subscriptions","organizations_url":"https://api.github.com/users/britta-wstnr/orgs","repos_url":"https://api.github.com/users/britta-wstnr/repos","events_url":"https://api.github.com/users/britta-wstnr/events{/privacy}","received_events_url":"https://api.github.com/users/britta-wstnr/received_events","type":"User","site_admin":false},"repo":{"id":86345521,"node_id":"MDEwOlJlcG9zaXRvcnk4NjM0NTUyMQ==","name":"mne-python","full_name":"britta-wstnr/mne-python","owner":{"login":"britta-wstnr","id":20129119,"node_id":"MDQ6VXNlcjIwMTI5MTE5","avatar_url":"https://avatars0.githubusercontent.com/u/20129119?v=4","gravatar_id":"","url":"https://api.github.com/users/britta-wstnr","html_url":"https://github.com/britta-wstnr","followers_url":"https://api.github.com/users/britta-wstnr/followers","following_url":"https://api.github.com/users/britta-wstnr/following{/other_user}","gists_url":"https://api.github.com/users/britta-wstnr/gists{/gist_id}","starred_url":"https://api.github.com/users/britta-wstnr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/britta-wstnr/subscriptions","organizations_url":"https://api.github.com/users/britta-wstnr/orgs","repos_url":"https://api.github.com/users/britta-wstnr/repos","events_url":"https://api.github.com/users/britta-wstnr/events{/privacy}","received_events_url":"https://api.github.com/users/britta-wstnr/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/britta-wstnr/mne-python","description":"MNE : Magnetoencephalography (MEG) and Electroencephalography (EEG) in Python","fork":true,"url":"https://api.github.com/repos/britta-wstnr/mne-python","forks_url":"https://api.github.com/repos/britta-wstnr/mne-python/forks","keys_url":"https://api.github.com/repos/britta-wstnr/mne-python/keys{/key_id}","collaborators_url":"https://api.github.com/repos/britta-wstnr/mne-python/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/britta-wstnr/mne-python/teams","hooks_url":"https://api.github.com/repos/britta-wstnr/mne-python/hooks","issue_events_url":"https://api.github.com/repos/britta-wstnr/mne-python/issues/events{/number}","events_url":"https://api.github.com/repos/britta-wstnr/mne-python/events","assignees_url":"https://api.github.com/repos/britta-wstnr/mne-python/assignees{/user}","branches_url":"https://api.github.com/repos/britta-wstnr/mne-python/branches{/branch}","tags_url":"https://api.github.com/repos/britta-wstnr/mne-python/tags","blobs_url":"https://api.github.com/repos/britta-wstnr/mne-python/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/britta-wstnr/mne-python/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/britta-wstnr/mne-python/git/refs{/sha}","trees_url":"https://api.github.com/repos/britta-wstnr/mne-python/git/trees{/sha}","statuses_url":"https://api.github.com/repos/britta-wstnr/mne-python/statuses/{sha}","languages_url":"https://api.github.com/repos/britta-wstnr/mne-python/languages","stargazers_url":"https://api.github.com/repos/britta-wstnr/mne-python/stargazers","contributors_url":"https://api.github.com/repos/britta-wstnr/mne-python/contributors","subscribers_url":"https://api.github.com/repos/britta-wstnr/mne-python/subscribers","subscription_url":"https://api.github.com/repos/britta-wstnr/mne-python/subscription","commits_url":"https://api.github.com/repos/britta-wstnr/mne-python/commits{/sha}","git_commits_url":"https://api.github.com/repos/britta-wstnr/mne-python/git/commits{/sha}","comments_url":"https://api.github.com/repos/britta-wstnr/mne-python/comments{/number}","issue_comment_url":"https://api.github.com/repos/britta-wstnr/mne-python/issues/comments{/number}","contents_url":"https://api.github.com/repos/britta-wstnr/mne-python/contents/{+path}","compare_url":"https://api.github.com/repos/britta-wstnr/mne-python/compare/{base}...{head}","merges_url":"https://api.github.com/repos/britta-wstnr/mne-python/merges","archive_url":"https://api.github.com/repos/britta-wstnr/mne-python/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/britta-wstnr/mne-python/downloads","issues_url":"https://api.github.com/repos/britta-wstnr/mne-python/issues{/number}","pulls_url":"https://api.github.com/repos/britta-wstnr/mne-python/pulls{/number}","milestones_url":"https://api.github.com/repos/britta-wstnr/mne-python/milestones{/number}","notifications_url":"https://api.github.com/repos/britta-wstnr/mne-python/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/britta-wstnr/mne-python/labels{/name}","releases_url":"https://api.github.com/repos/britta-wstnr/mne-python/releases{/id}","deployments_url":"https://api.github.com/repos/britta-wstnr/mne-python/deployments","created_at":"2017-03-27T14:29:06Z","updated_at":"2017-03-27T14:29:11Z","pushed_at":"2018-07-06T08:47:14Z","git_url":"git://github.com/britta-wstnr/mne-python.git","ssh_url":"git@github.com:britta-wstnr/mne-python.git","clone_url":"https://github.com/britta-wstnr/mne-python.git","svn_url":"https://github.com/britta-wstnr/mne-python","homepage":"http://martinos.org/mne/","size":99868,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":{"key":"other","name":"Other","spdx_id":null,"url":null,"node_id":"MDc6TGljZW5zZTA="},"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"mne-tools:master","ref":"master","sha":"005f4ab7fc0fc8b145172b72831b2262622b4075","user":{"login":"mne-tools","id":583875,"node_id":"MDEyOk9yZ2FuaXphdGlvbjU4Mzg3NQ==","avatar_url":"https://avatars3.githubusercontent.com/u/583875?v=4","gravatar_id":"","url":"https://api.github.com/users/mne-tools","html_url":"https://github.com/mne-tools","followers_url":"https://api.github.com/users/mne-tools/followers","following_url":"https://api.github.com/users/mne-tools/following{/other_user}","gists_url":"https://api.github.com/users/mne-tools/gists{/gist_id}","starred_url":"https://api.github.com/users/mne-tools/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mne-tools/subscriptions","organizations_url":"https://api.github.com/users/mne-tools/orgs","repos_url":"https://api.github.com/users/mne-tools/repos","events_url":"https://api.github.com/users/mne-tools/events{/privacy}","received_events_url":"https://api.github.com/users/mne-tools/received_events","type":"Organization","site_admin":false},"repo":{"id":1301584,"node_id":"MDEwOlJlcG9zaXRvcnkxMzAxNTg0","name":"mne-python","full_name":"mne-tools/mne-python","owner":{"login":"mne-tools","id":583875,"node_id":"MDEyOk9yZ2FuaXphdGlvbjU4Mzg3NQ==","avatar_url":"https://avatars3.githubusercontent.com/u/583875?v=4","gravatar_id":"","url":"https://api.github.com/users/mne-tools","html_url":"https://github.com/mne-tools","followers_url":"https://api.github.com/users/mne-tools/followers","following_url":"https://api.github.com/users/mne-tools/following{/other_user}","gists_url":"https://api.github.com/users/mne-tools/gists{/gist_id}","starred_url":"https://api.github.com/users/mne-tools/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mne-tools/subscriptions","organizations_url":"https://api.github.com/users/mne-tools/orgs","repos_url":"https://api.github.com/users/mne-tools/repos","events_url":"https://api.github.com/users/mne-tools/events{/privacy}","received_events_url":"https://api.github.com/users/mne-tools/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/mne-tools/mne-python","description":"MNE : Magnetoencephalography (MEG) and Electroencephalography (EEG) in Python","fork":false,"url":"https://api.github.com/repos/mne-tools/mne-python","forks_url":"https://api.github.com/repos/mne-tools/mne-python/forks","keys_url":"https://api.github.com/repos/mne-tools/mne-python/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mne-tools/mne-python/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mne-tools/mne-python/teams","hooks_url":"https://api.github.com/repos/mne-tools/mne-python/hooks","issue_events_url":"https://api.github.com/repos/mne-tools/mne-python/issues/events{/number}","events_url":"https://api.github.com/repos/mne-tools/mne-python/events","assignees_url":"https://api.github.com/repos/mne-tools/mne-python/assignees{/user}","branches_url":"https://api.github.com/repos/mne-tools/mne-python/branches{/branch}","tags_url":"https://api.github.com/repos/mne-tools/mne-python/tags","blobs_url":"https://api.github.com/repos/mne-tools/mne-python/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mne-tools/mne-python/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mne-tools/mne-python/git/refs{/sha}","trees_url":"https://api.github.com/repos/mne-tools/mne-python/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mne-tools/mne-python/statuses/{sha}","languages_url":"https://api.github.com/repos/mne-tools/mne-python/languages","stargazers_url":"https://api.github.com/repos/mne-tools/mne-python/stargazers","contributors_url":"https://api.github.com/repos/mne-tools/mne-python/contributors","subscribers_url":"https://api.github.com/repos/mne-tools/mne-python/subscribers","subscription_url":"https://api.github.com/repos/mne-tools/mne-python/subscription","commits_url":"https://api.github.com/repos/mne-tools/mne-python/commits{/sha}","git_commits_url":"https://api.github.com/repos/mne-tools/mne-python/git/commits{/sha}","comments_url":"https://api.github.com/repos/mne-tools/mne-python/comments{/number}","issue_comment_url":"https://api.github.com/repos/mne-tools/mne-python/issues/comments{/number}","contents_url":"https://api.github.com/repos/mne-tools/mne-python/contents/{+path}","compare_url":"https://api.github.com/repos/mne-tools/mne-python/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mne-tools/mne-python/merges","archive_url":"https://api.github.com/repos/mne-tools/mne-python/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mne-tools/mne-python/downloads","issues_url":"https://api.github.com/repos/mne-tools/mne-python/issues{/number}","pulls_url":"https://api.github.com/repos/mne-tools/mne-python/pulls{/number}","milestones_url":"https://api.github.com/repos/mne-tools/mne-python/milestones{/number}","notifications_url":"https://api.github.com/repos/mne-tools/mne-python/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mne-tools/mne-python/labels{/name}","releases_url":"https://api.github.com/repos/mne-tools/mne-python/releases{/id}","deployments_url":"https://api.github.com/repos/mne-tools/mne-python/deployments","created_at":"2011-01-28T03:31:13Z","updated_at":"2018-07-06T14:18:44Z","pushed_at":"2018-07-08T19:35:59Z","git_url":"git://github.com/mne-tools/mne-python.git","ssh_url":"git@github.com:mne-tools/mne-python.git","clone_url":"https://github.com/mne-tools/mne-python.git","svn_url":"https://github.com/mne-tools/mne-python","homepage":"http://martinos.org/mne/","size":99819,"stargazers_count":496,"watchers_count":496,"language":"Python","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":420,"mirror_url":null,"archived":false,"open_issues_count":253,"license":{"key":"other","name":"Other","spdx_id":null,"url":null,"node_id":"MDc6TGljZW5zZTA="},"forks":420,"open_issues":253,"watchers":496,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/mne-tools/mne-python/pulls/5135"},"html":{"href":"https://github.com/mne-tools/mne-python/pull/5135"},"issue":{"href":"https://api.github.com/repos/mne-tools/mne-python/issues/5135"},"comments":{"href":"https://api.github.com/repos/mne-tools/mne-python/issues/5135/comments"},"review_comments":{"href":"https://api.github.com/repos/mne-tools/mne-python/pulls/5135/comments"},"review_comment":{"href":"https://api.github.com/repos/mne-tools/mne-python/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/mne-tools/mne-python/pulls/5135/commits"},"statuses":{"href":"https://api.github.com/repos/mne-tools/mne-python/statuses/163128589549c85e743e7f3e8be485a53fc920f4"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"britta-wstnr"}}