IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 42089


Ignore:
Timestamp:
Feb 28, 2022, 2:43:25 PM (4 years ago)
Author:
eugene
Message:

plug a leak in psFitsTableNew.c; fix implementation of FITS_SCALE_ASINH_MANUAL; add option to reweight the errors in LMM; compile-time option to activate a hard-coded random seed (only needed for psLib testing)

Location:
trunk/psLib/src
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/fits/psFits.h

    r31633 r42089  
    6060    PS_FITS_SCALE_ASINH_STDEV_NEGATIVE, ///< Do asinh scaling, auto-scale to sample stdev, place mean at upper limit
    6161    PS_FITS_SCALE_ASINH_STDEV_BOTH,     ///< Do asinh scaling, auto-scale to sample stdev, place mean at middle
    62     PS_FITS_SCALE_ASINH_MANUAL          ///< Manual scaling after doing asinh scaling.
     62    PS_FITS_SCALE_ASINH_MANUAL          ///< Manual scaling after doing asinh scaling.(use specified BSCALE, BZERO, BOFFSET, BSOFTEN)
    6363} psFitsScaling;
    6464
  • trunk/psLib/src/fits/psFitsImage.c

    r41521 r42089  
    744744      }
    745745    }   
    746     // Remove any BSOFTENvalues that exist in the header if we are not using that scaling anymore
     746    // Remove any BSOFTEN values that exist in the header if we are not using that scaling anymore
    747747    if (options && (!((options->scaling == PS_FITS_SCALE_ASINH_RANGE)||
    748748                      (options->scaling == PS_FITS_SCALE_ASINH_MANUAL)||
  • trunk/psLib/src/fits/psFitsScale.c

    r41521 r42089  
    963963        *bzero = options->bzero;
    964964        *boffset = options->boffset;
     965        break;
     966      case PS_FITS_SCALE_ASINH_MANUAL:
     967        *bscale = options->bscale;
     968        *bzero = options->bzero;
     969        *boffset = options->boffset;
     970        *bsoften = options->bsoften;
    965971        break;
    966972      default:
  • trunk/psLib/src/fits/psFitsTableNew.c

    r41896 r42089  
    9797freeTable(psFitsTable *table)
    9898{
     99    if (!table) return;
    99100    for (int col = 0; col < table->numCols; col++) {
    100101        psFitsTableColumn *column = &table->columns[col];
     
    176177{
    177178    // add any element frees here
     179    if (!column) return;
     180
     181    psFree (column->name);
    178182    return;
    179183}
     
    195199    psFitsTableColumn *column = psFitsTableColumnAlloc (name, type);
    196200    psArrayAdd (tableColumns, 10, column);
     201    psFree (column);
    197202    return true;
    198203}
  • trunk/psLib/src/math/psMinimizeLMM.c

    r36082 r42089  
    220220
    221221    // XXX should we give up if chisq is nan?
    222     psF32 chisq = psMinLM_SetABX(alpha, Beta, params, paramMask, x, y, dy, func);
     222    // do not apply reweighting
     223    psF32 chisq = psMinLM_SetABX(alpha, Beta, params, paramMask, x, y, dy, false, func);
    223224    if (isnan(chisq)) {
    224225        psTrace ("psLib.math", 5, "psMinLM_SetABX() returned a NAN chisq.\n");
     
    282283    return(0.5*dLinear);
    283284}
     285
     286// NOTE : reweight is true, the implementation below calculates the alpha,beta terms
     287// including a modified weight, but the chi-square value is calculated usig standard
     288// weights. 
    284289
    285290// alpha, beta, params are already allocated
     
    292297    const psVector *y,
    293298    const psVector *dy,
     299    bool  reweight, // if true, we calculate the reweighting for each point based on distance from model
    294300    psMinimizeLMChi2Func func)
    295301{
     
    306312    }
    307313
    308     psF32 chisq;
    309     psF32 delta;
    310     psF32 weight;
    311     psF32 ymodel;
    312314    psVector *deriv = psVectorAlloc(params->n, PS_TYPE_F32);
    313315
     
    315317    psImageInit (alpha, 0.0);
    316318    psVectorInit (beta, 0.0);
    317     chisq = 0.0;
     319    psF32 chisq = 0.0;
    318320
    319321    // calculate chisq, alpha, beta. alpha & beta only represent unmasked parameters; skip
    320322    // masked ones
    321323    for (psS32 i = 0; i < y->n; i++) {
    322         ymodel = func(deriv, params, (psVector *) x->data[i]);
    323 
    324         delta = ymodel - y->data.F32[i];
    325         chisq += PS_SQR(delta) * dy->data.F32[i];
     324        psF32 ymodel = func(deriv, params, (psVector *) x->data[i]);
     325        psF32 delta  = ymodel - y->data.F32[i];
     326        psF32 dChi2  = PS_SQR(delta) * dy->data.F32[i];
     327        psF32 wtmod  = reweight ? 1.0 / (1.0 + 5.69*dChi2) : 1.0; // see fit1d_irls.c:weight_cauchy
     328        chisq += dChi2;
     329
     330        // if we are doing an IRLS-style fit, we downweight points based on the dChisq
     331        // value above: this means setting an additional weight term
    326332
    327333        // XXX remove this later:
     
    337343            if (paramMask && (paramMask->data.PS_TYPE_VECTOR_MASK_DATA[j])) continue;
    338344
    339             weight = deriv->data.F32[j] * dy->data.F32[i];
     345            psF32 weight = deriv->data.F32[j] * dy->data.F32[i] * wtmod;
    340346
    341347            for (int k = 0, K = 0; k <= j; k++) {
     
    477483
    478484    // calculate initial alpha and beta, set chisq (min->value)
    479     min->value = psMinLM_SetABX(alpha, beta, params, paramMask, x, y, dy, func);
     485    // do not apply IRLS reweighting yet, even if requested
     486    min->value = psMinLM_SetABX(alpha, beta, params, paramMask, x, y, dy, false, func);
    480487    if (isnan(min->value)) {
    481488        min->iter = min->maxIter;
     
    529536
    530537        // calculate Chisq for new guess, update Alpha & Beta
    531         Chisq = psMinLM_SetABX(Alpha, Beta, Params, paramMask, x, y, dy, func);
     538        Chisq = psMinLM_SetABX(Alpha, Beta, Params, paramMask, x, y, dy, min->useReweighting, func);
    532539        if (isnan(Chisq)) {
    533540            min->iter ++;
     
    716723
    717724    // calculate initial alpha and beta, set chisq (min->value)
    718     min->value = psMinLM_SetABX(alpha, beta, params, paramMask, x, y, dy, func);
     725    // do not apply IRLS reweighting yet, even if requested
     726    min->value = psMinLM_SetABX(alpha, beta, params, paramMask, x, y, dy, false, func);
    719727    if (isnan(min->value)) {
    720728        min->iter = min->maxIter;
     
    788796
    789797        // calculate Chisq for new guess, update Alpha & Beta
    790         Chisq = psMinLM_SetABX(Alpha, Beta, Params, paramMask, x, y, dy, func);
     798        // if requested, modify the weights IRLS-style
     799        Chisq = psMinLM_SetABX(Alpha, Beta, Params, paramMask, x, y, dy, min->useReweighting, func);
    791800        if (isnan(Chisq)) {
    792801            min->iter ++;
     
    985994    min->gainFactorMode = 0;
    986995    min->isInteractive = false;
     996    min->useReweighting = false;
    987997   
    988998    return(min);
  • trunk/psLib/src/math/psMinimizeLMM.h

    r36082 r42089  
    9090    bool chisqConvergence;
    9191    bool isInteractive;
     92    bool useReweighting;
    9293}
    9394psMinimization;
     
    194195    const psVector *y,                 ///< Measurement coordinates
    195196    const psVector *dy,                ///< Weights calculated from y-errors
     197    bool  reweight,                    ///< if true, we calculate the reweighting for each point based on distance from model
    196198    psMinimizeLMChi2Func func          ///< Specified function
    197199);
  • trunk/psLib/src/math/psRandom.c

    r23269 r42089  
    3838#include "psAssert.h"
    3939
    40 
    41 unsigned long seed = 0;                 // Seed for RNG
    42 
     40// XXX set to non-zero for a test
     41# define HARDWIRED_SEED 0
     42unsigned long seed = HARDWIRED_SEED;                 // Seed for RNG
    4343
    4444psU64 p_psRandomGetSystemSeed(bool log)
     
    7171psU64 psRandomSeed(psU64 value)
    7272{
     73    if (HARDWIRED_SEED) {
     74        seed = HARDWIRED_SEED;
     75        return seed;
     76    }
     77
    7378    while (value == 0) {
    7479        value = p_psRandomGetSystemSeed(false);
Note: See TracChangeset for help on using the changeset viewer.