From 3771800c616297e2ea7f11e9b0d2b40d8d70077e Mon Sep 17 00:00:00 2001 From: Jeremy Kubica <104161096+jeremykubica@users.noreply.github.com> Date: Fri, 15 Dec 2023 11:07:23 -0500 Subject: [PATCH 1/2] Version 1 --- src/kbmod/search/bindings.cpp | 6 ++-- src/kbmod/search/filtering.h | 15 --------- ...ltering.cpp => kernel_testing_helpers.cpp} | 33 +++++-------------- tests/test_filtering.py | 23 ------------- 4 files changed, 12 insertions(+), 65 deletions(-) delete mode 100644 src/kbmod/search/filtering.h rename src/kbmod/search/{filtering.cpp => kernel_testing_helpers.cpp} (65%) diff --git a/src/kbmod/search/bindings.cpp b/src/kbmod/search/bindings.cpp index fae426e91..f0d2d1b04 100644 --- a/src/kbmod/search/bindings.cpp +++ b/src/kbmod/search/bindings.cpp @@ -15,9 +15,10 @@ namespace py = pybind11; #include "image_stack.cpp" #include "stack_search.cpp" #include "stamp_creator.cpp" -#include "filtering.cpp" +#include "kernel_testing_helpers.cpp" #include "psi_phi_array.cpp" + PYBIND11_MODULE(search, m) { m.attr("KB_NO_DATA") = pybind11::float_(search::NO_DATA); m.attr("HAS_GPU") = pybind11::bool_(search::HAVE_GPU); @@ -45,7 +46,6 @@ PYBIND11_MODULE(search, m) { m.def("create_median_image", &search::create_median_image); m.def("create_summed_image", &search::create_summed_image); m.def("create_mean_image", &search::create_mean_image); - // Functions from filtering.cpp + // Functions from kernel_testing_helpers.cpp m.def("sigmag_filtered_indices", &search::sigmaGFilteredIndices); - m.def("calculate_likelihood_psi_phi", &search::calculateLikelihoodFromPsiPhi); } diff --git a/src/kbmod/search/filtering.h b/src/kbmod/search/filtering.h deleted file mode 100644 index 96c658c06..000000000 --- a/src/kbmod/search/filtering.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef FILTERING_H_ -#define FILTERING_H_ - -#include - -namespace search { -/* Return the list of indices from the values array such that those elements - pass the sigmaG filtering defined by percentiles [sGL0, sGL1] with coefficient - sigmag_coeff and a multiplicative factor of width. */ -std::vector sigmaGFilteredIndices(const std::vector& values, float sgl0, float sgl1, - float sigma_g_coeff, float width); - -} /* namespace search */ - -#endif /* FILTERING_H_ */ diff --git a/src/kbmod/search/filtering.cpp b/src/kbmod/search/kernel_testing_helpers.cpp similarity index 65% rename from src/kbmod/search/filtering.cpp rename to src/kbmod/search/kernel_testing_helpers.cpp index 912a82aa2..f27c1fcff 100644 --- a/src/kbmod/search/filtering.cpp +++ b/src/kbmod/search/kernel_testing_helpers.cpp @@ -1,5 +1,6 @@ -#include "filtering.h" -#include +/* Helper functions for testing functions in the .cu files from Python. */ + +#include namespace search { #ifdef HAVE_CUDA @@ -8,9 +9,12 @@ extern "C" void SigmaGFilteredIndicesCU(float *values, int num_values, float sgl float width, int *idx_array, int *min_keep_idx, int *max_keep_idx); #endif -/* Return the list of indices from the values array such that those elements - pass the sigmaG filtering defined by percentiles [sgl0, sgl1] with coefficient - sigma_g_coeff and a multiplicative factor of width. */ +/* Used for testing SigmaGFilteredIndicesCU for python + * + * Return the list of indices from the values array such that those elements + * pass the sigmaG filtering defined by percentiles [sgl0, sgl1] with coefficient + * sigma_g_coeff and a multiplicative factor of width. + */ std::vector sigmaGFilteredIndices(const std::vector &values, float sgl0, float sgl1, float sigma_g_coeff, float width) { // Bounds check the percentile values. @@ -43,23 +47,4 @@ std::vector sigmaGFilteredIndices(const std::vector &values, float s return result; } -/* Given a set of psi and phi values, - return a likelihood value */ -double calculateLikelihoodFromPsiPhi(std::vector psi_values, std::vector phi_values) { - assert(psi_values.size() == phi_values.size()); - double psi_sum = 0.0; - double phi_sum = 0.0; - - for (int i = 0; i < psi_values.size(); i++) { - psi_sum += psi_values[i]; - phi_sum += phi_values[i]; - } - - if (psi_sum == 0.0 || phi_sum <= 0.0) { - return 0.0; - } - - return psi_sum / sqrt(phi_sum); -} - } /* namespace search */ diff --git a/tests/test_filtering.py b/tests/test_filtering.py index 3c87f99f3..b90f779c2 100644 --- a/tests/test_filtering.py +++ b/tests/test_filtering.py @@ -98,29 +98,6 @@ def test_sigmag_filtered_indices_three_outliers(self): valid = i != 13 and i != 14 and i != 27 self.assertEqual(i in inds, valid) - def test_calculate_likelihood_psiphi(self): - # make sure that the calculate_likelihood_psi_phi works. - psi_values = [1.0 for _ in range(20)] - phi_values = [1.0 for _ in range(20)] - - lh = calculate_likelihood_psi_phi(psi_values, phi_values) - - self.assertEqual(lh, 4.47213595499958) - - def test_calculate_likelihood_psiphi_zero_or_negative_phi(self): - # make sure that the calculate_likelihood_psi_phi works - # properly when phi values are less than or equal to zero. - psi_values = [1.0 for _ in range(20)] - phi_values = [-1.0 for _ in range(20)] - - # test negatives - lh = calculate_likelihood_psi_phi(psi_values, phi_values) - self.assertEqual(lh, 0.0) - - # test zero - lh = calculate_likelihood_psi_phi([1.0], [0.0]) - self.assertEqual(lh, 0.0) - if __name__ == "__main__": unittest.main() From 1ad53081e998cae2342f3f67c70e8e6da0f2604d Mon Sep 17 00:00:00 2001 From: Jeremy Kubica <104161096+jeremykubica@users.noreply.github.com> Date: Fri, 15 Dec 2023 11:17:46 -0500 Subject: [PATCH 2/2] Simplify testing function --- src/kbmod/search/kernel_testing_helpers.cpp | 23 +++++++-------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/kbmod/search/kernel_testing_helpers.cpp b/src/kbmod/search/kernel_testing_helpers.cpp index f27c1fcff..83b79137e 100644 --- a/src/kbmod/search/kernel_testing_helpers.cpp +++ b/src/kbmod/search/kernel_testing_helpers.cpp @@ -14,26 +14,19 @@ extern "C" void SigmaGFilteredIndicesCU(float *values, int num_values, float sgl * Return the list of indices from the values array such that those elements * pass the sigmaG filtering defined by percentiles [sgl0, sgl1] with coefficient * sigma_g_coeff and a multiplicative factor of width. + * + * The vector values is passed by value to create a local copy which will be modified by + * SigmaGFilteredIndicesCU. */ -std::vector sigmaGFilteredIndices(const std::vector &values, float sgl0, float sgl1, - float sigma_g_coeff, float width) { - // Bounds check the percentile values. - assert(sgl0 > 0.0); - assert(sgl1 < 1.0); - - // Allocate space for the input and result. - const int num_values = values.size(); - float values_arr[num_values]; - int idx_array[num_values]; - for (int i = 0; i < num_values; ++i) { - values_arr[i] = values[i]; - } - +std::vector sigmaGFilteredIndices(std::vector values, float sgl0, float sgl1, float sigma_g_coeff, + float width) { + int num_values = values.size(); + std::vector idx_array(num_values, 0); int min_keep_idx = 0; int max_keep_idx = num_values - 1; #ifdef HAVE_CUDA - SigmaGFilteredIndicesCU(values_arr, num_values, sgl0, sgl1, sigma_g_coeff, width, idx_array, + SigmaGFilteredIndicesCU(values.data(), num_values, sgl0, sgl1, sigma_g_coeff, width, idx_array.data(), &min_keep_idx, &max_keep_idx); #else throw std::runtime_error("Non-GPU SigmaGFilteredIndicesCU is not implemented.");