Skip to content

Commit

Permalink
Adding real tests
Browse files Browse the repository at this point in the history
	modified:   obstools/celestial_time.py
	modified:   obstools/lmi_etc.py
	new file:   obstools/tests/test_celestial_time.py
	new file:   obstools/tests/test_lmi_etc.py
	new file:   obstools/tests/test_utils.py
	modified:   obstools/utils.py
  • Loading branch information
tbowers7 committed Nov 6, 2024
1 parent 55a863d commit af545b0
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 16 deletions.
12 changes: 8 additions & 4 deletions obstools/celestial_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@
# 3rd-Party Libraries
import astropy.coordinates
import astropy.time
import numpy as np

# Local Libraries


def lst_midnight(utdates: list):
def lst_midnight(utdates: list[str]) -> np.ndarray:
"""Compute the LST at midnight for LDT on a list of UT dates
The "LST at Midnight" is a helpful guide for determining what objects may
Expand All @@ -44,14 +45,17 @@ def lst_midnight(utdates: list):
Returns
-------
:obj:`list`
List of the output LST in HH:MM:SS format
:obj:`~numpy.ndarray`
Array of the output LST in HH:MM:SS format
"""
# Check input
if not isinstance(utdates, list):
raise ValueError(f"Incorrect input type {type(utdates)}")
midnights = [f"{date}T07:00:00" for date in utdates]
times = astropy.time.Time(
midnights,
format="isot",
scale="utc",
location=astropy.coordinates.EarthLocation.of_site("DCT"),
)
return times.sidereal_time("apparent").to_string(precision=0)
return times.sidereal_time("apparent").to_string(precision=0, sep=":", pad=True)
22 changes: 11 additions & 11 deletions obstools/lmi_etc.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def exptime_given_snr_mag(
phase: float,
seeing: float,
binning: int = 2,
):
) -> float:
"""Compute the exposure time given SNR and magnitude
Given a desired signal-to-noise ratio and stellar magnitude, compute the
Expand Down Expand Up @@ -131,7 +131,7 @@ def exptime_given_peak_mag(
phase: float,
seeing: float,
binning: int = 2,
):
) -> float:
"""Compute the exposure time given peak and mag
Given a desired peak count level on the CCD and stellar magnitude, compute
Expand Down Expand Up @@ -184,7 +184,7 @@ def snr_given_exptime_mag(
phase: float,
seeing: float,
binning: int = 2,
):
) -> float:
"""Compute the SNR given exposure time and magnitude
Given a desired exposure time and stellar magnitude, compute the resulting
Expand Down Expand Up @@ -238,7 +238,7 @@ def mag_given_snr_exptime(
phase: float,
seeing: float,
binning: int = 2,
):
) -> float:
"""Compute the magnitude given SNR and exposure time
Given a desired signal-to-noise ratio and exposure time, compute the
Expand Down Expand Up @@ -292,7 +292,7 @@ def peak_counts(
phase: float,
seeing: float,
binning: int = 2,
):
) -> float:
"""Compute the peak counts on the CCD for an exptime and mag
Given a desired exposure time and stellar magnitude, compute the resulting
Expand Down Expand Up @@ -388,7 +388,7 @@ def check_etc_inputs(
raise ValueError(f"Invalid signal-to-noise specified: {snr}")


def counts_from_star_per_sec(band_dict: dict, mag: float, airmass: float):
def counts_from_star_per_sec(band_dict: dict, mag: float, airmass: float) -> float:
"""Compute the counts per second from a star
Compute the counts per second from a star given a band, magnitude, and
Expand All @@ -412,7 +412,7 @@ def counts_from_star_per_sec(band_dict: dict, mag: float, airmass: float):
return band_dict["Star20"] * np.power(10, -((mag_corrected - 20) / 2.5))


def get_band_specific_values(band: str):
def get_band_specific_values(band: str) -> dict:
"""Return the band-specific star and sky values
Pull the correct row from ``etc_filter_info.ecsv`` containing the star count
Expand Down Expand Up @@ -443,7 +443,7 @@ def get_band_specific_values(band: str):
return {}


def number_pixels(seeing: float, binning: int):
def number_pixels(seeing: float, binning: int) -> float:
"""Number of pixels in the measuring aperture
Counts the number of pixels in the measuring aperture, based on the seeing
Expand All @@ -458,7 +458,7 @@ def number_pixels(seeing: float, binning: int):
----------
seeing : :obj:`float`
Size of the seeing disk (arcsec)
binning : :obj:`int`, optional
binning : :obj:`int`
Binning of the CCD
Returns
Expand All @@ -470,7 +470,7 @@ def number_pixels(seeing: float, binning: int):
return np.max([1.4 * fwhm * fwhm, 9.0])


def read_noise_contribution(seeing: float, binning: int):
def read_noise_contribution(seeing: float, binning: int) -> float:
"""Calculate read-noise contribution
Compute the read-noise contribution to the measuring aperture by
Expand All @@ -494,7 +494,7 @@ def read_noise_contribution(seeing: float, binning: int):

def sky_count_per_sec_per_ap(
band_dict: dict, phase: float, seeing: float, binning: int
):
) -> float:
"""Determine sky counts per aperture per second
[extended_summary]
Expand Down
45 changes: 45 additions & 0 deletions obstools/tests/test_celestial_time.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# pylint: disable=missing-function-docstring
# -*- coding: utf-8 -*-
#
# This file is part of LDTObserverTools.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Created on 06-Nov-2024
#
# @author: tbowers

"""Celestial Time Utilities TEST Module
"""

import numpy as np
import pytest

from obstools import celestial_time


def test_lst_midnight():
# Test the inputs / outputs
utdates = ["2020-01-24", "2021-03-02", "2022-04-19", "2023-06-21", "2024-11-29"]
lsts = celestial_time.lst_midnight(utdates)
assert isinstance(lsts, np.ndarray)
assert isinstance(lsts[0], str)
assert len(lsts) == len(utdates)
assert lsts[0] == "07:46:36" # LST at midnight on UT 2020-01-24
assert lsts[2] == "13:23:46" # LST at midnight on UT 2022-04-19

# Make sure the routine handles errors properly...
# Bad Month
with pytest.raises(ValueError):
_ = celestial_time.lst_midnight(["2022-13-17"])
# Bad Day
with pytest.raises(ValueError):
_ = celestial_time.lst_midnight(["2022-03-83"])
# No "-" separator
with pytest.raises(ValueError):
_ = celestial_time.lst_midnight(["20210409"])
# Bad inputs (not list)
with pytest.raises(ValueError):
_ = celestial_time.lst_midnight("2024-11-07")
63 changes: 63 additions & 0 deletions obstools/tests/test_lmi_etc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# pylint: disable=missing-function-docstring
# -*- coding: utf-8 -*-
#
# This file is part of LDTObserverTools.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Created on 06-Nov-2024
#
# @author: tbowers

"""LMI Exposure Time Calculator TEST Module
"""

import numpy as np

from obstools import lmi_etc


def test_exptime_given_snr_mag():
pass


def test_exptime_given_peak_mag():
pass


def test_snr_given_exptime_mag():
pass


def test_mag_given_snr_exptime():
pass


def test_peak_counts():
pass


def test_check_etc_inputs():
pass


def test_counts_from_star_per_sec():
pass


def test_get_band_specific_values():
pass


def test_number_pixels():
pass


def test_read_noise_contribution():
pass


def test_sky_count_per_sec_per_ap():
pass
80 changes: 80 additions & 0 deletions obstools/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# pylint: disable=missing-function-docstring
# -*- coding: utf-8 -*-
#
# This file is part of LDTObserverTools.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Created on 06-Nov-2024
#
# @author: tbowers

"""Utility TEST Module
"""

import numpy as np

from obstools import utils


def test_all_subclasses():
pass


def test_check_float():
# Actual Float
assert utils.check_float(3.14159)
# Not a float
assert not utils.check_float("This is a float!")
# Integer, but convertable
assert utils.check_float(1)
# Boolean is convertable to a float
assert utils.check_float(True)


def test_first_moment_1d():
pass


def test_flatten_comprehension():
pass


def test_gaussfit():
pass


def test_gaussian_function():
pass


def test_good_poly():
pass


def test_nearest_odd():
# Check return type and value
nearest = utils.nearest_odd(1.5)
assert isinstance(nearest, int)
assert nearest == 1
# Nearest odd to an odd is itself
assert utils.nearest_odd(5.0) == 5
# Rounds up from even integers
assert utils.nearest_odd(4) == 5
assert utils.nearest_odd(4) != 3
# But, just under goes down...
assert utils.nearest_odd(3.999999999) == 3


def test_set_std_tickparams():
pass


def test_sinusoid():
pass


def test_warn_and_return_zeros():
pass
2 changes: 1 addition & 1 deletion obstools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def all_subclasses(cls):


def check_float(potential_float) -> bool:
"""Simple funtion to check whether something is a float
"""Simple funtion to check whether something is (convertable to) a float
Parameters
----------
Expand Down

0 comments on commit af545b0

Please sign in to comment.