Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 7 additions & 21 deletions .github/workflows/typedoc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,10 @@ on:
types: [published]

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16.x
- name: Install dependencies
run: npm install
- name: Build documentation
uses: zakodium/typedoc-action@v2
with:
entry: src/index.ts

- name: Deploy to GitHub pages
uses: JamesIves/github-pages-deploy-action@releases/v4
with:
token: ${{ secrets.BOT_TOKEN }}
branch: gh-pages
folder: docs
clean: true
typedoc:
# Documentation: https://github.com/zakodium/workflows#typedoc
uses: zakodium/workflows/.github/workflows/typedoc.yml@typedoc-v1
with:
entry: 'src/index.ts'
secrets:
github-token: ${{ secrets.BOT_TOKEN }}
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
.claude
.idea
node_modules
lib
coverage

.DS_Store

lib-esm

docs
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
package-lock=false
ignore-scripts=true
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CHANGELOG.md
coverage
lib
File renamed without changes.
66 changes: 41 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# ml-distance

<h3 align="center">

<a href="https://www.zakodium.com">
<img src="https://www.zakodium.com/brand/zakodium-logo-white.svg" width="50" alt="Zakodium logo" />
</a>

<p>
Maintained by <a href="https://www.zakodium.com">Zakodium</a>
</p>

[![NPM version][npm-image]][npm-url]
[![Node.js CI](https://github.com/mljs/distance/actions/workflows/nodejs.yml/badge.svg)](https://github.com/mljs/distance/actions/workflows/nodejs.yml)
[![Test coverage][codecov-image]][codecov-url]
[![npm download][download-image]][download-url]

</h3>

Distance functions to compare vectors.
Expand All @@ -23,6 +23,22 @@ Distance functions to compare vectors.

`$ npm i ml-distance`

This package is ESM-only. CommonJS consumers need Node.js >= 20.19, >= 22.12, or
any 24.x or later to `require()` it, or should migrate to `import`.

## Usage

```js
import { distance, similarity } from 'ml-distance';

const p = [0, 1, 2, 3, 4];
const q = [4, 3, 2, 1, 0];

distance.euclidean(p, q); // 6.324555320336759
distance.manhattan(p, q); // 12
similarity.cosine(p, q); // 0.3333333333333333
```

## Methods

### Distances
Expand Down Expand Up @@ -65,13 +81,13 @@ $d(p,q)=\frac{\sum\limits_{i=1}^{n}{\left|p_i-q_i\right|}}{n}$

Returns the [Soergel distance](http://www.orgchm.bas.bg/~vmonev/SimSearch.pdf) between vectors p and q

$d(p,q)=\frac{\sum\limits_{i=1}^{n}{\left|p_i-q_i\right|}}{max(p_i,q_i)}$
$d(p,q)=\frac{\sum\limits_{i=1}^{n}{\left|p_i-q_i\right|}}{\sum\limits_{i=1}^{n}{max(p_i,q_i)}}$

- `kulczynski(p, q)`

Returns the [Kulczynski distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q

$d(p,q)=\frac{\sum\limits_{i=1}^{n}{\left|p_i-q_i\right|}}{min(p_i,q_i)}$
$d(p,q)=\frac{\sum\limits_{i=1}^{n}{\left|p_i-q_i\right|}}{\sum\limits_{i=1}^{n}{min(p_i,q_i)}}$

- `canberra(p, q)`

Expand Down Expand Up @@ -113,9 +129,9 @@ Note: distance between 2 identical vectors is 0.5 !

- `ruzicka(p, q)`

Returns the [Ruzicka similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
Returns the [Ruzicka similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q. The matching distance is `soergel`.

$d(p,q)=\frac{\sum\limits_{i=1}^{n}{max(p_i,q_i)}}{\sum\limits_{i=1}^{n}{min(p_i,q_i)}}$
$s(p,q)=\frac{\sum\limits_{i=1}^{n}{min(p_i,q_i)}}{\sum\limits_{i=1}^{n}{max(p_i,q_i)}}$

- `tanimoto(p, q, [bitVector])`

Expand All @@ -133,18 +149,6 @@ Returns the [Harmonic mean similarity](http://www.naun.org/main/NAUN/ijmmas/mmma

$d(p,q)=2\sum\limits_{i=1}^{n}\frac{p_i\cdot{q_i}}{p_i+q_i}$

- `cosine(p, q)`

Returns the [Cosine similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q

$d(p,q)=\frac{\sum\limits_{i=1}^{n}{p_i\cdot{q_i}}}{\sqrt{\sum\limits_{i=1}^{n}{p_i^2}}\sqrt{\sum\limits_{i=1}^{n}{q_i^2}}}$

- `kumarHassebrook(p, q)`

Returns the [Kumar-Hassebrook similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q

$d(p,q)=\frac{\sum\limits_{i=1}^{n}{p_i\cdot{q_i}}}{\sum\limits_{i=1}^{n}{p_i^2}+\sum\limits_{i=1}^{n}{q_i^2}-\sum\limits_{i=1}^{n}{p_i\cdot{q_i}}}$

- `jaccard(p, q)`

Returns the [Jaccard distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
Expand All @@ -155,7 +159,7 @@ $d(p,q)=1-\frac{\sum\limits_{i=1}^{n}{p_i\cdot{q_i}}}{\sum\limits_{i=1}^{n}{p_i^

Returns the [Dice distance](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q

$d(p,q)=1-\frac{\sum\limits_{i=1}^{n}{(p_i-q_i)^2}}{\sum\limits_{i=1}^{n}{p_i^2}+\sum\limits_{i=1}^{n}{q_i^2}}$
$d(p,q)=\frac{\sum\limits_{i=1}^{n}{(p_i-q_i)^2}}{\sum\limits_{i=1}^{n}{p_i^2}+\sum\limits_{i=1}^{n}{q_i^2}}$

- `fidelity(p, q)`

Expand Down Expand Up @@ -311,9 +315,21 @@ Returns the [Kulczynski similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-4

Returns the [Squared-chord similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q

- `jaccard(p, q)`
- `cosine(p, q)`

Returns the [Cosine similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q

$s(p,q)=\frac{\sum\limits_{i=1}^{n}{p_i\cdot{q_i}}}{\sqrt{\sum\limits_{i=1}^{n}{p_i^2}}\sqrt{\sum\limits_{i=1}^{n}{q_i^2}}}$

- `kumarHassebrook(p, q)`

Returns the [Kumar-Hassebrook similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q

$s(p,q)=\frac{\sum\limits_{i=1}^{n}{p_i\cdot{q_i}}}{\sum\limits_{i=1}^{n}{p_i^2}+\sum\limits_{i=1}^{n}{q_i^2}-\sum\limits_{i=1}^{n}{p_i\cdot{q_i}}}$

- `pearson(p, q)`

Returns the [Jaccard similarity](http://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf) between vectors p and q
Returns the [Pearson correlation](https://en.wikipedia.org/wiki/Pearson_correlation_coefficient) between vectors p and q, i.e. the cosine similarity of the mean-centred vectors

- `dice(p, q)`

Expand All @@ -329,8 +345,8 @@ Refer to [ml-tree-similarity](https://github.com/mljs/tree-similarity)

## Contributing

A new metric should normally be in its own file in the src/dist directory. There should be a corresponding test file in test/dist.
The metric should be then added in the exports of src/index.js with a relatively small but understandable name (use camelCase).
A new metric should normally be in its own file in the `src/distances` (or `src/similarities`) directory. There should be a corresponding test file in the neighbouring `__tests__` directory.
The metric should be then added in the exports of `src/distances.ts` (or `src/similarities.ts`) with a relatively small but understandable name (use camelCase).
It should also be added to this README with either a link to the formula or an inline description.

## Authors
Expand Down
4 changes: 0 additions & 4 deletions babel.config.js

This file was deleted.

4 changes: 4 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { defineConfig, globalIgnores } from 'eslint/config';
import ts from 'eslint-config-cheminfo-typescript';

export default defineConfig(globalIgnores(['coverage', 'lib']), ts);
9 changes: 0 additions & 9 deletions eslint.config.mjs

This file was deleted.

60 changes: 28 additions & 32 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,26 @@
"name": "ml-distance",
"version": "4.0.1",
"description": "Distance and similarity functions to compare vectors",
"main": "lib/index.js",
"module": "./lib-esm/index.js",
"type": "module",
"exports": {
".": "./lib/index.js"
},
"files": [
"src",
"lib",
"lib-esm"
"src"
],
"types": "./lib/index.d.ts",
"scripts": {
"check-types": "tsc --noEmit",
"clean": "rimraf lib lib-esm",
"eslint": "eslint src",
"eslint-fix": "npm run eslint -- --fix",
"clean": "rimraf coverage lib",
"eslint": "eslint .",
"eslint-fix": "eslint . --fix",
"prepack": "npm run tsc",
"prettier": "prettier --check src",
"prettier-write": "prettier --write src",
"test": "npm run test-coverage && npm run eslint && npm run prettier && npm run check-types",
"test-only": "vitest run",
"test-coverage": "vitest run --coverage",
"tsc": "npm run clean && npm run tsc-cjs && npm run tsc-esm",
"tsc-cjs": "tsc --project tsconfig.cjs.json",
"tsc-esm": "tsc --project tsconfig.esm.json"
"prettier": "prettier --check .",
"prettier-write": "prettier --write .",
"test": "npm run test-only && npm run check-types && npm run eslint && npm run prettier",
"test-only": "vitest run --coverage",
"tsc": "npm run clean && npm run tsc-build",
"tsc-build": "tsc --project tsconfig.build.json"
},
"repository": {
"type": "git",
Expand All @@ -49,23 +47,21 @@
"url": "https://github.com/mljs/distance/issues"
},
"homepage": "https://github.com/mljs/distance",
"devDependencies": {
"@babel/plugin-transform-modules-commonjs": "^7.25.7",
"@babel/preset-typescript": "^7.25.7",
"@types/node": "^22.7.6",
"@vitest/coverage-v8": "^2.1.3",
"cheminfo-types": "^1.8.0",
"eslint": "^9.12.0",
"eslint-config-cheminfo-typescript": "^16.0.0",
"esm": "^3.2.25",
"prettier": "^3.3.3",
"rimraf": "^6.0.1",
"typescript": "^5.6.3",
"vitest": "^2.1.3"
},
"dependencies": {
"ml-array-mean": "^1.1.6",
"ml-distance-euclidean": "^2.0.0",
"cheminfo-types": "^1.15.0",
"ml-array-mean": "^2.0.0",
"ml-distance-euclidean": "^3.0.1",
"ml-tree-similarity": "^1.0.0"
},
"devDependencies": {
"@types/node": "^26.1.1",
"@vitest/coverage-v8": "^4.1.10",
"@zakodium/tsconfig": "^1.0.5",
"eslint": "^9.39.5",
"eslint-config-cheminfo-typescript": "^22.1.0",
"prettier": "^3.9.6",
"rimraf": "^6.1.3",
"typescript": "^6.0.3",
"vitest": "^4.1.10"
}
}
6 changes: 2 additions & 4 deletions scripts/similarity.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { distance } from '../src';
import { distance } from '../src/index.ts';

let results = [];

Expand All @@ -16,9 +16,7 @@ let v11 = [-0.6, 0.7];

for (let algorithm in distance) {
if (algorithm.match(/fidelity|harmonicMean|innerProduct|ruzicka/)) continue;
if (algorithm.match(/minkowski/)) {
if (algorithm.match(/motyka/)) continue;
} // does not give a 0 with identical vector
if (algorithm.match(/minkowski/) && algorithm.match(/motyka/)) continue; // does not give a 0 with identical vector
let result = { algorithm };
let callback = distance[algorithm];
result.self = callback(v1, v1);
Expand Down
1 change: 1 addition & 0 deletions src/.npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
__tests__
.DS_Store
.npmignore
Loading