moce patches
This commit is contained in:
parent
2c0dc760d9
commit
5665d68db0
37 changed files with 1472 additions and 570 deletions
691
patches/patient-monitor-simulation-v0.4.2-to-v0.4.3.patch
Normal file
691
patches/patient-monitor-simulation-v0.4.2-to-v0.4.3.patch
Normal file
|
|
@ -0,0 +1,691 @@
|
|||
diff --git a/Cargo.toml b/Cargo.toml
|
||||
index 02a5d17..e273c5e 100644
|
||||
--- a/Cargo.toml
|
||||
+++ b/Cargo.toml
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "patient-monitor-simulation"
|
||||
-version = "0.4.2"
|
||||
+version = "0.4.3"
|
||||
edition = "2021"
|
||||
license = "MIT"
|
||||
description = "Synthetic patient-monitor simulation for teaching (not for clinical use)"
|
||||
diff --git a/README.md b/README.md
|
||||
index c693e16..cdc1f9b 100644
|
||||
--- a/README.md
|
||||
+++ b/README.md
|
||||
@@ -1,4 +1,4 @@
|
||||
-# Patient Monitor Simulation (PaMSim) — v0.4.2
|
||||
+# Patient Monitor Simulation (PaMSim) — v0.4.3
|
||||
|
||||
A two-window, Rust/GTK4 patient-monitor simulator for teaching on Fedora Linux.
|
||||
|
||||
@@ -131,15 +131,21 @@ Admin → **Pulse oximetry** has two additional controls:
|
||||
| Setting | Pleth waveform | Monitor display |
|
||||
| --- | --- | --- |
|
||||
| **0 — None** | No waveform | Flashing **NO READING**, PR `--`; no stars |
|
||||
-| **1 — Poor** | Small, irregularly amplitude-distorted pulse | 1 star (★) |
|
||||
+| **1 — Poor** | Small, irregularly distorted pulse, with temporary losses of signal | 1 star (★) when available; blinking **NO READING** during loss |
|
||||
| **2 — Adequate** | Medium, smoothly damped pulse | 2 stars (★★) |
|
||||
| **3 — Good** | Full nominal, smoothly damped pulse | 3 stars (★★★) |
|
||||
|
||||
Default: sensor attached; signal quality **3 — Good**. At levels 1–3, the
|
||||
instructor-set SpO₂ number is shown only when mechanical pulses are detected.
|
||||
-The signal quality does **not** randomly alter that number: it affects the
|
||||
-pleth morphology and the quality indicator only. The simulated pulse rate
|
||||
-remains derived from mechanically captured QRS complexes.
|
||||
+In **Poor** quality, the signal also has intermittent complete dropouts:
|
||||
+**30–180 seconds of signal** between interruptions, with each interruption
|
||||
+lasting **5–20 seconds**. During an interruption the pleth trace disappears,
|
||||
+SpO₂ and PR are unavailable, and **NO READING** flashes. Following recovery,
|
||||
+the Poor waveform and numerical readings return automatically. These intervals
|
||||
+are pseudo-random and driven by simulation time, independent of frame rate;
|
||||
+they reset when Poor quality is selected again. The dropout does not affect
|
||||
+the actual saturation setting, ECG, derived mechanical pulse generation, or ART.
|
||||
+The simulated pulse rate remains derived from captured QRS complexes.
|
||||
|
||||
The degraded waveform uses deterministic, smoothly interpolated pseudo-noise
|
||||
that scales only existing pleth pulses: it cannot manufacture pulses during
|
||||
@@ -159,8 +165,12 @@ can be taught.
|
||||
controlled by a pulse-rate slider.
|
||||
- A captured QRS leads to an ART upstroke after a schematic **120 ms**, and a
|
||||
peripheral pleth pulse after **300 ms**. Pleth is strongly damped/filtered.
|
||||
+- VT and PVT selections start with a heart rate of **160 bpm**, and update
|
||||
+ both the HR slider and numeric entry. The instructor may modify this afterward.
|
||||
- PVT, VF, asystole and PEA suppress mechanical pulses; the ART trace then
|
||||
drifts smoothly at 0–5 mmHg rather than representing a pulsatile pressure.
|
||||
+ The ART **SYS** number shows the rounded low transducer pressure (0–5 mmHg),
|
||||
+ while DIA and MAP show `--` because they cannot be reliably estimated.
|
||||
Previous pressure samples scroll out of view. NIBP is a static,
|
||||
separately entered cuff reading.
|
||||
|
||||
@@ -297,6 +307,26 @@ environment; run `cargo test` and `cargo run --release` on Fedora.
|
||||
- **VT** remains potentially perfusing. The new **PVT** (pulseless ventricular tachycardia) rhythm retains organized broad QRS complexes but produces no arterial/pleth pulses. VF and asystole are also pulseless regardless of capture selection.
|
||||
- **ECG**: a flashing **ARRHYTHMIA** message for VT, PVT and VF; a flashing **ASYSTOLE** message for asystole. These event alarms do not depend on numerical HR limits.
|
||||
- **SpO₂**: when the probe is disconnected, a flashing **NO SENSOR** replaces the saturation reading. Quality 0, loss of mechanical capture or a pulseless rhythm results in flashing **NO READING** (no numerical SpO₂/PR). Sensor disconnection takes precedence. No pleth waveform is displayed in these conditions.
|
||||
-- **ART**: without mechanical pulses (including PVT/VF/asystole/PEA), the simulated invasive pressure line has slow non-pulsatile drift between **0 and 5 mmHg**. The numerical blood-pressure reading remains unavailable (`--/--`), rather than misrepresenting this baseline as true systolic and diastolic pressures.
|
||||
+- **ART**: without mechanical pulses (including PVT/VF/asystole/PEA), the simulated invasive pressure line has slow non-pulsatile drift between **0 and 5 mmHg**. In v0.4.2, the numeric display showed `--/--`; v0.4.3 changes this to a rounded measured SYS of 0–5 mmHg, with `--` for DIA and MAP.
|
||||
- **ECG rhythm name** has been removed from the monitor; only HR and the associated alarm message, when present, are visible. The instructor continues to select the rhythm in Admin.
|
||||
- Numerical alarm thresholds and the Adult preset from v0.4.1 are unchanged. Synthetic demonstration behavior only; not suitable for clinical decisions.
|
||||
+
|
||||
+
|
||||
+## v0.4.3 — Low arterial pressure, VT rate preset and intermittent Poor SpO₂
|
||||
+
|
||||
+- Pulseless rhythms, PEA or otherwise absent mechanical capture: invasive SYS
|
||||
+ reports the **current measured 0–5 mmHg** pressure, e.g. `3/--` with
|
||||
+ `MAP -- mmHg`. **Only SYS** is tested against configured ART SYS limits;
|
||||
+ no spurious MAP/DIA threshold alarms are generated from missing readings.
|
||||
+- Selecting **VT** or **PVT** in Admin sets HR to **160 bpm** and updates the
|
||||
+ associated slider and text field. Subsequent manual rate changes are allowed.
|
||||
+ Perfusing VT still produces QRS-linked pulses subject to capture settings;
|
||||
+ PVT remains pulseless.
|
||||
+- **Poor (★)** SpO₂ signal periodically changes to **NO READING** for a
|
||||
+ pseudo-random **5–20 seconds**, after a pseudo-random **30–180 seconds** of
|
||||
+ usable low-quality signal. The technical alarm blinks, SpO₂/PR readings are
|
||||
+ suppressed, and the pleth waveform is absent. On recovery it resumes at Poor
|
||||
+ quality. The simulated signal history retains visible gaps for outages.
|
||||
+ Choosing another signal quality immediately clears the intermittent outage.
|
||||
+- No changes to Adult alarm presets, alarm limit ordering, or the 0–200 mmHg
|
||||
+ arterial scale. All behavior is synthetic and intended for teaching only.
|
||||
diff --git a/src/model.rs b/src/model.rs
|
||||
index 6b1f6d1..03d7bbe 100644
|
||||
--- a/src/model.rs
|
||||
+++ b/src/model.rs
|
||||
@@ -221,12 +221,29 @@ pub struct Parameters {
|
||||
}
|
||||
|
||||
impl Parameters {
|
||||
+ /// Selecting a ventricular tachycardia starts at the teaching default of
|
||||
+ /// 160 bpm. The instructor can subsequently adjust the heart rate.
|
||||
+ pub fn select_ecg_rhythm(&mut self, rhythm: EcgRhythm) {
|
||||
+ self.ecg_rhythm = rhythm;
|
||||
+ if matches!(rhythm, EcgRhythm::VentricularTachycardia
|
||||
+ | EcgRhythm::PulselessVentricularTachycardia) {
|
||||
+ self.heart_rate = 160;
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
/// No saturation without an attached sensor, usable signal quality and
|
||||
/// two observed mechanical pulse events. Thresholds do not control this alarm.
|
||||
pub fn spo2_status(&self, pulse_rate: Option<i32>) -> Spo2Status {
|
||||
+ self.spo2_status_during(pulse_rate, false)
|
||||
+ }
|
||||
+
|
||||
+ /// Poor-quality signal interruptions are generated by the simulator and
|
||||
+ /// override the normal reading, but not a physically disconnected sensor.
|
||||
+ pub fn spo2_status_during(&self, pulse_rate: Option<i32>, signal_lost: bool) -> Spo2Status {
|
||||
if !self.spo2_sensor_attached {
|
||||
Spo2Status::NoSensor
|
||||
- } else if self.spo2_quality == Spo2Quality::None || pulse_rate.is_none()
|
||||
+ } else if (signal_lost && self.spo2_quality == Spo2Quality::Poor)
|
||||
+ || self.spo2_quality == Spo2Quality::None || pulse_rate.is_none()
|
||||
|| !self.ecg_rhythm.can_generate_pulse()
|
||||
|| self.pulse_conduction == PulseConduction::None
|
||||
|| self.heart_rate <= 0
|
||||
@@ -368,6 +385,34 @@ mod tests {
|
||||
assert_eq!(AtrialFibrillation.rhythm_alarm(), None);
|
||||
}
|
||||
|
||||
+ #[test]
|
||||
+ fn tachycardia_selection_sets_160_bpm_but_other_rhythms_do_not() {
|
||||
+ let mut p = Parameters::default();
|
||||
+ p.select_ecg_rhythm(EcgRhythm::VentricularTachycardia);
|
||||
+ assert_eq!(p.heart_rate, 160);
|
||||
+ p.heart_rate = 185; // a subsequent instructor override remains possible
|
||||
+ p.select_ecg_rhythm(EcgRhythm::Sinus);
|
||||
+ assert_eq!(p.heart_rate, 185);
|
||||
+ p.select_ecg_rhythm(EcgRhythm::PulselessVentricularTachycardia);
|
||||
+ assert_eq!(p.heart_rate, 160);
|
||||
+ assert!(!p.ecg_rhythm.can_generate_pulse());
|
||||
+ }
|
||||
+
|
||||
+ #[test]
|
||||
+ fn transient_poor_signal_loss_preserves_sensor_precedence() {
|
||||
+ let mut p = Parameters::default();
|
||||
+ p.spo2_quality = Spo2Quality::Poor;
|
||||
+ assert_eq!(p.spo2_status_during(Some(80), false), Spo2Status::Available);
|
||||
+ assert_eq!(p.spo2_status_during(Some(80), true), Spo2Status::NoReading);
|
||||
+ p.spo2_sensor_attached = false;
|
||||
+ assert_eq!(p.spo2_status_during(Some(80), true), Spo2Status::NoSensor);
|
||||
+ p.spo2_sensor_attached = true;
|
||||
+ p.spo2_quality = Spo2Quality::Good;
|
||||
+ assert_eq!(p.spo2_status_during(Some(80), false), Spo2Status::Available);
|
||||
+ assert_eq!(p.spo2_status_during(Some(80), true), Spo2Status::Available);
|
||||
+
|
||||
+ }
|
||||
+
|
||||
#[test]
|
||||
fn sensor_and_no_reading_alarms_override_spo2_thresholds() {
|
||||
let mut p = Parameters::default();
|
||||
diff --git a/src/render.rs b/src/render.rs
|
||||
index c0f8474..3412859 100644
|
||||
--- a/src/render.rs
|
||||
+++ b/src/render.rs
|
||||
@@ -105,8 +105,9 @@ fn pleth_trace_enabled(p: &Parameters) -> bool {
|
||||
p.spo2_sensor_attached && p.spo2_quality != Spo2Quality::None
|
||||
}
|
||||
|
||||
-fn spo2_vitals(p: &Parameters, pulse_rate: Option<i32>) -> (String, String, String) {
|
||||
- match p.spo2_status(pulse_rate) {
|
||||
+fn spo2_vitals(p: &Parameters, pulse_rate: Option<i32>, signal_lost: bool)
|
||||
+ -> (String, String, String) {
|
||||
+ match p.spo2_status_during(pulse_rate, signal_lost) {
|
||||
Spo2Status::NoSensor => (
|
||||
"SpO₂".to_owned(), "NO SENSOR".to_owned(), "PR -- bpm".to_owned()),
|
||||
Spo2Status::NoReading => (
|
||||
@@ -143,7 +144,7 @@ fn draw_vitals(cr: &Context, ch: Channel, color: Color, x: f64,
|
||||
let ecg_hr = if p.ecg_rhythm.has_qrs() && p.heart_rate > 0 {
|
||||
Some(p.heart_rate)
|
||||
} else { None };
|
||||
- let spo2_status = p.spo2_status(pulse_rate);
|
||||
+ let spo2_status = p.spo2_status_during(pulse_rate, sim.poor_signal_lost());
|
||||
let oxygen = if spo2_status == Spo2Status::Available { Some(p.spo2) } else { None };
|
||||
let art_map = mean_arterial_pressure(p.art_systolic, p.art_diastolic);
|
||||
|
||||
@@ -173,7 +174,7 @@ fn draw_vitals(cr: &Context, ch: Channel, color: Color, x: f64,
|
||||
}
|
||||
}
|
||||
Channel::Pleth => {
|
||||
- let (label, value, info) = spo2_vitals(p, pulse_rate);
|
||||
+ let (label, value, info) = spo2_vitals(p, pulse_rate, sim.poor_signal_lost());
|
||||
text(cr, &label, x, top + row_h * 0.24, label_size, color, false);
|
||||
let technical_alarm = spo2_status != Spo2Status::Available;
|
||||
let value_size = if technical_alarm {
|
||||
@@ -212,8 +213,14 @@ fn draw_vitals(cr: &Context, ch: Channel, color: Color, x: f64,
|
||||
alarmed(p.alarms.art_map, Some(art_map)), flash);
|
||||
}
|
||||
} else {
|
||||
- draw_alarm_number(cr, "--/--", x, value_y, number_size * 0.78,
|
||||
- width, color, false, flash);
|
||||
+ // Without mechanical activity the transducer still reads the
|
||||
+ // slowly drifting 0..5 mmHg pressure. DIA/MAP are indeterminate.
|
||||
+ let pulseless = !has_capture;
|
||||
+ let sys = if pulseless { sim.measured_art_pressure() } else { None };
|
||||
+ let displayed_sys = sys.map_or_else(|| "--".to_owned(), |v| v.to_string());
|
||||
+ draw_pressure_components(cr, &displayed_sys, "--", x, value_y,
|
||||
+ number_size * 0.78, width, color,
|
||||
+ alarmed(p.alarms.art_sys, sys), false, flash);
|
||||
text(cr, "MAP -- mmHg", x, info_y, sub_size, color, false);
|
||||
}
|
||||
}
|
||||
@@ -270,17 +277,24 @@ fn draw_pressure_value(cr: &Context, systolic: i32, diastolic: i32,
|
||||
x: f64, baseline: f64, nominal_size: f64,
|
||||
available_width: f64, color: Color,
|
||||
sys_alarm: bool, dia_alarm: bool, flash: bool) {
|
||||
- let sys = systolic.to_string();
|
||||
- let dia = diastolic.to_string();
|
||||
+ draw_pressure_components(cr, &systolic.to_string(), &diastolic.to_string(),
|
||||
+ x, baseline, nominal_size, available_width, color, sys_alarm, dia_alarm, flash);
|
||||
+}
|
||||
+
|
||||
+#[allow(clippy::too_many_arguments)]
|
||||
+fn draw_pressure_components(cr: &Context, sys: &str, dia: &str,
|
||||
+ x: f64, baseline: f64, nominal_size: f64,
|
||||
+ available_width: f64, color: Color,
|
||||
+ sys_alarm: bool, dia_alarm: bool, flash: bool) {
|
||||
let full = format!("{}/{}", sys, dia);
|
||||
let size = fitting_size(cr, &full, nominal_size, available_width, true);
|
||||
let _ = cr.save();
|
||||
cr.rectangle(x, baseline - size * 1.28, available_width.max(1.0), size * 1.65);
|
||||
cr.clip();
|
||||
let mut cursor = x;
|
||||
- cursor += draw_segment(cr, &sys, cursor, baseline, size, color, sys_alarm && flash, true);
|
||||
+ cursor += draw_segment(cr, sys, cursor, baseline, size, color, sys_alarm && flash, true);
|
||||
cursor += draw_segment(cr, "/", cursor, baseline, size, color, false, true);
|
||||
- draw_segment(cr, &dia, cursor, baseline, size, color, dia_alarm && flash, true);
|
||||
+ draw_segment(cr, dia, cursor, baseline, size, color, dia_alarm && flash, true);
|
||||
let _ = cr.restore();
|
||||
}
|
||||
|
||||
@@ -378,7 +392,7 @@ fn draw_trace(cr: &Context, channel: Channel, color: Color,
|
||||
// Disconnected or absent pleth signal: no curve, including historical pulses.
|
||||
if matches!(channel, Channel::Pleth)
|
||||
&& (!pleth_trace_enabled(p)
|
||||
- || p.spo2_status(sim.pulse_rate()) != Spo2Status::Available)
|
||||
+ || p.spo2_status_during(sim.pulse_rate(), sim.poor_signal_lost()) != Spo2Status::Available)
|
||||
{
|
||||
let _ = cr.restore();
|
||||
return;
|
||||
@@ -393,6 +407,10 @@ fn draw_trace(cr: &Context, channel: Channel, color: Color,
|
||||
let x = right - (last_t - s.time) / VISIBLE_SECONDS * trace_width;
|
||||
let amplitude = normalized(channel, s, p);
|
||||
let y = bottom - amplitude * trace_height;
|
||||
+ if matches!(channel, Channel::Pleth) && !s.pleth_valid {
|
||||
+ if started { let _ = cr.stroke(); started = false; }
|
||||
+ continue;
|
||||
+ }
|
||||
if started { cr.line_to(x, y); }
|
||||
else { cr.move_to(x, y); started = true; }
|
||||
}
|
||||
@@ -540,13 +558,13 @@ mod tests {
|
||||
assert!(pleth_trace_enabled(&p));
|
||||
p.spo2_sensor_attached = false;
|
||||
assert!(!pleth_trace_enabled(&p));
|
||||
- assert_eq!(spo2_vitals(&p, Some(75)).1, "NO SENSOR");
|
||||
- assert!(spo2_vitals(&p, Some(75)).2.contains("PR --"));
|
||||
+ assert_eq!(spo2_vitals(&p, Some(75), false).1, "NO SENSOR");
|
||||
+ assert!(spo2_vitals(&p, Some(75), false).2.contains("PR --"));
|
||||
p.spo2_sensor_attached = true;
|
||||
p.spo2_quality = Spo2Quality::None;
|
||||
assert!(!pleth_trace_enabled(&p));
|
||||
- assert_eq!(spo2_vitals(&p, Some(75)).1, "NO READING");
|
||||
- assert!(spo2_vitals(&p, Some(75)).2.contains("PR --"));
|
||||
+ assert_eq!(spo2_vitals(&p, Some(75), false).1, "NO READING");
|
||||
+ assert!(spo2_vitals(&p, Some(75), false).2.contains("PR --"));
|
||||
assert!(p.show_spo2); // no readings, but visible row and status
|
||||
}
|
||||
|
||||
@@ -554,29 +572,37 @@ mod tests {
|
||||
fn poor_and_good_quality_keep_derived_pr_and_quality_indicators() {
|
||||
let mut p = Parameters::default();
|
||||
p.spo2_quality = Spo2Quality::Poor;
|
||||
- assert_eq!(spo2_vitals(&p, Some(75)).0, "SpO₂ ★");
|
||||
- assert_eq!(spo2_vitals(&p, Some(75)).1, "98");
|
||||
- assert!(spo2_vitals(&p, Some(75)).2.contains("PR 75 bpm"));
|
||||
+ assert_eq!(spo2_vitals(&p, Some(75), false).0, "SpO₂ ★");
|
||||
+ assert_eq!(spo2_vitals(&p, Some(75), false).1, "98");
|
||||
+ assert!(spo2_vitals(&p, Some(75), false).2.contains("PR 75 bpm"));
|
||||
p.spo2_quality = Spo2Quality::Good;
|
||||
- assert_eq!(spo2_vitals(&p, Some(75)).0, "SpO₂ ★★★");
|
||||
- assert_eq!(spo2_vitals(&p, None).1, "NO READING");
|
||||
- assert!(spo2_vitals(&p, None).2.contains("PR --"));
|
||||
+ assert_eq!(spo2_vitals(&p, Some(75), false).0, "SpO₂ ★★★");
|
||||
+ assert_eq!(spo2_vitals(&p, None, false).1, "NO READING");
|
||||
+ assert!(spo2_vitals(&p, None, false).2.contains("PR --"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_sensor_precedes_no_reading_and_both_are_spo2_alarms() {
|
||||
let mut p = Parameters::default();
|
||||
- assert_eq!(spo2_vitals(&p, None).1, "NO READING");
|
||||
+ assert_eq!(spo2_vitals(&p, None, false).1, "NO READING");
|
||||
p.ecg_rhythm = crate::model::EcgRhythm::PulselessVentricularTachycardia;
|
||||
- assert_eq!(spo2_vitals(&p, Some(100)).1, "NO READING");
|
||||
+ assert_eq!(spo2_vitals(&p, Some(100), false).1, "NO READING");
|
||||
p.spo2_sensor_attached = false;
|
||||
- assert_eq!(spo2_vitals(&p, Some(100)).1, "NO SENSOR");
|
||||
+ assert_eq!(spo2_vitals(&p, Some(100), false).1, "NO SENSOR");
|
||||
+ }
|
||||
+
|
||||
+ #[test]
|
||||
+ fn intermittent_poor_spo2_loss_raises_no_reading_not_no_sensor() {
|
||||
+ let p = Parameters { spo2_quality: Spo2Quality::Poor, ..Parameters::default() };
|
||||
+ assert_eq!(spo2_vitals(&p, Some(75), true).1, "NO READING");
|
||||
+ assert!(spo2_vitals(&p, Some(75), true).2.contains("PR --"));
|
||||
+ assert_eq!(spo2_vitals(&p, Some(75), false).1, "98");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn arterial_axis_is_absolute_and_fixed_to_200_mmhg() {
|
||||
let p = Parameters::default();
|
||||
- let mut s = Sample { time: 0.0, ecg: 0.0, pleth: 0.0, art: 100.0, co2: 0.0 };
|
||||
+ let mut s = Sample { time: 0.0, ecg: 0.0, pleth: 0.0, pleth_valid: true, art: 100.0, co2: 0.0 };
|
||||
assert_eq!(normalized(Channel::Art, &s, &p), 0.5);
|
||||
s.art = 200.0;
|
||||
assert_eq!(normalized(Channel::Art, &s, &p), 1.0);
|
||||
diff --git a/src/simulation.rs b/src/simulation.rs
|
||||
index a9f5c7b..a2b7bc5 100644
|
||||
--- a/src/simulation.rs
|
||||
+++ b/src/simulation.rs
|
||||
@@ -2,7 +2,7 @@
|
||||
//! A conducted QRS schedules an arterial pulse and a delayed, smoothed pleth pulse.
|
||||
use std::collections::VecDeque;
|
||||
|
||||
-use crate::model::{ArterialShape, CapnoShape, EcgRhythm, Parameters, PulseConduction};
|
||||
+use crate::model::{ArterialShape, CapnoShape, EcgRhythm, Parameters, PulseConduction, Spo2Quality};
|
||||
|
||||
const SAMPLE_STEP: f64 = 0.004; // 250 Hz; adequately samples the QRS upstroke
|
||||
const HISTORY_SECONDS: f64 = 10.0;
|
||||
@@ -32,10 +32,59 @@ pub struct Sample {
|
||||
pub time: f64,
|
||||
pub ecg: f64, // arbitrary amplitude
|
||||
pub pleth: f64, // attenuated, low-pass-filtered amplitude 0..1
|
||||
+ pub pleth_valid: bool, // gaps during intermittent signal dropout
|
||||
pub art: f64, // absolute pressure in mmHg; display clips above 200
|
||||
pub co2: f64, // mmHg
|
||||
}
|
||||
|
||||
+/// Reproducible pseudo-random outages of the pulse-oximeter in Poor quality.
|
||||
+/// Each recovery is followed by 30..180 s of signal before a 5..20 s dropout.
|
||||
+/// The schedule advances in simulation seconds and does not depend on FPS.
|
||||
+#[derive(Debug)]
|
||||
+struct PoorSignalOutages {
|
||||
+ rng: u64,
|
||||
+ enabled: bool,
|
||||
+ active: bool,
|
||||
+ next_start: f64,
|
||||
+ end: f64,
|
||||
+}
|
||||
+
|
||||
+impl PoorSignalOutages {
|
||||
+ fn new() -> Self {
|
||||
+ Self { rng: 0xC3A5_C85C_97CB_3127, enabled: false,
|
||||
+ active: false, next_start: 0.0, end: 0.0 }
|
||||
+ }
|
||||
+
|
||||
+ fn random_range(&mut self, lower: f64, upper: f64) -> f64 {
|
||||
+ // 64-bit LCG; deterministic, not cryptographic randomness.
|
||||
+ self.rng = self.rng.wrapping_mul(6_364_136_223_846_793_005)
|
||||
+ .wrapping_add(1_442_695_040_888_963_407);
|
||||
+ let fraction = (self.rng >> 11) as f64 / (1_u64 << 53) as f64;
|
||||
+ lower + (upper - lower) * fraction
|
||||
+ }
|
||||
+
|
||||
+ fn update(&mut self, now: f64, poor: bool) {
|
||||
+ if !poor {
|
||||
+ self.enabled = false;
|
||||
+ self.active = false;
|
||||
+ return;
|
||||
+ }
|
||||
+ // Deliberately do not schedule during the negative-time trace warmup.
|
||||
+ if now < 0.0 { return; }
|
||||
+ if !self.enabled {
|
||||
+ self.enabled = true;
|
||||
+ self.active = false;
|
||||
+ self.next_start = now + self.random_range(30.0, 180.0);
|
||||
+ } else if self.active && now >= self.end {
|
||||
+ self.active = false;
|
||||
+ self.next_start = now + self.random_range(30.0, 180.0);
|
||||
+ } else if !self.active && now >= self.next_start {
|
||||
+ self.active = true;
|
||||
+ self.end = now + self.random_range(5.0, 20.0);
|
||||
+ }
|
||||
+ }
|
||||
+}
|
||||
+
|
||||
pub struct Simulator {
|
||||
samples: VecDeque<Sample>,
|
||||
time: f64,
|
||||
@@ -48,6 +97,7 @@ pub struct Simulator {
|
||||
previous_rhythm: EcgRhythm,
|
||||
previous_conduction: PulseConduction,
|
||||
previous_heart_rate: i32,
|
||||
+ poor_signal_outages: PoorSignalOutages,
|
||||
pending_time: f64,
|
||||
}
|
||||
|
||||
@@ -65,6 +115,7 @@ impl Simulator {
|
||||
previous_rhythm: params.ecg_rhythm,
|
||||
previous_conduction: params.pulse_conduction,
|
||||
previous_heart_rate: params.heart_rate,
|
||||
+ poor_signal_outages: PoorSignalOutages::new(),
|
||||
pending_time: 0.0,
|
||||
};
|
||||
// Prepopulate the trace and pulse history, including the initial pulse rate.
|
||||
@@ -83,6 +134,15 @@ impl Simulator {
|
||||
}
|
||||
}
|
||||
|
||||
+ pub fn poor_signal_lost(&self) -> bool {
|
||||
+ self.poor_signal_outages.active
|
||||
+ }
|
||||
+
|
||||
+ /// Last arterial transducer sample, in mmHg (including slow 0..5 drift).
|
||||
+ pub fn measured_art_pressure(&self) -> Option<i32> {
|
||||
+ self.samples.back().map(|sample| sample.art.round() as i32)
|
||||
+ }
|
||||
+
|
||||
pub fn samples(&self) -> &VecDeque<Sample> {
|
||||
&self.samples
|
||||
}
|
||||
@@ -106,6 +166,8 @@ impl Simulator {
|
||||
|
||||
fn step(&mut self, params: &Parameters) {
|
||||
self.time += SAMPLE_STEP;
|
||||
+ self.poor_signal_outages.update(self.time,
|
||||
+ params.spo2_quality == Spo2Quality::Poor && params.spo2_sensor_attached);
|
||||
|
||||
if params.ecg_rhythm != self.previous_rhythm
|
||||
|| params.pulse_conduction != self.previous_conduction
|
||||
@@ -180,7 +242,10 @@ impl Simulator {
|
||||
capno_amplitude(self.respiratory_phase, params.capno_shape)
|
||||
};
|
||||
|
||||
- self.samples.push_back(Sample { time: self.time, ecg, pleth, art, co2 });
|
||||
+ let pleth_valid = params.spo2_sensor_attached
|
||||
+ && params.spo2_quality != Spo2Quality::None
|
||||
+ && !self.poor_signal_outages.active;
|
||||
+ self.samples.push_back(Sample { time: self.time, ecg, pleth, pleth_valid, art, co2 });
|
||||
while self.samples.front().is_some_and(|first| first.time < self.time - HISTORY_SECONDS) {
|
||||
self.samples.pop_front();
|
||||
}
|
||||
@@ -476,6 +541,68 @@ mod tests {
|
||||
assert!(spread > 0.25);
|
||||
}
|
||||
|
||||
+ #[test]
|
||||
+ fn low_arterial_pressure_stays_measurable_without_pulses() {
|
||||
+ for rhythm in [EcgRhythm::PulselessVentricularTachycardia,
|
||||
+ EcgRhythm::VentricularFibrillation, EcgRhythm::Asystole] {
|
||||
+ let p = Parameters { ecg_rhythm: rhythm, ..Parameters::default() };
|
||||
+ let mut sim = Simulator::new(&p);
|
||||
+ for _ in 0..500 { sim.step(&p); }
|
||||
+ assert_eq!(sim.pulse_rate(), None);
|
||||
+ let pressure = sim.measured_art_pressure().unwrap();
|
||||
+ assert!((0..=5).contains(&pressure), "{rhythm:?}: {pressure}");
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ #[test]
|
||||
+ fn poor_signal_outages_have_random_intervals_and_durations() {
|
||||
+ let mut outage = PoorSignalOutages::new();
|
||||
+ outage.update(0.0, true);
|
||||
+ let mut start = 0.0;
|
||||
+ let mut gaps = Vec::new();
|
||||
+ let mut durations = Vec::new();
|
||||
+ for _ in 0..16 {
|
||||
+ let next = outage.next_start;
|
||||
+ gaps.push(next - start);
|
||||
+ assert!((30.0..=180.0).contains(&(next - start)));
|
||||
+ outage.update(next, true);
|
||||
+ assert!(outage.active);
|
||||
+ let end = outage.end;
|
||||
+ durations.push(end - next);
|
||||
+ assert!((5.0..=20.0).contains(&(end - next)));
|
||||
+ outage.update(end, true);
|
||||
+ assert!(!outage.active);
|
||||
+ start = end;
|
||||
+ }
|
||||
+ assert!(gaps.iter().any(|v| (v - gaps[0]).abs() > 1.0));
|
||||
+ assert!(durations.iter().any(|v| (v - durations[0]).abs() > 1.0));
|
||||
+ outage.update(start + 1.0, false);
|
||||
+ assert!(!outage.active);
|
||||
+ outage.update(start + 2.0, true);
|
||||
+ assert!(!outage.active); // switching back to Poor reschedules
|
||||
+ }
|
||||
+
|
||||
+ #[test]
|
||||
+ fn poor_signal_dropout_does_not_change_art_or_ecg() {
|
||||
+ let mut p = Parameters::default();
|
||||
+ p.spo2_quality = Spo2Quality::Poor;
|
||||
+ let mut sim = Simulator::new(&p);
|
||||
+ // Force an outage to test signal gaps without synthesizing minutes.
|
||||
+ sim.poor_signal_outages.enabled = true;
|
||||
+ sim.poor_signal_outages.active = true;
|
||||
+ sim.poor_signal_outages.end = sim.time + 10.0;
|
||||
+ sim.step(&p);
|
||||
+ assert!(!sim.samples.back().unwrap().pleth_valid);
|
||||
+ assert!(sim.poor_signal_lost());
|
||||
+ assert!((0.0..=200.0).contains(&sim.samples.back().unwrap().art));
|
||||
+ assert_eq!(p.spo2_status_during(sim.pulse_rate(), sim.poor_signal_lost()),
|
||||
+ crate::model::Spo2Status::NoReading);
|
||||
+ p.spo2_quality = Spo2Quality::Good;
|
||||
+ sim.step(&p);
|
||||
+ assert!(!sim.poor_signal_lost());
|
||||
+ assert!(sim.samples.back().unwrap().pleth_valid);
|
||||
+ }
|
||||
+
|
||||
#[test]
|
||||
fn pulse_rate_follows_two_to_one_capture() {
|
||||
let p = Parameters { pulse_conduction: PulseConduction::TwoToOne, ..Parameters::default() };
|
||||
diff --git a/src/ui.rs b/src/ui.rs
|
||||
index 669c227..55d31cb 100644
|
||||
--- a/src/ui.rs
|
||||
+++ b/src/ui.rs
|
||||
@@ -145,11 +145,11 @@ fn create_admin(app: &Application, params: SharedParams,
|
||||
|
||||
let ecg = group(&controls, "ECG");
|
||||
add_visibility(&ecg, params.borrow().show_ecg, params.clone(), |p, show| p.show_ecg = show);
|
||||
- add_dropdown(&ecg, "ECG rhythm", &[
|
||||
+ let rhythm_dropdown = add_dropdown_at(&ecg, "ECG rhythm", &[
|
||||
"Sinus rhythm", "Atrial fibrillation", "Ventricular tachycardia (VT)",
|
||||
"Pulseless ventricular tachycardia (PVT)", "Ventricular fibrillation", "Asystole",
|
||||
- ], params.clone(), |p, selected| {
|
||||
- p.ecg_rhythm = match selected {
|
||||
+ ], 0, params.clone(), |p, selected| {
|
||||
+ let rhythm = match selected {
|
||||
1 => EcgRhythm::AtrialFibrillation,
|
||||
2 => EcgRhythm::VentricularTachycardia,
|
||||
3 => EcgRhythm::PulselessVentricularTachycardia,
|
||||
@@ -157,16 +157,25 @@ fn create_admin(app: &Application, params: SharedParams,
|
||||
5 => EcgRhythm::Asystole,
|
||||
_ => EcgRhythm::Sinus,
|
||||
};
|
||||
+ p.select_ecg_rhythm(rhythm);
|
||||
+ });
|
||||
+ let (hr_row, hr_slider, hr_entry) = add_numeric(&ecg, "Heart rate", "bpm", 0, 240,
|
||||
+ params.borrow().heart_rate, params.clone(), |p, v| p.heart_rate = v);
|
||||
+ // Programmatic 160 bpm VT/PVT preset must update both controls,
|
||||
+ // including an HR field that currently has keyboard focus.
|
||||
+ rhythm_dropdown.connect_selected_notify(move |selector| {
|
||||
+ if selector.selected() == 2 || selector.selected() == 3 {
|
||||
+ hr_slider.set_value(160.0);
|
||||
+ hr_entry.set_text("160");
|
||||
+ }
|
||||
});
|
||||
- let hr_row = add_numeric(&ecg, "Heart rate", "bpm", 0, 240, params.borrow().heart_rate,
|
||||
- params.clone(), |p, v| p.heart_rate = v);
|
||||
alarm_editors.push((add_alarm_limits(&hr_row, "", "", 240,
|
||||
params.borrow().alarms.hr, params.clone(), |p, v| p.alarms.hr = v,
|
||||
&preset_dropdown, &updating_preset), |s| s.hr));
|
||||
|
||||
let spo2 = group(&controls, "Pulse oximetry");
|
||||
add_visibility(&spo2, params.borrow().show_spo2, params.clone(), |p, show| p.show_spo2 = show);
|
||||
- let spo2_row = add_numeric(&spo2, "SpO₂", "%", 0, 100, params.borrow().spo2,
|
||||
+ let (spo2_row, _, _) = add_numeric(&spo2, "SpO₂", "%", 0, 100, params.borrow().spo2,
|
||||
params.clone(), |p, v| p.spo2 = v);
|
||||
alarm_editors.push((add_alarm_limits(&spo2_row, "", "", 100,
|
||||
params.borrow().alarms.spo2, params.clone(), |p, v| p.alarms.spo2 = v,
|
||||
@@ -181,7 +190,7 @@ fn create_admin(app: &Application, params: SharedParams,
|
||||
move |button| params.borrow_mut().spo2_sensor_attached = !button.is_active()
|
||||
});
|
||||
spo2.append(&sensor);
|
||||
- add_dropdown_at(&spo2, "Signal quality", &[
|
||||
+ let _ = add_dropdown_at(&spo2, "Signal quality", &[
|
||||
"0 — None (no reading)",
|
||||
"1 — Poor (*)",
|
||||
"2 — Adequate (**)",
|
||||
@@ -209,6 +218,18 @@ fn create_admin(app: &Application, params: SharedParams,
|
||||
_ => PulseConduction::OneToOne,
|
||||
};
|
||||
});
|
||||
+ let dropout_hint = gtk::Label::new(Some(
|
||||
+ "Poor signal: randomly loses all SpO₂/PR readings for 5–20 s,"
|
||||
+ ));
|
||||
+ dropout_hint.set_xalign(0.0);
|
||||
+ dropout_hint.set_wrap(true);
|
||||
+ spo2.append(&dropout_hint);
|
||||
+ let dropout_hint2 = gtk::Label::new(Some(
|
||||
+ "with 30–180 s of usable poor-quality signal between interruptions."
|
||||
+ ));
|
||||
+ dropout_hint2.set_xalign(0.0);
|
||||
+ dropout_hint2.set_wrap(true);
|
||||
+ spo2.append(&dropout_hint2);
|
||||
let conduction_hint = gtk::Label::new(Some(
|
||||
"Pulse rate is derived from captured QRS events.
|
||||
ART and pleth are delayed. VT can be perfusing; PVT, VF and asystole are pulseless."
|
||||
@@ -219,7 +240,7 @@ ART and pleth are delayed. VT can be perfusing; PVT, VF and asystole are pulsele
|
||||
|
||||
let art = group(&controls, "Invasive arterial blood pressure");
|
||||
add_visibility(&art, params.borrow().show_art, params.clone(), |p, show| p.show_art = show);
|
||||
- let art_sys_row = add_numeric(&art, "Systolic", "mmHg", 0, 200, params.borrow().art_systolic,
|
||||
+ let (art_sys_row, _, _) = add_numeric(&art, "Systolic", "mmHg", 0, 200, params.borrow().art_systolic,
|
||||
params.clone(), |p, v| p.art_systolic = v);
|
||||
alarm_editors.push((add_alarm_limits(&art_sys_row, "", "", 200,
|
||||
params.borrow().alarms.art_sys, params.clone(), |p, v| p.alarms.art_sys = v,
|
||||
@@ -227,12 +248,12 @@ ART and pleth are delayed. VT can be perfusing; PVT, VF and asystole are pulsele
|
||||
alarm_editors.push((add_alarm_limits(&art, "MAP (derived)", "mmHg", 200,
|
||||
params.borrow().alarms.art_map, params.clone(), |p, v| p.alarms.art_map = v,
|
||||
&preset_dropdown, &updating_preset), |s| s.art_map));
|
||||
- let art_dia_row = add_numeric(&art, "Diastolic", "mmHg", 0, 200, params.borrow().art_diastolic,
|
||||
+ let (art_dia_row, _, _) = add_numeric(&art, "Diastolic", "mmHg", 0, 200, params.borrow().art_diastolic,
|
||||
params.clone(), |p, v| p.art_diastolic = v);
|
||||
alarm_editors.push((add_alarm_limits(&art_dia_row, "", "", 200,
|
||||
params.borrow().alarms.art_dia, params.clone(), |p, v| p.alarms.art_dia = v,
|
||||
&preset_dropdown, &updating_preset), |s| s.art_dia));
|
||||
- let ppv_row = add_numeric(&art, "Pulse pressure variation", "%", 0, 60,
|
||||
+ let (ppv_row, _, _) = add_numeric(&art, "Pulse pressure variation", "%", 0, 60,
|
||||
params.borrow().pulse_pressure_variation, params.clone(),
|
||||
|p, v| p.pulse_pressure_variation = v);
|
||||
alarm_editors.push((add_alarm_limits(&ppv_row, "", "", 60,
|
||||
@@ -256,7 +277,7 @@ ART and pleth are delayed. VT can be perfusing; PVT, VF and asystole are pulsele
|
||||
|
||||
let nibp = group(&controls, "Non-invasive blood pressure (NIBP)");
|
||||
add_visibility(&nibp, params.borrow().show_nibp, params.clone(), |p, show| p.show_nibp = show);
|
||||
- let nibp_sys_row = add_numeric(&nibp, "Systolic", "mmHg", 0, 260, params.borrow().nibp_systolic,
|
||||
+ let (nibp_sys_row, _, _) = add_numeric(&nibp, "Systolic", "mmHg", 0, 260, params.borrow().nibp_systolic,
|
||||
params.clone(), |p, v| p.nibp_systolic = v);
|
||||
alarm_editors.push((add_alarm_limits(&nibp_sys_row, "", "", 260,
|
||||
params.borrow().alarms.nibp_sys, params.clone(), |p, v| p.alarms.nibp_sys = v,
|
||||
@@ -264,7 +285,7 @@ ART and pleth are delayed. VT can be perfusing; PVT, VF and asystole are pulsele
|
||||
alarm_editors.push((add_alarm_limits(&nibp, "MAP (derived)", "mmHg", 260,
|
||||
params.borrow().alarms.nibp_map, params.clone(), |p, v| p.alarms.nibp_map = v,
|
||||
&preset_dropdown, &updating_preset), |s| s.nibp_map));
|
||||
- let nibp_dia_row = add_numeric(&nibp, "Diastolic", "mmHg", 0, 200, params.borrow().nibp_diastolic,
|
||||
+ let (nibp_dia_row, _, _) = add_numeric(&nibp, "Diastolic", "mmHg", 0, 200, params.borrow().nibp_diastolic,
|
||||
params.clone(), |p, v| p.nibp_diastolic = v);
|
||||
alarm_editors.push((add_alarm_limits(&nibp_dia_row, "", "", 200,
|
||||
params.borrow().alarms.nibp_dia, params.clone(), |p, v| p.alarms.nibp_dia = v,
|
||||
@@ -277,12 +298,12 @@ ART and pleth are delayed. VT can be perfusing; PVT, VF and asystole are pulsele
|
||||
p.capno_shape = if selected == 1 { CapnoShape::Obstructive }
|
||||
else { CapnoShape::Normal };
|
||||
});
|
||||
- let etco2_row = add_numeric(&capno, "EtCO₂", "mmHg", 0, 100, params.borrow().etco2,
|
||||
+ let (etco2_row, _, _) = add_numeric(&capno, "EtCO₂", "mmHg", 0, 100, params.borrow().etco2,
|
||||
params.clone(), |p, v| p.etco2 = v);
|
||||
alarm_editors.push((add_alarm_limits(&etco2_row, "", "", 100,
|
||||
params.borrow().alarms.etco2, params.clone(), |p, v| p.alarms.etco2 = v,
|
||||
&preset_dropdown, &updating_preset), |s| s.etco2));
|
||||
- let rr_row = add_numeric(&capno, "Respiratory rate", "/min", 0, 60, params.borrow().respiratory_rate,
|
||||
+ let (rr_row, _, _) = add_numeric(&capno, "Respiratory rate", "/min", 0, 60, params.borrow().respiratory_rate,
|
||||
params.clone(), |p, v| p.respiratory_rate = v);
|
||||
alarm_editors.push((add_alarm_limits(&rr_row, "", "", 60,
|
||||
params.borrow().alarms.rr, params.clone(), |p, v| p.alarms.rr = v,
|
||||
@@ -355,11 +376,11 @@ where F: Fn(&mut Parameters, bool) + 'static {
|
||||
fn add_dropdown<F>(parent: >k::Box, label: &str, items: &[&str],
|
||||
params: SharedParams, setter: F)
|
||||
where F: Fn(&mut Parameters, u32) + 'static {
|
||||
- add_dropdown_at(parent, label, items, 0, params, setter);
|
||||
+ let _ = add_dropdown_at(parent, label, items, 0, params, setter);
|
||||
}
|
||||
|
||||
fn add_dropdown_at<F>(parent: >k::Box, label: &str, items: &[&str],
|
||||
- initial: u32, params: SharedParams, setter: F)
|
||||
+ initial: u32, params: SharedParams, setter: F) -> gtk::DropDown
|
||||
where F: Fn(&mut Parameters, u32) + 'static {
|
||||
let row = gtk::Box::new(Orientation::Horizontal, 8);
|
||||
let caption = gtk::Label::new(Some(label));
|
||||
@@ -374,12 +395,13 @@ where F: Fn(&mut Parameters, u32) + 'static {
|
||||
row.append(&caption);
|
||||
row.append(&combo);
|
||||
parent.append(&row);
|
||||
+ combo
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn add_numeric<F>(parent: >k::Box, label: &str, unit: &str,
|
||||
min: i32, max: i32, initial: i32,
|
||||
- params: SharedParams, setter: F) -> gtk::Box
|
||||
+ params: SharedParams, setter: F) -> (gtk::Box, gtk::Scale, gtk::Entry)
|
||||
where F: Fn(&mut Parameters, i32) + 'static {
|
||||
let row = gtk::Box::new(Orientation::Horizontal, 8);
|
||||
let caption = gtk::Label::new(Some(label));
|
||||
@@ -434,7 +456,7 @@ where F: Fn(&mut Parameters, i32) + 'static {
|
||||
row.append(&entry);
|
||||
row.append(&suffix);
|
||||
parent.append(&row);
|
||||
- row
|
||||
+ (row, slider, entry)
|
||||
}
|
||||
|
||||
fn normalize_entry(entry: >k::Entry, slider: >k::Scale, min: i32, max: i32) {
|
||||
Loading…
Reference in a new issue