def test_get_nmax(): """Test for method to get nmax""" # setup nsamples = cs.pars['nsamples'] norders = cs.pars['norders'] mod = asymp_spec_model(cs.st.f, norders) dnu, numax, eps = 10**cs.pars['asypars'][0], 10**cs.pars['asypars'][ 1], cs.pars['asypars'][2] func = mod._get_nmax inp = [dnu, numax, eps] inp_arr = [ np.float64(dnu).repeat(nsamples), np.float64(numax).repeat(nsamples), np.float64(eps).repeat(nsamples) ] # simple tests pbt.does_it_run(func, inp) pbt.does_it_return(func, inp) pbt.right_type(func, inp, float) pbt.right_type(func, inp_arr, np.ndarray) pbt.right_shape(func, inp, ()) pbt.right_shape(func, inp_arr, (nsamples, )) # check that function returns the same as during testing given the above # inputs assert (func(*inp) == 0.0)
def test_asymptotic_relation(): """Test for method to compute frequencies from the asymptotic relation""" # setup norders = cs.pars['norders'] nsamples = cs.pars['nsamples'] mod = asymp_spec_model(cs.st.f, norders) dnu, numax, eps, alpha = 3050.0, 135.0, 1.45, 10**-2.5 # roughly solar func = mod._asymptotic_relation inp = [dnu, numax, eps, alpha, norders] inp_arr = [ np.float64(numax).repeat(nsamples), np.float64(dnu).repeat(nsamples), np.float64(eps).repeat(nsamples), np.float64(alpha).repeat(nsamples), norders ] # simple tests pbt.does_it_run(func, inp) pbt.does_it_return(func, inp) pbt.right_type(func, inp, np.ndarray) pbt.right_shape(func, inp, (norders, )) pbt.right_shape(func, inp_arr, (norders, nsamples)) # check that function returns the same as during testing given the above # inputs assert_allclose(func(*inp), [2896.028668, 3030.75434], atol=0.001)
def test_outpath(): """Tests for the function that sets the output filename Notes ----- If adding more star inits, remember to os.rmdir(st.path) to clean up after the test. """ # setup pg = lk.periodogram.Periodogram(np.array([1,1])*units.microhertz, units.Quantity(np.array([1,1]), None)) st = star('thisisatest', pg, (220.0, 3.0), (16.97, 0.05), (4750, 250), (1.34, 0.1)) func = st._outpath inp = ['test.png'] # simple tests pbt.does_it_run(func, inp) pbt.does_it_return(func, inp) pbt.right_type(func, inp, str) # Some input tests assert(os.path.isdir(os.path.dirname(func(*inp)))) # cleanup os.rmdir(st.path)
def test_get_priorpath(): """Tests the function for getting the default prior_data filepath""" # setup func = get_priorpath # simple tests pbt.does_it_run(func, None) pbt.does_it_return(func, None) pbt.right_type(func, None, str) # check some inputs assert ('prior_data.csv' in func()) assert ('data' in func())
def test_get_asy_start(): """Test for getting starting location of asy fit""" # setup func = cs.st.asy_fit._get_asy_start # simple tests pbt.does_it_run(func, None) pbt.does_it_return(func, None) pbt.right_type(func, None, list) pbt.right_shape(func, None, (10, )) # check function returns expected values assert_allclose(func(), [10, 10, 1, 10, 10, 1, 1, 1, 10, 1])
def test_get_freq_range(): """Test for getting frequency range of modes""" # setup func = cs.st.asy_fit._get_freq_range # simple tests pbt.does_it_run(func, None) pbt.does_it_return(func, None) pbt.right_type(func, None, tuple) pbt.right_shape(func, None, (2, )) # check function returns expected values assert_allclose(func(), [-12.5, 22.5])
def test_prior_function(): """Tests for the prior function used by asy_fit""" # setup st = cs.st func = st.asy_fit.prior inp = [cs.pars['asypars']] # simple tests pbt.does_it_return(func, inp) pbt.right_type(func, inp, float) # Test that the prior function returns -inf given the above values as # during testing assert (func(*inp) == -np.inf)
def test_likelihood_function(): """Tests for the likelihood function used by asy_fit""" # setup st = cs.st func = st.asy_fit.likelihood inp = [cs.pars['asypars']] # simple tests pbt.does_it_return(func, inp) pbt.right_type(func, inp, float) # Test that the likelihood function returns nan given the above values as # during testing assert (np.isnan(func(*inp)))
def test_normal(): """Test for the log of a normal distribution""" # setup func = normal inp = [0, 0, 1] inp_arr = [np.linspace(-10, 10, 100), 0, 1] # simple tests pbt.does_it_run(func, inp) pbt.does_it_return(func, inp) pbt.right_type(func, inp, float) pbt.right_shape(func, inp_arr, (len(inp_arr[0]), )) # check height at mean is correct assert (10**func(*inp) == 1)
def test_asymp_spec_model_call(): """Test call method for asymptotic relation spectrum model class""" # setup norders = cs.pars['norders'] mod = asymp_spec_model([0.5, 1], norders) func = mod.model inp = cs.pars['asypars'] # simple tests pbt.does_it_run(func, inp) pbt.does_it_return(func, inp) pbt.right_type(func, inp, np.ndarray) pbt.right_shape(func, inp, np.shape(cs.st.f)) # check that the function doesn't change the output assert_allclose(mod(inp), mod.model(*inp))
def test_prior(): """Tests for kde.prior""" # Setup simp_kde.kde = type('kde', (object,), {})() simp_kde.kde.samples = np.ones((2,10)) data = np.array(solar_p.repeat(11).reshape((10,-1))) simp_kde.kde = sm.nonparametric.KDEMultivariate(data=data, var_type='cccccccccc', bw='scott') func = simp_kde.prior inp = [solar_p] # basic run/return/type/shape tests pbt.does_it_return(func, inp) pbt.right_type(func, inp, float) # Should be nan given the dummy inputs above assert(np.isnan(func(*inp)))
def test_to_log10(): """Test for the log of a normal distribution""" # setup func = to_log10 inp = [10, 1] # simple tests pbt.does_it_run(func, inp) pbt.does_it_return(func, inp) pbt.right_type(func, inp, list) # check some inputs assert (func(*inp)[0] == 1) assert_almost_equal(func(*inp)[1], 0.043429, 5) inp = [1e5, 1e4] assert (func(*inp)[0] == 5)
def test_get_modeIDs(): """Test for function that gets the mode ID from asy_fit""" # setup st = cs.st norders = cs.pars['norders'] func = st.asy_fit.get_modeIDs # simple tests pbt.does_it_return(func, [st.asy_fit.fit, norders]) pbt.right_type(func, [st.asy_fit.fit, norders], pd.DataFrame) # check that median absolute deviation is zero for all parameters df = st.asy_fit.get_modeIDs(st.asy_fit.fit, norders) assert (all(df['nu_mad'].values == np.zeros(2 * norders))) # check that median values are the same as for test setup assert_allclose(df['nu_med'], np.array([89.05, 90.05, 99., 100.]))
def test_model(): """Test for method to compute the total asymptotic relation model""" # setup norders = cs.pars['norders'] mod = asymp_spec_model([0.5, 1], norders) func = mod.model inp = cs.pars['asypars'] # simple tests pbt.does_it_run(func, inp) pbt.does_it_return(func, inp) pbt.right_type(func, inp, np.ndarray) pbt.right_shape(func, inp, np.shape(cs.st.f)) # check that function returns the same as during testing given the above # inputs assert_allclose(func(*inp), [7.93069307, 7.73076923], atol=0.001)
def test_lor(): """Test for method to compute the lorentzian profiles""" # setup norders = cs.pars['norders'] mod = asymp_spec_model(cs.st.f, norders) h, freq, w = 1, cs.st.f[0], 1 func = mod._lor inp = [freq, h, w] # simple tests pbt.does_it_run(func, inp) pbt.does_it_return(func, inp) pbt.right_type(func, inp, np.ndarray) pbt.right_shape(func, inp, np.shape(cs.st.f)) # check that function returns the same as during testing given the above # inputs assert_array_equal(func(*inp), [1, 1])
def test_pair(): """Test for method to compute the mode pairs""" # setup norders = cs.pars['norders'] mod = asymp_spec_model([0.5, 1], norders) h, freq, w, d02 = 1, cs.st.f[0], 1, 0.5 func = mod._pair inp = [h, freq, w, d02] # simple tests pbt.does_it_run(func, inp) pbt.does_it_return(func, inp) pbt.right_type(func, inp, np.ndarray) pbt.right_shape(func, inp, np.shape(cs.st.f)) # check that function returns the same as during testing given the above # inputs assert_array_equal(func(*inp), [1.2, 1.35])
def test_P_envelope(): """Test for method to get height of pmode envelope""" # setup norders = cs.pars['norders'] mod = asymp_spec_model(cs.st.f, norders) nus = cs.pars['freqs'] envheight, numax, envwidth = 10**1.5, nus[0], 10**2.2 func = mod._P_envelope inp = [nus, envheight, numax, envwidth] # simple tests pbt.does_it_run(func, inp) pbt.does_it_return(func, inp) pbt.right_type(func, inp, np.ndarray) pbt.right_shape(func, inp, np.shape(nus)) # check that function returns the same as during testing given the above # inputs assert_allclose(func(*inp), [31.6227766, 31.5718312], atol=0.001)
def test_get_percentiles(): """Tests the function for getting the percentiless of a distribution""" # setup func = get_percentiles inp = [np.random.normal(0, 1, size=10), 3] #print(func(*inp)[]) # simple tests pbt.does_it_run(func, inp) pbt.does_it_return(func, inp) pbt.right_type(func, inp, np.ndarray) pbt.right_shape(func, inp, (2 * inp[1] + 1, )) # check some different inputs inp = [np.random.normal(0, 1, size=30000), 5] pbt.right_shape(func, inp, (2 * inp[1] + 1, )) inp = [[0, 0, 0, 1, 1], 1] assert_array_equal(func(*inp), [0., 0., 1.])
def test_get_enns(): """Test for method to get the radial orders in the asymptotic relation""" # setup norders = cs.pars['norders'] nmax = cs.pars['nmax'] nsamples = cs.pars['nsamples'] mod = asymp_spec_model(cs.st.f, norders) func = mod._get_enns inp = [nmax, norders] inp_arr = [np.array([nmax]).repeat(nsamples), norders] # simple tests pbt.does_it_run(func, inp) pbt.does_it_return(func, inp) pbt.right_type(func, inp, np.ndarray) pbt.right_shape(func, inp, (norders, )) pbt.right_shape(func, inp_arr, (nsamples, norders)) # check that function returns the same as during testing given the above # inputs assert_array_equal(func(*inp), [0, 1])
def test_asymp_spec_model_call(): """Test call method for asymptotic relation spectrum model class""" # setup norders = cs.pars['norders'] mod = asymp_spec_model([0.5, 1], norders) func = mod.model inp = cs.pars['asypars'] # simple tests pbt.does_it_run(func, inp) pbt.does_it_return(func, inp) pbt.right_type(func, inp, np.ndarray) pbt.right_shape(func, inp, np.shape(cs.st.f)) # check that the function doesn't change the output assert_allclose(mod(inp), mod.model(*inp)) # The test functions below require longer runs and are not suitable for GitHub # workflows. the mark.slow decorator doesn't seem to work with GitHub workflows. # #def test_asymptotic_fit_call():
def test_likelihood(): """Tests for the kde.likelihood method""" # Setup simp_kde.kde = type('kde', (object,), {})() simp_kde.kde.samples = np.ones((2,10)) simp_kde._obs = {'dnu': [solar_p[0],0.1], 'numax': [solar_p[1],0.1], 'teff': [solar_p[-2],0.1], 'bp_rp': [solar_p[-1],0.1]} simp_kde._log_obs = {x: to_log10(*simp_kde._obs[x]) for x in simp_kde._obs.keys() if x != 'bp_rp'} # This is a silly KDE example that should return nonsense data = np.array(solar_p.repeat(11).reshape((10,-1))) simp_kde.kde = sm.nonparametric.KDEMultivariate(data=data, var_type='cccccccccc', bw='scott') func = simp_kde.likelihood inp = [solar_p] # basic run/return/type/shape tests pbt.does_it_return(func, inp) pbt.right_type(func, inp, float) # Should return real values < 0 for the dummy inputs above. assert(np.isreal(func(*inp))) assert(func(*inp) < 0)
def test_prior_size_check(): """Tests for kde.prior_size_check""" # Setup func = simp_kde._prior_size_check # Simple return test pbt.does_it_return(func, [pdata, to_log10(30, 10), 100]) # These combinations should be OK for KDEsize in [10, 20]: for numax in [30, 200]: for sigma in [10, 100]: pdata_cut = func(pdata, to_log10(numax, sigma), KDEsize) assert((len(pdata_cut) > 0) & (len(pdata_cut) <= KDEsize)) # These combinations should show warnings with pytest.warns(UserWarning): func(pdata, to_log10(300, 1), 500) # These combinations should raise errors with pytest.raises(ValueError): func(pdata, to_log10(300000, 1), 500)
def test_get_summary_stats(): """Test for method for getting summary stats from asy_fit""" # setup st = cs.st func = st.asy_fit._get_summary_stats #(st.asy_fit.fit) inp = [st.asy_fit.fit] # simple tests pbt.does_it_return(func, inp) pbt.right_type(func, inp, pd.DataFrame) pbt.right_shape(func, inp, (10, 9)) out = func(*inp) # check that function returns the same as during testing given the above # inputs for key in ['std', 'skew', 'MAD']: assert_array_equal(out.loc[:, key], 0) # same as above for key in ['mean', '2nd', '16th', '50th', '84th', '97th']: assert_array_equal(out.loc[:,key], np.array([ 1., 2., 1., 0., -2., 1., 1., 1., 1., 1.]))
def test_kde_predict(): """Tests for the kde.kde_predict method""" # Test raise statement with pytest.raises(ValueError): kde().kde_predict(8) # Setup err = 0.01 simp_kde.samples = np.vstack([np.random.normal(m, err*abs(m), 50) for m in solar_p]).T enns = range(15,25) func = simp_kde.kde_predict inp = [enns] # basic run/return/type/shape tests pbt.does_it_run(func, inp) pbt.does_it_return(func, inp) pbt.right_type(func, inp, tuple) pbt.right_shape(func, inp, (2, 10)) # Test that errors propogate more or less correctly (probably not the most # robust test) out = simp_kde.kde_predict(enns) assert_almost_equal(out[1]/out[0], np.ones_like(out[1])*err, decimal = 1)