diff --git a/Makefile b/Makefile index 555cb83..e180915 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ NODE_PATH = ./node_modules JS_COMPILER = $(NODE_PATH)/uglify-js/bin/uglifyjs +#JS_TESTER = $(NODE_PATH)/vows/bin/vows --nocolor -v all: \ reorder.v1.js \ diff --git a/TODO b/TODO index 7178c0f..43fb046 100644 --- a/TODO +++ b/TODO @@ -14,5 +14,12 @@ Reordering: Correspondence analysis Chun-Hou Chen methods: http://gap.stat.sinica.edu.tw/Papers/GAP_2002.pdf Elliptical - Barycenter method: http://www.informatica.si/PDF/29-3/13_Makinen-The%20Barycenter%20Heuristic....pdf - \ No newline at end of file + +Reordering done: + Barycenter method: + http://www.informatica.si/PDF/29-3/13_Makinen-The%20Barycenter%20Heuristic....pdf + Optimal Leaf Ordering + Ziv Bar-Joseph, Erik D. Demaine, David K. Gifford, Angèle M. Hamel, Tommy S. Jaakkola and Nathan Srebro + K-ary Clustering with Optimal Leaf Ordering for Gene Expression Data. + Bioinformatics, 19(9), pp 1070-8, 2003 + http://www.cs.cmu.edu/~zivbj/compBio/k-aryBio.pdf diff --git a/reorder.v1.js b/reorder.v1.js index c74be39..416448b 100644 --- a/reorder.v1.js +++ b/reorder.v1.js @@ -1,5 +1,5 @@ (function(exports){ -reorder = {version: "0.0.2"}; // semver +reorder = {version: "0.0.3"}; // semver reorder.dot = science.lin.dot; reorder.length = science.lin.length; @@ -644,6 +644,16 @@ function count_crossings(graph, north, south) { firstIndex, treeSize, tree, index, weightSum, invert = false, crosscount; + if (north==undefined) { + var comp = reorder.permutation(graph.nodes().length); + north = comp.filter(function(n) { + return graph.outEdges(n).length!=0; + }), + south = comp.filter(function(n) { + return graph.inEdges(n).length!=0; + }); + } + // Choose the smaller axis if (north.length < south.length) { var tmp = north; @@ -693,14 +703,18 @@ function count_crossings(graph, north, south) { } reorder.count_crossings = count_crossings; reorder.barycenter = function(graph, iter, comps) { - var perm = []; + var perms = [[], [], 0]; // Compute the barycenter heuristic on each connected component if (! comps) { comps = graph.components(); } - for (var i = 0; i < comps.length; i++) - perm = perm.concat(reorder.barycenter1(graph, comps[i], iter)); - return perm; + for (var i = 0; i < comps.length; i++) { + var p = reorder.barycenter1(graph, comps[i], iter); + perms = [ perms[0].concat(p[0]), + perms[1].concat(p[1]), + perms[2]+p[2] ]; + } + return perms; }; // Take the list of neighbor indexes and return the median according to @@ -733,8 +747,16 @@ reorder.barycenter1 = function(graph, comp, max_iter) { layer, i, v, neighbors; - if (comp.length < 3) - return comp; + layer1 = comp.filter(function(n) { + return graph.outEdges(n).length!=0; + }); + layer2 = comp.filter(function(n) { + return graph.inEdges(n).length!=0; + }); + if (comp.length < 3) { + return [layer1, layer2, + count_crossings(graph, layer1, layer2)]; + } if (! max_iter) max_iter = 24; @@ -786,7 +808,7 @@ reorder.barycenter1 = function(graph, comp, max_iter) { else if (d > 0) return 1; return 0; }); - for (i = 0; i < layer2.length; i++) + for (i = 0; i < layer.length; i++) nodes[layer[i]].pos = i; crossings = count_crossings(graph, layer1, layer2); if (crossings < best_crossings) { @@ -796,7 +818,7 @@ reorder.barycenter1 = function(graph, comp, max_iter) { best_iter = iter; } } - console.log('Best iter: '+best_iter); + //console.log('Best iter: '+best_iter); return [best_layer1, best_layer2, best_crossings]; }; reorder.dijkstra = function(graph) { diff --git a/reorder.v1.min.js b/reorder.v1.min.js index 71c200b..534f5fe 100644 --- a/reorder.v1.min.js +++ b/reorder.v1.min.js @@ -1 +1 @@ -(function(exports){reorder={version:"0.0.2"};reorder.dot=science.lin.dot;reorder.length=science.lin.length;reorder.normalize=science.lin.normalize;reorder.printmat=function(m){var i,j,row,line;for(i=0;i1)s+=v[i];return s};function isNum(a,b){return!(isNaN(a)||isNaN(b)||a==Infinity||b==Infinity)}reorder.distance={euclidean:function(a,b){var i=a.length,s=0,x;while(i-->0){if(isNum(a[i],b[i])){x=a[i]-b[i];s+=x*x}}return Math.sqrt(s)},manhattan:function(a,b){var i=a.length,s=0;while(i-->0){if(isNum(a[i],b[i])){s+=Math.abs(a[i]-b[i])}}return s},minkowski:function(p){return function(a,b){var i=a.length,s=0;while(i-->0){if(isNum(a[i],b[i])){s+=Math.pow(Math.abs(a[i]-b[i]),p)}}return Math.pow(s,1/p)}},chebyshev:function(a,b){var i=a.length,max=0,x;while(i-->0){if(isNum(a[i],b[i])){x=Math.abs(a[i]-b[i]);if(x>max)max=x}}return max},hamming:function(a,b){var i=a.length,d=0;while(i-->0){if(isNum(a[i],b[i])){if(a[i]!==b[i])d++}}return d},jaccard:function(a,b){var n=0,i=a.length,s=0;while(i-->0){if(isNum(a[i],b[i])){if(a[i]===b[i])s++;n++}}if(n==0)return 0;return s/n},braycurtis:function(a,b){var i=a.length,s0=0,s1=0,ai,bi;while(i-->0){ai=a[i];bi=b[i];if(isNum(ai,bi)){s0+=Math.abs(ai-bi);s1+=Math.abs(ai+bi)}}if(s1==0)return 0;return s0/s1}};reorder.range=function(start,stop,step){if(arguments.length<3){step=1;if(arguments.length<2){stop=start;start=0}}var range=[],i=start;if(step<0)for(;i>stop;i+=step)range.push(i);else for(;i1)percolateDown(0);delete index[top];return top};function lChild(i){return i*2+1}function rChild(i){return i*2+2}function parent(i){return Math.floor((i-1)/2)}function swap(i,j){var io=heap[i],jo=heap[j];heap[i]=jo;index[jo]=i;heap[j]=io;index[io]=j}function update(o){var i=percolateUp(index[o],o);percolateDown(i)}function percolateDown(cur){while(true){var left=lChild(cur),right=rChild(cur);var smallest;if(left0&&comp(heap[par],o)>0;par=parent(par)){var p=heap[par];heap[i]=p;index[p]=i;i=par}heap[i]=o;index[o]=i;return i}return Heap};reorder.permutation=reorder.range;function inverse_permutation(perm){var inv={};for(var i=0;i=0;i--){if(graph.inEdges(i).length!=0)break;else rows--}for(i=n-1;i>=0;i--){if(graph.outEdges(i).length!=0)break;else cols--}mat=science.zeroes(rows,cols);for(i=0;i0){if(index%2)crosscount+=tree[index+1];index=index-1>>1;tree[index]++}}return crosscount}reorder.count_crossings=count_crossings;reorder.barycenter=function(graph,iter,comps){var perm=[];if(!comps){comps=graph.components()}for(var i=0;i0)return 1;return 0});for(i=0;id){D.weight=d;D.edge=e;queue.update(D.vertex)}}}return queued}dijkstra.shortestPath=function(from,to){var map=allShortestPaths(from),path,v;v=map[to];path=[v];while(v.edge!=-1){v=map[graph.other(v.edge,v.vertex).index];path.unshift(v)}return path};return dijkstra};reorder.distmax=function(distMatrix){var max=0,n=distMatrix.length,i,j,row;for(i=0;imax)max=row[j]}return max};reorder.distmin=function(distMatrix){var min=Infinity,n=distMatrix.length,i,j,row;for(i=0;i0;)dist[i].splice(n,m-n);return dist};reorder.randomPermute=function(array,i,j){if(arguments.length<3){j=array.length;if(arguments.length<2){i=0}}var m=j-i,t,k;while(m>0){k=i+Math.floor(Math.random()*m--);t=array[i+m];array[i+m]=array[k];array[k]=t}return array};reorder.randomPermutation=function(n){return reorder.randomPermute(reorder.permutation(n))};reorder.randomMatrix=function(p,n,m,sym){if(!m)m=n;if(n!=m)sym=false;else if(!sym)sym=true;var mat=science.zeroes(n,m),i,j,cnt;if(sym){for(i=0;i0)array[m]=reorder.permute(array[m],indexes);return array};reorder.stablepermute=function(list,indexes){var p=reorder.permute(list,indexes);if(p[0]>p[p.length-1])p.reverse();return p};if(typeof science=="undefined"){science={version:"1.9.1"};science.stats={}}science.stats.hcluster=function(){var distance=reorder.distance.euclidean,linkage="single",distMatrix=null;function hcluster(vectors){var n=vectors.length,dMin=[],cSize=[],clusters=[],c1,c2,c1Cluster,c2Cluster,p,root,i,j,id=0;if(distMatrix==null){distMatrix=[];i=-1;while(++idistMatrix[i][j])dMin[i]=j}}}else{if(distMatrix.lengthdistMatrix[i][j])dMin[i]=j}}}i=-1;while(++idistMatrix[c2][j])distMatrix[j][c1]=distMatrix[c1][j]=distMatrix[c2][j];break;case"complete":if(distMatrix[c1][j]0?i-1:0,j0=j0;k-=2){low=except[k-1];high=except[k];if(high>=j0){if(j0>j){j0=Math.min(j0,low+1);except.splice(k-1,2)}else{high=j0}}else if(low<=i0){if(i0j){if(perm[perm.length-1]!=perm.length-1)perm=perm.reverse();reorder.assert(perm[perm.length-1]==perm.length-1,"Invalid constrained permutation end")}if(i0!=0){perm=reorder.permutation(i0).concat(perm.map(function(v){return v+i0}))}if(orig.length>j0){perm=perm.concat(reorder.range(j0,orig.length))}return perm}function _order_except(){var perm,k,l,low,high,pos;if(except.length==0)return _order_equiv();_compute_dist();for(k=except.length-1;k>0;k-=2){low=except[k-1];high=except[k];distanceMatrix=reorder.dist_remove(distanceMatrix,low+1,high-1);vector.splice(low+1,high-low-2);if(debug)console.log("Except["+low+", "+high+"]");if(distanceMatrix[low][low+1]!=0){distanceMatrix[low][low+1]=distanceMatrix[low+1][low]=-1}}perm=_order_equiv();for(k=0;klow)perm[l]+=high-low-2;else if(perm[l]==low)pos=l}if(pos>0&&perm[pos-1]==high-1){Array.prototype.splice.apply(perm,[pos,0].concat(reorder.range(high-2,low,-1)))}else if(perm[pos+1]==high-1){Array.prototype.splice.apply(perm,[pos+1,0].concat(reorder.range(low+1,high-1)))}else{throw"Range not respected"}}return perm}function _order_equiv(){var perm,row,e,j,k,l,m,n,has_1=false,equiv=[],fix_except={};_compute_dist();for(k=0;kk;){if(row[l]==0){j=distanceMatrix[l].indexOf(-1);if(j!=-1){fix_except[k]=[l,j];distanceMatrix[j][k]=row[j]=-1;has_1=true}e.unshift(l);distanceMatrix=reorder.dist_remove(distanceMatrix,l);vector.splice(l,1)}else if(row[l]<0)has_1=true}if(e.length!=0){e.unshift(k);equiv.push(e)}}if(has_1){for(k=0;k0;){e=equiv[k];l=perm.indexOf(e[0]);m=fix_except[e[0]];if(m&&m[0]==e[0]){l=_fix_exception(perm,l,m[0],m[1],0);m=undefined}for(n=1;n0&&perm[l-1]==next){_swap(perm,l,perm.indexOf(m));return l+1}else if(perm[l+len+1]==next){_swap(perm,l+len,perm.indexOf(m));return l}else throw"Index not found"}function _swap(perm,a,b){if(a==b)return;var c=perm[a];perm[a]=perm[b];perm[b]=c}function _order(){if(debug>1)reorder.printmat(distanceMatrix);if(debug>2)reorder.printmat(vector);var perm=ordering().debug(debug).linkage(linkage).distanceMatrix(distanceMatrix)(vector);if(debug)console.log("Permutation: "+perm);return perm}function _perm_insert(perm,i,nv){perm=perm.map(function(v){return v=b)throw"Invalid list, indices not sorted";return a-b});return order};function _orderExcept(vector,i,j){var distanceMatrix=reorder.dist().distance(distance)(vector);var row,k,l,rev=false,args,pos=-1;distanceMatrix[i][i+1]=0;distanceMatrix[i+1][i]=0;var perm=ordering().distanceMatrix(distanceMatrix)(vector);pos=perm.indexOf(i);for(k=0;ki)perm[k]+=j-i-2}if(pos!=0&&perm[pos-1]==j-1)rev=true;if(rev){perm.reverse();pos=perm.length-pos-1}args=[pos+1,0].concat(reorder.range(i+1,j-1));Array.prototype.splice.apply(perm,args);return perm}order.orderrowsexcept=order.orderexcept;return order};reorder.covariance=science.lin.dot;reorder.covariancetranspose=function(v,a,b){var n=v.length,cov=0,i;for(i=0;i0)v[i]/=norm;return v}reorder.poweriteration=function(v,eps){if(arguments.length<2)eps=1e-4;var n=v.length,b=Array(n),i,j,tmp=Array(n),norm,s=10;reorder.assert(n==v[0].length,"poweriteration needs a square matrix");for(i=0;i0){for(i=0;i1)s+=v[i];return s};function isNum(a,b){return!(isNaN(a)||isNaN(b)||a==Infinity||b==Infinity)}reorder.distance={euclidean:function(a,b){var i=a.length,s=0,x;while(i-->0){if(isNum(a[i],b[i])){x=a[i]-b[i];s+=x*x}}return Math.sqrt(s)},manhattan:function(a,b){var i=a.length,s=0;while(i-->0){if(isNum(a[i],b[i])){s+=Math.abs(a[i]-b[i])}}return s},minkowski:function(p){return function(a,b){var i=a.length,s=0;while(i-->0){if(isNum(a[i],b[i])){s+=Math.pow(Math.abs(a[i]-b[i]),p)}}return Math.pow(s,1/p)}},chebyshev:function(a,b){var i=a.length,max=0,x;while(i-->0){if(isNum(a[i],b[i])){x=Math.abs(a[i]-b[i]);if(x>max)max=x}}return max},hamming:function(a,b){var i=a.length,d=0;while(i-->0){if(isNum(a[i],b[i])){if(a[i]!==b[i])d++}}return d},jaccard:function(a,b){var n=0,i=a.length,s=0;while(i-->0){if(isNum(a[i],b[i])){if(a[i]===b[i])s++;n++}}if(n==0)return 0;return s/n},braycurtis:function(a,b){var i=a.length,s0=0,s1=0,ai,bi;while(i-->0){ai=a[i];bi=b[i];if(isNum(ai,bi)){s0+=Math.abs(ai-bi);s1+=Math.abs(ai+bi)}}if(s1==0)return 0;return s0/s1}};reorder.range=function(start,stop,step){if(arguments.length<3){step=1;if(arguments.length<2){stop=start;start=0}}var range=[],i=start;if(step<0)for(;i>stop;i+=step)range.push(i);else for(;i1)percolateDown(0);delete index[top];return top};function lChild(i){return i*2+1}function rChild(i){return i*2+2}function parent(i){return Math.floor((i-1)/2)}function swap(i,j){var io=heap[i],jo=heap[j];heap[i]=jo;index[jo]=i;heap[j]=io;index[io]=j}function update(o){var i=percolateUp(index[o],o);percolateDown(i)}function percolateDown(cur){while(true){var left=lChild(cur),right=rChild(cur);var smallest;if(left0&&comp(heap[par],o)>0;par=parent(par)){var p=heap[par];heap[i]=p;index[p]=i;i=par}heap[i]=o;index[o]=i;return i}return Heap};reorder.permutation=reorder.range;function inverse_permutation(perm){var inv={};for(var i=0;i=0;i--){if(graph.inEdges(i).length!=0)break;else rows--}for(i=n-1;i>=0;i--){if(graph.outEdges(i).length!=0)break;else cols--}mat=science.zeroes(rows,cols);for(i=0;i0){if(index%2)crosscount+=tree[index+1];index=index-1>>1;tree[index]++}}return crosscount}reorder.count_crossings=count_crossings;reorder.barycenter=function(graph,iter,comps){var perms=[[],[],0];if(!comps){comps=graph.components()}for(var i=0;i0)return 1;return 0});for(i=0;id){D.weight=d;D.edge=e;queue.update(D.vertex)}}}return queued}dijkstra.shortestPath=function(from,to){var map=allShortestPaths(from),path,v;v=map[to];path=[v];while(v.edge!=-1){v=map[graph.other(v.edge,v.vertex).index];path.unshift(v)}return path};return dijkstra};reorder.distmax=function(distMatrix){var max=0,n=distMatrix.length,i,j,row;for(i=0;imax)max=row[j]}return max};reorder.distmin=function(distMatrix){var min=Infinity,n=distMatrix.length,i,j,row;for(i=0;i0;)dist[i].splice(n,m-n);return dist};reorder.randomPermute=function(array,i,j){if(arguments.length<3){j=array.length;if(arguments.length<2){i=0}}var m=j-i,t,k;while(m>0){k=i+Math.floor(Math.random()*m--);t=array[i+m];array[i+m]=array[k];array[k]=t}return array};reorder.randomPermutation=function(n){return reorder.randomPermute(reorder.permutation(n))};reorder.randomMatrix=function(p,n,m,sym){if(!m)m=n;if(n!=m)sym=false;else if(!sym)sym=true;var mat=science.zeroes(n,m),i,j,cnt;if(sym){for(i=0;i0)array[m]=reorder.permute(array[m],indexes);return array};reorder.stablepermute=function(list,indexes){var p=reorder.permute(list,indexes);if(p[0]>p[p.length-1])p.reverse();return p};if(typeof science=="undefined"){science={version:"1.9.1"};science.stats={}}science.stats.hcluster=function(){var distance=reorder.distance.euclidean,linkage="single",distMatrix=null;function hcluster(vectors){var n=vectors.length,dMin=[],cSize=[],clusters=[],c1,c2,c1Cluster,c2Cluster,p,root,i,j,id=0;if(distMatrix==null){distMatrix=[];i=-1;while(++idistMatrix[i][j])dMin[i]=j}}}else{if(distMatrix.lengthdistMatrix[i][j])dMin[i]=j}}}i=-1;while(++idistMatrix[c2][j])distMatrix[j][c1]=distMatrix[c1][j]=distMatrix[c2][j];break;case"complete":if(distMatrix[c1][j]0?i-1:0,j0=j0;k-=2){low=except[k-1];high=except[k];if(high>=j0){if(j0>j){j0=Math.min(j0,low+1);except.splice(k-1,2)}else{high=j0}}else if(low<=i0){if(i0j){if(perm[perm.length-1]!=perm.length-1)perm=perm.reverse();reorder.assert(perm[perm.length-1]==perm.length-1,"Invalid constrained permutation end")}if(i0!=0){perm=reorder.permutation(i0).concat(perm.map(function(v){return v+i0}))}if(orig.length>j0){perm=perm.concat(reorder.range(j0,orig.length))}return perm}function _order_except(){var perm,k,l,low,high,pos;if(except.length==0)return _order_equiv();_compute_dist();for(k=except.length-1;k>0;k-=2){low=except[k-1];high=except[k];distanceMatrix=reorder.dist_remove(distanceMatrix,low+1,high-1);vector.splice(low+1,high-low-2);if(debug)console.log("Except["+low+", "+high+"]");if(distanceMatrix[low][low+1]!=0){distanceMatrix[low][low+1]=distanceMatrix[low+1][low]=-1}}perm=_order_equiv();for(k=0;klow)perm[l]+=high-low-2;else if(perm[l]==low)pos=l}if(pos>0&&perm[pos-1]==high-1){Array.prototype.splice.apply(perm,[pos,0].concat(reorder.range(high-2,low,-1)))}else if(perm[pos+1]==high-1){Array.prototype.splice.apply(perm,[pos+1,0].concat(reorder.range(low+1,high-1)))}else{throw"Range not respected"}}return perm}function _order_equiv(){var perm,row,e,j,k,l,m,n,has_1=false,equiv=[],fix_except={};_compute_dist();for(k=0;kk;){if(row[l]==0){j=distanceMatrix[l].indexOf(-1);if(j!=-1){fix_except[k]=[l,j];distanceMatrix[j][k]=row[j]=-1;has_1=true}e.unshift(l);distanceMatrix=reorder.dist_remove(distanceMatrix,l);vector.splice(l,1)}else if(row[l]<0)has_1=true}if(e.length!=0){e.unshift(k);equiv.push(e)}}if(has_1){for(k=0;k0;){e=equiv[k];l=perm.indexOf(e[0]);m=fix_except[e[0]];if(m&&m[0]==e[0]){l=_fix_exception(perm,l,m[0],m[1],0);m=undefined}for(n=1;n0&&perm[l-1]==next){_swap(perm,l,perm.indexOf(m));return l+1}else if(perm[l+len+1]==next){_swap(perm,l+len,perm.indexOf(m));return l}else throw"Index not found"}function _swap(perm,a,b){if(a==b)return;var c=perm[a];perm[a]=perm[b];perm[b]=c}function _order(){if(debug>1)reorder.printmat(distanceMatrix);if(debug>2)reorder.printmat(vector);var perm=ordering().debug(debug).linkage(linkage).distanceMatrix(distanceMatrix)(vector);if(debug)console.log("Permutation: "+perm);return perm}function _perm_insert(perm,i,nv){perm=perm.map(function(v){return v=b)throw"Invalid list, indices not sorted";return a-b});return order};function _orderExcept(vector,i,j){var distanceMatrix=reorder.dist().distance(distance)(vector);var row,k,l,rev=false,args,pos=-1;distanceMatrix[i][i+1]=0;distanceMatrix[i+1][i]=0;var perm=ordering().distanceMatrix(distanceMatrix)(vector);pos=perm.indexOf(i);for(k=0;ki)perm[k]+=j-i-2}if(pos!=0&&perm[pos-1]==j-1)rev=true;if(rev){perm.reverse();pos=perm.length-pos-1}args=[pos+1,0].concat(reorder.range(i+1,j-1));Array.prototype.splice.apply(perm,args);return perm}order.orderrowsexcept=order.orderexcept;return order};reorder.covariance=science.lin.dot;reorder.covariancetranspose=function(v,a,b){var n=v.length,cov=0,i;for(i=0;i0)v[i]/=norm;return v}reorder.poweriteration=function(v,eps){if(arguments.length<2)eps=1e-4;var n=v.length,b=Array(n),i,j,tmp=Array(n),norm,s=10;reorder.assert(n==v[0].length,"poweriteration needs a square matrix");for(i=0;i0){for(i=0;i 0) return 1; return 0; }); - for (i = 0; i < layer2.length; i++) + for (i = 0; i < layer.length; i++) nodes[layer[i]].pos = i; crossings = count_crossings(graph, layer1, layer2); if (crossings < best_crossings) { @@ -102,6 +114,6 @@ reorder.barycenter1 = function(graph, comp, max_iter) { best_iter = iter; } } - console.log('Best iter: '+best_iter); + //console.log('Best iter: '+best_iter); return [best_layer1, best_layer2, best_crossings]; }; diff --git a/src/core.js b/src/core.js index 95d345b..51892c4 100644 --- a/src/core.js +++ b/src/core.js @@ -1,2 +1,2 @@ -reorder = {version: "0.0.2"}; // semver +reorder = {version: "0.0.3"}; // semver diff --git a/src/count_crossings.js b/src/count_crossings.js index 6d3cd86..196d8ae 100644 --- a/src/count_crossings.js +++ b/src/count_crossings.js @@ -6,6 +6,16 @@ function count_crossings(graph, north, south) { firstIndex, treeSize, tree, index, weightSum, invert = false, crosscount; + if (north==undefined) { + var comp = reorder.permutation(graph.nodes().length); + north = comp.filter(function(n) { + return graph.outEdges(n).length!=0; + }), + south = comp.filter(function(n) { + return graph.inEdges(n).length!=0; + }); + } + // Choose the smaller axis if (north.length < south.length) { var tmp = north; diff --git a/test/barycenter-test.js b/test/barycenter-test.js index 65c001a..8d1a872 100644 --- a/test/barycenter-test.js +++ b/test/barycenter-test.js @@ -7,6 +7,17 @@ var vows = require("vows"), var suite = vows.describe("reorder.barycenter"); +function dotest(mat) { + var graph = reorder.mat2graph(mat, true); + //reorder.printmat(mat); + var initial_crossings = reorder.count_crossings(graph); + var perms = reorder.barycenter(graph); + //console.log('VOrder: %j, HOrder: %j, Crossings: %d', + //perms[1], perms[0], perms[2]); + //reorder.printmat(mat, perms[1], perms[0]); + assert.isTrue(initial_crossings > perms[2]); +} + suite.addBatch({ "barycenter": { "simple": function() { @@ -14,15 +25,16 @@ suite.addBatch({ [0, 1, 0, 1, 0], [1, 0, 1, 0, 1], [0, 1, 0, 1, 1], - [1, 1, 1, 0, 0]], - graph = reorder.mat2graph(mat, true), - expect = [0, 1, 3, 2, 4]; - reorder.printmat(mat); - perm = reorder.barycenter(graph); - console.log('VOrder: %j, HOrder: %j, Crossings: %d', - perm[1], perm[0], perm[2]); - reorder.printmat(mat, perm[1], perm[0]); - //assert.deepEqual(perm, expect); + [1, 1, 1, 0, 0]]; + dotest(mat); + }, + "hard": function() { + for (var i = 10; i < 100; i += 20) { + for (var j = 10; j < 100; j += 20) { + var mat = reorder.randomMatrix(0.2, i, j, false); + dotest(mat); + } + } } } }); diff --git a/test/ca-test.js b/test/ca-test.js index 2b8e78c..24b458e 100644 --- a/test/ca-test.js +++ b/test/ca-test.js @@ -9,6 +9,19 @@ Math.seedrandom('reorder'); var suite = vows.describe("reorder.ca"); +function inDeltaArray(actual, expected, delta) { + var n = expected.length, i = -1; + if (actual.length !== n) return false; + while (++i < n) if (!inDeltaNumber(actual[i], expected[i], delta)) return false; + return true; +} + +function inDeltaNumber(actual, expected, delta) { + var d = Math.abs(actual-expected); + //console.log("d="+d+": "+((d < delta) ? "pass" : "fail")); + return d < delta; +} + suite.addBatch({ "ca": { "simple": function() { @@ -20,7 +33,10 @@ suite.addBatch({ res = reorder.ca(mat, 0.0001); //assert.isTrue(true); - assert.inDeltaArray(res, [-0.5354605, -0.2626774, 0.8026722], 0.001); + if (!inDeltaArray(res, [-0.5354605, -0.2626774, 0.8026722], 0.001) + && !inDeltaArray(res, [0.5354605, 0.2626774, -0.8026722], 0.001)) { + assert.fail(actual, expected, message || "expected {actual} to be in within *" + delta + "* of {expected}", null, assert.inDelta); + } } } }); diff --git a/test/count_crossings-test.js b/test/count_crossings-test.js index a38666b..bc3e94c 100644 --- a/test/count_crossings-test.js +++ b/test/count_crossings-test.js @@ -34,7 +34,7 @@ function naive_count_crossings(graph, north, south) { return count; } -function dohard(mat) { +function dotest(mat) { var graph = reorder.mat2graph(mat, true), comps = graph.components(), comp = comps.reduce(function(a, b) { @@ -95,7 +95,7 @@ suite.addBatch({ 12); }, "bug": function() { - dohard([ + dotest([ [0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0], [0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], @@ -132,7 +132,7 @@ suite.addBatch({ for (var i = 10; i < 100; i += 20) { for (var j = 10; j < 100; j += 20) { var mat = reorder.randomMatrix(0.2, i, j, false); - dohard(mat); + dotest(mat); } } } diff --git a/test/graph2mat-test.js b/test/graph2mat-test.js index f3d4db3..90b93e3 100644 --- a/test/graph2mat-test.js +++ b/test/graph2mat-test.js @@ -9,6 +9,29 @@ Math.seedrandom('reorder'); var suite = vows.describe("reorder.graph2mat"); +function remove_zeroes(mat) { + var i, j; + for (i = mat.length-1; i >= 0; i--) { + var row = mat[i]; + if (row.some(function(a) { return a > 0; })) { + //console.log('remove row %d', i); + break; + } + mat.pop(); + } + for (j = mat[0].length-1; j >= 0; j--) { + if (mat.some(function(row) { + return row[j] != 0; + })) { + //console.log('remove column %d', j); + break; + } + for (i = 0; i < mat.length; i++) + mat[i].pop(); + } + + return mat; +} suite.addBatch({ "graph2mat": { @@ -33,7 +56,7 @@ suite.addBatch({ "hard": function() { for (var i = 10; i < 100; i += 20) { for (var j = 10; j < 100; j += 20) { - var mat = reorder.randomMatrix(0.2, i, j, false), + var mat = remove_zeroes(reorder.randomMatrix(0.2, i, j, false)), graph = reorder.mat2graph(mat, true), m2 = reorder.graph2mat(graph); assert.deepEqual(m2, mat);