use std::collections::VecDeque;
use num_traits::Float;
use rayon::iter::{IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator};
use srtree::{Euclidean, SRTree};
pub type Labels = Vec<i32>;
pub type Lifetimes = Vec<i32>;
pub fn fit<T>(data: &[Vec<T>], eps: T) -> (Labels, Lifetimes)
where
T: Float + Send + Sync,
{
let indices = build_neighbour_graph(data, eps);
let (labels, lifetime) = fit_with(&indices);
(labels, lifetime)
}
pub fn fit_with(indices: &[Vec<usize>]) -> (Labels, Lifetimes) {
let (lifetimes, sorted_ids) = compute_lifetime(indices);
let labels = cluster(indices, &lifetimes, &sorted_ids);
(labels, lifetimes)
}
fn build_neighbour_graph<T>(data: &[Vec<T>], eps: T) -> Vec<Vec<usize>>
where
T: Float + Send + Sync,
{
let index = SRTree::default(data, Euclidean::default()).unwrap();
data.par_iter()
.enumerate()
.map(|(row_i, p)| {
let mut neighbours = index.query_radius(p, eps);
neighbours.retain(|col_id| *col_id != row_i);
neighbours
})
.collect()
}
fn compute_lifetime(indices: &[Vec<usize>]) -> (Lifetimes, Vec<usize>) {
let n = indices.len();
let mut lifetimes = vec![-1; n];
let mut sorted_ids = vec![0; n];
let mut filter = vec![false; n];
let mut buckets = Vec::with_capacity(8);
let mut degree: Vec<_> = indices
.iter()
.enumerate()
.map(|(idx, neighbours)| {
let d = neighbours.len();
while d >= buckets.len() {
buckets.push(Vec::with_capacity(n / 8));
}
unsafe { buckets.get_unchecked_mut(d).push(idx) };
d
})
.collect();
let mut rpos = n; let mut r = 1; let mut current_min_deg = 0;
let mut worklist: Vec<usize> = Vec::with_capacity(64);
while current_min_deg < buckets.len() {
let mut current_bucket = std::mem::take(&mut buckets[current_min_deg]);
current_bucket.retain(|idx| *unsafe { lifetimes.get_unchecked(*idx) } == -1);
if current_bucket.is_empty() {
current_min_deg += 1;
continue;
}
for idx in current_bucket {
*unsafe { lifetimes.get_unchecked_mut(idx) } = r;
rpos -= 1;
*unsafe { sorted_ids.get_unchecked_mut(rpos) } = idx;
for &nb in unsafe { indices.get_unchecked(idx) }.iter() {
if unsafe { *lifetimes.get_unchecked(nb) } != -1 {
continue; }
let is_known = unsafe { filter.get_unchecked_mut(nb) };
if !*is_known {
*is_known = true;
worklist.push(nb);
}
}
}
for nb in worklist.drain(..) {
unsafe { *filter.get_unchecked_mut(nb) = false };
if *unsafe { lifetimes.get_unchecked(nb) } != -1 {
continue;
}
let deg = unsafe { degree.get_unchecked_mut(nb) };
if *deg < current_min_deg {
continue;
};
*deg -= 1;
unsafe { buckets.get_unchecked_mut(*deg).push(nb) };
}
r += 1
}
(lifetimes, sorted_ids)
}
fn cluster(indices: &[Vec<usize>], lifetimes: &[i32], sorted_ids: &[usize]) -> Labels {
let n = indices.len();
assert_eq!(lifetimes.len(), n);
assert_eq!(sorted_ids.len(), n);
let mut labels = vec![-2; n];
let mut filter = vec![false; n];
let mut seeds_idx = Vec::with_capacity(32);
for (idx, lt) in lifetimes.iter().enumerate() {
let (mut low, mut high) = (0, 0);
let indices = unsafe { indices.get_unchecked(idx) };
for ii in indices.iter() {
let nb_lt = unsafe { lifetimes.get_unchecked(*ii) };
if lt < nb_lt {
high += 1;
}
if lt > nb_lt {
low += 1;
}
}
let label = unsafe { labels.get_unchecked_mut(idx) };
if indices.is_empty() {
*label = -1;
} else {
if high < low {
*unsafe { filter.get_unchecked_mut(idx) } = true;
}
if high == 0 && low > 0 {
seeds_idx.push(idx);
}
}
}
let mut cluster_id = 0;
let mut to_visit = VecDeque::with_capacity(64);
for seed_idx in seeds_idx {
if !unsafe { *filter.get_unchecked(seed_idx) } {
continue;
}
to_visit.push_back(seed_idx);
while let Some(idx) = to_visit.pop_front() {
let first_visit = unsafe { filter.get_unchecked_mut(idx) };
if *first_visit {
*first_visit = false;
*unsafe { labels.get_unchecked_mut(idx) } = cluster_id;
for nb in unsafe { indices.get_unchecked(idx).iter() } {
if *unsafe { filter.get_unchecked(*nb) } {
to_visit.push_back(*nb);
}
}
}
}
cluster_id += 1;
}
let mut counts: Vec<usize> = vec![0; cluster_id as usize + 1];
for &idx in sorted_ids.iter() {
if unsafe { *labels.get_unchecked(idx) } != -2 {
continue;
}
counts.fill_with(Default::default);
let mut max_count = 0;
let mut max_label = -1;
for &nb in unsafe { indices.get_unchecked(idx) }.iter() {
let nb_label = unsafe { *labels.get_unchecked(nb) };
if nb_label < 0 {
continue;
}
let count = unsafe { counts.get_unchecked_mut(nb_label as usize) };
*count += 1;
if *count > max_count {
max_count = *count;
max_label = nb_label;
}
}
*unsafe { labels.get_unchecked_mut(idx) } = max_label;
}
labels
}