def div_normalize(holo, bg, df, model): if df is None: df = np.zeros_like(holo) if bg is not None: imagetofit = normalize((holo-df)/(bg-df)) else: imagetofit = normalize(holo) return imagetofit
def div_normalize(holo, bg, df, model): if df is None: df = np.zeros_like(holo) if bg is not None: imagetofit = normalize((holo - df) / (bg - df)) else: imagetofit = normalize(holo) return imagetofit
def preprocess(holo, bg, df, model): center = np.array(model.scatterer.guess.center[:2])/holo.spacing try: return normalize(subimage(holo/bg, center, size)) except IndexError: if not recenter_at_edge: raise new_center = np.array(model.scatterer.guess.center[:2]) new_center -= np.clip(new_center-np.array(size)/2, -np.inf, 0) new_center += np.clip(holo.shape[:2]-(new_center + np.array(size)/2), -np.inf, 0) return normalize(subimage(holo/bg, new_center, size))
def preprocess(holo, bg, df, model): center = np.array(model.scatterer.guess.center[:2]) / holo.spacing try: return normalize(subimage(holo / bg, center, size)) except IndexError: if not recenter_at_edge: raise new_center = np.array(model.scatterer.guess.center[:2]) new_center -= np.clip(new_center - np.array(size) / 2, -np.inf, 0) new_center += np.clip( holo.shape[:2] - (new_center + np.array(size) / 2), -np.inf, 0) return normalize(subimage(holo / bg, new_center, size))
def test_fit_mie_single(): holo = normalize(get_example_data('image0001.yaml')) parameters = [ Parameter(name='x', guess=.567e-5, limit=[0.0, 1e-5]), Parameter(name='y', guess=.576e-5, limit=[0, 1e-5]), Parameter(name='z', guess=15e-6, limit=[1e-5, 2e-5]), Parameter(name='n', guess=1.59, limit=[1, 2]), Parameter(name='r', guess=8.5e-7, limit=[1e-8, 1e-5]) ] def make_scatterer(x, y, z, r, n): return Sphere(n=n + 1e-4j, r=r, center=(x, y, z)) thry = Mie(False) model = Model(Parametrization(make_scatterer, parameters), thry.calc_holo, alpha=Parameter(name='alpha', guess=.6, limit=[.1, 1])) assert_raises(InvalidMinimizer, fit, model, holo, minimizer=Sphere) result = fit(model, holo) assert_obj_close(result.scatterer, gold_sphere, rtol=1e-3) assert_approx_equal(result.parameters['alpha'], gold_alpha, significant=3) assert_equal(model, result.model)
def load_bgdivide_crop_v2( path, metadata, particle_position, bkg, dark, channel=RGB_CHANNEL, size=HOLOGRAM_SIZE): data = hp.load_image(path, channel=channel, **metadata) data = bg_correct(data, bkg, dark) data = subimage(data, particle_position[::-1], size) data = normalize(data) return data
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)
def load_bgdivide_crop( path, metadata, particle_position, bg_prefix="bg", df_prefix=None, channel=RGB_CHANNEL, size=HOLOGRAM_SIZE): data = hp.load_image(path, channel=channel, **metadata) bkg = load_bkg(path, bg_prefix, refimg=data) dark = None # load_dark(path, df_prefix, refimg=data) data = bg_correct(data, bkg, dark) data = subimage(data, particle_position[::-1], size) data = normalize(data) return data
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)
def test_fit_multisphere_noisydimer_slow(): optics = Optics(wavelen=658e-9, polarization = [0., 1.0], divergence = 0., pixel_scale = [0.345e-6, 0.345e-6], index = 1.334) holo = normalize(get_example_data('image0002.yaml')) # Now construct the model, and fit parameters = [Parameter(name = 'x0', guess = 1.64155e-5, limit = [0, 1e-4]), Parameter(1.7247e-5, [0, 1e-4], 'y0'), Parameter(20.582e-6, [0, 1e-4], 'z0'), Parameter(.6856e-6, [1e-8, 1e-4], 'r0'), Parameter(1.6026, [1, 2], 'nr0'), Parameter(1.758e-5, [0, 1e-4], 'x1'), Parameter(1.753e-5, [0, 1e-4], 'y1'), Parameter(21.2698e-6, [1e-8, 1e-4], 'z1'), Parameter(.695e-6, [1e-8, 1e-4], 'r1'), Parameter(1.6026, [1, 2], 'nr1')] def make_scatterer(x0, x1, y0, y1, z0, z1, r0, r1, nr0, nr1): s = Spheres([ Sphere(center = (x0, y0, z0), r=r0, n = nr0+1e-5j), Sphere(center = (x1, y1, z1), r=r1, n = nr1+1e-5j)]) return s # initial guess #s1 = Sphere(n=1.6026+1e-5j, r = .6856e-6, # center=(1.64155e-05, 1.7247e-05, 20.582e-6)) #s2 = Sphere(n=1.6026+1e-5j, r = .695e-6, # center=(1.758e-05, 1.753e-05, 21.2698e-6)) #sc = Spheres([s1, s2]) #alpha = 0.99 #lb1 = Sphere(1+1e-5j, 1e-8, 0, 0, 0) #ub1 = Sphere(2+1e-5j, 1e-5, 1e-4, 1e-4, 1e-4) #step1 = Sphere(1e-4+1e-4j, 1e-8, 0, 0, 0) #lb = Spheres([lb1, lb1]), .1 #ub = Spheres([ub1, ub1]), 1 #step = Spheres([step1, step1]), 0 model = Model(parameters, Multisphere, make_scatterer=make_scatterer, alpha = Parameter(.99, [.1, 1.0], 'alpha')) result = fit(model, holo) print result.scatterer gold = np.array([1.642e-5, 1.725e-5, 2.058e-5, 1e-5, 1.603, 6.857e-7, 1.758e-5, 1.753e-5, 2.127e-5, 1e-5, 1.603, 6.964e-7]) gold_alpha = 1.0 assert_parameters_allclose(result.scatterer, gold, rtol=1e-2) # TODO: This test fails, alpha comes back as .9899..., where did # the gold come from? assert_approx_equal(result.alpha, gold_alpha, significant=2)
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 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)
def test_returns_close_values(self): model = self._make_model() holo = normalize(get_example_data('image0001')) np.random.seed(40) result = NmpfitStrategy(npixels=1000).fit(model, holo) # TODO: figure out if it is a problem that alpha is frequently # coming out wrong in the 3rd decimal place. self.assertAlmostEqual(result.parameters['alpha'], gold_alpha, places=3) # TODO: this tolerance has to be rather large to pass, we should # probably track down if this is a sign of a problem assert_obj_close(result.scatterer, gold_sphere, rtol=1e-2)
def test_fit_single_openopt(): holo = normalize(get_example_data('image0001.yaml')) s = Sphere(center = (par(guess=.567e-5, limit=[.4e-5,.6e-5]), par(.567e-5, (.4e-5, .6e-5)), par(15e-6, (1.3e-5, 1.8e-5))), r = par(8.5e-7, (5e-7, 1e-6)), n = ComplexParameter(par(1.59, (1.5,1.8)), 1e-4j)) model = Model(s, Mie(False).calc_holo, alpha = par(.6, [.1,1])) try: minimizer = OpenOpt('scipy_slsqp') except ImportError: raise SkipTest result = fit(model, holo, minimizer = minimizer) assert_obj_close(result.scatterer, gold_sphere, rtol=1e-3) # TODO: see if we can get this back to 3 sig figs correct alpha assert_approx_equal(result.parameters['alpha'], gold_alpha, significant=3) assert_equal(model, result.model)
def test_fit_mie_par_scatterer(): holo = normalize(get_example_data('image0001')) s = Sphere(center=(Uniform(0, 1e-5, guess=.567e-5), Uniform(0, 1e-5, .567e-5), Uniform(1e-5, 2e-5)), r=Uniform(1e-8, 1e-5, 8.5e-7), n=ComplexPrior(Uniform(1, 2, 1.59), 1e-4)) thry = Mie(False) model = AlphaModel(s, theory=thry, alpha=Uniform(.1, 1, .6)) result = fix_flat(NmpfitStrategy().fit(model, holo)) assert_obj_close(result.scatterer, gold_sphere, rtol=1e-3) # TODO: see if we can get this back to 3 sig figs correct alpha assert_approx_equal(result.parameters['alpha'], gold_alpha, significant=3) assert_equal(model, result.model) assert_read_matches_write(result)
def test_fit_mie_par_scatterer(): holo = normalize(get_example_data('image0001.yaml')) s = Sphere(center = (par(guess=.567e-5, limit=[0,1e-5]), par(.567e-5, (0, 1e-5)), par(15e-6, (1e-5, 2e-5))), r = par(8.5e-7, (1e-8, 1e-5)), n = ComplexParameter(par(1.59, (1,2)), 1e-4j)) thry = Mie(False) model = Model(s, thry.calc_holo, alpha = par(.6, [.1,1])) result = fit(model, holo) assert_obj_close(result.scatterer, gold_sphere, rtol=1e-3) # TODO: see if we can get this back to 3 sig figs correct alpha assert_approx_equal(result.parameters['alpha'], gold_alpha, significant=3) assert_equal(model, result.model) assert_read_matches_write(result)
def test_fit_random_subset(): holo = normalize(get_example_data('image0001.yaml')) s = Sphere(center = (par(guess=.567e-5, limit=[0,1e-5]), par(.567e-5, (0, 1e-5)), par(15e-6, (1e-5, 2e-5))), r = par(8.5e-7, (1e-8, 1e-5)), n = ComplexParameter(par(1.59, (1,2)),1e-4j)) model = Model(s, Mie.calc_holo, alpha = par(.6, [.1,1])) np.random.seed(40) result = fit(model, holo, random_subset=.1) # we have to use a relatively loose tolerance here because the random # selection occasionally causes the fit to be a bit worse assert_obj_close(result.scatterer, gold_sphere, rtol=1e-3) # TODO: figure out if it is a problem that alpha is frequently coming out # wrong in the 3rd decimal place. assert_approx_equal(result.parameters['alpha'], gold_alpha, significant=3) assert_equal(model, result.model) assert_read_matches_write(result)
def test_fit_random_subset(): holo = normalize(get_example_data('image0001.yaml')) s = Sphere(center = (par(guess=.567e-5, limit=[0,1e-5]), par(.567e-5, (0, 1e-5)), par(15e-6, (1e-5, 2e-5))), r = par(8.5e-7, (1e-8, 1e-5)), n = ComplexParameter(par(1.59, (1,2)),1e-4)) model = Model(s, Mie(False).calc_holo, alpha = par(.6, [.1,1])) np.random.seed(40) result = fit(model, holo, random_subset=.1) # TODO: this tolerance has to be rather large to pass, we should # probably track down if this is a sign of a problem assert_obj_close(result.scatterer, gold_sphere, rtol=1e-2) # TODO: figure out if it is a problem that alpha is frequently coming out # wrong in the 3rd decimal place. assert_approx_equal(result.parameters['alpha'], gold_alpha, significant=3) assert_equal(model, result.model) assert_read_matches_write(result)
def test_fit_random_subset(): holo = normalize(get_example_data('image0001.yaml')) s = Sphere(center = (par(guess=.567e-5, limit=[0,1e-5]), par(.567e-5, (0, 1e-5)), par(15e-6, (1e-5, 2e-5))), r = par(8.5e-7, (1e-8, 1e-5)), n = ComplexParameter(par(1.59, (1,2)),1e-4j)) model = Model(s, Mie.calc_holo, alpha = par(.6, [.1,1])) np.random.seed(40) result = fit(model, holo, use_random_fraction=.1) # we have to use a relatively loose tolerance here because the random # selection occasionally causes the fit to be a bit worse assert_obj_close(result.scatterer, gold_sphere, rtol=1e-2) # TODO: figure out if it is a problem that alpha is frequently coming out # wrong in the 3rd decimal place. assert_approx_equal(result.parameters['alpha'], gold_alpha, significant=2) assert_equal(model, result.model) assert_read_matches_write(result)
def test_fit_random_subset(): holo = normalize(get_example_data('image0001')) s = Sphere(center=(Uniform(0, 1e-5, guess=.567e-5), Uniform(0, 1e-5, .567e-5), Uniform(1e-5, 2e-5)), r=Uniform(1e-8, 1e-5, 8.5e-7), n=ComplexPrior(Uniform(1, 2, 1.59), 1e-4)) model = AlphaModel(s, theory=Mie(False), alpha=Uniform(.1, 1, .6)) np.random.seed(40) result = fix_flat(NmpfitStrategy(npixels=1000).fit(model, holo)) # TODO: this tolerance has to be rather large to pass, we should # probably track down if this is a sign of a problem assert_obj_close(result.scatterer, gold_sphere, rtol=1e-2) # TODO: figure out if it is a problem that alpha is frequently coming out # wrong in the 3rd decimal place. assert_approx_equal(result.parameters['alpha'], gold_alpha, significant=3) assert_equal(model, result.model) assert_read_matches_write(result)
def test_fit_random_subset(): holo = normalize(get_example_data('image0001.yaml')) s = Sphere(center=(par(guess=.567e-5, limit=[0, 1e-5]), par(.567e-5, (0, 1e-5)), par(15e-6, (1e-5, 2e-5))), r=par(8.5e-7, (1e-8, 1e-5)), n=ComplexParameter(par(1.59, (1, 2)), 1e-4)) model = Model(s, Mie(False).calc_holo, alpha=par(.6, [.1, 1])) np.random.seed(40) result = fit(model, holo, random_subset=.1) # TODO: this tolerance has to be rather large to pass, we should # probably track down if this is a sign of a problem assert_obj_close(result.scatterer, gold_sphere, rtol=1e-2) # TODO: figure out if it is a problem that alpha is frequently coming out # wrong in the 3rd decimal place. assert_approx_equal(result.parameters['alpha'], gold_alpha, significant=3) assert_equal(model, result.model) assert_read_matches_write(result)
def load_bgdivide_crop(path, metadata, particle_position, bkg, dark, channel=RGB_CHANNEL, size=HOLOGRAM_SIZE, recenter=True): data = hp.load_image(path, channel=channel, **metadata) data = bg_correct(data, bkg, dark) if recenter: bbox = subimage(data, particle_position, size) bbox_corner = np.array([bbox.x.min(), bbox.y.min()]) found_position = np.round( center_find(bbox) + bbox_corner / metadata['spacing']) data = subimage(data, found_position, size) else: data = subimage(data, particle_position, size) data = normalize(data) if recenter: return data, found_position return data, None
def test_fit_mie_single(): holo = normalize(get_example_data('image0001.yaml')) parameters = [Parameter(name='x', guess=.567e-5, limit = [0.0, 1e-5]), Parameter(name='y', guess=.576e-5, limit = [0, 1e-5]), Parameter(name='z', guess=15e-6, limit = [1e-5, 2e-5]), Parameter(name='n', guess=1.59, limit = [1, 2]), Parameter(name='r', guess=8.5e-7, limit = [1e-8, 1e-5])] def make_scatterer(x, y, z, r, n): return Sphere(n=n+1e-4j, r = r, center = (x, y, z)) thry = Mie(False) model = Model(Parametrization(make_scatterer, parameters), thry.calc_holo, alpha=Parameter(name='alpha', guess=.6, limit = [.1, 1])) assert_raises(InvalidMinimizer, fit, model, holo, minimizer=Sphere) result = fit(model, holo) assert_obj_close(result.scatterer, gold_sphere, rtol = 1e-3) assert_approx_equal(result.parameters['alpha'], gold_alpha, significant=3) assert_equal(model, result.model)
def test_fit_mie_single(): holo = normalize(get_example_data('image0001')) parameters = [ Uniform(0, 1e-5, name='x', guess=.567e-5), Uniform(0, 1e-5, name='y', guess=.576e-5), Uniform(1e-5, 2e-5, name='z', guess=15e-6), Uniform(1, 2, name='n', guess=1.59), Uniform(1e-8, 1e-5, name='r', guess=8.5e-7) ] def make_scatterer(parlist): return Sphere(n=parlist[3], r=parlist[4], center=parlist[0:3]) thry = Mie(False) model = AlphaModel(make_scatterer(parameters), theory=thry, alpha=Uniform(.1, 1, name='alpha', guess=.6)) result = NmpfitStrategy().fit(model, holo) assert_obj_close(result.scatterer, gold_sphere, rtol=1e-3) assert_approx_equal(result.parameters['alpha'], gold_alpha, significant=3) assert_equal(model, result.model)
def _load_data(self, name): # need metadata, particle_position! data = hp.load_image(name, channel=RGB_CHANNEL, **self.metadata) data = bg_correct(data, self._background, self._darkfield) data = subimage(data, self.particle_position[::-1], HOLOGRAM_SIZE) data = normalize(data) return data
def load_gold_example_data(): return normalize(hp.load(hp.core.io.get_example_data_path('image0001.h5')))
def test_hologram_io(): holo = normalize(get_example_data('image0001.yaml')) assert_read_matches_write(holo)
def fit_multisphere(data_path, a_p, n_p, z_guess, theta_guess, phi_guess, fit_a=False): px = cv2.imread(data_path).shape[0] data_holo = hp.load_image(data_path, spacing=mag, medium_index=n_m, illum_wavelen=wv, illum_polarization=(1, 0), channel=0) data_holo = normalize(data_holo) z_p = prior.Uniform(lower_bound=45, upper_bound=100, guess=z_guess, name='z_p') theta = prior.Uniform(lower_bound=0, upper_bound=np.pi / 2, guess=theta_guess, name='theta') phi = prior.Uniform(lower_bound=0, upper_bound=np.pi, guess=phi_guess, name='phi') ''' #idk why this doesn't work, maybe the fitter has some issue with numpy center = (mag*px, mag*px, z_p) delta = np.array([np.cos(phi)*np.cos(theta), np.sin(phi)*np.cos(theta), np.sin(theta)]) c1 = + 1.001*a_p*delta c2 = - 1.001*a_p*delta cluster = np.array([c1, c2]) s1 = Sphere(center = cluster[0], n = n_p, r = a_p) s2 = Sphere(center = cluster[1], n = n_p, r = a_p) ''' if fit_a: a_1 = prior.Uniform(lower_bound=a_p * 0.8, upper_bound=a_p * 1.2, guess=a_p, name='a_1') a_2 = prior.Uniform(lower_bound=a_p * 0.8, upper_bound=a_p * 1.2, guess=a_p, name='a_2') else: a_1 = a_p a_2 = a_p x1 = mag * px / 2 + a_1 * np.cos(phi) * np.cos(theta) * 1.001 x2 = mag * px / 2 - a_2 * np.cos(phi) * np.cos(theta) * 1.001 y1 = mag * px / 2 + a_1 * np.cos(theta) * np.sin(phi) * 1.001 y2 = mag * px / 2 + a_2 * np.cos(theta) * np.sin(phi) * 1.001 z1 = z_p + a_1 * np.sin(theta) * 1.001 z2 = z_p - a_2 * np.sin(theta) * 1.001 s1 = Sphere(center=[x1, y1, z1], n=n_p, r=a_1) s2 = Sphere(center=[x2, y2, z2], n=n_p, r=a_2) dimer = Spheres([s1, s2], warn=False) model = ExactModel(scatterer=dimer, calc_func=calc_holo, noise_sd=None, medium_index=n_m, illum_wavelen=wv, illum_polarization=(1, 0)) fit_result = hp.fit(data_holo, model) return fit_result
def preprocess(holo, bg, df, model): center = np.array(model.scatterer.guess.center[:2])/holo.spacing return normalize(subimage(holo/bg, center, size))
'value': y }, { 'parname': 'z', 'limited': [False, False], 'limits': [0, 0], 'step': 100e-9, 'mpmaxstep': 1e-6, 'value': z }, { 'parname': 'scaling_alpha', 'limited': [True, True], 'limits': [0.0, 1.0], 'value': scaling_alpha }] holo = normalize(get_example_data('image0001')) # define the residual function def residfunct(p, fjac=None): # nmpfit calls residfunct w/fjac as a kwarg, we ignore sphere = Sphere(n=p[0] + n_particle_imag * 1j, r=p[1], center=p[2:5]) thry = Mie(False) calculated = calc_holo(holo, sphere, scaling=p[5], theory=thry) status = 0 derivates = holo - calculated return ([status, get_values(flat(derivates))])
def test_hologram_io(self): assert_read_matches_write(normalize(self.holo))