From a2a2a030167649999322984260476670d17ae13f Mon Sep 17 00:00:00 2001 From: Jeremy Kubica <104161096+jeremykubica@users.noreply.github.com> Date: Thu, 8 Feb 2024 10:28:32 -0500 Subject: [PATCH] Add some basic checks --- src/kbmod/search/psf.cpp | 7 ++++++- src/kbmod/search/pydocs/psf_docs.h | 18 +++++++++++------- tests/test_psf.py | 8 ++++++++ 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/kbmod/search/psf.cpp b/src/kbmod/search/psf.cpp index 0ec947b8b..fa2735cbb 100644 --- a/src/kbmod/search/psf.cpp +++ b/src/kbmod/search/psf.cpp @@ -9,6 +9,10 @@ PSF::PSF() : kernel(1, 1.0) { } PSF::PSF(float stdev) { + if (stdev <= 0.0) { + throw std::runtime_error("PSF stdev must be > 0.0."); + } + width = stdev; float simple_gauss[MAX_KERNEL_RADIUS]; double psf_coverage = 0.0; @@ -28,7 +32,7 @@ PSF::PSF(float stdev) { i++; } - radius = i - 1; // This value is good for + radius = i - 1; dim = 2 * radius + 1; // Create 2D gaussain by multiplying with itself @@ -155,6 +159,7 @@ static void psf_bindings(py::module& m) { .def(py::init()) .def(py::init>()) .def(py::init()) + .def("__str__", &psf::print) .def("set_array", &psf::set_array, pydocs::DOC_PSF_set_array) .def("get_std", &psf::get_std, pydocs::DOC_PSF_get_std) .def("get_sum", &psf::get_sum, pydocs::DOC_PSF_get_sum) diff --git a/src/kbmod/search/pydocs/psf_docs.h b/src/kbmod/search/pydocs/psf_docs.h index 91ea88399..a58fb07f4 100644 --- a/src/kbmod/search/pydocs/psf_docs.h +++ b/src/kbmod/search/pydocs/psf_docs.h @@ -8,18 +8,22 @@ static const auto DOC_PSF = R"doc( Parameters ---------- stdev : `float`, optional - Standard deviation of the Gaussian PSF. + Standard deviation of the Gaussian PSF. Must be > 0.0. psf : `PSF`, optional Another PSF object. arr : `numpy.array`, optional A realization of the PSF. - Notes - ----- - When instantiated with another `psf` object, returns its copy. - When instantiated with an array-like object, that array must be - a square matrix and have an odd number of dimensions. Only one - of the arguments is required. + Notes + ----- + When instantiated with another `psf` object, returns its copy. + When instantiated with an array-like object, that array must be + a square matrix and have an odd number of dimensions. Only one + of the arguments is required. + + Raises + ------ + Raises a ``RuntimeError`` when given an invalid stdev. )doc"; static const auto DOC_PSF_set_array = R"doc( diff --git a/tests/test_psf.py b/tests/test_psf.py index f8cecb58e..a7eba0ea9 100644 --- a/tests/test_psf.py +++ b/tests/test_psf.py @@ -19,6 +19,14 @@ def test_make_noop(self): self.assertEqual(len(kernel0), 1) self.assertEqual(kernel0[0], 1.0) + def test_make_invalud(self): + # Raise an error if creating a PSF with a negative stdev. + self.assertRaises(RuntimeError, PSF, -1.0) + + def test_to_string(self): + result = self.psf_list[0].__str__() + self.assertGreater(len(result), 1) + def test_make_and_copy(self): psf1 = PSF(1.0) self.assertEqual(psf1.get_size(), 25)