-
Notifications
You must be signed in to change notification settings - Fork 12
/
first_level_model.html
461 lines (423 loc) · 33.7 KB
/
first_level_model.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta property="og:title" content="5.2. First level models" />
<meta property="og:type" content="website" />
<meta property="og:url" content="https://nilearn.github.io/glm/first_level_model.html" />
<meta property="og:site_name" content="Nilearn" />
<meta property="og:description" content="Page summary: First level models are, in essence, linear regression models run at the level of a single session or single subject. The model is applied on a voxel-wise basis, either on the whole br..." />
<meta property="og:image" content="../_images/sphx_glr_plot_design_matrix_001.png" />
<meta property="og:image:alt" content="Nilearn" />
<title>Nilearn: Statistical Analysis for NeuroImaging in Python — Machine learning for NeuroImaging</title>
<link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../_static/nature.css" />
<link rel="stylesheet" type="text/css" href="../_static/copybutton.css" />
<link rel="stylesheet" type="text/css" href="../_static/sg_gallery.css" />
<link rel="stylesheet" type="text/css" href="../_static/sg_gallery-binder.css" />
<link rel="stylesheet" type="text/css" href="../_static/sg_gallery-dataframe.css" />
<link rel="stylesheet" type="text/css" href="../_static/sg_gallery-rendered-html.css" />
<script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/clipboard.min.js"></script>
<script src="../_static/copybutton.js"></script>
<link rel="shortcut icon" href="../_static/favicon.ico"/>
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="5.3. Second level models" href="second_level_model.html" />
<link rel="prev" title="5.1. An introduction to GLMs in fMRI statistical analysis" href="glm_intro.html" />
<meta content="True" name="HandheldFriendly">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<meta name="keywords" content="nilearn, neuroimaging, python, neuroscience, machinelearning">
<script type="text/javascript">
function updateTopMenuPosition(height, width) {
if($(window).scrollTop() > height && $(window).outerWidth() > 1024) {
//begin to scroll
$('.related-wrapper').css("z-index", 1000);
$('.related-wrapper').css("position", "sticky");
$('.related-wrapper').css("top", 0);
$('.related-wrapper').css("width", width)
} else {
//lock it back into place
$('.related-wrapper').css("position", "relative");
$('.related-wrapper').css("top", 0)
}
}
$(function() {
var banner_height = $('#logo-banner').outerHeight();
var banner_width = $('#logo-banner').outerWidth();
var width = $('.related-wrapper').css("height", $('.related').outerHeight());
updateTopMenuPosition(banner_height, width);
$(window).scroll(function(event) {
updateTopMenuPosition(banner_height, width)
});
$(window).resize(function(event) {
var banner_width = $('#logo-banner').outerWidth();
var menu_height = $('.related').outerHeight();
$('.related').css("width", banner_width);
$('.related-wrapper').css("height", menu_height);
updateTopMenuPosition(banner_height, width)
})
});
</script>
<script type="text/javascript">
function updateSideBarPosition(top, offset, sections) {
var pos = $(window).scrollTop();
// Lock the table of content to a fixed position once we scroll enough
var topShift = 2 * offset;
if(pos > top + topShift + 1) {
// begin to scroll with sticky menu bar
var topShift = -topShift + 1;
if ($(window).outerWidth() < 1024) {
// compensate top menu that disappears
topShift -= offset + 1
}
$('.sphinxsidebarwrapper').css("position", "fixed");
$('.sphinxsidebarwrapper').css("top", topShift)
}
else {
//lock it back into place
$('.sphinxsidebarwrapper').css("position", "relative");
$('.sphinxsidebarwrapper').css("top",0)
}
// Highlight the current section
i = 0;
current_section = 0;
$('a.internal').removeClass('active');
for(i in sections) {
if(sections[i] > pos) {
break
}
if($('a.internal[href$="' + i + '"]').is(':visible')){
current_section = i
}
}
$('a.internal[href$="' + current_section + '"]').addClass('active');
$('a.internal[href$="' + current_section + '"]').parent().addClass('active')
}
$(function () {
// Lock the table of content to a fixed position once we scroll enough
var tocOffset = $('.related-wrapper').outerHeight();
var marginTop = parseFloat($('.sphinxsidebarwrapper').css('margin-top').replace(/auto/, 0));
var top = $('.sphinxsidebarwrapper').offset().top - marginTop;
sections = {};
url = document.URL.replace(/#.*$/, "");
// Grab positions of our sections
$('.headerlink').each(function(){
sections[this.href.replace(url, '')] = $(this).offset().top - 50
});
updateSideBarPosition(top, tocOffset, sections);
$(window).scroll(function(event) {
updateSideBarPosition(top, tocOffset, sections)
});
$(window).resize(function(event) {
tocOffset = $('.related-wrapper').outerHeight();
updateSideBarPosition(top, tocOffset, sections)
});
});
</script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-41920728-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head><body>
<div id="logo-banner">
<div class="logo">
<a href="../index.html">
<img src="../_static/nilearn-logo.png" alt="Nilearn logo" border="0" />
</a>
</div>
<!-- A tag cloud to make it easy for people to find what they are
looking for -->
<div class="tags">
<ul>
<li>
<big><a href="../auto_examples/decoding/plot_haxby_anova_svm.html">SVM</a></big>
</li>
<li>
<small><a href="../connectivity/parcellating.html">Ward
clustering</a></small>
</li>
<li>
<a href="../decoding/searchlight.html">Searchlight</a>
</li>
<li>
<big><a href="../connectivity/resting_state_networks.html">ICA</a></big>
</li>
<li>
<a href="../manipulating_images/data_preparation.html">Nifti IO</a>
</li>
<li>
<a href="../modules/reference.html#module-nilearn.datasets">Datasets</a>
</li>
</ul>
</div>
<div class="banner">
<h1>Nilearn:</h1>
<h2>Statistics for NeuroImaging in Python</h2>
</div>
<div class="search_form">
<div class="gcse-search" id="cse" style="width: 100%;"></div>
<script>
(function() {
var cx = '017289614950330089114:elrt9qoutrq';
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = 'https://cse.google.com/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s);
})();
</script>
</div>
</div>
<div class=related-wrapper>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../py-modindex.html" title="Python Module Index"
>modules</a></li>
<li class="right" >
<a href="second_level_model.html" title="5.3. Second level models"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="glm_intro.html" title="5.1. An introduction to GLMs in fMRI statistical analysis"
accesskey="P">previous</a> |</li>
<li><a href="../index.html">Nilearn Home</a> | </li>
<li><a href="../user_guide.html">User Guide</a> | </li>
<li><a href="../auto_examples/index.html">Examples</a> | </li>
<li><a href="../modules/reference.html">Reference</a> | </li>
<li id="navbar-about"><a href="../authors.html">About</a>| </li>
<li><a href="../glossary.html">Glossary</a>| </li>
<li><a href="../bibliography.html">Bibliography</a>| </li>
<li id="navbar-ecosystem"><a href="http://www.nipy.org/">Nipy ecosystem</a></li>
<li class="nav-item nav-item-1"><a href="../user_guide.html" >User guide: table of contents</a> »</li>
<li class="nav-item nav-item-2"><a href="index.html" accesskey="U"><span class="section-number">5. </span>Analyzing fMRI using GLMs</a> »</li>
<li class="nav-item nav-item-this"><a href="">Nilearn: Statistical Analysis for NeuroImaging in Python</a></li>
</ul>
</div>
</div>
<div class="stable-banner">
This is the <em>stable</em> documentation for the latest release of Nilearn,
the current development version is available <a href="https://nilearn.github.io/dev/index.html">here</a>.
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="first-level-models">
<span id="first-level-model"></span><h1><span class="section-number">5.2. </span>First level models<a class="headerlink" href="#first-level-models" title="Permalink to this headline">¶</a></h1>
<div class="topic">
<p class="topic-title"><strong>Page summary</strong></p>
<p>First level models are, in essence, linear regression models run at the level of a single
session or single subject. The model is applied on a voxel-wise basis, either on the whole
brain or within a region of interest. The timecourse of each voxel is regressed against a
predicted BOLD response created by convolving the haemodynamic response function (HRF) with
a set of predictors defined within the design matrix.</p>
</div>
<div class="contents local topic" id="contents">
<p class="topic-title"><strong>Contents</strong></p>
<ul class="simple">
<li><p><a class="reference internal" href="#hrf-models" id="id1">HRF models</a></p></li>
<li><p><a class="reference internal" href="#design-matrix-event-based-and-time-series-based" id="id2">Design matrix: event-based and time series-based</a></p></li>
<li><p><a class="reference internal" href="#fitting-a-first-level-model" id="id3">Fitting a first level model</a></p></li>
<li><p><a class="reference internal" href="#extracting-predicted-time-series-and-residuals" id="id4">Extracting predicted time series and residuals</a></p></li>
<li><p><a class="reference internal" href="#surface-based-analysis" id="id5">Surface-based analysis</a></p></li>
</ul>
</div>
<div class="section" id="hrf-models">
<h2><a class="toc-backref" href="#id1"><span class="section-number">5.2.1. </span>HRF models</a><a class="headerlink" href="#hrf-models" title="Permalink to this headline">¶</a></h2>
<p>Nilearn offers a few different HRF models including the commonly used double-gamma SPM model (‘spm’)
and the model shape proposed by G. Glover (‘glover’), both allowing the option of adding time and
dispersion derivatives. The addition of these derivatives allows to better model any uncertainty in
timing information. In addition, an FIR (finite impulse response, ‘fir’) model of the HRF is also available.</p>
<p>In order to visualize the predicted regressor prior to plugging it into the linear model, use the
function <a class="reference internal" href="../modules/generated/nilearn.glm.first_level.compute_regressor.html#nilearn.glm.first_level.compute_regressor" title="nilearn.glm.first_level.compute_regressor"><code class="xref py py-func docutils literal notranslate"><span class="pre">nilearn.glm.first_level.compute_regressor</span></code></a>, or explore the HRF plotting
example <a class="reference internal" href="../auto_examples/04_glm_first_level/plot_hrf.html#sphx-glr-auto-examples-04-glm-first-level-plot-hrf-py"><span class="std std-ref">Example of MRI response functions</span></a>.</p>
</div>
<div class="section" id="design-matrix-event-based-and-time-series-based">
<h2><a class="toc-backref" href="#id2"><span class="section-number">5.2.2. </span>Design matrix: event-based and time series-based</a><a class="headerlink" href="#design-matrix-event-based-and-time-series-based" title="Permalink to this headline">¶</a></h2>
<div class="section" id="event-based">
<h3><span class="section-number">5.2.2.1. </span>Event-based<a class="headerlink" href="#event-based" title="Permalink to this headline">¶</a></h3>
<p>To create an event-based design matrix, information about the trial type, onset time and duration of the
events in the experiment are necessary. This can be provided by the user, or be part of the dataset if
using a <a class="reference internal" href="../glossary.html#term-BIDS"><span class="xref std std-term">BIDS</span></a>-compatible dataset or one of the nilearn dataset fetcher functions like
<a class="reference internal" href="../modules/generated/nilearn.datasets.fetch_spm_multimodal_fmri.html#nilearn.datasets.fetch_spm_multimodal_fmri" title="nilearn.datasets.fetch_spm_multimodal_fmri"><code class="xref py py-func docutils literal notranslate"><span class="pre">nilearn.datasets.fetch_spm_multimodal_fmri</span></code></a>,
<a class="reference internal" href="../modules/generated/nilearn.datasets.fetch_language_localizer_demo_dataset.html#nilearn.datasets.fetch_language_localizer_demo_dataset" title="nilearn.datasets.fetch_language_localizer_demo_dataset"><code class="xref py py-func docutils literal notranslate"><span class="pre">nilearn.datasets.fetch_language_localizer_demo_dataset</span></code></a>, etc.</p>
<dl class="simple">
<dt>Refer to the examples below for usage under the different scenarios:</dt><dd><ul class="simple">
<li><p>User-defined: <a class="reference internal" href="../auto_examples/04_glm_first_level/plot_design_matrix.html#sphx-glr-auto-examples-04-glm-first-level-plot-design-matrix-py"><span class="std std-ref">Examples of design matrices</span></a></p></li>
<li><p>Using an OpenNEURO dataset: <a class="reference internal" href="../auto_examples/04_glm_first_level/plot_bids_features.html#sphx-glr-auto-examples-04-glm-first-level-plot-bids-features-py"><span class="std std-ref">First level analysis of a complete BIDS dataset from openneuro</span></a></p></li>
<li><p>Uing nilearn fetcher functions: <a class="reference internal" href="../auto_examples/04_glm_first_level/plot_spm_multimodal_faces.html#sphx-glr-auto-examples-04-glm-first-level-plot-spm-multimodal-faces-py"><span class="std std-ref">Single-subject data (two sessions) in native space</span></a></p></li>
</ul>
</dd>
</dl>
<p>To ascertain that the sequence of events provided to the first level model is accurate, Nilearn provides an
event visualization function called <a class="reference internal" href="../modules/generated/nilearn.plotting.plot_event.html#nilearn.plotting.plot_event" title="nilearn.plotting.plot_event"><code class="xref py py-func docutils literal notranslate"><span class="pre">nilearn.plotting.plot_event</span></code></a>. Sample usage for this is available
in <a class="reference internal" href="../auto_examples/02_decoding/plot_haxby_glm_decoding.html#sphx-glr-auto-examples-02-decoding-plot-haxby-glm-decoding-py"><span class="std std-ref">Decoding of a dataset after GLM fit for signal extraction</span></a>.</p>
<p>Once the events are defined, the design matrix is created using the
<a class="reference internal" href="../modules/generated/nilearn.glm.first_level.make_first_level_design_matrix.html#nilearn.glm.first_level.make_first_level_design_matrix" title="nilearn.glm.first_level.make_first_level_design_matrix"><code class="xref py py-func docutils literal notranslate"><span class="pre">nilearn.glm.first_level.make_first_level_design_matrix</span></code></a> function:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">nilearn.glm.first_level</span> <span class="kn">import</span> <span class="n">make_first_level_design_matrix</span>
<span class="n">design_matrices</span> <span class="o">=</span> <span class="n">make_first_level_design_matrix</span><span class="p">(</span><span class="n">frame_times</span><span class="p">,</span> <span class="n">events</span><span class="p">,</span>
<span class="n">drift_model</span><span class="o">=</span><span class="s1">'polynomial'</span><span class="p">,</span> <span class="n">drift_order</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Additional predictors, like subject motion, can be specified using the add_reg parameter. Look at the function definition for available arguments.</p>
</div>
<p>A handy function called <a class="reference internal" href="../modules/generated/nilearn.plotting.plot_design_matrix.html#nilearn.plotting.plot_design_matrix" title="nilearn.plotting.plot_design_matrix"><code class="xref py py-func docutils literal notranslate"><span class="pre">nilearn.plotting.plot_design_matrix</span></code></a> can be used to visualize the design matrix.
This is generally a good practice to follow before proceeding with the analysis:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">nilearn.plotting</span> <span class="kn">import</span> <span class="n">plot_design_matrix</span>
<span class="n">plot_design_matrix</span><span class="p">(</span><span class="n">design_matrices</span><span class="p">)</span>
</pre></div>
</div>
<a class="reference external image-reference" href="../auto_examples/04_glm_first_level/plot_design_matrix.html#sphx-glr-auto-examples-04-glm-first-level-models-plot-design-matrix-py"><img alt="../_images/sphx_glr_plot_design_matrix_001.png" src="../_images/sphx_glr_plot_design_matrix_001.png" /></a>
</div>
<div class="section" id="time-series-based">
<h3><span class="section-number">5.2.2.2. </span>Time series-based<a class="headerlink" href="#time-series-based" title="Permalink to this headline">¶</a></h3>
<p>The time series of a seed region can also be used as the predictor for a first level model. This approach would help
identify brain areas co-activating with the seed region. The time series is extracted using
<a class="reference internal" href="../modules/generated/nilearn.maskers.NiftiSpheresMasker.html#nilearn.maskers.NiftiSpheresMasker" title="nilearn.maskers.NiftiSpheresMasker"><code class="xref py py-class docutils literal notranslate"><span class="pre">nilearn.maskers.NiftiSpheresMasker</span></code></a>. For instance, if the seed region is the posterior
cingulate cortex with coordinate [pcc_coords]:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">nilearn.maskers</span> <span class="kn">import</span> <span class="n">NiftiSpheresMasker</span>
<span class="n">seed_masker</span> <span class="o">=</span> <span class="n">NiftiSpheresMasker</span><span class="p">([</span><span class="n">pcc_coords</span><span class="p">],</span> <span class="n">radius</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
<span class="n">seed_time_series</span> <span class="o">=</span> <span class="n">seed_masker</span><span class="o">.</span><span class="n">fit_transform</span><span class="p">(</span><span class="n">adhd_dataset</span><span class="o">.</span><span class="n">func</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
</pre></div>
</div>
<p>The seed_time_series is then passed into the design matrix using the add_reg argument mentioned in the note
above. Code for this approach is in <a class="reference internal" href="../auto_examples/04_glm_first_level/plot_adhd_dmn.html#sphx-glr-auto-examples-04-glm-first-level-plot-adhd-dmn-py"><span class="std std-ref">Default Mode Network extraction of AHDH dataset</span></a>.</p>
</div>
</div>
<div class="section" id="fitting-a-first-level-model">
<h2><a class="toc-backref" href="#id3"><span class="section-number">5.2.3. </span>Fitting a first level model</a><a class="headerlink" href="#fitting-a-first-level-model" title="Permalink to this headline">¶</a></h2>
<p>The <a class="reference internal" href="../modules/generated/nilearn.glm.first_level.FirstLevelModel.html#nilearn.glm.first_level.FirstLevelModel" title="nilearn.glm.first_level.FirstLevelModel"><code class="xref py py-class docutils literal notranslate"><span class="pre">nilearn.glm.first_level.FirstLevelModel</span></code></a> class provides the tools to fit the linear model to
the fMRI data. The <a class="reference internal" href="../modules/generated/nilearn.glm.first_level.FirstLevelModel.html#nilearn.glm.first_level.FirstLevelModel.fit" title="nilearn.glm.first_level.FirstLevelModel.fit"><code class="xref py py-func docutils literal notranslate"><span class="pre">nilearn.glm.first_level.FirstLevelModel.fit</span></code></a> function takes the fMRI data
and design matrix as input and fits the GLM. Like other Nilearn functions,
<a class="reference internal" href="../modules/generated/nilearn.glm.first_level.FirstLevelModel.html#nilearn.glm.first_level.FirstLevelModel.fit" title="nilearn.glm.first_level.FirstLevelModel.fit"><code class="xref py py-func docutils literal notranslate"><span class="pre">nilearn.glm.first_level.FirstLevelModel.fit</span></code></a> accepts file names as input, but can also
work with <a class="reference external" href="https://nipy.org/nibabel/nibabel_images.html">NiftiImage objects</a>. More information about
input formats is available <a class="reference external" href="http://nilearn.github.io/manipulating_images/input_output.html#inputing-data-file-names-or-image-objects">here</a></p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">nilearn.glm.first_level</span> <span class="kn">import</span> <span class="n">FirstLevelModel</span>
<span class="n">fmri_glm</span> <span class="o">=</span> <span class="n">FirstLevelModel</span><span class="p">()</span>
<span class="n">fmri_glm</span> <span class="o">=</span> <span class="n">fmri_glm</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">subject_data</span><span class="p">,</span> <span class="n">design_matrices</span><span class="o">=</span><span class="n">design_matrices</span><span class="p">)</span>
</pre></div>
</div>
<div class="section" id="computing-contrasts">
<h3><span class="section-number">5.2.3.1. </span>Computing contrasts<a class="headerlink" href="#computing-contrasts" title="Permalink to this headline">¶</a></h3>
<p>To get more interesting results out of the GLM model, contrasts can be computed between regressors of interest.
The <a class="reference internal" href="../modules/generated/nilearn.glm.first_level.FirstLevelModel.html#nilearn.glm.first_level.FirstLevelModel.compute_contrast" title="nilearn.glm.first_level.FirstLevelModel.compute_contrast"><code class="xref py py-func docutils literal notranslate"><span class="pre">nilearn.glm.first_level.FirstLevelModel.compute_contrast</span></code></a> function can be used for that. First,
the contrasts of interest must be defined. In the spm_multimodal_fmri dataset referenced above, subjects are
presented with ‘normal’ and ‘scrambled’ faces. The basic contrasts that can be constructed are the main effects
of ‘normal faces’ and ‘scrambled faces’. Once the basic_contrasts have been set up, we can construct more
interesting contrasts like ‘normal faces - scrambled faces’.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>The compute_contrast function can work with both numeric and symbolic arguments. See <a class="reference internal" href="../modules/generated/nilearn.glm.first_level.FirstLevelModel.html#nilearn.glm.first_level.FirstLevelModel.compute_contrast" title="nilearn.glm.first_level.FirstLevelModel.compute_contrast"><code class="xref py py-func docutils literal notranslate"><span class="pre">nilearn.glm.first_level.FirstLevelModel.compute_contrast</span></code></a> for more information.</p>
</div>
<p>And finally we can compute the contrasts using the compute_contrast function.
Refer to <a class="reference internal" href="../auto_examples/04_glm_first_level/plot_spm_multimodal_faces.html#sphx-glr-auto-examples-04-glm-first-level-plot-spm-multimodal-faces-py"><span class="std std-ref">Single-subject data (two sessions) in native space</span></a> for the full example.</p>
<p>The activation maps from these 3 contrasts is presented below:</p>
<a class="reference external image-reference" href="../auto_examples/04_glm_first_level/plot_spm_multimodal_faces.html"><img alt="../_images/sphx_glr_plot_spm_multimodal_faces_001.png" src="../_images/sphx_glr_plot_spm_multimodal_faces_001.png" style="width: 438.0px; height: 137.4px;" /></a>
<a class="reference external image-reference" href="../auto_examples/04_glm_first_level/plot_spm_multimodal_faces.html"><img alt="../_images/sphx_glr_plot_spm_multimodal_faces_002.png" src="../_images/sphx_glr_plot_spm_multimodal_faces_002.png" style="width: 438.0px; height: 137.4px;" /></a>
<a class="reference external image-reference" href="../auto_examples/04_glm_first_level/plot_spm_multimodal_faces.html"><img alt="../_images/sphx_glr_plot_spm_multimodal_faces_003.png" src="../_images/sphx_glr_plot_spm_multimodal_faces_003.png" style="width: 438.0px; height: 137.4px;" /></a>
<p>Additional example: <a class="reference internal" href="../auto_examples/04_glm_first_level/plot_fiac_analysis.html#sphx-glr-auto-examples-04-glm-first-level-plot-fiac-analysis-py"><span class="std std-ref">Simple example of two-session fMRI model fitting</span></a></p>
</div>
</div>
<div class="section" id="extracting-predicted-time-series-and-residuals">
<h2><a class="toc-backref" href="#id4"><span class="section-number">5.2.4. </span>Extracting predicted time series and residuals</a><a class="headerlink" href="#extracting-predicted-time-series-and-residuals" title="Permalink to this headline">¶</a></h2>
<p>One way to assess the quality of the fit is to compare the observed and predicted time series of voxels.
Nilearn makes the predicted time series easily accessible via a parameter called <cite>predicted</cite> that is part
of the <a class="reference internal" href="../modules/generated/nilearn.glm.first_level.FirstLevelModel.html#nilearn.glm.first_level.FirstLevelModel" title="nilearn.glm.first_level.FirstLevelModel"><code class="xref py py-class docutils literal notranslate"><span class="pre">nilearn.glm.first_level.FirstLevelModel</span></code></a>. This parameter is populated when
FistLevelModel is initialized with the <cite>minimize_memory</cite> flag set to <cite>False</cite>.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">observed_timeseries</span> <span class="o">=</span> <span class="n">masker</span><span class="o">.</span><span class="n">fit_transform</span><span class="p">(</span><span class="n">fmri_img</span><span class="p">)</span>
<span class="n">predicted_timeseries</span> <span class="o">=</span> <span class="n">masker</span><span class="o">.</span><span class="n">fit_transform</span><span class="p">(</span><span class="n">fmri_glm</span><span class="o">.</span><span class="n">predicted</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
</pre></div>
</div>
<p>Here, masker is an object of <a class="reference internal" href="../modules/generated/nilearn.maskers.NiftiSpheresMasker.html#nilearn.maskers.NiftiSpheresMasker" title="nilearn.maskers.NiftiSpheresMasker"><code class="xref py py-class docutils literal notranslate"><span class="pre">nilearn.maskers.NiftiSpheresMasker</span></code></a>. In the figure below,
predicted (red) and observed (not red) timecourses of 6 voxels are shown.</p>
<blockquote>
<div><a class="reference external image-reference" href="../auto_examples/04_glm_first_level/plot_predictions_residuals.html"><img alt="../_images/sphx_glr_plot_predictions_residuals_002.png" src="../_images/sphx_glr_plot_predictions_residuals_002.png" /></a>
</div></blockquote>
<p>In addition to the predicted timecourses, this flag also yields the residuals of the GLM. The residuals are
useful to calculate the F and R-squared statistic. For more information refer to
<a class="reference internal" href="../auto_examples/04_glm_first_level/plot_predictions_residuals.html#sphx-glr-auto-examples-04-glm-first-level-plot-predictions-residuals-py"><span class="std std-ref">Predicted time series and residuals</span></a></p>
</div>
<div class="section" id="surface-based-analysis">
<h2><a class="toc-backref" href="#id5"><span class="section-number">5.2.5. </span>Surface-based analysis</a><a class="headerlink" href="#surface-based-analysis" title="Permalink to this headline">¶</a></h2>
<p>fMRI analyses can also be performed on the cortical surface instead of a volumetric brain. Nilearn
provides functions to map subject brains on to a cortical mesh, which can be either a standard surface as
provided by, for e.g. Freesurfer, or a user-defined one. Freesurfer meshes can be accessed using
<a class="reference internal" href="../modules/generated/nilearn.datasets.fetch_surf_fsaverage.html#nilearn.datasets.fetch_surf_fsaverage" title="nilearn.datasets.fetch_surf_fsaverage"><code class="xref py py-func docutils literal notranslate"><span class="pre">nilearn.datasets.fetch_surf_fsaverage</span></code></a>, while the function <a class="reference internal" href="../modules/generated/nilearn.surface.vol_to_surf.html#nilearn.surface.vol_to_surf" title="nilearn.surface.vol_to_surf"><code class="xref py py-func docutils literal notranslate"><span class="pre">nilearn.surface.vol_to_surf</span></code></a>
does the projection from volumetric to surface space. Surface plotting functions like <a class="reference internal" href="../modules/generated/nilearn.plotting.plot_surf.html#nilearn.plotting.plot_surf" title="nilearn.plotting.plot_surf"><code class="xref py py-func docutils literal notranslate"><span class="pre">nilearn.plotting.plot_surf</span></code></a>
and <a class="reference internal" href="../modules/generated/nilearn.plotting.plot_surf_stat_map.html#nilearn.plotting.plot_surf_stat_map" title="nilearn.plotting.plot_surf_stat_map"><code class="xref py py-func docutils literal notranslate"><span class="pre">nilearn.plotting.plot_surf_stat_map</span></code></a> allow for easy visualization of surface-based data.</p>
<p>For a complete example refer to <a class="reference internal" href="../auto_examples/04_glm_first_level/plot_localizer_surface_analysis.html#sphx-glr-auto-examples-04-glm-first-level-plot-localizer-surface-analysis-py"><span class="std std-ref">Example of surface-based first-level analysis</span></a></p>
</div>
</div>
<div class="clearer"></div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h4> Giving credit </h4>
<ul class="simple">
<li><p>Please consider <a href="../authors.html#citing">citing the
papers</a>.</p></li>
</ul>
<h3><a href="../index.html">Table of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">5.2. First level models</a><ul>
<li><a class="reference internal" href="#hrf-models">5.2.1. HRF models</a></li>
<li><a class="reference internal" href="#design-matrix-event-based-and-time-series-based">5.2.2. Design matrix: event-based and time series-based</a><ul>
<li><a class="reference internal" href="#event-based">5.2.2.1. Event-based</a></li>
<li><a class="reference internal" href="#time-series-based">5.2.2.2. Time series-based</a></li>
</ul>
</li>
<li><a class="reference internal" href="#fitting-a-first-level-model">5.2.3. Fitting a first level model</a><ul>
<li><a class="reference internal" href="#computing-contrasts">5.2.3.1. Computing contrasts</a></li>
</ul>
</li>
<li><a class="reference internal" href="#extracting-predicted-time-series-and-residuals">5.2.4. Extracting predicted time series and residuals</a></li>
<li><a class="reference internal" href="#surface-based-analysis">5.2.5. Surface-based analysis</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="glm_intro.html"
title="previous chapter"><span class="section-number">5.1. </span>An introduction to GLMs in fMRI statistical analysis</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="second_level_model.html"
title="next chapter"><span class="section-number">5.3. </span>Second level models</a></p>
<div id="searchbox" style="display: none" role="search">
<h3 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" />
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
© The nilearn developers 2010-2022.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 4.0.2.
<span style="padding-left: 5ex;">
<a href="../_sources/glm/first_level_model.rst.txt"
rel="nofollow">Show this page source</a>
</span>
</div>
</body>
</html>