-
Notifications
You must be signed in to change notification settings - Fork 1
feat (umap, clustering): Added UMAP projection and clustering benchmark pipeline #14
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
76a2566
implemented umap along with different clustering algo
Harsh16gupta 636709c
improved hbdscan clustering (by reducing the noise) and in the testEm…
Harsh16gupta b11d24f
removed xmeans algo completely
Harsh16gupta ee22b80
removed kmeans-8
Harsh16gupta c9b5779
refactor(clustering): deduplicate PRNG and distance metrics into shar…
Harsh16gupta c4cecd4
addressed Copilot review comments
Harsh16gupta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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,66 @@ | ||
| import { UMAP } from 'umap-js'; | ||
| import { log } from '../utils/logger'; | ||
| import { mulberry32 } from '../utils/prng'; | ||
| import { cosineDistance, euclideanDistance } from './clustering/metrics'; | ||
| import { UmapProjectorOptions } from '../types/projector'; | ||
|
|
||
| export class UmapProjector { | ||
| private readonly nComponents: number; | ||
| private readonly nNeighbors: number; | ||
| private readonly minDist: number; | ||
| private readonly metric: 'cosine' | 'euclidean'; | ||
| private readonly seed: number; | ||
|
|
||
| constructor(options: UmapProjectorOptions = {}) { | ||
| this.nComponents = options.nComponents ?? 2; | ||
| this.nNeighbors = options.nNeighbors ?? 15; | ||
| this.minDist = options.minDist ?? 0.1; | ||
| this.metric = options.metric ?? 'cosine'; | ||
| this.seed = options.seed ?? 42; | ||
| } | ||
|
|
||
| /** | ||
| * Projects high-dimensional vectors to a lower-dimensional space using UMAP. | ||
| * @param vectors N vectors of dimension D (N x D) | ||
| * @returns N vectors of dimension nComponents | ||
| */ | ||
| public project(vectors: number[][]): number[][] { | ||
| if (vectors.length === 0) return []; | ||
|
|
||
| const dim = vectors[0].length; | ||
| for (let i = 0; i < vectors.length; i++) { | ||
| if (vectors[i].length !== dim) { | ||
| throw new Error(`Vector at index ${i} has dimension ${vectors[i].length}, expected ${dim}`); | ||
| } | ||
| } | ||
|
|
||
| // UMAP needs more points than output dimensions to be meaningful | ||
| if (vectors.length <= this.nComponents) { | ||
| log(`Too few vectors (${vectors.length}) for ${this.nComponents}D projection, padding with zeros.`); | ||
| return vectors.map((vec) => { | ||
| const out = vec.slice(0, this.nComponents); | ||
| while (out.length < this.nComponents) out.push(0); | ||
| return out; | ||
| }); | ||
| } | ||
|
|
||
| // nNeighbors must be less than the number of data points | ||
| const nNeighbors = Math.max(2, Math.min(this.nNeighbors, vectors.length - 1)); | ||
| const distanceFn = this.metric === 'euclidean' ? euclideanDistance : cosineDistance; | ||
|
|
||
| const umap = new UMAP({ | ||
| nComponents: this.nComponents, | ||
| nNeighbors, | ||
| minDist: this.minDist, | ||
| distanceFn, | ||
| random: mulberry32(this.seed), | ||
| }); | ||
|
|
||
| log( | ||
| `UMAP: projecting ${vectors.length} vectors (${dim}D → ${this.nComponents}D), ` + | ||
| `neighbors=${nNeighbors}, seed=${this.seed}`, | ||
| ); | ||
|
|
||
| return umap.fit(vectors); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.