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 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_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 _get_quad_pts_and_scattering_matrix(theory, scatterer, medium_wavevec, medium_index): theta, phi = cartesian(theory._theta_pts.ravel(), theory._phi_pts.ravel()).T pts = detector_points(theta=theta, phi=phi) illum_wavelen = 2 * np.pi * medium_index / medium_wavevec pts = update_metadata(pts, medium_index=medium_index, illum_wavelen=illum_wavelen) matr = theory.theory.calculate_scattering_matrix(scatterer, pts) return theta, phi, np.conj(matr)
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 _calc_scattering_matrix(self, scatterer, medium_wavevec, medium_index): theta, phi = np.meshgrid(self._theta_pts, self._phi_pts) pts = detector_points(theta=theta.ravel(), phi=phi.ravel()) illum_wavelen = 2 * np.pi * medium_index / medium_wavevec pts = update_metadata(pts, medium_index=medium_index, illum_wavelen=illum_wavelen) S = self.theory.calculate_scattering_matrix(scatterer, pts) S = np.conj( S.values.reshape(self.quad_npts_theta, self.quad_npts_phi, 2, 2)) S = np.swapaxes(S, 0, 1) S1 = S[:, :, 1, 1].reshape(self.quad_npts_theta, self.quad_npts_phi, 1) S2 = S[:, :, 0, 0].reshape(self.quad_npts_theta, self.quad_npts_phi, 1) S3 = S[:, :, 0, 1].reshape(self.quad_npts_theta, self.quad_npts_phi, 1) S4 = S[:, :, 1, 0].reshape(self.quad_npts_theta, self.quad_npts_phi, 1) return S1, S2, S3, S4
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.),
# 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, r=radius, center=(x, y, z))
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)