-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests of pykima.keplerian and of default prior example
- Loading branch information
Showing
5 changed files
with
138 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
__all__ = ['keplerian', 'true_anomaly', 'ecc_anomaly'] | ||
|
||
import numpy as np | ||
pi = np.pi | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import pytest | ||
def pytest_addoption(parser): | ||
parser.addoption("--slow", action="store_true", | ||
default=False, help="run slow tests") | ||
|
||
def pytest_collection_modifyitems(config, items): | ||
if config.getoption("--slow"): | ||
# --runslow given in cli: do not skip slow tests | ||
return | ||
skip_slow = pytest.mark.skip(reason="need --slow option to run") | ||
for item in items: | ||
if "slow" in item.keywords: | ||
item.add_marker(skip_slow) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import pytest | ||
import numpy as np | ||
import numpy.testing as npt | ||
|
||
def test_import(): | ||
import pykima | ||
|
||
def test_true_ecc_anomaly(): | ||
from pykima.keplerian import true_anomaly, ecc_anomaly | ||
|
||
# true anomaly f | ||
npt.assert_allclose(true_anomaly(0., 0.), 0.0) | ||
npt.assert_allclose(true_anomaly(np.pi, 0.), np.pi) | ||
|
||
# eccentric anomaly E | ||
npt.assert_allclose(ecc_anomaly(0., 0.), 0.) | ||
npt.assert_allclose(ecc_anomaly(np.pi, 0.), np.pi) | ||
npt.assert_allclose(ecc_anomaly(2*np.pi, 0.), 2*np.pi) | ||
npt.assert_allclose(ecc_anomaly(0., 0.2), 0.) | ||
# checked with http://orbitsimulator.com/sheela/kepler.htm | ||
E = np.rad2deg(ecc_anomaly(np.deg2rad(80), 0.2)) | ||
npt.assert_allclose(E, 91.45545886486815, rtol=1e-8) | ||
|
||
def test_keplerian(): | ||
from pykima.keplerian import keplerian | ||
# keplerian(time, p, k, ecc, omega, t0, vsys) | ||
|
||
times1 = [0.,] | ||
times2 = [0., 1., 2.] | ||
|
||
with pytest.warns(RuntimeWarning): | ||
result = keplerian(times1, 0., 0., 0., 0., 0., 0.) | ||
assert np.isnan(result) | ||
|
||
result = keplerian(times2, 0., 0., 0., 0., 0., 0.) | ||
assert np.all(np.isnan(result)) | ||
|
||
npt.assert_allclose(keplerian(times1, 100., 1., 0., 0., 0., 0.), 1.) | ||
npt.assert_allclose(keplerian(times2, 1., 1., 0., 0., 0., 0.), 1.) | ||
|
||
|
||
|
||
def test_percentiles(): | ||
from pykima.display import percentile68_ranges, percentile68_ranges_latex | ||
|
||
a = np.random.uniform(size=int(1e6)) | ||
b = np.random.randn(int(1e6)) | ||
|
||
ra = percentile68_ranges(a) | ||
rb = percentile68_ranges(b) | ||
|
||
npt.assert_allclose(ra[0], np.median(a)) | ||
npt.assert_allclose(ra[0]+ra[1], np.percentile(a, 84)) | ||
npt.assert_allclose(ra[0]-ra[2], np.percentile(a, 16)) | ||
npt.assert_allclose(ra[0], 0.5, rtol=1e-2) | ||
|
||
npt.assert_allclose(rb[0], np.median(b)) | ||
npt.assert_allclose(rb[1], 0.9944578832097531677397, rtol=1e-2) | ||
npt.assert_allclose(rb[2], 0.9944578832097531677397, rtol=1e-2) | ||
npt.assert_allclose(rb[0], 0.0, atol=1e-2) | ||
|
||
a = np.linspace(0, 10, 100) | ||
npt.assert_allclose(percentile68_ranges(a), [5.0, 3.4, 3.4]) | ||
assert percentile68_ranges_latex(a) == '$5.00 ^{+3.40} _{-3.40}$' | ||
|
||
|
||
def test_KimaResults(): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import pytest | ||
import numpy as np | ||
import numpy.testing as npt | ||
|
||
import os | ||
|
||
def example_priors_run(): | ||
""" | ||
Run kima with maxlevels=1 to sample from the default priors | ||
""" | ||
os.system('make -S' + ' >/dev/null 2>&1') # compile | ||
os.system('./run' + ' >/dev/null 2>&1') # run | ||
|
||
assert os.path.exists('sample.txt') | ||
assert os.path.exists('sample_info.txt') | ||
|
||
|
||
def cleanup(): | ||
os.system('make -S cleanout' + ' >/dev/null 2>&1') | ||
assert not os.path.exists('sample.txt') | ||
|
||
|
||
@pytest.mark.slow | ||
def test_priors(): | ||
""" | ||
After running kima with maxlevels=1 to sample from the default priors, | ||
test some basic statistics of the samples | ||
""" | ||
topdir = os.getcwd() | ||
os.chdir('examples/default_priors') | ||
|
||
example_priors_run() | ||
|
||
extra_sigma, vsys, P, ecc = np.loadtxt('sample.txt', unpack=True, | ||
usecols=(0, -1, 4, 7)) | ||
|
||
npt.assert_allclose(extra_sigma.min(), 0., rtol=0, atol=1e-1) | ||
npt.assert_allclose(extra_sigma.max(), 99., rtol=1e-1, atol=0) | ||
|
||
npt.assert_allclose(vsys.min(), -1000., rtol=0, atol=1) | ||
npt.assert_allclose(vsys.max(), 1000., rtol=0, atol=1) | ||
|
||
npt.assert_allclose(P.min(), 1., rtol=1e-2, atol=0) | ||
npt.assert_allclose(P.max(), 1e5, rtol=1e-2, atol=0) | ||
npt.assert_allclose(P.mean(), (1e5 - 1.)/(np.log(1e5) - np.log(1.)), | ||
rtol=1, atol=0) | ||
|
||
npt.assert_allclose(ecc.min(), 0., rtol=0, atol=1e-4) | ||
npt.assert_allclose(ecc.max(), 1., rtol=0, atol=1e-4) | ||
npt.assert_allclose(ecc.mean(), 0.5, rtol=0, atol=1e-1) | ||
|
||
|
||
cleanup() | ||
os.chdir(topdir) |