SourceXtractorPlusPlus  0.11
Please provide a description of the project.
MeasurementImageConfig.cpp
Go to the documentation of this file.
1 
17 /*
18  * @file MeasurementImageConfig.cpp
19  * @author Nikolaos Apostolakos <nikoapos@gmail.com>
20  */
21 
22 #include <utility>
23 #include <limits>
24 
25 #include <boost/algorithm/string.hpp>
26 #include <boost/python/extract.hpp>
27 #include <boost/tokenizer.hpp>
28 
29 #include <ElementsKernel/Logging.h>
30 
31 #include <SEUtils/Python.h>
32 
36 
42 
44 
45 using namespace Euclid::Configuration;
46 namespace fs = boost::filesystem;
47 namespace py = boost::python;
48 
49 namespace SourceXtractor {
50 
51 MeasurementImageConfig::MeasurementImageConfig(long manager_id) : Configuration(manager_id) {
52  declareDependency<PythonConfig>();
53  declareDependency<WeightImageConfig>();
54  declareDependency<DetectionImageConfig>();
55 }
56 
57 namespace {
58 
60 
68 };
69 
70 void validateImagePaths(const PyMeasurementImage& image) {
71  if (!fs::exists(image.file)) {
72  throw Elements::Exception() << "File " << image.file << " does not exist";
73  }
74  if (image.weight_file != "" && !fs::exists(image.weight_file)) {
75  throw Elements::Exception() << "File " << image.weight_file << " does not exist";
76  }
77  if (image.psf_file != "" && !fs::exists(image.psf_file)) {
78  throw Elements::Exception() << "File " << image.psf_file << " does not exist";
79  }
80 }
81 
82 std::shared_ptr<MeasurementImage> createMeasurementImage(
83  std::shared_ptr<FitsImageSource<DetectionImage::PixelType>> fits_image_source, double flux_scale) {
85  if (flux_scale != 1.) {
86  image = MultiplyImage<MeasurementImage::PixelType>::create(image, flux_scale);
87  }
88  return image;
89 }
90 
91 WeightImageConfig::WeightType getWeightType(const std::string& type_string, const std::string& file_name) {
92  // check for a valid weight type
93  auto weight_type_name = boost::to_upper_copy(type_string);
94  if (weight_type_map.find(weight_type_name) == weight_type_map.end()) {
95  throw Elements::Exception() << "Unknown weight map type for measurement weight image " << file_name << ": "<< type_string;
96  }
97 
98  return weight_type_map[weight_type_name];
99 }
100 
101 std::shared_ptr<WeightImage> createWeightMap(const PyMeasurementImage& py_image) {
102  auto weight_type = getWeightType(py_image.weight_type, py_image.weight_file);
103 
104  // without an image nothing can be done
105  if (py_image.weight_file == "") {
108  throw Elements::Exception() << "Weight type '" << py_image.weight_type << "' is meaningless without a weight image";
109  }
110 
111  return nullptr;
112  }
113 
116  throw Elements::Exception() << "Please give an appropriate weight type for image: " << py_image.weight_file;
117  }
118 
119  auto weight_image_source =
120  std::make_shared<FitsImageSource<DetectionImage::PixelType>>(py_image.weight_file, py_image.weight_hdu);
122 
123  logger.debug() << "w: " << weight_map->getWidth() << " h: " << weight_map->getHeight()
124  << " t: " << py_image.weight_type << " s: " << py_image.weight_scaling;
125  weight_map = WeightImageConfig::convertWeightMap(weight_map, weight_type, py_image.weight_scaling);
126 
127  return weight_map;
128 }
129 
130 WeightImage::PixelType extractWeightThreshold(const PyMeasurementImage& py_image) {
131  if (!py_image.has_weight_threshold) {
133  }
134  WeightImage::PixelType threshold = py_image.weight_threshold;
135  auto weight_type_name = boost::to_upper_copy(py_image.weight_type);
136  switch (weight_type_map[weight_type_name]) {
137  default:
140  threshold = threshold * threshold;
141  break;
143  break;
145  if (threshold>0)
146  threshold = 1.0 / threshold;
147  else
149  break;
150  }
151  return threshold;
152 }
153 
154 }
155 
157  auto images = getDependency<PythonConfig>().getInterpreter().getMeasurementImages();
158  auto groups = getDependency<PythonConfig>().getInterpreter().getMeasurementGroups();
159 
160  // Delegate into Python to log the measurement configuration
161  boost::char_separator<char> line_sep{"\n"};
162  for (auto &g : groups) {
163  GILStateEnsure ensure;
164  std::string group_str = py::extract<std::string>(g.attr("__str__")());
165  boost::tokenizer<decltype(line_sep)> tok(group_str, line_sep);
166  for (auto &l : tok) {
167  logger.info() << l;
168  }
169  }
170 
171  if (images.size() > 0) {
172  for (auto& p : images) {
173  PyMeasurementImage& py_image = p.second;
174  validateImagePaths(py_image);
175 
176  logger.debug() << "Loading measurement image: " << py_image.file << " HDU: " << py_image.image_hdu;
177  logger.debug() << "\tWeight: " << py_image.weight_file << " HDU: " << py_image.weight_hdu;
178  logger.debug() << "\tPSF: " << py_image.psf_file << " HDU: " << py_image.psf_hdu;
179  logger.debug() << "\tGain: " << py_image.gain;
180  logger.debug() << "\tSaturation: " << py_image.saturation;
181  logger.debug() << "\tFlux scale: " << py_image.flux_scale;
182 
183  auto flux_scale = py_image.flux_scale;
184 
186 
187  info.m_path = py_image.file;
188  info.m_psf_path = py_image.psf_file;
189 
190 
191  auto fits_image_source =
192  std::make_shared<FitsImageSource<MeasurementImage::PixelType>>(py_image.file, py_image.image_hdu);
193  info.m_measurement_image = createMeasurementImage(fits_image_source, py_image.flux_scale);
194  info.m_coordinate_system = std::make_shared<WCS>(*fits_image_source);
195 
196  info.m_gain = py_image.gain / flux_scale;
197  info.m_saturation_level = py_image.saturation * flux_scale;
198  info.m_id = py_image.id;
199 
200  info.m_absolute_weight= py_image.weight_absolute;
201  info.m_weight_threshold = extractWeightThreshold(py_image);
202 
205 
206  auto weight_map = createWeightMap(py_image);
207 
208  if (weight_map != nullptr && flux_scale != 1. && py_image.weight_absolute) {
210  weight_map, py_image.flux_scale * py_image.flux_scale);
211  } else {
212  info.m_weight_image = weight_map;
213  }
214 
215  info.m_weight_type = getWeightType(py_image.weight_type, py_image.weight_file);
216 
217  info.m_image_hdu = py_image.image_hdu;
218  info.m_psf_hdu = py_image.psf_hdu;
219  info.m_weight_hdu = py_image.weight_hdu;
220 
221  m_image_infos.emplace_back(std::move(info));
222  }
223  } else {
224  logger.debug() << "No measurement image provided, using the detection image for measurements";
225 
226  auto detection_image = getDependency<DetectionImageConfig>();
227  auto weight_image = getDependency<WeightImageConfig>();
228 
229  // note: flux scale was already applied
230 
231  m_image_infos.emplace_back(MeasurementImageInfo {
232  detection_image.getDetectionImagePath(),
233  "", // psf path
234 
235  detection_image.getDetectionImage(),
236  detection_image.getCoordinateSystem(),
237  weight_image.getWeightImage(),
238  weight_image.getWeightType(),
239 
240  weight_image.isWeightAbsolute(),
241  weight_image.getWeightThreshold(),
242  (SeFloat) detection_image.getGain(),
243  (SeFloat) detection_image.getSaturation(),
244 
245  false,
246  0.0,
247 
248  0, // id
249 
250  1,1,1 // HDUs
251  });
252 
253 
254  }
255 }
256 
257 } // end of namespace SourceXtractor
SourceXtractor::PyMeasurementImage::gain
double gain
Definition: PyMeasurementImage.h:36
SourceXtractor::MeasurementImageConfig::MeasurementImageInfo
Definition: MeasurementImageConfig.h:41
SourceXtractor::PyMeasurementImage::saturation
double saturation
Definition: PyMeasurementImage.h:37
SourceXtractor::MeasurementImageConfig::MeasurementImageInfo::m_constant_background_value
MeasurementImage::PixelType m_constant_background_value
Definition: MeasurementImageConfig.h:57
std::string
STL class.
std::shared_ptr
STL class.
std::move
T move(T... args)
SourceXtractor::WeightImageConfig::WeightType::WEIGHT_TYPE_NONE
@ WEIGHT_TYPE_NONE
SourceXtractor::Image< SeFloat >::PixelType
SeFloat PixelType
Definition: Image.h:47
SourceXtractor::SeFloat
SeFloat32 SeFloat
Definition: Types.h:32
Elements::Logging
SourceXtractor::PyMeasurementImage
Definition: PyMeasurementImage.h:30
SourceXtractor::MeasurementImageConfig::MeasurementImageInfo::m_absolute_weight
bool m_absolute_weight
Definition: MeasurementImageConfig.h:51
SourceXtractor::MeasurementImageConfig::MeasurementImageInfo::m_weight_image
std::shared_ptr< WeightImage > m_weight_image
Definition: MeasurementImageConfig.h:47
SourceXtractor::WeightImageConfig::WeightType
WeightType
Definition: WeightImageConfig.h:36
SourceXtractor::MeasurementImageConfig::MeasurementImageInfo::m_coordinate_system
std::shared_ptr< CoordinateSystem > m_coordinate_system
Definition: MeasurementImageConfig.h:46
SourceXtractor::PyMeasurementImage::is_background_constant
bool is_background_constant
Definition: PyMeasurementImage.h:47
WeightImageConfig.h
g
constexpr double g
Python.h
SourceXtractor::WeightImageConfig::WeightType::WEIGHT_TYPE_FROM_BACKGROUND
@ WEIGHT_TYPE_FROM_BACKGROUND
SourceXtractor::MeasurementImageConfig::MeasurementImageInfo::m_path
std::string m_path
Definition: MeasurementImageConfig.h:42
SourceXtractor::PyMeasurementImage::constant_background_value
double constant_background_value
Definition: PyMeasurementImage.h:48
Euclid::Configuration
SourceXtractor::WeightImageConfig::WeightType::WEIGHT_TYPE_WEIGHT
@ WEIGHT_TYPE_WEIGHT
SourceXtractor::MeasurementImageConfig::MeasurementImageInfo::m_psf_path
std::string m_psf_path
Definition: MeasurementImageConfig.h:43
SourceXtractor::PyId::id
const int id
Definition: PyId.h:35
SourceXtractor::PyMeasurementImage::image_hdu
int image_hdu
Definition: PyMeasurementImage.h:50
SourceXtractor::PyMeasurementImage::weight_absolute
bool weight_absolute
Definition: PyMeasurementImage.h:42
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:200
SourceXtractor::PyMeasurementImage::weight_file
std::string weight_file
Definition: PyMeasurementImage.h:40
SourceXtractor
Definition: Aperture.h:30
WCS.h
BufferedImage.h
MeasurementImageConfig.h
SourceXtractor::MeasurementImageConfig::MeasurementImageInfo::m_weight_hdu
int m_weight_hdu
Definition: MeasurementImageConfig.h:63
SourceXtractor::MeasurementImageConfig::MeasurementImageInfo::m_saturation_level
MeasurementImage::PixelType m_saturation_level
Definition: MeasurementImageConfig.h:54
SourceXtractor::MeasurementImageConfig::MeasurementImageInfo::m_weight_threshold
WeightImage::PixelType m_weight_threshold
Definition: MeasurementImageConfig.h:52
Elements::Exception
PythonConfig.h
SourceXtractor::MeasurementImageConfig::MeasurementImageInfo::m_measurement_image
std::shared_ptr< MeasurementImage > m_measurement_image
Definition: MeasurementImageConfig.h:45
SourceXtractor::PyMeasurementImage::weight_type
std::string weight_type
Definition: PyMeasurementImage.h:41
SourceXtractor::PyMeasurementImage::flux_scale
double flux_scale
Definition: PyMeasurementImage.h:38
ProcessedImage.h
Elements::Logging::debug
void debug(const std::string &logMessage)
std::map
STL class.
SourceXtractor::MeasurementImageConfig::MeasurementImageInfo::m_id
int m_id
Definition: MeasurementImageConfig.h:59
SourceXtractor::WeightImageConfig::WeightType::WEIGHT_TYPE_VARIANCE
@ WEIGHT_TYPE_VARIANCE
SourceXtractor::logger
static Elements::Logging logger
Definition: PluginManager.cpp:45
DetectionImageConfig.h
Elements::Logging::info
void info(const std::string &logMessage)
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:54
Elements::Logging::getLogger
static Logging getLogger(const std::string &name="")
SourceXtractor::PyMeasurementImage::psf_file
std::string psf_file
Definition: PyMeasurementImage.h:39
SourceXtractor::MeasurementImageConfig::MeasurementImageInfo::m_image_hdu
int m_image_hdu
Definition: MeasurementImageConfig.h:61
SourceXtractor::MeasurementImageConfig::MeasurementImageInfo::m_is_background_constant
bool m_is_background_constant
Definition: MeasurementImageConfig.h:56
SourceXtractor::MeasurementImageConfig::MeasurementImageInfo::m_psf_hdu
int m_psf_hdu
Definition: MeasurementImageConfig.h:62
SourceXtractor::MeasurementImageConfig::MeasurementImageInfo::m_weight_type
WeightImageConfig::WeightType m_weight_type
Definition: MeasurementImageConfig.h:49
SourceXtractor::PyMeasurementImage::weight_hdu
int weight_hdu
Definition: PyMeasurementImage.h:52
SourceXtractor::MeasurementImageConfig::m_image_infos
std::vector< MeasurementImageInfo > m_image_infos
Definition: MeasurementImageConfig.h:76
SourceXtractor::PyMeasurementImage::psf_hdu
int psf_hdu
Definition: PyMeasurementImage.h:51
Euclid::Configuration::Configuration
Logging.h
std::numeric_limits::max
T max(T... args)
SourceXtractor::MeasurementImageConfig::initialize
void initialize(const UserValues &args) override
Definition: MeasurementImageConfig.cpp:156
FitsImageSource.h
SourceXtractor::PyMeasurementImage::file
std::string file
Definition: PyMeasurementImage.h:35
SourceXtractor::GILStateEnsure
Definition: Python.h:45
SourceXtractor::MeasurementImageConfig::MeasurementImageInfo::m_gain
SeFloat m_gain
Definition: MeasurementImageConfig.h:53
PyMeasurementImage.h
SourceXtractor::BufferedImage::create
static std::shared_ptr< BufferedImage< T > > create(std::shared_ptr< const ImageSource< T >> source, std::shared_ptr< TileManager > tile_manager=TileManager::getInstance())
Definition: BufferedImage.cpp:34
SourceXtractor::WeightImageConfig::WeightType::WEIGHT_TYPE_RMS
@ WEIGHT_TYPE_RMS