# Nilearn学习笔记3-提取时间序列建立功能连接体

## 1. 基于大脑分区提取时间序列

（Time-series from a brain parcellation or “MaxProb” atlas） 1.1 一般而言，用“硬分区”定义用于提取信号的分区。人脑分区有很多个版本，可以结合自己的数据和自己的研究目的选取合理的分区图谱，用于自己的研究。 代码：（代码中有详细注释）

```# Load fmri image
# Note: functions in learn can accept parameters as: image object or fmri filepath

from nilearn import datasets
dataset = datasets.fetch_atlas_harvard_oxford('cort-maxprob-thr25-2mm')
atlas_filename = dataset.maps
labels = dataset.labels

# Apply atlas to my data
from nilearn.image import resample_to_img

# Gain the TimeSeries
memory='nilearn_cache', verbose=5)

# Extracting times series to build a functional connectome
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()```

## 2. 通过概率图谱建立时间序列

```'''
extracting TimeSeries from probabilistic atlas
'''
# Note: functions in learn can accept parameters as: image object or fmri filepath

# Retrieve the atlas and the data
from nilearn import datasets
atlas = datasets.fetch_atlas_msdl()
atlas_filename = atlas['maps']
labels = atlas['labels']

# Apply atlas to my data
from nilearn.image import resample_to_img

# Gain the TimeSeries
memory='nilearn_cache', verbose=5)

############################################################################
# Build and display a correlation matrix
from nilearn.connectome import ConnectivityMeasure
correlation_measure = ConnectivityMeasure(kind='correlation')
correlation_matrix = correlation_measure.fit_transform([time_series])[0]

# Display the correlation matrix
import numpy as np
from matplotlib import pyplot as plt
plt.figure(figsize=(10, 10))
# Mask out the major diagonal
np.fill_diagonal(correlation_matrix, 0)
plt.imshow(correlation_matrix, interpolation="nearest", cmap="RdBu_r",
vmax=0.8, vmin=-0.8)
plt.colorbar()
# And display the labels
x_ticks = plt.xticks(range(len(labels)), labels, rotation=90)
y_ticks = plt.yticks(range(len(labels)), labels)

############################################################################
# And now display the corresponding graph
from nilearn import plotting
coords = atlas.region_coords

# We threshold to keep only the 20% of edges with the highest value
# because the graph is very dense
plotting.plot_connectome(correlation_matrix, coords,
edge_threshold="80%", colorbar=True)

plotting.show()```

60 篇文章34 人订阅

0 条评论

## 相关文章

1313

### 实验5 OpenGL模型视图变换

2、移动或者旋转它，当然了，如果它只是计算机里面的物体，我们还可以放大或缩小它（物体运动，让人看它的不同部分）。（模型变换）

813

2065

### 怎样在Python的深度学习库Keras中使用度量

Keras库提供了一种在训练深度学习模型时计算并报告一套标准度量的方法。 除了提供分类和回归问题的标准度量外，Keras还允许在训练深度学习模型时，定义和报告你...

3998

4389

28812

44511

2425

3516