Esempio n. 1
0
def mesh_contour_with_files(input_mesh, input_labels, output_mesh=None,
                            output_labels=None):
    from gifti import loadImage, saveImage, GiftiDataArray, GiftiImage
    from gifti import GiftiImage_fromarray, GiftiImage_fromTriangles
    from gifti import GiftiIntentCode, GiftiEncoding

    labels = loadImage(input_labels).arrays[0].data.astype(int)
    cor, triangles = loadImage(input_mesh).arrays

    contour_cor, contour_tri = mesh_contour(cor.data, triangles.data.astype(int),
                                            labels)
    k = GiftiImage_fromarray(contour_cor)
    k.arrays[0].intentString = "NIFTI_INTENT_POINTSET"
    k.addDataArray_fromarray(contour_tri, GiftiIntentCode.NIFTI_INTENT_TRIANGLE)
    for a in k.arrays:
        a.encoding = GiftiEncoding.GIFTI_ENCODING_ASCII

    if output_mesh is None:
        output_mesh = non_existent_file(add_suffix(input_mesh, '_contour'))
    pyhrf.verbose(1, 'saving to %s' %output_mesh)
    k.save(output_mesh)
    # remove redundant edges
    G.cut_redundancies()

    # make it a metric graph
    G.set_euclidian(vertices)

    return G

#-------------------------------------------------
#--- Left hemisphere processing ------------------
#-------------------------------------------------
print "Processing left hemisphere.",
sys.stdout.flush()
# read the mesh
lmesh = loadImage(lmesh_path_gii)
if SUBJECT == "group":
    c, t = lmesh.getArrays()
else:
    c, n, t = lmesh.getArrays()
lvertices = c.getData()
ltriangles = t.getData()
print ".",
sys.stdout.flush()
# read the contrast texture
ltex = tio.Texture(glm_ltex_path).read(glm_ltex_path)
Fun = np.array(ltex.data)
Fun[np.isnan(Fun)] = 0
G = mesh_to_graph(lvertices, ltriangles)
F = ff.Field(G.V, G.get_edges(), G.get_weights(), Fun)
print ".",
Esempio n. 3
0
import numpy as np
from gifti import loadImage, saveImage, GiftiImage_fromTriangles

# list of subjects
subj = ['s12069', 's12300', 's12401', 's12431', 's12508', 's12532', 's12539', 's12562','s12590', 's12635', 's12636', 's12898', 's12081', 's12165', 's12207', 's12344', 's12352', 's12370', 's12381', 's12405', 's12414', 's12432']
nsubj = len(subj)

lvertices = 0.
rvertices = 0.
for s in subj:
    lmesh_path = '/data/home/virgile/virgile_internship/%s/surf/lh.r.white.normalized.gii' %s
    rmesh_path = '/data/home/virgile/virgile_internship/%s/surf/rh.r.white.normalized.gii' %s
    lmesh = loadImage(lmesh_path)
    rmesh = loadImage(rmesh_path)

    lc, ln, lt = lmesh.getArrays()
    lvertices += lc.getData()

    rc, rn, rt = rmesh.getArrays()
    rvertices += rc.getData()

lvertices /= float(nsubj)
rvertices /= float(nsubj)

mean_lsurf = GiftiImage_fromTriangles(lvertices, lt.getData())
mean_rsurf = GiftiImage_fromTriangles(rvertices, rt.getData())

saveImage(mean_lsurf, '/data/home/virgile/virgile_internship/group_analysis/surf/lh.r.white.normalized.gii')
saveImage(mean_rsurf, '/data/home/virgile/virgile_internship/group_analysis/surf/rh.r.white.normalized.gii')
#--------------------------------------------------------
all_lalone = {}
all_ralone = {}
all_llinked = {}
all_rlinked = {}
for s in subj:
    all_lalone[s] = []
    all_ralone[s] = []
    all_llinked[s] = []
    all_rlinked[s] = []
removed = 1
print 'Comparison %s -- gamma=%g' %(coord_type, gamma)
for s1_id, s1 in enumerate(subj):
    ### Read subject s1 meshes
    # left hemisphere
    left_mesh = loadImage("%s/%s/surf/%s" %(ROOT_PATH, s1, LMESH_GII))
    c, n, t = left_mesh.getArrays()
    s1_ltriangles = t.getData()
    s1_lvertices = c.getData()
    # right hemisphere
    right_mesh = loadImage("%s/%s/surf/%s" %(ROOT_PATH, s1, RMESH_GII))
    c, n, t = right_mesh.getArrays()
    s1_rtriangles = t.getData()
    s1_rvertices = c.getData()
    ### Read subject s1 coordinate textures (level 1)
    s1_lcoord_tex = "%s/%s/experiments/smoothed_FWHM%g/%s/results_%s_level001/left_%s_FWHM2D%g_smin2D%i_FWHM3D%g_smin3D%i.tex" %(ROOT_PATH, s1, FWHM, CONTRAST, coord_type, CONTRAST, FWHM, SMIN, FWHM3D, SMIN3D)
    s1_lcoord = tio.Texture(s1_lcoord_tex).read(s1_lcoord_tex).data
    s1_rcoord_tex = "%s/%s/experiments/smoothed_FWHM%g/%s/results_%s_level001/right_%s_FWHM2D%g_smin2D%i_FWHM3D%g_smin3D%i.tex" %(ROOT_PATH, s1, FWHM, CONTRAST, coord_type, CONTRAST, FWHM, SMIN, FWHM3D, SMIN3D)
    s1_rcoord = tio.Texture(s1_rcoord_tex).read(s1_rcoord_tex).data

    s1_lpeaks = np.where(s1_lcoord != -1)[0]
 print "Smoothing: processing %s hemisphere:" %hemisphere
 sys.stdout.flush()
 if hemisphere == "right":
     mesh_path = rmesh_path_gii
     orig_tex_path = orig_rtex_path
     smoothed_tex_path = smoothed_rtex_path
 else:
     mesh_path = lmesh_path_gii
     orig_tex_path = orig_ltex_path
     smoothed_tex_path = smoothed_ltex_path
 
 ### Get information from input mesh
 # /!\ fixme : check that input_mesh is a triangular mesh
 print "  * Getting information from input mesh"
 sys.stdout.flush()
 input_mesh = loadImage(mesh_path)
 input_mesh_arrays = input_mesh.getArrays()
 if len(input_mesh_arrays) == 2:
     c, t = input_mesh_arrays
 elif len(input_mesh_arrays) == 3:
     c, n, t = input_mesh_arrays
 else:
     raise ValueError("Error during gifti data extraction")
 vertices = c.getData()
 nb_vertices = vertices.shape[0]
 polygons = t.getData()
 nb_polygons = polygons.shape[0]
 edges = get_edges_from_polygons(polygons, vertices)
 
 ### Get information from input texture
 # /!\ fixme : check that input_tex corresponds to the mesh