IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Aug 3, 2011, 10:30:02 AM (15 years ago)
Author:
eugene
Message:

add new window function using first radial moment; use new sersic guess function

File:
1 edited

Legend:

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

    r31452 r31990  
    211211bool psphotFitBlob (pmReadout *readout, pmSource *source, psArray *newSources, pmPSF *psf, pmSourceFitOptions *fitOptions, psImageMaskType maskVal, psImageMaskType markVal) {
    212212
    213     float radius;
     213    float fitRadius, windowRadius;
    214214    bool okEXT, okDBL;
    215215    pmModel *ONE = NULL;
     
    225225    if (source->type == PM_SOURCE_TYPE_SATURATED) return false;
    226226
     227# define TEST_X -540.0
     228# define TEST_Y 540.0
     229   
     230    if ((fabs(source->peak->xf - TEST_X) < 5) && (fabs(source->peak->yf - TEST_Y) < 5)) {
     231        fprintf (stderr, "test galaxy\n");
     232    }
     233
     234# undef TEST_X
     235# undef TEST_Y
     236
    227237    // set the radius based on the footprint (also sets the mask pixels)
    228     if (!psphotSetRadiusFootprint(&radius, readout, source, markVal, 1.0)) return false;
     238    if (!psphotSetRadiusMoments(&fitRadius, &windowRadius, readout, source, markVal)) return false;
     239    // fprintf (stderr, "rad: %6.1f %6.1f  | %5.2f %5.2f %5.2f  ", source->peak->xf, source->peak->yf, source->moments->Mrf, fitRadius, windowRadius);
    229240
    230241    // XXX note that this changes the source moments that are published...
     
    234245    // this uses the footprint to judge both radius and aperture?
    235246    // XXX save the psf-based moments for output
     247    // XXX do not limit to pixels in the marked circle
    236248    psfMoments = *source->moments;
    237     if (!pmSourceMoments (source, radius, 0.0, 0.5, 0.0, maskVal)) {
     249    if (!pmSourceMoments (source, windowRadius, 0.0, 0.5, 0.0, maskVal & PS_NOT_IMAGE_MASK(markVal))) {
    238250      *source->moments = psfMoments;
     251      fprintf (stderr, "skip (bad moments)\n");
    239252      return false;
    240253    }
     254    // fprintf (stderr, "r1: %f\n", source->moments->Mrf);
     255
     256    // XXX note that we do not re-try to measure the moments if the resulting Mrf is large compared
     257    // to the initial value
    241258
    242259    psTrace ("psphot", 5, "trying blob...\n");
     
    263280        ONE = DBL->data[0];
    264281        if (ONE) {
    265             if (!isfinite(ONE->params->data.F32[PM_PAR_I0])) psAbort("nan in fit");
     282            psAssert (isfinite(ONE->params->data.F32[PM_PAR_I0]), "nan in fit");
    266283            chiDBL = ONE->chisqNorm; // save chisq for double-star/galaxy comparison
    267             ONE->fitRadius = radius;
     284            ONE->fitRadius = fitRadius;
    268285        }
    269286
     
    271288        ONE = DBL->data[1];
    272289        if (ONE) {
    273             if (!isfinite(ONE->params->data.F32[PM_PAR_I0])) psAbort("nan in fit");
    274             ONE->fitRadius = radius;
     290            psAssert (isfinite(ONE->params->data.F32[PM_PAR_I0]), "nan in fit");
     291            ONE->fitRadius = fitRadius;
    275292        }
    276293    }
     
    284301        okEXT = psphotEvalEXT (tmpSrc, EXT);
    285302        chiEXT = EXT ? EXT->chisqNorm : NAN;
     303        EXT->fitRadius = fitRadius;
    286304    }
    287305
     
    476494    // for sersic models, use a grid search to choose an index, then float the params there
    477495    if (modelType == pmModelClassGetType("PS_MODEL_SERSIC")) {
    478         // for the test fits, use a somewhat smaller radius
    479         psphotSetRadiusFootprint(&model->fitRadius, readout, source, markVal, 0.5);
    480496        psphotFitSersicIndex (model, readout, source, fitOptions, maskVal, markVal);
    481     }
    482 
    483     if (!psphotSetRadiusModel (model, readout, source, markVal, true)) {
    484         psphotSetRadiusFootprint(&model->fitRadius, readout, source, markVal, 1.0);
    485497    }
    486498
     
    499511}
    500512
     513# define TIMING 0
     514
    501515pmModel *psphotFitPCM (pmReadout *readout, pmSource *source, pmSourceFitOptions *fitOptions, pmModelType modelType, psImageMaskType maskVal, psImageMaskType markVal, int psfSize) {
    502516
     
    517531        return NULL;
    518532    }
     533
     534# define TEST_X -540.0
     535# define TEST_Y 540.0
     536   
     537    if ((fabs(source->peak->xf - TEST_X) < 5) && (fabs(source->peak->yf - TEST_Y) < 5)) {
     538        psTraceSetLevel("psModules.objects.pmPCM_MinimizeChisq", 5);
     539    }
     540
     541    float t1, t2, t4, t5;
     542    if (TIMING) { psTimerStart ("psphotFitPCM"); }
    519543
    520544    pmPCMdata *pcm = pmPCMinit (source, &options, model, maskVal, psfSize);
     
    524548        return model;
    525549    }
    526 
    527     // use the source moments, etc to guess basic model parameters
    528     if (!pmSourceModelGuessPCM (pcm, source, maskVal, markVal)) {
    529         psFree (pcm);
    530         model->flags |= PM_MODEL_STATUS_BADARGS;
    531         return model;
    532     }
    533 
    534     // for sersic models, use a grid search to choose an index, then float the params there
     550    if (TIMING) { t1 = psTimerMark ("psphotFitPCM"); }
     551
     552    // get the guess for sersic models
    535553    if (modelType == pmModelClassGetType("PS_MODEL_SERSIC")) {
    536         // for the test fits, use a somewhat smaller radius
    537         psphotSetRadiusFootprint(&model->fitRadius, readout, source, markVal, 0.5);
    538 
    539         if (!psphotFitSersicIndexPCM (pcm, readout, source, fitOptions, maskVal, markVal, psfSize)) {
     554        // use the source moments, etc to guess basic model parameters
     555        if (!psphotSersicModelClassGuessPCM (pcm, source)) {
    540556            psFree (pcm);
    541557            model->flags |= PM_MODEL_STATUS_BADARGS;
    542558            return model;
    543559        }
    544     }
    545 
    546     if (!psphotSetRadiusModel (model, readout, source, markVal, true)) {
    547         psphotSetRadiusFootprint(&model->fitRadius, readout, source, markVal, 1.0);
    548     }
     560    } else {
     561        // use the source moments, etc to guess basic model parameters
     562        if (!pmSourceModelGuessPCM (pcm, source, maskVal, markVal)) {
     563            psFree (pcm);
     564            model->flags |= PM_MODEL_STATUS_BADARGS;
     565            return model;
     566        }
     567    }
     568
     569    if (TIMING) { t2 = psTimerMark ("psphotFitPCM"); }
    549570
    550571    if (modelType == pmModelClassGetType("PS_MODEL_SERSIC")) {
     
    555576    // update the pcm elements if we have changed the circumstance (options.mode or source->pixels)
    556577    pmPCMupdate(pcm, source, &options, model);
     578    if (TIMING) { t4 = psTimerMark ("psphotFitPCM"); }
    557579
    558580    // psTraceSetLevel("psLib.math.psMinimizeLMChi2", 5);
    559581    pmSourceFitPCM (pcm, source, &options, maskVal, markVal, psfSize);
    560     // fprintf (stderr, "chisq: %f, nIter: %d, radius: %f, npix: %d\n", model->chisqNorm, model->nIter, model->fitRadius, model->nPix);
     582    if (TIMING) { t5 = psTimerMark ("psphotFitPCM"); }
     583
     584    if (TIMING) {
     585        fprintf (stderr, "psphotFitPCM : nIter: %2d, radius: %6.1f, npix: %5d, t1: %6.4f, t2: %6.4f, t4: %6.4f, t5: %6.4f\n", model->nIter, model->fitRadius, model->nPix, t1, t2, t4, t5);
     586    }
     587
     588    if ((fabs(source->peak->xf - TEST_X) < 5) && (fabs(source->peak->yf - TEST_Y) < 5)) {
     589        psTraceSetLevel("psModules.objects.pmPCM_MinimizeChisq", 0);
     590    }
    561591
    562592    // psTraceSetLevel("psLib.math.psMinimizeLMChi2", 0);
     
    566596}
    567597
     598# undef TEST_X
     599# undef TEST_Y
     600
    568601// note that these should be 1/2n of the standard sersic index
    569 float indexGuess[] = {0.5, 0.33, 0.25, 0.167, 0.125, 0.083};
    570 # define N_INDEX_GUESS 6
     602// float indexGuess[] = {0.8, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0};
     603// float indexGuess[] = {0.5, 0.33, 0.25, 0.167, 0.125, 0.083};
     604float indexGuess[] = {1.0, 2.0, 3.0, 4.0};
     605# define N_INDEX_GUESS 4
    571606
    572607// A sersic model is very sensitive to the index.  attempt to find the index first by grid search in just the index
     
    586621    float chiSquare[N_INDEX_GUESS];
    587622
     623# define TEST_X -540.0
     624# define TEST_Y 540.0
     625   
     626    if ((fabs(source->peak->xf - TEST_X) < 5) && (fabs(source->peak->yf - TEST_Y) < 5)) {
     627        psTraceSetLevel("psModules.objects.pmPCM_MinimizeChisq", 5);
     628    }
     629
    588630    for (int i = 0; i < N_INDEX_GUESS; i++) {
    589         model->params->data.F32[PM_PAR_7] = indexGuess[i];
     631        model->params->data.F32[PM_PAR_7] = 0.5/indexGuess[i];
    590632
    591633        if (!model->modelGuess(model, source)) {
     
    594636        }
    595637
    596         // each time we change the model guess, we need to adjust the radius
    597         // XXX this did not work : we do not need such a large radius -- just uses moments-based radius
    598         if (false && !psphotSetRadiusModel (model, readout, source, markVal, false)) {
    599             psphotSetRadiusFootprint(&model->fitRadius, readout, source, markVal, 0.5);
    600         }
    601        
    602638        pmSourceFitModel (source, model, &options, maskVal);
    603         // fprintf (stderr, "chisq: %f, nIter: %d, radius: %f, npix: %d\n", model->chisqNorm, model->nIter, model->fitRadius, model->nPix);
     639        fprintf (stderr, "index: %f, chisq: %f, nIter: %d, radius: %f, npix: %d\n", indexGuess[i], model->chisqNorm, model->nIter, model->fitRadius, model->nPix);
    604640
    605641        chiSquare[i] = model->chisqNorm;
     
    616652    assert (iMin >= 0);
    617653
     654    if ((fabs(source->peak->xf - TEST_X) < 5) && (fabs(source->peak->yf - TEST_Y) < 5)) {
     655        psTraceSetLevel("psModules.objects.pmPCM_MinimizeChisq", 0);
     656    }
     657
    618658    model->flags = PM_MODEL_STATUS_NONE; // do not attempt to handle failures here, let the next iteration deal with it
    619     model->params->data.F32[PM_PAR_7] = indexGuess[iMin];
     659    model->params->data.F32[PM_PAR_7] = 0.5/indexGuess[iMin];
    620660    model->modelGuess(model, source);
    621 
    622     // each time we change the model guess, we need to adjust the radius
    623     // if (!psphotSetRadiusModel (model, readout, source, markVal, true)) {
    624     //  psphotSetRadiusFootprint(&model->fitRadius, readout, source, markVal);
    625     // }
    626661
    627662    return true;
     
    648683    float xMin = NAN;
    649684    float chiSquare[N_INDEX_GUESS];
     685
     686    if ((fabs(source->peak->xf - TEST_X) < 5) && (fabs(source->peak->yf - TEST_Y) < 5)) {
     687        psTraceSetLevel("psModules.objects.pmPCM_MinimizeChisq", 5);
     688    }
    650689
    651690    for (int i = 0; i < N_INDEX_GUESS; i++) {
     
    657696        }
    658697
    659         // each time we change the model guess, we need to adjust the radius
    660         // XXX this did not work : we do not need such a large radius -- just uses moments-based radius
    661         if (false && !psphotSetRadiusModel (model, readout, source, markVal, false)) {
    662             psphotSetRadiusFootprint(&model->fitRadius, readout, source, markVal, 0.5);
    663         }
    664        
    665698# if (0)
     699        // this block is to test the relative speed of straight and PCM fits
    666700        pmSourceFitModel (source, model, &options, maskVal);
    667701# else
     
    669703        pmSourceFitPCM (pcm, source, &options, maskVal, markVal, psfSize);
    670704# endif
     705        fprintf (stderr, "index: %f, chisq: %f, nIter: %d, radius: %f, npix: %d\n", indexGuess[i], model->chisqNorm, model->nIter, model->fitRadius, model->nPix);
    671706        // fprintf (stderr, "chisq: %f, nIter: %d, radius: %f, npix: %d\n", model->chisqNorm, model->nIter, model->fitRadius, model->nPix);
    672707
     
    684719    assert (iMin >= 0);
    685720   
     721    if ((fabs(source->peak->xf - TEST_X) < 5) && (fabs(source->peak->yf - TEST_Y) < 5)) {
     722        psTraceSetLevel("psModules.objects.pmPCM_MinimizeChisq", 0);
     723    }
     724
    686725    model->flags = PM_MODEL_STATUS_NONE; // do not attempt to handle failures here, let the next iteration deal with it
    687726    model->params->data.F32[PM_PAR_7] = indexGuess[iMin];
Note: See TracChangeset for help on using the changeset viewer.