SourceXtractorPlusPlus  0.14
Please provide a description of the project.
 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 
44 
45 
46 namespace po = boost::program_options;
47 namespace timer = boost::timer;
48 using namespace Euclid;
49 using namespace SourceXtractor;
50 
52 static Elements::Logging logger = Elements::Logging::getLogger("BenchBackgroundModel");
53 
59 private:
62 
63  std::string m_output_bg, m_output_var;
64  std::vector<int> m_cell_size, m_smooth;
65 
66  enum class Algorithm {
67  SIMPLE, SE2, NG
68  } m_algorithm;
69 
71  {"simple", Algorithm::SIMPLE},
72  {"se2", Algorithm::SE2},
73  {"ng", Algorithm::NG}
74  };
75 
80  const std::string& default_suffix) {
81  auto i = args.find(var);
82  if (i != args.end())
83  return i->second.as<std::string>();
84  auto input = boost::filesystem::path(m_detection_config.getDetectionImagePath());
85  auto basename = input.leaf();
86  while (!basename.extension().empty())
87  basename = basename.stem();
88  auto algo = args.at("algorithm").as<std::string>();
89  auto output_area = boost::filesystem::path(args.at("output-area").as<std::string>());
90  auto output = output_area / boost::filesystem::path(basename.native() + "_" + algo + "_" + default_suffix);
91  output.replace_extension("fits");
92  return output.native();
93  }
94 
97  switch (m_algorithm) {
98  case Algorithm::SIMPLE:
99  return Euclid::make_unique<SimpleBackgroundAnalyzer>();
100  case Algorithm::SE2:
101  return Euclid::make_unique<SE2BackgroundLevelAnalyzer>(m_cell_size, m_smooth, m_weight_config.getWeightType());
102  case Algorithm::NG:
103  return Euclid::make_unique<SEBackgroundLevelAnalyzer>(m_cell_size, m_smooth, m_weight_config.getWeightType());
104  }
105  return nullptr;
106  }
107 
108 public:
109  BenchBackgroundModel() : m_detection_config(config_manager_id), m_weight_config(config_manager_id),
110  m_algorithm(Algorithm::SIMPLE) {
111  }
112 
113  po::options_description defineSpecificProgramOptions() override {
114  po::options_description options;
115 
117  config_manager.registerConfiguration<DetectionImageConfig>();
118  config_manager.registerConfiguration<WeightImageConfig>();
119 
120  options.add(config_manager.closeRegistration());
121  options.add_options()
122  ("output", po::value<std::string>(), "Output image for the background")
123  ("output-variance", po::value<std::string>(), "Output image for the variance")
124  ("output-area", po::value<std::string>()->default_value(boost::filesystem::temp_directory_path().native()), "Output area")
125  ("algorithm", po::value<std::string>()->required(), "Algorithm to use: Simple, SE2")
126  ("cell-size", po::value<std::string>()->default_value("64"), "Cell size for the histogram")
127  ("smooth-size", po::value<std::string>()->default_value("3"), "Box size for the median filtering")
128  ("no-write", po::bool_switch(), "Do not write the image (skip interpolation)")
129  ("tile-size", po::value<int>()->default_value(512), "Tile size")
130  ("tile-memory", po::value<int>()->default_value(2048), "Tile memory limit");
131 
132  return options;
133  }
134 
137  config_manager.initialize(args);
138 
139  m_detection_config = config_manager.getConfiguration<DetectionImageConfig>();
140  m_weight_config = config_manager.getConfiguration<WeightImageConfig>();
141 
142  m_output_bg = getOutputPath(args, "output", "background");
143  m_output_var = getOutputPath(args, "output-variance", "variance");
144 
145  auto algorithm_str = args.at("algorithm").as<std::string>();
146  boost::to_lower(algorithm_str);
147  if (s_algo_map.count(algorithm_str) > 0)
148  m_algorithm = s_algo_map[algorithm_str];
149  else
150  throw Elements::Exception() << "Unknown algorithm " << algorithm_str;
151 
152  m_cell_size = stringToVector<int>(args.at("cell-size").as<std::string>());
153  m_smooth = stringToVector<int>(args.at("smooth-size").as<std::string>());
154 
155  auto tile_size = args.at("tile-size").as<int>();
156  auto tile_memory = args.at("tile-memory").as<int>();
157  TileManager::getInstance()->setOptions(tile_size, tile_size, tile_memory);
158  }
159 
161  configure(args);
162 
163  logger.info() << "Input image: " << m_detection_config.getDetectionImagePath();
164  logger.info() << "Destination background image: " << m_output_bg;
165  logger.info() << "Destination variance image: " << m_output_var;
166 
167  auto bg_analyzer = getBackgroundAnalyzer();
168  assert (bg_analyzer != nullptr);
169 
170  auto image = m_detection_config.getDetectionImage();
171  auto weight_image = m_weight_config.getWeightImage();
172  auto threshold = m_weight_config.getWeightThreshold();
173 
174  auto mask = ConstantImage<unsigned char>::create(image->getWidth(), image->getHeight(), false);
175 
176  logger.info() << "Starting analysis";
177  if (weight_image)
178  logger.info() << "Weight image " << weight_image->getRepr();
179  else
180  logger.info() << "No weight image";
181 
182  timer::cpu_timer timer;
183  auto bg_model = bg_analyzer->analyzeBackground(image, weight_image, mask, threshold);
184 
185  if (!args.at("no-write").as<bool>()) {
186  logger.info() << "Writing background";
187  FitsWriter::writeFile(*bg_model.getLevelMap(), m_output_bg, m_detection_config.getCoordinateSystem());
188  logger.info() << "Writing variance map";
189  FitsWriter::writeFile(*bg_model.getVarianceMap(), m_output_var, m_detection_config.getCoordinateSystem());
190  }
191 
192  timer.stop();
193  logger.info() << "Done!";
194  std::cout << "Scaling factor: " << bg_model.getScalingFactor() << std::endl;
195  std::cout << "Elapsed: " << timer.elapsed().wall << std::endl;
196 
197  TileManager::getInstance()->saveAllTiles();
198  FitsFileManager::getInstance()->closeAllFiles();
199 
200  return Elements::ExitCode::OK;
201  }
202 };
203 
std::unique_ptr< BackgroundAnalyzer > getBackgroundAnalyzer()
WeightImageConfig m_weight_config
DetectionImageConfig m_detection_config
static ConfigManager & getInstance(long id)
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="")