Hello! I’ve been working to do data visualization with an fNIRS system. I’m trying to plot some data onto a 3D model of the head but I’m having issues with what I want to do. I have a series of MNI coordinates that I’m hoping to plot with multiple participants too so I’m not sure where to go with it. I’ve been using nilearn to do this so I’m going to include that code but I’d appreciate any help from anyone!! Thanks so much!!
- MNE version: e.g. 1.6.1
- operating system: Windows 10
import numpy as np
import pandas as pd
import nilearn
from nilearn import datasets
from nilearn import plotting
from nilearn.image import new_img_like
from nilearn.plotting import plot_stat_map, show
from nilearn.image import load_img, new_img_like
from nilearn.plotting import plot_stat_map, show
from nilearn.image import load_img, new_img_like
from nilearn.plotting import plot_stat_map, show
import nibabel as nib
template_img = load_img(template_path)
def extract_coordinates(df, coords_cols):
coords = []
for _, row in df.iterrows():
coords.append([row[coords_cols[0]], row[coords_cols[1]], row[coords_cols[2]]])
return np.array(coords)
coords_cols = ['x', 'y', 'z']
all_coords = np.vstack([
extract_coordinates(sources_all, coords_cols),
extract_coordinates(dets_all, coords_cols),
extract_coordinates(ors_all, coords_cols)
])
# Did this even pull anything??
# Checked-- yes it did
print(all_coords)
template_data = template_img.get_fdata()
new_img_data = np.zeros(template_data.shape, dtype=np.uint8) # Ensure correct dtype
# Convert MNI coordinates to voxel coordinates... not sure if I should do this
mni_to_vox = np.linalg.inv(template_img.affine[:3, :3])
voxel_coords = np.dot(all_coords - template_img.affine[:3, 3], mni_to_vox).astype(int)
for coord in voxel_coords:
if (0 <= coord[0] < new_img_data.shape[0] and
0 <= coord[1] < new_img_data.shape[1] and
0 <= coord[2] < new_img_data.shape[2]):
new_img_data[coord[0], coord[1], coord[2]] = 1
# Create a new NIfTI image
new_img = new_img_like(template_img, new_img_data)
plot_stat_map(new_img, title='MNI Coordinates', display_mode='ortho', cut_coords=(0, 0, 0), threshold = 1)
show()
img = nib.load('output_with_coordinates.nii')
img_data = img.get_fdata()