-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplanes.c
More file actions
515 lines (435 loc) · 13.6 KB
/
planes.c
File metadata and controls
515 lines (435 loc) · 13.6 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/************************************************************************
* *
* Program package 'som_pak': *
* *
* planes.c *
* -convert the map planes into postscript format for printing *
* *
* 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 <string.h>
#include <math.h>
#include "lvq_pak.h"
#include "datafile.h"
#include "labels.h"
#include "som_rout.h"
#define XMSTEP 40
#define YMSTEP ((int) ((int) XSTEP * 0.87))
struct traj_point {
long index;
struct traj_point *next;
};
struct teach_params params;
int plane;
char *mapname, *base_name;
int traj_ready = 0;
int *dens;
struct traj_point traj_root;
int ps = 0;
int XSTEP, YSTEP;
static char *usage[] = {
"planes - produce EPS images of self-organizing maps\n",
"Required parameters:\n",
" -cin filename codebook file\n",
"Optional parameters:\n",
" -din filename input data\n",
" -plane integer map reference vector component to be used (0=all)\n",
" -ps integer produce PS-code (instead of EPS)\n",
" -buffer integer buffered reading of data, integer lines at a time\n",
" -selfuncs name select a set of functions\n",
NULL};
/* ps_string_filter: escape ps special characters in string. Returns a
pointer to a static buffer containing the escaped string. */
char *ps_string_filter(char *text)
{
static char ps_string[1024]; /* This should be enough for most purposes */
char *s = ps_string, c;
if (text)
while ((c = *text++))
{
switch (c)
{
case '(':
case ')':
case '\\':
*s++ = '\\';
break;
default:
break;
}
*s++ = c;
}
*s++ = 0;
return ps_string;
}
void print_plane(int plane)
{
float cv;
int xp, yp;
long index, offset = 0;
int label;
float maxval, minval;
struct teach_params *teach = ¶ms;
struct entries *codes = teach->codes;
struct data_entry *codetmp;
FILE *fp;
char str[100];
int xsize, ysize;
eptr p;
if (ps)
sprintf(str, "%s_p%d.ps", base_name, plane+1);
else
sprintf(str, "%s_p%d.eps", base_name, plane+1);
fp = fopen(str, "w");
if (codes->topol == topol_type("hexa"))
offset = XSTEP/2;
xsize = XSTEP * codes->xdim + offset;
ysize = YSTEP * codes->ydim;
if (ps) {
/* print ps header */
fprintf(fp, "%%!PS-Adobe-2.0 EPSF-2.0\n");
fprintf(fp, "%%%%Title: %s\n%%%%Creator: planes\n", "undefined");
fprintf(fp, "%%%%Pages: 1\n%%%%EndComments\n");
fprintf(fp, "550 40 translate\n");
fprintf(fp, "90 rotate\n");
fprintf(fp, "760 %d div 510 %d div lt\n", xsize, ysize);
fprintf(fp, " {760 %d 0 sub div} {510 %d div} ifelse\n", xsize, ysize);
fprintf(fp, "/gscale exch def\n");
fprintf(fp, "gscale dup scale\n");
}
else {
/* print eps header */
fprintf(fp, "%%!PS-Adobe-2.0 EPSF-2.0\n");
fprintf(fp, "%%%%Title: %s\n%%%%Creator: planes\n", "undefined");
fprintf(fp, "%%%%BoundingBox: 0 0 %d %d\n", xsize, ysize);
fprintf(fp, "%%%%Pages: 0\n%%%%EndComments\n");
}
/* print header */
fprintf(fp, "/fontsize %d def\n", (int) (XSTEP/3));
fprintf(fp, "0 %d translate\n", ysize);
fprintf(fp, "1 -1 scale\n");
/* Find the lower and upper limits for gray level scaling */
minval = FLT_MAX;
maxval = -FLT_MAX;
codetmp = rewind_entries(codes, &p);
while (codetmp != NULL) {
if (maxval < codetmp->points[plane])
maxval = codetmp->points[plane];
if (minval > codetmp->points[plane])
minval = codetmp->points[plane];
codetmp = next_entry(&p);
}
/* initialize gray circles */
fprintf(fp, "/radius %d def\n", (int) (XSTEP/2.2));
fprintf(fp, "/LN\n");
fprintf(fp, "{ setgray\n");
fprintf(fp, "newpath\n");
fprintf(fp, "radius 0 360 arc fill\n");
fprintf(fp, "} def\n");
/* Print the gray level values */
index = 0;
codetmp = rewind_entries(codes, &p);
while (codetmp != NULL) {
/* Compute the gray level of next unit */
if ((maxval - minval) != 0.0)
cv = 0.05 + 0.9 * (codetmp->points[plane] - minval) /
(maxval - minval);
else
cv = 0.5;
xp = XSTEP * (index % codes->xdim) + XSTEP/2;
yp = YSTEP * (index / codes->xdim) + YSTEP/2;
if ((index / codes->xdim) % 2) xp += offset;
/* Print the value */
fprintf(fp, "%d %d %f LN\n", xp, yp, cv);
codetmp = next_entry(&p);
index++;
}
/* initialize label printing */
fprintf(fp, "0 setgray\n");
fprintf(fp, "/Helvetica findfont fontsize scalefont setfont\n");
fprintf(fp, "/LP\n");
fprintf(fp, "{ \n");
fprintf(fp, "1 -1 scale dup stringwidth pop\n");
fprintf(fp, "-2 div 0 rmoveto show\n");
fprintf(fp, "1 -1 scale } def\n");
/* Print the labels */
index = 0;
codetmp = rewind_entries(codes, &p);
while (codetmp != NULL) {
/* If there is label, then print it */
xp = XSTEP * (index % codes->xdim) + XSTEP/2;
yp = YSTEP * (index / codes->xdim) + YSTEP/2;
if ((index / codes->xdim) % 2) xp += offset;
if ((label = get_entry_label(codetmp)) != LABEL_EMPTY)
fprintf(fp, "%d %d moveto (%s) LP\n", xp, yp, ps_string_filter(find_conv_to_lab(label)));
codetmp = next_entry(&p);
index++;
}
if (ps) {
fprintf(fp, "showpage\n");
}
fclose(fp);
}
void scan_data_traj(struct entries *codes, struct entries *data)
{
struct teach_params *teach = ¶ms;
WINNER_FUNCTION *find_winner = teach->winner;
int i;
struct data_entry *datatmp;
struct traj_point *traj_po;
struct winner_info winner;
eptr p;
dens = (int *) malloc(sizeof(int) * codes->xdim * codes->ydim);
for (i = 0; i < codes->xdim * codes->ydim; i++)
dens[i] = 0;
traj_root.next = NULL;
traj_root.index = 0;
traj_po = &traj_root;
datatmp = rewind_entries(data, &p);
/* Scan all input entries */
while (datatmp != NULL) {
/* check if the vector is empty */
if (find_winner(codes, datatmp, &winner, 1) == 0) {
traj_po->next =
(struct traj_point *) malloc(sizeof(struct traj_point) * 1);
traj_po = traj_po->next;
traj_po->next = NULL;
/* save the winner index */
traj_po->index = -1;
}
else {
traj_po->next =
(struct traj_point *) malloc(sizeof(struct traj_point) * 1);
traj_po = traj_po->next;
traj_po->next = NULL;
/* save the winner index */
traj_po->index = winner.index;
/* Increment the winner */
dens[winner.index]++;
}
/* Take the next input entry */
datatmp = next_entry(&p);;
}
traj_ready = 1;
}
void print_trajectory()
{
int i, j;
int offset = 0;
struct teach_params *teach = ¶ms;
struct entries *data = teach->data;
struct entries *codes = teach->codes;
FILE *fp;
char str[100];
int xsize, ysize;
if (ps)
sprintf(str, "%s_tr.ps", base_name);
else
sprintf(str, "%s_tr.eps", base_name);
fp = fopen(str, "w");
if (codes->topol == TOPOL_HEXA)
offset = XSTEP/2;
xsize = XSTEP * codes->xdim + offset;
ysize = YSTEP * codes->ydim;
if (ps) {
/* print ps header */
fprintf(fp, "%%!PS-Adobe-2.0 EPSF-2.0\n");
fprintf(fp, "%%%%Title: %s\n%%%%Creator: planes\n", "undefined");
fprintf(fp, "%%%%Pages: 1\n%%%%EndComments\n");
fprintf(fp, "550 40 translate\n");
fprintf(fp, "90 rotate\n");
fprintf(fp, "760 %d div 510 %d div lt\n", xsize, ysize);
fprintf(fp, " {760 %d 0 sub div} {510 %d div} ifelse\n", xsize, ysize);
fprintf(fp, "/gscale exch def\n");
fprintf(fp, "gscale dup scale\n");
}
else {
/* print eps header */
fprintf(fp, "%%!PS-Adobe-2.0 EPSF-2.0\n");
fprintf(fp, "%%%%Title: %s\n%%%%Creator: planes\n", "undefined");
fprintf(fp, "%%%%BoundingBox: 0 0 %d %d\n", xsize, ysize);
fprintf(fp, "%%%%Pages: 0\n%%%%EndComments\n");
}
/* print header */
fprintf(fp, "0 %d translate\n", ysize);
fprintf(fp, "1 -1 scale\n");
/* draw empty circles */
fprintf(fp, "1 setlinewidth\n");
fprintf(fp, "0.8 setgray\n");
fprintf(fp, "/radius %d def\n", (int) (XSTEP/2.2));
fprintf(fp, "/LN\n");
fprintf(fp, "{ newpath\n");
fprintf(fp, "radius 0 360 arc\n");
fprintf(fp, "stroke } def\n");
for (i = 0; i < codes->xdim; i++)
for (j = 0; j < codes->ydim; j++) {
fprintf(fp, "%d %d LN\n",
((int) i * XSTEP + XSTEP/2 + ((j % 2) ? offset : 0)),
((int) j * (int) YSTEP + YSTEP/2));
}
/* Set linetype for trajectory */
fprintf(fp, "%d setlinewidth\n", XSTEP/10);
fprintf(fp, "1 setlinejoin\n");
fprintf(fp, "1 setlinecap\n");
fprintf(fp, "0 setgray\n");
/* If data values are present, print trajectory */
if (data != NULL) {
int cul[2];
int bpos;
int first = 1;
struct traj_point *traj_po;
if (traj_ready == 0)
scan_data_traj(codes,data);
traj_po = traj_root.next;
/* Scan all input entries */
while (traj_po != NULL) {
bpos = traj_po->index;
if (bpos == -1) {
if (!first) {
fprintf(fp, "stroke\n");
}
first = 1;
/* Take the next input entry */
traj_po = traj_po->next;
/* Skip until next real data point */
continue;
}
/* Draw the trajectory */
if (first) {
first = 0;
cul[0] = XSTEP * (bpos % codes->xdim) + XSTEP/2;
cul[1] = YSTEP * (bpos / codes->xdim) + YSTEP/2;
if (((bpos / codes->xdim) % 2)) cul[0] += offset;
/* print the initialization */
fprintf(fp, "newpath\n");
fprintf(fp, "%d %d moveto\n", cul[0], cul[1]);
}
else {
cul[0] = XSTEP * (bpos % codes->xdim) + XSTEP/2;
cul[1] = YSTEP * (bpos / codes->xdim) + YSTEP/2;
if (((bpos / codes->xdim) % 2)) cul[0] += offset;
/* Print one segment of trajectory */
fprintf(fp, "%d %d lineto\n", cul[0], cul[1]);
}
/* Take the next input entry */
traj_po = traj_po->next;
}
fprintf(fp, "stroke\n");
}
if (ps) {
fprintf(fp, "showpage\n");
}
fclose(fp);
}
void print_all_planes()
{
int i;
struct teach_params *teach = ¶ms;
struct entries *codes = teach->codes;
for (i = 0; i < codes->dimension; i++)
print_plane(i);
}
int main(int argc, char **argv)
{
int error;
long buffer;
char *in_code_file;
char *in_data_file = NULL;
struct entries *codes = NULL;
struct entries *data = NULL;
int offset = 0;
char *funcname = NULL;
error = 0;
global_options(argc, argv);
if (extract_parameter(argc, argv, "-help", OPTION2))
{
printhelp();
exit(0);
}
in_code_file = extract_parameter(argc, argv, IN_CODE_FILE, ALWAYS);
in_data_file = extract_parameter(argc, argv, IN_DATA_FILE, OPTION);
plane = (int) oatoi(extract_parameter(argc, argv, PLANE, OPTION), 1);
buffer = oatoi(extract_parameter(argc, argv, "-buffer", OPTION), 0);
ps = oatoi(extract_parameter(argc, argv, "-ps", OPTION), 0);
funcname = extract_parameter(argc, argv, "-selfuncs", OPTION);
mapname = in_code_file;
{
char *p;
base_name = ostrdup(mapname);
p = strrchr(base_name, '.');
if (p != NULL)
*p = '\0';
}
label_not_needed(1);
ifverbose(2)
fprintf(stdout, "Codebook entries are read from file %s\n", in_code_file);
codes = open_entries(in_code_file);
if (codes == NULL)
{
fprintf(stderr, "cant open code file '%s'\n", in_code_file);
error = 1;
goto cleanup;
}
if (codes->topol < TOPOL_HEXA) {
printf("File %s is not a map file\n", in_code_file);
error = 1;
goto cleanup;
}
if (in_data_file != NULL) {
ifverbose(2)
fprintf(stdout, "Data entries are read from file %s\n", in_data_file);
if ((data = open_entries(in_data_file)) == NULL)
{
fprintf(stderr, "cant open data file '%s'\n", in_data_file);
error = 1;
goto cleanup;
}
if (data->dimension > codes->dimension) {
fprintf(stderr, "Dimensions in data and codebook files are different");
error = 1;
goto cleanup;
}
data->flags.skip_empty = 0;
}
if (plane > codes->dimension) {
fprintf(stderr, "Required plane is bigger than codebook vector dimension");
error = 1;
goto cleanup;
}
set_teach_params(¶ms, codes, data, buffer, funcname);
set_som_params(¶ms);
XSTEP = XMSTEP;
YSTEP = XMSTEP;
if (codes->topol == topol_type("hexa")) {
offset = XSTEP/2;
YSTEP = YMSTEP;
}
/* indexing the tables by plane-1 */
plane--;
if (plane == -1)
print_all_planes();
else
print_plane(plane);
if (data != NULL)
print_trajectory();
cleanup:
if (codes)
close_entries(codes);
if (data)
close_entries(data);
return(error);
}