SourceXtractorPlusPlus 1.0.3
SourceXtractor++, the next generation SExtractor
Loading...
Searching...
No Matches
BenchBackgroundModel.cpp
Go to the documentation of this file.
1
17
23
24#include <boost/algorithm/string.hpp>
25#include <boost/timer/timer.hpp>
26
27#include <ElementsKernel/Exception.h>
28#include <ElementsKernel/Logging.h>
29#include <ElementsKernel/Program.h>
30#include <ElementsKernel/Main.h>
31#include <Configuration/ConfigManager.h>
32#include <Configuration/Utils.h>
33#include <AlexandriaKernel/memory_tools.h>
34#include <AlexandriaKernel/StringUtils.h>
35
43
44
45namespace po = boost::program_options;
46namespace timer = boost::timer;
47using namespace Euclid;
48using namespace SourceXtractor;
49
51static Elements::Logging logger = Elements::Logging::getLogger("BenchBackgroundModel");
52
58private:
61
64
68
73
78 const std::string& default_suffix) {
79 auto i = args.find(var);
80 if (i != args.end())
81 return i->second.as<std::string>();
82 auto input = boost::filesystem::path(m_detection_config.getDetectionImagePath());
83 auto basename = input.filename();
84 while (!basename.extension().empty())
85 basename = basename.stem();
86 auto algo = args.at("algorithm").as<std::string>();
87 auto output_area = boost::filesystem::path(args.at("output-area").as<std::string>());
88 auto output = output_area / boost::filesystem::path(basename.native() + "_" + algo + "_" + default_suffix);
89 output.replace_extension("fits");
90 return output.native();
91 }
92
103
104public:
108
109 po::options_description defineSpecificProgramOptions() override {
110 po::options_description options;
111
113 config_manager.registerConfiguration<DetectionImageConfig>();
114 config_manager.registerConfiguration<WeightImageConfig>();
115
116 options.add(config_manager.closeRegistration());
117 options.add_options()
118 ("output", po::value<std::string>(), "Output image for the background")
119 ("output-variance", po::value<std::string>(), "Output image for the variance")
120 ("output-area", po::value<std::string>()->default_value(boost::filesystem::temp_directory_path().native()), "Output area")
121 ("algorithm", po::value<std::string>()->required(), "Algorithm to use: Simple, ng")
122 ("cell-size", po::value<std::string>()->default_value("64"), "Cell size for the histogram")
123 ("smooth-size", po::value<std::string>()->default_value("3"), "Box size for the median filtering")
124 ("no-write", po::bool_switch(), "Do not write the image (skip interpolation)")
125 ("tile-size", po::value<int>()->default_value(512), "Tile size")
126 ("tile-memory", po::value<int>()->default_value(2048), "Tile memory limit");
127
128 return options;
129 }
130
133 config_manager.initialize(args);
134
135 m_detection_config = config_manager.getConfiguration<DetectionImageConfig>();
136 m_weight_config = config_manager.getConfiguration<WeightImageConfig>();
137
138 m_output_bg = getOutputPath(args, "output", "background");
139 m_output_var = getOutputPath(args, "output-variance", "variance");
140
141 auto algorithm_str = args.at("algorithm").as<std::string>();
142 boost::to_lower(algorithm_str);
143 if (s_algo_map.count(algorithm_str) > 0)
144 m_algorithm = s_algo_map[algorithm_str];
145 else
146 throw Elements::Exception() << "Unknown algorithm " << algorithm_str;
147
148 m_cell_size = stringToVector<int>(args.at("cell-size").as<std::string>());
149 m_smooth = stringToVector<int>(args.at("smooth-size").as<std::string>());
150
151 auto tile_size = args.at("tile-size").as<int>();
152 auto tile_memory = args.at("tile-memory").as<int>();
153 TileManager::getInstance()->setOptions(tile_size, tile_size, tile_memory);
154 }
155
157 configure(args);
158
159 logger.info() << "Input image: " << m_detection_config.getDetectionImagePath();
160 logger.info() << "Destination background image: " << m_output_bg;
161 logger.info() << "Destination variance image: " << m_output_var;
162
163 auto bg_analyzer = getBackgroundAnalyzer();
164 assert (bg_analyzer != nullptr);
165
166 auto image = m_detection_config.getDetectionImage();
167 auto weight_image = m_weight_config.getWeightImage();
168 auto threshold = m_weight_config.getWeightThreshold();
169
170 auto mask = ConstantImage<unsigned char>::create(image->getWidth(), image->getHeight(), false);
171
172 logger.info() << "Starting analysis";
173 if (weight_image)
174 logger.info() << "Weight image " << weight_image->getRepr();
175 else
176 logger.info() << "No weight image";
177
178 timer::cpu_timer timer;
179 auto bg_model = bg_analyzer->analyzeBackground(image, weight_image, mask, threshold);
180
181 if (!args.at("no-write").as<bool>()) {
182 logger.info() << "Writing background";
183 FitsWriter::writeFile(*bg_model.getLevelMap(), m_output_bg, m_detection_config.getCoordinateSystem());
184 logger.info() << "Writing variance map";
185 FitsWriter::writeFile(*bg_model.getVarianceMap(), m_output_var, m_detection_config.getCoordinateSystem());
186 }
187
188 timer.stop();
189 logger.info() << "Done!";
190 std::cout << "Scaling factor: " << bg_model.getScalingFactor() << std::endl;
191 std::cout << "Elapsed: " << timer.elapsed().wall << std::endl;
192
193 TileManager::getInstance()->saveAllTiles();
194
196 }
197};
198
static long config_manager_id
static Elements::Logging logger
T at(T... args)
DetectionImageConfig m_detection_config
std::string getOutputPath(const std::map< std::string, po::variable_value > &args, const std::string &var, const std::string &default_suffix)
WeightImageConfig m_weight_config
std::vector< int > m_cell_size
enum BenchBackgroundModel::Algorithm m_algorithm
Elements::ExitCode mainMethod(std::map< std::string, po::variable_value > &args) override
po::options_description defineSpecificProgramOptions() override
std::unique_ptr< BackgroundAnalyzer > getBackgroundAnalyzer()
void configure(const std::map< std::string, po::variable_value > &args)
std::vector< int > m_smooth
std::map< std::string, Algorithm > s_algo_map
static Logging getLogger(const std::string &name="")
static ConfigManager & getInstance(long id)
static std::shared_ptr< ConstantImage< T > > create(int width, int height, T constant_value)
static void writeFile(const Image< T > &image, const std::string &filename, const std::shared_ptr< CoordinateSystem > coord_system=nullptr, bool append=false)
Definition FitsWriter.h:54
static std::shared_ptr< TileManager > getInstance()
T end(T... args)
T endl(T... args)
T find(T... args)
#define MAIN_FOR(ELEMENTS_PROGRAM_NAME)
long getUniqueManagerId() noexcept
static Elements::Logging logger
std::vector< T > stringToVector(std::string str, const std::string &separators=std::string(", "))
std::unique_ptr< T > make_unique(Args &&... args)