1use std::collections::BTreeMap;
21
22use hpr_core::random::SeededRng;
23use hpr_flightdata::log::{FlightLog, Quantity};
24use hpr_flightdata::smoother::{self, Deweighted, Health, INTERVAL_Z, Options, Reason, Sensor};
25use serde::{Deserialize, Serialize};
26
27use super::logger::Profile;
28use super::{Flight, Set};
29
30#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
32#[serde(rename_all = "kebab-case")]
33pub enum Moment {
34 Burnout,
36 TopSpeed,
38 Apogee,
40 Drawn,
42}
43
44impl Moment {
45 pub const ALL: [Self; 4] = [Self::Burnout, Self::TopSpeed, Self::Apogee, Self::Drawn];
47
48 pub fn name(self) -> &'static str {
50 match self {
51 Self::Burnout => "burnout",
52 Self::TopSpeed => "top speed",
53 Self::Apogee => "apogee",
54 Self::Drawn => "a drawn moment",
55 }
56 }
57}
58
59#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
61#[serde(rename_all = "kebab-case")]
62pub enum StateName {
63 Height,
65 Speed,
67}
68
69impl StateName {
70 pub const ALL: [Self; 2] = [Self::Height, Self::Speed];
72
73 pub fn name(self) -> &'static str {
75 match self {
76 Self::Height => "height",
77 Self::Speed => "speed",
78 }
79 }
80}
81
82#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
84pub struct Compared {
85 pub error: f64,
87 pub sd: f64,
89}
90
91impl Compared {
92 pub fn truth_below(&self) -> bool {
94 !self.finite() || self.error > INTERVAL_Z * self.sd
95 }
96
97 pub fn truth_above(&self) -> bool {
99 !self.finite() || -self.error > INTERVAL_Z * self.sd
100 }
101
102 fn finite(&self) -> bool {
105 self.error.is_finite() && self.sd.is_finite()
106 }
107}
108
109#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
111pub struct Checked {
112 pub profile: Profile,
114 pub withheld: Option<Reason>,
116 pub compared: Vec<(Moment, StateName, Compared)>,
118 pub health: Option<Health>,
120 pub has_gnss: bool,
122}
123
124pub fn moment_drawn(set: Set, index: u64, liftoff_s: f64, landing_s: f64) -> f64 {
128 let mut rng = SeededRng::for_stream(set.seed(), &[index, super::SMOOTHER_MOMENT_KEY]);
129 liftoff_s + rng.uniform() * (landing_s - liftoff_s)
130}
131
132pub fn launch_started(flight: &Flight) -> FlightLog {
138 let from_s = flight.draw.logger.pad_s + flight.truth.events.liftoff_s;
139 let mut log = flight.log.clone();
140 let first = log.time_s.partition_point(|t| *t < from_s);
141 log.time_s.drain(..first);
142 if !log.altitude_m.is_empty() {
143 log.altitude_m.drain(..first);
144 }
145 for channel in &mut log.channels {
146 let first = channel.time_s.partition_point(|t| *t < from_s);
147 channel.time_s.drain(..first);
148 channel.values.drain(..first);
149 }
150 log
151}
152
153pub fn check(flight: &Flight, options: Options) -> Checked {
155 check_log(flight, &flight.log, options)
156}
157
158pub fn check_log(flight: &Flight, log: &FlightLog, options: Options) -> Checked {
160 let profile = flight.draw.logger.profile;
161 let has_gnss = log
162 .channels
163 .iter()
164 .any(|c| c.quantity == Quantity::GnssAltitude);
165 let smoothed = match smoother::smooth_with(log, &hpr_flightdata::readings::read(log), options) {
166 Ok(smoothed) => smoothed,
167 Err(withheld) => {
168 return Checked {
169 profile,
170 withheld: Some(withheld.reason),
171 compared: Vec::new(),
172 health: None,
173 has_gnss,
174 };
175 }
176 };
177 let truth = &flight.truth;
178 let events = &truth.events;
179 let moments = [
180 (Moment::Burnout, events.burnout_s),
181 (Moment::TopSpeed, truth.top_speed().1),
182 (Moment::Apogee, events.apogee_s),
183 (
184 Moment::Drawn,
185 moment_drawn(
186 flight.draw.set,
187 flight.draw.index,
188 events.liftoff_s,
189 events.landing_s,
190 ),
191 ),
192 ];
193 let mut compared = Vec::new();
194 for (moment, t) in moments {
195 let (Some(true_state), Some(state)) =
196 (truth.at(t), smoothed.at(t + flight.draw.logger.pad_s))
197 else {
198 for state in StateName::ALL {
201 compared.push((
202 moment,
203 state,
204 Compared {
205 error: f64::NAN,
206 sd: f64::NAN,
207 },
208 ));
209 }
210 continue;
211 };
212 compared.push((
213 moment,
214 StateName::Height,
215 Compared {
216 error: state.height_m - (true_state.height_m - truth.pad_height_m),
217 sd: state.height_sd_m,
218 },
219 ));
220 compared.push((
221 moment,
222 StateName::Speed,
223 Compared {
224 error: state.speed_m_s - true_state.vertical_speed_m_s,
225 sd: state.speed_sd_m_s,
226 },
227 ));
228 }
229 Checked {
230 profile,
231 withheld: None,
232 compared,
233 health: Some(smoothed.health),
234 has_gnss,
235 }
236}
237
238#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
240pub struct Cell {
241 pub moment: Moment,
243 pub state: StateName,
245 pub flights: u64,
247 pub below: u64,
249 pub above: u64,
251 pub coverage: f64,
253 pub lowest_miss_share: [f64; 2],
256 pub holds: bool,
258 pub median_sd: f64,
260 pub largest_sd: f64,
262}
263
264#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
266pub struct SetCheck {
267 pub flights: u64,
269 pub withheld: BTreeMap<String, u64>,
271 pub cells: Vec<Cell>,
273 pub by_logger: BTreeMap<String, Vec<Cell>>,
275 pub health: HealthTally,
277}
278
279#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
281pub struct SensorTally {
282 pub logs: u64,
284 pub samples: u64,
286 pub rejected: u64,
288 pub unweighed: u64,
290 pub resets: u64,
292 pub logs_rejecting: u64,
294}
295
296#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
298pub struct HealthTally {
299 pub sensors: BTreeMap<String, SensorTally>,
301 pub transonic_spans: u64,
303 pub charge_spans: u64,
305 pub gnss_trusted: u64,
307 pub unsmoothed_steps: u64,
309 pub logs: u64,
311 pub assumed_pad: u64,
313 pub accelerometer_set_aside: u64,
315 pub apogee_past_reading_bound: u64,
317}
318
319impl HealthTally {
320 fn add(&mut self, health: &Health) {
321 for s in &health.sensors {
322 let t = self.sensors.entry(s.sensor.name().to_owned()).or_default();
323 t.logs += 1;
324 t.samples += s.samples as u64;
325 t.rejected += s.rejected as u64;
326 t.unweighed += s.unweighed as u64;
327 t.resets += s.resets as u64;
328 t.logs_rejecting += u64::from(s.rejected > 0);
329 }
330 for span in &health.deweighted {
331 match span.why {
332 Deweighted::Transonic => self.transonic_spans += 1,
333 Deweighted::Charge => self.charge_spans += 1,
334 }
335 }
336 let has_gnss = health.sensors.iter().any(|s| s.sensor == Sensor::Gnss);
337 self.gnss_trusted += u64::from(has_gnss && health.gnss_trusted_s.is_some());
338 self.unsmoothed_steps += health.unsmoothed_steps as u64;
339 self.logs += 1;
340 self.assumed_pad += u64::from(health.assumed_pad);
341 self.accelerometer_set_aside += u64::from(health.accelerometer_set_aside);
342 self.apogee_past_reading_bound += u64::from(health.apogee_past_reading_bound);
343 }
344}
345
346impl SetCheck {
347 pub fn lost(&self) -> u64 {
350 let no_barometer = format!("{:?}", Reason::NoBarometer);
351 self.withheld
352 .iter()
353 .filter(|(reason, _)| **reason != no_barometer)
354 .map(|(_, n)| n)
355 .sum()
356 }
357
358 pub fn holds(&self) -> bool {
360 self.lost() == 0
361 && self
362 .cells
363 .iter()
364 .chain(self.by_logger.values().flatten())
365 .all(|c| c.holds)
366 }
367}
368
369pub const BAND_CONFIDENCE: f64 = 0.999;
371
372pub const MISS_SHARE: f64 = 0.05;
374
375fn ln_factorial(k: u64) -> f64 {
377 (2..=k).map(|i| (i as f64).ln()).sum()
378}
379
380fn upper_tail(x: u64, n: u64, p: f64) -> f64 {
382 if x == 0 {
383 return 1.0;
384 }
385 if p <= 0.0 {
386 return 0.0;
387 }
388 if p >= 1.0 {
389 return 1.0;
390 }
391 let ln_n = ln_factorial(n);
392 let (lp, lq) = (p.ln(), (1.0 - p).ln());
393 let mut ln_k = 0.0;
394 let mut below = 0.0;
395 for k in 0..x {
396 if k > 0 {
397 ln_k += (k as f64).ln();
398 }
399 let ln_choose = ln_n - ln_k - ln_factorial(n - k);
400 below += (ln_choose + k as f64 * lp + (n - k) as f64 * lq).exp();
401 }
402 (1.0 - below).max(0.0)
403}
404
405pub fn clopper_pearson_lower(x: u64, n: u64, confidence: f64) -> f64 {
410 if x > n || !(confidence > 0.0 && confidence < 1.0) {
411 return f64::NAN;
412 }
413 if x == 0 {
414 return 0.0;
415 }
416 let target = 0.5 * (1.0 - confidence);
417 let (mut lo, mut hi) = (0.0_f64, x as f64 / n as f64);
418 for _ in 0..200 {
419 let mid = 0.5 * (lo + hi);
420 if upper_tail(x, n, mid) < target {
421 lo = mid;
422 } else {
423 hi = mid;
424 }
425 }
426 0.5 * (lo + hi)
427}
428
429pub fn clopper_pearson_upper(x: u64, n: u64, confidence: f64) -> f64 {
433 if x > n {
434 return f64::NAN;
435 }
436 1.0 - clopper_pearson_lower(n - x, n, confidence)
437}
438
439fn cell(moment: Moment, state: StateName, compared: &[Compared]) -> Cell {
440 let flights = compared.len() as u64;
441 let below = compared.iter().filter(|c| c.truth_below()).count() as u64;
442 let above = compared.iter().filter(|c| c.truth_above()).count() as u64;
443 let inside = compared
445 .iter()
446 .filter(|c| !c.truth_below() && !c.truth_above())
447 .count() as u64;
448 let lowest = [
449 clopper_pearson_lower(below, flights, BAND_CONFIDENCE),
450 clopper_pearson_lower(above, flights, BAND_CONFIDENCE),
451 ];
452 let mut sds: Vec<f64> = compared.iter().map(|c| c.sd).collect();
453 sds.sort_by(f64::total_cmp);
454 let median_sd = if sds.is_empty() {
455 f64::NAN
456 } else {
457 sds[sds.len() / 2]
458 };
459 let largest_sd = sds.last().copied().unwrap_or(f64::NAN);
460 Cell {
461 moment,
462 state,
463 flights,
464 below,
465 above,
466 coverage: if flights == 0 {
467 f64::NAN
468 } else {
469 inside as f64 / flights as f64
470 },
471 lowest_miss_share: lowest,
472 holds: flights > 0 && lowest.iter().all(|&l| l <= MISS_SHARE),
474 median_sd,
475 largest_sd,
476 }
477}
478
479fn cells(checks: &[&Checked]) -> Vec<Cell> {
480 let mut out = Vec::new();
481 for moment in Moment::ALL {
482 for state in StateName::ALL {
483 let compared: Vec<Compared> = checks
484 .iter()
485 .flat_map(|c| c.compared.iter())
486 .filter(|(m, s, _)| *m == moment && *s == state)
487 .map(|(_, _, c)| *c)
488 .collect();
489 out.push(cell(moment, state, &compared));
490 }
491 }
492 out
493}
494
495pub fn tally(checks: &[Checked]) -> SetCheck {
497 let mut withheld = BTreeMap::new();
498 for check in checks {
499 if let Some(reason) = check.withheld {
500 *withheld.entry(format!("{reason:?}")).or_insert(0) += 1;
501 }
502 }
503 let all: Vec<&Checked> = checks.iter().collect();
504 let mut by_logger = BTreeMap::new();
505 for profile in Profile::ALL {
506 let mine: Vec<&Checked> = checks
507 .iter()
508 .filter(|c| c.profile == profile && c.withheld.is_none())
509 .collect();
510 if !mine.is_empty() {
511 by_logger.insert(profile.name().to_owned(), cells(&mine));
512 }
513 }
514 let mut health = HealthTally::default();
515 for h in checks.iter().filter_map(|c| c.health.as_ref()) {
516 health.add(h);
517 }
518 SetCheck {
519 flights: checks.len() as u64,
520 withheld,
521 cells: cells(&all),
522 by_logger,
523 health,
524 }
525}
526
527#[cfg(test)]
528mod tests {
529 use super::*;
530
531 #[test]
532 fn clopper_pearson_matches_the_definition() {
533 let lower = clopper_pearson_lower(5, 100, 0.95);
536 assert!((lower - 0.016_431_879).abs() < 1e-8, "{lower}");
537 let lower = clopper_pearson_lower(50, 1000, 0.999);
538 assert!((lower - 0.030_238_692).abs() < 1e-8, "{lower}");
539 let lower = clopper_pearson_lower(75, 1000, 0.999);
540 assert!((lower - 0.050_351_908).abs() < 1e-8, "{lower}");
541 assert_eq!(clopper_pearson_lower(0, 1000, 0.999), 0.0);
542 assert!(clopper_pearson_lower(5, 3, 0.999).is_nan());
544 assert!(clopper_pearson_lower(1, 0, 0.999).is_nan());
545 assert!(clopper_pearson_lower(5, 100, f64::NAN).is_nan());
546 assert!(clopper_pearson_lower(5, 100, 1.0).is_nan());
547 assert!(clopper_pearson_lower(5, 100, 0.0).is_nan());
548 }
549
550 #[test]
551 fn the_upper_edge_mirrors_the_lower_one() {
552 let holds = |x| {
554 clopper_pearson_lower(x, 1000, 0.999) <= 0.05
555 && 0.05 <= clopper_pearson_upper(x, 1000, 0.999)
556 };
557 assert!(!holds(28) && holds(29) && holds(74) && !holds(75));
558 let holds_820 = |x| {
560 clopper_pearson_lower(x, 820, 0.999) <= 0.05
561 && 0.05 <= clopper_pearson_upper(x, 820, 0.999)
562 };
563 assert!(!holds_820(21) && holds_820(22) && holds_820(63) && !holds_820(64));
564 let holds_845 = |x| {
566 clopper_pearson_lower(x, 845, 0.999) <= 0.05
567 && 0.05 <= clopper_pearson_upper(x, 845, 0.999)
568 };
569 assert!(!holds_845(22) && holds_845(23) && holds_845(64) && !holds_845(65));
570 assert_eq!(clopper_pearson_upper(1000, 1000, 0.999), 1.0);
571 assert!(clopper_pearson_upper(5, 3, 0.999).is_nan());
572 }
573
574 #[test]
575 fn a_log_withheld_with_a_barometer_fails_the_check() {
576 let inside = Compared {
577 error: 0.0,
578 sd: 1.0,
579 };
580 let smoothed = |profile: Profile| Checked {
581 profile,
582 withheld: None,
583 compared: Moment::ALL
584 .iter()
585 .flat_map(|&m| StateName::ALL.iter().map(move |&s| (m, s, inside)))
586 .collect(),
587 health: None,
588 has_gnss: false,
589 };
590 let withheld = |profile: Profile, reason: Reason| Checked {
591 profile,
592 withheld: Some(reason),
593 compared: Vec::new(),
594 health: None,
595 has_gnss: false,
596 };
597 let profile = Profile::ALL[0];
598 let mut checks: Vec<Checked> = (0..100).map(|_| smoothed(profile)).collect();
599 checks.push(withheld(profile, Reason::NoBarometer));
600 let set = tally(&checks);
601 assert_eq!(set.lost(), 0);
602 assert!(set.holds());
603 for reason in [Reason::Diverged, Reason::NoLiftoff, Reason::TooLong] {
604 let mut lost = checks.clone();
605 lost.push(withheld(profile, reason));
606 let set = tally(&lost);
607 assert_eq!(set.lost(), 1, "{reason:?}");
608 assert!(set.cells.iter().all(|c| c.holds), "{reason:?}");
610 assert!(!set.holds(), "{reason:?}");
611 }
612 }
613
614 #[test]
615 fn a_cell_holds_until_its_misses_show_more_than_five_percent() {
616 let make = |misses: usize| -> Vec<Compared> {
617 (0..1000)
618 .map(|k| Compared {
619 error: if k < misses { 2.0 } else { 0.0 },
620 sd: 1.0,
621 })
622 .collect()
623 };
624 assert!(cell(Moment::Apogee, StateName::Height, &make(50)).holds);
625 assert!(cell(Moment::Apogee, StateName::Height, &make(74)).holds);
627 assert!(!cell(Moment::Apogee, StateName::Height, &make(75)).holds);
628 }
629
630 #[test]
631 fn a_comparison_that_isnt_a_number_misses_and_an_empty_cell_shows_nothing() {
632 let nan = Compared {
633 error: f64::NAN,
634 sd: 1.0,
635 };
636 let inside = Compared {
637 error: 0.0,
638 sd: 1.0,
639 };
640 let c = cell(Moment::Apogee, StateName::Height, &[nan, inside, inside]);
641 assert_eq!((c.flights, c.below, c.above), (3, 1, 1));
642 assert!((c.coverage - 2.0 / 3.0).abs() < 1e-12, "{}", c.coverage);
643 assert!(!cell(Moment::Apogee, StateName::Height, &[]).holds);
644 }
645}