Skip to content

Commit

Permalink
refactor(agnes): rename options.kind to options.method
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

`options.kind` was renamed to `options.method` in `agnes`.
  • Loading branch information
targos committed May 2, 2019
1 parent 0d5704b commit acabbe6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
20 changes: 13 additions & 7 deletions hclust.d.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
declare module 'ml-hclust' {
export type LinkageKind =
export type AgglomerationMethod =
| 'single'
| 'complete'
| 'average'
| 'centroid'
| 'ward';
export interface AgnesOptions {
distanceFunction: (a: number[], b: number[]) => number;
kind: LinkageKind;
distanceFunction: (a: T, b: T) => number;
method: AgglomerationMethod;
isDistanceMatrix: boolean;
}

export interface DianaOptions {
distanceFunction: (a: number[], b: number[]) => number;
export interface DianaOptions<T> {
distanceFunction: (a: T, b: T) => number;
}

export interface Cluster {
Expand All @@ -24,6 +24,12 @@ declare module 'ml-hclust' {
traverse: (cb: (cluster: Cluster) => void) => void;
}

export function agnes(data: number[][], options?: AgnesOptions): Cluster;
export function diana(data: number[][], options?: DianaOptions): Cluster;
export function agnes<T = number[]>(
data: T[],
options?: AgnesOptions<T>
): Cluster;
export function diana<T = number[]>(
data: T[],
options?: DianaOptions<T>
): Cluster;
}
2 changes: 1 addition & 1 deletion src/__tests__/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('Hierarchical clustering test', function () {
it('AGNES centroid', function () {
var clust = agnes(data.distanceMatrix2, {
isDistanceMatrix: true,
kind: 'centroid'
method: 'centroid'
});

clust.traverse(function (node) {
Expand Down
28 changes: 14 additions & 14 deletions src/agnes.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,18 @@ function median(values, alreadySorted) {
* @param {Array<Array<number>>} distance - Array of points to be clustered
* @param {object} [options]
* @param {Function} [options.distanceFunction]
* @param {string} [options.kind]
* @param {string} [options.method]
* @param {boolean} [options.isDistanceMatrix]
* @option isDistanceMatrix: Is the input a distance matrix?
* @constructor
*/
export function agnes(data, options = {}) {
const {
distanceFunction = euclidean,
kind = 'single',
method = 'single',
isDistanceMatrix = false
} = options;
let kindFunc;
let methodFunc;

var len = data.length;
var distance = data; // If source
Expand All @@ -133,28 +133,28 @@ export function agnes(data, options = {}) {
}

// allows to use a string or a given function
if (typeof kind === 'string') {
switch (kind) {
if (typeof method === 'string') {
switch (method) {
case 'single':
kindFunc = simpleLink;
methodFunc = simpleLink;
break;
case 'complete':
kindFunc = completeLink;
methodFunc = completeLink;
break;
case 'average':
kindFunc = averageLink;
methodFunc = averageLink;
break;
case 'centroid':
kindFunc = centroidLink;
methodFunc = centroidLink;
break;
case 'ward':
kindFunc = wardLink;
methodFunc = wardLink;
break;
default:
throw new RangeError(`unknown kind of linkage: ${kind}`);
throw new RangeError(`unknown clustering method: ${method}`);
}
} else if (typeof kind !== 'function') {
throw new TypeError('kind must be a string or function');
} else if (typeof method !== 'function') {
throw new TypeError('method must be a string or function');
}

var list = new Array(len);
Expand Down Expand Up @@ -188,7 +188,7 @@ export function agnes(data, options = {}) {
sdistance[f] = list[k].index[f].index;
}
}
dis = kindFunc(fdistance, sdistance, distance).toFixed(4);
dis = methodFunc(fdistance, sdistance, distance).toFixed(4);
if (dis in d) {
d[dis].push([list[j], list[k]]);
} else {
Expand Down
1 change: 0 additions & 1 deletion src/diana.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ function intrDist(index, data, disFun) {
* @param {Array <Array <number>>} data - Array of points to be clustered
* @param {object} [options]
* @param {Function} [options.distanceFunction]
* @param {string} [options.kind]
* @constructor
*/
export function diana(data, options = {}) {
Expand Down

0 comments on commit acabbe6

Please sign in to comment.