Skip to content

Commit

Permalink
fix: correct more types
Browse files Browse the repository at this point in the history
  • Loading branch information
targos committed Jul 10, 2019
1 parent ad2e2e8 commit 1fa6854
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
12 changes: 9 additions & 3 deletions hclust.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,24 @@ export interface DianaOptions<T> {
export interface Cluster {
children: Cluster[];
distance: number;
index: number[];
index: ClusterLeaf[];
cut: (threshold: number) => Cluster[];
group: (minGroups: number) => Cluster;
traverse: (cb: (cluster: Cluster) => void) => void;
}

export interface ClusterLeaf extends Cluster {
children: [];
distance: 0;
index: number;
}

export function agnes<T = number[]>(
data: T[],
options?: AgnesOptions<T>
options?: AgnesOptions<T>,
): Cluster;

export function diana<T = number[]>(
data: T[],
options?: DianaOptions<T>
options?: DianaOptions<T>,
): Cluster;
2 changes: 2 additions & 0 deletions src/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__tests__
.npmignore
4 changes: 2 additions & 2 deletions src/ClusterLeaf.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import Cluster from './Cluster';
export default class ClusterLeaf extends Cluster {
constructor(index) {
super();
this.index = index;
this.distance = 0;
this.children = [];
this.distance = 0;
this.index = index;
}
}
24 changes: 12 additions & 12 deletions src/__tests__/test.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
import { agnes, diana } from '..';

import * as data from '../../testData';

describe('Hierarchical clustering test', function () {
it('AGNES test', function () {
import { agnes, diana } from '..';

describe('Hierarchical clustering test', () => {
it('AGNES test', () => {
var clust = agnes(data.features1);
expect(clust.distance).toBeCloseTo(3.1623, 4);
});

it('AGNES second test', function () {
it('AGNES second test', () => {
var clust = agnes(data.distanceMatrix2, { isDistanceMatrix: true });
expect(clust.distance).not.toBeGreaterThan(1);
});

it('AGNES centroid', function () {
it('AGNES centroid', () => {
var clust = agnes(data.distanceMatrix2, {
isDistanceMatrix: true,
method: 'centroid'
method: 'centroid',
});

clust.traverse(function (node) {
clust.traverse((node) => {
expect(typeof node.distance).toBe('number');
expect(node.distance).not.toBe(NaN);
expect(node.distance).not.toBeLessThan(0);
});
});

it('AGNES based on distance matrix test', function () {
it('AGNES based on distance matrix test', () => {
var clust = agnes(data.distanceMatrix1, { isDistanceMatrix: true });
expect(clust.distance).toBeCloseTo(3.1623, 4);
});

it('DIANA test', function () {
it('DIANA test', () => {
var clust = diana(data.features1);
expect(clust.distance).toBeCloseTo(3.136, 3);
});

it('cut test', function () {
it('cut test', () => {
var clust = agnes(data.features1);
expect(clust.cut(1.5)).toHaveLength(5);
});

it('group test', function () {
it('group test', () => {
var clust = agnes(data.features1);
var group = clust.group(8);
expect(group.distance).toBeCloseTo(clust.distance, 4);
Expand Down

0 comments on commit 1fa6854

Please sign in to comment.