2007-03-08 04:03:40 +08:00
|
|
|
/******************************************************************************
|
|
|
|
** Filename: cluster.h
|
|
|
|
** Purpose: Definition of feature space clustering routines
|
|
|
|
** Author: Dan Johnson
|
|
|
|
** History: 5/29/89, DSJ, Created.
|
|
|
|
**
|
|
|
|
** (c) Copyright Hewlett-Packard Company, 1988.
|
|
|
|
** Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
** you may not use this file except in compliance with the License.
|
|
|
|
** You may obtain a copy of the License at
|
|
|
|
** http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
** Unless required by applicable law or agreed to in writing, software
|
|
|
|
** distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
** See the License for the specific language governing permissions and
|
|
|
|
** limitations under the License.
|
|
|
|
******************************************************************************/
|
|
|
|
#ifndef CLUSTER_H
|
|
|
|
#define CLUSTER_H
|
|
|
|
|
|
|
|
#include "kdtree.h"
|
|
|
|
#include "oldlist.h"
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------
|
|
|
|
Types
|
|
|
|
----------------------------------------------------------------------*/
|
|
|
|
typedef struct sample
|
|
|
|
{
|
|
|
|
unsigned Clustered:1; // TRUE if included in a higher cluster
|
|
|
|
unsigned Prototype:1; // TRUE if cluster represented by a proto
|
|
|
|
unsigned SampleCount:30; // number of samples in this cluster
|
|
|
|
struct sample *Left; // ptr to left sub-cluster
|
|
|
|
struct sample *Right; // ptr to right sub-cluster
|
2008-04-22 08:23:04 +08:00
|
|
|
inT32 CharID; // identifier of char sample came from
|
2007-03-08 04:03:40 +08:00
|
|
|
FLOAT32 Mean[1]; // mean of cluster - SampleSize floats
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CLUSTER;
|
|
|
|
|
|
|
|
typedef CLUSTER SAMPLE; // can refer to as either sample or cluster
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
spherical, elliptical, mixed, automatic
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PROTOSTYLE;
|
|
|
|
|
|
|
|
typedef struct // parameters to control clustering
|
|
|
|
{
|
|
|
|
PROTOSTYLE ProtoStyle; // specifies types of protos to be made
|
|
|
|
FLOAT32 MinSamples; // min # of samples per proto - % of total
|
|
|
|
FLOAT32 MaxIllegal; // max percentage of samples in a cluster which have
|
|
|
|
// more than 1 feature in that cluster
|
|
|
|
FLOAT32 Independence; // desired independence between dimensions
|
|
|
|
FLOAT64 Confidence; // desired confidence in prototypes created
|
2008-02-01 08:07:59 +08:00
|
|
|
int MagicSamples; // Ideal number of samples in a cluster.
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CLUSTERCONFIG;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
normal, uniform, D_random
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DISTRIBUTION;
|
|
|
|
|
|
|
|
typedef union
|
|
|
|
{
|
|
|
|
FLOAT32 Spherical;
|
|
|
|
FLOAT32 *Elliptical;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FLOATUNION;
|
|
|
|
|
|
|
|
typedef struct proto
|
|
|
|
{
|
|
|
|
unsigned Significant:1; // TRUE if prototype is significant
|
2008-02-01 08:07:59 +08:00
|
|
|
unsigned Merged:1; // Merged after clustering so do not output
|
|
|
|
// but kept for display purposes. If it has no
|
|
|
|
// samples then it was actually merged.
|
|
|
|
// Otherwise it matched an already significant
|
|
|
|
// cluster.
|
2007-03-08 04:03:40 +08:00
|
|
|
unsigned Style:2; // spherical, elliptical, or mixed
|
2008-02-01 08:07:59 +08:00
|
|
|
unsigned NumSamples:28; // number of samples in the cluster
|
2007-03-08 04:03:40 +08:00
|
|
|
CLUSTER *Cluster; // ptr to cluster which made prototype
|
|
|
|
DISTRIBUTION *Distrib; // different distribution for each dimension
|
|
|
|
FLOAT32 *Mean; // prototype mean
|
|
|
|
FLOAT32 TotalMagnitude; // total magnitude over all dimensions
|
|
|
|
FLOAT32 LogMagnitude; // log base e of TotalMagnitude
|
|
|
|
FLOATUNION Variance; // prototype variance
|
|
|
|
FLOATUNION Magnitude; // magnitude of density function
|
|
|
|
FLOATUNION Weight; // weight of density function
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PROTOTYPE;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2008-04-22 08:23:04 +08:00
|
|
|
inT16 SampleSize; // number of parameters per sample
|
2007-03-08 04:03:40 +08:00
|
|
|
PARAM_DESC *ParamDesc; // description of each parameter
|
2008-04-22 08:23:04 +08:00
|
|
|
inT32 NumberOfSamples; // total number of samples being clustered
|
2007-03-08 04:03:40 +08:00
|
|
|
KDTREE *KDTree; // for optimal nearest neighbor searching
|
|
|
|
CLUSTER *Root; // ptr to root cluster of cluster tree
|
|
|
|
LIST ProtoList; // list of prototypes
|
2008-04-22 08:23:04 +08:00
|
|
|
inT32 NumChar; // # of characters represented by samples
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CLUSTERER;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2008-04-22 08:23:04 +08:00
|
|
|
inT32 NumSamples; // number of samples in list
|
|
|
|
inT32 MaxNumSamples; // maximum size of list
|
2007-03-08 04:03:40 +08:00
|
|
|
SAMPLE *Sample[1]; // array of ptrs to sample data structures
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SAMPLELIST;
|
|
|
|
|
|
|
|
// low level cluster tree analysis routines.
|
|
|
|
#define InitSampleSearch(S,C) (((C)==NULL)?(S=NIL):(S=push(NIL,(C))))
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------
|
|
|
|
Public Function Prototypes
|
|
|
|
--------------------------------------------------------------------------*/
|
2008-04-22 08:23:04 +08:00
|
|
|
CLUSTERER *MakeClusterer (inT16 SampleSize, PARAM_DESC ParamDesc[]);
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2008-04-22 08:23:04 +08:00
|
|
|
SAMPLE *MakeSample (CLUSTERER * Clusterer, FLOAT32 Feature[], inT32 CharID);
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2008-02-01 08:07:59 +08:00
|
|
|
LIST ClusterSamples(CLUSTERER *Clusterer, CLUSTERCONFIG *Config);
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2008-02-01 08:07:59 +08:00
|
|
|
void FreeClusterer(CLUSTERER *Clusterer);
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2008-02-01 08:07:59 +08:00
|
|
|
void FreeProtoList(LIST *ProtoList);
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
void FreePrototype(void *arg); //PROTOTYPE *Prototype);
|
|
|
|
|
2008-02-01 08:07:59 +08:00
|
|
|
CLUSTER *NextSample(LIST *SearchState);
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2008-04-22 08:23:04 +08:00
|
|
|
FLOAT32 Mean(PROTOTYPE *Proto, uinT16 Dimension);
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2008-04-22 08:23:04 +08:00
|
|
|
FLOAT32 StandardDeviation(PROTOTYPE *Proto, uinT16 Dimension);
|
2008-02-01 08:07:59 +08:00
|
|
|
|
2008-04-22 08:23:04 +08:00
|
|
|
inT32 MergeClusters(inT16 N, PARAM_DESC ParamDesc[], inT32 n1, inT32 n2,
|
2008-02-01 08:07:59 +08:00
|
|
|
FLOAT32 m[], FLOAT32 m1[], FLOAT32 m2[]);
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
//--------------Global Data Definitions and Declarations---------------------------
|
|
|
|
// define errors that can be trapped
|
|
|
|
#define ALREADYCLUSTERED 4000
|
|
|
|
#endif
|