IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 33071


Ignore:
Timestamp:
Jan 10, 2012, 8:02:55 AM (15 years ago)
Author:
eugene
Message:

working on model variance

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20111122/psphot/src/psphotFitSourcesLinear.c

    r33070 r33071  
    2424    assert (recipe);
    2525
     26    pmSourceFitVarMode fitVarMode = psphotGetFitVarMode (recipe);
     27    if (!fitVarMode) {
     28        psError (PSPHOT_ERR_CONFIG, true, "failed to get LINEAR_FIT_VARIANCE_MODE");
     29        return false;
     30    }
     31    pmSourceFitVarMode fitVarModePass1 = (fitVarMode == PM_SOURCE_PHOTFIT_MODEL_VAR) ? PM_SOURCE_PHOTFIT_CONST : fitVarMode;
     32
    2633    int num = psphotFileruleCount(config, filerule);
    2734
     
    5057        psAssert (psf, "missing psf?");
    5158
    52         if (!psphotFitSourcesLinearReadout (recipe, readout, sources, psf, final)) {
     59        if (!psphotFitSourcesLinearReadout (recipe, readout, sources, psf, final, fitVarModePass1)) {
    5360            psError (PSPHOT_ERR_CONFIG, false, "failed to fit sources (linear) for %s entry %d", filerule, i);
    5461            return false;
    5562        }
     63
     64        // the MODEL_VAR weighting scheme requires knowledge of the model fluxes to generate the variance
     65        // after we have determined the initial set of fits, then we can generate the variance image and
     66        // re-run the fit against that variance.
     67        if (fitVarMode == PM_SOURCE_PHOTFIT_MODEL_VAR) {
     68            // generate the model variance image
     69            if (!psphotGenerateModelVariance (recipe, readout, sources, psf, final, fitVarMode)) {
     70                psError (PSPHOT_ERR_CONFIG, false, "failed to fit sources (linear) for %s entry %d", filerule, i);
     71                return false;
     72            }
     73
     74            // rerun fit with correct fitVarMode
     75            if (!psphotFitSourcesLinearReadout (recipe, readout, sources, psf, final, fitVarMode)) {
     76                psError (PSPHOT_ERR_CONFIG, false, "failed to fit sources (linear) for %s entry %d", filerule, i);
     77                return false;
     78            }
     79        }
    5680
    5781        psphotVisualShowResidualImage (readout, (num > 0));
     
    6286}
    6387
    64 bool psphotFitSourcesLinearReadout (psMetadata *recipe, pmReadout *readout, psArray *sources, pmPSF *psf, bool final) {
     88pmSourceFitVarMode psphotGetFitVarMode (psMetadata *recipe) {
     89
     90    bool status = false;
     91
     92    char *fitVarModeString = psMetadataLookupStr(&status, recipe, "LINEAR_FIT_VARIANCE_MODE");
     93    if (!status) {
     94        bool CONSTANT_PHOTOMETRIC_WEIGHTS = psMetadataLookupBool(&status, recipe, "CONSTANT_PHOTOMETRIC_WEIGHTS");
     95        if (!status) {
     96            psAbort("You must provide a value for LINEAR_FIT_VARIANCE_MODE or CONSTANT_PHOTOMETRIC_WEIGHTS");
     97        }
     98        pmsourceFitVarMode fitVarMode = CONSTANT_PHOTOMETRIC_WEIGHTS ? PM_SOURCE_PHOTFIT_CONST : PM_SOURCE_PHOTFIT_IMAGE_VAR;
     99        return fitVarMode;
     100    }
     101    if (!strcasecmp(fitVarModeString, "CONSTANT") || !strcasecmp(fitVarModeString, "CONST")) {
     102        return PM_SOURCE_PHOTFIT_CONST;
     103    }
     104    if (!strcasecmp(fitVarModeString, "IMAGE") || !strcasecmp(fitVarModeString, "IMAGE_VAR")) {
     105        return PM_SOURCE_PHOTFIT_IMAGE_VAR;
     106    }
     107    if (!strcasecmp(fitVarModeString, "MODEL") || !strcasecmp(fitVarModeString, "MODEL_VAR")) {
     108        return PM_SOURCE_PHOTFIT_MODEL_VAR;
     109    }
     110    psError (PSPHOT_ERR_CONFIG, false, "Invalid value for LINEAR_FIT_VARIANCE_MODE (%s)", fitVarModeString);
     111    return PM_SOURCE_PHOTFIT_MODEL_NONE;
     112}
     113
     114bool psphotFitSourcesLinearReadout (psMetadata *recipe, pmReadout *readout, psArray *sources, pmPSF *psf, bool final, pmSourceFitVarMode fitVarMode) {
    65115
    66116    bool status;
     
    99149    if (psRegionIsNaN (AnalysisRegion)) psAbort("analysis region mis-defined");
    100150
    101     pmSourceFitVarMode fitVarMode = PM_SOURCE_PHOTFIT_CONST;
    102     char *fitVarModeString = psMetadataLookupStr(&status, recipe, "LINEAR_FIT_VARIANCE_MODE");
    103     if (!status) {
    104         bool CONSTANT_PHOTOMETRIC_WEIGHTS = psMetadataLookupBool(&status, recipe, "CONSTANT_PHOTOMETRIC_WEIGHTS");
    105         if (!status) {
    106             psAbort("You must provide a value for LINEAR_FIT_VARIANCE_MODE or CONSTANT_PHOTOMETRIC_WEIGHTS");
    107         }
    108         fitVarMode = CONSTANT_PHOTOMETRIC_WEIGHTS ? PM_SOURCE_PHOTFIT_CONST : PM_SOURCE_PHOTFIT_IMAGE_VAR;
    109     } else {
    110         if (!strcasecmp(fitVarModeString, "CONSTANT") || !strcasecmp(fitVarModeString, "CONST")) {
    111             fitVarMode = PM_SOURCE_PHOTFIT_CONST;
    112             goto gotit;
    113         }
    114         if (!strcasecmp(fitVarModeString, "IMAGE") || !strcasecmp(fitVarModeString, "IMAGE_VAR")) {
    115             fitVarMode = PM_SOURCE_PHOTFIT_IMAGE_VAR;
    116             goto gotit;
    117         }
    118         if (!strcasecmp(fitVarModeString, "MODEL") || !strcasecmp(fitVarModeString, "MODEL_VAR")) {
    119             fitVarMode = PM_SOURCE_PHOTFIT_MODEL_VAR;
    120             goto gotit;
    121         }
    122         psError (PSPHOT_ERR_CONFIG, false, "Invalid value for LINEAR_FIT_VARIANCE_MODE (%s)", fitVarModeString);
    123         return false;
    124     }
    125 
    126 gotit:
    127151    int SKY_FIT_ORDER = psMetadataLookupS32(&status, recipe, "SKY_FIT_ORDER");
    128152    if (!status) {
     
    259283    psSparseBorder *border = psSparseBorderAlloc (sparse, nBorder);
    260284
     285    // if fitVarMode is MODEL_VAR, then we need to generate the model image variance
     286    // XXX we have two possibilities here:
     287
     288    // 1) do 2 passes, where in the first case we use the CONST weighting, and in the second
     289    // use the fitted model values to define the model
     290
     291    // 2) do a single pass, and use the model guess to define the model variance (but do I trust the Model Guess?)
     292
    261293    // fill out the sparse matrix elements and border elements (B)
    262294    // SRCi is the current source of interest
     
    266298
    267299        // diagonal elements of the sparse matrix (auto-cross-product)
    268         f = pmSourceModelDotModel (SRCi, SRCi, CONSTANT_PHOTOMETRIC_WEIGHTS, covarFactor, maskVal);
     300        f = pmSourceModelDotModel (SRCi, SRCi, fitVarMode, covarFactor, maskVal);
    269301        psSparseMatrixElement (sparse, i, i, f);
    270302
    271         // the formal error depends on the weighting scheme
    272         if (CONSTANT_PHOTOMETRIC_WEIGHTS) {
    273             float var = pmSourceModelDotModel (SRCi, SRCi, false, covarFactor, maskVal);
     303        // if we have used CONSTANT errors, then we need to calculate the value of the parameter error
     304        if (fitVarMode != PM_SOURCE_PHOTFIT_IMAGE_VAR) {
     305            float var = pmSourceModelDotModel (SRCi, SRCi, PM_SOURCE_PHOTFIT_IMAGE_VAR, covarFactor, maskVal);
    274306            errors->data.F32[i] = 1.0 / sqrt(var);
    275307        } else {
     
    279311
    280312        // find the image x model value
    281         f = pmSourceDataDotModel (SRCi, SRCi, CONSTANT_PHOTOMETRIC_WEIGHTS, covarFactor, maskVal);
     313        f = pmSourceDataDotModel (SRCi, SRCi, fitVarMode, covarFactor, maskVal);
    282314        psSparseVectorElement (sparse, i, f);
    283315
     
    285317        switch (SKY_FIT_ORDER) {
    286318          case 1:
    287             f = pmSourceModelWeight (SRCi, 1, CONSTANT_PHOTOMETRIC_WEIGHTS, covarFactor, maskVal);
     319            f = pmSourceModelWeight (SRCi, 1, fitVarMode, covarFactor, maskVal);
    288320            psSparseBorderElementB (border, i, 1, f);
    289             f = pmSourceModelWeight (SRCi, 2, CONSTANT_PHOTOMETRIC_WEIGHTS, covarFactor, maskVal);
     321            f = pmSourceModelWeight (SRCi, 2, fitVarMode, covarFactor, maskVal);
    290322            psSparseBorderElementB (border, i, 2, f);
    291323
    292324          case 0:
    293             f = pmSourceModelWeight (SRCi, 0, CONSTANT_PHOTOMETRIC_WEIGHTS, covarFactor, maskVal);
     325            f = pmSourceModelWeight (SRCi, 0, fitVarMode, covarFactor, maskVal);
    294326            psSparseBorderElementB (border, i, 0, f);
    295327            break;
     
    311343
    312344            // got an overlap; calculate cross-product and add to output array
    313             f = pmSourceModelDotModel (SRCi, SRCj, CONSTANT_PHOTOMETRIC_WEIGHTS, covarFactor, maskVal);
     345            f = pmSourceModelDotModel (SRCi, SRCj, fitVarMode, covarFactor, maskVal);
    314346            psSparseMatrixElement (sparse, j, i, f);
    315347        }
     
    507539    return true;
    508540}
     541
     542bool psphotGenerateModelVariance (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe) {
     543
     544    // create a model variance image
     545    psImage *modelVar = psImageCopy (NULL, readout->variance, PS_TYPE_F32);
     546
     547    // find the pmFPAfile for the readout (background model is saved on PSPHOT.BACKMDL regardless of 'filename')
     548    pmFPAfile *backModellFile = pmFPAfileSelectSingle(config->files, "PSPHOT.BACKMDL", index); // File of interest
     549    assert (backModelFile);
     550
     551    // find the background model readout from the correct location
     552    pmReadout *backModel = READOUT_OR_INTERNAL(view, backModelFile);
     553    psAssert (backModel, "this must exist");
     554
     555    // find the binning information
     556    psImageBinning *backBinning = psMetadataLookupPtr(&status, model->analysis, "PSPHOT.BACKGROUND.BINNING");
     557    assert (backBinning);
     558
     559    // linear interpolation to full-scale
     560    if (!psImageUnbin (modelVar, backModel->image, backBinning)) {
     561        psError (PSPHOT_ERR_PROG, true, "inconsistent sizes for unbinning");
     562        return false;
     563    }
     564
     565    // XXX for a test:
     566    psphotSaveImage (NULL, modelVar, "model.bck.fits");
     567
     568    // insert all of the source models
     569    for (int i = 0; i < sources->n; i++) {
     570
     571        // source of interest
     572        pmSource *source = sources->data[i];
     573
     574        // XXX which sources need to be injected?
     575        // skip non-astronomical objects (very likely defects)
     576        if (source->type == PM_SOURCE_TYPE_DEFECT) continue;
     577        if (source->type == PM_SOURCE_TYPE_SATURATED) continue;
     578     
     579        // do not include CRs in the full ensemble fit
     580        if (source->mode & PM_SOURCE_MODE_CR_LIMIT) continue;
     581       
     582        // do not include MOMENTS_FAILURES in the fit
     583        if (source->mode & PM_SOURCE_MODE_MOMENTS_FAILURE) continue;
     584
     585        // XXX need to identify the region appropriate for the source
     586
     587        // define the source->modelVar pixels (view on modelVar image)
     588        psAssert (!source->modelVar, "programming error : modelVar should be NULL here");
     589        source->modelVar = psImageSubset(readout->image, region);
     590
     591        // add the source model to the model variance image
     592        pmSourceAdd (source, PM_MODEL_OP_XXX, maskVal);
     593    }
     594
     595    // XXX add the readnoise to the image?  This is a bit problematic because I don't have the
     596    // per-cell information.  Alternatively, I could use the median of the variance image or
     597    // something equivalent to a background model to define the base level of the variance
     598
     599    // XXX for a test:
     600    psphotSaveImage (NULL, modelVar, "model.var.fits");
     601
     602    return true;
     603}
     604
     605bool psphotFreeModelVariance (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe) {
     606
     607    // find the binning information
     608    psImage *modelVar = psMetadataLookupPtr(&status, model->analysis, "PSPHOT.MODEL.VAR");
     609    assert (modelVar);
     610
     611    // clear modelVar pointers for all of the source models
     612    for (int i = 0; i < sources->n; i++) {
     613
     614        // source of interest
     615        pmSource *source = sources->data[i];
     616        psFree (source->modelVar);
     617    }
     618    psFree (modelVar);
     619
     620    return true;
     621}
Note: See TracChangeset for help on using the changeset viewer.