IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 36651


Ignore:
Timestamp:
Apr 4, 2014, 4:19:48 PM (12 years ago)
Author:
mhuber
Message:

pix rej bugs fix and debugging

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psModules/src/imcombine/pmStack.c

    r35455 r36651  
    4343// #define TEST_X 972
    4444// #define TEST_Y 3213
    45 #define TEST_X 3289
    46 #define TEST_Y 4810
    47 #define TEST_RADIUS 0.5                 // Radius to examine
     45//#define TEST_X 3289
     46//#define TEST_Y 4810
     47//#define TEST_RADIUS 0.5                 // Radius to examine
     48//MEH -- streak-like junk md04s065i
     49#define TEST_X 1129
     50#define TEST_Y 4256
     51#define TEST_RADIUS 2.0                 // Radius to examine
    4852# endif
    4953
     
    108112
    109113// KMM functions to do bimodality rejection of pixels
    110 float gaussian(float x, float m, float s) {
     114double gaussian(float x, float m, float s) {
    111115  return(pow(s * sqrt(2 * M_PI),-1) * exp(-0.5 * pow( (x - m) / s, 2)));
    112116}
     
    116120                         float *mU, float *sU,
    117121                         float *pi1, float *m1, float *s1,
    118                          float *pi2, float *m2, float *s2) {
     122                         float *pi2, float *m2, float *s2,
     123                         int xyrdebug) {
    119124  assert(values);
    120125  assert(values->type.type == PS_TYPE_F32);
     
    151156  }
    152157
     158  if (xyrdebug == 1) {
     159      fprintf(stderr,"KMM uni: %d %f %d (%f %f)\n",
     160              xyrdebug,logL_unimodal,discrepant_index,
     161              *mU,*sU);
     162  }
     163
    153164  // Do EM loop
    154165  float dL = 0;
     
    186197  *pi2 = 0.5;
    187198
    188   float g1,g2,norm;
     199  //MEH -- need to be double to help avoid 0 in norm
     200  double g1,g2,norm;
    189201  float w1,w2;
    190202
     
    203215/*            *m2,*s2,*pi2); */
    204216/*     } */
     217
     218    if (xyrdebug == 1) {
     219        fprintf(stderr,"KMM EM iter: %d %f %f %f %f (%f %f %e) (%f %f %e)\n",
     220                *iter,logL_unimodal,logL_bimodal,oldL,dL,
     221                *m1,*s1,*pi1,
     222                *m2,*s2,*pi2);
     223    }
     224
    205225    // Expectation/P-stage
    206226    for (i = 0; i < values->n; i++) { // Calculate probabilities for each mode
     
    208228      g2 = gaussian(values->data.F32[i],*m2,*s2);
    209229      norm = (*pi1 * g1 + *pi2 * g2);
    210       P1->data.F32[i] = (*pi1 * g1) / norm;
    211       P2->data.F32[i] = (*pi2 * g2) / norm;
     230      //MEH -- must protect denom from norm=0
     231      if (norm > 0) {
     232          P1->data.F32[i] = (*pi1 * g1) / norm;
     233          P2->data.F32[i] = (*pi2 * g2) / norm;
     234      } else {
     235          P1->data.F32[i] = 0.0;
     236          P2->data.F32[i] = 0.0;
     237      } 
     238 
     239      if (xyrdebug == 1) {
     240          fprintf(stderr,"KMM EM-P loop: %d %d %le %le %le\n",
     241                         *iter,i,norm,g1,g2);
     242      }
     243
    212244    }
    213245    // Maximization/M-stage
     
    231263      w1 += P1->data.F32[i];
    232264      w2 += P2->data.F32[i];
     265
     266      if (xyrdebug == 1) {
     267          fprintf(stderr,"KMM EM-M loop: %d %d (%f %f %f %e) (%f %f %f %e)\n",
     268                         *iter,i,*m1,values->data.F32[i],w1,P1->data.F32[i],*m2,values->data.F32[i],w2,P2->data.F32[i]);
     269      }
     270
    233271    }
    234272    *m1 /= w1;
     
    250288      *pi2 = 0.0;
    251289    }
    252     if (*s1 == 0) { // sigma may not be zero
    253       *s1 = KMM_SMALL_NUMBER * *m1;
    254     }
    255     if (*s2 == 0) { // sigma may not be zero
    256       *s2 = KMM_SMALL_NUMBER * *m2;
    257     }
     290    if (*s1 == 0) { // sigma may not be zero -- MEH -- nor <0 and need additive offset if m~0
     291      *s1 = fabsf(KMM_SMALL_NUMBER * *m1) + KMM_SMALL_NUMBER;
     292    }
     293    if (*s2 == 0) { // sigma may not be zero
     294      *s2 = fabsf(KMM_SMALL_NUMBER * *m2) + KMM_SMALL_NUMBER;
     295    }
     296
     297    if (xyrdebug == 1) {
     298        fprintf(stderr,"KMM EM end: %d %f %f %f %f (%f %e %e %f) (%f %e %e %f)\n",
     299                *iter,logL_unimodal,logL_bimodal,oldL,dL,
     300                *m1,*s1,*pi1,w1,
     301                *m2,*s2,*pi2,w2);
     302    }
     303
    258304  } // End EM phase
    259305
     
    267313    *Punimodal = 1.0;
    268314  }
     315
     316  if (xyrdebug == 1) {
     317      fprintf(stderr,"KMM calc Puni: %d %f %d %f\n",
     318              xyrdebug,lambda,df,*Punimodal);
     319  }
     320
    269321  psFree(P1);
    270322  psFree(P2);
    271323}
    272324
    273 static void KMMFindPopular(const psVector *values, float *Punimodal, float *mean, float *sigma, float *pi) {
     325static void KMMFindPopular(const psVector *values, float *Punimodal, float *mean, float *sigma, float *pi, int xyrdebug) {
    274326  float KMM_MINIMUM_PVALUE = 0.05; // Should be an option.
    275327  float mU,sU;
     
    283335               &mU,&sU,
    284336               &pi1,&m1,&s1,
    285                &pi2,&m2,&s2);
     337               &pi2,&m2,&s2,xyrdebug);
    286338/*   fprintf(stdout,"%g %g : %g %g %g : %g %g %g : %g %d\t", */
    287339/*        mU,sU, */
     
    898950    psVector *pixelSuspects = buffer->suspects; // Is the pixel suspect?
    899951    psVector *pixelLimits = buffer->limits; // Is the pixel suspect?
     952    //MEH -- adding a debug option for TESTING xyr position but could be better..
     953    int xyrdebug = 0;
    900954
    901955    // KMM values;
     
    917971        // This should probably be an option
    918972        if (useKMM) {
    919           KMMFindPopular(pixelData,&Punimodal,&KMMmean,&KMMsigma,&KMMpi);
     973#ifdef TESTING
     974            if (PS_SQR(x - TEST_X) + PS_SQR(y - TEST_Y) <= PS_SQR(TEST_RADIUS)) {
     975                xyrdebug = 1;
     976            }
     977# endif
     978          KMMFindPopular(pixelData,&Punimodal,&KMMmean,&KMMsigma,&KMMpi,xyrdebug);
    920979          CHECKPIX(x,y,"KMM Popularity Contest: (%d,%d) Puni: %g Mean: %f Sigma %f Pi: %f\n",
    921980                   x,y,Punimodal,KMMmean,KMMsigma,KMMpi);
Note: See TracChangeset for help on using the changeset viewer.