codex/dual-range-optical-power-measurement.py

dual-range-optical-power-measurement.py
"""Illustrative calibrated gain ranges; write the PNG to the working directory."""
from pathlib import Path
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np


def main():
    noise, criterion, ceiling = 0.5e-3, 5, 1.0
    gains = np.array([1e5, 1e3])
    lower, upper = criterion * noise / gains, ceiling / gains
    assert np.allclose(upper / lower, 400)
    assert lower[1] < upper[0]
    assert np.isclose(upper[1] / lower[0], 40000)
    fig, ax = plt.subplots(figsize=(9, 3.6), dpi=100, facecolor='white')
    fig.subplots_adjust(left=.23, right=.97, bottom=.24, top=.76)
    ax.set_xscale('log')
    ax.axvspan(lower[1], upper[0], color='#d5dce3', alpha=.75)
    colors = ['#1967a3', '#cc651e']
    for j, (lo, hi, color) in enumerate(zip(lower, upper, colors)):
        y = 2-j
        ax.plot([lo, hi], [y, y], lw=12, color=color, solid_capstyle='butt')
        ax.text(lo, y+.20, ['25 nW', '2.5 µW'][j], ha='center', fontsize=10)
        ax.text(hi, y+.20, ['10 µW', '1 mW'][j], ha='center', fontsize=10)
    ax.plot([lower[0], upper[1]], [0, 0], lw=7, color='#287544', solid_capstyle='butt')
    ax.text(np.sqrt(lower[0]*upper[1]), .16, 'Combined range: 40,000 : 1', ha='center', fontsize=10)
    ax.set_yticks([2, 1, 0], ['High sensitivity\n400 : 1', 'Low sensitivity\n400 : 1', 'Calibrated union'])
    ax.set_ylim(-.35, 2.7)
    ax.set_xlim(1e-8, 2e-3)
    ax.set_xticks([1e-8, 1e-7, 1e-6, 1e-5, 1e-4, 1e-3], ['10 nW', '100 nW', '1 µW', '10 µW', '100 µW', '1 mW'])
    ax.set_xlabel('Optical power (logarithmic scale)')
    ax.grid(axis='x', alpha=.2)
    ax.tick_params(axis='y', length=0, pad=12)
    for side in ['top', 'right', 'left']:
        ax.spines[side].set_visible(False)
    fig.suptitle('Two calibrated ranges extend usable optical power', fontsize=14, y=.97)
    fig.text(.5, .84, 'Illustrative model: 2.5 mV detection threshold and 1 V output ceiling', ha='center', fontsize=10)
    fig.text(.5, .035, 'Overlap: 2.5–10 µW; agreement between settings must be established by calibration.', ha='center', fontsize=10)
    fig.savefig(Path.cwd() / 'dual-range-optical-power-measurement.png', dpi=100, facecolor='white')
    plt.close(fig)


if __name__ == '__main__':
    main()