Skip to content

Commit

Permalink
Add JS bindings for clustering crate
Browse files Browse the repository at this point in the history
  • Loading branch information
sd2k committed Jul 19, 2024
1 parent d78a35d commit 970e93a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
1 change: 1 addition & 0 deletions crates/augurs-js/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ default = ["console_error_panic_hook"]

[dependencies]
augurs-changepoint = { workspace = true }
augurs-clustering = { workspace = true }
augurs-core = { workspace = true }
augurs-dtw = { workspace = true }
augurs-ets = { workspace = true, features = ["mstl"] }
Expand Down
49 changes: 49 additions & 0 deletions crates/augurs-js/src/clustering.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//! Bindings for clustering algorithms.

use serde::Deserialize;
use tsify_next::Tsify;
use wasm_bindgen::prelude::*;

use crate::dtw::DistanceMatrix;

/// Options for the dynamic time warping calculation.
#[derive(Clone, Debug, Default, Deserialize, Tsify)]
#[serde(rename_all = "camelCase")]
#[tsify(from_wasm_abi)]
pub struct DbscanOpts {
/// The maximum distance between two samples for one to be considered as in the
/// neighborhood of the other.
pub epsilon: f64,

/// The number of samples in a neighborhood for a point to be considered as a core
/// point.
pub min_cluster_size: usize,
}

/// A DBSCAN clustering algorithm.
#[derive(Debug)]
#[wasm_bindgen]
pub struct Dbscan {
inner: augurs_clustering::Dbscan,
}

#[wasm_bindgen]
impl Dbscan {
/// Create a new DBSCAN instance.
#[wasm_bindgen(constructor)]
pub fn new(opts: DbscanOpts) -> Self {
Self {
inner: augurs_clustering::Dbscan::new(opts.epsilon, opts.min_cluster_size),
}
}

/// Fit the DBSCAN clustering algorithm to the given distance matrix.
///
/// The distance matrix can be obtained using the `Dtw` class.
///
/// The return value is an `Int32Array` of cluster IDs, with `-1` indicating noise.
#[wasm_bindgen]
pub fn fit(&self, distanceMatrix: &DistanceMatrix) -> Vec<isize> {
self.inner.fit(distanceMatrix.inner())
}
}
16 changes: 10 additions & 6 deletions crates/augurs-js/src/dtw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,24 @@ pub struct DtwOpts {
#[derive(Debug)]
#[wasm_bindgen]
pub struct DistanceMatrix {
inner: Vec<Vec<f64>>,
inner: augurs_core::DistanceMatrix,
}

impl DistanceMatrix {
pub fn inner(&self) -> &augurs_core::DistanceMatrix {
&self.inner
}
}

impl From<augurs_core::DistanceMatrix> for DistanceMatrix {
fn from(matrix: augurs_core::DistanceMatrix) -> Self {
Self {
inner: matrix.into_inner(),
}
fn from(inner: augurs_core::DistanceMatrix) -> Self {
Self { inner }
}
}

impl From<DistanceMatrix> for augurs_core::DistanceMatrix {
fn from(matrix: DistanceMatrix) -> Self {
Self::try_from_square(matrix.inner).unwrap()
Self::try_from_square(matrix.inner.into_inner()).unwrap()
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/augurs-js/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use tsify_next::Tsify;
use wasm_bindgen::prelude::*;

mod changepoints;
pub mod clustering;
mod dtw;
pub mod ets;
pub mod mstl;
Expand Down

0 comments on commit 970e93a

Please sign in to comment.