/
vsom.c
206 lines (170 loc) · 6.4 KB
/
vsom.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/************************************************************************
* *
* Program package 'som_pak': *
* *
* vsom.c *
* -Visualization Self-Organizing Map *
* *
* Version 3.0 *
* Date: 1 Mar 1995 *
* *
* NOTE: This program package is copyrighted in the sense that it *
* may be used for scientific purposes. The package as a whole, or *
* parts thereof, cannot be included or used in any commercial *
* application without written permission granted by its producents. *
* No programs contained in this package may be copied for commercial *
* distribution. *
* *
* All comments concerning this program package may be sent to the *
* e-mail address 'lvq@cochlea.hut.fi'. *
* *
************************************************************************/
#include <stdio.h>
#include <float.h>
#include <stdlib.h>
#include <math.h>
#include "lvq_pak.h"
#include "som_rout.h"
#include "datafile.h"
static char *usage[] = {
"vsom - teach self-organizing map\n",
"Required parameters:\n",
" -cin filename initial codebook file\n",
" -din filename teaching data\n",
" -cout filename output codebook filename\n",
" -rlen integer running length of teaching\n",
" -alpha float initial alpha value\n",
" -radius float initial radius of neighborhood\n",
"Optional parameters:\n",
" -rand integer seed for random number generator. 0 is current time\n",
" -fixed use fixed points\n",
" -weights use weights\n",
" -buffer integer buffered reading of data, integer lines at a time\n",
" -alpha_type type type of alpha decrease, linear (def) or inverse_t.\n",
" -snapfile filename snapshot filename\n",
" -selfuncs name select a set of functions\n",
" -snapinterval integer interval between snapshots\n",
NULL};
int main(int argc, char **argv)
{
char *in_data_file;
char *in_code_file;
char *out_code_file;
char *snapshot_file;
char *alpha_s, *rand_s;
struct entries *data = NULL, *codes = NULL;
long randomize;
int fixed;
int weights;
struct teach_params params;
long buffer = 0;
long snapshot_interval;
struct snapshot_info *snap = NULL;
int snap_type;
struct typelist *type_tmp;
int error = 0;
char *funcname = NULL;
data = codes = NULL;
global_options(argc, argv);
if (extract_parameter(argc, argv, "-help", OPTION2))
{
printhelp();
exit(0);
}
in_data_file = extract_parameter(argc, argv, IN_DATA_FILE, ALWAYS);
in_code_file = extract_parameter(argc, argv, IN_CODE_FILE, ALWAYS);
out_code_file = extract_parameter(argc, argv, OUT_CODE_FILE, ALWAYS);
params.length = oatoi(extract_parameter(argc, argv, RUNNING_LENGTH, ALWAYS),
1);
params.alpha = atof(extract_parameter(argc, argv, TRAINING_ALPHA, ALWAYS));
params.radius = atof(extract_parameter(argc, argv, TRAINING_RADIUS, ALWAYS));
rand_s = extract_parameter(argc, argv, RANDOM, OPTION);
randomize = oatoi(rand_s, 0);
fixed = (extract_parameter(argc, argv, FIXPOINTS, OPTION2) != NULL);
weights = (extract_parameter(argc, argv, WEIGHTS, OPTION2) != NULL);
buffer = oatoi(extract_parameter(argc, argv, "-buffer", OPTION), 0);
alpha_s = extract_parameter(argc, argv, "-alpha_type", OPTION);
funcname = extract_parameter(argc, argv, "-selfuncs", OPTION);
/* snapshots */
snapshot_file = extract_parameter(argc, argv, "-snapfile", OPTION);
snapshot_interval =
oatoi(extract_parameter(argc, argv, "-snapinterval", OPTION), 0);
snap_type =
get_id_by_str(snapshot_list,
extract_parameter(argc, argv, "-snaptype", OPTION));
use_fixed(fixed);
use_weights(weights);
label_not_needed(1);
if (snapshot_interval)
{
if (snapshot_file == NULL)
{
snapshot_file = out_code_file;
fprintf(stderr, "snapshot file not specified, using '%s'", snapshot_file);
}
snap = get_snapshot(snapshot_file, snapshot_interval, snap_type);
if (snap == NULL)
exit(1);
}
ifverbose(2)
fprintf(stderr, "Input entries are read from file %s\n", in_data_file);
data = open_entries(in_data_file);
if (data == NULL)
{
fprintf(stderr, "cant open data file '%s'\n", in_data_file);
error = 1;
goto end;
}
ifverbose(2)
fprintf(stderr, "Codebook entries are read from file %s\n", in_code_file);
codes = open_entries(in_code_file);
if (codes == NULL)
{
fprintf(stderr, "Can't open code file '%s'\n", in_code_file);
error = 1;
goto end;
}
if (codes->topol < TOPOL_HEXA) {
fprintf(stderr, "File %s is not a map file\n", in_code_file);
error = 1;
goto end;
}
if (data->dimension != codes->dimension) {
fprintf(stderr, "Data and codebook vectors have different dimensions");
error = 1;
goto end;
}
set_teach_params(¶ms, codes, data, buffer, funcname);
set_som_params(¶ms);
params.snapshot = snap;
init_random(randomize);
/* take teaching vectors in random order */
if (rand_s)
data->flags.random_order = 1;
if (alpha_s)
{
type_tmp = get_type_by_str(alpha_list, alpha_s);
if (type_tmp->data == NULL)
{
fprintf(stderr, "Unknown alpha type %s\n", alpha_s);
error = 1;
goto end;
}
}
else
type_tmp = get_type_by_id(alpha_list, ALPHA_LINEAR);
params.alpha_type = type_tmp->id;
params.alpha_func = (ALPHA_FUNC *)type_tmp->data;
codes = som_training(¶ms);
ifverbose(2)
fprintf(stderr, "Codebook entries are saved to file %s\n", out_code_file);
save_entries(codes, out_code_file);
end:
if (data)
close_entries(data);
if (codes)
close_entries(codes);
if (snap)
free_snapshot(snap);
return(error);
}