其他
Nilearn:绘制大脑图像
Nilearn简介
Nilearn安装
在Windows系统中
①下载并安装64位Anaconda (https://www.anaconda.com/products/distribution)。建议安装一个完整的64位科学Python发行版,比如Anaconda。因为它满足Nilearn的所有要求,也能节省时间和避免不必要的麻烦。Nilearn需要安装Python和以下依赖项:ipython、scipy、scikit-learn、joblib、matplotlib、nibabel和pandas。
②打开命令提示符。(按“Win-R”,输入“cmd”,然后按“Enter”。这将打开程序cmd.exe,这是命令提示符)。然后输入pip install -U --user nilearn,并按“Enter”键。
③打开IPython。(可以在命令提示符中输入“ipython”并按“Enter”打开)。然后输入In [1]: import nilearn,并按“Enter”。
注:如果没有提示出现错误,说明已经正确安装。
plotting.plot_anat(haxby_anat_filename, title="plot_anat")
# 导入图像处理工具
from nilearn import image
# 计算函数图像的voxel_wise平均值
# 将函数图像从4D降维至3D
mean_haxby_img = image.mean_img(haxby_func_filename)
# 可视化均值图像(3D)
plotting.plot_epi(mean_haxby_img, title="plot_epi")
from nilearn import plotting
from nilearn.plotting import plot_glass_brain
# 整个大脑的矢状切面和地图的阈值是3
plot_glass_brain(stat_img, threshold=3)
from nilearn import plotting
# 在EPI模板上手动可视化t-map图像
# 使用给定的cut_coords定位坐标
plotting.plot_stat_map(stat_img,
threshold=3, title="plot_stat_map",
cut_coords=[36, -27, 66])
plotting.plot_roi(haxby_mask_filename, bg_img=haxby_anat_filename,
title="plot_roi")
from nilearn import plotting
plotting.plot_connectome(partial_correlation_matrix, dmn_coords,
title="Default Mode Network Connectivity")
plotting.plot_connectome(partial_correlation_matrix, dmn_coords,
title="Connectivity projected on hemispheres",
display_mode='lyrz')
plotting.show()
# 计算每个节点的归一化绝对强度
node_strength = np.sum(np.abs(matrix), axis=0)
node_strength /= np.max(node_strength)
plotting.plot_markers(
node_strength,
coords,
title='Node strength for absolute value of edges for Power atlas',
)
import matplotlib.pyplot as plt
from nilearn.plotting import plot_carpet
display = plot_carpet(adhd_dataset.func[0], mask_img, t_r=t_r)
display.show()
plotting.plot_stat_map(stat_img, display_mode='ortho',
cut_coords=[36, -27, 60],
title="display_mode='ortho', cut_coords=[36, -27, 60]")
plotting.plot_stat_map(stat_img, display_mode='z', cut_coords=5,
title="display_mode='z', cut_coords=5")
plotting.plot_stat_map(stat_img, display_mode='x',
cut_coords=[-36, 36],
title="display_mode='x', cut_coords=[-36, 36]")
plotting.plot_stat_map(stat_img, display_mode='y', cut_coords=1,
title="display_mode='y', cut_coords=1")
plotting.plot_stat_map(stat_img, display_mode='z',
cut_coords=1, colorbar=False,
title="display_mode='z', cut_coords=1, colorbar=False")
plotting.plot_stat_map(stat_img, display_mode='xz',
cut_coords=[36, 60],
title="display_mode='xz', cut_coords=[36, 60]")
plotting.plot_stat_map(stat_img, display_mode='yx',
cut_coords=[-27, 36],
title="display_mode='yx', cut_coords=[-27, 36]")
plotting.plot_stat_map(stat_img, display_mode='yz',
cut_coords=[-27, 60],
title="display_mode='yz', cut_coords=[-27, 60]")
plotting.plot_stat_map(stat_img, display_mode='tiled',
cut_coords=[36, -27, 60],
title="display_mode='tiled'")
plotting.plot_stat_map(stat_img, display_mode='mosaic',
title="display_mode='mosaic' default cut_coords")
plot_glass_brain(
stat_img, title='plot_glass_brain with display_mode="lzr"',
black_bg=True, display_mode='lzr', threshold=3
)
plot_glass_brain(
stat_img, threshold=0, colorbar=True,
title='plot_glass_brain with display_mode="lyrz"',
plot_abs=False, display_mode='lyrz'
)
# 导入图像处理工具进行脑功能图像的基本处理
from nilearn import image
mean_haxby_img = image.mean_img(haxby_func_filename)
display = plotting.plot_anat(mean_haxby_img, title="add_edges")
display.add_edges(haxby_anat_filename, color='r')
display = plotting.plot_anat(mean_haxby_img, title="add_contours",
cut_coords=[-34, -39, -9])
display.add_contours(haxby_mask_filename, levels=[0.5], colors='r')
display = plotting.plot_anat(mean_haxby_img,
title="add_contours with filled=True",
cut_coords=[-34, -39, -9])
display.add_contours(haxby_mask_filename, filled=True, alpha=0.7,
levels=[0.5], colors='b')
display = plotting.plot_anat(mean_haxby_img, title="add_markers",
cut_coords=[-34, -39, -9])
coords = [(-34, -39, -9)]
display.add_markers(coords, marker_color='y', marker_size=100)
display = plotting.plot_anat(mean_haxby_img,
title="adding a scale bar",
cut_coords=[-34, -39, -9])
display.annotate(scalebar=True)
# 进一步的设置可以通过scale_*关键字参数来实现。例如,将单位更改为mm,或者使用不同的比列尺大小
display = plotting.plot_anat(mean_haxby_img,
title="adding a scale bar",
cut_coords=[-34, -39, -9])
display.annotate(scalebar=True, scale_size=25, scale_units='mm')
显示或保存到一个图像文件
from nilearn import plotting
plotting.show()
from nilearn import plotting
plotting.plot_stat_map(img, output_file='pretty_brain.png')
使用savefig方法将绘制好的图像保存为图像文件:
from nilearn import plotting
display = plotting.plot_stat_map(img)
display.savefig('pretty_brain.png')
display.close()
from nilearn import datasets
destrieux_atlas = datasets.fetch_atlas_surf_destrieux()
# 加载左脑图谱数据
parcellation = destrieux_atlas['map_left']
fsaverage = datasets.fetch_surf_fsaverage()
# fsaaverage数据集包含指向文件位置的文件名
print('Fsaverage5 pial surface of left hemisphere is at: %s' %
fsaverage['pial_left'])
print('Fsaverage5 inflated surface of left hemisphere is at: %s' %
fsaverage['infl_left'])
print('Fsaverage5 sulcal depth map of left hemisphere is at: %s' %
fsaverage['sulc_left'])
# 可视化
from nilearn import plotting
plotting.plot_surf_roi(fsaverage['pial_left'], roi_map=parcellation,
hemi='left', view='lateral',
bg_map=fsaverage['sulc_left'], bg_on_data=True,
darkness=.5)
# 在fsaverage5表面显示Destrieux分割
plotting.plot_surf_roi(fsaverage['infl_left'], roi_map=parcellation,
hemi='left', view='lateral',
bg_map=fsaverage['sulc_left'], bg_on_data=True,
darkness=.5)
# 以不同视角显示Destrieux分割:后部
plotting.plot_surf_roi(fsaverage['infl_left'], roi_map=parcellation,
hemi='left', view='posterior',
bg_map=fsaverage['sulc_left'], bg_on_data=True,
darkness=.5)
# 腹侧
plotting.plot_surf_roi(fsaverage['infl_left'], roi_map=parcellation,
hemi='left', view='ventral',
bg_map=fsaverage['sulc_left'], bg_on_data=True,
darkness=.5)
plotting.show()
# 读取数据
# 来自nilearn的NKI静息态数据
from nilearn import datasets
nki_dataset = datasets.fetch_surf_nki_enhanced(n_subjects=1)
print(('Resting state data of the first subjects on the '
'fsaverag5 surface left hemisphere is at: %s' %
nki_dataset['func_left'][0]))
# 左脑fsaverage5区Destrieux分割
destrieux_atlas = datasets.fetch_atlas_surf_destrieux()
parcellation = destrieux_atlas['map_left']
labels = destrieux_atlas['labels']
# Fsaverage5表面模板
fsaverage = datasets.fetch_surf_fsaverage()
# fsaaverage数据集包含指向文件位置的文件名
print('Fsaverage5 pial surface of left hemisphere is at: %s' %
fsaverage['pial_left'])
print('Fsaverage5 inflated surface of left hemisphere is at: %s' %
fsaverage['infl_left'])
print('Fsaverage5 sulcal depth map of left hemisphere is at: %s' %
fsaverage['sulc_left'])
# 提取种子的时间序列
# 从nilearn加载静息态时间序列
from nilearn import surface
timeseries = surface.load_surf_data(nki_dataset['func_left'][0])
# 通过标签提取种子区域
pcc_region = b'G_cingul-Post-dorsal'
import numpy as np
pcc_labels = np.where(parcellation == labels.index(pcc_region))[0]
# 从种子区域提取时间序列
seed_timeseries = np.mean(timeseries[pcc_labels], axis=0)
# 计算基于种子的功能连接
# 计算种子时间序列与半球所有皮层节点时间序列之间的皮尔逊积矩相关系数
from scipy import stats
stat_map = np.zeros(timeseries.shape[0])
for i in range(timeseries.shape[0]):
stat_map[i] = stats.pearsonr(seed_timeseries, timeseries[i])[0]
stat_map[np.where(np.mean(timeseries, axis=1) == 0)] = 0
# 在表面显示ROI
# 在ROI图中转换ROI指标
pcc_map = np.zeros(parcellation.shape[0], dtype=int)
pcc_map[pcc_labels] = 1
from nilearn import plotting
plotting.plot_surf_roi(fsaverage['pial_left'], roi_map=pcc_map,
hemi='left', view='medial',
bg_map=fsaverage['sulc_left'], bg_on_data=True,
title='PCC Seed')
为了不错过精彩内容,小伙伴们可以点个“在看”,加
更多精彩课程推荐
非常欢迎大家留言、转载、收藏或分享~ 本文来自微信公众号“茗创科技”。如需转载,请在“茗创科技”后台回复“转载”,并附上所需转载的文章标题以及您的ID。
MC_Brain
茗创科技工作室
觉得有帮助,欢迎转发收藏或者点个在看哦~
听说点在看的人SCI接收率都提升了18%呢!