diff --git a/hclust.d.ts b/hclust.d.ts index a18fc18..c1534c2 100644 --- a/hclust.d.ts +++ b/hclust.d.ts @@ -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 { + distanceFunction: (a: T, b: T) => number; } export interface Cluster { @@ -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( + data: T[], + options?: AgnesOptions + ): Cluster; + export function diana( + data: T[], + options?: DianaOptions + ): Cluster; } diff --git a/src/__tests__/test.js b/src/__tests__/test.js index a9dd65c..e8c0c34 100644 --- a/src/__tests__/test.js +++ b/src/__tests__/test.js @@ -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) { diff --git a/src/agnes.js b/src/agnes.js index 3764980..e2db64f 100644 --- a/src/agnes.js +++ b/src/agnes.js @@ -113,7 +113,7 @@ function median(values, alreadySorted) { * @param {Array>} 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 @@ -121,10 +121,10 @@ function median(values, alreadySorted) { 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 @@ -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); @@ -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 { diff --git a/src/diana.js b/src/diana.js index 76ce088..09f19c3 100644 --- a/src/diana.js +++ b/src/diana.js @@ -73,7 +73,6 @@ function intrDist(index, data, disFun) { * @param {Array >} data - Array of points to be clustered * @param {object} [options] * @param {Function} [options.distanceFunction] - * @param {string} [options.kind] * @constructor */ export function diana(data, options = {}) {