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
use super::*;

// protobuf Feature from/to crate's Feature

impl From<RawFeature> for Feature {
    fn from(from: RawFeature) -> Self {
        match from.kind {
            Some(Kind::BytesList(BytesList { value })) => Feature::BytesList(value),
            Some(Kind::FloatList(FloatList { value })) => Feature::FloatList(value),
            Some(Kind::Int64List(Int64List { value })) => Feature::Int64List(value),
            None => Feature::None,
        }
    }
}

impl From<&RawFeature> for Feature {
    fn from(from: &RawFeature) -> Self {
        Self::from(from.to_owned())
    }
}

impl From<Feature> for RawFeature {
    fn from(from: Feature) -> Self {
        let kind = match from {
            Feature::BytesList(value) => Some(Kind::BytesList(BytesList { value })),
            Feature::FloatList(value) => Some(Kind::FloatList(FloatList { value })),
            Feature::Int64List(value) => Some(Kind::Int64List(Int64List { value })),
            Feature::None => None,
        };
        Self { kind }
    }
}

impl From<&Feature> for RawFeature {
    fn from(from: &Feature) -> Self {
        Self::from(from.to_owned())
    }
}

// protobuf Example from/to crate's Example

impl From<RawExample> for Example {
    fn from(from: RawExample) -> Self {
        let features = match from.features {
            Some(features) => features,
            None => return HashMap::new(),
        };
        features
            .feature
            .into_iter()
            .map(|(name, feature)| (name, Feature::from(feature)))
            .collect::<HashMap<_, _>>()
    }
}

impl From<&RawExample> for Example {
    fn from(from: &RawExample) -> Self {
        Self::from(from.to_owned())
    }
}

impl From<Example> for RawExample {
    fn from(from: Example) -> Self {
        let feature = from
            .into_iter()
            .map(|(name, feature)| (name, RawFeature::from(feature)))
            .collect::<HashMap<_, _>>();
        if feature.is_empty() {
            RawExample { features: None }
        } else {
            RawExample {
                features: Some(Features { feature }),
            }
        }
    }
}

impl From<&Example> for RawExample {
    fn from(from: &Example) -> Self {
        Self::from(from.to_owned())
    }
}

// built-in Histogram to HistogramProto

impl From<Histogram> for HistogramProto {
    fn from(from: Histogram) -> Self {
        let Histogram {
            buckets,
            len,
            min,
            max,
            sum,
            sum_squares,
        } = from;

        let min = min.load(Ordering::Relaxed);
        let max = max.load(Ordering::Relaxed);
        let sum = sum.load(Ordering::Relaxed);
        let sum_squares = sum_squares.load(Ordering::Relaxed);
        let len = len.load(Ordering::Relaxed);

        let counts = buckets
            .iter()
            .map(|bucket| bucket.count.load(Ordering::Relaxed) as f64)
            .collect::<Vec<_>>();
        let limits = buckets
            .iter()
            .map(|bucket| bucket.limit.raw())
            .collect::<Vec<_>>();

        Self {
            min,
            max,
            num: len as f64,
            sum,
            sum_squares,
            bucket_limit: limits,
            bucket: counts,
        }
    }
}

// slice or vec to histogram

impl<T> From<&[T]> for HistogramProto
where
    T: HistogramProtoElement,
{
    fn from(from: &[T]) -> Self {
        let histogram = Histogram::default();
        from.iter()
            .cloned()
            .for_each(|value| histogram.add(R64::new(value.to_f64())));
        histogram.into()
    }
}

impl<T> From<&Vec<T>> for HistogramProto
where
    T: HistogramProtoElement,
{
    fn from(from: &Vec<T>) -> Self {
        Self::from(from.as_slice())
    }
}

impl<T> From<Vec<T>> for HistogramProto
where
    T: HistogramProtoElement,
{
    fn from(from: Vec<T>) -> Self {
        Self::from(from.as_slice())
    }
}

// slice or vec to TensorProto

impl<S> From<&[S]> for TensorProto
where
    S: AsRef<[u8]>,
{
    fn from(from: &[S]) -> Self {
        TensorProtoInit {
            shape: Some(vec![from.len()]),
        }
        .build_string(from)
    }
}

impl<S> From<&Vec<S>> for TensorProto
where
    S: AsRef<[u8]>,
{
    fn from(from: &Vec<S>) -> Self {
        From::<&[_]>::from(from.as_ref())
    }
}

impl<S> From<Vec<S>> for TensorProto
where
    S: AsRef<[u8]>,
{
    fn from(from: Vec<S>) -> Self {
        From::<&[_]>::from(from.as_ref())
    }
}