{"action":"created","comment":{"url":"https://api.github.com/repos/nilearn/nilearn/pulls/comments/54333054","id":54333054,"diff_hunk":"@@ -0,0 +1,233 @@\n+\"\"\"\n+============================================================\n+Encoding models for visual stimuli from Miyawaki et al. 2008\n+============================================================\n+\n+This example demonstrates how to build an encoding model for\n+functional MRI data with visual stimuli.\n+\n+Encoding models try to predict neuronal activity using properties of\n+a real world stimulus, like images or sound.\n+\n+In the dataset from :func:`nilearn.datasets.fetch_miyawaki2008`,\n+participants were shown images consisting of 10x10\n+binary (either black or white) pixels and the corresponding  fMRI activity\n+was recorded. We will predict the neuronal activity from the pixel-values\n+of the presented images for each voxel. Then we extract the receptive fields\n+for a set of voxels to see which pixel location a voxel is most sensitive to.\n+\n+See also :doc:`plot_miyawaki_reconstruction` for a decoding\n+approach for the same dataset.\n+\"\"\"\n+\n+##############################################################################\n+# Loading the data\n+# ----------------\n+# First some imports:\n+\n+import numpy as np\n+import pylab as plt\n+from nilearn.plotting import plot_stat_map\n+from nilearn import masking\n+\n+\n+##############################################################################\n+# Now we can load the data set:\n+from nilearn.datasets import fetch_miyawaki2008\n+\n+dataset = fetch_miyawaki2008()\n+\n+##############################################################################\n+# We only use the training data of this study,\n+# where random binary images were shown.\n+\n+X_random_filenames = dataset.func[12:]\n+y_random_filenames = dataset.label[12:]\n+\n+# shape of the binary image in pixels\n+y_shape = (10, 10)\n+\n+\n+##############################################################################\n+# We can use :func:`nilearn.input_data.MultiNiftiMaser` to load the fMRI data,\n+# clean and mask it.\n+\n+from nilearn.input_data import MultiNiftiMasker\n+\n+masker = MultiNiftiMasker(mask_img=dataset.mask, detrend=True,\n+                          standardize=True)\n+masker.fit()\n+X_train = masker.transform(X_random_filenames)\n+\n+# We load the visual stimuli from csv files\n+y_train = []\n+for y in y_random_filenames:\n+    y_train.append(np.reshape(np.loadtxt(y, dtype=np.int, delimiter=','),\n+                              (-1,) + y_shape, order='F'))\n+\n+\n+##############################################################################\n+# We now stack the X and y data and remove an offset in the beginning/end.\n+\n+X_train = np.vstack([x[2:] for x in X_train])\n+y_train = np.vstack([y[:-2] for y in y_train]).astype(float)\n+\n+\n+##############################################################################\n+# X_train is a matrix of *N_samples* x *N_voxels*\n+\n+print(X_train.shape)\n+\n+##############################################################################\n+# We flatten the last two dimensions of y_train\n+# so it is a matrix of *N_samples* x *N_pixels*.\n+\n+# Flatten the stimuli\n+y_train = np.reshape(y_train, (-1, y_shape[0] * y_shape[1]))\n+\n+print(y_train.shape)\n+\n+##############################################################################\n+# Building the encoding models\n+# ----------------------------\n+# We can now proceed to do a simple encoding using Ridge regression.\n+\n+from sklearn.linear_model import Ridge\n+from sklearn.cross_validation import KFold\n+\n+##############################################################################\n+# Using 10-fold cross-validation, we partition the data into 10 'Folds',\n+# we hold out each fold of the data for testing, then fit a ridge regression\n+# to the remaining 9/10 of the data, using y_train as predictors\n+# and X_train as targets, and create predictions for the held-out 10th.\n+\n+estimator = Ridge(alpha=100.)\n+cv = KFold(len(y_train), 10)\n+\n+predictions = [Ridge(alpha=100.).fit(\n+    y_train.reshape(-1, 100)[train], X_train[train]).predict(\n+        y_train.reshape(-1, 100)[test])\n+    for train, test in cv]\n+\n+\n+##############################################################################\n+# To have a measure for the quality of our encoding model, we estimate how\n+# much variance our encoding model explains in each voxel.\n+\n+scores = [1. - (((X_train[test] - pred) ** 2).sum(axis=0) /\n+         ((X_train[test] - X_train[test].mean(axis=0)) ** 2).sum(axis=0))\n+         for pred, (train, test) in zip(predictions, cv)]\n+\n+##############################################################################\n+# Mapping the encoding scores on the brain\n+# ----------------------------------------\n+# To plot the scores onto the brain, we use :func:`nilearn.image.new_img_like`\n+# to create a Nifti1Image containing the scores and then threshold it:\n+\n+from nilearn.image import threshold_img, new_img_like\n+from nilearn.image.resampling import coord_transform\n+\n+cut_score = np.array(scores).mean(axis=0)\n+cut_score[cut_score < 0] = 0\n+\n+# bring the scores into the shape of the background brain\n+score_map = new_img_like(dataset.background,\n+                         masking.unmask(cut_score, dataset.mask).get_data())\n+\n+thresholded_score_map = threshold_img(score_map, threshold = 1e-6)\n+\n+##############################################################################\n+# Plotting the statistical map on a background brain, we mark four voxels\n+# which we will inspect more closely later on.\n+\n+def index_to_xy_coord(x,y,z=10):\n+    '''Transforms data index to coordinates of the background + offset'''\n+    coords = coord_transform(x,y,z,affine=thresholded_score_map.get_affine())\n+    return coords + np.array([0,1,0])","path":"examples/02_decoding/plot_miyawaki_encoding.py","position":146,"original_position":146,"commit_id":"4c9b3c7643f992c188283eac8911324f1394b161","original_commit_id":"4c9b3c7643f992c188283eac8911324f1394b161","user":{"login":"GaelVaroquaux","id":208217,"avatar_url":"https://avatars.githubusercontent.com/u/208217?v=3","gravatar_id":"","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":"PEP8: you need a space after ','","created_at":"2016-02-27T13:00:15Z","updated_at":"2016-02-27T13:00:15Z","html_url":"https://github.com/nilearn/nilearn/pull/1028#discussion_r54333054","pull_request_url":"https://api.github.com/repos/nilearn/nilearn/pulls/1028","_links":{"self":{"href":"https://api.github.com/repos/nilearn/nilearn/pulls/comments/54333054"},"html":{"href":"https://github.com/nilearn/nilearn/pull/1028#discussion_r54333054"},"pull_request":{"href":"https://api.github.com/repos/nilearn/nilearn/pulls/1028"}}},"pull_request":{"url":"https://api.github.com/repos/nilearn/nilearn/pulls/1028","id":60948382,"html_url":"https://github.com/nilearn/nilearn/pull/1028","diff_url":"https://github.com/nilearn/nilearn/pull/1028.diff","patch_url":"https://github.com/nilearn/nilearn/pull/1028.patch","issue_url":"https://api.github.com/repos/nilearn/nilearn/issues/1028","number":1028,"state":"open","locked":false,"title":"Encoding example","user":{"login":"mjboos","id":7125006,"avatar_url":"https://avatars.githubusercontent.com/u/7125006?v=3","gravatar_id":"","url":"https://api.github.com/users/mjboos","html_url":"https://github.com/mjboos","followers_url":"https://api.github.com/users/mjboos/followers","following_url":"https://api.github.com/users/mjboos/following{/other_user}","gists_url":"https://api.github.com/users/mjboos/gists{/gist_id}","starred_url":"https://api.github.com/users/mjboos/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mjboos/subscriptions","organizations_url":"https://api.github.com/users/mjboos/orgs","repos_url":"https://api.github.com/users/mjboos/repos","events_url":"https://api.github.com/users/mjboos/events{/privacy}","received_events_url":"https://api.github.com/users/mjboos/received_events","type":"User","site_admin":false},"body":"This is the encoding example for the Miyawaki et al., 2008 dataset, made at the Brainhack in Paris with @GaelVaroquaux ","created_at":"2016-02-27T12:14:58Z","updated_at":"2016-02-27T13:00:15Z","closed_at":null,"merged_at":null,"merge_commit_sha":"3b1dd7149d9006fdf6c0fd966609d9fa173ad6b1","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/nilearn/nilearn/pulls/1028/commits","review_comments_url":"https://api.github.com/repos/nilearn/nilearn/pulls/1028/comments","review_comment_url":"https://api.github.com/repos/nilearn/nilearn/pulls/comments{/number}","comments_url":"https://api.github.com/repos/nilearn/nilearn/issues/1028/comments","statuses_url":"https://api.github.com/repos/nilearn/nilearn/statuses/4c9b3c7643f992c188283eac8911324f1394b161","head":{"label":"mjboos:encoding_example","ref":"encoding_example","sha":"4c9b3c7643f992c188283eac8911324f1394b161","user":{"login":"mjboos","id":7125006,"avatar_url":"https://avatars.githubusercontent.com/u/7125006?v=3","gravatar_id":"","url":"https://api.github.com/users/mjboos","html_url":"https://github.com/mjboos","followers_url":"https://api.github.com/users/mjboos/followers","following_url":"https://api.github.com/users/mjboos/following{/other_user}","gists_url":"https://api.github.com/users/mjboos/gists{/gist_id}","starred_url":"https://api.github.com/users/mjboos/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mjboos/subscriptions","organizations_url":"https://api.github.com/users/mjboos/orgs","repos_url":"https://api.github.com/users/mjboos/repos","events_url":"https://api.github.com/users/mjboos/events{/privacy}","received_events_url":"https://api.github.com/users/mjboos/received_events","type":"User","site_admin":false},"repo":{"id":52443979,"name":"nilearn","full_name":"mjboos/nilearn","owner":{"login":"mjboos","id":7125006,"avatar_url":"https://avatars.githubusercontent.com/u/7125006?v=3","gravatar_id":"","url":"https://api.github.com/users/mjboos","html_url":"https://github.com/mjboos","followers_url":"https://api.github.com/users/mjboos/followers","following_url":"https://api.github.com/users/mjboos/following{/other_user}","gists_url":"https://api.github.com/users/mjboos/gists{/gist_id}","starred_url":"https://api.github.com/users/mjboos/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mjboos/subscriptions","organizations_url":"https://api.github.com/users/mjboos/orgs","repos_url":"https://api.github.com/users/mjboos/repos","events_url":"https://api.github.com/users/mjboos/events{/privacy}","received_events_url":"https://api.github.com/users/mjboos/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/mjboos/nilearn","description":"Machine learning for NeuroImaging in Python","fork":true,"url":"https://api.github.com/repos/mjboos/nilearn","forks_url":"https://api.github.com/repos/mjboos/nilearn/forks","keys_url":"https://api.github.com/repos/mjboos/nilearn/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mjboos/nilearn/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mjboos/nilearn/teams","hooks_url":"https://api.github.com/repos/mjboos/nilearn/hooks","issue_events_url":"https://api.github.com/repos/mjboos/nilearn/issues/events{/number}","events_url":"https://api.github.com/repos/mjboos/nilearn/events","assignees_url":"https://api.github.com/repos/mjboos/nilearn/assignees{/user}","branches_url":"https://api.github.com/repos/mjboos/nilearn/branches{/branch}","tags_url":"https://api.github.com/repos/mjboos/nilearn/tags","blobs_url":"https://api.github.com/repos/mjboos/nilearn/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mjboos/nilearn/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mjboos/nilearn/git/refs{/sha}","trees_url":"https://api.github.com/repos/mjboos/nilearn/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mjboos/nilearn/statuses/{sha}","languages_url":"https://api.github.com/repos/mjboos/nilearn/languages","stargazers_url":"https://api.github.com/repos/mjboos/nilearn/stargazers","contributors_url":"https://api.github.com/repos/mjboos/nilearn/contributors","subscribers_url":"https://api.github.com/repos/mjboos/nilearn/subscribers","subscription_url":"https://api.github.com/repos/mjboos/nilearn/subscription","commits_url":"https://api.github.com/repos/mjboos/nilearn/commits{/sha}","git_commits_url":"https://api.github.com/repos/mjboos/nilearn/git/commits{/sha}","comments_url":"https://api.github.com/repos/mjboos/nilearn/comments{/number}","issue_comment_url":"https://api.github.com/repos/mjboos/nilearn/issues/comments{/number}","contents_url":"https://api.github.com/repos/mjboos/nilearn/contents/{+path}","compare_url":"https://api.github.com/repos/mjboos/nilearn/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mjboos/nilearn/merges","archive_url":"https://api.github.com/repos/mjboos/nilearn/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mjboos/nilearn/downloads","issues_url":"https://api.github.com/repos/mjboos/nilearn/issues{/number}","pulls_url":"https://api.github.com/repos/mjboos/nilearn/pulls{/number}","milestones_url":"https://api.github.com/repos/mjboos/nilearn/milestones{/number}","notifications_url":"https://api.github.com/repos/mjboos/nilearn/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mjboos/nilearn/labels{/name}","releases_url":"https://api.github.com/repos/mjboos/nilearn/releases{/id}","deployments_url":"https://api.github.com/repos/mjboos/nilearn/deployments","created_at":"2016-02-24T13:24:16Z","updated_at":"2016-02-24T13:24:17Z","pushed_at":"2016-02-27T12:13:06Z","git_url":"git://github.com/mjboos/nilearn.git","ssh_url":"git@github.com:mjboos/nilearn.git","clone_url":"https://github.com/mjboos/nilearn.git","svn_url":"https://github.com/mjboos/nilearn","homepage":"http://nilearn.github.io","size":8974,"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":"815900bc32153fe833858158c0414f6ce834c1e1","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}","deployments_url":"https://api.github.com/repos/nilearn/nilearn/deployments","created_at":"2011-01-09T19:02:23Z","updated_at":"2016-02-26T07:22:46Z","pushed_at":"2016-02-27T12:17:17Z","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":12112,"stargazers_count":154,"watchers_count":154,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":114,"mirror_url":null,"open_issues_count":126,"forks":114,"open_issues":126,"watchers":154,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/nilearn/nilearn/pulls/1028"},"html":{"href":"https://github.com/nilearn/nilearn/pull/1028"},"issue":{"href":"https://api.github.com/repos/nilearn/nilearn/issues/1028"},"comments":{"href":"https://api.github.com/repos/nilearn/nilearn/issues/1028/comments"},"review_comments":{"href":"https://api.github.com/repos/nilearn/nilearn/pulls/1028/comments"},"review_comment":{"href":"https://api.github.com/repos/nilearn/nilearn/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/nilearn/nilearn/pulls/1028/commits"},"statuses":{"href":"https://api.github.com/repos/nilearn/nilearn/statuses/4c9b3c7643f992c188283eac8911324f1394b161"}}}}