//! Sets: the shots of one moment, "close in time AND look alike" //! (DECISIONS.md "Sets"; proven on 64,000 synthetic items in `proto/` and in //! this module's tests: pair precision ≥ 99%, recall ≥ 98%). //! //! - **Close in time:** within [`WINDOW_MS`] of a shot taken before it, and no //! more than [`LOOKBACK`] shots back. //! - **Look alike:** both platform prints of one revision within //! [`PLATFORM_PRINT_MAX`]; otherwise the core's own print within //! [`CORE_PRINT_MAX`] and the difference hashes within [`HAMMING_MAX`]. //! - A set is what those links join (two moments a minute apart with //! different scenes stay apart; the same scene revisited hours later stays //! apart). //! - **Never in a set:** videos, screenshots and screen recordings (they have //! lists of their own), photos the core has not seen ([`Level::None`]), //! favourites, hidden photos, the phone's trash, the second part of a //! RAW+JPEG pair or an edit ([`super::parts`]), and anything he chose to //! keep or bin. //! //! The platform threshold is a starting value: Vision's distances have no //! documented scale, so it is calibrated on the device with the synthetic //! library before stage 1 ships (DECISIONS.md "Test plan"). use super::look::{hamming, print_distance, Level, Look}; use super::{Item, Kind}; pub const WINDOW_MS: i64 = 180_000; pub const LOOKBACK: usize = 40; pub const CORE_PRINT_MAX: f32 = 0.55; pub const HAMMING_MAX: u32 = 20; pub const PLATFORM_PRINT_MAX: f32 = 0.45; /// Whether an item may be grouped at all. pub fn groupable(item: &Item, look: Option<&Look>) -> bool { item.kind == Kind::Photo && !item.has("screenshot") && !item.has("screen_recording") && !item.has("favourite") && !item.has("hidden") && !item.has("trashed") && look.is_some_and(|l| l.level != Level::None) } /// Whether two looks are of one moment's scene. pub fn alike(a: &Look, b: &Look) -> bool { if a.level == Level::None || b.level == Level::None { return false; } if let (Some(pa), Some(pb)) = (&a.platform.print, &b.platform.print) { if pa.revision == pb.revision && pa.values.len() == pb.values.len() && !pa.values.is_empty() { let d: f32 = pa.values.iter().zip(&pb.values).map(|(x, y)| (x - y).powi(2)).sum::().sqrt(); return d <= PLATFORM_PRINT_MAX; } } hamming(a.dhash, b.dhash) <= HAMMING_MAX && print_distance(&a.print, &b.print) <= CORE_PRINT_MAX } /// One candidate for grouping: its index in the caller's list, when, and its look. pub struct Shot<'a> { pub index: usize, pub taken_at_ms: i64, pub look: &'a Look, } /// Group shots into sets. `shots` must be groupable ([`groupable`]); they are /// sorted here by time. Answers each set of two or more as the callers' /// indices, earliest first; the sets in time order. pub fn group(shots: &mut [Shot<'_>]) -> Vec> { shots.sort_by_key(|s| (s.taken_at_ms, s.index)); let n = shots.len(); let mut parent: Vec = (0..n).collect(); fn find(p: &mut [usize], mut x: usize) -> usize { while p[x] != x { p[x] = p[p[x]]; x = p[x]; } x } for i in 0..n { for j in (i.saturating_sub(LOOKBACK)..i).rev() { if shots[i].taken_at_ms - shots[j].taken_at_ms > WINDOW_MS { break; } if alike(shots[i].look, shots[j].look) { let (a, b) = (find(&mut parent, i), find(&mut parent, j)); if a != b { parent[a.max(b)] = a.min(b); } } } } let mut sets: std::collections::BTreeMap> = std::collections::BTreeMap::new(); for i in 0..n { let r = find(&mut parent, i); sets.entry(r).or_default().push(i); } sets.into_values().filter(|m| m.len() >= 2).map(|m| m.into_iter().map(|i| shots[i].index).collect()).collect() } /// A set's id: its earliest member's id, so a set keeps its name as later /// shots join it. pub fn set_id(earliest_item_id: &str) -> String { format!("set:{earliest_item_id}") }