IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 22, 2012, 3:33:32 PM (14 years ago)
Author:
eugene
Message:

read the time table in psTime.c; updates to the variance model tests; better trace options for output masks; convert to new PM_SOURCE_PHOTFIT version of weighting

Location:
branches/eam_branches/ipp-20120601/psphot/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20120601/psphot/src/psphot.h

    r34049 r34053  
    6161bool            psphotSetMaskAndVarianceReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe);
    6262
     63bool            psphotUpdateVariance (pmConfig *config, const pmFPAview *view, const char *filerule);
     64bool            psphotUpdateVarianceReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe);
     65
    6366bool            psphotModelBackground (pmConfig *config, const pmFPAview *view, const char *filerule);
    6467bool            psphotModelBackgroundReadoutFileIndex (pmConfig *config, const pmFPAview *view, const char *filerule, int index);
     
    99102
    100103bool            psphotFitSourcesLinear (pmConfig *config, const pmFPAview *view, const char *filerule, bool final);
    101 
    102 # if (HAVE_MODEL_VAR)
    103104bool            psphotFitSourcesLinearReadout (psMetadata *recipe, pmReadout *readout, psArray *sources, pmPSF *psf, bool final, pmSourceFitVarMode fitVarMode);
    104 # else
    105 bool            psphotFitSourcesLinearReadout (psMetadata *recipe, pmReadout *readout, psArray *sources, pmPSF *psf, bool final);
    106 # endif
    107105
    108106bool            psphotSourceSize (pmConfig *config, const pmFPAview *view, const char *filerule, bool getPSFsize);
  • branches/eam_branches/ipp-20120601/psphot/src/psphotEfficiency.c

    r34049 r34053  
    420420
    421421    // psphotFitSourcesLinearReadout subtracts the model fits
    422 # if (HAVE_MODEL_VAR)
    423422    if (!psphotFitSourcesLinearReadout(recipe, readout, fakeSourcesAll, psf, true, PM_SOURCE_PHOTFIT_CONST)) {
    424 # else
    425     if (!psphotFitSourcesLinearReadout(recipe, readout, fakeSourcesAll, psf, true)) {
    426 # endif
    427423        psError(PS_ERR_UNKNOWN, false, "Unable to perform linear fit on fake sources.");
    428424        psFree(fakeSources);
  • branches/eam_branches/ipp-20120601/psphot/src/psphotFitSourcesLinear.c

    r34049 r34053  
    1212static bool SetBorderMatrixElements (psSparseBorder *border, pmReadout *readout, psArray *sources, bool constant_weights, int SKY_FIT_ORDER, psImageMaskType markVal);
    1313
    14 # if (HAVE_MODEL_VAR)
    1514bool psphotGenerateModelVariance (pmConfig *config, const pmFPAview *view, pmFPAfile *file, int index, psMetadata *recipe, pmReadout *readout, psArray *sources);
    1615pmSourceFitVarMode psphotGetFitVarMode (psMetadata *recipe);
    1716bool psphotFreeModelVariance (pmReadout *readout, psArray *sources);
    18 # endif
    1917
    2018// for now, let's store the detections on the readout->analysis for each readout
     
    3028    assert (recipe);
    3129
    32 # if (HAVE_MODEL_VAR)
    3330    pmSourceFitVarMode fitVarMode = psphotGetFitVarMode (recipe);
    3431    if (!fitVarMode) {
     
    4037    // do a single pass.
    4138    pmSourceFitVarMode fitVarModePass1 = (fitVarMode == PM_SOURCE_PHOTFIT_MODEL_VAR) ? PM_SOURCE_PHOTFIT_CONST : fitVarMode;
    42 # endif
    4339
    4440    int num = psphotFileruleCount(config, filerule);
     
    6864        psAssert (psf, "missing psf?");
    6965
    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         {
     66        if (!psphotFitSourcesLinearReadout (recipe, readout, sources, psf, final, fitVarModePass1)) {
    7667            psError (PSPHOT_ERR_CONFIG, false, "failed to fit sources (linear) for %s entry %d", filerule, i);
    7768            return false;
    7869        }
    7970
    80 # if (HAVE_MODEL_VAR)
    8171        // the MODEL_VAR weighting scheme requires knowledge of the model fluxes to generate the variance
    8272        // after we have determined the initial set of fits, then we can generate the variance image and
     
    10494            }
    10595        }
    106 # endif
    10796
    10897        psphotVisualShowResidualImage (readout, (num > 0));
     
    113102}
    114103
    115 # if (HAVE_MODEL_VAR)
    116104// look up the fit variance mode from the recipe; older recipes do not have the value
    117105// 'LINEAR_FIT_VARIANCE_MODE'; in those cases, look for 'CONSTANT_PHOTOMETRIC_WEIGHTS' as a boolean and
     
    136124        return PM_SOURCE_PHOTFIT_IMAGE_VAR;
    137125    }
     126    if (!strcasecmp(fitVarModeString, "SKY")   || !strcasecmp(fitVarModeString, "MODEL_SKY")) {
     127        return PM_SOURCE_PHOTFIT_MODEL_SKY;
     128    }
    138129    if (!strcasecmp(fitVarModeString, "MODEL") || !strcasecmp(fitVarModeString, "MODEL_VAR")) {
    139130        return PM_SOURCE_PHOTFIT_MODEL_VAR;
     
    142133    return PM_SOURCE_PHOTFIT_NONE;
    143134}
    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 {
     135
     136bool psphotFitSourcesLinearReadout (psMetadata *recipe, pmReadout *readout, psArray *sources, pmPSF *psf, bool final, pmSourceFitVarMode fitVarMode) {
    152137    bool status;
    153138    float x;
     
    185170    if (psRegionIsNaN (AnalysisRegion)) psAbort("analysis region mis-defined");
    186171
    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
    194172    int SKY_FIT_ORDER = psMetadataLookupS32(&status, recipe, "SKY_FIT_ORDER");
    195173    if (!status) {
     
    319297    psSparseBorder *border = psSparseBorderAlloc (sparse, nBorder);
    320298
    321 # if (HAVE_MODEL_VAR)
    322299    // if fitVarMode is MODEL_VAR, then we need to generate the model image variance
    323300    // XXX we have two possibilities here:
     
    327304
    328305    // 2) do a single pass, and use the model guess to define the model variance (but do I trust the Model Guess?)
    329 # endif
    330306
    331307    // fill out the sparse matrix elements and border elements (B)
     
    336312
    337313        // diagonal elements of the sparse matrix (auto-cross-product)
    338 # if (HAVE_MODEL_VAR)
    339314        f = pmSourceModelDotModel (SRCi, SRCi, fitVarMode, covarFactor, maskVal);
    340 # else
    341         f = pmSourceModelDotModel (SRCi, SRCi, CONSTANT_PHOTOMETRIC_WEIGHTS, covarFactor, maskVal);
    342 # endif
    343315        psSparseMatrixElement (sparse, i, i, f);
    344316
    345 # if (HAVE_MODEL_VAR)
    346         // if we have used CONSTANT errors, then we need to calculate the value of the parameter error
    347         if (fitVarMode != PM_SOURCE_PHOTFIT_IMAGE_VAR) {
     317        // if we have used CONSTANT errors, then we need to re-calculate the value of the parameter error
     318        if (fitVarMode == PM_SOURCE_PHOTFIT_CONST) {
    348319            float var = pmSourceModelDotModel (SRCi, SRCi, PM_SOURCE_PHOTFIT_IMAGE_VAR, covarFactor, maskVal);
    349320            errors->data.F32[i] = 1.0 / sqrt(var);
     
    351322            errors->data.F32[i] = 1.0 / sqrt(f);
    352323        }
    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
    362324
    363325        // find the image x model value
    364 # if (HAVE_MODEL_VAR)
    365326        f = pmSourceDataDotModel (SRCi, SRCi, fitVarMode, covarFactor, maskVal);
    366 # else
    367         f = pmSourceDataDotModel (SRCi, SRCi, CONSTANT_PHOTOMETRIC_WEIGHTS, covarFactor, maskVal);
    368 # endif
    369327        psSparseVectorElement (sparse, i, f);
    370328
     
    372330        switch (SKY_FIT_ORDER) {
    373331          case 1:
    374 # if (HAVE_MODEL_VAR)
    375332            f = pmSourceModelWeight (SRCi, 1, fitVarMode, covarFactor, maskVal);
    376333            psSparseBorderElementB (border, i, 1, f);
    377334            f = pmSourceModelWeight (SRCi, 2, fitVarMode, covarFactor, maskVal);
    378335            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
    385336
    386337          case 0:
    387 # if (HAVE_MODEL_VAR)
    388338            f = pmSourceModelWeight (SRCi, 0, fitVarMode, covarFactor, maskVal);
    389 # else
    390             f = pmSourceModelWeight (SRCi, 0, CONSTANT_PHOTOMETRIC_WEIGHTS, covarFactor, maskVal);
    391 # endif
    392339            psSparseBorderElementB (border, i, 0, f);
    393340            break;
     
    409356
    410357            // got an overlap; calculate cross-product and add to output array
    411 # if (HAVE_MODEL_VAR)
    412358            f = pmSourceModelDotModel (SRCi, SRCj, fitVarMode, covarFactor, maskVal);
    413 # else
    414             f = pmSourceModelDotModel (SRCi, SRCj, CONSTANT_PHOTOMETRIC_WEIGHTS, covarFactor, maskVal);
    415 # endif
    416359            psSparseMatrixElement (sparse, j, i, f);
    417360        }
     
    451394
    452395    // set the sky, sky_x, sky_y components of border matrix
    453 # if (HAVE_MODEL_VAR)
    454396    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
    458397
    459398    psSparseConstraint constraint;
     
    614553}
    615554
    616 # if (HAVE_MODEL_VAR)
    617555bool psphotModelBackgroundReadout(psImage *model,  // Model image
    618556                                  psImage *modelStdev, // Model stdev image
     
    650588    psImage *varModelStdev = psImageAlloc(backBinning->nXruff, backBinning->nYruff, PS_TYPE_F32); // Background model
    651589
     590    // generate an image of the mean variance image in DN
    652591    if (!psphotModelBackgroundReadout(varModel, varModelStdev, NULL, readout, backBinning, config, true)) {
    653592        psError(PS_ERR_UNKNOWN, false, "Unable to generate background model");
     
    668607    psFree (varModelStdev);
    669608
    670     // XXX for a test:
    671     psphotSaveImage (NULL, modelVar, "model.bck.fits");
     609    float gain = 1.0;  // accept 1.0 as a default since it is not critical to the analysis
     610    pmCell *cell = readout->parent; // The parent cell
     611    if (cell) {
     612      gain = psMetadataLookupF32(&status, cell->concepts, "CELL.GAIN"); // Cell gain
     613      if (!status) {
     614        gain = 1.0;           // set note above
     615      }
     616    }
     617    if (gain > 2.0) { /* warn? */ }
    672618
    673619    // insert all of the source models
     
    690636
    691637        // add the source model to the model variance image
     638        // XXX note that this should be added with gain applied
     639        // var_DN = flux_DN / gain [e/DN]
     640        // to do this requires an API upgrade...
    692641        pmSourceAdd (source, PM_MODEL_OP_MODELVAR, maskVal);
    693642    }
    694 
    695     // XXX for a test:
    696     psphotSaveImage (NULL, modelVar, "model.var.fits");
    697     psphotSaveImage (NULL, readout->variance, "image.var.fits");
    698643
    699644    // we save the model variance for future reference
     
    724669    return true;
    725670}
    726 # endif
  • branches/eam_branches/ipp-20120601/psphot/src/psphotMaskReadout.c

    r29936 r34053  
    9494
    9595    // test output of files at this stage
    96     if (psTraceGetLevel("psphot") >= 5) {
     96    if (psTraceGetLevel("psphot.imsave") >= 5) {
    9797        psphotSaveImage (NULL, readout->image,  "image.fits");
    9898        psphotSaveImage (NULL, readout->mask,   "mask.fits");
     
    105105    return true;
    106106}
     107
     108// XXX this function and support below was created to test the theory that the faint-end
     109// bias results from the Poisson variation of the background pixels.  This is NOT the
     110// case.  Using the code below maintains the faint-end bias.
     111bool psphotUpdateVariance (pmConfig *config, const pmFPAview *view, const char *filerule) {
     112
     113    bool status = false;
     114
     115    // select the appropriate recipe information
     116    psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, PSPHOT_RECIPE);
     117    psAssert (recipe, "missing recipe?");
     118
     119    int num = psphotFileruleCount(config, filerule);
     120
     121    // loop over the available readouts
     122    for (int i = 0; i < num; i++) {
     123
     124        // Generate the mask and weight images, including the user-defined analysis region of interest
     125        if (!psphotUpdateVarianceReadout (config, view, filerule, i, recipe)) {
     126            psError (PSPHOT_ERR_CONFIG, false, "failed to generate mask for %s entry %d", filerule, i);
     127            return false;
     128        }
     129    }
     130    return true;
     131}
     132
     133// determine the mean variance image (equivalent to the background model, but for the variance image)
     134// set the variance image to the MAX(input, mean)
     135bool psphotUpdateVarianceReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe) {
     136
     137    bool status;
     138
     139    pmFPAfile *file = pmFPAfileSelectSingle(config->files, filerule, index); // File of interest
     140    psAssert (file, "missing file?");
     141
     142    // find the currently selected readout
     143    pmReadout  *readout = pmFPAviewThisReadout (view, file->fpa);
     144    psAssert (readout, "missing readout?");
     145
     146    pmSourceFitVarMode varMode = psphotGetFitVarMode (recipe);
     147    if (varMode == PM_SOURCE_PHOTFIT_NONE) {
     148      psError (PSPHOT_ERR_CONFIG, false, "need valid LINEAR_FIT_VARIANCE_MODE");
     149      return false;
     150    }
     151
     152    // make this an option via the recipe
     153    if (varMode != PM_SOURCE_PHOTFIT_MODEL_SKY) return true;
     154
     155    // create a model variance image (full-scale image to take result of psImageUnbin below)
     156    psImage *modelVar = psImageCopy (NULL, readout->variance, PS_TYPE_F32);
     157
     158    // find the binning information
     159    psImageBinning *backBinning = psphotBackgroundBinning (modelVar, config);
     160    assert (backBinning);
     161   
     162    psImage *varModel = psImageAlloc(backBinning->nXruff, backBinning->nYruff, PS_TYPE_F32); // Background model
     163    psImage *varModelStdev = psImageAlloc(backBinning->nXruff, backBinning->nYruff, PS_TYPE_F32); // Background model
     164
     165    if (!psphotModelBackgroundReadout(varModel, varModelStdev, NULL, readout, backBinning, config, true)) {
     166        psError(PS_ERR_UNKNOWN, false, "Unable to generate background model");
     167        psFree (varModel);
     168        psFree (varModelStdev);
     169        return false;
     170    }
     171
     172    // linear interpolation to full-scale
     173    if (!psImageUnbin (modelVar, varModel, backBinning)) {
     174        psError (PSPHOT_ERR_PROG, true, "inconsistent sizes for unbinning");
     175        psFree (varModel);
     176        psFree (varModelStdev);
     177        return false;
     178    }
     179
     180    // XXX save these?
     181    psFree (varModel);
     182    psFree (varModelStdev);
     183
     184    psImage *im = readout->image;
     185    psImage *wt = readout->variance;
     186    for (int j = 0; j < im->numRows; j++) {
     187      for (int i = 0; i < im->numCols; i++) {
     188        if (!isfinite(im->data.F32[j][i])) continue;
     189        if (!isfinite(wt->data.F32[j][i])) continue;
     190        // XXX for a test, make variance constant wt->data.F32[j][i] = PS_MAX(wt->data.F32[j][i], modelVar->data.F32[j][i]);
     191        wt->data.F32[j][i] = modelVar->data.F32[j][i];
     192      }
     193    }
     194
     195    // test output of files at this stage
     196    if (psTraceGetLevel("psphot.imsave") >= 5) {
     197        psphotSaveImage (NULL, readout->image,  "image.varsky.fits");
     198        psphotSaveImage (NULL, readout->mask,   "mask.varsky.fits");
     199        psphotSaveImage (NULL, readout->variance, "variance.varsky.fits");
     200    }
     201
     202    psFree (modelVar);
     203
     204    return true;
     205}
Note: See TracChangeset for help on using the changeset viewer.