IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Aug 26, 2010, 9:18:39 AM (16 years ago)
Author:
Serge CHASTEL
Message:

Merging trunk in branch

Location:
branches/sc_branches/trunkTest
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/sc_branches/trunkTest

  • branches/sc_branches/trunkTest/psModules

    • Property svn:mergeinfo deleted
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourceFitModel.c

    r26070 r29060  
    2323#include "pmHDU.h"
    2424#include "pmFPA.h"
     25
     26#include "pmTrend2D.h"
     27#include "pmResiduals.h"
     28#include "pmGrowthCurve.h"
    2529#include "pmSpan.h"
     30#include "pmFootprintSpans.h"
    2631#include "pmFootprint.h"
    2732#include "pmPeaks.h"
    2833#include "pmMoments.h"
    29 #include "pmGrowthCurve.h"
    30 #include "pmResiduals.h"
    31 #include "pmTrend2D.h"
    32 #include "pmPSF.h"
     34#include "pmModelFuncs.h"
    3335#include "pmModel.h"
     36#include "pmModelUtils.h"
     37#include "pmModelClass.h"
     38#include "pmSourceMasks.h"
     39#include "pmSourceExtendedPars.h"
     40#include "pmSourceDiffStats.h"
    3441#include "pmSource.h"
    35 #include "pmModelClass.h"
    3642#include "pmSourceFitModel.h"
    3743
    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)
     44void pmSourceFitOptionsFree(pmSourceFitOptions *opt)
    4545{
    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
     49pmSourceFitOptions *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;
    5363}
    5464
    5565bool pmSourceFitModel (pmSource *source,
    5666                       pmModel *model,
    57                        pmSourceFitMode mode,
     67                       pmSourceFitOptions *options,
    5868                       psImageMaskType maskVal)
    5969{
     
    7686    psVector *yErr = psVectorAllocEmpty(nPix, PS_TYPE_F32);
    7787
     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
    7893    // fill in the coordinate and value entries
    7994    nPix = 0;
     
    95110            // skip nan values in image
    96111            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            }
    102115
    103116            // Convert i/j to image space:
    104117            // 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;
    107132            x->data[nPix] = (psPtr *) coord;
    108133            y->data.F32[nPix] = source->pixels->data.F32[i][j];
     
    111136            // as variance to avoid the bias from systematic errors here we would just use the
    112137            // source sky variance
    113             if (PM_SOURCE_FIT_MODEL_PIX_WEIGHTS) {
     138            if (options->poissonErrors) {
    114139                yErr->data.F32[nPix] = 1.0 / source->variance->data.F32[i][j];
    115140            } else {
    116                 yErr->data.F32[nPix] = 1.0 / PM_SOURCE_FIT_MODEL_WEIGHT;
     141                yErr->data.F32[nPix] = 1.0 / options->weight;
    117142            }
    118143            nPix++;
     
    133158    // set parameter mask based on fitting mode
    134159    int nParams = 0;
    135     switch (mode) {
    136     case PM_SOURCE_FIT_NORM:
     160    switch (options->mode) {
     161      case PM_SOURCE_FIT_NORM:
    137162        // NORM-only model fits only source normalization (Io)
    138163        nParams = 1;
     
    140165        constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[PM_PAR_I0] = 0;
    141166        break;
    142     case PM_SOURCE_FIT_PSF:
     167      case PM_SOURCE_FIT_PSF:
    143168        // PSF model only fits x,y,Io
    144169        nParams = 3;
     
    148173        constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[PM_PAR_YPOS] = 0;
    149174        break;
    150     case PM_SOURCE_FIT_EXT:
     175      case PM_SOURCE_FIT_EXT:
    151176        // EXT model fits all params (except sky)
    152177        nParams = params->n - 1;
     
    154179        constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[PM_PAR_SKY] = 1;
    155180        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");
    158205    }
    159206    // force the floating parameters to fall within the contraint ranges
    160207    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);
    163210    }
    164211
     
    173220    }
    174221
    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);
    176223
    177224    psImage *covar = psImageAlloc (params->n, params->n, PS_TYPE_F32);
     
    194241    model->flags |= PM_MODEL_STATUS_FITTED;
    195242    if (!fitStatus) model->flags |= PM_MODEL_STATUS_NONCONVERGE;
     243    if (myMin->lastDelta > myMin->minTol) model->flags |= PM_MODEL_STATUS_WEAK_FIT;
    196244
    197245    // get the Gauss-Newton distance for fixed model parameters
Note: See TracChangeset for help on using the changeset viewer.