Skip to content

Commit

Permalink
working on dist matrix ordering, not quite ready yet
Browse files Browse the repository at this point in the history
  • Loading branch information
jdfekete committed Apr 21, 2015
1 parent 543f721 commit 508c09b
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 5 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ reorder.v1.js: \
src/count_crossings.js \
src/barycenter.js \
src/all_pairs_distance.js \
src/graph2distmat.js \
src/dist.js \
src/random.js \
src/permute.js \
Expand Down
19 changes: 17 additions & 2 deletions examples/matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ function matrix(json) {

var graph = reorder.graph()
.nodes(json.nodes)
.links(json.links).init();
.links(json.links)
.init();

var dist_adjacency;

// Precompute the orders.
var orders = {
Expand All @@ -59,6 +62,19 @@ function matrix(json) {
});
return nodes.map(function(n) { return n.leafOrder; });
},
leafOrderDist: function() {
if (! dist_adjacency)
dist_adjacency = reorder.graph2distmat(graph);

var leafOrder = reorder.leafOrder()
.distance(science.stats.distance.manhattan)(dist_adjacency);

leafOrder.forEach(function(lo, i) {
nodes[i].leafOrderDist = lo;
});
return nodes.map(function(n) { return n.leafOrderDist; });

},
barycenter: function() {
var barycenter = reorder.barycenter(graph);

Expand All @@ -74,7 +90,6 @@ function matrix(json) {
nodes[i].rcm = lo;
});


return nodes.map(function(n) { return n.rcm; });
}
};
Expand Down
1 change: 1 addition & 0 deletions examples/miserables/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ <h1><i>Les Misérables</i> Co-occurrence</h1>
<option value="count">by Frequency</option>
<option value="group">by Cluster</option>
<option value="leafOrder">by Leaf Order</option>
<option value="leafOrderDist">by Leaf Order over Distance Matrix</option>
<option value="barycenter">by Crossing Reduction</option>
<option value="rcm">by Bandwidth Reduction (RCM)</option>
</select>
Expand Down
2 changes: 1 addition & 1 deletion lib/tiny-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ Queue.prototype.slice = function (start, end) {
return output;
}

module.exports = Queue;

66 changes: 66 additions & 0 deletions reorder.v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ reorder.infinities = function(n) {
this, Array.prototype.slice.call(arguments, 1));
return a;
};

reorder.array1d = function(n, v) {
var i = -1,
a = Array(n);
while (++i < n)
a[i] = v;
return a;
};
reorder.dot = science.lin.dot;
reorder.length = science.lin.length;
reorder.normalize = science.lin.normalize;
Expand Down Expand Up @@ -1072,6 +1080,64 @@ reorder.floyd_warshall_path = function(next, u, v) {
}
return path;
};
// Converts a graph with weighted edges (weight in l.value)
// into a distance matrix suitable for reordering with e.g.
// Optimal Leaf Ordering.
reorder.graph2distmat = function(graph, directed) {
// Transforms the weights into a distance so take the max
var max_link = graph.links()
.reduce(function(a, b) {
if (b.value > a.value)
return b;
return a;
}),
// we don't want a distance of 0 so we add a relative margin
max_value = max_link.value*1.05,
// Transform link values into a normalized distance
links = graph.links()
.map(function(l) {
return {
value: (max_value - l.value)/max_value,
source: l.source.index,
target: l.target.index
};
}),
// use the origin node as id
nodes = graph.nodes()
.map(function(n) { return {id: n}; }),
// Create the graph structure
graph2 = reorder.graph()
.nodes(nodes)
.links(links)
.init(),
// compute the all_pairs distances for all the
// components
dists = reorder.all_pairs_distance(graph2),
// Compute the graph diameter as max distance
max_dist = dists.reduce(function(a, b) {
return Matm.max(a, reorder.distmax(b));
}),
// Infinity will be set to 2*the max distance
inf = 2*max_dist,
n = graph.nodes().length,
dist = Array(n),
i, j, k, d, start = 0;

dist[0] = reorder.array1d(n, inf);
for (i = 1; i < n; i++)
dist[i] = dist[0].slice();
for (k = 0; k < dists.length; k++) {
d = dists[k];
for (i = 0; i < d.length; i++) {
for (j = 0; j < dist.length; j++) {
dist[start+i][start+j] = d[i][j];
dist[start+j][start+i] = d[j][i];
}
}
start += d.length;
}
return dist;
};
reorder.distmax = function (distMatrix) {
var max = 0,
n=distMatrix.length,
Expand Down
4 changes: 2 additions & 2 deletions reorder.v1.min.js

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@ reorder.infinities = function(n) {
this, Array.prototype.slice.call(arguments, 1));
return a;
};

reorder.array1d = function(n, v) {
var i = -1,
a = Array(n);
while (++i < n)
a[i] = v;
return a;
};

0 comments on commit 508c09b

Please sign in to comment.