diff --git a/.gitignore b/.gitignore index d6d38f8..564e332 100644 --- a/.gitignore +++ b/.gitignore @@ -39,4 +39,6 @@ jspm_packages lib lib-esm -docs \ No newline at end of file +docs + +dist \ No newline at end of file diff --git a/package.json b/package.json index 689435e..a3a5bf2 100644 --- a/package.json +++ b/package.json @@ -39,18 +39,18 @@ "devDependencies": { "@babel/plugin-transform-modules-commonjs": "^7.19.6", "@babel/preset-typescript": "^7.18.6", - "@types/jest": "^29.2.3", + "@types/jest": "^29.2.4", "codecov": "^3.8.2", - "eslint": "^8.28.0", + "eslint": "^8.29.0", "eslint-config-cheminfo-typescript": "^11.2.2", "eslint-plugin-import": "^2.26.0", - "eslint-plugin-jest": "^27.1.5", + "eslint-plugin-jest": "^27.1.6", "eslint-plugin-no-only-tests": "^3.1.0", "eslint-plugin-prettier": "^4.2.1", "jest": "^29.3.1", "npm-run-all": "^4.1.5", - "prettier": "^2.7.1", + "prettier": "^2.8.1", "rimraf": "^3.0.2", - "typescript": "^4.9.3" + "typescript": "^4.9.4" } } diff --git a/src/__tests__/fromLabels.js b/src/__tests__/fromLabels.test.ts similarity index 97% rename from src/__tests__/fromLabels.js rename to src/__tests__/fromLabels.test.ts index 8933a2a..ad1622e 100644 --- a/src/__tests__/fromLabels.js +++ b/src/__tests__/fromLabels.test.ts @@ -36,7 +36,7 @@ describe('fromLabels', () => { it('should sort labels alphabetically', () => { const CM = ConfusionMatrix.fromLabels([1, 3, 2], [1, 1, 1], { sort: (a, b) => { - return a - b; + return (a as number) - (b as number); }, }); expect(CM.getLabels()).toStrictEqual([1, 2, 3]); diff --git a/src/__tests__/metrics.js b/src/__tests__/metrics.test.ts similarity index 100% rename from src/__tests__/metrics.js rename to src/__tests__/metrics.test.ts diff --git a/src/__tests__/test.js b/src/__tests__/test.test.ts similarity index 100% rename from src/__tests__/test.js rename to src/__tests__/test.test.ts diff --git a/src/index.js b/src/index.ts similarity index 63% rename from src/index.js rename to src/index.ts index d663af4..a36aa91 100644 --- a/src/index.js +++ b/src/index.ts @@ -1,14 +1,15 @@ /** - * Constructs a confusion matrix + * Constructs a confusion matrix * @class ConfusionMatrix * @example * const CM = new ConfusionMatrix([[13, 2], [10, 5]], ['cat', 'dog']) - * @param {Array>} matrix - The confusion matrix, a 2D Array. Rows represent the actual label and columns - * the predicted label. - * @param {Array} labels - Labels of the confusion matrix, a 1D Array + * @param matrix - The confusion matrix, a 2D Array. Rows represent the actual label and columns the predicted label. + * @param labels - Labels of the confusion matrix, a 1D Array */ export default class ConfusionMatrix { - constructor(matrix, labels) { + private labels: Label[]; + private matrix: number[][]; + constructor(matrix: number[][], labels: Label[]) { if (matrix.length !== matrix[0].length) { throw new Error('Confusion matrix must be square'); } @@ -24,52 +25,54 @@ export default class ConfusionMatrix { /** * Construct confusion matrix from the predicted and actual labels (classes). Be sure to provide the arguments in * the correct order! - * @param {Array} actual - The predicted labels of the classification - * @param {Array} predicted - The actual labels of the classification. Has to be of same length as - * predicted. - * @param {object} [options] - Additional options - * @param {Array} [options.labels] - The list of labels that should be used. If not provided the distinct set + * @param actual - The predicted labels of the classification + * @param predicted - The actual labels of the classification. Has to be of same length as predicted. + * @param [options] - Additional options + * @param [options.labels] - The list of labels that should be used. If not provided the distinct set * of labels present in predicted and actual is used. Labels are compared using the strict equality operator * '===' - * @param {any} [options.sort] - * @return {ConfusionMatrix} - Confusion matrix + * @param [options.sort] + * @return Confusion matrix */ - static fromLabels(actual, predicted, options = {}) { + static fromLabels( + actual: Label[], + predicted: Label[], + options: FromLabelsOptions = {}, + ) { if (predicted.length !== actual.length) { throw new Error('predicted and actual must have the same length'); } - let distinctLabels; + let distinctLabels: Set