def test_voxelated_complex(): s = Sphere(n = 1.2+2j, r = .2, center = (5,5,5)) sv = Scatterer(s.indicators, s.n, s.center) schema = detector_grid(10, .1) holo_dda = calc_holo(schema, sv, illum_wavelen=.66, medium_index=1.33, illum_polarization = (1, 0), theory=DDA) verify(holo_dda, 'dda_voxelated_complex', rtol=1e-5)
def test_calc_field(): s = Sphere(n=1.59, r=.5, center=(0, 0, 1)) t = update_metadata(detector_grid(shape=(2, 2), spacing=.1), illum_wavelen=0.66, medium_index=1.33, illum_polarization=(1, 0)) thry = Mie(False) f = calc_field(t, s, 1.33, .66, (1, 0), theory=thry) assert_obj_close(t.attrs, f.attrs) gold = xr.DataArray(np.array([[[ -3.95866810e-01 + 2.47924378e+00j, 0.00000000e+00 + 0.00000000e+00j, 0.00000000e+00 - 0.00000000e+00j ], [ -4.91260953e-01 + 2.32779296e+00j, 9.21716363e-20 - 5.72226912e-19j, 2.99878926e-18 - 1.41959276e-17j ]], [[ -4.89755627e-01 + 2.31844748e+00j, 0.00000000e+00 + 0.00000000e+00j, 4.89755627e-02 - 2.31844748e-01j ], [ -5.71886751e-01 + 2.17145168e+00j, 1.72579090e-03 - 8.72241140e-03j, 5.70160960e-02 - 2.16272927e-01j ]]]), dims=['x', 'y', 'vector'], coords={ 'x': t.x, 'y': t.y, 'vector': ['x', 'y', 'z'] }) assert abs((f - gold).max()) < 5e-9
def test_serialization(): par_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=Uniform(1, 2, 1.59)) alpha = Uniform(.1, 1, .6, 'alpha') schema = update_metadata(detector_grid(shape=100, spacing=.1151e-6), illum_wavelen=.66e-6, medium_index=1.33, illum_polarization=(1, 0)) model = AlphaModel(par_s, medium_index=schema.medium_index, illum_wavelen=schema.illum_wavelen, alpha=alpha) holo = calc_holo(schema, model.scatterer.guess, scaling=model.alpha.guess) result = fix_flat(NmpfitStrategy().fit(model, holo)) temp = tempfile.NamedTemporaryFile(suffix='.h5', delete=False) with warnings.catch_warnings(): warnings.simplefilter('ignore') save(temp.name, result) loaded = load(temp.name) assert_obj_close(result, loaded, context='serialized_result')
def test_janus(): schema = detector_grid(10, .1) s = JanusSphere_Uniform(n = [1.34, 2.0], r = [.5, .51], rotation = (0, -np.pi/2, 0), center = (5, 5, 5)) assert_almost_equal(s.index_at([5,5,5]),1.34) holo = calc_holo(schema, s, illum_wavelen=.66, medium_index=1.33, illum_polarization=(1, 0)) verify(holo, 'janus_dda')
def test_calc_holo(): s = Sphere(n=1.59, r=.5, center=(0, 0, 1)) t = detector_grid(shape=(2, 2), spacing=.1) thry = Mie(False) h = calc_holo(t, s, 1.33, .66, (1, 0), theory=thry) assert_allclose( h, np.array([[[6.51162661], [5.67743548]], [[5.63554802], [4.89856241]]]))
def test_model_optics_take_precedence(self): model = AlphaModel(Sphere(), medium_index=1.5, illum_wavelen=0.8) schema = detector_grid(2, 2) schema.attrs['illum_wavelen'] = 0.6 schema.attrs['illum_polarization'] = [1, 0] found_optics = model._find_optics([], schema) expected = {'medium_index': 1.5, 'illum_wavelen': 0.8, 'illum_polarization': [1, 0]} self.assertEqual(found_optics, expected)
def test_optics_from_schema(self): model = AlphaModel(Sphere(), medium_index=prior.Uniform(1, 2)) schema = detector_grid(2, 2) schema.attrs['illum_wavelen'] = 0.6 schema.attrs['illum_polarization'] = [1, 0] found_optics = model._find_optics([1.5], schema) expected = {'medium_index': 1.5, 'illum_wavelen': 0.6, 'illum_polarization': [1, 0]} self.assertEqual(found_optics, expected)
def test_transform_to_desired_coordinates(self): detector = detector_grid(shape=(2, 2), spacing=0.1) pos = ScatteringTheory._transform_to_desired_coordinates( detector, origin=(0, 0, 1), wavevec=2 * np.pi * 1.33 / .66) true_pos = np.transpose([[12.66157039, 0., 0.], [12.72472076, 0.09966865, 1.57079633], [12.72472076, 0.09966865, 0.], [12.78755927, 0.1404897, 0.78539816]]) self.assertTrue(np.allclose(pos, true_pos))
def test_calc_holo_theta_npts_not_equal_phi_npts(self): scatterer = test_common.sphere pts = detector_grid(shape=4, spacing=test_common.pixel_scale) pts = update_metadata(pts, illum_wavelen=test_common.wavelen, medium_index=test_common.index, illum_polarization=test_common.xpolarization) theory = Lens(LENS_ANGLE, Mie(), quad_npts_theta=8, quad_npts_phi=10) holo = calc_holo(pts, scatterer, theory=theory) self.assertTrue(True)
def setup_optics(): # set up optics class for use in several test functions global schema, wavelen, index wavelen = 658e-3 polarization = [0., 1.0] divergence = 0 pixel_scale = [.1151, .1151] index = 1.33 schema = detector_grid(12, spacing = pixel_scale) schema = update_metadata(schema, index, wavelen, polarization)
def test_propagate_e_field(): e = calc_field(detector_grid(100, 0.1), Sphere(1.59, .5, (5, 5, 5)), illum_wavelen=0.66, medium_index=1.33, illum_polarization=(1, 0), theory=Mie(False)) prop_e = propagate(e, 10) verify(prop_e, 'propagate_e_field')
def test_layered(): s = Sphere(n=(1, 2), r=(1, 2), center=(2, 2, 2)) sch = detector_grid((10, 10), .2) hs = calc_holo(sch, s, 1, .66, (1, 0)) guess = LayeredSphere((1, 2), (Uniform(1, 1.01), Uniform(.99, 1)), (2, 2, 2)) model = ExactModel(guess, calc_holo) res = NmpfitStrategy().fit(model, hs) assert_allclose(res.scatterer.t, (1, 1), rtol=1e-12)
def test_csg_dda(): s = Sphere(n = 1.6, r=.1, center=(5, 5, 5)) st = s.translated(.03, 0, 0) pacman = Difference(s, st) sch = detector_grid(10, .1) h = calc_holo(sch, pacman, 1.33, .66, illum_polarization=(0, 1)) verify(h, 'dda_csg') rotated_pac = pacman.rotated(np.pi/2, 0, 0) hr = calc_holo(sch, rotated_pac, 1.33, .66, illum_polarization=(0, 1)) verify(h/hr, 'dda_csg_rotated_div')
def test_n(): sph = Sphere(.5, 1.6, (5, 5, 5)) sch = detector_grid(shape=[100, 100], spacing=[0.1, 0.1]) model = ExactModel(sph, calc_holo, medium_index=1.33, illum_wavelen=.66, illum_polarization=(1, 0)) holo = calc_holo(sch, model.scatterer.guess, 1.33, .66, (1, 0)) assert_allclose(model._residuals({'n': .5}, holo, 1).sum(), 0)
def test_calc_intensity(): s = Sphere(n=1.59, r=.5, center=(0, 0, 1)) t = detector_grid(shape=(2, 2), spacing=.1) thry = Mie(False) i = calc_intensity(t, s, illum_wavelen=.66, medium_index=1.33, illum_polarization=(1, 0), theory=thry) assert_allclose( i, np.array([[[6.30336023], [5.65995739]], [[5.61505927], [5.04233591]]]))
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)
def test_Ellipsoid_dda(): e = Ellipsoid(1.5, r = (.5, .1, .1), center = (1, -1, 10)) schema = detector_grid(10, .1) try: h = calc_holo(schema, e, illum_wavelen=.66, medium_index=1.33, illum_polarization = (1,0), theory=DDA(use_indicators=False)) cmd = DDA()._adda_predefined( e, medium_wavelen=.66, medium_index=1.33, temp_dir='temp_dir') cmdlist = ['-eq_rad', '0.5', '-shape', 'ellipsoid', '0.2', '0.2', '-m', '1.1278195488721805', '0.0', '-orient', '0.0', '0.0', '0.0'] assert_equal(cmd, cmdlist) verify(h, 'ellipsoid_dda') except DependencyMissing: raise SkipTest()
def test_integer_correctness(): # we keep having bugs where the fitter doesn't schema = detector_grid(shape=10, spacing=1) s = Sphere(center=(10.2, 9.8, 10.3), r=.5, n=1.58) holo = calc_holo(schema, s, illum_wavelen=.660, medium_index=1.33, illum_polarization=(1, 0)) par_s = Sphere(r=.5, n=1.58, center=(Uniform(5, 15), Uniform(5, 15), Uniform(5, 15))) model = AlphaModel(par_s, alpha=Uniform(.1, 1, .6)) result = NmpfitStrategy().fit(model, holo) assert_allclose(result.scatterer.center, [10.2, 9.8, 10.3])
def test_predefined_scatterers(): # note this tests only that the code runs, not that it is correct try: scatterers = [Ellipsoid(n=1.5, r=(0.5, 1, 2), center=(0,0,1)), Spheroid(n=1.5, r=(0.5, 1), center=(0,0,1)), Capsule(n=1.5, h=1, d=0.5, center=(0,0,1)), Cylinder(n=1.5, h=1, d=0.5, center=(0,0,1)), Bisphere(n=1.5, h=1, d=0.5, center=(0,0,1)), Sphere(n=1.5, r=1, center=(0,0,1))] detector = detector_grid(5, .1) for s in scatterers: calc_holo(detector, s, illum_wavelen=.66, medium_index=1.33, illum_polarization = (1,0), theory=DDA(use_indicators=False)) except DependencyMissing: raise SkipTest
def test_Shell(): s = Sphere( center=[7.141442573813124, 7.160766866147957, 11.095409800342143], n=[(1.27121212428 + 0j), (1.49 + 0j)], r=[0.960957713253 - 0.0055, 0.960957713253]) t = detector_grid(200, .071333) thry = Mie(False) h = calc_holo(t, s, 1.36, .658, illum_polarization=(1, 0), theory=thry, scaling=0.4826042444701572) verify(h, 'shell')
def test_optimization_with_maxiter_of_2(): gold_fit_dict = { '0:r': 0.52480509800531849, '1:center.1': 14.003687569304704, 'alpha': 0.93045027963762217, '0:center.2': 19.93177549652841, '1:r': 0.56292664494653732, '0:center.1': 15.000340621607815, '1:center.0': 14.020984607646726, '0:center.0': 15.000222185576494, '1:center.2': 20.115613202192328 } #calculate a hologram with known particle positions to do a fit against schema = detector_grid(shape=100, spacing=.1) s1 = Sphere(center=(15, 15, 20), n=1.59, r=0.5) s2 = Sphere(center=(14, 14, 20), n=1.59, r=0.5) cluster = Spheres([s1, s2]) holo = calc_holo(schema, cluster, 1.33, .66, illum_polarization=(1, 0)) #trying to do a fast fit: guess1 = Sphere(center=(prior.Uniform(5, 25, guess=15), prior.Uniform(5, 25, 15), prior.Uniform(5, 25, 20)), r=(prior.Uniform(.4, .6, guess=.45)), n=1.59) guess2 = Sphere(center=(prior.Uniform(5, 25, guess=14), prior.Uniform(5, 25, 14), prior.Uniform(5, 25, 20)), r=(prior.Uniform(.4, .6, guess=.45)), n=1.59) par_s = Spheres([guess1, guess2]) model = AlphaModel(par_s, medium_index=1.33, illum_wavelen=.66, illum_polarization=(1, 0), alpha=prior.Uniform(.1, 1, .6)) optimizer = Nmpfit(maxiter=2) with warnings.catch_warnings(): warnings.simplefilter('ignore') result = optimizer.fit(model, holo) assert_obj_close(gold_fit_dict, result.parameters, rtol=1e-5)
import warnings from nose.plugins.attrib import attr from holopy.scattering import (Sphere, Spheres, Mie, Multisphere, Spheroid, Cylinder, Tmatrix) from holopy.core import detector_grid from holopy.core.tests.common import assert_obj_close from holopy.scattering.interface import * from holopy.scattering.errors import MissingParameter import xarray as xr SCATTERER = Sphere(n=1.6, r=.5, center=(5, 5, 5)) MED_INDEX = 1.33 LOCATIONS = detector_grid(shape=(2, 2), spacing=1) WAVELEN = 0.66 POL = (0, 1) class TestInterface(unittest.TestCase): @attr('fast') def test_calc_holo(self): # FIXME: Test results change when 'auto' theory for SCATTERER changes result = calc_holo(LOCATIONS, SCATTERER, MED_INDEX, WAVELEN, POL) expected = np.array([[1.03670094, 1.05260144], [1.04521558, 1.01477807]]) self.assertTrue(np.allclose(result.values.squeeze(), expected)) @attr('medium') def test_calc_field(self):
import yaml from nose.plugins.attrib import attr from nose.plugins.skip import SkipTest import holopy as hp from holopy.scattering import ( Tmatrix, DDA, Sphere, Spheroid, Ellipsoid, Cylinder, calc_holo) from holopy.scattering.theory import Mie from holopy.core.errors import DependencyMissing from holopy.core import detector_grid, update_metadata from holopy.scattering.theory.tmatrix_f.S import ampld SCHEMA = update_metadata( detector_grid(shape=20, spacing=0.1), illum_wavelen=.660, medium_index=1.33, illum_polarization=[1, 0]) MISHCHENKO_PARAMS = OrderedDict(( ('axi', 10.0), ('rat', 0.1), ('lam', 2 * np.pi), ('mrr', 1.5), ('mri', 0.02), ('eps', 0.5), ('np', -1), ('ndgs', 2), ('alpha', 145.), ('beta', 52.), ('thet0', 56.), ('thet', 65.),
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with HoloPy. If not, see <http://www.gnu.org/licenses/>. from holopy.core import detector_grid, update_metadata from holopy.scattering.scatterer import Sphere wavelen = 658e-9 ypolarization = [0., 1.0] # y-polarized xpolarization = [1.0, 0.] # x-polarized pixel_scale = [.1151e-6, .1151e-6] index = 1.33 xschema = update_metadata(detector_grid(shape=128, spacing=pixel_scale), illum_wavelen=wavelen, medium_index=index, illum_polarization=xpolarization) yschema = update_metadata(xschema, illum_polarization=ypolarization) scaling_alpha = .6 radius = .85e-6 n_particle_real = 1.59 n_particle_imag = 1e-4 n = n_particle_real + n_particle_imag * 1.0j x = .576e-05 y = .576e-05 z = 15e-6 sphere = Sphere(n=n_particle_real + n_particle_imag * 1j,
def test_model_noise_takes_precedence(self): model = AlphaModel(Sphere(), noise_sd=0.8) schema = detector_grid(2, 2) schema.attrs['noise_sd'] = 0.5 found_noise = model._find_noise([], schema) self.assertEqual(found_noise, 0.8)
def test_no_noise_if_all_uniform(self): sphere = Sphere(r=prior.Uniform(0, 1), n=prior.Uniform(1, 2)) model = AlphaModel(sphere) schema = detector_grid(2, 2) found_noise = model._find_noise([0.5, 0.5], schema) self.assertEqual(found_noise, 1)
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)
def test_missing_optics(self): model = AlphaModel(Sphere(), medium_index=1.5, illum_wavelen=0.8) schema = detector_grid(2, 2) schema.attrs['illum_wavelen'] = 0.6 self.assertRaises(MissingParameter, model._find_optics, [], schema)
def test_sphere_nocenter(): sphere = Sphere(n=1.59, r=.5) schema = detector_grid(spacing=.1, shape=1) assert_raises(MissingParameter, calc_holo, schema, sphere, 1.33, .66, [1, 0])
from holopy.core import detector_points, update_metadata, detector_grid from holopy.scattering import calc_holo, Sphere, Spheres from holopy.scattering.theory import Mie, MieLens, Multisphere from holopy.scattering.theory import lens from holopy.scattering.theory.lens import Lens from holopy.scattering.theory.mielensfunctions import MieLensCalculator import holopy.scattering.tests.common as test_common LENS_ANGLE = 1. QLIM_TOL = {'atol': 1e-2, 'rtol': 1e-2} LENSMIE = Lens(lens_angle=LENS_ANGLE, theory=Mie(False, False)) LENSMIE_NO_NE = Lens(lens_angle=LENS_ANGLE, theory=Mie(False, False), use_numexpr=False) SMALL_DETECTOR = update_metadata(detector_grid( shape=16, spacing=test_common.pixel_scale), illum_wavelen=test_common.wavelen, medium_index=test_common.index, illum_polarization=test_common.xpolarization) class TestLens(unittest.TestCase): def test_can_handle(self): theory = LENSMIE self.assertTrue(theory._can_handle(test_common.sphere)) def test_theta_quad_pts_min(self): assert_allclose(np.min(LENSMIE._theta_pts), 0., **QLIM_TOL) def test_theta_quad_pts_max(self): assert_allclose(np.max(LENSMIE._theta_pts), LENS_ANGLE, **QLIM_TOL)