def test_reconstruction(): im = get_example_data('image0003') rec = propagate(im, 4e-6) verify(rec, 'recon_single') rec = propagate(im, [4e-6, 7e-6, 10e-6]) verify(rec, 'recon_multiple')
def test_overlap(): # should raise a warning with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") sc = Spheres(scatterers=[ Sphere(center=[3e-6, 3e-6, 10e-6], n=1.59, r=.5e-6), Sphere(center=[3.4e-6, 3e-6, 10e-6], n=1.59, r=.5e-6) ]) assert len(w) > 0 # should fail to converge warnings.simplefilter("always") assert_raises(MultisphereFailure, calc_holo, schema, sc, index, wavelen, xpolarization) # but it should succeed with a small overlap, after raising a warning with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") sc = Spheres(scatterers=[ Sphere(center=[3e-6, 3e-6, 10e-6], n=1.59, r=.5e-6), Sphere(center=[3.9e-6, 3.e-6, 10e-6], n=1.59, r=.5e-6) ]) assert len(w) > 0 holo = calc_holo(schema, sc, index, wavelen, xpolarization) verify(holo, '2_sphere_allow_overlap')
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_Mie_multiple(): s1 = Sphere(n = 1.59, r = 5e-7, center = (1e-6, -1e-6, 10e-6)) s2 = Sphere(n = 1.59, r = 1e-6, center=[8e-6,5e-6,5e-6]) s3 = Sphere(n = 1.59+0.0001j, r = 5e-7, center=[5e-6,10e-6,3e-6]) sc = Spheres(scatterers=[s1, s2, s3]) thry = Mie(False) schema = yschema fields = calc_field(schema, sc, index, wavelen, ypolarization, thry) verify(fields, 'mie_multiple_fields') calc_intensity(schema, sc, index, wavelen, ypolarization, thry) holo = calc_holo(schema, sc, index, wavelen, theory=thry) verify(holo, 'mie_multiple_holo') # should throw exception when fed a ellipsoid el = Ellipsoid(n = 1.59, r = (1e-6, 2e-6, 3e-6), center=[8e-6,5e-6,5e-6]) with assert_raises(TheoryNotCompatibleError) as cm: calc_field(schema, el, index, wavelen, theory=Mie) assert_equal(str(cm.exception), "Mie scattering theory can't handle " "scatterers of type Ellipsoid") assert_raises(TheoryNotCompatibleError, calc_field, schema, el, index, wavelen, xpolarization, Mie) assert_raises(TheoryNotCompatibleError, calc_intensity, schema, el, index, wavelen, xpolarization, Mie) assert_raises(TheoryNotCompatibleError, calc_holo, schema, el, index, wavelen, xpolarization, Mie)
def test_overlap(): # should raise a warning with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") sc = Spheres(scatterers=[Sphere(center=[3e-6, 3e-6, 10e-6], n=1.59, r=.5e-6), Sphere(center=[3.4e-6, 3e-6, 10e-6], n=1.59, r=.5e-6)]) assert len(w) > 0 # should fail to converge warnings.simplefilter("always") assert_raises(MultisphereFailure, calc_holo, schema, sc, index, wavelen, xpolarization) # but it should succeed with a small overlap, after raising a warning with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") sc = Spheres(scatterers=[Sphere(center=[3e-6, 3e-6, 10e-6], n=1.59, r=.5e-6), Sphere(center=[3.9e-6, 3.e-6, 10e-6], n=1.59, r=.5e-6)]) assert len(w) > 0 holo = calc_holo(schema, sc, index, wavelen, xpolarization) verify(holo, '2_sphere_allow_overlap')
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_propagate_0_distance(): im = get_example_data('image0003') rec = propagate(im, 0) # propagating no distance should leave the image unchanged assert_obj_close(im, rec) rec = propagate(im, [0, 3e-6]) verify(rec, 'recon_multiple_with_0')
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_csg_dda(): s = Sphere(n = 1.6, r=.1, center=(5, 5, 5)) st = s.translated(.03, 0, 0) pacman = Difference(s, st) sch = ImageSchema(10, .1, Optics(.66, 1.33, (0, 1))) h = DDA.calc_holo(pacman, sch) verify(h, 'dda_csg') hr = DDA.calc_holo(pacman.rotated(np.pi/2, 0, 0), sch) rotated_pac = pacman.rotated(np.pi/2, 0, 0) verify(h/hr, 'dda_csg_rotated_div')
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_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_single_sphere(): # single sphere hologram (only tests that functions return) thry = Mie(False) holo = calc_holo(xschema, sphere, index, wavelen, xpolarization, theory=thry, scaling=scaling_alpha) field = calc_field(xschema, sphere, index, wavelen, xpolarization, theory=thry) intensity = calc_intensity(xschema, sphere, medium_index=index, illum_wavelen=wavelen, illum_polarization=xpolarization, theory=thry) verify(holo, 'single_holo') verify(field, 'single_field') # now test some invalid scatterers and confirm that it rejects calculating # for them # large radius (calculation not attempted because it would take forever assert_raises(InvalidScatterer, calc_holo, xschema, Sphere(r=1, n = 1.59, center = (5,5,5)), medium_index=index, illum_wavelen=wavelen)
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_ps(): imagepath = get_example_data_path('ps_image01.jpg') bgpath = get_example_data_path('ps_bg01.jpg') L = 0.0407 # distance from light source to screen cam_spacing = 12e-6 # linear size of camera pixels mag = 9.0 # magnification npix_out = 1020 # linear size of output image (pixels) zstack = [1.08e-3, 1.18e-3] # distances from camera to reconstruct holo = load_image(imagepath, spacing=cam_spacing, illum_wavelen=406e-9, medium_index=1) # load hologram bg = load_image(bgpath, spacing=cam_spacing) # load background image holo = bg_correct(holo, bg+1, bg) # subtract background holo = subimage(holo,[250,500],300) beam_c = center_of_mass(bg.values.squeeze()) # get beam center simplefilter('ignore') recons = ps_propagate(holo, zstack, L, beam_c) # do propagation verify(recons, 'ps_recon')
def test_calc_holo_succeeds_but_warns_with_small_overlap(self): with warnings.catch_warnings(): warnings.simplefilter("ignore") sc = Spheres(scatterers=SCATTERERS_SMALL_OVERLAP) holo = calc_holo(schema, sc, index, wavelen, xpolarization) verify(holo, '2_sphere_allow_overlap')
def test_farfield_matr(): schema = detector_points(theta = np.linspace(0, np.pi/2), phi = np.linspace(0, 1)) sphere = Sphere(r = .5, n = 1.59+0.1j) matr = calc_scat_matrix(schema, sphere, index, .66) verify(matr, 'farfield_matricies', rtol = 1e-6)
def test_gradient_filter(): im = get_example_data('image0003') rec = propagate(im, [4e-6, 7e-6, 10e-6], gradient_filter=1e-6) verify(rec, 'recon_multiple_gradient_filter')