Beispiel #1
0
def show_nifti(name, nifti_dir=None, save_fig=0, fig_dir=None):
    """
    name is the name of the nifti file, without the nii.gz extension
    nifti_dir is the path to the directory where the nifti is located. if not provided, looks in current directory.
    save_fig is 1 for save nifti fig as png, 0 for don't save
    fig_dir is the path to the directory for saving the png. if not provided, saves to nifti_dir
    """
    if nifti_dir==None:
	    nifti_dir = os.getcwd()
    im = nib.load(os.path.join(nifti_dir, '{}.nii.gz'.format(name)))
    data = im.get_data()
    min_val = data[~np.isnan(data)].min()
    max_val = data[~np.isnan(data)].max()
    fig = visualize.display_slices(data, min_val=min_val, max_val=max_val, cmap=plt.cm.RdBu_r)
    fig.suptitle(name)
    # plt.show()
    if save_fig:
        if fig_dir==None:
            fig_dir = nifti_dir
        fig.savefig(os.path.join(fig_dir,'{}.png'.format(name)))
Beispiel #2
0
def show_nifti(name, nifti_dir=None, save_fig=0, fig_dir=None):
    """
    name is the name of the nifti file, without the nii.gz extension
    nifti_dir is the path to the directory where the nifti is located. if not provided, looks in current directory.
    save_fig is 1 for save nifti fig as png, 0 for don't save
    fig_dir is the path to the directory for saving the png. if not provided, saves to nifti_dir
    """
    if nifti_dir == None:
        nifti_dir = os.getcwd()
    im = nib.load(os.path.join(nifti_dir, '{}.nii.gz'.format(name)))
    data = im.get_data()
    min_val = data[~np.isnan(data)].min()
    max_val = data[~np.isnan(data)].max()
    fig = visualize.display_slices(data,
                                   min_val=min_val,
                                   max_val=max_val,
                                   cmap=plt.cm.RdBu_r)
    fig.suptitle(name)
    # plt.show()
    if save_fig:
        if fig_dir == None:
            fig_dir = nifti_dir
        fig.savefig(os.path.join(fig_dir, '{}.png'.format(name)))
    coh_im[coords_indices] = coh
    cor_im[coords_indices] = cor

    # save the images as niftis
    if save_nii:
        print 'Saving niftis'
        coh_nii = nib.Nifti1Image(coh_im, fmri_data.get_affine())
        cor_nii = nib.Nifti1Image(cor_im, fmri_data.get_affine())

        coh_nii.to_filename(coh_nii_file)
        cor_nii.to_filename(cor_nii_file)

    # display the coh and coh maps
    fig_coh = visualize.display_slices(coh_im,
                                       min_val=0,
                                       max_val=1,
                                       cmap=plt.cm.RdBu_r)
    fig_coh.suptitle('coherence, hemi {0} {1} seed'.format(hemi, roi_name))

    fig_cor = visualize.display_slices(cor_im,
                                       min_val=-1,
                                       max_val=1,
                                       cmap=plt.cm.RdBu_r)
    fig_cor.suptitle('correlation hemi {0} {1} seed'.format(hemi, roi_name))

    # save the figures
    if save_fig:
        print 'Saving figs'
        fig_coh.savefig(coh_fig_file)
        fig_cor.savefig(cor_fig_file)
    coh_im = np.zeros(volume_shape)
    cor_im = np.zeros(volume_shape)

    coh_im[coords_indices] = coh
    cor_im[coords_indices] = cor

    # save the images as niftis
    if save_nii:
        print 'Saving niftis'
        coh_nii = nib.Nifti1Image(coh_im, fmri_data.get_affine())
        cor_nii = nib.Nifti1Image(cor_im, fmri_data.get_affine())

        coh_nii.to_filename(coh_nii_file)
        cor_nii.to_filename(cor_nii_file)

    # display the coh and coh maps
    fig_coh = visualize.display_slices(coh_im, 0, 1)
    fig_coh.suptitle('coherence, {0} seed'.format(roi_name))
    plt.show()

    fig_cor = visualize.display_slices(cor_im, 0, 1)
    fig_cor.suptitle('correlation {0} seed'.format(roi_name))
    plt.show()

    # save the figures
    if save_fig:
        print 'Saving figs'
        fig_coh.savefig(coh_fig_file)
        fig_cor.savefig(cor_fig_file)
    cor_im = np.zeros(volume_shape)
    
    coh_im[coords_indices] = coh
    cor_im[coords_indices] = cor
    
    # save the images as niftis
    if save_nii:
        print 'Saving niftis'
        coh_nii = nib.Nifti1Image(coh_im, fmri_data.get_affine())
        cor_nii = nib.Nifti1Image(cor_im, fmri_data.get_affine())
        
        coh_nii.to_filename(coh_nii_file)
        cor_nii.to_filename(cor_nii_file)
    
    # display the coh and coh maps
    fig_coh = visualize.display_slices(coh_im, 0, 1)
    fig_coh.suptitle('coherence, {0} seed'.format(roi_name))
    plt.show()
    
    fig_cor = visualize.display_slices(cor_im, 0, 1)
    fig_cor.suptitle('correlation {0} seed'.format(roi_name))
    plt.show()
    
    # save the figures
    if save_fig:
        print 'Saving figs'
        fig_coh.savefig(coh_fig_file)
        fig_cor.savefig(cor_fig_file)
    
    
    cor_im = np.zeros(volume_shape)
    
    coh_im[coords_indices] = coh
    cor_im[coords_indices] = cor
    
    # save the images as niftis
    if save_nii:
        print 'Saving niftis'
        coh_nii = nib.Nifti1Image(coh_im, fmri_data.get_affine())
        cor_nii = nib.Nifti1Image(cor_im, fmri_data.get_affine())
        
        coh_nii.to_filename(coh_nii_file)
        cor_nii.to_filename(cor_nii_file)
    
    # display the coh and coh maps
    fig_coh = visualize.display_slices(coh_im, min_val=0, max_val=1, cmap=plt.cm.RdBu_r)
    fig_coh.suptitle('coherence, hemi {0} {1} seed'.format(hemi, roi_name))
    
    fig_cor = visualize.display_slices(cor_im, min_val=-1, max_val=1, cmap=plt.cm.RdBu_r)
    fig_cor.suptitle('correlation hemi {0} {1} seed'.format(hemi, roi_name))
    
    # save the figures
    if save_fig:
        print 'Saving figs'
        fig_coh.savefig(coh_fig_file)
        fig_cor.savefig(cor_fig_file)

    plt.show()