Ejemplo n.º 1
0
def test_log_posterior():
    warnings.simplefilter('ignore', UserWarning)
    spectrum=Spectrum(500, reflectance = 0.5, sigma_r = 0.1)
    theta = (0.5, 0, 0)
    sample = Sample(500, 200, 200, 1.5, 1)
    post = log_posterior(theta, spectrum, sample, seed=2)
    assert_approx_equal(post, -6.418847855673672)
Ejemplo n.º 2
0
def test_calc_max_like():
    warnings.simplefilter('ignore', UserWarning)
    sample = Sample([450, 500, 550],
                    particle_radius=119,
                    thickness=120,
                    particle_index=1.59,
                    matrix_index=1)
    theta = (0.55, 0, 0
             )  #these are the default starting values for lmfit calculation
    spect = calc_model_spect(sample, theta, seed=2)
    max_like_vals = find_max_like(spect, sample, seed=2)
    assert_almost_equal(max_like_vals, (0.55, 0.0, 0.0, 0.0, 0))
def test_find_max_like():
    wavelength = [450, 500, 550, 600]
    wavelength_ind = find_close_indices(wavelength_sigma,
                                        sc.Quantity(wavelength, 'nm'))
    sigma_test = sigma[np.array(wavelength_ind)]

    sample = Sample(wavelength, particle_index=1.59, matrix_index=1)
    theta = (0.55, 120, 120, 0.02, 0, 0.02, 0
             )  #these are the default starting values for lmfit calculation
    spect = calc_model_spect(sample,
                             theta, (sigma_test, sigma_test),
                             ntrajectories,
                             nevents,
                             seed=2)

    theta_range = {
        'min_phi': 0.34,
        'max_phi': 0.74,
        'min_radius': 70,
        'max_radius': 160,
        'min_thickness': 1,
        'max_thickness': 1000,
        'min_l0_r': 0,
        'max_l0_r': 1,
        'min_l1_r': -1,
        'max_l1_r': 1,
        'min_l0_t': 0,
        'max_l0_t': 1,
        'min_l1_t': -1,
        'max_l1_t': 1
    }
    theta_guess = {
        'phi': 0.55,
        'radius': 120,
        'thickness': 120,
        'l0_r': 0.02,
        'l1_r': 0,
        'l0_t': 0.02,
        'l1_t': 0
    }

    max_like_vals = find_max_like(spect,
                                  sample,
                                  theta_guess=theta_guess,
                                  theta_range=theta_range,
                                  sigma=(sigma_test, sigma_test),
                                  ntrajectories=ntrajectories,
                                  nevents=nevents,
                                  seed=2)
    assert_almost_equal(max_like_vals, theta)
Ejemplo n.º 4
0
def test_calc_model_spect():
    wavelength = [500]
    sample = Sample(wavelength, 1.5, 1)
    theta = (0.5, 100, 200, 0, 0, 0, 0)

    wavelength_ind = find_close_indices(wavelength_sigma,
                                        sc.Quantity(wavelength, 'nm'))
    sigma_test = sigma[np.array(wavelength_ind)]
    assert_frame_equal(
        calc_model_spect(sample, theta, (sigma_test, sigma_test),
                         ntrajectories, nevents, 2),
        Spectrum(500,
                 reflectance=0.828595524325,
                 sigma_r=0.0193369922424,
                 transmittance=0.171404475675,
                 sigma_t=0.0193369922424))
def test_run_mcmc():
    spectrum = Spectrum([500, 550, 600, 650],
                        reflectance=[0.5, 0.5, 0.5, 0.5],
                        transmittance=[0.5, 0.5, 0.5, 0.5],
                        sigma_r=[0.1, 0.1, 0.1, 0.1],
                        sigma_t=[0.1, 0.1, 0.1, 0.1])
    sample = Sample([500, 550, 600, 650], 1.5, 1)
    theta_guess = {
        'phi': 0.5,
        'radius': 150,
        'thickness': 120,
        'l0_r': 0,
        'l1_r': 0,
        'l0_t': 0,
        'l1_t': 0
    }
    theta_range = {
        'min_phi': 0.35,
        'max_phi': 0.73,
        'min_radius': 70,
        'max_radius': 201,
        'min_thickness': 1,
        'max_thickness': 1000,
        'min_l0_r': 0,
        'max_l0_r': 1,
        'min_l1_r': -1,
        'max_l1_r': 1,
        'min_l0_t': 0,
        'max_l0_t': 1,
        'min_l1_t': -1,
        'max_l1_t': 1
    }
    # Test that run_mcmc runs correctly
    run_mcmc(spectrum,
             sample,
             nwalkers=14,
             nsteps=1,
             theta_guess=theta_guess,
             theta_range=theta_range,
             seed=3)
Ejemplo n.º 6
0
def test_log_posterior():
    wavelength = [500]
    spectrum = Spectrum(wavelength, reflectance=0.5, sigma_r=0.1)
    sample = Sample(wavelength, 1.5, 1)
    theta_range = {
        'min_phi': 0.35,
        'max_phi': 0.74,
        'min_radius': 70,
        'max_radius': 201,
        'min_thickness': 1,
        'max_thickness': 1000
    }
    wavelength_ind = find_close_indices(wavelength_sigma,
                                        sc.Quantity(wavelength, 'nm'))
    sigma_test = sigma[np.array(wavelength_ind)]

    # When parameters are within prior range
    theta1 = (0.5, 200, 200, 0, 0)
    post1 = log_posterior(theta1,
                          spectrum,
                          sample,
                          theta_range=theta_range,
                          sigma=(sigma_test, sigma_test),
                          ntrajectories=ntrajectories,
                          nevents=nevents,
                          seed=2)
    assert_approx_equal(post1, -6.0413752765269875)

    # When parameters are not within prior range
    theta2 = (0.3, 200, 200, 0, 0)
    post2 = log_posterior(theta2,
                          spectrum,
                          sample,
                          theta_range=theta_range,
                          sigma=(sigma_test, sigma_test),
                          ntrajectories=ntrajectories,
                          nevents=nevents,
                          seed=2)
    assert_approx_equal(post2, -1e100)
Ejemplo n.º 7
0
def test_run_structcol():
    # Test that structcol package is imported and reflectance calculation is correct
    import structcol as sc
    from infer_structcol.run_structcol import calc_refl_trans
    from infer_structcol.main import Sample

    wavelength = np.array([400., 500.])
    particle_radius = 150.
    thickness = 100.
    particle_index = np.array([1.40, 1.41])
    matrix_index = np.array([1.0, 1.0])
    volume_fraction = 0.5
    incident_angle = 0.0
    medium_index = np.array([1.0, 1.0])

    spectrum = calc_refl_trans(volume_fraction,
                               Sample(wavelength, particle_radius, thickness,
                                      particle_index, matrix_index,
                                      medium_index, incident_angle),
                               seed=1)

    outarray = np.array([0.8531, 0.7529])
    assert_almost_equal(spectrum.reflectance, outarray, decimal=5)
Ejemplo n.º 8
0
def test_run_structcol():
    # Test that structcol package is imported and reflectance calculation is correct
    import structcol as sc

    wavelength = np.array([400., 500.])
    particle_radius = 150.
    thickness = 100.
    particle_index = np.array([1.40, 1.41])
    matrix_index = np.array([1.0, 1.0])
    volume_fraction = 0.5
    incident_angle = 0.0
    medium_index = np.array([1.0, 1.0])
    front_index = np.array([1.0, 1.0])
    back_index = np.array([1.0, 1.0])

    wavelength_ind = find_close_indices(wavelength_sigma,
                                        sc.Quantity(wavelength, 'nm'))
    sigma_test = sigma[np.array(wavelength_ind)]

    refl, trans = calc_refl_trans(volume_fraction,
                                  particle_radius,
                                  thickness,
                                  Sample(wavelength, particle_index,
                                         matrix_index, medium_index,
                                         front_index, back_index,
                                         incident_angle),
                                  ntrajectories,
                                  nevents,
                                  seed=1)
    spectrum = Spectrum(wavelength,
                        reflectance=refl,
                        transmittance=trans,
                        sigma_r=sigma_test,
                        sigma_t=sigma_test)

    outarray = np.array([0.84576, 0.74796])
    assert_almost_equal(spectrum.reflectance, outarray, decimal=5)
Ejemplo n.º 9
0
def test_calc_model_spect():
    sample = Sample(500, 100, 200, 1.5, 1)
    theta = (0.5, 0, 0, 0, 0)
    assert_frame_equal(calc_model_spect(sample, theta, 2), Spectrum(500, reflectance = 0.813006364656, sigma_r = 0.0226350830535, 
                       transmittance =0.186993635344, sigma_t = 0.0226350830535))
Ejemplo n.º 10
0
def test_check_wavelength():
    spect = Spectrum([300,500], reflectance = [0.5,0.5], sigma_r = [0.1,0.1])
    samp = Sample([300,500], 1.5, 1.3)
    assert_equal(check_wavelength(spect, samp), samp.wavelength)
Ejemplo n.º 11
0
def test_Sample():
    # Test if Sample object is created correctly 
    samp = Sample([300,500], 1.5, 1.3)
    assert_equal(samp.particle_index, np.array([1.5, 1.5]))
Ejemplo n.º 12
0
def test_run_mcmc():
    spectrum = Spectrum(500, reflectance=0.5, sigma_r=0.1)
    sample = Sample(500, 200, 200, 1.5, 1)
    theta = (0.5, 0, 0)
    # Test that run_mcmc runs correctly
    run_mcmc(spectrum, sample, nwalkers=6, nsteps=1, theta=theta, seed=2)