- Timestamp:
- Aug 20, 2009, 11:47:38 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/czw_branch/cleanup/psModules/src/imcombine/pmSubtractionMatch.c
r24292 r25148 447 447 } 448 448 449 450 // Define kernel basis functions 451 if (optimum && (type == PM_SUBTRACTION_KERNEL_ISIS || type == PM_SUBTRACTION_KERNEL_GUNK)) { 452 kernels = pmSubtractionKernelsOptimumISIS(type, size, inner, spatialOrder, optFWHMs, optOrder, 453 stamps, footprint, optThreshold, penalty, subMode); 454 if (!kernels) { 455 psErrorClear(); 456 psWarning("Unable to derive optimum ISIS kernel --- switching to default."); 457 } 458 } 459 if (kernels == NULL) { 460 // Not an ISIS/GUNK kernel, or the optimum kernel search failed 461 kernels = pmSubtractionKernelsGenerate(type, size, spatialOrder, isisWidths, isisOrders, 462 inner, binning, ringsOrder, penalty, subMode); 463 } 464 465 memCheck("kernels"); 466 449 467 if (subMode == PM_SUBTRACTION_MODE_UNSURE) { 468 #if 0 450 469 // Get backgrounds 451 470 psStats *bgStats = psStatsAlloc(BG_STAT); // Statistics for background … … 469 488 470 489 pmSubtractionMode newMode = pmSubtractionOrder(stamps, bg1, bg2); // Subtraction mode to use 490 #endif 491 pmSubtractionMode newMode = pmSubtractionBestMode(&stamps, &kernels, subMask, rej); 471 492 switch (newMode) { 472 493 case PM_SUBTRACTION_MODE_1: … … 483 504 } 484 505 485 // Define kernel basis functions486 if (optimum && (type == PM_SUBTRACTION_KERNEL_ISIS || type == PM_SUBTRACTION_KERNEL_GUNK)) {487 kernels = pmSubtractionKernelsOptimumISIS(type, size, inner, spatialOrder, optFWHMs, optOrder,488 stamps, footprint, optThreshold, penalty, subMode);489 if (!kernels) {490 psErrorClear();491 psWarning("Unable to derive optimum ISIS kernel --- switching to default.");492 }493 }494 if (kernels == NULL) {495 // Not an ISIS/GUNK kernel, or the optimum kernel search failed496 kernels = pmSubtractionKernelsGenerate(type, size, spatialOrder, isisWidths, isisOrders,497 inner, binning, ringsOrder, penalty, subMode);498 }499 500 memCheck("kernels");501 502 506 int numRejected = -1; // Number of rejected stamps in each iteration 503 507 for (int k = 0; k < iter && numRejected != 0; k++) { … … 535 539 536 540 psTrace("psModules.imcombine", 3, "Rejecting stamps...\n"); 537 numRejected = pmSubtractionRejectStamps(kernels, stamps, deviations, subMask, rej , footprint);541 numRejected = pmSubtractionRejectStamps(kernels, stamps, deviations, subMask, rej); 538 542 if (numRejected < 0) { 539 543 psError(PS_ERR_UNKNOWN, false, "Unable to reject stamps."); … … 557 561 goto MATCH_ERROR; 558 562 } 559 pmSubtractionRejectStamps(kernels, stamps, deviations, subMask, NAN , footprint);563 pmSubtractionRejectStamps(kernels, stamps, deviations, subMask, NAN); 560 564 psFree(deviations); 561 565 } … … 842 846 return mode; 843 847 } 848 849 850 // Test a subtraction mode by performing a single iteration 851 static bool subtractionModeTest(pmSubtractionStampList *stamps, // Stamps to use to find best mode 852 pmSubtractionKernels *kernels, // Kernel description 853 const char *description, // Description for trace 854 psImage *subMask, // Subtraction mask 855 float rej // Rejection threshold 856 ) 857 { 858 assert(stamps); 859 assert(kernels); 860 861 psTrace("psModules.imcombine", 3, "Calculating %s equation...\n", description); 862 if (!pmSubtractionCalculateEquation(stamps, kernels)) { 863 psError(PS_ERR_UNKNOWN, false, "Unable to calculate least-squares equation."); 864 return false; 865 } 866 867 psTrace("psModules.imcombine", 3, "Solving %s equation...\n", description); 868 if (!pmSubtractionSolveEquation(kernels, stamps)) { 869 psError(PS_ERR_UNKNOWN, false, "Unable to calculate least-squares equation."); 870 return false; 871 } 872 873 psTrace("psModules.imcombine", 3, "Calculate %s deviations...\n", description); 874 psVector *deviations = pmSubtractionCalculateDeviations(stamps, kernels); // Stamp deviations 875 if (!deviations) { 876 psError(PS_ERR_UNKNOWN, false, "Unable to calculate deviations."); 877 return false; 878 } 879 880 psTrace("psModules.imcombine", 3, "Rejecting %s stamps...\n", description); 881 long numRejected = pmSubtractionRejectStamps(kernels, stamps, deviations, subMask, rej); 882 if (numRejected < 0) { 883 psError(PS_ERR_UNKNOWN, false, "Unable to reject stamps."); 884 psFree(deviations); 885 return false; 886 } 887 psFree(deviations); 888 889 if (numRejected > 0) { 890 // Allow re-fit with reduced stamps set 891 psTrace("psModules.imcombine", 3, "Resolving %s equation...\n", description); 892 if (!pmSubtractionSolveEquation(kernels, stamps)) { 893 psError(PS_ERR_UNKNOWN, false, "Unable to calculate least-squares equation."); 894 return false; 895 } 896 psTrace("psModules.imcombine", 3, "Recalculate %s deviations...\n", description); 897 psVector *deviations = pmSubtractionCalculateDeviations(stamps, kernels); // Stamp deviations 898 if (!deviations) { 899 psError(PS_ERR_UNKNOWN, false, "Unable to calculate deviations."); 900 return false; 901 } 902 psTrace("psModules.imcombine", 3, "Measuring %s quality...\n", description); 903 long numRejected = pmSubtractionRejectStamps(kernels, stamps, deviations, subMask, NAN); 904 if (numRejected < 0) { 905 psError(PS_ERR_UNKNOWN, false, "Unable to reject stamps."); 906 psFree(deviations); 907 return false; 908 } 909 psFree(deviations); 910 } 911 912 return true; 913 } 914 915 916 pmSubtractionMode pmSubtractionBestMode(pmSubtractionStampList **stamps, pmSubtractionKernels **kernels, 917 const psImage *subMask, float rej) 918 { 919 PM_ASSERT_SUBTRACTION_STAMP_LIST_NON_NULL(*stamps, PM_SUBTRACTION_MODE_ERR); 920 PM_ASSERT_SUBTRACTION_KERNELS_NON_NULL(*kernels, PM_SUBTRACTION_MODE_ERR); 921 922 // Copies of the inputs 923 pmSubtractionStampList *stamps1 = pmSubtractionStampListCopy(*stamps); 924 pmSubtractionKernels *kernels1 = pmSubtractionKernelsCopy(*kernels); 925 psImage *subMask1 = psImageCopy(NULL, subMask, subMask->type.type); 926 kernels1->mode = PM_SUBTRACTION_MODE_1; 927 928 if (!subtractionModeTest(stamps1, kernels1, "forward", subMask1, rej)) { 929 psError(PS_ERR_UNKNOWN, false, "Unable to test forward subtraction"); 930 psFree(stamps1); 931 psFree(kernels1); 932 psFree(subMask1); 933 return PM_SUBTRACTION_MODE_ERR; 934 } 935 psFree(subMask1); 936 937 // Copies of the inputs 938 pmSubtractionStampList *stamps2 = pmSubtractionStampListCopy(*stamps); 939 pmSubtractionKernels *kernels2 = pmSubtractionKernelsCopy(*kernels); 940 psImage *subMask2 = psImageCopy(NULL, subMask, subMask->type.type); 941 kernels2->mode = PM_SUBTRACTION_MODE_2; 942 943 if (!subtractionModeTest(stamps2, kernels2, "backward", subMask2, rej)) { 944 psError(PS_ERR_UNKNOWN, false, "Unable to test backward subtraction"); 945 psFree(stamps2); 946 psFree(kernels2); 947 psFree(subMask2); 948 psFree(stamps1); 949 psFree(kernels1); 950 return PM_SUBTRACTION_MODE_ERR; 951 } 952 psFree(subMask2); 953 954 955 pmSubtractionStampList *bestStamps = NULL; // Best choice for stamps 956 pmSubtractionKernels *bestKernels = NULL; // Best choice for kernels 957 psLogMsg("psModules.imcombine", PS_LOG_INFO, 958 "Forward: %f +/- %f from %d stamps\nBackward: %f +/- %f from %d stamps\n", 959 kernels1->mean, kernels1->rms, kernels1->numStamps, 960 kernels2->mean, kernels2->rms, kernels2->numStamps); 961 962 if (kernels1->mean < kernels2->mean) { 963 bestStamps = stamps1; 964 bestKernels = kernels1; 965 } else { 966 bestStamps = stamps2; 967 bestKernels = kernels2; 968 } 969 970 psFree(*stamps); 971 psFree(*kernels); 972 *stamps = psMemIncrRefCounter(bestStamps); 973 *kernels = psMemIncrRefCounter(bestKernels); 974 975 psFree(stamps1); 976 psFree(stamps2); 977 psFree(kernels1); 978 psFree(kernels2); 979 980 return bestKernels->mode; 981 } 982
Note:
See TracChangeset
for help on using the changeset viewer.
