Changeset 31631
- Timestamp:
- Jun 15, 2011, 4:53:30 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/czw_branch/20110406/psModules/src/imcombine/pmStack.c
r31629 r31631 37 37 38 38 39 # if ( 1)39 # if (0) 40 40 #define TESTING // Enable test output 41 41 /* #define TEST_X 5745 // x coordinate to examine */ … … 106 106 107 107 // KMM functions to do bimodality rejection of pixels 108 109 108 float gaussian(float x, float m, float s) { 110 109 return(pow(s * sqrt(2 * M_PI),-1) * exp(-0.5 * pow( (x - m) / s, 2))); … … 116 115 float *pi1, float *m1, float *s1, 117 116 float *pi2, float *m2, float *s2) { 117 assert(values); 118 assert(values->type.type == PS_TYPE_F32); 119 118 120 double logL_bimodal = 0, logL_unimodal; 119 121 psVector *P1 = psVectorAlloc(values->n,PS_TYPE_F32); … … 121 123 int i; 122 124 /* int debug = 0; */ 123 /* if (fabs(values->data.F32[0] - -145.624954) < 1e-4) { */124 /* debug = 1; */125 /* } */126 125 127 126 // Calculate unimodal properties … … 156 155 float w1,w2; 157 156 157 // These should be options. 158 158 float KMM_TOLERANCE = 1e-6; 159 159 int KMM_MAX_ITERATIONS = 500; … … 191 191 *s1 = 0; 192 192 *s2 = 0; 193 for (i = 0; i < values->n; i++) { // Calculate new means 193 for (i = 0; i < values->n; i++) { // Calculate new means and weights 194 194 *m1 += values->data.F32[i] * P1->data.F32[i]; 195 195 *m2 += values->data.F32[i] * P2->data.F32[i]; … … 226 226 // Calculate Punimodal 227 227 double lambda = -2.0 * (logL_unimodal - logL_bimodal); 228 int df = 2 + 2 * 1; 228 int df = 2 + 2 * 1; // I can't find my reference on this. 229 229 if (lambda > 0) { 230 230 *Punimodal = gsl_cdf_chisq_Q(lambda,df); 231 231 } 232 else { 232 else { // If lambda <= 0, then logL_unimodal > logL_bimodal, so Punimodal must be by definition 1.0 233 233 *Punimodal = 1.0; 234 234 } … … 238 238 239 239 static void KMMFindPopular(const psVector *values, float *Punimodal, float *mean, float *sigma, float *pi) { 240 float KMM_MINIMUM_PVALUE = 0.05; 240 float KMM_MINIMUM_PVALUE = 0.05; // Should be an option. 241 241 float mU,sU; 242 242 float pi1,m1,s1,pi2,m2,s2; 243 243 int iter; 244 244 245 assert(values); 246 assert(values->type.type == PS_TYPE_F32); 247 245 248 KMMcalculate(values,Punimodal,&iter, 246 249 &mU,&sU, … … 255 258 } 256 259 else { 260 // Is bimodal. Select most popular mode. 257 261 if (pi1 >= pi2) { 258 262 *mean = m1; … … 455 459 } 456 460 #if (0) 461 // Currently unused function to reject inputs for a given pixel(x,y) based on the selecting the 462 // most popular mode after running the KMM test, and rejecting all inputs that belong to the 463 // least popular mode. 457 464 static void KMMRejectUnpopular(const psArray *inputs, int x, int y) { 458 465 float KMM_MINIMUM_PVALUE = 0.05; … … 479 486 &pi1,&m1,&s1, 480 487 &pi2,&m2,&s2); 481 // fprintf(stderr,482 /* psTrace("psModules.imcombine",3, */483 488 484 489 CHECKPIX(x, y, … … 516 521 } 517 522 523 // Currently unused function to reject inputs for a given pixel(x,y) based on the selecting the 524 // faintest mode as determined by the KMM test, and rejecting all inputs that belong to the brighest. 518 525 static void KMMRejectBright(const psArray *inputs, int x, int y) { 519 526 float KMM_MINIMUM_PVALUE = 0.05; … … 558 565 // else do nothing. 559 566 } 560 #endif 567 #endif // End if(0) to prevent KMMReject{Unpopular|Bright} from being defined. 561 568 562 569 // Extract vectors for simple combination/rejection operations … … 701 708 if (PS_SQR(x - TEST_X) + PS_SQR(y - TEST_Y) <= PS_SQR(TEST_RADIUS)) { 702 709 for (int i = 0; i < numGood; i++) { 703 fprintf(stderr,"Input %d, pixel %d,%d (%" PRIu16 "): %f %f (%f) %f %f %d %x %x -> %x %x\n",710 fprintf(stderr,"Input %d, pixel %d,%d (%" PRIu16 "): %f %f (%f) %f %f %d %x %x -> %x %x\n", 704 711 i, x, y, pixelSources->data.U16[i], pixelData->data.F32[i], pixelVariances->data.F32[i], 705 712 addVariance->data.F32[i], pixelWeights->data.F32[i], pixelExps->data.F32[i], … … 843 850 844 851 // KMM values; 845 float Punimodal,KMMmean,KMMsigma,KMMpi; 852 float Punimodal = 1.0,KMMmean = NAN,KMMsigma = NAN,KMMpi = NAN; 853 int KMM_MINIMUM_INPUTS = 6; 854 bool useKMM = false; 855 if (num >= KMM_MINIMUM_INPUTS) { 856 useKMM = true; 857 } 846 858 847 859 // Set up rejection limits … … 853 865 854 866 // Determine the systematic error from the most popular population in the sample 855 KMMFindPopular(pixelData,&Punimodal,&KMMmean,&KMMsigma,&KMMpi); 856 CHECKPIX(x,y,"KMM Popularity Contest: (%d,%d) Puni: %g Mean: %f Sigma %f Pi: %f\n", 857 x,y,Punimodal,KMMmean,KMMsigma,KMMpi); 858 867 // This should probably be an option 868 if (useKMM) { 869 KMMFindPopular(pixelData,&Punimodal,&KMMmean,&KMMsigma,&KMMpi); 870 CHECKPIX(x,y,"KMM Popularity Contest: (%d,%d) Puni: %g Mean: %f Sigma %f Pi: %f\n", 871 x,y,Punimodal,KMMmean,KMMsigma,KMMpi); 872 } 859 873 for (int i = 0; i < num; i++) { 860 874 sumWeights += pixelWeights->data.F32[i]; 861 875 } 862 876 for (int i = 0; i < num; i++) { 863 // Systematic error contributes to the rejection level 864 /* float sysVar = PS_SQR(sys * pixelData->data.F32[i]); */ 865 float sysVar = PS_SQR(KMMsigma); 877 // Systematic error contributes to the rejection level 878 float sysVar; 879 if (useKMM) { // If we can trust KMM results, set the systematic variance 880 sysVar = PS_SQR(KMMsigma); 881 } 882 else { // Otherwise, use the 10% systematic variance we've done in the past. 883 sysVar = PS_SQR(sys * pixelData->data.F32[i]); 884 } 885 866 886 CHECKPIX(x, y, "Variance %d (%d), pixel %d,%d: %f %f %f\n", 867 887 i, pixelSources->data.U16[i], x, y, … … 1013 1033 if (useVariance) { 1014 1034 float median; 1015 if (Punimodal > 0.05) { 1035 if ((useKMM)&&(Punimodal < 0.05)) { 1036 median = KMMmean; 1037 } 1038 else { 1016 1039 median = combinationWeightedOlympic(pixelData, pixelWeights, 1017 1040 olympic, buffer->sort); // Median for stack 1018 1041 } 1019 else { 1020 median = KMMmean; 1021 } 1042 1022 1043 CHECKPIX(x, y, "Flag with variance pixel %d,%d: median = %f\n", x, y, median); 1023 1044 float worst = -INFINITY; // Largest deviation 1024 1045 for (int j = 0; j < num; j++) { 1025 1026 float diff = pixelData->data.F32[j] - median; // Difference from expected 1046 float diff = pixelData->data.F32[j] - median; // Difference from expected 1027 1047 CHECKPIX(x, y, "Testing input %d for pixel %d,%d: %f\n", j, x, y, diff); 1028 1048 … … 1273 1293 } 1274 1294 1275 1276 1277 1295 1278 1296 ////////////////////////////////////////////////////////////////////////////////////////////////////////////// … … 1445 1463 for (int y = minInputRows; y < maxInputRows; y++) { 1446 1464 for (int x = minInputCols; x < maxInputCols; x++) { 1447 1448 /* CHECKPIX(x, y, "Combining pixel %d,%d: %x %x %f %f %f %f %d %d %d\n", x, y, badMaskBits, blankMaskBits, iter, rej, sys, olympic, useVariance, safe, rejection); */ 1465 CHECKPIX(x, y, "Combining pixel %d,%d: %x %x %f %f %f %f %d %d %d\n", x, y, badMaskBits, blankMaskBits, iter, rej, sys, olympic, useVariance, safe, rejection); 1449 1466 1450 1467 /* // Pre-reject inputs using KMM bimodality test. */
Note:
See TracChangeset
for help on using the changeset viewer.
