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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
use crate::evaluator::*;
use crate::time_series::DataSample;
use conv::prelude::*;
use ndarray::{s, Array1, ArrayView1, Axis, Zip};
use ndarray_stats::QuantileExt;
macro_const! {
const DOC: &'static str = r#"
Otsu threshholding algorithm
Difference of subset means, standard deviation of the lower subset, standard deviation of the upper
subset and lower-to-all observation count ratio for two subsets of magnitudes obtained by Otsu's
method split. Otsu's method is used to perform automatic thresholding. The algorithm returns a
single threshold that separate values into two classes. This threshold is determined by minimizing
intra-class intensity variance, or equivalently, by maximizing inter-class variance.
The algorithm returns the minimum threshold which corresponds to the absolute maximum of the inter-class variance.
- Depends on: **magnitude**
- Minimum number of observations: **2**
- Number of features: **4**
Otsu, Nobuyuki 1979. [DOI:10.1109/tsmc.1979.4310076](https://doi.org/10.1109/tsmc.1979.4310076)
"#;
}
#[doc = DOC ! ()]
#[derive(Clone, Default, Debug, Serialize, Deserialize, JsonSchema)]
pub struct OtsuSplit {}
lazy_info!(
OTSU_SPLIT_INFO,
OtsuSplit,
size: 4,
min_ts_length: 2,
t_required: false,
m_required: true,
w_required: false,
sorting_required: false,
);
impl OtsuSplit {
pub fn new() -> Self {
Self {}
}
pub fn doc() -> &'static str {
DOC
}
pub fn threshold<'a, 'b, T>(
ds: &'b mut DataSample<'a, T>,
) -> Result<(T, ArrayView1<'b, T>, ArrayView1<'b, T>), EvaluatorError>
where
'a: 'b,
T: Float,
{
if ds.sample.len() < 2 {
return Err(EvaluatorError::ShortTimeSeries {
actual: ds.sample.len(),
minimum: 2,
});
}
let count = ds.sample.len();
let countf = count.approx().unwrap();
let sorted = ds.get_sorted();
if sorted.minimum() == sorted.maximum() {
return Err(EvaluatorError::FlatTimeSeries);
}
let cumsum1: Array1<_> = sorted
.iter()
.take(count - 1)
.scan(T::zero(), |state, &m| {
*state += m;
Some(*state)
})
.collect();
let cumsum2: Array1<_> = sorted
.iter()
.rev()
.scan(T::zero(), |state, &m| {
*state += m;
Some(*state)
})
.collect();
let cumsum2 = cumsum2.slice(s![0..count - 1; -1]);
let amounts = Array1::linspace(T::one(), (count - 1).approx().unwrap(), count - 1);
let mean1 = Zip::from(&cumsum1)
.and(&amounts)
.map_collect(|&c, &a| c / a);
let mean2 = Zip::from(&cumsum2)
.and(amounts.slice(s![..;-1]))
.map_collect(|&c, &a| c / a);
let inter_class_variance =
Zip::from(&amounts)
.and(&mean1)
.and(&mean2)
.map_collect(|&a, &m1, &m2| {
let w1 = a / countf;
let w2 = T::one() - w1;
w1 * w2 * (m1 - m2).powi(2)
});
let index = inter_class_variance.argmax().unwrap();
let (lower, upper) = sorted.0.view().split_at(Axis(0), index + 1);
Ok((sorted.0[index + 1], lower, upper))
}
}
impl FeatureNamesDescriptionsTrait for OtsuSplit {
fn get_names(&self) -> Vec<&str> {
vec![
"otsu_mean_diff",
"otsu_std_lower",
"otsu_std_upper",
"otsu_lower_to_all_ratio",
]
}
fn get_descriptions(&self) -> Vec<&str> {
vec!["difference between mean values of Otsu split subsets",
"standard deviation for observations below the threshold given by Otsu method",
"standard deviation for observations above the threshold given by Otsu method",
"ratio of quantity of observations bellow the threshold given by Otsu method to quantity of all observations"]
}
}
impl<T> FeatureEvaluator<T> for OtsuSplit
where
T: Float,
{
fn eval(&self, ts: &mut TimeSeries<T>) -> Result<Vec<T>, EvaluatorError> {
self.check_ts_length(ts)?;
let (_, lower, upper) = Self::threshold(&mut ts.m)?;
let mut lower: DataSample<_> = lower.into();
let mut upper: DataSample<_> = upper.into();
let std_lower = if lower.sample.len() == 1 {
T::zero()
} else {
lower.get_std()
};
let mean_lower = lower.get_mean();
let std_upper = if upper.sample.len() == 1 {
T::zero()
} else {
upper.get_std()
};
let mean_upper = upper.get_mean();
let mean_diff = mean_upper - mean_lower;
let lower_to_all = lower.sample.len().approx_as::<T>().unwrap() / ts.lenf();
Ok(vec![mean_diff, std_lower, std_upper, lower_to_all])
}
}
#[cfg(test)]
#[allow(clippy::unreadable_literal)]
#[allow(clippy::excessive_precision)]
mod tests {
use super::*;
use crate::tests::*;
use approx::assert_relative_eq;
use ndarray::array;
check_feature!(OtsuSplit);
feature_test!(
otsu_split,
[OtsuSplit::new()],
[
0.725,
0.012909944487358068,
0.07071067811865482,
0.6666666666666666
],
[0.51, 0.52, 0.53, 0.54, 1.2, 1.3],
);
feature_test!(
otsu_split_min_observations,
[OtsuSplit::new()],
[0.01, 0.0, 0.0, 0.5],
[0.51, 0.52],
);
feature_test!(
otsu_split_lower,
[OtsuSplit::new()],
[1.0, 0.0, 0.0, 0.25],
[0.5, 1.5, 1.5, 1.5],
);
feature_test!(
otsu_split_upper,
[OtsuSplit::new()],
[1.0, 0.0, 0.0, 0.75],
[0.5, 0.5, 0.5, 1.5],
);
#[test]
fn otsu_threshold() {
let mut ds = vec![0.5, 0.5, 0.5, 1.5].into();
let (expected_threshold, expected_lower, expected_upper) =
(1.5, array![0.5, 0.5, 0.5], array![1.5]);
let (actual_threshold, actual_lower, actual_upper) =
OtsuSplit::threshold(&mut ds).expect("input is not flat");
assert_eq!(expected_threshold, actual_threshold);
assert_eq!(expected_lower, actual_lower);
assert_eq!(expected_upper, actual_upper);
}
#[test]
fn otsu_two_max() {
let mut ds = vec![-1.5, 0.5, 0.5, 1.5].into();
let (expected_threshold, expected_lower, expected_upper) =
(0.5, array![-1.5], array![0.5, 0.5, 1.5]);
let (actual_threshold, actual_lower, actual_upper) =
OtsuSplit::threshold(&mut ds).expect("input is not flat");
assert_eq!(expected_threshold, actual_threshold);
assert_eq!(expected_lower, actual_lower);
assert_eq!(expected_upper, actual_upper);
}
#[test]
fn otsu_split_plateau() {
let eval = OtsuSplit::new();
let x = [1.5, 1.5, 1.5, 1.5];
let mut ts = TimeSeries::new_without_weight(&x, &x);
assert_eq!(eval.eval(&mut ts), Err(EvaluatorError::FlatTimeSeries));
}
#[test]
fn otsu_split_small() {
let eval = OtsuSplit::new();
let mut ts = light_curve_feature_test_util::issue_light_curve_mag::<f32, _>(
"light-curve-feature-72/1.csv",
None,
)
.into();
let desired = vec![
3.0221021243981205,
0.8847146372743603,
0.8826366394647659,
0.507,
];
let actual = eval.eval(&mut ts).unwrap();
assert_relative_eq!(&desired[..], &actual[..], epsilon = 1e-6);
}
#[test]
#[ignore] fn no_overflow() {
const N: usize = (1 << 25) + 57;
let feature = OtsuSplit::new();
let t = Array1::linspace(0.0_f32, 1.0, N);
let mut ts = TimeSeries::new_without_weight(t.view(), t.view());
let [mean_diff, _std_lower, _std_upper, lower_to_all]: [f32; 4] =
feature.eval(&mut ts).unwrap().try_into().unwrap();
assert_relative_eq!(mean_diff, 0.5, epsilon = 1e-3);
assert_relative_eq!(lower_to_all, 0.5, epsilon = 1e-6);
}
}