forked from nilearn/nilearn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_visualization.py
More file actions
82 lines (65 loc) · 2.37 KB
/
plot_visualization.py
File metadata and controls
82 lines (65 loc) · 2.37 KB
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
"""
Simple NeuroImaging data visualization and manipulation
========================================================
Simple example to show data manipulation and visualization.
"""
# Fetch data ################################################################
from nisl import datasets
haxby_files = datasets.fetch_haxby_simple()
# Get the file names relative to this dataset
bold = haxby_files.func
# Load the NIfTI data
import nibabel
nifti_img = nibabel.load(bold)
fmri_data = nifti_img.get_data()
fmri_affine = nifti_img.get_affine()
# Visualization #############################################################
import numpy as np
import pylab as pl
# Compute the mean EPI: we do the mean along the axis 3, which is time
mean_img = np.mean(fmri_data, axis=3)
# pl.figure() creates a new figure
pl.figure()
# First subplot: coronal view
# subplot: 1 line, 3 columns and use the first subplot
pl.subplot(1, 3, 1)
# Turn off the axes, we don't need it
pl.axis('off')
# We use pl.imshow to display an image, and use a 'gray' colormap
# we also use np.rot90 to rotate the image
pl.imshow(np.rot90(mean_img[:, 32, :]), interpolation='nearest',
cmap=pl.cm.gray)
pl.title('Coronal')
# Second subplot: sagittal view
pl.subplot(1, 3, 2)
pl.axis('off')
pl.title('Sagittal')
pl.imshow(np.rot90(mean_img[15, :, :]), interpolation='nearest',
cmap=pl.cm.gray)
# Third subplot: axial view
pl.subplot(1, 3, 3)
pl.axis('off')
pl.title('Axial')
pl.imshow(np.rot90(mean_img[:, :, 32]), interpolation='nearest',
cmap=pl.cm.gray)
# Extracting a brain mask ###################################################
# Simple computation of a mask from the fMRI data
from nisl.masking import compute_epi_mask
mask_img = compute_epi_mask(nifti_img)
mask_data = mask_img.get_data().astype(bool)
# We create a new figure
pl.figure()
# A plot the axial view of the mask to compare with the axial
# view of the raw data displayed previously
pl.imshow(np.rot90(mask_data[:, :, 32]), interpolation='nearest')
# Applying the mask #########################################################
from nisl.masking import apply_mask
masked_data = apply_mask(nifti_img, mask_img)
# masked_data shape is (instant number, voxel number). We can plot the first 10
# lines: they correspond to timeseries of 10 voxels on the side of the
# brain
pl.figure()
pl.plot(masked_data[:10].T)
pl.xlabel('Time')
pl.ylabel('Voxel')
pl.show()