IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 25869


Ignore:
Timestamp:
Oct 16, 2009, 6:37:29 PM (17 years ago)
Author:
Paul Price
Message:

Working on suppressing common convolutions in both images by masking out parameters that have no signal or are at odds with parameters in the other kernel.

File:
1 edited

Legend:

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

    r25860 r25869  
    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_VARIANCE                    // Include variance in equation?
     
    395395        for (int yOrder = 0, index = i; yOrder <= spatialOrder; yOrder++) {
    396396            for (int xOrder = 0; xOrder <= spatialOrder - yOrder; xOrder++, index += numKernels) {
     397                // Contribution to chi^2: a_i^2 P_i
    397398                matrix->data.F64[index][index] -= norm * penalties->data.F32[i];
    398399            }
     
    788789#endif
    789790
     791#if 0
    790792        int bgIndex = PM_SUBTRACTION_INDEX_BG(kernels); // Index for background
    791793        calculatePenalty(sumMatrix, sumVector, kernels, sumMatrix->data.F64[bgIndex][bgIndex]);
     794#endif
    792795
    793796#ifdef TESTING
     
    933936#endif
    934937
    935         psVector *permutation = NULL;       // Permutation vector, required for LU decomposition
    936         psImage *luMatrix = psMatrixLUDecomposition(NULL, &permutation, F);
    937         if (!luMatrix) {
    938             psError(PS_ERR_UNKNOWN, true, "LU Decomposition of least-squares matrix failed.\n");
     938        psVector *g = NULL;             // Solution!
     939        psVector *mask = psVectorAlloc(num, PS_TYPE_U8); // Mask of parameters
     940        psVectorInit(mask, 0);
     941        {
     942            g = psMatrixSolveSVD(g, F, h);
     943            if (!g) {
     944                psError(PS_ERR_UNKNOWN, false, "SVD solution of least-squares equation failed.\n");
     945                psFree(F);
     946                psFree(h);
     947                psFree(mask);
     948                return NULL;
     949            }
     950
     951            #define TOL 1.0e-5
     952            int normIndex = PM_SUBTRACTION_INDEX_NORM(kernels); // Index for normalisation
     953            double norm = g->data.F64[normIndex];               // Normalisation
     954            double thresh = norm * TOL;                         // Threshold for low parameters
     955            for (int j = 0; j < numParams2; j++) {
     956                double param1 = g->data.F64[j], param2 = g->data.F64[numParams + j]; // Parameters of interest
     957                fprintf(stderr, "%lf %lf   ", param1, param2);
     958                if (fabs(param1) < thresh) {
     959                    fprintf(stderr, "Parameter %d 1 below threshold\n", j);
     960                    for (int k = 0; k < numParams2; k++) {
     961                        // Matrix 1
     962                        F->data.F64[k][j] = 0.0;
     963                        F->data.F64[j][k] = 0.0;
     964#if 1
     965                        // Cross matrices
     966                        F->data.F64[numParams + j][k] = 0.0;
     967                        F->data.F64[j][numParams + k] = 0.0;
     968                        F->data.F64[k][numParams + j] = 0.0;
     969                        F->data.F64[numParams + k][j] = 0.0;
     970#endif
     971                    }
     972                    F->data.F64[bgIndex][j] = 0.0;
     973                    F->data.F64[j][bgIndex] = 0.0;
     974                    F->data.F64[normIndex][j] = 0.0;
     975                    F->data.F64[j][normIndex] = 0.0;
     976                    F->data.F64[j][j] = 1.0;
     977                    h->data.F64[j] = 0.0;
     978                    mask->data.U8[j] = 0xFF;
     979                }
     980                if (fabs(param2) < thresh) {
     981                    fprintf(stderr, "Parameter %d 2 below threshold\n", j);
     982                    for (int k = 0; k < numParams2; k++) {
     983                        // Matrix 2
     984                        F->data.F64[k][numParams + j] = 0.0;
     985                        F->data.F64[numParams + j][k] = 0.0;
     986#if 1
     987                        // Cross matrices
     988                        F->data.F64[numParams + j][k] = 0.0;
     989                        F->data.F64[j][numParams + k] = 0.0;
     990                        F->data.F64[k][numParams + j] = 0.0;
     991                        F->data.F64[numParams + k][j] = 0.0;
     992#endif
     993                    }
     994                    F->data.F64[bgIndex][numParams + j] = 0.0;
     995                    F->data.F64[numParams + j][bgIndex] = 0.0;
     996                    F->data.F64[normIndex][numParams + j] = 0.0;
     997                    F->data.F64[numParams + j][normIndex] = 0.0;
     998                    F->data.F64[numParams + j][numParams + j] = 1.0;
     999                    h->data.F64[numParams + j] = 0.0;
     1000                    mask->data.U8[numParams + j] = 0xFF;
     1001                }
     1002
     1003                if (!mask->data.U8[j] && !mask->data.U8[numParams + j]) {
     1004                    if (fabs(param1) < fabs(param2)) {
     1005                        fprintf(stderr, "Parameter %d 1 < 2\n", j);
     1006                        for (int k = 0; k < numParams2; k++) {
     1007                            // Matrix 1
     1008                            F->data.F64[k][j] = 0.0;
     1009                            F->data.F64[j][k] = 0.0;
     1010#if 0
     1011                            // Cross matrices
     1012                            F->data.F64[numParams + j][k] = 0.0;
     1013                            F->data.F64[j][numParams + k] = 0.0;
     1014                            F->data.F64[k][numParams + j] = 0.0;
     1015                            F->data.F64[numParams + k][j] = 0.0;
     1016#endif
     1017                        }
     1018                        F->data.F64[bgIndex][j] = 0.0;
     1019                        F->data.F64[j][bgIndex] = 0.0;
     1020                        F->data.F64[normIndex][j] = 0.0;
     1021                        F->data.F64[j][normIndex] = 0.0;
     1022                        F->data.F64[j][j] = 1.0;
     1023                        h->data.F64[j] = 0.0;
     1024                        mask->data.U8[j] = 0xFF;
     1025                    } else {
     1026                        fprintf(stderr, "Parameter %d 2 < 1\n", j);
     1027                        for (int k = 0; k < numParams2; k++) {
     1028                            // Matrix 1
     1029                            F->data.F64[k][numParams + j] = 0.0;
     1030                            F->data.F64[numParams + j][k] = 0.0;
     1031#if 0
     1032                            // Cross matrices
     1033                            F->data.F64[numParams + j][k] = 0.0;
     1034                            F->data.F64[j][numParams + k] = 0.0;
     1035                            F->data.F64[k][numParams + j] = 0.0;
     1036                            F->data.F64[numParams + k][j] = 0.0;
     1037#endif
     1038                        }
     1039                        F->data.F64[bgIndex][numParams + j] = 0.0;
     1040                        F->data.F64[numParams + j][bgIndex] = 0.0;
     1041                        F->data.F64[normIndex][numParams + j] = 0.0;
     1042                        F->data.F64[numParams + j][normIndex] = 0.0;
     1043                        F->data.F64[numParams + j][numParams + j] = 1.0;
     1044                        h->data.F64[numParams + j] = 0.0;
     1045                        mask->data.U8[numParams + j] = 0xFF;
     1046                    }
     1047                }
     1048            }
     1049        }
     1050
     1051        g = psMatrixSolveSVD(g, F, h);
     1052        if (!g) {
     1053            psError(PS_ERR_UNKNOWN, false, "SVD solution of least-squares equation failed.\n");
    9391054            psFree(F);
    9401055            psFree(h);
    941             psFree(luMatrix);
    942             psFree(permutation);
     1056            psFree(mask);
    9431057            return NULL;
    9441058        }
     1059        for (int i = 0; i < num; i++) {
     1060            if (mask->data.U8[i]) {
     1061                g->data.F64[i] = 0.0;
     1062            }
     1063        }
     1064        psFree(mask);
     1065
    9451066        psFree(F);
    946 
    947         psVector *g = psMatrixLUSolution(NULL, luMatrix, h, permutation); // Solution!
    948         if (!g) {
    949             psError(PS_ERR_UNKNOWN, true, "LU Decomposition of least-squares matrix failed.\n");
    950             psFree(h);
    951             psFree(luMatrix);
    952             psFree(permutation);
    953             return NULL;
    954         }
    955         psFree(permutation);
    956         psFree(luMatrix);
    9571067        psFree(h);
     1068
     1069#ifdef TESTING
     1070        {
     1071            psImage *image = psImageAlloc(1, num, PS_TYPE_F64);
     1072            psFits *fits = psFitsOpen("solnVector.fits", "w");
     1073            for (int i = 0; i < num; i++) {
     1074                image->data.F64[0][i] = g->data.F64[i];
     1075            }
     1076            psFitsWriteImage(fits, NULL, image, 0, NULL);
     1077            psFree(image);
     1078            psFitsClose(fits);
     1079        }
     1080#endif
    9581081
    9591082        if (!kernels->solution1) {
Note: See TracChangeset for help on using the changeset viewer.