GitHub PullRequestReviewCommentEvent at 2014-02-04 08:05:56
Repository: nilearn
Actor: GaelVaroquaux
Payload: {"comment":{"url":"https://api.github.com/repos/nilearn/nilearn/pulls/comments/9413176","id":9413176,"diff_hunk":"@@ -0,0 +1,117 @@\n+\"\"\"\n+Understanding NiftiMasker and mask computation\n+==================================================\n+\n+In this example, the Nifti masker is used to automatically compute a mask.\n+\n+For data that has already been masked, the default strategy works out of\n+the box. However, for raw EPI, as in resting-state time series, we need\n+to use the 'epi' strategy of the NiftiMasker. It may be useful to tweak\n+the different parameters of the underlying mask extraction routine\n+:func:`nilearn.masking.compute_epi_mask`.\n+\n+\"\"\"\n+\n+import matplotlib.pyplot as plt\n+import numpy as np\n+\n+import nibabel\n+from nilearn import datasets\n+\n+\n+###############################################################################\n+# Simple visualization helper\n+\n+def display_mask(background, mask, title):\n+    plt.axis('off')\n+    plt.imshow(np.rot90(background), interpolation='nearest', cmap=plt.cm.gray)\n+    ma = np.ma.masked_equal(mask, False)\n+    plt.imshow(np.rot90(ma), interpolation='nearest',\n+              cmap=plt.cm.autumn, alpha=0.5)\n+    plt.title(title)\n+\n+\n+###############################################################################\n+# From already masked data\n+from nilearn.input_data import NiftiMasker\n+\n+# Load Miyawaki dataset\n+miyawaki = datasets.fetch_miyawaki2008()\n+miyawaki_img = nibabel.load(miyawaki.func[0])\n+miyawaki_func = miyawaki_img.get_data()\n+\n+background = np.mean(miyawaki_func, axis=-1)[..., 14]\n+\n+# This time, we can use the NiftiMasker without changing the default mask\n+# strategy, as the data has already been masked, and thus lies on a\n+# homogeneous background\n+\n+masker = NiftiMasker()\n+masker.fit(miyawaki_img)\n+default_mask = masker.mask_img_.get_data().astype(np.bool)\n+plt.figure(figsize=(4, 4.5))\n+display_mask(background, default_mask[..., 14], 'Default background mask')\n+plt.tight_layout()\n+\n+\n+###############################################################################\n+# From raw EPI data\n+\n+# Load NYU resting-state dataset\n+nyu = datasets.fetch_nyu_rest(n_subjects=1)\n+nyu_img = nibabel.load(nyu.func[0])\n+# Restrict nyu to 100 frames to speed up computation\n+nyu_func = nyu_img.get_data()[..., :100]\n+\n+# nyu_func is a 4D-array, we want to make a Niimg out of it:\n+nyu_img = nibabel.Nifti1Image(nyu_func, nyu_img.get_affine())\n+\n+# To display the background\n+background = np.mean(nyu_func, axis=-1)[..., 21]\n+\n+\n+# Simple mask extraction from EPI images\n+from nilearn.input_data import NiftiMasker\n+# We need to specify an 'epi' mask_strategy, as this is raw EPI data\n+masker = NiftiMasker(mask_strategy='epi')\n+masker.fit(nyu_img)\n+default_mask = masker.mask_img_.get_data().astype(np.bool)\n+plt.figure(figsize=(4, 4.5))\n+display_mask(background, default_mask[..., 21], 'EPI automatic mask')\n+plt.tight_layout()\n+\n+# Generate mask with strong opening\n+masker = NiftiMasker(mask_strategy='epi', mask_args=dict(opening=10))\n+masker.fit(nyu_img)\n+opening_mask = masker.mask_img_.get_data().astype(np.bool)\n+plt.figure(figsize=(4, 4.5))\n+display_mask(background, opening_mask[..., 21], 'EPI Mask with strong opening')\n+plt.tight_layout()\n+\n+# Generate mask with a high lower cutoff\n+masker = NiftiMasker(mask_strategy='epi',\n+                     mask_args=dict(upper_cutoff=.9, lower_cutoff=.8,\n+                                    opening=False))\n+masker.fit(nyu_img)\n+cutoff_mask = masker.mask_img_.get_data().astype(np.bool)\n+\n+plt.figure(figsize=(4, 4.5))\n+display_mask(background, cutoff_mask[..., 21], 'EPI Mask: high lower_cutoff')\n+plt.tight_layout()\n+\n+################################################################################\n+# Extract time series\n+\n+# trended vs detrended\n+trended = NiftiMasker(mask_strategy='epi')\n+detrended = NiftiMasker(mask_strategy='epi', detrend=True)\n+trended_data = trended.fit_transform(nyu_img)\n+detrended_data = detrended.fit_transform(nyu_img)\n+\n+print \"Trended: mean %.2f, std %.2f\" % \\\n+    (np.mean(trended_data), np.std(trended_data))\n+print \"Detrended: mean %.2f, std %.2f\" % \\\n+    (np.mean(detrended_data), np.std(detrended_data))\n+\n+\n+plt.show()","path":"plot_mask_computation.py","position":117,"original_position":117,"commit_id":"55642298b28c0f5420ecb503be13a1c91698589e","original_commit_id":"55642298b28c0f5420ecb503be13a1c91698589e","user":{"login":"GaelVaroquaux","id":208217,"avatar_url":"https://gravatar.com/avatar/5c9fb379c4e97b58960d74dcbfc5dee5?d=https%3A%2F%2Fidenticons.github.com%2F0f015294465b35bf1108f310bb85ef0c.png&r=x","gravatar_id":"5c9fb379c4e97b58960d74dcbfc5dee5","url":"https://api.github.com/users/GaelVaroquaux","html_url":"https://github.com/GaelVaroquaux","followers_url":"https://api.github.com/users/GaelVaroquaux/followers","following_url":"https://api.github.com/users/GaelVaroquaux/following{/other_user}","gists_url":"https://api.github.com/users/GaelVaroquaux/gists{/gist_id}","starred_url":"https://api.github.com/users/GaelVaroquaux/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/GaelVaroquaux/subscriptions","organizations_url":"https://api.github.com/users/GaelVaroquaux/orgs","repos_url":"https://api.github.com/users/GaelVaroquaux/repos","events_url":"https://api.github.com/users/GaelVaroquaux/events{/privacy}","received_events_url":"https://api.github.com/users/GaelVaroquaux/received_events","type":"User","site_admin":false},"body":"> but the 4 figures are a bit messy. I'd rather have subplots.\n\nIt's too be able to use them separately in the documentation. When\nrunning interactively, I agree that it is not great. But for the generate\nwebpage it is better.","created_at":"2014-02-04T08:05:56Z","updated_at":"2014-02-04T08:05:56Z","html_url":"https://github.com/nilearn/nilearn/pull/161#discussion_r9413176","pull_request_url":"https://api.github.com/repos/nilearn/nilearn/pulls/161","_links":{"self":{"href":"https://api.github.com/repos/nilearn/nilearn/pulls/comments/9413176"},"html":{"href":"https://github.com/nilearn/nilearn/pull/161#discussion_r9413176"},"pull_request":{"href":"https://api.github.com/repos/nilearn/nilearn/pulls/161"}}}}