{"action":"created","comment":{"url":"https://api.github.com/repos/PyMVPA/PyMVPA/pulls/comments/81854805","pull_request_review_id":2804679,"id":81854805,"diff_hunk":"@@ -10,19 +10,70 @@\n \n __docformat__ = 'restructuredtext'\n \n-from itertools import combinations\n+from itertools import combinations, product\n import numpy as np\n from mvpa2.measures.base import Measure\n from mvpa2.datasets.base import Dataset\n from mvpa2.base import externals\n from mvpa2.base.param import Parameter\n from mvpa2.base.constraints import EnsureChoice\n+from mvpa2.mappers.fx import mean_group_sample\n \n if externals.exists('scipy', raise_=True):\n-    from scipy.spatial.distance import pdist, squareform\n+    from scipy.spatial.distance import pdist, squareform, cdist\n     from scipy.stats import rankdata, pearsonr\n \n \n+class CDist(Measure):\n+    \"\"\"Compute dissimiliarity matrix for samples in a dataset\n+\n+    This `Measure` can be trained on part of the dataset (for example,\n+    a partition) and called on another partition. It can be used in\n+    cross-validation to generate cross-validated RSA.\n+    \"\"\"\n+    pairwise_metric = Parameter('correlation', constraints='str', doc=\"\"\"\n+          Distance metric to use for calculating pairwise vector distances for\n+          dissimilarity matrix (DSM).  See scipy.spatial.distance.cdist for\n+          all possible metrics.\"\"\")\n+\n+    pairwise_metric_kwargs = Parameter({}, doc=\"\"\"\n+    kwargs dictionary passed to cdist. For example,\n+    if `pairwise_metric='mahalanobis'`, `pairwise_metric_kwargs`\n+    might contain the inverse of the covariance matrix.\"\"\")\n+\n+    sattr = Parameter(['targets'], doc=\"\"\"\n+        List of sample attributes whose unique values will be used to identify the\n+        samples groups. Typically your category labels or targets.\"\"\")\n+\n+    def __init__(self, **kwargs):\n+        Measure.__init__(self, **kwargs)\n+        self._train_ds = None\n+\n+    def _prepare_ds(self, ds):\n+        if self.params.sattr is not None:\n+            mgs = mean_group_sample(attrs=self.params.sattr)\n+            ds_ = mgs(ds)\n+        else:\n+            ds_ = ds.copy(deep=True)\n+        return ds_\n+\n+    def _train(self, ds):\n+        self._train_ds = self._prepare_ds(ds)\n+        self.is_trained = True\n+\n+    def _call(self, ds):\n+        test_ds = self._prepare_ds(ds)\n+        # Call actual distance metric\n+        distds = cdist(self._train_ds.samples, test_ds.samples,\n+                       metric=self.params.pairwise_metric,\n+                       **self.params.pairwise_metric_kwargs)\n+        # Make target pairs\n+        distds = Dataset(samples=distds.ravel()[:, None],\n+                         sa={'pairs': list(product(self._train_ds.T,\n+                                                   test_ds.T))})","path":"mvpa2/measures/rsa.py","position":66,"original_position":66,"commit_id":"ebced46968c3431abcc373bc2effe1bb49d7422d","original_commit_id":"ebced46968c3431abcc373bc2effe1bb49d7422d","user":{"login":"swaroopgj","id":139211,"avatar_url":"https://avatars.githubusercontent.com/u/139211?v=3","gravatar_id":"","url":"https://api.github.com/users/swaroopgj","html_url":"https://github.com/swaroopgj","followers_url":"https://api.github.com/users/swaroopgj/followers","following_url":"https://api.github.com/users/swaroopgj/following{/other_user}","gists_url":"https://api.github.com/users/swaroopgj/gists{/gist_id}","starred_url":"https://api.github.com/users/swaroopgj/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/swaroopgj/subscriptions","organizations_url":"https://api.github.com/users/swaroopgj/orgs","repos_url":"https://api.github.com/users/swaroopgj/repos","events_url":"https://api.github.com/users/swaroopgj/events{/privacy}","received_events_url":"https://api.github.com/users/swaroopgj/received_events","type":"User","site_admin":false},"body":"We could keep the sa from meang_group_sample if we don't ravel(). Reason I didn't choose flatten mapper is to flatten the matrix into samples/rows. Flatten mapper does it into columns/features (right?).","created_at":"2016-10-04T21:11:19Z","updated_at":"2016-10-04T21:11:19Z","html_url":"https://github.com/PyMVPA/PyMVPA/pull/504#discussion_r81854805","pull_request_url":"https://api.github.com/repos/PyMVPA/PyMVPA/pulls/504","_links":{"self":{"href":"https://api.github.com/repos/PyMVPA/PyMVPA/pulls/comments/81854805"},"html":{"href":"https://github.com/PyMVPA/PyMVPA/pull/504#discussion_r81854805"},"pull_request":{"href":"https://api.github.com/repos/PyMVPA/PyMVPA/pulls/504"}}},"pull_request":{"url":"https://api.github.com/repos/PyMVPA/PyMVPA/pulls/504","id":87407095,"html_url":"https://github.com/PyMVPA/PyMVPA/pull/504","diff_url":"https://github.com/PyMVPA/PyMVPA/pull/504.diff","patch_url":"https://github.com/PyMVPA/PyMVPA/pull/504.patch","issue_url":"https://api.github.com/repos/PyMVPA/PyMVPA/issues/504","number":504,"state":"open","locked":false,"title":"ENH: Basic implementation of cross-validation rsm using learner and CV.","user":{"login":"swaroopgj","id":139211,"avatar_url":"https://avatars.githubusercontent.com/u/139211?v=3","gravatar_id":"","url":"https://api.github.com/users/swaroopgj","html_url":"https://github.com/swaroopgj","followers_url":"https://api.github.com/users/swaroopgj/followers","following_url":"https://api.github.com/users/swaroopgj/following{/other_user}","gists_url":"https://api.github.com/users/swaroopgj/gists{/gist_id}","starred_url":"https://api.github.com/users/swaroopgj/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/swaroopgj/subscriptions","organizations_url":"https://api.github.com/users/swaroopgj/orgs","repos_url":"https://api.github.com/users/swaroopgj/repos","events_url":"https://api.github.com/users/swaroopgj/events{/privacy}","received_events_url":"https://api.github.com/users/swaroopgj/received_events","type":"User","site_admin":false},"body":"This implements cross-validated RSA.\r\nAdds CDist as a measure that will eventually be passed to `CrossValidation` to get cvRSA.\r\n\r\nWith help from @mvdoc @snastase @feilong ","created_at":"2016-09-29T22:39:43Z","updated_at":"2016-10-04T21:11:19Z","closed_at":null,"merged_at":null,"merge_commit_sha":"c3fd347ba085da53053b66f36f5dfaad4a438c84","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/PyMVPA/PyMVPA/pulls/504/commits","review_comments_url":"https://api.github.com/repos/PyMVPA/PyMVPA/pulls/504/comments","review_comment_url":"https://api.github.com/repos/PyMVPA/PyMVPA/pulls/comments{/number}","comments_url":"https://api.github.com/repos/PyMVPA/PyMVPA/issues/504/comments","statuses_url":"https://api.github.com/repos/PyMVPA/PyMVPA/statuses/ebced46968c3431abcc373bc2effe1bb49d7422d","head":{"label":"swaroopgj:cvRSA","ref":"cvRSA","sha":"ebced46968c3431abcc373bc2effe1bb49d7422d","user":{"login":"swaroopgj","id":139211,"avatar_url":"https://avatars.githubusercontent.com/u/139211?v=3","gravatar_id":"","url":"https://api.github.com/users/swaroopgj","html_url":"https://github.com/swaroopgj","followers_url":"https://api.github.com/users/swaroopgj/followers","following_url":"https://api.github.com/users/swaroopgj/following{/other_user}","gists_url":"https://api.github.com/users/swaroopgj/gists{/gist_id}","starred_url":"https://api.github.com/users/swaroopgj/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/swaroopgj/subscriptions","organizations_url":"https://api.github.com/users/swaroopgj/orgs","repos_url":"https://api.github.com/users/swaroopgj/repos","events_url":"https://api.github.com/users/swaroopgj/events{/privacy}","received_events_url":"https://api.github.com/users/swaroopgj/received_events","type":"User","site_admin":false},"repo":{"id":336044,"name":"PyMVPA","full_name":"swaroopgj/PyMVPA","owner":{"login":"swaroopgj","id":139211,"avatar_url":"https://avatars.githubusercontent.com/u/139211?v=3","gravatar_id":"","url":"https://api.github.com/users/swaroopgj","html_url":"https://github.com/swaroopgj","followers_url":"https://api.github.com/users/swaroopgj/followers","following_url":"https://api.github.com/users/swaroopgj/following{/other_user}","gists_url":"https://api.github.com/users/swaroopgj/gists{/gist_id}","starred_url":"https://api.github.com/users/swaroopgj/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/swaroopgj/subscriptions","organizations_url":"https://api.github.com/users/swaroopgj/orgs","repos_url":"https://api.github.com/users/swaroopgj/repos","events_url":"https://api.github.com/users/swaroopgj/events{/privacy}","received_events_url":"https://api.github.com/users/swaroopgj/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/swaroopgj/PyMVPA","description":"multivariate pattern analysis in Python","fork":true,"url":"https://api.github.com/repos/swaroopgj/PyMVPA","forks_url":"https://api.github.com/repos/swaroopgj/PyMVPA/forks","keys_url":"https://api.github.com/repos/swaroopgj/PyMVPA/keys{/key_id}","collaborators_url":"https://api.github.com/repos/swaroopgj/PyMVPA/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/swaroopgj/PyMVPA/teams","hooks_url":"https://api.github.com/repos/swaroopgj/PyMVPA/hooks","issue_events_url":"https://api.github.com/repos/swaroopgj/PyMVPA/issues/events{/number}","events_url":"https://api.github.com/repos/swaroopgj/PyMVPA/events","assignees_url":"https://api.github.com/repos/swaroopgj/PyMVPA/assignees{/user}","branches_url":"https://api.github.com/repos/swaroopgj/PyMVPA/branches{/branch}","tags_url":"https://api.github.com/repos/swaroopgj/PyMVPA/tags","blobs_url":"https://api.github.com/repos/swaroopgj/PyMVPA/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/swaroopgj/PyMVPA/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/swaroopgj/PyMVPA/git/refs{/sha}","trees_url":"https://api.github.com/repos/swaroopgj/PyMVPA/git/trees{/sha}","statuses_url":"https://api.github.com/repos/swaroopgj/PyMVPA/statuses/{sha}","languages_url":"https://api.github.com/repos/swaroopgj/PyMVPA/languages","stargazers_url":"https://api.github.com/repos/swaroopgj/PyMVPA/stargazers","contributors_url":"https://api.github.com/repos/swaroopgj/PyMVPA/contributors","subscribers_url":"https://api.github.com/repos/swaroopgj/PyMVPA/subscribers","subscription_url":"https://api.github.com/repos/swaroopgj/PyMVPA/subscription","commits_url":"https://api.github.com/repos/swaroopgj/PyMVPA/commits{/sha}","git_commits_url":"https://api.github.com/repos/swaroopgj/PyMVPA/git/commits{/sha}","comments_url":"https://api.github.com/repos/swaroopgj/PyMVPA/comments{/number}","issue_comment_url":"https://api.github.com/repos/swaroopgj/PyMVPA/issues/comments{/number}","contents_url":"https://api.github.com/repos/swaroopgj/PyMVPA/contents/{+path}","compare_url":"https://api.github.com/repos/swaroopgj/PyMVPA/compare/{base}...{head}","merges_url":"https://api.github.com/repos/swaroopgj/PyMVPA/merges","archive_url":"https://api.github.com/repos/swaroopgj/PyMVPA/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/swaroopgj/PyMVPA/downloads","issues_url":"https://api.github.com/repos/swaroopgj/PyMVPA/issues{/number}","pulls_url":"https://api.github.com/repos/swaroopgj/PyMVPA/pulls{/number}","milestones_url":"https://api.github.com/repos/swaroopgj/PyMVPA/milestones{/number}","notifications_url":"https://api.github.com/repos/swaroopgj/PyMVPA/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/swaroopgj/PyMVPA/labels{/name}","releases_url":"https://api.github.com/repos/swaroopgj/PyMVPA/releases{/id}","deployments_url":"https://api.github.com/repos/swaroopgj/PyMVPA/deployments","created_at":"2009-10-13T15:16:13Z","updated_at":"2015-02-23T20:01:22Z","pushed_at":"2016-10-04T19:15:36Z","git_url":"git://github.com/swaroopgj/PyMVPA.git","ssh_url":"git@github.com:swaroopgj/PyMVPA.git","clone_url":"https://github.com/swaroopgj/PyMVPA.git","svn_url":"https://github.com/swaroopgj/PyMVPA","homepage":"http://www.pymvpa.org","size":26131,"stargazers_count":1,"watchers_count":1,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":0,"forks":1,"open_issues":0,"watchers":1,"default_branch":"master"}},"base":{"label":"PyMVPA:master","ref":"master","sha":"73ddbd0744603ad65ea9f9443a8868c521ee4c14","user":{"login":"PyMVPA","id":341117,"avatar_url":"https://avatars.githubusercontent.com/u/341117?v=3","gravatar_id":"","url":"https://api.github.com/users/PyMVPA","html_url":"https://github.com/PyMVPA","followers_url":"https://api.github.com/users/PyMVPA/followers","following_url":"https://api.github.com/users/PyMVPA/following{/other_user}","gists_url":"https://api.github.com/users/PyMVPA/gists{/gist_id}","starred_url":"https://api.github.com/users/PyMVPA/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/PyMVPA/subscriptions","organizations_url":"https://api.github.com/users/PyMVPA/orgs","repos_url":"https://api.github.com/users/PyMVPA/repos","events_url":"https://api.github.com/users/PyMVPA/events{/privacy}","received_events_url":"https://api.github.com/users/PyMVPA/received_events","type":"Organization","site_admin":false},"repo":{"id":330077,"name":"PyMVPA","full_name":"PyMVPA/PyMVPA","owner":{"login":"PyMVPA","id":341117,"avatar_url":"https://avatars.githubusercontent.com/u/341117?v=3","gravatar_id":"","url":"https://api.github.com/users/PyMVPA","html_url":"https://github.com/PyMVPA","followers_url":"https://api.github.com/users/PyMVPA/followers","following_url":"https://api.github.com/users/PyMVPA/following{/other_user}","gists_url":"https://api.github.com/users/PyMVPA/gists{/gist_id}","starred_url":"https://api.github.com/users/PyMVPA/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/PyMVPA/subscriptions","organizations_url":"https://api.github.com/users/PyMVPA/orgs","repos_url":"https://api.github.com/users/PyMVPA/repos","events_url":"https://api.github.com/users/PyMVPA/events{/privacy}","received_events_url":"https://api.github.com/users/PyMVPA/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/PyMVPA/PyMVPA","description":"MultiVariate Pattern Analysis in Python","fork":false,"url":"https://api.github.com/repos/PyMVPA/PyMVPA","forks_url":"https://api.github.com/repos/PyMVPA/PyMVPA/forks","keys_url":"https://api.github.com/repos/PyMVPA/PyMVPA/keys{/key_id}","collaborators_url":"https://api.github.com/repos/PyMVPA/PyMVPA/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/PyMVPA/PyMVPA/teams","hooks_url":"https://api.github.com/repos/PyMVPA/PyMVPA/hooks","issue_events_url":"https://api.github.com/repos/PyMVPA/PyMVPA/issues/events{/number}","events_url":"https://api.github.com/repos/PyMVPA/PyMVPA/events","assignees_url":"https://api.github.com/repos/PyMVPA/PyMVPA/assignees{/user}","branches_url":"https://api.github.com/repos/PyMVPA/PyMVPA/branches{/branch}","tags_url":"https://api.github.com/repos/PyMVPA/PyMVPA/tags","blobs_url":"https://api.github.com/repos/PyMVPA/PyMVPA/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/PyMVPA/PyMVPA/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/PyMVPA/PyMVPA/git/refs{/sha}","trees_url":"https://api.github.com/repos/PyMVPA/PyMVPA/git/trees{/sha}","statuses_url":"https://api.github.com/repos/PyMVPA/PyMVPA/statuses/{sha}","languages_url":"https://api.github.com/repos/PyMVPA/PyMVPA/languages","stargazers_url":"https://api.github.com/repos/PyMVPA/PyMVPA/stargazers","contributors_url":"https://api.github.com/repos/PyMVPA/PyMVPA/contributors","subscribers_url":"https://api.github.com/repos/PyMVPA/PyMVPA/subscribers","subscription_url":"https://api.github.com/repos/PyMVPA/PyMVPA/subscription","commits_url":"https://api.github.com/repos/PyMVPA/PyMVPA/commits{/sha}","git_commits_url":"https://api.github.com/repos/PyMVPA/PyMVPA/git/commits{/sha}","comments_url":"https://api.github.com/repos/PyMVPA/PyMVPA/comments{/number}","issue_comment_url":"https://api.github.com/repos/PyMVPA/PyMVPA/issues/comments{/number}","contents_url":"https://api.github.com/repos/PyMVPA/PyMVPA/contents/{+path}","compare_url":"https://api.github.com/repos/PyMVPA/PyMVPA/compare/{base}...{head}","merges_url":"https://api.github.com/repos/PyMVPA/PyMVPA/merges","archive_url":"https://api.github.com/repos/PyMVPA/PyMVPA/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/PyMVPA/PyMVPA/downloads","issues_url":"https://api.github.com/repos/PyMVPA/PyMVPA/issues{/number}","pulls_url":"https://api.github.com/repos/PyMVPA/PyMVPA/pulls{/number}","milestones_url":"https://api.github.com/repos/PyMVPA/PyMVPA/milestones{/number}","notifications_url":"https://api.github.com/repos/PyMVPA/PyMVPA/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/PyMVPA/PyMVPA/labels{/name}","releases_url":"https://api.github.com/repos/PyMVPA/PyMVPA/releases{/id}","deployments_url":"https://api.github.com/repos/PyMVPA/PyMVPA/deployments","created_at":"2009-10-07T18:57:13Z","updated_at":"2016-09-22T23:33:23Z","pushed_at":"2016-10-04T19:15:37Z","git_url":"git://github.com/PyMVPA/PyMVPA.git","ssh_url":"git@github.com:PyMVPA/PyMVPA.git","clone_url":"https://github.com/PyMVPA/PyMVPA.git","svn_url":"https://github.com/PyMVPA/PyMVPA","homepage":"http://www.pymvpa.org","size":39920,"stargazers_count":140,"watchers_count":140,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":73,"mirror_url":null,"open_issues_count":89,"forks":73,"open_issues":89,"watchers":140,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/PyMVPA/PyMVPA/pulls/504"},"html":{"href":"https://github.com/PyMVPA/PyMVPA/pull/504"},"issue":{"href":"https://api.github.com/repos/PyMVPA/PyMVPA/issues/504"},"comments":{"href":"https://api.github.com/repos/PyMVPA/PyMVPA/issues/504/comments"},"review_comments":{"href":"https://api.github.com/repos/PyMVPA/PyMVPA/pulls/504/comments"},"review_comment":{"href":"https://api.github.com/repos/PyMVPA/PyMVPA/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/PyMVPA/PyMVPA/pulls/504/commits"},"statuses":{"href":"https://api.github.com/repos/PyMVPA/PyMVPA/statuses/ebced46968c3431abcc373bc2effe1bb49d7422d"}}}}
{"actor":{"display_login":"swaroopgj"}}