28 #define ND_id(n) (((Agnodeinfo_t*)((n)->base.data))->id)
32 posStr (
int len_buf,
char* buf,
int dim,
real* x,
double sc)
39 for (i = 0; i < dim; i++){
41 sprintf(s,
"%f,",sc*x[i]);
43 sprintf(s,
"%f",sc*x[i]);
65 posStr (
SLEN, buf, dim, x + i*dim, sc);
73 static void color_string(
int slen,
char *buf,
int dim,
real *color){
74 if (dim > 3 || dim < 1){
75 fprintf(stderr,
"can only 1, 2 or 3 dimensional color space. with color value between 0 to 1\n");
80 sprintf(buf,
"#%02x%02x%02x",
MIN((
unsigned int)(color[0]*255),255),
81 MIN((
unsigned int) (color[1]*255), 255),
MIN((
unsigned int)(color[2]*255), 255));
83 sprintf(buf,
"#%02x%02x%02x",
MIN((
unsigned int)(color[0]*255),255),
84 MIN((
unsigned int) (color[0]*255), 255),
MIN((
unsigned int)(color[0]*255), 255));
86 sprintf(buf,
"#%02x%02x%02x",
MIN((
unsigned int)(color[0]*255),255),
87 0,
MIN((
unsigned int)(color[1]*255), 255));
109 if (row == col)
continue;
110 color_string(slen, buf, dim, colors + ie*dim);
150 size_t sz =
sizeof(
real);
160 fprintf (stderr,
"Format %d not supported\n", format);
176 I =
N_NEW(nedges,
int);
177 J =
N_NEW(nedges,
int);
189 I =
N_NEW(nedges,
int);
190 J =
N_NEW(nedges,
int);
201 if (edge_label_nodes && strncmp(
agnameof(n),
"|edgelabel|",11)==0) nedge_nodes++;
209 if (sscanf (
agxget(e,sym),
"%lf", &v) != 1) v = 1;
217 if (sscanf (
agxget (e, symD),
"%lf", &v) != 1) {
231 if (edge_label_nodes) {
232 *edge_label_nodes =
MALLOC(
sizeof(
int)*nedge_nodes);
237 if (label_sizes) *label_sizes =
MALLOC(
sizeof(
real)*2*nnodes);
241 if (edge_label_nodes && strncmp(
agnameof(n),
"|edgelabel|",11)==0) {
242 (*edge_label_nodes)[nedge_nodes++] = i;
246 sscanf(
agget(n,
"width"),
"%lf", &sz);
248 (*label_sizes)[i*2] =
POINTS(sz)*.5 + padding*0.5;
249 sscanf(
agget(n,
"height"),
"%lf", &sz);
251 (*label_sizes)[i*2+1] =
POINTS(sz)*.5 + padding*0.5;
253 (*label_sizes)[i*2] = 4*
POINTS(0.75)*.5;
254 (*label_sizes)[i*2+1] = 4*
POINTS(0.5)*.5;
260 int has_positions =
TRUE;
270 if ((pval =
agxget(n, psym)) && *pval) {
272 nitems = sscanf(pval,
"%lf,%lf", &xx, &yy);
274 has_positions =
FALSE;
279 }
else if (dim == 3){
280 nitems = sscanf(pval,
"%lf,%lf,%lf", &xx, &yy, &zz);
282 has_positions =
FALSE;
288 }
else if (dim == 4){
289 nitems = sscanf(pval,
"%lf,%lf,%lf,%lf", &xx, &yy, &zz,&ww);
291 has_positions =
FALSE;
298 }
else if (dim == 1){
299 nitems = sscanf(pval,
"%lf", &xx);
300 if (nitems != 1)
return NULL;
306 has_positions =
FALSE;
310 if (!has_positions) {
319 if (edge_label_nodes) *n_edge_label_nodes = nedge_nodes;
328 if (valD)
FREE(valD);
337 for (k = 0; k < dim; k++) d += (x[k] - y[k])*(x[k]-y[k]);
368 if (!(*xsplines)) *xsplines = malloc(
sizeof(
char*)*nedges);
375 char *pos =
agxget (e, sym);
376 (*xsplines)[i] = malloc(
sizeof(
char)*(strlen(pos)+1));
377 strcpy((*xsplines)[i], pos);
379 (*xsplines)[i] =
NULL;
388 int n = A->
m, *ia = A->
ia, *ja = A->
ja;
390 real max_edge_len, min_edge_len;
392 for (i = 0; i < n; i++){
393 for (j = ia[i]; j < ia[i+1]; j++){
394 max_edge_len =
MAX(max_edge_len,
dist(dim, &x[dim*i], &x[dim*ja[j]]));
395 if (min_edge_len < 0){
396 min_edge_len =
dist(dim, &x[dim*i], &x[dim*ja[j]]);
398 min_edge_len =
MIN(min_edge_len,
dist(dim, &x[dim*i], &x[dim*ja[j]]));
415 fprintf(stderr,
"writing a total of %d edges\n",A->
nz);
416 fwrite(&(A->
n),
sizeof(
int), 1, f);
417 fwrite(&(A->
nz),
sizeof(
int), 1, f);
418 fwrite(&dim,
sizeof(
int), 1, f);
419 fwrite(x,
sizeof(
real), dim*n, f);
420 fwrite(&min_edge_len,
sizeof(
real), 1, f);
421 fwrite(&max_edge_len,
sizeof(
real), 1, f);
422 for (i = 0; i < n; i++){
423 if (i%1000 == 0) fprintf(stderr,
"%6.2f%% done\r", i/(
real) n*100);
424 fwrite(&i,
sizeof(
int), 1, f);
425 len = ia[i+1] - ia[i];
426 fwrite(&len,
sizeof(
int), 1, f);
427 fwrite(&(ja[ia[i]]),
sizeof(
int), len, f);
446 fprintf(fp,
"%d %d\n",n, dim);
447 for (i = 0; i < n; i++){
448 for (k = 0; k < dim; k++){
449 fprintf(fp,
"%f ",x[i*dim+k]);
473 char buf[1024], buf2[1024];
488 label_string =
MALLOC(
sizeof(
char)*1000);
495 sprintf (buf,
"%f", 1.0);
497 label_string = strcpy(label_string, name);
498 label_string = strcat(label_string,
". ");
499 sprintf (buf,
"%d", A->
m);
500 label_string = strcat(label_string, buf);
501 label_string = strcat(label_string,
" nodes, ");
502 sprintf (buf,
"%d", A->
nz);
503 label_string = strcat(label_string, buf);
505 label_string = strcat(label_string,
" edges.");
508 if (with_label) sym =
agattr(g,
AGRAPH,
"label", label_string);
510 if (with_color) sym =
agattr(g,
AGRAPH,
"bgcolor",
"black");
537 }
else if (A->
m < 500){
539 }
else if (A->
m < 5000){
549 for (i = 0; i < A->
m; i++) {
550 sprintf (buf,
"%d", i);
560 color = malloc(
sizeof(
real)*A->
nz);
564 for (j = ia[i]; j < ia[i+1]; j++) {
565 color[j] =
distance(x, dim, i, ja[j]);
571 mindist =
MIN(mindist, color[j]);
574 maxdist =
MAX(color[j], maxdist);
577 for (j = ia[i]; j < ia[i+1]; j++) {
578 color[j] =
ABS(val[j]);
584 mindist =
MIN(mindist, color[j]);
587 maxdist =
MAX(color[j], maxdist);
593 for (j = ia[i]; j < ia[i+1]; j++) {
595 color[j] = ((color[j] - mindist)/
MAX(maxdist-mindist, 0.000001));
606 for (j = ia[i]; j < ia[i+1]; j++) {
611 sprintf (buf,
"%f", ((
real*)A->
a)[j]);
614 sprintf (buf,
"%d", ((
int*)A->
a)[j]);
617 sprintf (buf,
"%f", ((
real*)A->
a)[2*j]);
622 sprintf (buf2,
"%s",
hue2rgb(.65*color[j], cstring));
624 sprintf (buf2,
"#000000");
628 sprintf (buf,
"%f", 1.);
631 sprintf (buf2,
"%s",
hue2rgb(.65*color[j], cstring));
633 sprintf (buf2,
"#000000");
640 sprintf (buf2,
"%f", color[j]);
655 s = malloc(
sizeof(
char)*(strlen(s1)+strlen(s2)+1+1));
665 sprintf(sid,
"%d",
id);
666 s = malloc(
sizeof(
char)*(strlen(s1)+strlen(s2)+strlen(s3)+strlen(sid)+1+3));
702 ndmap = malloc(
sizeof(
Agnode_t *)*nnodes);
713 agset(newnode,
"shape",
"box");
733 if (sym && (elabel =
agxget(ep,sym)) && strcmp(elabel,
"") != 0) {
735 newnode = mkNode(dg,s);
736 agset(newnode,
"label",elabel);
737 agset(newnode,
"shape",
"plaintext");
739 agset(e,
"arrowsize",
"0");
771 static int hex2int(
char h){
772 if (h >=
'0' && h <=
'9')
return h -
'0';
773 if (h >=
'a' && h <=
'f')
return 10 + h -
'a';
774 if (h >=
'A' && h <=
'F')
return 10 + h -
'A';
777 static float hexcol2rgb(
char *h){
778 return (hex2int(h[0])*16 + hex2int(h[1]))/255.;
788 if (!clust_clr_sym) clust_clr_sym =
agattr(g,
AGNODE,
"clustercolor",
"-1");
791 if (rgb_r && rgb_g && rgb_b) {
792 rgb2hex((rgb_r)[(clusters)[i]],(rgb_g)[(clusters)[i]],(rgb_b)[(clusters)[i]], scluster,
NULL);
795 agxset(n,clust_clr_sym,scluster);
799 SparseMatrix Import_coord_clusters_from_dot(
Agraph_t* g,
int maxcluster,
int dim,
int *nn,
real **label_sizes,
real **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){
808 int i, row, ic,nc, j;
814 size_t sz =
sizeof(
real);
818 int MAX_GRPS, MIN_GRPS, noclusterinfo =
FALSE, first =
TRUE;
822 switch (default_color_scheme){
839 fprintf(stderr,
" HERE!\n");
843 fprintf(stderr,
" HERE!\n");
875 I =
N_NEW(nedges,
int);
876 J =
N_NEW(nedges,
int);
890 if (sscanf (
agxget(e,sym),
"%lf", &v) != 1)
902 *clusters =
MALLOC(
sizeof(
int)*nnodes);
911 memset (*clusters, 0,
sizeof(
int)*nnodes);
913 if (strncmp(
agnameof(sg),
"cluster", 7))
continue;
918 fprintf (stderr,
"Warning: node %s appears in multiple clusters.\n",
agnameof(n));
920 (*clusters)[i] = gid;
925 if ((*clusters)[i] == 0)
931 else if (clust_sym) {
934 if ((sscanf(
agxget(n,clust_sym),
"%d", &ic)>0)) {
941 MIN_GRPS =
MIN(MIN_GRPS, ic);
944 noclusterinfo =
TRUE;
950 noclusterinfo =
TRUE;
954 int use_value =
TRUE, flag = 0;
956 if (!clust_sym) clust_sym =
agattr(g,
AGNODE,
"cluster",
"-1");
960 &nc, clusters, &modularity, &flag);
963 &nc, clusters, &modularity, &flag);
967 for (i = 0; i < nnodes; i++) (*clusters)[i]++;
970 sprintf(scluster,
"%d",(*clusters)[i]);
971 agxset(n,clust_sym,scluster);
976 fprintf(stderr,
" no complement clustering info in dot file, using modularity clustering. Modularity = %f, ncluster=%d\n",modularity, nc);
980 *label_sizes =
MALLOC(
sizeof(
real)*dim*nnodes);
981 if (pal || (!noclusterinfo && clust_clr_sym)){
982 *rgb_r =
MALLOC(
sizeof(
float)*(1+MAX_GRPS));
983 *rgb_g =
MALLOC(
sizeof(
float)*(1+MAX_GRPS));
984 *rgb_b =
MALLOC(
sizeof(
float)*(1+MAX_GRPS));
990 *fsz =
MALLOC(
sizeof(
float)*nnodes);
991 *labels =
MALLOC(
sizeof(
char*)*nnodes);
998 sscanf(
agget(n,
"width"),
"%lf", &sz);
999 (*label_sizes)[i*2] =
POINTS(sz*0.5);
1000 sscanf(
agget(n,
"height"),
"%lf", &sz);
1001 (*label_sizes)[i*2+1] =
POINTS(sz*0.5);
1003 (*label_sizes)[i*2] =
POINTS(0.75/2);
1004 (*label_sizes)[i*2+1] =
POINTS(0.5*2);
1007 if (
agget(n,
"fontsize")){
1008 sscanf(
agget(n,
"fontsize"),
"%f", &ff);
1014 if (
agget(n,
"label") && strcmp(
agget(n,
"label"),
"") != 0 && strcmp(
agget(n,
"label"),
"\\N") != 0){
1016 lbs =
agget(n,
"label");
1017 (*labels)[i] =
MALLOC(
sizeof(
char)*(strlen(lbs)+1));
1018 strcpy((*labels)[i], lbs);
1027 if (MAX_GRPS-MIN_GRPS < max_color) {
1028 j = (j-MIN_GRPS)*((
int)((max_color-1)/
MAX((MAX_GRPS-MIN_GRPS),1)));
1030 j = (j-MIN_GRPS)%max_color;
1035 (*rgb_r)[(*clusters)[i]] = pal[3*j+0];
1036 (*rgb_g)[(*clusters)[i]] = pal[3*j+1];
1037 (*rgb_b)[(*clusters)[i]] = pal[3*j+2];
1041 (*rgb_r)[(*clusters)[i]] = color.
u.
RGBA[0];
1042 (*rgb_g)[(*clusters)[i]] = color.
u.
RGBA[1];
1043 (*rgb_b)[(*clusters)[i]] = color.
u.
RGBA[2];
1046 if (!noclusterinfo &&
agget(n,
"cluster") &&
agget(n,
"clustercolor") && strlen(
agget(n,
"clustercolor") ) >= 7 && pal){
1048 strcpy(cc,
agget(n,
"clustercolor"));
1049 (*rgb_r)[(*clusters)[i]] = hexcol2rgb(cc+1);
1050 (*rgb_g)[(*clusters)[i]] = hexcol2rgb(cc+3);
1051 (*rgb_b)[(*clusters)[i]] = hexcol2rgb(cc+5);
1058 int has_position =
FALSE;
1063 if (
agget(n,
"pos")){
1064 has_position =
TRUE;
1065 sscanf(
agget(n,
"pos"),
"%lf,%lf", &xx, &yy);
1069 fprintf(stderr,
"WARNING: pos field missing for node %d, set to origin\n",i);
1101 size_t sz =
sizeof(
real);
1119 I =
N_NEW(nedges,
int);
1120 J =
N_NEW(nedges,
int);
1133 if (sscanf (
agxget(e,sym),
"%lf", &v) != 1)
1144 clusters =
MALLOC(
sizeof(
int)*nnodes);
1147 int use_value =
TRUE, flag = 0;
1149 if (!clust_sym) clust_sym =
agattr(g,
AGNODE,
"cluster",
"-1");
1153 &nc, &clusters, &modularity, &flag);
1156 &nc, &clusters, &modularity, &flag);
1160 for (i = 0; i < nnodes; i++) (clusters)[i]++;
1163 sprintf(scluster,
"%d",(clusters)[i]);
1164 agxset(n,clust_sym,scluster);
1167 fprintf(stderr,
" no complement clustering info in dot file, using modularity clustering. Modularity = %f, ncluster=%d\n",modularity, nc);
void s1(graph_t *, node_t *)
char * cat_string(char *s1, char *s2)
CGRAPH_API Agnode_t * agnode(Agraph_t *g, char *name, int createflag)
CGRAPH_API Agraph_t * agopen(char *name, Agdesc_t desc, Agdisc_t *disc)
#define SparseMatrix_known_undirected(A)
Agsym_t * agattr(Agraph_t *g, int kind, char *name, char *value)
float palette_grey[1001][3]
int getDotNodeID(Agnode_t *n)
float palette_pastel[1001][3]
void modularity_clustering(SparseMatrix A, int inplace, int ncluster_target, int use_value, int *nclusters, int **assignment, real *modularity, int *flag)
int agxset(void *obj, Agsym_t *sym, char *value)
SparseMatrix SparseMatrix_new(int m, int n, int nz, int type, int format)
float palette_adam[11][3]
void rgb2hex(float r, float g, float b, char *cstring, char *opacity)
void attach_embedding(Agraph_t *g, int dim, double sc, real *x)
void edgelist_export(FILE *f, SparseMatrix A, int dim, real *x)
SparseMatrix SparseMatrix_import_dot(Agraph_t *g, int dim, real **label_sizes, real **x, int *n_edge_label_nodes, int **edge_label_nodes, int format, SparseMatrix *D)
CGRAPH_API Agraph_t * agread(void *chan, Agdisc_t *disc)
CGRAPH_API Agedge_t * agfstedge(Agraph_t *g, Agnode_t *n)
void mq_clustering(SparseMatrix A, int inplace, int maxcluster, int use_value, int *nclusters, int **assignment, real *mq, int *flag)
int agerr(agerrlevel_t level, const char *fmt,...)
CGRAPH_API Agraph_t * agfstsubg(Agraph_t *g)
real distance(real *x, int dim, int i, int j)
CGRAPH_API Agedge_t * agfstout(Agraph_t *g, Agnode_t *n)
float palette_white_to_red[1001][3]
char * hue2rgb(real hue, char *color)
CGRAPH_API Agdesc_t Agundirected
void initDotIO(Agraph_t *g)
char * agget(void *obj, char *name)
float palette_sequential_singlehue_red[1001][3]
CGRAPH_API Agraph_t * agnxtsubg(Agraph_t *subg)
CGRAPH_API Agnode_t * agtail(Agedge_t *e)
CGRAPH_API Agdesc_t Agdirected
void attach_edge_colors(Agraph_t *g, int dim, real *colors)
int agset(void *obj, char *name, char *value)
CGRAPH_API Agnode_t * agnxtnode(Agraph_t *g, Agnode_t *n)
CGRAPH_API Agnode_t * aghead(Agedge_t *e)
char * strip_dir(char *s)
float palette_adam_blend[1001][3]
CGRAPH_API char * agnameof(void *)
int Import_dot_splines(Agraph_t *g, int *ne, char ***xsplines)
void attached_clustering(Agraph_t *g, int maxcluster, int clustering_scheme)
float palette_grey_to_red[1001][3]
float palette_blue_to_yellow[1001][3]
char * cat_string3(char *s1, char *s2, char *s3, int id)
CGRAPH_API Agnode_t * agfstnode(Agraph_t *g)
CGRAPH_API void aginit(Agraph_t *g, int kind, char *rec_name, int rec_size, int move_to_front)
void SparseMatrix_delete(SparseMatrix A)
Agraph_t * SparseMatrix_read_dot(FILE *f)
int colorxlate(char *str, gvcolor_t *color, color_type_t target_type)
CGRAPH_API Agedge_t * agnxtedge(Agraph_t *g, Agedge_t *e, Agnode_t *n)
SparseMatrix SparseMatrix_from_coordinate_arrays(int nz, int m, int n, int *irn, int *jcn, void *val0, int type, size_t sz)
EXTERN unsigned char Verbose
CGRAPH_API int agnnodes(Agraph_t *g)
Agraph_t * makeDotGraph(SparseMatrix A, char *name, int dim, real *x, int with_color, int with_label, int use_matrix_value)
void Dot_SetClusterColor(Agraph_t *g, float *rgb_r, float *rgb_g, float *rgb_b, int *clusters)
CGRAPH_API Agedge_t * agedge(Agraph_t *g, Agnode_t *t, Agnode_t *h, char *name, int createflag)
Agraph_t * convert_edge_labels_to_nodes(Agraph_t *g)
CGRAPH_API void * agbindrec(void *obj, char *name, unsigned int size, int move_to_front)
float palette_sequential_singlehue_red_lighter[1001][3]
struct Agnodeinfo_t Agnodeinfo_t
void setDotNodeID(Agnode_t *n, int v)
CGRAPH_API int agnedges(Agraph_t *g)
SparseMatrix Import_coord_clusters_from_dot(Agraph_t *g, int maxcluster, int dim, int *nn, real **label_sizes, real **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)
char * agxget(void *obj, Agsym_t *sym)
double dist(Site *s, Site *t)
CGRAPH_API Agedge_t * agnxtout(Agraph_t *g, Agedge_t *e)
void dump_coordinates(char *name, int n, int dim, real *x)
Agraph_t * assign_random_edge_color(Agraph_t *g)
float palette_primary[1001][3]