From 1894632d1727ef4bf47dda5a6d598628652c4649 Mon Sep 17 00:00:00 2001 From: Mees Fix Date: Wed, 9 Jun 2021 17:03:11 -0400 Subject: [PATCH 1/5] Adding some utility tests --- exoctk/tests/test_utils.py | 49 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 exoctk/tests/test_utils.py diff --git a/exoctk/tests/test_utils.py b/exoctk/tests/test_utils.py new file mode 100644 index 00000000..0a391ee5 --- /dev/null +++ b/exoctk/tests/test_utils.py @@ -0,0 +1,49 @@ +#! /usr/bin/env python + +"""Tests for the ``utils`` script. + +Authors +------- + + Mees Fix + +Use +--- + + These tests can be run via the command line (omit the ``-s`` to + suppress verbose output to stdout): + :: + + pytest -s test_utils.py +""" + +import numpy as np +import pytest + +from exoctk.utils import get_canonical_name, get_target_data + +args = ['planet_name', 'planet_data'] +test_data = [('HD108236f', {'canonical_name':'HD 108236 f', 'Fe/H':-0.28, 'Teff':5660.0, 'stellar_gravity':4.49, 'transit_duration':0.13625, 'RA':186.5740627, 'DEC':-51.3630519}), + ('NGTS-14Ab', {'canonical_name':'NGTS-14 A b', 'Fe/H':0.1, 'Teff':5187.0, 'stellar_gravity':4.2, 'transit_duration':0.09333333333333334, 'RA':328.5174799, 'DEC':-38.3774193}), + ('2MASSJ10193800-0948225b', {'canonical_name':'WASP-43 b', 'Fe/H':-0.01, 'Teff':4400.0, 'stellar_gravity':3.707669652604325, 'transit_duration':0.0483, 'RA':154.9081869, 'DEC':-9.8064431})] + + +@pytest.mark.parametrize(args, test_data) +def test_get_canoical_name(planet_name, planet_data): + 'Test that the canonical name of the planet is returned by exomast' + + canonical_name = get_canonical_name(planet_name) + assert canonical_name == planet_data['canonical_name'] + + +@pytest.mark.parametrize(args, test_data) +def test_get_target_data(planet_name, planet_data): + 'Test that the canonical name of the planet is returned by exomast' + + data, _ = get_target_data(planet_name) + + # these are some params that the webapp uses for it's tools + exomast_params = ['Fe/H', 'Teff', 'stellar_gravity', 'transit_duration', 'RA', 'DEC'] + + for value in exomast_params: + assert data[value] == planet_data[value] \ No newline at end of file From f53df377c57b6aac145124b68670e7086edf152b Mon Sep 17 00:00:00 2001 From: Mees Fix Date: Fri, 11 Jun 2021 16:55:42 -0400 Subject: [PATCH 2/5] working on ways to test median filter util and find_closest --- exoctk/tests/test_utils.py | 46 +++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/exoctk/tests/test_utils.py b/exoctk/tests/test_utils.py index 0a391ee5..5d991c17 100644 --- a/exoctk/tests/test_utils.py +++ b/exoctk/tests/test_utils.py @@ -17,20 +17,24 @@ pytest -s test_utils.py """ +from astropy.table import Table import numpy as np import pytest -from exoctk.utils import get_canonical_name, get_target_data +from exoctk.utils import filter_table, get_canonical_name, get_target_data, color_gen, medfilt args = ['planet_name', 'planet_data'] + +# Include 3 actual planets with data and one fake planet to xfail. test_data = [('HD108236f', {'canonical_name':'HD 108236 f', 'Fe/H':-0.28, 'Teff':5660.0, 'stellar_gravity':4.49, 'transit_duration':0.13625, 'RA':186.5740627, 'DEC':-51.3630519}), ('NGTS-14Ab', {'canonical_name':'NGTS-14 A b', 'Fe/H':0.1, 'Teff':5187.0, 'stellar_gravity':4.2, 'transit_duration':0.09333333333333334, 'RA':328.5174799, 'DEC':-38.3774193}), - ('2MASSJ10193800-0948225b', {'canonical_name':'WASP-43 b', 'Fe/H':-0.01, 'Teff':4400.0, 'stellar_gravity':3.707669652604325, 'transit_duration':0.0483, 'RA':154.9081869, 'DEC':-9.8064431})] + ('2MASSJ10193800-0948225b', {'canonical_name':'WASP-43 b', 'Fe/H':-0.01, 'Teff':4400.0, 'stellar_gravity':4.49, 'transit_duration':0.0483, 'RA':154.9081869, 'DEC':-9.8064431}), + pytest.param('djgfjhsg', {'canonical_name':'sfghsfkjg', 'Fe/H':-999, 'Teff':-999, 'stellar_gravity':-999, 'transit_duration':-999, 'RA':-999, 'DEC':-999}, marks=pytest.mark.xfail)] @pytest.mark.parametrize(args, test_data) def test_get_canoical_name(planet_name, planet_data): - 'Test that the canonical name of the planet is returned by exomast' + '''Test that the canonical name of the planet is returned by exomast''' canonical_name = get_canonical_name(planet_name) assert canonical_name == planet_data['canonical_name'] @@ -38,7 +42,7 @@ def test_get_canoical_name(planet_name, planet_data): @pytest.mark.parametrize(args, test_data) def test_get_target_data(planet_name, planet_data): - 'Test that the canonical name of the planet is returned by exomast' + '''Test that the canonical name of the planet is returned by exomast''' data, _ = get_target_data(planet_name) @@ -46,4 +50,36 @@ def test_get_target_data(planet_name, planet_data): exomast_params = ['Fe/H', 'Teff', 'stellar_gravity', 'transit_duration', 'RA', 'DEC'] for value in exomast_params: - assert data[value] == planet_data[value] \ No newline at end of file + assert data[value] == planet_data[value] + + +@pytest.fixture +def build_table(): + """ Fixture to build table for tests requiring table """ + column_names = ['wavelength', 'flux'] + wavelength = np.arange(3000, 5000) + flux = np.random.rand(wavelength.shape[0])*10e-13 + + table = Table(data=[wavelength, flux], names=column_names) + + return table + + +@pytest.mark.parametrize("operator", ['>4021', '<3856', '>=4928', '<=3740', '==4000', '4*']) +def test_filter_table(build_table, operator): + '''test table filter function with fake table''' + # Test wavelength sort + test_data = filter_table(build_table, wavelength=operator) + assert all(test_data['wavelength'] == eval("build_table[np.where(build_table['wavelength'] {})]['wavelength']".format(operator))) + + +@pytest.mark.parametrize("colormap", ['viridis', pytest.param('hjdsgfdhsf', marks=pytest.mark.xfail)]) +def test_color_gen(colormap): + color_gen(colormap) + + +medfilt_data = [(np.array([ 1, 1, 8, 12, 2, 10, 5, 2, 5, 2]), 4), + (np.array([ 1, 1, 8, 12, 2, 10, 5, 2, 5, 2]), 4)] +@pytest.mark.parametrize(['data', 'filter_window'], medfilt_data) +def test_medfilt(data,filter_window): + filtered_data = medfilt(data, filter_window) \ No newline at end of file From c31302b0f95bfea86cbe3f69aeb519e7a212f163 Mon Sep 17 00:00:00 2001 From: Matthew Bourque Date: Wed, 4 Aug 2021 09:36:57 -0400 Subject: [PATCH 3/5] Removed one parameter that was breaking the test_filter_table unit test --- exoctk/tests/test_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exoctk/tests/test_utils.py b/exoctk/tests/test_utils.py index 5d991c17..c6cd51e7 100644 --- a/exoctk/tests/test_utils.py +++ b/exoctk/tests/test_utils.py @@ -65,7 +65,7 @@ def build_table(): return table -@pytest.mark.parametrize("operator", ['>4021', '<3856', '>=4928', '<=3740', '==4000', '4*']) +@pytest.mark.parametrize("operator", ['>4021', '<3856', '>=4928', '<=3740', '==4000']) def test_filter_table(build_table, operator): '''test table filter function with fake table''' # Test wavelength sort From 25128a0269e5012b97cfe1f4951279c8bc420102 Mon Sep 17 00:00:00 2001 From: Matthew Bourque Date: Wed, 4 Aug 2021 09:42:43 -0400 Subject: [PATCH 4/5] Fixed various issues raised by flake8 --- exoctk/tests/test_utils.py | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/exoctk/tests/test_utils.py b/exoctk/tests/test_utils.py index c6cd51e7..0cbfc686 100644 --- a/exoctk/tests/test_utils.py +++ b/exoctk/tests/test_utils.py @@ -21,28 +21,31 @@ import numpy as np import pytest -from exoctk.utils import filter_table, get_canonical_name, get_target_data, color_gen, medfilt +from exoctk.utils import color_gen, filter_table, get_canonical_name, get_target_data, medfilt -args = ['planet_name', 'planet_data'] +ARGS = ['planet_name', 'planet_data'] # Include 3 actual planets with data and one fake planet to xfail. -test_data = [('HD108236f', {'canonical_name':'HD 108236 f', 'Fe/H':-0.28, 'Teff':5660.0, 'stellar_gravity':4.49, 'transit_duration':0.13625, 'RA':186.5740627, 'DEC':-51.3630519}), - ('NGTS-14Ab', {'canonical_name':'NGTS-14 A b', 'Fe/H':0.1, 'Teff':5187.0, 'stellar_gravity':4.2, 'transit_duration':0.09333333333333334, 'RA':328.5174799, 'DEC':-38.3774193}), - ('2MASSJ10193800-0948225b', {'canonical_name':'WASP-43 b', 'Fe/H':-0.01, 'Teff':4400.0, 'stellar_gravity':4.49, 'transit_duration':0.0483, 'RA':154.9081869, 'DEC':-9.8064431}), - pytest.param('djgfjhsg', {'canonical_name':'sfghsfkjg', 'Fe/H':-999, 'Teff':-999, 'stellar_gravity':-999, 'transit_duration':-999, 'RA':-999, 'DEC':-999}, marks=pytest.mark.xfail)] +TEST_DATA = [('HD108236f', {'canonical_name': 'HD 108236 f', 'Fe/H': -0.28, 'Teff': 5660.0, 'stellar_gravity': 4.49, 'transit_duration': 0.13625, 'RA': 186.5740627, 'DEC': -51.3630519}), + ('NGTS-14Ab', {'canonical_name': 'NGTS-14 A b', 'Fe/H': 0.1, 'Teff': 5187.0, 'stellar_gravity': 4.2, 'transit_duration': 0.09333333333333334, 'RA': 328.5174799, 'DEC': -38.3774193}), + ('2MASSJ10193800-0948225b', {'canonical_name': 'WASP-43 b', 'Fe/H': -0.01, 'Teff': 4400.0, 'stellar_gravity': 4.49, 'transit_duration': 0.0483, 'RA': 154.9081869, 'DEC': -9.8064431}), + pytest.param('djgfjhsg', {'canonical_name': 'sfghsfkjg', 'Fe/H': -999, 'Teff': -999, 'stellar_gravity': -999, 'transit_duration': -999, 'RA': -999, 'DEC': -999}, marks=pytest.mark.xfail)] +MEDFILT_DATA = [(np.array([1, 1, 8, 12, 2, 10, 5, 2, 5, 2]), 4), + (np.array([1, 1, 8, 12, 2, 10, 5, 2, 5, 2]), 4)] -@pytest.mark.parametrize(args, test_data) + +@pytest.mark.parametrize(ARGS, TEST_DATA) def test_get_canoical_name(planet_name, planet_data): - '''Test that the canonical name of the planet is returned by exomast''' + """Test that the canonical name of the planet is returned by exomast""" canonical_name = get_canonical_name(planet_name) assert canonical_name == planet_data['canonical_name'] -@pytest.mark.parametrize(args, test_data) +@pytest.mark.parametrize(ARGS, TEST_DATA) def test_get_target_data(planet_name, planet_data): - '''Test that the canonical name of the planet is returned by exomast''' + """Test that the canonical name of the planet is returned by exomast""" data, _ = get_target_data(planet_name) @@ -67,10 +70,10 @@ def build_table(): @pytest.mark.parametrize("operator", ['>4021', '<3856', '>=4928', '<=3740', '==4000']) def test_filter_table(build_table, operator): - '''test table filter function with fake table''' + """test table filter function with fake table""" # Test wavelength sort - test_data = filter_table(build_table, wavelength=operator) - assert all(test_data['wavelength'] == eval("build_table[np.where(build_table['wavelength'] {})]['wavelength']".format(operator))) + TEST_DATA = filter_table(build_table, wavelength=operator) + assert all(TEST_DATA['wavelength'] == eval("build_table[np.where(build_table['wavelength'] {})]['wavelength']".format(operator))) @pytest.mark.parametrize("colormap", ['viridis', pytest.param('hjdsgfdhsf', marks=pytest.mark.xfail)]) @@ -78,8 +81,6 @@ def test_color_gen(colormap): color_gen(colormap) -medfilt_data = [(np.array([ 1, 1, 8, 12, 2, 10, 5, 2, 5, 2]), 4), - (np.array([ 1, 1, 8, 12, 2, 10, 5, 2, 5, 2]), 4)] -@pytest.mark.parametrize(['data', 'filter_window'], medfilt_data) -def test_medfilt(data,filter_window): - filtered_data = medfilt(data, filter_window) \ No newline at end of file +@pytest.mark.parametrize(['data', 'filter_window'], MEDFILT_DATA) +def test_medfilt(data, filter_window): + medfilt(data, filter_window) From aa186e32f98c800aabfce5260e49fda3a3eff393 Mon Sep 17 00:00:00 2001 From: Matthew Bourque Date: Wed, 4 Aug 2021 09:43:50 -0400 Subject: [PATCH 5/5] Fixed issues raised by @pepe8speaks --- exoctk/tests/test_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exoctk/tests/test_utils.py b/exoctk/tests/test_utils.py index 0cbfc686..e6e0b1ea 100644 --- a/exoctk/tests/test_utils.py +++ b/exoctk/tests/test_utils.py @@ -31,8 +31,8 @@ ('2MASSJ10193800-0948225b', {'canonical_name': 'WASP-43 b', 'Fe/H': -0.01, 'Teff': 4400.0, 'stellar_gravity': 4.49, 'transit_duration': 0.0483, 'RA': 154.9081869, 'DEC': -9.8064431}), pytest.param('djgfjhsg', {'canonical_name': 'sfghsfkjg', 'Fe/H': -999, 'Teff': -999, 'stellar_gravity': -999, 'transit_duration': -999, 'RA': -999, 'DEC': -999}, marks=pytest.mark.xfail)] -MEDFILT_DATA = [(np.array([1, 1, 8, 12, 2, 10, 5, 2, 5, 2]), 4), - (np.array([1, 1, 8, 12, 2, 10, 5, 2, 5, 2]), 4)] +MEDFILT_DATA = [(np.array([1, 1, 8, 12, 2, 10, 5, 2, 5, 2]), 4), + (np.array([1, 1, 8, 12, 2, 10, 5, 2, 5, 2]), 4)] @pytest.mark.parametrize(ARGS, TEST_DATA)