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
use std::error;
use std::fmt;

/// An error that can occur in this crate.
///
/// Generally this error corresponds to problems with input data or fitting
/// a regression model.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum Error {
    /// Number of slopes and output names is inconsistent.
    InconsistentSlopes(InconsistentSlopes),
    /// Cannot fit model without data.
    NoData,
    /// Cannot fit model without formula.
    NoFormula,
    /// Given formula is invalid.
    InvalidFormula,
    /// Requested column is not in data. (Column given as String)
    ColumnNotInData(String),
    /// A column used in the model is misising from the provided data
    ModelColumnNotInData(String),
    /// Regressor and regressand dimensions do not match. (Column given as String)
    RegressorRegressandDimensionMismatch(String),
    /// Error while processing the regression data. (Details given as String)
    RegressionDataError(String),
    /// Error while fitting the model. (Details given as String)
    ModelFittingError(String),
    /// The given vectors have inconsistent lengths
    InconsistentVectors,
    /// The RegressionModel internal state is inconsistent
    InconsistentRegressionModel,
}

#[derive(Debug, Clone, Copy)]
pub struct InconsistentSlopes {
    output_name_count: usize,
    slope_count: usize,
}

impl InconsistentSlopes {
    pub(crate) fn new(output_name_count: usize, slope_count: usize) -> Self {
        Self {
            output_name_count,
            slope_count,
        }
    }

    pub fn get_output_name_count(&self) -> usize {
        self.output_name_count
    }

    pub fn get_slope_count(&self) -> usize {
        self.slope_count
    }
}

impl error::Error for Error {}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::InconsistentSlopes(inconsistent_slopes) => write!(
                f,
                "Number of slopes and output names is inconsistent. {} outputs != {} sloped",
                inconsistent_slopes.get_output_name_count(),
                inconsistent_slopes.get_slope_count()
            ),
            Error::NoData => write!(f, "Cannot fit model without data"),
            Error::NoFormula => write!(f, "Cannot fit model without formula"),
            Error::InvalidFormula => write!(
                f,
                "Invalid formula. Expected formula of the form 'y ~ x1 + x2'"
            ),
            Error::ColumnNotInData(column) => {
                write!(f, "Requested column {} is not in the data", column)
            }
            Error::RegressorRegressandDimensionMismatch(column) => write!(
                f,
                "Regressor dimensions for {} do not match regressand dimensions",
                column
            ),
            Error::RegressionDataError(detail) => {
                write!(f, "Error while processing the regression data: {}", detail)
            }
            Error::ModelFittingError(detail) => {
                write!(f, "Error while fitting the model: {}", detail)
            }
            Error::ModelColumnNotInData(column) => write!(
                f,
                "The column {} used in the model is misising from the provided data",
                column
            ),
            Error::InconsistentVectors => write!(f, "The given vectors have inconsistent lengths"),
            Error::InconsistentRegressionModel => write!(
                f,
                concat!(
                    "The RegressionModel internal state is inconsistent:",
                    " The number of regressor names and values differ."
                )
            ),
        }
    }
}