Skip to content

Commit

Permalink
Use imports rather than reorder.XYZ
Browse files Browse the repository at this point in the history
  • Loading branch information
curran committed Dec 30, 2020
1 parent 457cb99 commit 6b27627
Show file tree
Hide file tree
Showing 21 changed files with 169 additions and 103 deletions.
5 changes: 3 additions & 2 deletions src/all_pairs_distance.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { infinities } from './utils';
/**
* Returns a list of distance matrices, computed for the specified
* connected components of a graph, or all the components if none is
Expand Down Expand Up @@ -26,7 +27,7 @@ export function all_pairs_distance(graph, comps) {
* nodes in the list of connected components.
*/
export function all_pairs_distance_floyd_warshall(graph, comp) {
var dist = reorder.infinities(comp.length, comp.length),
var dist = infinities(comp.length, comp.length),
i, j, k, inv;
// Floyd Warshall,
// see http://ai-depot.com/BotNavigation/Path-AllPairs.html
Expand Down Expand Up @@ -79,7 +80,7 @@ export function floyd_warshall_with_path(graph, comp) {
if (! comp)
comp = graph.components()[0];

var dist = reorder.infinities(comp.length, comp.length),
var dist = infinities(comp.length, comp.length),
next = Array(comp.length),
directed = graph.directed(),
i, j, k, inv;
Expand Down
4 changes: 3 additions & 1 deletion src/bandwidth.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { range } from './range';

export function bandwidth(graph, order) {
if (! order)
order = reorder.range(graph.nodes().length);
order = range(graph.nodes().length);

var inv = inverse_permutation(order),
links = graph.links(),
Expand Down
9 changes: 6 additions & 3 deletions src/barycenter_order.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { cmp_number } from './utils';
import { debug } from './core';

export function barycenter_order(graph, comps, max_iter) {
var orders = [[], [], 0];
// Compute the barycenter heuristic on each connected component
if (! comps) {
comps = graph.components();
}
for (var i = 0; i < comps.length; i++) {
var o = reorder.barycenter(graph, comps[i], max_iter);
var o = barycenter(graph, comps[i], max_iter);
orders = [ orders[0].concat(o[0]),
orders[1].concat(o[1]),
orders[2]+o[2] ];
Expand All @@ -23,7 +26,7 @@ function median(neighbors) {
return neighbors[0];
if (neighbors.length === 2)
return (neighbors[0]+neighbors[1])/2;
neighbors.sort(reorder.cmp_number);
neighbors.sort(cmp_number);
if (neighbors.length % 2)
return neighbors[(neighbors.length-1)/2];
var rm = neighbors.length/2,
Expand Down Expand Up @@ -110,7 +113,7 @@ export function barycenter(graph, comp, max_iter) {
max_iter = Math.max(max_iter, iter + 2); // we improved so go on
}
}
if (reorder.debug) {
if (debug) {
console.log('Best iter: '+best_iter);
}

Expand Down
10 changes: 6 additions & 4 deletions src/bfs.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { flatten, cmp_number } from './utils';

export function bfs(graph, v, fn) {
var q = new Queue(),
discovered = {}, i, e, v2, edges;
Expand All @@ -24,7 +26,7 @@ export function bfs(graph, v, fn) {
export function bfs_distances(graph, v) {
var dist = {};
dist[v] = 0;
reorder.bfs(graph, v, function(v, c) {
bfs(graph, v, function(v, c) {
if (c >= 0 && v != c)
dist[c] = dist[v]+1;
});
Expand All @@ -34,16 +36,16 @@ export function bfs_distances(graph, v) {
export function all_pairs_distance_bfs(graph, comps) {
if (! comps)
comps = [ graph.nodes_indices() ];
var nodes = comps.reduce(reorder.flatten)
.sort(reorder.cmp_number),
var nodes = comps.reduce(flatten)
.sort(cmp_number),
mat = Array(nodes.length),
i, j, dist;

for (i = 0; i < nodes.length; i++)
mat[i] = Array(nodes.length);

for (i = 0; i < nodes.length; i++) {
dist = reorder.bfs_distances(graph, i);
dist = bfs_distances(graph, i);
for (j in dist) {
mat[i][j] = dist[j];
mat[j][i] = dist[j];
Expand Down
30 changes: 17 additions & 13 deletions src/ca.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { zeroes } from './aliases';
import { array1d } from './utils';
import { debug } from './core';
import { sort_order } from './sort_order';
//Corresponence Analysis
// see http://en.wikipedia.org/wiki/Correspondence_analysis

Expand All @@ -20,7 +24,7 @@ function sumrows(v) {
function sumcols(v) {
var n = v.length,
o = v[0].length,
sumcol = reorder.zeroes(o),
sumcol = zeroes(o),
i, j, row;

for (i = 0; i < n; i++) {
Expand Down Expand Up @@ -49,11 +53,11 @@ function decorana(dat) {

adotj = sumcols(dat);
aidot = sumrows(dat);
//console.log('adotj='); reorder.printvec(adotj);
//console.log('aidot='); reorder.printvec(aidot);
//console.log('adotj='); printvec(adotj);
//console.log('aidot='); printvec(aidot);

s1 = eigy(reorder.array1d(nr, 1.0),
reorder.array1d(nc, 1.0),
s1 = eigy(array1d(nr, 1.0),
array1d(nc, 1.0),
nr, nc, dat, aidot, adotj);
if (s1.eig < ZEROEIG) {
s1.rows = s1.cols = [];
Expand Down Expand Up @@ -139,10 +143,10 @@ function eigy(x, y, mi, n, dat, aidot, adotj) {
a11, a12, a22, a23, a33, a34, a44,
res, ax1, ax2, ax3, ax4,
b13, b14, b24, row,
y2 = reorder.zeroes(n),
y3 = reorder.zeroes(n),
y4 = reorder.zeroes(n),
y5 = reorder.zeroes(n),
y2 = zeroes(n),
y3 = zeroes(n),
y4 = zeroes(n),
y5 = zeroes(n),
tol;

tot = 0.0;
Expand Down Expand Up @@ -269,7 +273,7 @@ function eigy(x, y, mi, n, dat, aidot, adotj) {
}
// 200
//console.log('eigenvalue',a11.toFixed(6));
if (a12 > tol && reorder.debug > 0) {
if (a12 > tol && debug > 0) {
console.log("residual bigger than tolerance on axis 1");
}
var aymax = y[0],
Expand Down Expand Up @@ -366,8 +370,8 @@ export const ca_decorana = decorana;
export const ca = decorana;

export function ca_order(dat) {
var res = reorder.ca(dat);
return { rows: reorder.sort_order(res.rows),
cols: reorder.sort_order(res.cols),
var res = ca(dat);
return { rows: sort_order(res.rows),
cols: sort_order(res.cols),
details: res };
};
10 changes: 6 additions & 4 deletions src/correlation.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { zeroes } from './aliases';

export const correlation = {
pearson: function(a, b) {
var ma = science.stats.mean(a),
Expand All @@ -18,12 +20,12 @@ export const correlation = {
pearsonMatrix: function(matrix) {
var a, ma,
i, j, dx,
cor = reorder.correlation.pearson,
cor = correlation.pearson,
n = matrix.length, ret, mx, sx, sx2;
if (n === 0)
return NaN;
// do it the hard way for now, we'll optimize later
ret = reorder.zeroes(n, n);
ret = zeroes(n, n);
for (i = 0; i < (n-1); i++) {
for (j = i+1; j < n; j++) {
var p = cor(matrix[i], matrix[j]);
Expand All @@ -32,8 +34,8 @@ export const correlation = {
}
return ret;
// mx = Array(n);
// sx = reorder.zeroes(n);
// sx2 = reorder.zeroes(n);
// sx = zeroes(n);
// sx2 = zeroes(n);
// for (i = 0; i < n; i++) {
// mx[i] = science.stats.mean(matrix[i]);
// }
Expand Down
10 changes: 7 additions & 3 deletions src/count_crossings.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { permutation } from './permutation';
import { cmp_number } from './utils';
import { zeroes } from './aliases';

// Wilhelm Barth, Petra Mutzel, Michael Jünger:
// Simple and Efficient Bilayer Cross Counting.
// J. Graph Algorithms Appl. 8(2): 179-194 (2004)
Expand All @@ -7,7 +11,7 @@ export function count_crossings(graph, north, south) {
firstIndex, treeSize, tree, index, weightSum,
invert = false, crosscount;

var comp = reorder.permutation(graph.nodes().length);
var comp = permutation(graph.nodes().length);

if (north===undefined) {
north = comp.filter(function(n) { return graph.outDegree(n) !== 0; });
Expand Down Expand Up @@ -38,7 +42,7 @@ export function count_crossings(graph, north, south) {
return south_inv[e.source.index];
});
}
n.sort(reorder.cmp_number);
n.sort(cmp_number);
southsequence = southsequence.concat(n);
}

Expand All @@ -47,7 +51,7 @@ export function count_crossings(graph, north, south) {
firstIndex <<= 1;
treeSize = 2 * firstIndex - 1;
firstIndex -= 1;
tree = reorder.zeroes(treeSize);
tree = zeroes(treeSize);

crosscount = 0;
for (i = 0; i < southsequence.length; i++) {
Expand Down
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ export { array_to_dicts, dicts_to_array, pcp, parcoords } from './pcp';
export { permutation, inverse_permutation } from './permutation';
export { permute, permute_inplace, permutetranspose } from './permute';
export { poweriteration, poweriteration_n } from './poweriteration';
export {
randomPermute,
randomPermutation,
random_array,
random_matrix
} from './random';
export { range } from './range';
export {
sort_order,
Expand Down
7 changes: 5 additions & 2 deletions src/laplacian.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { zeroes } from './aliases';
import { assert } from './debug';

export function laplacian(graph, comp) {
var n = comp.length,
lap = reorder.zeroes(n, n),
lap = zeroes(n, n),
inv = inverse_permutation(comp),
i, j, k, row, sum, edges, v, e, other;

reorder.assert(! graph.directed(), "Laplacian only for undirected graphs");
assert(! graph.directed(), "Laplacian only for undirected graphs");
for (i = 0; i < n; i++) {
v = comp[i];
row = lap[i];
Expand Down
4 changes: 3 additions & 1 deletion src/mat2graph.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { graph } from './graph';

export function mat2graph(mat, directed) {
var n = mat.length,
nodes = [],
Expand All @@ -22,7 +24,7 @@ export function mat2graph(mat, directed) {
}
}
}
return reorder.graph(nodes, links, directed)
return graph(nodes, links, directed)
.linkDistance(function(l, i) {
return 1 + max_value - l.value;
})
Expand Down
13 changes: 9 additions & 4 deletions src/optimal_leaf_order.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import { distance } from './distance';
import { debug } from './core';
import { printhcluster } from './debug';
import { dist } from './dist';

/**
* optimal dendrogram ordering
*
Expand All @@ -15,7 +20,7 @@

export function optimal_leaf_order() {
var distanceMatrix = null,
distance = reorder.distance.euclidean,
distance = distance.euclidean,
linkage = "complete",
leavesMap = {},
orderMap = {};
Expand Down Expand Up @@ -93,8 +98,8 @@ export function optimal_leaf_order() {
left = leaves(v.left),
right = leaves(v.right);

if (reorder.debug)
console.log(reorder.printhcluster(v,0));
if (debug)
console.log(printhcluster(v,0));

for (var i = 0; i < left.length; i++) {
for (var j = 0; j < right.length; j++) {
Expand All @@ -111,7 +116,7 @@ export function optimal_leaf_order() {

function optimal_leaf_order(matrix) {
if (distanceMatrix === null)
distanceMatrix = (reorder.dist().distance(distance))(matrix);
distanceMatrix = (dist().distance(distance))(matrix);
var hcluster = science.stats.hcluster()
.linkage(linkage)
.distanceMatrix(distanceMatrix);
Expand Down
Loading

0 comments on commit 6b27627

Please sign in to comment.