def read_texture(tex): if has_ext_gzsafe(tex, 'gii'): #from gifti import loadImage #texture = loadImage(tex).arrays[0].data tex_gii = gifti.read(tex) if len(tex_gii.darrays) > 1: # 2D texture ... #TODO: check texture = np.vstack([a.data for a in tex_gii.darrays]) else: texture = tex_gii.darrays[0].data return texture, tex_gii elif has_ext_gzsafe(tex, 'tex'): from pyhrf.tools._io.tio import Texture texture = Texture.read(tex).data return texture, None else: raise NotImplementedError('Unsupported %s extension' % op.splitext(tex)[1])
def test_single_surface_PFPS_ML(self): """PF estimation method : path sampling. ML on p(label|beta). topology from a surfacic RDI """ # generate a field: beta = 0.4 nbClasses = 2 print 'generating potts ..., beta =', beta # grab surfacic data: from pyhrf.graph import graph_from_mesh, sub_graph, graph_is_sane from pyhrf.tools._io.tio import Texture from soma import aims print 'import done' roiId = 20 mfn = pyhrf.get_data_file_name('right_hemisphere.mesh') print 'mesh file:', mfn mesh = aims.read(mfn) print 'mesh read' triangles = [t.arraydata() for t in mesh.polygon().list()] print 'building graph ... ' wholeGraph = graph_from_mesh(triangles) roiMaskFile = pyhrf.get_data_file_name('roimask_gyrii_tuned.tex') roiMask = Texture.read(roiMaskFile).data.astype(int) mroi = np.where(roiMask==roiId) g, nm = sub_graph(wholeGraph, mroi[0]) print "g:", len(g), len(g[0]) nnodes = len(g) points = np.vstack([v.arraydata() for v in mesh.vertex().list()]) weights = [[1./dist(points[j],points[k]) for k in g[j]] for j in xrange(nnodes)] print "weights:", len(weights), len(weights[0]) if 1: for j in xrange(nnodes): s = sum(weights[j]) * 1. for k in xrange(len(weights[j])): weights[j][k] = weights[j][k]/s * len(weights[j]) labels = genPotts(g, beta, nbClasses, weights=weights) print labels # partition function estimation gridLnz = Cpt_Vec_Estim_lnZ_Graph(g, nbClasses, GraphWeight=weights) print 'gridLnz with weights:' print gridLnz # beta estimation be, pb = beta_estim_obs_field(g, labels, gridLnz, 'ML', weights) print 'betaML:', be weights = None gridLnz = Cpt_Vec_Estim_lnZ_Graph(g, nbClasses, GraphWeight=weights) print 'gridLnz without weights:' print gridLnz # beta estimation be, pb = beta_estim_obs_field(g, labels, gridLnz, 'ML', weights) print 'betaML:', be gridPace = gridLnz[1][1] - gridLnz[1][0] assert abs(be-beta) <= gridPace