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
use linfa::Float;
#[cfg(feature = "serde")]
use serde_crate::{Deserialize, Serialize};

#[cfg_attr(
    feature = "serde",
    derive(Serialize, Deserialize),
    serde(crate = "serde_crate")
)]
#[derive(Clone, Debug, PartialEq)]
/// The set of hyperparameters that can be specified for the execution of
/// the [Approximated DBSCAN algorithm](struct.AppxDbscan.html).
pub struct AppxDbscanHyperParams<F: Float> {
    /// Distance between points for them to be considered neighbours.
    tolerance: F,
    /// Minimum number of neighboring points a point needs to have to be a core
    /// point and not a noise point.
    min_points: usize,
    /// Approximation factor, allows the distance between two points
    /// for them to be considered neighbours to reach
    /// `tolerance * (1 + slack)`
    slack: F,
    appx_tolerance: F,
}

/// Helper struct used to construct a set of hyperparameters for
pub struct AppxDbscanHyperParamsBuilder<F: Float> {
    tolerance: F,
    min_points: usize,
    slack: F,
}

impl<F: Float> AppxDbscanHyperParamsBuilder<F> {
    /// Distance between points for them to be considered neighbours.
    pub fn tolerance(mut self, tolerance: F) -> Self {
        self.tolerance = tolerance;
        self
    }

    /// Approximation factor, allows the distance between two points
    /// for them to be considered neighbours to reach
    /// `tolerance * (1 + slack)`
    pub fn slack(mut self, slack: F) -> Self {
        self.slack = slack;
        self
    }

    /// Return an instance of `AppxDbscanHyperParams` after having performed
    /// validation checks on all hyperparameters.
    ///
    /// **Panics** if any of the validation checks fail.
    pub fn build(&self) -> AppxDbscanHyperParams<F> {
        AppxDbscanHyperParams::build(self.tolerance, self.min_points, self.slack)
    }
}

impl<F: Float> AppxDbscanHyperParams<F> {
    /// Minimum number of neighboring points a point needs to have to be a core
    /// point and not a noise point.
    ///
    /// Defaults are provided if the optional parameters are not specified:
    /// * `tolerance = 1e-4`
    /// * `slack = 1e-2`
    // Violates the convention that new should return a value of type `Self`
    #[allow(clippy::new_ret_no_self)]
    pub fn new(min_points: usize) -> AppxDbscanHyperParamsBuilder<F> {
        let default_slack = F::from(1e-2).unwrap();
        let default_tolerance = F::from(1e-4).unwrap();

        AppxDbscanHyperParamsBuilder {
            min_points,
            tolerance: default_tolerance,
            slack: default_slack,
        }
    }

    /// Two points are considered neighbors if the euclidean distance between
    /// them is below the tolerance
    pub fn tolerance(&self) -> F {
        self.tolerance
    }

    /// Minimum number of a points in a neighborhood around a point for it to
    /// not be considered noise
    pub fn minimum_points(&self) -> usize {
        self.min_points
    }

    /// Approximation factor, allows the distance between two points
    /// for them to be considered neighbours to reach
    /// `tolerance * (1 + slack)`
    pub fn slack(&self) -> F {
        self.slack
    }

    pub fn appx_tolerance(&self) -> F {
        self.appx_tolerance
    }

    fn build(tolerance: F, min_points: usize, slack: F) -> Self {
        if tolerance <= F::from(0.).unwrap() {
            panic!("`tolerance` must be greater than 0!");
        }
        // There is always at least one neighbor to a point (itself)
        if min_points <= 1 {
            panic!("`min_points` must be greater than 1!");
        }

        if slack <= F::from(0.).unwrap() {
            panic!("`slack` must be greater than 0!");
        }
        Self {
            tolerance: tolerance,
            min_points: min_points,
            slack: slack,
            appx_tolerance: tolerance * (F::one() + slack),
        }
    }
}