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)
def __new__(self, scatterer, calc_func, medium_index=None, illum_wavelen=None, illum_polarization=None, theory='auto', alpha=None, constraints=[]): if calc_func is calc_holo: fit_warning('hp.inference.AlphaModel', 'Model') if alpha is None: alpha = 1.0 model = AlphaModel(scatterer, alpha, None, medium_index, illum_wavelen, illum_polarization, theory, constraints) elif alpha is None: fit_warning('hp.inference.ExactModel','Model') model = ExactModel(scatterer, calc_func, None, medium_index, illum_wavelen, illum_polarization, theory, constraints) else: raise ValueError("Cannot interpret alpha for non-hologram scattering \ calculations") def residual(pars, data): return model._residuals(pars, data, 1/np.sqrt(2)).flatten() setattr(model, '_calc', model.forward) setattr(model, 'residual', residual) def get_alpha(pars): try: return model._get_parameter('alpha', pars) except MissingParameter: return 1.0 setattr(model, 'get_alpha', get_alpha) return model
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)
def make_default_model(base_scatterer, fitting_parameters=None): if fitting_parameters is None: fitting_parameters = base_scatterer.parameters.keys() scatterer = parameterize_scatterer(base_scatterer, fitting_parameters) alpha_prior = Uniform(0, 1, guess=0.7, name='alpha') return AlphaModel(scatterer, noise_sd=1, alpha=alpha_prior)