codexend-inspiratory-pressure-relaxation.py
end-inspiratory-pressure-relaxation.py
"""Original illustrative hold trace; run from _media with Python 3.14.
Uses the repository's pinned NumPy/Matplotlib environment. Values are model
parameters, not patient measurements or recommended ventilator settings.
"""
from pathlib import Path
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np
def hold_pressure(seconds):
return 18.0 + 4.0 * np.exp(-np.asarray(seconds) / 0.5)
def main():
assert np.isclose(hold_pressure(0), 22)
assert np.isclose(hold_pressure(0.5), 19.47151776468577)
assert np.isclose(hold_pressure(1.5), 18.199148273471455)
t = np.linspace(0, 2.5, 501)
fig, ax = plt.subplots(figsize=(8, 4.8), dpi=100, facecolor="white")
ax.set_facecolor("white")
ax.plot([-0.65, 0], [26, 26], color="#235b83", lw=2.5)
ax.plot([0, 0], [26, 22], color="#235b83", lw=2.5)
ax.plot(t, hold_pressure(t), color="#235b83", lw=2.5,
label=r"Hold model: $18+4e^{-t/0.5}$")
ax.axhline(18, color="#248256", ls="--", lw=1.6,
label="Long-time model pressure: 18")
ax.axhline(8, color="#767676", ls=":", lw=1.6,
label="Total end-expiratory pressure: 8")
ax.axvline(0, color="#aaaaaa", lw=0.9, zorder=0)
ax.annotate("Flow stops", (0, 26), (-0.48, 28.2),
arrowprops={"arrowstyle": "->", "color": "#333333"}, fontsize=10)
ax.annotate("Immediate drop: 4", (0.01, 24), (0.34, 25.6),
arrowprops={"arrowstyle": "->", "color": "#333333"}, fontsize=10)
for x, label, offset in [(0.5, "0.5 s: 19.47", (12, 20)),
(1.5, "1.5 s: 18.20", (0, 22))]:
y = float(hold_pressure(x))
ax.scatter([x], [y], color="#235b83", s=28, zorder=4)
ax.annotate(label, (x, y), xytext=offset, textcoords="offset points",
fontsize=10, ha="left")
ax.set(xlim=(-0.7, 2.6), ylim=(6, 30),
xlabel="Time after flow interruption (s)",
ylabel=r"Airway pressure (cm H$_2$O)",
title="An end-inspiratory hold: interruption versus relaxation")
ax.grid(alpha=0.18)
ax.legend(loc="lower right", fontsize=9, framealpha=1)
fig.text(0.5, 0.025, "Constructed passive model; not patient data or a prescribed hold duration.",
ha="center", fontsize=9, color="#444444")
fig.tight_layout(rect=(0, 0.07, 1, 1))
fig.savefig(Path("end-inspiratory-pressure-relaxation.png"), facecolor="white", dpi=100)
plt.close(fig)
if __name__ == "__main__":
main()