29 #include <boost/program_options.hpp>
30 #include <boost/algorithm/string/predicate.hpp>
83 namespace po = boost::program_options;
84 namespace fs = boost::filesystem;
98 m_list.push_back(group);
107 m_list.push_back(source);
124 ::setenv(
"LC_ALL",
"C", 1);
135 bool omp_env_present =
getenv(
"OMP_NUM_THREADS") ||
getenv(
"OMP_DYNAMIC");
136 bool mkl_env_present =
getenv(
"MKL_NUM_THREADS") ||
getenv(
"MKL_DYNAMIC");
137 if (!omp_env_present && !mkl_env_present) {
139 void (*set_num_threads)(int) =
reinterpret_cast<void (*)(
int)
>(dlsym(RTLD_DEFAULT,
"MKL_Set_Num_Threads"));
140 void (*set_dynamic)(int) =
reinterpret_cast<void (*)(
int)
>(dlsym(RTLD_DEFAULT,
"MKL_Set_Dynamic"));
141 if (set_num_threads) {
142 logger.debug() <<
"Disabling multithreading";
146 logger.debug() <<
"Disabling dynamic multithreading";
161 std::make_shared<SourceWithOnDemandPropertiesFactory>(task_provider);
163 std::make_shared<SourceGroupWithOnDemandPropertiesFactory>(task_provider);
170 bool config_initialized =
false;
176 : plugin_manager { task_factory_registry, output_registry,
config_manager_id, plugin_path, plugin_list } {
183 if (!config_initialized) {
197 segmentation_factory.reportConfigDependencies(config_manager);
198 partition_factory.reportConfigDependencies(config_manager);
199 grouping_factory.reportConfigDependencies(config_manager);
200 deblending_factory.reportConfigDependencies(config_manager);
201 measurement_factory.reportConfigDependencies(config_manager);
202 output_factory.reportConfigDependencies(config_manager);
204 config_parameters.add(config_manager.closeRegistration());
205 config_initialized =
true;
207 return config_parameters;
212 auto options = getConfigParameters();
215 "List the possible output properties for the given input parameters and exit");
217 "Show the columns created for each property");
219 "Show the columns created for each property, for the given configuration");
221 "Dump parameters with default values into a configuration file");
222 progress_printer_factory.addOptions(options);
225 po::positional_options_description p;
226 p.add(
"python-arg", -1);
232 template <
typename T>
234 out << opt.long_name() <<
'=' << boost::any_cast<T>(default_value) <<
std::endl;
238 template <
typename T>
240 auto values = boost::any_cast<std::vector<T>>(default_value);
241 if (values.empty()) {
242 out <<
"# " << opt.long_name() <<
'=' <<
std::endl;
245 for (
const auto& v : values)
246 out << opt.long_name() <<
'=' << v <<
std::endl;
254 {
typeid(bool), &writeDefault<bool>},
255 {
typeid(int), &writeDefault<int>},
256 {
typeid(double), &writeDefault<double>},
260 decltype(printers)::const_iterator printer;
262 auto config_parameters = getConfigParameters();
263 for (
const auto& p : config_parameters.options()) {
264 boost::any default_value;
267 if (!p->semantic()->apply_default(default_value)) {
270 else if ((printer = printers.find(default_value.type())) == printers.end()) {
271 std::cout <<
'#' << p->long_name() <<
"=<Unknown type " << default_value.type().name() <<
'>' <<
std::endl;
274 printer->second(
std::cout, *p, default_value);
295 return Elements::ExitCode::OK;
300 return Elements::ExitCode::OK;
305 return Elements::ExitCode::OK;
313 if (args.
find(
"config-file") != args.
end()) {
314 auto cfg_file = args.
at(
"config-file").as<fs::path>();
315 if (cfg_file !=
"" && !fs::exists(cfg_file)) {
316 throw Elements::Exception() <<
"The configuration file '" << cfg_file <<
"' does not exist";
321 progress_printer_factory.configure(args);
322 auto progress_mediator = progress_printer_factory.createProgressMediator();
326 config_manager.initialize(args);
329 auto memory_config = config_manager.getConfiguration<
MemoryConfig>();
331 memory_config.getTileSize(), memory_config.getTileMaxMemory());
335 task_factory_registry->
configure(config_manager);
338 segmentation_factory.configure(config_manager);
339 partition_factory.configure(config_manager);
340 grouping_factory.configure(config_manager);
341 deblending_factory.configure(config_manager);
342 measurement_factory.configure(config_manager);
343 output_factory.configure(config_manager);
347 return Elements::ExitCode::OK;
350 auto detection_image = config_manager.getConfiguration<
DetectionImageConfig>().getDetectionImage();
351 auto detection_image_path = config_manager.getConfiguration<
DetectionImageConfig>().getDetectionImagePath();
352 auto weight_image = config_manager.getConfiguration<
WeightImageConfig>().getWeightImage();
353 bool is_weight_absolute = config_manager.getConfiguration<
WeightImageConfig>().isWeightAbsolute();
354 auto weight_threshold = config_manager.getConfiguration<
WeightImageConfig>().getWeightThreshold();
356 auto detection_image_coordinate_system = config_manager.getConfiguration<
DetectionImageConfig>().getCoordinateSystem();
358 auto detection_image_saturation = config_manager.getConfiguration<
DetectionImageConfig>().getSaturation();
360 auto segmentation = segmentation_factory.createSegmentation();
369 prefetcher = std::make_shared<Prefetcher>(thread_pool);
373 auto partition = partition_factory.getPartition();
374 auto source_grouping = grouping_factory.createGrouping();
385 auto sorter = std::make_shared<Sorter>();
391 segmentation->Observable<ProcessSourcesEvent>::addObserver(prefetcher);
392 prefetcher->Observable<ProcessSourcesEvent>::addObserver(source_grouping);
397 segmentation->Observable<ProcessSourcesEvent>::addObserver(source_grouping);
401 source_grouping->addObserver(deblending);
404 sorter->addObserver(output);
406 segmentation->Observable<SegmentationProgress>::addObserver(progress_mediator->getSegmentationObserver());
408 deblending->
addObserver(progress_mediator->getDeblendingObserver());
409 measurement->
addObserver(progress_mediator->getMeasurementObserver());
414 std::make_shared<DetectionIdCheckImage>());
418 std::make_shared<SourceIdCheckImage>());
422 std::make_shared<GroupIdCheckImage>());
426 std::make_shared<MoffatCheckImage>());
429 auto interpolation_gap = config_manager.getConfiguration<
DetectionImageConfig>().getInterpolationGap();
430 auto detection_frame = std::make_shared<DetectionImageFrame>(detection_image, weight_image,
431 weight_threshold, detection_image_coordinate_system, detection_image_gain,
432 detection_image_saturation, interpolation_gap);
433 detection_frame->setLabel(boost::filesystem::basename(detection_image_path));
436 auto background_model = background_analyzer->analyzeBackground(detection_frame->getOriginalImage(), weight_image,
443 detection_frame->setBackgroundLevel(background_model.getLevelMap(), background_model.getMedianRms());
445 if (weight_image !=
nullptr) {
446 if (is_weight_absolute) {
447 detection_frame->setVarianceMap(weight_image);
450 detection_frame->setVarianceMap(scaled_image);
453 detection_frame->setVarianceMap(background_model.getVarianceMap());
458 const auto& background_config = config_manager.getConfiguration<
BackgroundConfig>();
461 if (background_config.isBackgroundLevelAbsolute()) {
463 detection_image->getWidth(), detection_image->getHeight(), background_config.getBackgroundLevel());
465 detection_frame->setBackgroundLevel(background, 0.);
469 if (background_config.isDetectionThresholdAbsolute()) {
470 detection_frame->setDetectionThreshold(background_config.getDetectionThreshold());
481 segmentation->processFrame(detection_frame);
484 logger.error() <<
"Failed to process the frame! " <<
e.what();
486 return Elements::ExitCode::NOT_OK;
500 size_t n_writen_rows = output->
flush();
502 progress_mediator->done();
504 if (n_writen_rows > 0) {
505 logger.info() << n_writen_rows <<
" sources detected";
507 logger.info() <<
"NO SOURCES DETECTED";
510 return Elements::ExitCode::OK;
519 m_plugin_path(plugin_path), m_plugin_list(plugin_list) {
527 auto options = config_manager.closeRegistration();
529 options.add_options()(
"*", po::value<std::vector<std::string>>());
535 config_manager.initialize(args);
537 m_plugin_path =
conf.getPluginPath();
538 m_plugin_list =
conf.getPluginList();
539 return Elements::ExitCode::OK;
552 for (
int i = 0; i < argc; ++i) {
554 if (option ==
"--config-file") {
558 if (boost::starts_with(option,
"--config-file=")) {
561 if (option ==
"--plugin-directory") {
562 plugin_options_input.
emplace_back(
"--plugin-directory");
565 if (boost::starts_with(option,
"--plugin-directory=")) {
568 if (option ==
"--plugin") {
572 if (boost::starts_with(option,
"--plugin=")) {
585 if (local_env[
"ELEMENTS_CONF_PATH"].empty()) {
586 local_env[
"ELEMENTS_CONF_PATH"] =
".:/etc";
588 local_env[
"ELEMENTS_CONF_PATH"] =
".:" + local_env[
"ELEMENTS_CONF_PATH"] +
":/etc";
605 plugin_options_input.emplace_back(
"--log-level");
606 plugin_options_input.emplace_back(
"ERROR");
609 int argc_tmp = plugin_options_input.size();
611 for (
unsigned int i = 0; i < plugin_options_input.size(); ++i) {
612 auto& option_str = plugin_options_input[i];
613 argv_tmp[i] = option_str.
data();
617 plugin_options_program.run(argc_tmp,
const_cast<char **
>(argv_tmp.
data()));
628 logger.fatal() <<
"Unknown exception type!";
629 logger.fatal() <<
"Please, report this as a bug";