codex/equal-expiratory-volumes-different-flows.py

equal-expiratory-volumes-different-flows.py
#!/usr/bin/env python3
"""Original zero-integral flow perturbation; write PNG to cwd."""
import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt


def main():
    t = np.linspace(0, 1, 1001)
    q0 = np.ones_like(t)
    q1 = q0 + .5 * np.sin(2 * np.pi * t)
    v0 = t
    v1 = t + .5 / (2 * np.pi) * (1 - np.cos(2 * np.pi * t))
    fig, axes = plt.subplots(1, 2, figsize=(8, 3.5), dpi=120, facecolor="white")
    axes[0].plot(t, q0, color="#2367a1", label="Constant flow")
    axes[0].plot(t, q1, color="#b85224", label="Modified flow")
    axes[0].fill_between(t, q0, q1, where=q1 >= q0, color="#24845e", alpha=.15)
    axes[0].fill_between(t, q0, q1, where=q1 <= q0, color="#b85224", alpha=.15)
    axes[0].set(xlabel="Time (s)", ylabel="Outward flow (L/s)", ylim=(.35, 1.65))
    axes[0].legend(loc="lower left", fontsize=8)
    axes[1].plot(t, v0, color="#2367a1")
    axes[1].plot(t, v1, color="#b85224")
    axes[1].scatter([1], [1], color="#3c3c3c", s=24, zorder=5)
    axes[1].annotate("Both end at 1 L", (1, 1), xytext=(.35, .98), fontsize=9,
                     arrowprops={"arrowstyle": "-", "color": ".4"})
    axes[1].set(xlabel="Time (s)", ylabel="Cumulative expelled volume (L)", ylim=(0, 1.1))
    for ax in axes:
        ax.set_xlim(0, 1.03)
        ax.grid(alpha=.18)
        ax.set_facecolor("white")
    fig.suptitle("Different flows, identical first-second volume", fontsize=13)
    fig.text(.5, .015, "Constructed waveforms; not patient traces", ha="center", fontsize=9)
    fig.tight_layout(rect=(0, .055, 1, .96))
    fig.savefig("equal-expiratory-volumes-different-flows.png", dpi=120, facecolor="white", transparent=False)
    plt.close(fig)


if __name__ == "__main__":
    main()