IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jul 20, 2010, 7:21:53 PM (16 years ago)
Author:
eugene
Message:

update PCM fitting (not quite ready)

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
     45bool pmPCM_MinimizeChisq (
    546    psMinimization *min,
    647    psImage *covar,
    748    psVector *params,
    8     psMinConstraint *constraint,
    949    pmSource *source,
    10     const psKernel *psf,
    11     psMinimizeLMChi2Func func)
     50    pmPCMdata *pcm)
    1251{
    13     psTrace("psphot", 3, "---- begin ----\n");
     52    psTrace(FACILITY, 3, "---- begin ----\n");
    1453    PS_ASSERT_PTR_NON_NULL(min, false);
    1554    PS_ASSERT_VECTOR_NON_NULL(params, false);
    1655    PS_ASSERT_VECTOR_NON_EMPTY(params, false);
    1756    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);
    2757    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;
    3365
    3466    // this function has test values and current values for several things
     
    5486    psF32 dLinear = 0.0;
    5587
    56     // generate PCM data storage structure
    57     pmPCMData *pcm = pmPCMDataAlloc (params, paramMask, source);
    58 
    5988    // calculate initial alpha and beta, set chisq (min->value)
    60     min->value = psphotModelWithPSF_SetABX(alpha, beta, params, paramMask, pcm, source, psf, func);
     89    min->value = pmPCM_SetABX(alpha, beta, params, paramMask, pcm, source);
    6190    if (isnan(min->value)) {
    6291        min->iter = min->maxIter;
     
    6493    }
    6594    // dump some useful info if trace is defined
    66     if (psTraceGetLevel("psphot") >= 6) {
     95    if (psTraceGetLevel(FACILITY) >= 6) {
    6796        p_psImagePrint(psTraceGetDestination(), alpha, "alpha guess (0)");
    6897        p_psVectorPrint(psTraceGetDestination(), beta, "beta guess (0)");
    6998    }
    70     if (psTraceGetLevel("psphot") >= 5) {
     99    if (psTraceGetLevel(FACILITY) >= 5) {
    71100        p_psVectorPrint(psTraceGetDestination(), params, "params guess (0)");
    72101    }
    73102
    74103    // 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("psphot", 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);
    79108
    80109        // set a new guess for Alpha, Beta, Params
    81110        if (!psMinLM_GuessABP(Alpha, Beta, Params, alpha, beta, params, paramMask, checkLimits, lambda, &dLinear)) {
    82111            min->iter ++;
     112            if (min->iter >=  min->maxIter) break;
    83113            lambda *= 10.0;
    84114            continue;
     
    86116
    87117        // dump some useful info if trace is defined
    88         if (psTraceGetLevel("psphot") >= 6) {
     118        if (psTraceGetLevel(FACILITY) >= 6) {
    89119            p_psImagePrint(psTraceGetDestination(), Alpha, "Alpha guess (1)");
    90120            p_psVectorPrint(psTraceGetDestination(), Beta, "Beta guess (1)");
    91121            p_psVectorPrint(psTraceGetDestination(), beta, "beta current (1)");
    92122        }
    93         if (psTraceGetLevel("psphot") >= 5) {
     123        if (psTraceGetLevel(FACILITY) >= 5) {
    94124            p_psVectorPrint(psTraceGetDestination(), Params, "params guess (1)");
    95125        }
    96126
    97127        // calculate Chisq for new guess, update Alpha & Beta
    98         Chisq = psphotModelWithPSF_SetABX(Alpha, Beta, Params, paramMask, pcm, source, psf, func);
     128        Chisq = pmPCM_SetABX(Alpha, Beta, Params, paramMask, pcm, source);
    99129        if (isnan(Chisq)) {
    100130            min->iter ++;
     131            if (min->iter >=  min->maxIter) break;
    101132            lambda *= 10.0;
    102133            continue;
     
    109140        psF32 rho = (min->value - Chisq) / dLinear;
    110141
    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);
    113143
    114144        // dump some useful info if trace is defined
    115         if (psTraceGetLevel("psphot") >= 6) {
     145        if (psTraceGetLevel(FACILITY) >= 6) {
    116146            p_psImagePrint(psTraceGetDestination(), Alpha, "alpha guess (2)");
    117147            p_psVectorPrint(psTraceGetDestination(), Beta, "beta guess (2)");
     
    119149
    120150        /* if (Chisq < min->value) {  */
    121         if (rho > 0.0) {
     151        if (rho >= -1e-6) {
    122152            min->lastDelta = (min->value - Chisq) / (source->pixels->numCols*source->pixels->numRows - params->n);
    123153            min->value = Chisq;
     
    129159            // save the new convolved model image
    130160            psFree (source->modelFlux);
    131             source->modelFlux = pmPCMDataSaveImage(pcm);
     161            source->modelFlux = pmPCMdataSaveImage(pcm);
    132162        } else {
    133163            lambda *= 10.0;
    134164        }
    135165        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);
    138176
    139177    // construct & return the covariance matrix (if requested)
    140178    if (covar != NULL) {
    141179        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");
    143181        }
    144182        // set covar values which are not masked
     
    164202    psFree(Beta);
    165203    psFree(Params);
    166     psFree(pcm);
    167204
    168205    // if the last improvement was at least as good as maxTol, accept the fit:
    169206    if (min->lastDelta <= min->maxTol) {
    170         psTrace("psphot", 6, "---- end (true) ----\n");
     207        psTrace(FACILITY, 6, "---- end (true) ----\n");
    171208        return(true);
    172209    }
    173     psTrace("psphot", 6, "---- end (false) ----\n");
     210    psTrace(FACILITY, 6, "---- end (false) ----\n");
    174211    return(false);
    175212}
    176213
    177 psF32 psphotModelWithPSF_SetABX(
     214psF32 pmPCM_SetABX(
    178215    psImage  *alpha,
    179216    psVector *beta,
    180217    const psVector *params,
    181218    const psVector *paramMask,
    182     pmPCMData *pcm,
    183     const pmSource *source,
    184     const psKernel *psf,
    185     psMinimizeLMChi2Func func)
     219    pmPCMdata *pcm,
     220    const pmSource *source)
    186221{
    187222    // XXX: Check vector sizes.
     
    208243    psVector *coord = psVectorAlloc(2, PS_TYPE_F32);
    209244
    210     psImageInit (pcm->model, 0.0);
     245    psImageInit (pcm->modelFlux, 0.0);
    211246    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);
    214249    }
    215250
     
    243278            coord->data.F32[1] = (psF32) (i + source->pixels->row0);
    244279
    245             pcm->model->data.F32[i][j] = func (deriv, params, coord);
     280            pcm->modelFlux->data.F32[i][j] = pcm->modelConv->modelFunc (deriv, params, coord);
    246281
    247282            for (int n = 0; n < params->n; n++) {
    248283                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];
    250285                dmodel->data.F32[i][j] = deriv->data.F32[n];
    251286            }
     
    256291
    257292    // 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, psf);
     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);
    264299    }
    265300
    266301    // XXX TEST : SAVE IMAGES
    267302# if (SAVE_IMAGES)
    268     psphotSaveImage (NULL, psf->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");
    271306    psphotSaveImage (NULL, source->pixels, "obj.fits");
    272307    psphotSaveImage (NULL, source->maskObj, "mask.fits");
     
    297332            }
    298333
    299             float ymodel  = pcm->modelConv->data.F32[i][j];
     334            float ymodel  = pcm->modelConvFlux->data.F32[i][j];
    300335            float yweight = 1.0 / source->variance->data.F32[i][j];
    301336            float delta = ymodel - source->pixels->data.F32[i][j];
     
    309344            for (int n1 = 0, N1 = 0; n1 < params->n; n1++) {
    310345                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];
    312347                float weight = dmodel->data.F32[i][j] * yweight;
    313348                for (int n2 = 0, N2 = 0; n2 <= n1; n2++) {
    314349                    if ((paramMask != NULL) && (paramMask->data.PS_TYPE_VECTOR_MASK_DATA[n2])) continue;
    315                     dmodel = pcm->dmodelsConv->data[n2];
     350                    dmodel = pcm->dmodelsConvFlux->data[n2];
    316351                    alpha->data.F32[N1][N2] += weight * dmodel->data.F32[i][j];
    317352                    N2++;
Note: See TracChangeset for help on using the changeset viewer.