示例#1
0
 def test_BaseModel_lnprior(self):
     scat = Sphere(r=prior.Gaussian(1, 1), n=prior.Gaussian(1, 1),
                   center=[10, 10, 10])
     mod = Model(scat, noise_sd=0.1)
     # Desired: log(sqrt(0.5/pi))-1/2
     desired_sigma = -1.4189385332
     assert_obj_close(mod.lnprior({'n': 0, 'r': 0}), desired_sigma * 2)
示例#2
0
def test_PerfectLensModelholo_likelihood():
    holo = get_example_data('image0001')
    sphere_center = (prior.Gaussian(5, 1), prior.Gaussian(5, 1),
                     prior.Gaussian(5, 1))
    s = Sphere(n=prior.Gaussian(1.6, .1),
               r=prior.Gaussian(.5, .1),
               center=sphere_center)
    model = PerfectLensModel(s, noise_sd=0.01, lens_angle=0.8)
    assert_pickle_roundtrip(model)
示例#3
0
def test_ExactModelholo_likelihood():
    holo = get_example_data('image0001')
    sphere_center = (prior.Gaussian(5, 1), prior.Gaussian(5, 1),
                     prior.Gaussian(5, 1))
    s = Sphere(n=prior.Gaussian(1.6, .1),
               r=prior.Gaussian(.5, .1),
               center=sphere_center)
    model = ExactModel(s, noise_sd=.01)
    assert_pickle_roundtrip(model)
示例#4
0
def test_multidim():
    s = Sphere(n = {'r':par(0,[-1,1]),'g':par(0,0),'b':prior.Gaussian(0,1),'a':0}, r = xr.DataArray([prior.Gaussian(0,1),par(0,[-1,1]),par(0,0),0], dims='alph',coords={'alph':['a','b','c','d']}), center=[par(0,[-1,1]),par(0,0),0])
    par_s = ParameterizedObject(s)
    params = {'n_r':3,'n_g':4,'n_b':5,'n_a':6,'r_a':7,'r_b':8,'r_c':9,'r_d':10,'center[0]':7,'center[1]':8,'center[2]':9}
    out_s = Sphere(n={'r':3,'g':0,'b':5,'a':0}, r = xr.DataArray([7,8,0,0], dims='alph',coords={'alph':['a','b','c','d']}), center=[7,0,0])
    assert_obj_close(par_s.make_from(params), out_s)

    m = Model(s, np.sum)
    m._use_parameter(OrderedDict([('r',par(0,[-1,1])),('g',par(0,0)),('b',prior.Gaussian(0,1)),('a',0)]),'letters')
    m._use_parameter(xr.DataArray([prior.Gaussian(0,1),par(0,[-1,1]),par(0,0),0],dims='numbers',coords={'numbers':['one','two','three','four']}),'count')
    expected_params = [par(0,[-1,1],'letters_r'),par(0,0,'letters_g'),prior.Gaussian(0,1,'letters_b'),prior.Gaussian(0,1,'count_one'),par(0,[-1,1],'count_two'), par(0,0,'count_three')]
    assert_equal(m.parameters[-6:], expected_params)
示例#5
0
def test_TimeSeriesAlphaModel():
    n = TimeIndependent(prior.Gaussian(5, .5))
    assert_pickle_roundtrip(n)
    st = Sphere(n=n,
                r=TimeIndependent(prior.BoundedGaussian(1.6, .1, 0, np.inf)),
                center=(prior.Gaussian(10, 1), prior.Gaussian(10, 1),
                        prior.BoundedGaussian(1.6, .1, 0, np.inf)))
    assert_pickle_roundtrip(st)
    noise_sd = .1
    mt = TimeSeriesAlphaModel(st,
                              Mie,
                              noise_sd,
                              alpha=prior.Uniform(0, 1),
                              n_frames=2)
    assert_pickle_roundtrip(mt)
示例#6
0
def test_Emcee_Class():
    np.random.seed(40)
    scat = model.Parametrization(0,[prior.Gaussian(0,1)])
    mod = model.BaseModel(scat)
    e = mcmc.Emcee(mod,[],nwalkers=3)
    assert_equal(e.nwalkers,3)
    assert_obj_close(e.make_guess(),prior_dist)
示例#7
0
def test_subset_tempering():
    holo = normalize(get_example_data('image0001'))
    scat = Sphere(r=0.65e-6,n=1.58,center=[5.5e-6,5.8e-6,14e-6])
    mod = AlphaModel(scat,noise_sd=.1, alpha=prior.Gaussian(0.7,0.1))
    with warnings.catch_warnings():
        warnings.simplefilter('ignore')
        inf = sample.tempered_sample(mod, holo, nwalkers=4, samples=10, stages=1, stage_len=10, threads=None, seed=40)
    assert_obj_close(inf.MAP,gold_alpha, rtol=1e-3)
示例#8
0
def test_subset_tempering():
    np.random.seed(40)
    holo = normalize(get_example_data('image0001.yaml'))
    scat = Sphere(r=0.65e-6,n=1.58,center=[5.5e-6,5.8e-6,14e-6])
    mod = AlphaModel(scat,Mie,noise_sd=.1,alpha=prior.Gaussian(0.7,0.1))
    inf=mcmc.subset_tempering(mod,holo,final_len=10,nwalkers=4,stages=1,stage_len=10,threads=None, verbose=False,seed=40)
    assert_obj_close(inf.most_probable_values(),gold_alpha)
    assert_equal(inf.n_steps,gold_nsteps)
    assert_obj_close(inf.acceptance_fraction,gold_frac)
    assert_obj_close(float(inf.data_frame(burn_in=6)[1:2].alpha),gold_alpha)
示例#9
0
 def test_alpha_subset_tempering(self):
     holo = normalize(get_example_data('image0001'))
     scat = Sphere(r=0.65e-6, n=1.58, center=[5.5e-6, 5.8e-6, 14e-6])
     mod = AlphaModel(scat, noise_sd=.1, alpha=prior.Gaussian(0.7, 0.1))
     strat = TemperedStrategy(nwalkers=4, nsamples=10, stages=1,
                              stage_len=10, parallel=None, seed=40)
     with warnings.catch_warnings():
         warnings.simplefilter('ignore')
         inference_result = strat.sample(mod, holo)
     desired_alpha = np.array([0.650348])
     assert_allclose(inference_result._parameters, desired_alpha, rtol=5e-3)
示例#10
0
def test_AlphaModelholo_likelihood():
    holo = get_example_data('image0001')
    s = Sphere(
        prior.Gaussian(.5, .1), prior.Gaussian(1.6, .1),
        (prior.Gaussian(5, 1), prior.Gaussian(5, 1), prior.Gaussian(5, 1)))
    model = AlphaModel(s, alpha=prior.Gaussian(.7, .1), noise_sd=.01)
    assert_pickle_roundtrip(model)
示例#11
0
def test_emcee():
    holo = get_example_data('image0001.yaml')
    s = Sphere(
        prior.Gaussian(.5, .1), prior.Gaussian(1.6, .1),
        (prior.Gaussian(5, 1), prior.Gaussian(5, 1), prior.Gaussian(5, 1)))
    model = AlphaModel(s, Mie, alpha=prior.Gaussian(.7, .1), noise_sd=.01)
    emcee = Emcee(model, holo)
    assert_pickle_roundtrip(emcee)
示例#12
0
 def test_perfectlens_subset_tempering(self):
     data = normalize(get_example_data('image0001'))
     scatterer = Sphere(r=0.65e-6, n=1.58, center=[5.5e-6, 5.8e-6, 14e-6])
     model = PerfectLensModel(
         scatterer, noise_sd=.1, lens_angle=prior.Gaussian(0.7, 0.1))
     strat = TemperedStrategy(nwalkers=4, nsamples=10, stages=1,
                              stage_len=10, parallel=None, seed=40)
     with warnings.catch_warnings():
         warnings.simplefilter('ignore')
         inference_result = strat.sample(model, data)
     desired_lens_angle = np.array([0.7197887])
     is_ok = np.allclose(
         inference_result._parameters, desired_lens_angle, rtol=1e-3)
     self.assertTrue(is_ok)
示例#13
0
def test_find_noise():
    noise = 0.5
    s = Sphere(n=prior.Uniform(1.5, 1.7), r=2, center=[1, 2, 3])
    data_base = detector_grid(10, spacing=0.5)
    data_noise = update_metadata(data_base, noise_sd=noise)
    model_u = AlphaModel(s, alpha=prior.Uniform(0.7, 0.9))
    model_g = AlphaModel(s, alpha=prior.Gaussian(0.8, 0.1))
    pars = {'n': 1.6, 'alpha': 0.8}
    assert_equal(model_u._find_noise(pars, data_noise), noise)
    assert_equal(model_g._find_noise(pars, data_noise), noise)
    assert_equal(model_u._find_noise(pars, data_base), 1)
    assert_raises(MissingParameter, model_g._find_noise, pars, data_base)
    pars.update({'noise_sd': noise})
    assert_equal(model_g._find_noise(pars, data_base), noise)
示例#14
0
def test_multidim():
    par_s = Sphere(n={
        'r': prior.Uniform(-1, 1),
        'g': 0,
        'b': prior.Gaussian(0, 1),
        'a': 0
    },
                   r=xr.DataArray(
                       [prior.Gaussian(0, 1),
                        prior.Uniform(-1, 1), 0, 0],
                       dims='alph',
                       coords={'alph': ['a', 'b', 'c', 'd']}),
                   center=[prior.Uniform(-1, 1), 0, 0])
    params = {
        'n:r': 3,
        'n:g': 4,
        'n:b': 5,
        'n:a': 6,
        'r:a': 7,
        'r:b': 8,
        'r:c': 9,
        'r:d': 10,
        'center.0': 7,
        'center.1': 8,
        'center.2': 9
    }
    out_s = Sphere(n={
        'r': 3,
        'g': 0,
        'b': 5,
        'a': 0
    },
                   r={
                       'a': 7,
                       'b': 8,
                       'c': 0,
                       'd': 0
                   },
                   center=[7, 0, 0])
    assert_obj_close(par_s.from_parameters(params), out_s)

    m = ExactModel(out_s, np.sum)
    parletters = {
        'r': prior.Uniform(-1, 1),
        'g': 0,
        'b': prior.Gaussian(0, 1),
        'a': 0
    }
    parcount = xr.DataArray(
        [prior.Gaussian(0, 1),
         prior.Uniform(-1, 1), 0, 0],
        dims='numbers',
        coords={'numbers': ['one', 'two', 'three', 'four']})

    m._use_parameters({'letters': parletters, 'count': parcount})
    expected_params = {
        'letters:r': prior.Uniform(-1, 1, 0, 'letters:r'),
        'letters:b': prior.Gaussian(0, 1, 'letters:b'),
        'count:one': prior.Gaussian(0, 1, 'count:one'),
        'count:two': prior.Uniform(-1, 1, 0, 'count:two')
    }
    assert_equal(m.parameters, expected_params)
示例#15
0
def test_gaussian():
    g = prior.Gaussian(1, 1)
    assert_equal(g.guess, 1)
    assert_obj_close(g.lnprob(0),gold_sigma)
示例#16
0
def test_NoiseModel_lnprior():
    scat=Sphere(r=prior.Gaussian(1,1),n=prior.Gaussian(1,1),center=[10,10,10])
    mod=NoiseModel(scat,None,noise_sd=.1)
    assert_obj_close(mod.lnprior([0,0]),gold_sigma*2)
示例#17
0
 def test_require_noise_if_nonuniform(self):
     sphere = Sphere(r=prior.Gaussian(0, 1), n=prior.Uniform(1, 2))
     model = AlphaModel(sphere)
     schema = detector_grid(2, 2)
     pars = [0.5, 0.5]
     self.assertRaises(MissingParameter, model._find_noise, pars, schema)
示例#18
0
def test_prior():
    g = prior.Gaussian(1, 1)
    assert_pickle_roundtrip(g)
    assert_pickle_roundtrip(g.lnprob)