IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 6, 2004, 2:32:53 PM (22 years ago)
Author:
gusciora
Message:

Lots of bug fixes and such.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/math/psStats.c

    r831 r889  
    8585    float binSize = 0.0;
    8686
     87    if (n == 0) {
     88        psAbort(__func__, "psHistogram requested with 0 bins");
     89    }
     90
    8791    newHist = (psHistogram *) psAlloc(sizeof(psHistogram));
    8892    newHist->bounds = psVectorAlloc(n+1, PS_TYPE_F32);
     93    newHist->bounds->n = newHist->bounds->nalloc;
     94
    8995    binSize = (upper - lower) / (float) n;
    9096    for (i=0;i<n+1;i++) {
     
    9298    }
    9399    newHist->nums =  psVectorAlloc(n, PS_TYPE_S32);
     100    newHist->nums->n = newHist->nums->nalloc;
     101    for (i=0;i<newHist->nums->n;i++) {
     102        newHist->nums->data.S32[i] = 0;
     103    }
    94104    newHist->minNum = 0;
    95105    newHist->maxNum = 0;
     
    99109}
    100110
    101 
     111// When this is called, the number of data elements in bounds
     112// is n.  Therefore, the number of bins is n-1.
    102113psHistogram *psHistogramAllocGeneric(const psVector *restrict bounds)
    103114{
     
    107118    newHist = (psHistogram *) psAlloc(sizeof(psHistogram));
    108119    newHist->bounds = psVectorAlloc(bounds->n, PS_TYPE_F32);
     120    newHist->bounds->n = newHist->bounds->nalloc;
    109121    for (i=0;i<bounds->n;i++) {
    110122        newHist->bounds->data.F32[i] = bounds->data.F32[i];
    111123    }
    112124    newHist->nums = psVectorAlloc((bounds->n)-1, PS_TYPE_S32);
     125    newHist->nums->n = newHist->nums->nalloc;
     126    for (i=0;i<newHist->nums->n;i++) {
     127        newHist->nums->data.S32[i] = 0;
     128    }
    113129
    114130    newHist->minNum = 0;
     
    119135}
    120136
    121 void psHistogramFree(psHistogram *restrict myHist)
    122 {
     137void psHistogramFree(psHistogram *myHist)
     138{
     139    psVectorFree(myHist->bounds);
     140    psVectorFree(myHist->nums);
    123141    psFree(myHist);
    124142}
     
    150168
    151169    numBins = out->nums->n;
    152     for (i=0;i<in->n;i++) {
    153         // Check if this pixel is masked, and if so, skip it.
    154         if (!(mask->data.S32[i] & maskVal)) {
    155             // Check if this pixel is below the minimum value, and if so
    156             // count it, then skip it.
     170
     171    if (mask != NULL) {
     172        for (i=0;i<in->n;i++) {
     173            // Check if this pixel is masked, and if so, skip it.
     174            if (!(mask->data.U8[i] & maskVal)) {
     175                // Check if this pixel is below the minimum value, and if so
     176                // count it, then skip it.
     177                if (in->data.F32[i] < out->bounds->data.F32[0]) {
     178                    out->minNum++;
     179
     180                    // Check if this pixel is above the maximum value, and if so
     181                    // count it, then skip it.
     182                } else if (in->data.F32[i] > out->bounds->data.F32[numBins]) {
     183                    out->maxNum++;
     184                } else {
     185                    // If this is a uniform histogram, determining the correct
     186                    // number is trivial.
     187                    if (out->uniform == true) {
     188                        binSize = out->bounds->data.F32[1] - out->bounds->data.F32[0];
     189
     190                        binNum = (int) ((in->data.F32[i] - out->bounds->data.F32[0]) /
     191                                        binSize);
     192                        (out->nums->data.S32[binNum])++;
     193                        // If this is a non-uniform histogram, determining the correct
     194                        // bin number requires a bit more work.
     195                    } else {
     196                        // GUS: This is slow.  Put a smarter algorithm here to
     197                        // find the correct bin number (bin search, probably)
     198                        for (j=0;j<(out->bounds->n)-1;j++) {
     199                            if ((out->bounds->data.S32[j] <= in->data.F32[i]) &&
     200                                    (in->data.F32[i] <= out->bounds->data.S32[j+1])) {
     201                                (out->nums->data.S32[j])++;
     202                            }
     203                        }
     204                    }
     205                }
     206            }
     207        }
     208    } else {
     209        for (i=0;i<in->n;i++) {
    157210            if (in->data.F32[i] < out->bounds->data.F32[0]) {
     211                // Check if this pixel is below the minimum value, and if so
     212                // count it, then skip it.
    158213                out->minNum++;
    159 
     214            } else if (in->data.F32[i] > out->bounds->data.F32[numBins]) {
    160215                // Check if this pixel is above the maximum value, and if so
    161216                // count it, then skip it.
    162             } else if (in->data.F32[i] > out->bounds->data.F32[numBins]) {
    163217                out->maxNum++;
    164218            } else {
     
    171225                                    binSize);
    172226                    (out->nums->data.S32[binNum])++;
     227
    173228                    // If this is a non-uniform histogram, determining the correct
    174229                    // bin number requires a bit more work.
     
    185240            }
    186241        }
     242
    187243    }
    188244    return(out);
     
    471527    unsortedVector = psVectorAlloc(nValues, PS_TYPE_F32);
    472528    unsortedVector->n = unsortedVector->nalloc;
     529
    473530    sortedVector   = psVectorAlloc(nValues, PS_TYPE_F32);
    474531    sortedVector->n = sortedVector->nalloc;
     
    494551                if ((rangeMin <= myVector->data.F32[i]) &&
    495552                        (myVector->data.F32[i] <= rangeMax)) {
    496                     unsortedVector->data.F32[count++] = maskVector->data.F32[i];
     553                    unsortedVector->data.F32[count++] = myVector->data.F32[i];
    497554                }
    498555            }
     
    504561            for (i=0;i<myVector->n;i++) {
    505562                if (!(maskVal & maskVector->data.U8[i])) {
    506                     unsortedVector->data.F32[count++] = maskVector->data.F32[i];
    507                 }
    508             }
    509         } else {
    510             for (i=0;i<myVector->n;i++) {
    511                 unsortedVector->data.F32[i] = maskVector->data.F32[i];
     563                    unsortedVector->data.F32[count++] = myVector->data.F32[i];
     564                }
     565            }
     566        } else {
     567            for (i=0;i<myVector->n;i++) {
     568                unsortedVector->data.F32[i] = myVector->data.F32[i];
    512569            }
    513570        }
     
    596653    float rangeMin = 0.0;
    597654    float rangeMax = 0.0;
    598     psStats *stats2 = NULL;
    599655
    600656    // Determine if the number of data points exceed a threshold which will
    601657    // cause to generate robust stats, as opposed to exact stats.
    602     if (myVector->n > stats->sampleLimit) {
    603         // Calculate the robust quartiles.
    604         stats2 = psStatsAlloc(PS_STAT_ROBUST_QUARTILE);
    605         p_psVectorRobustStats(myVector, maskVector, maskVal, stats2);
    606 
    607         // Store the robust quartiles into the sample quartile members.
    608         stats->sampleUQ = stats2->robustUQ;
    609         stats->sampleLQ = stats2->robustLQ;
    610 
    611         // Free temporary data buffers.
    612         psStatsFree(stats2);
    613 
    614         // Set the PS_STAT_ROBUST_FOR_SAMPLE bit in the stats structure.
    615         stats->options = stats->options | PS_STAT_ROBUST_FOR_SAMPLE;
    616 
    617         return;
    618     }
     658    /* GUS: When IfA provides the algorithm, insert is here.
     659        if (myVector->n > stats->sampleLimit) {
     660            psStats *stats2 = NULL;
     661            // Calculate the robust quartiles.
     662            stats2 = psStatsAlloc(PS_STAT_ROBUST_QUARTILE);
     663            p_psVectorRobustStats(myVector, maskVector, maskVal, stats2);
     664     
     665            // Store the robust quartiles into the sample quartile members.
     666            stats->sampleUQ = stats2->robustUQ;
     667            stats->sampleLQ = stats2->robustLQ;
     668     
     669            // Free temporary data buffers.
     670            psStatsFree(stats2);
     671     
     672            // Set the PS_STAT_ROBUST_FOR_SAMPLE bit in the stats structure.
     673            stats->options = stats->options | PS_STAT_ROBUST_FOR_SAMPLE;
     674     
     675            return;
     676        }
     677    */
    619678
    620679    // Determine how many data points fit inside this min/max range
     
    640699                        (rangeMin <= myVector->data.F32[i]) &&
    641700                        (myVector->data.F32[i] <= rangeMax)) {
    642                     unsortedVector->data.F32[count++] = maskVector->data.F32[i];
     701                    unsortedVector->data.F32[count++] = myVector->data.F32[i];
    643702                }
    644703            }
     
    647706                if ((rangeMin <= myVector->data.F32[i]) &&
    648707                        (myVector->data.F32[i] <= rangeMax)) {
    649                     unsortedVector->data.F32[count++] = maskVector->data.F32[i];
     708                    unsortedVector->data.F32[count++] = myVector->data.F32[i];
    650709                }
    651710            }
     
    657716            for (i=0;i<myVector->n;i++) {
    658717                if (!(maskVal & maskVector->data.U8[i])) {
    659                     unsortedVector->data.F32[count++] = maskVector->data.F32[i];
    660                 }
    661             }
    662         } else {
    663             for (i=0;i<myVector->n;i++) {
    664                 unsortedVector->data.F32[i] = maskVector->data.F32[i];
     718                    unsortedVector->data.F32[count++] = myVector->data.F32[i];
     719                }
     720            }
     721        } else {
     722            for (i=0;i<myVector->n;i++) {
     723                unsortedVector->data.F32[i] = myVector->data.F32[i];
    665724            }
    666725        }
     
    679738    psVectorFree(unsortedVector);
    680739    psVectorFree(sortedVector);
     740    // GUS: This is the
    681741}
    682742
     
    691751        PS_STAT_ROBUST_QUARTILE
    692752    I have included all that computation in a single function, as opposed to
    693     breaking it across several functions for one primary reason: the all
     753    breaking it across several functions for one primary reason: they all
    694754    require the same basic initial processing steps (calculate the histogram,
    695755    etc.) however there is no currently defined means, in the SDRS, to
     
    719779    // is really required.
    720780
    721     if (0.0 == stats->sampleUQ) {
    722 
    723         // GUS: fix this
    724         //        stats->options = stats->options | PS_STAT_SAMPLE_UQ;
     781    if (isnan(stats->sampleUQ) ||
     782            isnan(stats->sampleLQ)) {
     783        stats->options = stats->options | PS_STAT_SAMPLE_QUARTILE;
    725784        p_psVectorSampleQuartiles(myVector,
    726785                                  maskVector,
     
    729788    }
    730789
    731     if (isnan(stats->sampleLQ)) {
    732         // GUS: fix this
    733         //        stats->options = stats->options | PS_STAT_SAMPLE_LQ;
    734         p_psVectorSampleQuartiles(myVector,
    735                                   maskVector,
    736                                   maskVal,
    737                                   stats);
    738     }
    739 
    740790    // Compute the initial bin size of the robust histogram.
    741791    sigmaE = (stats->sampleUQ - stats->sampleLQ) / 1.34f;
     
    753803    robustHistogram = psHistogramAlloc(stats->min,
    754804                                       stats->max,
    755                                        binSize);
    756     // Populate the histogram arrat.
     805                                       1 + (int) ((stats->max - stats->min) / binSize));
     806
     807
     808    // Populate the histogram array.
    757809    // GUS: fix this
    758810    //    robustHistogram = psHistogramVector(robustHistogram, myVector);
     
    811863        stats->robustLQ = 0.0;
    812864    }
     865    stats->robustNfit = 0.0;
     866    stats->robustN50 = 0.0;
     867    psHistogramFree(robustHistogram);
    813868}
    814869
     
    836891
    837892    if (0 != isnan(stats->sampleMean)) {
    838         psAbort(__func__, "stats->sampleMean == NAN");
     893        p_psVectorSampleMean(myVector, maskVector, maskVal, stats);
    839894    }
    840895    mean = stats->sampleMean;
     
    920975    }
    921976
    922     tmpMask = psVectorAlloc(myVector->nalloc, maskVector->type.type);
    923 
    924     tmpMask->n = maskVector->n;
    925     for (i=0;i<tmpMask->n;i++) {
    926         tmpMask->data.U8[i] = maskVector->data.U8[i];
     977
     978    tmpMask = psVectorAlloc(myVector->n, PS_TYPE_U8);
     979    tmpMask->n = myVector->n;
     980
     981    if (maskVector != NULL) {
     982        for (i=0;i<tmpMask->n;i++) {
     983            tmpMask->data.U8[i] = maskVector->data.U8[i];
     984        }
    927985    }
    928986
    929987    // 1. Compute the sample median.
     988    // GUS: This seems odd.  Verify with IfA that we want to calculate the
     989    // median here, not the mean.
    930990    p_psVectorSampleMedian(myVector, maskVector, maskVal, stats);
    931991
     
    9421002
    9431003    for (i=0;i<stats->clipIter;i++) {
     1004        //        printf("p_psVectorClippedStats(): Iteration %d (%f %f)\n", i,
     1005        //                clippedMean, clippedStdev);
    9441006        for (j=0;j<myVector->n;j++) {
    9451007            // a) Exclude all values x_i for which |x_i - x| > K * stdev
    946             if ( fabs(myVector->data.F32[j] - clippedMean) >
     1008            if (fabs(myVector->data.F32[j] - clippedMean) >
    9471009                    (stats->clipSigma * clippedStdev)) {
    9481010                tmpMask->data.U8[i] = 0xff;
     
    9941056    if (in->type.type != PS_TYPE_F32) {
    9951057        psAbort(__func__,
    996                 "Only data type PS_TYPE_F32 is currently supported.");
    997     }
     1058                "Only data type PS_TYPE_F32 is currently supported (0x%x).",
     1059                in->type.type);
     1060    }
     1061
    9981062    if (mask != NULL) {
    9991063        if (in->n != mask->n) {
     
    10181082
    10191083    // ************************************************************************
     1084    // GUS: The Stdev calculation requires the mean.  Should we assume the
     1085    // mean has already been calculated?  Or should we always calculate it?
    10201086    if (stats->options & PS_STAT_SAMPLE_STDEV) {
     1087        p_psVectorSampleMean(in, mask, maskVal, stats);
    10211088        p_psVectorSampleStdev(in, mask, maskVal, stats);
    10221089    }
Note: See TracChangeset for help on using the changeset viewer.