Skip to content

Commit 717e356

Browse files
committed
change api: use Option for smooth and spline instead of expect panic
1 parent e7c7a1c commit 717e356

File tree

4 files changed

+18
-32
lines changed

4 files changed

+18
-32
lines changed

src/umv.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -281,24 +281,14 @@ impl<'a, T, D> CubicSmoothingSpline<'a, T, D>
281281
Ok(yi)
282282
}
283283

284-
/// Returns the smoothing parameter
285-
///
286-
/// # Panics
287-
///
288-
/// - If the smoothing parameter has not been set or computed
289-
///
290-
pub fn smooth(&self) -> T {
291-
self.smooth.expect("The smoothing parameter yet has not been set or computed.")
284+
/// Returns the smoothing parameter or None
285+
pub fn smooth(&self) -> Option<T> {
286+
self.smooth
292287
}
293288

294-
/// Returns `NdSpline` struct with data of computed spline
295-
///
296-
/// # Panics
297-
///
298-
/// - If the spline has not been computed
299-
///
300-
pub fn spline(&self) -> &NdSpline<'a, T> {
301-
self.spline.as_ref().expect("The spline yet has not been computed.")
289+
/// Returns the ref to `NdSpline` struct with data of computed spline or None
290+
pub fn spline(&self) -> Option<&NdSpline<'a, T>> {
291+
self.spline.as_ref()
302292
}
303293

304294
/// Invalidate computed spline

tests/umv_full.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn test_without_weights_auto_smooth_1() {
1616
.make()
1717
.unwrap();
1818

19-
assert_abs_diff_eq!(s.smooth(), 0.8999999999999999);
19+
assert_abs_diff_eq!(s.smooth().unwrap(), 0.8999999999999999);
2020

2121
let coeffs_expected = array![[
2222
-0.41780962939499505, 0.39429046563192893,
@@ -30,7 +30,7 @@ fn test_without_weights_auto_smooth_1() {
3030

3131
]];
3232

33-
assert_abs_diff_eq!(s.spline().coeffs(), coeffs_expected, epsilon = EPS);
33+
assert_abs_diff_eq!(s.spline().unwrap().coeffs(), coeffs_expected, epsilon = EPS);
3434

3535
let ys = s.evaluate(&x).unwrap();
3636

@@ -70,7 +70,7 @@ fn test_without_weights_auto_smooth_2() {
7070
2.87797144570959, 6.6215506411899465,
7171
]).insert_axis(Axis(0));
7272

73-
assert_abs_diff_eq!(s.spline().coeffs(), coeffs_expected, epsilon = EPS);
73+
assert_abs_diff_eq!(s.spline().unwrap().coeffs(), coeffs_expected, epsilon = EPS);
7474

7575
let ys = s.evaluate(&x).unwrap();
7676

@@ -103,7 +103,7 @@ fn test_with_weights_and_smooth() {
103103
1.7816259351620949, 2.3935461346633415, 2.247780548628429, 2.226284289276808
104104
]];
105105

106-
assert_abs_diff_eq!(s.spline().coeffs(), coeffs_expected, epsilon = EPS);
106+
assert_abs_diff_eq!(s.spline().unwrap().coeffs(), coeffs_expected, epsilon = EPS);
107107

108108
let ys = s.evaluate(&x).unwrap();
109109

tests/umv_make.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn test_driver_make_nd_npt<T, D>(x: Array1<T>, y: Array<T, D>,
1111
.make()
1212
.unwrap();
1313

14-
let spline = s.spline();
14+
let spline = s.spline().unwrap();
1515

1616
assert_eq!(spline.order(), order);
1717
assert_eq!(spline.pieces(), pieces);

tests/umv_validate.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ fn test_sites_invalid_order_1() {
1313
.unwrap();
1414
}
1515

16+
1617
#[test]
1718
#[should_panic(expected = "Data site values must satisfy the condition: x1 < x2 < ... < xN")]
1819
fn test_sites_invalid_order_2() {
@@ -24,6 +25,7 @@ fn test_sites_invalid_order_2() {
2425
.unwrap();
2526
}
2627

28+
2729
#[test]
2830
#[should_panic(expected = "`y` has zero dimensionality")]
2931
fn test_zero_ndim_y_error() {
@@ -35,6 +37,7 @@ fn test_zero_ndim_y_error() {
3537
.unwrap();
3638
}
3739

40+
3841
#[test]
3942
#[should_panic(expected = "The shape[0] (5) of `y` data is not equal to `x` size (4)")]
4043
fn test_data_size_mismatch_error() {
@@ -46,6 +49,7 @@ fn test_data_size_mismatch_error() {
4649
.unwrap();
4750
}
4851

52+
4953
#[test]
5054
#[should_panic(expected = "`axis` value (1) is out of bounds `y` dimensionality (1)")]
5155
fn test_axis_out_of_bounds_error() {
@@ -58,6 +62,7 @@ fn test_axis_out_of_bounds_error() {
5862
.unwrap();
5963
}
6064

65+
6166
#[test]
6267
#[should_panic(expected = "`weights` size (5) is not equal to `x` size (4)")]
6368
fn test_weights_size_mismatch_error() {
@@ -71,6 +76,7 @@ fn test_weights_size_mismatch_error() {
7176
.unwrap();
7277
}
7378

79+
7480
#[test]
7581
#[should_panic(expected = "`smooth` value must be in range 0..1, given -0.5")]
7682
fn test_smooth_less_than_error() {
@@ -84,6 +90,7 @@ fn test_smooth_less_than_error() {
8490
.unwrap();
8591
}
8692

93+
8794
#[test]
8895
#[should_panic(expected = "`smooth` value must be in range 0..1, given 1.5")]
8996
fn test_smooth_greater_than_error() {
@@ -97,17 +104,6 @@ fn test_smooth_greater_than_error() {
97104
.unwrap();
98105
}
99106

100-
#[test]
101-
#[should_panic(expected = "The size of `xi` must be greater or equal to 2")]
102-
fn test_evaluate_invalid_xi_error() {
103-
let x = array![1., 2., 3., 4.];
104-
let y = array![1., 2., 3., 4.];
105-
let xi = array![1.];
106-
107-
let spline = CubicSmoothingSpline::new(&x, &y);
108-
109-
spline.evaluate(&xi).unwrap();
110-
}
111107

112108
#[test]
113109
#[should_panic(expected = "The spline has not been computed, use `make` method before")]

0 commit comments

Comments
 (0)