25 #include <boost/algorithm/string.hpp>
26 #include <boost/regex.hpp>
28 using boost::regex_match;
44 namespace po = boost::program_options;
55 WeightImageConfig::WeightImageConfig(
long manager_id) :
57 m_weight_type(
WeightType::WEIGHT_TYPE_FROM_BACKGROUND),
58 m_absolute_weight(false),
60 m_weight_threshold(0),
61 m_symmetry_usage(true) {
63 declareDependency<DetectionImageConfig>();
67 return { {
"Weight image", {
69 "Path to a fits format image to be used as weight image."},
71 "Is the weight map provided as absolute values or relative to background."},
73 "Weight image type [none|background|rms|variance|weight]."},
75 "Weight map scaling factor."},
77 "Threshold for pixels to be considered bad pixels. In same units as weight map."},
79 "Use object symmetry to replace pixels above the weight threshold for photometry."},
86 switch (weight_type) {
88 case WeightType::WEIGHT_TYPE_FROM_BACKGROUND:
89 case WeightType::WEIGHT_TYPE_RMS:
90 return threshold * threshold;
91 case WeightType::WEIGHT_TYPE_VARIANCE:
93 case WeightType::WEIGHT_TYPE_WEIGHT:
95 return 1.0 / threshold;
117 auto weight_iter = WEIGHT_MAP.find(weight_type_name);
118 if (weight_iter == WEIGHT_MAP.end()) {
133 throw Elements::Exception() <<
"Please give an appropriate weight type for image: " << weight_image_filename;
135 throw Elements::Exception() <<
"Please give an appropriate weight type for image: " << weight_image_filename;
137 throw Elements::Exception() <<
"Setting absolute weight but providing *no* weight image does not make sense.";
139 if (weight_image_filename !=
"") {
140 boost::regex hdu_regex(
".*\\[[0-9]*\\]$");
142 for (
int i=0;; i++) {
144 if (boost::regex_match(weight_image_filename, hdu_regex)) {
146 fits_image_source = std::make_shared<FitsImageSource>(weight_image_filename, 0,
ImageTile::FloatImage);
152 fits_image_source = std::make_shared<FitsImageSource>(weight_image_filename, i+1,
ImageTile::FloatImage);
159 throw Elements::Exception() <<
"Can't find 2D image in FITS file: " << weight_image_filename;
170 auto flux_scale = getDependency<DetectionImageConfig>().getOriginalFluxScale(
m_weight_images.size());
177 scaled_weight_threshold *= flux_scale * flux_scale;
196 return "WeightMapImageSource(" + getImageRepr() +
")";
199 void generateTile(
const std::shared_ptr<Image<WeightImage::PixelType>>& image,
200 ImageTileWithType<WeightImage::PixelType>& tile,
int x,
int y,
int width,
int height)
const final {
201 auto image_chunk = image->getChunk(
x,
y, width, height);
202 switch (m_weight_type) {
204 generateFromRms(tile, width, height, *image_chunk);
207 generateFromVariance(tile, width, height, *image_chunk);
210 generateFromWeight(tile, width, height, *image_chunk);
219 void generateFromWeight(ImageTileWithType<WeightImage::PixelType>& tile,
int width,
int height,
220 const ImageChunk<WeightImage::PixelType>& image_chunk)
const {
221 auto& tile_image = *tile.getImage();
222 for (
int iy = 0; iy < height; iy++) {
223 for (
int ix = 0; ix < width; ix++) {
224 auto value = image_chunk.getValue(ix, iy) * m_scaling;
226 tile_image.setValue(ix, iy, 1.0 / value);
235 void generateFromVariance(ImageTileWithType<WeightImage::PixelType>& tile,
int width,
int height,
236 const ImageChunk<WeightImage::PixelType>& image_chunk)
const {
237 auto& tile_image = *tile.getImage();
238 for (
int iy = 0; iy < height; iy++) {
239 for (
int ix = 0; ix < width; ix++) {
240 auto value = image_chunk.getValue(ix, iy) * m_scaling;
241 tile_image.setValue(ix, iy, value);
246 void generateFromRms(ImageTileWithType<WeightImage::PixelType>& tile,
int width,
int height,
247 const ImageChunk<WeightImage::PixelType>& image_chunk)
const {
248 auto& tile_image = *tile.getImage();
249 for (
int iy = 0; iy < height; iy++) {
250 for (
int ix = 0; ix < width; ix++) {
251 auto value = image_chunk.getValue(ix, iy) * m_scaling;
252 tile_image.setValue(ix, iy, value * value);
275 std::make_shared<WeightMapImageSource>(weight_image, weight_type, scaling));
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > y
T emplace_back(T... args)