codex/ventricular-system-connections.py

ventricular-system-connections.py
"""Draw a schematic connection map, rather than a spatial brain reconstruction."""
from pathlib import Path

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from matplotlib.patches import FancyBboxPatch


def main():
    fig, ax = plt.subplots(figsize=(8.5, 6.4), dpi=100, facecolor="white")
    fig.subplots_adjust(left=0.03, right=0.97, bottom=0.15, top=0.87)
    fig.suptitle("Connections of the ventricular system", y=0.97,
                 fontsize=16, weight="bold", color="#172330")
    positions = {}
    entries = [
        ("left", 2.25, 9.0, 3.4, "Left lateral ventricle"),
        ("right", 7.75, 9.0, 3.4, "Right lateral ventricle"),
        ("left_foramen", 2.25, 7.45, 3.4, "Left interventricular\nforamen"),
        ("right_foramen", 7.75, 7.45, 3.4, "Right interventricular\nforamen"),
        ("third", 5.0, 5.9, 4.5, "Third ventricle\nwithin the diencephalon"),
        ("aqueduct", 5.0, 4.35, 4.5, "Cerebral aqueduct"),
        ("fourth", 5.0, 2.8, 4.5, "Fourth ventricle"),
        ("subarachnoid", 5.0, 1.25, 4.5, "Subarachnoid spaces"),
    ]
    half_height = 0.43
    for key, x, y, width, text in entries:
        positions[key] = (x, y)
        color = "#e5eff6" if "foramen" not in key and key != "aqueduct" else "#edf5ef"
        ax.add_patch(FancyBboxPatch((x - width / 2, y - half_height),
                                    width, 2 * half_height,
                                    boxstyle="round,pad=0.03,rounding_size=0.12",
                                    facecolor=color, edgecolor="#607e91", linewidth=1.2))
        ax.text(x, y, text, ha="center", va="center", fontsize=11, color="#172330")
    for source, target in [
        ("left", "left_foramen"), ("right", "right_foramen"),
        ("left_foramen", "third"), ("right_foramen", "third"),
        ("third", "aqueduct"), ("aqueduct", "fourth"), ("fourth", "subarachnoid"),
    ]:
        sx, sy = positions[source]
        tx, ty = positions[target]
        if target == "third":
            tx += -1.25 if source == "left_foramen" else 1.25
        ax.annotate("", xy=(tx, ty + half_height + 0.05),
                    xytext=(sx, sy - half_height - 0.05),
                    arrowprops={"arrowstyle": "->", "linewidth": 1.5,
                                "color": "#4c677a"})
    ax.text(7.5, 2.04, "median and lateral apertures", fontsize=9,
            color="#45535d", va="center")
    ax.set_xlim(0, 10.5)
    ax.set_ylim(0.4, 9.75)
    ax.axis("off")
    fig.text(0.5, 0.09, "Schematic connections; arrows follow the conventional passage sequence.",
             ha="center", fontsize=10, color="#45535d")
    fig.text(0.5, 0.04, "CSF movement is pulsatile and can form local recirculating patterns.",
             ha="center", fontsize=10, color="#45535d")
    fig.savefig(Path.cwd() / (Path(__file__).stem + ".png"), dpi=100,
                facecolor="white", transparent=False)
    plt.close(fig)


if __name__ == "__main__":
    main()