Skip to content

Commit

Permalink
fix: remove dependency on ml-distance (#198)
Browse files Browse the repository at this point in the history
Inline cosine similarity to avoid big distance package.
  • Loading branch information
targos authored Jun 29, 2024
1 parent 28dae91 commit 2009cae
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
1 change: 0 additions & 1 deletion packages/ms-spectrum/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"is-any-array": "^2.0.1",
"mf-parser": "^3.2.0",
"mf-utilities": "^3.2.0",
"ml-distance": "^4.0.1",
"ml-gsd": "^12.1.6",
"ml-regression-power": "^3.0.0",
"ml-spectra-processing": "^14.5.1",
Expand Down
28 changes: 23 additions & 5 deletions packages/ms-spectrum/src/MSComparator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { similarity } from 'ml-distance';
import {
xIsMonotonic,
xyArrayAlign,
Expand Down Expand Up @@ -145,14 +144,14 @@ function normalizeAndCacheData(cache, dataXY, options = {}) {
}

/**
*
* @param {*} aligned
*
* @param {*} aligned
* @param {object} [options={}]
* @param {number} [options.massPower]
* @param {number} [options.intensityPower]
* @param {number} [options.minNbCommonPeaks]
* @returns
* @returns
*/
function returnSimilarity(aligned, options = {}) {
const { massPower, intensityPower, minNbCommonPeaks } = options;
Expand Down Expand Up @@ -194,6 +193,25 @@ function returnSimilarity(aligned, options = {}) {
nbPeaks1,
nbPeaks2,
tanimoto: nbCommonPeaks / (nbPeaks1 + nbPeaks2 - nbCommonPeaks),
cosine: similarity.cosine(vector1, vector2),
cosine: cosineSimilarity(vector1, vector2),
};
}

/**
* Returns the average of cosine distances between vectors a and b
* Copied from https://github.com/mljs/distance/blob/0b15acd6476413f4111cb4852ca1bec9edaa2805/src/similarities/cosine.ts
* @param a {import('cheminfo-types').NumberArray} - first vector
* @param b {import('cheminfo-types').NumberArray} - second vector
* @returns {number} - cosine similarity
*/
function cosineSimilarity(a, b) {
let p = 0;
let p2 = 0;
let q2 = 0;
for (let i = 0; i < a.length; i++) {
p += a[i] * b[i];
p2 += a[i] * a[i];
q2 += b[i] * b[i];
}
return p / (Math.sqrt(p2) * Math.sqrt(q2));
}

0 comments on commit 2009cae

Please sign in to comment.