IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jul 17, 2014, 12:30:45 PM (12 years ago)
Author:
eugene
Message:

merge changes (from past YEAR) into trunk

Location:
branches/eam_branches/ipp-ops-20130712/psphot
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-ops-20130712/psphot

    • Property svn:mergeinfo deleted
  • branches/eam_branches/ipp-ops-20130712/psphot/src

    • Property svn:ignore
      •  

        old new  
        2424psphotModelTest
        2525psphotMinimal
         26psphotFullForce
         27psmakecff
         28psphotFullForceSummary
    • Property svn:mergeinfo deleted
  • branches/eam_branches/ipp-ops-20130712/psphot/src/psphotExtendedSourceFits.c

    r35769 r37066  
    111111    }
    112112
     113    float fitNsigmaConv = psMetadataLookupF32 (&status, recipe, "EXT_FIT_NSIGMA_CONV"); // number of sigma for the convolution
     114    if (!status || !isfinite(fitNsigmaConv) || fitNsigmaConv <= 0) {
     115        fitNsigmaConv = 5.0;
     116    }
     117
    113118    bool chisqConvergence = psMetadataLookupBool (&status, recipe, "LMM_FIT_CHISQ_CONVERGENCE"); // Fit tolerance
    114119    if (!status) {
     
    123128    }
    124129
     130    // perform full extended source non-linear fits?
     131    bool isInteractive = psMetadataLookupBool (&status, recipe, "EXTENDED_SOURCE_FITS_INTERACTIVE");
     132    if (!status) isInteractive = false;
     133
    125134    // Define source fitting parameters for extended source fits
    126135    pmSourceFitOptions *fitOptions = pmSourceFitOptionsAlloc();
    127     fitOptions->mode           = PM_SOURCE_FIT_EXT;
     136    fitOptions->mode           = PM_SOURCE_FIT_EXT_AND_SKY;
    128137    fitOptions->saveCovariance = true;  // XXX make this a user option?
    129138    fitOptions->covarFactor    = psImageCovarianceFactorForAperture(readout->covariance, 10.0); // Covariance matrix
     
    131140    fitOptions->minTol         = fitMinTol;
    132141    fitOptions->maxTol         = fitMaxTol;
     142    fitOptions->nsigma         = fitNsigmaConv;
    133143
    134144    fitOptions->gainFactorMode   = gainFactorMode;
    135145    fitOptions->chisqConvergence = chisqConvergence;
     146    fitOptions->isInteractive    = isInteractive;
     147
     148    // use poissonian errors or local-sky errors
     149    fitOptions->poissonErrors = psMetadataLookupBool (&status, recipe, "EXTENDED_SOURCE_FITS_POISSON");
     150    if (!status) fitOptions->poissonErrors = true;
    136151
    137152    // maskVal is used to test for rejected pixels, and must include markVal
     
    278293            psArrayAdd(job->args, 1, cells->data[j]); // sources
    279294            psArrayAdd(job->args, 1, models);
     295
    280296            // Allocate a metadata iterator here because psMetadataIteratorAlloc/Free are not thread safe
    281297            psMetadataIterator *iter = psMetadataIteratorAlloc (models, PS_LIST_HEAD, NULL);
     
    296312            PS_ARRAY_ADD_SCALAR(job->args, 0, PS_TYPE_S32); // this is used as a return value for Nfail
    297313
    298 // set this to 0 to run without threading
    299 # if (1)           
    300             if (!psThreadJobAddPending(job)) {
    301                 psError(PS_ERR_UNKNOWN, false, "Unable to guess model.");
    302                 psFree(AnalysisRegion);
    303                 psFree (fitOptions);
    304                 psFree (models);
    305                 psphotSersicModelClassCleanup();
    306                 return false;
    307             }
    308 # else
    309             if (!psphotExtendedSourceFits_Threaded(job)) {
    310                 psError(PS_ERR_UNKNOWN, false, "Unable to guess model.");
    311                 psFree(AnalysisRegion);
    312                 psFree (fitOptions);
    313                 psFree (models);
    314                 psphotSersicModelClassCleanup();
    315                 return false;
     314// XXX TEST
     315            if (!isInteractive) {
     316                if (!psThreadJobAddPending(job)) {
     317                    psError(PS_ERR_UNKNOWN, false, "Unable to guess model.");
     318                    psFree(AnalysisRegion);
     319                    psFree (fitOptions);
     320                    psFree (models);
     321                    psphotSersicModelClassCleanup();
     322                    return false;
     323                }
     324            } else {
     325                // run without threading
     326                if (!psphotExtendedSourceFits_Threaded(job)) {
     327                    psError(PS_ERR_UNKNOWN, false, "Unable to guess model.");
     328                    psFree(AnalysisRegion);
     329                    psFree (fitOptions);
     330                    psFree (models);
     331                    psphotSersicModelClassCleanup();
     332                    return false;
     333                }
     334                psScalar *scalar = NULL;
     335                scalar = job->args->data[9];
     336                Next += scalar->data.S32;
     337                scalar = job->args->data[10];
     338                Nconvolve += scalar->data.S32;
     339                scalar = job->args->data[11];
     340                NconvolvePass += scalar->data.S32;
     341                scalar = job->args->data[12];
     342                Nplain += scalar->data.S32;
     343                scalar = job->args->data[13];
     344                NplainPass += scalar->data.S32;
     345                scalar = job->args->data[14];
     346                Nfaint += scalar->data.S32;
     347                scalar = job->args->data[15];
     348                Nfail += scalar->data.S32;
     349                psFree(job->args->data[3]); // iterator allocated above
     350                psFree(job);
    316351            }
    317             psScalar *scalar = NULL;
    318             scalar = job->args->data[9];
    319             Next += scalar->data.S32;
    320             scalar = job->args->data[10];
    321             Nconvolve += scalar->data.S32;
    322             scalar = job->args->data[11];
    323             NconvolvePass += scalar->data.S32;
    324             scalar = job->args->data[12];
    325             Nplain += scalar->data.S32;
    326             scalar = job->args->data[13];
    327             NplainPass += scalar->data.S32;
    328             scalar = job->args->data[14];
    329             Nfaint += scalar->data.S32;
    330             scalar = job->args->data[15];
    331             Nfail += scalar->data.S32;
    332             psFree(job->args->data[3]); // iterator allocated above
    333             psFree(job);
    334 # endif
    335352        }
    336353
     
    418435    // psTraceSetLevel ("psLib.math.psMinimizeLMChi2_Alt", 5);
    419436
     437    pmModelStatus badModel = PM_MODEL_STATUS_NONE;
     438    badModel |= PM_MODEL_STATUS_BADARGS;
     439    badModel |= PM_MODEL_STATUS_OFFIMAGE;
     440    badModel |= PM_MODEL_STATUS_NAN_CHISQ;
     441    badModel |= PM_MODEL_SERSIC_PCM_FAIL_GUESS;
     442    badModel |= PM_MODEL_SERSIC_PCM_FAIL_GRID;
     443    badModel |= PM_MODEL_PCM_FAIL_GUESS;
     444
    420445    // choose the sources of interest
    421446    for (int i = 0; i < sources->n; i++) {
     
    423448        pmSource *source = sources->data[i];
    424449
    425         // skip PSF-like and non-astronomical objects
    426         if (!(source->mode & PM_SOURCE_MODE_EXT_LIMIT)) continue;
    427         if (source->type == PM_SOURCE_TYPE_DEFECT) continue;
    428         if (source->type == PM_SOURCE_TYPE_SATURATED) continue;
    429 
    430         // skip saturated stars modeled with a radial profile
    431         if (source->mode2 & PM_SOURCE_MODE2_SATSTAR_PROFILE) continue;
    432 
    433         // XXX this should use peak?
     450        // rules for measuring petrosian parameters for specific objects are set in
     451        // psphotChooseAnalysisOptions.c
     452        if (!(source->tmpFlags & PM_SOURCE_TMPF_EXT_FIT)) continue;
     453
     454        // limit selection by analysis region (XXX move this into psphotChooseAnalysisOption?)
    434455        if (source->peak->x < region->x0) continue;
    435456        if (source->peak->y < region->y0) continue;
    436457        if (source->peak->x > region->x1) continue;
    437458        if (source->peak->y > region->y1) continue;
    438 
    439 
    440         // XXX for a test, just do the obvious trail
    441         // XXX if (source->peak->xf < 1100) continue;
    442         // XXX if (source->peak->xf > 1400) continue;
    443         // XXX if (source->peak->yf >  245) continue;
    444459
    445460        // replace object in image
     
    452467        psphotSetRadiusMomentsExact(&fitRadius, &windowRadius, readout, source, markVal); // NOTE : 6 allocs
    453468
    454         // XXX WATCH OUT HERE!!
    455         // fitRadius = 30;
    456 
    457469        // UPDATE : we have changed the moments calculation.  There is now an iteration within
    458470        // psphotKronMasked to determine moments appropriate for a larger object.  The values
     
    464476            source->modelFits = psArrayAllocEmpty (models->list->n);
    465477        }
     478
     479# ifdef TEST_OBJECT
     480        bool testObject = false;
     481        testObject |= ((fabs(source->peak->xf -  179) < 5) && (fabs(source->peak->yf - 1138) < 5));
     482        if (testObject) {
     483            fprintf (stderr, "test object @ %f, %f\n", source->peak->xf, source->peak->yf);
     484            psTraceSetLevel ("psModules.objects.pmPCM_MinimizeChisq", 5);
     485            psTraceSetLevel ("psphot.psphotExtendedSourceFits_Threaded", 5);
     486        }
     487# endif
    466488
    467489        // loop here over the models chosen for each source (exclude by S/N)
     
    479501          assert (status);
    480502
    481           // limit selection to some SN limit
    482           bool skipSource = false;
    483           if (source->mode & PM_SOURCE_MODE_EXT_LIMIT) {
    484               skipSource = (source->moments->KronFlux < FIT_SN_LIM * source->moments->KronFluxErr);
    485           } else {
    486               skipSource = (sqrt(source->peak->detValue) < FIT_SN_LIM);
    487           }
    488           if (skipSource) {
     503          // limit selection to some SN limit for specific models (this value only applies if > EXTENDED_SOURCE_SN_LIM)
     504          if (isfinite(FIT_SN_LIM)) {
     505            if (source->moments->KronFlux < FIT_SN_LIM * source->moments->KronFluxErr) {
    489506              Nfaint ++;
    490507              continue;
     508            }
    491509          }
     510
     511          source->mode2 |= PM_SOURCE_MODE2_EXT_FITS_RUN;
    492512
    493513          // check on the model type
     
    507527                  psTrace ("psphot", 5, "failed to fit psf-conv model for object at %f, %f", source->moments->Mx, source->moments->My);
    508528                  Nfail ++;
     529                  source->mode2 |= PM_SOURCE_MODE2_EXT_FITS_FAIL;
    509530                  continue;
    510531              }
     
    512533                       source->moments->Mx, source->moments->My, pmModelClassGetName (modelFit->type), modelFit->chisq, modelFit->nPix, modelFit->nIter);
    513534              Nconvolve ++;
    514               if (!(modelFit->flags & (PM_MODEL_STATUS_BADARGS | PM_MODEL_STATUS_NONCONVERGE | PM_MODEL_STATUS_OFFIMAGE))) {
     535              if (!(modelFit->flags & badModel)) {
    515536                  NconvolvePass ++;
    516537                  source->mode |= PM_SOURCE_MODE_EXTENDED_FIT;
     
    526547                      Nfail ++;
    527548                      doneFits = true;
     549                      source->mode2 |= PM_SOURCE_MODE2_EXT_FITS_FAIL;
    528550                      continue;
    529551                  }
    530552                  psTrace ("psphot", 4, "fit plain model for %f, %f : %s chisq = %f (npix: %d, niter: %d)\n", source->moments->Mx, source->moments->My, pmModelClassGetName (modelFit->type), modelFit->chisq, modelFit->nPix, modelFit->nIter);
    531553                  Nplain ++;
    532                   if (!(modelFit->flags & (PM_MODEL_STATUS_BADARGS | PM_MODEL_STATUS_NONCONVERGE | PM_MODEL_STATUS_OFFIMAGE))) {
     554                  if (!(modelFit->flags & badModel)) {
    533555                      NplainPass ++;
    534556                      source->mode |= PM_SOURCE_MODE_EXTENDED_FIT;
     
    544566                      float Ymax = PAR[PM_PAR_YPOS] + 0.5*PAR[PM_PAR_LENGTH]*cos(PAR[PM_PAR_THETA]);
    545567
    546                       if (false && (source->peak->xf > 1100) &&
    547                           (source->peak->xf < 1400) &&
    548                           (source->peak->yf < 245)) {
     568                      if ((fabs(source->peak->xf - 2572) < 20) && (fabs(source->peak->yf - 5874) < 20)) {
    549569                          fprintf (stderr, "src vs fit : %d %d - %d %d | %f %f - %f %f\n",
    550570                                   source->pixels->col0, source->pixels->row0,
     
    556576                          fprintf (stderr, "update window : %f %f : %f -> %f\n", source->peak->xf, source->peak->yf, fitRadius, 2*fitRadius);
    557577                          psphotSetWindowTrail (&fitRadius, &windowRadius, readout, source, markVal, fitRadius*2.0);
     578                          source->mode2 |= PM_SOURCE_MODE2_EXT_FITS_RETRY;
    558579                      }
    559580                  }
    560581              }
    561582          }
    562           // XXX really need to do this in a cleaner way:
    563           if (!modelFit) continue;
     583          psAssert (modelFit, "modelFit not set?");
    564584
    565585          // test for fit quality / result
     
    570590        }
    571591
     592        // we are allowed to fit both stars and non-stars here -- if we have fitted
     593        // something which we think is a star, we should use that model to subtract the
     594        // object from the image.
     595        if (source->type == PM_SOURCE_TYPE_STAR) {
     596          // ensure the modelPSF is cached
     597          pmSourceCacheModel (source, maskVal);
     598          pmSourceSub (source, PM_MODEL_OP_FULL, maskVal);
     599          continue;
     600        }
     601
    572602        // evaluate the relative quality of the models, choose one
     603        // the PSF model might be the best fit : allow it to succeed
    573604        float minChisq = NAN;
    574605        int minModel = -1;
     
    576607            pmModel *model = source->modelFits->data[i];
    577608
     609            // skip the really bad fits
    578610            if (!(model->flags & PM_MODEL_STATUS_FITTED)) continue;
    579 
    580             if (model->flags & (PM_MODEL_STATUS_BADARGS)) continue;
    581             if (model->flags & (PM_MODEL_STATUS_NONCONVERGE)) continue;
    582             if (model->flags & (PM_MODEL_STATUS_OFFIMAGE)) continue;
     611            if (model->flags & badModel) continue;
     612
     613            // if (model->flags & (PM_MODEL_STATUS_NONCONVERGE)) continue;
    583614
    584615            if ((minModel < 0) || (model->chisq < minChisq)) {
     
    598629          pmSourceCacheModel (source, maskVal);
    599630
     631# if (PS_TRACE_ON)
     632          pmModel *model = source->modelFits->data[0];
     633          int flags = 0xffffffff;
     634          if (model) {
     635            flags = model->flags;
     636          }
     637          fprintf (stderr, "failed to fit extended source model to object %d @ %f, %f (%x)\n", source->id, source->moments->Mx, source->moments->My, flags);
     638#endif
    600639          pmSourceSub (source, PM_MODEL_OP_FULL, maskVal);
    601640
     641          source->mode2 |= PM_SOURCE_MODE2_EXT_FITS_NONE;
    602642          continue;
    603643        }
     
    615655        // cache the model flux
    616656        if (source->modelEXT->isPCM) {
    617             pmPCMCacheModel (source, maskVal, psfSize);
     657            // fprintf (stderr, "subtract PCM extended source model for object %d @ %f, %f\n", source->id, source->moments->Mx, source->moments->My);
     658            pmPCMCacheModel (source, maskVal, psfSize, fitOptions->nsigma);
    618659        } else {
     660            // fprintf (stderr, "subtract non-PCM extended source model for object %d @ %f, %f\n", source->id, source->moments->Mx, source->moments->My);
    619661            pmSourceCacheModel (source, maskVal);
    620662        }
     663        source->modelEXT->flags |= PM_MODEL_BEST_FIT;
    621664
    622665        // subtract the best fit from the object, leave local sky
     
    625668        psTrace ("psphot", 4, "best ext model for %f, %f : %s chisq = %f\n", source->moments->Mx, source->moments->My, pmModelClassGetName (source->modelEXT->type), source->modelEXT->chisq);
    626669        psTrace ("psphot", 5, "extended source model for source at %7.1f, %7.1f", source->moments->Mx, source->moments->My);
     670
     671# ifdef TEST_OBJECT
     672        if (testObject) {
     673            psTraceSetLevel ("psModules.objects.pmPCM_MinimizeChisq", 0);
     674            psTraceSetLevel ("psphot.psphotExtendedSourceFits_Threaded", 0);
     675        }
     676# endif
    627677    }
    628678
Note: See TracChangeset for help on using the changeset viewer.