-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: make AGNES algorithm closer to R (#11)
BREAKING CHANGE: - use the Lance-Williams algorithm to update cluster distances. - add other methods that exist in R. - remove `ClusterLeaf` class and use an `isLeaf` property instead. - remove `index` array from clusters. Instead, an `indexes()` method has been added to compute it. - add a `size` property to clusters that indicates the number of leaves below it. - the default `method` is now `'complete'`. - DIANA has been removed from the package pending rewriting it.
- Loading branch information
Showing
12 changed files
with
518 additions
and
465 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { agnes } from './src'; | ||
|
||
const d = [ | ||
[0, 17, 21, 31, 23], | ||
[17, 0, 30, 34, 21], | ||
[21, 30, 0, 28, 39], | ||
[31, 34, 28, 0, 43], | ||
[23, 21, 39, 43, 0], | ||
]; | ||
|
||
const c = agnes(d, { | ||
method: 'ward', | ||
isDistanceMatrix: true, | ||
}); | ||
|
||
const heights = []; | ||
c.traverse((cluster) => { | ||
if (cluster.isLeaf) { | ||
console.log(cluster.index + 1); | ||
} | ||
if (cluster.height > 0) { | ||
heights.push(cluster.height); | ||
} | ||
}); | ||
|
||
heights.sort((h1, h2) => h1 - h2); | ||
|
||
console.log(heights); | ||
// console.log(require('util').inspect(c, { depth: Infinity, colors: true })); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import * as data from '../../testData'; | ||
|
||
import { agnes } from '..'; | ||
|
||
test('AGNES with feature matrix', () => { | ||
const clust = agnes(data.features1); | ||
expect(clust.height).toBeCloseTo(7.2111, 4); | ||
}); | ||
|
||
test('AGNES with distance matrix', () => { | ||
var clust = agnes(data.distanceMatrix1, { isDistanceMatrix: true }); | ||
expect(clust.height).toBeCloseTo(7.2111, 4); | ||
}); | ||
|
||
test('AGNES with distance matrix 2', () => { | ||
const clust = agnes(data.distanceMatrix2, { isDistanceMatrix: true }); | ||
expect(clust.height).not.toBeGreaterThan(1); | ||
}); | ||
|
||
test('AGNES centroid', () => { | ||
const clust = agnes(data.distanceMatrix2, { | ||
isDistanceMatrix: true, | ||
method: 'centroid', | ||
}); | ||
|
||
clust.traverse((node) => { | ||
expect(typeof node.height).toBe('number'); | ||
expect(node.height).not.toBe(NaN); | ||
expect(node.height).not.toBeLessThan(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import * as data from '../../testData'; | ||
|
||
import { agnes } from '..'; | ||
|
||
test('size', () => { | ||
const clust = agnes(data.features1); | ||
expect(clust.size).toBe(10); | ||
const [child1, child2] = clust.children; | ||
expect(child1.size).toBe(5); | ||
expect(child2.size).toBe(5); | ||
}); | ||
|
||
test('cut', () => { | ||
const clust = agnes(data.features1); | ||
expect(clust.cut(1.5)).toHaveLength(5); | ||
}); | ||
|
||
test('group', () => { | ||
const clust = agnes(data.features1); | ||
const group = clust.group(8); | ||
expect(group.children).toHaveLength(8); | ||
}); | ||
|
||
test('indices', () => { | ||
const clust = agnes(data.features1); | ||
const indices = clust.indices(); | ||
expect(indices).toHaveLength(data.features1.length); | ||
expect(indices).toStrictEqual([6, 5, 9, 8, 7, 3, 1, 0, 4, 2]); | ||
}); | ||
|
||
test('traverse, isLeaf and index', () => { | ||
const clust = agnes(data.features1); | ||
let other = 0; | ||
let leaves = 0; | ||
clust.traverse((cluster) => { | ||
if (cluster.isLeaf) { | ||
leaves++; | ||
expect(cluster.index).toBeGreaterThan(-1); | ||
} else { | ||
other++; | ||
expect(cluster.index).toBe(-1); | ||
} | ||
}); | ||
expect(other).toBe(9); | ||
expect(leaves).toBe(10); | ||
}); |
Oops, something went wrong.