Skip to content

How to replace the voxel values based on a csv file and save it as a new nifti file? #1259

@mz1180

Description

@mz1180

Hello there,

I would like to calculate the mean of of a task map and the principal gradient map (i.e. from Daniel Marguilies) within each parcel of Schaefer 400 parcellations, and then zscored these parcels for each of the task activation and gradient maps. Next, we calculated the zscore difference between these two maps. We have got the difference scores for each parcel, the thing we would like to try is to project the difference value to the voxel within each corresponding parcel in the Schaefer 400 parcellations, and then visualise that map. Would that be possible to replace the parcel number (i.e, voxel value) with the calculated difference score and save that as a new nifti file?

Any help would be appreciated!

Best wishes,
Meichao.

The code we tried is shown as below:

import numpy as np
import nibabel as nib
import os
from scipy.stats import zscore
# Load the fMRI data and the parcellation atlas

path_input = '/groups/labs/semwandering_scratch/Meichao/P1336_NBack_task_Project/Gradient_Analysis/Sept_2023_State_space_Triangle'
file_task_activation = os.path.join(path_input,'1back_over_0back','1back_over_0back.nii.gz')
file_gradient_1 = os.path.join(path_input,'Gradient','Gradient1.nii.gz')

file_parcellation = os.path.join(path_input,'Schaefer2018_400Parcels_7Networks_order_FSLMNI152_2mm.nii.gz')

def parcellate_image(file_map_voxel,file_parcellation):
    """
    parcellate a voxel-based map based on the specific parcellaltion
    :param file_map_voxel: the voxel-based brain map
    :param file_parcellation: the parcellation file
    :return: the parcel-based value
    """
    fmri_img = nib.load(file_map_voxel)
    parcellation_img = nib.load(file_parcellation)

    fmri_data = fmri_img.get_fdata()
    parcellation_data = parcellation_img.get_fdata()

    # Get the unique parcels/labels in the parcellation data excluding the background (usually 0)
    unique_parcels = np.unique(parcellation_data)[1:]  # Excludes background
    
    print(unique_parcels)

    # Initialize an empty array to store the mean values of each parcel
    parcel_means = np.zeros_like(unique_parcels)

    # Parcellate the fMRI data
    for i, parcel in enumerate(unique_parcels):
        # Get the mask of the current parcel
        mask = parcellation_data == parcel

        # Extract the fMRI values of this parcel and compute their mean
        parcel_means[i] = fmri_data[mask].mean()

    return parcel_means

parcel_means_activation = parcellate_image(file_task_activation,file_parcellation)
print(parcel_means_activation)
parcel_means_gradient_1 = parcellate_image(file_gradient_1,file_parcellation)
print(parcel_means_gradient_1)

# Compute z-scores
parcel_means_activation_z = zscore(parcel_means_activation)
parcel_means_gradient_1_z = zscore(parcel_means_gradient_1)

# Compute the z-scores diff
diff_z = parcel_means_activation_z - parcel_means_gradient_1_z

print(diff_z)

parcellation_file = '/groups/labs/semwandering_scratch/Meichao/P1336_NBack_task_Project/Gradient_Analysis/Sept_2023_State_space_Triangle/Schaefer2018_400Parcels_7Networks_order_FSLMNI152_2mm.nii.gz'
parcellation_image = nib.load(parcellation_file)

diff_img = nib.Nifti1Image(diff_z, parcellation_image.affine, parcellation_image.header)

nib.save(diff_img, os.path.join(path_input,'1back_over_0back','1back_over_G1.nii.gz'))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions