SourceXtractorPlusPlus  0.13
Please provide a description of the project.
SegmentationConfig.cpp
Go to the documentation of this file.
1 
23 #include <iostream>
24 #include <fstream>
25 
26 #include <boost/regex.hpp>
27 using boost::regex;
28 using boost::regex_match;
29 using boost::smatch;
30 
31 #include <boost/algorithm/string.hpp>
32 
34 
36 
39 
42 
43 using namespace Euclid::Configuration;
44 namespace po = boost::program_options;
45 
46 namespace SourceXtractor {
47 
49 
50 static const std::string SEGMENTATION_ALGORITHM {"segmentation-algorithm" };
51 static const std::string SEGMENTATION_DISABLE_FILTERING {"segmentation-disable-filtering" };
52 static const std::string SEGMENTATION_FILTER {"segmentation-filter" };
53 static const std::string SEGMENTATION_LUTZ_WINDOW_SIZE {"segmentation-lutz-window-size" };
54 static const std::string SEGMENTATION_BFS_MAX_DELTA {"segmentation-bfs-max-delta" };
55 
56 SegmentationConfig::SegmentationConfig(long manager_id) : Configuration(manager_id),
57  m_selected_algorithm(Algorithm::UNKNOWN), m_lutz_window_size(0), m_bfs_max_delta(1000) {
58 }
59 
61  return { {"Detection image", {
62  {SEGMENTATION_ALGORITHM.c_str(), po::value<std::string>()->default_value("LUTZ"),
63  "Segmentation algorithm to be used (LUTZ or TILES)"},
64  {SEGMENTATION_DISABLE_FILTERING.c_str(), po::bool_switch(),
65  "Disables filtering"},
66  {SEGMENTATION_FILTER.c_str(), po::value<std::string>()->default_value(""),
67  "Loads a filter"},
68  {SEGMENTATION_LUTZ_WINDOW_SIZE.c_str(), po::value<int>()->default_value(0),
69  "Lutz sliding window size (0=disable)"},
70  {SEGMENTATION_BFS_MAX_DELTA.c_str(), po::value<int>()->default_value(1000),
71  "BFS algorithm max source x/y size (default=1000)"},
72  }}};
73 }
74 
76  auto algorithm_name = boost::to_upper_copy(args.at(SEGMENTATION_ALGORITHM).as<std::string>());
77  if (algorithm_name == "LUTZ") {
79  } else if (algorithm_name == "BFS") {
81  } else {
82  throw Elements::Exception() << "Unknown segmentation algorithm : " << algorithm_name;
83  }
84 
85 
86  if (args.at(SEGMENTATION_DISABLE_FILTERING).as<bool>()) {
87  m_filter = nullptr;
88  } else {
89  auto filter_filename = args.at(SEGMENTATION_FILTER).as<std::string>();
90  if (filter_filename != "") {
91  m_filter = loadFilter(filter_filename);
92  if (m_filter == nullptr)
93  throw Elements::Exception() << "Can not load filter: " << filter_filename;
94  } else {
96  }
97  }
98 
101 }
102 
104 }
105 
107  segConfigLogger.info() << "Using the default segmentation (3x3) filter.";
108  auto convolution_kernel = VectorImage<SeFloat>::create(3, 3);
109  convolution_kernel->setValue(0,0, 1);
110  convolution_kernel->setValue(0,1, 2);
111  convolution_kernel->setValue(0,2, 1);
112 
113  convolution_kernel->setValue(1,0, 2);
114  convolution_kernel->setValue(1,1, 4);
115  convolution_kernel->setValue(1,2, 2);
116 
117  convolution_kernel->setValue(2,0, 1);
118  convolution_kernel->setValue(2,1, 2);
119  convolution_kernel->setValue(2,2, 1);
120 
121  return std::make_shared<BackgroundConvolution>(convolution_kernel, true);
122 }
123 
125  // check for the extension ".fits"
126  std::string fits_ending(".fits");
127  if (filename.length() >= fits_ending.length()
128  && filename.compare (filename.length() - fits_ending.length(), fits_ending.length(), fits_ending)==0) {
129  // load a FITS filter
130  return loadFITSFilter(filename);
131  }
132  else{
133  // load an ASCII filter
134  return loadASCIIFilter(filename);
135  }
136 }
137 
139 
140  // read in the FITS file
141  auto convolution_kernel = FitsReader<SeFloat>::readFile(filename);
142 
143  // give some feedback on the filter
144  segConfigLogger.info() << "Loaded segmentation filter: " << filename << " height: " << convolution_kernel->getHeight() << " width: " << convolution_kernel->getWidth();
145 
146  // return the correct object
147  return std::make_shared<BackgroundConvolution>(convolution_kernel, true);
148 }
149 
150 static bool getNormalization(std::istream& line_stream) {
151  std::string conv, norm_type;
152  line_stream >> conv >> norm_type;
153  if (conv != "CONV") {
154  throw Elements::Exception() << "Unexpected start for ASCII filter: " << conv;
155  }
156  if (norm_type == "NORM") {
157  return true;
158  }
159  else if (norm_type == "NONORM") {
160  return false;
161  }
162 
163  throw Elements::Exception() << "Unexpected normalization type: " << norm_type;
164 }
165 
166 template <typename T>
167 static void extractValues(std::istream& line_stream, std::vector<T>& data) {
168  T value;
169  while (line_stream.good()) {
170  line_stream >> value;
171  data.push_back(value);
172  }
173 }
174 
176  std::ifstream file;
177 
178  // open the file and check
179  file.open(filename);
180  if (!file.good() || !file.is_open()){
181  throw Elements::Exception() << "Can not load filter: " << filename;
182  }
183 
184  enum class LoadState {
185  STATE_START,
186  STATE_FIRST_LINE,
187  STATE_OTHER_LINES
188  };
189 
190  LoadState state = LoadState::STATE_START;
191  bool normalize = false;
192  std::vector<SeFloat> kernel_data;
193  unsigned int kernel_width = 0;
194 
195  while (file.good()) {
196  std::string line;
197  std::getline(file, line);
198  line = regex_replace(line, regex("\\s*#.*"), std::string(""));
199  line = regex_replace(line, regex("\\s*$"), std::string(""));
200  if (line.size() == 0) {
201  continue;
202  }
203 
204  std::stringstream line_stream(line);
205 
206  switch (state) {
207  case LoadState::STATE_START:
208  normalize = getNormalization(line_stream);
209  state = LoadState::STATE_FIRST_LINE;
210  break;
211  case LoadState::STATE_FIRST_LINE:
212  extractValues(line_stream, kernel_data);
213  kernel_width = kernel_data.size();
214  state = LoadState::STATE_OTHER_LINES;
215  break;
216  case LoadState::STATE_OTHER_LINES:
217  extractValues(line_stream, kernel_data);
218  break;
219  }
220  }
221 
222  // compute the dimensions and create the kernel
223  auto kernel_height = kernel_data.size() / kernel_width;
224  auto convolution_kernel = VectorImage<SeFloat>::create(kernel_width, kernel_height, kernel_data);
225 
226  // give some feedback on the filter
227  segConfigLogger.info() << "Loaded segmentation filter: " << filename << " width: " << convolution_kernel->getWidth() << " height: " << convolution_kernel->getHeight();
228 
229  // return the correct object
230  return std::make_shared<BackgroundConvolution>(convolution_kernel, normalize);
231 }
232 
233 } // SourceXtractor namespace
SourceXtractor::SegmentationConfig::m_filter
std::shared_ptr< DetectionImageFrame::ImageFilter > m_filter
Definition: SegmentationConfig.h:84
std::string
STL class.
std::shared_ptr
STL class.
SourceXtractor::SegmentationConfig::preInitialize
void preInitialize(const UserValues &args) override
Definition: SegmentationConfig.cpp:75
conf.filename
string filename
Definition: conf.py:63
Elements::Logging
SegmentationConfig.h
std::vector
STL class.
BackgroundConvolution.h
std::string::length
T length(T... args)
std::stringstream
STL class.
SourceXtractor::SegmentationConfig::getProgramOptions
std::map< std::string, Configuration::OptionDescriptionList > getProgramOptions() override
Definition: SegmentationConfig.cpp:60
SourceXtractor::SegmentationConfig::m_bfs_max_delta
int m_bfs_max_delta
Definition: SegmentationConfig.h:87
SourceXtractor::SegmentationConfig::m_lutz_window_size
int m_lutz_window_size
Definition: SegmentationConfig.h:86
SourceXtractor::SegmentationConfig::getDefaultFilter
std::shared_ptr< DetectionImageFrame::ImageFilter > getDefaultFilter() const
Definition: SegmentationConfig.cpp:106
VectorImage.h
Euclid::Configuration
SourceXtractor::SEGMENTATION_LUTZ_WINDOW_SIZE
static const std::string SEGMENTATION_LUTZ_WINDOW_SIZE
Definition: SegmentationConfig.cpp:53
std::vector::push_back
T push_back(T... args)
SourceXtractor::extractValues
static void extractValues(std::istream &line_stream, std::vector< T > &data)
Definition: SegmentationConfig.cpp:167
SourceXtractor
Definition: Aperture.h:30
SourceXtractor::SegmentationConfig::m_selected_algorithm
Algorithm m_selected_algorithm
Definition: SegmentationConfig.h:83
std::map::at
T at(T... args)
Exception.h
std::string::c_str
T c_str(T... args)
SourceXtractor::SEGMENTATION_DISABLE_FILTERING
static const std::string SEGMENTATION_DISABLE_FILTERING
Definition: SegmentationConfig.cpp:51
SourceXtractor::segConfigLogger
static Elements::Logging segConfigLogger
Definition: SegmentationConfig.cpp:48
SourceXtractor::SegmentationConfig::loadFilter
std::shared_ptr< DetectionImageFrame::ImageFilter > loadFilter(const std::string &filename) const
Definition: SegmentationConfig.cpp:124
SourceXtractor::VectorImage::create
static std::shared_ptr< VectorImage< T > > create(Args &&... args)
Definition: VectorImage.h:89
Elements::Exception
std::ifstream::open
T open(T... args)
SourceXtractor::getNormalization
static bool getNormalization(std::istream &line_stream)
Definition: SegmentationConfig.cpp:150
std::map
STL class.
std::regex
std::istream::good
T good(T... args)
Elements::Logging::info
void info(const std::string &logMessage)
Elements::Logging::getLogger
static Logging getLogger(const std::string &name="")
FitsReader.h
SourceXtractor::SegmentationConfig::loadFITSFilter
std::shared_ptr< DetectionImageFrame::ImageFilter > loadFITSFilter(const std::string &filename) const
Definition: SegmentationConfig.cpp:138
SourceXtractor::SegmentationConfig::Algorithm::BFS
@ BFS
Euclid::Table::FitsReader
std::getline
T getline(T... args)
SourceXtractor::SegmentationConfig::Algorithm::LUTZ
@ LUTZ
SourceXtractor::SEGMENTATION_BFS_MAX_DELTA
static const std::string SEGMENTATION_BFS_MAX_DELTA
Definition: SegmentationConfig.cpp:54
SourceXtractor::SegmentationConfig::loadASCIIFilter
std::shared_ptr< DetectionImageFrame::ImageFilter > loadASCIIFilter(const std::string &filename) const
Definition: SegmentationConfig.cpp:175
Euclid::Configuration::Configuration
SourceXtractor::SEGMENTATION_FILTER
static const std::string SEGMENTATION_FILTER
Definition: SegmentationConfig.cpp:52
std::istream
STL class.
ConfigManager.h
SourceXtractor::SegmentationConfig::initialize
void initialize(const UserValues &args) override
Definition: SegmentationConfig.cpp:103
std::regex_replace
T regex_replace(T... args)
std::ifstream::is_open
T is_open(T... args)
SourceXtractor::SegmentationConfig::Algorithm
Algorithm
Definition: SegmentationConfig.h:39
SourceXtractor::SEGMENTATION_ALGORITHM
static const std::string SEGMENTATION_ALGORITHM
Definition: SegmentationConfig.cpp:50
std::ifstream
STL class.