Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update tests for changes to pytest #820

Merged
merged 2 commits into from Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 26 additions & 32 deletions tests/tests/test_backward_compatibility/test_import.py
Expand Up @@ -17,16 +17,14 @@ def _export_21(filename, plot=False):
be imported later
"""


if parse(phoebe.__version__) >= parse("2.2"):
raise ImportError("script runs on PHOEBE 2.1.x")
exit()
raise ImportError("script runs on PHOEBE 2.1.x")

b = phoebe.default_binary()
# TESS:default was renamed to TESS:T in 2.2
b.add_dataset('lc', times=np.linspace(0,1,11), passband='TESS:default')
b.add_dataset('rv', times=phoebe.linspace(0,1,4))
b.add_dataset('lp', times=phoebe.linspace(0,1,4), wavelengths=np.linspace(500,510,51))
b.add_dataset('lc', times=np.linspace(0, 1, 11), passband='TESS:default')
b.add_dataset('rv', times=phoebe.linspace(0, 1, 4))
b.add_dataset('lp', times=phoebe.linspace(0, 1, 4), wavelengths=np.linspace(500, 510, 51))
b.add_dataset('mesh', times=[0])

b.run_compute()
Expand All @@ -36,23 +34,22 @@ def _export_21(filename, plot=False):

b.save(os.path.join(dir, filename))


def _export_22(filename, plot=False):
"""
this isn't run during testing, but should be edited to run on certain versions
of phoebe to then store the .phoebe file in this directory and ensure it can
be imported later
"""


if parse(phoebe.__version__) >= parse("2.3"):
raise ImportError("script runs on PHOEBE 2.2.x")
exit()
raise ImportError("script runs on PHOEBE 2.2.x")

b = phoebe.default_binary()
b.add_dataset('lc', times=np.linspace(0,1,11), passband='Johnson:V', Av=0.1)
b.add_dataset('lc', times=np.linspace(0,1,11), passband='Johnson:R', Av=0.2)
b.add_dataset('rv', times=phoebe.linspace(0,1,4))
b.add_dataset('lp', times=phoebe.linspace(0,1,4), wavelengths=np.linspace(500,510,51))
b.add_dataset('lc', times=np.linspace(0, 1, 11), passband='Johnson:V', Av=0.1)
b.add_dataset('lc', times=np.linspace(0, 1, 11), passband='Johnson:R', Av=0.2)
b.add_dataset('rv', times=phoebe.linspace(0, 1, 4))
b.add_dataset('lp', times=phoebe.linspace(0, 1, 4), wavelengths=np.linspace(500, 510, 51))
b.add_dataset('mesh', times=[0])

b.run_compute()
Expand All @@ -62,23 +59,22 @@ def _export_22(filename, plot=False):

b.save(os.path.join(dir, filename))


def _export_23(filename, plot=False):
"""
this isn't run during testing, but should be edited to run on certain versions
of phoebe to then store the .phoebe file in this directory and ensure it can
be imported later
"""


if parse(phoebe.__version__) >= parse("2.4"):
raise ImportError("script runs on PHOEBE 2.3.x")
exit()
raise ImportError("script runs on PHOEBE 2.3.x")

b = phoebe.default_binary()
b.add_dataset('lc', times=np.linspace(0,1,11), passband='Johnson:V', Av=0.1)
b.add_dataset('lc', times=np.linspace(0,1,11), passband='Johnson:R', Av=0.2)
b.add_dataset('rv', times=phoebe.linspace(0,1,4))
b.add_dataset('lp', times=phoebe.linspace(0,1,4), wavelengths=np.linspace(500,510,51))
b.add_dataset('lc', times=np.linspace(0, 1, 11), passband='Johnson:V', Av=0.1)
b.add_dataset('lc', times=np.linspace(0, 1, 11), passband='Johnson:R', Av=0.2)
b.add_dataset('rv', times=phoebe.linspace(0, 1, 4))
b.add_dataset('lp', times=phoebe.linspace(0, 1, 4), wavelengths=np.linspace(500, 510, 51))
b.add_dataset('mesh', times=[0])

b.run_compute()
Expand All @@ -101,7 +97,6 @@ def test_21(verbose=False, plot=False):
if plot:
b.plot(show=True, time=0)

return b
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are these intentionally removed (in several places throughout this PR)? They can be useful when running the script directly and debugging the bundle. If we do need to remove these, then the calls in __main__ need to be updated as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(turns out this is necessary otherwise pytest yells, so we'll just need to manually make these edits if we need to inspect the bundle to debug a broken test).

Copy link
Contributor

@aprsa aprsa Jan 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, pytest currently raises a warning but the next version will raise an error whenever tests return anything. So we can't put those back in, unfortunately. See here for details.


def test_22(verbose=False, plot=False):
b = phoebe.load(os.path.join(dir, '22_export.phoebe'))
Expand All @@ -110,7 +105,6 @@ def test_22(verbose=False, plot=False):
if plot:
b.plot(show=True, time=0)

return b

def test_23(verbose=False, plot=False):
b = phoebe.load(os.path.join(dir, '23_export.phoebe'))
Expand All @@ -123,20 +117,20 @@ def test_23(verbose=False, plot=False):
if plot:
b.plot(show=True, time=0)

return b

if __name__ == '__main__':
logger = phoebe.logger(clevel='WARNING')

# if parse(phoebe.__version__) >= parse("2.1.0") and parse(phoebe.__version__) < parse("2.2.0"):
# _export_21('21_export.phoebe')
# exit()
# if parse(phoebe.__version__) >= parse("2.2.0") and parse(phoebe.__version__) < parse("2.3.0"):
# _export_22('22_export.phoebe')
# exit()
# if parse(phoebe.__version__) >= parse("2.3.0") and parse(phoebe.__version__) < parse("2.4.0"):
# _export_23('23_export.phoebe')
# exit()
if False:
if parse(phoebe.__version__) >= parse("2.1.0") and parse(phoebe.__version__) < parse("2.2.0"):
_export_21('21_export.phoebe')
exit()
if parse(phoebe.__version__) >= parse("2.2.0") and parse(phoebe.__version__) < parse("2.3.0"):
_export_22('22_export.phoebe')
exit()
if parse(phoebe.__version__) >= parse("2.3.0") and parse(phoebe.__version__) < parse("2.4.0"):
_export_23('23_export.phoebe')
exit()

b = test_21(verbose=True, plot=True)
b = test_22(verbose=True, plot=True)
Expand Down
18 changes: 8 additions & 10 deletions tests/tests/test_blackbody/test_blackbody.py
Expand Up @@ -14,7 +14,7 @@ def test_binary(plot=False, gen_comp=False):
b.set_value('sma', component='binary', value=100.)
b.set_value('period', component='binary', value=81.955)

b.add_dataset('lc', times=np.linspace(0,100,21))
b.add_dataset('lc', times=np.linspace(0, 100, 21))
b.add_compute('phoebe', compute='phoebe2')
if gen_comp:
b.add_compute('legacy', compute='phoebe1')
Expand All @@ -31,31 +31,29 @@ def test_binary(plot=False, gen_comp=False):
b.set_value_all('ld_func', 'linear')
b.set_value_all('ld_coeffs', [0.0])

#turn off albedos (legacy requirement)
# turn off albedos (legacy requirement):
b.set_value_all('irrad_frac_refl_bol', 0.0)

if plot: print("running phoebe2 model...")
if plot:
print("running phoebe2 model...")
b.run_compute(compute='phoebe2', irrad_method='none', model='phoebe2model')
if gen_comp:
if plot: print("running phoebe1 model...")
if plot:
print("running phoebe1 model...")
b.run_compute(compute='phoebe1', refl_num=0, model='phoebe1model')
b.filter(model='phoebe1model').save('test_blackbody.comp.model')
else:
b.import_model(os.path.join(os.path.dirname(__file__), 'test_blackbody.comp.model'), model='phoebe1model')


phoebe2_val = b.get_value('fluxes@phoebe2model')
phoebe1_val = b.get_value('fluxes@phoebe1model')

if plot:
b.plot(dataset='lc01', show=True)

assert(np.allclose(phoebe2_val, phoebe1_val, rtol=1e-3, atol=0.))
assert np.allclose(phoebe2_val, phoebe1_val, rtol=1e-3, atol=0.)

return b

if __name__ == '__main__':
logger = phoebe.logger(clevel='INFO')


b = test_binary(plot=True, gen_comp=True)
test_binary(plot=True, gen_comp=True)
3 changes: 3 additions & 0 deletions tests/tests/test_cached_bundles/test_cached_bundles.py
Expand Up @@ -7,18 +7,21 @@ def test_star():

# TODO: add comparison


def test_binary():
b1 = phoebe.default_binary(force_build=True)
b2 = phoebe.default_binary()

# TODO: add comparison


def test_contact_binary():
b1 = phoebe.default_binary(contact_binary=True, force_build=True)
b2 = phoebe.default_binary(contact_binary=True)

# TODO: add comparison


if __name__ == '__main__':
logger = phoebe.logger(clevel='debug')

Expand Down
16 changes: 7 additions & 9 deletions tests/tests/test_cbs/test_cbs.py
Expand Up @@ -4,7 +4,6 @@


def test_binary(plot=False, gen_comp=False):

cb = phoebe.Bundle.default_binary(contact_binary=True)
cb.flip_constraint('pot', solve_for='requiv@primary')
cb['pot@contact_envelope@component'] = 3.5
Expand All @@ -14,7 +13,7 @@ def test_binary(plot=False, gen_comp=False):

# cb.set_value_all('incl',90.0)

times = cb.to_time(np.linspace(-.1,1.1,100))
times = cb.to_time(np.linspace(-.1, 1.1, 100))
cb.add_dataset('lc', times=times, dataset='lc01')
cb.add_dataset('rv', time=times, dataset='rv01')

Expand All @@ -36,7 +35,7 @@ def test_binary(plot=False, gen_comp=False):
cb.set_value_all('rv_grav', False)
cb.set_value_all('ltte', False)

#turn off albedos (legacy requirement)
# turn off albedos (legacy requirement):
cb.set_value_all('irrad_frac_refl_bol', 0.0)

print("running phoebe2 model...")
Expand All @@ -62,16 +61,15 @@ def test_binary(plot=False, gen_comp=False):
cb.plot(dataset='lc01', legend=True, show=True)
cb.plot(dataset='rv01', legend=True, show=True)

assert(np.allclose(phoebe2_val_lc, phoebe1_val_lc, rtol=7e-3, atol=0.))
assert np.allclose(phoebe2_val_lc, phoebe1_val_lc, rtol=7e-3, atol=0.)
# note we can't use relative tolerances because those blow up near 0, so
# instead we'll fake a relative tolerance by using the amplitude of the RV curve.
rv_ampl = np.max(np.abs(phoebe1_val_rv1))
rtol = 7e-3
assert(np.allclose(phoebe2_val_rv1, phoebe1_val_rv1, rtol=0., atol=rtol*rv_ampl))
assert(np.allclose(phoebe2_val_rv2, phoebe1_val_rv2, rtol=0., atol=rtol*rv_ampl))
return cb
assert np.allclose(phoebe2_val_rv1, phoebe1_val_rv1, rtol=0., atol=rtol*rv_ampl)
assert np.allclose(phoebe2_val_rv2, phoebe1_val_rv2, rtol=0., atol=rtol*rv_ampl)


if __name__ == '__main__':
logger = phoebe.logger(clevel='debug')

cb = test_binary(plot=True, gen_comp=True)
test_binary(plot=True, gen_comp=True)
38 changes: 12 additions & 26 deletions tests/tests/test_checks/test_checks.py
Expand Up @@ -3,74 +3,60 @@

import phoebe


phoebe.logger('DEBUG')


def test_checks():
b = phoebe.Bundle.default_binary()


b.add_dataset('lc')

# test overflow
report = b.run_checks()
if not report.passed:
raise AssertionError(msg)
assert report.passed

b.set_value('requiv', component='primary', value=9.0)
report = b.run_checks()
if report.passed:
raise AssertionError
assert not report.passed

b.set_value('requiv', component='primary', value=1.0)

# TODO: test overlap scenario


# test ld_func vs ld_coeffs
report = b.run_checks()
if not report.passed:
raise AssertionError(msg)
assert report.passed

b.set_value_all('ld_mode_bol', 'manual')
b.set_value('ld_coeffs_bol', component='primary', value=[0.])
report = b.run_checks()
if report.passed:
raise AssertionError
b.set_value('ld_coeffs_bol', component='primary', value=[0.5, 0.5])
assert not report.passed

b.set_value('ld_coeffs_bol', component='primary', value=[0.5, 0.5])
b.set_value('ld_mode', component='primary', value='manual')
b.set_value('ld_func', component='primary', value='logarithmic')
b.set_value('ld_coeffs', component='primary', value=[0.])
report = b.run_checks()
if report.passed:
raise AssertionError
assert not report.passed

b.set_value('ld_coeffs', component='primary', value=[0., 0.])
b.set_value('ld_mode', component='primary', value='interp')

# test ld_func vs atm
report = b.run_checks()
if not report.passed:
raise AssertionError(msg)
assert report.passed

b.set_value('atm', component='primary', value='blackbody')
report = b.run_checks()
if report.passed:
raise AssertionError
assert not report.passed
b.set_value('atm', component='primary', value='ck2004')


# test gravb vs teff warning
b.set_value('teff', component='primary', value=6000)
b.set_value('gravb_bol', component='primary', value=1.0)
report = b.run_checks()
if not report.passed or not len(report.items):
raise AssertionError
assert report.passed and len(report.items) > 0

return b

if __name__ == '__main__':
logger = phoebe.logger(clevel='INFO')


b = test_checks()
test_checks()