Skip to content

Instantly share code, notes, and snippets.

@satra
Created December 11, 2011 10:04

Revisions

  1. satra revised this gist Dec 13, 2011. 2 changed files with 32 additions and 0 deletions.
    1 change: 1 addition & 0 deletions fs_gifti_xfm.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    #DOES NOT WORK: GIFTI READ/WRITE is broken
    import nibabel as nb
    import nibabel.gifti as gifti

    31 changes: 31 additions & 0 deletions fs_to_native_vtk.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    import nibabel as nb
    import nibabel.gifti as gifti

    # load combined file
    # created with: mris_convert --combinesurfs lh.pial rh.pial ./pial.gii
    surf = gifti.read('pial.gii')

    #load orig affine transform
    ao = nb.load('orig.nii').get_affine()

    # load conformed affine
    ac = nb.load('conformed.nii').get_affine()

    # create freesurfer conformed volume to surface transform matrix
    import numpy as np
    M = np.array([[-1,0,0,128],
    [0, 0, 1, -128],
    [0, -1, 0, 128],
    [0,0,0,1]],dtype=np.float32)

    # create transform to go to native space
    xfm = np.dot(ac, np.linalg.inv(M))

    # transform the two data arrays storing left and right vertices
    xfmda0 = np.dot(xfm, np.hstack((surf.darrays[0].data, np.ones((surf.darrays[0].data.shape[0],1)))).T)[:3,:].T

    # write directly to vtk
    from tvtk.api import tvtk
    mesh = tvtk.PolyData(points=xfmda0, polys=surf.darrays[1].data)
    w = tvtk.XMLPolyDataWriter(input=mesh, file_name='polydata.vtp')
    w.write()
  2. satra revised this gist Dec 12, 2011. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions fs_gifti_xfm.py
    Original file line number Diff line number Diff line change
    @@ -25,11 +25,12 @@
    xfmda0 = np.dot(xfm, np.hstack((surf.darrays[0].data, np.ones((surf.darrays[0].data.shape[0],1)))).T)[:3,:].T
    xfmda1 = np.dot(xfm, np.hstack((surf.darrays[1].data, np.ones((surf.darrays[1].data.shape[0],1)))).T)[:3,:].T

    indtype = surf.darrays[0].data.dtype
    da = surf.darrays[0]
    indtype = da.data.dtype

    # set the data
    surf.darrays[0].data = xfmda0.astype(indtype)
    surf.darrays[1].data = xfmda1.astype(indtype)
    surf.darrays[0] = da.from_array(xfmda0.astype(indtype), da.intent, da.datatype)
    surf.darrays[1] = da.from_array(xfmda1.astype(indtype), da.intent, da.datatype)

    # write to file
    gifti.write(surf, 'xfmpial.gii')
    gifti.write(surf, 'xfmpial.gii')
  3. satra revised this gist Dec 12, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion fs_gifti_xfm.py
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@
    M = np.array([[-1,0,0,128],
    [0, 0, 1, -128],
    [0, -1, 0, 128],
    [0,0,0,1]],dtype=float)
    [0,0,0,1]],dtype=np.float32)

    # create transform to go to native space
    xfm = np.dot(ac, np.linalg.inv(M))
  4. satra revised this gist Dec 12, 2011. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions fs_gifti_xfm.py
    Original file line number Diff line number Diff line change
    @@ -25,9 +25,11 @@
    xfmda0 = np.dot(xfm, np.hstack((surf.darrays[0].data, np.ones((surf.darrays[0].data.shape[0],1)))).T)[:3,:].T
    xfmda1 = np.dot(xfm, np.hstack((surf.darrays[1].data, np.ones((surf.darrays[1].data.shape[0],1)))).T)[:3,:].T

    indtype = surf.darrays[0].data.dtype

    # set the data
    surf.darrays[0].data = xfmda0
    surf.darrays[1].data = xfmda1
    surf.darrays[0].data = xfmda0.astype(indtype)
    surf.darrays[1].data = xfmda1.astype(indtype)

    # write to file
    gifti.write(surf, 'xfmpial.gii')
  5. satra created this gist Dec 11, 2011.
    33 changes: 33 additions & 0 deletions fs_gifti_xfm.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    import nibabel as nb
    import nibabel.gifti as gifti

    # load combined file
    # created with: mris_convert --combinesurfs lh.pial rh.pial ./pial.gii
    surf = gifti.read('pial.gii')

    #load orig affine transform
    ao = nb.load('orig.nii').get_affine()

    # load conformed affine
    ac = nb.load('conformed.nii').get_affine()

    # create freesurfer conformed volume to surface transform matrix
    import numpy as np
    M = np.array([[-1,0,0,128],
    [0, 0, 1, -128],
    [0, -1, 0, 128],
    [0,0,0,1]],dtype=float)

    # create transform to go to native space
    xfm = np.dot(ac, np.linalg.inv(M))

    # transform the two data arrays storing left and right vertices
    xfmda0 = np.dot(xfm, np.hstack((surf.darrays[0].data, np.ones((surf.darrays[0].data.shape[0],1)))).T)[:3,:].T
    xfmda1 = np.dot(xfm, np.hstack((surf.darrays[1].data, np.ones((surf.darrays[1].data.shape[0],1)))).T)[:3,:].T

    # set the data
    surf.darrays[0].data = xfmda0
    surf.darrays[1].data = xfmda1

    # write to file
    gifti.write(surf, 'xfmpial.gii')