- Timestamp:
- Jul 20, 2010, 7:21:53 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branches/ipp-20100621/psModules/src/objects/pmPCM_MinimizeChisq.c
r28687 r28692 1 # include "psphotInternal.h" 2 # define SAVE_IMAGES 0 3 4 bool psphotModelWithPSF_LMM ( 1 /* @file pmPCM_MinimizeChisq.c 2 * structures and functions to support PSF-convolved model fitting 3 * 4 * @author EAM, IfA 5 * 6 * @version $Revision: 1.29 $ 7 * @date $Date: 2009-02-16 22:30:50 $ 8 * Copyright 2010 Institute for Astronomy, University of Hawaii 9 */ 10 11 #ifdef HAVE_CONFIG_H 12 #include <config.h> 13 #endif 14 15 #include <stdio.h> 16 #include <math.h> 17 #include <string.h> 18 #include <strings.h> 19 #include <pslib.h> 20 #include "pmHDU.h" 21 #include "pmFPA.h" 22 #include "pmFPAMaskWeight.h" 23 24 #include "pmTrend2D.h" 25 #include "pmResiduals.h" 26 #include "pmGrowthCurve.h" 27 #include "pmSpan.h" 28 #include "pmFootprintSpans.h" 29 #include "pmFootprint.h" 30 #include "pmPeaks.h" 31 #include "pmMoments.h" 32 #include "pmModelFuncs.h" 33 #include "pmModel.h" 34 #include "pmModelUtils.h" 35 #include "pmModelClass.h" 36 #include "pmSourceMasks.h" 37 #include "pmSourceExtendedPars.h" 38 #include "pmSourceDiffStats.h" 39 #include "pmSource.h" 40 #include "pmSourceFitModel.h" 41 #include "pmPCMdata.h" 42 43 # define FACILITY "psModules.objects" 44 45 bool pmPCM_MinimizeChisq ( 5 46 psMinimization *min, 6 47 psImage *covar, 7 48 psVector *params, 8 psMinConstraint *constraint,9 49 pmSource *source, 10 const psKernel *psf, 11 psMinimizeLMChi2Func func) 50 pmPCMdata *pcm) 12 51 { 13 psTrace( "psphot", 3, "---- begin ----\n");52 psTrace(FACILITY, 3, "---- begin ----\n"); 14 53 PS_ASSERT_PTR_NON_NULL(min, false); 15 54 PS_ASSERT_VECTOR_NON_NULL(params, false); 16 55 PS_ASSERT_VECTOR_NON_EMPTY(params, false); 17 56 PS_ASSERT_VECTOR_TYPE(params, PS_TYPE_F32, false); 18 psVector *paramMask = NULL;19 if (constraint != NULL) {20 paramMask = constraint->paramMask;21 if (paramMask != NULL) {22 PS_ASSERT_VECTOR_TYPE(paramMask, PS_TYPE_VECTOR_MASK, false);23 PS_ASSERT_VECTORS_SIZE_EQUAL(params, paramMask, false);24 }25 }26 PS_ASSERT_PTR_NON_NULL(func, false);27 57 PS_ASSERT_PTR_NON_NULL(source, false); 28 29 psMinimizeLMLimitFunc checkLimits = NULL; 30 if (constraint) { 31 checkLimits = constraint->checkLimits; 32 } 58 PS_ASSERT_PTR_NON_NULL(pcm, false); 59 PS_ASSERT_VECTOR_TYPE(pcm->constraint->paramMask, PS_TYPE_VECTOR_MASK, false); 60 PS_ASSERT_VECTORS_SIZE_EQUAL(params, pcm->constraint->paramMask, false); 61 62 psVector *paramMask = pcm->constraint->paramMask; 63 64 psMinimizeLMLimitFunc checkLimits = pcm->constraint->checkLimits; 33 65 34 66 // this function has test values and current values for several things … … 54 86 psF32 dLinear = 0.0; 55 87 56 // generate PCM data storage structure57 pmPCMData *pcm = pmPCMDataAlloc (params, paramMask, source);58 59 88 // calculate initial alpha and beta, set chisq (min->value) 60 min->value = p sphotModelWithPSF_SetABX(alpha, beta, params, paramMask, pcm, source, psf, func);89 min->value = pmPCM_SetABX(alpha, beta, params, paramMask, pcm, source); 61 90 if (isnan(min->value)) { 62 91 min->iter = min->maxIter; … … 64 93 } 65 94 // dump some useful info if trace is defined 66 if (psTraceGetLevel( "psphot") >= 6) {95 if (psTraceGetLevel(FACILITY) >= 6) { 67 96 p_psImagePrint(psTraceGetDestination(), alpha, "alpha guess (0)"); 68 97 p_psVectorPrint(psTraceGetDestination(), beta, "beta guess (0)"); 69 98 } 70 if (psTraceGetLevel( "psphot") >= 5) {99 if (psTraceGetLevel(FACILITY) >= 5) { 71 100 p_psVectorPrint(psTraceGetDestination(), params, "params guess (0)"); 72 101 } 73 102 74 103 // iterate until the tolerance is reached, or give up 75 while ((min->iter < min->maxIter) && ((min->lastDelta > min->minTol) || !isfinite(min->lastDelta))) {76 psTrace("psphot", 5, "Iteration number %d. (max iterations is %d).\n", min->iter, min->maxIter);77 psTrace("ps phot", 5, "Last delta is %f. Min->minTol is %f.\n", min->lastDelta, min->minTol);78 104 bool done = (min->iter >= min->maxIter); 105 while (!done) { 106 psTrace("psModules.objects", 5, "Iteration number %d. (max iterations is %d).\n", min->iter, min->maxIter); 107 psTrace("psModules.objects", 5, "Last delta is %f. stop if < %f, accept if < %f\n", min->lastDelta, min->minTol, min->maxTol); 79 108 80 109 // set a new guess for Alpha, Beta, Params 81 110 if (!psMinLM_GuessABP(Alpha, Beta, Params, alpha, beta, params, paramMask, checkLimits, lambda, &dLinear)) { 82 111 min->iter ++; 112 if (min->iter >= min->maxIter) break; 83 113 lambda *= 10.0; 84 114 continue; … … 86 116 87 117 // dump some useful info if trace is defined 88 if (psTraceGetLevel( "psphot") >= 6) {118 if (psTraceGetLevel(FACILITY) >= 6) { 89 119 p_psImagePrint(psTraceGetDestination(), Alpha, "Alpha guess (1)"); 90 120 p_psVectorPrint(psTraceGetDestination(), Beta, "Beta guess (1)"); 91 121 p_psVectorPrint(psTraceGetDestination(), beta, "beta current (1)"); 92 122 } 93 if (psTraceGetLevel( "psphot") >= 5) {123 if (psTraceGetLevel(FACILITY) >= 5) { 94 124 p_psVectorPrint(psTraceGetDestination(), Params, "params guess (1)"); 95 125 } 96 126 97 127 // calculate Chisq for new guess, update Alpha & Beta 98 Chisq = p sphotModelWithPSF_SetABX(Alpha, Beta, Params, paramMask, pcm, source, psf, func);128 Chisq = pmPCM_SetABX(Alpha, Beta, Params, paramMask, pcm, source); 99 129 if (isnan(Chisq)) { 100 130 min->iter ++; 131 if (min->iter >= min->maxIter) break; 101 132 lambda *= 10.0; 102 133 continue; … … 109 140 psF32 rho = (min->value - Chisq) / dLinear; 110 141 111 psTrace("psphot", 5, "last chisq: %f, new chisq %f, delta: %f, rho: %f\n", min->value, 112 Chisq, min->lastDelta, rho); 142 psTrace(FACILITY, 5, "last chisq: %f, new chisq %f, delta: %f, rho: %f\n", min->value, Chisq, min->lastDelta, rho); 113 143 114 144 // dump some useful info if trace is defined 115 if (psTraceGetLevel( "psphot") >= 6) {145 if (psTraceGetLevel(FACILITY) >= 6) { 116 146 p_psImagePrint(psTraceGetDestination(), Alpha, "alpha guess (2)"); 117 147 p_psVectorPrint(psTraceGetDestination(), Beta, "beta guess (2)"); … … 119 149 120 150 /* if (Chisq < min->value) { */ 121 if (rho > 0.0) {151 if (rho >= -1e-6) { 122 152 min->lastDelta = (min->value - Chisq) / (source->pixels->numCols*source->pixels->numRows - params->n); 123 153 min->value = Chisq; … … 129 159 // save the new convolved model image 130 160 psFree (source->modelFlux); 131 source->modelFlux = pmPCM DataSaveImage(pcm);161 source->modelFlux = pmPCMdataSaveImage(pcm); 132 162 } else { 133 163 lambda *= 10.0; 134 164 } 135 165 min->iter++; 136 } 137 psTrace("psphot", 5, "chisq: %f, last delta: %f, Niter: %d\n", min->value, min->lastDelta, min->iter); 166 167 done = (min->iter >= min->maxIter); 168 169 // check for convergence: 170 float chisqDOF = Chisq / pcm->nDOF; 171 if (!isfinite(min->maxChisqDOF) || ((chisqDOF < min->maxChisqDOF) && isfinite(min->lastDelta))) { 172 done |= (min->lastDelta < min->minTol); 173 } 174 } 175 psTrace(FACILITY, 5, "chisq: %f, last delta: %f, Niter: %d\n", min->value, min->lastDelta, min->iter); 138 176 139 177 // construct & return the covariance matrix (if requested) 140 178 if (covar != NULL) { 141 179 if (!psMinLM_GuessABP(Alpha, Beta, Params, alpha, beta, params, paramMask, NULL, 0.0, NULL)) { 142 psTrace ( "psphot", 5, "failure to calculate covariance matrix\n");180 psTrace (FACILITY, 5, "failure to calculate covariance matrix\n"); 143 181 } 144 182 // set covar values which are not masked … … 164 202 psFree(Beta); 165 203 psFree(Params); 166 psFree(pcm);167 204 168 205 // if the last improvement was at least as good as maxTol, accept the fit: 169 206 if (min->lastDelta <= min->maxTol) { 170 psTrace( "psphot", 6, "---- end (true) ----\n");207 psTrace(FACILITY, 6, "---- end (true) ----\n"); 171 208 return(true); 172 209 } 173 psTrace( "psphot", 6, "---- end (false) ----\n");210 psTrace(FACILITY, 6, "---- end (false) ----\n"); 174 211 return(false); 175 212 } 176 213 177 psF32 p sphotModelWithPSF_SetABX(214 psF32 pmPCM_SetABX( 178 215 psImage *alpha, 179 216 psVector *beta, 180 217 const psVector *params, 181 218 const psVector *paramMask, 182 pmPCMData *pcm, 183 const pmSource *source, 184 const psKernel *psf, 185 psMinimizeLMChi2Func func) 219 pmPCMdata *pcm, 220 const pmSource *source) 186 221 { 187 222 // XXX: Check vector sizes. … … 208 243 psVector *coord = psVectorAlloc(2, PS_TYPE_F32); 209 244 210 psImageInit (pcm->model , 0.0);245 psImageInit (pcm->modelFlux, 0.0); 211 246 for (int n = 0; n < params->n; n++) { 212 if (!pcm->dmodels ->data[n]) continue;213 psImageInit (pcm->dmodels ->data[n], 0.0);247 if (!pcm->dmodelsFlux->data[n]) continue; 248 psImageInit (pcm->dmodelsFlux->data[n], 0.0); 214 249 } 215 250 … … 243 278 coord->data.F32[1] = (psF32) (i + source->pixels->row0); 244 279 245 pcm->model ->data.F32[i][j] = func (deriv, params, coord);280 pcm->modelFlux->data.F32[i][j] = pcm->modelConv->modelFunc (deriv, params, coord); 246 281 247 282 for (int n = 0; n < params->n; n++) { 248 283 if ((paramMask != NULL) && (paramMask->data.PS_TYPE_VECTOR_MASK_DATA[n])) { continue; } 249 psImage *dmodel = pcm->dmodels ->data[n];284 psImage *dmodel = pcm->dmodelsFlux->data[n]; 250 285 dmodel->data.F32[i][j] = deriv->data.F32[n]; 251 286 } … … 256 291 257 292 // convolve model and dmodel arrays with PSF 258 psImageConvolveDirect (pcm->modelConv , pcm->model,psf);259 for (int n = 0; n < pcm->dmodels ->n; n++) {260 if (pcm->dmodels ->data[n] == NULL) continue;261 psImage *dmodel = pcm->dmodels ->data[n];262 psImage *dmodelConv = pcm->dmodelsConv ->data[n];263 psImageConvolveDirect (dmodelConv, dmodel, p sf);293 psImageConvolveDirect (pcm->modelConvFlux, pcm->modelFlux, pcm->psf); 294 for (int n = 0; n < pcm->dmodelsFlux->n; n++) { 295 if (pcm->dmodelsFlux->data[n] == NULL) continue; 296 psImage *dmodel = pcm->dmodelsFlux->data[n]; 297 psImage *dmodelConv = pcm->dmodelsConvFlux->data[n]; 298 psImageConvolveDirect (dmodelConv, dmodel, pcm->psf); 264 299 } 265 300 266 301 // XXX TEST : SAVE IMAGES 267 302 # if (SAVE_IMAGES) 268 psphotSaveImage (NULL, p sf->image, "psf.fits");269 psphotSaveImage (NULL, pcm->model , "model.fits");270 psphotSaveImage (NULL, pcm->modelConv , "modelConv.fits");303 psphotSaveImage (NULL, pcm->psf->image, "psf.fits"); 304 psphotSaveImage (NULL, pcm->modelFlux, "model.fits"); 305 psphotSaveImage (NULL, pcm->modelConvFlux, "modelConv.fits"); 271 306 psphotSaveImage (NULL, source->pixels, "obj.fits"); 272 307 psphotSaveImage (NULL, source->maskObj, "mask.fits"); … … 297 332 } 298 333 299 float ymodel = pcm->modelConv ->data.F32[i][j];334 float ymodel = pcm->modelConvFlux->data.F32[i][j]; 300 335 float yweight = 1.0 / source->variance->data.F32[i][j]; 301 336 float delta = ymodel - source->pixels->data.F32[i][j]; … … 309 344 for (int n1 = 0, N1 = 0; n1 < params->n; n1++) { 310 345 if ((paramMask != NULL) && (paramMask->data.PS_TYPE_VECTOR_MASK_DATA[n1])) continue; 311 psImage *dmodel = pcm->dmodelsConv ->data[n1];346 psImage *dmodel = pcm->dmodelsConvFlux->data[n1]; 312 347 float weight = dmodel->data.F32[i][j] * yweight; 313 348 for (int n2 = 0, N2 = 0; n2 <= n1; n2++) { 314 349 if ((paramMask != NULL) && (paramMask->data.PS_TYPE_VECTOR_MASK_DATA[n2])) continue; 315 dmodel = pcm->dmodelsConv ->data[n2];350 dmodel = pcm->dmodelsConvFlux->data[n2]; 316 351 alpha->data.F32[N1][N2] += weight * dmodel->data.F32[i][j]; 317 352 N2++;
Note:
See TracChangeset
for help on using the changeset viewer.
