Skip to main content

qualia_vision/biosense/affect/
valence_arousal_proposal.rs

1//! Affective **proposal** from simple image stats — never a silent fact.
2//! Excellence: always emit uncertainty + non-claim flags.
3
4use crate::biosense::consent::BiosenseConsent;
5use crate::cv::buffer::RgbView;
6use crate::cv::error::CvError;
7
8#[derive(Debug, Clone, Copy)]
9pub struct AffectProposal {
10    /// -1..1
11    pub valence: f32,
12    /// 0..1
13    pub arousal: f32,
14    pub uncertainty: f32,
15    /// Machine flag: not diagnosis / not courtroom truth.
16    pub is_proposal_only: bool,
17    pub method: &'static str,
18}
19
20/// Heuristic colour/brightness proxy — **low assurance**. Consent required.
21pub fn valence_arousal_proposal(
22    consent: BiosenseConsent,
23    src: RgbView<'_>,
24) -> Result<AffectProposal, CvError> {
25    if !consent.may_process() {
26        return Err(CvError::InvalidParameter);
27    }
28    let mut r = 0.0f32;
29    let mut g = 0.0f32;
30    let mut b = 0.0f32;
31    let mut n = 0.0f32;
32    for y in 0..src.height {
33        for x in 0..src.width {
34            let (rr, gg, bb) = src.pixel(x, y);
35            r += rr as f32;
36            g += gg as f32;
37            b += bb as f32;
38            n += 1.0;
39        }
40    }
41    n = n.max(1.0);
42    r /= n;
43    g /= n;
44    b /= n;
45    let bright = (r + g + b) / (3.0 * 255.0);
46    let warm = (r - b) / 255.0;
47    Ok(AffectProposal {
48        valence: warm.clamp(-1.0, 1.0) * 0.3,
49        arousal: bright.clamp(0.0, 1.0) * 0.4,
50        uncertainty: 0.85,
51        is_proposal_only: true,
52        method: "heuristic_rgb_proxy_v1_not_clinical",
53    })
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59    use crate::biosense::consent::{BiosenseConsent, BiosensePurpose};
60    #[test]
61    fn always_proposal() {
62        let rgb = [200u8, 100, 50, 200, 100, 50];
63        let v = RgbView::new(2, 1, 6, &rgb).unwrap();
64        let c = BiosenseConsent::grant_process(BiosensePurpose::Research, 1);
65        let p = valence_arousal_proposal(c, v).unwrap();
66        assert!(p.is_proposal_only);
67        assert!(p.uncertainty > 0.5);
68    }
69}