@@ -307,7 +307,21 @@ void FlexibleModelFittingTask::computeProperties(SourceGroupInterface& group) co
307307 LevmarEngine engine{m_max_iterations, 1E-6 , 1E-6 , 1E-6 , 1E-6 , 1E-4 };
308308 auto solution = engine.solveProblem (engine_parameter_manager, res_estimator);
309309 size_t iterations = (size_t ) boost::any_cast<std::array<double , 10 >>(solution.underlying_framework_info )[5 ];
310- SeFloat avg_reduced_chi_squared = computeReducedChiSquared (group, pixel_scale, parameter_manager);
310+
311+ int total_data_points = 0 ;
312+ SeFloat avg_reduced_chi_squared = computeChiSquared (group, pixel_scale, parameter_manager, total_data_points);
313+
314+ int nb_of_free_parameters = 0 ;
315+ for (auto & source : group) {
316+ for (auto parameter : m_parameters) {
317+ bool is_free_parameter = std::dynamic_pointer_cast<FlexibleModelFittingFreeParameter>(parameter).get ();
318+ bool accessed_by_modelfitting = parameter_manager.isParamAccessed (source, parameter);
319+ if (is_free_parameter && accessed_by_modelfitting) {
320+ nb_of_free_parameters++;
321+ }
322+ }
323+ }
324+ avg_reduced_chi_squared /= (total_data_points - nb_of_free_parameters);
311325
312326 // Collect parameters for output
313327 for (auto & source : group) {
@@ -316,10 +330,10 @@ void FlexibleModelFittingTask::computeProperties(SourceGroupInterface& group) co
316330
317331 for (auto parameter : m_parameters) {
318332 bool is_dependent_parameter = std::dynamic_pointer_cast<FlexibleModelFittingDependentParameter>(parameter).get ();
319- bool accesed_by_modelfitting = parameter_manager.isParamAccessed (source, parameter);
333+ bool accessed_by_modelfitting = parameter_manager.isParamAccessed (source, parameter);
320334 auto modelfitting_parameter = parameter_manager.getParameter (source, parameter);
321335
322- if (is_dependent_parameter || accesed_by_modelfitting ) {
336+ if (is_dependent_parameter || accessed_by_modelfitting ) {
323337 parameter_values[parameter->getId ()] = modelfitting_parameter->getValue ();
324338 parameter_sigmas[parameter->getId ()] = parameter->getSigma (parameter_manager, source, solution.parameter_sigmas );
325339 }
@@ -334,6 +348,7 @@ void FlexibleModelFittingTask::computeProperties(SourceGroupInterface& group) co
334348 source_flags |= Flags::PARTIAL_FIT;
335349 }
336350 }
351+
337352 source.setProperty <FlexibleModelFitting>(iterations, avg_reduced_chi_squared, source_flags, parameter_values,
338353 parameter_sigmas);
339354 }
@@ -374,11 +389,10 @@ void FlexibleModelFittingTask::updateCheckImages(SourceGroupInterface& group,
374389 }
375390}
376391
377- SeFloat FlexibleModelFittingTask::computeReducedChiSquaredForFrame (std::shared_ptr<const Image<SeFloat>> image,
378- std::shared_ptr<const Image<SeFloat>> model, std::shared_ptr<const Image<SeFloat>> weights,
379- int nb_of_free_params) const {
392+ SeFloat FlexibleModelFittingTask::computeChiSquaredForFrame (std::shared_ptr<const Image<SeFloat>> image,
393+ std::shared_ptr<const Image<SeFloat>> model, std::shared_ptr<const Image<SeFloat>> weights, int & data_points) const {
380394 double reduced_chi_squared = 0.0 ;
381- int data_points = 0 ;
395+ data_points = 0 ;
382396 for (int y=0 ; y < image->getHeight (); y++) {
383397 for (int x=0 ; x < image->getWidth (); x++) {
384398 double tmp = image->getValue (x, y) - model->getValue (x, y);
@@ -388,13 +402,14 @@ SeFloat FlexibleModelFittingTask::computeReducedChiSquaredForFrame(std::shared_p
388402 }
389403 }
390404 }
391- return reduced_chi_squared / (data_points - nb_of_free_params) ;
405+ return reduced_chi_squared;
392406}
393407
394- SeFloat FlexibleModelFittingTask::computeReducedChiSquared (SourceGroupInterface& group,
395- double pixel_scale, FlexibleModelFittingParameterManager& manager) const {
408+ SeFloat FlexibleModelFittingTask::computeChiSquared (SourceGroupInterface& group,
409+ double pixel_scale, FlexibleModelFittingParameterManager& manager, int & total_data_points ) const {
396410
397- SeFloat avg_reduced_chi_squared = 0 ;
411+ SeFloat total_chi_squared = 0 ;
412+ total_data_points = 0 ;
398413 int valid_frames = 0 ;
399414 for (auto frame : m_frames) {
400415 int frame_index = frame->getFrameNb ();
@@ -408,13 +423,16 @@ SeFloat FlexibleModelFittingTask::computeReducedChiSquared(SourceGroupInterface&
408423 auto image = createImageCopy (group, frame_index);
409424 auto weight = createWeightImage (group, frame_index);
410425
411- SeFloat reduced_chi_squared = computeReducedChiSquaredForFrame (
412- image, final_stamp, weight, manager.getParameterNb ());
413- avg_reduced_chi_squared += reduced_chi_squared;
426+ int data_points = 0 ;
427+ SeFloat chi_squared = computeChiSquaredForFrame (
428+ image, final_stamp, weight, data_points);
429+
430+ total_data_points += data_points;
431+ total_chi_squared += chi_squared;
414432 }
415433 }
416434
417- return avg_reduced_chi_squared / valid_frames ;
435+ return total_chi_squared ;
418436}
419437
420438FlexibleModelFittingTask::~FlexibleModelFittingTask () {
0 commit comments