Skip to content
Snippets Groups Projects
Select Git revision
  • c2478a598e2346f44cc4b4dc7a8fbf008c0885dd
  • main default protected
  • pixel-fitter
  • remove-pybind11
  • multiscale_cuda
  • profiling
  • wavelet-deconvolution
  • ast-1102-nanobind
  • temp-fix-for-local-rms-multiscale-crash
  • ast-943-use-central-frequency-only
  • add-sphinx
  • test-radler-as-static
12 results

test_python_deconvolution.cc

  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    test_python_deconvolution.cc 2.86 KiB
    // SPDX-License-Identifier: LGPL-3.0-only
    
    #include "radler.h"
    
    #include <filesystem>
    #include <fstream>
    #include <string>
    
    #include <boost/test/unit_test.hpp>
    
    #include <aocommon/image.h>
    #include <aocommon/logger.h>
    
    #include "settings.h"
    
    namespace radler {
    
    namespace {
    const std::string kPythonFilename = "tmp-error-reporting-test.py";
    const std::size_t kWidth = 64;
    const std::size_t kHeight = 64;
    const double kPixelScale = 1.0 / 60.0 * (M_PI / 180.0);  // 1amin in rad
    
    class PythonFileFixture {
     public:
      PythonFileFixture()
          : psf_image(kWidth, kHeight, 0.0),
            residual_image(kWidth, kHeight, 0.0),
            model_image(kWidth, kHeight, 0.0) {
        settings.trimmed_image_width = kWidth;
        settings.trimmed_image_height = kHeight;
        settings.pixel_scale.x = kPixelScale;
        settings.pixel_scale.y = kPixelScale;
        settings.minor_iteration_count = 1000;
        settings.absolute_threshold = 1.0e-8;
        settings.algorithm_type = AlgorithmType::kPython;
        settings.python.filename = kPythonFilename;
      }
    
      ~PythonFileFixture() { std::filesystem::remove(kPythonFilename); }
    
      void Write(const std::string& contents) const {
        std::ofstream python_file(kPythonFilename);
        python_file << contents;
      }
    
      Settings settings;
      aocommon::Image psf_image;
      aocommon::Image residual_image;
      aocommon::Image model_image;
    };
    
    }  // namespace
    
    BOOST_AUTO_TEST_SUITE(python_deconvolution)
    
    BOOST_FIXTURE_TEST_CASE(non_existent_file, PythonFileFixture) {
      BOOST_CHECK_THROW(
          Radler(settings, psf_image, residual_image, model_image, 0.0),
          std::runtime_error);
    }
    
    BOOST_FIXTURE_TEST_CASE(bad_file, PythonFileFixture) {
      // A syntax error or direct raise may throw an exception in the constructor of
      // Radler
      Write(R"(#! /usr/bin/python
    
    raise RuntimeError("This should give an error during construction")
    )");
      try {
        Radler(settings, psf_image, residual_image, model_image, 0.0);
        // Should have thrown
        BOOST_ASSERT(false);
      } catch (std::runtime_error& e) {
        const std::string what = e.what();
        BOOST_CHECK_NE(what.find("This should give an error"), std::string::npos);
      }
    }
    
    BOOST_FIXTURE_TEST_CASE(error_reporting, PythonFileFixture) {
      Write(R"(#! /usr/bin/python
    
    def deconvolve(residual, model, psf, meta):
      raise RuntimeError("This is a test to see if WSClean handles a raise correctly")
    )");
    
      aocommon::Logger::SetVerbosity(
          aocommon::Logger::VerbosityLevel::kQuietVerbosity);
    
      Radler radler(settings, psf_image, residual_image, model_image, 0.0);
      try {
        bool reached_threshold = false;
        const std::size_t iteration_number = 1;
        radler.Perform(reached_threshold, iteration_number);
        // Should have thrown
        BOOST_ASSERT(false);
      } catch (std::runtime_error& e) {
        const std::string what = e.what();
        BOOST_CHECK_NE(what.find("This is a test"), std::string::npos);
      }
    }
    
    BOOST_AUTO_TEST_SUITE_END()
    
    }  // namespace radler