def create_3Dlabels_Potts(condition_defs, beta, dims, mask): labels = [] graph = graph_from_lattice(mask, kerMask=kerMask3D_6n, toroidal=True) for c in condition_defs: tmp = genPotts(graph, beta, 2) tmp = np.reshape(tmp, dims) labels.append(tmp) return np.array(labels)
def create_labels_Potts(condition_defs, beta, nb_voxels): labels = [] nb_voxels = nb_voxels**.5 # assume square shape shape = (nb_voxels, nb_voxels) mask = np.ones(shape, dtype=int) graph = graph_from_lattice(mask, kerMask=kerMask2D_4n, toroidal=True) shape = (1, nb_voxels, nb_voxels) for c in condition_defs: tmp = genPotts(graph, beta, 2) tmp = np.reshape(tmp, shape) labels.append(tmp) return np.array(labels)
def create_labels_Potts(condition_defs, beta, nb_voxels): labels = [] nb_voxels = nb_voxels ** 0.5 # assume square shape shape = (nb_voxels, nb_voxels) mask = np.ones(shape, dtype=int) graph = graph_from_lattice(mask, kerMask=kerMask2D_4n, toroidal=True) shape = (1, nb_voxels, nb_voxels) for c in condition_defs: tmp = genPotts(graph, beta, 2) tmp = np.reshape(tmp, shape) labels.append(tmp) return np.array(labels)
def test_single_PFPS_ML(self): """PF estimation method : path sampling. ML on p(label|beta). """ # generate a field: beta = 0.4 nbClasses = 2 print 'generating potts ..., beta =', beta labels = genPotts(self.g, beta, nbClasses) # partition function estimation gridLnz = Cpt_Vec_Estim_lnZ_Graph(self.g, nbClasses) gridPace = gridLnz[1][1] - gridLnz[1][0] # beta estimation be, pb = beta_estim_obs_field(self.g, labels, gridLnz, 'ML') print 'betaML:', be assert abs(be-beta) <= gridPace
def test_beta_estim_jde_mcmc(self): nbCond = 2 betaMax = 1.2 dBeta = .1 betas = np.arange(0, betaMax, dBeta) height = 20 width = 20 graph = graph_from_lattice(np.ones((height,width),dtype=int), kerMask2D_4n) gridLnZ = cached_eval(Cpt_Vec_Estim_lnZ_Graph, (graph, 2)) nbIt = 3 bolds = [] outputs = [] for beta in betas: labels = [genPotts(graph, beta).reshape((height,width)) for i in range(nbCond)] b = simulate_bold_from_labels(labels) bolds.append(b) p = {'data':b.to_fmri_data(np.ones((height,width),dtype=int)), 'nbIterations':nbIt, 'betaSimu':beta, #'noiseVarSimu':noiseVar, 'gridLnZ':gridLnZ} #outs = cached_eval(runEstimationBetaEstim, (p,)) #outputs.append(outs) #view_results(bolds, outputs, betas) scm = "0;0;0.333333;1;0.651282;0;1;1#" \ "0;0;0.333333;1;0.661538;0;1;0#" \ "0;0;0.333333;1;0.664103;1;1;0" cm = cmstring_to_mpl_cmap(scm) import plt for output, beta in zip(outputs,betas): lmap = output['pm_Labels_marked'].data[0,:,:,0] plt.figure() img = plt.matshow(lmap, cmap=cm) img.get_axes().set_axis_off() sbeta = ('%1.2f'%beta).replace('.','_') plt.savefig('./hidden_field_pm_labels_marked_'+sbeta)
def test_obs_field_ML(self): """ Test estimation of beta with an observed field: a small 2D case. Partition function estimation method : extrapolation scheme. Use the ML on p(label|beta). PF estimation: Onsager """ shape = (6, 6) mask = _np.ones(shape, dtype=int) # full mask g = graph_from_lattice(mask, kerMask=kerMask2D_4n, toroidal=True) # generate a field: beta = 0.4 nbClasses = 2 labels = genPotts(g, beta, nbClasses) # partition function estimation dbeta = 0.05 betaGrid = _np.arange(0, 1.5, dbeta) lpf = logpf_ising_onsager(labels.size, betaGrid) betaML = beta_estim_obs_field(g, labels, (lpf, betaGrid), 'ML')
def test_obs_3Dfield_MAP(self): """ Test estimation of beta with an observed field: a small 3D case. Partition function estimation method : extrapolation scheme. Use the MAP on p(beta|label). """ shape = (5, 5, 5) mask = _np.ones(shape, dtype=int) # full mask g = graph_from_lattice(mask, kerMask=kerMask3D_6n) # generate a field: beta = 0.4 nbClasses = 2 labels = genPotts(g, beta, nbClasses) # partition function estimation gridLnz = Cpt_Vec_Estim_lnZ_Graph(g, nbClasses) gridPace = gridLnz[1][1] - gridLnz[1][0] # beta estimation be, pb = beta_estim_obs_field(g, labels, gridLnz)
def test_single_Onsager_MAP(self): """PF method: Onsager. MAP on p(label|beta). """ # generate a field: beta = 0.4 nbClasses = 2 print 'generating potts ..., beta =', beta labels = genPotts(self.g, beta, nbClasses) # partition function estimation dbeta = 0.05 betaGrid = np.arange(0, 1.5, dbeta) lpf = logpf_ising_onsager(labels.size, betaGrid) #lpf, betaGrid = Cpt_Vec_Estim_lnZ_Graph(g, nbClasses) print 'lpf:' print lpf betaMAP = beta_estim_obs_field(self.g, labels, (lpf, betaGrid), 'MAP') print 'betaMAP:', betaMAP
def test_obs_2Dfield_MAP(self): """ Test estimation of beta with an observed 2D field. Partition function estimation method : extrapolation scheme. Use the MAP on p(beta|label). """ shape = (6, 6) mask = _np.ones(shape, dtype=int) # full mask g = graph_from_lattice(mask, kerMask=kerMask2D_4n) # generate a field: beta = 0.4 nbClasses = 2 labels = genPotts(g, beta, nbClasses) # partition function estimation old_settings = np.seterr('raise') gridLnz = Cpt_Vec_Estim_lnZ_Graph(g, nbClasses) gridPace = gridLnz[1][1] - gridLnz[1][0] # beta estimation be, pb = beta_estim_obs_field(g, labels, gridLnz) np.seterr(**old_settings)
def test_single_PFES_MAP(self): """PF estimation method : extrapolation scheme. MAP on p(beta|label). """ # generate a field: beta = 0.4 nbClasses = 2 nbTests = 30 #print 'g:', self.g # partition function estimation gridLnz = Cpt_Vec_Estim_lnZ_Graph_fast3(self.g, nbClasses) gridPace = gridLnz[1][1] - gridLnz[1][0] #print 'generating potts ..., beta =', beta bes = np.zeros(nbTests) for t in xrange(nbTests): labels = genPotts(self.g, beta, nbClasses) # beta estimation be, pb = beta_estim_obs_field(self.g, labels, gridLnz, 'MAP') print 'betaMAP:', be bes[t] = be print 'mean betaMAP:', bes.mean(), bes.std() assert abs(bes.mean()-beta) <= gridPace
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