IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 39236 for trunk


Ignore:
Timestamp:
Dec 7, 2015, 7:52:53 PM (11 years ago)
Author:
eugene
Message:

fix fitplx_irls to generate accurate plx fits

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/opihi/cmd.astro/fitplx_irls.c

    r39228 r39236  
    2525  }
    2626
    27   int Noutlier = 0;
    28   float dPsigMax = FLT_MAX;
    29   if ((N = get_argument (argc, argv, "-outlier-tests"))) {
    30     remove_argument (N, &argc, argv);
    31     Noutlier = atoi(argv[N]);
    32     remove_argument (N, &argc, argv);
    33     dPsigMax = atof(argv[N]);
    34     remove_argument (N, &argc, argv);
    35   }
    36 
    3727  int Nresample = 0;
    3828  if ((N = get_argument (argc, argv, "-bootstrap-resample"))) {
     
    4232  }
    4333
    44   Vector *dPvec = NULL;
    45   if ((N = get_argument (argc, argv, "-dPsig"))) {
    46     if (!Noutlier) { gprint (GP_ERR, "-dPsig requires -outlier-tests to be non-zero\n"); return FALSE; }
    47     remove_argument (N, &argc, argv);
    48     if (!(dPvec = SelectVector (argv[N], ANYVECTOR, TRUE))) return FALSE;
    49     remove_argument (N, &argc, argv);
    50   }
    51 
    5234  int max_iterations = 10;
    5335  if ((N = get_argument (argc, argv, "-max-iterations"))) {
     
    5739  }
    5840
    59   Vector *outMask = NULL;
    60   if ((N = get_argument (argc, argv, "-out-mask"))) {
    61     remove_argument (N, &argc, argv);
    62     if (!(outMask = SelectVector(argv[N], ANYVECTOR, TRUE))) return FALSE;
    63     remove_argument (N, &argc, argv);
    64   }
    65 
    6641  double outlier_limit = 0.1;
    6742  if ((N = get_argument (argc, argv, "-outlier-limit"))) {
     
    7449  if (argc != 6) {
    7550    gprint (GP_ERR, "USAGE: fitplx (ra) (dR) (dec) (dD) (mjd) [-mask mask] [-v] [-vv]\n");
    76     gprint (GP_ERR, "  -outlier-tests Nsamples dPsigMax : run Nsample bootstrap-resamples to define the path deviations and reject based on dPsigMax\n");
    77     gprint (GP_ERR, "  -dPsig vec : save path deviations in vec\n");
    7851    gprint (GP_ERR, "  -max-iterations : maximum number of IRLS iterations to run (default 10)\n");
    79     gprint (GP_ERR, "  -outMask vect : save outlier mask in vec\n");
    8052    gprint (GP_ERR, "  -outlier-limit : fraction of average weight to reject on (default 0.1)\n");
    8153    return (FALSE);
     
    10981  // Nsubset : unmasked points
    11082  int Ntotal = tvec->Nelements; // XXX check other lengths
    111   if (dPvec) ResetVector (dPvec, OPIHI_FLT, Ntotal);
    112   if (outMask) ResetVector (outMask, OPIHI_INT, Ntotal);
    11383 
    11484  double Rmean, Dmean, Tmean;
     
    12595  PlxFitDataAlloc (&fitdata, Ntotal);
    12696  PlxSetEpochPosition (&fitdata, R, D, dR, dD, T, mask, Ntotal, &coords, Tmean);
     97  // fitdata only contains the points which are not masked
    12798
    12899  PlxFit fit; memset (&fit, 0, sizeof(PlxFit));
    129100
    130   if (0) {
    131     // This should be accomplished via the IRLS call
    132     // determine dPsig for detections based on Noutlier attempts
    133     if (Noutlier) {
    134       int clipRetry = TRUE;
    135       for (i = 0; clipRetry && (i < 3); i++) {
    136         clipRetry = !PlxOutlierClip (&fitdata, mask, Noutlier, dPsigMax, dPvec, VERBOSE);
    137        
    138         // using the new mask values, reset fitdata
    139         PlxSetMeanEpoch (R, D, T, &Rmean, &Dmean, &Tmean, mask, Ntotal);
    140         PlxSetEpochPosition (&fitdata, R, D, dR, dD, T, mask, Ntotal, &coords, Tmean);
    141         if (VERBOSE) fprintf (stderr, "keep %d of %d\n", fitdata.Npts, Ntotal);
    142       }
    143     }
    144   }
    145  
    146101  for (i = 0; (VERBOSE == 2) && (i < fitdata.Npts); i++) {
    147102    int n = fitdata.index[i];
    148     int maskValue = mask ? mask[n] : 0;
     103    int maskValue = mask ? mask[n] : 1;
    149104    fprintf (stderr, "%f %f : %f %d : %f %f %f\n", R[n], D[n], T[n], maskValue, fitdata.t[i], fitdata.X[i], fitdata.Y[i]);
    150105  }
     
    159114    return FALSE;
    160115  }
    161 
    162   // Set the output mask based on the input mask and the outlier limits.
    163   if (outMask) {
    164     double Sum_Wx = 0;
    165     double Sum_Wy = 0;
    166     int *outMask_V = outMask->elements.Int;
    167     for (i = 0; i < fitdata.Npts; i++) {
    168       Sum_Wx += fitdata.Wx[i];
    169       Sum_Wy += fitdata.Wy[i];
    170     }
    171     for (i = 0; i < Ntotal; i++) {
    172       outMask_V[i] = mask ? mask[i] : 0;
    173     }
    174    
    175     for (i = 0; i < fitdata.Npts; i++) {
    176       if ((fitdata.Wx[i] > outlier_limit * Sum_Wx / (1.0 * fitdata.Npts))||
    177           (fitdata.Wy[i] > outlier_limit * Sum_Wy / (1.0 * fitdata.Npts))) {
    178         int n = fitdata.index[i];
    179         outMask_V[n] = 1;
    180 
    181         if (VERBOSE == 2) {
    182           fprintf (stderr, "%f %f : %f %d : %f %f %f : %f %f %f %f\n", R[n], D[n], T[n], outMask_V[n], fitdata.t[i], fitdata.X[i], fitdata.Y[i], fitdata.Wx[i], fitdata.Wy[i], Sum_Wx, Sum_Wy);
    183         }
    184       }
    185     }
    186   }
    187 
    188  
     116  // FitPMandPar_IRLS sets the values of Wx,Wy based on the fit distance
     117
     118  // update the mask based on the input mask and the outlier limits.
     119  double Sum_Wx = 0;
     120  double Sum_Wy = 0;
     121  mask = mvec->elements.Int;
     122  for (i = 0; i < fitdata.Npts; i++) {
     123    Sum_Wx += fitdata.Wx[i];
     124    Sum_Wy += fitdata.Wy[i];
     125  }
     126  for (i = 0; i < fitdata.Npts; i++) {
     127    // fitdata only includes the previously unmasked points
     128    if ((fitdata.Wx[i] < outlier_limit * Sum_Wx / (1.0 * fitdata.Npts))||
     129        (fitdata.Wy[i] < outlier_limit * Sum_Wy / (1.0 * fitdata.Npts))) {
     130      int n = fitdata.index[i];
     131      mask[n] = 0;
     132     
     133      if (VERBOSE == 2) {
     134          fprintf (stderr, "%f %f : %f %d : %f %f %f : %f %f %f %f\n", R[n], D[n], T[n], mask[n], fitdata.t[i], fitdata.X[i], fitdata.Y[i], fitdata.Wx[i], fitdata.Wy[i], Sum_Wx, Sum_Wy);
     135      }
     136    }
     137  }
     138
    189139  if (Nresample){
     140    // now that the mask has been updated, we need to recalculate mean epoch and positions
     141    // XXX make this conditional on actually masking unmasked points above
     142    PlxSetMeanEpoch (R, D, T, &Rmean, &Dmean, &Tmean, mask, Ntotal);
     143    PlxSetEpochPosition (&fitdata, R, D, dR, dD, T, mask, Ntotal, &coords, Tmean);
     144
    190145    PlxFitData sample;
    191146    PlxFitDataAlloc (&sample, fitdata.Npts);
     
    319274  }
    320275
     276  // XXX EAM : I suggest making these static as well like the A and B matrices
    321277  // Allocate the IRLS specific objects
    322278  ALLOCATE (Cov, double *, 5);
     
    327283  ALLOCATE(Beta, double, 5);
    328284  ALLOCATE(Beta_prev, double, 5);
    329   //  ALLOCATE(Wx, double, Npts);
    330   //  ALLOCATE(Wy, double, Npts);
    331285  ALLOCATE(rx,  double, Npts);
    332286  ALLOCATE(ry,  double, Npts);
     
    348302  sigma_ols = 0.0;
    349303  for (i = 0; i < Npts; i++) {
     304    // double Xf = B[0][0] + B[1][0]*T[i] + B[4][0]*pR[i];
     305    // double Yf = B[2][0] + B[3][0]*T[i] + B[4][0]*pD[i];
     306
    350307    rx[i] = X[i] - (T[i] * B[1][0] + B[0][0] + B[4][0] * pR[i]);
    351308    ry[i] = Y[i] - (T[i] * B[3][0] + B[2][0] + B[4][0] * pD[i]);
     309
     310
    352311    //    u[i] = r[i] /
    353312    sigma_ols += SQ(rx[i]) + SQ(ry[i]);
    354 
    355313  }
    356314  sigma_ols = sqrt(sigma_ols / (Npts - 5.0));
     
    390348
    391349    // Solve
    392     if (!weighted_LS_PLX(T,pD,pR,X,Wx,Y,Wy,Npts,A,B,VERBOSE)) {
     350    if (!weighted_LS_PLX(T,pR,pD,X,Wx,Y,Wy,Npts,A,B,VERBOSE)) {
    393351      // Handle fail case
    394352      return(FALSE);
     
    478436    }
    479437  }
    480  
    481  
    482438
    483439  // Finish
Note: See TracChangeset for help on using the changeset viewer.