Skip to content

Commit

Permalink
Implement union and mult_dist
Browse files Browse the repository at this point in the history
  • Loading branch information
nvbeusekom committed Apr 20, 2022
1 parent fb178ef commit fb0c7f9
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 6,219 deletions.
6,199 changes: 10 additions & 6,189 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"build": "rollup -c",
"dev": "rollup -c rollup.config.dev.js -w",
"pretest": "npm run build",
"test": "vows --nocolor; echo",
"test": "vows test/*.js --nocolor; echo",
"cover": "nyc --silent vows --nocolor",
"cover:report": "nyc report --reporter=lcov --reporter=text",
"clean": "rm -rf node_modules dist",
Expand Down
12 changes: 6 additions & 6 deletions src/distance.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,16 @@ export const distance = {
// N. van Beusekom, W. Meulemans, B. Speckmann, Simultaneous Orderings for Graph Collections
// IEEE Transactions on Visualization and Computer Graphics, vol. 28, no. 1, pp. 1-10, Jan. 2022
morans(matrix){
var m = 0;
var n = matrix.length * matrix[0].length;
for (var i = 0; i <matrix.length; i++) {
for (var j = 0; j < matrix[0].length; j++) {
let m = 0;
const n = matrix.length * matrix[0].length;
for (let i = 0; i <matrix.length; i++) {
for (let j = 0; j < matrix[0].length; j++) {
m += matrix[i][j];
}
}
return (a,b) => {
var result = 0;
for (var i = 0; i < a.length; i++) {
let result = 0;
for (let i = 0; i < a.length; i++) {
if (isNum(a[i], b[i])) {
result += (a[i] * n - m) * (b[i] * n - m);
}
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export { intersect_sorted_ints } from './intersect';
export { laplacian } from './laplacian';
export { mat2graph } from './mat2graph';
export { meantranspose, meancolumns } from './mean';
export { morans_i } from './morans_i';
export { optimal_leaf_order } from './optimal_leaf_order';
export { order } from './order';
export { pca1d, pca_order } from './pca_order';
Expand All @@ -70,6 +71,7 @@ export { spectral_order } from './spectral_order';
export { stablepermute } from './stablepermute';
export { sum } from './sum';
export { transposeSlice } from './transpose';
export { union } from './union';
export {
cmp_number,
cmp_number_asc,
Expand Down
24 changes: 12 additions & 12 deletions src/morans_i.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
// Compute Moran's I of a permuted matrix using Rook's adjacency
export function morans_i(matrix) {
// N is the number of cells, W is the number of adjacencies, mean is the mean value.
var N = matrix.length * matrix.length;
var W = (matrix.length-2) * (matrix.length-2) * 4 + (matrix.length-2) * 3 * 2 + (matrix.length-2) * 3 * 2 + 8;
var m = 0;
for (var i = 0; i < matrix.length; i++) {
for (var j = 0; j < matrix[0].length; j++) {
const N = matrix.length * matrix.length;
const W = (matrix.length-2) * (matrix.length-2) * 4 + (matrix.length-2) * 3 * 2 + (matrix.length-2) * 3 * 2 + 8;
let m = 0;
for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[0].length; j++) {
m += matrix[i][j];
}
}
var mean = m/N;
const mean = m/N;

var num = 0, denom = 0;
for (var j = 0; j < matrix.length; j++) {
for (var i = 0; i < matrix[0].length; i++) {
let num = 0, denom = 0;
for (let j = 0; j < matrix.length; j++) {
for (let i = 0; i < matrix[0].length; i++) {
denom += Math.pow(matrix[j][i] - mean, 2);
var innersum = 0;
for (var y = Math.max(0,j-1); y < Math.min(matrix.length,j+2); y++) {
for (var x = Math.max(0,i-1); x < Math.min(matrix[0].length,i+2); x++) {
let innersum = 0;
for (let y = Math.max(0,j-1); y < Math.min(matrix.length,j+2); y++) {
for (let x = Math.max(0,i-1); x < Math.min(matrix[0].length,i+2); x++) {
if(y !== j || x !== i){
// Not Counting Diagonal Neighbours: Rook's adjacency (Recommended)
if(i - x >= -1 && i - x <= 1 && j === y){
Expand Down
53 changes: 53 additions & 0 deletions src/mult_dist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { distance as distances } from './distance';

export function mult_dist() {
let distance = distances.euclidean;

function mult_dist(matrices) {
const n = matrices.length;

const res = [];
for (let i = 0; i < matrices[0].length; i++) {
const newrow = [];
for (let j = 0; j < matrices[0][0].length; j++) {
newrow.push(0);
}
res.push(newrow);
}
for (let k = 0; k < n; k++){
let distMatrix = [];
const vector = matrices[k];
const n1 = vector.length;
for (let i = 0; i < n1; i++) {
const d = [];
distMatrix[i] = d;
for (let j = 0; j < n1; j++) {
if (j < i) {
d[j] = (distMatrix[j][i]);
} else if (i === j) {
d[j] = 0;
} else {
d[j] = distance(vector[i], vector[j]);
}
}
}
for (let i = 0; i < distMatrix.length; i++) {
for (let j = 0; j <distMatrix[0].length; j++) {
res[i][j] += distMatrix[i][j];
}

}
}
return res;
}

mult_dist.distance = function (x) {
if (!arguments.length) {
return distance;
}
distance = x;
return mult_dist;
};

return mult_dist;
}
18 changes: 18 additions & 0 deletions src/union.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Computes the union of a list of matrices
export function union(matrices) {
let pile = [];
for(let i = 0 ; i< matrices[0].length; i++){
if(!pile[i]){
pile.push([]);
}
for(let j = 0 ; j<matrices[0][0].length ; j++){
if(!pile[i].length < matrices[0][0].length){
pile[i].push(0);
}
for(let k = 0; k<matrices.length; k++){
pile[i][j] = pile[i][j] + matrices[k][i][j];
}
}
}
return pile;
}
39 changes: 28 additions & 11 deletions test/morans_i-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,43 @@ const reorder = require('../dist/reorder.cjs');
const vows = require('vows');
const assert = require('assert');

const suite = vows.describe('reorder.morans_i');
const suite = vows.describe('reorder.union');

suite.addBatch({
morans_i: {
union: {
simple() {
var mat1 = [
const mats = [[
[0,0,0,0],
[0,0,0,0],
[0,0,1,1],
[0,0,1,1]
],
[
[0,0,0,0],
[0,0,0,0],
[1,1,1,1],
[1,1,1,1]
],
[
[0,0,1,1],
[0,0,1,1],
[1,1,1,1],
[1,1,1,1]
],
[
[1,1,1,1],
[1,1,1,1],
[1,1,1,1],
[1,1,1,1]
];
var mat2 = [
[1,0,1,0],
[0,1,0,1],
[1,0,1,0],
[0,1,0,1]
]];
const result = [
[1,1,2,2],
[1,1,2,2],
[3,3,4,4],
[3,3,4,4]
];

assert.equal(reorder.morans_i(mat1), 1);
assert.equal(reorder.morans_i(mat2), -1);
assert.deepequal(reorder.union(mats), result);
},
},
});
Expand Down
30 changes: 30 additions & 0 deletions test/union-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const reorder = require('../dist/reorder.cjs');

const vows = require('vows');
const assert = require('assert');

const suite = vows.describe('reorder.morans_i');

suite.addBatch({
morans_i: {
simple() {
const mat1 = [
[1,1,1,1],
[1,1,1,1],
[1,1,1,1],
[1,1,1,1]
];
const mat2 = [
[1,0,1,0],
[0,1,0,1],
[1,0,1,0],
[0,1,0,1]
];

assert.equal(reorder.morans_i(mat1), 1);
assert.equal(reorder.morans_i(mat2), -1);
},
},
});

suite.export(module);

0 comments on commit fb0c7f9

Please sign in to comment.