- Timestamp:
- Jun 19, 2012, 5:24:19 PM (14 years ago)
- Location:
- branches/meh_branches/ppstack_test
- Files:
-
- 4 edited
-
. (modified) (1 prop)
-
psphot (modified) (1 prop)
-
psphot/src (modified) (1 prop)
-
psphot/src/psphotFitSourcesLinear.c (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/meh_branches/ppstack_test
- Property svn:mergeinfo changed
-
branches/meh_branches/ppstack_test/psphot
- Property svn:mergeinfo changed
/branches/eam_branches/ipp-20111122/psphot merged: 33070-33071,33086,33088,33094-33095,33612,33640 /branches/eam_branches/ipp-20120405/psphot (added) merged: 33946,33953
- Property svn:mergeinfo changed
-
branches/meh_branches/ppstack_test/psphot/src
- Property svn:mergeinfo changed
-
branches/meh_branches/ppstack_test/psphot/src/psphotFitSourcesLinear.c
r33415 r34041 12 12 static bool SetBorderMatrixElements (psSparseBorder *border, pmReadout *readout, psArray *sources, bool constant_weights, int SKY_FIT_ORDER, psImageMaskType markVal); 13 13 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 14 20 // for now, let's store the detections on the readout->analysis for each readout 15 21 bool psphotFitSourcesLinear (pmConfig *config, const pmFPAview *view, const char *filerule, bool final) … … 24 30 assert (recipe); 25 31 32 # if (HAVE_MODEL_VAR) 33 pmSourceFitVarMode fitVarMode = psphotGetFitVarMode (recipe); 34 if (!fitVarMode) { 35 psError (PSPHOT_ERR_CONFIG, true, "failed to get LINEAR_FIT_VARIANCE_MODE"); 36 return false; 37 } 38 // MODEL_VAR requires 2 passes -- in the first, we get the rough fluxes; in the second, we 39 // use the flux to define the model variance before fitting the objects. Other modes only 40 // do a single pass. 41 pmSourceFitVarMode fitVarModePass1 = (fitVarMode == PM_SOURCE_PHOTFIT_MODEL_VAR) ? PM_SOURCE_PHOTFIT_CONST : fitVarMode; 42 # endif 43 26 44 int num = psphotFileruleCount(config, filerule); 27 45 … … 50 68 psAssert (psf, "missing psf?"); 51 69 52 if (!psphotFitSourcesLinearReadout (recipe, readout, sources, psf, final)) { 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 { 53 76 psError (PSPHOT_ERR_CONFIG, false, "failed to fit sources (linear) for %s entry %d", filerule, i); 54 77 return false; 55 78 } 79 80 # if (HAVE_MODEL_VAR) 81 // the MODEL_VAR weighting scheme requires knowledge of the model fluxes to generate the variance 82 // after we have determined the initial set of fits, then we can generate the variance image and 83 // re-run the fit against that variance. 84 if (fitVarMode == PM_SOURCE_PHOTFIT_MODEL_VAR) { 85 // generate the model variance image & source pointers 86 if (!psphotGenerateModelVariance (config, view, file, i, recipe, readout, sources)) { 87 psError (PSPHOT_ERR_CONFIG, false, "failed to fit sources (linear) for %s entry %d", filerule, i); 88 return false; 89 } 90 91 // replace all sources (use TMPF_SUBTRACTED as test) 92 psphotReplaceAllSourcesReadout (config, view, filerule, i, recipe, false); 93 94 // rerun fit with correct fitVarMode 95 if (!psphotFitSourcesLinearReadout (recipe, readout, sources, psf, final, fitVarMode)) { 96 psError (PSPHOT_ERR_CONFIG, false, "failed to fit sources (linear) for %s entry %d", filerule, i); 97 return false; 98 } 99 100 // free the model variance image & source pointers 101 if (!psphotFreeModelVariance (readout, sources)) { 102 psError (PSPHOT_ERR_CONFIG, false, "failed to fit sources (linear) for %s entry %d", filerule, i); 103 return false; 104 } 105 } 106 # endif 56 107 57 108 psphotVisualShowResidualImage (readout, (num > 0)); … … 62 113 } 63 114 64 bool psphotFitSourcesLinearReadout (psMetadata *recipe, pmReadout *readout, psArray *sources, pmPSF *psf, bool final) { 65 115 # if (HAVE_MODEL_VAR) 116 // look up the fit variance mode from the recipe; older recipes do not have the value 117 // 'LINEAR_FIT_VARIANCE_MODE'; in those cases, look for 'CONSTANT_PHOTOMETRIC_WEIGHTS' as a boolean and 118 // set the value to either CONST or IMAGE_VAR 119 pmSourceFitVarMode psphotGetFitVarMode (psMetadata *recipe) { 120 121 bool status = false; 122 123 char *fitVarModeString = psMetadataLookupStr(&status, recipe, "LINEAR_FIT_VARIANCE_MODE"); 124 if (!status) { 125 bool CONSTANT_PHOTOMETRIC_WEIGHTS = psMetadataLookupBool(&status, recipe, "CONSTANT_PHOTOMETRIC_WEIGHTS"); 126 if (!status) { 127 psAbort("You must provide a value for LINEAR_FIT_VARIANCE_MODE or CONSTANT_PHOTOMETRIC_WEIGHTS"); 128 } 129 pmSourceFitVarMode fitVarMode = CONSTANT_PHOTOMETRIC_WEIGHTS ? PM_SOURCE_PHOTFIT_CONST : PM_SOURCE_PHOTFIT_IMAGE_VAR; 130 return fitVarMode; 131 } 132 if (!strcasecmp(fitVarModeString, "CONSTANT") || !strcasecmp(fitVarModeString, "CONST")) { 133 return PM_SOURCE_PHOTFIT_CONST; 134 } 135 if (!strcasecmp(fitVarModeString, "IMAGE") || !strcasecmp(fitVarModeString, "IMAGE_VAR")) { 136 return PM_SOURCE_PHOTFIT_IMAGE_VAR; 137 } 138 if (!strcasecmp(fitVarModeString, "MODEL") || !strcasecmp(fitVarModeString, "MODEL_VAR")) { 139 return PM_SOURCE_PHOTFIT_MODEL_VAR; 140 } 141 psError (PSPHOT_ERR_CONFIG, false, "Invalid value for LINEAR_FIT_VARIANCE_MODE (%s)", fitVarModeString); 142 return PM_SOURCE_PHOTFIT_NONE; 143 } 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 { 66 152 bool status; 67 153 float x; … … 99 185 if (psRegionIsNaN (AnalysisRegion)) psAbort("analysis region mis-defined"); 100 186 187 # if (!HAVE_MODEL_VAR) 101 188 bool CONSTANT_PHOTOMETRIC_WEIGHTS = 102 189 psMetadataLookupBool(&status, recipe, "CONSTANT_PHOTOMETRIC_WEIGHTS"); … … 104 191 psAbort("You must provide a value for the BOOL recipe CONSTANT_PHOTOMETRIC_WEIGHTS"); 105 192 } 193 # endif 106 194 int SKY_FIT_ORDER = psMetadataLookupS32(&status, recipe, "SKY_FIT_ORDER"); 107 195 if (!status) { … … 231 319 psSparseBorder *border = psSparseBorderAlloc (sparse, nBorder); 232 320 321 # if (HAVE_MODEL_VAR) 322 // if fitVarMode is MODEL_VAR, then we need to generate the model image variance 323 // XXX we have two possibilities here: 324 325 // 1) do 2 passes, where in the first case we use the CONST weighting, and in the second 326 // use the fitted model values to define the model 327 328 // 2) do a single pass, and use the model guess to define the model variance (but do I trust the Model Guess?) 329 # endif 330 233 331 // fill out the sparse matrix elements and border elements (B) 234 332 // SRCi is the current source of interest … … 238 336 239 337 // diagonal elements of the sparse matrix (auto-cross-product) 338 # if (HAVE_MODEL_VAR) 339 f = pmSourceModelDotModel (SRCi, SRCi, fitVarMode, covarFactor, maskVal); 340 # else 240 341 f = pmSourceModelDotModel (SRCi, SRCi, CONSTANT_PHOTOMETRIC_WEIGHTS, covarFactor, maskVal); 342 # endif 241 343 psSparseMatrixElement (sparse, i, i, f); 242 344 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) { 348 float var = pmSourceModelDotModel (SRCi, SRCi, PM_SOURCE_PHOTFIT_IMAGE_VAR, covarFactor, maskVal); 349 errors->data.F32[i] = 1.0 / sqrt(var); 350 } else { 351 errors->data.F32[i] = 1.0 / sqrt(f); 352 } 353 # else 243 354 // the formal error depends on the weighting scheme 244 355 if (CONSTANT_PHOTOMETRIC_WEIGHTS) { … … 248 359 errors->data.F32[i] = 1.0 / sqrt(f); 249 360 } 250 361 # endif 251 362 252 363 // find the image x model value 364 # if (HAVE_MODEL_VAR) 365 f = pmSourceDataDotModel (SRCi, SRCi, fitVarMode, covarFactor, maskVal); 366 # else 253 367 f = pmSourceDataDotModel (SRCi, SRCi, CONSTANT_PHOTOMETRIC_WEIGHTS, covarFactor, maskVal); 368 # endif 254 369 psSparseVectorElement (sparse, i, f); 255 370 … … 257 372 switch (SKY_FIT_ORDER) { 258 373 case 1: 374 # if (HAVE_MODEL_VAR) 375 f = pmSourceModelWeight (SRCi, 1, fitVarMode, covarFactor, maskVal); 376 psSparseBorderElementB (border, i, 1, f); 377 f = pmSourceModelWeight (SRCi, 2, fitVarMode, covarFactor, maskVal); 378 psSparseBorderElementB (border, i, 2, f); 379 # else 259 380 f = pmSourceModelWeight (SRCi, 1, CONSTANT_PHOTOMETRIC_WEIGHTS, covarFactor, maskVal); 260 381 psSparseBorderElementB (border, i, 1, f); 261 382 f = pmSourceModelWeight (SRCi, 2, CONSTANT_PHOTOMETRIC_WEIGHTS, covarFactor, maskVal); 262 383 psSparseBorderElementB (border, i, 2, f); 384 # endif 263 385 264 386 case 0: 387 # if (HAVE_MODEL_VAR) 388 f = pmSourceModelWeight (SRCi, 0, fitVarMode, covarFactor, maskVal); 389 # else 265 390 f = pmSourceModelWeight (SRCi, 0, CONSTANT_PHOTOMETRIC_WEIGHTS, covarFactor, maskVal); 391 # endif 266 392 psSparseBorderElementB (border, i, 0, f); 267 393 break; … … 283 409 284 410 // got an overlap; calculate cross-product and add to output array 411 # if (HAVE_MODEL_VAR) 412 f = pmSourceModelDotModel (SRCi, SRCj, fitVarMode, covarFactor, maskVal); 413 # else 285 414 f = pmSourceModelDotModel (SRCi, SRCj, CONSTANT_PHOTOMETRIC_WEIGHTS, covarFactor, maskVal); 415 # endif 286 416 psSparseMatrixElement (sparse, j, i, f); 287 417 } … … 321 451 322 452 // set the sky, sky_x, sky_y components of border matrix 453 # if (HAVE_MODEL_VAR) 454 SetBorderMatrixElements (border, readout, fitSources, (fitVarMode == PM_SOURCE_PHOTFIT_CONST), SKY_FIT_ORDER, markVal); 455 # else 323 456 SetBorderMatrixElements (border, readout, fitSources, CONSTANT_PHOTOMETRIC_WEIGHTS, SKY_FIT_ORDER, markVal); 457 # endif 324 458 325 459 psSparseConstraint constraint; … … 479 613 return true; 480 614 } 615 616 # if (HAVE_MODEL_VAR) 617 bool psphotModelBackgroundReadout(psImage *model, // Model image 618 psImage *modelStdev, // Model stdev image 619 psMetadata *analysis, // Analysis metadata for outputs 620 pmReadout *readout, // Readout for which to generate a background model 621 psImageBinning *binning, // Binning parameters 622 const pmConfig *config,// Configuration 623 bool useVarianceImage 624 ); 625 626 bool psphotGenerateModelVariance (pmConfig *config, const pmFPAview *view, pmFPAfile *file, int index, psMetadata *recipe, pmReadout *readout, psArray *sources) { 627 628 bool status = false; 629 psRegion fullRegion = psRegionSet (0, 0, 0, 0); 630 631 // bit-masks to test for good/bad pixels 632 psImageMaskType maskVal = psMetadataLookupImageMask(&status, recipe, "MASK.PSPHOT"); 633 assert (maskVal); 634 635 // bit-mask to mark pixels not used in analysis 636 psImageMaskType markVal = psMetadataLookupImageMask(&status, recipe, "MARK.PSPHOT"); 637 assert (markVal); 638 639 // maskVal is used to test for rejected pixels, and must include markVal 640 maskVal |= markVal; 641 642 // create a model variance image 643 psImage *modelVar = psImageCopy (NULL, readout->variance, PS_TYPE_F32); 644 645 // find the binning information 646 psImageBinning *backBinning = psphotBackgroundBinning (modelVar, config); 647 assert (backBinning); 648 649 psImage *varModel = psImageAlloc(backBinning->nXruff, backBinning->nYruff, PS_TYPE_F32); // Background model 650 psImage *varModelStdev = psImageAlloc(backBinning->nXruff, backBinning->nYruff, PS_TYPE_F32); // Background model 651 652 if (!psphotModelBackgroundReadout(varModel, varModelStdev, NULL, readout, backBinning, config, true)) { 653 psError(PS_ERR_UNKNOWN, false, "Unable to generate background model"); 654 psFree (varModel); 655 psFree (varModelStdev); 656 return false; 657 } 658 659 // linear interpolation to full-scale 660 if (!psImageUnbin (modelVar, varModel, backBinning)) { 661 psError (PSPHOT_ERR_PROG, true, "inconsistent sizes for unbinning"); 662 psFree (varModel); 663 psFree (varModelStdev); 664 return false; 665 } 666 667 psFree (varModel); 668 psFree (varModelStdev); 669 670 // XXX for a test: 671 psphotSaveImage (NULL, modelVar, "model.bck.fits"); 672 673 // insert all of the source models 674 for (int i = 0; i < sources->n; i++) { 675 676 // source of interest 677 pmSource *source = sources->data[i]; 678 679 // skip sources which were not fitted already 680 if (!(source->mode & PM_SOURCE_MODE_LINEAR_FIT)) continue; 681 682 // pixel region appropriate for the source 683 psRegion region = psRegionForImage (source->pixels, fullRegion); 684 685 // define the source->modelVar pixels (view on modelVar image) 686 psAssert (!source->modelVar, "programming error : modelVar should be NULL here"); 687 psAssert (source->modelFlux, "programming error : modelFlux should not be NULL here"); 688 psAssert (source->modelFlux->data.F32, "programming error : modelFlux should not be NULL here"); 689 source->modelVar = psImageSubset(modelVar, region); 690 691 // add the source model to the model variance image 692 pmSourceAdd (source, PM_MODEL_OP_MODELVAR, maskVal); 693 } 694 695 // XXX for a test: 696 psphotSaveImage (NULL, modelVar, "model.var.fits"); 697 psphotSaveImage (NULL, readout->variance, "image.var.fits"); 698 699 // we save the model variance for future reference 700 psMetadataAddImage(readout->analysis, PS_LIST_TAIL, "PSPHOT.MODEL.VAR", PS_META_REPLACE, "model variance", modelVar); 701 psFree (modelVar); 702 703 return true; 704 } 705 706 bool psphotFreeModelVariance (pmReadout *readout, psArray *sources) { 707 708 bool status = false; 709 710 // find the binning information 711 psImage *modelVar = psMetadataLookupPtr(&status, readout->analysis, "PSPHOT.MODEL.VAR"); 712 assert (modelVar); 713 714 psMetadataRemoveKey (readout->analysis, "PSPHOT.MODEL.VAR"); 715 716 // clear modelVar pointers for all of the source models 717 for (int i = 0; i < sources->n; i++) { 718 719 // source of interest 720 pmSource *source = sources->data[i]; 721 psFree (source->modelVar); 722 } 723 724 return true; 725 } 726 # endif
Note:
See TracChangeset
for help on using the changeset viewer.
