SourceXtractorPlusPlus  0.16
Please provide a description of the project.
WeightImageConfig.cpp
Go to the documentation of this file.
1 
17 /*
18  * WeightImageConfig.cpp
19  *
20  * Created on: Oct 7, 2016
21  * Author: mschefer
22  */
23 
24 #include <limits>
25 #include <boost/algorithm/string.hpp>
26 
28 
33 
35 
37 
38 using namespace Euclid::Configuration;
39 namespace po = boost::program_options;
40 
41 namespace SourceXtractor {
42 
43 static const std::string WEIGHT_IMAGE {"weight-image" };
44 static const std::string WEIGHT_TYPE {"weight-type" };
45 static const std::string WEIGHT_ABSOLUTE {"weight-absolute" };
46 static const std::string WEIGHT_SCALING {"weight-scaling" };
47 static const std::string WEIGHT_THRESHOLD {"weight-threshold" };
48 static const std::string WEIGHT_SYMMETRYUSAGE {"weight-use-symmetry" };
49 
50 WeightImageConfig::WeightImageConfig(long manager_id) :
51  Configuration(manager_id),
52  m_weight_type(WeightType::WEIGHT_TYPE_FROM_BACKGROUND),
53  m_absolute_weight(false),
54  m_weight_scaling(1),
55  m_weight_threshold(0),
56  m_symmetry_usage(true) {
57 
58  declareDependency<DetectionImageConfig>();
59 }
60 
62  return { {"Weight image", {
63  {WEIGHT_IMAGE.c_str(), po::value<std::string>()->default_value(""),
64  "Path to a fits format image to be used as weight image."},
65  {WEIGHT_ABSOLUTE.c_str(), po::value<bool>()->default_value(false),
66  "Is the weight map provided as absolute values or relative to background."},
67  {WEIGHT_TYPE.c_str(), po::value<std::string>()->default_value("none"),
68  "Weight image type [none|background|rms|variance|weight]."},
69  {WEIGHT_SCALING.c_str(), po::value<double>()->default_value(1.0),
70  "Weight map scaling factor."},
71  {WEIGHT_THRESHOLD.c_str(), po::value<double>(),
72  "Threshold for pixels to be considered bad pixels. In same units as weight map."},
73  {WEIGHT_SYMMETRYUSAGE.c_str(), po::value<bool>()->default_value(true),
74  "Use object symmetry to replace pixels above the weight threshold for photometry."},
75  }}};
76 }
77 
79  using WeightType = WeightImageConfig::WeightType;
80 
81  switch (weight_type) {
82  default:
83  case WeightType::WEIGHT_TYPE_FROM_BACKGROUND:
84  case WeightType::WEIGHT_TYPE_RMS:
85  return threshold * threshold;
86  case WeightType::WEIGHT_TYPE_VARIANCE:
87  return threshold;
88  case WeightType::WEIGHT_TYPE_WEIGHT:
89  if (threshold > 0)
90  return 1.0 / threshold;
91  else
93  }
94 }
95 
97  static const std::map<std::string, WeightType> WEIGHT_MAP{
101  {"VARIANCE", WeightType::WEIGHT_TYPE_VARIANCE},
103  };
104 
105  m_absolute_weight = args.find(WEIGHT_ABSOLUTE)->second.as<bool>();
106  m_symmetry_usage = args.find(WEIGHT_SYMMETRYUSAGE)->second.as<bool>();
107  auto weight_image_filename = args.find(WEIGHT_IMAGE)->second.as<std::string>();
108  if (weight_image_filename != "") {
110  }
111 
112  auto weight_type_name = boost::to_upper_copy(args.at(WEIGHT_TYPE).as<std::string>());
113  auto weight_iter = WEIGHT_MAP.find(weight_type_name);
114  if (weight_iter == WEIGHT_MAP.end()) {
115  throw Elements::Exception() << "Unknown weight map type : " << weight_type_name;
116  }
117  m_weight_type = weight_iter->second;
118  m_weight_scaling = args.find(WEIGHT_SCALING)->second.as<double>();
119 
120  if (m_weight_image != nullptr) {
122 
123  auto flux_scale = getDependency<DetectionImageConfig>().getOriginalFluxScale();
124  if (flux_scale != 1. && m_absolute_weight) {
126  }
127  }
128 
129  if (args.count(WEIGHT_THRESHOLD) != 0) {
130  auto threshold = args.find(WEIGHT_THRESHOLD)->second.as<double>();
132  } else {
134  }
135 
136  // some safeguards that the user provides reasonable input and gets defined results
137  if (weight_image_filename != "" && m_weight_type == WeightType::WEIGHT_TYPE_FROM_BACKGROUND)
138  throw Elements::Exception() << "Please give an appropriate weight type for image: " << weight_image_filename;
139  if (weight_image_filename != "" && m_weight_type == WeightType::WEIGHT_TYPE_NONE)
140  throw Elements::Exception() << "Please give an appropriate weight type for image: " << weight_image_filename;
141  if (m_absolute_weight && weight_image_filename == "")
142  throw Elements::Exception() << "Setting absolute weight but providing *no* weight image does not make sense.";
143 }
144 
145 class WeightMapImageSource : public ProcessingImageSource<WeightImage::PixelType> {
146 public:
148  : ProcessingImageSource<DetectionImage::PixelType>(image), m_weight_type(weight_type), m_scaling(scaling)
149  {}
150 
151 protected:
152 
153  std::string getRepr() const override {
154  return "WeightMapImageSource(" + getImageRepr() + ")";
155  }
156 
158  ImageTileWithType<WeightImage::PixelType>& tile, int x, int y, int width, int height) const final {
159  auto image_chunk = image->getChunk(x, y, width, height);
160  switch (m_weight_type) {
162  generateFromRms(tile, width, height, *image_chunk);
163  break;
165  generateFromVariance(tile, width, height, *image_chunk);
166  break;
168  generateFromWeight(tile, width, height, *image_chunk);
169  break;
170  default:
172  assert(false);
173  break;
174  }
175  }
176 
178  const ImageChunk<WeightImage::PixelType>& image_chunk) const {
179  auto& tile_image = *tile.getImage();
180  for (int iy = 0; iy < height; iy++) {
181  for (int ix = 0; ix < width; ix++) {
182  auto value = image_chunk.getValue(ix, iy) * m_scaling;
183  if (value > 0) {
184  tile_image.setValue(ix, iy, 1.0 / value);
185  }
186  else {
187  tile_image.setValue(ix, iy, std::numeric_limits<WeightImage::PixelType>::infinity());
188  }
189  }
190  }
191  }
192 
194  const ImageChunk<WeightImage::PixelType>& image_chunk) const {
195  auto& tile_image = *tile.getImage();
196  for (int iy = 0; iy < height; iy++) {
197  for (int ix = 0; ix < width; ix++) {
198  auto value = image_chunk.getValue(ix, iy) * m_scaling;
199  tile_image.setValue(ix, iy, value);
200  }
201  }
202  }
203 
205  const ImageChunk<WeightImage::PixelType>& image_chunk) const {
206  auto& tile_image = *tile.getImage();
207  for (int iy = 0; iy < height; iy++) {
208  for (int ix = 0; ix < width; ix++) {
209  auto value = image_chunk.getValue(ix, iy) * m_scaling;
210  tile_image.setValue(ix, iy, value * value);
211  }
212  }
213  }
214 
215 private:
218 };
219 
220 
223  WeightImage::PixelType scaling) {
224 
225  if (weight_type == WeightType::WEIGHT_TYPE_FROM_BACKGROUND) {
226  return nullptr;
227  }
228  else if (weight_type == WeightType::WEIGHT_TYPE_NONE) {
229  return nullptr;
230  }
231  else {
233  std::make_shared<WeightMapImageSource>(weight_image, weight_type, scaling));
234  return result_image;
235  }
236 }
237 
238 }
239 
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > y
T at(T... args)
T c_str(T... args)
static std::shared_ptr< BufferedImage< T > > create(std::shared_ptr< const ImageSource > source, std::shared_ptr< TileManager > tile_manager=TileManager::getInstance())
T getValue(int x, int y) const
Returns the value of the pixel with the coordinates (x,y)
Definition: ImageChunk.h:56
const std::shared_ptr< VectorImage< T > > & getImage() const
Definition: ImageTile.h:178
Interface representing an image.
Definition: Image.h:43
static std::shared_ptr< ProcessedImage< T, P > > create(std::shared_ptr< const Image< T >> image_a, std::shared_ptr< const Image< T >> image_b)
static std::shared_ptr< WeightImage > convertWeightMap(std::shared_ptr< WeightImage > weight_image, WeightType weight_type, WeightImage::PixelType scaling=1)
WeightImage::PixelType m_weight_threshold
WeightImage::PixelType m_weight_scaling
std::shared_ptr< WeightImage > m_weight_image
void initialize(const UserValues &args) override
std::map< std::string, Configuration::OptionDescriptionList > getProgramOptions() override
void generateFromWeight(ImageTileWithType< WeightImage::PixelType > &tile, int width, int height, const ImageChunk< WeightImage::PixelType > &image_chunk) const
void generateFromRms(ImageTileWithType< WeightImage::PixelType > &tile, int width, int height, const ImageChunk< WeightImage::PixelType > &image_chunk) const
void generateFromVariance(ImageTileWithType< WeightImage::PixelType > &tile, int width, int height, const ImageChunk< WeightImage::PixelType > &image_chunk) const
void generateTile(const std::shared_ptr< Image< WeightImage::PixelType >> &image, ImageTileWithType< WeightImage::PixelType > &tile, int x, int y, int width, int height) const final
WeightMapImageSource(std::shared_ptr< Image< WeightImage::PixelType >> image, WeightImageConfig::WeightType weight_type, WeightImage::PixelType scaling)
WeightImageConfig::WeightType m_weight_type
std::string getRepr() const override
Human readable representation of this source.
T count(T... args)
T find(T... args)
T max(T... args)
static const std::string WEIGHT_IMAGE
static const std::string WEIGHT_TYPE
static WeightImage::PixelType computeWeightThreshold(WeightImageConfig::WeightType weight_type, double threshold)
static const std::string WEIGHT_ABSOLUTE
static const std::string WEIGHT_SCALING
static const std::string WEIGHT_SYMMETRYUSAGE
static const std::string WEIGHT_THRESHOLD