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.cpp b/src/kbmod/search/filtering.cpp deleted file mode 100644 index 912a82aa2..000000000 --- a/src/kbmod/search/filtering.cpp +++ /dev/null @@ -1,65 +0,0 @@ -#include "filtering.h" -#include - -namespace search { -#ifdef HAVE_CUDA -/* The filter_kenerls.cu functions. */ -extern "C" void SigmaGFilteredIndicesCU(float *values, int num_values, float sgl0, float sgl1, float sg_coeff, - 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. */ -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]; - } - - 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, - &min_keep_idx, &max_keep_idx); -#else - throw std::runtime_error("Non-GPU SigmaGFilteredIndicesCU is not implemented."); -#endif - - // Copy the result into a vector and return it. - std::vector result; - for (int i = min_keep_idx; i <= max_keep_idx; ++i) { - result.push_back(idx_array[i]); - } - 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/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/kernel_testing_helpers.cpp b/src/kbmod/search/kernel_testing_helpers.cpp new file mode 100644 index 000000000..83b79137e --- /dev/null +++ b/src/kbmod/search/kernel_testing_helpers.cpp @@ -0,0 +1,43 @@ +/* Helper functions for testing functions in the .cu files from Python. */ + +#include + +namespace search { +#ifdef HAVE_CUDA +/* The filter_kenerls.cu functions. */ +extern "C" void SigmaGFilteredIndicesCU(float *values, int num_values, float sgl0, float sgl1, float sg_coeff, + float width, int *idx_array, int *min_keep_idx, int *max_keep_idx); +#endif + +/* 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. + * + * The vector values is passed by value to create a local copy which will be modified by + * SigmaGFilteredIndicesCU. + */ +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.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."); +#endif + + // Copy the result into a vector and return it. + std::vector result; + for (int i = min_keep_idx; i <= max_keep_idx; ++i) { + result.push_back(idx_array[i]); + } + return result; +} + +} /* 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()