{"action":"created","comment":{"url":"https://api.github.com/repos/nilearn/nilearn/pulls/comments/46589269","id":46589269,"diff_hunk":"@@ -0,0 +1,313 @@\n+\"\"\"\n+Better brain parcellations for Region of Interest analysis\n+\"\"\"\n+import warnings\n+import numbers\n+import nibabel\n+import numpy as np\n+\n+from scipy.ndimage import label\n+from scipy.stats import scoreatpercentile\n+\n+from sklearn.base import clone\n+from sklearn.externals.joblib import Memory\n+\n+from .. import masking\n+from ..input_data import NiftiMapsMasker\n+from .._utils import check_niimg, check_niimg_4d\n+from ..image import new_img_like, resample_img\n+from ..image.image import _smooth_array, threshold_img\n+from .._utils.niimg_conversions import concat_niimgs, _check_same_fov\n+from .._utils.compat import _basestring\n+from .._utils.ndimage import _peak_local_max\n+from .._utils.segmentation import _random_walker\n+\n+\n+def _threshold_maps(maps_img, threshold):\n+    \"\"\" Automatic thresholding of atlas maps image.\n+\n+    Considers the given threshold as a ratio to the total number of voxels\n+    in the brain volume. This gives a certain number within the data\n+    voxel size which means that nonzero voxels which fall above than this\n+    size will be kept across all the maps.\n+\n+    Parameters\n+    ----------\n+    maps_img: Niimg-like object\n+        an image of brain atlas maps.\n+    threshold: float\n+        value used as a ratio to n_voxels to get a certain threshold size in\n+        number to threshold the image. The value should be positive and within\n+        the range of number of maps (i.e. n_maps in 4th dimension).\n+\n+    Returns\n+    -------\n+    threshold_maps_img: Nifti1Image object\n+        gives us thresholded image.\n+    \"\"\"\n+    maps = check_niimg(maps_img)\n+    n_maps = maps.shape[-1]\n+    if not isinstance(threshold, float) or threshold <= 0 or threshold > maps.shape[-1]:\n+        raise ValueError(\"threshold given as ratio to the number of voxels must \"\n+                         \"be float value and should be positive and between 0 and \"\n+                         \"total number of maps i.e. n_maps={0}. \"\n+                         \"You provided {1}\".format(maps.shape[-1], threshold))\n+    else:\n+        ratio = threshold\n+\n+    maps_data = maps.get_data()\n+    abs_maps = np.abs(maps_data)\n+    # thresholding\n+    cutoff_threshold = scoreatpercentile(\n+        abs_maps, 100. - (100. / n_maps) * ratio)\n+    maps_data[abs_maps < cutoff_threshold] = 0.\n+\n+    threshold_maps_img = new_img_like(maps, maps_data)\n+\n+    return threshold_maps_img\n+\n+\n+def connected_regions(maps_img, min_region_size=50, extract_type='local_regions',\n+                      smoothing_fwhm=6, mask_img=None):\n+    \"\"\" Extraction of brain connected regions into separate regions.\n+\n+    Parameters\n+    ----------\n+    maps_img: Niimg-like object\n+        an image of brain activation or atlas maps to be extracted into set of\n+        separate brain regions.\n+\n+    min_region_size: int, default 50, optional\n+        Minimum number of voxels for a region to be kept. Useful to suppress\n+        small spurious regions.\n+\n+    extract_type: str {'connected_components', 'local_regions'} \\\n+        default local_regions, optional\n+        If 'connected_components', each component/region in the image is extracted\n+        automatically by labelling each region based upon the presence of unique\n+        features in their respective regions.\n+        If 'local_regions', each component/region is extracted based on their\n+        maximum peak value to define a seed marker and then using random walker\n+        segementation algorithm on these markers for region separation.\n+\n+    smoothing_fwhm: scalar, default 6mm, optional\n+        To smooth an image to extract most sparser regions. This parameter\n+        is passed `_smooth_array` and exists only for extract_type 'local_regions'.\n+\n+    mask_img: Niimg-like object, default None\n+        If given, mask image is applied to input data.\n+        If None, no masking is applied.\n+\n+    Returns\n+    -------\n+    regions_extracted_img: Nifti1Image object\n+        gives the image in 4D of extracted brain regions. Each 3D image consists\n+        of only one separated region.\n+\n+    index_of_each_map: numpy array\n+        an array of list of indices where each index denotes the identity\n+        of each extracted region to their family of brain maps.\n+    \"\"\"\n+    all_regions_imgs = []\n+    index_of_each_map = []\n+    maps_img = check_niimg(maps_img, atleast_4d=True)\n+    maps = maps_img.get_data()\n+    affine = maps_img.get_affine()\n+\n+    allowed_extract_types = ['connected_components', 'local_regions']\n+    if extract_type not in allowed_extract_types:\n+        message = (\"'extract_type' should be given either of these {0} \"\n+                   \"You provided extract_type='{1}'\").format(allowed_extract_types, extract_type)\n+        raise ValueError(message)\n+\n+    if mask_img is not None:\n+        if not _check_same_fov(maps_img, mask_img):\n+            mask_img = resample_img(mask_img,\n+                                    target_affine=maps_img.get_affine(),\n+                                    target_shape=maps_img.shape[:3],\n+                                    interpolation=\"nearest\")\n+            mask_data, _ = masking._load_mask_img(mask_img)\n+            # Set as 0 to the values which are outside of the mask\n+            maps[mask_data == 0.] = 0.\n+\n+    for index in range(maps.shape[-1]):\n+        regions = []\n+        map_3d = maps[..., index]\n+        # Mark the seeds using random walker\n+        if extract_type == 'local_regions':\n+            smooth_map = _smooth_array(map_3d, affine=affine, fwhm=smoothing_fwhm)\n+            seeds = _peak_local_max(smooth_map)\n+            seeds_label, seeds_id = label(seeds)\n+            # Assign -1 to values which are 0. to indicate to ignore\n+            seeds_label[map_3d == 0.] = -1\n+            rw_maps = _random_walker(map_3d, seeds_label)\n+            # Now simply replace \"-1\" with \"0\" for regions seperation","path":"nilearn/regions/region_extractor.py","position":144,"original_position":144,"commit_id":"b851452bfc933f78ee1c4e42188744a8c5f14c52","original_commit_id":"b851452bfc933f78ee1c4e42188744a8c5f14c52","user":{"login":"lesteve","id":1680079,"avatar_url":"https://avatars.githubusercontent.com/u/1680079?v=3","gravatar_id":"","url":"https://api.github.com/users/lesteve","html_url":"https://github.com/lesteve","followers_url":"https://api.github.com/users/lesteve/followers","following_url":"https://api.github.com/users/lesteve/following{/other_user}","gists_url":"https://api.github.com/users/lesteve/gists{/gist_id}","starred_url":"https://api.github.com/users/lesteve/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lesteve/subscriptions","organizations_url":"https://api.github.com/users/lesteve/orgs","repos_url":"https://api.github.com/users/lesteve/repos","events_url":"https://api.github.com/users/lesteve/events{/privacy}","received_events_url":"https://api.github.com/users/lesteve/received_events","type":"User","site_admin":false},"body":"still a seperat -> separat","created_at":"2015-12-03T18:19:46Z","updated_at":"2015-12-03T18:19:46Z","html_url":"https://github.com/nilearn/nilearn/pull/651#discussion_r46589269","pull_request_url":"https://api.github.com/repos/nilearn/nilearn/pulls/651","_links":{"self":{"href":"https://api.github.com/repos/nilearn/nilearn/pulls/comments/46589269"},"html":{"href":"https://github.com/nilearn/nilearn/pull/651#discussion_r46589269"},"pull_request":{"href":"https://api.github.com/repos/nilearn/nilearn/pulls/651"}}},"pull_request":{"url":"https://api.github.com/repos/nilearn/nilearn/pulls/651","id":39828251,"html_url":"https://github.com/nilearn/nilearn/pull/651","diff_url":"https://github.com/nilearn/nilearn/pull/651.diff","patch_url":"https://github.com/nilearn/nilearn/pull/651.patch","issue_url":"https://api.github.com/repos/nilearn/nilearn/issues/651","number":651,"state":"open","locked":false,"title":"[MRG] Region Extractor","user":{"login":"KamalakerDadi","id":11410385,"avatar_url":"https://avatars.githubusercontent.com/u/11410385?v=3","gravatar_id":"","url":"https://api.github.com/users/KamalakerDadi","html_url":"https://github.com/KamalakerDadi","followers_url":"https://api.github.com/users/KamalakerDadi/followers","following_url":"https://api.github.com/users/KamalakerDadi/following{/other_user}","gists_url":"https://api.github.com/users/KamalakerDadi/gists{/gist_id}","starred_url":"https://api.github.com/users/KamalakerDadi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/KamalakerDadi/subscriptions","organizations_url":"https://api.github.com/users/KamalakerDadi/orgs","repos_url":"https://api.github.com/users/KamalakerDadi/repos","events_url":"https://api.github.com/users/KamalakerDadi/events{/privacy}","received_events_url":"https://api.github.com/users/KamalakerDadi/received_events","type":"User","site_admin":false},"body":"Fix #633 \r\nTo Do List:\r\n- [x] Code\r\n- [x] Documentation\r\n- [x] Tests\r\n- [x] Example\r\n\r\n* Code design\r\n   * ```class``` named as ```RegionExtractor``` - to *extract regions* from the maps and also extracts *subjects timeseries signals* from those extracted regions by inheriting ```NiftiMapsMasker```.\r\n         * function named as ```threshold_img``` to keep meaningful voxels for region extraction.\r\n         * function named as ```connected_regions``` to extract regions from thresholded images.\r\n\r\n*  ``` RegionExtractor ```\r\n      * ```fit()``` extract regions.\r\n      * ```transform()``` regions to signals.\r\n         * example: ```plot_extract_regions_canica_maps.py``` \r\n         * example: ```plot_extract_regions_smith_atlas_networks.py```\r\n\r\n*  ```threshold_img``` - two different ways of thresholding. (integrated in ```nilearn.image.image```)\r\n      1. ```percentile``` - ```scoreatpercentile```.\r\n      2. ```img_value``` - based on voxel intensity value.\r\n          * example: ```plot_extract_regions_statistical_maps.py```\r\n\r\n*  ```connected_regions``` - two different ways of extracting regions.\r\n      1. ```local_regions``` uses random walker method.\r\n      2. ```connected_components``` uses only labelling which labels based on unique features. \r\n           * example: ```plot_extract_regions_statistical_maps.py```\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n","created_at":"2015-07-13T16:08:17Z","updated_at":"2015-12-03T18:19:46Z","closed_at":null,"merged_at":null,"merge_commit_sha":"f8d036bd464aba6c511c0781d662677dd3285ad1","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/nilearn/nilearn/pulls/651/commits","review_comments_url":"https://api.github.com/repos/nilearn/nilearn/pulls/651/comments","review_comment_url":"https://api.github.com/repos/nilearn/nilearn/pulls/comments{/number}","comments_url":"https://api.github.com/repos/nilearn/nilearn/issues/651/comments","statuses_url":"https://api.github.com/repos/nilearn/nilearn/statuses/b851452bfc933f78ee1c4e42188744a8c5f14c52","head":{"label":"KamalakerDadi:AutoExtractor","ref":"AutoExtractor","sha":"b851452bfc933f78ee1c4e42188744a8c5f14c52","user":{"login":"KamalakerDadi","id":11410385,"avatar_url":"https://avatars.githubusercontent.com/u/11410385?v=3","gravatar_id":"","url":"https://api.github.com/users/KamalakerDadi","html_url":"https://github.com/KamalakerDadi","followers_url":"https://api.github.com/users/KamalakerDadi/followers","following_url":"https://api.github.com/users/KamalakerDadi/following{/other_user}","gists_url":"https://api.github.com/users/KamalakerDadi/gists{/gist_id}","starred_url":"https://api.github.com/users/KamalakerDadi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/KamalakerDadi/subscriptions","organizations_url":"https://api.github.com/users/KamalakerDadi/orgs","repos_url":"https://api.github.com/users/KamalakerDadi/repos","events_url":"https://api.github.com/users/KamalakerDadi/events{/privacy}","received_events_url":"https://api.github.com/users/KamalakerDadi/received_events","type":"User","site_admin":false},"repo":{"id":32379132,"name":"nilearn","full_name":"KamalakerDadi/nilearn","owner":{"login":"KamalakerDadi","id":11410385,"avatar_url":"https://avatars.githubusercontent.com/u/11410385?v=3","gravatar_id":"","url":"https://api.github.com/users/KamalakerDadi","html_url":"https://github.com/KamalakerDadi","followers_url":"https://api.github.com/users/KamalakerDadi/followers","following_url":"https://api.github.com/users/KamalakerDadi/following{/other_user}","gists_url":"https://api.github.com/users/KamalakerDadi/gists{/gist_id}","starred_url":"https://api.github.com/users/KamalakerDadi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/KamalakerDadi/subscriptions","organizations_url":"https://api.github.com/users/KamalakerDadi/orgs","repos_url":"https://api.github.com/users/KamalakerDadi/repos","events_url":"https://api.github.com/users/KamalakerDadi/events{/privacy}","received_events_url":"https://api.github.com/users/KamalakerDadi/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/KamalakerDadi/nilearn","description":"Machine learning for NeuroImaging in Python","fork":true,"url":"https://api.github.com/repos/KamalakerDadi/nilearn","forks_url":"https://api.github.com/repos/KamalakerDadi/nilearn/forks","keys_url":"https://api.github.com/repos/KamalakerDadi/nilearn/keys{/key_id}","collaborators_url":"https://api.github.com/repos/KamalakerDadi/nilearn/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/KamalakerDadi/nilearn/teams","hooks_url":"https://api.github.com/repos/KamalakerDadi/nilearn/hooks","issue_events_url":"https://api.github.com/repos/KamalakerDadi/nilearn/issues/events{/number}","events_url":"https://api.github.com/repos/KamalakerDadi/nilearn/events","assignees_url":"https://api.github.com/repos/KamalakerDadi/nilearn/assignees{/user}","branches_url":"https://api.github.com/repos/KamalakerDadi/nilearn/branches{/branch}","tags_url":"https://api.github.com/repos/KamalakerDadi/nilearn/tags","blobs_url":"https://api.github.com/repos/KamalakerDadi/nilearn/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/KamalakerDadi/nilearn/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/KamalakerDadi/nilearn/git/refs{/sha}","trees_url":"https://api.github.com/repos/KamalakerDadi/nilearn/git/trees{/sha}","statuses_url":"https://api.github.com/repos/KamalakerDadi/nilearn/statuses/{sha}","languages_url":"https://api.github.com/repos/KamalakerDadi/nilearn/languages","stargazers_url":"https://api.github.com/repos/KamalakerDadi/nilearn/stargazers","contributors_url":"https://api.github.com/repos/KamalakerDadi/nilearn/contributors","subscribers_url":"https://api.github.com/repos/KamalakerDadi/nilearn/subscribers","subscription_url":"https://api.github.com/repos/KamalakerDadi/nilearn/subscription","commits_url":"https://api.github.com/repos/KamalakerDadi/nilearn/commits{/sha}","git_commits_url":"https://api.github.com/repos/KamalakerDadi/nilearn/git/commits{/sha}","comments_url":"https://api.github.com/repos/KamalakerDadi/nilearn/comments{/number}","issue_comment_url":"https://api.github.com/repos/KamalakerDadi/nilearn/issues/comments{/number}","contents_url":"https://api.github.com/repos/KamalakerDadi/nilearn/contents/{+path}","compare_url":"https://api.github.com/repos/KamalakerDadi/nilearn/compare/{base}...{head}","merges_url":"https://api.github.com/repos/KamalakerDadi/nilearn/merges","archive_url":"https://api.github.com/repos/KamalakerDadi/nilearn/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/KamalakerDadi/nilearn/downloads","issues_url":"https://api.github.com/repos/KamalakerDadi/nilearn/issues{/number}","pulls_url":"https://api.github.com/repos/KamalakerDadi/nilearn/pulls{/number}","milestones_url":"https://api.github.com/repos/KamalakerDadi/nilearn/milestones{/number}","notifications_url":"https://api.github.com/repos/KamalakerDadi/nilearn/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/KamalakerDadi/nilearn/labels{/name}","releases_url":"https://api.github.com/repos/KamalakerDadi/nilearn/releases{/id}","created_at":"2015-03-17T07:55:36Z","updated_at":"2015-04-29T07:58:19Z","pushed_at":"2015-12-03T15:39:43Z","git_url":"git://github.com/KamalakerDadi/nilearn.git","ssh_url":"git@github.com:KamalakerDadi/nilearn.git","clone_url":"https://github.com/KamalakerDadi/nilearn.git","svn_url":"https://github.com/KamalakerDadi/nilearn","homepage":"http://nilearn.github.io","size":8412,"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":"nilearn:master","ref":"master","sha":"21b6c6a72cd60134b6391ae23a0dd34963899775","user":{"login":"nilearn","id":4646912,"avatar_url":"https://avatars.githubusercontent.com/u/4646912?v=3","gravatar_id":"","url":"https://api.github.com/users/nilearn","html_url":"https://github.com/nilearn","followers_url":"https://api.github.com/users/nilearn/followers","following_url":"https://api.github.com/users/nilearn/following{/other_user}","gists_url":"https://api.github.com/users/nilearn/gists{/gist_id}","starred_url":"https://api.github.com/users/nilearn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nilearn/subscriptions","organizations_url":"https://api.github.com/users/nilearn/orgs","repos_url":"https://api.github.com/users/nilearn/repos","events_url":"https://api.github.com/users/nilearn/events{/privacy}","received_events_url":"https://api.github.com/users/nilearn/received_events","type":"Organization","site_admin":false},"repo":{"id":1235740,"name":"nilearn","full_name":"nilearn/nilearn","owner":{"login":"nilearn","id":4646912,"avatar_url":"https://avatars.githubusercontent.com/u/4646912?v=3","gravatar_id":"","url":"https://api.github.com/users/nilearn","html_url":"https://github.com/nilearn","followers_url":"https://api.github.com/users/nilearn/followers","following_url":"https://api.github.com/users/nilearn/following{/other_user}","gists_url":"https://api.github.com/users/nilearn/gists{/gist_id}","starred_url":"https://api.github.com/users/nilearn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nilearn/subscriptions","organizations_url":"https://api.github.com/users/nilearn/orgs","repos_url":"https://api.github.com/users/nilearn/repos","events_url":"https://api.github.com/users/nilearn/events{/privacy}","received_events_url":"https://api.github.com/users/nilearn/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/nilearn/nilearn","description":"Machine learning for NeuroImaging in Python","fork":false,"url":"https://api.github.com/repos/nilearn/nilearn","forks_url":"https://api.github.com/repos/nilearn/nilearn/forks","keys_url":"https://api.github.com/repos/nilearn/nilearn/keys{/key_id}","collaborators_url":"https://api.github.com/repos/nilearn/nilearn/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/nilearn/nilearn/teams","hooks_url":"https://api.github.com/repos/nilearn/nilearn/hooks","issue_events_url":"https://api.github.com/repos/nilearn/nilearn/issues/events{/number}","events_url":"https://api.github.com/repos/nilearn/nilearn/events","assignees_url":"https://api.github.com/repos/nilearn/nilearn/assignees{/user}","branches_url":"https://api.github.com/repos/nilearn/nilearn/branches{/branch}","tags_url":"https://api.github.com/repos/nilearn/nilearn/tags","blobs_url":"https://api.github.com/repos/nilearn/nilearn/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/nilearn/nilearn/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/nilearn/nilearn/git/refs{/sha}","trees_url":"https://api.github.com/repos/nilearn/nilearn/git/trees{/sha}","statuses_url":"https://api.github.com/repos/nilearn/nilearn/statuses/{sha}","languages_url":"https://api.github.com/repos/nilearn/nilearn/languages","stargazers_url":"https://api.github.com/repos/nilearn/nilearn/stargazers","contributors_url":"https://api.github.com/repos/nilearn/nilearn/contributors","subscribers_url":"https://api.github.com/repos/nilearn/nilearn/subscribers","subscription_url":"https://api.github.com/repos/nilearn/nilearn/subscription","commits_url":"https://api.github.com/repos/nilearn/nilearn/commits{/sha}","git_commits_url":"https://api.github.com/repos/nilearn/nilearn/git/commits{/sha}","comments_url":"https://api.github.com/repos/nilearn/nilearn/comments{/number}","issue_comment_url":"https://api.github.com/repos/nilearn/nilearn/issues/comments{/number}","contents_url":"https://api.github.com/repos/nilearn/nilearn/contents/{+path}","compare_url":"https://api.github.com/repos/nilearn/nilearn/compare/{base}...{head}","merges_url":"https://api.github.com/repos/nilearn/nilearn/merges","archive_url":"https://api.github.com/repos/nilearn/nilearn/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/nilearn/nilearn/downloads","issues_url":"https://api.github.com/repos/nilearn/nilearn/issues{/number}","pulls_url":"https://api.github.com/repos/nilearn/nilearn/pulls{/number}","milestones_url":"https://api.github.com/repos/nilearn/nilearn/milestones{/number}","notifications_url":"https://api.github.com/repos/nilearn/nilearn/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/nilearn/nilearn/labels{/name}","releases_url":"https://api.github.com/repos/nilearn/nilearn/releases{/id}","created_at":"2011-01-09T19:02:23Z","updated_at":"2015-12-01T12:51:02Z","pushed_at":"2015-12-03T17:27:12Z","git_url":"git://github.com/nilearn/nilearn.git","ssh_url":"git@github.com:nilearn/nilearn.git","clone_url":"https://github.com/nilearn/nilearn.git","svn_url":"https://github.com/nilearn/nilearn","homepage":"http://nilearn.github.io","size":9883,"stargazers_count":126,"watchers_count":126,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":93,"mirror_url":null,"open_issues_count":106,"forks":93,"open_issues":106,"watchers":126,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/nilearn/nilearn/pulls/651"},"html":{"href":"https://github.com/nilearn/nilearn/pull/651"},"issue":{"href":"https://api.github.com/repos/nilearn/nilearn/issues/651"},"comments":{"href":"https://api.github.com/repos/nilearn/nilearn/issues/651/comments"},"review_comments":{"href":"https://api.github.com/repos/nilearn/nilearn/pulls/651/comments"},"review_comment":{"href":"https://api.github.com/repos/nilearn/nilearn/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/nilearn/nilearn/pulls/651/commits"},"statuses":{"href":"https://api.github.com/repos/nilearn/nilearn/statuses/b851452bfc933f78ee1c4e42188744a8c5f14c52"}}}}