How to save p-value into sig.mgh format for FreeSurfer visualization

Hi, I want to do some visualization with FreeSurfer like this command:

freeview -f $SUBJECTS_DIR/fsaverage/surf/lh.inflated:annot=aparc.annot:annot_outline=1:overlay=lh.gender_age.glmdir/lh-Avg-thickness-age-Cor/sig.mgh:overlay_threshold=4,5 -viewport 3d

For example, I did the statistical analysis with my own data, but not with Freesurfer, to visualize my result, i want to use freeview, How can I save my p-value for vertx-wise or ROI-wise into a format .mgh and fit it into Freeview???

Or if any other softwares to visualize the result, I know PySurfer.

Any suggestion will be appreciated

Thanks in advance

If I remember correctly, the significance statistic is sign(beta) * -log(p), where beta is your result statistic and p is the associated p-value. Assuming you have images with these two values, nilearn will get you what you want:

import nilearn.image as nli
nli.math_img('np.sign(img) * -np.log10(p)',
             img=beta_fname, p=p_fname).to_filename('sig.nii')

I’m assuming your inputs are NIfTI, but you can do sig.mgh as easily.

Thanks very much for your reply, before I checked out a little for Nilearn, but I still got a little confused about this:
> where beta is your result statistic and p is the associated p-value. Assuming you have images with these two values,what is beta_fname and p_fname? these are two niffti images???

Actually, for example, I did some regional-wise or voxel-wise statistics, and I got the pvalue for each ROI/voxel, and I want to write this p-value into nifti file to visulize with good colormap.

If you have any good example, that would be great, thanks very much.

Hao

My above description will work for voxel-wise statistics, but if all you have is a p-value, you’ll have to omit the np.sign(img) part. You could instead do:

nli.math_img('-np.log10(img)', img=p_fname)

As to ROI-based analyses, if you have a voxel-wise map of ROI ID numbers, you could do something like

roi_img = nb.load(in_fname)
roi_data = roi_img.get_data()

new_data = np.zeros(roi_data.shape, dtype=np.float32)
for roi_id, pval in ...:
    new_data[roi_data == roi_id] = pval

pval_img = roi_img.__class__(new_data, roi_img.affine)
pval_img.to_filename(out_fname)

I hope this is what you’re looking for.