1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
use ndarray::{ArrayView1, ArrayView2};
use petal_neighbors::{distance, BallTree};
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use super::Fit;

/// OPTICS (ordering points to identify the clustering structure) clustering
/// algorithm.
#[derive(Debug, Deserialize, Serialize)]
pub struct Optics {
    /// The radius of a neighborhood.
    pub eps: f64,

    /// The minimum number of points required to form a dense region.
    pub min_samples: usize,

    ordered: Vec<usize>,
    reacheability: Vec<f64>,
    neighborhoods: Vec<Neighborhood>,
}

impl Default for Optics {
    #[must_use]
    fn default() -> Self {
        Self {
            eps: 0.5,
            min_samples: 5,
            ordered: vec![],
            reacheability: vec![],
            neighborhoods: vec![],
        }
    }
}

impl Optics {
    #[must_use]
    pub fn new(eps: f64, min_samples: usize) -> Self {
        Self {
            eps,
            min_samples,
            ordered: vec![],
            reacheability: vec![],
            neighborhoods: vec![],
        }
    }

    #[must_use]
    pub fn extract_clusters_and_outliers(
        &self,
        eps: f64,
    ) -> (HashMap<usize, Vec<usize>>, Vec<usize>) {
        let mut outliers = vec![];
        let mut clusters: HashMap<usize, Vec<usize>> = HashMap::new();

        for &id in &self.ordered {
            if self.reacheability[id].is_normal() && self.reacheability[id] <= eps {
                if clusters.is_empty() {
                    outliers.push(id);
                } else {
                    let v = clusters
                        .get_mut(&(clusters.len() - 1))
                        .expect("cluster map crashed");
                    v.push(id);
                }
            } else {
                let n = &self.neighborhoods[id];
                if n.neighbors.len() >= self.min_samples && n.core_distance <= eps {
                    clusters.entry(clusters.len()).or_insert_with(|| vec![id]);
                } else {
                    outliers.push(id);
                }
            }
        }
        (clusters, outliers)
    }
}

impl<'a> Fit<'a> for Optics {
    type Input = ArrayView2<'a, f64>;
    type Output = (HashMap<usize, Vec<usize>>, Vec<usize>);

    fn fit(&mut self, input: Self::Input) -> Self::Output {
        if input.is_empty() {
            return (HashMap::new(), vec![]);
        }
        self.neighborhoods = build_neighborhoods(&input, self.eps);
        let mut visited = vec![false; input.nrows()];
        self.ordered = Vec::with_capacity(input.nrows());
        self.reacheability = vec![std::f64::NAN; input.nrows()];
        for (idx, n) in self.neighborhoods.iter().enumerate() {
            if visited[idx] || n.neighbors.len() < self.min_samples {
                continue;
            }
            process(
                idx,
                &input,
                self.min_samples,
                &self.neighborhoods,
                &mut self.ordered,
                &mut self.reacheability,
                &mut visited,
            );
        }
        self.extract_clusters_and_outliers(self.eps)
    }
}

fn process<'a>(
    idx: usize,
    input: &ArrayView2<'a, f64>,
    min_samples: usize,
    neighborhoods: &[Neighborhood],
    ordered: &mut Vec<usize>,
    reacheability: &mut Vec<f64>,
    visited: &mut [bool],
) {
    let mut to_visit = vec![idx];
    while let Some(cur) = to_visit.pop() {
        if visited[cur] {
            continue;
        }
        visited[cur] = true;
        ordered.push(cur);
        if neighborhoods[cur].neighbors.len() < min_samples {
            continue;
        }
        let mut seeds = vec![];
        update(
            cur,
            &neighborhoods[cur],
            input,
            &visited,
            &mut seeds,
            reacheability,
        );
        while let Some(s) = seeds.pop() {
            if visited[s] {
                continue;
            }
            visited[s] = true;
            ordered.push(s);
            let n = &neighborhoods[s];
            if n.neighbors.len() < min_samples {
                continue;
            }
            update(
                s,
                &neighborhoods[s],
                input,
                &visited,
                &mut seeds,
                reacheability,
            );
        }
    }
}

fn update<'a>(
    id: usize,
    neighborhood: &Neighborhood,
    input: &ArrayView2<'a, f64>,
    visited: &[bool],
    seeds: &mut Vec<usize>,
    reacheability: &mut [f64],
) {
    for &o in &neighborhood.neighbors {
        if visited[o] {
            continue;
        }
        let reachdist = reacheability_distance(o, id, input, neighborhood);
        if !reacheability[o].is_normal() {
            reacheability[o] = reachdist;
            seeds.push(o);
        } else if reacheability[o].lt(&reachdist) {
            reacheability[o] = reachdist;
        }
    }
    seeds.sort_unstable_by(|a, b| {
        reacheability[*a]
            .partial_cmp(&reacheability[*b])
            .unwrap()
            .reverse()
    });
}

#[derive(Debug, Deserialize, Serialize)]
struct Neighborhood {
    pub neighbors: Vec<usize>,
    pub core_distance: f64,
}

fn build_neighborhoods<'a>(input: &ArrayView2<'a, f64>, eps: f64) -> Vec<Neighborhood> {
    let rows: Vec<_> = input.genrows().into_iter().collect();
    let db = BallTree::with_metric(*input, distance::EUCLIDEAN);
    rows.into_par_iter()
        .map(|p| {
            let neighbors = db.query_radius(&p, eps).into_iter().collect::<Vec<usize>>();
            let core_distance = if neighbors.len() > 1 {
                let ns = db.query(&p, 2);
                if ns[0].distance.gt(&ns[1].distance) {
                    ns[0].distance
                } else {
                    ns[1].distance
                }
            } else {
                0.0
            };
            Neighborhood {
                neighbors,
                core_distance,
            }
        })
        .collect()
}

fn distance(a: &ArrayView1<f64>, b: &ArrayView1<f64>) -> f64 {
    (a - b).mapv(|x| x.powi(2)).sum().sqrt()
}

fn reacheability_distance<'a>(
    o: usize,
    p: usize,
    input: &ArrayView2<'a, f64>,
    neighbors: &Neighborhood,
) -> f64 {
    let dist = distance(&input.row(o), &input.row(p));
    if dist.gt(&neighbors.core_distance) {
        dist
    } else {
        neighbors.core_distance
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use maplit::hashmap;
    use ndarray::aview2;

    #[test]
    fn optics() {
        let data = vec![
            [1.0, 2.0],
            [1.1, 2.2],
            [0.9, 1.9],
            [1.0, 2.1],
            [-2.0, 3.0],
            [-2.2, 3.1],
        ];
        let input = aview2(&data);

        let mut model = Optics::new(0.5, 2);
        let (mut clusters, mut outliers) = model.fit(input);
        outliers.sort_unstable();
        for (_, v) in clusters.iter_mut() {
            v.sort_unstable();
        }

        assert_eq!(hashmap! {0 => vec![0, 1, 2, 3], 1 => vec![4, 5]}, clusters);
        assert_eq!(Vec::<usize>::new(), outliers);
    }

    #[test]
    fn core_samples() {
        let data = vec![[0.], [2.], [3.], [4.], [6.], [8.], [10.]];
        let mut model = Optics::new(1.01, 1);
        let (clusters, outliers) = model.fit(aview2(&data));
        assert_eq!(clusters.len(), 5); // {0: [0], 1: [1, 2, 3], 2: [4], 3: [5], 4: [6]}
        assert!(outliers.is_empty());
    }

    #[test]
    fn fit_empty() {
        let data: Vec<[f64; 8]> = vec![];
        let input = aview2(&data);

        let mut model = Optics::new(0.5, 2);
        let (clusters, outliers) = model.fit(input);
        assert!(clusters.is_empty());
        assert!(outliers.is_empty());
    }
}