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
use std::fmt::{Debug, Display};
use num_traits::Float;
use super::{Axis, BinInterval};
#[derive(Clone, PartialEq, Debug, Eq)]
pub struct Uniform<T = f64> {
num: usize,
low: T,
high: T,
}
impl<T> Uniform<T>
where
T: PartialOrd,
{
pub fn new(num: usize, low: T, high: T) -> Uniform<T> {
if num == 0 {
panic!("Invalid axis num bins ({})", num);
}
if low >= high {
panic!("Invalid axis range bins (low >= high)");
}
Uniform { num, low, high }
}
}
impl<T> Uniform<T> {
pub fn low(&self) -> &T {
&self.low
}
pub fn high(&self) -> &T {
&self.high
}
}
impl<T: Float> Axis for Uniform<T> {
type Coordinate = T;
type BinInterval = BinInterval<T>;
fn index(&self, coordinate: &Self::Coordinate) -> Option<usize> {
let frac = (*coordinate - self.low) / (self.high - self.low);
if frac < T::zero() {
return Some(0);
} else if frac >= T::one() {
return Some(self.num + 1);
}
let idx: T = T::from(self.num).unwrap() * frac;
Some((idx.to_usize().unwrap()) + 1)
}
fn numbins(&self) -> usize {
self.num + 2
}
fn bin(&self, index: usize) -> std::option::Option<<Self as Axis>::BinInterval> {
if index == 0 {
return Some(Self::BinInterval::underflow(self.low));
} else if index == (self.num + 1) {
return Some(Self::BinInterval::overflow(self.high));
} else if index > (self.num + 1) {
return None;
}
let start =
self.low + (T::from(index - 1)?) * (self.high - self.low) / (T::from(self.num)?);
let end = self.low + (T::from(index)?) * (self.high - self.low) / (T::from(self.num)?);
Some(Self::BinInterval::new(start, end))
}
fn indices(&self) -> Box<dyn Iterator<Item = usize>> {
Box::new(0..self.numbins())
}
}
impl<T: Display> Display for Uniform<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Axis{{# bins={}, range=[{}, {}), class={}}}",
self.num,
self.low,
self.high,
stringify!(Uniform)
)
}
}
impl<'a, T: Float> IntoIterator for &'a Uniform<T> {
type Item = (usize, <Uniform<T> as Axis>::BinInterval);
type IntoIter = Box<dyn Iterator<Item = Self::Item> + 'a>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}