static window_regression_stats *get_window_regression_stats( const grid<pixel_t> &red, const grid<pixel_t> &nir, size_t size, bool force) { size_t output_block = 100; size_t row_count = (red.height() / size); IF_NORMAL(std::cout << row_count << " rows" << std::endl); size_t window_count = (red.width() / size) * row_count; array<numeric_t> slopes(window_count); array<numeric_t> intercepts(window_count); array<numeric_t> r2s(window_count); array<bool> goodness(window_count); numeric_t *slopes_ptr = slopes.data(); numeric_t *intercepts_ptr = intercepts.data(); numeric_t *r2s_ptr = r2s.data(); bool *ptr_goodness = goodness.data(); size_t good_count = 0; rect<size_t> subr = {0, 0, size, size}; for (subr.y = 0; subr.y + size <= red.height() && subr.y + size <= nir.height(); subr.y += size) { for (subr.x = 0; subr.x + size <= red.width() && subr.x + size <= nir.width(); subr.x += size) { const grid<pixel_t> red_sub( const_cast<grid<pixel_t>*>(&red), subr); const grid<pixel_t> nir_sub( const_cast<grid<pixel_t>*>(&nir), subr); if (force || is_good_data(red_sub, nir_sub)) { linear_regression *reg = stats_find_linear_regression(red_sub, nir_sub); *slopes_ptr = reg->eq.slope; *intercepts_ptr = reg->eq.intercept; *r2s_ptr = reg->r2; *ptr_goodness = true; good_count++; delete reg; } else { IF_VERBOSE(std::cout << "Bad sector: "); IF_VERBOSE(std::cout << "(" << subr.x); IF_VERBOSE(std::cout << ", " << subr.y); IF_VERBOSE(std::cout << ") "); *ptr_goodness = false; } slopes_ptr++; intercepts_ptr++; r2s_ptr++; ptr_goodness++; } if ((subr.y / size) % output_block == 0) { IF_NORMAL(std::cout << "\ranalyzing rows " "starting at " << (subr.y / size) << "..."); IF_NORMAL(std::cout.flush()); } } IF_NORMAL(std::cout << std::endl); // okay, we have slope data, now find statistics window_regression_stats *stats = new window_regression_stats; // first, filter out the bad ones IF_VERBOSE(std::cout << "Found " << good_count); IF_VERBOSE(std::cout << " good sectors out of "); IF_VERBOSE(std::cout << window_count << std::endl); array<numeric_t> good_slope_data(good_count); array<numeric_t> good_intercept_data(good_count); array<numeric_t> good_r2_data(good_count); size_t good_data_cur = 0; for (size_t i = 0; i < window_count; i++) { if (goodness[i]) { good_slope_data[good_data_cur] = slopes[i]; good_intercept_data[good_data_cur] = intercepts[i]; good_r2_data[good_data_cur] = r2s[i]; good_data_cur++; } } stats->slope_mean = numeric_mean(good_slope_data); stats->slope_stddev = numeric_stddev(good_slope_data); stats->intercept_mean = numeric_mean(good_intercept_data); stats->intercept_stddev = numeric_stddev(good_intercept_data); stats->r2_mean = numeric_mean(good_r2_data); stats->r2_stddev = numeric_stddev(good_r2_data); stats->window_size = size; return stats; }
void Histogram::createGoodnessArray(){ for(int i=0; i < 256; i++){ goodnessArray[i]=goodness(i); //cout << goodnessArray[i] << endl; } }
static cell_regression_stats *get_cell_regression_stats( grid<pixel_t> const &red, grid<pixel_t> const &nir, size_t size, bool force) { size_t output_block = 100; rect<size_t> sub = {0, 0, size, size}; size_t xCount = red.width() / size; size_t yCount = red.height() / size; IF_NORMAL(std::cout << yCount << " rows" << std::endl); size_t cell_count = xCount * yCount; array<numeric_t> red_avg(cell_count); array<numeric_t> nir_avg(cell_count); array<bool> goodness(cell_count); size_t good_count = 0; bool *goodness_ptr = goodness.data(); numeric_t *red_avg_ptr = red_avg.data(); numeric_t *nir_avg_ptr = nir_avg.data(); for (sub.y = 0; sub.y + size <= red.height(); sub.y += size) { for (sub.x = 0; sub.x + size <= red.width(); sub.x += size) { const grid<pixel_t> sub_red( const_cast<grid<pixel_t>*>(&red), sub); const grid<pixel_t> sub_nir( const_cast<grid<pixel_t>*>(&nir), sub); if (force || is_good_data(sub_red, sub_nir)) { *red_avg_ptr = stats_mean(sub_red); *nir_avg_ptr = stats_mean(sub_nir); *goodness_ptr = true; good_count++; } else { IF_VERBOSE(std::cout << "Bad sector: (" ); IF_VERBOSE(std::cout << sub.x << ", "); IF_VERBOSE(std::cout << sub.y << ") "); *goodness_ptr = false; } red_avg_ptr++; nir_avg_ptr++; goodness_ptr++; } if ((sub.y / size) % output_block == 0) { IF_NORMAL(std::cout << "\ranalyzing rows "); IF_NORMAL(std::cout << "starting at "); IF_NORMAL(std::cout << (sub.y / size)); IF_NORMAL(std::cout << "..."); IF_NORMAL(std::cout.flush()); } } IF_NORMAL(std::cout << std::endl); IF_VERBOSE(std::cout << "Found " << good_count << " good "); IF_VERBOSE(std::cout << "sectors out of " << cell_count); IF_VERBOSE(std::cout << std::endl); array<numeric_t> good_red_data(good_count); array<numeric_t> good_nir_data(good_count); size_t good_data_cur = 0; for (size_t i = 0; i < cell_count; i++) { if (goodness[i]) { good_red_data[good_data_cur] = red_avg[i]; good_nir_data[good_data_cur] = nir_avg[i]; good_data_cur++; } } linear_regression *reg = numeric_find_linear_regression( good_red_data, good_nir_data); cell_regression_stats *stats = new cell_regression_stats; stats->cell_size = size; stats->regression = *reg; delete reg; return stats; }
void nemo_main() { int i, dir, nrad, npots=0, ltype, ndim = NDIM, nx, ny, ns, ndat, nret; int cols[4], n, idx, idx_max; real pmax, symsize, rr, omk_max = 0.0, omk_rmax; real rad[MAXPT], *vel, *vel1, *vel2, *vel3, *vel4, *curve; real *ome, *kap, *opk, *omk, r0l[MAXPT+2], omega, *f; real inrad[MAXPT], invel[MAXPT], inrade[MAXPT], invele[MAXPT]; double pos[3], acc[3], pot, time = 0.0; /* char *fmt, s[20], pfmt[256]; */ char headline[256], fmt1[80]; string axis, mode, infile, plotlabel; stream instr; bool Qtab, Qplot, Qome, Qvel, Qlv, Qin, QoILR; mode = getparam("mode"); n = getiparam("n"); plotlabel = getparam("headline"); sprintf(fmt1,"%s ",getparam("format")); Qome = (*mode == 'o'); /* options are: velocity|omega|lv */ Qlv = (*mode == 'l'); Qvel = (*mode == 'v'); Qtab = getbparam("tab"); Qplot = getbparam("plot"); infile = getparam("in"); Qin = (*infile != 0); if (Qin) { nret = nemoinpi(getparam("cols"),cols,4); if (nret<0 || nret > 4) error("cols= requires 4 numbers"); for (i=nret; i<4; i++) cols[i] = 0; instr = stropen(infile,"r"); ndat = read_table(instr,MAXPT,inrad,invel,inrade,invele,cols); strclose(instr); } mypot1 = get_potential(getparam("name1"),getparam("pars1"),getparam("file1")); omega = get_pattern(); dprintf(0,"Pattern speed: %f\n",omega); mypot2 = get_potential(getparam("name2"),getparam("pars2"),getparam("file2")); mypot3 = get_potential(getparam("name3"),getparam("pars3"),getparam("file3")); mypot4 = get_potential(getparam("name4"),getparam("pars4"),getparam("file4")); headline[0] = '\0'; /* accumulate headline */ if (mypot1) { strcat(headline,getparam("name1")); strcat(headline,"("); strcat(headline,getparam("pars1")); strcat(headline,")"); npots++; } if (mypot2) { strcat(headline,getparam("name2")); strcat(headline,"("); strcat(headline,getparam("pars2")); strcat(headline,") "); npots++; } if (mypot3) { strcat(headline,getparam("name3")); strcat(headline,"("); strcat(headline,getparam("pars3")); strcat(headline,") "); npots++; } if (mypot4) { strcat(headline,getparam("name4")); strcat(headline,"("); strcat(headline,getparam("pars4")); strcat(headline,")"); npots++; } nrad = nemoinpr(getparam("radii"),rad,MAXPT); /* get radii */ if (nrad <= 0) warning("Using %d radii is not very productive",nrad); vel = (real *) allocate(sizeof(real) * nrad); /* allocate stuff */ vel1 = (real *) allocate(sizeof(real) * nrad); vel2 = (real *) allocate(sizeof(real) * nrad); vel3 = (real *) allocate(sizeof(real) * nrad); vel4 = (real *) allocate(sizeof(real) * nrad); if (Qome) { ome = (real *) allocate(4 * sizeof(real) * nrad); /* plus spline */ kap = (real *) allocate(sizeof(real) * nrad); opk = (real *) allocate(sizeof(real) * nrad); omk = (real *) allocate(sizeof(real) * nrad); } axis = getparam("axis"); dir = 0; if (*axis == 'x') dir=0; if (*axis == 'y') dir=1; if (*axis == 'z') dir=2; if (dir>NDIM) error("Axis %s not supported in NDIM=%d",axis,NDIM); pmax = 0.0; for (i=0; i<nrad; i++) { /* loop to compute */ CLRV(pos); /* clear positions */ pos[dir] = rad[i]; /* set the right axis */ vel[i] = 0.0; if (mypot1) { CLRV(acc); (*mypot1) (&ndim,pos,acc,&pot,&time); vel1[i] = -rad[i] * acc[dir]; vel[i] += vel1[i]; vel1[i] = sqrt(vel1[i]); } if (mypot2) { CLRV(acc); (*mypot2) (&ndim,pos,acc,&pot,&time); vel2[i] = -rad[i] * acc[dir]; vel[i] += vel2[i]; vel2[i] = sqrt(vel2[i]); } if (mypot3) { CLRV(acc); (*mypot3) (&ndim,pos,acc,&pot,&time); vel3[i] = -rad[i] * acc[dir]; vel[i] += vel3[i]; vel3[i] = sqrt(vel3[i]); } if (mypot4) { CLRV(acc); (*mypot4) (&ndim,pos,acc,&pot,&time); vel4[i] = -rad[i] * acc[dir]; vel[i] += vel4[i]; vel4[i] = sqrt(vel4[i]); } vel[i] = sqrt(vel[i]); } if (Qome) { lindblad(nrad,rad,vel,ome,kap,opk,omk,n); if (omega> 0.0) { /* compute resonances */ f = opk; idx = nrad-1; if (omega < f[idx]) { warning("Radii not far enough out for OLR: %g",f[idx]); f = ome; if (omega < f[idx]) { warning("Radii not far enough out for CR: %g",f[idx]); f = omk; } } QoILR = FALSE; for(; idx>0; idx--) { if (omk[idx] > omk_max) { idx_max = idx; omk_max = omk[idx]; } if (f==omk) { if (QoILR) { if (omega < f[idx]) continue; } else { if (omega > f[idx]) continue; } } else { if (omega > f[idx]) continue; } /* found a resonance: */ rr = rad[idx] + (rad[idx+1]-rad[idx])* (omega-f[idx])/(f[idx+1]-f[idx]); if (f == omk) { #if 0 if (QoILR) { dprintf(0,"iILR: %g\n",rr); break; } else { dprintf(0,"oILR: %g\n",rr); QoILR = TRUE; } #endif } else if (f == ome) { dprintf(0,"CR: %g\n",rr); f = omk; } else if (f == opk) { dprintf(0,"OLR: %g\n",rr); f = ome; } else error("impossble resonance"); } peak(nrad,rad,omk,idx_max,1, &omk_rmax, &omk_max); dprintf(0,"OMK_max: %g\n",omk_max); dprintf(0,"OMK_rmax: %g\n",omk_rmax); if (omega < omk_max) { /* search for ILR */ for (idx=idx_max; idx<nrad; idx++) { if (omega > omk[idx]) { rr = rad[idx-1] + (rad[idx]-rad[idx-1])* (omega-f[idx-1])/(f[idx]-f[idx-1]); dprintf(0,"oILR: %g\n",rr); break; } } for (idx=idx_max; idx>0; idx--) { if (omega > omk[idx]) { rr = rad[idx] + (rad[idx+1]-rad[idx])* (omega-f[idx])/(f[idx+1]-f[idx]); dprintf(0,"iILR: %g\n",rr); break; } } } } } for (i=0; i<nrad; i++) { /* loop to print */ if (Qtab) { printf(fmt1,rad[i]); printf(fmt1,vel[i]); } if (Qtab && npots>1 && !Qome) { if (mypot1) printf(fmt1,vel1[i]); if (mypot2) printf(fmt1,vel2[i]); if (mypot3) printf(fmt1,vel3[i]); if (mypot4) printf(fmt1,vel4[i]); } if (Qtab && Qome) { printf(fmt1,ome[i]); printf(fmt1,kap[i]); printf(fmt1,opk[i]); printf(fmt1,omk[i]); } if (Qtab) printf("\n"); if (Qome) pmax = MAX(pmax,opk[i]); else pmax = MAX(pmax,vel[i]); } if (Qin && Qvel) goodness(nrad,rad,vel,ndat,inrad,invel,(cols[3]>0?invele:NULL)); if (Qplot) { plinit("***",0.0,20.0,0.0,20.0); /* open device */ nx = nemoinpr(getparam("xrange"),xplot,2); /* get xrange in plot */ switch(nx) { case 0: xplot[0] = rad[0]; case 1: xplot[1] = rad[nrad-1]; break; case 2: break; default: warning("xrange= only accepts two values"); break; } ny = nemoinpr(getparam("yrange"),yplot,2); /* get yrange in plot */ switch(ny) { case 0: yplot[0] = 0.0; yplot[1] = 1.1 * pmax; /* extra 10% for egde */ break; case 1: yplot[1] = 1.1 * pmax; /* extra 10% for egde */ break; case 2: break; default: warning("yrange= only accepts two values"); break; } xaxis ( 2.0, 2.0, 16.0, xplot, -7, xtrans, "R"); /* plot axes */ xaxis ( 2.0,18.0, 16.0, xplot, -7, xtrans, NULL); if (Qome) yaxis ( 2.0, 2.0, 16.0, yplot, -7, ytrans, "[V/R]"); else yaxis ( 2.0, 2.0, 16.0, yplot, -7, ytrans, "V"); yaxis (18.0, 2.0, 16.0, yplot, -7, ytrans, NULL); if (*plotlabel) pltext(plotlabel,2.0,18.5,0.5,0.0); else pltext(headline,2.0,18.5,0.35,0.0); if (*plotmsg) pltext(plotmsg,8.0,2.5,0.25,0.0); curve = (Qome ? ome : vel); /* assign first curve */ plltype(3,1); /* thick solid line */ plmove(xtrans(rad[0]),ytrans(curve[0])); for (i=1; i<nrad; i++) plline(xtrans(rad[i]),ytrans(curve[i])); if (Qome) { /* if Lindblad - plot omk, opk */ plltype(1,1); /* all regular solid lines */ plmove(xtrans(rad[0]), ytrans(omk[0])); for (i=1; i<nrad; i++) plline(xtrans(rad[i]),ytrans(omk[i])); plmove(xtrans(rad[0]), ytrans(opk[0])); for (i=1; i<nrad; i++) plline(xtrans(rad[i]),ytrans(opk[i])); } else if (npots>1) { /* if velocity and > 1 component */ ltype = 1; if (mypot1) { plltype(1,++ltype); plmove(xtrans(rad[0]),ytrans(vel1[0])); for (i=1; i<nrad; i++) plline(xtrans(rad[i]),ytrans(vel1[i])); } if (mypot2) { plltype(1,++ltype); plmove(xtrans(rad[0]),ytrans(vel2[0])); for (i=1; i<nrad; i++) plline(xtrans(rad[i]),ytrans(vel2[i])); } if (mypot3) { plltype(1,++ltype); plmove(xtrans(rad[0]),ytrans(vel2[0])); for (i=1; i<nrad; i++) plline(xtrans(rad[i]),ytrans(vel3[i])); } if (mypot4) { plltype(1,++ltype); plmove(xtrans(rad[0]),ytrans(vel2[0])); for (i=1; i<nrad; i++) plline(xtrans(rad[i]),ytrans(vel4[i])); } } plltype(1,1); symsize = 0.1; if (Qin && Qvel) { /* if input file with velocities */ for (i=0; i<ndat; i++) plbox(xtrans(inrad[i]),ytrans(invel[i]),symsize); if (cols[3]>0) { /* if error bars in radius */ for (i=0; i<ndat; i++) { plmove(xtrans(inrad[i]-inrade[i]),ytrans(invel[i])); plline(xtrans(inrad[i]+inrade[i]),ytrans(invel[i])); } } if (cols[4]>0) { /* if error bars in velocity */ for (i=0; i<ndat; i++) { plmove(xtrans(inrad[i]),ytrans(invel[i]-invele[i])); plline(xtrans(inrad[i]),ytrans(invel[i]+invele[i])); } } } else if (Qin && Qome) { /* if input file with omega */ for (i=0; i<ndat; i++) plbox(xtrans(inrad[i]),ytrans(invel[i]/inrad[i]),symsize); } plstop(); } /* if plot vel/ome */ if (Qlv) { ns = nemoinpr(getparam("r0l"),r0l,MAXPT+2) - 2; if (ns < 0) error("r0l= needs at least two values: r0 and l"); else if (ns==0) warning("r0l= no lv-radii array supplied"); lv(nrad,rad,vel,r0l[0],r0l[1],ns,&r0l[2]); } }