use std::error::Error;
use std::fmt;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum ModelError {
EmptyData,
TargetLength {
rows: usize,
targets: usize,
},
SampleWeightLength {
rows: usize,
weights: usize,
},
NonFiniteFeature {
row: usize,
column: usize,
},
InvalidEstimatorCount,
InvalidMaxDepth,
InvalidMinSamplesSplit,
InvalidMinSamplesLeaf,
InvalidMaxFeatures {
requested: usize,
available: usize,
},
InvalidJobCount,
TooManyRows,
TooManyFeatures,
TreeTooLarge,
InvalidTreeStructure,
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,
InvalidPenaltyAlpha,
InvalidL1Ratio,
InvalidLearningRate,
InvalidBoostingIterationCount,
InvalidMaxLeafNodes,
InvalidBoostingMaxDepth,
InvalidMaxBins,
InvalidL2Regularization,
BoostingModelTooLarge,
NumericalOverflow,
NonFinitePrediction {
row: usize,
},
LinearSolveFailed,
SolverDidNotConverge {
iterations: usize,
},
MulticlassOutput {
columns: usize,
},
InvalidQuantileRange,
InvalidFeatureRange,
InvalidThreshold,
NoInverseFunction,
MulticlassSystemTooLarge {
classes: usize,
features: usize,
limit: usize,
},
}
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::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::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::InvalidTreeStructure => {
f.write_str("tree topology or value is invalid for 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::InvalidPenaltyAlpha => {
f.write_str("penalty alpha must be finite and non-negative")
}
Self::InvalidL1Ratio => f.write_str("l1_ratio must be finite and in 0..=1"),
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"),
Self::SolverDidNotConverge { iterations } => write!(
f,
"solver reached max_iter after {iterations} iterations without converging"
),
Self::MulticlassOutput { columns } => write!(
f,
"operation returns one value per row, but this model produces {columns}"
),
Self::InvalidQuantileRange => {
f.write_str("quantile range must be two percentiles in 0..=100, lowest first")
}
Self::InvalidFeatureRange => {
f.write_str("feature range must be finite with its minimum below its maximum")
}
Self::InvalidThreshold => f.write_str("threshold must be finite"),
Self::NoInverseFunction => {
f.write_str("this transformer was not given an inverse function")
}
Self::MulticlassSystemTooLarge {
classes,
features,
limit,
} => write!(
f,
"{classes} classes over {features} features exceed the {limit}-parameter \
multiclass solver bound"
),
}
}
}
impl Error for ModelError {}