{"action":"created","comment":{"url":"https://api.github.com/repos/nipy/nipype/pulls/comments/78497908","id":78497908,"diff_hunk":"@@ -0,0 +1,178 @@\n+# -*- coding: utf-8 -*-\n+# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-\n+# vi: set ft=python sts=4 ts=4 sw=4 et:\n+'''\n+Miscellaneous algorithms\n+\n+    Change directory to provide relative paths for doctests\n+    >>> import os\n+    >>> filepath = os.path.dirname(os.path.realpath(__file__))\n+    >>> datadir = os.path.realpath(os.path.join(filepath, '../testing/data'))\n+    >>> os.chdir(datadir)\n+\n+'''\n+from ..interfaces.base import (BaseInterfaceInputSpec, TraitedSpec,\n+                               BaseInterface, traits, File)\n+from ..pipeline import engine as pe\n+from ..interfaces.utility import IdentityInterface\n+from .misc import regress_poly\n+\n+import nibabel as nb\n+import numpy as np\n+from scipy import linalg, stats\n+import os\n+\n+class CompCorInputSpec(BaseInterfaceInputSpec):\n+    realigned_file = File(exists=True, mandatory=True,\n+                          desc='already realigned brain image (4D)')\n+    mask_file = File(exists=True, mandatory=False,\n+                     desc='mask file that determines ROI (3D)')\n+    components_file = File('components_file.txt', exists=False,\n+                           mandatory=False, usedefault=True,\n+                           desc='filename to store physiological components')\n+    num_components = traits.Int(6, usedefault=True) # 6 for BOLD, 4 for ASL\n+    use_regress_poly = traits.Bool(True, usedefault=True,\n+                                   desc='use polynomial regression'\n+                                   'pre-component extraction')\n+    regress_poly_degree = traits.Range(low=1, default=1, usedefault=True,\n+                                       desc='the degree polynomial to use')\n+    extra_regressors = File(exists=True, mandatory=False,\n+                            desc='additional regressors to add')\n+\n+class CompCorOutputSpec(TraitedSpec):\n+    components_file = File(exists=True,\n+                           desc='text file containing the noise components')\n+\n+class CompCor(BaseInterface):\n+    '''\n+    Interface with core CompCor computation, used in aCompCor and tCompCor\n+\n+    Example\n+    -------\n+\n+    >>> ccinterface = CompCor()\n+    >>> ccinterface.inputs.realigned_file = 'functional.nii'\n+    >>> ccinterface.inputs.mask_file = 'mask.nii'\n+    >>> ccinterface.inputs.num_components = 1\n+    >>> ccinterface.inputs.use_regress_poly = True\n+    >>> ccinterface.inputs.regress_poly_degree = 2\n+    '''\n+    input_spec = CompCorInputSpec\n+    output_spec = CompCorOutputSpec\n+\n+    def _run_interface(self, runtime):\n+        imgseries = nb.load(self.inputs.realigned_file).get_data()\n+        mask = nb.load(self.inputs.mask_file).get_data()\n+        voxel_timecourses = imgseries[mask > 0]\n+        # Zero-out any bad values\n+        voxel_timecourses[np.isnan(np.sum(voxel_timecourses, axis=1)), :] = 0\n+\n+        # from paper:\n+        # \"The constant and linear trends of the columns in the matrix M were\n+        # removed [prior to ...]\"\n+        if self.inputs.use_regress_poly:\n+            voxel_timecourses = regress_poly(self.inputs.regress_poly_degree,\n+                                             voxel_timecourses)\n+        voxel_timecourses = voxel_timecourses - np.mean(voxel_timecourses,\n+                                                        axis=1)[:, np.newaxis]\n+\n+        # \"Voxel time series from the noise ROI (either anatomical or tSTD) were\n+        # placed in a matrix M of size Nxm, with time along the row dimension\n+        # and voxels along the column dimension.\"\n+        M = voxel_timecourses.T\n+        numvols = M.shape[0]\n+        numvoxels = M.shape[1]\n+\n+        # \"[... were removed] prior to column-wise variance normalization.\"\n+        M = M / self._compute_tSTD(M, 1.)\n+\n+        # \"The covariance matrix C = MMT was constructed and decomposed into its\n+        # principal components using a singular value decomposition.\"\n+        u, _, _ = linalg.svd(M, full_matrices=False)\n+        components = u[:, :self.inputs.num_components]\n+        if self.inputs.extra_regressors:\n+            components = self._add_extras(components,\n+                                          self.inputs.extra_regressors)\n+\n+        components_file = os.path.join(os.getcwd(), self.inputs.components_file)\n+        np.savetxt(components_file, components, fmt=\"%.10f\")\n+        return runtime\n+\n+    def _list_outputs(self):\n+        outputs = self._outputs().get()\n+        outputs['components_file'] = os.path.abspath(self.inputs.components_file)\n+        return outputs\n+\n+    def _compute_tSTD(self, M, x):\n+        stdM = np.std(M, axis=0)\n+        # set bad values to x\n+        stdM[stdM == 0] = x\n+        stdM[np.isnan(stdM)] = x\n+        stdM[np.isinf(stdM)] = x\n+        return stdM\n+\n+    def _add_extras(self, components, extra_regressors):\n+            regressors = np.genfromtxt(self.inputs.extra_regressors)\n+            return np.hstack((components, regressors))\n+\n+class TCompCorInputSpec(CompCorInputSpec):\n+    # and all the fields in CompCorInputSpec\n+    percentile_threshold = traits.Range(low=0., high=1., value=.02,\n+                                        exclude_low=True, exclude_high=True,\n+                                        usedefault=True, desc='the percentile '\n+                                        'used to select highest-variance '\n+                                        'voxels. By default the 2% of voxels '","path":"nipype/algorithms/compcor.py","position":124,"original_position":124,"commit_id":"53478811b8dfbad2f93a36fdcc788e8029173f0a","original_commit_id":"53478811b8dfbad2f93a36fdcc788e8029173f0a","user":{"login":"shoshber","id":1402948,"avatar_url":"https://avatars.githubusercontent.com/u/1402948?v=3","gravatar_id":"","url":"https://api.github.com/users/shoshber","html_url":"https://github.com/shoshber","followers_url":"https://api.github.com/users/shoshber/followers","following_url":"https://api.github.com/users/shoshber/following{/other_user}","gists_url":"https://api.github.com/users/shoshber/gists{/gist_id}","starred_url":"https://api.github.com/users/shoshber/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shoshber/subscriptions","organizations_url":"https://api.github.com/users/shoshber/orgs","repos_url":"https://api.github.com/users/shoshber/repos","events_url":"https://api.github.com/users/shoshber/events{/privacy}","received_events_url":"https://api.github.com/users/shoshber/received_events","type":"User","site_admin":false},"body":"I'll fix the documentation.","created_at":"2016-09-13T05:21:25Z","updated_at":"2016-09-13T05:21:25Z","html_url":"https://github.com/nipy/nipype/pull/1599#discussion_r78497908","pull_request_url":"https://api.github.com/repos/nipy/nipype/pulls/1599","_links":{"self":{"href":"https://api.github.com/repos/nipy/nipype/pulls/comments/78497908"},"html":{"href":"https://github.com/nipy/nipype/pull/1599#discussion_r78497908"},"pull_request":{"href":"https://api.github.com/repos/nipy/nipype/pulls/1599"}}},"pull_request":{"url":"https://api.github.com/repos/nipy/nipype/pulls/1599","id":83561748,"html_url":"https://github.com/nipy/nipype/pull/1599","diff_url":"https://github.com/nipy/nipype/pull/1599.diff","patch_url":"https://github.com/nipy/nipype/pull/1599.patch","issue_url":"https://api.github.com/repos/nipy/nipype/issues/1599","number":1599,"state":"open","locked":false,"title":"WIP CompCor","user":{"login":"shoshber","id":1402948,"avatar_url":"https://avatars.githubusercontent.com/u/1402948?v=3","gravatar_id":"","url":"https://api.github.com/users/shoshber","html_url":"https://github.com/shoshber","followers_url":"https://api.github.com/users/shoshber/followers","following_url":"https://api.github.com/users/shoshber/following{/other_user}","gists_url":"https://api.github.com/users/shoshber/gists{/gist_id}","starred_url":"https://api.github.com/users/shoshber/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shoshber/subscriptions","organizations_url":"https://api.github.com/users/shoshber/orgs","repos_url":"https://api.github.com/users/shoshber/repos","events_url":"https://api.github.com/users/shoshber/events{/privacy}","received_events_url":"https://api.github.com/users/shoshber/received_events","type":"User","site_admin":false},"body":"Putting this up early","created_at":"2016-09-01T02:42:21Z","updated_at":"2016-09-13T05:21:25Z","closed_at":null,"merged_at":null,"merge_commit_sha":"fe1e7c41e5426f1c89fbbad5102b9a2fd8ea6325","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/nipy/nipype/pulls/1599/commits","review_comments_url":"https://api.github.com/repos/nipy/nipype/pulls/1599/comments","review_comment_url":"https://api.github.com/repos/nipy/nipype/pulls/comments{/number}","comments_url":"https://api.github.com/repos/nipy/nipype/issues/1599/comments","statuses_url":"https://api.github.com/repos/nipy/nipype/statuses/53478811b8dfbad2f93a36fdcc788e8029173f0a","head":{"label":"shoshber:compcor1594","ref":"compcor1594","sha":"53478811b8dfbad2f93a36fdcc788e8029173f0a","user":{"login":"shoshber","id":1402948,"avatar_url":"https://avatars.githubusercontent.com/u/1402948?v=3","gravatar_id":"","url":"https://api.github.com/users/shoshber","html_url":"https://github.com/shoshber","followers_url":"https://api.github.com/users/shoshber/followers","following_url":"https://api.github.com/users/shoshber/following{/other_user}","gists_url":"https://api.github.com/users/shoshber/gists{/gist_id}","starred_url":"https://api.github.com/users/shoshber/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shoshber/subscriptions","organizations_url":"https://api.github.com/users/shoshber/orgs","repos_url":"https://api.github.com/users/shoshber/repos","events_url":"https://api.github.com/users/shoshber/events{/privacy}","received_events_url":"https://api.github.com/users/shoshber/received_events","type":"User","site_admin":false},"repo":{"id":59062696,"name":"nipype","full_name":"shoshber/nipype","owner":{"login":"shoshber","id":1402948,"avatar_url":"https://avatars.githubusercontent.com/u/1402948?v=3","gravatar_id":"","url":"https://api.github.com/users/shoshber","html_url":"https://github.com/shoshber","followers_url":"https://api.github.com/users/shoshber/followers","following_url":"https://api.github.com/users/shoshber/following{/other_user}","gists_url":"https://api.github.com/users/shoshber/gists{/gist_id}","starred_url":"https://api.github.com/users/shoshber/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shoshber/subscriptions","organizations_url":"https://api.github.com/users/shoshber/orgs","repos_url":"https://api.github.com/users/shoshber/repos","events_url":"https://api.github.com/users/shoshber/events{/privacy}","received_events_url":"https://api.github.com/users/shoshber/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/shoshber/nipype","description":"Workflows and interfaces for neuroimaging packages","fork":true,"url":"https://api.github.com/repos/shoshber/nipype","forks_url":"https://api.github.com/repos/shoshber/nipype/forks","keys_url":"https://api.github.com/repos/shoshber/nipype/keys{/key_id}","collaborators_url":"https://api.github.com/repos/shoshber/nipype/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/shoshber/nipype/teams","hooks_url":"https://api.github.com/repos/shoshber/nipype/hooks","issue_events_url":"https://api.github.com/repos/shoshber/nipype/issues/events{/number}","events_url":"https://api.github.com/repos/shoshber/nipype/events","assignees_url":"https://api.github.com/repos/shoshber/nipype/assignees{/user}","branches_url":"https://api.github.com/repos/shoshber/nipype/branches{/branch}","tags_url":"https://api.github.com/repos/shoshber/nipype/tags","blobs_url":"https://api.github.com/repos/shoshber/nipype/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/shoshber/nipype/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/shoshber/nipype/git/refs{/sha}","trees_url":"https://api.github.com/repos/shoshber/nipype/git/trees{/sha}","statuses_url":"https://api.github.com/repos/shoshber/nipype/statuses/{sha}","languages_url":"https://api.github.com/repos/shoshber/nipype/languages","stargazers_url":"https://api.github.com/repos/shoshber/nipype/stargazers","contributors_url":"https://api.github.com/repos/shoshber/nipype/contributors","subscribers_url":"https://api.github.com/repos/shoshber/nipype/subscribers","subscription_url":"https://api.github.com/repos/shoshber/nipype/subscription","commits_url":"https://api.github.com/repos/shoshber/nipype/commits{/sha}","git_commits_url":"https://api.github.com/repos/shoshber/nipype/git/commits{/sha}","comments_url":"https://api.github.com/repos/shoshber/nipype/comments{/number}","issue_comment_url":"https://api.github.com/repos/shoshber/nipype/issues/comments{/number}","contents_url":"https://api.github.com/repos/shoshber/nipype/contents/{+path}","compare_url":"https://api.github.com/repos/shoshber/nipype/compare/{base}...{head}","merges_url":"https://api.github.com/repos/shoshber/nipype/merges","archive_url":"https://api.github.com/repos/shoshber/nipype/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/shoshber/nipype/downloads","issues_url":"https://api.github.com/repos/shoshber/nipype/issues{/number}","pulls_url":"https://api.github.com/repos/shoshber/nipype/pulls{/number}","milestones_url":"https://api.github.com/repos/shoshber/nipype/milestones{/number}","notifications_url":"https://api.github.com/repos/shoshber/nipype/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/shoshber/nipype/labels{/name}","releases_url":"https://api.github.com/repos/shoshber/nipype/releases{/id}","deployments_url":"https://api.github.com/repos/shoshber/nipype/deployments","created_at":"2016-05-17T22:10:15Z","updated_at":"2016-05-17T22:10:19Z","pushed_at":"2016-09-12T06:28:12Z","git_url":"git://github.com/shoshber/nipype.git","ssh_url":"git@github.com:shoshber/nipype.git","clone_url":"https://github.com/shoshber/nipype.git","svn_url":"https://github.com/shoshber/nipype","homepage":"http://nipype.readthedocs.org/en/latest/","size":64932,"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":"nipy:master","ref":"master","sha":"10f28fe28df27e5ffc59930143ddec6caf47bb11","user":{"login":"nipy","id":233707,"avatar_url":"https://avatars.githubusercontent.com/u/233707?v=3","gravatar_id":"","url":"https://api.github.com/users/nipy","html_url":"https://github.com/nipy","followers_url":"https://api.github.com/users/nipy/followers","following_url":"https://api.github.com/users/nipy/following{/other_user}","gists_url":"https://api.github.com/users/nipy/gists{/gist_id}","starred_url":"https://api.github.com/users/nipy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nipy/subscriptions","organizations_url":"https://api.github.com/users/nipy/orgs","repos_url":"https://api.github.com/users/nipy/repos","events_url":"https://api.github.com/users/nipy/events{/privacy}","received_events_url":"https://api.github.com/users/nipy/received_events","type":"Organization","site_admin":false},"repo":{"id":791477,"name":"nipype","full_name":"nipy/nipype","owner":{"login":"nipy","id":233707,"avatar_url":"https://avatars.githubusercontent.com/u/233707?v=3","gravatar_id":"","url":"https://api.github.com/users/nipy","html_url":"https://github.com/nipy","followers_url":"https://api.github.com/users/nipy/followers","following_url":"https://api.github.com/users/nipy/following{/other_user}","gists_url":"https://api.github.com/users/nipy/gists{/gist_id}","starred_url":"https://api.github.com/users/nipy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nipy/subscriptions","organizations_url":"https://api.github.com/users/nipy/orgs","repos_url":"https://api.github.com/users/nipy/repos","events_url":"https://api.github.com/users/nipy/events{/privacy}","received_events_url":"https://api.github.com/users/nipy/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/nipy/nipype","description":"Workflows and interfaces for neuroimaging packages","fork":false,"url":"https://api.github.com/repos/nipy/nipype","forks_url":"https://api.github.com/repos/nipy/nipype/forks","keys_url":"https://api.github.com/repos/nipy/nipype/keys{/key_id}","collaborators_url":"https://api.github.com/repos/nipy/nipype/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/nipy/nipype/teams","hooks_url":"https://api.github.com/repos/nipy/nipype/hooks","issue_events_url":"https://api.github.com/repos/nipy/nipype/issues/events{/number}","events_url":"https://api.github.com/repos/nipy/nipype/events","assignees_url":"https://api.github.com/repos/nipy/nipype/assignees{/user}","branches_url":"https://api.github.com/repos/nipy/nipype/branches{/branch}","tags_url":"https://api.github.com/repos/nipy/nipype/tags","blobs_url":"https://api.github.com/repos/nipy/nipype/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/nipy/nipype/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/nipy/nipype/git/refs{/sha}","trees_url":"https://api.github.com/repos/nipy/nipype/git/trees{/sha}","statuses_url":"https://api.github.com/repos/nipy/nipype/statuses/{sha}","languages_url":"https://api.github.com/repos/nipy/nipype/languages","stargazers_url":"https://api.github.com/repos/nipy/nipype/stargazers","contributors_url":"https://api.github.com/repos/nipy/nipype/contributors","subscribers_url":"https://api.github.com/repos/nipy/nipype/subscribers","subscription_url":"https://api.github.com/repos/nipy/nipype/subscription","commits_url":"https://api.github.com/repos/nipy/nipype/commits{/sha}","git_commits_url":"https://api.github.com/repos/nipy/nipype/git/commits{/sha}","comments_url":"https://api.github.com/repos/nipy/nipype/comments{/number}","issue_comment_url":"https://api.github.com/repos/nipy/nipype/issues/comments{/number}","contents_url":"https://api.github.com/repos/nipy/nipype/contents/{+path}","compare_url":"https://api.github.com/repos/nipy/nipype/compare/{base}...{head}","merges_url":"https://api.github.com/repos/nipy/nipype/merges","archive_url":"https://api.github.com/repos/nipy/nipype/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/nipy/nipype/downloads","issues_url":"https://api.github.com/repos/nipy/nipype/issues{/number}","pulls_url":"https://api.github.com/repos/nipy/nipype/pulls{/number}","milestones_url":"https://api.github.com/repos/nipy/nipype/milestones{/number}","notifications_url":"https://api.github.com/repos/nipy/nipype/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/nipy/nipype/labels{/name}","releases_url":"https://api.github.com/repos/nipy/nipype/releases{/id}","deployments_url":"https://api.github.com/repos/nipy/nipype/deployments","created_at":"2010-07-22T17:06:49Z","updated_at":"2016-09-12T17:50:12Z","pushed_at":"2016-09-13T03:58:53Z","git_url":"git://github.com/nipy/nipype.git","ssh_url":"git@github.com:nipy/nipype.git","clone_url":"https://github.com/nipy/nipype.git","svn_url":"https://github.com/nipy/nipype","homepage":"http://nipype.readthedocs.org/en/latest/","size":69341,"stargazers_count":199,"watchers_count":199,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":227,"mirror_url":null,"open_issues_count":268,"forks":227,"open_issues":268,"watchers":199,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/nipy/nipype/pulls/1599"},"html":{"href":"https://github.com/nipy/nipype/pull/1599"},"issue":{"href":"https://api.github.com/repos/nipy/nipype/issues/1599"},"comments":{"href":"https://api.github.com/repos/nipy/nipype/issues/1599/comments"},"review_comments":{"href":"https://api.github.com/repos/nipy/nipype/pulls/1599/comments"},"review_comment":{"href":"https://api.github.com/repos/nipy/nipype/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/nipy/nipype/pulls/1599/commits"},"statuses":{"href":"https://api.github.com/repos/nipy/nipype/statuses/53478811b8dfbad2f93a36fdcc788e8029173f0a"}}}}
{"actor":{"display_login":"shoshber"}}