
本文旨在指导读者如何修改现有的MONAI代码,以加载和处理存储在特定文件夹结构中的自定义fMRI数据(NIfTI格式)。文章将详细解释如何修改文件路径、调整数据裁剪参数,以及如何将nilearn库集成到现有的MONAI工作流程中,以便更方便地加载NIfTI图像并提取数据,最终实现高效的fMRI数据预处理。
原始代码使用硬编码的文件路径和数据集名称。你需要根据你的数据存储结构修改以下几个关键变量:
load_root: 将此变量设置为你的fMRI数据所在的根目录。例如:
load_root = 'F:\New folder\cn_processed data'
filenames: 代码使用os.listdir(load_root)获取文件名列表。 确保load_root目录下直接包含各个被试的文件夹(例如Sub1、Sub2等)。
subj_name: 在循环中,subj_name = filename[:-7]用于从文件名中提取被试名称。根据你的文件名格式进行调整。 如果你的文件命名为S1.nii 且位于Sub1文件夹下,则需要在循环中构建完整的文件路径:
for filename in sorted(filenames):
subj_name = filename # 文件名为Sub1
# 构建完整路径
full_path = os.path.join(load_root, subj_name, "S1.nii")
# ...后续处理data = data[:, 14:-7, :, :] 这行代码用于裁剪fMRI数据。你需要根据你的数据的实际尺寸和感兴趣的脑区进行调整。
重要提示: 确保裁剪后的数据维度在后续处理中保持一致。
虽然原始代码使用了MONAI的LoadImage,但Nilearn 提供了更简洁的NIfTI图像加载方式。 可以考虑将Nilearn集成到你的代码中。
安装 Nilearn:
pip install nilearn
替换 MONAI 的 LoadImage:
from nilearn.image import load_img
import numpy as np
def read_data(filename,load_root,save_root,subj_name,count,queue=None,scaling_method=None, fill_zeroback=False):
print("processing: " + filename, flush=True)
# 构建完整路径
path = os.path.join(load_root, filename, "S1.nii") # 修改这里
try:
# 使用 nilearn 加载图像
nifti_image = load_img(path)
data = nifti_image.get_fdata() # 获取 numpy 数组
except Exception as e:
print(f"Error loading image: {path}, {e}")
return None
# ...后续处理from nilearn.image import load_img
import numpy as np
import torch
import os
def read_data(filename,load_root,save_root,subj_name,count,queue=None,scaling_method=None, fill_zeroback=False):
print("processing: " + filename, flush=True)
# 构建完整路径
path = os.path.join(load_root, filename, "S1.nii") # 修改这里
try:
# 使用 nilearn 加载图像
nifti_image = load_img(path)
data = nifti_image.get_fdata() # 获取 numpy 数组
except Exception as e:
print(f"Error loading image: {path}, {e}")
return None
#change this line according to your file names
save_dir = os.path.join(save_root,subj_name)
isExist = os.path.exists(save_dir)
if not isExist:
os.makedirs(save_dir)
# change this line according to your dataset
data = data[:, 14:-7, :, :] # 裁剪数据,根据你的数据调整
# width, height, depth, time
# Inspect the fMRI file first using your visualization tool.
# Limit the ranges of width, height, and depth to be under 96. Crop the background, not the brain regions.
# Each dimension of fMRI registered to MNI space (2mm) is expected to be around 100.
# You can do this when you load each volume at the Dataset class, including padding backgrounds to fill dimensions under 96.
background = data==0
if scaling_method == 'z-norm':
global_mean = data[~background].mean()
global_std = data[~background].std()
data_temp = (data - global_mean) / global_std
elif scaling_method == 'minmax':
data_temp = (data - data[~background].min()) / (data[~background].max() - data[~background].min())
data_global = torch.empty(data.shape)
data_global[background] = data_temp[~background].min() if not fill_zeroback else 0
# data_temp[~background].min() is expected to be 0 for scaling_method == 'minmax', and minimum z-value for scaling_method == 'z-norm'
data_global[~background] = data_temp[~background]
# save volumes one-by-one in fp16 format.
data_global = torch.tensor(data_global, dtype=torch.float16) # 确保是 torch.Tensor
data_global_split = torch.split(data_global, 1, 3)
for i, TR in enumerate(data_global_split):
torch.save(TR.clone(), os.path.join(save_dir,"frame_"+str(i)+".pt"))注意事项:
通过修改文件路径、调整裁剪参数以及集成 Nilearn 库,你可以轻松地将自定义 fMRI 数据加载到现有的 MONAI 代码中进行处理。 这种方法可以简化数据加载流程,并提高代码的可读性和可维护性。 记得在修改代码后进行充分测试,以确保数据处理的正确性。
以上就是如何使用MONAI加载和处理自定义fMRI数据的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号