Changeset 26703
- Timestamp:
- Jan 28, 2010, 4:16:19 AM (16 years ago)
- Location:
- branches/eam_branches/20091201
- Files:
-
- 8 edited
-
ippconfig/recipes/ppSub.config (modified) (2 diffs)
-
ppStack/src/ppStackMatch.c (modified) (2 diffs)
-
ppSub/src/ppSubMatchPSFs.c (modified) (9 diffs)
-
psModules/src/imcombine/pmSubtractionEquation.c (modified) (18 diffs)
-
psModules/src/imcombine/pmSubtractionMatch.c (modified) (13 diffs)
-
psModules/src/imcombine/pmSubtractionMatch.h (modified) (1 diff)
-
psModules/src/imcombine/pmSubtractionStamps.c (modified) (19 diffs)
-
psModules/src/imcombine/pmSubtractionStamps.h (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branches/20091201/ippconfig/recipes/ppSub.config
r26689 r26703 2 2 3 3 KERNEL.TYPE STR ISIS_RADIAL # Kernel type to use (POIS|ISIS|SPAM|FRIES|GUNK|RINGS) 4 KERNEL.SIZE S32 3 0# Kernel half-size (pixels)4 KERNEL.SIZE S32 35 # Kernel half-size (pixels) 5 5 SPATIAL.ORDER S32 1 # Spatial polynomial order 6 6 REGION.SIZE F32 0 # Iso-kernel region size (pixels) 7 7 SOURCE.RADIUS F32 3.0 # Source matching radius (pixels) 8 8 STAMP.SPACING F32 300 # Typical spacing between stamps (pixels) 9 STAMP.FOOTPRINT S32 35# Size of stamps (pixels)9 STAMP.FOOTPRINT S32 40 # Size of stamps (pixels) 10 10 STAMP.THRESHOLD F32 5 # Flux threshold for stamps (stdev above background) 11 11 STRIDE S32 100 # Size of convolution patches (pixels) 12 ITER S32 10# Number of rejection iterations12 ITER S32 2 # Number of rejection iterations 13 13 REJ F32 2.5 # Rejection level (std dev) 14 14 KERNEL.ERR F32 0.05 # Relative systematic error in kernel 15 15 SYS.ERR F32 0.1 # Relative systematic error in images 16 16 SKY.ERR F32 0.0 # Relative systematic error in images 17 NORM.FRAC F32 0.03 # Fraction of window for normalisation window 17 18 18 19 MASK.VAL STR MASK.VALUE,CONV.BAD # Mask value for input … … 73 74 STACK METADATA 74 75 KERNEL.TYPE STR ISIS # Kernel type 76 SCALE.REF F32 9.0 # Reference for kernel parameter scaling 75 77 END 76 78 -
branches/eam_branches/20091201/ppStack/src/ppStackMatch.c
r26697 r26703 298 298 float rej = psMetadataLookupF32(NULL, ppsub, "REJ"); // Rejection threshold 299 299 float kernelError = psMetadataLookupF32(NULL, ppsub, "KERNEL.ERR"); // Relative systematic error in kernel 300 float normFrac = psMetadataLookupF32(NULL, recipe, "NORM.FRAC"); // Fraction of window for normalisn windw 300 301 float sysError = psMetadataLookupF32(NULL, ppsub, "SYS.ERR"); // Relative systematic error in images 301 302 float skyErr = psMetadataLookupF32(NULL, ppsub, "SKY.ERR"); // Additional error in sky … … 444 445 threshold, stampSources, stampsName, type, size, order, widthsCopy, 445 446 orders, inner, ringsOrder, binning, penalty, 446 optimum, optWidths, optOrder, optThresh, iter, rej, sysError, skyErr,447 kernelError, maskVal, maskBad, maskPoor, poorFrac, badFrac,448 PM_SUBTRACTION_MODE_2)) {447 optimum, optWidths, optOrder, optThresh, iter, rej, normFrac, 448 sysError, skyErr, kernelError, maskVal, maskBad, maskPoor, 449 poorFrac, badFrac, PM_SUBTRACTION_MODE_2)) { 449 450 psError(PS_ERR_UNKNOWN, false, "Unable to match images."); 450 451 psFree(fake); -
branches/eam_branches/20091201/ppSub/src/ppSubMatchPSFs.c
r26667 r26703 22 22 #include "ppSub.h" 23 23 24 #define COVAR_FRAC 0.01 // Truncation fraction for covariance matrix 25 24 26 // Normalise a region on an image 25 27 static void normaliseRegion(psImage *image, // Image to normalise … … 38 40 } 39 41 42 #if 0 40 43 // Measure the PSF for an image 41 44 static float subImagePSF(ppSubData *data, // Processing data … … 85 88 return fwhm; 86 89 } 90 #endif 87 91 88 92 // Scale the kernel parameters according to the PSFs … … 114 118 } 115 119 120 #if 0 116 121 // Input images 117 122 pmReadout *inRO = pmFPAfileThisReadout(config->files, view, "PPSUB.INPUT"); // Input readout … … 133 138 float inFWHM = subImagePSF(data, inRO, inSources); // FWHM for input 134 139 float refFWHM = subImagePSF(data, refRO, refSources); // FWHM for reference 140 #else 141 float inFWHM = 7.631371; 142 float refFWHM = 10.005879; 143 #endif 144 135 145 psLogMsg("ppSub", PS_LOG_INFO, "Input FWHM: %f\nReference FWHM: %f\n", inFWHM, refFWHM); 136 146 if (!isfinite(inFWHM) || !isfinite(refFWHM)) { … … 252 262 float rej = psMetadataLookupF32(NULL, recipe, "REJ"); // Rejection threshold 253 263 float kernelErr = psMetadataLookupF32(NULL, recipe, "KERNEL.ERR"); // Relative systematic error in kernel 264 float normFrac = psMetadataLookupF32(NULL, recipe, "NORM.FRAC"); // Fraction of window for normalisn windw 254 265 float sysErr = psMetadataLookupF32(NULL, recipe, "SYS.ERR"); // Relative systematic error in images 255 266 float skyErr = psMetadataLookupF32(NULL, recipe, "SKY.ERR"); // Additional error in sky … … 306 317 } 307 318 319 if (inRO->covariance) { 320 psImageCovarianceTruncate(inRO->covariance, COVAR_FRAC); 321 } 322 if (refRO->covariance) { 323 psImageCovarianceTruncate(refRO->covariance, COVAR_FRAC); 324 } 325 308 326 // Match the PSFs 309 327 bool success = false; // Operation was successful? … … 315 333 spacing, threshold, sources, data->stamps, type, size, order, 316 334 widths, orders, inner, ringsOrder, binning, penalty, optimum, 317 optWidths, optOrder, optThresh, iter, rej, sysErr, skyErr, kernelErr, maskVal, 318 maskBad, maskPoor, poorFrac, badFrac, subMode); 335 optWidths, optOrder, optThresh, iter, rej, normFrac, 336 sysErr, skyErr, kernelErr, maskVal, maskBad, maskPoor, 337 poorFrac, badFrac, subMode); 319 338 } 320 339 … … 407 426 pmConceptsCopyFPA(refConv->parent->parent->parent, refRO->parent->parent->parent, true, true); 408 427 428 if (inConv->covariance) { 429 psImageCovarianceTruncate(inConv->covariance, COVAR_FRAC); 430 } 431 if (refConv->covariance) { 432 psImageCovarianceTruncate(refConv->covariance, COVAR_FRAC); 433 } 434 409 435 if (inConv->variance) { 410 436 psImageCovarianceTransfer(inConv->variance, inConv->covariance); -
branches/eam_branches/20091201/psModules/src/imcombine/pmSubtractionEquation.c
r26702 r26703 222 222 static bool calculateDualMatrixVector(psImage *matrix, // Least-squares matrix, updated 223 223 psVector *vector, // Least-squares vector, updated 224 double *norm, // Normalisation, updated 224 225 const psKernel *image1, // Image 1 225 226 const psKernel *image2, // Image 2 … … 231 232 const psImage *polyValues, // Spatial polynomial values 232 233 int footprint, // (Half-)Size of stamp 233 const pmSubtractionEquationCalculationMode mode 234 int normWindow, // Window (half-)size for normalisation measurement 235 const pmSubtractionEquationCalculationMode mode 234 236 ) 235 237 { … … 315 317 // Spatial variation of kernel coeffs 316 318 if (mode & PM_SUBTRACTION_EQUATION_KERNELS) { 317 for (int iTerm = 0, iIndex = i; iTerm < numPoly; iTerm++, iIndex += numKernels) {318 for (int jTerm = 0, jIndex = j; jTerm < numPoly; jTerm++, jIndex += numKernels) {319 double aa = sumAA * poly2[iTerm][jTerm];320 double bb = sumBB * poly2[iTerm][jTerm];321 double ab = sumAB * poly2[iTerm][jTerm];322 323 matrix->data.F64[iIndex][jIndex] = aa;324 matrix->data.F64[jIndex][iIndex] = aa;325 326 matrix->data.F64[iIndex + numParams][jIndex + numParams] = bb;327 matrix->data.F64[jIndex + numParams][iIndex + numParams] = bb;328 329 matrix->data.F64[iIndex][jIndex + numParams] = ab;330 matrix->data.F64[jIndex + numParams][iIndex] = ab;331 }332 }333 }319 for (int iTerm = 0, iIndex = i; iTerm < numPoly; iTerm++, iIndex += numKernels) { 320 for (int jTerm = 0, jIndex = j; jTerm < numPoly; jTerm++, jIndex += numKernels) { 321 double aa = sumAA * poly2[iTerm][jTerm]; 322 double bb = sumBB * poly2[iTerm][jTerm]; 323 double ab = sumAB * poly2[iTerm][jTerm]; 324 325 matrix->data.F64[iIndex][jIndex] = aa; 326 matrix->data.F64[jIndex][iIndex] = aa; 327 328 matrix->data.F64[iIndex + numParams][jIndex + numParams] = bb; 329 matrix->data.F64[jIndex + numParams][iIndex + numParams] = bb; 330 331 matrix->data.F64[iIndex][jIndex + numParams] = ab; 332 matrix->data.F64[jIndex + numParams][iIndex] = ab; 333 } 334 } 335 } 334 336 } 335 337 for (int j = 0; j < i; j++) { … … 351 353 // Spatial variation of kernel coeffs 352 354 if (mode & PM_SUBTRACTION_EQUATION_KERNELS) { 353 for (int iTerm = 0, iIndex = i; iTerm < numPoly; iTerm++, iIndex += numKernels) {354 for (int jTerm = 0, jIndex = j; jTerm < numPoly; jTerm++, jIndex += numKernels) {355 double ab = sumAB * poly2[iTerm][jTerm];356 matrix->data.F64[iIndex][jIndex + numParams] = ab;357 matrix->data.F64[jIndex + numParams][iIndex] = ab;358 }359 }360 }355 for (int iTerm = 0, iIndex = i; iTerm < numPoly; iTerm++, iIndex += numKernels) { 356 for (int jTerm = 0, jIndex = j; jTerm < numPoly; jTerm++, jIndex += numKernels) { 357 double ab = sumAB * poly2[iTerm][jTerm]; 358 matrix->data.F64[iIndex][jIndex + numParams] = ab; 359 matrix->data.F64[jIndex + numParams][iIndex] = ab; 360 } 361 } 362 } 361 363 } 362 364 … … 419 421 420 422 if ((mode & PM_SUBTRACTION_EQUATION_NORM) && (mode & PM_SUBTRACTION_EQUATION_KERNELS)) { 421 matrix->data.F64[iIndex][normIndex] = ai1;422 matrix->data.F64[normIndex][iIndex] = ai1;423 matrix->data.F64[iIndex + numParams][normIndex] = bi1;424 matrix->data.F64[normIndex][iIndex + numParams] = bi1;425 }423 matrix->data.F64[iIndex][normIndex] = ai1; 424 matrix->data.F64[normIndex][iIndex] = ai1; 425 matrix->data.F64[iIndex + numParams][normIndex] = bi1; 426 matrix->data.F64[normIndex][iIndex + numParams] = bi1; 427 } 426 428 if ((mode & PM_SUBTRACTION_EQUATION_BG) && (mode & PM_SUBTRACTION_EQUATION_KERNELS)) { 427 matrix->data.F64[iIndex][bgIndex] = a;428 matrix->data.F64[bgIndex][iIndex] = a;429 matrix->data.F64[iIndex + numParams][bgIndex] = b;430 matrix->data.F64[bgIndex][iIndex + numParams] = b;431 }429 matrix->data.F64[iIndex][bgIndex] = a; 430 matrix->data.F64[bgIndex][iIndex] = a; 431 matrix->data.F64[iIndex + numParams][bgIndex] = b; 432 matrix->data.F64[bgIndex][iIndex + numParams] = b; 433 } 432 434 if (mode & PM_SUBTRACTION_EQUATION_KERNELS) { 433 vector->data.F64[iIndex] = ai2;434 vector->data.F64[iIndex + numParams] = bi2;435 vector->data.F64[iIndex] = ai2; 436 vector->data.F64[iIndex + numParams] = bi2; 435 437 if (!(mode & PM_SUBTRACTION_EQUATION_NORM)) { 436 438 // subtract norm * sumRC * poly[iTerm] … … 441 443 vector->data.F64[iIndex + numParams] -= norm * bi1; 442 444 } 443 }445 } 444 446 } 445 447 } … … 450 452 double sumI2 = 0.0; // Sum of I_2 (for vector, background) 451 453 double sumI1I2 = 0.0; // Sum of I_1.I_2 (for vector, normalisation) 454 double normI1 = 0.0, normI2 = 0.0; // Sum of I_1 and I_2 within the normalisation window 452 455 for (int y = - footprint; y <= footprint; y++) { 453 456 for (int x = - footprint; x <= footprint; x++) { … … 458 461 double one = 1.0; 459 462 double i1i2 = i1 * i2; 463 464 if (PS_SQR(x) + PS_SQR(y) <= PS_SQR(normWindow)) { 465 normI1 += i1; 466 normI2 += i2; 467 } 460 468 461 469 if (weight) { … … 482 490 } 483 491 } 492 493 *norm = normI2 / normI1; 494 495 fprintf(stderr, "Sums: %f %f %f\n", normI1, normI2, *norm); 496 484 497 if (mode & PM_SUBTRACTION_EQUATION_NORM) { 485 matrix->data.F64[normIndex][normIndex] = sumI1I1;486 vector->data.F64[normIndex] = sumI1I2;498 matrix->data.F64[normIndex][normIndex] = sumI1I1; 499 vector->data.F64[normIndex] = sumI1I2; 487 500 } 488 501 if (mode & PM_SUBTRACTION_EQUATION_BG) { 489 matrix->data.F64[bgIndex][bgIndex] = sum1;490 vector->data.F64[bgIndex] = sumI2;502 matrix->data.F64[bgIndex][bgIndex] = sum1; 503 vector->data.F64[bgIndex] = sumI2; 491 504 } 492 505 if ((mode & PM_SUBTRACTION_EQUATION_NORM) && (mode & PM_SUBTRACTION_EQUATION_BG)) { 493 matrix->data.F64[bgIndex][normIndex] = sumI1;494 matrix->data.F64[normIndex][bgIndex] = sumI1;506 matrix->data.F64[bgIndex][normIndex] = sumI1; 507 matrix->data.F64[normIndex][bgIndex] = sumI1; 495 508 } 496 509 return true; … … 512 525 int spatialOrder = kernels->spatialOrder; // Order of spatial variations 513 526 int numKernels = kernels->num; // Number of kernel components 514 for (int i = 0; i < numKernels; i++) { 527 int numSpatial = PM_SUBTRACTION_POLYTERMS(spatialOrder); // Number of spatial variations 528 int numParams = numKernels * numSpatial; // Number of kernel parameters 529 for (int i = 0; i < numParams; i++) { 515 530 for (int yOrder = 0, index = i; yOrder <= spatialOrder; yOrder++) { 516 531 for (int xOrder = 0; xOrder <= spatialOrder - yOrder; xOrder++, index += numKernels) { … … 518 533 psAssert(isfinite(penalties->data.F32[i]), "Invalid penalty"); 519 534 matrix->data.F64[index][index] += norm * penalties->data.F32[i]; 535 if (kernels->mode == PM_SUBTRACTION_MODE_DUAL) { 536 matrix->data.F64[index + numParams + 2][index + numParams + 2] += norm * penalties->data.F32[i]; 537 } 520 538 } 521 539 } … … 708 726 break; 709 727 case PM_SUBTRACTION_MODE_DUAL: 710 status = calculateDualMatrixVector(stamp->matrix, stamp->vector,stamp->image1, stamp->image2, 728 status = calculateDualMatrixVector(stamp->matrix, stamp->vector, &stamp->norm, 729 stamp->image1, stamp->image2, 711 730 weight, window, stamp->convolutions1, stamp->convolutions2, 712 kernels, polyValues, footprint, mode);731 kernels, polyValues, footprint, stamps->normWindow, mode); 713 732 break; 714 733 default: … … 751 770 } 752 771 753 bool pmSubtractionCalculateEquation(pmSubtractionStampList *stamps, const pmSubtractionKernels *kernels, const pmSubtractionEquationCalculationMode mode) 772 bool pmSubtractionCalculateEquation(pmSubtractionStampList *stamps, const pmSubtractionKernels *kernels, 773 const pmSubtractionEquationCalculationMode mode) 754 774 { 755 775 PM_ASSERT_SUBTRACTION_STAMP_LIST_NON_NULL(stamps, false); … … 1219 1239 psVectorInit(sumVector, 0.0); 1220 1240 1241 psVector *norms = psVectorAllocEmpty(stamps->num, PS_TYPE_F64); // Normalisations 1242 1221 1243 int numStamps = 0; // Number of good stamps 1222 1244 for (int i = 0; i < stamps->num; i++) { … … 1226 1248 (void)psBinaryOp(sumVector, sumVector, "+", stamp->vector); 1227 1249 1250 psVectorAppend(norms, stamp->norm); 1251 1228 1252 pmSubtractionStampPrint(ds9, stamp->x, stamp->y, stamps->footprint, "green"); 1229 1253 numStamps++; … … 1246 1270 1247 1271 #if 0 1272 // Regular, straight-forward solution 1273 solution = psMatrixSolveSVD(solution, sumMatrix, sumVector, NAN); 1274 #else 1248 1275 { 1249 solution = psMatrixSolveSVD(solution, sumMatrix, sumVector, NAN); 1250 if (!solution) { 1251 psError(PS_ERR_UNKNOWN, false, "SVD solution of least-squares equation failed.\n"); 1276 // Solve normalisation and background separately 1277 int normIndex = PM_SUBTRACTION_INDEX_NORM(kernels); // Index for normalisation 1278 1279 #if 0 1280 psImage *normMatrix = psImageAlloc(2, 2, PS_TYPE_F64); 1281 psVector *normVector = psVectorAlloc(2, PS_TYPE_F64); 1282 1283 normMatrix->data.F64[0][0] = sumMatrix->data.F64[normIndex][normIndex]; 1284 normMatrix->data.F64[1][1] = sumMatrix->data.F64[bgIndex][bgIndex]; 1285 normMatrix->data.F64[0][1] = normMatrix->data.F64[1][0] = sumMatrix->data.F64[normIndex][bgIndex]; 1286 1287 normVector->data.F64[0] = sumVector->data.F64[normIndex]; 1288 normVector->data.F64[1] = sumVector->data.F64[bgIndex]; 1289 1290 psVector *normSolution = psMatrixSolveSVD(NULL, normMatrix, normVector, NAN); 1291 1292 double normValue = normSolution->data.F64[0]; 1293 double bgValue = normSolution->data.F64[1]; 1294 1295 psFree(normMatrix); 1296 psFree(normVector); 1297 psFree(normSolution); 1298 #endif 1299 1300 psStats *stats = psStatsAlloc(PS_STAT_ROBUST_MEDIAN); // Statistics for norm 1301 if (!psVectorStats(stats, norms, NULL, NULL, 0)) { 1302 psError(PS_ERR_UNKNOWN, false, "Unable to determine median normalisation"); 1303 psFree(stats); 1252 1304 psFree(sumMatrix); 1253 1305 psFree(sumVector); 1254 return NULL; 1255 } 1256 1257 #ifdef TESTING 1258 for (int i = 0; i < numParams; i++) { 1259 fprintf(stderr, "Solution %d: %lf\n", i, solution->data.F64[i]); 1260 } 1261 #endif 1262 1263 int numSpatial = PM_SUBTRACTION_POLYTERMS(kernels->spatialOrder); // Number of spatial variations 1264 int numKernels = kernels->num; // Number of kernel basis functions 1265 1266 #define MASK_BASIS(INDEX) \ 1267 { \ 1268 for (int j = 0, index = INDEX; j < numSpatial; j++, index += numKernels) { \ 1269 for (int k = 0; k < numParams; k++) { \ 1270 sumMatrix->data.F64[index][k] = sumMatrix->data.F64[k][index] = 0.0; \ 1271 } \ 1272 sumMatrix->data.F64[index][index] = 1.0; \ 1273 sumVector->data.F64[index] = 0.0; \ 1274 } \ 1275 } 1276 1277 #define TOL 1.0e-3 1278 int normIndex = PM_SUBTRACTION_INDEX_NORM(kernels); // Index for normalisation 1279 double norm = fabs(solution->data.F64[normIndex]); // Normalisation 1280 double thresh = norm * TOL; // Threshold for low parameters 1281 for (int i = 0; i < numKernels; i++) { 1282 // Getting 0th order parameter value. In the presence of spatial variation, the actual value 1283 // of the parameter will vary over the image. We are in effect getting the value in the 1284 // centre of the image. If we use different polynomial functions (e.g., Chebyshev), we may 1285 // have to change this to properly determine the value of the parameter at the centre. 1286 double param1 = solution->data.F64[i], 1287 param2 = solution->data.F64[numSolution1 + i]; // Parameters of interest 1288 bool mask1 = false, mask2 = false; // Masked the parameter? 1289 if (fabs(param1) < thresh) { 1290 psTrace("psModules.imcombine", 7, "Parameter %d: 1 below threshold\n", i); 1291 MASK_BASIS(i); 1292 mask1 = true; 1293 } 1294 if (fabs(param2) < thresh) { 1295 psTrace("psModules.imcombine", 7, "Parameter %d: 2 below threshold\n", i); 1296 MASK_BASIS(numSolution1 + i); 1297 mask2 = true; 1298 } 1299 1300 if (!mask1 && !mask2) { 1301 if (fabs(param1) > fabs(param2)) { 1302 psTrace("psModules.imcombine", 7, "Parameter %d: 1 > 2\n", i); 1303 MASK_BASIS(numSolution1 + i); 1304 } else { 1305 psTrace("psModules.imcombine", 7, "Parameter %d: 2 > 1\n", i); 1306 MASK_BASIS(i); 1307 } 1308 } 1309 } 1310 1311 #ifdef TESTING 1312 psFitsWriteImageSimple ("sumMatrixFix.fits", sumMatrix, NULL); 1313 psVectorWriteFile("sumVectorFix.dat", sumVector); 1314 #endif 1315 } 1316 #endif /*** kernel-masking block ***/ 1317 solution = psMatrixSolveSVD(solution, sumMatrix, sumVector, NAN); 1306 psFree(norms); 1307 return false; 1308 } 1309 1310 double normValue = stats->robustMedian; 1311 // double bgValue = 0.0; 1312 1313 psFree(stats); 1314 1315 fprintf(stderr, "Norm: %lf\n", normValue); 1316 // fprintf(stderr, "BG: %lf\n", bgValue); 1317 1318 // Solve kernel components 1319 for (int i = 0; i < numSolution2; i++) { 1320 sumVector->data.F64[i] -= normValue * sumMatrix->data.F64[normIndex][i]; // + bgValue * sumMatrix->data.F64[bgIndex][i]; 1321 sumVector->data.F64[i + numSolution1] -= normValue * sumMatrix->data.F64[normIndex][i + numSolution1]; // + bgValue * sumMatrix->data.F64[bgIndex][i + numSolution1]; 1322 1323 sumMatrix->data.F64[i][normIndex] = 0.0; 1324 sumMatrix->data.F64[normIndex][i] = 0.0; 1325 1326 // sumMatrix->data.F64[i][bgIndex] = 0.0; 1327 // sumMatrix->data.F64[bgIndex][i] = 0.0; 1328 sumMatrix->data.F64[i + numSolution1][normIndex] = 0.0; 1329 sumMatrix->data.F64[normIndex][i + numSolution1] = 0.0; 1330 // sumMatrix->data.F64[i + numSolution1][bgIndex] = 0.0; 1331 // sumMatrix->data.F64[bgIndex][i + numSolution1] = 0.0; 1332 } 1333 1334 sumMatrix->data.F64[normIndex][normIndex] = 1.0; 1335 // sumMatrix->data.F64[bgIndex][bgIndex] = 1.0; 1336 // sumMatrix->data.F64[normIndex][bgIndex] = sumMatrix->data.F64[bgIndex][normIndex] = 0.0; 1337 1338 sumVector->data.F64[normIndex] = 0.0; 1339 // sumVector->data.F64[bgIndex] = 0.0; 1340 1341 solution = psMatrixSolveSVD(solution, sumMatrix, sumVector, NAN); 1342 1343 solution->data.F64[normIndex] = normValue; 1344 // solution->data.F64[bgIndex] = bgValue; 1345 } 1346 #endif 1347 1318 1348 1319 1349 #ifdef TESTING … … 1325 1355 psFree(sumMatrix); 1326 1356 psFree(sumVector); 1357 1358 psFree(norms); 1327 1359 1328 1360 if (!kernels->solution1) { … … 1347 1379 int numKernels = kernels->num; 1348 1380 for (int i = 0; i < numKernels * numSpatial; i++) { 1349 // XXX fprintf (stderr, "keep\n");1350 kernels->solution1->data.F64[i] = solution->data.F64[i];1351 kernels->solution2->data.F64[i] = solution->data.F64[i + numSolution1];1381 // XXX fprintf (stderr, "keep\n"); 1382 kernels->solution1->data.F64[i] = solution->data.F64[i]; 1383 kernels->solution2->data.F64[i] = solution->data.F64[i + numSolution1]; 1352 1384 } 1353 1385 } -
branches/eam_branches/20091201/psModules/src/imcombine/pmSubtractionMatch.c
r26702 r26703 28 28 static bool useFFT = true; // Do convolutions using FFT 29 29 30 # define SEPARATE 130 # define SEPARATE 0 31 31 # if (SEPARATE) 32 32 # define SUBMODE PM_SUBTRACTION_EQUATION_NORM … … 74 74 float thresh2, // Threshold for stamp finding on readout 2 75 75 float stampSpacing, // Spacing between stamps 76 float normFrac, // Fraction of flux in window for normalisation window 76 77 float sysError, // Relative systematic error in images 77 78 float skyError, // Relative systematic error in images … … 86 87 87 88 *stamps = pmSubtractionStampsFind(*stamps, image1, image2, subMask, region, thresh1, thresh2, 88 size, footprint, stampSpacing, sysError, skyError, mode);89 size, footprint, stampSpacing, normFrac, sysError, skyError, mode); 89 90 if (!*stamps) { 90 91 psError(psErrorCodeLast(), false, "Unable to find stamps."); … … 109 110 const pmReadout *ro1, const pmReadout *ro2, // Input images 110 111 int stride, // Size for convolution patches 112 float normFrac, // Fraction of window for normalisation window 111 113 float sysError, // Systematic error in images 112 114 float skyError, // Systematic error in images … … 157 159 PS_ASSERT_IMAGES_SIZE_EQUAL(ro1->image, ro2->image, false); 158 160 PS_ASSERT_INT_NONNEGATIVE(stride, false); 161 if (isfinite(normFrac)) { 162 PS_ASSERT_FLOAT_LARGER_THAN(normFrac, 0.0, false); 163 PS_ASSERT_FLOAT_LESS_THAN(normFrac, 1.0, false); 164 } 159 165 if (isfinite(sysError)) { 160 166 PS_ASSERT_FLOAT_LARGER_THAN_OR_EQUAL(sysError, 0.0, false); … … 281 287 } 282 288 283 if (!subtractionMatchCheck(conv1, conv2, ro1, ro2, stride, NAN, NAN, kernelError,289 if (!subtractionMatchCheck(conv1, conv2, ro1, ro2, stride, NAN, NAN, NAN, kernelError, 284 290 maskVal, maskBad, maskPoor, poorFrac, badFrac, mode)) { 285 291 psFree(kernels); … … 347 353 int inner, int ringsOrder, int binning, float penalty, 348 354 bool optimum, const psVector *optFWHMs, int optOrder, float optThreshold, 349 int iter, float rej, float sysError, float skyError, float kernelError, psImageMaskType maskVal,350 psImageMaskType maskBad, psImageMaskType maskPoor, float poorFrac,351 float badFrac, pmSubtractionMode subMode)352 { 353 if (!subtractionMatchCheck(conv1, conv2, ro1, ro2, stride, sysError, skyError, kernelError,355 int iter, float rej, float normFrac, float sysError, float skyError, 356 float kernelError, psImageMaskType maskVal, psImageMaskType maskBad, 357 psImageMaskType maskPoor, float poorFrac, float badFrac, pmSubtractionMode subMode) 358 { 359 if (!subtractionMatchCheck(conv1, conv2, ro1, ro2, stride, normFrac, sysError, skyError, kernelError, 354 360 maskVal, maskBad, maskPoor, poorFrac, badFrac, subMode)) { 355 361 return false; … … 443 449 } 444 450 451 // General background subtraction and measurement of stamp threshold 445 452 float stampThresh1 = NAN, stampThresh2 = NAN; // Stamp thresholds for images 446 453 { 447 454 psStats *bg = psStatsAlloc(PS_STAT_ROBUST_MEDIAN | PS_STAT_ROBUST_STDEV); // Statistics for backgroun 448 455 if (ro1) { 456 psStatsInit(bg); 449 457 if (!psImageBackground(bg, NULL, ro1->image, ro1->mask, maskVal, rng)) { 450 458 psError(PS_ERR_UNKNOWN, false, "Unable to measure background statistics."); … … 452 460 goto MATCH_ERROR; 453 461 } 454 stampThresh1 = bg->robustMedian + threshold * bg->robustStdev; 462 stampThresh1 = threshold * bg->robustStdev; 463 psBinaryOp(ro1->image, ro1->image, "-", psScalarAlloc((float)bg->robustMedian, PS_TYPE_F32)); 455 464 } 456 465 if (ro2) { 466 psStatsInit(bg); 457 467 if (!psImageBackground(bg, NULL, ro2->image, ro2->mask, maskVal, rng)) { 458 468 psError(PS_ERR_UNKNOWN, false, "Unable to measure background statistics."); … … 460 470 goto MATCH_ERROR; 461 471 } 462 stampThresh2 = bg->robustMedian + threshold * bg->robustStdev; 463 } 472 stampThresh2 = threshold * bg->robustStdev; 473 psBinaryOp(ro2->image, ro2->image, "-", psScalarAlloc((float)bg->robustMedian, PS_TYPE_F32)); 474 } 464 475 psFree(bg); 465 476 } … … 481 492 if (stampsName && strlen(stampsName) > 0) { 482 493 stamps = pmSubtractionStampsSetFromFile(stampsName, ro1->image, subMask, region, size, 483 footprint, stampSpacing, sysError, skyError, subMode); 494 footprint, stampSpacing, normFrac, 495 sysError, skyError, subMode); 484 496 } else if (sources) { 485 497 stamps = pmSubtractionStampsSetFromSources(sources, ro1->image, subMask, region, size, 486 footprint, stampSpacing, sysError, skyError, subMode); 498 footprint, stampSpacing, normFrac, 499 sysError, skyError, subMode); 487 500 } 488 501 … … 490 503 // doesn't matter. 491 504 if (!subtractionGetStamps(&stamps, ro1, ro2, subMask, variance, NULL, stampThresh1, stampThresh2, 492 stampSpacing, sysError, skyError, size, footprint, subMode)) {505 stampSpacing, normFrac, sysError, skyError, size, footprint, subMode)) { 493 506 goto MATCH_ERROR; 494 507 } … … 562 575 psLogMsg("psModules.imcombine", PS_LOG_INFO, "Iteration %d.", k); 563 576 577 #if 0 564 578 if (!subtractionGetStamps(&stamps, ro1, ro2, subMask, variance, region, 565 stampThresh1, stampThresh2, stampSpacing, sysError, skyError, 566 size, footprint, subMode)) { 567 goto MATCH_ERROR; 568 } 579 stampThresh1, stampThresh2, stampSpacing, normFrac, 580 sysError, skyError, size, footprint, subMode)) { 581 goto MATCH_ERROR; 582 } 583 #endif 569 584 570 585 // generate the window function from the set of stamps -
branches/eam_branches/20091201/psModules/src/imcombine/pmSubtractionMatch.h
r26667 r26703 39 39 int iter, ///< Rejection iterations 40 40 float rej, ///< Rejection threshold 41 float normFrac, ///< Fraction of flux in window for normalisation window 41 42 float sysError, ///< Relative systematic error in images 42 43 float skyError, ///< Relative systematic error in images -
branches/eam_branches/20091201/psModules/src/imcombine/pmSubtractionStamps.c
r26562 r26703 176 176 177 177 pmSubtractionStampList *pmSubtractionStampListAlloc(int numCols, int numRows, const psRegion *region, 178 int footprint, float spacing, float sysErr, float skyErr) 178 int footprint, float spacing, float normFrac, 179 float sysErr, float skyErr) 179 180 { 180 181 pmSubtractionStampList *list = psAlloc(sizeof(pmSubtractionStampList)); // Stamp list to return … … 217 218 list->flux = NULL; 218 219 list->window = NULL; 220 list->normFrac = normFrac; 221 list->normWindow = 0; 219 222 list->footprint = footprint; 220 223 list->sysErr = sysErr; … … 308 311 stamp->matrix = NULL; 309 312 stamp->vector = NULL; 313 stamp->norm = NAN; 310 314 311 315 return stamp; … … 316 320 const psImage *image2, const psImage *subMask, 317 321 const psRegion *region, float thresh1, float thresh2, 318 int size, int footprint, float spacing, float sysErr, float skyErr,319 pmSubtractionMode mode)322 int size, int footprint, float spacing, float normFrac, 323 float sysErr, float skyErr, pmSubtractionMode mode) 320 324 { 321 325 if (!image1 && !image2) { … … 373 377 374 378 if (!stamps) { 375 stamps = pmSubtractionStampListAlloc(numCols, numRows, region, footprint, spacing, sysErr, skyErr); 379 stamps = pmSubtractionStampListAlloc(numCols, numRows, region, footprint, spacing, 380 normFrac, sysErr, skyErr); 376 381 } 377 382 … … 407 412 // Take stamps off the top of the (sorted) list 408 413 for (int j = xList->n - 1; j >= 0 && !goodStamp; j--) { 409 int xCentre = xList->data.F32[j] + 0.5, yCentre = yList->data.F32[j] + 0.5;// Stamp centre410 411 414 // Chop off the top of the list 412 415 xList->n = j; … … 414 417 fluxList->n = j; 415 418 419 #if 0 416 420 // Fish around a bit to see if we can find a pixel that isn't masked 421 // This is not a good idea if we're using the window feature 417 422 psTrace("psModules.imcombine", 7, "Searching for stamp %d around %d,%d\n", 418 423 i, xCentre, yCentre); 419 424 420 425 // Search bounds 426 int xCentre = xList->data.F32[j] + 0.5, yCentre = yList->data.F32[j] + 0.5;// Stamp centre 421 427 int search = footprint - size; // Search radius 422 428 int xMin = PS_MAX(border, xCentre - search); … … 427 433 goodStamp = stampSearch(&xStamp, &yStamp, &fluxStamp, image1, image2, thresh1, thresh2, 428 434 subMask, xMin, xMax, yMin, yMax, numCols, numRows, border); 429 430 // XXX reset for a test: 431 xStamp = xList->data.F32[j]; 432 yStamp = yList->data.F32[j]; 435 #else 436 // Only search the exact centre pixel 437 goodStamp = stampSearch(&xStamp, &yStamp, &fluxStamp, image1, image2, thresh1, thresh2, 438 subMask, xList->data.F32[j], xList->data.F32[j], 439 yList->data.F32[j], yList->data.F32[j], numCols, numRows, border); 440 #endif 433 441 // fprintf (stderr, "find: %d %d ==> %5.1f %5.1f\n", xCentre, yCentre, xStamp, yStamp); 434 442 } … … 486 494 const psImage *image, const psImage *subMask, 487 495 const psRegion *region, int size, int footprint, 488 float spacing, float sysErr, float skyErr, pmSubtractionMode mode) 496 float spacing, float normFrac, float sysErr, float skyErr, 497 pmSubtractionMode mode) 489 498 490 499 { … … 507 516 pmSubtractionStampList *stamps = pmSubtractionStampListAlloc(subMask->numCols, subMask->numRows, 508 517 region, footprint, spacing, 509 sysErr, skyErr); // Stamp list518 normFrac, sysErr, skyErr); // Stamp list 510 519 int numStamps = stamps->num; // Number of stamps 511 520 … … 612 621 PS_ASSERT_INT_NONNEGATIVE(kernelSize, false); 613 622 614 int size = kernelSize +stamps->footprint; // Size of postage stamps623 int size = stamps->footprint; // Size of postage stamps 615 624 616 625 psFree (stamps->window); … … 641 650 // storage vector for flux data 642 651 psStats *stats = psStatsAlloc (PS_STAT_ROBUST_MEDIAN | PS_STAT_ROBUST_STDEV); 643 psVector *flux = psVectorAllocEmpty(2*stamps->num, PS_TYPE_F32); 644 645 double maxValue = 0.0; 652 psVector *flux1 = psVectorAllocEmpty(2*stamps->num, PS_TYPE_F32); 653 psVector *flux2 = psVectorAllocEmpty(2*stamps->num, PS_TYPE_F32); 646 654 647 655 // generate the window pixels 656 double sum = 0.0; // Sum inside the window 657 float maxValue = 0.0; // Maximum value, for normalisation 648 658 for (int y = -size; y <= size; y++) { 649 659 for (int x = -size; x <= size; x++) { 650 660 651 flux->n = 0; 661 flux1->n = 0; 662 flux2->n = 0; 652 663 for (int i = 0; i < stamps->num; i++) { 653 664 pmSubtractionStamp *stamp = stamps->stamps->data[i]; // Stamp of interest … … 656 667 if (!stamp->image2) continue; 657 668 658 psVectorAppend (flux, stamp->image1->kernel[y][x] / norm1->data.F32[i]);659 psVectorAppend (flux, stamp->image2->kernel[y][x] / norm2->data.F32[i]);669 psVectorAppend(flux1, stamp->image1->kernel[y][x] / norm1->data.F32[i]); 670 psVectorAppend(flux2, stamp->image2->kernel[y][x] / norm2->data.F32[i]); 660 671 } 661 672 662 673 psStatsInit (stats); 663 if (!psVectorStats (stats, flux , NULL, NULL, 0)) {674 if (!psVectorStats (stats, flux1, NULL, NULL, 0)) { 664 675 psAbort ("failed to generate stats"); 665 676 } 666 stamps->window->kernel[y][x] = stats->robustMedian; 667 if (maxValue < stats->robustMedian) { 668 maxValue = stats->robustMedian; 669 } 670 } 677 float f1 = stats->robustMedian; 678 psStatsInit (stats); 679 if (!psVectorStats (stats, flux2, NULL, NULL, 0)) { 680 psAbort ("failed to generate stats"); 681 } 682 float f2 = stats->robustMedian; 683 684 stamps->window->kernel[y][x] = f1 + f2; 685 if (PS_SQR(x) + PS_SQR(y) <= PS_SQR(size)) { 686 sum += stamps->window->kernel[y][x]; 687 } 688 maxValue = PS_MAX(maxValue, stamps->window->kernel[y][x]); 689 } 690 } 691 692 psTrace("psModules.imcombine", 3, "Window total: %f, threshold: %f\n", 693 sum, (1.0 - stamps->normFrac) * sum); 694 bool done = false; 695 for (int radius = 1; radius <= size && !done; radius++) { 696 double within = 0.0; 697 for (int y = -radius; y <= radius; y++) { 698 for (int x = -radius; x <= radius; x++) { 699 if (PS_SQR(x) + PS_SQR(y) <= PS_SQR(radius)) { 700 within += stamps->window->kernel[y][x]; 701 } 702 } 703 } 704 psTrace("psModules.imcombine", 5, "Radius %d: %f\n", radius, within); 705 if (within > (1.0 - stamps->normFrac) * sum) { 706 stamps->normWindow = radius; 707 done = true; 708 } 709 } 710 psTrace("psModules.imcombine", 3, "Normalisation window radius set to %d\n", stamps->normWindow); 711 if (stamps->normWindow == 0 || stamps->normWindow >= size) { 712 psError(PS_ERR_UNKNOWN, true, "Unable to determine normalisation window size."); 713 return false; 671 714 } 672 715 … … 678 721 } 679 722 680 # ifdef TESTING723 #if 1 681 724 { 682 725 psFits *fits = psFitsOpen ("window.fits", "w"); … … 684 727 psFitsClose (fits); 685 728 } 686 # endif729 #endif 687 730 688 731 psFree (stats); 689 psFree (flux); 732 psFree (flux1); 733 psFree(flux2); 690 734 psFree (norm1); 691 735 psFree (norm2); … … 788 832 pmSubtractionStampList *pmSubtractionStampsSetFromSources(const psArray *sources, const psImage *image, 789 833 const psImage *subMask, const psRegion *region, 790 int size, int footprint, float spacing, float sysErr, float skyErr, 834 int size, int footprint, float spacing, 835 float normFrac, float sysErr, float skyErr, 791 836 pmSubtractionMode mode) 792 837 { … … 819 864 820 865 pmSubtractionStampList *stamps = pmSubtractionStampsSet(x, y, image, subMask, region, size, 821 footprint, spacing, sysErr, skyErr, mode); // Stamps 866 footprint, spacing, normFrac, 867 sysErr, skyErr, mode); // Stamps 822 868 psFree(x); 823 869 psFree(y); … … 833 879 pmSubtractionStampList *pmSubtractionStampsSetFromFile(const char *filename, const psImage *image, 834 880 const psImage *subMask, const psRegion *region, 835 int size, int footprint, float spacing, float sysErr, float skyErr,836 pmSubtractionMode mode)881 int size, int footprint, float spacing, float normFrac, 882 float sysErr, float skyErr, pmSubtractionMode mode) 837 883 { 838 884 PS_ASSERT_STRING_NON_EMPTY(filename, NULL); … … 851 897 852 898 pmSubtractionStampList *stamps = pmSubtractionStampsSet(x, y, image, subMask, region, size, footprint, 853 spacing, sysErr, skyErr, mode);899 spacing, normFrac, sysErr, skyErr, mode); 854 900 psFree(data); 855 901 -
branches/eam_branches/20091201/psModules/src/imcombine/pmSubtractionStamps.h
r26578 r26703 25 25 int footprint; ///< Half-size of stamps 26 26 psKernel *window; ///< window function generated from ensemble of stamps 27 float normFrac; ///< Fraction of flux in window for normalisation window 28 int normWindow; ///< Size of window for measuring normalisation 27 29 float sysErr; ///< Systematic error 28 30 float skyErr; ///< increase effective readnoise … … 30 32 31 33 /// Allocate a list of stamps 32 pmSubtractionStampList *pmSubtractionStampListAlloc(int numCols, // Number of columns in image 33 int numRows, // Number of rows in image 34 const psRegion *region, // Region for stamps, or NULL 35 int footprint, // Half-size of stamps 36 float spacing, // Rough average spacing between stamps 37 float sysErr, // Relative systematic error or NAN 38 float skyErr // Relative systematic error or NAN 34 pmSubtractionStampList *pmSubtractionStampListAlloc( 35 int numCols, // Number of columns in image 36 int numRows, // Number of rows in image 37 const psRegion *region, // Region for stamps, or NULL 38 int footprint, // Half-size of stamps 39 float spacing, // Rough average spacing between stamps 40 float normFrac, // Fraction of flux in window for normalisation window 41 float sysErr, // Relative systematic error or NAN 42 float skyErr // Relative systematic error or NAN 39 43 ); 40 44 … … 78 82 psImage *matrix; ///< Least-squares matrix, or NULL 79 83 psVector *vector; ///< Least-squares vector, or NULL 84 double norm; ///< Normalisation difference 80 85 pmSubtractionStampStatus status; ///< Status of stamp 81 86 } pmSubtractionStamp; … … 85 90 86 91 /// Find stamps on an image 87 pmSubtractionStampList *pmSubtractionStampsFind(pmSubtractionStampList *stamps, ///< Output stamps, or NULL 88 const psImage *image1, ///< Image for which to find stamps 89 const psImage *image2, ///< Image for which to find stamps 90 const psImage *mask, ///< Mask, or NULL 91 const psRegion *region, ///< Region to search, or NULL 92 float thresh1, ///< Threshold for stamps in image 1 93 float thresh2, ///< Threshold for stamps in image 2 94 int size, ///< Kernel half-size 95 int footprint, ///< Half-size for stamps 96 float spacing, ///< Rough spacing for stamps 97 float sysErr, ///< Relative systematic error in images 98 float skyErr, ///< Relative systematic error in images 99 pmSubtractionMode mode ///< Mode for subtraction 92 pmSubtractionStampList *pmSubtractionStampsFind( 93 pmSubtractionStampList *stamps, ///< Output stamps, or NULL 94 const psImage *image1, ///< Image for which to find stamps 95 const psImage *image2, ///< Image for which to find stamps 96 const psImage *mask, ///< Mask, or NULL 97 const psRegion *region, ///< Region to search, or NULL 98 float thresh1, ///< Threshold for stamps in image 1 99 float thresh2, ///< Threshold for stamps in image 2 100 int size, ///< Kernel half-size 101 int footprint, ///< Half-size for stamps 102 float spacing, ///< Rough spacing for stamps 103 float normFrac, // Fraction of flux in window for normalisation window 104 float sysErr, ///< Relative systematic error in images 105 float skyErr, ///< Relative systematic error in images 106 pmSubtractionMode mode ///< Mode for subtraction 100 107 ); 101 108 102 109 /// Set stamps based on a list of x,y 103 pmSubtractionStampList *pmSubtractionStampsSet(const psVector *x, ///< x coordinates for each stamp 104 const psVector *y, ///< y coordinates for each stamp 105 const psImage *image, ///< Image for flux of stamp 106 const psImage *mask, ///< Mask, or NULL 107 const psRegion *region, ///< Region to search, or NULL 108 int size, ///< Kernel half-size 109 int footprint, ///< Half-size for stamps 110 float spacing, ///< Rough spacing for stamps 111 float sysErr, ///< Systematic error in images 112 float skyErr, ///< Systematic error in images 113 pmSubtractionMode mode ///< Mode for subtraction 110 pmSubtractionStampList *pmSubtractionStampsSet( 111 const psVector *x, ///< x coordinates for each stamp 112 const psVector *y, ///< y coordinates for each stamp 113 const psImage *image, ///< Image for flux of stamp 114 const psImage *mask, ///< Mask, or NULL 115 const psRegion *region, ///< Region to search, or NULL 116 int size, ///< Kernel half-size 117 int footprint, ///< Half-size for stamps 118 float spacing, ///< Rough spacing for stamps 119 float normFrac, // Fraction of flux in window for normalisation window 120 float sysErr, ///< Systematic error in images 121 float skyErr, ///< Systematic error in images 122 pmSubtractionMode mode ///< Mode for subtraction 114 123 ); 115 124 … … 123 132 int footprint, ///< Half-size for stamps 124 133 float spacing, ///< Rough spacing for stamps 134 float normFrac, // Fraction of flux in window for normalisation window 125 135 float sysErr, ///< Systematic error in images 126 136 float skyErr, ///< Systematic error in images … … 137 147 int footprint, ///< Half-size for stamps 138 148 float spacing, ///< Rough spacing for stamps 149 float normFrac, // Fraction of flux in window for normalisation window 139 150 float sysErr, ///< Systematic error in images 140 151 float skyErr, ///< Systematic error in images … … 142 153 ); 143 154 144 bool pmSubtractionStampsGetWindow(pmSubtractionStampList *stamps, int kernelSize); 155 /// Calculate the window and normalisation window from the stamps 156 bool pmSubtractionStampsGetWindow( 157 pmSubtractionStampList *stamps, ///< List of stamps 158 int kernelSize ///< Half-size of kernel 159 ); 145 160 146 161 /// Extract stamps from the images
Note:
See TracChangeset
for help on using the changeset viewer.
