-
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: change exposed methods and accepted types
refactor!: treeSimilarity now only accepts Tree objects refactor!: createTree expects an object {x: [],y: []}
- Loading branch information
Showing
10 changed files
with
144 additions
and
112 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -40,6 +40,7 @@ | |
}, | ||
"dependencies": { | ||
"binary-search": "^1.3.6", | ||
"cheminfo-types": "^1.7.2", | ||
"num-sort": "^3.0.0" | ||
} | ||
} |
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 |
---|---|---|
@@ -1,28 +1,21 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
|
||
import { treeSimilarity, getFunction } from '..'; | ||
import { createTree, treeSimilarity } from '../index'; | ||
|
||
let a = [ | ||
[1, 2, 3, 4, 5, 6, 7], | ||
[0.3, 0.7, 4, 0.3, 0.2, 5, 0.3], | ||
]; | ||
let b = [ | ||
[1, 2, 3, 4, 5, 6, 7], | ||
[0.3, 4, 0.7, 0.3, 5, 0.2, 0.3], | ||
]; | ||
const a = { | ||
x: [1, 2, 3, 4, 5, 6, 7], | ||
y: [0.3, 0.7, 4, 0.3, 0.2, 5, 0.3], | ||
}; | ||
const b = { | ||
x: [1, 2, 3, 4, 5, 6, 7], | ||
y: [0.3, 4, 0.7, 0.3, 5, 0.2, 0.3], | ||
}; | ||
|
||
describe('Tree similarity', () => { | ||
it('should work with two arrays', () => { | ||
expect(treeSimilarity(a, b)).toBeCloseTo(0.653354, 4); | ||
}); | ||
|
||
it('should currify the options', () => { | ||
let options = { | ||
alpha: 0.4, | ||
beta: 0.5, | ||
gamma: 0.001, | ||
}; | ||
let func = getFunction(options); | ||
expect(func(a, b)).toBe(treeSimilarity(a, b, options)); | ||
expect(treeSimilarity(createTree(a), createTree(b))).toBeCloseTo( | ||
0.653354, | ||
4, | ||
); | ||
}); | ||
}); |
8 changes: 4 additions & 4 deletions
8
src/__tests__/getSimilarity.test.js → src/__tests__/treeSimilarity.test.js
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,15 +1,15 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
|
||
import { createTree } from '../createTree'; | ||
import { getSimilarity } from '../getSimilarity'; | ||
import { treeSimilarity } from '../treeSimilarity'; | ||
|
||
describe('simple trees', () => { | ||
it('same tree', () => { | ||
let x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | ||
let y = [0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0]; | ||
let tree1 = createTree([x, y]); | ||
let tree2 = createTree([x, y]); | ||
let tree1 = createTree({ x, y }); | ||
let tree2 = createTree({ x, y }); | ||
|
||
expect(getSimilarity(tree1, tree2, { beta: 1 })).toBe(1); | ||
expect(treeSimilarity(tree1, tree2, { beta: 1 })).toBe(1); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,11 +1,2 @@ | ||
import { getSimilarity } from './getSimilarity'; | ||
|
||
export { treeSimilarity } from './treeSimilarity'; | ||
export { createTree } from './createTree'; | ||
|
||
export function treeSimilarity(A, B, options = {}) { | ||
return getSimilarity(A, B, options); | ||
} | ||
|
||
export function getFunction(options = {}) { | ||
return (A, B) => getSimilarity(A, B, options); | ||
} |
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,42 @@ | ||
/** | ||
* @typedef {import("../types").Tree} Tree | ||
* @typedef {import("../types").TreeSimilarityOptions} TreeSimilarityOptions | ||
*/ | ||
/** | ||
* Similarity between two nodes | ||
* @param {Tree | null} a - tree A node | ||
* @param {Tree | null} b - tree B node | ||
* @param {TreeSimilarityOptions} [options] | ||
* @return {number} similarity measure between tree nodes | ||
*/ | ||
export function treeSimilarity(a, b, options = {}) { | ||
const { alpha = 0.1, beta = 0.33, gamma = 0.001 } = options; | ||
|
||
if (a === null || b === null) { | ||
return 0; | ||
} | ||
|
||
if (!isTree(a) || !isTree(b)) { | ||
throw new Error('tree similarity expects tree as inputs'); | ||
} | ||
|
||
if (a.sum === 0 && b.sum === 0) { | ||
return 1; | ||
} | ||
|
||
const C = | ||
(alpha * Math.min(a.sum, b.sum)) / Math.max(a.sum, b.sum) + | ||
(1 - alpha) * Math.exp(-gamma * Math.abs(a.center - b.center)); | ||
|
||
return ( | ||
beta * C + | ||
((1 - beta) * | ||
(treeSimilarity(a.left, b.left, options) + | ||
treeSimilarity(a.right, b.right, options))) / | ||
2 | ||
); | ||
} | ||
|
||
function isTree(a) { | ||
return ['sum', 'center', 'left', 'right'].every((key) => key in a); | ||
} |
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,45 @@ | ||
import { NumberArray } from "cheminfo-types"; | ||
|
||
export interface Tree { | ||
sum: number; | ||
center: number; | ||
/** | ||
* left and right have the same structure than the parent, | ||
* or are null if they are leaves | ||
*/ | ||
left: Tree | null; | ||
right: Tree | null; | ||
} | ||
|
||
export interface CreateTreeOptions { | ||
/** | ||
* low limit of the tree | ||
* @default x[0] | ||
*/ | ||
from?: number | ||
/** | ||
* high limit of the tree | ||
* @default x.at(-1) | ||
*/ | ||
to?: number | ||
/** | ||
* minimal sum value to accept a node | ||
* @default 0.01 | ||
*/ | ||
threshold?: number; | ||
/** | ||
* minimal window width to create a node | ||
* @default 0.16 | ||
*/ | ||
minWindow?: number | ||
} | ||
|
||
export interface TreeSimilarityOptions { | ||
alpha?: number; | ||
beta?: number; | ||
gamma?: number; | ||
} | ||
export interface Spectrum { | ||
x: NumberArray; | ||
y: NumberArray; | ||
} |