IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 42497


Ignore:
Timestamp:
Aug 15, 2023, 12:16:20 PM (3 years ago)
Author:
eugene
Message:

working on the polynomial-fitting tests

Location:
branches/eam_branches/ipp-20230313/psLib/test/math
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20230313/psLib/test/math/tap_psPolyFit1D.c

    r23259 r42497  
    88 
    99XXX: Try null stats.
     10
     11XXX: this function is confused about POLY_ORDER (5 should have x^5, but setData stops at x^4)
    1012 *****************************************************************************/
    1113#include <stdio.h>
     
    350352    plan_tests(104);
    351353
     354    // psTraceSetLevel ("psLib.math.psMatrixGJSolve", 4);
    352355
    353356    // psVectorFitPolynomial1D()
  • branches/eam_branches/ipp-20230313/psLib/test/math/tap_psPolyFit_IRLS.c

    r42493 r42497  
    55#include "pstap.h"
    66
    7 #define POLY_ORDER 5
     7// #define POLY_ORDER 5
    88// #define A -560.0
    99// #define B +1116.0
     
    2020// #define F +2.0
    2121
    22 #define A -300.0
    23 #define B +1200.0
    24 #define C -400.0
    25 #define D +500.0
    26 #define E -100.0
    27 #define F +3.0
     22// #define A -300.0
     23// #define B +1200.0
     24// #define C -400.0
     25// #define D +500.0
     26// #define E -100.0
     27// #define F +3.0
     28
     29#define POLY_ORDER 1
     30#define A -1.0
     31#define B +2.0
    2832
    2933#define MASK_VALUE 1
    3034
     35static psRandom *rng = NULL;
     36
    3137psF32 setData(psF32 x)
    3238{
    33   return(A + (B * x) + (C * x * x) + (D * x * x * x) + (E * x * x * x * x) + (F * x * x * x * x * x));
     39//  return(A + (B * x) + (C * x * x) + (D * x * x * x) + (E * x * x * x * x) + (F * x * x * x * x * x));
     40    return(A + (B * x));
     41}
     42
     43# define DUMP_FIT                                                       \
     44    /* compare the fitted values to the input coefficients */           \
     45    fprintf (stderr, "%5.3f vs %5.3f : %5.2f\n", myPoly->coeff[0], A, myPoly->coeff[0] - A); \
     46    fprintf (stderr, "%5.3f vs %5.3f : %5.2f\n", myPoly->coeff[1], B, myPoly->coeff[1] - B);
     47
     48//    fprintf (stderr, "%5.2f vs %5.2f : %5.2f = %5.2f\n", myPoly->coeff[2], C, myPoly->coeff[2] - C, (myPoly->coeff[2] - C)/myPoly->coeff[2]);
     49//    fprintf (stderr, "%5.2f vs %5.2f : %5.2f = %5.2f\n", myPoly->coeff[3], D, myPoly->coeff[3] - D, (myPoly->coeff[3] - D)/myPoly->coeff[3]);
     50//    fprintf (stderr, "%5.2f vs %5.2f : %5.2f = %5.2f\n", myPoly->coeff[4], E, myPoly->coeff[4] - E, (myPoly->coeff[4] - E)/myPoly->coeff[4]);
     51//    fprintf (stderr, "%5.2f vs %5.2f : %5.2f = %5.2f\n", myPoly->coeff[5], F, myPoly->coeff[5] - F, (myPoly->coeff[5] - F)/myPoly->coeff[5]);
     52
     53# define IS_IRLS 1
     54int genericFit (psVector *xTruth, psVector *fTruth, float sigma, float outfrac, int isIRLS) {
     55
     56    psPolynomial1D *myPoly = psPolynomial1DAlloc(PS_POLYNOMIAL_ORD, POLY_ORDER);
     57    psVector *x    = psVectorAlloc(xTruth->n, PS_TYPE_F64);
     58    psVector *f    = psVectorAlloc(xTruth->n, PS_TYPE_F64);
     59    psVector *fErr = psVectorAlloc(xTruth->n, PS_TYPE_F64);
     60    // psVector *mask = psVectorAlloc(numData, PS_TYPE_U8);
     61
     62    // set values with an error of 0.5
     63    for (int i = 0; i < xTruth->n; i++) {
     64        x->data.F64[i] = xTruth->data.F64[i];
     65        f->data.F64[i] = fTruth->data.F64[i] + sigma*psRandomGaussian(rng);
     66        fErr->data.F64[i] = sigma;
     67    }
     68
     69    // add in 5% random outliers
     70    for (int i = 0; i < outfrac*xTruth->n; i++) {
     71        int n = (xTruth->n - 1)*psRandomUniform(rng); // n is in range 0 - (numData-1)
     72        float nsig = 20.0*psRandomUniform(rng) +10.0;
     73        // symmetric outliers do not change the fit parameters
     74        // nsig = (psRandomUniform(rng) > 0.5) ? nsig : -1.0 * nsig;
     75        f->data.F64[n] += nsig*sigma;
     76    }
     77    psMemId id = psMemGetId();
     78    psStats *stats = psStatsAlloc(PS_STAT_CLIPPED_MEAN);
     79    stats->clipIter = 10; // max number of iterations
     80   
     81    if (isIRLS) {
     82        diag ("IRLS fits with %f outliers: sigma = %f", outfrac, sigma);
     83        bool rc = psVectorIRLSFitPolynomial1D(myPoly, stats, NULL, MASK_VALUE, f, fErr, x);
     84        ok(rc == true, "fit succeeded mechanically");
     85
     86        // XXX test:
     87        FILE *ftest = fopen ("irls.ft.dat", "w");
     88        for (int i = 0; i < f->n; i++) {
     89          fprintf (ftest, "%d %f %f %f\n", i, x->data.F64[i], f->data.F64[i], fErr->data.F64[i]);
     90        }
     91        fclose (ftest);
     92    } else {
     93        diag ("ORD fits with %f outliers: sigma = %f", outfrac, sigma);
     94        bool rc = psVectorFitPolynomial1D(myPoly, NULL, MASK_VALUE, f, fErr, x);
     95        ok(rc == true, "fit succeeded mechanically");
     96    }
     97
     98    // compare the fitted values to the input coefficients
     99    DUMP_FIT;
     100
     101    // is_float_tol (myPoly->coeff[0], A, 1e-5, "A coeffs match");
     102    // is_float_tol (myPoly->coeff[1], B, 1e-5, "B coeffs match");
     103    // is_float_tol (myPoly->coeff[2], C, 1e-5, "C coeffs match");
     104    // is_float_tol (myPoly->coeff[3], D, 1e-5, "D coeffs match");
     105    // is_float_tol (myPoly->coeff[4], E, 1e-5, "E coeffs match");
     106    // is_float_tol (myPoly->coeff[5], F, 1e-5, "F coeffs match");
     107
     108    psFree (stats);
     109    psFree (x);
     110    psFree (f);
     111    psFree (fErr);
     112    psFree (myPoly);
     113    ok(!psMemCheckLeaks (id, NULL, NULL, false), "no memory leaks");
     114
     115    return true;
    34116}
    35117
    36118int main()
    37119{
    38   psLogSetFormat("HLNM");
    39   psLogSetLevel(PS_LOG_INFO);
    40   plan_tests(104);
     120    psLogSetFormat("HLNM");
     121    psLogSetLevel(PS_LOG_INFO);
     122    plan_tests(104);
    41123
    42   // create x vector running from -1 to 11 in steps of 0.1
    43   int numData = 120;
    44   psVector *xTruth = psVectorAlloc(numData, PS_TYPE_F64);
    45   psVector *fTruth = psVectorAlloc(numData, PS_TYPE_F64);
    46   psRandom *rng = psRandomAllocSpecific(PS_RANDOM_TAUS, 1); // Using a known seed
     124    // create x vector running from -1 to 11 in steps of 0.1
     125    int numData = 120;
     126    psVector *xTruth = psVectorAlloc(numData, PS_TYPE_F64);
     127    psVector *fTruth = psVectorAlloc(numData, PS_TYPE_F64);
     128    rng = psRandomAllocSpecific(PS_RANDOM_TAUS, 1); // Using a known seed
     129   
     130    // create reference data
     131    for (int i = 0; i < numData; i++) {
     132        xTruth->data.F64[i] = 0.1*i - 1.0;
     133        fTruth->data.F64[i] = setData(xTruth->data.F64[i]);
     134    }
    47135
    48   psPolynomial1D *myPoly = psPolynomial1DAlloc(PS_POLYNOMIAL_ORD, POLY_ORDER);
    49   psVector *x = psVectorAlloc(numData, PS_TYPE_F64);
    50   psVector *f = psVectorAlloc(numData, PS_TYPE_F64);
    51   psVector *fErr = psVectorAlloc(numData, PS_TYPE_F64);
    52   // psVector *mask = psVectorAlloc(numData, PS_TYPE_U8);
    53    
    54   // XXX F64 or F32??
    55   for (int i = 0; i < numData; i++) {
    56     xTruth->data.F64[i] = 0.1*i - 1.0;
    57     fTruth->data.F64[i] = setData(xTruth->data.F64[i]);
    58   }
     136    genericFit (xTruth, fTruth, 0.1, 0.20, !IS_IRLS);
     137    genericFit (xTruth, fTruth, 0.1, 0.20,  IS_IRLS);
    59138
    60   // set values with an error of sigma
    61   double sigma = 0.01;
    62   for (int i = 0; i < numData; i++) {
    63     x->data.F64[i] = xTruth->data.F64[i];
    64     f->data.F64[i] = fTruth->data.F64[i] + sigma*psRandomGaussian(rng);
    65     fErr->data.F64[i] = sigma;
    66     // fprintf (stdout, "%d %f %f %f\n", i, x->data.F64[i], f->data.F64[i], fErr->data.F64[i]);
    67   }
    68 
    69   {
    70     psMemId id = psMemGetId();
    71     bool rc = psVectorFitPolynomial1D(myPoly, NULL, MASK_VALUE, f, fErr, x);
    72     ok(rc == true, "fit succeeded mechanically");
    73 
    74     // compare the fitted values to the input coefficients
    75 
    76     fprintf (stderr, "%5.2f vs %5.2f\n", myPoly->coeff[0], A);
    77     fprintf (stderr, "%5.2f vs %5.2f\n", myPoly->coeff[1], B);
    78     fprintf (stderr, "%5.2f vs %5.2f\n", myPoly->coeff[2], C);
    79     fprintf (stderr, "%5.2f vs %5.2f\n", myPoly->coeff[3], D);
    80     fprintf (stderr, "%5.2f vs %5.2f\n", myPoly->coeff[4], E);
    81     fprintf (stderr, "%5.2f vs %5.2f\n", myPoly->coeff[5], F);
    82 
    83     ok(!psMemCheckLeaks (id, NULL, NULL, false), "no memory leaks");
    84   }
    85 
     139    // genericFit (xTruth, fTruth, 0.01, 0.00, !IS_IRLS);
     140    // genericFit (xTruth, fTruth, 0.50, 0.00, !IS_IRLS);
     141    // genericFit (xTruth, fTruth, 0.01, 0.05, !IS_IRLS);
     142    // genericFit (xTruth, fTruth, 0.50, 0.05, !IS_IRLS);
     143    //
     144    // genericFit (xTruth, fTruth, 0.01, 0.00,  IS_IRLS);
     145    // genericFit (xTruth, fTruth, 0.50, 0.00,  IS_IRLS);
     146    // genericFit (xTruth, fTruth, 0.01, 0.05,  IS_IRLS);
     147    // genericFit (xTruth, fTruth, 0.50, 0.05,  IS_IRLS);
    86148}
    87149
Note: See TracChangeset for help on using the changeset viewer.