IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 27708


Ignore:
Timestamp:
Apr 16, 2010, 12:52:37 PM (16 years ago)
Author:
Paul Price
Message:

Branching to test fix of variance/covariance calculation in ppSub.

The calculations of the variance and covariance are usually straight-forward --- the variance gets scaled by the square of any scaling factor applied to the image, and the covariance may be calculated from a convolution kernel. However, when two images are combined, it's not so straight-forward. The standard rules for the propagation of uncertainty tell us to sum the (full) covariance matrices, but it's not immediately clear how to do this in our model where we've separated the diagonal and off-diagonal elements. I had been summing the variance maps and averaging the covariance pseudo-matrices, but this appears to be robbing us of nearly 1 mag of depth. I've reworked this calculation to be a bit smarter: I normalise the (mean of the) variance maps and the covariance pseudo-matrices to unity, calculate from the normalisations the desired mean variance, sum the normalised variance maps with the appropriate scaling, and average (with weights) the covariance pseudo-matrices. This results in deeper photometry on the diff, but there are still a few things to consider before we put this into production: the effect on Magic and the effect on warp-stack and stack-stack diffs (should probably review stack variance/covariance in the light of this fix).

Location:
branches/pap
Files:
1 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/pap/ppSub/src/ppSubReadoutSubtract.c

    r26982 r27708  
    4848    pmReadout *outRO = pmFPAfileThisReadout(config->files, view, "PPSUB.OUTPUT");
    4949    outRO->image = (psImage*)psBinaryOp(outRO->image, minuend->image, "-", subtrahend->image);
    50     if (minuend->variance && subtrahend->variance) {
    51         outRO->variance = (psImage*)psBinaryOp(outRO->variance, minuend->variance, "+", subtrahend->variance);
    52     }
    5350    outRO->mask = (psImage*)psBinaryOp(outRO->mask, minuend->mask, "|", subtrahend->mask);
    5451
     52
     53    // Combining the variance is tricky.  The problem is, we're representing the variance by a variance map
     54    // and a covariance pseudo-matrix, and we cannot simply add them and get the correct result, because they
     55    // don't combine nicely.  It's like we want dD = aA + bB, but the upper- and lower-case variables can't be
     56    // merged.  Our solution is to combine the variance maps and covariance pseudo-matrices all normalised to
     57    // unity, and then scale the result by the desired variance normalisation.  It's not perfectly correct,
     58    // but it's gotta be good enough.
     59
     60    // Measure the variance scales
     61    psStats *varStats = psStatsAlloc(PS_STAT_ROBUST_MEDIAN); // Statistics for variance images
     62    psRandom *rng = psRandomAlloc(PS_RANDOM_TAUS);           // Random number generator
     63    psImageMaskType maskVal = pmConfigMaskGet("MASK.VALUE", config) |
     64        pmConfigMaskGet("BLANK", config); // Bits to mask in inputs
     65    psImageBackground(varStats, NULL, minuend->variance, minuend->mask, maskVal, rng);
     66    float minuendVar = varStats->robustMedian; // Mean variance for minuend
     67    psImageBackground(varStats, NULL, subtrahend->variance, subtrahend->mask, maskVal, rng);
     68    float subtrahendVar = varStats->robustMedian; // Mean variance for subtrahend
     69    psFree(varStats);
     70    psFree(rng);
     71
     72    // Measure and apply the covariance scales
     73    float minuendSum = psImageCovarianceSum(minuend->covariance); // Sum of covariance for minuend
     74    float subtrahendSum = psImageCovarianceSum(subtrahend->covariance); // Sum of covariance for subtrahend
     75    psBinaryOp(minuend->covariance->image, minuend->covariance->image, "/",
     76               psScalarAlloc(minuendSum, PS_TYPE_F32));
     77    psBinaryOp(subtrahend->covariance->image, subtrahend->covariance->image, "/",
     78               psScalarAlloc(subtrahendSum, PS_TYPE_F32));
     79
     80    // Combine the scaled covariances
    5581    psArray *covars = psArrayAlloc(2);  // Covariance pseudo-matrices
     82    psVector *covarWeights = psVectorAlloc(2, PS_TYPE_F32); // Weights for covariances
    5683    covars->data[0] = psMemIncrRefCounter(minuend->covariance);
    5784    covars->data[1] = psMemIncrRefCounter(subtrahend->covariance);
    58     outRO->covariance = psImageCovarianceAverage(covars);
     85    covarWeights->data.F32[0] = minuendSum * minuendVar;
     86    covarWeights->data.F32[1] = subtrahendSum * subtrahendVar;
     87    outRO->covariance = psImageCovarianceAverageWeighted(covars, covarWeights);
    5988    psFree(covars);
     89    psFree(covarWeights);
     90
     91    // Combine the variance maps
     92    int numCols = outRO->image->numCols, numRows = outRO->image->numRows; // Size of image
     93    outRO->variance = psImageAlloc(numCols, numRows, PS_TYPE_F32);
     94    // The factor of 0.5 in the target variance is because there's an addition here *and* an addition below,
     95    // and we only want the first to count (the second should add to 2 in the background).
     96    float target = (minuendVar * minuendSum + subtrahendVar * subtrahendSum); // Target mean variance
     97    fprintf(stderr, "%f %f %f %f --> %f %f\n", minuendVar, minuendSum, subtrahendVar, subtrahendSum, target, psImageCovarianceFactor(outRO->covariance));
     98    for (int y = 0; y < numRows; y++) {
     99        for (int x = 0; x < numCols; x++) {
     100            outRO->variance->data.F32[y][x] = target *
     101                (minuend->variance->data.F32[y][x] / minuendVar +
     102                 subtrahend->variance->data.F32[y][x] / subtrahendVar);
     103        }
     104    }
     105    psImageCovarianceTransfer(outRO->variance, outRO->covariance);
    60106
    61107    outRO->data_exists = true;
Note: See TracChangeset for help on using the changeset viewer.