IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 1, 2014, 4:36:26 PM (12 years ago)
Author:
bills
Message:

Modify psphotFullForce and psphotFullForceSummary to support forcing
multiple extended model fits per source.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psphot/src/psphotGalaxyShape.c

    r36633 r36718  
    238238bool psphotGalaxyShapeGrid (pmSource *source, pmSourceFitOptions *fitOptions, psphotGalaxyShapeOptions *opt, psImageMaskType maskVal, int psfSize) {
    239239
    240     // we use the 0th model to define the initial guess shape
    241     pmModel *model = source->modelFits->data[0];
    242     if (!model) return false;
    243 
    244     pmModelType modelType = model->type;
    245 
    246     // we are using fitOptions->mode : be sure this makes sense
    247     pmPCMdata *pcm = pmPCMinit (source, fitOptions, model, maskVal, psfSize);
    248     if (!pcm) return false;
    249 
    250     // we are fitting only PM_PAR_I0; the shape elements are generated from a grid
    251     psF32 *PAR = pcm->modelConv->params->data.F32;
    252 
    253     // double check that the guess is carried along...
    254 
    255     // I have some source guess (e0, e1, e2)
    256     psEllipseAxes guessAxes = pmPSF_ModelToAxes (PAR, modelType);
    257 
    258     if (!source->galaxyFits) {
    259         source->galaxyFits = pmSourceGalaxyFitsAlloc();
    260     }
    261 
    262     float fRmajorBest = NAN;
    263     float fRminorBest = NAN;
    264     float chisqBest = NAN;
    265     for (float fRmajor = opt->fRmajorMin; fRmajor < opt->fRmajorMax + 0.5*opt->fRmajorDel; fRmajor += opt->fRmajorDel) {
    266         for (float fRminor = opt->fRminorMin; fRminor < opt->fRminorMax + 0.5*opt->fRminorDel; fRminor += opt->fRminorDel) {
    267  
    268             psEllipseAxes testAxes = guessAxes;
    269             testAxes.major = guessAxes.major * fRmajor;
    270             testAxes.minor = guessAxes.minor * fRminor;
    271            
    272             pmPSF_AxesToModel (PAR, testAxes, modelType);
    273            
    274             psphotGalaxyShapeSource (pcm, source, maskVal, psfSize, true);
    275 
    276             int i = source->galaxyFits->chisq->n - 1;
    277             float flux = source->galaxyFits->Flux->data.F32[i];
    278             if (isfinite(flux)) {
    279                 float thisChisq = source->galaxyFits->chisq->data.F32[i];
    280                 if (isfinite(thisChisq) && isfinite(flux) && (!isfinite(chisqBest) || thisChisq < chisqBest)) {
    281                     chisqBest = thisChisq;
    282                     fRmajorBest = fRmajor;
    283                     fRminorBest = fRminor;
     240    for (int iModel = 0 ; iModel < source->modelFits->n; iModel++) {
     241        pmModel *model = source->modelFits->data[iModel];
     242        if (!model) return false;
     243
     244        pmModelType modelType = model->type;
     245
     246        // we are using fitOptions->mode : be sure this makes sense
     247        pmPCMdata *pcm = pmPCMinit (source, fitOptions, model, maskVal, psfSize);
     248        if (!pcm) return false;
     249
     250        // we are fitting only PM_PAR_I0; the shape elements are generated from a grid
     251        psF32 *PAR = pcm->modelConv->params->data.F32;
     252
     253        // double check that the guess is carried along...
     254
     255        // I have some source guess (e0, e1, e2)
     256        psEllipseAxes guessAxes = pmPSF_ModelToAxes (PAR, modelType);
     257
     258        if (!source->galaxyFits) {
     259            source->galaxyFits = psArrayAllocEmpty(1);
     260            // source->galaxyFits = pmSourceGalaxyFitsAlloc();
     261        }
     262        pmSourceGalaxyFits *galaxyFits = pmSourceGalaxyFitsAlloc();
     263        psArrayAdd(source->galaxyFits, 1, galaxyFits);
     264        psFree(galaxyFits);
     265
     266        float fRmajorBest = NAN;
     267        float fRminorBest = NAN;
     268        float chisqBest = NAN;
     269        for (float fRmajor = opt->fRmajorMin; fRmajor < opt->fRmajorMax + 0.5*opt->fRmajorDel; fRmajor += opt->fRmajorDel) {
     270            for (float fRminor = opt->fRminorMin; fRminor < opt->fRminorMax + 0.5*opt->fRminorDel; fRminor += opt->fRminorDel) {
     271     
     272                psEllipseAxes testAxes = guessAxes;
     273                testAxes.major = guessAxes.major * fRmajor;
     274                testAxes.minor = guessAxes.minor * fRminor;
     275               
     276                pmPSF_AxesToModel (PAR, testAxes, modelType);
     277               
     278                psphotGalaxyShapeSource (pcm, source, galaxyFits, maskVal, psfSize, true);
     279
     280                int i = galaxyFits->chisq->n - 1;
     281                float flux = galaxyFits->Flux->data.F32[i];
     282                if (isfinite(flux)) {
     283                    float thisChisq = galaxyFits->chisq->data.F32[i];
     284                    if (isfinite(thisChisq) && isfinite(flux) && (!isfinite(chisqBest) || thisChisq < chisqBest)) {
     285                        chisqBest = thisChisq;
     286                        fRmajorBest = fRmajor;
     287                        fRminorBest = fRminor;
     288                    }
    284289                }
     290                // reset I0 to avoid potential problems on the next iteration
     291                PAR[PM_PAR_I0] = 1.0;
    285292            }
    286             // reset I0 to avoid potential problems on the next iteration
    287             PAR[PM_PAR_I0] = 1.0;
    288         }
    289     }
    290 
    291 #define SAVE_BEST_MODEL
    292 #ifdef SAVE_BEST_MODEL
    293     // Save model with smallest chisq
    294     if (isfinite(chisqBest)) {
    295 #else
    296     // Save model with nominal parameters
    297     {
    298         fRmajorBest = 1;
    299         fRminorBest = 1;
     293        }
     294
     295        if (isfinite(chisqBest)) {
     296            // now save the best fitting model as the source's extended model ...
     297            psEllipseAxes testAxes = guessAxes;
     298
     299            // ... unless this macro is defined
     300#ifndef SAVE_NOMINAL_MODEL
     301            testAxes.major = guessAxes.major * fRmajorBest;
     302            testAxes.minor = guessAxes.minor * fRminorBest;
    300303#endif
    301         // now save the best fitting model as the source's extended model
    302         psEllipseAxes testAxes = guessAxes;
    303         testAxes.major = guessAxes.major * fRmajorBest;
    304         testAxes.minor = guessAxes.minor * fRminorBest;
    305        
    306         pmPSF_AxesToModel (PAR, testAxes, modelType);
    307        
    308         psphotGalaxyShapeSource (pcm, source, maskVal, psfSize, false);
    309 
    310         // Replace modelEXT with this model, if the model is good
    311         if (isfinite(PAR[PM_PAR_I0])) {
    312             psFree (source->modelEXT);
    313 
    314             source->modelEXT = psMemIncrRefCounter (pcm->modelConv);
    315             source->type = PM_SOURCE_TYPE_EXTENDED;
    316             source->mode |= PM_SOURCE_MODE_EXTMODEL;
    317             source->mode |= PM_SOURCE_MODE_NONLINEAR_FIT;
    318 
    319             // cache the model flux
    320             pmPCMCacheModel (source, maskVal, psfSize, fitOptions->nsigma);
    321         }
    322     }
    323 
    324     psFree (pcm);
     304           
     305            pmPSF_AxesToModel (PAR, testAxes, modelType);
     306               
     307            psphotGalaxyShapeSource (pcm, source, galaxyFits, maskVal, psfSize, false);
     308
     309            // Replace modelEXT with the best model from the first of the model fits if one of them is good
     310            if (isfinite(PAR[PM_PAR_I0]) && iModel == 0) {
     311                psFree (source->modelEXT);
     312
     313                source->modelEXT = psMemIncrRefCounter (pcm->modelConv);
     314                source->type = PM_SOURCE_TYPE_EXTENDED;
     315                source->mode |= PM_SOURCE_MODE_EXTMODEL;
     316                source->mode |= PM_SOURCE_MODE_NONLINEAR_FIT;
     317
     318                // cache the model flux
     319                pmPCMCacheModel (source, maskVal, psfSize, fitOptions->nsigma);
     320            }
     321        }
     322
     323        psFree (pcm);
     324    }
    325325    return true;
    326326}
     
    328328// fit the given model to the source and find chisq & normalization
    329329// XXX is this a single-component model? sersic with a supplied index, Reff, axis ratio, and theta?
    330 bool psphotGalaxyShapeSource (pmPCMdata *pcm, pmSource *source, psImageMaskType maskVal, int psfSize, bool saveResults) {
     330bool psphotGalaxyShapeSource (pmPCMdata *pcm, pmSource *source, pmSourceGalaxyFits *galaxyFits, psImageMaskType maskVal, int psfSize, bool saveResults) {
    331331
    332332    PS_ASSERT_PTR_NON_NULL(source, false);
     
    384384
    385385    if (saveResults) {
    386         psVectorAppend (source->galaxyFits->Flux, flux);
    387         psVectorAppend (source->galaxyFits->dFlux, dflux);
    388         psVectorAppend (source->galaxyFits->chisq, Chisq);
    389         source->galaxyFits->nPix = nPix;
     386        psVectorAppend (galaxyFits->Flux, flux);
     387        psVectorAppend (galaxyFits->dFlux, dflux);
     388        psVectorAppend (galaxyFits->chisq, Chisq);
     389        galaxyFits->nPix = nPix;
     390        galaxyFits->modelType = pcm->modelConv->type;
    390391    }
    391392
Note: See TracChangeset for help on using the changeset viewer.