コード例 #1
0
ファイル: io.py プロジェクト: thomas-vincent/pyhrf
def write_texture(tex_data, filename, intent=None, meta_data=None):
    """ Write the n-dimensional numpy array 'text_data' into filename
    using gifti (.gii) or tex format.
    """
    if tex_data.dtype == np.bool:
        tex_data = tex_data.astype(np.int16)

    if has_ext_gzsafe(filename, 'gii'):
        #tex = np.arange(len(cor), dtype=int)
        if intent is None:
            if np.issubdtype(tex_data.dtype, np.int):
                intent = 'NIFTI_INTENT_LABEL'
                dtype = None
            elif np.issubdtype(tex_data.dtype, np.float):
                intent = 'NIFTI_INTENT_NONE'
                #intent = 'dimensionless' #?fg
                #dtype = 'NIFTI_TYPE_FLOAT32'
                tex_data.astype(np.float32)
                if pyhrf.cfg['global']['write_texture_minf']:
                    s = "attributes = {'data_type' : 'FLOAT'}"
                    f = open(filename + '.minf', 'w')
                    f.write(s)
                    f.close()
            else:
                raise NotImplementedError("Unsupported dtype %s" \
                                              %str(tex_data.dtype))

        gii_array = gifti.GiftiDataArray.from_array(tex_data, intent)
        if meta_data is not None:
            md = {'pyhrf_cuboid_data':meta_data}
            gmeta_data = gifti.GiftiMetaData.from_dict(md)
        else:
            gmeta_data = None
        tex_gii = gifti.GiftiImage(darrays=[gii_array,], meta=gmeta_data)
        pyhrf.verbose(3, 'Write texture to %s'%filename)
        gifti.write(tex_gii, filename)

    elif has_ext_gzsafe(filename, 'tex'):
        if meta_data is not None:
            print 'Warning: meta ignored when saving to tex format'

        from pyhrf.tools.io.tio import Texture
        tex = Texture(filename, data=tex_data)
        tex.write()
    else:
        raise NotImplementedError('Unsupported extension for file %s' \
                                      %filename)
コード例 #2
0
ファイル: io.py プロジェクト: thomas-vincent/pyhrf
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])
コード例 #3
0
ファイル: test_beta_estim.py プロジェクト: Solvi/pyhrf
    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