/** * Get a summary statistic for the orbital elements; for instance, * the median value calculated over all the elements of the list. * @param kl List * @param what Can be one of: STAT_MEAN, STAT_MEDIAN, STAT_STDDEV, STAT_MAD. * Summary statistic is calculated correctly for angle parameters. * @return A matrix whose entries are the summary statistic for the * corresponding orbital element. */ gsl_matrix* KL_getElementsStats(const ok_list* kl, const int what) { int npl = MROWS(kl->kernels[0]->elements); if (npl == 0) return NULL; gsl_vector* v = gsl_vector_alloc(kl->size); gsl_matrix* m = gsl_matrix_alloc(npl, ALL_ELEMENTS_SIZE); gsl_matrix_set_all(m, 0.); for (int i = 0; i < npl; i++) for (int j = 0; j < ALL_ELEMENTS_SIZE; j++) { for (int n = 0; n < kl->size; n++) { VSET(v, n, MGET(kl->kernels[n]->elements, i, j)); } switch (what) { case STAT_MEAN: if (j == MA || j == LOP || j == INC || j == NODE || j == TRUEANOMALY) MSET(m, i, j, ok_average_angle(v->data, v->size, false)); else MSET(m, i, j, gsl_stats_mean(v->data, 1, v->size)); break; case STAT_STDDEV: if (j == MA || j == LOP || j == INC || j == NODE || j == TRUEANOMALY) { MSET(m, i, j, ok_stddev_angle(v->data, v->size, false)); } else MSET(m, i, j, gsl_stats_sd(v->data, 1, v->size)); break; case STAT_MEDIAN: if (j == MA || j == LOP || j == INC || j == NODE || j == TRUEANOMALY) MSET(m, i, j, ok_median_angle(v->data, v->size, false)); else { gsl_sort_vector(v); MSET(m, i, j, gsl_stats_median_from_sorted_data(v->data, 1, v->size)); } break; case STAT_MAD: if (j == MA || j == LOP || j == INC || j == NODE || j == TRUEANOMALY) { double med = ok_median_angle(v->data, v->size, false); MSET(m, i, j, 1.4826 * ok_mad_angle(v->data, v->size, med, false)); } else { gsl_sort_vector(v); double med = gsl_stats_median_from_sorted_data(v->data, 1, v->size); MSET(m, i, j, 1.4826 * ok_mad(v->data, v->size, med)); } break; default: // percentiles gsl_sort_vector(v); MSET(m, i, j, gsl_stats_quantile_from_sorted_data(v->data, 1, v->size, (double)(what)/100.)); }; } gsl_vector_free(v); return m; }
/** * \brief Subtract the running median from complex data * * This function uses \c gsl_stats_median_from_sorted_data to subtract a running median, calculated from the 30 * consecutive point around a set point, from the data. At the start of the data running median is calculated from * 30-15+(i-1) points, and at the end it is calculated from 15+(N-i) points, where i is the point index and N is the * total number of data points. * * \param data [in] A complex data vector * * \return A complex vector containing data with the running median removed */ COMPLEX16Vector *subtract_running_median( COMPLEX16Vector *data ){ COMPLEX16Vector *submed = NULL; UINT4 length = data->length, i = 0, j = 0, n = 0; UINT4 mrange = 0; UINT4 N = 0; INT4 sidx = 0; if ( length > 30 ){ mrange = 30; } /* perform running median with 30 data points */ else { mrange = 2*floor((length-1)/2); } /* an even number less than length */ N = (UINT4)floor(mrange/2); submed = XLALCreateCOMPLEX16Vector( length ); for ( i = 1; i < length+1; i++ ){ REAL8 *dre = NULL; REAL8 *dim = NULL; /* get median of data within RANGE */ if ( i < N ){ n = N+i; sidx = 0; } else{ if ( i > length - N ){ n = length - i + N; } else{ n = mrange; } sidx = i-N; } dre = XLALCalloc( n, sizeof(REAL8) ); dim = XLALCalloc( n, sizeof(REAL8) ); for ( j = 0; j < n; j++ ){ dre[j] = creal(data->data[j+sidx]); dim[j] = cimag(data->data[j+sidx]); } /* sort data */ gsl_sort( dre, 1, n ); gsl_sort( dim, 1, n ); /* get median and subtract from data*/ submed->data[i-1] = ( creal(data->data[i-1]) - gsl_stats_median_from_sorted_data( dre, 1, n ) ) + I * ( cimag(data->data[i-1]) - gsl_stats_median_from_sorted_data( dim, 1, n ) ); XLALFree( dre ); XLALFree( dim ); } return submed; }
/* Statistics code * Make only one call to reading for a particular time range and compute all our stats */ void _compute_statistics(cdb_range_t *range, uint64_t *num_recs, cdb_record_t *records) { uint64_t i = 0; uint64_t valid = 0; double sum = 0.0; double *values = calloc(*num_recs, sizeof(double)); for (i = 0; i < *num_recs; i++) { if (!isnan(records[i].value)) { sum += values[valid] = records[i].value; valid++; } } range->num_recs = valid; range->mean = gsl_stats_mean(values, 1, valid); range->max = gsl_stats_max(values, 1, valid); range->min = gsl_stats_min(values, 1, valid); range->sum = sum; range->stddev = gsl_stats_sd(values, 1, valid); range->absdev = gsl_stats_absdev(values, 1, valid); /* The rest need sorted data */ gsl_sort(values, 1, valid); range->median = gsl_stats_median_from_sorted_data(values, 1, valid); range->pct95th = gsl_stats_quantile_from_sorted_data(values, 1, valid, 0.95); range->pct75th = gsl_stats_quantile_from_sorted_data(values, 1, valid, 0.75); range->pct50th = gsl_stats_quantile_from_sorted_data(values, 1, valid, 0.50); range->pct25th = gsl_stats_quantile_from_sorted_data(values, 1, valid, 0.25); /* MAD must come last because it alters the values array * http://en.wikipedia.org/wiki/Median_absolute_deviation */ for (i = 0; i < valid; i++) { values[i] = fabs(values[i] - range->median); if (values[i] < 0.0) { values[i] *= -1.0; } } /* Final sort is required MAD */ gsl_sort(values, 1, valid); range->mad = gsl_stats_median_from_sorted_data(values, 1, valid); free(values); }
static double robust_madsigma(const gsl_vector *r, gsl_multifit_robust_workspace *w) { size_t n = r->size; const size_t p = w->p; double sigma; size_t i; /* allow for the possibility that r->size < w->n */ gsl_vector_view v1 = gsl_vector_subvector(w->workn, 0, n); gsl_vector_view v2; /* copy |r| into workn */ for (i = 0; i < n; ++i) { gsl_vector_set(&v1.vector, i, fabs(gsl_vector_get(r, i))); } gsl_sort_vector(&v1.vector); /* * ignore the smallest p residuals when computing the median * (see Street et al 1988) */ v2 = gsl_vector_subvector(&v1.vector, p - 1, n - p + 1); sigma = gsl_stats_median_from_sorted_data(v2.vector.data, v2.vector.stride, v2.vector.size) / 0.6745; return sigma; } /* robust_madsigma() */
int main(void) { double data[5] = {17.2, 18.1, 16.5, 18.3, 12.6}; double median, upperq, lowerq; printf ("Original dataset: %g, %g, %g, %g, %g\n", data[0], data[1], data[2], data[3], data[4]); gsl_sort (data, 1, 5); printf ("Sorted dataset: %g, %g, %g, %g, %g\n", data[0], data[1], data[2], data[3], data[4]); median = gsl_stats_median_from_sorted_data (data, 1, 5); upperq = gsl_stats_quantile_from_sorted_data (data, 1, 5, 0.75); lowerq = gsl_stats_quantile_from_sorted_data (data, 1, 5, 0.25); printf ("The median is %g\n", median); printf ("The upper quartile is %g\n", upperq); printf ("The lower quartile is %g\n", lowerq); return 0; }
/** * Get a summary statistic for the parameters; for instance, * the median value calculated over all the elements of the list. * @param kl List * @param what Can be one of: STAT_MEAN, STAT_MEDIAN, STAT_STDDEV, STAT_MAD. * @return A vector whose entries are the summary statistic for the * corresponding orbital parameter. */ gsl_vector* KL_getParsStats(const ok_list* kl, const int what) { gsl_vector* v = gsl_vector_alloc(kl->size); gsl_vector* ret = gsl_vector_calloc(PARAMS_SIZE + 1); for (int j = 0; j < PARAMS_SIZE + 1; j++) { if (j == PARAMS_SIZE) for (int n = 0; n < kl->size; n++) { VSET(v, n, kl->kernels[n]->merit); } else for (int n = 0; n < kl->size; n++) { VSET(v, n, VGET(kl->kernels[n]->params, j)); } switch (what) { case STAT_MEAN: VSET(ret, j, gsl_stats_mean(v->data, 1, v->size)); break; case STAT_STDDEV: VSET(ret, j, gsl_stats_sd(v->data, 1, v->size)); break; case STAT_MEDIAN: gsl_sort_vector(v); VSET(ret, j, gsl_stats_median_from_sorted_data(v->data, 1, v->size)); break; case STAT_MAD: gsl_sort_vector(v); double med = gsl_stats_median_from_sorted_data(v->data, 1, v->size); VSET(ret, j, 1.4826 * ok_mad(v->data, v->size, med)); break; default: // percentiles gsl_sort_vector(v); VSET(v , j, gsl_stats_quantile_from_sorted_data(v->data, 1, v->size, (double)(what)/100.)); }; }; gsl_vector_free(v); return ret; }
void updateEpsilon_particle_arr(particle_arr *part, double *diffArr) { int p = part->p; int i; for (i=0; i<p; i++) diffArr[i] = part->array[i]->diff; gsl_sort(diffArr, 1, p); part->epsilon = gsl_stats_median_from_sorted_data(diffArr, 1, p); printf("epsilon_median = %.5f\n", part->epsilon); printf("Success rate = %.5f\n", p / (double)part->nbAttempts); return; }
// feature 03: Mean average deviation from median (in 2D xy) int LSL_lfeatures_class :: feature_03(LSL_Point3D_container *laserfeat_cluster, Real *out) { char ret = 1; double s_sum = 0; LSL_Point3D_str pts_median; std::vector <double> pts_coordx,pts_coordy; // take x laserfeat_cluster -> get_coords(pts_coordx, GEOMETRY_COORD_X); // sort data gsl_sort (&pts_coordx[0], 1, pts_coordx.size()); // median pts_median.x = gsl_stats_median_from_sorted_data (&pts_coordx[0], 1, pts_coordx.size()); // take y laserfeat_cluster -> get_coords(pts_coordy, GEOMETRY_COORD_Y); // sort data gsl_sort (&pts_coordy[0], 1, pts_coordy.size()); // median pts_median.y = gsl_stats_median_from_sorted_data (&pts_coordy[0], 1, pts_coordy.size()); // project on xy plane pts_median.z = 0; for(unsigned int i = 0; i < laserfeat_cluster -> pts.size(); i++ ) { double val = distance_L2_XY (&pts_median, &laserfeat_cluster -> pts[i]); s_sum += val * val; } if(s_sum == 0) ret = 0; *out = sqrt( ( 1.0 / (double)laserfeat_cluster -> pts.size() ) * s_sum); return(ret); }
StatBox2D::BoxWhiskerData Layout2D::generateBoxWhiskerData(Column *colData, int from, int to, int key) { size_t size = static_cast<size_t>((to - from) + 1); double *data = new double[size]; for (int i = 0, j = from; j < to + 1; i++, j++) { data[i] = colData->valueAt(i); } // sort the data gsl_sort(data, 1, size - 1); StatBox2D::BoxWhiskerData statBoxData; statBoxData.key = key; // basic stats statBoxData.mean = gsl_stats_mean(data, 1, size); statBoxData.median = gsl_stats_median_from_sorted_data(data, 1, size); statBoxData.sd = gsl_stats_sd(data, 1, size); statBoxData.se = statBoxData.sd / sqrt(static_cast<double>(size)); // data bounds statBoxData.boxWhiskerDataBounds.sd_lower = statBoxData.mean - statBoxData.sd; statBoxData.boxWhiskerDataBounds.sd_upper = statBoxData.mean + statBoxData.sd; statBoxData.boxWhiskerDataBounds.se_lower = statBoxData.mean - statBoxData.se; statBoxData.boxWhiskerDataBounds.se_upper = statBoxData.mean + statBoxData.se; statBoxData.boxWhiskerDataBounds.perc_1 = gsl_stats_quantile_from_sorted_data(data, 1, size, 0.01); statBoxData.boxWhiskerDataBounds.perc_5 = gsl_stats_quantile_from_sorted_data(data, 1, size, 0.05); statBoxData.boxWhiskerDataBounds.perc_10 = gsl_stats_quantile_from_sorted_data(data, 1, size, 0.10); statBoxData.boxWhiskerDataBounds.perc_25 = gsl_stats_quantile_from_sorted_data(data, 1, size, 0.25); statBoxData.boxWhiskerDataBounds.perc_75 = gsl_stats_quantile_from_sorted_data(data, 1, size, 0.75); statBoxData.boxWhiskerDataBounds.perc_90 = gsl_stats_quantile_from_sorted_data(data, 1, size, 0.90); statBoxData.boxWhiskerDataBounds.perc_95 = gsl_stats_quantile_from_sorted_data(data, 1, size, 0.95); statBoxData.boxWhiskerDataBounds.perc_99 = gsl_stats_quantile_from_sorted_data(data, 1, size, 0.99); statBoxData.boxWhiskerDataBounds.max = data[size - 1]; statBoxData.boxWhiskerDataBounds.min = data[0]; // delete the double data pointer delete[] data; return statBoxData; }
/*! \fn VImage VMedianImage2d (VImage src, VImage dest, int dim, VBoolean ignore) \param src input image \param dest output image \param dim kernel size \param ignore whether to ignore zero voxels */ VImage VMedianImage2d (VImage src, VImage dest, int dim, VBoolean ignore) { int nbands,nrows,ncols; int i,len,b,r,c,rr,cc,d=0; gsl_vector *vec=NULL; double u,tiny=1.0e-10; if (dim%2 == 0) VError("VMedianImage2d: dim (%d) must be odd",dim); nrows = VImageNRows (src); ncols = VImageNColumns (src); nbands = VImageNBands (src); d = dim/2; len = dim * dim; vec = gsl_vector_calloc(len); dest = VCopyImage(src,dest,VAllBands); for (b=0; b<nbands; b++) { for (r=d; r<nrows-d; r++) { for (c=d; c<ncols-d; c++) { gsl_vector_set_zero(vec); u = VGetPixel(src,b,r,c); if ( !ignore || ABS(u) > tiny) { i = 0; for (rr=r-d; rr<=r+d; rr++) { for (cc=c-d; cc<=c+d; cc++) { u = VGetPixel(src,b,rr,cc); if (ABS(u) > 0) { gsl_vector_set(vec,i,u); i++; } } } gsl_sort_vector(vec); u = gsl_stats_median_from_sorted_data(vec->data,vec->stride,i); VSetPixel(dest,b,r,c,u); } } } } return dest; }
void print_measurement_results_prediction(const job_t* job_p, const reprompib_common_options_t* opts_p, double* maxRuntimes_sec, const nrep_pred_params_t* pred_params_p, const pred_conditions_t* conds_p) { int j; int my_rank, np; double mean_runtime_sec, median_runtime_sec; FILE* f; MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); MPI_Comm_size(MPI_COMM_WORLD, &np); f = stdout; if (my_rank == OUTPUT_ROOT_PROC) { if (opts_p->output_file != NULL) { f = fopen(opts_p->output_file, "a"); } if (job_p->n_rep == 0) { // no correct results // runtime_sec = 0; mean_runtime_sec = 0; median_runtime_sec = 0; } else { // print the last measured runtime //runtime_sec = maxRuntimes_sec[job_p->n_rep - 1]; mean_runtime_sec = gsl_stats_mean(maxRuntimes_sec, 1, job_p->n_rep); qsort(maxRuntimes_sec, job_p->n_rep, sizeof(double), cmpfunc); median_runtime_sec = gsl_stats_median_from_sorted_data(maxRuntimes_sec, 1, job_p->n_rep); } for (j = 0; j < conds_p->n_methods; j++) { fprintf(f, "%s %ld %ld %.10f %.10f %s %.10f\n", get_call_from_index(job_p->call_index), job_p->n_rep, job_p->count, mean_runtime_sec, median_runtime_sec, get_prediction_methods_list()[pred_params_p->info[j].method], conds_p->conditions[j]); } if (opts_p->output_file != NULL) { fclose(f); } } }
void BoxCurve::drawBox(QPainter *painter, const QwtDiMap &xMap, const QwtDiMap &yMap, double *dat, int size) { const int px = xMap.transform(x(0)); const int px_min = xMap.transform(x(0) - 0.5); const int px_max = xMap.transform(x(0) + 0.5); const int box_width = 1+(px_max - px_min)*b_width/100; const int hbw = box_width/2; const int median = yMap.transform(gsl_stats_median_from_sorted_data (dat, 1, size)); int b_lowerq, b_upperq; double sd, se, mean; if(w_range == SD || w_range == SE || b_range == SD || b_range == SE) { sd = gsl_stats_sd(dat, 1, size); se = sd/sqrt((double)size); mean = gsl_stats_mean(dat, 1, size); } if(b_range == SD) { b_lowerq = yMap.transform(mean - sd*b_coeff); b_upperq = yMap.transform(mean + sd*b_coeff); } else if(b_range == SE) { b_lowerq = yMap.transform(mean - se*b_coeff); b_upperq = yMap.transform(mean + se*b_coeff); } else { b_lowerq = yMap.transform(gsl_stats_quantile_from_sorted_data (dat, 1, size, 1-0.01*b_coeff)); b_upperq = yMap.transform(gsl_stats_quantile_from_sorted_data (dat, 1, size, 0.01*b_coeff)); } //draw box if (b_style == Rect) { const QRect r = QRect(px - hbw, b_upperq, box_width, b_lowerq - b_upperq + 1); painter->fillRect(r, QwtPlotCurve::brush()); painter->drawRect(r); } else if (b_style == Diamond) { const QPointArray pa(4); pa[0] = QPoint(px, b_upperq); pa[1] = QPoint(px + hbw, median); pa[2] = QPoint(px, b_lowerq); pa[3] = QPoint(px - hbw, median); painter->setBrush(QwtPlotCurve::brush()); painter->drawPolygon(pa); } else if (b_style == WindBox) { const int lowerq = yMap.transform(gsl_stats_quantile_from_sorted_data (dat, 1, size, 0.25)); const int upperq = yMap.transform(gsl_stats_quantile_from_sorted_data (dat, 1, size, 0.75)); const QPointArray pa(8); pa[0] = QPoint(px + hbw, b_upperq); pa[1] = QPoint(int(px + 0.4*box_width), upperq); pa[2] = QPoint(int(px + 0.4*box_width), lowerq); pa[3] = QPoint(px + hbw, b_lowerq); pa[4] = QPoint(px - hbw, b_lowerq); pa[5] = QPoint(int(px - 0.4*box_width), lowerq); pa[6] = QPoint(int(px - 0.4*box_width), upperq); pa[7] = QPoint(px - hbw, b_upperq); painter->setBrush(QwtPlotCurve::brush()); painter->drawPolygon(pa); } else if (b_style == Notch) { int j = (int)ceil(0.5*(size - 1.96*sqrt((double)size))); int k = (int)ceil(0.5*(size + 1.96*sqrt((double)size))); const int lowerCI = yMap.transform(dat[j]); const int upperCI = yMap.transform(dat[k]); const QPointArray pa(10); pa[0] = QPoint(px + hbw, b_upperq); pa[1] = QPoint(px + hbw, upperCI); pa[2] = QPoint(int(px + 0.25*hbw), median); pa[3] = QPoint(px + hbw, lowerCI); pa[4] = QPoint(px + hbw, b_lowerq); pa[5] = QPoint(px - hbw, b_lowerq); pa[6] = QPoint(px - hbw, lowerCI); pa[7] = QPoint(int(px - 0.25*hbw), median); pa[8] = QPoint(px - hbw, upperCI); pa[9] = QPoint(px - hbw, b_upperq); painter->setBrush(QwtPlotCurve::brush()); painter->drawPolygon(pa); } if (w_range) {//draw whiskers const int l = int(0.1*box_width); int w_upperq, w_lowerq; if(w_range == SD) { w_lowerq = yMap.transform(mean - sd*w_coeff); w_upperq = yMap.transform(mean + sd*w_coeff); } else if(w_range == SE) { w_lowerq = yMap.transform(mean - se*w_coeff); w_upperq = yMap.transform(mean + se*w_coeff); } else { w_lowerq = yMap.transform(gsl_stats_quantile_from_sorted_data (dat, 1, size, 1-0.01*w_coeff)); w_upperq = yMap.transform(gsl_stats_quantile_from_sorted_data (dat, 1, size, 0.01*w_coeff)); } painter->drawLine(px - l, w_lowerq, px + l, w_lowerq); painter->drawLine(px - l, w_upperq, px + l, w_upperq); if (b_style) { if (w_upperq != b_upperq) painter->drawLine(px, w_upperq, px, b_upperq); if (w_lowerq != b_lowerq) painter->drawLine(px, w_lowerq, px, b_lowerq); } else painter->drawLine(px, w_upperq, px, w_lowerq); } //draw median line if (b_style == Notch || b_style == NoBox) return; if (b_style == WindBox) painter->drawLine(int(px - 0.4*box_width), median, int(px + 0.4*box_width), median); else painter->drawLine(px - hbw, median, px + hbw, median); }
/*! \fn VImage VMedianImage3d (VImage src, VImage dest, int dim, VBoolean ignore) \param src input image \param dest output image \param dim kernel size (3,5,7...) \param ignore whether to ignore zero voxels */ VImage VMedianImage3d (VImage src, VImage dest, int dim, VBoolean ignore) { int nbands,nrows,ncols; int i,len,len2,b,r,c,bb,rr,cc,b0,b1,r0,r1,c0,c1,d=0; gsl_vector *vec=NULL; double u,tiny=1.0e-10; if (dim%2 == 0) VError("VMedianImage3d: dim (%d) must be odd",dim); nrows = VImageNRows (src); ncols = VImageNColumns (src); nbands = VImageNBands (src); if (nbands <= d) VError("VMedianImage3d: number of slices too small (%d)",nbands); d = dim/2; len = dim * dim * dim; len2 = 10; if (len < len2) len2 = len; vec = gsl_vector_calloc(len); dest = VCopyImage(src,dest,VAllBands); for (b=d; b<nbands-d; b++) { for (r=d; r<nrows-d; r++) { for (c=d; c<ncols-d; c++) { u = VGetPixel(src,b,r,c); if ( !ignore || ABS(u) > tiny) { i = 0; b0 = b-d; b1 = b+d; for (bb=b0; bb<=b1; bb++) { r0 = r-d; r1 = r+d; for (rr=r0; rr<=r1; rr++) { c0 = c-d; c1 = c+d; for (cc=c0; cc<=c1; cc++) { u = VGetPixel(src,bb,rr,cc); if (ABS(u) > tiny) { gsl_vector_set(vec,i,u); i++; } } } } if (i < len2) continue; gsl_sort_vector(vec); u = gsl_stats_median_from_sorted_data(vec->data,vec->stride,i); VSetPixel(dest,b,r,c,u); } } } } gsl_vector_free(vec); return dest; }
void posterior_summary(const gsl_matrix *theta, FILE *ofile, long M) { size_t T=theta->size1; size_t npar=theta->size2; gsl_vector *tmp=gsl_vector_alloc(T); int i,j; double median,lower,upper; printf("\n Writing MCMC draws to out\n\n"); FILE *file = fopen("out","w"); for(i=0;i<T;i++){ for(j=0;j<npar;j++) fprintf(file,"%14.6e ",mget(theta,i,j)); fprintf(file,"\n"); } fprintf(ofile,"\n\n Posterior Summary \n"); fprintf(ofile,"\n T=%lu\n\n",T); fprintf(ofile,"\n Mean Median Stdev 0.95 DI\n\n"); for(i=0;i<npar;i++){ gsl_matrix_get_col( tmp, theta, i); gsl_sort_vector(tmp); median=gsl_stats_median_from_sorted_data(tmp->data,tmp->stride,tmp->size); lower=gsl_stats_quantile_from_sorted_data(tmp->data,tmp->stride,tmp->size,0.025); upper=gsl_stats_quantile_from_sorted_data(tmp->data,tmp->stride,tmp->size,0.975); fprintf(ofile,"%2d %14.7e %14.7e %14.7e (%14.7e,%14.7e)\n" ,i,mean(tmp),median,sqrt(var(tmp)),lower,upper); } long tau; if( M < 0 ) tau=1000; else tau=M; gsl_vector *rho=gsl_vector_alloc(tau); fprintf(ofile,"\n ACF"); fprintf(ofile,"\n NSE Ineff 1 50 100 500\n"); for(i=0;i<npar;i++){ gsl_matrix_get_col( tmp, theta, i); acf(tmp,tau,rho); /* write out ACF for each parameter */ char file_name[20] = "acf.dat"; sprintf( &file_name[7],"%d",i); FILE *fp_acf = fopen( file_name, "w"); for(j=0;j<tau;j++) fprintf(fp_acf,"%g\n",vget(rho,j)); fclose(fp_acf); /* get inefficiency factor using Newey-West estimate of Long-run var*/ double ineff=1.0; for(j=0;j<tau-1;j++){ ineff += 2.0*(tau-j-1)/tau*vget(rho,j); } /* numerical standard error for posterior mean */ double nse=sqrt(var(tmp)*ineff/T); fprintf(ofile,"%2d %12.5e %12.5e %12.5e %12.5e %12.5e %12.5e\n" ,i,nse,ineff,vget(rho,0),vget(rho,49),vget(rho,99),vget(rho,499)); /* produce kernel density plot for each parameter */ char file_name2[20] = "den.dat"; sprintf( &file_name2[7],"%d",i); FILE *fp_den = fopen( file_name2, "w"); double stdev = sqrt(var(tmp)); lower = gsl_vector_min(tmp) - stdev; upper = gsl_vector_max(tmp) + stdev; den_est_file(tmp, lower , upper ,100, fp_den, -1.0); } fprintf(ofile,"\n\n"); gsl_vector_free(rho); gsl_vector_free(tmp); }
void LALCleanCOMPLEX8SFT (LALStatus *status,/**< pointer to LALStatus structure */ SFTtype *sft, /**< SFT to be cleaned */ INT4 width, /**< maximum width to be cleaned -- set sufficiently large if all bins in each line are to be cleaned*/ INT4 window,/**< window size for noise floor estimation in vicinity of a line */ LineNoiseInfo *lineInfo, /**< list of lines */ RandomParams *randPar /**< parameters for generating random noise */) { /* function to clean the SFT based on the line information read earlier */ INT4 nLines, count, leftCount, rightCount, lineBin, minBin, maxBin, k, tempk; INT4 leftWingBins, rightWingBins, length, sumBins; REAL8 deltaF, f0, tBase, bias; REAL8 stdPow, medianPow; REAL8 *tempDataPow=NULL; REAL8 *lineFreq=NULL; REAL8 *leftWing=NULL; REAL8 *rightWing=NULL; COMPLEX8 *inData; /* FILE *fp=NULL; INT4 seed, ranCount; RandomParams *randPar=NULL; */ /* 09/09/05 gam; randPar now a function argument */ static REAL4Vector *ranVector=NULL; REAL4 *randVal; /* --------------------------------------------- */ INITSTATUS(status); ATTATCHSTATUSPTR (status); /* Make sure the arguments are not NULL: */ ASSERT (sft, status, SFTCLEANH_ENULL, SFTCLEANH_MSGENULL); inData = sft->data->data; ASSERT (inData, status, SFTCLEANH_ENULL, SFTCLEANH_MSGENULL); ASSERT (lineInfo, status, SFTCLEANH_ENULL, SFTCLEANH_MSGENULL); ASSERT (lineInfo->nLines, status, SFTCLEANH_EVAL, SFTCLEANH_MSGEVAL); ASSERT (lineInfo->lineFreq, status, SFTCLEANH_ENULL, SFTCLEANH_MSGENULL); ASSERT (lineInfo->leftWing, status, SFTCLEANH_ENULL, SFTCLEANH_MSGENULL); ASSERT (lineInfo->rightWing, status, SFTCLEANH_ENULL, SFTCLEANH_MSGENULL); ASSERT (window > 0, status, SFTCLEANH_EVAL, SFTCLEANH_MSGEVAL); ASSERT (width >= 0, status, SFTCLEANH_EVAL, SFTCLEANH_MSGEVAL); length = sft->data->length; ASSERT (length > 0, status, SFTCLEANH_EHEADER, SFTCLEANH_MSGEVAL); /* get the value of RngMedBias from the window size */ TRY( LALRngMedBias( status->statusPtr, &bias, 2*window ), status ); /* copy pointers from input */ nLines = lineInfo->nLines; lineFreq = lineInfo->lineFreq; leftWing = lineInfo->leftWing; rightWing = lineInfo->rightWing; deltaF = sft->deltaF; tBase = 1.0/deltaF; f0 = sft->f0; minBin = lround(f0/deltaF); maxBin = minBin + length - 1; /* allocate memory for storing sft power */ tempDataPow = LALMalloc(2*window*sizeof(REAL8)); /* 09/09/05 gam; randPar now a function argument */ /* fp=fopen("/dev/urandom", "r"); ASSERT(fp, status, SFTCLEANH_EFILE, SFTCLEANH_MSGEFILE); ranCount = fread(&seed, sizeof(seed), 1, fp); ASSERT(ranCount==1, status, SFTCLEANH_EREAD, SFTCLEANH_MSGEREAD); fclose(fp); */ /* calculate total number of bins to see how many random numbers must be generated */ sumBins = 0; for (count = 0; count < nLines; count++) { { INT4 tempSumBins; tempSumBins = floor(tBase*leftWing[count]) + floor(tBase*rightWing[count]); sumBins += tempSumBins < 2*width ? tempSumBins : 2*width; } } /* TRY ( LALCreateRandomParams (status->statusPtr, &randPar, seed), status); */ /* 09/09/05 gam; randPar now a function argument */ TRY ( LALCreateVector (status->statusPtr, &ranVector, 2*(sumBins + nLines)), status); TRY ( LALNormalDeviates (status->statusPtr, ranVector, randPar), status); /* TRY ( LALDestroyRandomParams (status->statusPtr, &randPar), status); */ /* 09/09/05 gam; randPar now a function argument */ tempk = 0; /* loop over the lines */ for (count = 0; count < nLines; count++){ /* find frequency bins for line frequency and wings */ lineBin = lround(tBase * lineFreq[count]); leftWingBins = floor(tBase * leftWing[count]); rightWingBins = floor(tBase * rightWing[count]); /* check that frequency is within band of sft and line is not too wide*/ if ((lineBin >= minBin) && (lineBin <= maxBin) && (leftWingBins <= width) && (rightWingBins <= width)){ /* estimate the sft power in "window" # of bins each side */ for (k = 0; k < window ; k++){ if (maxBin - lineBin - rightWingBins - k > 0) inData = sft->data->data + lineBin - minBin + rightWingBins + k + 1; else inData = sft->data->data + length - 1; tempDataPow[k] = (crealf(*inData))*(crealf(*inData)) + (cimagf(*inData))*(cimagf(*inData)); if (lineBin - minBin -leftWingBins - k > 0) inData = sft->data->data + lineBin - minBin - leftWingBins - k - 1; else inData = sft->data->data; tempDataPow[k+window] = (crealf(*inData))*(crealf(*inData)) + (cimagf(*inData))*(cimagf(*inData)); } gsl_sort( tempDataPow, 1, 2*window); medianPow = gsl_stats_median_from_sorted_data(tempDataPow, 1, 2*window); stdPow = sqrt(medianPow/(2 * bias)); /* set sft value at central frequency to noise */ inData = sft->data->data + lineBin - minBin; randVal = ranVector->data + tempk; *(inData) = crectf( stdPow * (*randVal), cimagf(*(inData)) ); tempk++; randVal = ranVector->data + tempk; *(inData) = crectf( crealf(*(inData)), stdPow * (*randVal) ); tempk++; /* now go left and set the left wing to noise */ /* make sure that we are always within the sft band */ /* and set bins to zero only if Wing width is smaller than "width" */ if ((leftWingBins <= width)){ for (leftCount = 0; leftCount < leftWingBins; leftCount++){ if ( (lineBin - minBin - leftCount > 0)){ inData = sft->data->data + lineBin - minBin - leftCount - 1; randVal = ranVector->data + tempk; *(inData) = crectf( stdPow * (*randVal), cimagf(*(inData)) ); tempk++; randVal = ranVector->data + tempk; *(inData) = crectf( crealf(*(inData)), stdPow * (*randVal) ); tempk++; } } } /* now go right making sure again to stay within the sft band */ if ((rightWingBins <= width )){ for (rightCount = 0; rightCount < rightWingBins; rightCount++){ if ( (maxBin - lineBin - rightCount > 0)){ inData = sft->data->data + lineBin - minBin + rightCount + 1; randVal = ranVector->data + tempk; *(inData) = crectf( stdPow * (*randVal), cimagf(*(inData)) ); tempk++; randVal = ranVector->data + tempk; *(inData) = crectf( crealf(*(inData)), stdPow * (*randVal) ); tempk++; } } } } } /* end loop over lines */ /* free memory */ LALFree(tempDataPow); TRY (LALDestroyVector (status->statusPtr, &ranVector), status); DETATCHSTATUSPTR (status); /* normal exit */ RETURN (status); }
/** * Finds the median of values in single bin histograms rejecting spectra from masked * detectors and the results of divide by zero (infinite and NaN). * The median is an average that is less affected by small numbers of very large values. * @param input :: A histogram workspace with one entry in each bin * @param excludeZeroes :: If true then zeroes will not be included in the median calculation * @param indexmap :: indexmap * @return The median value of the histograms in the workspace that was passed to it * @throw out_of_range if a value is negative */ std::vector<double> DetectorDiagnostic::calculateMedian(const API::MatrixWorkspace_sptr input, bool excludeZeroes, std::vector<std::vector<size_t> > indexmap) { std::vector<double> medianvec; g_log.debug("Calculating the median count rate of the spectra"); for (size_t j=0; j< indexmap.size(); ++j) { std::vector<double> medianInput; std::vector<size_t> hists=indexmap.at(j); const int nhists = static_cast<int>(hists.size()); // The maximum possible length is that of workspace length medianInput.reserve(nhists); bool checkForMask = false; Geometry::Instrument_const_sptr instrument = input->getInstrument(); if (instrument != NULL) { checkForMask = ((instrument->getSource() != NULL) && (instrument->getSample() != NULL)); } PARALLEL_FOR1(input) for (int i = 0; i<static_cast<int>(hists.size()); ++i) { PARALLEL_START_INTERUPT_REGION if (checkForMask) { const std::set<detid_t>& detids = input->getSpectrum(hists[i])->getDetectorIDs(); if (instrument->isDetectorMasked(detids)) continue; if (instrument->isMonitor(detids)) continue; } const double yValue = input->readY(hists[i])[0]; if ( yValue < 0.0 ) { throw std::out_of_range("Negative number of counts found, could be corrupted raw counts or solid angle data"); } if( boost::math::isnan(yValue) || boost::math::isinf(yValue) || (excludeZeroes && yValue < DBL_EPSILON)) // NaNs/Infs { continue; } // Now we have a good value PARALLEL_CRITICAL(DetectorDiagnostic_median_d) { medianInput.push_back(yValue); } PARALLEL_END_INTERUPT_REGION } PARALLEL_CHECK_INTERUPT_REGION if(medianInput.empty()) { g_log.information("some group has no valid histograms. Will use 0 for median."); medianInput.push_back(0.); } // We need a sorted array to calculate the median std::sort(medianInput.begin(), medianInput.end()); double median = gsl_stats_median_from_sorted_data( &medianInput[0], 1, medianInput.size() ); if ( median < 0 || median > DBL_MAX/10.0 ) { throw std::out_of_range("The calculated value for the median was either negative or unreliably large"); } medianvec.push_back(median); } return medianvec; }
Datum array_mad(PG_FUNCTION_ARGS) { // The formal PostgreSQL array object ArrayType *array; // The array element type Oid arrayElementType; // The array element type width int16 arrayElementTypeWidth; // The array element type "is passed by value" flags (not used, should always be true) bool arrayElementTypeByValue; // The array element type alignment codes (not used) char arrayElementTypeAlignmentCode; // The array contents, as PostgreSQL "datum" objects Datum *arrayContent; // List of "is null" flags for the array contents bool *arrayNullFlags; // The size of each array int arrayLength; int i,j, nelem; double median, mad; double *inarray; if (PG_ARGISNULL(0)) ereport(ERROR, (errmsg("Null arrays not accepted"))); // Get array from input array = PG_GETARG_ARRAYTYPE_P(0); if (ARR_NDIM(array) != 1) ereport(ERROR, (errmsg("One-dimesional arrays are required"))); if (array_contains_nulls(array)) ereport(ERROR, (errmsg("Array contains null elements"))); arrayLength = (ARR_DIMS(array))[0]; arrayElementType = ARR_ELEMTYPE(array); get_typlenbyvalalign(arrayElementType, &arrayElementTypeWidth, &arrayElementTypeByValue, &arrayElementTypeAlignmentCode); deconstruct_array(array, arrayElementType, arrayElementTypeWidth, arrayElementTypeByValue, arrayElementTypeAlignmentCode, &arrayContent, &arrayNullFlags, &arrayLength); inarray = (double*)malloc(arrayLength*sizeof(double)); for (i=0; i<arrayLength; i++) { inarray[i] = DatumGetFloat4(arrayContent[i]); } gsl_sort (inarray, 1, arrayLength); median = gsl_stats_median_from_sorted_data (inarray, 1, arrayLength); for (i=0; i<arrayLength; i++) { inarray[i] = fabs(inarray[i]-median); } gsl_sort (inarray, 1, arrayLength); mad = 1.486 * gsl_stats_median_from_sorted_data (inarray, 1, arrayLength); PG_RETURN_FLOAT8(mad); }
FrequencyCountDialog::FrequencyCountDialog(Table *t, QWidget* parent, Qt::WFlags fl ) : QDialog( parent, fl ), d_source_table(t), d_result_table(NULL), d_col_name(""), d_col_values(NULL), d_bins(10) { setObjectName( "FrequencyCountDialog" ); setWindowTitle(tr("QtiPlot - Frequency count")); setSizeGripEnabled( true ); setAttribute(Qt::WA_DeleteOnClose); QGroupBox *gb1 = new QGroupBox(); QGridLayout *gl1 = new QGridLayout(gb1); ApplicationWindow *app = (ApplicationWindow *)parent; double min = 0.0, max = 0.0, step = 0.0; if (t){ int col = -1; int sr = 0; int er = t->numRows(); int ts = t->table()->currentSelection(); if (ts >= 0){ Q3TableSelection sel = t->table()->selection(ts); sr = sel.topRow(); er = sel.bottomRow() + 1; col = sel.leftCol(); d_col_name = t->colName(col); } int size = 0; for (int i = sr; i < er; i++){ if (!t->text(i, col).isEmpty()) size++; } if (size > 1) d_col_values = gsl_vector_alloc(size); if (d_col_values){ int aux = 0; for (int i = sr; i < er; i++){ if (!t->text(i, col).isEmpty()){ gsl_vector_set(d_col_values, aux, t->cell(i, col)); aux++; } } gsl_sort_vector(d_col_values); min = floor(gsl_vector_get(d_col_values, 0)); max = ceil(gsl_vector_get(d_col_values, size - 1)); step = (max - min)/(double)d_bins; int p = app->d_decimal_digits; double *data = d_col_values->data; QLocale l = app->locale(); QString s = "[" + QDateTime::currentDateTime().toString(Qt::LocalDate)+ " \"" + t->objectName() + "\"]\n"; s += tr("Statistics on %1").arg(d_col_name) + ":\n"; s += tr("Mean") + " = " + l.toString(gsl_stats_mean (data, 1, size), 'f', p) + "\n"; s += tr("Standard Deviation") + " = " + l.toString(gsl_stats_sd(data, 1, size), 'f', p) + "\n"; s += tr("Median") + " = " + l.toString(gsl_stats_median_from_sorted_data(data, 1, size), 'f', p) + "\n"; s += tr("Size") + " = " + QString::number(size) + "\n"; s += "--------------------------------------------------------------------------------------\n"; app->updateLog(s); } } gl1->addWidget(new QLabel(tr("From Minimum")), 0, 0); boxStart = new DoubleSpinBox(); boxStart->setLocale(app->locale()); boxStart->setValue(min); boxStart->setDecimals(app->d_decimal_digits); gl1->addWidget(boxStart, 0, 1); gl1->addWidget(new QLabel(tr("To Maximum")), 1, 0); boxEnd = new DoubleSpinBox(); boxEnd->setLocale(app->locale()); boxEnd->setValue(max); boxEnd->setDecimals(app->d_decimal_digits); gl1->addWidget(boxEnd, 1, 1); gl1->addWidget(new QLabel(tr("Step Size")), 2, 0); boxStep = new DoubleSpinBox(); boxStep->setLocale(app->locale()); boxStep->setValue(step); boxStep->setDecimals(app->d_decimal_digits); gl1->addWidget(boxStep, 2, 1); gl1->setRowStretch(3, 1); gl1->setColumnStretch(1, 1); buttonApply = new QPushButton(tr( "&Apply" )); buttonApply->setDefault( true ); buttonCancel = new QPushButton(tr( "&Cancel" )); buttonOk = new QPushButton(tr( "&Ok" )); QVBoxLayout *vl = new QVBoxLayout(); vl->addWidget(buttonApply); vl->addWidget(buttonOk); vl->addWidget(buttonCancel); vl->addStretch(); QHBoxLayout *hb = new QHBoxLayout(this); hb->addWidget(gb1, 1); hb->addLayout(vl); connect( buttonApply, SIGNAL( clicked() ), this, SLOT( apply() ) ); connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( reject() ) ); connect( buttonOk, SIGNAL( clicked() ), this, SLOT( accept() ) ); }
int main(int argc, char *argv[]) { if(argc<3) { printf("Not enough arguments\n"); printf("Number of arguments:%d\n",argc); exit(1); } if(!CheckFile(argv[1])) { printf("\nfile argv[1]:%s does not exist!.\n",argv[1]); // USAGE; exit(1); } char *name_file_input = argv[1]; char *name_file_output = argv[2]; int i, j; int nb_lines, nb_counts, nb_massRanges; char char_dumb[20]; int *treeId; double mass, dmass, median, quartil1, quartil3, sigma2, mean; double *nodeMass, *diskStellarMass, *diskGasMass, *diskCircularVelocity, *spheroidStellarMass, *spheroidGasMass, *bulgeFractionInRange; FILE *file_input, *file_output; //////////////////////////////////////////////////////////////////////////////////////////////////// file_input = fopen(name_file_input,"r"); nb_lines = 0; while( !feof(file_input) ) { for(i=0; i<5; i++) fscanf(file_input,"%s", char_dumb); nb_lines++; } fclose(file_input); nb_lines=nb_lines-2; printf("Number of lines:%d\n",nb_lines); treeId = malloc((size_t)nb_lines*sizeof(int)); nodeMass = malloc((size_t)nb_lines*sizeof(double)); diskStellarMass = malloc((size_t)nb_lines*sizeof(double)); diskGasMass = malloc((size_t)nb_lines*sizeof(double)); diskCircularVelocity = malloc((size_t)nb_lines*sizeof(double)); spheroidStellarMass = malloc((size_t)nb_lines*sizeof(double)); spheroidGasMass = malloc((size_t)nb_lines*sizeof(double)); file_input = fopen(name_file_input,"r"); for(i=0; i<5; i++) fscanf(file_input,"%s", char_dumb); for(i=0; i<nb_lines; i++ ) fscanf(file_input,"%lf %lf %lf %lf %lf" , &nodeMass[i], &spheroidStellarMass[i], &spheroidGasMass[i], &diskStellarMass[i], &diskGasMass[i] ); fclose(file_input); //////////////////////////////////////////////////////////////////////////////////////////////////// file_output = fopen(name_file_output,"w"); fprintf(file_output, "#1.haloMass #2.quartil1 #3.median #4.quartil3 #5.quartil4\n"); nb_massRanges = 60; dmass = (11.2-10.0)/nb_massRanges; for(mass=10.0; mass<11.2; mass+=dmass) { j = 0; for(i=0; i<nb_lines; i++ ) if( (log10(diskStellarMass[i] + spheroidStellarMass[i]) >= mass) && (log10(diskStellarMass[i] + spheroidStellarMass[i]) < mass+dmass) && (spheroidStellarMass[i]/(diskStellarMass[i] + spheroidStellarMass[i] + diskGasMass[i])<=0.6) ) j++; //&& (diskStellarMass[i] + spheroidStellarMass[i]> 0.0) nb_counts = j; //printf("number of matches:%d\n",nb_counts); bulgeFractionInRange = malloc((size_t)nb_counts*sizeof(double)); j = 0; for(i=0; i<nb_lines; i++ ) if( (log10(diskStellarMass[i] + spheroidStellarMass[i]) >= mass) && (log10(diskStellarMass[i] + spheroidStellarMass[i]) < mass+dmass) && (spheroidStellarMass[i]/(diskStellarMass[i] + spheroidStellarMass[i] + diskGasMass[i])<=0.6)) { //&& (diskStellarMass[i] + spheroidStellarMass[i]> 0.0) bulgeFractionInRange[j] = spheroidStellarMass[i]/(diskGasMass[i] + spheroidGasMass[i] + diskStellarMass[i] + spheroidStellarMass[i]); j++; } gsl_sort(bulgeFractionInRange, 1, nb_counts); mean = gsl_stats_mean (bulgeFractionInRange, 1, nb_counts); median = gsl_stats_median_from_sorted_data(bulgeFractionInRange, 1, nb_counts); sigma2 = gsl_stats_variance (bulgeFractionInRange, 1, nb_counts); quartil1 = gsl_stats_quantile_from_sorted_data (bulgeFractionInRange, 1, nb_counts, 0.25); quartil3 = gsl_stats_quantile_from_sorted_data (bulgeFractionInRange, 1, nb_counts, 0.75); //quartil4 = gsl_stats_quantile_from_sorted_data (gasFractionInRange, 1, nb_counts, 1.0); fprintf(file_output,"%f %f %f %f %f %f\n", mass, quartil1, median, quartil3, mean, sigma2); } fclose(file_output); return 0; }
void sigma_clip_median__cy(double* pixels, int n_pixels, int n_images, double* output, double nsigma, int max_repeat) { int x,y,l, repeat; int n_bytes = n_images*sizeof(bool); // printf("n bytes to hold mask: %d\n", n_bytes); bool *good_value = (bool*)malloc(n_images*sizeof(bool)); // printf("\n\n\ngood-value=%x\n\n\n",good_value); double *pixelvalue = (double*)malloc(n_images*sizeof(double)); for (x=0; x<n_pixels ; x++) { // set output to NaN, this is the default output[x] = nan; /* printf("\n\n\n\n\n\n\rColumn %d", x+1); */ // // Figure out how many pixels are not NaN // int n_good_pixels = 0; for (y=0; y<n_images; y++) { if (!isnan(pixels[x*n_images+y])) { good_value[y] = true; pixelvalue[n_good_pixels] = pixels[x*n_images+y]; n_good_pixels++; } } if (n_good_pixels < 1) { // all pixels appear to be NaNs, nothing left to do continue; } // // Now sort all values // gsl_sort(pixelvalue, 1, n_good_pixels); /* for (l=0; l<n_good_pixels; l++) { */ /* printf("%2d -> %.0f\n", l, pixelvalue[l]); */ /* } */ /* printf("\n"); */ double median, sigma_plus, sigma_minus, upper, lower, sigma; int start=0; int end=n_good_pixels; int i, new_start, new_end, n_valid_values; new_start = start; new_end = end; /* printf("Starting iterating: start/end=%d,%d, new-start/end=%d,%d\n", start, end, new_start, new_end); */ /* for (i=0; i<end; i++) { */ /* printf("%2d: %f\n", i, pixelvalue[i]); */ /* } */ for (repeat=0; repeat<max_repeat && (end-start)>=3; repeat++) { end = new_end; start = new_start; n_valid_values = end - start; /* printf("Iteration %d: start/end=%d,%d #=%d\n", repeat, start, end, n_valid_values); */ // Compute median and the sigma-widths median = gsl_stats_median_from_sorted_data(&pixelvalue[start], 1, n_valid_values); sigma_minus = median - gsl_stats_quantile_from_sorted_data(&pixelvalue[start], 1, n_valid_values, 0.16); sigma_plus = gsl_stats_quantile_from_sorted_data(&pixelvalue[start], 1, n_valid_values, 0.84) - median; sigma = 0.5 * (gsl_stats_quantile_from_sorted_data(&pixelvalue[start], 1, n_valid_values, 0.84) - gsl_stats_quantile_from_sorted_data(&pixelvalue[start], 1, n_valid_values, 0.16)); // Compute the valid range of pixels lower = median - nsigma * sigma; upper = median + nsigma * sigma; /* lower = median - nsigma * sigma_minus; */ /* upper = median + nsigma * sigma_plus; */ // Now figure out the start and end range of pixels within the range /* printf("Lower limit:\n"); */ for (new_start=start; new_start<end; new_start++) { if (pixelvalue[new_start] >= lower) { /* printf("Value %d is %f >= %f, taking as new lower limit\n", */ /* new_start, pixelvalue[new_start], lower); */ break; } /* printf("Value %d is %f < %f, skipping\n", */ /* new_start, pixelvalue[new_start], lower); */ } for (new_end = end; new_end > new_start;) { new_end--; if (pixelvalue[new_end] <= upper) { /* printf("Value %d is %f <= %f, taking as new upper limit\n", */ /* new_end, pixelvalue[new_end], upper); */ // Make stick to nomenclature that end means the index of the first // entry that is no longer contained in the list new_end++; break; } /* printf("Value %d is %f > %f, skipping\n", */ /* new_end, pixelvalue[new_end], upper); */ } /* printf("Was: %d/%d now is %d,%d\n", */ /* start, end, new_start, new_end); */ /* printf("Iteration %d: median=%f (sigma= %f / %f) lower/upper: %f %f #=%d\n", */ /* repeat, median, sigma_minus, sigma_plus, lower, upper, n_valid_values); */ /* printf("%d --> %d D=%d (%d)\n", */ /* pixelvalue, &pixelvalue[start], &pixelvalue[start]-pixelvalue, start); */ /* printf("Remaining valid values:\n"); */ /* for (i=new_start; i<new_end; i++) { */ /* printf("%f\n", pixelvalue[i]); */ /* } */ if (new_start == start && new_end == end) { // This iteration hasn't changed anything, so future iteration won't change // anything either, so we can stop right here /* printf("No change in this iteration skipping rest of work.\n"); */ break; } } if ((new_end - new_start) >= 3) { end = new_end; start = new_start; } // // Now we have all pixels limited down to iteratively select only the // 3-sigma range for each pixel; Now compute the mean value. // output[x] = gsl_stats_median_from_sorted_data(&pixelvalue[start], 1, (end-start)); // output[x] = (double)(end-start); //gsl_stats_mean(&pixelvalue[start], 1, (end-start)); // printf("filtered output: %f (%d, %d)\n", output[x], start, end); } // printf("\n\ngood-value=%x\n\n",good_value); /* free(good_value); */ // printf("Freeing memory\n"); free((void*)good_value); // printf("done freeing\n"); // printf("Work done in c_sigclip.c\n"); return; }
int main (void) { int i, j, warn, lim[13], counter; lim[0]=0.0; lim[1]=1.0; lim[2]=2.0; lim[3]=3.0; lim[4]=4.0; lim[5]=5.0; lim[6]=8.0; lim[7]=12.0; lim[8]=16.0; lim[9]=20.0; lim[10]=24.0; lim[11]=28.0; lim[12]=45.0; counter = 0.0; double pos[N],vel[N],err[N],vel_sq, sum; double mean, median, skew, sd, kurtosis; FILE *velo, *sig, *script; velo = fopen("velocity.dat", "r"); sig = fopen("sigma.dat","w"); for(i=0.0; i<N; i++) { fscanf(velo, "%lf %lf %lf", &pos[i], &vel[i], &err[i]); } //printf ("The sample mean is %g\n", mean); fclose(velo); for(j=0.0; j<12.0; j=j+1.0) { counter = 0.0; for(i = 0.0;(i)<=N; i++) { if( lim[j] <= pos[i] && pos[i] < lim[(j)+1] ) { counter++; gsl_sort (vel, 1, counter); mean = gsl_stats_mean(vel, 1, counter); median = gsl_stats_median_from_sorted_data (vel, 1, counter); sd = gsl_stats_sd (vel, 1, counter); skew = gsl_stats_skew(vel, 1, counter); kurtosis = gsl_stats_kurtosis (vel, 1, counter); } } printf("Particulas entre %d y %d es %d\n",lim[j],lim[j +1], counter); printf ("The sample mean is %g\n", mean); printf ("The median is %g\n", median); printf ("Standard deviation is %g\n", sd); printf ("Skewness is %g\n", skew); printf ("kurtosis is %g\n\n", kurtosis); //printf("Sigma in the bin is: %.2f\n\n", sig); fprintf(sig, "%.2f\t %.2f\n", (lim[j]+lim[j+1])*0.5, sd); } fclose(sig); script = fopen( "script1.gpl", "w" ); fprintf(script, "set grid\nset terminal png\nset output 'Sigma_vs_rad.png'\nset nokey\n"); fprintf( script, "set title 'Sigma vs Radius'\n" ); //fprintf( script, "set logscale x\n" ); fprintf( script, "set xlabel 'Radius in Arcmin'\n" ); //fprintf( script, "set yrange [25:100]\n" ); fprintf( script, "set ylabel 'Sigma (km/s)'\n" ); fprintf( script, "plot 'sigma.dat' u 1:2 pt 7 ps 1\n"); fclose(script); warn = system("gnuplot script1.gpl"); return(warn); }
int main(int argc, char *argv []) { if(populate_env_variable(REF_ERROR_CODES_FILE, "L2_ERROR_CODES_FILE")) { printf("\nUnable to populate [REF_ERROR_CODES_FILE] variable with corresponding environment variable. Routine will proceed without error handling\n"); } if (argc != 7) { if(populate_env_variable(SPE_BLURB_FILE, "L2_SPE_BLURB_FILE")) { RETURN_FLAG = 1; } else { print_file(SPE_BLURB_FILE); } write_key_to_file(ERROR_CODES_FILE, REF_ERROR_CODES_FILE, "L2STATEX", -1, "Status flag for L2 spextract routine", ERROR_CODES_FILE_WRITE_ACCESS); return 1; } else { // *********************************************************************** // Redefine routine input parameters char *input_f = strdup(argv[1]); char *method = strdup(argv[2]); char *ss_method = strdup(argv[3]); double target_half_aperture_px = strtod(argv[4], NULL); int sky_window_half_aperture_px = strtol(argv[5], NULL, 0); char *output_f = strdup(argv[6]); // *********************************************************************** // Open input file (ARG 1), get parameters and perform any data format // checks fitsfile *input_f_ptr; int input_f_maxdim = 2, input_f_status = 0, input_f_bitpix, input_f_naxis; long input_f_naxes [2] = {1,1}; if(!fits_open_file(&input_f_ptr, input_f, IMG_READ_ACCURACY, &input_f_status)) { if(!populate_img_parameters(input_f, input_f_ptr, input_f_maxdim, &input_f_bitpix, &input_f_naxis, input_f_naxes, &input_f_status, "INPUT FRAME")) { if (input_f_naxis != 2) { // any data format checks here write_key_to_file(ERROR_CODES_FILE, REF_ERROR_CODES_FILE, "L2STATEX", -2, "Status flag for L2 spextract routine", ERROR_CODES_FILE_WRITE_ACCESS); free(input_f); free(output_f); free(method); free(ss_method); if(fits_close_file(input_f_ptr, &input_f_status)) fits_report_error (stdout, input_f_status); return 1; } } else { write_key_to_file(ERROR_CODES_FILE, REF_ERROR_CODES_FILE, "L2STATEX", -3, "Status flag for L2 spextract routine", ERROR_CODES_FILE_WRITE_ACCESS); fits_report_error(stdout, input_f_status); free(input_f); free(output_f); free(method); free(ss_method); if(fits_close_file(input_f_ptr, &input_f_status)) fits_report_error (stdout, input_f_status); return 1; } } else { write_key_to_file(ERROR_CODES_FILE, REF_ERROR_CODES_FILE, "L2STATEX", -4, "Status flag for L2 spextract routine", ERROR_CODES_FILE_WRITE_ACCESS); fits_report_error(stdout, input_f_status); free(input_f); free(output_f); free(method); free(ss_method); return 1; } // *********************************************************************** // Set the range limits int cut_x [2] = {1, input_f_naxes[0]}; int cut_y [2] = {1, input_f_naxes[1]}; // *********************************************************************** // Set parameters used when reading data from input file (ARG 1) long fpixel [2] = {cut_x[0], cut_y[0]}; long nxelements = (cut_x[1] - cut_x[0]) + 1; long nyelements = (cut_y[1] - cut_y[0]) + 1; // *********************************************************************** // Create arrays to store pixel values from input fits file (ARG 1) double input_f_pixels [nxelements]; // *********************************************************************** // Get input fits file (ARG 1) values and store in 2D array int ii; double input_frame_values [nyelements][nxelements]; memset(input_frame_values, 0, sizeof(double)*nxelements*nyelements); for (fpixel[1] = cut_y[0]; fpixel[1] <= cut_y[1]; fpixel[1]++) { memset(input_f_pixels, 0, sizeof(double)*nxelements); if(!fits_read_pix(input_f_ptr, TDOUBLE, fpixel, nxelements, NULL, input_f_pixels, NULL, &input_f_status)) { for (ii=0; ii<nxelements; ii++) { input_frame_values[fpixel[1]-1][ii] = input_f_pixels[ii]; } } else { write_key_to_file(ERROR_CODES_FILE, REF_ERROR_CODES_FILE, "L2STATEX", -5, "Status flag for L2 spextract routine", ERROR_CODES_FILE_WRITE_ACCESS); fits_report_error(stdout, input_f_status); free(input_f); free(output_f); free(method); free(ss_method); if(fits_close_file(input_f_ptr, &input_f_status)) fits_report_error (stdout, input_f_status); return 1; } } // *********************************************************************** // Open [SPFIND_OUTPUTF_PEAKS_FILE] input file FILE *inputfile; if (!check_file_exists(SPFIND_OUTPUTF_PEAKS_FILE)) { inputfile = fopen(SPFIND_OUTPUTF_PEAKS_FILE , "r"); } else { write_key_to_file(ERROR_CODES_FILE, REF_ERROR_CODES_FILE, "L2STATEX", -6, "Status flag for L2 spextract routine", ERROR_CODES_FILE_WRITE_ACCESS); return 1; } // *********************************************************************** // Find some [SPFIND_OUTPUTF_PEAKS_FILE] input file details char input_string [150]; int row_count = 0; while(!feof(inputfile)) { memset(input_string, '\0', sizeof(char)*150); fgets(input_string, 150, inputfile); if (strtol(&input_string[0], NULL, 0) > 0) { // check the line begins with a positive number (usable) row_count++; } } rewind(inputfile); // *********************************************************************** // Store [SPFIND_OUTPUTF_PEAKS_FILE] data double x_coords[row_count]; memset(x_coords, 0, sizeof(double)*(row_count)); double y_coords[row_count]; memset(y_coords, 0, sizeof(double)*(row_count)); double coord_x, coord_y; int idx = 0; while(!feof(inputfile)) { memset(input_string, '\0', sizeof(char)*150); fgets(input_string, 150, inputfile); if (strtol(&input_string[0], NULL, 0) > 0) { // check the line begins with a positive number (usable) sscanf(input_string, "%lf\t%lf\n", &coord_x, &coord_y); x_coords[idx] = coord_x; y_coords[idx] = coord_y; idx++; } } double output_frame_values[nxelements]; memset(output_frame_values, 0, sizeof(double)*nxelements); // *********************************************************************** // EXTRACTION if (strcmp(method, "simple") == 0) { // *********************************************************************** // PARTIAL PIXEL APERTURE int ii, jj; double y; y = y_coords[0]; // should only be one bin in [SPFIND_OUTPUTF_PEAKS_FILE] data file double this_col_value; for (ii=0; ii<nxelements; ii++) { this_col_value = 0.; // *********************************************************************** // Does [y] violate the img boundaries? if ((y + target_half_aperture_px > nyelements) || (y - target_half_aperture_px <= 0)) { write_key_to_file(ERROR_CODES_FILE, REF_ERROR_CODES_FILE, "L2STATEX", -7, "Status flag for L2 spextract routine", ERROR_CODES_FILE_WRITE_ACCESS); fits_report_error(stdout, input_f_status); free(input_f); free(output_f); free(method); free(ss_method); if(fits_close_file(input_f_ptr, &input_f_status)) fits_report_error (stdout, input_f_status); fclose(inputfile); return 1; } // *********************************************************************** // Extract flux within aperture double y_low, y_high; y_low = y-target_half_aperture_px-0.5; y_high = y+target_half_aperture_px+0.5; int y_low_floor, y_high_floor; y_low_floor = floor(y-target_half_aperture_px-0.5); // 0.5 as taking (half_ap*2) + 1 as total aperture y_high_floor = floor(y+target_half_aperture_px+0.5); for (jj=y_low_floor; jj<=y_high_floor; jj++) { if (jj == y_low_floor) { // outside pixel where partial flux needs to be taken into account double partial_fraction_of_bin = (y_low_floor + 1) - y_low; this_col_value += partial_fraction_of_bin * input_frame_values[jj][ii]; } else if (jj == y_high_floor) { // outside pixel where partial flux needs to be taken into account double partial_fraction_of_bin = y_high - y_high_floor; this_col_value += partial_fraction_of_bin * input_frame_values[jj][ii]; } else { this_col_value += input_frame_values[jj][ii]; } } output_frame_values[ii] = this_col_value; } } else { write_key_to_file(ERROR_CODES_FILE, REF_ERROR_CODES_FILE, "L2STATEX", -8, "Status flag for L2 spextract routine", ERROR_CODES_FILE_WRITE_ACCESS); fits_report_error(stdout, input_f_status); free(input_f); free(output_f); free(method); free(ss_method); if(fits_close_file(input_f_ptr, &input_f_status)) fits_report_error (stdout, input_f_status); fclose(inputfile); return 1; } // *********************************************************************** // SKY SUBTRACTION if (strcmp(ss_method, "none") == 0) { } else if (strcmp(ss_method, "median") == 0) { // *********************************************************************** // MEDIAN SUBTRACTION int ii, jj; double y; y = y_coords[0]; // should only be one bin in [SPFIND_OUTPUTF_PEAKS_FILE] data file double this_col_value; for (ii=0; ii<nxelements; ii++) { this_col_value = 0.; // *********************************************************************** // Does [y] violate the img boundaries? if ((y + target_half_aperture_px + sky_window_half_aperture_px > nyelements) || (y - target_half_aperture_px - sky_window_half_aperture_px <= 0)) { write_key_to_file(ERROR_CODES_FILE, REF_ERROR_CODES_FILE, "L2STATEX", -9, "Status flag for L2 spextract routine", ERROR_CODES_FILE_WRITE_ACCESS); fits_report_error(stdout, input_f_status); free(input_f); free(output_f); free(method); free(ss_method); if(fits_close_file(input_f_ptr, &input_f_status)) fits_report_error (stdout, input_f_status); fclose(inputfile); return 1; } // *********************************************************************** // Find pixels for sky aperture int ap_lower_lo, ap_lower_hi, ap_upper_lo, ap_upper_hi; ap_lower_lo = floor(y-target_half_aperture_px-0.5) - sky_window_half_aperture_px - 1; ap_lower_hi = floor(y-target_half_aperture_px-0.5) - 1; ap_upper_lo = floor(y+target_half_aperture_px+0.5) + 1; ap_upper_hi = floor(y+target_half_aperture_px+0.5) + sky_window_half_aperture_px + 1; int n_ap_values = (ap_lower_hi-ap_lower_lo) + (ap_upper_hi-ap_upper_lo) + 2; double ap_values[n_ap_values]; int idx = 0; for (jj=ap_lower_lo; jj<=ap_lower_hi; jj++) { ap_values[idx] = input_frame_values[jj][ii]; idx++; } for (jj=ap_upper_lo; jj<=ap_upper_hi; jj++) { ap_values[idx] = input_frame_values[jj][ii]; idx++; } // DEBUG /*for (jj=0; jj<idx; jj++) printf("%f,", ap_values[jj]); printf("\n"); for (jj=0; jj<idx; jj++) printf("%d,", jj); printf("\n");*/ // Take median double ap_values_sorted [n_ap_values]; memcpy(ap_values_sorted, ap_values, sizeof(double)*n_ap_values); gsl_sort(ap_values_sorted, 1, (ap_lower_hi-ap_lower_lo) + (ap_upper_hi-ap_upper_lo) + 2); double ap_values_median = gsl_stats_median_from_sorted_data(ap_values_sorted, 1, n_ap_values); this_col_value = ap_values_median * ((target_half_aperture_px*2) + 1); // need to scale up to target extraction aperture size output_frame_values[ii] -= this_col_value; } } else { write_key_to_file(ERROR_CODES_FILE, REF_ERROR_CODES_FILE, "L2STATEX", -10, "Status flag for L2 spextract routine", ERROR_CODES_FILE_WRITE_ACCESS); fits_report_error(stdout, input_f_status); free(input_f); free(output_f); free(method); free(ss_method); if(fits_close_file(input_f_ptr, &input_f_status)) fits_report_error (stdout, input_f_status); fclose(inputfile); return 1; } // *********************************************************************** // Set output frame parameters fitsfile *output_f_ptr; int output_f_status = 0; long output_f_naxes [2] = {nxelements, 1}; long output_f_fpixel = 1; // *********************************************************************** // Create and write [output_frame_values] to output file (ARG 6) if (!fits_create_file(&output_f_ptr, output_f, &output_f_status)) { if (!fits_create_img(output_f_ptr, INTERMEDIATE_IMG_ACCURACY[0], 2, output_f_naxes, &output_f_status)) { if (!fits_write_img(output_f_ptr, INTERMEDIATE_IMG_ACCURACY[1], output_f_fpixel, nxelements, output_frame_values, &output_f_status)) { } else { write_key_to_file(ERROR_CODES_FILE, REF_ERROR_CODES_FILE, "L2STATEX", -11, "Status flag for L2 spextract routine", ERROR_CODES_FILE_WRITE_ACCESS); fits_report_error(stdout, output_f_status); free(input_f); free(output_f); free(method); free(ss_method); if(fits_close_file(input_f_ptr, &input_f_status)) fits_report_error (stdout, input_f_status); if(fits_close_file(output_f_ptr, &output_f_status)) fits_report_error (stdout, output_f_status); fclose(inputfile); return 1; } } else { write_key_to_file(ERROR_CODES_FILE, REF_ERROR_CODES_FILE, "L2STATEX", -12, "Status flag for L2 spextract routine", ERROR_CODES_FILE_WRITE_ACCESS); fits_report_error(stdout, output_f_status); free(input_f); free(output_f); free(method); free(ss_method); if(fits_close_file(input_f_ptr, &input_f_status)) fits_report_error (stdout, input_f_status); if(fits_close_file(output_f_ptr, &output_f_status)) fits_report_error (stdout, output_f_status); fclose(inputfile); return 1; } } else { write_key_to_file(ERROR_CODES_FILE, REF_ERROR_CODES_FILE, "L2STATEX", -13, "Status flag for L2 spextract routine", ERROR_CODES_FILE_WRITE_ACCESS); fits_report_error(stdout, output_f_status); free(input_f); free(output_f); free(method); if(fits_close_file(input_f_ptr, &input_f_status)) fits_report_error (stdout, input_f_status); fclose(inputfile); return 1; } // *********************************************************************** // Free arrays on heap free(input_f); free(output_f); free(method); free(ss_method); // *********************************************************************** // Close input file (ARG 1) and output file (ARG 6) if(fits_close_file(input_f_ptr, &input_f_status)) { write_key_to_file(ERROR_CODES_FILE, REF_ERROR_CODES_FILE, "L2STATEX", -14, "Status flag for L2 spextract routine", ERROR_CODES_FILE_WRITE_ACCESS); fits_report_error (stdout, input_f_status); return 1; } if(fits_close_file(output_f_ptr, &output_f_status)) { write_key_to_file(ERROR_CODES_FILE, REF_ERROR_CODES_FILE, "L2STATEX", -15, "Status flag for L2 spextract routine", ERROR_CODES_FILE_WRITE_ACCESS); fits_report_error (stdout, output_f_status); return 1; } if (fclose(inputfile)) { write_key_to_file(ERROR_CODES_FILE, REF_ERROR_CODES_FILE, "L2STATTR", -16, "Status flag for L2 spextract routine", ERROR_CODES_FILE_WRITE_ACCESS); return 1; } // Write success to [ERROR_CODES_FILE] write_key_to_file(ERROR_CODES_FILE, REF_ERROR_CODES_FILE, "L2STATEX", RETURN_FLAG, "Status flag for L2 spextract routine", ERROR_CODES_FILE_WRITE_ACCESS); return 0; } }
int main (void) { int i, j, warn, counter; double lim[15]; lim[0]=0.01; //lim[1]=0.02; lim[1]=0.03; //lim[3]=0.04; lim[2]=0.05; //lim[5]=0.09; lim[3]=0.11; //lim[7]=0.14; lim[4]=0.17; //lim[9]=0.208; lim[5]=0.26; //lim[11]=0.32; lim[6]=0.4; //lim[13]=0.51; lim[7]=0.64; //lim[15]=0.8; lim[8]=1.0; //lim[17]=1.25; lim[9]=1.56; //lim[19]=1.95; lim[10]=2.44; //lim[21]=3.05; lim[11]=3.81; //lim[23]=4.76; lim[12]=5.96; //lim[25]=7.45; lim[13]=9.31; //lim[27]=11.64; lim[14]=14.55; //lim[29]=18.18; counter = 0.0; double pos[N],vel[N],err[N],vel_sq, sum; double mean, median, skew, sd, kurtosis, error, sum1, sum2; FILE *velo, *sig, *script; velo = fopen("organized.dat", "r"); sig = fopen("sigma.dat","w"); for(i=0.0; i<N; i++) { fscanf(velo, "%lf %lf %lf", &pos[i], &vel[i], &err[i]); } //printf ("The sample mean is %g\n", mean); fclose(velo); for(j=0.0; j<14.0; j=j+1.0) { counter = 0.0; for(i = 0.0;(i)<=N; i++) { if( lim[j] <= pos[i] && pos[i] < lim[(j)+1] ) { counter++; gsl_sort (vel, 1, counter); mean = gsl_stats_mean(vel, 1, counter); median = gsl_stats_median_from_sorted_data (vel, 1, counter); sd = gsl_stats_sd (vel, 1, counter); skew = gsl_stats_skew(vel, 1, counter); kurtosis = gsl_stats_kurtosis (vel, 1, counter); sum1 = (vel[i]-mean)*(vel[i]-mean); sum1++; sum2 = (vel[i]-mean)*err[i]; sum2++; error = 1.0/(sqrt(counter)) * pow(sum1,-0.5) * sum2; } } printf("Particulas entre %lf y %lf es %d\n",lim[j],lim[j+1], counter); printf("Error %lf\n", error); printf ("The sample mean is %g\n", mean); printf ("The median is %g\n", median); printf ("Standard deviation is %g\n", sd); printf ("Skewness is %g\n", skew); printf ("kurtosis is %g\n\n", kurtosis); //printf("Sigma in the bin is: %.2f\n\n", sig); fprintf(sig, "%lf\t %lf\t %lf\t %d\n", (lim[j]+lim[j+1])*0.5, sd, error, counter); } fclose(sig); script = fopen( "script1.gpl", "w" ); fprintf(script, "set grid\nset terminal png\nset output 'Sigma_vs_rad.png'\nset nokey\n"); fprintf( script, "set title 'Sigma vs Radius'\n" ); fprintf( script, "set logscale x\n" ); fprintf( script, "set xlabel 'Radius in Arcmin'\n" ); fprintf( script, "set xrange [0.01:20]\n" ); //fprintf( script, "set logscale x 10\n" ); fprintf( script, "set ylabel 'Sigma (km/s)'\n" ); fprintf( script, "plot 'sigma.dat' u 1:2:3 pt 7 ps 1 with errorbars\n"); fclose(script); warn = system("gnuplot script1.gpl"); return(warn); }