SourceXtractorPlusPlus  0.19
SourceXtractor++, the next generation SExtractor
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BenchBackgroundModel.cpp
Go to the documentation of this file.
1 
24 #include <boost/algorithm/string.hpp>
25 #include <boost/timer/timer.hpp>
26 
28 #include <ElementsKernel/Logging.h>
29 #include <ElementsKernel/Program.h>
30 #include <ElementsKernel/Main.h>
32 #include <Configuration/Utils.h>
35 
43 
44 
45 namespace po = boost::program_options;
46 namespace timer = boost::timer;
47 using namespace Euclid;
48 using namespace SourceXtractor;
49 
51 static Elements::Logging logger = Elements::Logging::getLogger("BenchBackgroundModel");
52 
58 private:
61 
62  std::string m_output_bg, m_output_var;
63  std::vector<int> m_cell_size, m_smooth;
64 
65  enum class Algorithm {
66  SIMPLE, NG
67  } m_algorithm;
68 
70  {"simple", Algorithm::SIMPLE},
71  {"ng", Algorithm::NG}
72  };
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.leaf();
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 
95  switch (m_algorithm) {
96  case Algorithm::SIMPLE:
97  return Euclid::make_unique<SimpleBackgroundAnalyzer>();
98  case Algorithm::NG:
99  return Euclid::make_unique<SEBackgroundLevelAnalyzer>(m_cell_size, m_smooth, m_weight_config.getWeightType());
100  }
101  return nullptr;
102  }
103 
104 public:
105  BenchBackgroundModel() : m_detection_config(config_manager_id), m_weight_config(config_manager_id),
106  m_algorithm(Algorithm::SIMPLE) {
107  }
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 
195  return Elements::ExitCode::OK;
196  }
197 };
198 
std::unique_ptr< BackgroundAnalyzer > getBackgroundAnalyzer()
WeightImageConfig m_weight_config
DetectionImageConfig m_detection_config
static ConfigManager & getInstance(long id)
long getUniqueManagerId() noexcept
T endl(T...args)
void info(const std::string &logMessage)
T end(T...args)
static Elements::Logging logger
Provides the detection image.
STL class.
T at(T...args)
void configure(const std::map< std::string, po::variable_value > &args)
#define MAIN_FOR(ELEMENTS_PROGRAM_NAME)
std::string getOutputPath(const std::map< std::string, po::variable_value > &args, const std::string &var, const std::string &default_suffix)
po::options_description defineSpecificProgramOptions() override
T find(T...args)
STL class.
std::vector< int > m_smooth
static long config_manager_id
Elements::ExitCode mainMethod(std::map< std::string, po::variable_value > &args) override
static Logging getLogger(const std::string &name="")