Skip to content

Commit a9ceac6

Browse files
committed
MNT update notebooks
1 parent f76e166 commit a9ceac6

3 files changed

Lines changed: 105 additions & 44 deletions

File tree

tutorials/notebooks/movies_3T/00_load_colab.ipynb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@
3333
"cell_type": "markdown",
3434
"metadata": {},
3535
"source": [
36-
"## Change runtime to use a GPU\n\nThis tutorial is much faster when a GPU is available to run the computations.\nIn Google Colab you can request access to a GPU by changing the runtime type. \nTo do so, click the following menu options in Google Colab: \n\n(Menu) \"Runtime\" -> \"Change runtime type\" -> \"Hardware accelerator\" -> \"GPU\".\n\n"
36+
"## Change runtime to use a GPU\n\nThis tutorial is much faster when a GPU is available to run the computations.\nIn Google Colab you can request access to a GPU by changing the runtime type.\nTo do so, click the following menu options in Google Colab:\n\n(Menu) \"Runtime\" -> \"Change runtime type\" -> \"Hardware accelerator\" -> \"GPU\".\n\n"
3737
]
3838
},
3939
{
4040
"cell_type": "markdown",
4141
"metadata": {},
4242
"source": [
43-
"## Download the data and install all required dependencies\n\nUncomment and run the following cell to download the tutorial data and\ninstall the required dependencies\n\n"
43+
"## Download the data and install all required dependencies\n\nUncomment and run the following cell to download the tutorial data and\ninstall the required dependencies.\n\n"
4444
]
4545
},
4646
{
@@ -51,14 +51,14 @@
5151
},
5252
"outputs": [],
5353
"source": [
54-
"# !gdown --id 1b0I0Ytj06m6GCmfxfNrZuyF97fDo3NZb && \\\n# tar xzf vim-5-for-ccn.tar.gz && \\\n# pip install -q voxelwise_tutorials && \\\n# git clone https://github.com/gallantlab/pycortex"
54+
"# ![ -f \"vim-5-for-ccn.tar.gz\" ] || gdown --id 1b0I0Ytj06m6GCmfxfNrZuyF97fDo3NZb\n# ![ -d \"vim-5\" ] || tar xzf vim-5-for-ccn.tar.gz\n# ![ -d \"pycortex\" ] || git clone https://github.com/gallantlab/pycortex\n# !pip install -q voxelwise_tutorials"
5555
]
5656
},
5757
{
5858
"cell_type": "markdown",
5959
"metadata": {},
6060
"source": [
61-
"Now run the following cell to set up the environment variables for the tutorials\nand pycortex.\n\n"
61+
"Now run the following cell to set up the environment variables for the\ntutorials and pycortex.\n\n"
6262
]
6363
},
6464
{

tutorials/notebooks/movies_3T/05_plot_banded_ridge_model.ipynb

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,24 @@
7676
"import numpy as np\n\nfrom voxelwise_tutorials.io import load_hdf5_array\n\nfile_name = os.path.join(directory, \"responses\", f\"{subject}_responses.hdf\")\nY_train = load_hdf5_array(file_name, key=\"Y_train\")\nY_test = load_hdf5_array(file_name, key=\"Y_test\")\n\nprint(\"(n_samples_train, n_voxels) =\", Y_train.shape)\nprint(\"(n_repeats, n_samples_test, n_voxels) =\", Y_test.shape)"
7777
]
7878
},
79+
{
80+
"cell_type": "markdown",
81+
"metadata": {},
82+
"source": [
83+
"We also compute the explainable variance, to exclude voxels with low\nexplainable variance from the fit, and speed up the model fitting.\n\n"
84+
]
85+
},
86+
{
87+
"cell_type": "code",
88+
"execution_count": null,
89+
"metadata": {
90+
"collapsed": false
91+
},
92+
"outputs": [],
93+
"source": [
94+
"from voxelwise_tutorials.utils import explainable_variance\nev = explainable_variance(Y_test)\nprint(\"(n_voxels,) =\", ev.shape)\n\nmask = ev > 0.1\nprint(\"(n_voxels_mask,) =\", ev[mask].shape)"
95+
]
96+
},
7997
{
8098
"cell_type": "markdown",
8199
"metadata": {},
@@ -357,7 +375,7 @@
357375
"cell_type": "markdown",
358376
"metadata": {},
359377
"source": [
360-
"## Fit the model\n\nWe fit on the train set, and score on the test set.\n\nWith a GPU backend, the fitting of this model takes around 6 minutes. With a\nCPU backend, it can last 10 times more.\n\n"
378+
"## Fit the model\n\nWe fit on the train set, and score on the test set.\n\nTo speed up the fit and to limit the memory peak in Colab, we only fit on\nvoxels with explainable variance above 0.1.\n\nWith a GPU backend, the fitting of this model takes around 6 minutes. With a\nCPU backend, it can last 10 times more.\n\n"
361379
]
362380
},
363381
{
@@ -368,7 +386,7 @@
368386
},
369387
"outputs": [],
370388
"source": [
371-
"pipeline.fit(X_train, Y_train)\n\nscores = pipeline.score(X_test, Y_test)\nscores = backend.to_numpy(scores)\n\nprint(\"(n_voxels,) =\", scores.shape)"
389+
"pipeline.fit(X_train, Y_train[:, mask])\n\nscores_mask = pipeline.score(X_test, Y_test[:, mask])\nscores_mask = backend.to_numpy(scores_mask)\nprint(\"(n_voxels_mask,) =\", scores_mask.shape)\n\n# Then we extend the scores to all voxels, giving a score of zero to unfitted\n# voxels.\nn_voxels = Y_train.shape[1]\nscores = np.zeros(n_voxels)\nscores[mask] = scores_mask"
372390
]
373391
},
374392
{
@@ -397,14 +415,14 @@
397415
},
398416
"outputs": [],
399417
"source": [
400-
"pipeline_baseline.fit(X_train, Y_train)\nscores_baseline = pipeline_baseline.score(X_test, Y_test)\nscores_baseline = backend.to_numpy(scores_baseline)"
418+
"pipeline_baseline.fit(X_train, Y_train[:, mask])\nscores_baseline_mask = pipeline_baseline.score(X_test, Y_test[:, mask])\nscores_baseline_mask = backend.to_numpy(scores_baseline_mask)\n\n# extend to unfitted voxels\nn_voxels = Y_train.shape[1]\nscores_baseline = np.zeros(n_voxels)\nscores_baseline[mask] = scores_baseline_mask"
401419
]
402420
},
403421
{
404422
"cell_type": "markdown",
405423
"metadata": {},
406424
"source": [
407-
"Here we plot the comparison of model prediction accuracies with a 2D histogram.\nAll 70k voxels are represented in this histogram, where the diagonal\ncorresponds to identical model prediction accuracy for both models. A distibution deviating\nfrom the diagonal means that one model has better predictive performance\nthan the other.\n\n"
425+
"Here we plot the comparison of model prediction accuracies with a 2D\nhistogram. All 70k voxels are represented in this histogram, where the\ndiagonal corresponds to identical model prediction accuracy for both models.\nA distibution deviating from the diagonal means that one model has better\npredictive performance than the other.\n\n"
408426
]
409427
},
410428
{
@@ -440,7 +458,7 @@
440458
},
441459
"outputs": [],
442460
"source": [
443-
"from himalaya.scoring import r2_score_split\n\nY_test_pred_split = pipeline.predict(X_test, split=True)\nsplit_scores = r2_score_split(Y_test, Y_test_pred_split)\nsplit_scores.shape\n\nprint(\"(n_kernels, n_samples_test, n_voxels) =\", Y_test_pred_split.shape)\nprint(\"(n_kernels, n_voxels) =\", split_scores.shape)"
461+
"from himalaya.scoring import r2_score_split\n\nY_test_pred_split = pipeline.predict(X_test, split=True)\nsplit_scores_mask = r2_score_split(Y_test[:, mask], Y_test_pred_split)\n\nprint(\"(n_kernels, n_samples_test, n_voxels_mask) =\", Y_test_pred_split.shape)\nprint(\"(n_kernels, n_voxels_mask) =\", split_scores_mask.shape)\n\n# extend to unfitted voxels\nn_kernels = split_scores_mask.shape[0]\nn_voxels = Y_train.shape[1]\nsplit_scores = np.zeros((n_kernels, n_voxels))\nsplit_scores[:, mask] = split_scores_mask\nprint(\"(n_kernels, n_voxels) =\", split_scores.shape)"
444462
]
445463
},
446464
{
@@ -465,7 +483,7 @@
465483
"cell_type": "markdown",
466484
"metadata": {},
467485
"source": [
468-
"The blue regions are better predicted by the motion-energy features, the orange\nregions are better predicted by the wordnet features, and the white regions are\nwell predicted by both feature spaces.\n\nCompared to the last figure of the previous example, we see that most white\nregions have been replaced by either blue or orange regions. The\nbanded ridge regression disentangled the two feature spaces in voxels where\nboth feature spaces had good prediction accuracy (see previous example). For\nexample, motion-energy features predict brain activity in early visual\ncortex, while wordnet features predict in semantic visual areas. For more\ndiscussions about these results, we refer the reader to the original\npublication [1]_.\n\n"
486+
"The blue regions are better predicted by the motion-energy features, the\norange regions are better predicted by the wordnet features, and the white\nregions are well predicted by both feature spaces.\n\nCompared to the last figure of the previous example, we see that most white\nregions have been replaced by either blue or orange regions. The banded ridge\nregression disentangled the two feature spaces in voxels where both feature\nspaces had good prediction accuracy (see previous example). For example,\nmotion-energy features predict brain activity in early visual cortex, while\nwordnet features predict in semantic visual areas. For more discussions about\nthese results, we refer the reader to the original publication [1]_.\n\n"
469487
]
470488
},
471489
{

tutorials/notebooks/movies_3T/merged_for_colab.ipynb

Lines changed: 77 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949
"## Change runtime to use a GPU\n",
5050
"\n",
5151
"This tutorial is much faster when a GPU is available to run the computations.\n",
52-
"In Google Colab you can request access to a GPU by changing the runtime type. \n",
53-
"To do so, click the following menu options in Google Colab: \n",
52+
"In Google Colab you can request access to a GPU by changing the runtime type.\n",
53+
"To do so, click the following menu options in Google Colab:\n",
5454
"\n",
5555
"(Menu) \"Runtime\" -> \"Change runtime type\" -> \"Hardware accelerator\" -> \"GPU\".\n",
5656
"\n"
@@ -63,7 +63,7 @@
6363
"## Download the data and install all required dependencies\n",
6464
"\n",
6565
"Uncomment and run the following cell to download the tutorial data and\n",
66-
"install the required dependencies\n",
66+
"install the required dependencies.\n",
6767
"\n"
6868
]
6969
},
@@ -75,18 +75,18 @@
7575
},
7676
"outputs": [],
7777
"source": [
78-
"# !gdown --id 1b0I0Ytj06m6GCmfxfNrZuyF97fDo3NZb && \\\n",
79-
"# tar xzf vim-5-for-ccn.tar.gz && \\\n",
80-
"# pip install -q voxelwise_tutorials && \\\n",
81-
"# git clone https://github.com/gallantlab/pycortex"
78+
"# ![ -f \"vim-5-for-ccn.tar.gz\" ] || gdown --id 1b0I0Ytj06m6GCmfxfNrZuyF97fDo3NZb\n",
79+
"# ![ -d \"vim-5\" ] || tar xzf vim-5-for-ccn.tar.gz\n",
80+
"# ![ -d \"pycortex\" ] || git clone https://github.com/gallantlab/pycortex\n",
81+
"# !pip install -q voxelwise_tutorials"
8282
]
8383
},
8484
{
8585
"cell_type": "markdown",
8686
"metadata": {},
8787
"source": [
88-
"Now run the following cell to set up the environment variables for the tutorials\n",
89-
"and pycortex.\n",
88+
"Now run the following cell to set up the environment variables for the\n",
89+
"tutorials and pycortex.\n",
9090
"\n"
9191
]
9292
},
@@ -2781,6 +2781,31 @@
27812781
"print(\"(n_repeats, n_samples_test, n_voxels) =\", Y_test.shape)"
27822782
]
27832783
},
2784+
{
2785+
"cell_type": "markdown",
2786+
"metadata": {},
2787+
"source": [
2788+
"We also compute the explainable variance, to exclude voxels with low\n",
2789+
"explainable variance from the fit, and speed up the model fitting.\n",
2790+
"\n"
2791+
]
2792+
},
2793+
{
2794+
"cell_type": "code",
2795+
"execution_count": null,
2796+
"metadata": {
2797+
"collapsed": false
2798+
},
2799+
"outputs": [],
2800+
"source": [
2801+
"from voxelwise_tutorials.utils import explainable_variance\n",
2802+
"ev = explainable_variance(Y_test)\n",
2803+
"print(\"(n_voxels,) =\", ev.shape)\n",
2804+
"\n",
2805+
"mask = ev > 0.1\n",
2806+
"print(\"(n_voxels_mask,) =\", ev[mask].shape)"
2807+
]
2808+
},
27842809
{
27852810
"cell_type": "markdown",
27862811
"metadata": {},
@@ -3203,6 +3228,9 @@
32033228
"\n",
32043229
"We fit on the train set, and score on the test set.\n",
32053230
"\n",
3231+
"To speed up the fit and to limit the memory peak in Colab, we only fit on\n",
3232+
"voxels with explainable variance above 0.1.\n",
3233+
"\n",
32063234
"With a GPU backend, the fitting of this model takes around 6 minutes. With a\n",
32073235
"CPU backend, it can last 10 times more.\n",
32083236
"\n"
@@ -3216,12 +3244,17 @@
32163244
},
32173245
"outputs": [],
32183246
"source": [
3219-
"pipeline.fit(X_train, Y_train)\n",
3247+
"pipeline.fit(X_train, Y_train[:, mask])\n",
32203248
"\n",
3221-
"scores = pipeline.score(X_test, Y_test)\n",
3222-
"scores = backend.to_numpy(scores)\n",
3249+
"scores_mask = pipeline.score(X_test, Y_test[:, mask])\n",
3250+
"scores_mask = backend.to_numpy(scores_mask)\n",
3251+
"print(\"(n_voxels_mask,) =\", scores_mask.shape)\n",
32233252
"\n",
3224-
"print(\"(n_voxels,) =\", scores.shape)"
3253+
"# Then we extend the scores to all voxels, giving a score of zero to unfitted\n",
3254+
"# voxels.\n",
3255+
"n_voxels = Y_train.shape[1]\n",
3256+
"scores = np.zeros(n_voxels)\n",
3257+
"scores[mask] = scores_mask"
32253258
]
32263259
},
32273260
{
@@ -3266,20 +3299,25 @@
32663299
},
32673300
"outputs": [],
32683301
"source": [
3269-
"pipeline_baseline.fit(X_train, Y_train)\n",
3270-
"scores_baseline = pipeline_baseline.score(X_test, Y_test)\n",
3271-
"scores_baseline = backend.to_numpy(scores_baseline)"
3302+
"pipeline_baseline.fit(X_train, Y_train[:, mask])\n",
3303+
"scores_baseline_mask = pipeline_baseline.score(X_test, Y_test[:, mask])\n",
3304+
"scores_baseline_mask = backend.to_numpy(scores_baseline_mask)\n",
3305+
"\n",
3306+
"# extend to unfitted voxels\n",
3307+
"n_voxels = Y_train.shape[1]\n",
3308+
"scores_baseline = np.zeros(n_voxels)\n",
3309+
"scores_baseline[mask] = scores_baseline_mask"
32723310
]
32733311
},
32743312
{
32753313
"cell_type": "markdown",
32763314
"metadata": {},
32773315
"source": [
3278-
"Here we plot the comparison of model prediction accuracies with a 2D histogram.\n",
3279-
"All 70k voxels are represented in this histogram, where the diagonal\n",
3280-
"corresponds to identical model prediction accuracy for both models. A distibution deviating\n",
3281-
"from the diagonal means that one model has better predictive performance\n",
3282-
"than the other.\n",
3316+
"Here we plot the comparison of model prediction accuracies with a 2D\n",
3317+
"histogram. All 70k voxels are represented in this histogram, where the\n",
3318+
"diagonal corresponds to identical model prediction accuracy for both models.\n",
3319+
"A distibution deviating from the diagonal means that one model has better\n",
3320+
"predictive performance than the other.\n",
32833321
"\n"
32843322
]
32853323
},
@@ -3344,10 +3382,16 @@
33443382
"from himalaya.scoring import r2_score_split\n",
33453383
"\n",
33463384
"Y_test_pred_split = pipeline.predict(X_test, split=True)\n",
3347-
"split_scores = r2_score_split(Y_test, Y_test_pred_split)\n",
3348-
"split_scores.shape\n",
3385+
"split_scores_mask = r2_score_split(Y_test[:, mask], Y_test_pred_split)\n",
3386+
"\n",
3387+
"print(\"(n_kernels, n_samples_test, n_voxels_mask) =\", Y_test_pred_split.shape)\n",
3388+
"print(\"(n_kernels, n_voxels_mask) =\", split_scores_mask.shape)\n",
33493389
"\n",
3350-
"print(\"(n_kernels, n_samples_test, n_voxels) =\", Y_test_pred_split.shape)\n",
3390+
"# extend to unfitted voxels\n",
3391+
"n_kernels = split_scores_mask.shape[0]\n",
3392+
"n_voxels = Y_train.shape[1]\n",
3393+
"split_scores = np.zeros((n_kernels, n_voxels))\n",
3394+
"split_scores[:, mask] = split_scores_mask\n",
33513395
"print(\"(n_kernels, n_voxels) =\", split_scores.shape)"
33523396
]
33533397
},
@@ -3381,18 +3425,17 @@
33813425
"cell_type": "markdown",
33823426
"metadata": {},
33833427
"source": [
3384-
"The blue regions are better predicted by the motion-energy features, the orange\n",
3385-
"regions are better predicted by the wordnet features, and the white regions are\n",
3386-
"well predicted by both feature spaces.\n",
3428+
"The blue regions are better predicted by the motion-energy features, the\n",
3429+
"orange regions are better predicted by the wordnet features, and the white\n",
3430+
"regions are well predicted by both feature spaces.\n",
33873431
"\n",
33883432
"Compared to the last figure of the previous example, we see that most white\n",
3389-
"regions have been replaced by either blue or orange regions. The\n",
3390-
"banded ridge regression disentangled the two feature spaces in voxels where\n",
3391-
"both feature spaces had good prediction accuracy (see previous example). For\n",
3392-
"example, motion-energy features predict brain activity in early visual\n",
3393-
"cortex, while wordnet features predict in semantic visual areas. For more\n",
3394-
"discussions about these results, we refer the reader to the original\n",
3395-
"publication [1]_.\n",
3433+
"regions have been replaced by either blue or orange regions. The banded ridge\n",
3434+
"regression disentangled the two feature spaces in voxels where both feature\n",
3435+
"spaces had good prediction accuracy (see previous example). For example,\n",
3436+
"motion-energy features predict brain activity in early visual cortex, while\n",
3437+
"wordnet features predict in semantic visual areas. For more discussions about\n",
3438+
"these results, we refer the reader to the original publication [1]_.\n",
33963439
"\n"
33973440
]
33983441
},

0 commit comments

Comments
 (0)