IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 31631


Ignore:
Timestamp:
Jun 15, 2011, 4:53:30 PM (15 years ago)
Author:
watersc1
Message:

disabled debug mode, and added test case for the number of inputs so that we can be sure that we're only running KMM when we have enough inputs to believe the results (N > 6). Ready for merging back into the trunk.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/czw_branch/20110406/psModules/src/imcombine/pmStack.c

    r31629 r31631  
    3737
    3838
    39 # if (1)
     39# if (0)
    4040#define TESTING                         // Enable test output
    4141/* #define TEST_X 5745                       // x coordinate to examine */
     
    106106
    107107// KMM functions to do bimodality rejection of pixels
    108 
    109108float gaussian(float x, float m, float s) {
    110109  return(pow(s * sqrt(2 * M_PI),-1) * exp(-0.5 * pow( (x - m) / s, 2)));
     
    116115                         float *pi1, float *m1, float *s1,
    117116                         float *pi2, float *m2, float *s2) {
     117  assert(values);
     118  assert(values->type.type == PS_TYPE_F32);
     119 
    118120  double logL_bimodal = 0, logL_unimodal;
    119121  psVector *P1 = psVectorAlloc(values->n,PS_TYPE_F32);
     
    121123  int i;
    122124/*   int debug = 0; */
    123 /*   if (fabs(values->data.F32[0] - -145.624954) < 1e-4) { */
    124 /*     debug = 1; */
    125 /*   } */
    126125 
    127126  // Calculate unimodal properties
     
    156155  float w1,w2;
    157156
     157  // These should be options.
    158158  float KMM_TOLERANCE = 1e-6;
    159159  int KMM_MAX_ITERATIONS = 500;
     
    191191    *s1 = 0;
    192192    *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
    194194      *m1 += values->data.F32[i] * P1->data.F32[i];
    195195      *m2 += values->data.F32[i] * P2->data.F32[i];
     
    226226  // Calculate Punimodal
    227227  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.
    229229  if (lambda > 0) {
    230230    *Punimodal = gsl_cdf_chisq_Q(lambda,df);
    231231  }
    232   else {
     232  else { // If lambda <= 0, then logL_unimodal > logL_bimodal, so Punimodal must be by definition 1.0
    233233    *Punimodal = 1.0;
    234234  }
     
    238238
    239239static 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.
    241241  float mU,sU;
    242242  float pi1,m1,s1,pi2,m2,s2;
    243243  int iter;
    244244
     245  assert(values);
     246  assert(values->type.type == PS_TYPE_F32);
     247 
    245248  KMMcalculate(values,Punimodal,&iter,
    246249               &mU,&sU,
     
    255258  }
    256259  else {
     260    // Is bimodal. Select most popular mode.
    257261    if (pi1 >= pi2) {
    258262      *mean = m1;
     
    455459}
    456460#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.
    457464static void KMMRejectUnpopular(const psArray *inputs, int x, int y) {
    458465  float KMM_MINIMUM_PVALUE = 0.05;
     
    479486               &pi1,&m1,&s1,
    480487               &pi2,&m2,&s2);
    481   //  fprintf(stderr,
    482 /*   psTrace("psModules.imcombine",3, */
    483488
    484489   CHECKPIX(x, y,
     
    516521}
    517522
     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.
    518525static void KMMRejectBright(const psArray *inputs, int x, int y) {
    519526  float KMM_MINIMUM_PVALUE = 0.05;
     
    558565  // else do nothing.
    559566}
    560 #endif
     567#endif // End if(0) to prevent KMMReject{Unpopular|Bright} from being defined.
    561568
    562569// Extract vectors for simple combination/rejection operations
     
    701708    if (PS_SQR(x - TEST_X) + PS_SQR(y - TEST_Y) <= PS_SQR(TEST_RADIUS)) {
    702709        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",
    704711                    i, x, y, pixelSources->data.U16[i], pixelData->data.F32[i], pixelVariances->data.F32[i],
    705712                    addVariance->data.F32[i], pixelWeights->data.F32[i], pixelExps->data.F32[i],
     
    843850
    844851    // 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    }
    846858   
    847859    // Set up rejection limits
     
    853865
    854866        // 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        }
    859873        for (int i = 0; i < num; i++) {
    860874            sumWeights += pixelWeights->data.F32[i];
    861875        }
    862876        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
    866886            CHECKPIX(x, y, "Variance %d (%d), pixel %d,%d: %f %f %f\n",
    867887                     i, pixelSources->data.U16[i], x, y,
     
    10131033              if (useVariance) {
    10141034                float median;
    1015                 if (Punimodal > 0.05) {
     1035                if ((useKMM)&&(Punimodal < 0.05)) {
     1036                  median = KMMmean;
     1037                }
     1038                else {
    10161039                  median = combinationWeightedOlympic(pixelData, pixelWeights,
    10171040                                                      olympic, buffer->sort); // Median for stack
    10181041                }
    1019                 else {
    1020                   median = KMMmean;
    1021                 }
     1042               
    10221043                  CHECKPIX(x, y, "Flag with variance pixel %d,%d: median = %f\n", x, y, median);
    10231044                  float worst = -INFINITY; // Largest deviation
    10241045                  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
    10271047                      CHECKPIX(x, y, "Testing input %d for pixel %d,%d: %f\n", j, x, y, diff);
    10281048
     
    12731293}
    12741294
    1275  
    1276  
    12771295
    12781296//////////////////////////////////////////////////////////////////////////////////////////////////////////////
     
    14451463    for (int y = minInputRows; y < maxInputRows; y++) {
    14461464        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);
    14491466
    14501467/*          // Pre-reject inputs using KMM bimodality test. */
Note: See TracChangeset for help on using the changeset viewer.