IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 26502


Ignore:
Timestamp:
Jan 2, 2010, 6:13:37 PM (16 years ago)
Author:
eugene
Message:

adding window to dual convolution; fixing normalization for deconvolved-hermitians

Location:
branches/eam_branches/20091201/psModules/src/imcombine
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/20091201/psModules/src/imcombine/pmSubtractionEquation.c

    r26470 r26502  
    1515#include "pmSubtractionVisual.h"
    1616
    17 //#define TESTING                         // TESTING output for debugging; may not work with threads!
     17// #define TESTING                         // TESTING output for debugging; may not work with threads!
    1818
    1919#define USE_WEIGHT                      // Include weight (1/variance) in equation?
    20 #define USE_WINDOW                      // Include weight (1/variance) in equation?
     20// #define USE_WINDOW                      // Include weight (1/variance) in equation?
    2121
    2222
     
    157157            if (mode & PM_SUBTRACTION_EQUATION_KERNELS) {
    158158                vector->data.F64[iIndex] = sumIC * poly[iTerm];
    159                 if (!(mode & PM_SUBTRACTION_EQUATION_NORM)) {
     159                // XXX TEST for Hermitians: do not calculate A - norm*B - \sum(k x B),
     160                // instead, calculate A - \sum(k x B), with full hermitians
     161                if (0 && !(mode & PM_SUBTRACTION_EQUATION_NORM)) {
    160162                    // subtract norm * sumRC * poly[iTerm]
    161163                    psAssert (kernels->solution1, "programming error: define solution first!");
     
    228230                                      const psKernel *image2, // Image 2
    229231                                      const psKernel *weight,  // Weight image
     232                                      const psKernel *window,  // Window image
    230233                                      const psArray *convolutions1, // Convolutions of image 1 for each kernel
    231234                                      const psArray *convolutions2, // Convolutions of image 2 for each kernel
     
    291294                        ab *= wtVal;
    292295                    }
     296                    if (window) {
     297                        float wtVal = window->kernel[y][x];
     298                        aa *= wtVal;
     299                        bb *= wtVal;
     300                        ab *= wtVal;
     301                    }
    293302                    sumAA += aa;
    294303                    sumBB += bb;
     
    319328                    if (weight) {
    320329                        ab *= weight->kernel[y][x];
     330                    }
     331                    if (window) {
     332                        ab *= window->kernel[y][x];
    321333                    }
    322334                    sumAB += ab;
     
    365377                    i2 *= wtVal;
    366378                }
     379                if (window) {
     380                    float wtVal = window->kernel[y][x];
     381                    ai2 *= wtVal;
     382                    bi2 *= wtVal;
     383                    ai1 *= wtVal;
     384                    bi1 *= wtVal;
     385                    i1i2 *= wtVal;
     386                    a *= wtVal;
     387                    b *= wtVal;
     388                    i2 *= wtVal;
     389                }
    367390                sumAI2 += ai2;
    368391                sumBI2 += bi2;
     
    411434            if (weight) {
    412435                float wtVal = weight->kernel[y][x];
     436                i1 *= wtVal;
     437                i1i1 *= wtVal;
     438                one *= wtVal;
     439                i2 *= wtVal;
     440                i1i2 *= wtVal;
     441            }
     442            if (window) {
     443                float wtVal = window->kernel[y][x];
    413444                i1 *= wtVal;
    414445                i1i1 *= wtVal;
     
    516547            for (int xOrder = 0; xOrder <= spatialOrder - yOrder; xOrder++, index += numKernels) {
    517548                // Contribution to chi^2: a_i^2 P_i
     549                if (!isfinite(penalties->data.F32[i])) {
     550                    psAbort ("invalid penalty");
     551                }
    518552                matrix->data.F64[index][index] -= norm * penalties->data.F32[i];
    519553            }
     
    702736        break;
    703737      case PM_SUBTRACTION_MODE_DUAL:
    704         psAbort ("dual is disabled: need to add window and calculation mode");
    705738        if (new) {
    706739            stamp->matrix2 = psImageAlloc(numKernels * numSpatial, numKernels * numSpatial, PS_TYPE_F64);
     
    714747#endif
    715748        status = calculateDualMatrixVector(stamp->matrix1, stamp->vector1, stamp->matrix2, stamp->vector2,
    716                                            stamp->matrixX, stamp->image1, stamp->image2, weight,
     749                                           stamp->matrixX, stamp->image1, stamp->image2, weight, window,
    717750                                           stamp->convolutions1, stamp->convolutions2, kernels, polyValues,
    718751                                           footprint);
     
    15871620                }
    15881621            }
     1622
     1623            // XXX visualize the target, source, convolution and residual
     1624            pmSubtractionVisualShowFitAddStamp (image2, image1, residual, background, norm, i);
     1625
    15891626            for (int y = - footprint; y <= footprint; y++) {
    15901627                for (int x = - footprint; x <= footprint; x++) {
  • branches/eam_branches/20091201/psModules/src/imcombine/pmSubtractionKernels.c

    r26491 r26502  
    132132            kernels->preCalc->data[index] = NULL;
    133133            kernels->penalties->data.F32[index] = kernels->penalty * PS_SQR(PS_SQR(u) + PS_SQR(v));
     134            if (!isfinite(kernels->penalties->data.F32[index])) {
     135                psAbort ("invalid penalty");
     136            }
    134137
    135138            psTrace("psModules.imcombine", 7, "Kernel %d: %d %d\n", index, u, v);
     
    146149}
    147150
    148 bool pmSubtractionKernelPreCalcNormalize (pmSubtractionKernels *kernels, pmSubtractionKernelPreCalc *preCalc, int index, int size, int uOrder, int vOrder, float fwhm) {
     151bool pmSubtractionKernelPreCalcNormalize (pmSubtractionKernels *kernels, pmSubtractionKernelPreCalc *preCalc, int index, int size, int uOrder, int vOrder, float fwhm, bool AlardLuptonStyle) {
    149152
    150153    // Calculate moments
     
    157160    }
    158161
    159     // Normalize sum of kernel component to unity for even functions
    160     if (uOrder % 2 == 0 && vOrder % 2 == 0) {
    161         double sum = 0.0;   // Sum of kernel component
    162         for (int v = -size; v <= size; v++) {
    163             for (int u = -size; u <= size; u++) {
    164                 sum += preCalc->kernel->kernel[v][u];
    165             }
    166         }
    167         sum = 1.0 / sqrt(sum);
    168         psBinaryOp(preCalc->xKernel, preCalc->xKernel, "*", psScalarAlloc(sum, PS_TYPE_F32));
    169         psBinaryOp(preCalc->yKernel, preCalc->yKernel, "*", psScalarAlloc(sum, PS_TYPE_F32));
    170         psBinaryOp(preCalc->kernel->image, preCalc->kernel->image, "*", psScalarAlloc(PS_SQR(sum), PS_TYPE_F32));
    171 
    172 #if 1
    173         fprintf(stderr, "%d norm: %lf, null: %f\n", index, sum, preCalc->kernel->kernel[0][0]);
    174 #endif
    175                    
    176         preCalc->kernel->kernel[0][0] -= 1.0;
    177         moment *= PS_SQR(sum);
    178     }
    179 
    180 #if 1
     162    // we have 4 cases here:
     163    // 1) for odd functions, normalize the kernel by the maximum swing / Npix
     164    // 2) for even functions, normalize the kernel to unity
     165    // 3) for alard-lupton style normalization, subtract 1 from the 0,0 pixel for all even functions
     166    // 4) for deconvolved hermitians, subtract 1 from the 0,0 pixel for the 0,0 function(s)
     167
    181168    double sum = 0.0;   // Sum of kernel component
     169    double min = FLT_MAX;
     170    double max = FLT_MIN;
     171
    182172    for (int v = -size; v <= size; v++) {
    183173        for (int u = -size; u <= size; u++) {
    184174            sum += preCalc->kernel->kernel[v][u];
     175            min = PS_MIN(preCalc->kernel->kernel[v][u], min);
     176            max = PS_MAX(preCalc->kernel->kernel[v][u], max);
    185177        }
    186178    }
    187     fprintf(stderr, "%d sum: %lf\n", index, sum);
     179#if 1
     180    fprintf(stderr, "%d raw: %lf, null: %f, min: %lf, max: %lf, moment: %lf\n", index, sum, preCalc->kernel->kernel[0][0], min, max, moment);
     181#endif
     182
     183    // only even terms have non-zero sums
     184    if ((uOrder % 2 == 0) && (vOrder % 2 == 0)) {
     185        moment /= sum;
     186    } else {
     187        moment = 0.0;
     188    }
     189
     190    bool zeroNull = false;
     191    float scale1D = 1.0 / sqrt(sum);
     192    float scale2D = 1.0 / sum;
     193
     194    if (AlardLuptonStyle && (uOrder % 2 == 0 && vOrder % 2 == 0)) {
     195        zeroNull = true;
     196    }
     197    if (!AlardLuptonStyle && (uOrder == 0 && vOrder == 0)) {
     198        zeroNull = true;
     199    }
     200    if ((uOrder % 2) || (vOrder % 2)) {
     201        // scale2D = 1.0 / (preCalc->kernel->image->numCols * preCalc->kernel->image->numRows * max);
     202        scale2D = 1.0 / max;
     203        scale1D = sqrt(scale2D);
     204    }
     205
     206    psBinaryOp(preCalc->xKernel, preCalc->xKernel, "*", psScalarAlloc(scale1D, PS_TYPE_F32));
     207    psBinaryOp(preCalc->yKernel, preCalc->yKernel, "*", psScalarAlloc(scale1D, PS_TYPE_F32));
     208    psBinaryOp(preCalc->kernel->image, preCalc->kernel->image, "*", psScalarAlloc(scale2D, PS_TYPE_F32));
     209    if (zeroNull) {
     210        preCalc->kernel->kernel[0][0] -= 1.0;
     211    }
     212
     213#if 1
     214    sum = 0.0;   // Sum of kernel component
     215    min = FLT_MAX;
     216    max = FLT_MIN;
     217    for (int v = -size; v <= size; v++) {
     218        for (int u = -size; u <= size; u++) {
     219            sum += preCalc->kernel->kernel[v][u];
     220            min = PS_MIN(preCalc->kernel->kernel[v][u], min);
     221            max = PS_MAX(preCalc->kernel->kernel[v][u], max);
     222        }
     223    }
     224    fprintf(stderr, "%d mod: %lf, null: %f, min: %lf, max: %lf, scale: %f\n", index, sum, preCalc->kernel->kernel[0][0], min, max, scale2D);
    188225#endif
    189226
     
    196233    kernels->preCalc->data[index] = preCalc;
    197234    kernels->penalties->data.F32[index] = kernels->penalty * fabsf(moment);
     235    if (!isfinite(kernels->penalties->data.F32[index])) {
     236        psAbort ("invalid penalty");
     237    }
    198238
    199239    psTrace("psModules.imcombine", 7, "Kernel %d: %f %d %d %f\n", index,
     
    250290
    251291                pmSubtractionKernelPreCalc *preCalc = pmSubtractionKernelPreCalcAlloc(PM_SUBTRACTION_KERNEL_ISIS, uOrder, vOrder, size, sigma); // structure to hold precalculated values
    252                 pmSubtractionKernelPreCalcNormalize (kernels, preCalc, index, size, uOrder, vOrder, fwhms->data.F32[i]);
     292                pmSubtractionKernelPreCalcNormalize (kernels, preCalc, index, size, uOrder, vOrder, fwhms->data.F32[i], true);
    253293            }
    254294        }
     
    302342            for (int vOrder = 0; vOrder <= orders->data.S32[i] - uOrder; vOrder++, index++) {
    303343                pmSubtractionKernelPreCalc *preCalc = pmSubtractionKernelPreCalcAlloc(PM_SUBTRACTION_KERNEL_HERM, uOrder, vOrder, size, sigma); // structure to hold precalculated values
    304                 pmSubtractionKernelPreCalcNormalize (kernels, preCalc, index, size, uOrder, vOrder, fwhms->data.F32[i]);
     344                pmSubtractionKernelPreCalcNormalize (kernels, preCalc, index, size, uOrder, vOrder, fwhms->data.F32[i], true);
    305345            }
    306346        }
     
    338378        int gaussOrder = orders->data.S32[i]; // Polynomial order to apply to Gaussian
    339379        psStringAppend(&params, "(%.1f,%d)", fwhms->data.F32[i], orders->data.S32[i]);
    340         num += (gaussOrder + 1) * (gaussOrder + 2) / 2;
     380        num += PS_SQR(gaussOrder + 1);
    341381    }
    342382
     
    349389    // XXXXX hard-wired reference sigma for now of 1.7 pix (== 4.0 pix fwhm == 1.0 arcsec in simtest)
    350390    // generate the Gaussian deconvolution kernel
    351     # define DECONV_SIGMA 1.7
     391    # define DECONV_SIGMA 1.6
    352392    psKernel *kernelGauss = pmSubtractionDeconvolveGauss (size, DECONV_SIGMA);
     393
     394# if 1
     395    psArray *deconKernels = psArrayAllocEmpty(100);
     396# endif
    353397
    354398    // Set the kernel parameters
     
    357401        // Iterate over (u,v) order
    358402        for (int uOrder = 0; uOrder <= orders->data.S32[i]; uOrder++) {
    359             for (int vOrder = 0; vOrder <= orders->data.S32[i] - uOrder; vOrder++, index++) {
     403            for (int vOrder = 0; vOrder <= orders->data.S32[i]; vOrder++, index++) {
    360404
    361405                pmSubtractionKernelPreCalc *preCalc = pmSubtractionKernelPreCalcAlloc(PM_SUBTRACTION_KERNEL_HERM, uOrder, vOrder, size, sigma); // structure to hold precalculated values
     
    365409                preCalc->kernel = pmSubtractionDeconvolveKernel(kernelTarget, kernelGauss); // Kernel
    366410
     411                pmSubtractionKernelPreCalcNormalize (kernels, preCalc, index, size, uOrder, vOrder, fwhms->data.F32[i], false);
     412
    367413                // XXXX test demo that deconvolved kernel is valid
    368                 // psImage *kernelConv = psImageConvolveFFT(NULL, preCalc->kernel->image, NULL, 0, kernelGauss);
    369                 // pmSubtractionVisualShowSubtraction (kernelTarget->image, preCalc->kernel->image, kernelConv);
    370 
    371                 pmSubtractionKernelPreCalcNormalize (kernels, preCalc, index, size, uOrder, vOrder, fwhms->data.F32[i]);
     414# if 1
     415                psImage *kernelConv = psImageConvolveFFT(NULL, preCalc->kernel->image, NULL, 0, kernelGauss);
     416                psArrayAdd (deconKernels, 100, kernelConv);
     417                psFree (kernelConv);
     418
     419                if (!uOrder && !vOrder){
     420                    pmSubtractionVisualShowSubtraction (kernelTarget->image, preCalc->kernel->image, kernelConv);
     421                }
     422# endif
    372423            }
    373424        }
    374425    }
     426
     427# if 1
     428    psImage *dot = psImageAlloc(deconKernels->n, deconKernels->n, PS_TYPE_F32);
     429    for (int i = 0; i < deconKernels->n; i++) {
     430        for (int j = 0; j <= i; j++) {
     431            psImage *t1 = deconKernels->data[i];
     432            psImage *t2 = deconKernels->data[j];
     433
     434            double sum = 0.0;
     435            for (int iy = 0; iy < t1->numRows; iy++) {
     436                for (int ix = 0; ix < t1->numCols; ix++) {
     437                    sum += t1->data.F32[iy][ix] * t2->data.F32[iy][ix];
     438                }
     439            }
     440            dot->data.F32[j][i] = sum;
     441            dot->data.F32[i][j] = sum;
     442        }
     443    }
     444    pmSubtractionVisualShowSubtraction (dot, NULL, NULL);
     445    psFree (dot);
     446    psFree (deconKernels);
     447# endif
    375448
    376449    return kernels;
     
    857930                kernels->v->data.S32[index] = vOrder;
    858931                kernels->penalties->data.F32[index] = kernels->penalty * fabsf(moment);
     932                if (!isfinite(kernels->penalties->data.F32[index])) {
     933                    psAbort ("invalid penalty");
     934                }
    859935
    860936                psTrace("psModules.imcombine", 7, "Kernel %d: %d %d %d\n", index,
  • branches/eam_branches/20091201/psModules/src/imcombine/pmSubtractionStamps.c

    r26471 r26502  
    8181
    8282// Search a region for a suitable stamp
    83 bool stampSearch(int *xStamp, int *yStamp, // Coordinates of stamp, to return
     83bool stampSearch(float *xStamp, float *yStamp, // Coordinates of stamp, to return
    8484                 float *fluxStamp, // Flux of stamp, to return
    8585                 const psImage *image1, const psImage *image2, // Images to search
     
    116116                ((image1) ? image1->data.F32[y][x] : image2->data.F32[y][x]); // Flux at pixel
    117117            if (flux > *fluxStamp) {
    118                 *xStamp = x;
    119                 *yStamp = y;
     118                *xStamp = x + 0.5;
     119                *yStamp = y + 0.5;
    120120                *fluxStamp = flux;
    121121                found = true;
     
    405405            numSearch++;
    406406
    407             int xStamp = 0, yStamp = 0; // Coordinates of stamp
     407            float xStamp = 0.0, yStamp = 0.0; // Coordinates of stamp
    408408            float fluxStamp = -INFINITY; // Flux of stamp
    409409            bool goodStamp = false;     // Found a good stamp?
     
    437437                    goodStamp = stampSearch(&xStamp, &yStamp, &fluxStamp, image1, image2, thresh1, thresh2,
    438438                                            subMask, xMin, xMax, yMin, yMax, numCols, numRows, border);
     439
     440                    // XXX reset for a test:
     441                    xStamp = xList->data.F32[j];
     442                    yStamp = yList->data.F32[j];
     443                    fprintf (stderr, "find: %d %d ==> %5.1f %5.1f\n", xCentre, yCentre, xStamp, yStamp);
    439444                }
    440445            } else {
     
    543548        }
    544549
     550        fprintf (stderr, "stamp: %5.1f %5.1f == %d %d\n", xStamp, yStamp, xPix, yPix);
     551
    545552        bool found = false;
    546553        for (int j = 0; j < numStamps && !found; j++) {
     
    735742            return false;
    736743        }
     744        fprintf (stderr, "stamp: %5.1f %5.1f == %d %d\n", stamp->x, stamp->y, x, y);
    737745
    738746        // Catch memory leaks --- these should have been freed and NULLed before
     
    811819            y->data.F32[numOut] = source->peak->yf;
    812820        }
     821        fprintf (stderr, "stamp: %5.1f %5.1f\n", x->data.F32[numOut], y->data.F32[numOut]);
    813822        numOut++;
    814823    }
  • branches/eam_branches/20091201/psModules/src/imcombine/pmSubtractionVisual.c

    r26491 r26502  
    251251        int yPix = ySub * (2*footprint + 1 + 3) + footprint;
    252252
     253        double sum = 0.0;
    253254        for (int y = -footprint; y <= footprint; y++) {
    254255            for (int x = -footprint; x <= footprint; x++) {
    255256                output->data.F32[y + yPix][x + xPix] = kernel->kernel[y][x];
     257                sum += kernel->kernel[y][x];
    256258            }
    257259        }
     260        fprintf (stderr, "kernel %d, sum %f\n", i, sum);
    258261    }                                                   
    259262       
     
    274277
    275278    // choose the brightest stamp
    276     pmSubtractionStamp *maxStamp = stamps->stamps->data[0];
    277     float maxFlux = maxStamp->flux;
    278     for (int i = 1; i < stamps->num; i++) {
     279    pmSubtractionStamp *maxStamp = NULL;
     280    float maxFlux = NAN;
     281    for (int i = 0; i < stamps->num; i++) {
    279282        pmSubtractionStamp *stamp = stamps->stamps->data[i];
     283        if (!isfinite(stamp->flux)) continue;
     284        if (!stamp->convolutions1 && !stamp->convolutions2) continue;
     285        if (!maxStamp) {
     286            maxFlux = stamp->flux;
     287            maxStamp = stamp;
     288            continue;
     289        }
    280290        if (stamp->flux > maxFlux) {
    281291            maxFlux = stamp->flux;
    282292            maxStamp = stamp;
    283293        }
     294    }
     295
     296    if (!isfinite(maxStamp->flux)) {
     297        fprintf (stderr, "no valid stamps?\n");
    284298    }
    285299
     
    308322            int yPix = ySub * (2*footprint + 1 + 3) + footprint;
    309323           
     324            double sum = 0.0;
    310325            for (int y = -footprint; y <= footprint; y++) {
    311326                for (int x = -footprint; x <= footprint; x++) {
    312327                    output->data.F32[y + yPix][x + xPix] = kernel->kernel[y][x];
     328                    sum += kernel->kernel[y][x];
    313329                }
    314330            }
     331            fprintf (stderr, "kernel %d, sum %f\n", i, sum);
    315332        }               
    316333        pmVisualScaleImage(kapa2, output, "Image", 0, true);
     
    339356            int yPix = ySub * (2*footprint + 1 + 3) + footprint;
    340357           
     358            double sum = 0.0;
    341359            for (int y = -footprint; y <= footprint; y++) {
    342360                for (int x = -footprint; x <= footprint; x++) {
    343361                    output->data.F32[y + yPix][x + xPix] = kernel->kernel[y][x];
     362                    sum += kernel->kernel[y][x];
    344363                }
    345364            }
     365            fprintf (stderr, "kernel %d, sum %f\n", i, sum);
    346366        }               
    347367        pmVisualScaleImage(kapa2, output, "Image", 1, true);
     
    375395        overlay[Noverlay].type = KII_OVERLAY_BOX;
    376396        if ((stamp->x < 1.0) && (stamp->y < 1.0)) {
    377             fprintf (stderr, "stamp zero: %f %f\n", stamp->x, stamp->y);
     397            // fprintf (stderr, "stamp zero: %f %f\n", stamp->x, stamp->y);
    378398            continue;
    379399        }
    380400        if (!isfinite(stamp->x) && !isfinite(stamp->y)) {
    381             fprintf (stderr, "stamp nan: %f %f\n", stamp->x, stamp->y);
     401            // fprintf (stderr, "stamp nan: %f %f\n", stamp->x, stamp->y);
    382402            continue;
    383403        }
     
    409429bool pmSubtractionVisualShowFitInit(pmSubtractionStampList *stamps) {
    410430
    411     if (!pmVisualIsVisual()) return true;
     431    // if (!pmVisualIsVisual()) return true;
    412432
    413433    // generate 4 storage images large enough to hold the N stamps:
     
    446466bool pmSubtractionVisualShowFitAddStamp(psKernel *target, psKernel *source, psKernel *convolution, double background, double norm, int index) {
    447467
    448     if (!pmVisualIsVisual()) return true;
     468    // if (!pmVisualIsVisual()) return true;
    449469
    450470    double sum;
     
    517537bool pmSubtractionVisualShowFit() {
    518538
    519     if (!pmVisualIsVisual()) return true;
     539    // if (!pmVisualIsVisual()) return true;
    520540    if (!pmVisualInitWindow(&kapa1, "ppSub:Images")) return false;
    521541    if (!pmVisualInitWindow(&kapa2, "ppSub:Misc")) return false;
Note: See TracChangeset for help on using the changeset viewer.