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
use thiserror::Error;

/// An error when fitting with an invalid hyperparameter
#[derive(Error, Debug)]
pub enum KMeansParamsError {
    #[error("n_clusters cannot be 0")]
    NClusters,
    #[error("n_runs cannot be 0")]
    NRuns,
    #[error("tolerance must be greater than 0")]
    Tolerance,
    #[error("max_n_iterations cannot be 0")]
    MaxIterations,
}

/// An error when modeling a KMeans algorithm
#[derive(Error, Debug)]
pub enum KMeansError {
    /// When any of the hyperparameters are set the wrong value
    #[error("Invalid hyperparameter: {0}")]
    InvalidParams(#[from] KMeansParamsError),
    /// When inertia computation fails
    #[error("Fitting failed: No inertia improvement (-inf)")]
    InertiaError,
    #[error(transparent)]
    LinfaError(#[from] linfa::error::Error),
}

#[derive(Error, Debug)]
pub enum IncrKMeansError<M: std::fmt::Debug> {
    /// When any of the hyperparameters are set the wrong value
    #[error("Invalid hyperparameter: {0}")]
    InvalidParams(#[from] KMeansParamsError),
    /// When the distance between the old and new centroids exceeds the tolerance parameter. Not an
    /// actual error, just there to signal that the algorithm should keep running.
    #[error("Algorithm has not yet converged, Keep on running the algorithm.")]
    NotConverged(M),
    #[error(transparent)]
    LinfaError(#[from] linfa::error::Error),
}