31 #define node_degree(i) (ia[(i)+1] - ia[(i)])
34 static void get_neighborhood_precision_recall(
char *outfile,
SparseMatrix A0,
real *ideal_dist_matrix,
real *dist_matrix){
36 int i, j, k, n = A->
m;
38 int *g_order =
NULL, *p_order =
NULL;
39 real *gdist, *pdist, radius;
47 fp = fopen(outfile,
"w");
55 for (k = 5; k <= 50; k+= 5){
57 for (i = 0; i < n; i++){
58 gdist = &(ideal_dist_matrix[i*n]);
60 pdist = &(dist_matrix[i*n]);
62 ng_neighbors =
MIN(n-1, k);
63 np_neighbors = ng_neighbors;
64 radius = pdist[p_order[np_neighbors]];
66 for (j = 1; j <= ng_neighbors; j++){
67 node_dist = pdist[g_order[j]];
68 if (node_dist <= radius) true_positive++;
70 recall += true_positive/np_neighbors;
74 fprintf(fp,
"%d %f\n", k, recall);
76 fprintf(stderr,
"wrote precision/recall in file %s\n", outfile);
90 real *dij, *xij, wij,
top = 0, bot = 0, t;
93 real dmin, dmax,
xmax,
xmin, bsta, bwidth, *xbin, x25, x75, median;
94 int nbins = 30, nsta, nz = 0;
97 fp = fopen(outfile,
"w");
105 for (i = 0; i < n; i++){
106 for (j = i+1; j < n; j++){
107 dij[nz] = dist_matrix[i*n+j];
110 wij = 1/(dij[nz]*dij[nz]);
114 top += wij * dij[nz] * xij[nz];
115 bot += wij*xij[nz]*xij[nz];
124 fprintf(stderr,
"scaling factor = %f\n", t);
126 for (i = 0; i < nz; i++) xij[i] *= t;
131 nbins =
MIN(nbins, dmax/
MAX(1,dmin));
132 bwidth = (dmax - dmin)/nbins;
134 nsta = 0; bsta = dmin;
138 for (i = 0; i < nbins; i++){
140 nz = 0; xmin = xmax = xij[p[nsta]];
141 while (nsta < nzz && dij[p[nsta]] >= bsta && dij[p[nsta]] <= bsta + bwidth){
142 xbin[nz++] = xij[p[nsta]];
143 xmin =
MIN(xmin, xij[p[nsta]]);
144 xmax =
MAX(xmax, xij[p[nsta]]);
152 fprintf(fp,
"%d %f %f %f %f %f %f %f\n", nz, bsta, bsta + bwidth, xmin, x25, median, x75, xmax);
154 xmin = xmax = median = x25 = x75 = (bsta + 0.5*bwidth);
173 real *ideal_dist_matrix =
NULL, *dist_matrix;
175 real t, top = 0, bot = 0, lstress, stress = 0, dij, wij, xij;
176 real sne_disimilarity = 0;
184 for (i = 0; i < n; i++){
185 for (j = i+1; j < n; j++){
186 dij = ideal_dist_matrix[i*n+j];
190 switch (weighting_scheme){
206 top += wij * dij * xij;
216 for (i = 0; i < n; i++){
217 dist_matrix[i*n+i] = 0.;
218 for (j = i+1; j < n; j++){
219 dij = ideal_dist_matrix[i*n+j];
222 dist_matrix[i*n+j] = xij*t;
223 dist_matrix[j*n+i] = xij*t;
229 lstress = (xij*t - dij);
230 stress += wij*lstress*lstress;
235 sne_disimilarity = get_sne_disimilarity(n, ideal_dist_matrix, dist_matrix, K);
238 get_neighborhood_precision_recall(
"/tmp/recall.txt", A, ideal_dist_matrix, dist_matrix);
239 get_neighborhood_precision_recall(
"/tmp/precision.txt", A, dist_matrix, ideal_dist_matrix);
241 fprintf(stderr,
"sne_disimilarity = %f\n",sne_disimilarity);
242 if (n > 0) fprintf(stderr,
"sstress per edge = %f\n",stress/n/(n-1)*2);
245 FREE(ideal_dist_matrix);
255 int *ia, *ja, i, j, k, l, nz;
258 real len, di, sum, sumd;
273 for (i = 0; i < D->
m; i++) mask[i] = -1;
275 for (i = 0; i < D->
m; i++){
278 for (j = ia[i]; j < ia[i+1]; j++){
279 if (i == ja[j])
continue;
282 for (j = ia[i]; j < ia[i+1]; j++){
284 if (i == k)
continue;
286 for (l = ia[k]; l < ia[k+1]; l++){
287 if (mask[ja[l]] == i) len--;
297 for (i = 0; i < D->
m; i++){
298 for (j = ia[i]; j < ia[i+1]; j++){
299 if (i == ja[j])
continue;
305 sum /= nz; sumd /= nz;
308 for (i = 0; i < D->
m; i++){
309 for (j = ia[i]; j < ia[i+1]; j++){
310 if (i == ja[j])
continue;
321 int ideal_dist_scheme){
327 int i, j, k, l, m = A->
m, *ia = A->
ia, *ja = A->
ja, *iw, *jw, *id, *jd;
329 real *d, *w, *lambda;
330 real *avg_dist, diag_d, diag_w,
dist,
s = 0, stop = 0, sbot = 0;
345 for (i = 0; i < m; i++) sm->
lambda[i] = lambda0;
350 for (i = 0; i < m ;i++){
353 for (j = ia[i]; j < ia[i+1]; j++){
354 if (i == ja[j])
continue;
355 avg_dist[i] +=
distance(x, dim, i, ja[j]);
363 for (i = 0; i < m; i++) mask[i] = -1;
366 for (i = 0; i < m; i++){
368 for (j = ia[i]; j < ia[i+1]; j++){
375 for (j = ia[i]; j < ia[i+1]; j++){
377 for (l = ia[k]; l < ia[k+1]; l++){
378 if (mask[ja[l]] != i){
388 if (!(sm->
Lw) || !(sm->
Lwd)) {
401 for (i = 0; i < m; i++){
404 for (j = ia[i]; j < ia[i+1]; j++){
413 dist = (avg_dist[i] + avg_dist[k])*0.5;
417 fprintf(stderr,
"ideal_dist_scheme value wrong");
426 w[nz] = -1/(dist*
dist);
446 for (j = ia[i]; j < ia[i+1]; j++){
448 for (l = ia[k]; l < ia[k+1]; l++){
449 if (mask[ja[l]] != i+m){
455 dist = (avg_dist[i] + 2*avg_dist[k] + avg_dist[ja[l]])*0.5;
459 fprintf(stderr,
"ideal_dist_scheme value wrong");
470 w[nz] = -1/(dist*
dist);
481 stop += d[nz]*
distance(x,dim,ja[l],k);
490 lambda[i] *= (-diag_w);
492 w[nz] = -diag_w + lambda[i];
501 for (i = 0; i < nz; i++) d[i] *= s;
514 int weighting_scheme,
int scale_initial_coord){
519 int i, j, k, m = A->
m, *ia, *ja, *iw, *jw, *id, *jd;
521 real *d, *w, *lambda;
522 real diag_d, diag_w, *a,
dist,
s = 0, stop = 0, sbot = 0;
528 for (i = 0; i < m*dim; i++) xdot += x[i]*x[i];
530 for (i = 0; i < m*dim; i++) x[i] = 72*
drand();
547 for (i = 0; i < m; i++) sm->
lambda[i] = lambda0;
553 if (!(sm->
Lw) || !(sm->
Lwd)) {
564 for (i = 0; i < m; i++){
566 for (j = ia[i]; j < ia[i+1]; j++){
572 switch (weighting_scheme){
577 w[nz] = -1/(dist*
dist);
607 lambda[i] *= (-diag_w);
608 w[nz] = -diag_w + lambda[i];
617 if (scale_initial_coord){
625 for (i = 0; i < nz; i++) d[i] *= s;
635 static real total_distance(
int m,
int dim,
real* x,
real* y){
639 for (i = 0; i < m; i++){
641 for (j = 0; j < dim; j++){
642 dist += (y[i*dim+j] - x[i*dim+j])*(y[i*dim+j] - x[i*dim+j]);
668 int *ia = A_constr->
ia, *ja = A_constr->
ja, ii, jj, nz, l, ll, i, j;
669 int *irn = data->
irn, *jcn = data->
jcn;
694 for (i = 0; i < n_constr_nodes; i++){
695 ii = constr_nodes[i];
696 k = ia[ii+1] - ia[ii];
697 nz += (
int)((k+1)*(k+1));
700 irn = data->
irn =
MALLOC(
sizeof(
int)*nz);
701 jcn = data->
jcn =
MALLOC(
sizeof(
int)*nz);
702 val = data->
val =
MALLOC(
sizeof(
double)*nz);
705 for (i = 0; i < n_constr_nodes; i++){
706 ii = constr_nodes[i];
707 jj = ja[ia[ii]]; ll = ja[ia[ii] + 1];
708 if (jj == ll)
continue;
712 k = ia[ii+1] - ia[ii];
714 irn[nz] = ii; jcn[nz] = ii; val[nz++] = constr_penalty/(
dist);
715 k = constr_penalty/(k*
dist); kk = constr_penalty/(kk*
dist);
716 for (j = ia[ii]; j < ia[ii+1]; j++){
717 irn[nz] = ii; jcn[nz] = ja[j]; val[nz++] = -k;
719 for (j = ia[ii]; j < ia[ii+1]; j++){
721 irn[nz] = jj; jcn[nz] = ii; val[nz++] = -k;
722 for (l = ia[ii]; l < ia[ii+1]; l++){
724 irn[nz] = jj; jcn[nz] = ll; val[nz++] = kk;
736 irn = data->
irn =
MALLOC(
sizeof(
int)*nz);
737 jcn = data->
jcn =
MALLOC(
sizeof(
int)*nz);
738 val = data->
val =
MALLOC(
sizeof(
double)*nz);
741 for (i = 0; i < m*dim; i++) x00[i] = 0;
743 for (i = 0; i < n_constr_nodes; i++){
744 ii = constr_nodes[i];
745 jj = ja[ia[ii]]; ll = ja[ia[ii] + 1];
747 irn[nz] = ii; jcn[nz] = ii; val[nz++] = constr_penalty/(
dist);
748 for (j = ia[ii]; j < ia[ii+1]; j++){
750 for (l = 0; l < dim; l++){
751 x00[ii*dim+l] += x[jj*dim+l];
754 for (l = 0; l < dim; l++) {
755 x00[ii*dim+l] *= constr_penalty/(
dist)/(ia[ii+1] - ia[ii]);
769 for (i = 0; i < m; i++){
770 for (j = iw[i]; j < iw[i+1]; j++){
782 return 0.5*res/scaling/scaling;
789 for (i = 0; i < m; i++){
790 for (j = i+1; j < m; j++){
792 for (k = 0; k < dim; k++){
793 distij = (x[i*dim+k] - x[j*dim+k])/dist;
794 y[i*dim+k] += alpha*M*distij;
795 y[j*dim+k] += alpha*M*(-distij);
808 return cg(Ax, Precon, Lw->
m, dim, x0, rhs, tol, maxit, flag);
814 int i, j, k, m, *id, *jd, *iw, *jw, idiag, flag = 0, iter = 0;
815 real *w, *dd, *d, *y =
NULL, *x0 =
NULL, *x00 =
NULL, diag, diff = 1, *lambda = sm->
lambda, res, alpha = 0., M = 0.;
823 if (!x0)
goto RETURN;
829 id = Lwd->ia; jd = Lwd->ja;
831 dd = (
real*) Lwdd->a;
833 iw = Lw->
ia; jw = Lw->
ja;
836 if (
Verbose) fprintf(stderr,
"initial stress = %f\n",
get_stress(m, dim, iw, jw, w, d, x, sm->
scaling, sm->
data, 1));
840 get_edge_label_matrix(sm->
data, m, dim, x, &Lc, &x00);
847 while (iter++ < maxit_sm && diff > tol){
851 if (iter%2 == 0) gviewer_dump_current_frame();
856 for (i = 0; i < m; i++){
859 for (j =
id[i]; j <
id[i+1]; j++){
875 for (k = 0; k < dim; k++) x[jd[j]*dim+k] += 0.0001*(
drand()+.0001)*dij;
885 if (dist < -d[j]*0.0000001){
902 for (i = 0; i < m; i++){
903 for (j = 0; j < dim; j++){
910 for (i = 0; i < m; i++){
911 for (j = 0; j < dim; j++){
912 y[i*dim+j] += lambda[i]*x0[i*dim+j];
920 for (i = 0; i < m; i++){
921 for (j = 0; j < dim; j++){
922 y[i*dim+j] += x00[i*dim+j];
928 uniform_stress_augment_rhs(m, dim, x, y, alpha, M);
936 lab =
MALLOC(
sizeof(
char)*100);
937 sprintf(lab,
"maxent. alpha=%10.2g, iter=%d",stress_maxent_data_get_alpha(sm->
data), iter);
938 gviewer_set_label(lab);
942 stress_maxent_augment_rhs_fast(sm, dim, x, y, &flag);
943 if (flag)
goto RETURN;
947 stress_approx_augment_rhs(sm, dim, x, y, &flag);
948 if (flag)
goto RETURN;
955 lab =
MALLOC(
sizeof(
char)*100);
956 sprintf(lab,
"pmds(k), iter=%d", iter);
957 gviewer_set_label(lab);
969 fprintf(stderr,
"stress1 = %g\n",
get_stress(m, dim, iw, jw, w, d, x, sm->
scaling, sm->
data, 1));
974 res = uniform_stress_solve(Lw, alpha, dim, x, y, sm->
tol_cg, sm->
maxit_cg, &flag);
980 if (flag)
goto RETURN;
982 if (
Verbose) fprintf(stderr,
"stress2 = %g\n",
get_stress(m, dim, iw, jw, w, d, y, sm->
scaling, sm->
data, 1));
984 diff = total_distance(m, dim, x, y)/sqrt(
vector_product(m*dim, x, x));
987 fprintf(stderr,
"Outer iter = %d, cg res = %g, ||x_{k+1}-x_k||/||x_k|| = %g\n",iter, res, diff);
996 _statistics[1] += iter-1;
1000 if (
Verbose) fprintf(stderr,
"iter = %d, final stress = %f\n", iter,
get_stress(m, dim, iw, jw, w, d, x, sm->
scaling, sm->
data, 1));
1029 int i, j, k, m = A->
m, *ia = A->
ia, *ja = A->
ja, *iw, *jw, jdiag, nz;
1031 real *avg_dist, *lambda, *d, *w, diag_d, diag_w,
dist;
1032 real s = 0, stop = 0, sbot = 0;
1038 for (i = 0; i < m ;i++){
1041 for (j = ia[i]; j < ia[i+1]; j++){
1042 if (i == ja[j])
continue;
1043 avg_dist[i] +=
distance(x, dim, i, ja[j]);
1058 for (i = 0; i < m; i++) sm->
lambda[i] = lambda0;
1061 if (use_triangularization){
1076 if (!(sm->
Lw) || !(sm->
Lwd)) {
1085 for (i = 0; i < m; i++){
1086 diag_d = diag_w = 0;
1088 for (j = iw[i]; j < iw[i+1]; j++){
1098 w[j] = 1/(dist*
dist);
1110 lambda[i] *= (-diag_w);
1113 w[jdiag] = -diag_w + lambda[i];
1118 for (i = 0; i < iw[m]; i++) d[i] *= s;
1143 int i, j, k, l, m = A->
m, *ia = A->
ia, *ja = A->
ja, *id, *jd;
1159 for (i = 0; i < m ;i++){
1162 for (j = ia[i]; j < ia[i+1]; j++){
1163 if (i == ja[j])
continue;
1164 avg_dist[i] +=
distance(x, dim, i, ja[j]);
1172 for (i = 0; i < m; i++) mask[i] = -1;
1175 for (i = 0; i < m; i++){
1177 for (j = ia[i]; j < ia[i+1]; j++){
1184 for (j = ia[i]; j < ia[i+1]; j++){
1186 for (l = ia[k]; l < ia[k+1]; l++){
1187 if (mask[ja[l]] != i){
1201 id = sm->
D->
ia; jd = sm->
D->
ja;
1206 for (i = 0; i < m; i++){
1208 for (j = ia[i]; j < ia[i+1]; j++){
1210 if (mask[k] != i+m){
1213 d[nz] = (avg_dist[i] + avg_dist[k])*0.5;
1219 for (j = ia[i]; j < ia[i+1]; j++){
1221 for (l = ia[k]; l < ia[k+1]; l++){
1222 if (mask[ja[l]] != i+m){
1225 d[nz] = (avg_dist[i] + 2*avg_dist[k] + avg_dist[ja[l]])*0.5;
1226 d[nz] = dd[j]+dd[l];
1235 *(sm->
ctrl) = *ctrl;
1309 for (k = 0; k < 1; k++){
1320 for (k = 0; k < 1; k++){
1332 if (
Verbose) fprintf(stderr,
"post processing %f\n",((
real) (clock() - cpu)) / CLOCKS_PER_SEC);
void StressMajorizationSmoother_delete(StressMajorizationSmoother sm)
SparseMatrix ideal_distance_matrix(SparseMatrix A, int dim, real *x)
spring_electrical_control spring_electrical_control_new()
void spring_electrical_control_delete(spring_electrical_control ctrl)
SpringSmoother SpringSmoother_new(SparseMatrix A, int dim, spring_electrical_control ctrl, real *x)
SparseMatrix SparseMatrix_copy(SparseMatrix A)
SparseMatrix SparseMatrix_new(int m, int n, int nz, int type, int format)
void(* data_deallocator)(void *)
void SpringSmoother_smooth(SpringSmoother sm, SparseMatrix A, real *node_weights, int dim, real *x)
real distance(real *x, int dim, int i, int j)
void SparseMatrix_multiply_dense(SparseMatrix A, int ATransposed, real *v, int vTransposed, real **res, int res_transposed, int dim)
StressMajorizationSmoother SparseStressMajorizationSmoother_new(SparseMatrix A, int dim, real lambda0, real *x, int weighting_scheme, int scale_initial_coord)
int SparseMatrix_is_symmetric(SparseMatrix A, int test_pattern_symmetry_only)
#define TriangleSmoother_struct
void spring_electrical_spring_embedding(int dim, SparseMatrix A0, SparseMatrix D, spring_electrical_control ctrl, real *node_weights, real *x, int *flag)
void SpringSmoother_delete(SpringSmoother sm)
real get_stress(int m, int dim, int *iw, int *jw, real *w, real *d, real *x, real scaling, void *data, int weighted)
void SparseStressMajorizationSmoother_delete(SparseStressMajorizationSmoother sm)
StressMajorizationSmoother StressMajorizationSmoother2_new(SparseMatrix A, int dim, real lambda0, real *x, int ideal_dist_scheme)
int SparseMatrix_distance_matrix(SparseMatrix D0, int weighted, real **dist0)
real StressMajorizationSmoother_smooth(StressMajorizationSmoother sm, int dim, real *x, int maxit_sm, real tol)
real vector_product(int n, real *x, real *y)
SparseMatrix SparseMatrix_symmetrize(SparseMatrix A, int pattern_symmetric_only)
TriangleSmoother TriangleSmoother_new(SparseMatrix A, int dim, real lambda0, real *x, int use_triangularization)
Operator Operator_uniform_stress_diag_precon_new(SparseMatrix A, real alpha)
spring_electrical_control ctrl
void vector_ordering(int n, real *v, int **p, int ascending)
void SparseMatrix_delete(SparseMatrix A)
real SparseStressMajorizationSmoother_smooth(SparseStressMajorizationSmoother sm, int dim, real *x, int maxit_sm, real tol)
if(aagss+aagstacksize-1<=aagssp)
SparseMatrix call_tri(int n, int dim, real *x)
void post_process_smoothing(int dim, SparseMatrix A, spring_electrical_control ctrl, real *node_weights, real *x, int *flag)
void TriangleSmoother_delete(TriangleSmoother sm)
real cg(Operator Ax, Operator precond, int n, int dim, real *x0, real *rhs, real tol, int maxit, int *flag)
real get_full_stress(SparseMatrix A, int dim, real *x, int weighting_scheme)
void TriangleSmoother_smooth(TriangleSmoother sm, int dim, real *x)
SparseMatrix SparseMatrix_from_coordinate_arrays(int nz, int m, int n, int *irn, int *jcn, void *val0, int type, size_t sz)
real SparseMatrix_solve(SparseMatrix A, int dim, real *x0, real *rhs, real tol, int maxit, int method, int *flag)
EXTERN unsigned char Verbose
Operator Operator_uniform_stress_matmul(SparseMatrix A, real alpha)
real vector_median(int n, real *x)
real vector_percentile(int n, real *x, real y)
void dump_distance_edge_length(char *outfile, SparseMatrix A, int dim, real *x)
SparseMatrix call_tri2(int n, int dim, real *xx)
double dist(Site *s, Site *t)
real distance_cropped(real *x, int dim, int i, int j)
SparseMatrix SparseMatrix_add(SparseMatrix A, SparseMatrix B)