SourceXtractorPlusPlus  0.11
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
78 void WeightImageConfig::initialize(const UserValues& args) {
79  m_absolute_weight = args.find(WEIGHT_ABSOLUTE)->second.as<bool>();
80  m_symmetry_usage = args.find(WEIGHT_SYMMETRYUSAGE)->second.as<bool>();
81  auto weight_image_filename = args.find(WEIGHT_IMAGE)->second.as<std::string>();
82  if (weight_image_filename != "") {
84  }
85 
86  auto weight_type_name = boost::to_upper_copy(args.at(WEIGHT_TYPE).as<std::string>());
87  if (weight_type_name == "NONE") {
89  } else if (weight_type_name == "BACKGROUND") {
91  } else if (weight_type_name == "RMS") {
93  } else if (weight_type_name == "VARIANCE") {
95  } else if (weight_type_name == "WEIGHT") {
97  } else {
98  throw Elements::Exception() << "Unknown weight map type : " << weight_type_name;
99  }
100 
101  m_weight_scaling = args.find(WEIGHT_SCALING)->second.as<double>();
102 
103  if (m_weight_image != nullptr) {
105 
106  auto flux_scale = getDependency<DetectionImageConfig>().getOriginalFluxScale();
107  if (flux_scale != 1. && m_absolute_weight) {
109  }
110  }
111 
112  if (args.count(WEIGHT_THRESHOLD) != 0) {
113  auto threshold = args.find(WEIGHT_THRESHOLD)->second.as<double>();
114  switch (m_weight_type) {
115  default:
118  m_weight_threshold = threshold * threshold;
119  break;
121  m_weight_threshold = threshold;
122  break;
124  if (threshold>0)
125  m_weight_threshold = 1.0 / threshold;
126  else
128  break;
129  }
130  } else {
132  }
133 
134  // some safeguards that the user provides reasonable input and gets defined results
135  if (weight_image_filename != "" && m_weight_type == WeightType::WEIGHT_TYPE_FROM_BACKGROUND)
136  throw Elements::Exception() << "Please give an appropriate weight type for image: " << weight_image_filename;
137  if (weight_image_filename != "" && m_weight_type == WeightType::WEIGHT_TYPE_NONE)
138  throw Elements::Exception() << "Please give an appropriate weight type for image: " << weight_image_filename;
139  if (m_absolute_weight && weight_image_filename == "")
140  throw Elements::Exception() << "Setting absolute weight but providing *no* weight image does not make sense.";
141 }
142 
143 class WeightMapImageSource : public ProcessingImageSource<WeightImage::PixelType> {
144 public:
146  : ProcessingImageSource<DetectionImage::PixelType>(image), m_weight_type(weight_type), m_scaling(scaling)
147  {}
148 
149 protected:
150 
151  std::string getRepr() const override {
152  return "WeightMapImageSource(" + getImageRepr() + ")";
153  }
154 
156  ImageTile<DetectionImage::PixelType>& tile, int x, int y, int width, int height) const final {
157  auto image_chunk = image->getChunk(x, y, width, height);
158  switch (m_weight_type) {
160  for (int iy = 0; iy < height; iy++) {
161  for (int ix = 0; ix < width; ix++) {
162  auto value = image_chunk->getValue(ix, iy) * m_scaling;
163  tile.getImage()->setValue(ix, iy, value * value);
164  }
165  }
166  break;
168  for (int iy = 0; iy < height; iy++) {
169  for (int ix = 0; ix < width; ix++) {
170  auto value = image_chunk->getValue(ix, iy) * m_scaling;
171  tile.getImage()->setValue(ix, iy, value);
172  }
173  }
174  break;
176  for (int iy = 0; iy < height; iy++) {
177  for (int ix = 0; ix < width; ix++) {
178  auto value = image_chunk->getValue(ix, iy) * m_scaling;
179  if (value > 0) {
180  tile.getImage()->setValue(ix, iy, 1.0 / value);
181  } else {
182  tile.getImage()->setValue(ix, iy, std::numeric_limits<WeightImage::PixelType>::max());
183  }
184  }
185  }
186  break;
187  default:
189  assert(false);
190  break;
191  }
192  }
193 
194 private:
197 };
198 
199 
201 
202  if (weight_type == WeightType::WEIGHT_TYPE_FROM_BACKGROUND) {
203  return nullptr;
204  } else if (weight_type == WeightType::WEIGHT_TYPE_NONE) {
205  return nullptr;
206  } else {
207  auto result_image = BufferedImage<WeightImage::PixelType>::create(std::make_shared<WeightMapImageSource>(weight_image, weight_type, scaling));
208  return result_image;
209  }
210 }
211 
212 }
213 
WeightImage::PixelType m_weight_threshold
WeightMapImageSource(std::shared_ptr< Image< WeightImage::PixelType >> image, WeightImageConfig::WeightType weight_type, WeightImage::PixelType scaling)
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
std::shared_ptr< WeightImage > m_weight_image
STL class.
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > y
STL class.
static std::shared_ptr< WeightImage > convertWeightMap(std::shared_ptr< WeightImage > weight_image, WeightType weight_type, WeightImage::PixelType scaling=1)
static const std::string WEIGHT_ABSOLUTE
std::string getRepr() const override
Human readable representation of this source.
WeightImage::PixelType m_weight_scaling
std::map< std::string, Configuration::OptionDescriptionList > getProgramOptions() override
void generateTile(const std::shared_ptr< Image< WeightImage::PixelType >> &image, ImageTile< DetectionImage::PixelType > &tile, int x, int y, int width, int height) const final
void initialize(const UserValues &args) override
T c_str(T...args)
static std::shared_ptr< BufferedImage< T > > create(std::shared_ptr< const ImageSource< T >> source, std::shared_ptr< TileManager > tile_manager=TileManager::getInstance())
Interface representing an image.
Definition: Image.h:43
static const std::string WEIGHT_IMAGE
static const std::string WEIGHT_SCALING
static const std::string WEIGHT_TYPE
WeightImageConfig::WeightType m_weight_type
static const std::string WEIGHT_THRESHOLD
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 const std::string WEIGHT_SYMMETRYUSAGE