コード例 #1
0
ファイル: valid_beta_estim.py プロジェクト: zddzxxsmile/pyhrf
    def MC_comp_pfmethods_ML(self, shape):

        mask = np.ones(shape, dtype=int) #full mask
        g = graph_from_lattice(mask, kerMask=kerMask2D_4n, toroidal=True)

        # Betas to iterate over ...
        betaMax = 1.4
        dbeta = .1
        betas = np.arange(0., betaMax, dbeta)

        # nb of monte carlo iterations
        mcit = 40#100
        
        # nb of classes
        nbc = 2

        # Path sampling:
        mBePS, stdBePS, stdBeMCPS = cached_eval(test_beta_estim_obs_fields,
                                                ([g], betas, nbc, 'PS',
                                                 mcit), path=config.cacheDir,
                                                prefix='pyhrf_',
                                                new=False)
        
        # Extrapolation scheme:
        mBeES, stdBeES, stdBeMCES = cached_eval(test_beta_estim_obs_fields,
                                                ([g], betas, nbc, 'ES',
                                                 mcit), path=config.cacheDir,
                                                prefix='pyhrf_',
                                                new=False)

        # Onsager:
        #mBeON, stdBeON, stdBeMCON = cached_eval(test_beta_estim_obs_fields,
        #                                        ([g], betas, nbc, 'ONSAGER',
        #                                         mcit), path=config.cacheDir,
        #                                        prefix='pyhrf_',
        #                                        new=True)
        
        if self.plot:
            import matplotlib.pyplot as plt
            plt.figure()

            plt.errorbar(betas, mBeES, stdBeMCES, fmt='r', label='ES')
            plt.errorbar(betas, mBePS, stdBeMCPS, fmt='b', label='PS')
            #plt.errorbar(betas, mBeON, stdBeMCON, fmt='g', label='Onsager')
            plt.plot(betas, betas, 'k', label='true')

            #plt.xlabel('simulated beta')
            #plt.ylabel('beta MAP')
            plt.legend(loc='upper left')
            plt.ylim((0.0,1.45))
            #plt.title('MC validation (%d it) for beta estimation on '\
            #                ' observed fields.' %mcit)
            #fn = 'comp_obs_field_MC_PS_ES_Onsager_%dx%d' %shape 
            fn = 'comp_obs_field_MC_PS_%dx%d' %shape 
            figFn = os.path.join(self.outDir, figfn(fn))
            plt.savefig(figFn)
コード例 #2
0
ファイル: test_beta_estim.py プロジェクト: Solvi/pyhrf
    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)
コード例 #3
0
ファイル: toolsTest.py プロジェクト: ainafp/pyhrf
 def test_slow_func(self):
     t0 = time.time()
     cached_eval(slow_func, {'a': 4, 'b': 6}, path=self.cache_dir)
     delta = time.time() - t0
     t0 = time.time()
     cached_eval(slow_func, {'a': 4, 'b': 6}, path=self.cache_dir)
     delta = time.time() - t0
     assert delta < .1
     t0 = time.time()
     cached_eval(slow_func, {'a': 4, 'b': 8}, path=self.cache_dir)
     delta = time.time() - t0
コード例 #4
0
ファイル: toolsTest.py プロジェクト: zddzxxsmile/pyhrf
 def test_slow_func(self):
     t0 = time.time()
     cached_eval(slow_func, {'a': 4, 'b': 6}, path=self.cache_dir)
     delta = time.time() - t0
     t0 = time.time()
     cached_eval(slow_func, {'a': 4, 'b': 6}, path=self.cache_dir)
     delta = time.time() - t0
     assert delta < .1
     t0 = time.time()
     cached_eval(slow_func, {'a': 4, 'b': 8}, path=self.cache_dir)
     delta = time.time() - t0
コード例 #5
0
ファイル: valid_beta_estim.py プロジェクト: zddzxxsmile/pyhrf
def beta_estim_obs_field_mc(graph, nbClasses, beta, gridLnz, mcit=1, 
                            cachePotts=False):

    cumulBetaEstim = 0.
    cumul2BetaEstim = 0.

    for mci in xrange(mcit):
        labels = cached_eval(genPotts, (graph, beta, nbClasses), 
                             save=cachePotts, new=(not cachePotts))
        print 'labels:',labels
        betaEstim, _ = beta_estim_obs_field(graph, labels, gridLnz,
                                            method='ML')
        #betaEstim, pb = beta_estim_obs_field(graph, labels, gridLnz,
        #                                     method='MAP')
        print ' -mc- be=%f' %betaEstim
        cumulBetaEstim += betaEstim
        cumul2BetaEstim += betaEstim**2

    meanBetaEstim = cumulBetaEstim / mcit
    stdBetaEstim = ((cumul2BetaEstim / mcit) - meanBetaEstim**2)**.5

    return meanBetaEstim, stdBetaEstim
コード例 #6
0
ファイル: toolsTest.py プロジェクト: ainafp/pyhrf
 def test_code_digest(self):
     t0 = time.time()
     cached_eval(slow_func, {'a': 4, 'b': 8}, digest_code=True,
                 path=self.cache_dir)
     delta = time.time() - t0
コード例 #7
0
ファイル: toolsTest.py プロジェクト: ainafp/pyhrf
 def test_simple_args(self):
     cached_eval(foo_func, {'a': 4, 'b': 6}, path=self.cache_dir)
コード例 #8
0
ファイル: toolsTest.py プロジェクト: ainafp/pyhrf
 def test_simple(self):
     cached_eval(foo_func, (1, 2), path=self.cache_dir)
コード例 #9
0
ファイル: valid_beta_estim.py プロジェクト: zddzxxsmile/pyhrf
def test_beta_estim_obs_fields(graphs, betas, nbLabels, pfmethod, mcit=1):
    
    nbGraphs = len(graphs)
    meanBetaEstims = np.zeros(len(betas))
    stdBetaEstims = np.zeros(len(betas))
    stdMCBetaEstims = np.zeros(len(betas))
    gridLnz = range(nbGraphs)
    if mcit == 1:
        cachePotts = True
    else:
        cachePotts = False
        
    # compute partition functions:
    print 'compute partition functions ... nbgraphs:', nbGraphs
    print 'Method =', pfmethod
    print 'nbLabels =', nbLabels
    if pfmethod == 'ES':
        for ig, g in enumerate(graphs):
            grid = cached_eval(Cpt_Vec_Estim_lnZ_Graph_fast3, (g,nbLabels),
                               path=config.cacheDir, prefix='pyhrf_')
            gridLnz[ig] = (grid[0][:-1], grid[1][:-1])    
    elif pfmethod == 'PS':
        for ig, g in enumerate(graphs):
            gridLnz[ig] = cached_eval(Cpt_Vec_Estim_lnZ_Graph, (g,nbLabels),
                                      path=config.cacheDir, new=False,
                                      prefix='pyhrf_')
    elif pfmethod == 'ONSAGER':
        for ig, g in enumerate(graphs):
            betaGrid = cached_eval(Cpt_Vec_Estim_lnZ_Graph,([[]],nbLabels),
                                   path=config.cacheDir)[1]
            lpf = cached_eval(logpf_ising_onsager, (len(g),betaGrid),
                              path=config.cacheDir, prefix='pyhrf_')
            gridLnz[ig] = (lpf, betaGrid)
    else:
        print 'unknown pf method!'
        sys.exit(1)

    print 'gridLnz:'
    print gridLnz

    # Beta estimation:
    print 'beta estimation test loop ...'
    for ib, b in enumerate(betas):
        cumulBetaEstim = 0.
        cumul2BetaEstim = 0.
        cumulStdMC = 0.
        print 'testing with beta =', b, '...'
        for ig, graph in enumerate(graphs):
            mbe, stdmc = beta_estim_obs_field_mc(graph, nbLabels, b,
                                                 gridLnz[ig], mcit, 
                                                 cachePotts=False)
            print '%f(%f)' %(mbe,stdmc)
            cumulBetaEstim += mbe
            cumulStdMC += stdmc
            cumul2BetaEstim += mbe**2
            #print betaEstim, cumulBetaEstim, cumul2BetaEstim

        meanBetaEstims[ib] = cumulBetaEstim / nbGraphs
        stdBetaEstims[ib] = ((cumul2BetaEstim / nbGraphs) - \
                                 meanBetaEstims[ib]**2)**.5
        stdMCBetaEstims[ib] = cumulStdMC / nbGraphs
        #print '-> ', meanBetaEstims[ib], stdBetaEstims[ib]
    return meanBetaEstims, stdBetaEstims, stdMCBetaEstims
コード例 #10
0
ファイル: toolsTest.py プロジェクト: zddzxxsmile/pyhrf
 def test_code_digest(self):
     t0 = time.time()
     cached_eval(slow_func, {'a': 4, 'b': 8}, digest_code=True,
                 path=self.cache_dir)
     delta = time.time() - t0
コード例 #11
0
ファイル: toolsTest.py プロジェクト: zddzxxsmile/pyhrf
 def test_simple_args(self):
     cached_eval(foo_func, {'a': 4, 'b': 6}, path=self.cache_dir)
コード例 #12
0
ファイル: toolsTest.py プロジェクト: zddzxxsmile/pyhrf
 def test_simple(self):
     cached_eval(foo_func, (1, 2), path=self.cache_dir)
コード例 #13
0
ファイル: test_beta_estim.py プロジェクト: Solvi/pyhrf
    def MC_comp_pfmethods_ML_3C(self, shape):

        newEval = config.updateCache
        
        mask = np.ones(shape, dtype=int) #full mask
        g = graph_from_lattice(mask, kerMask=kerMask2D_4n, toroidal=True)

        # Betas to iterate over ...
        betaMax = 1.4
        dbeta = .1
        betas = np.arange(0., betaMax, dbeta)

        # nb of monte carlo iterations
        mcit = 100#100
        
        # nb of classes
        nbc = 3

        # Path sampling:
        mBePS, stdBePS, stdBeMCPS = cached_eval(test_beta_estim_obs_fields,
                                                ([g], betas, nbc, 'PS',
                                                 mcit), path=config.cacheDir,
                                                prefix=pyhrf.tmpPrefix,
                                                new=False)
        
        # Extrapolation scheme:
        mBeES, stdBeES, stdBeMCES = cached_eval(test_beta_estim_obs_fields,
                                                ([g], betas, nbc, 'ES',
                                                 mcit), path=config.cacheDir,
                                                prefix=pyhrf.tmpPrefix,
                                                new=False)

        # Onsager:
        #mBeON, stdBeON, stdBeMCON = cached_eval(test_beta_estim_obs_fields,
        #                                        ([g], betas, nbc, 'ONSAGER',
        #                                         mcit), path=config.cacheDir,
        #                                        prefix=pyhrf.tmpPrefix,
        #                                        new=True)
        
        if self.plot:
            import plt
            plt.figure()

            plt.errorbar(betas, mBeES, stdBeMCES, fmt='r', label='ES')
            plt.errorbar(betas, mBePS, stdBeMCPS, fmt='b', label='PS')
            #plt.errorbar(betas, mBeON, stdBeMCON, fmt='g', label='Onsager')
            plt.plot(betas, betas, 'k', label='true')
            #plt.xlabel('simulated beta')
            #plt.ylabel('beta MAP')
            plt.legend(loc='upper left')
            #plt.title('MC validation (%d it) for beta estimation on '\
            #                ' observed fields.' %mcit)
            fn = 'comp_obs_field_3class_MC_PS_ES_%dx%d' %shape 
            figFn = os.path.join(self.outDir, figfn(fn))
            plt.savefig(figFn)
            
            
            plt.figure()
            grid = cached_eval(Cpt_Vec_Estim_lnZ_Graph_fast3, (g,nbc),
                               path=config.cacheDir, prefix=pyhrf.tmpPrefix)
            lnzES, betas = (grid[0][:-1], grid[1][:-1])    
            lnzPS, betas = cached_eval(Cpt_Vec_Estim_lnZ_Graph, (g,nbc),
                                       path=config.cacheDir, new=False,
                                       prefix=pyhrf.tmpPrefix)

            plt.plot(betas, lnzES, 'r', label='ES')
            plt.plot(betas, lnzPS, 'b', label='PS')
            fn = 'comp_PF_3class_PS_ES_%dx%d' %shape 
            figFn = os.path.join(self.outDir, figfn(fn))
            plt.savefig(figFn)