codex/microdissection-purity-and-cell-state.py

microdissection-purity-and-cell-state.py
"""Draw microdissection-purity-and-cell-state.png in CWD on opaque white.

Original two-population mixture illustration, not experimental data.
Tested with Python 3.14.4, NumPy 2.3.5, Matplotlib 3.10.7.
"""
from pathlib import Path
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np


def main():
    p = np.linspace(0, 1, 201)
    background = 10
    fig, ax = plt.subplots(figsize=(7.2, 4.8), dpi=100)
    fig.patch.set_facecolor('white')
    ax.set_facecolor('white')
    fig.subplots_adjust(left=.12, right=.97, bottom=.19, top=.72)
    ax.plot(p, p*100 + (1-p)*background, color='#1768ac', lw=2.3,
            label='Target signal a = 100; background b = 10')
    ax.plot(p, p*60 + (1-p)*background, color='#b66316', lw=2.3,
            label='Target signal a = 60; background b = 10')
    ax.scatter([.2], [28], color='#273849', s=42, zorder=5)
    ax.annotate('Initial sample\np = 0.20, y = 28', (.2, 28), xytext=(.04, 56),
                arrowprops={'arrowstyle': '-', 'color': '#273849'}, fontsize=10)
    ax.scatter([.8, .8], [82, 50], color=['#1768ac', '#b66316'], s=42, zorder=5)
    ax.annotate('Purity change only\np = 0.80, y = 82', (.8, 82), xytext=(.50, 97),
                arrowprops={'arrowstyle': '-', 'color': '#1768ac'}, fontsize=10,
                color='#1768ac')
    ax.annotate('Purity and target change\np = 0.80, y = 50', (.8, 50), xytext=(.44, 15),
                arrowprops={'arrowstyle': '-', 'color': '#b66316'}, fontsize=10,
                color='#b66316')
    ax.set(xlim=(0, 1), ylim=(0, 110), xlabel='Target-cell fraction p',
           ylabel='Mean signal per starting cell y')
    ax.spines[['top', 'right']].set_visible(False)
    ax.grid(alpha=.15)
    fig.suptitle('More target cells can raise a mixture signal\neven when their per-cell signal falls',
                 fontsize=14, y=.98)
    fig.legend(loc='upper left', bbox_to_anchor=(.12, .85), fontsize=9,
               frameon=False, borderaxespad=0)
    fig.text(.5, .055, r'Original model: $y=pa+(1-p)b$. Equal per-cell weighting; no recovery losses.',
             ha='center', fontsize=10)
    fig.savefig(Path.cwd() / 'microdissection-purity-and-cell-state.png',
                transparent=False, facecolor='white')
    plt.close(fig)


if __name__ == '__main__':
    main()