Skip to content

Commit 32cb234

Browse files
committed
add GLM analysis section
1 parent 1f06fe2 commit 32cb234

1 file changed

Lines changed: 65 additions & 16 deletions

File tree

8-mri/fmri_glm.ipynb

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
},
1313
{
1414
"cell_type": "code",
15-
"execution_count": 19,
15+
"execution_count": 1,
1616
"id": "11ebf871",
1717
"metadata": {},
1818
"outputs": [],
@@ -51,7 +51,7 @@
5151
},
5252
{
5353
"cell_type": "code",
54-
"execution_count": 3,
54+
"execution_count": 2,
5555
"id": "1179daa0",
5656
"metadata": {},
5757
"outputs": [
@@ -91,7 +91,7 @@
9191
},
9292
{
9393
"cell_type": "code",
94-
"execution_count": 4,
94+
"execution_count": 3,
9595
"id": "6546e085",
9696
"metadata": {},
9797
"outputs": [],
@@ -120,7 +120,7 @@
120120
},
121121
{
122122
"cell_type": "code",
123-
"execution_count": 5,
123+
"execution_count": 4,
124124
"id": "777d7579",
125125
"metadata": {},
126126
"outputs": [
@@ -156,7 +156,7 @@
156156
},
157157
{
158158
"cell_type": "code",
159-
"execution_count": 6,
159+
"execution_count": 5,
160160
"id": "340d3f6f",
161161
"metadata": {},
162162
"outputs": [
@@ -229,7 +229,7 @@
229229
},
230230
{
231231
"cell_type": "code",
232-
"execution_count": 7,
232+
"execution_count": 6,
233233
"id": "066d0cc0",
234234
"metadata": {},
235235
"outputs": [
@@ -272,7 +272,7 @@
272272
},
273273
{
274274
"cell_type": "code",
275-
"execution_count": 8,
275+
"execution_count": 7,
276276
"id": "032c5de0",
277277
"metadata": {},
278278
"outputs": [
@@ -308,7 +308,7 @@
308308
},
309309
{
310310
"cell_type": "code",
311-
"execution_count": 9,
311+
"execution_count": 8,
312312
"id": "59f716e8",
313313
"metadata": {},
314314
"outputs": [
@@ -350,7 +350,7 @@
350350
},
351351
{
352352
"cell_type": "code",
353-
"execution_count": 10,
353+
"execution_count": 9,
354354
"id": "20df407e",
355355
"metadata": {},
356356
"outputs": [
@@ -403,7 +403,7 @@
403403
},
404404
{
405405
"cell_type": "code",
406-
"execution_count": 11,
406+
"execution_count": 10,
407407
"id": "995e8fa4",
408408
"metadata": {},
409409
"outputs": [
@@ -458,7 +458,7 @@
458458
},
459459
{
460460
"cell_type": "code",
461-
"execution_count": 12,
461+
"execution_count": 11,
462462
"id": "e106e87b",
463463
"metadata": {},
464464
"outputs": [
@@ -513,7 +513,7 @@
513513
},
514514
{
515515
"cell_type": "code",
516-
"execution_count": 13,
516+
"execution_count": 12,
517517
"id": "aa7b9dc1",
518518
"metadata": {},
519519
"outputs": [
@@ -620,7 +620,7 @@
620620
},
621621
{
622622
"cell_type": "code",
623-
"execution_count": 20,
623+
"execution_count": 13,
624624
"id": "2630ea0c",
625625
"metadata": {},
626626
"outputs": [
@@ -737,7 +737,7 @@
737737
},
738738
{
739739
"cell_type": "code",
740-
"execution_count": 15,
740+
"execution_count": 14,
741741
"id": "7bbe06c1",
742742
"metadata": {},
743743
"outputs": [
@@ -793,7 +793,7 @@
793793
},
794794
{
795795
"cell_type": "code",
796-
"execution_count": 16,
796+
"execution_count": 15,
797797
"id": "cec2a080",
798798
"metadata": {},
799799
"outputs": [
@@ -839,7 +839,7 @@
839839
},
840840
{
841841
"cell_type": "code",
842-
"execution_count": null,
842+
"execution_count": 16,
843843
"id": "9aaf4a75",
844844
"metadata": {},
845845
"outputs": [
@@ -859,6 +859,55 @@
859859
"plot_design_matrix(design_matrix) \n",
860860
"plt.show()"
861861
]
862+
},
863+
{
864+
"cell_type": "markdown",
865+
"id": "49719602",
866+
"metadata": {},
867+
"source": [
868+
"## Run the GLM Analysis"
869+
]
870+
},
871+
{
872+
"cell_type": "code",
873+
"execution_count": 19,
874+
"id": "8f03a7c0",
875+
"metadata": {},
876+
"outputs": [
877+
{
878+
"name": "stderr",
879+
"output_type": "stream",
880+
"text": [
881+
"/var/folders/nq/chbqhzjx5hb6hfjx708rh2_40000gn/T/ipykernel_15985/2329769883.py:4: UserWarning: If design matrices are supplied, [t_r] will be ignored.\n",
882+
" first_level_model = first_level_model.fit(bold_img, design_matrices=design_matrix)\n"
883+
]
884+
}
885+
],
886+
"source": [
887+
"# run the GLM analysis\n",
888+
"from nilearn.glm.first_level import FirstLevelModel\n",
889+
"first_level_model = FirstLevelModel(t_r=tr_sec, hrf_model='glover', high_pass=0.01)\n",
890+
"first_level_model = first_level_model.fit(bold_img, design_matrices=design_matrix)"
891+
]
892+
},
893+
{
894+
"cell_type": "code",
895+
"execution_count": null,
896+
"id": "602630d3",
897+
"metadata": {},
898+
"outputs": [],
899+
"source": [
900+
"# Define contrasts\n",
901+
"contrast_motion = np.array([1 if 'motion' in name else 0 for name in design_matrix.columns])\n",
902+
"# Estimate contrast\n",
903+
"z_map_motion = first_level_model.compute_contrast(contrast_motion, output_type='z_score')\n",
904+
"# Plot the resulting z-map\n",
905+
"plt.rcParams['figure.figsize'] = [12.5, 10]\n",
906+
"from nilearn.plotting import plot_stat_map\n",
907+
"plot_stat_map(z_map_motion, bg_img=anat_img, threshold=3.0,\n",
908+
" title='Z-map for Motion Contrast (thresholded at z>3.0)')\n",
909+
"show() "
910+
]
862911
}
863912
],
864913
"metadata": {

0 commit comments

Comments
 (0)