forrust/time_series/
mod.rs1pub mod season;
2pub mod moving_median;
3pub mod merger;
4pub mod grouper;
5
6pub use season::Season;
7pub use moving_median::MovingMedian;
8pub use merger::Merger;
9pub use grouper::Grouper;
10
11use crate::plotable::Plotable;
12
13use std::fmt;
14
15use plotlib::view::{View, CategoricalView, ContinuousView};
16use plotlib::repr::{Plot, CategoricalRepresentation};
17use plotlib::style::{PointStyle, LineStyle, PointMarker};
18
19#[derive(Clone)]
22pub struct TimeSeries {
23 data: Vec<(f64, f64)>,
24 dom_ran: Option<(String, String)>,
25 style: Style,
26}
27
28impl TimeSeries {
29
30 pub fn new(data: Vec<f64>) -> Self {
32 let mut v = Vec::new();
33 for i in 0..data.len() {
34 v.push(((i + 1) as f64, data[i]));
35 }
36 Self {
37 data: v,
38 dom_ran: None,
39 style: Default::default(),
40 }
41 }
42
43 pub fn from_pairs_vec(pairs: Vec<(f64, f64)>) -> Self {
44 Self {
48 data: pairs,
49 dom_ran: None,
50 style: Default::default(),
51 }
52 }
53
54 pub fn get_data(&self) ->Vec<(f64, f64)> {
56 self.data.clone()
64 }
65
66 pub fn get_range_at(&self, u: usize) -> f64 {
69 for (x, y) in self.data.iter() {
70 if *x as usize == u {
71 return *y;
72 }
73 }
74 panic!(format!("domain value {} doesn't exist in the time series", u));
75 }
76
77 pub fn get_range_at_ord(&self, n: usize) -> f64 {
79 self.get_data()[n].1
80 }
81
82 pub fn push(&mut self, data: f64) {
84 let dom_last = self.data[self.data.len() - 1].0; self.data.push(((dom_last + 1 as f64) , data)); }
87
88 pub fn with_push(mut self, data: f64) -> Self {
89 self.push(data);
90 self
91 }
92
93 pub fn get_range(&self) -> Vec<f64> {
95 self.data.iter().map(|(x, y)| *y).collect()
96 }
97
98 pub fn style(&self) -> Style {
99 self.style.clone()
100 }
101
102 pub fn style_mut(&mut self) -> &mut Style {
103 &mut self.style
104 }
105
106 pub fn len(&self) -> usize {
107 self.data.len()
108 }
109}
110
111#[derive(Clone)]
112pub struct Style{
113 pub point: PointStyle,
114 pub line: LineStyle,
115}
116
117impl Style {
118 pub fn from_color(color: &str) -> Self {
119 let mut style: Style = Default::default();
120 style.point = style.point.colour(color);
121 style.line = style.line.colour(color);
122 style
123 }
124}
125
126impl Default for Style {
127 fn default() -> Self {
128 Self {
129 point: PointStyle::new().colour("#000000").marker(PointMarker::Circle),
130 line: LineStyle::new().colour("#000000")
131 }
132 }
133}
134
135
136impl Plotable for TimeSeries {
137 fn plot(&self) -> Box<dyn View> {
138 let plot = self.as_plot();
139 let mut view = ContinuousView::new()
140 .add(plot);
141 Box::new(view)
142 }
143
144 fn as_plot(&self) -> Plot {
145 let mut plot = Plot::new(self.get_data());
146 plot.point_style(self.style().point).line_style(self.style().line)
147 }
148}
149
150
151#[derive(Copy, Clone, Debug)]
153pub enum Mes {
154 Enero,
155 Febrero,
156 Marzo,
157 Abril,
158 Mayo,
159 Junio,
160 Julio,
161 Agosto,
162 Septiembre,
163 Octubre,
164 Noviembre,
165 Diciembre
166
167}
168
169impl fmt::Display for Mes {
170 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
171 write!(f, "{:?}", self)
172 }
173}
174
175
176impl From<u32> for Mes {
177 fn from(v: u32) -> Self {
178
179 match v {
180 0 => Self::Enero,
181 1 => Self::Febrero,
182 2 => Self::Marzo,
183 3 => Self::Abril,
184 4 => Self::Mayo,
185 5 => Self::Junio,
186 6 => Self::Julio,
187 7 => Self::Agosto,
188 8 => Self::Septiembre,
189 9 => Self::Octubre,
190 10 => Self::Noviembre,
191 11 => Self::Diciembre,
192 _ => panic!(format!("Número de mes inválido: {}", v))
193 }
194 }
195}
196
197impl From<&str> for Mes {
198 fn from(v: &str) -> Self {
199 let v = v.to_lowercase();
200 match v.as_str() {
201 "enero" => Self::Enero,
202 "febrero" => Self::Febrero,
203 "marzo" => Self::Marzo,
204 "abril" => Self::Abril,
205 "mayo" => Self::Mayo,
206 "junio" => Self::Junio,
207 "julio" => Self::Julio,
208 "agosto" => Self::Agosto,
209 "septiembre" => Self::Septiembre,
210 "octubre" => Self::Octubre,
211 "noviembre" => Self::Noviembre,
212 "diciembre" => Self::Diciembre,
213 _ => panic!(format!("Mes inválido: {}", v))
214 }
215 }
216}
217