# Nilearn学习笔记2-从FMRI数据到时间序列

``````(1) nilearn.masking.compute_background_mask for brain images where the brain stands out of a constant background. This is typically the case when working on statistic maps output after a brain extraction
``````

(两个函数应用不同的数据，如果数据是EPI采样，就用第二个)

``````from nilearn import masking
# masked_data shape is (timepoints, voxels). We can plot the first 150
# timepoints from two voxels

# And now plot a few of these
import matplotlib.pyplot as plt
plt.figure(figsize=(7, 5))
plt.xlabel('Time [TRs]', fontsize=16)
plt.ylabel('Intensity', fontsize=16)
plt.xlim(0, 150)

plt.show()
``````

##2. Timeseries

``````#导入图谱，目前用的图谱有好几个，具体选择哪个没有定论。
from nilearn import datasets
dataset = datasets.fetch_atlas_harvard_oxford('cort-maxprob-thr25-2mm')
atlas_filename = dataset.maps
labels = dataset.labels

``````

``````例如：from nilearn.image import resample_to_img
``````

(这个是针对于已有参考图谱，最后一定要选最邻近的。如果没有参考图谱，有相应的函数)

``````from nilearn.input_data import NiftiLabelsMasker
memory='nilearn_cache', verbose=5)

(r'E:\home\bct_test\NC_01_0001\rs6_f8dGR_w3_rabrat_4D.nii')

from nilearn.connectome import ConnectivityMeasure
correlation_measure = ConnectivityMeasure(kind='correlation')
correlation_matrix = correlation_measure.fit_transform([time_series])[0]

# Plot the correlation matrix
import numpy as np
from matplotlib import pyplot as plt
plt.figure(figsize=(10, 10))
# Mask the main diagonal for visualization:
np.fill_diagonal(correlation_matrix, 0)

plt.imshow(correlation_matrix, interpolation="nearest", cmap="RdBu_r",
vmax=0.8, vmin=-0.8)