34#define omp_get_max_threads() 1
35#define omp_get_thread_num() 0
45#include <gsl/gsl_multifit.h>
46#include <gsl/gsl_vector.h>
47#include <gsl/gsl_histogram.h>
48#include <gsl/gsl_poly.h>
63 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
64 "mode method unknown");
90 const char *base_context,
94 cpl_ensure(base_context && prefix && defaults,
95 CPL_ERROR_NULL_INPUT, NULL);
98 CPL_ERROR_INCOMPATIBLE_INPUT, NULL);
100 cpl_parameterlist * parlist = cpl_parameterlist_new();
103 hdrl_setup_vparameter(parlist, prefix,
".",
"",
104 "histo-min", base_context,
105 "Minimum pixel value to accept for mode computation",
110 hdrl_setup_vparameter(parlist, prefix,
".",
"",
111 "histo-max", base_context,
112 "Maximum pixel value to accept for mode computation",
117 hdrl_setup_vparameter(parlist, prefix,
".",
"",
118 "bin-size", base_context,
119 "Binsize of the histogram",
128 const char * method_def = method_to_string(method);
131 cpl_parameter * par = cpl_parameter_new_enum(name, CPL_TYPE_STRING,
132 "Mode method (algorithm) to use",
133 base_context, method_def, 3,
"MEDIAN",
"WEIGHTED",
"FIT");
136 cpl_parameter_set_alias(par, CPL_PARAMETER_MODE_CLI, name);
137 cpl_parameter_disable(par, CPL_PARAMETER_MODE_ENV);
139 cpl_parameterlist_append(parlist, par);
151 hdrl_setup_vparameter(parlist, prefix,
".",
"",
152 "error-niter", base_context,
153 "Iterations to compute the mode error",
157 if (cpl_error_get_code()) {
158 cpl_parameterlist_delete(parlist);
187 const cpl_parameterlist * parlist,
193 cpl_size * error_niter)
195 cpl_ensure_code(prefix && parlist, CPL_ERROR_NULL_INPUT);
200 const cpl_parameter * par = cpl_parameterlist_find_const(parlist, name);
201 *histo_min = cpl_parameter_get_double(par);
206 const cpl_parameter * par = cpl_parameterlist_find_const(parlist, name);
207 *histo_max = cpl_parameter_get_double(par);
213 const cpl_parameter * par = cpl_parameterlist_find_const(parlist, name);
214 *bin_size = cpl_parameter_get_double(par);
222 const cpl_parameter * par = cpl_parameterlist_find_const(parlist, name) ;
223 const char * tmp_str = cpl_parameter_get_string(par);
224 if (tmp_str == NULL) {
226 return cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
227 "Parameter mode.method not found");
229 if(!strcmp(tmp_str,
"MEDIAN")) {
231 }
else if(!strcmp(tmp_str,
"WEIGHTED")) {
233 }
else if(!strcmp(tmp_str,
"FIT")) {
251 const cpl_parameter * par = cpl_parameterlist_find_const(parlist, name);
252 *error_niter = cpl_parameter_get_int(par);
256 if (cpl_error_get_code()) {
257 return cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
258 "Error while parsing parameterlist "
259 "with prefix %s", prefix);
262 return CPL_ERROR_NONE;
278hdrl_mode_compute_binsize(cpl_vector* vec)
281 cpl_size size = cpl_vector_get_size(vec);
283 hcpl_vector_get_mad_window(vec, 1, size, &mad);
284 double std_deviation_from_mad = mad * CPL_MATH_STD_MAD;
290 double binsize = 2. * 3.49 * std_deviation_from_mad / pow(size, 1. / 3. );
293 binsize = nextafter(0,1.0);
307hdrl_mode_get_nbin(
const double min,
const double max,
const double size)
310 return (cpl_size)floor( (max - min) / size ) + 1;
326static gsl_histogram *
327hdrl_mode_histogram(
const cpl_vector* vec,
const double histogram_min,
328 const double histogram_max,
const cpl_size nbin)
331 cpl_error_ensure(nbin > 0, CPL_ERROR_ILLEGAL_INPUT,
332 return NULL,
"Number of bins must be > 0");
334 cpl_error_ensure(histogram_max > histogram_min, CPL_ERROR_ILLEGAL_INPUT,
335 return NULL,
"histo_max must be larger than histo_min");
337 gsl_histogram * histogram = gsl_histogram_alloc(nbin);
339 gsl_histogram_set_ranges_uniform (histogram, histogram_min, histogram_max);
341 const cpl_size size = cpl_vector_get_size(vec);
342 const double* data = cpl_vector_get_data_const(vec);
343 for(cpl_size i = 0; i < size; i++) {
345 gsl_histogram_increment(histogram, data[i]);
373hdrl_mode_histogram_to_table(gsl_histogram* histogram,
374 const double histogram_min,
375 const double histogram_step,
const cpl_size nbin){
377 cpl_table* table = cpl_table_new(nbin);
379 cpl_table_new_column(table,
"BIN", CPL_TYPE_DOUBLE);
380 cpl_table_new_column(table,
"INTERVAL_LOWER", CPL_TYPE_DOUBLE);
381 cpl_table_new_column(table,
"INTERVAL_UPPER", CPL_TYPE_DOUBLE);
382 cpl_table_new_column(table,
"COUNTS", CPL_TYPE_DOUBLE);
384 cpl_table_fill_column_window(table,
"BIN", 0, nbin, 0);
385 cpl_table_fill_column_window(table,
"INTERVAL_LOWER", 0, nbin, 0);
386 cpl_table_fill_column_window(table,
"INTERVAL_UPPER", 0, nbin, 0);
387 cpl_table_fill_column_window(table,
"COUNTS", 0, nbin, 0);
389 double* phb = cpl_table_get_data_double(table,
"BIN");
390 double* phl = cpl_table_get_data_double(table,
"INTERVAL_LOWER");
391 double* phu = cpl_table_get_data_double(table,
"INTERVAL_UPPER");
392 double* phc = cpl_table_get_data_double(table,
"COUNTS");
395 for(cpl_size i = 0; i < nbin; i++) {
397 phl[i] = histogram_min + phb[i] * histogram_step;
398 phu[i] = phl[i] + histogram_step;
399 phc[i] = histogram->bin[i];
424hdrl_gsl_fit_poly (
const double* data_x,
425 const double* data_y,
426 const double* errs_y,
427 const size_t n_sampling_points,
428 const cpl_size poly_degree,
435 double err, chisq_loc;
436 gsl_vector *x = NULL, *y = NULL, *w = NULL, *p = NULL;
437 gsl_matrix *X = NULL, *covar = NULL;
440 gsl_multifit_linear_workspace *work = NULL;
442 x = gsl_vector_alloc (n_sampling_points);
443 y = gsl_vector_alloc (n_sampling_points);
444 w = gsl_vector_alloc (n_sampling_points);
445 p = gsl_vector_alloc (poly_degree);
446 X = gsl_matrix_alloc (n_sampling_points, poly_degree);
447 covar = gsl_matrix_alloc (poly_degree, poly_degree);
449 for (i = 0; i < (cpl_size) n_sampling_points; i++) {
450 gsl_vector_set (x, i, data_x[i]);
451 gsl_vector_set (y, i, data_y[i]);
454 gsl_vector_set (w, i, 1.0 / err / err);
456 for (j = 0; j < poly_degree; j++)
457 gsl_matrix_set (X, i, j, gsl_pow_int(gsl_vector_get(x, i), j));
460 work = gsl_multifit_linear_alloc (n_sampling_points, poly_degree);
461 gsl_multifit_wlinear (X, w, y, p, covar, &chisq_loc, work);
462 gsl_multifit_linear_free (work);
464 for (i = 0; i < (cpl_size) n_sampling_points; i++) {
467 for (j = 0; j < poly_degree; j++)
468 fit[i] += gsl_matrix_get (X, i, j) * gsl_vector_get (p, j);
471 *chisq = chisq_loc/(n_sampling_points - poly_degree);
476 for (j = 0; j < poly_degree; j++) {
477 gsl_matrix_set (covar, j, j, gsl_matrix_get (covar, j, j)*chisq_loc);
478 coeffs_val[j] = gsl_vector_get (p, j);
479 coeffs_err[j] = sqrt(gsl_matrix_get (covar, j, j));
514hdrl_mode_vector_trim(
const cpl_vector* vec,
const double min,
516 cpl_size size = cpl_vector_get_size(vec);
517 cpl_error_ensure(size > 0, CPL_ERROR_ILLEGAL_INPUT,
518 return NULL,
"vector size must be > 0");
519 cpl_vector * vec_trim = cpl_vector_new(size);
520 const double * pvec = cpl_vector_get_data_const(vec);
521 double * pvec_trim = cpl_vector_get_data(vec_trim);
525 for (cpl_size i = 0; i < size; i++) {
526 if (pvec[i] >= min && pvec[i] <= max) {
527 pvec_trim[index] = pvec[i];
536 cpl_vector_set_size(vec_trim, index);
540 cpl_vector_delete(vec_trim);
564static cpl_error_code hdrl_mode_median(
565 const cpl_vector * vec,
566 const double histo_min,
567 const double histo_max,
569 const cpl_size error_niter,
570 double * mode_median,
571 double * mode_median_err)
575 cpl_error_ensure(vec != NULL, CPL_ERROR_NULL_INPUT,
576 return CPL_ERROR_NULL_INPUT,
"Null input vector data");
578 gsl_histogram * histogram = hdrl_mode_histogram(vec, histo_min, histo_max,
581 cpl_error_ensure(histogram != NULL, CPL_ERROR_NULL_INPUT,
582 return CPL_ERROR_NULL_INPUT,
"Histogram can not be created");
589 cpl_size histogram_bin_max = gsl_histogram_max_bin(histogram);
599 gsl_histogram_get_range(histogram, histogram_bin_max, &lower, &upper);
601 cpl_vector * vec_final = hdrl_mode_vector_trim(vec, lower, upper);
602 *mode_median = cpl_vector_get_median(vec_final);
604 if(error_niter == 0) {
605 *mode_median_err = cpl_vector_get_stdev(vec_final);
606 cpl_msg_debug(cpl_func,
"(method median) computed mode: %g, associated error: %g",
607 *mode_median, *mode_median_err);
610 *mode_median_err = 0;
614 gsl_histogram_free(histogram);
615 cpl_vector_delete(vec_final);
617 return cpl_error_get_code();
640static cpl_error_code hdrl_mode_weight(
641 const cpl_vector * vec,
642 const double histo_min,
643 const double histo_max,
644 const double bin_size,
646 const cpl_size error_niter,
647 double * mode_weight,
648 double * mode_weight_err)
651 cpl_error_ensure(vec != NULL, CPL_ERROR_NULL_INPUT,
652 return CPL_ERROR_NULL_INPUT,
"Null input vector data");
654 gsl_histogram * histogram = hdrl_mode_histogram(vec, histo_min, histo_max,
657 cpl_error_ensure(histogram != NULL, CPL_ERROR_NULL_INPUT,
658 return CPL_ERROR_NULL_INPUT,
"Histogram can not be created");
660 cpl_table* table = hdrl_mode_histogram_to_table(histogram, histo_min,
664 double histogram_max = gsl_histogram_max_val(histogram);
666 cpl_size histogram_bin_max = gsl_histogram_max_bin(histogram);
669 if(histogram_bin_max > 0 &&
670 (histogram_bin_max < ((cpl_size)gsl_histogram_bins(histogram) - 1))) {
671 cpl_msg_debug(cpl_func,
"histogram (bin_max-1) value: %16.8g",
672 gsl_histogram_get(histogram,histogram_bin_max-1));
673 cpl_msg_debug(cpl_func,
"histogram (bin_max+1) value: %16.8g",
674 gsl_histogram_get(histogram,histogram_bin_max+1));
680 gsl_histogram_get_range(histogram, histogram_bin_max, &lower, &upper);
682 cpl_table_and_selected_double(table,
"COUNTS",CPL_EQUAL_TO, histogram_max);
683 cpl_table* extract = cpl_table_extract_selected(table);
687 double mean = cpl_table_get_column_mean(extract,
"INTERVAL_LOWER");
689 cpl_table_delete(extract);
691 cpl_size max_pos = 0;
692 cpl_table_get_column_maxpos(table,
"INTERVAL_LOWER", &max_pos);
694 cpl_table_delete(table);
696 double freq1 = histogram_max;
697 double level1 = mean;
701 if(histogram_bin_max + 1 <= ((cpl_size)nbin - 1)) {
702 freq2 = gsl_histogram_get(histogram, histogram_bin_max + 1);
708 if(histogram_bin_max > 0) {
709 freq0 = gsl_histogram_get(histogram, histogram_bin_max - 1);
719 double diff1 = freq1 - freq0;
720 double diff2 = freq1 - freq2;
721 double factor = diff1 / (diff1 + diff2);
722 if( factor == 0 || isnan(factor) ) {
726 *mode_weight = level1 + bin_size * factor;
729 if (error_niter == 0) {
730 double dd1 = sqrt(freq0 + freq1);
731 double dd2 = sqrt(freq1 + freq2);
734 double term1 = diff2 * dd1 / pow((diff1 + diff2), 2);
735 double term2 = diff1 * dd2 / pow((diff1 + diff2), 2);
736 dw2 = pow(term1, 2.) + pow(term2, 2.);
737 *mode_weight_err = bin_size * sqrt(dw2);
746 *mode_weight_err = 0;
749 cpl_msg_debug(cpl_func,
"(method weight) computed mode: %16.10g error: %16.10g",
750 *mode_weight, *mode_weight_err);
753 gsl_histogram_free(histogram);
755 return cpl_error_get_code();
766hdrl_mode_fit_analytical_error(
const double* coeffs_val,
767 const double* coeffs_err, gsl_matrix* covar,
768 const double scale_factor){
771 double a2 = coeffs_val[2];
772 double a1 = coeffs_val[1];
778 double cvar_matrix = gsl_matrix_get (covar, 2, 1) * scale_factor;
780 double da2 = coeffs_err[2];
781 double da1 = coeffs_err[1];
784 double cvr_term = 2 * (-1. / (2 * a2)) * (a1 / (2 * a2 * a2)) * cvar_matrix;
786 double add1 = (da1 / (2 * a2));
788 double a_square = a2 * a2;
789 double add2 = (a1 * da2 / (2 * a_square ));
791 double err2 = add1 + add2;
818static cpl_error_code hdrl_mode_fit(
819 const cpl_vector * vec,
820 const double histo_min,
821 const double histo_max,
822 const double bin_size,
824 const cpl_size error_niter,
826 double * mode_fit_err)
829 cpl_error_ensure(vec != NULL, CPL_ERROR_NULL_INPUT,
830 return CPL_ERROR_NULL_INPUT,
"Null input vector data");
835 cpl_size semi_fit_window = 2;
837 double hmin = histo_min;
838 double hmax = histo_max;
840 gsl_histogram * h = hdrl_mode_histogram(vec, hmin, hmax, nbin);
842 cpl_error_ensure(h != NULL, CPL_ERROR_NULL_INPUT,
843 return CPL_ERROR_NULL_INPUT,
"Histogram can not be created");
845 cpl_size histogram_bin_max = (cpl_size) gsl_histogram_max_bin(h);
846 cpl_size histo_nbins = (cpl_size) gsl_histogram_bins(h);
848 if(histogram_bin_max > 0) {
849 cpl_msg_debug(cpl_func,
"histogram (bin_max-1) value: %16.8g",
850 gsl_histogram_get(h, histogram_bin_max - 1));
852 if(histogram_bin_max < (histo_nbins-1)) {
853 cpl_msg_debug(cpl_func,
"histogram (bin_max+1) value: %16.8g",
854 gsl_histogram_get(h, histogram_bin_max + 1));
859 gsl_histogram_get_range(h, gsl_histogram_max_bin(h), &lower, &upper);
860 double value_at_max = lower;
865 if( histo_nbins < 3 ) {
866 cpl_error_set(cpl_func, CPL_ERROR_ILLEGAL_INPUT);
867 cpl_msg_debug(cpl_func,
"Cannot do polynomial fit with less than 3 points.");
868 gsl_histogram_free(h);
869 return cpl_error_get_code();
871 cpl_size mn = histogram_bin_max - semi_fit_window;
872 cpl_size mx = histogram_bin_max + semi_fit_window;
873 mn = histogram_bin_max - semi_fit_window;
874 mn = (mn > 0) ? mn : 0;
875 mx = histogram_bin_max + semi_fit_window;
876 mx = (mx < histo_nbins) ? mx : histo_nbins-1;
879 const cpl_size good_points = (mx - mn + 1);
880 const cpl_size win_size = 2 * semi_fit_window + 1;
881 const cpl_size sz = (good_points < win_size) ? good_points : win_size;
883 double* x_vals = cpl_calloc(sz,
sizeof(
double));
884 double* y_vals = cpl_calloc(sz,
sizeof(
double));
885 double* y_errs = cpl_calloc(sz,
sizeof(
double));
890 for(cpl_size i = mn; i <= mx; i++) {
891 double lower_local = 0.;
892 double upper_local = 0.;
893 gsl_histogram_get_range(h, i, &lower_local, &upper_local);
894 x_vals[j] = lower_local;
895 y_vals[j] = gsl_histogram_get(h, i);
903 double* coeffs_val = (
double *) cpl_calloc (sz,
sizeof(
double));
904 double* coeffs_err = (
double *) cpl_calloc (sz,
sizeof(
double));
905 double* fit = (
double *) cpl_calloc (sz,
sizeof(
double));
907 hdrl_gsl_fit_poly (x_vals, y_vals, y_errs, sz, deg+1, fit, coeffs_val,
910 double m = -coeffs_val[1] / 2. / coeffs_val[2];
911 double value_at_mode = gsl_poly_eval(coeffs_val, sz, m);
913 *mode_fit = m + bin_size / 2.;
918 double y_min_val = gsl_poly_eval(coeffs_val, sz,x_vals[0]);
919 double y_max_val = gsl_poly_eval(coeffs_val, sz,x_vals[sz - 1]);
920 double max_parab = (y_max_val > y_min_val ) ? y_max_val : y_min_val;
921 cpl_boolean is_not_supported_fit = CPL_FALSE;
922 if( (fabs(value_at_max - m) > bin_size / 2.) ||
923 (value_at_mode < max_parab ) ) {
927 if( fabs(value_at_max - m) > bin_size / 2. ) {
928 cpl_error_set(cpl_func, CPL_ERROR_ILLEGAL_INPUT);
929 cpl_msg_debug(cpl_func,
"Max too close to point distribution edge: abs(value_at_max+bin_size/2-m) > bin_size");
930 is_not_supported_fit = CPL_TRUE;
933 if(value_at_mode < max_parab) {
934 cpl_error_set(cpl_func, CPL_ERROR_ILLEGAL_INPUT);
935 cpl_msg_debug(cpl_func,
"Value at mode is not a valid maximum: value_at_mode < np.max(parab)");
936 is_not_supported_fit = CPL_TRUE;
938 if(is_not_supported_fit == CPL_TRUE) {
939 gsl_matrix_free(covar);
940 gsl_histogram_free(h);
942 cpl_free(coeffs_val);
943 cpl_free(coeffs_err);
947 return cpl_error_get_code();
951 if (error_niter == 0) {
953 double dof = sz - (deg +1);
954 double scale_factor = (chi2 / dof);
957 hdrl_mode_fit_analytical_error(coeffs_val, coeffs_err, covar,
966 if(!isfinite(*mode_fit_err) || !isfinite(*mode_fit)) {
967 cpl_error_set(cpl_func, CPL_ERROR_ILLEGAL_OUTPUT);
971 cpl_msg_debug(cpl_func,
"(method fit) computed mode: %16.10g err: %16.10g ",
972 *mode_fit, *mode_fit_err);
974 gsl_matrix_free(covar);
976 cpl_free(coeffs_val);
977 cpl_free(coeffs_err);
985 gsl_histogram_free(h);
987 return cpl_error_get_code();
1066 const cpl_image * source,
1067 const double histo_min,
1068 const double histo_max,
1069 const double bin_size,
1071 const cpl_size error_niter,
1073 double * mode_error,
1074 cpl_size * naccepted)
1076 cpl_vector * vec_source = NULL;
1079 cpl_error_ensure(source != NULL, CPL_ERROR_NULL_INPUT,
1080 return CPL_ERROR_NULL_INPUT,
"Null input source image!");
1082 cpl_image* data =(cpl_image*) source;
1085 vec_source = hdrl_image_to_vector(data, cpl_image_get_bpm_const(source));
1087 if (vec_source != NULL) {
1089 hdrl_mode_clip(vec_source, histo_min, histo_max, bin_size, method,
1090 error_niter, mode, mode_error, naccepted);
1092 if(error_niter > 0) {
1095 method, error_niter, mode_error);
1098 if (CPL_ERROR_NONE != cpl_error_get_code()) {
1100 cpl_vector_delete(vec_source);
1101 return cpl_error_get_code();
1108 cpl_error_set(cpl_func, CPL_ERROR_ILLEGAL_INPUT);
1111 cpl_vector_delete(vec_source);
1112 return cpl_error_get_code();
1139 const double histo_min,
1140 const double histo_max,
1141 const double bin_size,
1143 const cpl_size error_niter,
1145 double * mode_error,
1146 cpl_size * naccepted)
1148 cpl_error_ensure(vec != NULL, CPL_ERROR_NULL_INPUT,
1149 return CPL_ERROR_NULL_INPUT,
"Null input source image!");
1153 cpl_vector * loc_vec = NULL;
1154 double bsize = bin_size;
1157 if (bsize <= DBL_EPSILON) {
1158 bsize = hdrl_mode_compute_binsize(vec);
1161 double hmin = histo_min;
1162 double hmax = histo_max;
1164 cpl_vector* trim_vec = NULL;
1168 if ( histo_min >= histo_max) {
1170 trim_vec = cpl_vector_duplicate(vec);
1171 hmin = cpl_vector_get_min(vec) - bsize / 2;
1172 hmax = cpl_vector_get_max(vec) + bsize / 2;
1176 nbin = hdrl_mode_get_nbin(hmin, hmax, bsize);
1177 hmax = hmin + (nbin * bsize);
1180 hmin = nextafter(hmin, hmin - FLT_EPSILON);
1181 hmax = nextafter(hmax, hmax + FLT_EPSILON);
1182 bsize = nextafter(0.0, 1.0);
1188 nbin = hdrl_mode_get_nbin(hmin, hmax, bsize);
1189 trim_vec = hdrl_mode_vector_trim(vec, hmin, hmax);
1190 if( hmin + (nbin * bsize) >= hmax) {
1193 hmax = hmin + (nbin * bsize);
1196 cpl_msg_debug(cpl_func,
"Histogram bin size: %g min: %g max: %g number of bins: %lld",
1197 bsize, hmin, hmax, nbin);
1199 cpl_error_ensure(trim_vec != NULL, CPL_ERROR_NULL_INPUT,
1200 return CPL_ERROR_NULL_INPUT,
"No data for mode "
1201 "computation. Try to change mode parameters ... ");
1211 if (CPL_ERROR_NONE != hdrl_mode_fit(loc_vec, hmin, hmax, bsize, nbin, error_niter, mode,
1214 cpl_msg_debug(cpl_func,
"Mode computation failed using method fit. Try method weight or median.");
1220 if (CPL_ERROR_NONE != hdrl_mode_weight(loc_vec, hmin, hmax, bsize, nbin,
1221 error_niter, mode, mode_error) ) {
1223 cpl_msg_debug(cpl_func,
"Mode computation failed using method weight. Try method fit or median.");
1230 if (CPL_ERROR_NONE != hdrl_mode_median(loc_vec, hmin, hmax, nbin,
1231 error_niter, mode, mode_error) ) {
1233 cpl_msg_debug(cpl_func,
"Mode computation failed using method median. Try method fit or weight");
1240 cpl_msg_debug(cpl_func,
"Unsupported mode method. Supported methods are: fit, weight, median");
1241 return CPL_ERROR_UNSUPPORTED_MODE;
1245 *naccepted = cpl_vector_get_size(vec);
1246 cpl_vector_delete(trim_vec);
1248 return cpl_error_get_code();
1274 const cpl_vector * vec,
1275 const double histo_min,
1276 const double histo_max,
1277 const double bin_size,
1279 const cpl_size error_niter,
1280 double * mode_error)
1289 sizeof(hdrl_random_state *));
1292 uint64_t seed[2] = {(uint64_t)rand(), (uint64_t)rand()};
1296 cpl_size vec_size = cpl_vector_get_size(vec);
1297 const double * pvec = cpl_vector_get_data_const(vec);
1298 cpl_image * ima_mode = cpl_image_new(1, error_niter, CPL_TYPE_DOUBLE);
1299 double * pima_mode = cpl_image_get_data_double(ima_mode);
1303 cpl_binary * pima_mode_bpm = cpl_mask_get_data(cpl_image_get_bpm(ima_mode));
1306 for (cpl_size i = 0; i < error_niter; i++) {
1307 cpl_error_code err = CPL_ERROR_NONE;
1308 cpl_vector * vec_simul = cpl_vector_new(vec_size);
1310 double mode_err = 0.;
1311 cpl_size naccepted = 0;
1312 double * pvec_simul = cpl_vector_get_data(vec_simul);
1315 for (cpl_size j = 0; j < vec_size; j++) {
1319 err =
hdrl_mode_clip(vec_simul, histo_min, histo_max, bin_size, method,
1320 -1, &mode, &mode_err, &naccepted);
1321 cpl_vector_delete(vec_simul);
1323 if (err == CPL_ERROR_NONE) {
1324 pima_mode[i] = mode;
1325 pima_mode_bpm[i] = CPL_BINARY_0;
1328 pima_mode_bpm[i] = CPL_BINARY_1;
1341 *mode_error = cpl_image_get_stdev(ima_mode);
1344 cpl_image_delete(ima_mode);
1351 return cpl_error_get_code();
cpl_error_code hdrl_mode_clip(cpl_vector *vec, const double histo_min, const double histo_max, const double bin_size, const hdrl_mode_type method, const cpl_size error_niter, double *mode, double *mode_error, cpl_size *naccepted)
Compute mode of data.
Definition hdrl_mode.c:1137
#define omp_get_max_threads()
Definition hdrl_mode.c:34
#define omp_get_thread_num()
Definition hdrl_mode.c:35
cpl_error_code hdrl_mode_bootstrap(const cpl_vector *vec, const double histo_min, const double histo_max, const double bin_size, const hdrl_mode_type method, const cpl_size error_niter, double *mode_error)
uses Montecarlo simulations based on the bootstrap technique to determine the error of the mode of th...
Definition hdrl_mode.c:1273
cpl_error_code hdrl_mode_clip_image(const cpl_image *source, const double histo_min, const double histo_max, const double bin_size, const hdrl_mode_type method, const cpl_size error_niter, double *mode, double *mode_error, cpl_size *naccepted)
Compute mode of data.
Definition hdrl_mode.c:1065
cpl_parameterlist * hdrl_mode_parameter_create_parlist(const char *base_context, const char *prefix, const hdrl_parameter *defaults)
Create parameters for the mode collapse.
Definition hdrl_mode.c:89
cpl_error_code hdrl_mode_parameter_parse_parlist(const cpl_parameterlist *parlist, const char *prefix, double *histo_min, double *histo_max, double *bin_size, hdrl_mode_type *method, cpl_size *error_niter)
parse parameterlist for mode parameters to init corresponding hdrl structure parameters
Definition hdrl_mode.c:186
struct _hdrl_parameter_ hdrl_parameter
Definition hdrl_parameter.h:27
hdrl_mode_type
Define the type of the mode that should be computed.
Definition hdrl_mode_defs.h:33
@ HDRL_MODE_MEDIAN
Definition hdrl_mode_defs.h:36
@ HDRL_MODE_WEIGHTED
Definition hdrl_mode_defs.h:38
@ HDRL_MODE_FIT
Definition hdrl_mode_defs.h:40
#define HDRL_OMP(x)
Definition hdrl_cat_def.h:35
hdrl_random_state * hdrl_random_state_new(int type, uint64_t *seed)
create random number generator state
Definition hdrl_random.c:108
void hdrl_random_state_delete(hdrl_random_state *state)
delete random number generator state structure
Definition hdrl_random.c:130
int64_t hdrl_random_uniform_int64(hdrl_random_state *state, int64_t minval, int64_t maxval)
generatore uniformly distributed 64 bit integers within range
Definition hdrl_random.c:153
double hdrl_collapse_mode_parameter_get_bin_size(const hdrl_parameter *p)
get size of the histogram bins
Definition hdrl_collapse.c:690
double hdrl_collapse_mode_parameter_get_histo_min(const hdrl_parameter *p)
get min value
Definition hdrl_collapse.c:658
double hdrl_collapse_mode_parameter_get_histo_max(const hdrl_parameter *p)
get high value
Definition hdrl_collapse.c:674
cpl_size hdrl_collapse_mode_parameter_get_error_niter(const hdrl_parameter *p)
get the error type of the mode
Definition hdrl_collapse.c:722
cpl_boolean hdrl_collapse_parameter_is_mode(const hdrl_parameter *self)
check if parameter is a mode parameter
Definition hdrl_collapse.c:594
hdrl_mode_type hdrl_collapse_mode_parameter_get_method(const hdrl_parameter *p)
get the mode determination method
Definition hdrl_collapse.c:706
char * hdrl_join_string(const char *sep_, int n,...)
join strings together
Definition hdrl_utils.c:810