IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 26, 2012, 11:33:10 AM (14 years ago)
Author:
eugene
Message:

re-enable MODEL_VAR option for linear photometry fit

Location:
trunk/psphot
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/psphot

  • trunk/psphot/src

  • trunk/psphot/src/psphotFitSourcesLinear.c

    r33994 r34086  
    1212static bool SetBorderMatrixElements (psSparseBorder *border, pmReadout *readout, psArray *sources, bool constant_weights, int SKY_FIT_ORDER, psImageMaskType markVal);
    1313
    14 # if (HAVE_MODEL_VAR)
    15 bool psphotGenerateModelVariance (pmConfig *config, const pmFPAview *view, pmFPAfile *file, int index, psMetadata *recipe, pmReadout *readout, psArray *sources);
    16 pmSourceFitVarMode psphotGetFitVarMode (psMetadata *recipe);
    17 bool psphotFreeModelVariance (pmReadout *readout, psArray *sources);
    18 # endif
    19 
    2014// for now, let's store the detections on the readout->analysis for each readout
    2115bool psphotFitSourcesLinear (pmConfig *config, const pmFPAview *view, const char *filerule, bool final)
     
    3024    assert (recipe);
    3125
    32 # if (HAVE_MODEL_VAR)
    3326    pmSourceFitVarMode fitVarMode = psphotGetFitVarMode (recipe);
    3427    if (!fitVarMode) {
     
    4033    // do a single pass.
    4134    pmSourceFitVarMode fitVarModePass1 = (fitVarMode == PM_SOURCE_PHOTFIT_MODEL_VAR) ? PM_SOURCE_PHOTFIT_CONST : fitVarMode;
    42 # endif
    4335
    4436    int num = psphotFileruleCount(config, filerule);
     
    6860        psAssert (psf, "missing psf?");
    6961
    70 # if (HAVE_MODEL_VAR)
    71         if (!psphotFitSourcesLinearReadout (recipe, readout, sources, psf, final, fitVarModePass1))
    72 # else
    73         if (!psphotFitSourcesLinearReadout (recipe, readout, sources, psf, final))
    74 # endif
    75         {
     62        if (!psphotFitSourcesLinearReadout (recipe, readout, sources, psf, final, fitVarModePass1)) {
    7663            psError (PSPHOT_ERR_CONFIG, false, "failed to fit sources (linear) for %s entry %d", filerule, i);
    7764            return false;
    7865        }
    7966
    80 # if (HAVE_MODEL_VAR)
    8167        // the MODEL_VAR weighting scheme requires knowledge of the model fluxes to generate the variance
    8268        // after we have determined the initial set of fits, then we can generate the variance image and
     
    10490            }
    10591        }
    106 # endif
    10792
    10893        psphotVisualShowResidualImage (readout, (num > 0));
     
    11398}
    11499
    115 # if (HAVE_MODEL_VAR)
    116100// look up the fit variance mode from the recipe; older recipes do not have the value
    117101// 'LINEAR_FIT_VARIANCE_MODE'; in those cases, look for 'CONSTANT_PHOTOMETRIC_WEIGHTS' as a boolean and
     
    136120        return PM_SOURCE_PHOTFIT_IMAGE_VAR;
    137121    }
     122    if (!strcasecmp(fitVarModeString, "SKY")   || !strcasecmp(fitVarModeString, "MODEL_SKY")) {
     123        return PM_SOURCE_PHOTFIT_MODEL_SKY;
     124    }
    138125    if (!strcasecmp(fitVarModeString, "MODEL") || !strcasecmp(fitVarModeString, "MODEL_VAR")) {
    139126        return PM_SOURCE_PHOTFIT_MODEL_VAR;
     
    142129    return PM_SOURCE_PHOTFIT_NONE;
    143130}
    144 # endif
    145 
    146 # if (HAVE_MODEL_VAR)
    147 bool psphotFitSourcesLinearReadout (psMetadata *recipe, pmReadout *readout, psArray *sources, pmPSF *psf, bool final, pmSourceFitVarMode fitVarMode)
    148 # else
    149 bool psphotFitSourcesLinearReadout (psMetadata *recipe, pmReadout *readout, psArray *sources, pmPSF *psf, bool final)
    150 # endif
    151 {
     131
     132bool psphotFitSourcesLinearReadout (psMetadata *recipe, pmReadout *readout, psArray *sources, pmPSF *psf, bool final, pmSourceFitVarMode fitVarMode) {
    152133    bool status;
    153134    float x;
     
    185166    if (psRegionIsNaN (AnalysisRegion)) psAbort("analysis region mis-defined");
    186167
    187 # if (!HAVE_MODEL_VAR)
    188     bool CONSTANT_PHOTOMETRIC_WEIGHTS =
    189         psMetadataLookupBool(&status, recipe, "CONSTANT_PHOTOMETRIC_WEIGHTS");
    190     if (!status) {
    191         psAbort("You must provide a value for the BOOL recipe CONSTANT_PHOTOMETRIC_WEIGHTS");
    192     }
    193 # endif
    194168    int SKY_FIT_ORDER = psMetadataLookupS32(&status, recipe, "SKY_FIT_ORDER");
    195169    if (!status) {
     
    299273            source->mode |= PM_SOURCE_MODE_PSFMODEL;
    300274        }           
    301        
     275
    302276        psArrayAdd (fitSources, 100, source);
    303277    }
     
    319293    psSparseBorder *border = psSparseBorderAlloc (sparse, nBorder);
    320294
    321 # if (HAVE_MODEL_VAR)
    322295    // if fitVarMode is MODEL_VAR, then we need to generate the model image variance
    323296    // XXX we have two possibilities here:
     
    327300
    328301    // 2) do a single pass, and use the model guess to define the model variance (but do I trust the Model Guess?)
    329 # endif
    330302
    331303    // fill out the sparse matrix elements and border elements (B)
     
    336308
    337309        // diagonal elements of the sparse matrix (auto-cross-product)
    338 # if (HAVE_MODEL_VAR)
    339         f = pmSourceModelDotModel (SRCi, SRCi, fitVarMode, covarFactor, maskVal);
    340 # else
    341         f = pmSourceModelDotModel (SRCi, SRCi, CONSTANT_PHOTOMETRIC_WEIGHTS, covarFactor, maskVal);
    342 # endif
    343         psSparseMatrixElement (sparse, i, i, f);
    344 
    345 # if (HAVE_MODEL_VAR)
    346         // if we have used CONSTANT errors, then we need to calculate the value of the parameter error
     310        float MM = pmSourceModelDotModel (SRCi, SRCi, fitVarMode, covarFactor, maskVal);
     311        psSparseMatrixElement (sparse, i, i, MM);
     312
     313        // if we have used CONSTANT errors, then we need to re-calculate the value of the parameter error
    347314        if (fitVarMode != PM_SOURCE_PHOTFIT_IMAGE_VAR) {
    348315            float var = pmSourceModelDotModel (SRCi, SRCi, PM_SOURCE_PHOTFIT_IMAGE_VAR, covarFactor, maskVal);
    349316            errors->data.F32[i] = 1.0 / sqrt(var);
    350317        } else {
    351             errors->data.F32[i] = 1.0 / sqrt(f);
    352         }
    353 # else
    354         // the formal error depends on the weighting scheme
    355         if (CONSTANT_PHOTOMETRIC_WEIGHTS) {
    356             float var = pmSourceModelDotModel (SRCi, SRCi, false, covarFactor, maskVal);
    357             errors->data.F32[i] = 1.0 / sqrt(var);
    358         } else {
    359             errors->data.F32[i] = 1.0 / sqrt(f);
    360         }
    361 # endif
     318            errors->data.F32[i] = 1.0 / sqrt(MM);
     319        }
    362320
    363321        // find the image x model value
    364 # if (HAVE_MODEL_VAR)
    365         f = pmSourceDataDotModel (SRCi, SRCi, fitVarMode, covarFactor, maskVal);
    366 # else
    367         f = pmSourceDataDotModel (SRCi, SRCi, CONSTANT_PHOTOMETRIC_WEIGHTS, covarFactor, maskVal);
    368 # endif
    369         psSparseVectorElement (sparse, i, f);
     322        float FM = pmSourceDataDotModel (SRCi, SRCi, fitVarMode, covarFactor, maskVal);
     323        psSparseVectorElement (sparse, i, FM);
    370324
    371325        // add the per-source variances (border region)
    372326        switch (SKY_FIT_ORDER) {
    373327          case 1:
    374 # if (HAVE_MODEL_VAR)
    375328            f = pmSourceModelWeight (SRCi, 1, fitVarMode, covarFactor, maskVal);
    376329            psSparseBorderElementB (border, i, 1, f);
    377330            f = pmSourceModelWeight (SRCi, 2, fitVarMode, covarFactor, maskVal);
    378331            psSparseBorderElementB (border, i, 2, f);
    379 # else
    380             f = pmSourceModelWeight (SRCi, 1, CONSTANT_PHOTOMETRIC_WEIGHTS, covarFactor, maskVal);
    381             psSparseBorderElementB (border, i, 1, f);
    382             f = pmSourceModelWeight (SRCi, 2, CONSTANT_PHOTOMETRIC_WEIGHTS, covarFactor, maskVal);
    383             psSparseBorderElementB (border, i, 2, f);
    384 # endif
    385332
    386333          case 0:
    387 # if (HAVE_MODEL_VAR)
    388334            f = pmSourceModelWeight (SRCi, 0, fitVarMode, covarFactor, maskVal);
    389 # else
    390             f = pmSourceModelWeight (SRCi, 0, CONSTANT_PHOTOMETRIC_WEIGHTS, covarFactor, maskVal);
    391 # endif
    392335            psSparseBorderElementB (border, i, 0, f);
    393336            break;
     
    409352
    410353            // got an overlap; calculate cross-product and add to output array
    411 # if (HAVE_MODEL_VAR)
    412354            f = pmSourceModelDotModel (SRCi, SRCj, fitVarMode, covarFactor, maskVal);
    413 # else
    414             f = pmSourceModelDotModel (SRCi, SRCj, CONSTANT_PHOTOMETRIC_WEIGHTS, covarFactor, maskVal);
    415 # endif
    416355            psSparseMatrixElement (sparse, j, i, f);
    417356        }
     
    451390
    452391    // set the sky, sky_x, sky_y components of border matrix
    453 # if (HAVE_MODEL_VAR)
    454392    SetBorderMatrixElements (border, readout, fitSources, (fitVarMode == PM_SOURCE_PHOTFIT_CONST), SKY_FIT_ORDER, markVal);
    455 # else
    456     SetBorderMatrixElements (border, readout, fitSources, CONSTANT_PHOTOMETRIC_WEIGHTS, SKY_FIT_ORDER, markVal);
    457 # endif
    458393
    459394    psSparseConstraint constraint;
     
    500435    psLogMsg ("psphot.ensemble", PS_LOG_MINUTIA, "sub models: %f sec (%d elements)\n", psTimerMark ("psphot.linear"), sparse->Nelem);
    501436
     437    // mean stats on fit windows
     438    float sumRadius = 0.0;
     439    float sumPixels = 0.0;
     440    float sumSource = 0.0;
     441
    502442    // measure chisq for each source
    503443    // for (int i = 0; final && (i < fitSources->n); i++) {
     
    505445        pmSource *source = fitSources->data[i];
    506446        pmModel *model = pmSourceGetModel (NULL, source);
     447
     448        // accumulate fit windows statistics
     449        sumRadius += model->fitRadius;
     450        sumPixels += M_PI*PS_SQR(model->fitRadius);
     451        sumSource += 1.0;
     452
    507453        if (!(source->mode & PM_SOURCE_MODE_NONLINEAR_FIT)) {
    508454            model->nPar = 1; // LINEAR-only sources have 1 parameter; NONLINEAR sources have their original value
     
    511457    }
    512458    psLogMsg ("psphot.ensemble", PS_LOG_MINUTIA, "get chisqs: %f sec (%d elements)\n", psTimerMark ("psphot.linear"), sparse->Nelem);
     459
     460    float meanRadius = sumRadius / sumSource;
     461    float meanPixels = sumPixels / sumSource;
    513462
    514463    // psFree (index);
     
    520469    psFree (border);
    521470
    522     psLogMsg ("psphot.ensemble", PS_LOG_WARN, "measure ensemble of PSFs: %f sec\n", psTimerMark ("psphot.linear"));
     471    psLogMsg ("psphot.ensemble", PS_LOG_WARN, "measure ensemble of PSFs (mean radius = %f pixels, mean area = %f pixels: %f sec\n", meanRadius, meanPixels, psTimerMark ("psphot.linear"));
    523472
    524473    psphotVisualPlotChisq (sources);
     
    531480    return true;
    532481}
    533 
    534 // XXX do we need this?
    535 // XXX disallow the simultaneous sky fit and remove this code...
    536482
    537483// Calculate the weight terms for the sky fit component of the matrix.  This function operates
     
    614560}
    615561
    616 # if (HAVE_MODEL_VAR)
    617562bool psphotModelBackgroundReadout(psImage *model,  // Model image
    618563                                  psImage *modelStdev, // Model stdev image
     
    624569    );
    625570
    626 bool psphotGenerateModelVariance (pmConfig *config, const pmFPAview *view, pmFPAfile *file, int index, psMetadata *recipe, pmReadout *readout, psArray *sources) {
     571 bool psphotGenerateModelVariance (pmConfig *config, const pmFPAview *view, pmFPAfile *file, int index, psMetadata *recipe, pmReadout *readout, psArray *sources) {
    627572
    628573    bool status = false;
     
    650595    psImage *varModelStdev = psImageAlloc(backBinning->nXruff, backBinning->nYruff, PS_TYPE_F32); // Background model
    651596
     597    // generate an image of the mean variance image in DN
    652598    if (!psphotModelBackgroundReadout(varModel, varModelStdev, NULL, readout, backBinning, config, true)) {
    653599        psError(PS_ERR_UNKNOWN, false, "Unable to generate background model");
     
    664610        return false;
    665611    }
    666 
    667612    psFree (varModel);
    668613    psFree (varModelStdev);
    669614
    670     // XXX for a test:
    671     psphotSaveImage (NULL, modelVar, "model.bck.fits");
     615    float gain = 1.0;  // accept 1.0 as a default since it is not critical to the analysis
     616    pmCell *cell = readout->parent; // The parent cell
     617    if (cell) {
     618      gain = psMetadataLookupF32(&status, cell->concepts, "CELL.GAIN"); // Cell gain
     619      if (!status) {
     620        gain = 1.0;           // set note above
     621      }
     622    }
     623    if (gain > 2.0) { /* warn? */ }
     624    // XXX we are not actually using the gain, but need to test it to avoid gcc pedantic warnings
    672625
    673626    // insert all of the source models
     
    690643
    691644        // add the source model to the model variance image
     645        // XXX note that this should be added with gain applied
     646        // var_DN = flux_DN / gain [e/DN]
     647        // to do this requires an API upgrade...
    692648        pmSourceAdd (source, PM_MODEL_OP_MODELVAR, maskVal);
    693649    }
    694 
    695     // XXX for a test:
    696     psphotSaveImage (NULL, modelVar, "model.var.fits");
    697     psphotSaveImage (NULL, readout->variance, "image.var.fits");
    698650
    699651    // we save the model variance for future reference
     
    724676    return true;
    725677}
    726 # endif
Note: See TracChangeset for help on using the changeset viewer.