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 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
|
/*************************************************************************
* Copyright (c) 2011 AT&T Intellectual Property
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors: Details at https://graphviz.org
*************************************************************************/
#include "config.h"
#define STANDALONE
#include <sparse/general.h>
#include <sparse/DotIO.h>
#include <sparse/clustering.h>
#include <math.h>
#include <sparse/mq.h>
#include <sparse/color_palette.h>
#include <sparse/colorutil.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include <util/agxbuf.h>
#include <util/alloc.h>
#include <util/itos.h>
#include <util/startswith.h>
#include <util/unreachable.h>
typedef struct {
Agrec_t h;
unsigned int id;
} Agnodeinfo_t;
#define ND_id(n) (((Agnodeinfo_t*)((n)->base.data))->id)
static void color_string(agxbuf *buf, int dim, double *color){
if (dim > 3 || dim < 1){
fprintf(stderr,"can only 1, 2 or 3 dimensional color space. with color value between 0 to 1\n");
assert(0);
}
if (dim == 3){
agxbprint(buf, "#%02x%02x%02x", MIN((unsigned int)(color[0] *255), 255),
MIN((unsigned int) (color[1]*255), 255), MIN((unsigned int)(color[2]*255), 255));
} else if (dim == 1){
agxbprint(buf, "#%02x%02x%02x", MIN((unsigned int)(color[0] * 255), 255),
MIN((unsigned int) (color[0]*255), 255), MIN((unsigned int)(color[0]*255), 255));
} else if (dim == 2){
agxbprint(buf, "#%02x%02x%02x", MIN((unsigned int)(color[0] * 255), 255),
0, MIN((unsigned int)(color[1]*255), 255));
}
}
void attach_edge_colors(Agraph_t* g, int dim, double *colors){
/* colors is array of dim*nedges, with color for edge i at colors[dim*i, dim(i+1))
*/
Agsym_t* sym = agattr_text(g, AGEDGE, "color", 0);
Agedge_t* e;
Agnode_t* n;
agxbuf buf = {0};
unsigned row, col;
int ie = 0;
if (!sym)
sym = agattr_text (g, AGEDGE, "color", "");
for (n = agfstnode (g); n; n = agnxtnode (g, n)) {
row = ND_id(n);
for (e = agfstout (g, n); e; e = agnxtout (g, e)) {
col = ND_id(aghead(e));
if (row == col) continue;
color_string(&buf, dim, colors + ie*dim);
agxset(e, sym, agxbuse(&buf));
ie++;
}
}
agxbfree(&buf);
}
/* SparseMatrix_import_dot:
* Assumes g is connected and simple, i.e., we can have a->b and b->a
* but not a->b and a->b
*/
SparseMatrix SparseMatrix_import_dot(Agraph_t *g, int dim,
double **x, int format) {
SparseMatrix A = 0;
Agnode_t* n;
Agedge_t* e;
Agsym_t *sym;
Agsym_t *psym;
int nnodes;
int nedges;
int i, row;
int* I;
int* J;
double *val;
double v;
int type = MATRIX_TYPE_REAL;
if (!g) return NULL;
nnodes = agnnodes (g);
nedges = agnedges (g);
if (format != FORMAT_CSR && format != FORMAT_COORD) {
fprintf (stderr, "Format %d not supported\n", format);
graphviz_exit(1);
}
/* Assign node ids */
i = 0;
for (n = agfstnode (g); n; n = agnxtnode (g, n))
ND_id(n) = i++;
if (format == FORMAT_COORD){
A = SparseMatrix_new(i, i, (size_t)nedges, MATRIX_TYPE_REAL, format);
A->nz = (size_t)nedges;
I = A->ia;
J = A->ja;
val = A->a;
} else {
I = gv_calloc(nedges, sizeof(int));
J = gv_calloc(nedges, sizeof(int));
val = gv_calloc(nedges, sizeof(double));
}
sym = agattr_text(g, AGEDGE, "weight", NULL);
i = 0;
for (n = agfstnode (g); n; n = agnxtnode (g, n)) {
row = ND_id(n);
for (e = agfstout (g, n); e; e = agnxtout (g, e)) {
I[i] = row;
J[i] = ND_id(aghead(e));
/* edge weight */
if (sym) {
if (sscanf (agxget(e,sym), "%lf", &v) != 1) v = 1;
} else {
v = 1;
}
val[i] = v;
i++;
}
}
if (x && (psym = agattr_text(g, AGNODE, "pos", NULL))) {
bool has_positions = true;
char* pval;
if (!(*x)) {
*x = gv_calloc(dim * nnodes, sizeof(double));
}
for (n = agfstnode (g); n && has_positions; n = agnxtnode (g, n)) {
double xx,yy, zz,ww;
int nitems;
i = ND_id(n);
if ((pval = agxget(n, psym)) && *pval) {
if (dim == 2){
nitems = sscanf(pval, "%lf,%lf", &xx, &yy);
if (nitems != 2) {
has_positions = false;
agerrorf("Node \"%s\" pos has %d < 2 values", agnameof(n), nitems);
}
(*x)[i*dim] = xx;
(*x)[i*dim+1] = yy;
} else if (dim == 3){
nitems = sscanf(pval, "%lf,%lf,%lf", &xx, &yy, &zz);
if (nitems != 3) {
has_positions = false;
agerrorf("Node \"%s\" pos has %d < 3 values", agnameof(n), nitems);
}
(*x)[i*dim] = xx;
(*x)[i*dim+1] = yy;
(*x)[i*dim+2] = zz;
} else if (dim == 4){
nitems = sscanf(pval, "%lf,%lf,%lf,%lf", &xx, &yy, &zz,&ww);
if (nitems != 4) {
has_positions = false;
agerrorf("Node \"%s\" pos has %d < 4 values", agnameof(n), nitems);
}
(*x)[i*dim] = xx;
(*x)[i*dim+1] = yy;
(*x)[i*dim+2] = zz;
(*x)[i*dim+3] = ww;
} else if (dim == 1){
nitems = sscanf(pval, "%lf", &xx);
if (nitems != 1){
SparseMatrix_delete(A);
A = NULL;
goto done;
}
(*x)[i*dim] = xx;
} else {
assert(0);
}
} else {
has_positions = false;
agerrorf("Node \"%s\" lacks position info", agnameof(n));
}
}
if (!has_positions) {
free(*x);
*x = NULL;
}
}
else if (x)
agerrorf("Error: graph %s has missing \"pos\" information", agnameof(g));
size_t sz = sizeof(double);
if (format == FORMAT_CSR) {
A = SparseMatrix_from_coordinate_arrays((size_t)nedges, nnodes, nnodes, I,
J, val, type, sz);
}
done:
if (format != FORMAT_COORD){
free(I);
free(J);
free(val);
}
return A;
}
/* get spline info */
int Import_dot_splines(Agraph_t* g, int *ne, char ***xsplines){
/* get the list of splines for the edges in the order they appear, and store as a list of strings in xspline.
If *xsplines = NULL, it will be allocated. On exit (*xsplines)[i] is the control point string for the i-th edge. This string
is of the form "x1,y1 x2,y2...", the two end points of the edge is not included per Dot format
Return 1 if success. 0 if not.
*/
Agnode_t* n;
Agedge_t* e;
Agsym_t *sym;
int nedges;
unsigned i;
if (!g){
return 0;
}
*ne = nedges = agnedges (g);
/* Assign node ids */
i = 0;
for (n = agfstnode (g); n; n = agnxtnode (g, n))
ND_id(n) = i++;
sym = agattr_text(g, AGEDGE, "pos", 0);
if (!sym) return 0;
*xsplines = gv_calloc(nedges, sizeof(char*));
i = 0;
for (n = agfstnode (g); n; n = agnxtnode (g, n)) {
for (e = agfstout (g, n); e; e = agnxtout (g, e)) {
/* edge weight */
char *pos = agxget (e, sym);
(*xsplines)[i] = strdup(pos);
i++;
}
}
return 1;
}
static int hex2int(char h){
if (h >= '0' && h <= '9') return h - '0';
if (h >= 'a' && h <= 'f') return 10 + h - 'a';
if (h >= 'A' && h <= 'F') return 10 + h - 'A';
return 0;
}
static float hexcol2rgb(const char *h) {
return (hex2int(h[0])*16 + hex2int(h[1]))/255.;
}
void Dot_SetClusterColor(Agraph_t* g, float *rgb_r, float *rgb_g, float *rgb_b, int *clusters){
Agnode_t* n;
agxbuf scluster = {0};
unsigned i;
Agsym_t* clust_clr_sym = agattr_text(g, AGNODE, "clustercolor", NULL);
if (!clust_clr_sym) clust_clr_sym = agattr_text(g, AGNODE, "clustercolor", "-1");
for (n = agfstnode (g); n; n = agnxtnode (g, n)) {
i = ND_id(n);
if (rgb_r && rgb_g && rgb_b) {
rgb2hex(rgb_r[clusters[i]], rgb_g[clusters[i]], rgb_b[clusters[i]],
&scluster, NULL);
}
agxset(n, clust_clr_sym, agxbuse(&scluster));
}
agxbfree(&scluster);
}
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){
SparseMatrix A = 0;
Agnode_t* n;
Agedge_t* e;
Agsym_t* sym;
Agsym_t* clust_sym;
Agsym_t* clust_clr_sym;
int nnodes;
int nedges;
int i, row, ic,nc, j;
double v;
int type = MATRIX_TYPE_REAL;
float ff;
int MAX_GRPS, MIN_GRPS;
bool noclusterinfo = false;
bool first = true;
const float *pal;
int max_color = MAX_COLOR;
switch (default_color_scheme){
case COLOR_SCHEME_BLUE_YELLOW:
pal = &(palette_blue_to_yellow[0][0]);
break;
case COLOR_SCHEME_WHITE_RED:
pal = &(palette_white_to_red[0][0]);
break;
case COLOR_SCHEME_GREY_RED:
pal = &(palette_grey_to_red[0][0]);
break;
case COLOR_SCHEME_GREY:
pal = &(palette_grey[0][0]);
break;
case COLOR_SCHEME_PASTEL:
pal = &(palette_pastel[0][0]);
break;
case COLOR_SCHEME_SEQUENTIAL_SINGLEHUE_RED:
pal = &(palette_sequential_singlehue_red[0][0]);
break;
case COLOR_SCHEME_SEQUENTIAL_SINGLEHUE_RED_LIGHTER:
pal = &(palette_sequential_singlehue_red_lighter[0][0]);
break;
case COLOR_SCHEME_PRIMARY:
pal = &(palette_primary[0][0]);
break;
case COLOR_SCHEME_ADAM_BLEND:
pal = &(palette_adam_blend[0][0]);
break;
case COLOR_SCHEME_ADAM:
pal = &(palette_adam[0][0]);
max_color = 11;
break;
case COLOR_SCHEME_NONE:
pal = NULL;
break;
default:
pal = &(palette_pastel[0][0]);
break;
}
if (!g) return NULL;
nnodes = agnnodes (g);
nedges = agnedges (g);
*nn = nnodes;
/* Assign node ids */
i = 0;
for (n = agfstnode (g); n; n = agnxtnode (g, n))
ND_id(n) = i++;
/* form matrix */
int* I = gv_calloc(nedges, sizeof(int));
int* J = gv_calloc(nedges, sizeof(int));
double* val = gv_calloc(nedges, sizeof(double));
sym = agattr_text(g, AGEDGE, "weight", NULL);
clust_sym = agattr_text(g, AGNODE, "cluster", NULL);
clust_clr_sym = agattr_text(g, AGNODE, "clustercolor", NULL);
i = 0;
for (n = agfstnode (g); n; n = agnxtnode (g, n)) {
row = ND_id(n);
for (e = agfstout (g, n); e; e = agnxtout (g, e)) {
I[i] = row;
J[i] = ND_id(aghead(e));
if (sym) {
if (sscanf (agxget(e,sym), "%lf", &v) != 1)
v = 1;
}
else
v = 1;
val[i] = v;
i++;
}
}
A = SparseMatrix_from_coordinate_arrays((size_t)nedges, nnodes, nnodes, I, J,
val, type, sizeof(double));
/* get clustering info */
*clusters = gv_calloc(nnodes, sizeof(int));
nc = 1;
MIN_GRPS = 0;
/* if useClusters, the nodes in each top-level cluster subgraph are assigned to
* clusters 2, 3, .... Any nodes not in a cluster subgraph are tossed into cluster 1.
*/
if (useClusters) {
Agraph_t* sg;
int gid = 1;
memset (*clusters, 0, sizeof(int)*nnodes);
for (sg = agfstsubg(g); sg; sg = agnxtsubg(sg)) {
if (!startswith(agnameof(sg), "cluster")) continue;
gid++;
for (n = agfstnode(sg); n; n = agnxtnode (sg, n)) {
i = ND_id(n);
if ((*clusters)[i])
fprintf (stderr, "Warning: node %s appears in multiple clusters.\n", agnameof(n));
else
(*clusters)[i] = gid;
}
}
for (n = agfstnode(g); n; n = agnxtnode (g, n)) {
i = ND_id(n);
if ((*clusters)[i] == 0)
(*clusters)[i] = 1;
}
MIN_GRPS = 1;
nc = gid;
}
else if (clust_sym) {
for (n = agfstnode (g); n; n = agnxtnode (g, n)) {
i = ND_id(n);
if ((sscanf(agxget(n,clust_sym), "%d", &ic)>0)) {
(*clusters)[i] = ic;
nc = MAX(nc, ic);
if (first){
MIN_GRPS = ic;
first = false;
} else {
MIN_GRPS = MIN(MIN_GRPS, ic);
}
} else {
noclusterinfo = true;
break;
}
}
}
else
noclusterinfo = true;
MAX_GRPS = nc;
if (noclusterinfo) {
double modularity;
if (!clust_sym) clust_sym = agattr_text(g,AGNODE,"cluster","-1");
if (clustering_scheme == CLUSTERING_MQ){
mq_clustering(A, maxcluster,
&nc, clusters, &modularity);
} else if (clustering_scheme == CLUSTERING_MODULARITY){
modularity_clustering(A, false, maxcluster,
&nc, clusters, &modularity);
} else {
UNREACHABLE();
}
for (i = 0; i < nnodes; i++) (*clusters)[i]++;/* make into 1 based */
for (n = agfstnode (g); n; n = agnxtnode (g, n)) {
i = ND_id(n);
agxset(n, clust_sym, ITOS((*clusters)[i]));
}
MIN_GRPS = 1;
MAX_GRPS = nc;
if (Verbose){
fprintf(stderr," no complement clustering info in dot file, using modularity clustering. Modularity = %f, ncluster=%d\n",modularity, nc);
}
}
*label_sizes = gv_calloc(dim * nnodes, sizeof(double));
if (pal || (!noclusterinfo && clust_clr_sym)){
*rgb_r = gv_calloc(1 + MAX_GRPS, sizeof(float));
*rgb_g = gv_calloc(1 + MAX_GRPS, sizeof(float));
*rgb_b = gv_calloc(1 + MAX_GRPS, sizeof(float));
} else {
*rgb_r = NULL;
*rgb_g = NULL;
*rgb_b = NULL;
}
*fsz = gv_calloc(nnodes, sizeof(float));
*labels = gv_calloc(nnodes, sizeof(char*));
for (n = agfstnode (g); n; n = agnxtnode (g, n)) {
gvcolor_t color;
double sz;
i = ND_id(n);
if (agget(n, "width") && agget(n, "height")){
sscanf(agget(n, "width"), "%lf", &sz);
(*label_sizes)[i*2] = POINTS(sz*0.5);
sscanf(agget(n, "height"), "%lf", &sz);
(*label_sizes)[i*2+1] = POINTS(sz*0.5);
} else {
(*label_sizes)[i*2] = POINTS(0.75/2);
(*label_sizes)[i*2+1] = POINTS(0.5*2);
}
if (agget(n, "fontsize")){
sscanf(agget(n, "fontsize"), "%f", &ff);
(*fsz)[i] = ff;
} else {
(*fsz)[i] = 14;
}
if (agget(n, "label") && strcmp(agget(n, "label"), "") != 0 && strcmp(agget(n, "label"), "\\N") != 0){
char *lbs = agget(n, "label");
(*labels)[i] = strdup(lbs);
} else {
(*labels)[i] = strdup(agnameof(n));
}
j = (*clusters)[i];
if (MAX_GRPS-MIN_GRPS < max_color) {
j = (j-MIN_GRPS)*((int)((max_color-1)/MAX((MAX_GRPS-MIN_GRPS),1)));
} else {
j = (j-MIN_GRPS)%max_color;
}
if (pal){
(*rgb_r)[(*clusters)[i]] = pal[3*j+0];
(*rgb_g)[(*clusters)[i]] = pal[3*j+1];
(*rgb_b)[(*clusters)[i]] = pal[3*j+2];
}
if (!noclusterinfo && clust_clr_sym && (colorxlate(agxget(n,clust_clr_sym),&color,RGBA_DOUBLE) == COLOR_OK)) {
(*rgb_r)[(*clusters)[i]] = color.u.RGBA[0];
(*rgb_g)[(*clusters)[i]] = color.u.RGBA[1];
(*rgb_b)[(*clusters)[i]] = color.u.RGBA[2];
}
const char *cc = agget(n, "clustercolor");
if (!noclusterinfo && agget(n, "cluster") && cc && strlen(cc) >= 7 && pal) {
(*rgb_r)[(*clusters)[i]] = hexcol2rgb(cc+1);
(*rgb_g)[(*clusters)[i]] = hexcol2rgb(cc+3);
(*rgb_b)[(*clusters)[i]] = hexcol2rgb(cc+5);
}
}
if (x){
bool has_position = false;
*x = gv_calloc(dim * nnodes, sizeof(double));
for (n = agfstnode (g); n; n = agnxtnode (g, n)) {
double xx,yy;
i = ND_id(n);
if (agget(n, "pos")){
has_position = true;
sscanf(agget(n, "pos"), "%lf,%lf", &xx, &yy);
(*x)[i*dim] = xx;
(*x)[i*dim+1] = yy;
} else {
fprintf(stderr,"WARNING: pos field missing for node %d, set to origin\n",i);
(*x)[i*dim] = 0;
(*x)[i*dim+1] = 0;
}
}
if (!has_position){
free(*x);
*x = NULL;
}
}
free(I);
free(J);
free(val);
return A;
}
void attached_clustering(Agraph_t* g, int maxcluster, int clustering_scheme){
SparseMatrix A = 0;
Agnode_t* n;
Agedge_t* e;
Agsym_t *sym, *clust_sym;
int nnodes;
int nedges;
int i, row,nc;
double v;
int type = MATRIX_TYPE_REAL;
size_t sz = sizeof(double);
if (!g) return;
nnodes = agnnodes (g);
nedges = agnedges (g);
/* Assign node ids */
i = 0;
for (n = agfstnode (g); n; n = agnxtnode (g, n))
ND_id(n) = i++;
/* form matrix */
int* I = gv_calloc(nedges, sizeof(int));
int* J = gv_calloc(nedges, sizeof(int));
double* val = gv_calloc(nedges, sizeof(double));
sym = agattr_text(g, AGEDGE, "weight", NULL);
clust_sym = agattr_text(g, AGNODE, "cluster", NULL);
i = 0;
for (n = agfstnode (g); n; n = agnxtnode (g, n)) {
row = ND_id(n);
for (e = agfstout (g, n); e; e = agnxtout (g, e)) {
I[i] = row;
J[i] = ND_id(aghead(e));
if (sym) {
if (sscanf (agxget(e,sym), "%lf", &v) != 1)
v = 1;
}
else
v = 1;
val[i] = v;
i++;
}
}
A = SparseMatrix_from_coordinate_arrays((size_t)nedges, nnodes, nnodes, I, J,
val, type, sz);
int *clusters = gv_calloc(nnodes, sizeof(int));
{
double modularity;
if (!clust_sym) clust_sym = agattr_text(g,AGNODE,"cluster","-1");
if (clustering_scheme == CLUSTERING_MQ){
mq_clustering(A, maxcluster,
&nc, &clusters, &modularity);
} else if (clustering_scheme == CLUSTERING_MODULARITY){
modularity_clustering(A, false, maxcluster,
&nc, &clusters, &modularity);
} else {
UNREACHABLE();
}
for (i = 0; i < nnodes; i++) (clusters)[i]++;/* make into 1 based */
for (n = agfstnode (g); n; n = agnxtnode (g, n)) {
i = ND_id(n);
agxset(n, clust_sym, ITOS(clusters[i]));
}
if (Verbose){
fprintf(stderr," no complement clustering info in dot file, using modularity clustering. Modularity = %f, ncluster=%d\n",modularity, nc);
}
}
free(I);
free(J);
free(val);
free(clusters);
SparseMatrix_delete(A);
}
void initDotIO (Agraph_t *g)
{
aginit(g, AGNODE, "info", sizeof(Agnodeinfo_t), true);
}
void setDotNodeID (Agnode_t* n, int v)
{
ND_id(n) = v;
}
int getDotNodeID (Agnode_t* n)
{
return ND_id(n);
}
|