GRASS GIS 7 Programmer's Manual  7.0.2(2015)-r00000
c_exec.c
Go to the documentation of this file.
1 
14 #include <grass/cluster.h>
15 #include <grass/glocale.h>
16 
32 int I_cluster_exec(struct Cluster *C, int maxclass, int iterations,
33  double convergence,
34  double separation, int min_class_size,
35  int (*checkpoint) (), int *interrupted)
36 {
37  int changes;
38 
39  /* set interrupted to false */
40  *interrupted = 0;
41 
42  /* check for valid inputs */
43  if (C->npoints < 2) {
44  G_warning(_("Not enough data points (%d) in cluster"), C->npoints);
45  return 1;
46  }
47 
48  /* check other parms */
49  if (maxclass < 0)
50  maxclass = 1;
51  C->nclasses = maxclass;
52 
53  if (min_class_size <= 0)
54  min_class_size = 17;
55  if (min_class_size < 2)
56  min_class_size = 2;
57 
58  if (iterations <= 0)
59  iterations = 20;
60  if (convergence <= 0.0)
61  convergence = 98.0;
62  if (separation < 0.0)
63  separation = 0.5;
64 
65 
66  /* allocate memory */
68  return -1;
69 
70 
71  /* generate class means */
72  I_cluster_means(C);
73  if (checkpoint)
74  (*checkpoint) (C, 1);
75 
76  /* now assign points to nearest class */
77  I_cluster_assign(C, interrupted);
78  if (*interrupted)
79  return -2;
80  I_cluster_sum2(C);
81  if (checkpoint)
82  (*checkpoint) (C, 2);
83 
84  /* get rid of empty classes now */
85  I_cluster_reclass(C, 1);
86 
87  for (C->iteration = 1;; C->iteration++) {
88  if (*interrupted)
89  return -2;
90 
91  changes = 0;
92 
93  /* re-assign points to nearest class */
94 
95  changes = I_cluster_reassign(C, interrupted);
96  if (*interrupted)
97  return -2;
98 
99  /* if too many points have changed class, re-assign points */
100  C->percent_stable = (C->npoints - changes) * 100.0;
101  C->percent_stable /= (double)C->npoints;
102 
103  if (checkpoint)
104  (*checkpoint) (C, 3);
105 
106  if (C->iteration >= iterations)
107  break;
108 
109  if (C->percent_stable < convergence)
110  continue;
111 
112  /* otherwise merge non-distinct classes */
113 
114  if (I_cluster_distinct(C, separation))
115  break;
116 
117  if (checkpoint)
118  (*checkpoint) (C, 4);
119 
120  I_cluster_merge(C);
121  }
122 
123  /* get rid of small classes */
124  I_cluster_reclass(C, min_class_size);
125  I_cluster_sum2(C);
126 
127  /* compute the resulting signatures */
129 
130 
131  return 0;
132 }
int I_cluster_merge(struct Cluster *C)
?
Definition: c_merge.c:23
int I_cluster_exec_allocate(struct Cluster *C)
Allocate Cluster structure.
Definition: c_execmem.c:24
int I_cluster_exec(struct Cluster *C, int maxclass, int iterations, double convergence, double separation, int min_class_size, int(*checkpoint)(), int *interrupted)
Definition: c_exec.c:32
int I_cluster_means(struct Cluster *C)
Calculate means value.
Definition: c_means.c:24
int I_cluster_assign(struct Cluster *C, int *interrupted)
Assign cluster.
Definition: c_assign.c:26
int I_cluster_sum2(struct Cluster *C)
Compute sum of squares for each class.
Definition: c_sum2.c:23
int I_cluster_reclass(struct Cluster *C, int minsize)
Reclass data.
Definition: c_reclass.c:25
if(!DBFLoadRecord(psDBF, hEntity)) return NULL
int I_cluster_distinct(struct Cluster *C, double separation)
Get distinct value.
Definition: c_distinct.c:24
int I_cluster_reassign(struct Cluster *C, int *interrupted)
?
Definition: c_reassign.c:25
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition: gis/error.c:203
int I_cluster_signatures(struct Cluster *C)
Create signatures.
Definition: c_sig.c:23