Hello,大家好!
Rose小哥今天给大家介绍一款用于神经成像工具Nilearn以及它的基本操作和数据保存查看。
Nilearn是一个Python模块,用于对神经成像数据进行快速、简单的统计学习。它利用scikit-learn python工具箱进行多变量统计,应用程序包括预测建模、分类、解码或连接性分析。
下面对它的基本操作进行简要介绍:
这里我们使用nilearn随附的Nifti文件
# 导入自带的Nifti文件
from nilearn.datasets import MNI152_FILE_PATH
# 注:变量mni152_file_path只是nifti文件的路径
print('Path to MNI152 template: %r' % MNI152_FILE_PATH)
# 导入nilearn提供的绘图工具
from nilearn import plotting
# 绘制图形
plotting.plot_img(MNI152_FILE_PATH)
让我们使用nilearn中的图像平滑功能:nilearn.image.smooth_img
包含"img"的函数可以使用文件名或图像作为输入。
在这里,我们以毫米为单位输入图像文件名和平滑值
# 从nilearn导入 image工具库
from nilearn import image
smooth_anat_img = image.smooth_img(MNI152_FILE_PATH, fwhm=3)
# 打印平滑
print(smooth_anat_img)
有多种平滑方式:
# 平滑方式一
plotting.plot_img(smooth_anat_img)
# 平滑方式二
more_smooth_anat_img = image.smooth_img(smooth_anat_img, fwhm=3)
plotting.plot_img(more_smooth_anat_img)
上面两步为方式一和方式二的平滑效果。
下面我们将平滑的结果保存到.nii.gz文件中,方便后续的使用。
# 保存结果到文件中
more_smooth_anat_img.to_filename('more_smooth_anat_img.nii.gz')
plotting.show()
概括说,所有nilearn工具都可以将数据作为文件名或内存中的对象,并将大脑体积作为内存中的对象返回。这些可以传递给其他nilearn工具,或保存到磁盘。