def test_dda_fit(): s = Sphere(n=1.59, r=.2, center=(5, 5, 5)) o = Optics(wavelen=.66, index=1.33, pixel_scale=.1) schema = ImageSchema(optics=o, shape=100) h = Mie.calc_holo(s, schema) def make_scatterer(r, x, y, z): local_s = Sphere(r=r, center=(x, y, z)) return Scatterer(local_s.indicators, n=s.n) parameters = [ par(.18, [.1, .3], name='r', step=.1), par(5, [4, 6], 'x'), par(5, [4, 6], 'y'), par(5.2, [4, 6], 'z') ] p = Parametrization(make_scatterer, parameters) model = Model(p, DDA.calc_holo) res = fit(model, h) assert_parameters_allclose(res.parameters, dict([('r', 0.2003609439787491), ('x', 5.0128083665603995), ('y', 5.0125252883133617), ('z', 4.9775097284878775)]), rtol=1e-3)
def test_naming(): #parameterizing with fixed params def makeScatterer(n,m): n**2+m return fake_sph parm = Parametrization(makeScatterer, [par(limit=4),par(2, [1,5])]) assert_equal(parm._fixed_params,{None: 4})
def test_layered(): s = Sphere(n = (1,2), r = (1, 2), center = (2, 2, 2)) sch = ImageSchema((10, 10), .2, Optics(.66, 1, (1, 0))) hs = Mie.calc_holo(s, sch) guess = hp.scattering.scatterer.sphere.LayeredSphere((1,2), (par(1.01), par(.99)), (2, 2, 2)) model = Model(guess, Mie.calc_holo) res = fit(model, hs) assert_allclose(res.scatterer.t, (1, 1), rtol = 1e-12)
def test_pullingoutguess(): g = 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(g, Mie.calc_holo) s = Sphere(center=[.567e-5, .567e-5, 15e-6], n=1.59 + 1e-4j, r=8.5e-7) assert_equal(s.n, model.scatterer.guess.n) assert_equal(s.r, model.scatterer.guess.r) assert_equal(s.center, model.scatterer.guess.center) g = 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=1.59 + 1e-4j) model = Model(g, Mie.calc_holo) s = Sphere(center=[.567e-5, .567e-5, 15e-6], n=1.59 + 1e-4j, r=8.5e-7) assert_equal(s.n, model.scatterer.guess.n) assert_equal(s.r, model.scatterer.guess.r) assert_equal(s.center, model.scatterer.guess.center)
def test_Tying(): #tied parameters n1 = par(1.59) sc = Spheres([Sphere(n = n1, r = par(0.5e-6), center = np.array([10., 10., 20.])), Sphere(n = n1, r = par(0.5e-6), center = np.array([9., 11., 21.]))]) model = Model(sc, calc_holo, alpha = par(.7,[.6,1])) assert_equal(model.parameters[0].guess, 1.59) assert_equal(model.parameters[1].guess, 5e-7) assert_equal(len(model.parameters),4)
def test_ComplexPar(): #complex parameter def makeScatterer(n): n**2 return fake_sph parm = Parametrization(makeScatterer, [ComplexParameter(real = par(1.58),imag = par(.001), name='n')]) model = Model(parm, calc_holo, alpha = par(.7,[.6,1])) assert_equal(model.parameters[0].name,'n.real') assert_equal(model.parameters[1].name,'n.imag')
def test_integer_correctness(): # we keep having bugs where the fitter doesn't schema = ImageSchema(shape = 100, spacing = .1, optics = Optics(wavelen = .660, index = 1.33, polarization = (1, 0))) s = Sphere(center = (10.2, 9.8, 10.3), r = .5, n = 1.58) holo = Mie.calc_holo(s, schema) par_s = Sphere(center = (par(guess = 10, limit = [5,15]), par(10, [5, 15]), par(10, [5, 15])), r = .5, n = 1.58) model = Model(par_s, Mie.calc_holo, alpha = par(.6, [.1, 1])) result = fit(model, holo) assert_allclose(result.scatterer.center, [10.2, 9.8, 10.3])
def test_Get_Alpha(): #checking get_alpha function sc = Spheres([Sphere(n = 1.58, r = par(0.5e-6), center = np.array([10., 10., 20.])), Sphere(n = 1.58, r = par(0.5e-6), center = np.array([9., 11., 21.]))]) model = Model(sc, calc_holo, alpha = par(.7,[.6,1])) sc = Spheres([Sphere(n = 1.58, r = par(0.5e-6), center = np.array([10., 10., 20.])), Sphere(n = 1.58, r = par(0.5e-6), center = np.array([9., 11., 21.]))]) model2 = Model(sc, calc_holo) assert_equal(model.get_alpha(model.parameters).guess, 0.7) assert_equal(model.get_alpha(model.parameters).name, 'alpha') assert_equal(model2.get_alpha(model2.parameters), 1.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)
def test_fit_complex_parameter(): ''' Test that complex parameters are handled correctly when fit. ''' # use a Sphere with complex n # a fake scattering model def scat_func(sph, schema, scaling=None): # TODO: scaling kwarg required, seems like a silly kluge def silly_function(theta): return theta * sph.r + sph.n.real * theta**2 + 2. * sph.n.imag #import pdb #pdb.set_trace() return Marray( np.array([ silly_function(theta) for theta, phi in schema.positions_theta_phi() ]), **schema._dict) # generate data schema = Schema(positions=Angles(np.linspace(0., np.pi / 2., 6))) ref_sph = Sphere(r=1.5, n=0.4 + 0.8j) data = scat_func(ref_sph, schema) # varying both real and imaginary parts par_s = Sphere(r=par(1.49), n=ComplexParameter(real=par(0.405), imag=par(0.81))) model = Model(par_s, scat_func) result = fit(model, data) assert_allclose(result.scatterer.r, ref_sph.r) assert_allclose(result.scatterer.n.real, ref_sph.n.real) assert_allclose(result.scatterer.n.imag, ref_sph.n.imag) # varying just the real part par_s2 = Sphere(r=par(1.49), n=ComplexParameter(real=par(0.405), imag=0.8)) model2 = Model(par_s2, scat_func) result2 = fit(model2, data) assert_allclose(result2.scatterer.r, ref_sph.r) assert_allclose(result2.scatterer.n.real, ref_sph.n.real) assert_allclose(result2.scatterer.n.imag, ref_sph.n.imag)
def test_serialization(): par_s = Sphere(center=(par(.567e-5, [0, 1e-5]), par(.576e-6, [0, 1e-5]), par(15e-6, [1e-5, 2e-5])), r=par(8.5e-7, [1e-8, 1e-5]), n=par(1.59, [1, 2])) alpha = par(.6, [.1, 1], 'alpha') schema = ImageSchema(shape=100, spacing=.1151e-6, optics=Optics(.66e-6, 1.33)) model = Model(par_s, Mie.calc_holo, alpha=alpha) holo = Mie.calc_holo(model.scatterer.guess, schema, model.alpha.guess) result = fit(model, holo) temp = tempfile.NamedTemporaryFile() save(temp, result) temp.flush() temp.seek(0) loaded = load(temp) assert_obj_close(result, loaded, context='serialized_result')
def test_n(): sph = Sphere(par(.5), 1.6, (5,5,5)) sch = ImageSchema(shape=[100, 100], spacing=[0.1, 0.1], optics=Optics(wavelen=0.66, index=1.33, polarization=[1, 0], divergence=0.0), origin=[0.0, 0.0, 0.0]) model = Model(sph, Mie.calc_holo, alpha=1) holo = Mie.calc_holo(model.scatterer.guess, sch) coster = CostComputer(holo, model, random_subset=.1) assert_allclose(coster.flattened_difference({'n' : .5}), 0)
def test_fit_complex_parameter(): ''' Test that complex parameters are handled correctly when fit. ''' # use a Sphere with complex n # a fake scattering model def scat_func(sph, schema, scaling = None): # TODO: scaling kwarg required, seems like a silly kluge def silly_function(theta): return theta * sph.r + sph.n.real * theta **2 + 2. * sph.n.imag #import pdb #pdb.set_trace() return Marray(np.array([silly_function(theta) for theta, phi in schema.positions_theta_phi()]), **schema._dict) # generate data schema = Schema(positions = Angles(np.linspace(0., np.pi/2., 6))) ref_sph = Sphere(r = 1.5, n = 0.4 + 0.8j) data = scat_func(ref_sph, schema) # varying both real and imaginary parts par_s = Sphere(r = par(1.49), n = ComplexParameter(real = par(0.405), imag = par(0.81))) model = Model(par_s, scat_func) result = fit(model, data) assert_allclose(result.scatterer.r, ref_sph.r) assert_allclose(result.scatterer.n.real, ref_sph.n.real) assert_allclose(result.scatterer.n.imag, ref_sph.n.imag) # varying just the real part par_s2 = Sphere(r = par(1.49), n = ComplexParameter(real = par(0.405), imag = 0.8)) model2 = Model(par_s2, scat_func) result2 = fit(model2, data) assert_allclose(result2.scatterer.r, ref_sph.r) assert_allclose(result2.scatterer.n.real, ref_sph.n.real) assert_allclose(result2.scatterer.n.imag, ref_sph.n.imag)
def test_n(): sph = Sphere(par(.5), 1.6, (5,5,5)) sch = ImageSchema(shape=[100, 100], spacing=[0.1, 0.1], optics=Optics(wavelen=0.66, index=1.33, polarization=[1, 0], divergence=0.0), origin=[0.0, 0.0, 0.0]) model = Model(sph, Mie.calc_holo, alpha=1) holo = Mie.calc_holo(model.scatterer.guess, sch) coster = CostComputer(holo, model, use_random_fraction=.1) assert_allclose(coster.flattened_difference({'n' : .5}), 0)
def test_constraint(): sch = ImageSchema(100) with warnings.catch_warnings(): # TODO: we should really only supress overlap warnings here, # but I am too lazy to figure it out right now, and I don't # think we are likely to hit warnings here that won't get # caught elsewhere -tgd 2013-12-01 warnings.simplefilter("ignore") spheres = Spheres([Sphere(r=.5, center=(0,0,0)), Sphere(r=.5, center=(0,0,par(.2)))]) model = Model(spheres, Multisphere.calc_holo, constraints=limit_overlaps()) coster = CostComputer(sch, model) cost = coster._calc({'1:Sphere.center[2]' : .2}) assert_equal(cost, np.ones_like(sch)*np.inf)
def test_dda_fit(): s = Sphere(n = 1.59, r = .2, center = (5, 5, 5)) o = Optics(wavelen = .66, index=1.33, pixel_scale=.1) schema = ImageSchema(optics = o, shape = 100) h = Mie.calc_holo(s, schema) def make_scatterer(r, x, y, z): local_s = Sphere(r = r, center = (x, y, z)) return Scatterer(local_s.indicators, n = s.n) parameters = [par(.18, [.1, .3], name='r', step=.1), par(5, [4, 6], 'x'), par(5, [4,6], 'y'), par(5.2, [4, 6], 'z')] p = Parametrization(make_scatterer, parameters) model = Model(p, DDA.calc_holo) res = fit(model, h) assert_parameters_allclose(res.parameters, OrderedDict([('r', 0.2003609439787491), ('x', 5.0128083665603995), ('y', 5.0125252883133617), ('z', 4.9775097284878775)]), rtol=1e-3)
def test_pullingoutguess(): g = 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(g, calc_holo) s = Sphere(center = [.567e-5, .567e-5, 15e-6], n = 1.59 + 1e-4j, r = 8.5e-7) assert_equal(s.n, model.scatterer.guess.n) assert_equal(s.r, model.scatterer.guess.r) assert_equal(s.center, model.scatterer.guess.center) g = 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 = 1.59 + 1e-4j) model = Model(g, calc_holo) s = Sphere(center = [.567e-5, .567e-5, 15e-6], n = 1.59 + 1e-4j, r = 8.5e-7) assert_equal(s.n, model.scatterer.guess.n) assert_equal(s.r, model.scatterer.guess.r) assert_equal(s.center, model.scatterer.guess.center)
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.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-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.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_serialization(): par_s = Sphere(center = (par(.567e-5, [0, 1e-5]), par(.576e-6, [0, 1e-5]), par(15e-6, [1e-5, 2e-5])), r = par(8.5e-7, [1e-8, 1e-5]), n = par(1.59, [1,2])) alpha = par(.6, [.1, 1], 'alpha') schema = ImageSchema(shape = 100, spacing = .1151e-6, optics = Optics(.66e-6, 1.33)) model = Model(par_s, Mie.calc_holo, alpha=alpha) holo = Mie.calc_holo(model.scatterer.guess, schema, model.alpha.guess) result = fit(model, holo) temp = tempfile.NamedTemporaryFile() save(temp, result) temp.flush() temp.seek(0) loaded = load(temp) assert_obj_close(result, loaded, context = 'serialized_result')
def test_io(): model = Model(Sphere(par(1)), calc_holo) assert_read_matches_write(model) model = Model(Sphere(par(1)), calc_holo, theory=Mie(False)) assert_read_matches_write(model)
def test_io(): model = Model(Sphere(par(1)), Mie.calc_holo) assert_read_matches_write(model) model = Model(Sphere(par(1)), Mie(False).calc_holo) assert_read_matches_write(model)
def test_model_guess(): ps = Sphere(n=par(1.59, [1.5,1.7]), r = .5, center=(5,5,5)) m = Model(ps, Mie) assert_obj_close(m.scatterer.guess, Sphere(n=1.59, r=0.5, center=[5, 5, 5]))