{"action":"created","comment":{"url":"https://api.github.com/repos/LABSN/mnefun/pulls/comments/86590554","pull_request_review_id":7244145,"id":86590554,"diff_hunk":"@@ -2391,6 +2391,194 @@ def plot_reconstruction(evoked, origin=(0., 0., 0.04)):\n     return fig\n \n \n+def _chpi_snr_epochs(epochs, n_lineharm=5, channels='grad', hpi_coil='median'):\n+    \"\"\" Compute SNR of continuous HPI for each epoch in epochs\n+    (mne.Epochs instance).\n+\n+    Parameters\n+    ----------\n+\n+    n_lineharm : int\n+        Number of line harmonics to use\n+\n+    channels : 'grad' | 'mag'\n+        Which channels to use for the SNR estimate\n+\n+    hpi_coil = 'median' | 'best' | 'worst'\n+        How to compute the SNR. 'best', 'worst' and 'median' picks the\n+        highest, lowest and median SNR at each time point, respectively.\n+        (Note that the corresponding coil may change over time)\n+    \"\"\"\n+\n+    if len(epochs.event_id) > 1:\n+        raise ValueError('Epochs object should contain one category only')\n+\n+    epochs.load_data()\n+    alldata = epochs.pick_types(meg=True).get_data()\n+    nepochs, nchan, buflen = alldata.shape\n+    sfreq = epochs.info['sfreq']\n+    t = np.linspace(0, buflen/sfreq, endpoint=False, num=buflen)\n+    (cfreqs, _, _, _, _) = _get_hpi_info(epochs.info)\n+    ncoils = len(cfreqs)\n+    linefreq = epochs.info['line_freq']\n+    linefreqs = (np.arange(n_lineharm+1)+1) * linefreq\n+\n+    # gradiometer and magmeter indices\n+    pick_mag = pick_types(epochs.info, meg='mag')\n+    pick_grad = pick_types(epochs.info, meg='grad')\n+\n+    # create linear model\n+    model = np.c_[t, np.ones(t.shape)]  # model slope and DC\n+    # add sine and cosine term for each freq\n+    for f in list(linefreqs)+list(cfreqs):\n+        model = np.c_[model, np.cos(2*np.pi*f*t), np.sin(2*np.pi*f*t)]\n+    inv_model = np.linalg.pinv(model)\n+\n+    # loop through epochs\n+    snr_avg_grad = np.zeros([ncoils, nepochs])\n+    snr_avg_mag = np.zeros([ncoils, nepochs])\n+    resid_vars = np.zeros([nchan, nepochs])\n+\n+    for epn in range(nepochs):\n+        epdata = alldata[epn, :, :].transpose()\n+        coeffs = np.dot(inv_model, epdata)\n+        coeffs_hpi = coeffs[2 + 2*len(linefreqs):]\n+        resid_vars[:, epn] = np.var(epdata - np.dot(model, coeffs), 0)\n+        # get total hpi amplitudes by combining sine and cosine terms\n+        hpi_amps = np.sqrt(coeffs_hpi[0::2, :]**2 + coeffs_hpi[1::2, :]**2)\n+        # divide average HPI power by average variance\n+        snr_avg_grad[:, epn] = np.divide((hpi_amps**2/2)[:, pick_grad].mean(1),\n+                                         resid_vars[pick_grad, epn].mean())\n+        snr_avg_mag[:, epn] = np.divide((hpi_amps**2/2)[:, pick_mag].mean(1),\n+                                        resid_vars[pick_mag, epn].mean())\n+    if hpi_coil == 'median':\n+        fun = np.median\n+    elif hpi_coil == 'best':\n+        fun = np.max\n+    elif hpi_coil == 'worst':\n+        fun = np.min\n+    else:\n+        raise ValueError('Invalid HPI coil selection parameter')\n+\n+    if channels == 'grad':\n+        snr = fun(snr_avg_grad, axis=0)\n+    elif channels == 'mag':\n+        snr = fun(snr_avg_mag, axis=0)\n+    return snr\n+\n+\n+def _weigh_epochs(epochs, weights):\n+    \"\"\" Weigh epochs in mne.Epochs instance (= elementwise multiply by\n+    weights vector). \"\"\"\n+    epochs.load_data()\n+    weights = np.array(weights)  # will accept either list or numpy array\n+    n_epochs = len(epochs)\n+    if not len(weights) == n_epochs:\n+        raise ValueError('Need as many weights as epochs')\n+    # reshape for broadcasting\n+    w_ = weights.squeeze()[:, np.newaxis, np.newaxis]\n+    # normalize by n_epochs/sum(weights), so that averaging\n+    # will yield a correct result\n+    epochs._data *= n_epochs * w_ / np.sum(w_)\n+\n+\n+def chpi_weighted_average(raw_snr, raw_epochs=None, reject=False, flat=False,\n+                          nharm=None, epoch_start=None, epoch_end=None,\n+                          stim_channel=None, mask=None):\n+    \"\"\" Average epochs weighted by SNR of continuous HPI (cHPI).\n+\n+    Parameters\n+    ----------\n+\n+    raw_chpi : instance of Raw\n+        The raw instance to get the cHPI SNR from. This file must not be\n+        MaxFiltered, because MaxFilter removes the cHPI signals.","path":"mnefun/_mnefun.py","position":114,"original_position":114,"commit_id":"85f7ac12b6972deacb3bfb9bb154e8ac83cd1ff9","original_commit_id":"85f7ac12b6972deacb3bfb9bb154e8ac83cd1ff9","user":{"login":"kambysese","id":4381290,"avatar_url":"https://avatars.githubusercontent.com/u/4381290?v=3","gravatar_id":"","url":"https://api.github.com/users/kambysese","html_url":"https://github.com/kambysese","followers_url":"https://api.github.com/users/kambysese/followers","following_url":"https://api.github.com/users/kambysese/following{/other_user}","gists_url":"https://api.github.com/users/kambysese/gists{/gist_id}","starred_url":"https://api.github.com/users/kambysese/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kambysese/subscriptions","organizations_url":"https://api.github.com/users/kambysese/orgs","repos_url":"https://api.github.com/users/kambysese/repos","events_url":"https://api.github.com/users/kambysese/events{/privacy}","received_events_url":"https://api.github.com/users/kambysese/received_events","type":"User","site_admin":false},"body":"This is not always true i.e., `maxwell_filter` does not remove the cHPI signals. ","created_at":"2016-11-04T17:09:09Z","updated_at":"2016-11-04T17:09:10Z","html_url":"https://github.com/LABSN/mnefun/pull/154#discussion_r86590554","pull_request_url":"https://api.github.com/repos/LABSN/mnefun/pulls/154","_links":{"self":{"href":"https://api.github.com/repos/LABSN/mnefun/pulls/comments/86590554"},"html":{"href":"https://github.com/LABSN/mnefun/pull/154#discussion_r86590554"},"pull_request":{"href":"https://api.github.com/repos/LABSN/mnefun/pulls/154"}}},"pull_request":{"url":"https://api.github.com/repos/LABSN/mnefun/pulls/154","id":92328345,"html_url":"https://github.com/LABSN/mnefun/pull/154","diff_url":"https://github.com/LABSN/mnefun/pull/154.diff","patch_url":"https://github.com/LABSN/mnefun/pull/154.patch","issue_url":"https://api.github.com/repos/LABSN/mnefun/issues/154","number":154,"state":"open","locked":false,"title":"WIP: cHPI weighted averager","user":{"login":"jjnurminen","id":8491321,"avatar_url":"https://avatars.githubusercontent.com/u/8491321?v=3","gravatar_id":"","url":"https://api.github.com/users/jjnurminen","html_url":"https://github.com/jjnurminen","followers_url":"https://api.github.com/users/jjnurminen/followers","following_url":"https://api.github.com/users/jjnurminen/following{/other_user}","gists_url":"https://api.github.com/users/jjnurminen/gists{/gist_id}","starred_url":"https://api.github.com/users/jjnurminen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jjnurminen/subscriptions","organizations_url":"https://api.github.com/users/jjnurminen/orgs","repos_url":"https://api.github.com/users/jjnurminen/repos","events_url":"https://api.github.com/users/jjnurminen/events{/privacy}","received_events_url":"https://api.github.com/users/jjnurminen/received_events","type":"User","site_admin":false},"body":"Integration into mnefun.\r\n","created_at":"2016-11-04T12:05:48Z","updated_at":"2016-11-04T17:09:10Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d82c2c122ff04762dbf2a9a79cbe641d15d67099","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/LABSN/mnefun/pulls/154/commits","review_comments_url":"https://api.github.com/repos/LABSN/mnefun/pulls/154/comments","review_comment_url":"https://api.github.com/repos/LABSN/mnefun/pulls/comments{/number}","comments_url":"https://api.github.com/repos/LABSN/mnefun/issues/154/comments","statuses_url":"https://api.github.com/repos/LABSN/mnefun/statuses/85f7ac12b6972deacb3bfb9bb154e8ac83cd1ff9","head":{"label":"jjnurminen:chpi_averager","ref":"chpi_averager","sha":"85f7ac12b6972deacb3bfb9bb154e8ac83cd1ff9","user":{"login":"jjnurminen","id":8491321,"avatar_url":"https://avatars.githubusercontent.com/u/8491321?v=3","gravatar_id":"","url":"https://api.github.com/users/jjnurminen","html_url":"https://github.com/jjnurminen","followers_url":"https://api.github.com/users/jjnurminen/followers","following_url":"https://api.github.com/users/jjnurminen/following{/other_user}","gists_url":"https://api.github.com/users/jjnurminen/gists{/gist_id}","starred_url":"https://api.github.com/users/jjnurminen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jjnurminen/subscriptions","organizations_url":"https://api.github.com/users/jjnurminen/orgs","repos_url":"https://api.github.com/users/jjnurminen/repos","events_url":"https://api.github.com/users/jjnurminen/events{/privacy}","received_events_url":"https://api.github.com/users/jjnurminen/received_events","type":"User","site_admin":false},"repo":{"id":61375504,"name":"mnefun","full_name":"jjnurminen/mnefun","owner":{"login":"jjnurminen","id":8491321,"avatar_url":"https://avatars.githubusercontent.com/u/8491321?v=3","gravatar_id":"","url":"https://api.github.com/users/jjnurminen","html_url":"https://github.com/jjnurminen","followers_url":"https://api.github.com/users/jjnurminen/followers","following_url":"https://api.github.com/users/jjnurminen/following{/other_user}","gists_url":"https://api.github.com/users/jjnurminen/gists{/gist_id}","starred_url":"https://api.github.com/users/jjnurminen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jjnurminen/subscriptions","organizations_url":"https://api.github.com/users/jjnurminen/orgs","repos_url":"https://api.github.com/users/jjnurminen/repos","events_url":"https://api.github.com/users/jjnurminen/events{/privacy}","received_events_url":"https://api.github.com/users/jjnurminen/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/jjnurminen/mnefun","description":"Facilitating lab integration with mne-python","fork":true,"url":"https://api.github.com/repos/jjnurminen/mnefun","forks_url":"https://api.github.com/repos/jjnurminen/mnefun/forks","keys_url":"https://api.github.com/repos/jjnurminen/mnefun/keys{/key_id}","collaborators_url":"https://api.github.com/repos/jjnurminen/mnefun/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/jjnurminen/mnefun/teams","hooks_url":"https://api.github.com/repos/jjnurminen/mnefun/hooks","issue_events_url":"https://api.github.com/repos/jjnurminen/mnefun/issues/events{/number}","events_url":"https://api.github.com/repos/jjnurminen/mnefun/events","assignees_url":"https://api.github.com/repos/jjnurminen/mnefun/assignees{/user}","branches_url":"https://api.github.com/repos/jjnurminen/mnefun/branches{/branch}","tags_url":"https://api.github.com/repos/jjnurminen/mnefun/tags","blobs_url":"https://api.github.com/repos/jjnurminen/mnefun/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/jjnurminen/mnefun/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/jjnurminen/mnefun/git/refs{/sha}","trees_url":"https://api.github.com/repos/jjnurminen/mnefun/git/trees{/sha}","statuses_url":"https://api.github.com/repos/jjnurminen/mnefun/statuses/{sha}","languages_url":"https://api.github.com/repos/jjnurminen/mnefun/languages","stargazers_url":"https://api.github.com/repos/jjnurminen/mnefun/stargazers","contributors_url":"https://api.github.com/repos/jjnurminen/mnefun/contributors","subscribers_url":"https://api.github.com/repos/jjnurminen/mnefun/subscribers","subscription_url":"https://api.github.com/repos/jjnurminen/mnefun/subscription","commits_url":"https://api.github.com/repos/jjnurminen/mnefun/commits{/sha}","git_commits_url":"https://api.github.com/repos/jjnurminen/mnefun/git/commits{/sha}","comments_url":"https://api.github.com/repos/jjnurminen/mnefun/comments{/number}","issue_comment_url":"https://api.github.com/repos/jjnurminen/mnefun/issues/comments{/number}","contents_url":"https://api.github.com/repos/jjnurminen/mnefun/contents/{+path}","compare_url":"https://api.github.com/repos/jjnurminen/mnefun/compare/{base}...{head}","merges_url":"https://api.github.com/repos/jjnurminen/mnefun/merges","archive_url":"https://api.github.com/repos/jjnurminen/mnefun/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/jjnurminen/mnefun/downloads","issues_url":"https://api.github.com/repos/jjnurminen/mnefun/issues{/number}","pulls_url":"https://api.github.com/repos/jjnurminen/mnefun/pulls{/number}","milestones_url":"https://api.github.com/repos/jjnurminen/mnefun/milestones{/number}","notifications_url":"https://api.github.com/repos/jjnurminen/mnefun/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/jjnurminen/mnefun/labels{/name}","releases_url":"https://api.github.com/repos/jjnurminen/mnefun/releases{/id}","deployments_url":"https://api.github.com/repos/jjnurminen/mnefun/deployments","created_at":"2016-06-17T13:27:38Z","updated_at":"2016-06-17T13:27:39Z","pushed_at":"2016-11-04T13:25:45Z","git_url":"git://github.com/jjnurminen/mnefun.git","ssh_url":"git@github.com:jjnurminen/mnefun.git","clone_url":"https://github.com/jjnurminen/mnefun.git","svn_url":"https://github.com/jjnurminen/mnefun","homepage":null,"size":967,"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":"LABSN:master","ref":"master","sha":"1b540d7db31adfd2ca3f63aa291f1f0817051d2e","user":{"login":"LABSN","id":3766097,"avatar_url":"https://avatars.githubusercontent.com/u/3766097?v=3","gravatar_id":"","url":"https://api.github.com/users/LABSN","html_url":"https://github.com/LABSN","followers_url":"https://api.github.com/users/LABSN/followers","following_url":"https://api.github.com/users/LABSN/following{/other_user}","gists_url":"https://api.github.com/users/LABSN/gists{/gist_id}","starred_url":"https://api.github.com/users/LABSN/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/LABSN/subscriptions","organizations_url":"https://api.github.com/users/LABSN/orgs","repos_url":"https://api.github.com/users/LABSN/repos","events_url":"https://api.github.com/users/LABSN/events{/privacy}","received_events_url":"https://api.github.com/users/LABSN/received_events","type":"Organization","site_admin":false},"repo":{"id":17719360,"name":"mnefun","full_name":"LABSN/mnefun","owner":{"login":"LABSN","id":3766097,"avatar_url":"https://avatars.githubusercontent.com/u/3766097?v=3","gravatar_id":"","url":"https://api.github.com/users/LABSN","html_url":"https://github.com/LABSN","followers_url":"https://api.github.com/users/LABSN/followers","following_url":"https://api.github.com/users/LABSN/following{/other_user}","gists_url":"https://api.github.com/users/LABSN/gists{/gist_id}","starred_url":"https://api.github.com/users/LABSN/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/LABSN/subscriptions","organizations_url":"https://api.github.com/users/LABSN/orgs","repos_url":"https://api.github.com/users/LABSN/repos","events_url":"https://api.github.com/users/LABSN/events{/privacy}","received_events_url":"https://api.github.com/users/LABSN/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/LABSN/mnefun","description":"Facilitating lab integration with mne-python","fork":false,"url":"https://api.github.com/repos/LABSN/mnefun","forks_url":"https://api.github.com/repos/LABSN/mnefun/forks","keys_url":"https://api.github.com/repos/LABSN/mnefun/keys{/key_id}","collaborators_url":"https://api.github.com/repos/LABSN/mnefun/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/LABSN/mnefun/teams","hooks_url":"https://api.github.com/repos/LABSN/mnefun/hooks","issue_events_url":"https://api.github.com/repos/LABSN/mnefun/issues/events{/number}","events_url":"https://api.github.com/repos/LABSN/mnefun/events","assignees_url":"https://api.github.com/repos/LABSN/mnefun/assignees{/user}","branches_url":"https://api.github.com/repos/LABSN/mnefun/branches{/branch}","tags_url":"https://api.github.com/repos/LABSN/mnefun/tags","blobs_url":"https://api.github.com/repos/LABSN/mnefun/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/LABSN/mnefun/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/LABSN/mnefun/git/refs{/sha}","trees_url":"https://api.github.com/repos/LABSN/mnefun/git/trees{/sha}","statuses_url":"https://api.github.com/repos/LABSN/mnefun/statuses/{sha}","languages_url":"https://api.github.com/repos/LABSN/mnefun/languages","stargazers_url":"https://api.github.com/repos/LABSN/mnefun/stargazers","contributors_url":"https://api.github.com/repos/LABSN/mnefun/contributors","subscribers_url":"https://api.github.com/repos/LABSN/mnefun/subscribers","subscription_url":"https://api.github.com/repos/LABSN/mnefun/subscription","commits_url":"https://api.github.com/repos/LABSN/mnefun/commits{/sha}","git_commits_url":"https://api.github.com/repos/LABSN/mnefun/git/commits{/sha}","comments_url":"https://api.github.com/repos/LABSN/mnefun/comments{/number}","issue_comment_url":"https://api.github.com/repos/LABSN/mnefun/issues/comments{/number}","contents_url":"https://api.github.com/repos/LABSN/mnefun/contents/{+path}","compare_url":"https://api.github.com/repos/LABSN/mnefun/compare/{base}...{head}","merges_url":"https://api.github.com/repos/LABSN/mnefun/merges","archive_url":"https://api.github.com/repos/LABSN/mnefun/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/LABSN/mnefun/downloads","issues_url":"https://api.github.com/repos/LABSN/mnefun/issues{/number}","pulls_url":"https://api.github.com/repos/LABSN/mnefun/pulls{/number}","milestones_url":"https://api.github.com/repos/LABSN/mnefun/milestones{/number}","notifications_url":"https://api.github.com/repos/LABSN/mnefun/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/LABSN/mnefun/labels{/name}","releases_url":"https://api.github.com/repos/LABSN/mnefun/releases{/id}","deployments_url":"https://api.github.com/repos/LABSN/mnefun/deployments","created_at":"2014-03-13T17:20:05Z","updated_at":"2016-10-25T16:40:22Z","pushed_at":"2016-11-04T13:25:45Z","git_url":"git://github.com/LABSN/mnefun.git","ssh_url":"git@github.com:LABSN/mnefun.git","clone_url":"https://github.com/LABSN/mnefun.git","svn_url":"https://github.com/LABSN/mnefun","homepage":null,"size":89568,"stargazers_count":3,"watchers_count":3,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":9,"mirror_url":null,"open_issues_count":6,"forks":9,"open_issues":6,"watchers":3,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/LABSN/mnefun/pulls/154"},"html":{"href":"https://github.com/LABSN/mnefun/pull/154"},"issue":{"href":"https://api.github.com/repos/LABSN/mnefun/issues/154"},"comments":{"href":"https://api.github.com/repos/LABSN/mnefun/issues/154/comments"},"review_comments":{"href":"https://api.github.com/repos/LABSN/mnefun/pulls/154/comments"},"review_comment":{"href":"https://api.github.com/repos/LABSN/mnefun/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/LABSN/mnefun/pulls/154/commits"},"statuses":{"href":"https://api.github.com/repos/LABSN/mnefun/statuses/85f7ac12b6972deacb3bfb9bb154e8ac83cd1ff9"}}}}
{"actor":{"display_login":"kambysese"}}