SourceXtractorPlusPlus  0.15
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 
SourceXtractor::ProcessingImageSource< WeightImage::PixelType >::getImageRepr
std::string getImageRepr() const
Definition: ProcessingImageSource.h:70
SourceXtractor::WeightMapImageSource::getRepr
std::string getRepr() const override
Human readable representation of this source.
Definition: WeightImageConfig.cpp:153
std::string
STL class.
std::shared_ptr
STL class.
SourceXtractor::WeightMapImageSource::generateTile
void generateTile(const std::shared_ptr< Image< WeightImage::PixelType >> &image, ImageTileWithType< WeightImage::PixelType > &tile, int x, int y, int width, int height) const final
Definition: WeightImageConfig.cpp:157
SourceXtractor::WeightImageConfig::WeightType::WEIGHT_TYPE_NONE
@ WEIGHT_TYPE_NONE
SourceXtractor::ImageChunk
Definition: ImageChunk.h:34
SourceXtractor::Image::PixelType
T PixelType
Definition: Image.h:47
SourceXtractor::WeightMapImageSource::generateFromVariance
void generateFromVariance(ImageTileWithType< WeightImage::PixelType > &tile, int width, int height, const ImageChunk< WeightImage::PixelType > &image_chunk) const
Definition: WeightImageConfig.cpp:193
std::map::find
T find(T... args)
SourceXtractor::WEIGHT_THRESHOLD
static const std::string WEIGHT_THRESHOLD
Definition: WeightImageConfig.cpp:47
SourceXtractor::WeightImageConfig::WeightType
WeightType
Definition: WeightImageConfig.h:36
SourceXtractor::ImageTileWithType
Definition: ImageTile.h:160
SourceXtractor::WEIGHT_SYMMETRYUSAGE
static const std::string WEIGHT_SYMMETRYUSAGE
Definition: WeightImageConfig.cpp:48
SourceXtractor::ProcessingImageSource
Definition: ProcessingImageSource.h:33
SourceXtractor::WeightImageConfig::m_weight_image
std::shared_ptr< WeightImage > m_weight_image
Definition: WeightImageConfig.h:74
WeightImageConfig.h
SourceXtractor::WeightMapImageSource::m_scaling
WeightImage::PixelType m_scaling
Definition: WeightImageConfig.cpp:217
SourceXtractor::WeightMapImageSource::generateFromRms
void generateFromRms(ImageTileWithType< WeightImage::PixelType > &tile, int width, int height, const ImageChunk< WeightImage::PixelType > &image_chunk) const
Definition: WeightImageConfig.cpp:204
SourceXtractor::WeightImageConfig::WeightType::WEIGHT_TYPE_FROM_BACKGROUND
@ WEIGHT_TYPE_FROM_BACKGROUND
SourceXtractor::Image< WeightImage::PixelType >
SourceXtractor::BufferedImage::create
static std::shared_ptr< BufferedImage< T > > create(std::shared_ptr< const ImageSource > source, std::shared_ptr< TileManager > tile_manager=TileManager::getInstance())
Definition: BufferedImage.cpp:32
Euclid::Configuration
SourceXtractor::WeightImageConfig::WeightType::WEIGHT_TYPE_WEIGHT
@ WEIGHT_TYPE_WEIGHT
SourceXtractor::WEIGHT_TYPE
static const std::string WEIGHT_TYPE
Definition: WeightImageConfig.cpp:44
SourceXtractor::WeightMapImageSource
Definition: WeightImageConfig.cpp:145
SourceXtractor::WEIGHT_SCALING
static const std::string WEIGHT_SCALING
Definition: WeightImageConfig.cpp:46
SourceXtractor::WeightImageConfig::convertWeightMap
static std::shared_ptr< WeightImage > convertWeightMap(std::shared_ptr< WeightImage > weight_image, WeightType weight_type, WeightImage::PixelType scaling=1)
Definition: WeightImageConfig.cpp:222
SourceXtractor
Definition: Aperture.h:30
SourceXtractor::WeightMapImageSource::m_weight_type
WeightImageConfig::WeightType m_weight_type
Definition: WeightImageConfig.cpp:216
std::map::at
T at(T... args)
SourceXtractor::WeightImageConfig::m_weight_scaling
WeightImage::PixelType m_weight_scaling
Definition: WeightImageConfig.h:77
std::string::c_str
T c_str(T... args)
SourceXtractor::WEIGHT_ABSOLUTE
static const std::string WEIGHT_ABSOLUTE
Definition: WeightImageConfig.cpp:45
SourceXtractor::WeightMapImageSource::WeightMapImageSource
WeightMapImageSource(std::shared_ptr< Image< WeightImage::PixelType >> image, WeightImageConfig::WeightType weight_type, WeightImage::PixelType scaling)
Definition: WeightImageConfig.cpp:147
SourceXtractor::WeightImageConfig::getProgramOptions
std::map< std::string, Configuration::OptionDescriptionList > getProgramOptions() override
Definition: WeightImageConfig.cpp:61
Elements::Exception
ProcessedImage.h
std::map
STL class.
SourceXtractor::WeightImageConfig::WeightType::WEIGHT_TYPE_VARIANCE
@ WEIGHT_TYPE_VARIANCE
SourceXtractor::WEIGHT_IMAGE
static const std::string WEIGHT_IMAGE
Definition: WeightImageConfig.cpp:43
DetectionImageConfig.h
SourceXtractor::WeightImageConfig::m_absolute_weight
bool m_absolute_weight
Definition: WeightImageConfig.h:76
SourceXtractor::ProcessedImage::create
static std::shared_ptr< ProcessedImage< T, P > > create(std::shared_ptr< const Image< T >> image_a, std::shared_ptr< const Image< T >> image_b)
Definition: ProcessedImage.h:53
FitsReader.h
SourceXtractor::WeightImageConfig::m_symmetry_usage
bool m_symmetry_usage
Definition: WeightImageConfig.h:79
SourceXtractor::WeightMapImageSource::generateFromWeight
void generateFromWeight(ImageTileWithType< WeightImage::PixelType > &tile, int width, int height, const ImageChunk< WeightImage::PixelType > &image_chunk) const
Definition: WeightImageConfig.cpp:177
SourceXtractor::computeWeightThreshold
static WeightImage::PixelType computeWeightThreshold(WeightImageConfig::WeightType weight_type, double threshold)
Definition: WeightImageConfig.cpp:78
Euclid::Table::FitsReader
x
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
Definition: MoffatModelFittingTask.cpp:94
SourceXtractor::WeightImageConfig::initialize
void initialize(const UserValues &args) override
Definition: WeightImageConfig.cpp:96
std::map::count
T count(T... args)
ProcessingImageSource.h
ImageSource.h
SourceXtractor::ImageChunk::getValue
T getValue(int x, int y) const
Returns the value of the pixel with the coordinates (x,y)
Definition: ImageChunk.h:56
Euclid::Configuration::Configuration
std::numeric_limits::max
T max(T... args)
SourceXtractor::WeightImageConfig::m_weight_type
WeightType m_weight_type
Definition: WeightImageConfig.h:75
y
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > y
Definition: MoffatModelFittingTask.cpp:94
ConfigManager.h
SourceXtractor::WeightImageConfig::m_weight_threshold
WeightImage::PixelType m_weight_threshold
Definition: WeightImageConfig.h:78
std::numeric_limits
SourceXtractor::ImageTileWithType::getImage
const std::shared_ptr< VectorImage< T > > & getImage() const
Definition: ImageTile.h:176
SourceXtractor::WeightImageConfig::WeightType::WEIGHT_TYPE_RMS
@ WEIGHT_TYPE_RMS