IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 36025


Ignore:
Timestamp:
Aug 25, 2013, 12:11:59 PM (13 years ago)
Author:
eugene
Message:

grid search on index

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20130711/psphot/src/psphotSourceFits.c

    r35972 r36025  
    558558}
    559559
    560 # define TIMING 0
     560# define TIMING 1
     561
     562bool psphotSersicModelGuessPCM (pmPCMdata *pcm, pmSource *source, psImageMaskType maskVal, float psfSize);
    561563
    562564pmModel *psphotFitPCM (pmReadout *readout, pmSource *source, pmSourceFitOptions *fitOptions, pmModelType modelType, psImageMaskType maskVal, psImageMaskType markVal, int psfSize) {
     
    580582
    581583    float t1, t2, t4, t5;
     584    t1 = t2 = t4 = t5 = 0.0;
    582585    if (TIMING) { psTimerStart ("psphotFitPCM"); }
     586
     587    if (modelType == pmModelClassGetType("PS_MODEL_SERSIC")) {
     588        options.mode = PM_SOURCE_FIT_NO_INDEX;
     589    } else {
     590        options.mode = PM_SOURCE_FIT_EXT_AND_SKY; // XXX note that there may be a conflict with psphotExtendedSourceFits.c:133
     591    }
    583592
    584593    pmPCMdata *pcm = pmPCMinit (source, &options, model, maskVal, psfSize);
     
    594603    if (modelType == pmModelClassGetType("PS_MODEL_SERSIC")) {
    595604        // use the source moments, etc to guess basic model parameters
    596         if (!psphotSersicModelClassGuessPCM (pcm, source)) {
     605        if (!psphotSersicModelGuessPCM (pcm, source, maskVal, psfSize)) {
    597606            psFree (pcm);
    598607            model->flags |= PM_MODEL_STATUS_BADARGS;
     
    613622    if (TIMING) { t2 = psTimerMark ("psphotFitPCM"); }
    614623
    615     if (modelType == pmModelClassGetType("PS_MODEL_SERSIC")) {
    616         options.mode = PM_SOURCE_FIT_NO_INDEX;
    617     } else {
    618       options.mode = PM_SOURCE_FIT_EXT_AND_SKY; // XXX note that there may be a conflict with psphotExtendedSourceFits.c:133
    619     }
    620624    // update the pcm elements if we have changed the circumstance (options.mode or source->pixels)
    621     pmPCMupdate(pcm, source, &options, model);
    622     if (TIMING) { t4 = psTimerMark ("psphotFitPCM"); }
     625    // pmPCMupdate(pcm, source, &options, model);
     626    // if (TIMING) { t4 = psTimerMark ("psphotFitPCM"); }
    623627
    624628    // psTraceSetLevel("psLib.math.psMinimizeLMChi2", 5);
     
    751755    return true;
    752756}
     757
     758// float indexGuessInv[] = {0.5, 0.33, 0.25, 0.167, 0.125, 0.083};
     759float indexGuessInv[] = {0.5, 0.25, 0.167, 0.125};
     760# define N_INDEX_GUESS_INV 4
     761
     762// A sersic model is very sensitive to the index.  attempt to find the index first by grid search in just the index
     763// for a sersic model, attempt to fit just the index and normalization with a modest number of iterations
     764bool psphotSersicModelGuessPCM (pmPCMdata *pcm, pmSource *source, psImageMaskType maskVal, float psfSize) {
     765
     766    // we get a reasonable guess from:
     767    // * Reff = Kron R1
     768    // * Rmajor / Rminor & Theta from moments
     769    // * Io from total Kron flux
     770
     771    // the guesses are used to fill in PAR:
     772    psF32 *PAR = pcm->modelConv->params->data.F32;
     773
     774    // convert the moments to Major,Minor,Theta
     775    psEllipseMoments moments;
     776
     777    if (!isfinite(source->moments->Mrf)) return false;
     778    if (!isfinite(source->moments->Mxx)) return false;
     779    if (!isfinite(source->moments->Mxy)) return false;
     780    if (!isfinite(source->moments->Myy)) return false;
     781
     782    moments.x2 = source->moments->Mxx;
     783    moments.y2 = source->moments->Myy;
     784    moments.xy = source->moments->Mxy;
     785   
     786    // limit axis ratio < 20.0
     787    psEllipseAxes momentAxes = psEllipseMomentsToAxes (moments, 20.0);
     788
     789    psEllipseAxes guessAxes;
     790    guessAxes.major = source->moments->Mrf;
     791    guessAxes.minor = (momentAxes.minor / momentAxes.minor) * guessAxes.major;
     792    guessAxes.theta = momentAxes.theta;
     793
     794    if (!isfinite(guessAxes.major)) return false;
     795    if (!isfinite(guessAxes.minor)) return false;
     796    if (!isfinite(guessAxes.theta)) return false;
     797
     798    // convert the major,minor,theta to shape parameters for an Reff-like model
     799    pmModelAxesToParams (&PAR[PM_PAR_SXX], &PAR[PM_PAR_SXY], &PAR[PM_PAR_SYY], guessAxes, true);
     800
     801    // set the model position
     802    if (!pmModelSetPosition(&PAR[PM_PAR_XPOS], &PAR[PM_PAR_YPOS], source)) {
     803      return false;
     804    }
     805
     806    // sky is zero (no longer fitted, but not yet deprecated)
     807    PAR[PM_PAR_SKY]  = 0.0;
     808
     809    // for the index loop, use Io = 1.0, use fitted values to determine Io
     810    PAR[PM_PAR_I0] = 1.0;
     811
     812    float xMin = NAN;
     813    float iMin = NAN;
     814    float sMin = NAN;
     815
     816    // loop over index and Reff, keeping the ARatio and Theta constant?
     817    // loop over index guesses and find the best fit
     818    for (int i = 0; i < N_INDEX_GUESS_INV; i++) {
     819        PAR[PM_PAR_7] = indexGuessInv[i];
     820
     821        // generated the modelFlux
     822        pmPCMMakeModel (source, pcm->modelConv, maskVal, psfSize);
     823       
     824        float YY = 0.0;
     825        float YM = 0.0;
     826        float MM = 0.0;
     827        bool usePoisson = false;
     828
     829        for (int iy = 0; iy < source->pixels->numRows; iy++) {
     830            for (int ix = 0; ix < source->pixels->numCols; ix++) {
     831                // skip masked points
     832                if (source->maskObj->data.PS_TYPE_IMAGE_MASK_DATA[iy][ix]) {
     833                    continue;
     834                }
     835                // skip zero-variance points
     836                if (source->variance->data.F32[iy][ix] == 0) {
     837                    continue;
     838                }
     839                // skip nan value points
     840                if (!isfinite(source->pixels->data.F32[iy][ix])) {
     841                    continue;
     842                }
     843
     844                float fy = source->pixels->data.F32[iy][ix];
     845                float fm = source->modelFlux->data.F32[iy][ix];
     846                float wt = (usePoisson) ? 1.0 / source->variance->data.F32[iy][ix] : 1.0;
     847
     848                YY += PS_SQR(fy) * wt;
     849                YM += fm * fy * wt;
     850                MM += PS_SQR(fm) * wt;
     851            }
     852        }
     853
     854        float Io = YM / MM;
     855        float Chisq = YY - 2 * Io * YM + Io * Io * MM;
     856        if ((i == 0) || (Chisq < xMin)) {
     857            xMin = Chisq;
     858            iMin = Io;
     859            sMin = indexGuessInv[i];
     860        }
     861        fprintf (stderr, "%d | %f %f %f | %f %f %f", i, indexGuessInv[i], Io, Chisq, sMin, iMin, xMin);
     862        fprintf (stderr, "\n");
     863    }
     864
     865    PAR[PM_PAR_I0] = iMin;
     866    PAR[PM_PAR_7] = sMin;
     867
     868    return true;
     869}
Note: See TracChangeset for help on using the changeset viewer.