Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Types to compress tree and exports functions from .d.ts #16

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/__tests__/tree.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ describe('Tree similarity', () => {
4,
);
});
it('should throws', () => {
jobo322 marked this conversation as resolved.
Show resolved Hide resolved
expect(() => treeSimilarity(createTree(a), {})).toThrow(
'tree similarity expects tree as inputs',
);
});
});
5 changes: 4 additions & 1 deletion src/compressTree.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
20 changes: 16 additions & 4 deletions src/createTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down
22 changes: 17 additions & 5 deletions tree-similarity.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NumberArray } from "cheminfo-types";
import { DataXY } from "cheminfo-types";

export interface Tree {
sum: number;
Expand Down Expand Up @@ -39,7 +39,19 @@ export interface TreeSimilarityOptions {
beta?: number;
gamma?: number;
}
export interface Spectrum {
x: NumberArray;
y: NumberArray;
}

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 }
)
Loading