SourceXtractorPlusPlus  0.8
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FlexibleModelFittingTask.cpp
Go to the documentation of this file.
1 
17 /*
18  * FlexibleModelFittingTask.cpp
19  *
20  * Created on: Sep 17, 2018
21  * Author: mschefer
22  */
23 
24 #include <mutex>
25 
35 
37 
39 
42 
49 
53 
55 
56 namespace SourceXtractor {
57 
58 using namespace ModelFitting;
59 
60 
61 namespace {
62 
63 __attribute__((unused))
64 void printLevmarInfo(std::array<double, 10> info) {
65  std::cerr << "\nMinimization info:\n";
66  std::cerr << " ||e||_2 at initial p: " << info[0] << '\n';
67  std::cerr << " ||e||_2: " << info[1] << '\n';
68  std::cerr << " ||J^T e||_inf: " << info[2] << '\n';
69  std::cerr << " ||Dp||_2: " << info[3] << '\n';
70  std::cerr << " mu/max[J^T J]_ii: " << info[4] << '\n';
71  std::cerr << " # iterations: " << info[5] << '\n';
72  switch ((int) info[6]) {
73  case 1:
74  std::cerr << " stopped by small gradient J^T e\n";
75  break;
76  case 2:
77  std::cerr << " stopped by small Dp\n";
78  break;
79  case 3:
80  std::cerr << " stopped by itmax\n";
81  break;
82  case 4:
83  std::cerr << " singular matrix. Restart from current p with increased mu\n";
84  break;
85  case 5:
86  std::cerr << " no further error reduction is possible. Restart with increased mu\n";
87  break;
88  case 6:
89  std::cerr << " stopped by small ||e||_2\n";
90  break;
91  case 7:
92  std::cerr << " stopped by invalid (i.e. NaN or Inf) func values; a user error\n";
93  break;
94  }
95  std::cerr << " # function evaluations: " << info[7] << '\n';
96  std::cerr << " # Jacobian evaluations: " << info[8] << '\n';
97  std::cerr << " # linear systems solved: " << info[9] << "\n\n";
98 }
99 
100 }
101 
103  unsigned int max_iterations, double modified_chi_squared_scale,
107  : m_least_squares_engine(least_squares_engine),
108  m_max_iterations(max_iterations), m_modified_chi_squared_scale(modified_chi_squared_scale),
109  m_parameters(parameters), m_frames(frames), m_priors(priors) {}
110 
112  auto stamp_rect = group.getProperty<MeasurementFrameGroupRectangle>(frame_index);
113  return stamp_rect.getWidth() > 0 && stamp_rect.getHeight() > 0;
114 }
115 
117  SourceGroupInterface& group, int frame_index) const {
119 
120  auto frame = group.begin()->getProperty<MeasurementFrame>(frame_index).getFrame();
121  auto frame_image = frame->getSubtractedImage();
122 
123  auto rect = group.getProperty<MeasurementFrameGroupRectangle>(frame_index);
124  auto image = VectorImage<SeFloat>::create(rect.getWidth(), rect.getHeight());
125  for (int y = 0; y < rect.getHeight(); y++) {
126  for (int x = 0; x < rect.getWidth(); x++) {
127  image->at(x, y) = frame_image->getValue(rect.getTopLeft().m_x + x, rect.getTopLeft().m_y + y);
128  }
129  }
130 
131  return image;
132 }
133 
135  SourceGroupInterface& group, int frame_index) const {
137 
138  auto frame = group.begin()->getProperty<MeasurementFrame>(frame_index).getFrame();
139  auto frame_image = frame->getSubtractedImage();
140  auto frame_image_thresholded = frame->getThresholdedImage();
141  auto variance_map = frame->getVarianceMap();
142 
143  auto rect = group.getProperty<MeasurementFrameGroupRectangle>(frame_index);
144  auto weight = VectorImage<SeFloat>::create(rect.getWidth(), rect.getHeight());
145  std::fill(weight->getData().begin(), weight->getData().end(), 1);
146 
147  auto measurement_frame = group.begin()->getProperty<MeasurementFrame>(frame_index).getFrame();
148  SeFloat gain = measurement_frame->getGain();
149  SeFloat saturation = measurement_frame->getSaturation();
150 
151  for (int y = 0; y < rect.getHeight(); y++) {
152  for (int x = 0; x < rect.getWidth(); x++) {
153  auto back_var = variance_map->getValue(rect.getTopLeft().m_x + x, rect.getTopLeft().m_y + y);
154  if (saturation > 0 && frame_image->getValue(rect.getTopLeft().m_x + x, rect.getTopLeft().m_y + y) > saturation) {
155  weight->at(x, y) = 0;
156  } else if (weight->at(x, y) > 0) {
157  if (gain > 0.0) {
158  weight->at(x, y) = sqrt(
159  1.0 / (back_var + frame_image->getValue(rect.getTopLeft().m_x + x, rect.getTopLeft().m_y + y) / gain));
160  } else {
161  weight->at(x, y) = sqrt(1.0 / back_var); // infinite gain
162  }
163  }
164  }
165  }
166 
167  return weight;
168 }
169 
171  SourceGroupInterface& group,
174 
175  int frame_index = frame->getFrameNb();
176 
177  auto frame_coordinates =
178  group.begin()->getProperty<MeasurementFrame>(frame_index).getFrame()->getCoordinateSystem();
179  auto ref_coordinates =
180  group.begin()->getProperty<DetectionFrame>().getFrame()->getCoordinateSystem();
181 
182  auto stamp_rect = group.getProperty<MeasurementFrameGroupRectangle>(frame_index);
183  auto psf_property = group.getProperty<PsfProperty>(frame_index);
184  auto jacobian = group.getProperty<JacobianGroup>(frame_index).asTuple();
185 
186  // The model fitting module expects to get a PSF with a pixel scale, but we have the pixel sampling step size
187  // It will be used to compute the rastering grid size, and after convolving with the PSF the result will be
188  // downscaled before copied into the frame image.
189  // We can multiply here then, as the unit is pixel/pixel, rather than "/pixel or similar
190  auto group_psf = ImagePsf(pixel_scale * psf_property.getPixelSampling(), psf_property.getPsf());
191 
192  std::vector<ConstantModel> constant_models;
193  std::vector<PointModel> point_models;
194  std::vector<TransformedModel> extended_models;
195 
196  for (auto& source : group) {
197  for (auto model : frame->getModels()) {
198  model->addForSource(manager, source, constant_models, point_models, extended_models, jacobian, ref_coordinates, frame_coordinates,
199  stamp_rect.getTopLeft());
200  }
201  }
202 
203  // Full frame model with all sources
205  pixel_scale, (size_t) stamp_rect.getWidth(), (size_t) stamp_rect.getHeight(),
206  std::move(constant_models), std::move(point_models), std::move(extended_models), group_psf);
207 
208  return frame_model;
209 }
210 
211 
213  double pixel_scale = 1;
214  FlexibleModelFittingParameterManager parameter_manager;
215  ModelFitting::EngineParameterManager engine_parameter_manager{};
216  int n_free_parameters = 0;
217 
218  {
220 
221  // Prepare parameters
222  for (auto& source : group) {
223  for (auto parameter : m_parameters) {
224  if (std::dynamic_pointer_cast<FlexibleModelFittingFreeParameter>(parameter)) {
225  ++n_free_parameters;
226  }
227  parameter_manager.addParameter(source, parameter,
228  parameter->create(parameter_manager, engine_parameter_manager, source));
229  }
230  }
231  }
232 
233  // Reset access checks, as a dependent parameter could have triggered it
234  parameter_manager.clearAccessCheck();
235 
236  // Add models for all frames
237  ResidualEstimator res_estimator{};
238 
239  int valid_frames = 0;
240  int n_good_pixels = 0;
241  for (auto frame : m_frames) {
242  int frame_index = frame->getFrameNb();
243  // Validate that each frame covers the model fitting region
244  if (isFrameValid(group, frame_index)) {
245  valid_frames++;
246 
247  auto frame_model = createFrameModel(group, pixel_scale, parameter_manager, frame);
248 
249  auto image = createImageCopy(group, frame_index);
250  auto weight = createWeightImage(group, frame_index);
251 
252  for (int y = 0; y < weight->getHeight(); ++y) {
253  for (int x = 0; x < weight->getWidth(); ++x) {
254  n_good_pixels += (weight->at(x, y) != 0.);
255  }
256  }
257 
258  // Setup residuals
259  auto data_vs_model =
260  createDataVsModelResiduals(image, std::move(frame_model), weight,
262  res_estimator.registerBlockProvider(std::move(data_vs_model));
263  }
264  }
265 
266  // Check that we had enough data for the fit
267  Flags group_flags = Flags::NONE;
268  if (valid_frames == 0) {
269  group_flags = Flags::OUTSIDE;
270  }
271  else if (n_good_pixels < n_free_parameters) {
272  group_flags = Flags::INSUFFICIENT_DATA;
273  }
274 
275  if (group_flags != Flags::NONE) {
276  // Can't do model fitting as no measurement frame overlaps the detected source, or there are not enough pixels
277  // We still need to provide a property
278  for (auto& source : group) {
279  std::unordered_map<int, double> dummy_values;
280  for (auto parameter : m_parameters) {
281  auto modelfitting_parameter = parameter_manager.getParameter(source, parameter);
282  auto manual_parameter = std::dynamic_pointer_cast<ManualParameter>(modelfitting_parameter);
283  if (manual_parameter) {
285  }
286  dummy_values[parameter->getId()] = std::numeric_limits<double>::quiet_NaN();
287  }
288  source.setProperty<FlexibleModelFitting>(0, std::numeric_limits<double>::quiet_NaN(), group_flags,
289  dummy_values, dummy_values);
290  }
291  return;
292  }
293 
294  // Add priors
295  for (auto& source : group) {
296  for (auto prior : m_priors) {
297  prior->setupPrior(parameter_manager, source, res_estimator);
298  }
299  }
300 
301  // Model fitting
303  auto solution = engine->solveProblem(engine_parameter_manager, res_estimator);
304  size_t iterations = (size_t) boost::any_cast<std::array<double, 10>>(solution.underlying_framework_info)[5];
305 
306  int total_data_points = 0;
307  SeFloat avg_reduced_chi_squared = computeChiSquared(group, pixel_scale, parameter_manager, total_data_points);
308 
309  int nb_of_free_parameters = 0;
310  for (auto& source : group) {
311  for (auto parameter : m_parameters) {
312  bool is_free_parameter = std::dynamic_pointer_cast<FlexibleModelFittingFreeParameter>(parameter).get();
313  bool accessed_by_modelfitting = parameter_manager.isParamAccessed(source, parameter);
314  if (is_free_parameter && accessed_by_modelfitting) {
315  nb_of_free_parameters++;
316  }
317  }
318  }
319  avg_reduced_chi_squared /= (total_data_points - nb_of_free_parameters);
320 
321  // Collect parameters for output
322  for (auto& source : group) {
323  std::unordered_map<int, double> parameter_values, parameter_sigmas;
324  auto source_flags = Flags::NONE;
325 
326  for (auto parameter : m_parameters) {
327  bool is_dependent_parameter = std::dynamic_pointer_cast<FlexibleModelFittingDependentParameter>(parameter).get();
328  bool accessed_by_modelfitting = parameter_manager.isParamAccessed(source, parameter);
329  auto modelfitting_parameter = parameter_manager.getParameter(source, parameter);
330 
331  if (is_dependent_parameter || accessed_by_modelfitting) {
332  parameter_values[parameter->getId()] = modelfitting_parameter->getValue();
333  parameter_sigmas[parameter->getId()] = parameter->getSigma(parameter_manager, source, solution.parameter_sigmas);
334  }
335  else {
336  // Need to cascade the NaN to any potential dependent parameter
337  auto engine_parameter = std::dynamic_pointer_cast<EngineParameter>(modelfitting_parameter);
338  if (engine_parameter) {
340  }
341  parameter_values[parameter->getId()] = std::numeric_limits<double>::quiet_NaN();
342  parameter_sigmas[parameter->getId()] = std::numeric_limits<double>::quiet_NaN();
344  }
345  }
346 
347  source.setProperty<FlexibleModelFitting>(iterations, avg_reduced_chi_squared, source_flags, parameter_values,
348  parameter_sigmas);
349  }
350 
351  updateCheckImages(group, pixel_scale, parameter_manager);
352 }
353 
355  double pixel_scale, FlexibleModelFittingParameterManager& manager) const {
356 
357  int frame_id = 0;
358  for (auto frame : m_frames) {
359  int frame_index = frame->getFrameNb();
360  // Validate that each frame covers the model fitting region
361  if (isFrameValid(group, frame_index)) {
362  auto frame_model = createFrameModel(group, pixel_scale, manager, frame);
363  auto final_stamp = frame_model.getImage();
364 
365  auto stamp_rect = group.getProperty<MeasurementFrameGroupRectangle>(frame_index);
366  auto frame = group.begin()->getProperty<MeasurementFrame>(frame_index).getFrame();
367 
368  auto debug_image = CheckImages::getInstance().getModelFittingImage(frame);
369  if (debug_image) {
371  for (int x = 0; x < final_stamp->getWidth(); x++) {
372  for (int y = 0; y < final_stamp->getHeight(); y++) {
373  auto x_coord = stamp_rect.getTopLeft().m_x + x;
374  auto y_coord = stamp_rect.getTopLeft().m_y + y;
375  debug_image->setValue(x_coord, y_coord,
376  debug_image->getValue(x_coord, y_coord) + final_stamp->getValue(x, y));
377  }
378  }
379  }
380 
382  }
383  frame_id++;
384  }
385 }
386 
388  std::shared_ptr<const Image<SeFloat>> model, std::shared_ptr<const Image<SeFloat>> weights, int& data_points) const {
389  double reduced_chi_squared = 0.0;
390  data_points = 0;
391  for (int y=0; y < image->getHeight(); y++) {
392  for (int x=0; x < image->getWidth(); x++) {
393  double tmp = image->getValue(x, y) - model->getValue(x, y);
394  reduced_chi_squared += tmp * tmp * weights->getValue(x, y) * weights->getValue(x, y);
395  if (weights->getValue(x, y) > 0) {
396  data_points++;
397  }
398  }
399  }
400  return reduced_chi_squared;
401 }
402 
404  double pixel_scale, FlexibleModelFittingParameterManager& manager, int& total_data_points) const {
405 
406  SeFloat total_chi_squared = 0;
407  total_data_points = 0;
408  int valid_frames = 0;
409  for (auto frame : m_frames) {
410  int frame_index = frame->getFrameNb();
411  // Validate that each frame covers the model fitting region
412  if (isFrameValid(group, frame_index)) {
413  valid_frames++;
414  auto frame_model = createFrameModel(group, pixel_scale, manager, frame);
415  auto final_stamp = frame_model.getImage();
416 
417  auto stamp_rect = group.getProperty<MeasurementFrameGroupRectangle>(frame_index);
418  auto image = createImageCopy(group, frame_index);
419  auto weight = createWeightImage(group, frame_index);
420 
421  int data_points = 0;
422  SeFloat chi_squared = computeChiSquaredForFrame(
423  image, final_stamp, weight, data_points);
424 
425  total_data_points += data_points;
426  total_chi_squared += chi_squared;
427  }
428  }
429 
430  return total_chi_squared;
431 }
432 
434 }
435 
436 }
static StaticPlugin< SourceFlagsPlugin > source_flags
void addParameter(const SourceInterface &source, std::shared_ptr< const FlexibleModelFittingParameter > parameter, std::shared_ptr< ModelFitting::BasicParameter > engine_parameter)
const PropertyType & getProperty(unsigned int index=0) const
Convenience template method to call getProperty() with a more user-friendly syntax.
void updateCheckImages(SourceGroupInterface &group, double pixel_scale, FlexibleModelFittingParameterManager &manager) const
EngineParameter are those derived from the minimization process.
The object is completely outside of the measurement frame.
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
void setEngineValue(const double engine_value)
virtual void computeProperties(SourceGroupInterface &group) const override
Computes one or more properties for the SourceGroup and/or the Sources it contains.
std::shared_ptr< VectorImage< SeFloat > > createWeightImage(SourceGroupInterface &group, int frame_index) const
#define __attribute__(x)
SeFloat32 SeFloat
Definition: Types.h:32
static std::shared_ptr< VectorImage< T > > create(Args &&...args)
Definition: VectorImage.h:89
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > y
STL class.
FlexibleModelFittingTask(const std::string &least_squares_engine, unsigned int max_iterations, double modified_chi_squared_scale, std::vector< std::shared_ptr< FlexibleModelFittingParameter >> parameters, std::vector< std::shared_ptr< FlexibleModelFittingFrame >> frames, std::vector< std::shared_ptr< FlexibleModelFittingPrior >> priors)
std::shared_ptr< WriteableImage< MeasurementImage::PixelType > > getModelFittingImage(std::shared_ptr< const MeasurementImageFrame > frame)
static CheckImages & getInstance()
Definition: CheckImages.h:114
SeFloat computeChiSquaredForFrame(std::shared_ptr< const Image< SeFloat >> image, std::shared_ptr< const Image< SeFloat >> model, std::shared_ptr< const Image< SeFloat >> weights, int &data_points) const
T lock(T...args)
T dynamic_pointer_cast(T...args)
T move(T...args)
Data vs model comparator which computes a modified residual, using asinh.
bool isParamAccessed(const SourceInterface &source, std::shared_ptr< const FlexibleModelFittingParameter > parameter) const
void setValue(const double new_value)
Defines the interface used to group sources.
std::vector< std::shared_ptr< FlexibleModelFittingPrior > > m_priors
std::vector< std::shared_ptr< FlexibleModelFittingFrame > > m_frames
bool isFrameValid(SourceGroupInterface &group, int frame_index) const
STL class.
There are not enough good pixels to fit the parameters.
std::shared_ptr< ModelFitting::BasicParameter > getParameter(const SourceInterface &source, std::shared_ptr< const FlexibleModelFittingParameter > parameter) const
SeFloat computeChiSquared(SourceGroupInterface &group, double pixel_scale, FlexibleModelFittingParameterManager &manager, int &total_data_points) const
Some/all of the model parameters could not be fitted.
Flags
Flagging of bad sources.
Definition: SourceFlags.h:34
STL class.
Interface representing an image.
Definition: Image.h:43
std::shared_ptr< VectorImage< SeFloat > > createImageCopy(SourceGroupInterface &group, int frame_index) const
Class responsible for managing the parameters the least square engine minimizes.
static std::shared_ptr< LeastSquareEngine > create(const std::string &name, unsigned max_iterations=1000)
std::vector< std::shared_ptr< FlexibleModelFittingParameter > > m_parameters
std::unique_ptr< DataVsModelResiduals< typename std::remove_reference< DataType >::type, typename std::remove_reference< ModelType >::type, typename std::remove_reference< WeightType >::type, typename std::remove_reference< Comparator >::type > > createDataVsModelResiduals(DataType &&data, ModelType &&model, WeightType &&weight, Comparator &&comparator)
void printLevmarInfo(std::array< double, 10 > info)
Definition: utils.h:118
T quiet_NaN(T...args)
T sqrt(T...args)
T fill(T...args)
Provides to the LeastSquareEngine the residual values.
T getValue(int x, int y) const override
Returns the value of the pixel with the coordinates (x,y)
Definition: VectorImage.h:106
const double pixel_scale
Definition: TestImage.cpp:72
ModelFitting::FrameModel< ImagePsf, std::shared_ptr< VectorImage< SourceXtractor::SeFloat > > > createFrameModel(SourceGroupInterface &group, double pixel_scale, FlexibleModelFittingParameterManager &manager, std::shared_ptr< FlexibleModelFittingFrame > frame) const