IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 30266


Ignore:
Timestamp:
Jan 12, 2011, 9:32:08 PM (15 years ago)
Author:
eugene
Message:

add errors to kernel coeffs; calculate and plot chisq and moments

Location:
branches/eam_branches/ipp-20101205/psModules/src
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20101205/psModules/src/extras/pmVisual.c

    r29545 r30266  
    306306}
    307307
     308bool pmVisualRangeImage (int kapaFD, psImage *inImage, const char *name, int channel, float min, float max) {
     309
     310    KiiImage image;
     311    KapaImageData data;
     312    Coords coords;
     313
     314    strcpy (coords.ctype, "RA---TAN");
     315
     316    image.data2d = inImage->data.F32;
     317    image.Nx = inImage->numCols;
     318    image.Ny = inImage->numRows;
     319
     320    strcpy (data.name, name);
     321    strcpy (data.file, name);
     322    data.zero = min;
     323    data.range = max - min;
     324    data.logflux = 0;
     325
     326    KiiSetChannel (kapaFD, channel);
     327    KiiNewPicture2D (kapaFD, &image, &data, &coords);
     328
     329    return true;
     330}
    308331
    309332psImage* pmVisualImageToFloat(psImage *image) {
  • branches/eam_branches/ipp-20101205/psModules/src/extras/pmVisual.h

    r23242 r30266  
    6464                        const char *name, int channel, bool clip);
    6565
     66bool pmVisualRangeImage (int kapaFD, psImage *inImage, const char *name, int channel, float min, float max);
    6667
    6768/** Calculate statistics on an image.
  • branches/eam_branches/ipp-20101205/psModules/src/imcombine/pmSubtractionEquation.c

    r30021 r30266  
    88
    99#include "pmErrorCodes.h"
     10#include "pmVisual.h"
    1011#include "pmSubtraction.h"
    1112#include "pmSubtractionKernels.h"
     
    1617#include "pmSubtractionVisual.h"
    1718
    18 //#define TESTING                         // TESTING output for debugging; may not work with threads!
    19 
    20 //#define USE_WEIGHT                      // Include weight (1/variance) in equation?
    21 
    22 // XXX TEST:
    23 //# define USE_WINDOW                      // window to avoid neighbor contamination
     19//# define TESTING                         // TESTING output for debugging; may not work with threads!
     20//# define USE_WEIGHT                      // Include weight (1/variance) in equation?
     21# define USE_WINDOW                      // window to avoid neighbor contamination
     22// XXX if we want to have a weight and window, we'll need to pass through to the chisq function
    2423
    2524# define PENALTY false
     
    983982    }
    984983
    985     pmSubtractionVisualPlotLeastSquares(stamps);
    986984    pmSubtractionVisualShowKernels((pmSubtractionKernels  *)kernels);
    987985    pmSubtractionVisualShowBasis(stamps);
     986    pmSubtractionVisualPlotLeastSquares(stamps);
    988987
    989988    psLogMsg("psModules.imcombine", PS_LOG_INFO, "Calculate equation: %f sec",
     
    10801079        }
    10811080
     1081        pmSubtractionVisualPlotLeastSquaresResid(stamps, sumMatrix, numStamps);
     1082
    10821083#if 0
    10831084        psImage *save = psImageCopy(NULL, sumMatrix, PS_TYPE_F32);
     
    10871088#endif
    10881089
     1090        psImage *invMatrix = NULL;
    10891091        psVector *solution = NULL;                       // Solution to equation!
    10901092        solution = psVectorAlloc(numParams, PS_TYPE_F64);
     
    10961098        if (1) {
    10971099            solution = psMatrixSolveSVD(solution, sumMatrix, sumVector, NAN);
     1100            invMatrix = psMatrixInvert(NULL, sumMatrix, NULL);
    10981101        } else {
    10991102            psVector *PERM = NULL;
     
    11041107        }
    11051108
    1106 # if (0)
     1109# if (1)
    11071110        for (int i = 0; i < solution->n; i++) {
    1108             psLogMsg("psModules.imcombine", PS_LOG_DETAIL, "Single solution %d: %lf\n", i, solution->data.F64[i]);
     1111            psLogMsg("psModules.imcombine", PS_LOG_DETAIL, "Single solution %d: %lf +/- %lf\n", i, solution->data.F64[i], sqrt(invMatrix->data.F64[i][i]));
    11091112        }
    11101113# endif
     
    11131116            kernels->solution1 = psVectorAlloc(sumVector->n + 2, PS_TYPE_F64); // 1 for norm, 1 for bg
    11141117            psVectorInit(kernels->solution1, 0.0);
     1118            kernels->solution1err = psVectorAlloc(sumVector->n + 2, PS_TYPE_F64); // 1 for norm, 1 for bg
     1119            psVectorInit(kernels->solution1err, 0.0);
    11151120        }
    11161121
     
    11201125        for (int i = 0; i < numKernels * numPoly; i++) {
    11211126            kernels->solution1->data.F64[i] = solution->data.F64[i];
     1127            kernels->solution1err->data.F64[i] = sqrt(invMatrix->data.F64[i][i]);
    11221128        }
    11231129
     
    11311137        psFree(sumVector);
    11321138        psFree(sumMatrix);
     1139        psFree(invMatrix);
    11331140
    11341141    } else {
     
    11601167        }
    11611168
     1169        pmSubtractionVisualPlotLeastSquaresResid(stamps, sumMatrix, numStamps);
     1170
    11621171#if 0
    11631172        psImage *save = psImageCopy(NULL, sumMatrix, PS_TYPE_F32);
     
    11711180        }
    11721181
     1182        psImage *invMatrix = NULL;
    11731183        psVector *solution = NULL;                       // Solution to equation!
    11741184        solution = psVectorAlloc(numParams, PS_TYPE_F64);
     
    11761186
    11771187        // DUAL solution
    1178         solution = psMatrixSolveSVD(solution, sumMatrix, sumVector, NAN);
    1179 
    1180 #if (0)
     1188# if (1)
     1189        solution = psMatrixSolveSVD(solution, sumMatrix, sumVector, 1e-7);
     1190        invMatrix = psMatrixInvert(NULL, sumMatrix, NULL);
     1191# endif
     1192# if (0)
     1193        psMatrixLUSolve(sumMatrix, sumVector);
     1194        solution = sumVector;
     1195        invMatrix = sumMatrix;
     1196# endif
     1197
     1198#if (1)
    11811199        for (int i = 0; i < solution->n; i++) {
    1182             fprintf(stderr, "Dual solution %d: %lf\n", i, solution->data.F64[i]);
     1200            fprintf(stderr, "Dual solution %d: %lf +/- %lf\n", i, solution->data.F64[i], sqrt(invMatrix->data.F64[i][i]));
    11831201        }
    11841202#endif
     
    11871205            kernels->solution1 = psVectorAlloc(numSolution1 + 2, PS_TYPE_F64);
    11881206            psVectorInit (kernels->solution1, 0.0);
     1207            kernels->solution1err = psVectorAlloc(numSolution1 + 2, PS_TYPE_F64); // 1 for norm, 1 for bg
     1208            psVectorInit(kernels->solution1err, 0.0);
    11891209        }
    11901210        if (!kernels->solution2) {
    11911211            kernels->solution2 = psVectorAlloc(numSolution2, PS_TYPE_F64);
    11921212            psVectorInit (kernels->solution2, 0.0);
     1213            kernels->solution2err = psVectorAlloc(numSolution2, PS_TYPE_F64);
     1214            psVectorInit(kernels->solution2err, 0.0);
    11931215        }
    11941216
     
    12051227            kernels->solution1->data.F64[i] = solution->data.F64[i] * stamps->normValue;
    12061228            kernels->solution2->data.F64[i] = solution->data.F64[i + numSolution1];
     1229
     1230            kernels->solution1err->data.F64[i] = sqrt(invMatrix->data.F64[i][i]) * stamps->normValue;
     1231            int i2 = i + numSolution1;
     1232            kernels->solution2err->data.F64[i] = sqrt(invMatrix->data.F64[i2][i2]);
    12071233        }
    12081234
     
    12131239        kernels->solution1->data.F64[bgIndex] = 0.0;
    12141240
     1241        psFree(solution);
     1242        psFree(sumVector);
    12151243        psFree(sumMatrix);
    1216         psFree(sumVector);
    1217         psFree(solution);
     1244        psFree(invMatrix);
    12181245    }
    12191246
     
    12241251    if (psTraceGetLevel("psModules.imcombine") >= 7) {
    12251252        for (int i = 0; i < kernels->solution1->n; i++) {
    1226             psTrace("psModules.imcombine", 7, "Solution 1 %d: %f\n", i, kernels->solution1->data.F64[i]);
     1253            psTrace("psModules.imcombine", 7, "Solution 1 %d: %f +/- %f\n", i, kernels->solution1->data.F64[i], kernels->solution1err->data.F64[i]);
    12271254        }
    12281255        if (kernels->mode == PM_SUBTRACTION_MODE_DUAL) {
    12291256            for (int i = 0; i < kernels->solution2->n; i++) {
    1230                 psTrace("psModules.imcombine", 7, "Solution 2 %d: %f\n", i, kernels->solution2->data.F64[i]);
     1257                psTrace("psModules.imcombine", 7, "Solution 2 %d: %f +/- %f\n", i, kernels->solution2->data.F64[i], kernels->solution2err->data.F64[i]);
    12311258            }
    12321259        }
     
    12801307    psVectorAppend(fResOuter, dOuter/sum);
    12811308    psVectorAppend(fResTotal, dTotal/sum);
     1309    return true;
     1310}
     1311
     1312// given the convolved image(s) and the residual image, calculate the second moment(s) and the chisq
     1313bool pmSubtractionChisqStats(psVector *fluxesVector, psVector *chisqVector, psVector *momentVector, psKernel *convolved1, psKernel *convolved2, psKernel *residual, psKernel *weight, psKernel *window) {
     1314
     1315    int npix = 0;
     1316    float chisq = 0;
     1317
     1318    // get the chisq
     1319    for (int y = residual->yMin; y <= residual->yMax; y++) {
     1320        for (int x = residual->xMin; x <= residual->xMax; x++) {
     1321            chisq  += PS_SQR(residual->kernel[y][x]);
     1322            npix ++;
     1323        }
     1324    }
     1325    psVectorAppend(chisqVector, chisq / npix);
     1326
     1327    float value1 = 0;
     1328    float value2 = 0;
     1329    float flux2 = 0;
     1330    float fluxX = 0;
     1331    float fluxY = 0;
     1332    float fluxX2 = 0;
     1333    float fluxY2 = 0;
     1334
     1335    float fluxC1 = 0;
     1336    float fluxC2 = 0;
     1337
     1338    float moment = 0;
     1339
     1340    // get the moments from convolved1
     1341    if (convolved1) {
     1342        for (int y = residual->yMin; y <= residual->yMax; y++) {
     1343            for (int x = residual->xMin; x <= residual->xMax; x++) {
     1344                value1  = convolved1->kernel[y][x];
     1345                value2  = PS_SQR(value1);
     1346
     1347                if (weight) {
     1348                    value2 *= weight->kernel[y][x];
     1349                }
     1350                if (window) {
     1351                    value1 *= window->kernel[y][x];
     1352                    value2 *= window->kernel[y][x];
     1353                }
     1354
     1355                fluxC1 += value1;
     1356                flux2  += value2;
     1357                fluxX  += x * value2;
     1358                fluxY  += y * value2;
     1359                fluxX2 += PS_SQR(x) * value2;
     1360                fluxY2 += PS_SQR(y) * value2;
     1361            }
     1362        }
     1363        // float Mx = fluxX / flux2;
     1364        // float My = fluxY / flux2;
     1365        float Mxx = fluxX2 / flux2;
     1366        float Myy = fluxY2 / flux2;
     1367
     1368        // fprintf (stderr, "conv1, flux2: %f, Mx: %f, My: %f, Mxx: %f, Myy: %f, chisq: %f, npix: %d\n", flux2, Mx, My, Mxx, Myy, chisq, npix);
     1369        moment += Mxx + Myy;
     1370    }
     1371
     1372    // get the moments from convolved1
     1373    if (convolved2) {
     1374        for (int y = residual->yMin; y <= residual->yMax; y++) {
     1375            for (int x = residual->xMin; x <= residual->xMax; x++) {
     1376                value1  = convolved2->kernel[y][x];
     1377                value2  = PS_SQR(value1);
     1378
     1379                if (weight) {
     1380                    value2 *= weight->kernel[y][x];
     1381                }
     1382                if (window) {
     1383                    value1 *= window->kernel[y][x];
     1384                    value2 *= window->kernel[y][x];
     1385                }
     1386
     1387                fluxC2 += value1;
     1388                flux2  += value2;
     1389                fluxX  += x * value2;
     1390                fluxY  += y * value2;
     1391                fluxX2 += PS_SQR(x) * value2;
     1392                fluxY2 += PS_SQR(y) * value2;
     1393            }
     1394        }
     1395        // float Mx = fluxX / flux2;
     1396        // float My = fluxY / flux2;
     1397        float Mxx = fluxX2 / flux2;
     1398        float Myy = fluxY2 / flux2;
     1399
     1400        // fprintf (stderr, "conv2, flux2: %f, Mx: %f, My: %f, Mxx: %f, Myy: %f, chisq: %f, npix: %d\n", flux2, Mx, My, Mxx, Myy, chisq, npix);
     1401        moment += Mxx + Myy;
     1402    }
     1403
     1404    float flux = fluxC1 + fluxC2;
     1405   
     1406    if (convolved1 && convolved2) {
     1407        moment *= 0.5;
     1408        flux *= 0.5;
     1409    }
     1410    psVectorAppend(momentVector, moment);
     1411    psVectorAppend(fluxesVector, flux);
     1412    return true;
     1413}
     1414
     1415// typedef struct {
     1416//     float moment;
     1417//     float chisq;
     1418// } diffStat;
     1419
     1420bool pmSubtractionCalculateChisqAndMoments(pmSubtractionStampList *stamps,
     1421                                           pmSubtractionKernels *kernels)
     1422{
     1423    PM_ASSERT_SUBTRACTION_STAMP_LIST_NON_NULL(stamps, NULL);
     1424    PM_ASSERT_SUBTRACTION_KERNELS_NON_NULL(kernels, NULL);
     1425    PM_ASSERT_SUBTRACTION_KERNELS_SOLUTION(kernels, NULL);
     1426
     1427    psVector *fluxes = psVectorAllocEmpty(stamps->num, PS_TYPE_F32);
     1428    psVector *chisq = psVectorAllocEmpty(stamps->num, PS_TYPE_F32);
     1429    psVector *moments = psVectorAllocEmpty(stamps->num, PS_TYPE_F32);
     1430
     1431    int footprint = stamps->footprint; // Half-size of stamps
     1432    int numKernels = kernels->num;      // Number of kernels
     1433
     1434    psImage *polyValues = NULL;         // Polynomial values
     1435
     1436    psKernel *residual = psKernelAlloc(-footprint, footprint, -footprint, footprint); // Residual image
     1437
     1438    // storage for the convolved source image (only convolved1 is used in SINGLE mode)
     1439    psKernel *convolved1 = psKernelAlloc(-footprint, footprint, -footprint, footprint); // Residual image
     1440    psKernel *convolved2 = psKernelAlloc(-footprint, footprint, -footprint, footprint); // Residual image
     1441
     1442    for (int i = 0; i < stamps->num; i++) {
     1443        pmSubtractionStamp *stamp = stamps->stamps->data[i]; // The stamp of interest
     1444        if (stamp->status != PM_SUBTRACTION_STAMP_USED) {
     1445            continue;
     1446        }
     1447
     1448        // Calculate coefficients of the kernel basis functions
     1449        polyValues = p_pmSubtractionPolynomial(polyValues, kernels->spatialOrder, stamp->xNorm, stamp->yNorm);
     1450        double norm = p_pmSubtractionSolutionNorm(kernels); // Normalisation
     1451        double background = p_pmSubtractionSolutionBackground(kernels, polyValues);// Difference in background
     1452
     1453        // Calculate residuals
     1454        psImageInit(residual->image, 0.0);
     1455
     1456    psKernel *weight = NULL;
     1457    psKernel *window = NULL;
     1458
     1459#ifdef USE_WEIGHT
     1460    weight = stamp->weight;
     1461#endif
     1462#ifdef USE_WINDOW
     1463    window = stamps->window;
     1464#endif
     1465
     1466        if (kernels->mode != PM_SUBTRACTION_MODE_DUAL) {
     1467
     1468            // the single-direction psf match code attempts to find the kernel such that:
     1469            // source * kernel = target.  we need to assign 'source' and 'target' correctly
     1470            // depending on which of image1 or image2 we asked to be convolved.
     1471
     1472            psKernel *target;           // Target postage stamp (convolve source to match the target)
     1473            psKernel *source;           // Source postage stamp (convolve source to match the target)
     1474            psArray *convolutions;      // Convolution postage stamps for each kernel basis function
     1475
     1476            // init the accumulation image
     1477            psImageInit(convolved1->image, 0.0);
     1478
     1479            switch (kernels->mode) {
     1480              case PM_SUBTRACTION_MODE_1:
     1481                target = stamp->image2;
     1482                source = stamp->image1;
     1483                convolutions = stamp->convolutions1;
     1484                break;
     1485              case PM_SUBTRACTION_MODE_2:
     1486                target = stamp->image1;
     1487                source = stamp->image2;
     1488                convolutions = stamp->convolutions2;
     1489                break;
     1490              default:
     1491                psAbort("Unsupported subtraction mode: %x", kernels->mode);
     1492            }
     1493
     1494            // generate the convolved source image (sum over kernels)
     1495            for (int j = 0; j < numKernels; j++) {
     1496                psKernel *convolution = convolutions->data[j]; // Convolution
     1497                double coefficient = p_pmSubtractionSolutionCoeff(kernels, polyValues, j, false); // Coefficient
     1498                for (int y = - footprint; y <= footprint; y++) {
     1499                    for (int x = - footprint; x <= footprint; x++) {
     1500                        convolved1->kernel[y][x] += convolution->kernel[y][x] * coefficient;
     1501                    }
     1502                }
     1503            }
     1504
     1505            // generate the residual image
     1506            for (int y = - footprint; y <= footprint; y++) {
     1507                for (int x = - footprint; x <= footprint; x++) {
     1508                    convolved1->kernel[y][x] += source->kernel[y][x] * norm;
     1509                    residual->kernel[y][x] = target->kernel[y][x] - convolved1->kernel[y][x] - background;
     1510                }
     1511            }
     1512            // XXX if we want to have a weight and window, we'll need to pass through to here
     1513            pmSubtractionChisqStats(fluxes, chisq, moments, convolved1, NULL, residual, weight, window);
     1514
     1515        } else {
     1516
     1517            // Dual convolution
     1518            psArray *convolutions1 = stamp->convolutions1; // Convolutions of the first image
     1519            psArray *convolutions2 = stamp->convolutions2; // Convolutions of the second image
     1520            psKernel *image1 = stamp->image1; // The first image
     1521            psKernel *image2 = stamp->image2; // The second image
     1522
     1523            // init the accumulation images
     1524            psImageInit(convolved1->image, 0.0);
     1525            psImageInit(convolved2->image, 0.0);
     1526
     1527            for (int j = 0; j < numKernels; j++) {
     1528                psKernel *conv1 = convolutions1->data[j]; // Convolution of first image
     1529                psKernel *conv2 = convolutions2->data[j]; // Convolution of second image
     1530                double coeff1 = p_pmSubtractionSolutionCoeff(kernels, polyValues, j, false); // Coefficient 1
     1531                double coeff2 = p_pmSubtractionSolutionCoeff(kernels, polyValues, j, true); // Coefficient 2
     1532
     1533                for (int y = - footprint; y <= footprint; y++) {
     1534                    for (int x = - footprint; x <= footprint; x++) {
     1535                        // NOTE sign for coeff2
     1536                        convolved1->kernel[y][x] += +conv1->kernel[y][x] * coeff1;
     1537                        convolved2->kernel[y][x] += -conv2->kernel[y][x] * coeff2;
     1538                    }
     1539                }
     1540            }
     1541
     1542            for (int y = - footprint; y <= footprint; y++) {
     1543                for (int x = - footprint; x <= footprint; x++) {
     1544                    convolved1->kernel[y][x] += image1->kernel[y][x] * norm;
     1545                    convolved2->kernel[y][x] += image2->kernel[y][x];
     1546                    residual->kernel[y][x] = convolved2->kernel[y][x] - convolved1->kernel[y][x] - background;
     1547                }
     1548            }
     1549
     1550            if (0) {
     1551                psFitsWriteImageSimple("conv1.fits", convolved1->image, NULL);
     1552                psFitsWriteImageSimple("conv2.fits", convolved2->image, NULL);
     1553                psFitsWriteImageSimple("resid.fits", residual->image,   NULL);
     1554                pmVisualAskUser(NULL);
     1555            }
     1556
     1557            pmSubtractionChisqStats(fluxes, chisq, moments, convolved1, convolved2, residual, weight, window);
     1558        }
     1559    }
     1560
     1561    pmSubtractionVisualPlotChisqAndMoments(fluxes, chisq, moments);
     1562
     1563    // find the mean chisq and mean moment
     1564    psStats *stats = psStatsAlloc(PS_STAT_SAMPLE_MEAN);
     1565    psVectorStats (stats, chisq, NULL, NULL, 0);
     1566    float chisqValue = stats->sampleMean;
     1567
     1568    psStatsInit(stats);
     1569    psVectorStats (stats, moments, NULL, NULL, 0);
     1570    float momentValue = stats->sampleMean;
     1571
     1572    fprintf (stderr, "chisq: %f, moment: %f\n", chisqValue, momentValue);
     1573
     1574    psFree(stats);
     1575    psFree(chisq);
     1576    psFree(fluxes);
     1577    psFree(moments);
     1578
     1579    psFree(residual);
     1580    psFree(convolved1);
     1581    psFree(convolved2);
     1582    psFree(polyValues);
     1583
    12821584    return true;
    12831585}
  • branches/eam_branches/ipp-20101205/psModules/src/imcombine/pmSubtractionEquation.h

    r29543 r30266  
    9292bool pmSubtractionCalculateMomentsKernel(double *Mxx, double *Myy, psKernel *image, int footprint, int window);
    9393
     94bool pmSubtractionChisqStats(psVector *fluxesVector, psVector *chisqVector, psVector *momentVector, psKernel *convolved1, psKernel *convolved2, psKernel *residual, psKernel *weight, psKernel *window);
     95
     96bool pmSubtractionCalculateChisqAndMoments(pmSubtractionStampList *stamps,
     97                                           pmSubtractionKernels *kernels);
     98
    9499#endif
  • branches/eam_branches/ipp-20101205/psModules/src/imcombine/pmSubtractionKernels.c

    r29597 r30266  
    3030    psFree(kernels->solution1);
    3131    psFree(kernels->solution2);
     32    psFree(kernels->solution1err);
     33    psFree(kernels->solution2err);
    3234    psFree(kernels->sampleStamps);
    3335}
     
    745747    kernels->solution1 = NULL;
    746748    kernels->solution2 = NULL;
     749    kernels->solution1err = NULL;
     750    kernels->solution2err = NULL;
    747751    kernels->mean = NAN;
    748752    kernels->rms = NAN;
     
    14351439    out->solution1 = in->solution1 ? psVectorCopy(NULL, in->solution1, PS_TYPE_F64) : NULL;
    14361440    out->solution2 = in->solution2 ? psVectorCopy(NULL, in->solution2, PS_TYPE_F64) : NULL;
     1441    out->solution1err = in->solution1err ? psVectorCopy(NULL, in->solution1err, PS_TYPE_F64) : NULL;
     1442    out->solution2err = in->solution2err ? psVectorCopy(NULL, in->solution2err, PS_TYPE_F64) : NULL;
    14371443    out->sampleStamps = psMemIncrRefCounter(in->sampleStamps);
    14381444
  • branches/eam_branches/ipp-20101205/psModules/src/imcombine/pmSubtractionKernels.h

    r29601 r30266  
    4848    pmSubtractionMode mode;             ///< Mode for subtraction
    4949    psVector *solution1, *solution2;    ///< Solution for the PSF matching
     50    psVector *solution1err, *solution2err; ///< error in solution for the PSF matching
    5051    // Quality information
    5152    float mean, rms;                    ///< Mean and RMS of chi^2 from stamps
  • branches/eam_branches/ipp-20101205/psModules/src/imcombine/pmSubtractionMatch.c

    r29596 r30266  
    743743                memCheck("   calculate deviations");
    744744
     745                pmSubtractionCalculateChisqAndMoments(stamps, kernels); // Stamp deviations
     746
    745747                psTrace("psModules.imcombine", 3, "Rejecting stamps...\n");
    746748                numRejected = pmSubtractionRejectStamps(kernels, stamps, deviations, subMask, rej);
  • branches/eam_branches/ipp-20101205/psModules/src/imcombine/pmSubtractionStamps.c

    r29543 r30266  
    812812    stamps->normWindow2 = 2.0*R2;
    813813    psLogMsg ("psModules.imcombine", PS_LOG_DETAIL, "Kron Radii: %f for 1, %f for 2\n", stamps->normWindow1, stamps->normWindow2);
     814
     815    // Generate a weighting window based on the kron radii
     816    {
     817        float radius = 1.5 * PS_MAX(R1, R2);
     818
     819        psImageInit(stamps->window->image, 0.0);
     820
     821        for (int y = -size; y <= size; y++) {
     822            for (int x = -size; x <= size; x++) {
     823                if (hypot(x,y) > radius) continue;
     824                stamps->window->kernel[y][x] = 1.0;
     825            }
     826        }
     827    }
    814828
    815829# else
  • branches/eam_branches/ipp-20101205/psModules/src/imcombine/pmSubtractionVisual.c

    r29600 r30266  
    210210        psImageOverlaySection(canvas, im, x0, y0, "=");
    211211        stampNum++;
     212
     213        // renormalize the section
     214        float maxValue = 0;
     215        for (int iy = 0; iy < im->numRows; iy++) {
     216            for (int ix = 0; ix < im->numCols; ix++) {
     217                maxValue = PS_MAX(maxValue, im->data.F64[iy][ix]);
     218            }
     219        }
     220        if (maxValue == 0.0) continue;
     221        for (int iy = 0; iy < im->numRows; iy++) {
     222            for (int ix = 0; ix < im->numCols; ix++) {
     223                canvas->data.F64[y0 + iy][x0 + ix] /= maxValue;
     224            }
     225        }
    212226    }
    213227
    214228    psImage *canvas32 = pmVisualImageToFloat(canvas);
    215229    pmVisualScaleImage(kapa1, canvas32, "Least_Squares", 0, true);
     230
     231    if (0) {
     232        static int count = 0;
     233        char filename[64];
     234        sprintf (filename, "chisq.%02d.fits", count);
     235        count ++;
     236        psFits *fits = psFitsOpen (filename, "w");
     237        psFitsWriteImage (fits, NULL, canvas32, 0, NULL);
     238        psFitsClose (fits);
     239    }
     240
     241    pmVisualAskUser(&plotLeastSquares);
     242    psFree(canvas);
     243    psFree(canvas32);
     244    return true;
     245}
     246
     247/** Plot the least-squares matrix of each stamp */
     248bool pmSubtractionVisualPlotLeastSquaresResid (const pmSubtractionStampList *stamps, psImage *matrixIn, int nUsed) {
     249
     250    if (!pmVisualTestLevel("ppsub.chisq", 1)) return true;
     251
     252    if (!plotLeastSquares) return true;
     253
     254    if (!pmVisualInitWindow (&kapa1, "PPSub:Images")) {
     255        return false;
     256    }
     257
     258    psImage *matrixNorm = psImageCopy(NULL, matrixIn, PS_TYPE_F64);
     259    {
     260        // renormalize the matrix
     261        float maxValue = 0;
     262        for (int iy = 0; iy < matrixNorm->numRows; iy++) {
     263            for (int ix = 0; ix < matrixNorm->numCols; ix++) {
     264                maxValue = PS_MAX(maxValue, matrixNorm->data.F64[iy][ix]);
     265            }
     266        }
     267        for (int iy = 0; iy < matrixNorm->numRows; iy++) {
     268            for (int ix = 0; ix < matrixNorm->numCols; ix++) {
     269                matrixNorm->data.F64[iy][ix] /= maxValue;
     270            }
     271        }
     272    }
     273
     274    // Find the stamp size
     275    int imageMax = -1;
     276    int numStamps = 0;
     277    psElemType type = PS_TYPE_F64;
     278
     279    for(int i = 0; i < stamps->num; i++) {
     280        pmSubtractionStamp *stamp = stamps->stamps->data[i];
     281        if (stamp == NULL) continue;
     282
     283        psImage *im = stamp->matrix;
     284        if (im == NULL) continue;
     285
     286        imageMax = PS_MAX(imageMax, im->numCols);
     287        imageMax = PS_MAX(imageMax, im->numRows);
     288        numStamps++;
     289        type = im->type.type;
     290    }
     291    if (imageMax == -1) return false;
     292
     293    int border = 15;
     294    imageMax += border;
     295    int tileRowCount = (int) ceil(sqrt(numStamps));
     296    int canvasX = tileRowCount * (imageMax);
     297    int canvasY = tileRowCount * (imageMax);
     298    psImage *canvas = psImageAlloc (canvasX, canvasY, type);
     299    psImageInit (canvas, NAN);
     300
     301    // overlay the images
     302    int stampNum = 0;
     303    int stampListNum = 0;
     304    while (stampNum < numStamps) {
     305        int x0 = (imageMax) * (stampNum % tileRowCount);
     306        int y0 = (imageMax) * (stampNum / tileRowCount);
     307
     308        pmSubtractionStamp *stamp = stamps->stamps->data[stampListNum++];
     309        if (stamp == NULL) continue;
     310
     311        psImage *im = stamp->matrix;
     312        if (im == NULL) continue;
     313
     314        stampNum++;
     315
     316        if (stamp->status != PM_SUBTRACTION_STAMP_USED) continue;
     317
     318        // renormalize the section
     319        float maxValue = 0;
     320        for (int iy = 0; iy < im->numRows; iy++) {
     321            for (int ix = 0; ix < im->numCols; ix++) {
     322                maxValue = PS_MAX(maxValue, im->data.F64[iy][ix]);
     323            }
     324        }
     325        if (maxValue == 0.0) continue;
     326        for (int iy = 0; iy < im->numRows; iy++) {
     327            for (int ix = 0; ix < im->numCols; ix++) {
     328                canvas->data.F64[y0 + iy][x0 + ix] = (im->data.F64[iy][ix] / maxValue - matrixNorm->data.F64[iy][ix]) / matrixNorm->data.F64[iy][ix];
     329            }
     330        }
     331    }
     332
     333    psImage *canvas32 = pmVisualImageToFloat(canvas);
     334    pmVisualRangeImage(kapa2, canvas32, "Least_Squares", 0, -100.0, 100.0);
    216335
    217336    if (0) {
     
    722841}
    723842
     843// plot log(flux) vs log(chisq), log(flux) vs log(moments), log(chisq) vs log(moments)
     844bool pmSubtractionVisualPlotChisqAndMoments(psVector *fluxes, psVector *chisq, psVector *moments) {
     845
     846    Graphdata graphdata;
     847    KapaSection section;
     848
     849    if (!pmVisualTestLevel("ppsub.fit", 1)) return true;
     850
     851    if (!pmVisualInitWindow(&kapa3, "ppSub:plots")) return false;
     852
     853    KapaClearSections (kapa3);
     854    KapaInitGraph (&graphdata);
     855    KiiResize(kapa3, 1500, 500);
     856
     857    psVector *lchi = psVectorAlloc (fluxes->n, PS_TYPE_F32);
     858    psVector *lflx = psVectorAlloc (fluxes->n, PS_TYPE_F32);
     859    psVector *lMxx = psVectorAlloc (fluxes->n, PS_TYPE_F32);
     860
     861    // construct the plot vectors
     862    for (int i = 0; i < fluxes->n; i++) {
     863        lchi->data.F32[i] = log10(chisq->data.F32[i]);
     864        lflx->data.F32[i] = log10(fluxes->data.F32[i]);
     865        lMxx->data.F32[i] = log10(moments->data.F32[i]);
     866    }
     867
     868    section.bg = KapaColorByName ("none"); // XXX probably should be 'none'
     869
     870    // section 1: lflux vs lchi
     871    section.dx = 0.33;
     872    section.dy = 1.00;
     873    section.x  = 0.00;
     874    section.y  = 0.00;
     875    section.name = psStringCopy ("flux.v.chi");
     876    KapaSetSection (kapa3, &section);
     877    psFree (section.name);
     878
     879    graphdata.color = KapaColorByName ("black");
     880    pmVisualScaleGraphdata(&graphdata, lflx, lchi, false);
     881    KapaSetLimits (kapa3, &graphdata);
     882
     883    KapaSetFont (kapa3, "helvetica", 14);
     884    KapaBox (kapa3, &graphdata);
     885    KapaSendLabel (kapa3, "log(flux)", KAPA_LABEL_XM);
     886    KapaSendLabel (kapa3, "log(chisq)", KAPA_LABEL_YM);
     887
     888    graphdata.color = KapaColorByName ("black");
     889    graphdata.ptype = 2;
     890    graphdata.size = 0.5;
     891    graphdata.style = 2;
     892
     893    KapaPrepPlot   (kapa3, lflx->n, &graphdata);
     894    KapaPlotVector (kapa3, lflx->n, lflx->data.F32, "x");
     895    KapaPlotVector (kapa3, lflx->n, lchi->data.F32, "y");
     896
     897    // section 2: lflux vs lMxx
     898    section.dx = 0.33;
     899    section.dy = 1.00;
     900    section.x  = 0.33;
     901    section.y  = 0.00;
     902    section.name = psStringCopy ("flux.v.mom");
     903    KapaSetSection (kapa3, &section);
     904    psFree (section.name);
     905
     906    graphdata.color = KapaColorByName ("black");
     907    pmVisualScaleGraphdata(&graphdata, lflx, lMxx, false);
     908    KapaSetLimits (kapa3, &graphdata);
     909
     910    KapaSetFont (kapa3, "helvetica", 14);
     911    KapaBox (kapa3, &graphdata);
     912    KapaSendLabel (kapa3, "log(flux)", KAPA_LABEL_XM);
     913    KapaSendLabel (kapa3, "log(moments)", KAPA_LABEL_YM);
     914
     915    graphdata.color = KapaColorByName ("black");
     916    graphdata.ptype = 2;
     917    graphdata.size = 0.5;
     918    graphdata.style = 2;
     919
     920    KapaPrepPlot   (kapa3, lflx->n, &graphdata);
     921    KapaPlotVector (kapa3, lflx->n, lflx->data.F32, "x");
     922    KapaPlotVector (kapa3, lflx->n, lMxx->data.F32, "y");
     923
     924    // section 1: lflux vs lchi
     925    section.dx = 0.33;
     926    section.dy = 1.00;
     927    section.x  = 0.66;
     928    section.y  = 0.00;
     929    section.name = psStringCopy ("chi.v.mom");
     930    KapaSetSection (kapa3, &section);
     931    psFree (section.name);
     932
     933    graphdata.color = KapaColorByName ("black");
     934    pmVisualScaleGraphdata(&graphdata, lchi, lMxx, false);
     935    KapaSetLimits (kapa3, &graphdata);
     936
     937    KapaSetFont (kapa3, "helvetica", 14);
     938    KapaBox (kapa3, &graphdata);
     939    KapaSendLabel (kapa3, "log(chisq)", KAPA_LABEL_XM);
     940    KapaSendLabel (kapa3, "log(moments)", KAPA_LABEL_YM);
     941
     942    graphdata.color = KapaColorByName ("black");
     943    graphdata.ptype = 2;
     944    graphdata.size = 0.5;
     945    graphdata.style = 2;
     946
     947    KapaPrepPlot   (kapa3, lflx->n, &graphdata);
     948    KapaPlotVector (kapa3, lflx->n, lchi->data.F32, "x");
     949    KapaPlotVector (kapa3, lflx->n, lMxx->data.F32, "y");
     950
     951    pmVisualAskUser(NULL);
     952    return true;
     953}
     954
    724955#else
    725956bool pmSubtractionVisualClose(void) {return true;}
  • branches/eam_branches/ipp-20101205/psModules/src/imcombine/pmSubtractionVisual.h

    r29601 r30266  
    66bool pmSubtractionVisualPlotStamps(pmSubtractionStampList *stamps, pmReadout *ro);
    77bool pmSubtractionVisualPlotLeastSquares(pmSubtractionStampList *stamps);
     8bool pmSubtractionVisualPlotLeastSquaresResid (const pmSubtractionStampList *stamps, psImage *matrixIn, int nUsed);
    89bool pmSubtractionVisualShowSubtraction(psImage *image, psImage *ref, psImage *sub);
    910bool pmSubtractionVisualShowFitInit(pmSubtractionStampList *stamps);
     
    1314bool pmSubtractionVisualShowKernels(pmSubtractionKernels *kernels);
    1415bool pmSubtractionVisualShowBasis(pmSubtractionStampList *stamps);
     16bool pmSubtractionVisualPlotChisqAndMoments(psVector *fluxes, psVector *chisq, psVector *moments);
    1517
    1618#endif
Note: See TracChangeset for help on using the changeset viewer.