-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: setup project for typescript * chore: update test cases * chore: remove old .d.ts * feat: adapt functions * chore: fix prettier * chore: set skipLibCheck as true * chore: export types from each file * chore: set default branch Co-authored-by: Michaël Zasso <targos@protonmail.com> * chore: empty line at the end of the file Co-authored-by: Michaël Zasso <targos@protonmail.com> * chore: empty line at the end of .gitignore Co-authored-by: Michaël Zasso <targos@protonmail.com> * chore: remove types from jsdoc * chore: fix input types in compressTree * chore: add missing test case * chore: only tree accepted in compressTree --------- Co-authored-by: Michaël Zasso <targos@protonmail.com>
- Loading branch information
Showing
21 changed files
with
184 additions
and
150 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
extends: cheminfo | ||
extends: cheminfo-typescript | ||
parserOptions: | ||
sourceType: module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,4 +36,5 @@ jspm_packages | |
# Optional REPL history | ||
.node_repl_history | ||
|
||
lib | ||
lib | ||
lib-esm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
"presets": ["@babel/preset-typescript"], | ||
"plugins": ["@babel/plugin-transform-modules-commonjs"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
8 changes: 4 additions & 4 deletions
8
src/__tests__/compressTree.test.js → src/__tests__/compressTree.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 15 additions & 11 deletions
26
src/__tests__/createTree.test.js → src/__tests__/createTree.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './treeSimilarity'; | ||
export * from './createTree'; | ||
export { compressTree } from './compressTree'; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Tree } from './createTree'; | ||
|
||
export interface TreeSimilarityOptions { | ||
alpha?: number; | ||
beta?: number; | ||
gamma?: number; | ||
} | ||
|
||
/** | ||
* Similarity between two nodes | ||
* @return similarity measure between tree nodes | ||
*/ | ||
export function treeSimilarity( | ||
treeA: Tree | null, | ||
treeB: Tree | null, | ||
options: TreeSimilarityOptions = {}, | ||
): number { | ||
const { alpha = 0.1, beta = 0.33, gamma = 0.001 } = options; | ||
|
||
if (treeA === null || treeB === null) { | ||
return 0; | ||
} | ||
|
||
if (!isTree(treeA) || !isTree(treeB)) { | ||
throw new Error('tree similarity expects tree as inputs'); | ||
} | ||
|
||
if (treeA.sum === 0 && treeB.sum === 0) { | ||
return 1; | ||
} | ||
|
||
const C = | ||
(alpha * Math.min(treeA.sum, treeB.sum)) / Math.max(treeA.sum, treeB.sum) + | ||
(1 - alpha) * Math.exp(-gamma * Math.abs(treeA.center - treeB.center)); | ||
|
||
return ( | ||
beta * C + | ||
((1 - beta) * | ||
(treeSimilarity(treeA.left, treeB.left, options) + | ||
treeSimilarity(treeA.right, treeB.right, options))) / | ||
2 | ||
); | ||
} | ||
|
||
function isTree(tree: object): tree is Tree { | ||
return ['sum', 'center', 'left', 'right'].every((key) => key in tree); | ||
} |
Oops, something went wrong.