reductionml_core/metrics/
parsed_features.rs

1use crate::{metrics::Metric, Features};
2
3use super::MetricValue;
4
5pub struct ParsedFeaturesMetric {
6    pub count: u64,
7}
8
9impl ParsedFeaturesMetric {
10    pub fn new() -> ParsedFeaturesMetric {
11        ParsedFeaturesMetric { count: 0 }
12    }
13}
14
15impl Default for ParsedFeaturesMetric {
16    fn default() -> Self {
17        Self::new()
18    }
19}
20
21impl Metric for ParsedFeaturesMetric {
22    fn add_point(
23        &mut self,
24        features: &Features,
25        _label: &crate::types::Label,
26        _prediction: &crate::types::Prediction,
27    ) {
28        match features {
29            Features::SparseSimple(s) => self.count += s.all_features().count() as u64,
30            Features::SparseSimpleRef(s) => self.count += s.all_features().count() as u64,
31            Features::SparseCBAdf(feats) => {
32                self.count += feats
33                    .shared
34                    .as_ref()
35                    .map_or(0, |x| x.all_features().count()) as u64;
36                self.count += feats
37                    .actions
38                    .iter()
39                    .map(|x| x.all_features().count())
40                    .sum::<usize>() as u64;
41            }
42            Features::SparseCBAdfRef(feats) => {
43                self.count += feats
44                    .shared
45                    .as_ref()
46                    .map_or(0, |x| x.all_features().count()) as u64;
47                self.count += feats
48                    .actions
49                    .iter()
50                    .map(|x| x.all_features().count())
51                    .sum::<usize>() as u64;
52            }
53        }
54    }
55
56    fn get_value(&self) -> MetricValue {
57        MetricValue::Int(self.count.try_into().unwrap())
58    }
59
60    fn get_name(&self) -> String {
61        "Parsed features".to_owned()
62    }
63}