# 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.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)

x_ticks = plt.xticks(range(len(labels) - 1), labels[1:], rotation=90)
y_ticks = plt.yticks(range(len(labels) - 1), labels[1:])
plt.gca().yaxis.tick_right()
plt.show()```

60 篇文章34 人订阅

0 条评论

## 相关文章

1.1K16

2904

4584

### O'ReillyAI系列：将学习速率可视化来优化神经网络

O’Reilly和Intel人工智能2018北京大会售票系统已经上线，现在是Best Price票价阶段。目前已经公布部分讲师及议题，详情请登录官网：https...

1908

4746

832

1120

3588

4768

4429