diff --git a/src/__tests__/tree.test.js b/src/__tests__/tree.test.js index b11337f..7399403 100644 --- a/src/__tests__/tree.test.js +++ b/src/__tests__/tree.test.js @@ -18,4 +18,9 @@ describe('Tree similarity', () => { 4, ); }); + it('should throw with wrong input', () => { + expect(() => treeSimilarity(createTree(a), {})).toThrow( + 'tree similarity expects tree as inputs', + ); + }); }); diff --git a/src/compressTree.js b/src/compressTree.js index 076732f..eca20c2 100644 --- a/src/compressTree.js +++ b/src/compressTree.js @@ -1,6 +1,9 @@ +/** + * @typedef {import("../tree-similarity").Tree} Tree + */ /** * Destructive compression in which we reduce the number of decimals - * @param {object} tree + * @param {Tree} tree * @param {object} [options={}] * @param {number} [options.fixed=undefined] - number of decimal ot keep * @returns diff --git a/src/createTree.js b/src/createTree.js index b0905cd..d1ca4c1 100644 --- a/src/createTree.js +++ b/src/createTree.js @@ -3,17 +3,18 @@ import binarySearch from 'binary-search'; /** * @typedef {import("../tree-similarity").Tree} Tree * @typedef {import("../tree-similarity").CreateTreeOptions} CreateTreeOptions - * @typedef {import("../tree-similarity").Spectrum} Spectrum + * @typedef {import("cheminfo-types").DataXY} DataXY + * @typedef {import("cheminfo-types").DoubleArray} DoubleArray */ /** * Function that creates the tree - * @param {Spectrum} spectrum + * @param {DataXY} dataXY * @param {CreateTreeOptions} [options] * @return { Tree | null } */ -export function createTree(spectrum, options = {}) { - const { x, y } = spectrum; +export function createTree(dataXY, options = {}) { + const { x, y } = dataXY; const { minWindow = 0.16, threshold = 0.01, @@ -24,6 +25,17 @@ export function createTree(spectrum, options = {}) { return mainCreateTree(x, y, from, to, minWindow, threshold); } +/** + * recursive function to generate all the nodes in the tree + * @param {DoubleArray} x + * @param {DoubleArray} y + * @param {number} from + * @param {number} to + * @param {number} minWindow + * @param {number} threshold + * @returns {Tree | null} + */ + function mainCreateTree(x, y, from, to, minWindow, threshold) { if (to - from < minWindow) { return null; diff --git a/tree-similarity.d.ts b/tree-similarity.d.ts index 12c537e..ec08501 100644 --- a/tree-similarity.d.ts +++ b/tree-similarity.d.ts @@ -1,4 +1,4 @@ -import { NumberArray } from "cheminfo-types"; +import { DataXY } from "cheminfo-types"; export interface Tree { sum: number; @@ -39,7 +39,19 @@ export interface TreeSimilarityOptions { beta?: number; gamma?: number; } -export interface Spectrum { - x: NumberArray; - y: NumberArray; -} \ No newline at end of file + +export function createTree( + dataXY: DataXY, + options: CreateTreeOptions = {}, +): Tree + +export function treeSimilarity( + tree1: Tree, + tree2: Tree, + options: TreeSimilarityOptions = {}, +): number; + +export function compressTree( + tree: Tree, + options: { fixed?: number } +) \ No newline at end of file