Skip to main content

hpr_forensics/synthetic/
held.rs

1//! The readings held to their bounds on the synthetic flights: each reading a log gives, against
2//! its flight's truth, beside the bound the reading states ([M7.2b][m7-2b], the readings on every
3//! logger).
4//!
5//! A tuning flight is checked on its own logger's log and, if that logger has a barometer, on
6//! the same barometer written again at every rate of [`GRID_RATES_HZ`] in every way of
7//! [`GRID_WRITTEN`]: from 500 samples a second to one every two seconds, and from no step to 10 m
8//! in height or 100 Pa in pressure, the range the readers' families span; and, as a logger that
9//! records from launch writes them, the two recordings of [`CROPPED`] cut to each start of
10//! [`Crop`]. A reading holds where its error is no larger than its bound; `cargo xtask synthetic`
11//! fails unless every reading given on the tuning set holds, and reports the worst share of its
12//! bound each reading's error reached, on each log, and the readings withheld, by reason.
13//!
14//! **The truths**, on the log's clock (the truth's time plus the seconds logged before launch):
15//! liftoff and landing are the truth's events, the center of mass starting along the rail and
16//! reaching the ground; apogee is the center of mass's highest point, its height above where it
17//! stood at launch; the top speed is the truth's highest vertical speed; the flight time is
18//! landing less liftoff; and the mean rate of descent is the center of mass's height at apogee
19//! above the ground over the time from apogee to landing.
20//!
21//! [m7-2b]: https://hpr.fusionspace.co/decisions-and-roadmap.html#m7-2b
22
23use std::collections::BTreeMap;
24
25use hpr_flightdata::log::FlightLog;
26use hpr_flightdata::readings::{self, Clip, Reading, Reason};
27use serde::{Deserialize, Serialize};
28
29use super::logger::{
30    LOWER_RANGE_G, Recording, Written, accelerometer_log, accelerometer_log_set_lower,
31    barometer_log,
32};
33use super::{Flight, SyntheticError};
34
35/// The foot, m.
36const FOOT_M: f64 = 0.3048;
37
38/// The rates, samples a second, the grid writes a barometer at: Blue Raven's 500, AltOS's 100,
39/// the 20 most barometric loggers keep, then slower than any but a radio's telemetry.
40pub const GRID_RATES_HZ: [f64; 6] = [500.0, 100.0, 20.0, 5.0, 1.0, 0.5];
41
42/// How the grid writes a barometer's reading: as an altitude with no step, in whole feet and in
43/// 10 m steps, and as a pressure alone in 1, 10 and 100 Pa steps, past the coarsest the readers'
44/// families write (MARSA's 8.8 m, an ARTS2's 92 Pa).
45pub const GRID_WRITTEN: [Written; 6] = [
46    Written::Altitude { step_m: 0.0 },
47    Written::Altitude { step_m: FOOT_M },
48    Written::Altitude { step_m: 10.0 },
49    Written::PressureAlone { step_pa: 1.0 },
50    Written::PressureAlone { step_pa: 10.0 },
51    Written::PressureAlone { step_pa: 100.0 },
52];
53
54/// The rates, samples a second, the check writes each accelerometer at again: Blue Raven's 500, a
55/// Raven's axial 400 and AltOS's 100 lie within them, and 20 is the slowest a logger's accelerometer
56/// is written at among the families read.
57pub const ACCELEROMETER_RATES_HZ: [f64; 3] = [500.0, 100.0, 20.0];
58
59/// The recordings the check also cuts to each [`Crop`]: 20 samples a second, as most loggers that
60/// record from launch write, in whole feet as a PerfectFlite, an RRC3 or an Eggtimer, and as a
61/// pressure alone in 10 Pa steps as a Raven or an AltOS keeps its raw pressure.
62pub const CROPPED: [Recording; 2] = [
63    Recording {
64        climbing_hz: 20.0,
65        descending_hz: 20.0,
66        written: Written::Altitude { step_m: FOOT_M },
67    },
68    Recording {
69        climbing_hz: 20.0,
70        descending_hz: 20.0,
71        written: Written::PressureAlone { step_pa: 10.0 },
72    },
73];
74
75/// Where a logger that records from launch starts its log.
76#[derive(Debug, Clone, Copy, PartialEq, Eq)]
77pub enum Crop {
78    /// 0.3 s before liftoff, as a Raven or an AltOS keeps a little of its pad.
79    BeforeLiftoff,
80    /// 0.1 s after liftoff, with nothing of the pad.
81    AfterLiftoff,
82    /// 0.5 s after liftoff, its first reading of the pad written first, as an RRC3 or an
83    /// Eggtimer writes its zero and then the climb from launch detection.
84    ZeroFirst,
85}
86
87impl Crop {
88    /// Every crop.
89    pub const ALL: [Self; 3] = [Self::BeforeLiftoff, Self::AfterLiftoff, Self::ZeroFirst];
90
91    /// Its name in the report.
92    pub fn name(self) -> &'static str {
93        match self {
94            Self::BeforeLiftoff => "from 0.3 s before liftoff",
95            Self::AfterLiftoff => "from 0.1 s after liftoff",
96            Self::ZeroFirst => "from 0.5 s after liftoff, its zero first",
97        }
98    }
99
100    /// `log` cut to start where this crop does, `liftoff_s` on the log's clock: every track
101    /// keeps its samples from then on, and a [`Crop::ZeroFirst`] puts each track's first sample
102    /// back an interval before its first kept one.
103    pub fn cut(self, log: &FlightLog, liftoff_s: f64) -> FlightLog {
104        let from_s = match self {
105            Self::BeforeLiftoff => liftoff_s - 0.3,
106            Self::AfterLiftoff => liftoff_s + 0.1,
107            Self::ZeroFirst => liftoff_s + 0.5,
108        };
109        let cut_track = |time_s: &[f64], values: &[f64]| -> (Vec<f64>, Vec<f64>) {
110            let first = time_s.partition_point(|t| *t < from_s);
111            let mut time: Vec<f64> = time_s[first..].to_vec();
112            let mut kept: Vec<f64> = values[first..].to_vec();
113            if let (Self::ZeroFirst, Some(&zero), [t0, t1, ..]) =
114                (self, values.first(), time.as_slice())
115            {
116                let back = t0 - (t1 - t0);
117                time.insert(0, back);
118                kept.insert(0, zero);
119            }
120            (time, kept)
121        };
122        let mut cut = log.clone();
123        (cut.time_s, cut.altitude_m) = cut_track(&log.time_s, &log.altitude_m);
124        for channel in &mut cut.channels {
125            (channel.time_s, channel.values) = cut_track(&channel.time_s, &channel.values);
126        }
127        cut
128    }
129}
130
131/// Every rate of [`GRID_RATES_HZ`] with every way of [`GRID_WRITTEN`], the same rate climbing
132/// and descending.
133pub fn grid() -> Vec<Recording> {
134    GRID_RATES_HZ
135        .iter()
136        .flat_map(|&hz| {
137            GRID_WRITTEN.iter().map(move |&written| Recording {
138                climbing_hz: hz,
139                descending_hz: hz,
140                written,
141            })
142        })
143        .collect()
144}
145
146/// A recording's name in the report, such as `20 Hz, altitude in 0.3048 m steps`.
147pub fn name(recording: &Recording) -> String {
148    let rate = if recording.climbing_hz == recording.descending_hz {
149        format!("{} Hz", recording.climbing_hz)
150    } else {
151        format!(
152            "{} Hz up, {} Hz down",
153            recording.climbing_hz, recording.descending_hz
154        )
155    };
156    let written = match recording.written {
157        Written::Altitude { step_m: 0.0 } => "altitude, no step".to_owned(),
158        Written::Altitude { step_m } => format!("altitude in {step_m} m steps"),
159        Written::Pressure { step_pa } => format!("pressure in {step_pa} Pa steps and altitude"),
160        Written::PressureAlone { step_pa } => format!("pressure alone in {step_pa} Pa steps"),
161    };
162    format!("{rate}, {written}")
163}
164
165/// A reading the check holds.
166#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
167#[serde(rename_all = "snake_case")]
168pub enum Quantity {
169    /// Liftoff's time.
170    Liftoff,
171    /// Apogee's height above the pad.
172    ApogeeHeight,
173    /// Apogee's time.
174    ApogeeTime,
175    /// The top vertical speed.
176    TopSpeed,
177    /// Landing's time.
178    Landing,
179    /// The time from liftoff to landing.
180    FlightTime,
181    /// The mean rate of descent from apogee to landing.
182    DescentRate,
183    /// The top acceleration, from the axial accelerometer.
184    TopAcceleration,
185    /// Burnout's time.
186    Burnout,
187    /// The rate of descent under the drogue.
188    DrogueRate,
189    /// The main's opening, found back from landing.
190    MainOpening,
191    /// The rate of descent under the main.
192    MainRate,
193}
194
195impl Quantity {
196    /// Every quantity, in the report's order.
197    pub const ALL: [Self; 12] = [
198        Self::Liftoff,
199        Self::ApogeeHeight,
200        Self::ApogeeTime,
201        Self::TopSpeed,
202        Self::Landing,
203        Self::FlightTime,
204        Self::DescentRate,
205        Self::TopAcceleration,
206        Self::Burnout,
207        Self::DrogueRate,
208        Self::MainOpening,
209        Self::MainRate,
210    ];
211
212    /// Its name in the report.
213    pub fn name(self) -> &'static str {
214        match self {
215            Self::Liftoff => "liftoff",
216            Self::ApogeeHeight => "apogee height",
217            Self::ApogeeTime => "apogee time",
218            Self::TopSpeed => "top speed",
219            Self::Landing => "landing",
220            Self::FlightTime => "flight time",
221            Self::DescentRate => "descent rate",
222            Self::TopAcceleration => "top acceleration",
223            Self::Burnout => "burnout",
224            Self::DrogueRate => "drogue rate",
225            Self::MainOpening => "main opening",
226            Self::MainRate => "main rate",
227        }
228    }
229
230    /// Its unit.
231    pub fn unit(self) -> &'static str {
232        match self {
233            Self::ApogeeHeight => "m",
234            Self::TopSpeed | Self::DescentRate | Self::DrogueRate | Self::MainRate => "m/s",
235            Self::TopAcceleration => "m/s²",
236            _ => "s",
237        }
238    }
239}
240
241/// What one reading of one log came to.
242#[derive(Debug, Clone, Copy, PartialEq)]
243pub enum Outcome {
244    /// Given: its error from the truth and the bound it states.
245    Given {
246        /// The reading less the truth.
247        error: f64,
248        /// The bound it states.
249        bound: f64,
250    },
251    /// Given as a floor: the truth is no lower than the reading less its bound, and may be higher
252    /// by any amount, as a clipped top acceleration or a burnout from the speed's peak states.
253    Floor {
254        /// The reading less the truth: it holds while this is no more than the bound.
255        error: f64,
256        /// The bound it states.
257        bound: f64,
258        /// Why a top acceleration is clipped, where it is; `None` for any other floor, such as a
259        /// burnout from the speed's peak or a top whose thrust the accelerometer shows no end to.
260        clip: Option<Clip>,
261    },
262    /// Withheld, for this reason.
263    Withheld(Reason),
264    /// Not read where the truth has none to read: a drogue, or a main's opening after one, on a
265    /// flight that came down under one canopy or none.
266    Absent,
267}
268
269/// Each reading `log` gives of `flight`, against the truth.
270pub fn outcomes(flight: &Flight, log: &FlightLog) -> Vec<(Quantity, Outcome)> {
271    let read = readings::read(log);
272    let truth = &flight.truth;
273    let events = &truth.events;
274    let pad_s = flight.draw.logger.pad_s;
275    let given = |error: f64, bound: f64| Outcome::Given { error, bound };
276    fn of<T>(reading: &Reading<T>, take: impl Fn(&T) -> Outcome) -> Outcome {
277        match reading {
278            Reading::Read(value) => take(value),
279            Reading::Withheld(withheld) => Outcome::Withheld(withheld.reason),
280        }
281    }
282    let descent_rate = events.apogee_m / (events.landing_s - events.apogee_s);
283    // The truth on the log's clock.
284    let height = |t_log: f64| truth.at(t_log - pad_s).map_or(f64::NAN, |x| x.height_m);
285    let chord = |from_s: f64, to_s: f64| (height(from_s) - height(to_s)) / (to_s - from_s);
286    let top_acceleration = top_specific_force(flight);
287    let floor_or_given = |is_floor: bool, error: f64, bound: f64, clip: Option<Clip>| {
288        if is_floor {
289            Outcome::Floor { error, bound, clip }
290        } else {
291            Outcome::Given { error, bound }
292        }
293    };
294    let main_opening = match &read.main_descent {
295        Reading::Read(main) => match (&main.opening, events.deployments.len()) {
296            (Some(opening), n) if n >= 2 => {
297                let opened = pad_s + events.deployments[n - 1].1;
298                given(opening.time_s - opened, opening.bound_s)
299            }
300            // A main named where the flight opened one canopy, or none, is a miss; so is one
301            // canopy read where the flight opened two, as its main's leg then holds the drogue's.
302            (Some(opening), _) => given(f64::INFINITY, opening.bound_s),
303            (None, n) if n >= 2 => given(f64::INFINITY, 0.0),
304            (None, _) => Outcome::Absent,
305        },
306        Reading::Withheld(withheld) => Outcome::Withheld(withheld.reason),
307    };
308    let two_canopies = events.deployments.len() >= 2;
309    let drogue_rate = match &read.drogue_descent {
310        Reading::Read(d) => given(d.rate_m_s - chord(d.from_s, d.to_s), d.bound_m_s),
311        Reading::Withheld(withheld) if withheld.reason == Reason::OneCanopy && !two_canopies => {
312            Outcome::Absent
313        }
314        Reading::Withheld(withheld) => Outcome::Withheld(withheld.reason),
315    };
316    vec![
317        (
318            Quantity::Liftoff,
319            of(&read.liftoff, |l| {
320                given(l.time_s - (pad_s + events.liftoff_s), l.bound_s)
321            }),
322        ),
323        (
324            Quantity::ApogeeHeight,
325            of(&read.apogee, |a| {
326                given(a.height_m - truth.apogee_above_pad_m(), a.bound_m)
327            }),
328        ),
329        (
330            Quantity::ApogeeTime,
331            of(&read.apogee, |a| {
332                given(a.time_s - (pad_s + events.apogee_s), a.time_bound_s)
333            }),
334        ),
335        (
336            Quantity::TopSpeed,
337            of(&read.max_speed, |s| {
338                floor_or_given(
339                    s.is_floor,
340                    s.speed_m_s - truth.top_speed().0,
341                    s.bound_m_s,
342                    None,
343                )
344            }),
345        ),
346        (
347            Quantity::Landing,
348            of(&read.landing, |l| {
349                given(l.time_s - (pad_s + events.landing_s), l.bound_s)
350            }),
351        ),
352        (
353            Quantity::FlightTime,
354            of(&read.flight_time, |f| {
355                given(f.time_s - (events.landing_s - events.liftoff_s), f.bound_s)
356            }),
357        ),
358        (
359            Quantity::DescentRate,
360            of(&read.landing, |l| {
361                given(
362                    l.mean_descent_rate_m_s - descent_rate,
363                    l.mean_descent_rate_bound_m_s,
364                )
365            }),
366        ),
367        (
368            Quantity::TopAcceleration,
369            of(&read.max_acceleration, |a| {
370                floor_or_given(
371                    a.is_floor,
372                    a.acceleration_m_s2 - top_acceleration,
373                    a.bound_m_s2,
374                    a.clip,
375                )
376            }),
377        ),
378        (
379            Quantity::Burnout,
380            of(&read.burnout, |b| {
381                floor_or_given(
382                    b.is_floor,
383                    b.time_s - (pad_s + events.burnout_s),
384                    b.bound_s,
385                    None,
386                )
387            }),
388        ),
389        (Quantity::DrogueRate, drogue_rate),
390        (Quantity::MainOpening, main_opening),
391        (
392            Quantity::MainRate,
393            of(&read.main_descent, |d| {
394                given(d.rate_m_s - chord(d.from_s, d.to_s), d.bound_m_s)
395            }),
396        ),
397    ]
398}
399
400/// The truth's highest specific force along the axis from liftoff to apogee, m/s²: the top
401/// acceleration a reading is checked against.
402pub(super) fn top_specific_force(flight: &Flight) -> f64 {
403    let events = &flight.truth.events;
404    flight
405        .truth
406        .samples
407        .iter()
408        .filter(|x| x.time_s >= events.liftoff_s && x.time_s <= events.apogee_s)
409        .map(|x| x.axial_specific_force_m_s2)
410        .fold(f64::NEG_INFINITY, f64::max)
411}
412
413/// How near flight's top acceleration came to the range its logger's accelerometer clips at: the
414/// truth's top over the range, which a part reaches at 1; `None` for a logger with no
415/// accelerometer.
416pub fn top_over_range(flight: &Flight) -> Option<f64> {
417    flight
418        .draw
419        .logger
420        .profile
421        .accelerometer_range_m_s2()
422        .map(|range| top_specific_force(flight) / range)
423}
424
425/// As [`top_over_range`], against the lower range its logger's accelerometer is set to on the
426/// log [`accelerometer_log_set_lower`] writes; `None` for a part with no such setting.
427pub fn top_over_lower_range(flight: &Flight) -> Option<f64> {
428    flight
429        .draw
430        .logger
431        .profile
432        .accelerometer_lower_range_m_s2()
433        .map(|range| top_specific_force(flight) / range)
434}
435
436/// The name in the report of the log [`accelerometer_log_set_lower`] writes.
437pub fn lower_range_name() -> String {
438    format!("its accelerometer set to ±{LOWER_RANGE_G} g")
439}
440
441/// The logs flight `index` is checked on: its own logger's, named by its profile; its
442/// barometer's at each of [`grid`]'s recordings, named by [`name`]; its own logger's log with the
443/// accelerometer at each of [`ACCELEROMETER_RATES_HZ`], and with it set to the lower range its
444/// part offers, named by [`lower_range_name`], so that some tops clip at the range and the rest
445/// don't; and its barometer's at each of [`CROPPED`]'s recordings, cut each [`Crop`] way. A
446/// logger with no barometer gives no grid's or crop's, one with no accelerometer no
447/// accelerometer's, and one whose part has no lower setting no lower range's.
448///
449/// # Errors
450///
451/// [`SyntheticError`] if a log can't be written.
452pub fn logs(
453    flight: &Flight,
454    seed: u64,
455    index: u64,
456) -> Result<Vec<(String, FlightLog)>, SyntheticError> {
457    let mut logs = vec![(
458        flight.draw.logger.profile.name().to_owned(),
459        flight.log.clone(),
460    )];
461    for recording in grid() {
462        if let Some(log) =
463            barometer_log(&flight.draw.logger, &recording, &flight.truth, seed, index)?
464        {
465            logs.push((name(&recording), log));
466        }
467    }
468    for rate_hz in ACCELEROMETER_RATES_HZ {
469        if let Some(log) =
470            accelerometer_log(&flight.draw.logger, rate_hz, &flight.truth, seed, index)?
471        {
472            logs.push((format!("its accelerometer at {rate_hz} Hz"), log));
473        }
474    }
475    if let Some(log) = accelerometer_log_set_lower(&flight.draw.logger, &flight.truth, seed, index)?
476    {
477        logs.push((lower_range_name(), log));
478    }
479    let liftoff_s = flight.draw.logger.pad_s + flight.truth.events.liftoff_s;
480    for recording in CROPPED {
481        if let Some(log) =
482            barometer_log(&flight.draw.logger, &recording, &flight.truth, seed, index)?
483        {
484            for crop in Crop::ALL {
485                let name = format!("{}, {}", name(&recording), crop.name());
486                logs.push((name, crop.cut(&log, liftoff_s)));
487            }
488        }
489    }
490    Ok(logs)
491}
492
493/// The worst a reading came to on one kind of log.
494#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
495pub struct Worst {
496    /// The flight's index in its set.
497    pub flight: u64,
498    /// Its error.
499    pub error: f64,
500    /// Its bound.
501    pub bound: f64,
502    /// The error's size over the bound: 1 or less holds.
503    pub share: f64,
504}
505
506/// How one reading fared on one kind of log over a set.
507#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
508pub struct Tally {
509    /// The log: a profile's name, or a recording's.
510    pub log: String,
511    /// The reading.
512    pub quantity: Quantity,
513    /// How many flights gave it.
514    pub given: u64,
515    /// How many withheld it, by reason.
516    pub withheld: BTreeMap<Reason, u64>,
517    /// How many had none to read: a drogue on a flight under one canopy.
518    #[serde(default)]
519    pub absent: u64,
520    /// The flight whose error was the largest share of its bound; `None` if none gave it.
521    pub worst: Option<Worst>,
522}
523
524impl Tally {
525    /// Whether every reading given held to its bound.
526    pub fn holds(&self) -> bool {
527        self.worst.as_ref().is_none_or(|worst| worst.share <= 1.0)
528    }
529}
530
531/// A flight's outcomes: its index in its set, and each of its logs, named, with each quantity's
532/// outcome.
533pub type FlightOutcomes = (u64, Vec<(String, Vec<(Quantity, Outcome)>)>);
534
535/// The tallies of every flight's outcomes: `outcomes[k]` holds flight `flights[k]`'s logs, each
536/// named, with their outcomes. In the order logs are first met, then [`Quantity::ALL`]'s.
537pub fn tally(flights: &[FlightOutcomes]) -> Vec<Tally> {
538    let mut order: Vec<String> = Vec::new();
539    let mut tallies: BTreeMap<(String, Quantity), Tally> = BTreeMap::new();
540    for (flight, logs) in flights {
541        for (log, results) in logs {
542            if !order.contains(log) {
543                order.push(log.clone());
544            }
545            for (quantity, outcome) in results {
546                let tally = tallies
547                    .entry((log.clone(), *quantity))
548                    .or_insert_with(|| Tally {
549                        log: log.clone(),
550                        quantity: *quantity,
551                        given: 0,
552                        withheld: BTreeMap::new(),
553                        absent: 0,
554                        worst: None,
555                    });
556                match *outcome {
557                    Outcome::Given { error, bound } | Outcome::Floor { error, bound, .. } => {
558                        tally.given += 1;
559                        // A floor errs only above the truth; a NaN, which `max` would take to 0, is
560                        // never a hold.
561                        let size = match outcome {
562                            Outcome::Floor { .. } if error.is_nan() => f64::INFINITY,
563                            Outcome::Floor { .. } => error.max(0.0),
564                            _ => error.abs(),
565                        };
566                        // A bound of 0 holds only an exact reading; NaN is never a hold.
567                        let share = if bound > 0.0 {
568                            size / bound
569                        } else if size == 0.0 {
570                            0.0
571                        } else {
572                            f64::INFINITY
573                        };
574                        let share = if share.is_nan() { f64::INFINITY } else { share };
575                        if tally.worst.as_ref().is_none_or(|w| share > w.share) {
576                            tally.worst = Some(Worst {
577                                flight: *flight,
578                                error,
579                                bound,
580                                share,
581                            });
582                        }
583                    }
584                    Outcome::Withheld(reason) => {
585                        *tally.withheld.entry(reason).or_insert(0) += 1;
586                    }
587                    Outcome::Absent => tally.absent += 1,
588                }
589            }
590        }
591    }
592    order
593        .iter()
594        .flat_map(|log| {
595            Quantity::ALL
596                .iter()
597                .filter_map(|quantity| tallies.remove(&(log.clone(), *quantity)))
598                .collect::<Vec<_>>()
599        })
600        .collect()
601}
602
603/// The errors one reading gave over a set, every log of it together: a floor's apart from the
604/// same reading's measured.
605#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
606pub struct Errors {
607    /// The reading.
608    pub quantity: Quantity,
609    /// Whether these were given as floors.
610    pub floor: bool,
611    /// How many readings were given.
612    pub given: u64,
613    /// The finite errors' spread, the reading less the truth; `None` if none was finite.
614    pub spread: Option<Spread>,
615}
616
617/// The spread of a reading's finite errors.
618#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
619pub struct Spread {
620    /// The mean error.
621    pub mean_error: f64,
622    /// The least error, with its sign.
623    pub least_error: f64,
624    /// The most error, with its sign.
625    pub most_error: f64,
626    /// The mean bound the readings stated.
627    pub mean_bound: f64,
628}
629
630/// The errors of every reading given in `flights`, every log together, in [`Quantity::ALL`]'s
631/// order, each reading's measured before its floors. An error of infinite size, such as a main
632/// named on a flight that opened one canopy, is counted in `given` and left out of the spread;
633/// the bound's check fails on it.
634pub fn errors(flights: &[FlightOutcomes]) -> Vec<Errors> {
635    #[derive(Default)]
636    struct Sum {
637        given: u64,
638        finite: u64,
639        error: f64,
640        bound: f64,
641        least: Option<f64>,
642        most: Option<f64>,
643    }
644    let mut sums: BTreeMap<(Quantity, bool), Sum> = BTreeMap::new();
645    for (_, logs) in flights {
646        for (_, results) in logs {
647            for (quantity, outcome) in results {
648                let (error, bound, floor) = match *outcome {
649                    Outcome::Given { error, bound } => (error, bound, false),
650                    Outcome::Floor { error, bound, .. } => (error, bound, true),
651                    Outcome::Withheld(_) | Outcome::Absent => continue,
652                };
653                let sum = sums.entry((*quantity, floor)).or_default();
654                sum.given += 1;
655                if error.is_finite() && bound.is_finite() {
656                    sum.finite += 1;
657                    sum.error += error;
658                    sum.bound += bound;
659                    sum.least = Some(sum.least.map_or(error, |least| least.min(error)));
660                    sum.most = Some(sum.most.map_or(error, |most| most.max(error)));
661                }
662            }
663        }
664    }
665    sums.into_iter()
666        .map(|((quantity, floor), sum)| {
667            #[expect(
668                clippy::cast_precision_loss,
669                reason = "a count of readings converts exactly"
670            )]
671            let count = sum.finite as f64;
672            Errors {
673                quantity,
674                floor,
675                given: sum.given,
676                spread: sum.least.zip(sum.most).map(|(least, most)| Spread {
677                    mean_error: sum.error / count,
678                    least_error: least,
679                    most_error: most,
680                    mean_bound: sum.bound / count,
681                }),
682            }
683        })
684        .collect()
685}
686
687#[cfg(test)]
688mod tests {
689    use super::*;
690    use crate::synthetic::designs::DesignFiles;
691    use crate::synthetic::{Flyer, Set};
692
693    /// The errors keep a floor apart from the same reading measured, count an infinite miss
694    /// without letting it into the spread, and come in [`Quantity::ALL`]'s order.
695    #[test]
696    fn the_errors_keep_floors_apart() {
697        let mut sorted = Quantity::ALL;
698        sorted.sort();
699        assert_eq!(sorted, Quantity::ALL, "the errors' map orders by `Ord`");
700        let given = |error: f64| Outcome::Given { error, bound: 1.0 };
701        let flights: Vec<FlightOutcomes> = vec![
702            (
703                0,
704                vec![(
705                    "a".to_owned(),
706                    vec![
707                        (Quantity::MainRate, given(0.5)),
708                        (Quantity::Burnout, given(-0.25)),
709                        (Quantity::MainOpening, given(f64::INFINITY)),
710                    ],
711                )],
712            ),
713            (
714                1,
715                vec![(
716                    "b".to_owned(),
717                    vec![
718                        (
719                            Quantity::Burnout,
720                            Outcome::Floor {
721                                error: -0.75,
722                                bound: 2.0,
723                                clip: None,
724                            },
725                        ),
726                        (Quantity::Burnout, given(0.75)),
727                        (Quantity::Liftoff, Outcome::Withheld(Reason::TooShort)),
728                        (Quantity::DrogueRate, Outcome::Absent),
729                    ],
730                )],
731            ),
732        ];
733        // None to read is counted apart, neither given nor withheld.
734        let drogue = tally(&flights)
735            .into_iter()
736            .find(|t| t.quantity == Quantity::DrogueRate)
737            .unwrap();
738        assert_eq!((drogue.given, drogue.absent), (0, 1));
739        assert!(drogue.withheld.is_empty());
740        let errors = errors(&flights);
741        let rows: Vec<(Quantity, bool, u64)> = errors
742            .iter()
743            .map(|row| (row.quantity, row.floor, row.given))
744            .collect();
745        assert_eq!(
746            rows,
747            [
748                (Quantity::Burnout, false, 2),
749                (Quantity::Burnout, true, 1),
750                (Quantity::MainOpening, false, 1),
751                (Quantity::MainRate, false, 1),
752            ]
753        );
754        assert_eq!(
755            errors[0].spread,
756            Some(Spread {
757                mean_error: 0.25,
758                least_error: -0.25,
759                most_error: 0.75,
760                mean_bound: 1.0
761            })
762        );
763        assert_eq!(errors[1].spread.map(|s| s.mean_bound), Some(2.0));
764        assert_eq!(errors[2].spread, None);
765    }
766
767    /// The grid spans every rate and way of writing, each once, and names each apart.
768    #[test]
769    fn the_grid_names_each_recording_apart() {
770        let names: Vec<String> = grid().iter().map(name).collect();
771        let mut unique = names.clone();
772        unique.sort();
773        unique.dedup();
774        assert_eq!(unique.len(), GRID_RATES_HZ.len() * GRID_WRITTEN.len());
775        assert!(names.contains(&"20 Hz, altitude in 0.3048 m steps".to_owned()));
776        assert!(names.contains(&"0.5 Hz, pressure alone in 100 Pa steps".to_owned()));
777    }
778
779    /// A floor holds while its error is no more than its bound above the truth, however far below;
780    /// a NaN error, which `max` would take to 0, never holds, a floor's or a reading's.
781    #[test]
782    fn a_floor_holds_one_side_and_never_on_nan() {
783        let floor = |error: f64| Outcome::Floor {
784            error,
785            bound: 1.0,
786            clip: None,
787        };
788        let holds = |outcome: Outcome| {
789            tally(&[(
790                0,
791                vec![("a".to_owned(), vec![(Quantity::Burnout, outcome)])],
792            )])[0]
793                .holds()
794        };
795        assert!(holds(floor(-100.0)));
796        assert!(holds(floor(1.0)));
797        assert!(!holds(floor(1.01)));
798        assert!(!holds(floor(f64::NAN)));
799        assert!(!holds(Outcome::Given {
800            error: f64::NAN,
801            bound: 1.0
802        }));
803    }
804
805    /// A flight's top acceleration is the truth's over its logger's accelerometer range, and a
806    /// logger with none has no share.
807    #[test]
808    fn a_top_is_measured_against_its_parts_range() {
809        let mut flyer = Flyer::new(DesignFiles::committed());
810        let mut flights = (0..40).filter_map(|k| flyer.flight(Set::Tuning, k).ok());
811        let with = flights
812            .by_ref()
813            .find(|f| f.draw.logger.profile.accelerometer_range_m_s2().is_some())
814            .unwrap();
815        let range = with.draw.logger.profile.accelerometer_range_m_s2().unwrap();
816        let share = top_over_range(&with).unwrap();
817        assert_eq!(share, top_specific_force(&with) / range);
818        // A climb's top acceleration is some g, short of the 32 g and 180 g parts' ranges.
819        assert!(share > 0.0 && share < 1.0, "{share}");
820        let without = flights
821            .find(|f| f.draw.logger.profile.accelerometer_range_m_s2().is_none())
822            .unwrap();
823        assert_eq!(top_over_range(&without), None);
824    }
825
826    /// A log cut on the climb: its top speed is counted as a floor, one way only (#1089).
827    #[test]
828    fn a_log_cut_on_the_climb_counts_its_top_speed_as_a_floor() {
829        let mut flyer = Flyer::new(DesignFiles::committed());
830        // A flight whose whole log gives its top speed: a transonic one is withheld, cut or not.
831        let flight = (0..40)
832            .filter_map(|k| flyer.flight(Set::Tuning, k).ok())
833            .find(|f| readings::read(&f.log).max_speed.value().is_some())
834            .unwrap();
835        let events = &flight.truth.events;
836        let cut_s = flight.draw.logger.pad_s + 0.5 * (events.liftoff_s + events.apogee_s);
837        let mut log = flight.log.clone();
838        let keep = log.time_s.partition_point(|t| *t < cut_s);
839        log.time_s.truncate(keep);
840        log.altitude_m.truncate(keep);
841        for channel in &mut log.channels {
842            let keep = channel.time_s.partition_point(|t| *t < cut_s);
843            channel.time_s.truncate(keep);
844            channel.values.truncate(keep);
845        }
846        let outcome = outcomes(&flight, &log)
847            .into_iter()
848            .find(|(quantity, _)| *quantity == Quantity::TopSpeed)
849            .unwrap()
850            .1;
851        assert!(matches!(outcome, Outcome::Floor { .. }), "{outcome:?}");
852    }
853
854    /// The IMU-like logger's part set to its lower range: the readings state the range it clips
855    /// at, and a true top past it reads as clipped at the range, a floor that holds; a part with
856    /// no lower setting writes no such log.
857    #[test]
858    fn a_part_set_to_its_lower_range_clips_there() {
859        let mut flyer = Flyer::new(DesignFiles::committed());
860        let mut flights = (0..).filter_map(|k| flyer.flight(Set::Tuning, k).ok().map(|f| (k, f)));
861        let (index, imu) = flights
862            .by_ref()
863            .take(40)
864            .find(|(_, f)| top_over_lower_range(f).is_some_and(|share| share > 1.1))
865            .unwrap();
866        let lower = imu.draw.logger.profile.accelerometer_lower_range_m_s2();
867        assert_eq!(
868            lower,
869            Some(LOWER_RANGE_G * hpr_core::gravity::STANDARD_GRAVITY_MPS2)
870        );
871        let log =
872            accelerometer_log_set_lower(&imu.draw.logger, &imu.truth, Set::Tuning.seed(), index)
873                .unwrap()
874                .unwrap();
875        let read = readings::read(&log);
876        let top = read.max_acceleration.value().unwrap();
877        assert_eq!(top.range_m_s2, lower, "{top:?}");
878        assert_eq!(top.clip, Some(Clip::Range), "{top:?}");
879        assert!(top.is_floor, "{top:?}");
880        let outcome = outcomes(&imu, &log)
881            .into_iter()
882            .find(|(quantity, _)| *quantity == Quantity::TopAcceleration)
883            .map(|(_, outcome)| outcome);
884        let Some(Outcome::Floor { error, bound, clip }) = outcome else {
885            panic!("{outcome:?}");
886        };
887        assert_eq!(clip, Some(Clip::Range));
888        assert!(error < 0.0 && error <= bound, "{error}, {bound}");
889        let (index, altos) = flights
890            .take(40)
891            .find(|(_, f)| {
892                f.draw.logger.profile.accelerometer_range_m_s2().is_some()
893                    && top_over_lower_range(f).is_none()
894            })
895            .unwrap();
896        assert!(
897            accelerometer_log_set_lower(
898                &altos.draw.logger,
899                &altos.truth,
900                Set::Tuning.seed(),
901                index
902            )
903            .unwrap()
904            .is_none()
905        );
906    }
907
908    /// A tuning flight's readings hold on its own log and every grid log; a reading pushed past
909    /// its bound is counted as not holding.
910    #[test]
911    fn a_tuning_flight_holds_and_a_miss_is_caught() {
912        let mut flyer = Flyer::new(DesignFiles::committed());
913        // The first tuning flight with a barometer.
914        let (index, flight) = (0..)
915            .map(|k| (k, flyer.flight(Set::Tuning, k)))
916            .find_map(|(k, flight)| {
917                flight
918                    .ok()
919                    .filter(|f| f.draw.logger.profile.recording().is_some())
920                    .map(|f| (k, f))
921            })
922            .unwrap();
923        let logs = logs(&flight, Set::Tuning.seed(), index).unwrap();
924        let accelerometers = if flight
925            .log
926            .channels
927            .iter()
928            .any(|channel| channel.quantity == hpr_flightdata::log::Quantity::Acceleration)
929        {
930            ACCELEROMETER_RATES_HZ.len()
931        } else {
932            0
933        } + usize::from(
934            flight
935                .draw
936                .logger
937                .profile
938                .accelerometer_lower_range_m_s2()
939                .is_some(),
940        );
941        assert_eq!(
942            logs.len(),
943            1 + grid().len() + accelerometers + CROPPED.len() * Crop::ALL.len()
944        );
945        let results: Vec<(String, Vec<(Quantity, Outcome)>)> = logs
946            .iter()
947            .map(|(name, log)| (name.clone(), outcomes(&flight, log)))
948            .collect();
949        let tallies = tally(&[(index, results.clone())]);
950        for tally in &tallies {
951            assert!(tally.holds(), "{tally:?}");
952        }
953        // The same outcomes with one apogee's error past its bound.
954        let mut missed = results;
955        for (quantity, outcome) in &mut missed[0].1 {
956            if let (Quantity::ApogeeHeight, Outcome::Given { error, bound }) = (quantity, outcome) {
957                *error = 1.01 * *bound;
958            }
959        }
960        let tallies = tally(&[(index, missed)]);
961        assert!(!tallies[1].holds(), "{:?}", tallies[1]);
962        assert_eq!(tallies[1].quantity, Quantity::ApogeeHeight);
963    }
964}