-
Notifications
You must be signed in to change notification settings - Fork 12
/
plot_3d_and_4d_niimg.html
488 lines (449 loc) · 36.7 KB
/
plot_3d_and_4d_niimg.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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
<!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="9.1.3. 3D and 4D niimgs: handling and visualizing" />
<meta property="og:type" content="website" />
<meta property="og:url" content="https://nilearn.github.io/auto_examples/plot_3d_and_4d_niimg.html" />
<meta property="og:site_name" content="Nilearn" />
<meta property="og:description" content="Here we discover how to work with 3D and 4D niimgs. Downloading tutorial datasets from Internet: Nilearn comes with functions that download public data from Internet Let’s first check where the dat..." />
<meta property="og:image" content="https://nilearn.github.io/_static/nilearn-logo.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="9.1.4. A introduction tutorial to fMRI decoding" href="plot_decoding_tutorial.html" />
<link rel="prev" title="9.1.2. Basic nilearn example: manipulating and looking at data" href="plot_nilearn_101.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="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="plot_decoding_tutorial.html" title="9.1.4. A introduction tutorial to fMRI decoding"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="plot_nilearn_101.html" title="9.1.2. Basic nilearn example: manipulating and looking at data"
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="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">9. </span>Nilearn usage examples</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="sphx-glr-download-link-note admonition note">
<p class="admonition-title">Note</p>
<p>Click <a class="reference internal" href="#sphx-glr-download-auto-examples-plot-3d-and-4d-niimg-py"><span class="std std-ref">here</span></a>
to download the full example code or to run this example in your browser via Binder</p>
</div>
<div class="sphx-glr-example-title section" id="d-and-4d-niimgs-handling-and-visualizing">
<span id="sphx-glr-auto-examples-plot-3d-and-4d-niimg-py"></span><h1><span class="section-number">9.1.3. </span>3D and 4D niimgs: handling and visualizing<a class="headerlink" href="#d-and-4d-niimgs-handling-and-visualizing" title="Permalink to this headline">¶</a></h1>
<p>Here we discover how to work with 3D and 4D niimgs.</p>
<div class="section" id="downloading-tutorial-datasets-from-internet">
<h2><span class="section-number">9.1.3.1. </span>Downloading tutorial datasets from Internet<a class="headerlink" href="#downloading-tutorial-datasets-from-internet" title="Permalink to this headline">¶</a></h2>
<p>Nilearn comes with functions that download public data from Internet</p>
<p>Let’s first check where the data is downloaded on our disk:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">nilearn</span> <span class="kn">import</span> <span class="n">datasets</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">'Datasets are stored in: </span><span class="si">%r</span><span class="s1">'</span> <span class="o">%</span> <a href="../modules/generated/nilearn.datasets.get_data_dirs.html#nilearn.datasets.get_data_dirs" title="nilearn.datasets.get_data_dirs" class="sphx-glr-backref-module-nilearn-datasets sphx-glr-backref-type-py-function"><span class="n">datasets</span><span class="o">.</span><span class="n">get_data_dirs</span></a><span class="p">())</span>
</pre></div>
</div>
<p class="sphx-glr-script-out">Out:</p>
<div class="sphx-glr-script-out highlight-none notranslate"><div class="highlight"><pre><span></span>Datasets are stored in: ['/home/nicolas/nilearn_data']
</pre></div>
</div>
<p>Let’s now retrieve a motor contrast from a Neurovault repository</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><a href="https://scikit-learn.org/stable/modules/generated/sklearn.utils.Bunch.html#sklearn.utils.Bunch" title="sklearn.utils.Bunch" class="sphx-glr-backref-module-sklearn-utils sphx-glr-backref-type-py-function"><span class="n">motor_images</span></a> <span class="o">=</span> <a href="../modules/generated/nilearn.datasets.fetch_neurovault_motor_task.html#nilearn.datasets.fetch_neurovault_motor_task" title="nilearn.datasets.fetch_neurovault_motor_task" class="sphx-glr-backref-module-nilearn-datasets sphx-glr-backref-type-py-function"><span class="n">datasets</span><span class="o">.</span><span class="n">fetch_neurovault_motor_task</span></a><span class="p">()</span>
<a href="https://docs.python.org/3.8/library/stdtypes.html#list" title="builtins.list" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">motor_images</span><span class="o">.</span><span class="n">images</span></a>
</pre></div>
</div>
<p class="sphx-glr-script-out">Out:</p>
<div class="sphx-glr-script-out highlight-none notranslate"><div class="highlight"><pre><span></span>['/home/nicolas/nilearn_data/neurovault/collection_658/image_10426.nii.gz']
</pre></div>
</div>
<p>motor_images is a list of filenames. We need to take the first one</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><a href="https://docs.python.org/3.8/library/stdtypes.html#str" title="builtins.str" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">tmap_filename</span></a> <span class="o">=</span> <a href="https://docs.python.org/3.8/library/stdtypes.html#list" title="builtins.list" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">motor_images</span><span class="o">.</span><span class="n">images</span></a><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
</pre></div>
</div>
</div>
<div class="section" id="visualizing-a-3d-file">
<h2><span class="section-number">9.1.3.2. </span>Visualizing a 3D file<a class="headerlink" href="#visualizing-a-3d-file" title="Permalink to this headline">¶</a></h2>
<p>The file contains a 3D volume, we can easily visualize it as a
statistical map:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">nilearn</span> <span class="kn">import</span> <span class="n">plotting</span>
<a href="../modules/generated/nilearn.plotting.plot_stat_map.html#nilearn.plotting.plot_stat_map" title="nilearn.plotting.plot_stat_map" class="sphx-glr-backref-module-nilearn-plotting sphx-glr-backref-type-py-function"><span class="n">plotting</span><span class="o">.</span><span class="n">plot_stat_map</span></a><span class="p">(</span><a href="https://docs.python.org/3.8/library/stdtypes.html#str" title="builtins.str" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">tmap_filename</span></a><span class="p">)</span>
</pre></div>
</div>
<img src="../_images/sphx_glr_plot_3d_and_4d_niimg_001.png" srcset="../_images/sphx_glr_plot_3d_and_4d_niimg_001.png" alt="plot 3d and 4d niimg" class = "sphx-glr-single-img"/><p class="sphx-glr-script-out">Out:</p>
<div class="sphx-glr-script-out highlight-none notranslate"><div class="highlight"><pre><span></span>/home/nicolas/GitRepos/nilearn-fork/nilearn/plotting/img_plotting.py:300: FutureWarning: Default resolution of the MNI template will change from 2mm to 1mm in version 0.10.0
anat_img = load_mni152_template()
<nilearn.plotting.displays._slicers.OrthoSlicer object at 0x7fa52d3da2b0>
</pre></div>
</div>
<p>Visualizing works better with a threshold</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><a href="../modules/generated/nilearn.plotting.plot_stat_map.html#nilearn.plotting.plot_stat_map" title="nilearn.plotting.plot_stat_map" class="sphx-glr-backref-module-nilearn-plotting sphx-glr-backref-type-py-function"><span class="n">plotting</span><span class="o">.</span><span class="n">plot_stat_map</span></a><span class="p">(</span><a href="https://docs.python.org/3.8/library/stdtypes.html#str" title="builtins.str" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">tmap_filename</span></a><span class="p">,</span> <span class="n">threshold</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
</pre></div>
</div>
<img src="../_images/sphx_glr_plot_3d_and_4d_niimg_002.png" srcset="../_images/sphx_glr_plot_3d_and_4d_niimg_002.png" alt="plot 3d and 4d niimg" class = "sphx-glr-single-img"/><p class="sphx-glr-script-out">Out:</p>
<div class="sphx-glr-script-out highlight-none notranslate"><div class="highlight"><pre><span></span><nilearn.plotting.displays._slicers.OrthoSlicer object at 0x7fa52d4e1b20>
</pre></div>
</div>
</div>
<div class="section" id="visualizing-one-volume-in-a-4d-file">
<h2><span class="section-number">9.1.3.3. </span>Visualizing one volume in a 4D file<a class="headerlink" href="#visualizing-one-volume-in-a-4d-file" title="Permalink to this headline">¶</a></h2>
<p>We can download resting-state networks from the Smith 2009 study on
correspondence between rest and task</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><a href="https://docs.python.org/3.8/library/stdtypes.html#str" title="builtins.str" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">rsn</span></a> <span class="o">=</span> <a href="../modules/generated/nilearn.datasets.fetch_atlas_smith_2009.html#nilearn.datasets.fetch_atlas_smith_2009" title="nilearn.datasets.fetch_atlas_smith_2009" class="sphx-glr-backref-module-nilearn-datasets sphx-glr-backref-type-py-function"><span class="n">datasets</span><span class="o">.</span><span class="n">fetch_atlas_smith_2009</span></a><span class="p">()[</span><span class="s1">'rsn10'</span><span class="p">]</span>
<a href="https://docs.python.org/3.8/library/stdtypes.html#str" title="builtins.str" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">rsn</span></a>
</pre></div>
</div>
<p class="sphx-glr-script-out">Out:</p>
<div class="sphx-glr-script-out highlight-none notranslate"><div class="highlight"><pre><span></span>'/home/nicolas/nilearn_data/smith_2009/PNAS_Smith09_rsn10.nii.gz'
</pre></div>
</div>
<p>It is a 4D nifti file. We load it into the memory to print its
shape.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">nilearn</span> <span class="kn">import</span> <span class="n">image</span>
<span class="nb">print</span><span class="p">(</span><a href="../modules/generated/nilearn.image.load_img.html#nilearn.image.load_img" title="nilearn.image.load_img" class="sphx-glr-backref-module-nilearn-image sphx-glr-backref-type-py-function"><span class="n">image</span><span class="o">.</span><span class="n">load_img</span></a><span class="p">(</span><a href="https://docs.python.org/3.8/library/stdtypes.html#str" title="builtins.str" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">rsn</span></a><span class="p">)</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
</pre></div>
</div>
<p class="sphx-glr-script-out">Out:</p>
<div class="sphx-glr-script-out highlight-none notranslate"><div class="highlight"><pre><span></span>(91, 109, 91, 10)
</pre></div>
</div>
<p>We can retrieve the first volume (note that Python indexing starts at 0):</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><a href="https://nipy.org/nibabel/reference/nibabel.nifti1.html#nibabel.nifti1.Nifti1Image" title="nibabel.nifti1.Nifti1Image" class="sphx-glr-backref-module-nibabel-nifti1 sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">first_rsn</span></a> <span class="o">=</span> <a href="../modules/generated/nilearn.image.index_img.html#nilearn.image.index_img" title="nilearn.image.index_img" class="sphx-glr-backref-module-nilearn-image sphx-glr-backref-type-py-function"><span class="n">image</span><span class="o">.</span><span class="n">index_img</span></a><span class="p">(</span><a href="https://docs.python.org/3.8/library/stdtypes.html#str" title="builtins.str" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">rsn</span></a><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><a href="https://nipy.org/nibabel/reference/nibabel.dataobj_images.html#nibabel.dataobj_images.DataobjImage.shape" title="nibabel.dataobj_images.DataobjImage.shape" class="sphx-glr-backref-module-nibabel-dataobj_images sphx-glr-backref-type-py-method"><span class="n">first_rsn</span><span class="o">.</span><span class="n">shape</span></a><span class="p">)</span>
</pre></div>
</div>
<p class="sphx-glr-script-out">Out:</p>
<div class="sphx-glr-script-out highlight-none notranslate"><div class="highlight"><pre><span></span>(91, 109, 91)
</pre></div>
</div>
<p>first_rsn is a 3D image.</p>
<p>We can then plot it</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><a href="../modules/generated/nilearn.plotting.plot_stat_map.html#nilearn.plotting.plot_stat_map" title="nilearn.plotting.plot_stat_map" class="sphx-glr-backref-module-nilearn-plotting sphx-glr-backref-type-py-function"><span class="n">plotting</span><span class="o">.</span><span class="n">plot_stat_map</span></a><span class="p">(</span><a href="https://nipy.org/nibabel/reference/nibabel.nifti1.html#nibabel.nifti1.Nifti1Image" title="nibabel.nifti1.Nifti1Image" class="sphx-glr-backref-module-nibabel-nifti1 sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">first_rsn</span></a><span class="p">)</span>
</pre></div>
</div>
<img src="../_images/sphx_glr_plot_3d_and_4d_niimg_003.png" srcset="../_images/sphx_glr_plot_3d_and_4d_niimg_003.png" alt="plot 3d and 4d niimg" class = "sphx-glr-single-img"/><p class="sphx-glr-script-out">Out:</p>
<div class="sphx-glr-script-out highlight-none notranslate"><div class="highlight"><pre><span></span><nilearn.plotting.displays._slicers.OrthoSlicer object at 0x7fa52d336a60>
</pre></div>
</div>
</div>
<div class="section" id="looping-on-all-volumes-in-a-4d-file">
<h2><span class="section-number">9.1.3.4. </span>Looping on all volumes in a 4D file<a class="headerlink" href="#looping-on-all-volumes-in-a-4d-file" title="Permalink to this headline">¶</a></h2>
<p>If we want to plot all the volumes in this 4D file, we can use iter_img
to loop on them.</p>
<p>Then we give a few arguments to plot_stat_map in order to have a more
compact display.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">for</span> <a href="https://nipy.org/nibabel/reference/nibabel.nifti1.html#nibabel.nifti1.Nifti1Image" title="nibabel.nifti1.Nifti1Image" class="sphx-glr-backref-module-nibabel-nifti1 sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">img</span></a> <span class="ow">in</span> <a href="../modules/generated/nilearn.image.iter_img.html#nilearn.image.iter_img" title="nilearn.image.iter_img" class="sphx-glr-backref-module-nilearn-image sphx-glr-backref-type-py-function"><span class="n">image</span><span class="o">.</span><span class="n">iter_img</span></a><span class="p">(</span><a href="https://docs.python.org/3.8/library/stdtypes.html#str" title="builtins.str" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">rsn</span></a><span class="p">):</span>
<span class="c1"># img is now an in-memory 3D img</span>
<a href="../modules/generated/nilearn.plotting.plot_stat_map.html#nilearn.plotting.plot_stat_map" title="nilearn.plotting.plot_stat_map" class="sphx-glr-backref-module-nilearn-plotting sphx-glr-backref-type-py-function"><span class="n">plotting</span><span class="o">.</span><span class="n">plot_stat_map</span></a><span class="p">(</span><a href="https://nipy.org/nibabel/reference/nibabel.nifti1.html#nibabel.nifti1.Nifti1Image" title="nibabel.nifti1.Nifti1Image" class="sphx-glr-backref-module-nibabel-nifti1 sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">img</span></a><span class="p">,</span> <span class="n">threshold</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">display_mode</span><span class="o">=</span><span class="s2">"z"</span><span class="p">,</span> <span class="n">cut_coords</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
<span class="n">colorbar</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
</pre></div>
</div>
<ul class="sphx-glr-horizontal">
<li><img src="../_images/sphx_glr_plot_3d_and_4d_niimg_004.png" srcset="../_images/sphx_glr_plot_3d_and_4d_niimg_004.png" alt="plot 3d and 4d niimg" class = "sphx-glr-multi-img"/></li>
<li><img src="../_images/sphx_glr_plot_3d_and_4d_niimg_005.png" srcset="../_images/sphx_glr_plot_3d_and_4d_niimg_005.png" alt="plot 3d and 4d niimg" class = "sphx-glr-multi-img"/></li>
<li><img src="../_images/sphx_glr_plot_3d_and_4d_niimg_006.png" srcset="../_images/sphx_glr_plot_3d_and_4d_niimg_006.png" alt="plot 3d and 4d niimg" class = "sphx-glr-multi-img"/></li>
<li><img src="../_images/sphx_glr_plot_3d_and_4d_niimg_007.png" srcset="../_images/sphx_glr_plot_3d_and_4d_niimg_007.png" alt="plot 3d and 4d niimg" class = "sphx-glr-multi-img"/></li>
<li><img src="../_images/sphx_glr_plot_3d_and_4d_niimg_008.png" srcset="../_images/sphx_glr_plot_3d_and_4d_niimg_008.png" alt="plot 3d and 4d niimg" class = "sphx-glr-multi-img"/></li>
<li><img src="../_images/sphx_glr_plot_3d_and_4d_niimg_009.png" srcset="../_images/sphx_glr_plot_3d_and_4d_niimg_009.png" alt="plot 3d and 4d niimg" class = "sphx-glr-multi-img"/></li>
<li><img src="../_images/sphx_glr_plot_3d_and_4d_niimg_010.png" srcset="../_images/sphx_glr_plot_3d_and_4d_niimg_010.png" alt="plot 3d and 4d niimg" class = "sphx-glr-multi-img"/></li>
<li><img src="../_images/sphx_glr_plot_3d_and_4d_niimg_011.png" srcset="../_images/sphx_glr_plot_3d_and_4d_niimg_011.png" alt="plot 3d and 4d niimg" class = "sphx-glr-multi-img"/></li>
<li><img src="../_images/sphx_glr_plot_3d_and_4d_niimg_012.png" srcset="../_images/sphx_glr_plot_3d_and_4d_niimg_012.png" alt="plot 3d and 4d niimg" class = "sphx-glr-multi-img"/></li>
<li><img src="../_images/sphx_glr_plot_3d_and_4d_niimg_013.png" srcset="../_images/sphx_glr_plot_3d_and_4d_niimg_013.png" alt="plot 3d and 4d niimg" class = "sphx-glr-multi-img"/></li>
</ul>
</div>
<div class="section" id="looping-through-selected-volumes-in-a-4d-file">
<h2><span class="section-number">9.1.3.5. </span>Looping through selected volumes in a 4D file<a class="headerlink" href="#looping-through-selected-volumes-in-a-4d-file" title="Permalink to this headline">¶</a></h2>
<p>If we want to plot selected volumes in this 4D file, we can use index_img
with the <cite>slice</cite> constructor to select the desired volumes.</p>
<p>Afterwards, we’ll use iter_img to loop through them following the same
formula as before.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><a href="https://nipy.org/nibabel/reference/nibabel.nifti1.html#nibabel.nifti1.Nifti1Image" title="nibabel.nifti1.Nifti1Image" class="sphx-glr-backref-module-nibabel-nifti1 sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">selected_volumes</span></a> <span class="o">=</span> <a href="../modules/generated/nilearn.image.index_img.html#nilearn.image.index_img" title="nilearn.image.index_img" class="sphx-glr-backref-module-nilearn-image sphx-glr-backref-type-py-function"><span class="n">image</span><span class="o">.</span><span class="n">index_img</span></a><span class="p">(</span><a href="https://docs.python.org/3.8/library/stdtypes.html#str" title="builtins.str" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">rsn</span></a><span class="p">,</span> <span class="nb">slice</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">))</span>
</pre></div>
</div>
<p>If you’re new to Python, one thing to note is that the slice constructor
uses 0-based indexing. You can confirm this by matching these slices
to the previous plot above.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">for</span> <a href="https://nipy.org/nibabel/reference/nibabel.nifti1.html#nibabel.nifti1.Nifti1Image" title="nibabel.nifti1.Nifti1Image" class="sphx-glr-backref-module-nibabel-nifti1 sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">img</span></a> <span class="ow">in</span> <a href="../modules/generated/nilearn.image.iter_img.html#nilearn.image.iter_img" title="nilearn.image.iter_img" class="sphx-glr-backref-module-nilearn-image sphx-glr-backref-type-py-function"><span class="n">image</span><span class="o">.</span><span class="n">iter_img</span></a><span class="p">(</span><a href="https://nipy.org/nibabel/reference/nibabel.nifti1.html#nibabel.nifti1.Nifti1Image" title="nibabel.nifti1.Nifti1Image" class="sphx-glr-backref-module-nibabel-nifti1 sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">selected_volumes</span></a><span class="p">):</span>
<a href="../modules/generated/nilearn.plotting.plot_stat_map.html#nilearn.plotting.plot_stat_map" title="nilearn.plotting.plot_stat_map" class="sphx-glr-backref-module-nilearn-plotting sphx-glr-backref-type-py-function"><span class="n">plotting</span><span class="o">.</span><span class="n">plot_stat_map</span></a><span class="p">(</span><a href="https://nipy.org/nibabel/reference/nibabel.nifti1.html#nibabel.nifti1.Nifti1Image" title="nibabel.nifti1.Nifti1Image" class="sphx-glr-backref-module-nibabel-nifti1 sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">img</span></a><span class="p">)</span>
</pre></div>
</div>
<ul class="sphx-glr-horizontal">
<li><img src="../_images/sphx_glr_plot_3d_and_4d_niimg_014.png" srcset="../_images/sphx_glr_plot_3d_and_4d_niimg_014.png" alt="plot 3d and 4d niimg" class = "sphx-glr-multi-img"/></li>
<li><img src="../_images/sphx_glr_plot_3d_and_4d_niimg_015.png" srcset="../_images/sphx_glr_plot_3d_and_4d_niimg_015.png" alt="plot 3d and 4d niimg" class = "sphx-glr-multi-img"/></li>
</ul>
<p>plotting.show is useful to force the display of figures when running
outside IPython</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><a href="../modules/generated/nilearn.plotting.show.html#nilearn.plotting.show" title="nilearn.plotting.show" class="sphx-glr-backref-module-nilearn-plotting sphx-glr-backref-type-py-function"><span class="n">plotting</span><span class="o">.</span><span class="n">show</span></a><span class="p">()</span>
</pre></div>
</div>
<div class="line-block">
<div class="line"><br /></div>
</div>
<hr class="docutils" />
<p>To recap, neuroimaging images (niimgs as we call them) come in
different flavors:</p>
<ul class="simple">
<li><p>3D images, containing only one brain volume</p></li>
<li><p>4D images, containing multiple brain volumes.</p></li>
</ul>
<p>More details about the input formats in nilearn for 3D and 4D images is
given in the documentation section: <a class="reference internal" href="../manipulating_images/input_output.html#loading-data"><span class="std std-ref">Inputing data: file names or image objects</span></a>.</p>
<p>Functions accept either 3D or 4D images, and we need to use on the one
hand <a class="reference internal" href="../modules/generated/nilearn.image.index_img.html#nilearn.image.index_img" title="nilearn.image.index_img"><code class="xref py py-func docutils literal notranslate"><span class="pre">nilearn.image.index_img</span></code></a> or <a class="reference internal" href="../modules/generated/nilearn.image.iter_img.html#nilearn.image.iter_img" title="nilearn.image.iter_img"><code class="xref py py-func docutils literal notranslate"><span class="pre">nilearn.image.iter_img</span></code></a>
to break down 4D images into 3D images, and on the other hand
<a class="reference internal" href="../modules/generated/nilearn.image.concat_imgs.html#nilearn.image.concat_imgs" title="nilearn.image.concat_imgs"><code class="xref py py-func docutils literal notranslate"><span class="pre">nilearn.image.concat_imgs</span></code></a> to group a list of 3D images into a 4D
image.</p>
<p class="sphx-glr-timing"><strong>Total running time of the script:</strong> ( 0 minutes 15.346 seconds)</p>
<p><strong>Estimated memory usage:</strong> 127 MB</p>
<div class="sphx-glr-footer class sphx-glr-footer-example docutils container" id="sphx-glr-download-auto-examples-plot-3d-and-4d-niimg-py">
<div class="binder-badge docutils container">
<a class="reference external image-reference" href="https://mybinder.org/v2/gh/nilearn/nilearn.github.io/main?filepath=examples/auto_examples/plot_3d_and_4d_niimg.ipynb"><img alt="Launch binder" src="../_images/binder_badge_logo7.svg" width="150px" /></a>
</div>
<div class="sphx-glr-download sphx-glr-download-python docutils container">
<p><a class="reference download internal" download="" href="../_downloads/65521385405be8b1422476b8a60d9262/plot_3d_and_4d_niimg.py"><code class="xref download docutils literal notranslate"><span class="pre">Download</span> <span class="pre">Python</span> <span class="pre">source</span> <span class="pre">code:</span> <span class="pre">plot_3d_and_4d_niimg.py</span></code></a></p>
</div>
<div class="sphx-glr-download sphx-glr-download-jupyter docutils container">
<p><a class="reference download internal" download="" href="../_downloads/7d25b4b12763d2e907508e8f3401bddf/plot_3d_and_4d_niimg.ipynb"><code class="xref download docutils literal notranslate"><span class="pre">Download</span> <span class="pre">Jupyter</span> <span class="pre">notebook:</span> <span class="pre">plot_3d_and_4d_niimg.ipynb</span></code></a></p>
</div>
</div>
<p class="sphx-glr-signature"><a class="reference external" href="https://sphinx-gallery.github.io">Gallery generated by Sphinx-Gallery</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="#">9.1.3. 3D and 4D niimgs: handling and visualizing</a><ul>
<li><a class="reference internal" href="#downloading-tutorial-datasets-from-internet">9.1.3.1. Downloading tutorial datasets from Internet</a></li>
<li><a class="reference internal" href="#visualizing-a-3d-file">9.1.3.2. Visualizing a 3D file</a></li>
<li><a class="reference internal" href="#visualizing-one-volume-in-a-4d-file">9.1.3.3. Visualizing one volume in a 4D file</a></li>
<li><a class="reference internal" href="#looping-on-all-volumes-in-a-4d-file">9.1.3.4. Looping on all volumes in a 4D file</a></li>
<li><a class="reference internal" href="#looping-through-selected-volumes-in-a-4d-file">9.1.3.5. Looping through selected volumes in a 4D file</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="plot_nilearn_101.html"
title="previous chapter"><span class="section-number">9.1.2. </span>Basic nilearn example: manipulating and looking at data</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="plot_decoding_tutorial.html"
title="next chapter"><span class="section-number">9.1.4. </span>A introduction tutorial to fMRI decoding</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/auto_examples/plot_3d_and_4d_niimg.rst.txt"
rel="nofollow">Show this page source</a>
</span>
</div>
</body>
</html>