rustyml 0.11.0

A high-performance machine learning & deep learning library in pure Rust, offering ML algorithms and neural network support
Documentation
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
pub use super::DistanceCalculationMetric;
use super::helper_function::preliminary_check;
use crate::error::ModelError;
use crate::math::{manhattan_distance_row, minkowski_distance_row, squared_euclidean_distance_row};
use crate::{Deserialize, Serialize};
use ahash::AHashSet;
use indicatif::{ProgressBar, ProgressStyle};
use ndarray::{Array1, ArrayBase, ArrayView1, Data, Ix2};
use rayon::prelude::{IntoParallelIterator, ParallelBridge, ParallelIterator};
use std::collections::VecDeque;

/// Threshold for parallelization: only use parallel processing for larger datasets
const DBSCAN_PARALLEL_THRESHOLD: usize = 1000;

/// DBSCAN (Density-Based Spatial Clustering of Applications with Noise) algorithm implementation
///
/// DBSCAN is a popular density-based clustering algorithm that can discover clusters of arbitrary shapes
/// without requiring the number of clusters to be specified beforehand.
///
/// # Fields
///
/// - `eps` - Neighborhood radius used to find neighbors
/// - `min_samples` - Minimum number of neighbors required to form a core point
/// - `metric` - Distance metric, options: Euclidean, Manhattan, Minkowski(p=3)
///
/// # Examples
/// ```rust
/// use rustyml::machine_learning::dbscan::DBSCAN;
/// use ndarray::Array2;
/// use rustyml::machine_learning::DistanceCalculationMetric;
///
/// let data = Array2::from_shape_vec((5, 2), vec![
///     0.0, 0.0,
///     0.1, 0.1,
///     1.0, 1.0,
///     1.1, 1.1,
///     2.0, 2.0,
/// ]).unwrap();
///
/// let mut dbscan = DBSCAN::new(0.5, 2, DistanceCalculationMetric::Euclidean).unwrap();
/// let labels = dbscan.fit_predict(&data).unwrap();
/// ```
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct DBSCAN {
    eps: f64,
    min_samples: usize,
    metric: DistanceCalculationMetric,
    labels_: Option<Array1<i32>>,
    core_sample_indices: Option<Array1<usize>>,
}

impl Default for DBSCAN {
    /// Default parameters for DBSCAN model
    ///
    /// # Default Values
    /// - eps = 0.5
    /// - min_samples = 5
    /// - metric = Euclidean
    fn default() -> Self {
        DBSCAN {
            eps: 0.5,
            min_samples: 5,
            metric: DistanceCalculationMetric::Euclidean,
            labels_: None,
            core_sample_indices: None,
        }
    }
}

impl DBSCAN {
    /// Creates a new DBSCAN instance with specified parameters
    ///
    /// # Parameters
    ///
    /// - `eps` - Neighborhood radius used to find neighbors
    /// - `min_samples` - Minimum number of neighbors required to form a core point
    /// - `metric` - Distance metric to use (Euclidean, Manhattan, Minkowski)
    ///
    /// # Returns
    ///
    /// - `Ok(DBSCAN)` - A new DBSCAN instance with the specified parameters
    /// - `Err(ModelError::InputValidationError)` - If parameters are invalid (e.g., eps <= 0)
    ///
    /// # Errors
    ///
    /// Returns `ModelError::InputValidationError` if `eps` is non-positive or not finite,
    /// if `min_samples` is 0, or if Minkowski `p` is non-positive or not finite.
    pub fn new(
        eps: f64,
        min_samples: usize,
        metric: DistanceCalculationMetric,
    ) -> Result<Self, ModelError> {
        // Validate eps parameter
        if eps <= 0.0 || !eps.is_finite() {
            return Err(ModelError::InputValidationError(format!(
                "eps must be positive and finite, got {}",
                eps
            )));
        }

        // Validate min_samples parameter
        if min_samples == 0 {
            return Err(ModelError::InputValidationError(
                "min_samples must be greater than 0".to_string(),
            ));
        }

        // Validate metric parameter
        match metric {
            DistanceCalculationMetric::Minkowski(p) => {
                if p <= 0.0 || !p.is_finite() {
                    return Err(ModelError::InputValidationError(format!(
                        "Minkowski p must be positive and finite, got {}",
                        p
                    )));
                }
            }
            _ => {} // Euclidean and Manhattan don't need additional validation
        }

        Ok(DBSCAN {
            eps,
            min_samples,
            metric,
            labels_: None,
            core_sample_indices: None,
        })
    }

    // Getters
    get_field!(get_epsilon, eps, f64);
    get_field!(get_min_samples, min_samples, usize);
    get_field!(get_metric, metric, DistanceCalculationMetric);
    get_field_as_ref!(get_labels, labels_, Option<&Array1<i32>>);
    get_field_as_ref!(
        get_core_sample_indices,
        core_sample_indices,
        Option<&Array1<usize>>
    );

    /// Computes distance between two data points using the specified metric
    fn compute_distance(&self, p_row: ArrayView1<f64>, q_row: ArrayView1<f64>) -> f64 {
        match self.metric {
            DistanceCalculationMetric::Euclidean => {
                squared_euclidean_distance_row(&p_row, &q_row).sqrt()
            }
            DistanceCalculationMetric::Manhattan => manhattan_distance_row(&p_row, &q_row),
            DistanceCalculationMetric::Minkowski(p) => minkowski_distance_row(&p_row, &q_row, p),
        }
    }

    /// Find all neighbors of point `p` (points within eps distance)
    ///
    /// Uses parallelization for datasets larger than a threshold to improve performance
    fn region_query<S>(&self, data: &ArrayBase<S, Ix2>, p: usize) -> Result<Vec<usize>, ModelError>
    where
        S: Data<Elem = f64> + Send + Sync,
    {
        // Bounds check
        if p >= data.nrows() {
            return Err(ModelError::InputValidationError(format!(
                "Point index {} is out of bounds (max: {})",
                p,
                data.nrows() - 1
            )));
        }

        // Pre-compute row p (read-only view) to avoid fetching it repeatedly in each iteration
        let p_row = data.row(p);
        let eps = self.eps;
        let n_samples = data.nrows();

        let neighbors: Vec<usize> = if n_samples >= DBSCAN_PARALLEL_THRESHOLD {
            // Parallel iteration through all rows, calculating distances and filtering points that satisfy the eps condition
            (0..n_samples)
                .into_par_iter()
                .filter_map(|q| {
                    let q_row = data.row(q);
                    let dist = self.compute_distance(p_row, q_row);
                    if dist <= eps { Some(q) } else { None }
                })
                .collect()
        } else {
            // Sequential iteration for smaller datasets
            (0..n_samples)
                .filter_map(|q| {
                    let q_row = data.row(q);
                    let dist = self.compute_distance(p_row, q_row);
                    if dist <= eps { Some(q) } else { None }
                })
                .collect()
        };

        Ok(neighbors)
    }

    /// Performs DBSCAN clustering on the input data
    ///
    /// # Parameters
    /// - `data` - Input data as a reference 2D array in ndarray where each row is a sample
    ///
    /// # Returns
    /// - `Ok(&mut Self)` - The trained instance containing cluster labels and core sample indices
    /// - `Err(ModelError::InputValidationError)` - If dataset size exceeds limit or is empty
    /// - `Err(ModelError::ProcessingError)` - If numerical issues occur or cluster ID overflows
    ///
    /// # Errors
    /// - `ModelError::InputValidationError` - If the number of samples exceeds `i32::MAX`
    /// - `ModelError::ProcessingError` - If the number of discovered clusters exceeds `i32::MAX`
    ///
    /// # Performance
    /// Uses parallel processing for region queries if the number of samples is greater than or equal to 1000.
    pub fn fit<S>(&mut self, data: &ArrayBase<S, Ix2>) -> Result<&mut Self, ModelError>
    where
        S: Data<Elem = f64> + Send + Sync,
    {
        preliminary_check(&data, None)?;

        // Check if dataset is empty
        let n_samples = data.nrows();

        // Check for cluster_id overflow early
        if n_samples > i32::MAX as usize {
            return Err(ModelError::InputValidationError(
                "Dataset too large: exceeds maximum number of samples".to_string(),
            ));
        }

        let mut labels = Array1::from(vec![-1; n_samples]); // -1 represents unclassified or noise
        let mut core_samples = AHashSet::with_capacity(n_samples / 4); // Estimate 25% core samples
        let mut cluster_id = 0i32;

        // Initialize progress bar for tracking clustering progress
        let pb = ProgressBar::new(n_samples as u64);
        pb.set_style(
            ProgressStyle::default_bar()
                .template("[{elapsed_precise}] {bar:40.cyan/blue} {pos}/{len} | Clusters: {msg}")
                .expect("Failed to set progress bar template")
                .progress_chars("█▓░"),
        );
        pb.set_message("0 | Core points: 0");

        // Main loop processes each point sequentially, the algorithm as a whole remains sequential
        for p in 0..n_samples {
            pb.inc(1);
            if labels[p] != -1 {
                continue;
            }

            let neighbors = self.region_query(&data, p).map_err(|e| {
                ModelError::ProcessingError(format!("Region query failed: {:?}", e))
            })?;

            if neighbors.len() < self.min_samples {
                labels[p] = -1; // Mark as noise
                continue;
            }

            // Start a new cluster
            labels[p] = cluster_id;
            core_samples.insert(p);
            let mut seeds: VecDeque<usize> = neighbors.into_iter().collect();

            // Expand cluster (the expansion process is still sequential)
            while let Some(q) = seeds.pop_front() {
                // If already processed in this cluster, skip
                if labels[q] == cluster_id {
                    continue;
                }

                // Assign to current cluster (could be noise or unvisited)
                labels[q] = cluster_id;

                let q_neighbors = self.region_query(&data, q).map_err(|e| {
                    ModelError::ProcessingError(format!(
                        "Region query failed for point {}: {:?}",
                        q, e
                    ))
                })?;

                if q_neighbors.len() >= self.min_samples {
                    core_samples.insert(q);
                    for r in q_neighbors {
                        if labels[r] != cluster_id {
                            seeds.push_back(r);
                        }
                    }
                }
            }

            cluster_id += 1;

            // Update progress bar message with current statistics
            pb.set_message(format!(
                "{} | Core points: {}",
                cluster_id,
                core_samples.len()
            ));

            // Check for cluster_id overflow
            if cluster_id >= i32::MAX {
                pb.finish_with_message("Error: cluster ID overflow");
                return Err(ModelError::ProcessingError(
                    "Too many clusters: cluster ID overflow".to_string(),
                ));
            }
        }

        // Finish progress bar with final statistics
        pb.finish_with_message(format!(
            "{} | Core points: {} | Noise points: {}",
            cluster_id,
            core_samples.len(),
            labels.iter().filter(|&&x| x == -1).count()
        ));

        self.labels_ = Some(labels);
        // Convert HashSet to sorted Vec for consistent ordering
        let mut core_indices: Vec<usize> = core_samples.into_iter().collect();
        core_indices.sort_unstable();
        self.core_sample_indices = Some(Array1::from(core_indices));

        Ok(self)
    }

    /// Predicts cluster labels for new data points based on trained model
    ///
    /// # Parameters
    ///
    /// - `trained_data` - Original data array that was used for training
    /// - `new_data` - New data points to classify
    ///
    /// # Returns
    ///
    /// - `Ok(Array1<i32>)` - Array of predicted cluster labels
    /// - `Err(ModelError::NotFitted)` - If the model has not been fitted yet
    /// - `Err(ModelError::InputValidationError)` - If input validation fails or dimensions mismatch
    ///
    /// # Errors
    ///
    /// - `ModelError::NotFitted` - If `labels_` or `core_sample_indices` are `None`
    /// - `ModelError::InputValidationError` - If feature dimensions don't match or data contains non-finite values
    ///
    /// # Performance
    ///
    /// Processes new data points in parallel using a thread pool.
    ///
    /// # Notes
    ///
    /// New points are assigned to the nearest cluster if they are within `eps` distance
    /// of a core point, otherwise they are labeled as noise (-1)
    pub fn predict<S>(
        &self,
        trained_data: &ArrayBase<S, Ix2>,
        new_data: &ArrayBase<S, Ix2>,
    ) -> Result<Array1<i32>, ModelError>
    where
        S: Data<Elem = f64> + Send + Sync,
    {
        // Ensure the model has been trained
        let labels = self.labels_.as_ref().ok_or(ModelError::NotFitted)?;
        let core_samples = self
            .core_sample_indices
            .as_ref()
            .ok_or(ModelError::NotFitted)?;

        // Check if trained data is empty
        if trained_data.nrows() == 0 {
            return Err(ModelError::InputValidationError(
                "Trained data is empty".to_string(),
            ));
        }

        // Check if new data is empty
        if new_data.nrows() == 0 {
            return Ok(Array1::from(vec![]));
        }

        // Check dimension matching
        if trained_data.ncols() != new_data.ncols() {
            return Err(ModelError::InputValidationError(format!(
                "Feature dimension mismatch: trained data has {} features, new data has {} features",
                trained_data.ncols(),
                new_data.ncols()
            )));
        }

        if trained_data.nrows() != labels.len() {
            return Err(ModelError::InputValidationError(format!(
                "Trained data rows ({}) don't match labels length ({})",
                trained_data.nrows(),
                labels.len()
            )));
        }

        // Check for invalid values in trained data
        if trained_data.iter().any(|&val| !val.is_finite()) {
            return Err(ModelError::InputValidationError(
                "Trained data contains NaN or infinite values".to_string(),
            ));
        }

        // Check for invalid values in new data
        if new_data.iter().any(|&val| !val.is_finite()) {
            return Err(ModelError::InputValidationError(
                "New data contains NaN or infinite values".to_string(),
            ));
        }

        // Create a set for faster core sample lookup
        let core_set: AHashSet<usize> = core_samples.iter().copied().collect();

        // Process each row in parallel, collecting into Result<Vec<i32>, ModelError>
        let predictions: Result<Vec<i32>, ModelError> = new_data
            .rows()
            .into_iter()
            .par_bridge() // Convert sequential iterator to parallel iterator
            .map(|row| -> Result<i32, ModelError> {
                let mut min_dist = f64::MAX;
                let mut closest_label = -1;

                // Find the closest classified data point
                for (j, orig_row) in trained_data.rows().into_iter().enumerate() {
                    if labels[j] == -1 {
                        continue; // Skip noise points
                    }

                    let dist = self.compute_distance(row, orig_row);

                    // Check if distance computation is valid
                    if dist.is_nan() || dist.is_infinite() {
                        continue;
                    }

                    // If a core point is found within eps range, assign its label directly
                    if dist <= self.eps && core_set.contains(&j) {
                        return Ok(labels[j]);
                    }

                    if dist < min_dist {
                        min_dist = dist;
                        closest_label = labels[j];
                    }
                }

                // Only assign to closest cluster if within eps distance, otherwise mark as noise
                if min_dist <= self.eps {
                    Ok(closest_label)
                } else {
                    Ok(-1)
                }
            })
            .collect();

        Ok(Array1::from(predictions?))
    }

    /// Performs clustering and returns the labels in one step
    ///
    /// # Parameters
    ///
    /// - `data` - Input data as a 2D array where each row is a sample
    ///
    /// # Returns
    ///
    /// - `Ok(Array1<i32>)` - Array of cluster labels for each sample
    /// - `Err(ModelError)` - If fitting fails due to validation or processing errors
    ///
    /// # Performance
    ///
    /// Inherits parallelization behavior from the `fit` method.
    pub fn fit_predict<S>(&mut self, data: &ArrayBase<S, Ix2>) -> Result<Array1<i32>, ModelError>
    where
        S: Data<Elem = f64> + Send + Sync,
    {
        self.fit(data)?;
        Ok(self.labels_.as_ref().unwrap().clone())
    }

    model_save_and_load_methods!(DBSCAN);
}