def test_parameterize_scatterer_makes_priors(self): scatterer = parameterize_scatterer(Sphere(), ['r']) self.assertTrue(isinstance(scatterer.r, prior.Prior))
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_io(): model = ExactModel(Sphere(1), calc_holo) assert_read_matches_write(model) model = ExactModel(Sphere(1), calc_holo, theory=Mie(False)) assert_read_matches_write(model)
def test_noise_from_schema(self): model = AlphaModel(Sphere(), noise_sd=None) schema = detector_grid(2, 2) schema.attrs['noise_sd'] = 0.5 found_noise = model._find_noise([], schema) self.assertEqual(found_noise, 0.5)
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_internal_scatterer_from_parameters_list(self): sphere = Sphere(n=prior.Uniform(1, 2), r=prior.Uniform(0, 1)) model = AlphaModel(sphere) pars = [1.6, 0.8] expected = Sphere(n=1.6, r=0.8) self.assertEqual(model._scatterer_from_parameters(pars), expected)
def test_add_unequal_tie_fails(self): sphere = Sphere(n=prior.Uniform(1, 2), r=prior.Uniform(0, 1), center=[10, 10, 10]) model = AlphaModel(sphere) self.assertRaises(ValueError, model.add_tie, ['r', 'n'])
from holopy.core.metadata import detector_points from holopy.scattering import (calc_holo, calc_scat_matrix, calc_cross_sections, Multisphere, Sphere, Spheres) from holopy.scattering.errors import (InvalidScatterer, TheoryNotCompatibleError, MultisphereFailure, OverlapWarning) from holopy.scattering.tests.common import (xschema, yschema, index, wavelen, xpolarization, ypolarization, scaling_alpha, sphere) from holopy.core.tests.common import assert_obj_close, verify schema = xschema SCATTERERS_LARGE_OVERLAP = [ 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), ] SCATTERERS_SMALL_OVERLAP = [ 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), ] @attr('fast') def test_construction(): # test constructor to make sure it works properly and calls base # class constructor properly theory = Multisphere(niter=100, eps=1e-6, meth=0, qeps1=1e-5, qeps2=1e-8) assert_equal(theory.niter, 100)
def test_wrap_sphere(): sphere = Sphere(center=[7.1e-6, 7e-6, 10e-6], n=1.5811 + 1e-4j, r=5e-07) sphere_w = Spheres([sphere]) holo = calc_holo(schema, sphere, theory=Multisphere, scaling=.6) holo_w = calc_holo(schema, sphere_w, theory=Multisphere, scaling=.6) assert_array_equal(holo, holo_w)
def test_cannot_sample_without_model(self): self.assertRaises(ValueError, sample, DATA, Sphere())
import xarray as xr import numpy as np from nose.plugins.attrib import attr from holopy.inference.prior import Uniform from holopy.inference.result import UncertainValue, FitResult, SamplingResult from holopy.inference import (AlphaModel, CmaStrategy, EmceeStrategy, NmpfitStrategy) from holopy.scattering import Sphere, Mie from holopy.scattering.errors import MissingParameter from holopy.core.metadata import detector_grid, update_metadata from holopy.core.tests.common import (assert_read_matches_write, get_example_data) DATA = update_metadata(detector_grid(shape=10, spacing=2), 1.33, 0.660, (0, 1)) par_s = Sphere(n=Uniform(1.5, 1.65), r=Uniform(0.5, 0.7), center=[10, 10, 10]) MODEL = AlphaModel(par_s, alpha=Uniform(0.6, 0.9, guess=0.8)) INTERVALS = [ UncertainValue(1.6, 0.1, name='n'), UncertainValue(0.6, 0.1, name='r'), UncertainValue(0.7, 0.1, name='alpha') ] def generate_fit_result(): return FitResult(DATA, MODEL, CmaStrategy(), 10, {'intervals': INTERVALS}) def generate_sampling_result(): samples = np.array([[[1, 2], [11, 12], [3, 3]], [[0, 3], [1, 3], [5, 6]]]) samples = xr.DataArray(samples,
def test_parameterize_scatterer_spheres_minval(self): sphere = Sphere(r=0.5, n=1, center=[0, 0, 0]) model = make_default_model(Spheres([sphere, sphere], warn=False)) self.assertEqual(model.parameters['0:n'].lower_bound, 0) self.assertEqual(model.parameters['1:n'].lower_bound, 0)
def test_parameterize_scatterer_center(self): fit_pars = ['center'] scatterer = parameterize_scatterer(Sphere(center=[0, 0, 0]), fit_pars) for i, coord in enumerate('xyz'): expected = prior.Uniform(-np.inf, np.inf, 0, coord) self.assertEqual(scatterer.center[i], expected)
def test_parameterize_scatterer_xyz(self): scatterer = parameterize_scatterer(Sphere(center=[0, 0, 0]), ['x']) self.assertTrue(isinstance(scatterer.center[0], prior.Prior))
import holopy as hp from holopy.scattering import calc_holo, Sphere sphere = Sphere(n=1.59, r=0.5, center=(4, 4, 5)) medium_index = 1.33 illum_wavelen = 0.660 illum_polarization = (1, 0) detector = hp.detector_grid(shape=100, spacing=0.1) holo = calc_holo(detector, sphere, medium_index, illum_wavelen, illum_polarization, theory='auto') hp.show(holo)
from nose.plugins.attrib import attr from numpy.testing import assert_equal, assert_approx_equal, assert_allclose, assert_raises from holopy.scattering import Sphere, Spheres, LayeredSphere, Mie, calc_holo from holopy.core import detector_grid, load, save, update_metadata from holopy.core.process import normalize from holopy.core.tests.common import (assert_obj_close, get_example_data, assert_read_matches_write) from holopy.scattering.errors import OverlapWarning from holopy.inference import (LimitOverlaps, ExactModel, AlphaModel, NmpfitStrategy, LeastSquaresScipyStrategy) from holopy.inference.prior import ComplexPrior, Uniform gold_alpha = .6497 gold_sphere = Sphere(1.582 + 1e-4j, 6.484e-7, (5.534e-6, 5.792e-6, 1.415e-5)) def fix_flat(result): if 'flat' in result.data.coords: result.data = result.data.rename({'flat': 'point'}) result.data['point'].values = np.arange(len(result.data.point)) return result @attr('slow') def test_fit_mie_single(): holo = normalize(get_example_data('image0001')) parameters = [ Uniform(0, 1e-5, name='x', guess=.567e-5),
def test_internal_scatterer_from_parameters_dict_fails(self): sphere = Sphere(n=prior.Uniform(1, 2), r=prior.Uniform(0, 1)) model = AlphaModel(sphere) pars = {'r': 0.8, 'n': 1.6} self.assertRaises(KeyError, model._scatterer_from_parameters, pars)
def test_model_guess(): ps = Sphere(n=Uniform(1.5, 1.7, 1.59), r=.5, center=(5, 5, 5)) m = ExactModel(ps, calc_holo) assert_obj_close(m.scatterer.guess, Sphere(n=1.59, r=0.5, center=[5, 5, 5]))
def test_initial_guess(self): sphere = Sphere(n=prior.Uniform(1, 2), r=prior.Uniform(0, 1, guess=0.8)) model = AlphaModel(sphere) self.assertEqual(model.initial_guess, {'n': 1.5, 'r': 0.8})
def make_scatterer(parlist): return Sphere(n=parlist[3], r=parlist[4], center=parlist[0:3])
def test_reads_noise_map(self): noise = {'red': 0.5, 'green': prior.Uniform(0, 1)} model = AlphaModel(Sphere(), noise_sd=noise) found_noise = model._find_noise([0.7], None) self.assertEqual(found_noise, {'red': 0.5, 'green': 0.7})
import unittest import warnings import numpy as np from nose.plugins.attrib import attr import holopy from holopy.scattering import Sphere, Mie, calc_holo from holopy.core.process import normalize from holopy.inference import (AlphaModel, LeastSquaresScipyStrategy, NmpfitStrategy) from holopy.inference.prior import Uniform SPHERE = Sphere(n=1.59, r=8e-7, center=(5.7e-6, 5.7e-6, 15e-6)) CORRECT_ALPHA = 0.7 class TestLeastSquaresScipyStrategy(unittest.TestCase): @attr("slow") def test_fit_complete_model_on_complete_data(self): data = make_fake_data() model = make_model() fitter = LeastSquaresScipyStrategy(max_nfev=25) with warnings.catch_warnings(): warnings.simplefilter('ignore') result = fitter.fit(model, data) fitted = result.scatterer self.assertTrue(np.isclose(fitted.n, SPHERE.n, rtol=1e-3)) self.assertTrue(np.isclose(fitted.r, SPHERE.r, rtol=1e-3))
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_translate(): s = Sphere(n=1.59, r=.5, center=(0, 0, 0)) s2 = s.translated(1, 1, 1) assert_equal(s.r, s2.r) assert_equal(s.n, s2.n) assert_allclose(s2.center, (1, 1, 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_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])
def make_sphere(): index = prior.Uniform(1.4, 1.6, name='n') radius = prior.Uniform(0.2, 0.8, name='r') return Sphere(n=index, r=radius)
def test_Sphere_construct_tuple(): # specify center as list center = (1e-6, -1e-6, 10e-6) s = Sphere(n=1.59 + 0.0001j, r=5e-7, center=center) assert_equal(s.center, np.array(center))
def test_scatterer_is_parameterized(self): sphere = Sphere(n=prior.Uniform(1, 2), r=prior.Uniform(0, 1)) model = AlphaModel(sphere) self.assertEqual(model.scatterer, sphere)
def test_make_default_model_construction(self): model = make_default_model(Sphere(), None) self.assertEqual(model.noise_sd, 1) self.assertTrue(isinstance(model, AlphaModel))