Debian Codegraph @

lib/sparse/DotIO.c

raw
1 /*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11 #include "config.h"
12
13 #define STANDALONE
14 #include <sparse/general.h>
15 #include <sparse/DotIO.h>
16 #include <sparse/clustering.h>
17 #include <math.h>
18 #include <sparse/mq.h>
19 #include <sparse/color_palette.h>
20 #include <sparse/colorutil.h>
21 #include <stdbool.h>
22 #include <stddef.h>
23 #include <string.h>
24 #include <util/agxbuf.h>
25 #include <util/alloc.h>
26 #include <util/itos.h>
27 #include <util/startswith.h>
28 #include <util/unreachable.h>
29
30 typedef struct {
31 Agrec_t h;
32 unsigned int id;
33 } Agnodeinfo_t;
34
35 #define ND_id(n) (((Agnodeinfo_t*)((n)->base.data))->id)
36
37 static void color_string(agxbuf *buf, int dim, double *color){
38 if (dim > 3 || dim < 1){
39 fprintf(stderr,"can only 1, 2 or 3 dimensional color space. with color value between 0 to 1\n");
40 assert(0);
41 }
42 if (dim == 3){
43 agxbprint(buf, "#%02x%02x%02x", MIN((unsigned int)(color[0] *255), 255),
44 MIN((unsigned int) (color[1]*255), 255), MIN((unsigned int)(color[2]*255), 255));
45 } else if (dim == 1){
46 agxbprint(buf, "#%02x%02x%02x", MIN((unsigned int)(color[0] * 255), 255),
47 MIN((unsigned int) (color[0]*255), 255), MIN((unsigned int)(color[0]*255), 255));
48 } else if (dim == 2){
49 agxbprint(buf, "#%02x%02x%02x", MIN((unsigned int)(color[0] * 255), 255),
50 0, MIN((unsigned int)(color[1]*255), 255));
51 }
52 }
53
54 void attach_edge_colors(Agraph_t* g, int dim, double *colors){
55 /* colors is array of dim*nedges, with color for edge i at colors[dim*i, dim(i+1))
56 */
57 Agsym_t* sym = agattr_text(g, AGEDGE, "color", 0);
58 Agedge_t* e;
59 Agnode_t* n;
60 agxbuf buf = {0};
61 unsigned row, col;
62 int ie = 0;
63
64 if (!sym)
65 sym = agattr_text (g, AGEDGE, "color", "");
66
67 for (n = agfstnode (g); n; n = agnxtnode (g, n)) {
68 row = ND_id(n);
69 for (e = agfstout (g, n); e; e = agnxtout (g, e)) {
70 col = ND_id(aghead(e));
71 if (row == col) continue;
72 color_string(&buf, dim, colors + ie*dim);
73 agxset(e, sym, agxbuse(&buf));
74 ie++;
75 }
76 }
77 agxbfree(&buf);
78 }
79
80 /* SparseMatrix_import_dot:
81 * Assumes g is connected and simple, i.e., we can have a->b and b->a
82 * but not a->b and a->b
83 */
84 SparseMatrix SparseMatrix_import_dot(Agraph_t *g, int dim,
85 double **x, int format) {
86 SparseMatrix A = 0;
87 Agnode_t* n;
88 Agedge_t* e;
89 Agsym_t *sym;
90 Agsym_t *psym;
91 int nnodes;
92 int nedges;
93 int i, row;
94 int* I;
95 int* J;
96 double *val;
97 double v;
98 int type = MATRIX_TYPE_REAL;
99
100 if (!g) return NULL;
101 nnodes = agnnodes (g);
102 nedges = agnedges (g);
103 if (format != FORMAT_CSR && format != FORMAT_COORD) {
104 fprintf (stderr, "Format %d not supported\n", format);
105 graphviz_exit(1);
106 }
107
108 /* Assign node ids */
109 i = 0;
110 for (n = agfstnode (g); n; n = agnxtnode (g, n))
111 ND_id(n) = i++;
112
113 if (format == FORMAT_COORD){
114 A = SparseMatrix_new(i, i, (size_t)nedges, MATRIX_TYPE_REAL, format);
115 A->nz = (size_t)nedges;
116 I = A->ia;
117 J = A->ja;
118 val = A->a;
119 } else {
120 I = gv_calloc(nedges, sizeof(int));
121 J = gv_calloc(nedges, sizeof(int));
122 val = gv_calloc(nedges, sizeof(double));
123 }
124
125 sym = agattr_text(g, AGEDGE, "weight", NULL);
126 i = 0;
127 for (n = agfstnode (g); n; n = agnxtnode (g, n)) {
128 row = ND_id(n);
129 for (e = agfstout (g, n); e; e = agnxtout (g, e)) {
130 I[i] = row;
131 J[i] = ND_id(aghead(e));
132
133 /* edge weight */
134 if (sym) {
135 if (sscanf (agxget(e,sym), "%lf", &v) != 1) v = 1;
136 } else {
137 v = 1;
138 }
139 val[i] = v;
140
141 i++;
142 }
143 }
144
145 if (x && (psym = agattr_text(g, AGNODE, "pos", NULL))) {
146 bool has_positions = true;
147 char* pval;
148 if (!(*x)) {
149 *x = gv_calloc(dim * nnodes, sizeof(double));
150 }
151 for (n = agfstnode (g); n && has_positions; n = agnxtnode (g, n)) {
152 double xx,yy, zz,ww;
153 int nitems;
154 i = ND_id(n);
155 if ((pval = agxget(n, psym)) && *pval) {
156 if (dim == 2){
157 nitems = sscanf(pval, "%lf,%lf", &xx, &yy);
158 if (nitems != 2) {
159 has_positions = false;
160 agerrorf("Node \"%s\" pos has %d < 2 values", agnameof(n), nitems);
161 }
162 (*x)[i*dim] = xx;
163 (*x)[i*dim+1] = yy;
164 } else if (dim == 3){
165 nitems = sscanf(pval, "%lf,%lf,%lf", &xx, &yy, &zz);
166 if (nitems != 3) {
167 has_positions = false;
168 agerrorf("Node \"%s\" pos has %d < 3 values", agnameof(n), nitems);
169 }
170 (*x)[i*dim] = xx;
171 (*x)[i*dim+1] = yy;
172 (*x)[i*dim+2] = zz;
173 } else if (dim == 4){
174 nitems = sscanf(pval, "%lf,%lf,%lf,%lf", &xx, &yy, &zz,&ww);
175 if (nitems != 4) {
176 has_positions = false;
177 agerrorf("Node \"%s\" pos has %d < 4 values", agnameof(n), nitems);
178 }
179 (*x)[i*dim] = xx;
180 (*x)[i*dim+1] = yy;
181 (*x)[i*dim+2] = zz;
182 (*x)[i*dim+3] = ww;
183 } else if (dim == 1){
184 nitems = sscanf(pval, "%lf", &xx);
185 if (nitems != 1){
186 SparseMatrix_delete(A);
187 A = NULL;
188 goto done;
189 }
190 (*x)[i*dim] = xx;
191 } else {
192 assert(0);
193 }
194 } else {
195 has_positions = false;
196 agerrorf("Node \"%s\" lacks position info", agnameof(n));
197 }
198 }
199 if (!has_positions) {
200 free(*x);
201 *x = NULL;
202 }
203 }
204 else if (x)
205 agerrorf("Error: graph %s has missing \"pos\" information", agnameof(g));
206
207 size_t sz = sizeof(double);
208 if (format == FORMAT_CSR) {
209 A = SparseMatrix_from_coordinate_arrays((size_t)nedges, nnodes, nnodes, I,
210 J, val, type, sz);
211 }
212
213 done:
214 if (format != FORMAT_COORD){
215 free(I);
216 free(J);
217 free(val);
218 }
219
220 return A;
221 }
222
223 /* get spline info */
224 int Import_dot_splines(Agraph_t* g, int *ne, char ***xsplines){
225 /* get the list of splines for the edges in the order they appear, and store as a list of strings in xspline.
226 If *xsplines = NULL, it will be allocated. On exit (*xsplines)[i] is the control point string for the i-th edge. This string
227 is of the form "x1,y1 x2,y2...", the two end points of the edge is not included per Dot format
228 Return 1 if success. 0 if not.
229 */
230 Agnode_t* n;
231 Agedge_t* e;
232 Agsym_t *sym;
233 int nedges;
234 unsigned i;
235
236 if (!g){
237 return 0;
238 }
239
240 *ne = nedges = agnedges (g);
241
242 /* Assign node ids */
243 i = 0;
244 for (n = agfstnode (g); n; n = agnxtnode (g, n))
245 ND_id(n) = i++;
246
247 sym = agattr_text(g, AGEDGE, "pos", 0);
248 if (!sym) return 0;
249
250 *xsplines = gv_calloc(nedges, sizeof(char*));
251
252 i = 0;
253 for (n = agfstnode (g); n; n = agnxtnode (g, n)) {
254 for (e = agfstout (g, n); e; e = agnxtout (g, e)) {
255 /* edge weight */
256 char *pos = agxget (e, sym);
257 (*xsplines)[i] = strdup(pos);
258 i++;
259 }
260 }
261 return 1;
262 }
263
264 static int hex2int(char h){
265 if (h >= '0' && h <= '9') return h - '0';
266 if (h >= 'a' && h <= 'f') return 10 + h - 'a';
267 if (h >= 'A' && h <= 'F') return 10 + h - 'A';
268 return 0;
269 }
270
271 static float hexcol2rgb(const char *h) {
272 return (hex2int(h[0])*16 + hex2int(h[1]))/255.;
273 }
274
275 void Dot_SetClusterColor(Agraph_t* g, float *rgb_r, float *rgb_g, float *rgb_b, int *clusters){
276
277 Agnode_t* n;
278 agxbuf scluster = {0};
279 unsigned i;
280 Agsym_t* clust_clr_sym = agattr_text(g, AGNODE, "clustercolor", NULL);
281
282 if (!clust_clr_sym) clust_clr_sym = agattr_text(g, AGNODE, "clustercolor", "-1");
283 for (n = agfstnode (g); n; n = agnxtnode (g, n)) {
284 i = ND_id(n);
285 if (rgb_r && rgb_g && rgb_b) {
286 rgb2hex(rgb_r[clusters[i]], rgb_g[clusters[i]], rgb_b[clusters[i]],
287 &scluster, NULL);
288 }
289 agxset(n, clust_clr_sym, agxbuse(&scluster));
290 }
291 agxbfree(&scluster);
292 }
293
294 SparseMatrix Import_coord_clusters_from_dot(Agraph_t* g, int maxcluster, int dim, int *nn, double **label_sizes, double **x, int **clusters, float **rgb_r, float **rgb_g, float **rgb_b, float **fsz, char ***labels, int default_color_scheme, int clustering_scheme, int useClusters){
295 SparseMatrix A = 0;
296 Agnode_t* n;
297 Agedge_t* e;
298 Agsym_t* sym;
299 Agsym_t* clust_sym;
300 Agsym_t* clust_clr_sym;
301 int nnodes;
302 int nedges;
303 int i, row, ic,nc, j;
304 double v;
305 int type = MATRIX_TYPE_REAL;
306 float ff;
307
308 int MAX_GRPS, MIN_GRPS;
309 bool noclusterinfo = false;
310 bool first = true;
311 const float *pal;
312 int max_color = MAX_COLOR;
313
314 switch (default_color_scheme){
315 case COLOR_SCHEME_BLUE_YELLOW:
316 pal = &(palette_blue_to_yellow[0][0]);
317 break;
318 case COLOR_SCHEME_WHITE_RED:
319 pal = &(palette_white_to_red[0][0]);
320 break;
321 case COLOR_SCHEME_GREY_RED:
322 pal = &(palette_grey_to_red[0][0]);
323 break;
324 case COLOR_SCHEME_GREY:
325 pal = &(palette_grey[0][0]);
326 break;
327 case COLOR_SCHEME_PASTEL:
328 pal = &(palette_pastel[0][0]);
329 break;
330 case COLOR_SCHEME_SEQUENTIAL_SINGLEHUE_RED:
331 pal = &(palette_sequential_singlehue_red[0][0]);
332 break;
333 case COLOR_SCHEME_SEQUENTIAL_SINGLEHUE_RED_LIGHTER:
334 pal = &(palette_sequential_singlehue_red_lighter[0][0]);
335 break;
336 case COLOR_SCHEME_PRIMARY:
337 pal = &(palette_primary[0][0]);
338 break;
339 case COLOR_SCHEME_ADAM_BLEND:
340 pal = &(palette_adam_blend[0][0]);
341 break;
342 case COLOR_SCHEME_ADAM:
343 pal = &(palette_adam[0][0]);
344 max_color = 11;
345 break;
346 case COLOR_SCHEME_NONE:
347 pal = NULL;
348 break;
349 default:
350 pal = &(palette_pastel[0][0]);
351 break;
352 }
353
354 if (!g) return NULL;
355 nnodes = agnnodes (g);
356 nedges = agnedges (g);
357 *nn = nnodes;
358
359 /* Assign node ids */
360 i = 0;
361 for (n = agfstnode (g); n; n = agnxtnode (g, n))
362 ND_id(n) = i++;
363
364 /* form matrix */
365 int* I = gv_calloc(nedges, sizeof(int));
366 int* J = gv_calloc(nedges, sizeof(int));
367 double* val = gv_calloc(nedges, sizeof(double));
368
369 sym = agattr_text(g, AGEDGE, "weight", NULL);
370 clust_sym = agattr_text(g, AGNODE, "cluster", NULL);
371 clust_clr_sym = agattr_text(g, AGNODE, "clustercolor", NULL);
372 i = 0;
373 for (n = agfstnode (g); n; n = agnxtnode (g, n)) {
374 row = ND_id(n);
375 for (e = agfstout (g, n); e; e = agnxtout (g, e)) {
376 I[i] = row;
377 J[i] = ND_id(aghead(e));
378 if (sym) {
379 if (sscanf (agxget(e,sym), "%lf", &v) != 1)
380 v = 1;
381 }
382 else
383 v = 1;
384 val[i] = v;
385 i++;
386 }
387 }
388 A = SparseMatrix_from_coordinate_arrays((size_t)nedges, nnodes, nnodes, I, J,
389 val, type, sizeof(double));
390
391 /* get clustering info */
392 *clusters = gv_calloc(nnodes, sizeof(int));
393 nc = 1;
394 MIN_GRPS = 0;
395 /* if useClusters, the nodes in each top-level cluster subgraph are assigned to
396 * clusters 2, 3, .... Any nodes not in a cluster subgraph are tossed into cluster 1.
397 */
398 if (useClusters) {
399 Agraph_t* sg;
400 int gid = 1;
401 memset (*clusters, 0, sizeof(int)*nnodes);
402 for (sg = agfstsubg(g); sg; sg = agnxtsubg(sg)) {
403 if (!startswith(agnameof(sg), "cluster")) continue;
404 gid++;
405 for (n = agfstnode(sg); n; n = agnxtnode (sg, n)) {
406 i = ND_id(n);
407 if ((*clusters)[i])
408 fprintf (stderr, "Warning: node %s appears in multiple clusters.\n", agnameof(n));
409 else
410 (*clusters)[i] = gid;
411 }
412 }
413 for (n = agfstnode(g); n; n = agnxtnode (g, n)) {
414 i = ND_id(n);
415 if ((*clusters)[i] == 0)
416 (*clusters)[i] = 1;
417 }
418 MIN_GRPS = 1;
419 nc = gid;
420 }
421 else if (clust_sym) {
422 for (n = agfstnode (g); n; n = agnxtnode (g, n)) {
423 i = ND_id(n);
424 if ((sscanf(agxget(n,clust_sym), "%d", &ic)>0)) {
425 (*clusters)[i] = ic;
426 nc = MAX(nc, ic);
427 if (first){
428 MIN_GRPS = ic;
429 first = false;
430 } else {
431 MIN_GRPS = MIN(MIN_GRPS, ic);
432 }
433 } else {
434 noclusterinfo = true;
435 break;
436 }
437 }
438 }
439 else
440 noclusterinfo = true;
441 MAX_GRPS = nc;
442
443 if (noclusterinfo) {
444 double modularity;
445 if (!clust_sym) clust_sym = agattr_text(g,AGNODE,"cluster","-1");
446
447 if (clustering_scheme == CLUSTERING_MQ){
448 mq_clustering(A, maxcluster,
449 &nc, clusters, &modularity);
450 } else if (clustering_scheme == CLUSTERING_MODULARITY){
451 modularity_clustering(A, false, maxcluster,
452 &nc, clusters, &modularity);
453 } else {
454 UNREACHABLE();
455 }
456 for (i = 0; i < nnodes; i++) (*clusters)[i]++;/* make into 1 based */
457 for (n = agfstnode (g); n; n = agnxtnode (g, n)) {
458 i = ND_id(n);
459 agxset(n, clust_sym, ITOS((*clusters)[i]));
460 }
461 MIN_GRPS = 1;
462 MAX_GRPS = nc;
463 if (Verbose){
464 fprintf(stderr," no complement clustering info in dot file, using modularity clustering. Modularity = %f, ncluster=%d\n",modularity, nc);
465 }
466 }
467
468 *label_sizes = gv_calloc(dim * nnodes, sizeof(double));
469 if (pal || (!noclusterinfo && clust_clr_sym)){
470 *rgb_r = gv_calloc(1 + MAX_GRPS, sizeof(float));
471 *rgb_g = gv_calloc(1 + MAX_GRPS, sizeof(float));
472 *rgb_b = gv_calloc(1 + MAX_GRPS, sizeof(float));
473 } else {
474 *rgb_r = NULL;
475 *rgb_g = NULL;
476 *rgb_b = NULL;
477 }
478 *fsz = gv_calloc(nnodes, sizeof(float));
479 *labels = gv_calloc(nnodes, sizeof(char*));
480
481 for (n = agfstnode (g); n; n = agnxtnode (g, n)) {
482 gvcolor_t color;
483 double sz;
484 i = ND_id(n);
485 if (agget(n, "width") && agget(n, "height")){
486 sscanf(agget(n, "width"), "%lf", &sz);
487 (*label_sizes)[i*2] = POINTS(sz*0.5);
488 sscanf(agget(n, "height"), "%lf", &sz);
489 (*label_sizes)[i*2+1] = POINTS(sz*0.5);
490 } else {
491 (*label_sizes)[i*2] = POINTS(0.75/2);
492 (*label_sizes)[i*2+1] = POINTS(0.5*2);
493 }
494
495 if (agget(n, "fontsize")){
496 sscanf(agget(n, "fontsize"), "%f", &ff);
497 (*fsz)[i] = ff;
498 } else {
499 (*fsz)[i] = 14;
500 }
501
502 if (agget(n, "label") && strcmp(agget(n, "label"), "") != 0 && strcmp(agget(n, "label"), "\\N") != 0){
503 char *lbs = agget(n, "label");
504 (*labels)[i] = strdup(lbs);
505 } else {
506 (*labels)[i] = strdup(agnameof(n));
507 }
508
509
510
511 j = (*clusters)[i];
512 if (MAX_GRPS-MIN_GRPS < max_color) {
513 j = (j-MIN_GRPS)*((int)((max_color-1)/MAX((MAX_GRPS-MIN_GRPS),1)));
514 } else {
515 j = (j-MIN_GRPS)%max_color;
516 }
517
518 if (pal){
519 (*rgb_r)[(*clusters)[i]] = pal[3*j+0];
520 (*rgb_g)[(*clusters)[i]] = pal[3*j+1];
521 (*rgb_b)[(*clusters)[i]] = pal[3*j+2];
522 }
523
524 if (!noclusterinfo && clust_clr_sym && (colorxlate(agxget(n,clust_clr_sym),&color,RGBA_DOUBLE) == COLOR_OK)) {
525 (*rgb_r)[(*clusters)[i]] = color.u.RGBA[0];
526 (*rgb_g)[(*clusters)[i]] = color.u.RGBA[1];
527 (*rgb_b)[(*clusters)[i]] = color.u.RGBA[2];
528 }
529
530 const char *cc = agget(n, "clustercolor");
531 if (!noclusterinfo && agget(n, "cluster") && cc && strlen(cc) >= 7 && pal) {
532 (*rgb_r)[(*clusters)[i]] = hexcol2rgb(cc+1);
533 (*rgb_g)[(*clusters)[i]] = hexcol2rgb(cc+3);
534 (*rgb_b)[(*clusters)[i]] = hexcol2rgb(cc+5);
535 }
536
537 }
538
539
540 if (x){
541 bool has_position = false;
542 *x = gv_calloc(dim * nnodes, sizeof(double));
543 for (n = agfstnode (g); n; n = agnxtnode (g, n)) {
544 double xx,yy;
545 i = ND_id(n);
546 if (agget(n, "pos")){
547 has_position = true;
548 sscanf(agget(n, "pos"), "%lf,%lf", &xx, &yy);
549 (*x)[i*dim] = xx;
550 (*x)[i*dim+1] = yy;
551 } else {
552 fprintf(stderr,"WARNING: pos field missing for node %d, set to origin\n",i);
553 (*x)[i*dim] = 0;
554 (*x)[i*dim+1] = 0;
555 }
556 }
557 if (!has_position){
558 free(*x);
559 *x = NULL;
560 }
561 }
562
563
564 free(I);
565 free(J);
566 free(val);
567
568 return A;
569 }
570
571 void attached_clustering(Agraph_t* g, int maxcluster, int clustering_scheme){
572 SparseMatrix A = 0;
573 Agnode_t* n;
574 Agedge_t* e;
575 Agsym_t *sym, *clust_sym;
576 int nnodes;
577 int nedges;
578 int i, row,nc;
579 double v;
580 int type = MATRIX_TYPE_REAL;
581 size_t sz = sizeof(double);
582
583 if (!g) return;
584 nnodes = agnnodes (g);
585 nedges = agnedges (g);
586
587 /* Assign node ids */
588 i = 0;
589 for (n = agfstnode (g); n; n = agnxtnode (g, n))
590 ND_id(n) = i++;
591
592 /* form matrix */
593 int* I = gv_calloc(nedges, sizeof(int));
594 int* J = gv_calloc(nedges, sizeof(int));
595 double* val = gv_calloc(nedges, sizeof(double));
596
597 sym = agattr_text(g, AGEDGE, "weight", NULL);
598 clust_sym = agattr_text(g, AGNODE, "cluster", NULL);
599
600 i = 0;
601 for (n = agfstnode (g); n; n = agnxtnode (g, n)) {
602 row = ND_id(n);
603 for (e = agfstout (g, n); e; e = agnxtout (g, e)) {
604 I[i] = row;
605 J[i] = ND_id(aghead(e));
606 if (sym) {
607 if (sscanf (agxget(e,sym), "%lf", &v) != 1)
608 v = 1;
609 }
610 else
611 v = 1;
612 val[i] = v;
613 i++;
614 }
615 }
616 A = SparseMatrix_from_coordinate_arrays((size_t)nedges, nnodes, nnodes, I, J,
617 val, type, sz);
618
619 int *clusters = gv_calloc(nnodes, sizeof(int));
620
621 {
622 double modularity;
623 if (!clust_sym) clust_sym = agattr_text(g,AGNODE,"cluster","-1");
624
625 if (clustering_scheme == CLUSTERING_MQ){
626 mq_clustering(A, maxcluster,
627 &nc, &clusters, &modularity);
628 } else if (clustering_scheme == CLUSTERING_MODULARITY){
629 modularity_clustering(A, false, maxcluster,
630 &nc, &clusters, &modularity);
631 } else {
632 UNREACHABLE();
633 }
634 for (i = 0; i < nnodes; i++) (clusters)[i]++;/* make into 1 based */
635 for (n = agfstnode (g); n; n = agnxtnode (g, n)) {
636 i = ND_id(n);
637 agxset(n, clust_sym, ITOS(clusters[i]));
638 }
639 if (Verbose){
640 fprintf(stderr," no complement clustering info in dot file, using modularity clustering. Modularity = %f, ncluster=%d\n",modularity, nc);
641 }
642 }
643
644 free(I);
645 free(J);
646 free(val);
647 free(clusters);
648
649 SparseMatrix_delete(A);
650
651 }
652
653
654
655 void initDotIO (Agraph_t *g)
656 {
657 aginit(g, AGNODE, "info", sizeof(Agnodeinfo_t), true);
658 }
659
660 void setDotNodeID (Agnode_t* n, int v)
661 {
662 ND_id(n) = v;
663 }
664
665 int getDotNodeID (Agnode_t* n)
666 {
667 return ND_id(n);
668 }
669