edge_rs/errors.rs
1//! The crate-level error enum.
2//!
3//! One `thiserror` enum for all of `edge-rs`. Variants are grouped by subsystem
4//! with section comments; add new ones to the matching section rather than the
5//! bottom, so the file stays navigable as the port grows.
6//!
7//! Anything a caller could plausibly hit returns `Err` rather than panicking.
8//! Panics are reserved for broken invariants that the type system cannot state.
9
10use thiserror::Error;
11
12/// Every failure mode in the crate.
13#[derive(Debug, Error)]
14pub enum EdgeErrors {
15 // -- arguments --
16 /// A parameter fell outside its documented domain.
17 #[error("Invalid argument: {0}")]
18 InvalidArgument(String),
19
20 /// A count, size or index that must be non-zero was zero.
21 #[error("Parameter '{0}' must be a positive non-zero integer.")]
22 MustBePositive(String),
23
24 /// A named method string did not match any known variant.
25 #[error("Unknown {kind} method '{name}'. Expected one of: {expected}.")]
26 UnknownMethod {
27 /// What kind of method was being selected, e.g. "normalisation"
28 kind: &'static str,
29 /// The string the caller supplied
30 name: String,
31 /// Comma-separated list of accepted values
32 expected: &'static str,
33 },
34
35 // -- dimensions --
36 /// Two operands disagreed on shape.
37 #[error("Expected shape {expected:?}; got {got:?}.")]
38 ShapeMismatch {
39 /// Shape that was required
40 expected: (usize, usize),
41 /// Shape that was supplied
42 got: (usize, usize),
43 },
44
45 /// A vector was not the length its consumer required.
46 #[error("'{name}' has length {got}, expected {expected}.")]
47 LengthMismatch {
48 /// Name of the offending argument
49 name: &'static str,
50 /// Length that was required
51 expected: usize,
52 /// Length that was supplied
53 got: usize,
54 },
55
56 /// The counts matrix had no genes or no samples.
57 ///
58 /// Every downstream estimator divides by one of these, so an empty input is
59 /// an error rather than a trivially empty result.
60 #[error("Counts matrix is empty: {n_genes} genes by {n_samples} samples.")]
61 EmptyCounts {
62 /// Number of genes (rows)
63 n_genes: usize,
64 /// Number of samples (columns)
65 n_samples: usize,
66 },
67
68 // -- design matrices --
69 /// The design matrix is rank deficient, so coefficients are not identifiable.
70 #[error("Design matrix is not full rank: {n_cols} columns but rank {rank}.")]
71 DesignNotFullRank {
72 /// Number of columns in the design
73 n_cols: usize,
74 /// Numerical rank actually found
75 rank: usize,
76 },
77
78 /// A contrast or coefficient index pointed outside the design.
79 #[error("Coefficient index {index} is out of range for a design with {n_coef} coefficients.")]
80 CoefOutOfRange {
81 /// Index that was requested
82 index: usize,
83 /// Number of coefficients available
84 n_coef: usize,
85 },
86
87 // -- linear algebra --
88 /// A Cholesky factorisation failed, meaning the matrix was not positive definite.
89 #[error("Cholesky factorisation failed: {0}")]
90 CholeskyFailed(String),
91
92 /// A linear solve produced a non-finite result.
93 #[error("Linear solve failed: {0}")]
94 SolveFailed(String),
95
96 // -- numeric support --
97 /// A scalar optimiser or root finder did not converge inside its iteration budget.
98 #[error(
99 "{routine} failed to converge after {iterations} iterations (last change {last_delta:e})."
100 )]
101 NoConvergence {
102 /// Name of the routine that gave up
103 routine: &'static str,
104 /// Iterations actually performed
105 iterations: usize,
106 /// Magnitude of the final update
107 last_delta: f64,
108 },
109
110 /// A bracketing root finder was handed an interval that does not bracket a root.
111 #[error(
112 "Interval [{lower}, {upper}] does not bracket a root: f(lower) = {f_lower:e}, f(upper) = {f_upper:e}."
113 )]
114 RootNotBracketed {
115 /// Lower end of the interval
116 lower: f64,
117 /// Upper end of the interval
118 upper: f64,
119 /// Function value at `lower`
120 f_lower: f64,
121 /// Function value at `upper`
122 f_upper: f64,
123 },
124
125 /// A bounded optimisation was given a box with a lower bound above its upper bound.
126 #[error("Invalid bounds for parameter {index}: lower {lower} exceeds upper {upper}.")]
127 InvalidBounds {
128 /// Index of the offending parameter
129 index: usize,
130 /// Lower bound supplied
131 lower: f64,
132 /// Upper bound supplied
133 upper: f64,
134 },
135
136 // -- sparse --
137 /// A compressed sparse structure violated its own invariants.
138 #[error("Malformed sparse matrix: {0}")]
139 MalformedSparse(String),
140
141 // -- normalisation --
142 /// Every gene was filtered out before a normalisation factor could be computed.
143 #[error("No genes survived filtering for normalisation; all {n_genes} were dropped.")]
144 NoGenesAfterFiltering {
145 /// Number of genes present before filtering
146 n_genes: usize,
147 },
148
149 /// The reference column for TMM could not be chosen or was out of range.
150 #[error("Invalid TMM reference column: {0}")]
151 InvalidReferenceColumn(String),
152
153 // -- GLM --
154 /// A dispersion outside `[0, inf)` was supplied to a negative binomial routine.
155 #[error("Dispersion must be non-negative and finite; got {0}.")]
156 InvalidDispersion(f64),
157
158 // -- single cell / NEBULA --
159 /// Cells were not grouped contiguously by subject, which every inner loop assumes.
160 #[error(
161 "Cells are not contiguous by subject: subject {subject} appears in more than one block."
162 )]
163 SubjectsNotContiguous {
164 /// Identifier of the subject found in multiple blocks
165 subject: usize,
166 },
167
168 /// The design matrix handed to NEBULA has no intercept column.
169 ///
170 /// Centring and the starting values both key off the intercept, so its
171 /// absence is a hard error rather than something to work around.
172 #[error("NEBULA requires an intercept column in the design matrix; none was found.")]
173 MissingIntercept,
174
175 /// Fewer subjects than the mixed model can identify a random effect from.
176 #[error("NEBULA needs at least {required} subjects; got {got}.")]
177 TooFewSubjects {
178 /// Minimum number of subjects needed
179 required: usize,
180 /// Number of subjects supplied
181 got: usize,
182 },
183}