Skip to content

Commit

Permalink
Use FloatCore for compatiblity with ordered-float
Browse files Browse the repository at this point in the history
  • Loading branch information
msk committed Dec 21, 2023
1 parent d637ab3 commit a73c213
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ codecov = { repository = "petabi/petal-clustering", service = "github" }
[dependencies]
ndarray = "0.15"
num-traits = "0.2"
petal-neighbors = "0.9.0"
petal-neighbors = "0.10.0"
rayon = "1"
serde = { version = "1", features = ["derive"] }
succinct = "0.5"
Expand Down
8 changes: 4 additions & 4 deletions src/dbscan.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ndarray::{ArrayBase, Data, Ix2};
use num_traits::{Float, FromPrimitive};
use num_traits::{float::FloatCore, FromPrimitive};
use petal_neighbors::{
distance::{Euclidean, Metric},
BallTree,
Expand Down Expand Up @@ -41,7 +41,7 @@ pub struct Dbscan<A, M> {

impl<A> Default for Dbscan<A, Euclidean>
where
A: Float,
A: FloatCore,
{
#[must_use]
fn default() -> Self {
Expand All @@ -66,7 +66,7 @@ impl<A, M> Dbscan<A, M> {

impl<S, A, M> Fit<ArrayBase<S, Ix2>, (HashMap<usize, Vec<usize>>, Vec<usize>)> for Dbscan<A, M>
where
A: AddAssign + DivAssign + Float + FromPrimitive + Sync,
A: AddAssign + DivAssign + FloatCore + FromPrimitive + Sync,
S: Data<Elem = A>,
M: Metric<A> + Clone + Sync,
{
Expand Down Expand Up @@ -112,7 +112,7 @@ where

fn build_neighborhoods<S, A, M>(input: &ArrayBase<S, Ix2>, eps: A, metric: M) -> Vec<Vec<usize>>
where
A: AddAssign + DivAssign + Float + FromPrimitive + Sync,
A: AddAssign + DivAssign + FloatCore + FromPrimitive + Sync,
S: Data<Elem = A>,
M: Metric<A> + Sync,
{
Expand Down
26 changes: 13 additions & 13 deletions src/hdbscan.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ndarray::{Array1, ArrayBase, ArrayView1, ArrayView2, Data, Ix2};
use num_traits::{Float, FromPrimitive};
use num_traits::{float::FloatCore, FromPrimitive};
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
Expand Down Expand Up @@ -29,7 +29,7 @@ pub struct HDbscan<A, M> {

impl<A> Default for HDbscan<A, Euclidean>
where
A: Float,
A: FloatCore,
{
#[must_use]
fn default() -> Self {
Expand All @@ -46,7 +46,7 @@ where

impl<S, A, M> Fit<ArrayBase<S, Ix2>, (HashMap<usize, Vec<usize>>, Vec<usize>)> for HDbscan<A, M>
where
A: AddAssign + DivAssign + Float + FromPrimitive + Sync + Send + TryFrom<u32>,
A: AddAssign + DivAssign + FloatCore + FromPrimitive + Sync + Send + TryFrom<u32>,
<A as std::convert::TryFrom<u32>>::Error: Debug,
S: Data<Elem = A>,
M: Metric<A> + Clone + Sync + Send,
Expand Down Expand Up @@ -92,7 +92,7 @@ where
}
}

fn mst_linkage<A: Float>(
fn mst_linkage<A: FloatCore>(
input: ArrayView2<A>,
metric: &dyn Metric<A>,
core_distances: ArrayView1<A>,
Expand Down Expand Up @@ -176,7 +176,7 @@ fn mst_linkage<A: Float>(
unsafe { mst.assume_init() }
}

fn label<A: Float>(mst: Array1<(usize, usize, A)>) -> Array1<(usize, usize, A, usize)> {
fn label<A: FloatCore>(mst: Array1<(usize, usize, A)>) -> Array1<(usize, usize, A, usize)> {
let n = mst.len() + 1;
let mut uf = UnionFind::new(n);
mst.into_iter()
Expand All @@ -188,7 +188,7 @@ fn label<A: Float>(mst: Array1<(usize, usize, A)>) -> Array1<(usize, usize, A, u
.collect()
}

fn condense_mst<A: Float + Div>(
fn condense_mst<A: FloatCore + Div>(
mst: ArrayView1<(usize, usize, A, usize)>,
min_cluster_size: usize,
) -> Vec<(usize, usize, A, usize)> {
Expand Down Expand Up @@ -275,7 +275,7 @@ fn condense_mst<A: Float + Div>(
result
}

fn get_stability<A: Float + AddAssign + Sub + TryFrom<u32>>(
fn get_stability<A: FloatCore + AddAssign + Sub + TryFrom<u32>>(
condensed_tree: &ArrayView1<(usize, usize, A, usize)>,
) -> HashMap<usize, A>
where
Expand Down Expand Up @@ -310,7 +310,7 @@ where
)
}

fn find_clusters<A: Float + AddAssign + Sub + TryFrom<u32>>(
fn find_clusters<A: FloatCore + AddAssign + Sub + TryFrom<u32>>(
condensed_tree: &ArrayView1<(usize, usize, A, usize)>,
) -> (HashMap<usize, Vec<usize>>, Vec<usize>)
where
Expand Down Expand Up @@ -411,7 +411,7 @@ fn bfs_tree(tree: &[(usize, usize)], root: usize) -> Vec<usize> {
result
}

fn bfs_mst<A: Float>(mst: ArrayView1<(usize, usize, A, usize)>, start: usize) -> Vec<usize> {
fn bfs_mst<A: FloatCore>(mst: ArrayView1<(usize, usize, A, usize)>, start: usize) -> Vec<usize> {
let n = mst.len() + 1;

let mut to_process = vec![start];
Expand Down Expand Up @@ -545,7 +545,7 @@ impl UnionFind {
#[allow(dead_code)]
struct Boruvka<'a, A, M>
where
A: Float,
A: FloatCore,
M: Metric<A>,
{
db: BallTree<'a, A, M>,
Expand All @@ -560,7 +560,7 @@ where
#[allow(dead_code)]
impl<'a, A, M> Boruvka<'a, A, M>
where
A: Float + AddAssign + DivAssign + FromPrimitive + Sync + Send,
A: FloatCore + AddAssign + DivAssign + FromPrimitive + Sync + Send,
M: Metric<A> + Sync + Send,
{
fn new(db: BallTree<'a, A, M>, min_samples: usize) -> Self {
Expand Down Expand Up @@ -789,7 +789,7 @@ fn compute_core_distances<A, M>(
candidates: &mut Candidates<A>,
) -> Array1<A>
where
A: AddAssign + DivAssign + FromPrimitive + Float + Sync + Send,
A: AddAssign + DivAssign + FromPrimitive + FloatCore + Sync + Send,
M: Metric<A> + Sync + Send,
{
let mut knn_indices = vec![0; db.points.nrows() * min_samples];
Expand Down Expand Up @@ -828,7 +828,7 @@ struct Candidates<A> {
}

#[allow(dead_code)]
impl<A: Float> Candidates<A> {
impl<A: FloatCore> Candidates<A> {
fn new(n: usize) -> Self {
// define max_value as NULL
let neighbors = vec![u32::max_value(); n];
Expand Down
16 changes: 8 additions & 8 deletions src/optics.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ndarray::{Array, ArrayBase, Data, Ix2};
use num_traits::{Float, FromPrimitive};
use num_traits::{float::FloatCore, FromPrimitive};
use petal_neighbors::{
distance::{Euclidean, Metric},
BallTree,
Expand Down Expand Up @@ -46,7 +46,7 @@ pub struct Optics<A, M> {

impl<A> Default for Optics<A, Euclidean>
where
A: Float,
A: FloatCore,
{
#[must_use]
fn default() -> Self {
Expand All @@ -63,7 +63,7 @@ where

impl<A, M> Optics<A, M>
where
A: Float,
A: FloatCore,
M: Metric<A>,
{
#[must_use]
Expand Down Expand Up @@ -111,7 +111,7 @@ where

impl<S, A, M> Fit<ArrayBase<S, Ix2>, (HashMap<usize, Vec<usize>>, Vec<usize>)> for Optics<A, M>
where
A: AddAssign + DivAssign + Float + FromPrimitive + Send + Sync,
A: AddAssign + DivAssign + FloatCore + FromPrimitive + Send + Sync,
S: Data<Elem = A> + Sync,
M: Metric<A> + Clone + Sync,
{
Expand Down Expand Up @@ -160,7 +160,7 @@ fn process<S, A, M>(
reacheability: &mut [A],
visited: &mut [bool],
) where
A: Float,
A: FloatCore,
S: Data<Elem = A>,
M: Metric<A>,
{
Expand Down Expand Up @@ -216,7 +216,7 @@ fn update<S, A, M>(
seeds: &mut Vec<usize>,
reacheability: &mut [A],
) where
A: Float,
A: FloatCore,
S: Data<Elem = A>,
M: Metric<A>,
{
Expand Down Expand Up @@ -252,7 +252,7 @@ fn build_neighborhoods<S, A, M>(
metric: M,
) -> Vec<Neighborhood<A>>
where
A: AddAssign + DivAssign + Float + FromPrimitive + Send + Sync,
A: AddAssign + DivAssign + FloatCore + FromPrimitive + Send + Sync,
S: Data<Elem = A>,
M: Metric<A> + Sync,
{
Expand Down Expand Up @@ -285,7 +285,7 @@ fn reacheability_distance<S, A, M>(
metric: &M,
) -> A
where
A: Float,
A: FloatCore,
S: Data<Elem = A>,
M: Metric<A>,
{
Expand Down

0 comments on commit a73c213

Please sign in to comment.