SourceXtractorPlusPlus  0.14
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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> 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>(py_image.weight_file, py_image.weight_hdu, ImageTile::FloatImage);
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 
156 void MeasurementImageConfig::initialize(const UserValues&) {
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>(py_image.file, py_image.image_hdu, ImageTile::FloatImage);
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
static auto logger
Definition: WCS.cpp:46
const int id
Definition: PyId.h:35
void info(const std::string &logMessage)
SeFloat32 SeFloat
Definition: Types.h:32
void debug(const std::string &logMessage)
STL class.
STL class.
void initialize(const UserValues &args) override
static std::shared_ptr< WeightImage > convertWeightMap(std::shared_ptr< WeightImage > weight_image, WeightType weight_type, WeightImage::PixelType scaling=1)
std::vector< MeasurementImageInfo > m_image_infos
T move(T...args)
static Logging getLogger(const std::string &name="")
constexpr double g
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< BufferedImage< T > > create(std::shared_ptr< const ImageSource > source, std::shared_ptr< TileManager > tile_manager=TileManager::getInstance())