Changeset 29004 for trunk/psModules/src/objects/pmSourceFitModel.c
- Timestamp:
- Aug 20, 2010, 1:14:11 PM (16 years ago)
- Location:
- trunk/psModules
- Files:
-
- 2 edited
-
. (modified) (1 prop)
-
src/objects/pmSourceFitModel.c (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psModules
- Property svn:mergeinfo deleted
-
trunk/psModules/src/objects/pmSourceFitModel.c
r26070 r29004 23 23 #include "pmHDU.h" 24 24 #include "pmFPA.h" 25 26 #include "pmTrend2D.h" 27 #include "pmResiduals.h" 28 #include "pmGrowthCurve.h" 25 29 #include "pmSpan.h" 30 #include "pmFootprintSpans.h" 26 31 #include "pmFootprint.h" 27 32 #include "pmPeaks.h" 28 33 #include "pmMoments.h" 29 #include "pmGrowthCurve.h" 30 #include "pmResiduals.h" 31 #include "pmTrend2D.h" 32 #include "pmPSF.h" 34 #include "pmModelFuncs.h" 33 35 #include "pmModel.h" 36 #include "pmModelUtils.h" 37 #include "pmModelClass.h" 38 #include "pmSourceMasks.h" 39 #include "pmSourceExtendedPars.h" 40 #include "pmSourceDiffStats.h" 34 41 #include "pmSource.h" 35 #include "pmModelClass.h"36 42 #include "pmSourceFitModel.h" 37 43 38 // save as static values so they may be set externally 39 static psF32 PM_SOURCE_FIT_MODEL_NUM_ITERATIONS = 15; 40 static psF32 PM_SOURCE_FIT_MODEL_TOLERANCE = 0.1; 41 static psF32 PM_SOURCE_FIT_MODEL_WEIGHT = 1.0; 42 static bool PM_SOURCE_FIT_MODEL_PIX_WEIGHTS = true; 43 44 bool pmSourceFitModelInit (float nIter, float tol, float weight, bool poissonErrors) 44 void pmSourceFitOptionsFree(pmSourceFitOptions *opt) 45 45 { 46 47 PM_SOURCE_FIT_MODEL_NUM_ITERATIONS = nIter; 48 PM_SOURCE_FIT_MODEL_TOLERANCE = tol; 49 PM_SOURCE_FIT_MODEL_WEIGHT = weight; 50 PM_SOURCE_FIT_MODEL_PIX_WEIGHTS = poissonErrors; 51 52 return true; 46 return; 47 } 48 49 pmSourceFitOptions *pmSourceFitOptionsAlloc(void) { 50 51 pmSourceFitOptions *opt = (pmSourceFitOptions *) psAlloc(sizeof(pmSourceFitOptions)); 52 psMemSetDeallocator(opt, (psFreeFunc) pmSourceFitOptionsFree); 53 54 opt->mode = PM_SOURCE_FIT_PSF; 55 opt->nIter = 15; 56 opt->minTol = 0.01; 57 opt->maxTol = 1.00; 58 opt->weight = 1.00; 59 opt->maxChisqDOF = NAN; 60 opt->poissonErrors = true; 61 62 return opt; 53 63 } 54 64 55 65 bool pmSourceFitModel (pmSource *source, 56 66 pmModel *model, 57 pmSourceFit Mode mode,67 pmSourceFitOptions *options, 58 68 psImageMaskType maskVal) 59 69 { … … 76 86 psVector *yErr = psVectorAllocEmpty(nPix, PS_TYPE_F32); 77 87 88 // XXX for a test, skip the central pixel in the sersic fit 89 bool skipCenter = false && (model->type == pmModelClassGetType("PS_MODEL_SERSIC")); 90 float Xo = model->params->data.F32[PM_PAR_XPOS]; 91 float Yo = model->params->data.F32[PM_PAR_YPOS]; 92 78 93 // fill in the coordinate and value entries 79 94 nPix = 0; … … 95 110 // skip nan values in image 96 111 if (!isfinite(source->variance->data.F32[i][j])) { 97 fprintf (stderr, "impossible! %x vs %x\n", source->maskObj->data.PS_TYPE_IMAGE_MASK_DATA[i][j], maskVal); 98 continue; 99 } 100 101 psVector *coord = psVectorAlloc(2, PS_TYPE_F32); 112 fprintf (stderr, "impossible! %x vs %x\n", source->maskObj->data.PS_TYPE_IMAGE_MASK_DATA[i][j], maskVal); 113 continue; 114 } 102 115 103 116 // Convert i/j to image space: 104 117 // 0.5 PIX: the coordinate values must be in pixel coords, not index 105 coord->data.F32[0] = (psF32) (j + 0.5 + source->pixels->col0); 106 coord->data.F32[1] = (psF32) (i + 0.5 + source->pixels->row0); 118 float Xv = (psF32) (j + 0.5 + source->pixels->col0); 119 float Yv = (psF32) (i + 0.5 + source->pixels->row0); 120 121 // XXX possible skip of center pixel: 122 if (skipCenter) { 123 float r = hypot(Xv - Xo, Yv - Yo); 124 if (r < 0.75) { 125 continue; 126 } 127 } 128 129 psVector *coord = psVectorAlloc(2, PS_TYPE_F32); 130 coord->data.F32[0] = Xv; 131 coord->data.F32[1] = Yv; 107 132 x->data[nPix] = (psPtr *) coord; 108 133 y->data.F32[nPix] = source->pixels->data.F32[i][j]; … … 111 136 // as variance to avoid the bias from systematic errors here we would just use the 112 137 // source sky variance 113 if ( PM_SOURCE_FIT_MODEL_PIX_WEIGHTS) {138 if (options->poissonErrors) { 114 139 yErr->data.F32[nPix] = 1.0 / source->variance->data.F32[i][j]; 115 140 } else { 116 yErr->data.F32[nPix] = 1.0 / PM_SOURCE_FIT_MODEL_WEIGHT;141 yErr->data.F32[nPix] = 1.0 / options->weight; 117 142 } 118 143 nPix++; … … 133 158 // set parameter mask based on fitting mode 134 159 int nParams = 0; 135 switch ( mode) {136 case PM_SOURCE_FIT_NORM:160 switch (options->mode) { 161 case PM_SOURCE_FIT_NORM: 137 162 // NORM-only model fits only source normalization (Io) 138 163 nParams = 1; … … 140 165 constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[PM_PAR_I0] = 0; 141 166 break; 142 case PM_SOURCE_FIT_PSF:167 case PM_SOURCE_FIT_PSF: 143 168 // PSF model only fits x,y,Io 144 169 nParams = 3; … … 148 173 constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[PM_PAR_YPOS] = 0; 149 174 break; 150 case PM_SOURCE_FIT_EXT:175 case PM_SOURCE_FIT_EXT: 151 176 // EXT model fits all params (except sky) 152 177 nParams = params->n - 1; … … 154 179 constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[PM_PAR_SKY] = 1; 155 180 break; 156 default: 157 psAbort("invalid fitting mode"); 181 case PM_SOURCE_FIT_INDEX: 182 // PSF model only fits Io, index (PAR7) -- only Io for models with < 8 params 183 psVectorInit (constraint->paramMask, 1); 184 constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[PM_PAR_I0] = 0; 185 if (params->n == 7) { 186 nParams = 1; 187 } else { 188 nParams = 2; 189 constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[PM_PAR_7] = 0; 190 } 191 break; 192 case PM_SOURCE_FIT_NO_INDEX: 193 // PSF model only fits Io, index (PAR7) -- only Io for models with < 8 params 194 psVectorInit (constraint->paramMask, 0); 195 constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[PM_PAR_SKY] = 1; 196 if (params->n == 7) { 197 nParams = params->n - 1; 198 } else { 199 nParams = params->n - 2; 200 constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[PM_PAR_7] = 1; 201 } 202 break; 203 default: 204 psAbort("invalid fitting mode"); 158 205 } 159 206 // force the floating parameters to fall within the contraint ranges 160 207 for (int i = 0; i < params->n; i++) { 161 model->modelLimits (PS_MINIMIZE_PARAM_MIN, i, params->data.F32, NULL);162 model->modelLimits (PS_MINIMIZE_PARAM_MAX, i, params->data.F32, NULL);208 model->modelLimits (PS_MINIMIZE_PARAM_MIN, i, params->data.F32, NULL); 209 model->modelLimits (PS_MINIMIZE_PARAM_MAX, i, params->data.F32, NULL); 163 210 } 164 211 … … 173 220 } 174 221 175 psMinimization *myMin = psMinimizationAlloc ( PM_SOURCE_FIT_MODEL_NUM_ITERATIONS, PM_SOURCE_FIT_MODEL_TOLERANCE);222 psMinimization *myMin = psMinimizationAlloc (options->nIter, options->minTol, options->maxTol); 176 223 177 224 psImage *covar = psImageAlloc (params->n, params->n, PS_TYPE_F32); … … 194 241 model->flags |= PM_MODEL_STATUS_FITTED; 195 242 if (!fitStatus) model->flags |= PM_MODEL_STATUS_NONCONVERGE; 243 if (myMin->lastDelta > myMin->minTol) model->flags |= PM_MODEL_STATUS_WEAK_FIT; 196 244 197 245 // get the Gauss-Newton distance for fixed model parameters
Note:
See TracChangeset
for help on using the changeset viewer.
