Skip to content

Commit d29f7a0

Browse files
committed
api histogram aggregation fix
1 parent 04b2ef8 commit d29f7a0

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

src/analysis/api.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -213,16 +213,16 @@ impl API {
213213

214214
// histogram aggregation via join
215215
pub async fn abs_histogram(&self, abs: Abstraction) -> Result<Histogram, E> {
216+
let idx = i64::from(abs);
216217
let mass = abs.street().n_children() as f32;
217-
let abs = i64::from(abs);
218218
const SQL: &'static str = r#"
219219
SELECT next, dx
220220
FROM transitions
221221
WHERE prev = $1
222222
"#;
223223
Ok(self
224224
.0
225-
.query(SQL, &[&abs])
225+
.query(SQL, &[&idx])
226226
.await?
227227
.iter()
228228
.map(|row| (row.get::<_, i64>(0), row.get::<_, Energy>(1)))
@@ -235,26 +235,26 @@ impl API {
235235
}
236236
pub async fn obs_histogram(&self, obs: Observation) -> Result<Histogram, E> {
237237
// Kd8s~6dJsAc
238-
let isos = obs
239-
.children()
240-
.map(Isomorphism::from)
241-
.map(Observation::from)
242-
.map(i64::from)
243-
.collect::<Vec<i64>>();
238+
let idx = i64::from(Observation::from(Isomorphism::from(obs)));
239+
let mass = obs.street().n_children() as f32;
244240
const SQL: &'static str = r#"
245-
SELECT abs
246-
FROM encoder
247-
WHERE obs = ANY($1)
241+
SELECT next, dx
242+
FROM transitions
243+
JOIN encoder ON encoder.abs = transitions.prev
244+
WHERE encoder.obs = $1
248245
"#;
249246
Ok(self
250247
.0
251-
.query(SQL, &[&isos])
248+
.query(SQL, &[&idx])
252249
.await?
253250
.iter()
254-
.map(|row| row.get::<_, i64>(0))
255-
.map(Abstraction::from)
256-
.collect::<Vec<Abstraction>>()
257-
.into())
251+
.map(|row| (row.get::<_, i64>(0), row.get::<_, Energy>(1)))
252+
.map(|(next, dx)| (next, (dx * mass).round() as usize))
253+
.map(|(next, dx)| (Abstraction::from(next), dx))
254+
.fold(Histogram::default(), |mut h, (next, dx)| {
255+
h.set(next, dx);
256+
h
257+
}))
258258
}
259259

260260
// observation similarity lookups

0 commit comments

Comments
 (0)