GRASS 8 Programmer's Manual 8.6.0dev(2026)-55de52a352
Loading...
Searching...
No Matches
c_exec.c
Go to the documentation of this file.
1/*!
2 \file cluster/c_exec.c
3
4 \brief Cluster library - Execute clusterring
5
6 SPDX-FileCopyrightText: 2001-2009 GRASS Development Team
7 SPDX-License-Identifier: GPL-2.0-or-later
8
9 \author Original author CERL
10 */
11
12#include <grass/cluster.h>
13#include <grass/glocale.h>
14
15/*!
16 \param C pointer to Cluster structure
17 \param maxclass maximum number of classes
18 \param iterations maximum number of iterations
19 \param convergence percentage of points stable
20 \param separation minimum distance between class centroids
21 \param min_class_size minimum size of class
22 \param checkpoint routine to be called at various steps
23 \param interrupted boolean to check for interrupt
24
25 \return 0 ok
26 \return -1 out of memory
27 \return -2 interrupted
28 \return 1 not enough data points
29 */
30int I_cluster_exec(struct Cluster *C, int maxclass, int iterations,
31 double convergence, double separation, int min_class_size,
32 int (*checkpoint)(struct Cluster *, int), int *interrupted)
33{
34 int changes;
35
36 /* set interrupted to false */
37 *interrupted = 0;
38
39 /* check for valid inputs */
40 if (C->npoints < 2) {
41 G_warning(_("Not enough data points (%d) in cluster"), C->npoints);
42 return 1;
43 }
44
45 /* check other parms */
46 if (maxclass < 0)
47 maxclass = 1;
48 C->nclasses = maxclass;
49
50 if (min_class_size <= 0)
51 min_class_size = 17;
52 if (min_class_size < 2)
54
55 if (iterations <= 0)
56 iterations = 20;
57 if (convergence <= 0.0)
58 convergence = 98.0;
59 if (separation < 0.0)
60 separation = 0.5;
61
62 /* allocate memory */
64 return -1;
65
66 /* generate class means */
68 if (checkpoint)
69 (*checkpoint)(C, 1);
70
71 /* now assign points to nearest class */
73 if (*interrupted)
74 return -2;
76 if (checkpoint)
77 (*checkpoint)(C, 2);
78
79 /* get rid of empty classes now */
81
82 for (C->iteration = 1;; C->iteration++) {
83 if (*interrupted)
84 return -2;
85
86 changes = 0;
87
88 /* re-assign points to nearest class */
89
91 if (*interrupted)
92 return -2;
93
94 /* if too many points have changed class, re-assign points */
95 C->percent_stable = (C->npoints - changes) * 100.0;
96 C->percent_stable /= (double)C->npoints;
97
98 if (checkpoint)
99 (*checkpoint)(C, 3);
100
101 if (C->iteration >= iterations)
102 break;
103
104 if (C->percent_stable < convergence)
105 continue;
106
107 /* otherwise merge non-distinct classes */
108
110 break;
111
112 if (checkpoint)
113 (*checkpoint)(C, 4);
114
116 }
117
118 /* get rid of small classes */
121
122 /* compute the resulting signatures */
124
125 return 0;
126}
int I_cluster_exec(struct Cluster *C, int maxclass, int iterations, double convergence, double separation, int min_class_size, int(*checkpoint)(struct Cluster *, int), int *interrupted)
Definition c_exec.c:30
int I_cluster_signatures(struct Cluster *)
Create signatures.
Definition c_sig.c:21
int I_cluster_means(struct Cluster *)
Calculate means value.
Definition c_means.c:22
int I_cluster_exec_allocate(struct Cluster *)
Allocate Cluster structure.
Definition c_execmem.c:22
int I_cluster_merge(struct Cluster *)
?
Definition c_merge.c:21
int I_cluster_reassign(struct Cluster *, int *)
?
Definition c_reassign.c:23
int I_cluster_sum2(struct Cluster *)
Compute sum of squares for each class.
Definition c_sum2.c:21
int I_cluster_distinct(struct Cluster *, double)
Get distinct value.
Definition c_distinct.c:22
int I_cluster_reclass(struct Cluster *, int)
Reclass data.
Definition c_reclass.c:23
int I_cluster_assign(struct Cluster *, int *)
Assign cluster.
Definition c_assign.c:24
void G_warning(const char *,...) __attribute__((format(printf
#define _(str)
Definition glocale.h:10
int npoints
Definition cluster.h:9
int nclasses
Definition cluster.h:26