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
//! Error types and enums for the power spectra module.
use crate::config::*;
use snafu::prelude::*;
use std::fmt::{Display, Formatter};
use strum::IntoEnumIterator;
use strum_macros::{Display, EnumIter, EnumMessage};
/// Errors that can occur during frequency-domain smoothing of power spectra.
#[cfg_attr(
feature = "python-bindings",
gen_stub_pyclass_complex_enum,
pyclass(from_py_object)
)]
#[derive(Debug, Snafu, Clone, PartialEq)]
#[snafu(visibility(pub))]
#[allow(missing_docs)]
pub enum FreqSmoothError {
/// The frequency vector is too short (must have at least 2 elements).
#[snafu(display("Invalid frequency vector length {length}. Must be >= 2"))]
FreqTooShort {
/// Actual length of the frequency vector.
length: usize,
},
/// The frequency and spectrum vectors have different lengths.
#[snafu(display(
"Frequency vector length ({freq_len}) does not match spectrum vector length ({x_len})"
))]
SizeMismatch {
/// Length of the frequency vector.
freq_len: usize,
/// Length of the spectrum vector.
x_len: usize,
},
/// The minimum non-DC frequency is not strictly positive, which prevents
/// construction of a logarithmic frequency grid.
#[snafu(display(
"Minimum non-DC frequency ({freq_min}) must be strictly positive for log-scale interpolation"
))]
InvalidFreqMin {
/// The offending frequency value.
freq_min: Flt,
},
/// An error occurred during interpolation (e.g. from `ndarray-interp`).
#[snafu(display("Interpolation failed: {msg}"))]
InterpolationFailed {
/// Description of the interpolation failure.
msg: String,
},
/// Power spectrum values must be non-negative.
#[snafu(display("Power spectrum contains negative values (minimum: {min_value})"))]
NegativePower {
/// The minimum (negative) value found in the spectrum.
min_value: Flt,
},
/// Decibel (levels) input must be real-valued, but complex data was given.
#[snafu(display("Decibel (levels) input must be real-valued, not complex"))]
ComplexLevels {},
}
#[cfg(feature = "python-bindings")]
#[gen_stub_pymethods]
#[pymethods]
impl FreqSmoothError {
fn __str__(&self) -> String {
self.to_string()
}
}
cfg_select! {
any(feature = "python-bindings") => {
use pyo3::exceptions::PyException;
#[gen_stub_pyclass]
#[pyclass(extends = PyException, str, from_py_object)]
/// Wrapper for FreqSmoothError, to make it a struct, not enum, as enums
/// cannot be extended from PyBaseException.
#[derive(Debug, Clone)]
pub struct PyFreqSmoothError {
/// The inner error
#[pyo3(get)]
pub inner: FreqSmoothError,
}
impl Display for PyFreqSmoothError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.inner)
}
}
#[gen_stub_pymethods]
#[pymethods]
impl PyFreqSmoothError {
/// Create a new PyFreqSmoothError from a FreqSmoothError
#[new]
fn new(inner: FreqSmoothError) -> Self {
PyFreqSmoothError { inner }
}
}
impl From<FreqSmoothError> for PyErr {
fn from(value: FreqSmoothError) -> Self {
PyErr::new::<PyFreqSmoothError, _>(value)
}
}
}
_ => {}
}