tesseract
4.0.0-1-g2a2b
|
#include <cfloat>
#include <cmath>
#include <vector>
#include "cluster.h"
#include "cutil.h"
#include "emalloc.h"
#include "genericheap.h"
#include "helpers.h"
#include "kdpair.h"
#include "matrix.h"
#include "tprintf.h"
Go to the source code of this file.
Classes | |
struct | TEMPCLUSTER |
struct | STATISTICS |
struct | BUCKETS |
struct | CHISTRUCT |
struct | ClusteringContext |
Macros | |
#define | HOTELLING 1 |
#define | FTABLE_X 10 |
#define | FTABLE_Y 100 |
#define | MINVARIANCE 0.0004 |
#define | MINSAMPLESPERBUCKET 5 |
#define | MINSAMPLES (MINBUCKETS * MINSAMPLESPERBUCKET) |
#define | MINSAMPLESNEEDED 1 |
#define | BUCKETTABLESIZE 1024 |
#define | NORMALEXTENT 3.0 |
#define | Odd(N) ((N)%2) |
#define | Mirror(N, R) ((R) - (N) - 1) |
#define | Abs(N) (((N) < 0) ? (-(N)) : (N)) |
#define | SqrtOf2Pi 2.506628275 |
#define | LOOKUPTABLESIZE 8 |
#define | MAXDEGREESOFFREEDOM MAXBUCKETS |
#define | MAXNEIGHBORS 2 |
#define | MAXDISTANCE FLT_MAX |
#define | CHIACCURACY 0.01 |
#define | MINALPHA (1e-200) |
#define | INITIALDELTA 0.1 |
#define | DELTARATIO 0.1 |
#define | ILLEGAL_CHAR 2 |
Typedefs | |
using | ClusterPair = tesseract::KDPairInc< float, TEMPCLUSTER * > |
using | ClusterHeap = tesseract::GenericHeap< ClusterPair > |
typedef double(* | DENSITYFUNC) (int32_t) |
typedef double(* | SOLVEFUNC) (CHISTRUCT *, double) |
Functions | |
void | CreateClusterTree (CLUSTERER *Clusterer) |
void | MakePotentialClusters (ClusteringContext *context, CLUSTER *Cluster, int32_t Level) |
CLUSTER * | FindNearestNeighbor (KDTREE *Tree, CLUSTER *Cluster, float *Distance) |
CLUSTER * | MakeNewCluster (CLUSTERER *Clusterer, TEMPCLUSTER *TempCluster) |
int32_t | MergeClusters (int16_t N, PARAM_DESC ParamDesc[], int32_t n1, int32_t n2, float m[], float m1[], float m2[]) |
void | ComputePrototypes (CLUSTERER *Clusterer, CLUSTERCONFIG *Config) |
PROTOTYPE * | MakePrototype (CLUSTERER *Clusterer, CLUSTERCONFIG *Config, CLUSTER *Cluster) |
PROTOTYPE * | MakeDegenerateProto (uint16_t N, CLUSTER *Cluster, STATISTICS *Statistics, PROTOSTYLE Style, int32_t MinSamples) |
PROTOTYPE * | TestEllipticalProto (CLUSTERER *Clusterer, CLUSTERCONFIG *Config, CLUSTER *Cluster, STATISTICS *Statistics) |
PROTOTYPE * | MakeSphericalProto (CLUSTERER *Clusterer, CLUSTER *Cluster, STATISTICS *Statistics, BUCKETS *Buckets) |
PROTOTYPE * | MakeEllipticalProto (CLUSTERER *Clusterer, CLUSTER *Cluster, STATISTICS *Statistics, BUCKETS *Buckets) |
PROTOTYPE * | MakeMixedProto (CLUSTERER *Clusterer, CLUSTER *Cluster, STATISTICS *Statistics, BUCKETS *NormalBuckets, double Confidence) |
void | MakeDimRandom (uint16_t i, PROTOTYPE *Proto, PARAM_DESC *ParamDesc) |
void | MakeDimUniform (uint16_t i, PROTOTYPE *Proto, STATISTICS *Statistics) |
STATISTICS * | ComputeStatistics (int16_t N, PARAM_DESC ParamDesc[], CLUSTER *Cluster) |
PROTOTYPE * | NewSphericalProto (uint16_t N, CLUSTER *Cluster, STATISTICS *Statistics) |
PROTOTYPE * | NewEllipticalProto (int16_t N, CLUSTER *Cluster, STATISTICS *Statistics) |
PROTOTYPE * | NewMixedProto (int16_t N, CLUSTER *Cluster, STATISTICS *Statistics) |
PROTOTYPE * | NewSimpleProto (int16_t N, CLUSTER *Cluster) |
bool | Independent (PARAM_DESC *ParamDesc, int16_t N, float *CoVariance, float Independence) |
BUCKETS * | GetBuckets (CLUSTERER *clusterer, DISTRIBUTION Distribution, uint32_t SampleCount, double Confidence) |
BUCKETS * | MakeBuckets (DISTRIBUTION Distribution, uint32_t SampleCount, double Confidence) |
uint16_t | OptimumNumberOfBuckets (uint32_t SampleCount) |
double | ComputeChiSquared (uint16_t DegreesOfFreedom, double Alpha) |
double | NormalDensity (int32_t x) |
double | UniformDensity (int32_t x) |
double | Integral (double f1, double f2, double Dx) |
void | FillBuckets (BUCKETS *Buckets, CLUSTER *Cluster, uint16_t Dim, PARAM_DESC *ParamDesc, float Mean, float StdDev) |
uint16_t | NormalBucket (PARAM_DESC *ParamDesc, float x, float Mean, float StdDev) |
uint16_t | UniformBucket (PARAM_DESC *ParamDesc, float x, float Mean, float StdDev) |
bool | DistributionOK (BUCKETS *Buckets) |
void | FreeStatistics (STATISTICS *Statistics) |
void | FreeBuckets (BUCKETS *Buckets) |
void | FreeCluster (CLUSTER *Cluster) |
uint16_t | DegreesOfFreedom (DISTRIBUTION Distribution, uint16_t HistogramBuckets) |
int | NumBucketsMatch (void *arg1, void *arg2) |
int | ListEntryMatch (void *arg1, void *arg2) |
void | AdjustBuckets (BUCKETS *Buckets, uint32_t NewSampleCount) |
void | InitBuckets (BUCKETS *Buckets) |
int | AlphaMatch (void *arg1, void *arg2) |
CHISTRUCT * | NewChiStruct (uint16_t DegreesOfFreedom, double Alpha) |
double | Solve (SOLVEFUNC Function, void *FunctionParams, double InitialGuess, double Accuracy) |
double | ChiArea (CHISTRUCT *ChiParams, double x) |
bool | MultipleCharSamples (CLUSTERER *Clusterer, CLUSTER *Cluster, float MaxIllegal) |
double | InvertMatrix (const float *input, int size, float *inv) |
CLUSTERER * | MakeClusterer (int16_t SampleSize, const PARAM_DESC ParamDesc[]) |
SAMPLE * | MakeSample (CLUSTERER *Clusterer, const float *Feature, int32_t CharID) |
LIST | ClusterSamples (CLUSTERER *Clusterer, CLUSTERCONFIG *Config) |
void | FreeClusterer (CLUSTERER *Clusterer) |
void | FreeProtoList (LIST *ProtoList) |
void | FreePrototype (void *arg) |
CLUSTER * | NextSample (LIST *SearchState) |
float | Mean (PROTOTYPE *Proto, uint16_t Dimension) |
float | StandardDeviation (PROTOTYPE *Proto, uint16_t Dimension) |
Variables | |
const double | FTable [FTABLE_Y][FTABLE_X] |
#define Abs | ( | N | ) | (((N) < 0) ? (-(N)) : (N)) |
Definition at line 209 of file cluster.cpp.
#define BUCKETTABLESIZE 1024 |
define the size of the table which maps normalized samples to histogram buckets. Also define the number of standard deviations in a normal distribution which are considered to be significant. The mapping table will be defined in such a way that it covers the specified number of standard deviations on either side of the mean. BUCKETTABLESIZE should always be even.
Definition at line 161 of file cluster.cpp.
#define CHIACCURACY 0.01 |
#define DELTARATIO 0.1 |
#define FTABLE_X 10 |
Definition at line 32 of file cluster.cpp.
#define FTABLE_Y 100 |
Definition at line 33 of file cluster.cpp.
#define HOTELLING 1 |
Definition at line 31 of file cluster.cpp.
#define ILLEGAL_CHAR 2 |
#define INITIALDELTA 0.1 |
#define LOOKUPTABLESIZE 8 |
define lookup tables used to compute the number of histogram buckets that should be used for a given number of samples.
Definition at line 229 of file cluster.cpp.
#define MAXDEGREESOFFREEDOM MAXBUCKETS |
Definition at line 230 of file cluster.cpp.
#define MAXDISTANCE FLT_MAX |
#define MAXNEIGHBORS 2 |
#define MINALPHA (1e-200) |
#define MINSAMPLES (MINBUCKETS * MINSAMPLESPERBUCKET) |
Definition at line 152 of file cluster.cpp.
#define MINSAMPLESNEEDED 1 |
Definition at line 153 of file cluster.cpp.
#define MINSAMPLESPERBUCKET 5 |
define the absolute minimum number of samples which must be present in order to accurately test hypotheses about underlying probability distributions. Define separately the minimum samples that are needed before a statistical analysis is attempted; this number should be equal to MINSAMPLES but can be set to a lower number for early testing when very few samples are available.
Definition at line 151 of file cluster.cpp.
#define MINVARIANCE 0.0004 |
define the variance which will be used as a minimum variance for any dimension of any feature. Since most features are calculated from numbers with a precision no better than 1 in 128, the variance should never be less than the square of this number for parameters whose range is 1.
Definition at line 143 of file cluster.cpp.
#define Mirror | ( | N, | |
R | |||
) | ((R) - (N) - 1) |
Definition at line 208 of file cluster.cpp.
#define NORMALEXTENT 3.0 |
Definition at line 162 of file cluster.cpp.
#define Odd | ( | N | ) | ((N)%2) |
Definition at line 207 of file cluster.cpp.
#define SqrtOf2Pi 2.506628275 |
the following variables describe a discrete normal distribution which is used by NormalDensity() and NormalBucket(). The constant NORMALEXTENT determines how many standard deviations of the distribution are mapped onto the fixed discrete range of x. x=0 is mapped to -NORMALEXTENT standard deviations and x=BUCKETTABLESIZE is mapped to +NORMALEXTENT standard deviations.
Definition at line 219 of file cluster.cpp.
using ClusterHeap = tesseract::GenericHeap<ClusterPair> |
Definition at line 170 of file cluster.cpp.
using ClusterPair = tesseract::KDPairInc<float, TEMPCLUSTER*> |
Definition at line 169 of file cluster.cpp.
typedef double(* DENSITYFUNC) (int32_t) |
Definition at line 204 of file cluster.cpp.
typedef double(* SOLVEFUNC) (CHISTRUCT *, double) |
Definition at line 205 of file cluster.cpp.
void AdjustBuckets | ( | BUCKETS * | Buckets, |
uint32_t | NewSampleCount | ||
) |
This routine multiplies each ExpectedCount histogram entry by NewSampleCount/OldSampleCount so that the histogram is now adjusted to the new sample count.
Buckets | histogram data structure to adjust |
NewSampleCount | new sample count to adjust to |
Definition at line 2162 of file cluster.cpp.
int AlphaMatch | ( | void * | arg1, |
void * | arg2 | ||
) |
This routine is used to search a list of structures which hold pre-computed chi-squared values for a chi-squared value whose corresponding alpha field matches the alpha field of SearchKey.
It is called by the list search routines.
arg1 | chi-squared struct being tested for a match |
arg2 | chi-squared struct that is the search key |
Definition at line 2204 of file cluster.cpp.
double ChiArea | ( | CHISTRUCT * | ChiParams, |
double | x | ||
) |
This routine computes the area under a chi density curve from 0 to x, minus the desired area under the curve. The number of degrees of freedom of the chi curve is specified in the ChiParams structure. The desired area is also specified in the ChiParams structure as Alpha (or 1 minus the desired area). This routine is intended to be passed to the Solve() function to find the value of chi-squared which will yield a desired area under the right tail of the chi density curve. The function will only work for even degrees of freedom. The equations are based on integrating the chi density curve in parts to obtain a series that can be used to compute the area under the curve.
ChiParams | contains degrees of freedom and alpha |
x | value of chi-squared to evaluate |
Definition at line 2310 of file cluster.cpp.
LIST ClusterSamples | ( | CLUSTERER * | Clusterer, |
CLUSTERCONFIG * | Config | ||
) |
This routine first checks to see if the samples in this clusterer have already been clustered before; if so, it does not bother to recreate the cluster tree. It simply recomputes the prototypes based on the new Config info.
If the samples have not been clustered before, the samples in the KD tree are formed into a cluster tree and then the prototypes are computed from the cluster tree.
In either case this routine returns a pointer to a list of prototypes that best represent the samples given the constraints specified in Config.
Clusterer | data struct containing samples to be clustered |
Config | parameters which control clustering process |
Definition at line 506 of file cluster.cpp.
double ComputeChiSquared | ( | uint16_t | DegreesOfFreedom, |
double | Alpha | ||
) |
This routine computes the chi-squared value which will leave a cumulative probability of Alpha in the right tail of a chi-squared distribution with the specified number of degrees of freedom. Alpha must be between 0 and 1. DegreesOfFreedom must be even. The routine maintains an array of lists. Each list corresponds to a different number of degrees of freedom. Each entry in the list corresponds to a different alpha value and its corresponding chi-squared value. Therefore, once a particular chi-squared value is computed, it is stored in the list and never needs to be computed again.
DegreesOfFreedom | determines shape of distribution |
Alpha | probability of right tail |
Definition at line 1799 of file cluster.cpp.
void ComputePrototypes | ( | CLUSTERER * | Clusterer, |
CLUSTERCONFIG * | Config | ||
) |
This routine decides which clusters in the cluster tree should be represented by prototypes, forms a list of these prototypes, and places the list in the Clusterer data structure.
Clusterer | data structure holding cluster tree |
Config | parameters used to control prototype generation |
Definition at line 894 of file cluster.cpp.
STATISTICS * ComputeStatistics | ( | int16_t | N, |
PARAM_DESC | ParamDesc[], | ||
CLUSTER * | Cluster | ||
) |
This routine searches the cluster tree for all leaf nodes which are samples in the specified cluster. It computes a full covariance matrix for these samples as well as keeping track of the ranges (min and max) for each dimension. A special data structure is allocated to return this information to the caller. An incremental algorithm for computing statistics is not used because it will not work with circular dimensions.
N | number of dimensions |
ParamDesc | array of dimension descriptions |
Cluster | cluster whose stats are to be computed |
Definition at line 1360 of file cluster.cpp.
void CreateClusterTree | ( | CLUSTERER * | Clusterer | ) |
This routine performs a bottoms-up clustering on the samples held in the kd-tree of the Clusterer data structure. The result is a cluster tree. Each node in the tree represents a cluster which conceptually contains a subset of the samples. More precisely, the cluster contains all of the samples which are contained in its two sub-clusters. The leaves of the tree are the individual samples themselves; they have no sub-clusters. The root node of the tree conceptually contains all of the samples.
Clusterer | data structure holdings samples to be clustered |
Definition at line 678 of file cluster.cpp.
uint16_t DegreesOfFreedom | ( | DISTRIBUTION | Distribution, |
uint16_t | HistogramBuckets | ||
) |
This routine computes the degrees of freedom that should be used in a chi-squared test with the specified number of histogram buckets. The result is always rounded up to the next even number so that the value of chi-squared can be computed more easily. This will cause the value of chi-squared to be higher than the optimum value, resulting in the chi-square test being more lenient than optimum.
Distribution | distribution being tested for |
HistogramBuckets | number of buckets in chi-square test |
Definition at line 2113 of file cluster.cpp.
bool DistributionOK | ( | BUCKETS * | Buckets | ) |
This routine performs a chi-square goodness of fit test on the histogram data in the Buckets data structure. TRUE is returned if the histogram matches the probability distribution which was specified when the Buckets structure was originally created. Otherwise FALSE is returned.
Buckets | histogram data to perform chi-square test on |
Definition at line 2040 of file cluster.cpp.
void FillBuckets | ( | BUCKETS * | Buckets, |
CLUSTER * | Cluster, | ||
uint16_t | Dim, | ||
PARAM_DESC * | ParamDesc, | ||
float | Mean, | ||
float | StdDev | ||
) |
This routine counts the number of cluster samples which fall within the various histogram buckets in Buckets. Only one dimension of each sample is examined. The exact meaning of the Mean and StdDev parameters depends on the distribution which is being analyzed (this info is in the Buckets data structure). For normal distributions, Mean and StdDev have the expected meanings. For uniform and random distributions the Mean is the center point of the range and the StdDev is 1/2 the range. A dimension with zero standard deviation cannot be statistically analyzed. In this case, a pseudo-analysis is used.
Buckets | histogram buckets to count samples |
Cluster | cluster whose samples are being analyzed |
Dim | dimension of samples which is being analyzed |
ParamDesc | description of the dimension |
Mean | "mean" of the distribution |
StdDev | "standard deviation" of the distribution |
Definition at line 1905 of file cluster.cpp.
This routine searches the specified kd-tree for the nearest neighbor of the specified cluster. It actually uses the kd routines to find the 2 nearest neighbors since one of them will be the original cluster. A pointer to the nearest neighbor is returned, if it can be found, otherwise nullptr is returned. The distance between the 2 nodes is placed in the specified variable.
Tree | kd-tree to search in for nearest neighbor |
Cluster | cluster whose nearest neighbor is to be found |
Distance | ptr to variable to report distance found |
Definition at line 775 of file cluster.cpp.
void FreeBuckets | ( | BUCKETS * | buckets | ) |
This routine properly frees the memory used by a BUCKETS.
buckets | pointer to data structure to be freed |
Definition at line 2078 of file cluster.cpp.
void FreeCluster | ( | CLUSTER * | Cluster | ) |
This routine frees the memory consumed by the specified cluster and all of its subclusters. This is done by recursive calls to FreeCluster().
Cluster | pointer to cluster to be freed |
Definition at line 2093 of file cluster.cpp.
void FreeClusterer | ( | CLUSTERER * | Clusterer | ) |
This routine frees all of the memory allocated to the specified data structure. It will not, however, free the memory used by the prototype list. The pointers to the clusters for each prototype in the list will be set to nullptr to indicate that the cluster data structures no longer exist. Any sample lists that have been obtained via calls to GetSamples are no longer valid.
Clusterer | pointer to data structure to be freed |
Definition at line 538 of file cluster.cpp.
void FreeProtoList | ( | LIST * | ProtoList | ) |
This routine frees all of the memory allocated to the specified list of prototypes. The clusters which are pointed to by the prototypes are not freed.
ProtoList | pointer to list of prototypes to be freed |
Definition at line 563 of file cluster.cpp.
void FreePrototype | ( | void * | arg | ) |
This routine deallocates the memory consumed by the specified prototype and modifies the corresponding cluster so that it is no longer marked as a prototype. The cluster is NOT deallocated by this routine.
arg | prototype data structure to be deallocated |
Definition at line 575 of file cluster.cpp.
void FreeStatistics | ( | STATISTICS * | Statistics | ) |
This routine frees the memory used by the statistics data structure.
Statistics | pointer to data structure to be freed |
Definition at line 2066 of file cluster.cpp.
BUCKETS * GetBuckets | ( | CLUSTERER * | clusterer, |
DISTRIBUTION | Distribution, | ||
uint32_t | SampleCount, | ||
double | Confidence | ||
) |
This routine returns a histogram data structure which can be used by other routines to place samples into histogram buckets, and then apply a goodness of fit test to the histogram data to determine if the samples belong to the specified probability distribution. The routine keeps a list of bucket data structures which have already been created so that it minimizes the computation time needed to create a new bucket.
clusterer | which keeps a bucket_cache for us. |
Distribution | type of probability distribution to test for |
SampleCount | number of samples that are available |
Confidence | probability of a Type I error |
Definition at line 1624 of file cluster.cpp.
bool Independent | ( | PARAM_DESC * | ParamDesc, |
int16_t | N, | ||
float * | CoVariance, | ||
float | Independence | ||
) |
This routine returns TRUE if the specified covariance matrix indicates that all N dimensions are independent of one another. One dimension is judged to be independent of another when the magnitude of the corresponding correlation coefficient is less than the specified Independence factor. The correlation coefficient is calculated as: (see Duda and Hart, pg. 247) coeff[ij] = stddev[ij] / sqrt (stddev[ii] * stddev[jj]) The covariance matrix is assumed to be symmetric (which should always be true).
ParamDesc | descriptions of each feature space dimension |
N | number of dimensions |
CoVariance | ptr to a covariance matrix |
Independence | max off-diagonal correlation coefficient |
Definition at line 1579 of file cluster.cpp.
void InitBuckets | ( | BUCKETS * | Buckets | ) |
This routine sets the bucket counts in the specified histogram to zero.
Buckets | histogram data structure to init |
Definition at line 2183 of file cluster.cpp.
double Integral | ( | double | f1, |
double | f2, | ||
double | Dx | ||
) |
This routine computes a trapezoidal approximation to the integral of a function over a small delta in x.
f1 | value of function at x1 |
f2 | value of function at x2 |
Dx | x2 - x1 (should always be positive) |
Definition at line 1881 of file cluster.cpp.
double InvertMatrix | ( | const float * | input, |
int | size, | ||
float * | inv | ||
) |
Compute the inverse of a matrix using LU decomposition with partial pivoting. The return value is the sum of norms of the off-diagonal terms of the product of a and inv. (A measure of the error.)
Definition at line 2409 of file cluster.cpp.
int ListEntryMatch | ( | void * | arg1, |
void * | arg2 | ||
) |
This routine is used to search a list for a list node whose contents match Key. It is called by the list delete_d routine.
Definition at line 2148 of file cluster.cpp.
BUCKETS * MakeBuckets | ( | DISTRIBUTION | Distribution, |
uint32_t | SampleCount, | ||
double | Confidence | ||
) |
This routine creates a histogram data structure which can be used by other routines to place samples into histogram buckets, and then apply a goodness of fit test to the histogram data to determine if the samples belong to the specified probability distribution. The buckets are allocated in such a way that the expected frequency of samples in each bucket is approximately the same. In order to make this possible, a mapping table is computed which maps "normalized" samples into the appropriate bucket.
Distribution | type of probability distribution to test for |
SampleCount | number of samples that are available |
Confidence | probability of a Type I error |
Definition at line 1669 of file cluster.cpp.
CLUSTERER* MakeClusterer | ( | int16_t | SampleSize, |
const PARAM_DESC | ParamDesc[] | ||
) |
This routine creates a new clusterer data structure, initializes it, and returns a pointer to it.
SampleSize | number of dimensions in feature space |
ParamDesc | description of each dimension |
Definition at line 399 of file cluster.cpp.
PROTOTYPE * MakeDegenerateProto | ( | uint16_t | N, |
CLUSTER * | Cluster, | ||
STATISTICS * | Statistics, | ||
PROTOSTYLE | Style, | ||
int32_t | MinSamples | ||
) |
This routine checks for clusters which are degenerate and therefore cannot be analyzed in a statistically valid way. A cluster is defined as degenerate if it does not have at least MINSAMPLESNEEDED samples in it. If the cluster is found to be degenerate, a prototype of the specified style is generated and marked as insignificant. A cluster is also degenerate if it does not have at least MinSamples samples in it.
If the cluster is not degenerate, nullptr is returned.
N | number of dimensions |
Cluster | cluster being analyzed |
Statistics | statistical info about cluster |
Style | type of prototype to be generated |
MinSamples | minimum number of samples in a cluster |
Definition at line 1027 of file cluster.cpp.
void MakeDimRandom | ( | uint16_t | i, |
PROTOTYPE * | Proto, | ||
PARAM_DESC * | ParamDesc | ||
) |
This routine alters the ith dimension of the specified mixed prototype to be D_random.
i | index of dimension to be changed |
Proto | prototype whose dimension is to be altered |
ParamDesc | description of specified dimension |
Definition at line 1304 of file cluster.cpp.
void MakeDimUniform | ( | uint16_t | i, |
PROTOTYPE * | Proto, | ||
STATISTICS * | Statistics | ||
) |
This routine alters the ith dimension of the specified mixed prototype to be uniform.
i | index of dimension to be changed |
Proto | prototype whose dimension is to be altered |
Statistics | statistical info about prototype |
Definition at line 1326 of file cluster.cpp.
PROTOTYPE * MakeEllipticalProto | ( | CLUSTERER * | Clusterer, |
CLUSTER * | Cluster, | ||
STATISTICS * | Statistics, | ||
BUCKETS * | Buckets | ||
) |
This routine tests the specified cluster to see if it can be approximated by an elliptical normal distribution. If it can be, then a new prototype is formed and returned to the caller. If it can't be, then nullptr is returned to the caller.
Clusterer | data struct containing samples being clustered |
Cluster | cluster to be made into an elliptical prototype |
Statistics | statistical info about cluster |
Buckets | histogram struct used to analyze distribution |
Definition at line 1205 of file cluster.cpp.
PROTOTYPE * MakeMixedProto | ( | CLUSTERER * | Clusterer, |
CLUSTER * | Cluster, | ||
STATISTICS * | Statistics, | ||
BUCKETS * | NormalBuckets, | ||
double | Confidence | ||
) |
This routine tests each dimension of the specified cluster to see what distribution would best approximate that dimension. Each dimension is compared to the following distributions in order: normal, random, uniform. If each dimension can be represented by one of these distributions, then a new prototype is formed and returned to the caller. If it can't be, then nullptr is returned to the caller.
Clusterer | data struct containing samples being clustered |
Cluster | cluster to be made into a prototype |
Statistics | statistical info about cluster |
NormalBuckets | histogram struct used to analyze distribution |
Confidence | confidence level for alternate distributions |
Definition at line 1245 of file cluster.cpp.
CLUSTER * MakeNewCluster | ( | CLUSTERER * | Clusterer, |
TEMPCLUSTER * | TempCluster | ||
) |
This routine creates a new permanent cluster from the clusters specified in TempCluster. The 2 clusters in TempCluster are marked as "clustered" and deleted from the kd-tree. The new cluster is then added to the kd-tree.
Clusterer | current clustering environment |
TempCluster | potential cluster to make permanent |
Definition at line 810 of file cluster.cpp.
void MakePotentialClusters | ( | ClusteringContext * | context, |
CLUSTER * | Cluster, | ||
int32_t | Level | ||
) |
This routine is designed to be used in concert with the KDWalk routine. It will create a potential cluster for each sample in the kd-tree that is being walked. This potential cluster will then be pushed on the heap.
context | ClusteringContext (see definition above) |
Cluster | current cluster being visited in kd-tree walk |
Level | level of this cluster in the kd-tree |
Definition at line 745 of file cluster.cpp.
PROTOTYPE * MakePrototype | ( | CLUSTERER * | Clusterer, |
CLUSTERCONFIG * | Config, | ||
CLUSTER * | Cluster | ||
) |
This routine attempts to create a prototype from the specified cluster that conforms to the distribution specified in Config. If there are too few samples in the cluster to perform a statistical analysis, then a prototype is generated but labelled as insignificant. If the dimensions of the cluster are not independent, no prototype is generated and nullptr is returned. If a prototype can be found that matches the desired distribution then a pointer to it is returned, otherwise nullptr is returned.
Clusterer | data structure holding cluster tree |
Config | parameters used to control prototype generation |
Cluster | cluster to be made into a prototype |
Definition at line 937 of file cluster.cpp.
This routine creates a new sample data structure to hold the specified feature. This sample is added to the clusterer data structure (so that it knows which samples are to be clustered later), and a pointer to the sample is returned to the caller.
Clusterer | clusterer data structure to add sample to |
Feature | feature to be added to clusterer |
CharID | unique ident. of char that sample came from |
Definition at line 452 of file cluster.cpp.
PROTOTYPE * MakeSphericalProto | ( | CLUSTERER * | Clusterer, |
CLUSTER * | Cluster, | ||
STATISTICS * | Statistics, | ||
BUCKETS * | Buckets | ||
) |
This routine tests the specified cluster to see if it can be approximated by a spherical normal distribution. If it can be, then a new prototype is formed and returned to the caller. If it can't be, then nullptr is returned to the caller.
Clusterer | data struct containing samples being clustered |
Cluster | cluster to be made into a spherical prototype |
Statistics | statistical info about cluster |
Buckets | histogram struct used to analyze distribution |
Definition at line 1170 of file cluster.cpp.
float Mean | ( | PROTOTYPE * | Proto, |
uint16_t | Dimension | ||
) |
This routine returns the mean of the specified prototype in the indicated dimension.
Proto | prototype to return mean of |
Dimension | dimension whose mean is to be returned |
Definition at line 628 of file cluster.cpp.
int32_t MergeClusters | ( | int16_t | N, |
PARAM_DESC | ParamDesc[], | ||
int32_t | n1, | ||
int32_t | n2, | ||
float | m[], | ||
float | m1[], | ||
float | m2[] | ||
) |
This routine merges two clusters into one larger cluster. To do this it computes the number of samples in the new cluster and the mean of the new cluster. The ParamDesc information is used to ensure that circular dimensions are handled correctly.
N | # of dimensions (size of arrays) |
ParamDesc | array of dimension descriptions |
n1,n2 | number of samples in each old cluster |
m | array to hold mean of new cluster |
m1,m2 | arrays containing means of old clusters |
Definition at line 852 of file cluster.cpp.
This routine looks at all samples in the specified cluster. It computes a running estimate of the percentage of the characters which have more than 1 sample in the cluster. When this percentage exceeds MaxIllegal, TRUE is returned. Otherwise FALSE is returned. The CharID fields must contain integers which identify the training characters which were used to generate the sample. One integer is used for each sample. The NumChar field in the Clusterer must contain the number of characters in the training set. All CharID fields must be between 0 and NumChar-1. The main function of this routine is to help identify clusters which need to be split further, i.e. if numerous training characters have 2 or more features which are contained in the same cluster, then the cluster should be split.
Clusterer | data structure holding cluster tree |
Cluster | cluster containing samples to be tested |
MaxIllegal | max percentage of samples allowed to have more than 1 feature in the cluster |
Definition at line 2353 of file cluster.cpp.
CHISTRUCT * NewChiStruct | ( | uint16_t | DegreesOfFreedom, |
double | Alpha | ||
) |
This routine allocates a new data structure which is used to hold a chi-squared value along with its associated number of degrees of freedom and alpha value.
DegreesOfFreedom | degrees of freedom for new chi value |
Alpha | confidence level for new chi value |
Definition at line 2222 of file cluster.cpp.
PROTOTYPE * NewEllipticalProto | ( | int16_t | N, |
CLUSTER * | Cluster, | ||
STATISTICS * | Statistics | ||
) |
This routine creates an elliptical prototype data structure to approximate the samples in the specified cluster. Elliptical prototypes have a variance for each dimension. All dimensions are normally distributed and independent.
N | number of dimensions |
Cluster | cluster to be made into an elliptical prototype |
Statistics | statistical info about samples in cluster |
Definition at line 1476 of file cluster.cpp.
PROTOTYPE * NewMixedProto | ( | int16_t | N, |
CLUSTER * | Cluster, | ||
STATISTICS * | Statistics | ||
) |
This routine creates a mixed prototype data structure to approximate the samples in the specified cluster. Mixed prototypes can have different distributions for each dimension. All dimensions are independent. The structure is initially filled in as though it were an elliptical prototype. The actual distributions of the dimensions can be altered by other routines.
N | number of dimensions |
Cluster | cluster to be made into a mixed prototype |
Statistics | statistical info about samples in cluster |
Definition at line 1518 of file cluster.cpp.
This routine allocates memory to hold a simple prototype data structure, i.e. one without independent distributions and variances for each dimension.
N | number of dimensions |
Cluster | cluster to be made into a prototype |
Definition at line 1540 of file cluster.cpp.
PROTOTYPE * NewSphericalProto | ( | uint16_t | N, |
CLUSTER * | Cluster, | ||
STATISTICS * | Statistics | ||
) |
This routine creates a spherical prototype data structure to approximate the samples in the specified cluster. Spherical prototypes have a single variance which is common across all dimensions. All dimensions are normally distributed and independent.
N | number of dimensions |
Cluster | cluster to be made into a spherical prototype |
Statistics | statistical info about samples in cluster |
Definition at line 1445 of file cluster.cpp.
This routine is used to find all of the samples which belong to a cluster. It starts by removing the top cluster on the cluster list (SearchState). If this cluster is a leaf it is returned. Otherwise, the right subcluster is pushed on the list and we continue the search in the left subcluster. This continues until a leaf is found. If all samples have been found, nullptr is returned. InitSampleSearch() must be called before NextSample() to initialize the search.
SearchState | ptr to list containing clusters to be searched |
Definition at line 606 of file cluster.cpp.
uint16_t NormalBucket | ( | PARAM_DESC * | ParamDesc, |
float | x, | ||
float | Mean, | ||
float | StdDev | ||
) |
This routine determines which bucket x falls into in the discrete normal distribution defined by kNormalMean and kNormalStdDev. x values which exceed the range of the discrete distribution are clipped.
ParamDesc | used to identify circular dimensions |
x | value to be normalized |
Mean | mean of normal distribution |
StdDev | standard deviation of normal distribution |
Definition at line 1975 of file cluster.cpp.
double NormalDensity | ( | int32_t | x | ) |
This routine computes the probability density function of a discrete normal distribution defined by the global variables kNormalMean, kNormalVariance, and kNormalMagnitude. Normal magnitude could, of course, be computed in terms of the normal variance but it is precomputed for efficiency.
x | number to compute the normal probability density for |
Definition at line 1850 of file cluster.cpp.
int NumBucketsMatch | ( | void * | arg1, |
void * | arg2 | ||
) |
This routine is used to search a list of histogram data structures to find one with the specified number of buckets. It is called by the list search routines.
arg1 | current histogram being tested for a match |
arg2 | match key |
Definition at line 2133 of file cluster.cpp.
uint16_t OptimumNumberOfBuckets | ( | uint32_t | SampleCount | ) |
This routine computes the optimum number of histogram buckets that should be used in a chi-squared goodness of fit test for the specified number of samples. The optimum number is computed based on Table 4.1 on pg. 147 of "Measurement and Analysis of Random Data" by Bendat & Piersol. Linear interpolation is used to interpolate between table values. The table is intended for a 0.05 level of significance (alpha). This routine assumes that it is equally valid for other alpha's, which may not be true.
SampleCount | number of samples to be tested |
Definition at line 1764 of file cluster.cpp.
double Solve | ( | SOLVEFUNC | Function, |
void * | FunctionParams, | ||
double | InitialGuess, | ||
double | Accuracy | ||
) |
This routine attempts to find an x value at which Function goes to zero (i.e. a root of the function). It will only work correctly if a solution actually exists and there are no extrema between the solution and the InitialGuess. The algorithms used are extremely primitive.
Function | function whose zero is to be found |
FunctionParams | arbitrary data to pass to function |
InitialGuess | point to start solution search at |
Accuracy | maximum allowed error |
Definition at line 2246 of file cluster.cpp.
float StandardDeviation | ( | PROTOTYPE * | Proto, |
uint16_t | Dimension | ||
) |
This routine returns the standard deviation of the prototype in the indicated dimension.
Proto | prototype to return standard deviation of |
Dimension | dimension whose stddev is to be returned |
Definition at line 639 of file cluster.cpp.
PROTOTYPE * TestEllipticalProto | ( | CLUSTERER * | Clusterer, |
CLUSTERCONFIG * | Config, | ||
CLUSTER * | Cluster, | ||
STATISTICS * | Statistics | ||
) |
This routine tests the specified cluster to see if ** there is a statistically significant difference between the sub-clusters that would be made if the cluster were to be split. If not, then a new prototype is formed and returned to the caller. If there is, then nullptr is returned to the caller.
Clusterer | data struct containing samples being clustered |
Config | provides the magic number of samples that make a good cluster |
Cluster | cluster to be made into an elliptical prototype |
Statistics | statistical info about cluster |
Definition at line 1069 of file cluster.cpp.
uint16_t UniformBucket | ( | PARAM_DESC * | ParamDesc, |
float | x, | ||
float | Mean, | ||
float | StdDev | ||
) |
This routine determines which bucket x falls into in the discrete uniform distribution defined by BUCKETTABLESIZE. x values which exceed the range of the discrete distribution are clipped.
ParamDesc | used to identify circular dimensions |
x | value to be normalized |
Mean | center of range of uniform distribution |
StdDev | 1/2 the range of the uniform distribution |
Definition at line 2008 of file cluster.cpp.
double UniformDensity | ( | int32_t | x | ) |
This routine computes the probability density function of a uniform distribution at the specified point. The range of the distribution is from 0 to BUCKETTABLESIZE.
x | number to compute the uniform probability density for |
Definition at line 1864 of file cluster.cpp.
Definition at line 36 of file cluster.cpp.