use std::error::Error;
use std::fmt;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum ModelError {
EmptyData,
EmptyTargets,
TargetLength { rows: usize, targets: usize },
SampleWeightLength { rows: usize, weights: usize },
NonFiniteFeature { row: usize, column: usize },
InvalidBinaryTarget { index: usize, value: u8 },
NonFiniteTarget { index: usize },
InvalidEstimatorCount,
InvalidMaxDepth,
InvalidMinSamplesSplit,
InvalidMinSamplesLeaf,
InvalidMaxFeatures { requested: usize, available: usize },
InvalidJobCount,
TooManyRows,
TooManyFeatures,
TreeTooLarge,
FeatureDimension { expected: usize, actual: usize },
OutputLength { expected: usize, actual: usize },
NonFiniteTransform { row: usize, column: usize },
OutputShapeOverflow { rows: usize, columns: usize },
UnknownClass { class: u8 },
WorkerPanicked,
RequiresTwoClasses,
InvalidRegularization,
InvalidIterationCount,
InvalidTolerance,
InvalidLeastSquaresTolerance,
InvalidRidgeAlpha,
InvalidLearningRate,
InvalidBoostingIterationCount,
InvalidMaxLeafNodes,
InvalidBoostingMaxDepth,
InvalidMaxBins,
InvalidL2Regularization,
BoostingModelTooLarge,
NumericalOverflow,
NonFinitePrediction { row: usize },
LinearSolveFailed,
}
impl fmt::Display for ModelError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::EmptyData => f.write_str("training data must have at least one row and column"),
Self::EmptyTargets => f.write_str("targets must not be empty"),
Self::TargetLength { rows, targets } => {
write!(f, "target length {targets} does not match row count {rows}")
}
Self::SampleWeightLength { rows, weights } => {
write!(
f,
"sample-weight length {weights} does not match row count {rows}"
)
}
Self::NonFiniteFeature { row, column } => {
write!(f, "feature at row {row}, column {column} is not finite")
}
Self::InvalidBinaryTarget { index, value } => write!(
f,
"binary target at index {index} is {value}, expected 0 or 1"
),
Self::NonFiniteTarget { index } => {
write!(f, "regression target at index {index} is not finite")
}
Self::InvalidEstimatorCount => f.write_str("n_estimators must be at least one"),
Self::InvalidMaxDepth => f.write_str("max_depth must be at least one when set"),
Self::InvalidMinSamplesSplit => f.write_str("min_samples_split must be at least two"),
Self::InvalidMinSamplesLeaf => f.write_str("min_samples_leaf must be at least one"),
Self::InvalidMaxFeatures {
requested,
available,
} => write!(
f,
"max_features count {requested} must be in 1..={available}"
),
Self::InvalidJobCount => f.write_str("n_jobs count must be at least one"),
Self::TooManyRows => f.write_str("row count exceeds the bootstrap counter format"),
Self::TooManyFeatures => f.write_str("feature count exceeds the packed node format"),
Self::TreeTooLarge => f.write_str("tree exceeds the packed node format"),
Self::FeatureDimension { expected, actual } => {
write!(f, "expected {expected} features, got {actual}")
}
Self::OutputLength { expected, actual } => {
write!(f, "expected output length {expected}, got {actual}")
}
Self::NonFiniteTransform { row, column } => {
write!(
f,
"transformed value at row {row}, column {column} is not finite"
)
}
Self::OutputShapeOverflow { rows, columns } => {
write!(f, "output shape overflows usize: {rows} x {columns}")
}
Self::UnknownClass { class } => {
write!(f, "class {class} was not observed during fitting")
}
Self::WorkerPanicked => f.write_str("a model training worker panicked"),
Self::RequiresTwoClasses => f.write_str("classifier requires both classes 0 and 1"),
Self::InvalidRegularization => {
f.write_str("inverse regularization strength must be finite and positive")
}
Self::InvalidIterationCount => f.write_str("max_iter must be at least one"),
Self::InvalidTolerance => f.write_str("tolerance must be finite and positive"),
Self::InvalidLeastSquaresTolerance => {
f.write_str("least-squares tolerance must be finite and non-negative")
}
Self::InvalidRidgeAlpha => f.write_str("ridge alpha must be finite and non-negative"),
Self::InvalidLearningRate => {
f.write_str("boosting learning rate must be finite and positive")
}
Self::InvalidBoostingIterationCount => {
f.write_str("boosting max_iter is outside the supported bound")
}
Self::InvalidMaxLeafNodes => {
f.write_str("boosting max_leaf_nodes is outside the supported bound")
}
Self::InvalidBoostingMaxDepth => {
f.write_str("boosting max_depth is outside the supported bound")
}
Self::InvalidMaxBins => f.write_str("boosting max_bins must be in 2..=255"),
Self::InvalidL2Regularization => {
f.write_str("boosting L2 regularization must be finite and non-negative")
}
Self::BoostingModelTooLarge => {
f.write_str("requested boosting model exceeds the logical-node limit")
}
Self::NumericalOverflow => {
f.write_str("finite training values overflowed numerical model state")
}
Self::NonFinitePrediction { row } => {
write!(f, "prediction for row {row} is not finite")
}
Self::LinearSolveFailed => f.write_str("linear solve failed during optimization"),
}
}
}
impl Error for ModelError {}