Turn any AI agent into an AI Scientist. The #1 Agent Skills library for science, used by 250,000+ scientists worldwide. 177 ready-to-use validated skills plus 100+ scientific databases covering biology, chemistry, medicine, and drug discovery. Compatible with Cursor, Claude Code, Codex, Pi, Antigravity, and the open Agent Skills standard.
Python agent-skillsai-scientistbioinformaticschemoinformaticsclaudeclaude-skillsclaudecodeclinical-researchcomputational-biologydata-analysisdrug-discoverygenomicsmaterials-sciencemetabolomicsproteomicsscientific-computingscientific-visualization
Add uncertainty-and-units skill under skills/uncertainty-and-units
Timothy Kassiscca1e05ddff5
diff --git a/skills/uncertainty-and-units/SKILL.md b/skills/uncertainty-and-units/SKILL.mdnew file mode 100644index 00000000..d4ec3eff--- /dev/null+++ b/skills/uncertainty-and-units/SKILL.md@@ -0,0 +1,384 @@+---+name: uncertainty-and-units+description: Track physical units and propagate measurement uncertainty in scientific calculations using pint and uncertainties. Use for unit conversion and dimensional checking, GUM uncertainty budgets, Type A and Type B evaluation, coverage factors and expanded uncertainty, Monte Carlo propagation, significant-figure and plus-minus reporting, error propagation through curve fits, CODATA constants, auditing Python code for stripped units or broken uncertainty propagation, and order-of-magnitude plausibility checks using dimensionless groups (Reynolds, Peclet, Damkohler, Knudsen, Biot, Womersley), characteristic scales such as diffusion time or Debye length, and observed magnitude ranges. Trigger on "is this number physically reasonable", "sanity check these units", "what regime is this flow in", or a result that looks off by orders of magnitude.+license: MIT+compatibility: Requires Python 3.12+. The numeric CLIs need pint, uncertainties, NumPy, and SciPy; the static auditor is standard-library only. All bundled tooling runs locally with no network access.+allowed-tools: Read Write Edit Bash+metadata:+ version: "1.0"+ skill-author: K-Dense Inc.+---++# Uncertainty and units++## Scope++Use this skill whenever a calculation carries physical units or a reported number needs+an uncertainty. Concretely:++- converting between units, including conversions that need a physical context+ (wavelength to photon energy, mass to amount of substance, energy to temperature);+- propagating uncertainty through a measurement model, with or without correlated inputs;+- building a GUM uncertainty budget from calibration certificates, specifications, and+ repeatability data;+- choosing a coverage factor and deciding whether `k = 2` is defensible;+- rounding and writing a result so a reader knows what the `±` means;+- extracting parameter uncertainties from a curve fit without discarding correlations;+- reviewing existing analysis code for silent unit and uncertainty defects;+- checking that a dimensionally consistent answer is also physically possible — the+ order of magnitude, the dimensionless group, and the regime it implies.++This skill covers the metrology and the two libraries that implement it. It does not+cover statistical inference, model selection, or study design — see `statistical-analysis`,+`statistical-power`, and `experimental-design`.++## Current release and installation++Verified 2026-07-26:++- **pint 0.25.3**, released 2026-03-19; requires Python 3.11+.+- **uncertainties 3.2.3**, released 2025-04-21; requires Python 3.8+.+- **NumPy 2.5.1** and **SciPy 1.18.0**; both require Python 3.12+.+- `scipy.constants` in SciPy 1.18.0 serves **CODATA 2022**. SciPy 1.11 and earlier+ served CODATA 2018, and several recommended values differ between them.++```bash+uv venv --python 3.13+source .venv/bin/activate+uv pip install "pint==0.25.3" "uncertainties==3.2.3" "numpy==2.5.1" "scipy==1.18.0"+```++`pint-pandas` and `pint-xarray` add unit-aware columns and arrays and are separate+installs.++## Non-negotiable workflow++1. **Attach units at input and strip them only at output.** Convert at function+ boundaries with `ureg.wraps` or `m_as("unit")`, never mid-calculation.+2. **Write the measurement model explicitly** before computing anything, including+ corrections whose estimated value is zero. A correction left out of the model leaves+ its uncertainty out of the budget.+3. **Give every input four things**: an estimate, a standard uncertainty, the+ distribution the uncertainty came from, and its degrees of freedom.+4. **Convert Type B statements with the right divisor.** A certificate's expanded+ uncertainty divides by its stated `k`; rectangular limits divide by `sqrt(3)`.+5. **Identify correlations before combining.** Inputs calibrated against the same+ standard, measured on the same instrument, or drawn from the same fit are correlated.+6. **Compute sensitivity coefficients**, and read the budget from `c_i * u(x_i)` rather+ than from the raw uncertainties.+7. **Check the linearization.** Run Monte Carlo alongside the GUM framework and apply+ the JCGM 101 clause 8 comparison. Report the Monte Carlo result when it fails.+8. **Choose `k` from the effective degrees of freedom**, not by habit.+9. **Round the uncertainty first, then the value to the same decimal place.**+10. **State what the `±` is** — standard or expanded, with `k`, the coverage probability,+ and the method.+11. **Sanity-check the magnitude before reporting.** A dimensionally consistent result can+ still be impossible. Compare it against a known scale or a dimensionless group, and+ confirm every assumption you relied on still holds in that regime.++## The failures this skill exists to prevent++Each of the following runs without error and produces a plausible number.++### A unit stripped at an unknown scale++```python+length = (12.7 * ureg.mm).magnitude # 12.7 -- of what?+length = (12.7 * ureg.mm).m_as("m") # 0.0127 metres, stated+```++`.magnitude` returns whatever the quantity happened to be carrying. Name the unit at the+point of extraction, every time.++### Offset temperature arithmetic++```python+Q(20, "degC") + Q(5, "degC") # OffsetUnitCalculusError -- correctly refused+Q(20, "degC") + Q(5, "delta_degC") # 25 degree_Celsius+Q(25, "degC") - Q(20, "degC") # 5 delta_degree_Celsius+```++Celsius and Fahrenheit are interval scales. An uncertainty on a temperature is always a+difference and belongs in a `delta_` unit: converting `20 ± 0.5 degC` to Fahrenheit+gives `68 degF ± 0.9 delta_degF`, two different conversions on one line.++### Logarithmic units that add by multiplying++```python+Q(10, "dBm") + Q(10, "dBm") # 0.0001 kilogram**2 * meter**4 / second**6+```++That is 10 mW × 10 mW, not 20 mW and not 13 dBm. Nothing raises. Convert to a linear+unit before any arithmetic.++### A correlation destroyed by a round trip++```python+x = ufloat(1.0, 0.1)+x - x # 0.0+/-0+x - ufloat(x.nominal_value, x.std_dev) # 0.00+/-0.14+```++Rebuilding a variable from its nominal value and standard deviation creates an+independent variable. So does any serialization that passes through a pair of floats.+Use `correlated_values(values, covariance_matrix)` to rebuild a correlated set.++### A covariance matrix silently rescaled++```python+popt, pcov = curve_fit(f, x, y, sigma=sigma) # default+popt, pcov = curve_fit(f, x, y, sigma=sigma, absolute_sigma=True)+```++The default rescales `pcov` by the reduced chi-square, so the parameter uncertainties+absorb the goodness of fit and match what you would get by passing no `sigma` at all. On+one synthetic straight-line fit the two give `[0.0364, 0.2154]` and `[0.0477, 0.2820]` —+a 31% difference. Pass `absolute_sigma=True` whenever `sigma` holds real standard+uncertainties.++### A linearization that was never checked++For `y = x²` with `x = 1.0 ± 0.5`, the GUM framework gives `y = 1.0`, `u_c = 1.0`, and a+95% interval of `[-0.96, 2.96]` — mostly negative, for a squared quantity. Monte Carlo+gives a mean of 1.25, `u_c = 1.06`, and a shortest 95% interval of `[0, 3.32]`. Nothing+in a linear-propagation library will tell you this happened.++## Bundled local CLIs++All helpers run offline, reject URLs and symlinks, bound their inputs, write output+atomically with private permissions, and refuse to overwrite without `--force`.++```bash+python skills/uncertainty-and-units/scripts/propagate_uncertainty.py --help+python skills/uncertainty-and-units/scripts/uncertainty_budget.py --help+python skills/uncertainty-and-units/scripts/format_result.py --help+python skills/uncertainty-and-units/scripts/convert_units.py --help+python skills/uncertainty-and-units/scripts/audit_units.py --help+python skills/uncertainty-and-units/scripts/check_plausibility.py --help+```++### propagate_uncertainty.py++Runs both propagation methods on the same model and applies the JCGM 101 clause 8+validation test.++```bash+python skills/uncertainty-and-units/scripts/propagate_uncertainty.py \+ --expression "m / (pi * (d / 2) ** 2 * h)" \+ --variable "m=250.0,0.05" \+ --variable "d=20.0,0.02,rectangular" \+ --variable "h=40.0,0.05,rectangular" \+ --measurand density --unit "g/cm3" --format markdown+```++Each `--variable` is `name=value,standard_uncertainty[,distribution[,dof]]`, where the+distribution is `normal`, `rectangular`, `triangular`, `arcsine`, or `exact` and controls+Monte Carlo sampling only. Correlations go in as `--correlation "a,b=0.9"`. A JSON+`--spec` file holds the same model for anything long-lived.++The expression is parsed into an abstract syntax tree and reduced by an explicit walk+over `+ - * / **` and a fixed list of functions. It is never compiled or executed.++The report gives the estimate, `u_c`, sensitivity coefficients, the budget in percent,+effective degrees of freedom, `k`, `U`, both Monte Carlo coverage intervals, and the+verdict on whether the linearized result may be reported.++### uncertainty_budget.py++Combines components stated the way certificates and data sheets state them.++```bash+python skills/uncertainty-and-units/scripts/uncertainty_budget.py --template > budget.json+python skills/uncertainty-and-units/scripts/uncertainty_budget.py --spec budget.json --format markdown+```++Each component names a `distribution` that fixes its divisor — `expanded` divides by its+`coverage_factor`, `rectangular` by `sqrt(3)`, `triangular` by `sqrt(6)`, `arcsine` by+`sqrt(2)`, `normal` by 1 — with an optional `sensitivity`, `dof`, and `relative: true`.+The tool computes `u_c`, the Welch-Satterthwaite effective degrees of freedom, `k` from+the t-distribution, and `U`, and warns when a Type A component has no degrees of+freedom, when `nu_eff` is small enough that `k = 2` is wrong, when one component+dominates, and when a Type B component declared `normal` is probably an undivided+expanded uncertainty.++### format_result.py++```bash+python skills/uncertainty-and-units/scripts/format_result.py \+ --value 12.34567 --uncertainty 0.02345 --unit mm \+ --coverage-factor 2.26 --coverage-probability 0.95+```++Returns `12.346 ± 0.023 mm`, `12.346(23) mm`, the scientific and LaTeX forms, and the+sentence that has to accompany the number. Warns when one significant digit is requested+for an uncertainty beginning in 1 or 2, and when the uncertainty exceeds the estimate.++### convert_units.py++```bash+python skills/uncertainty-and-units/scripts/convert_units.py \+ --value 532 --unit nm --to eV --context spectroscopy --uncertainty 0.5++python skills/uncertainty-and-units/scripts/convert_units.py \+ --value 1.0 --unit g --to mol --context chemistry --context-parameter "mw=180.156 g/mol"+```++Carries the uncertainty through the conversion's local derivative, which matters because+context conversions are reciprocal rather than proportional. Names the context in the+error message when a conversion needs one, and flags offset and logarithmic units.+`--list-contexts` shows what the registry defines.++### audit_units.py++Static review of existing analysis code. Parses, never imports or runs.++```bash+python skills/uncertainty-and-units/scripts/audit_units.py \+ --input analysis.py --format markdown --fail-on medium+```++| Rule | Severity | Detects |+| --- | --- | --- |+| `UNIT001` | medium | a second `UnitRegistry` in one module — cross-registry `ValueError` |+| `UNIT002` | medium | offset temperature units with no `delta_` unit anywhere |+| `UNIT003` | high | `.magnitude` without a preceding `.to(...)` or `.m_as(...)` |+| `UNIT004` | medium | logarithmic units, whose `+` multiplies |+| `UNC001` | high | `curve_fit` without `absolute_sigma` |+| `UNC002` | medium | `np.std` / `np.var` without `ddof` |+| `UNC003` | medium | `math` or `numpy` functions in a module that uses `uncertainties` |+| `UNC004` | high | a `ufloat` rebuilt from `.nominal_value` and `.std_dev` |+| `CONST001` | low | a literal within 0.1% of a CODATA constant |++Exit status is 1 when a finding meets `--fail-on` (default `high`), which makes it usable+as a pre-commit or CI check.++The rules are heuristics, so a false positive is suppressed with a directive comment —+trailing to cover its own line, or alone on a line to cover the next one:++```python+value = quantity.magnitude # audit-units: ignore UNIT003 -- already converted upstream++# audit-units: ignore UNC003 -- the argument here is a plain float array+scaled = np.log10(counts)+```++`# audit-units: ignore-file CONST001` covers a whole module, and naming no rule+suppresses all of them. Suppressions are counted in the report rather than hidden, so a+file that silences everything still says so.++### check_plausibility.py++Dimensional consistency is not physical possibility. A cell 2 m across and a Reynolds+number of 4e7 in a capillary both pass every unit check. This tool tests a set of+quantities against dimensionless groups, characteristic scales, and curated magnitude+bands, and verifies each formula's dimensionality before reporting a number.++```bash+python skills/uncertainty-and-units/scripts/check_plausibility.py \+ --quantity "density=1060 kg/m**3" --quantity "velocity=0.5 mm/s" \+ --quantity "length=8 um" --quantity "viscosity=3.5 mPa*s" \+ --group reynolds --format markdown+# Re = 0.001211 -- laminar (circular pipe, length = diameter)++python skills/uncertainty-and-units/scripts/check_plausibility.py \+ --quantity "diameter=2 m" --band "eukaryotic_cell_diameter=diameter"+# implausible: 4.3 decades outside the 5-100 um range+```++`--group` evaluates one of 14 dimensionless groups and names the regime it places the+system in; `--scale` computes a characteristic scale such as a diffusion time, Debye+length, or Stokes settling velocity; `--band` compares a supplied quantity against an+observed range. `--list` prints the whole catalogue with the inputs each formula needs.++Physical constants (`k_B`, `N_A`, `R_gas`, `g_earth`, and the rest) are available to every+formula without being supplied, and are read from `scipy.constants` at run time rather+than written as literals, so they track the CODATA release SciPy ships.++The dimensionality check is the point. Passing a kinematic viscosity where the formula+needs a dynamic one — both called "viscosity", both tabulated for water, differing by a+factor of ρ — is refused before any number is computed:++```+error: viscosity must have dimensionality [mass] / ([length] * [time]),+ but m²/s is [length] ** 2 / [time]+```++Exit status is 1 when the verdict meets `--fail-on` (default `implausible`; a value+within one decade of a band is `questionable`). The thresholds are conventions with soft+edges and assume the geometry their correlation was fitted for — see+`references/plausibility-scales.md` for the characteristic length to use in each case.++## Choosing a propagation method++| Situation | Method |+| --- | --- |+| Linear or near-linear model, normal-ish inputs, large dof | GUM framework alone |+| Any nonlinearity across ±2u of an input | run both, apply the clause 8 test |+| Relative uncertainty above ~20% on any input | Monte Carlo |+| Dominant rectangular or otherwise non-normal component | Monte Carlo |+| Output bounded below (variance, concentration, squared quantity) | Monte Carlo |+| Asymmetric output distribution | Monte Carlo, shortest coverage interval |+| Correlated inputs | either, but supply the covariance matrix, not the standard uncertainties alone |++A model dominated by rectangular contributions fails the clause 8 test even when it is+perfectly linear: the framework's `k = 1.96` over-covers a nearly trapezoidal output.+The estimate and `u_c` are still right; only the interval is too wide.++## Constants++Never type a constant from memory. The 2019 SI redefinition fixed `c`, `h`, `e`, `k`,+and `N_A` exactly, so their relative standard uncertainty is zero; everything else is a+measured value that moves between CODATA releases.++```python+import scipy.constants as constants++constants.value("electron mass") # 9.1093837139e-31+constants.unit("electron mass") # kg+constants.precision("electron mass") # 3.07e-10, relative standard uncertainty+constants.precision("Planck constant") # 0.0, exact by definition+```++`precision` returns a *relative* standard uncertainty; multiply by the value for the+absolute one.++## Reference files++- `references/gum-methodology.md` — Type A and Type B evaluation, distribution divisors,+ the law of propagation, Welch-Satterthwaite, when the framework fails, the Monte Carlo+ procedure, and the clause 8 validation test.+- `references/pint-recipes.md` — registries, offset and logarithmic units, contexts,+ boundary enforcement with `wraps` and `check`, NumPy interoperability, custom units,+ formatting.+- `references/uncertainties-recipes.md` — variable identity and correlation,+ `correlated_values`, `umath` and `unumpy`, format specs, fit covariance matrices, and+ the package's limits.+- `references/domain-conversions.md` — the energy ladder, spectroscopy, concentration,+ pressure, radiation and magnetism, mass spectrometry, logarithmic quantities, and the+ pairs that share dimensions without sharing meaning.+- `references/reporting-rules.md` — rounding, notations, the sentence that must+ accompany a result, SD versus SEM versus CI in figures, non-detects, and conformity+ decision rules.+- `references/plausibility-scales.md` — choosing the characteristic length, the+ dimensionless groups and the modelling assumption each one gates, characteristic+ scales, the observed magnitude bands and their sources, and the caveats on every+ threshold.++## Dated sources++Checked 2026-07-26:++- [JCGM 100:2008, Evaluation of measurement data — Guide to the expression of+ uncertainty in measurement](https://www.bipm.org/documents/20126/2071204/JCGM_100_2008_E.pdf)+- [JCGM 101:2008, Supplement 1 — Propagation of distributions using a Monte Carlo+ method](https://www.bipm.org/documents/20126/2071204/JCGM_101_2008_E.pdf)+- [NIST Technical Note 1297](https://nvlpubs.nist.gov/nistpubs/Legacy/TN/nbstechnicalnote1297.pdf)+- [CODATA internationally recommended values](https://physics.nist.gov/cuu/Constants/)+- [Pint on PyPI](https://pypi.org/project/Pint/) — 0.25.3, released 2026-03-19.+- [Pint documentation](https://pint.readthedocs.io/en/stable/), including+ [non-multiplicative units](https://pint.readthedocs.io/en/stable/user/nonmult.html)+ and [contexts](https://pint.readthedocs.io/en/stable/user/contexts.html).+- [uncertainties on PyPI](https://pypi.org/project/uncertainties/) — 3.2.3, released+ 2025-04-21.+- [uncertainties documentation](https://uncertainties.readthedocs.io/en/latest/)+- [scipy.constants reference](https://docs.scipy.org/doc/scipy/reference/constants.html)diff --git a/skills/uncertainty-and-units/references/domain-conversions.md b/skills/uncertainty-and-units/references/domain-conversions.mdnew file mode 100644index 00000000..209e32ad--- /dev/null+++ b/skills/uncertainty-and-units/references/domain-conversions.md@@ -0,0 +1,188 @@+# Domain conversions and dimensional blind spots++Values below were produced with pint 0.25.3 and SciPy 1.18.0 (CODATA 2022). Anything+marked *exact* is fixed by definition and carries zero uncertainty.++## Dimensional analysis does not catch these++Two quantities with the same dimensions convert freely, whether or not the conversion+means anything.++| Pair | Shared dimension | What pint does | Why it matters |+| --- | --- | --- | --- |+| gray and sievert | L²T⁻² | converts 1:1, silently | Sv includes a radiation weighting factor; the numbers coincide only for photons and electrons |+| newton-metre and joule | ML²T⁻² | converts 1:1, silently | torque is a vector product, energy a scalar; adding them is meaningless |+| hertz and becquerel | T⁻¹ | converts 1:1, silently | one is periodic, the other stochastic |+| radian and dimensionless | none | radians vanish | `sin(x)` needs radians; a degrees value that lost its unit is silently wrong |+| mol/L and mol/kg | different | raises | molarity and molality are genuinely different quantities |+| mg/L and ppm | different | raises | equal only for dilute aqueous solutions near 1 g/mL |++The last two raise because they *are* dimensionally distinct. The first four are the+dangerous ones: no tool will warn you.++## Energy ladder++Molecular science quotes the same energy in at least six units, three of which are+per-mole and therefore need the Avogadro constant.++| From | To | Factor |+| --- | --- | --- |+| 1 eV | kJ/mol | 96.48533212331002 |+| 1 hartree | eV | 27.21138624598103 |+| 1 hartree | kcal/mol | 627.5094740628942 |+| 1 cm⁻¹ | eV | 1.2398419843320026e-4 |+| 1 cm⁻¹ | K (as E/k_B) | 1.4387768775039336 |+| k_B T at 298.15 K | eV | 0.02569257912108585 |+| k_B T at 298.15 K | kJ/mol | 2.478957029602389 |+| 1 cal (thermochemical) | J | 4.184 (exact) |+| 1 cal_IT | J | 4.1868 (exact) |++Two traps. First, **per-mole and per-particle units are not dimensionally+interchangeable**: eV is an energy, kJ/mol is an energy per amount of substance, and the+conversion needs N_A. Pint will refuse `Q(1, "eV").to("kJ/mol")` and accept+`Q(1, "eV * N_A").to("kJ/mol")`. Second, **there are two calories** and a factor of+1.00067 between them; thermochemical is the default in chemistry, IT in engineering.++```python+Q(1, "eV * N_A").to("kJ/mol") # 96.48533212331002 kilojoule / mole+Q(1, "1/cm").to("eV", "sp") # 0.00012398419843320026 electron_volt+Q(298.15, "K").to("eV", "boltzmann") # 0.02569257912108585 electron_volt+```++## Spectroscopy++Wavelength, frequency, wavenumber, and photon energy are related by physics, not by+dimensional analysis, and the relations are *reciprocal* — an uncertainty does not+convert by the same factor as the value.++```python+Q(532, "nm").to("THz", "sp") # 563.5196578947367 terahertz+Q(532, "nm").to("1/cm", "sp") # 18796.992481203004 / centimeter+Q(532, "nm").to("eV", "sp") # 2.3305300457368467 electron_volt+```++Because E = hc/λ, a constant wavelength uncertainty becomes an energy uncertainty that+scales as 1/λ². Propagate through the relation, do not scale the uncertainty by the+value's conversion factor. `scripts/convert_units.py --uncertainty` does this with the+conversion's local derivative.++The `sp` context assumes vacuum unless given a refractive index: `n=1.33` for water+shifts a 532 nm frequency from 563.5 THz to 423.7 THz.++## Concentration++| Quantity | Unit | Depends on |+| --- | --- | --- |+| Molarity | mol/L | temperature, through solution volume |+| Molality | mol/kg solvent | nothing — preferred for thermodynamics |+| Mole fraction | dimensionless | nothing |+| Mass fraction, ppm(m/m) | dimensionless | nothing |+| Volume fraction, ppm(v/v) | dimensionless | temperature |+| Mass concentration | mg/L, g/L | temperature |++"ppm" alone is ambiguous: mass/mass, volume/volume, and mol/mol differ by the ratio of+densities or molar masses. In environmental water chemistry ppm conventionally means+mg/L, which equals mg/kg only because dilute water is close to 1 kg/L. In gas analysis+it conventionally means volume/volume. State which.++Pint treats `ppm` and `percent` as plain dimensionless scale factors (1e-6 and 0.01),+which is right for arithmetic and gives no protection against mixing the three senses.+Defining `ureg.define("ppm_v = 1e-6 = ppmv")` as a distinct unit does give protection.++Mass and amount of substance need the `chemistry` context and a molar mass:++```python+Q(1, "g").to("mol", "chemistry", mw=Q(180.156, "g/mol")) # 0.005550744909966918 mole+```++## Pressure++| From | To Pa | Note |+| --- | --- | --- |+| 1 atm | 101325 | exact |+| 1 bar | 100000 | exact |+| 1 torr | 133.32236842105263 | atm/760, exact by definition |+| 1 psi | 6894.7572931683635 | |+| 1 mmHg | 133.322387415 | *not* identical to torr, differs in the 8th digit |++**Gauge and absolute pressure are different quantities and no unit library models the+difference.** "psig" and "psia" have the same dimensions; a gauge reading needs the+ambient pressure added before it can be used in a gas law. Vacuum work, autoclave+protocols, and chromatography backpressures are where this bites.++## Radiation, magnetism, rotation++```python+Q(1, "Ci").to("Bq") # 37000000000.0 becquerel (exact by definition)+Q(1, "gauss").to("T", "Gaussian") # 9.999999999338245e-05 tesla+Q(1, "rpm").to("rad/s") # 0.10471975511965977 radian / second+```++Gauss fails *without* the Gaussian context: CGS electromagnetic units have different+dimensions from SI ones, not merely different scales. Magnetic field strength H (A/m,+oersted) and magnetic flux density B (T, gauss) are distinct quantities that literature+routinely calls "the field".++## Mass spectrometry++The unified atomic mass unit and the dalton are the same thing:+1 Da = 1.66053906892e-27 kg (CODATA 2022, relative standard uncertainty 3.1e-10).++m/z is conventionally reported as a dimensionless number: the ratio of mass in daltons+to charge number. The thomson (Th) exists but is not SI and is rarely used. Treating m/z+as a mass is wrong for any ion with z > 1, which is most of a protein spectrum.++## Logarithmic quantities++pH, pKa, dB, and magnitudes are logarithms of ratios. They do not add, average, or+propagate like ordinary quantities:++- the mean of pH 5 and pH 7 is not pH 6 — averaging requires converting to+ concentration, averaging, and converting back;+- a standard deviation in pH units is a *relative* standard deviation in concentration;+- adding two dB quantities multiplies the underlying linear quantities (see+ `pint-recipes.md`);+- decibel scales differ by reference: dBm references 1 mW, dBW references 1 W, dBV+ references 1 V, and dB alone references nothing until you say so.++## Temperature++Kelvin and rankine are ratio scales and behave normally. Celsius and Fahrenheit are+interval scales: 20 degC is not "twice" 10 degC, and their differences live in+`delta_degC` / `delta_degF`. See `pint-recipes.md` for what pint permits.++Absolute zero is exactly 273.15 K below 0 degC — `scipy.constants.zero_Celsius`.++## Constants: which values, and which uncertainties++The 2019 SI redefinition fixed several constants **exactly**, so their relative standard+uncertainty is zero and no future CODATA release will change them:++| Constant | Exact value |+| --- | --- |+| speed of light in vacuum, c | 299792458 m/s |+| Planck constant, h | 6.62607015e-34 J/Hz |+| elementary charge, e | 1.602176634e-19 C |+| Boltzmann constant, k | 1.380649e-23 J/K |+| Avogadro constant, N_A | 6.02214076e23 /mol |++Everything else is a measured recommended value that moves between CODATA releases —+electron mass, the gravitational constant, the fine-structure constant, the Rydberg+constant, and every derived quantity built from them.++```python+import scipy.constants as constants++constants.value("electron mass") # 9.1093837139e-31+constants.unit("electron mass") # kg+constants.precision("electron mass") # 3.07e-10 relative standard uncertainty+constants.precision("Planck constant") # 0.0 exact by definition+```++`scipy.constants` in SciPy 1.18.0 defaults to **CODATA 2022**; SciPy 1.11 and earlier+served CODATA 2018. Hard-coding a constant pins you to whichever release you copied it+from and discards its uncertainty entirely. `scripts/audit_units.py` flags literals that+match a known constant (`CONST001`).++Note also that `constants.precision` returns a *relative* standard uncertainty. The+absolute standard uncertainty is `value * precision`.diff --git a/skills/uncertainty-and-units/references/gum-methodology.md b/skills/uncertainty-and-units/references/gum-methodology.mdnew file mode 100644index 00000000..043293bf--- /dev/null+++ b/skills/uncertainty-and-units/references/gum-methodology.md@@ -0,0 +1,219 @@+# GUM methodology++The *Guide to the Expression of Uncertainty in Measurement* (JCGM 100:2008, "the GUM")+and its Supplement 1 (JCGM 101:2008, the Monte Carlo method) define how an uncertainty+is evaluated, combined, and reported. This file covers the parts that decide whether a+number is defensible.++## Vocabulary that has to stay straight++| Term | Symbol | Meaning |+| --- | --- | --- |+| Measurand | Y | the quantity intended to be measured |+| Estimate | y | the value obtained for it |+| Standard uncertainty | u(x) | uncertainty of an input, expressed as a standard deviation |+| Combined standard uncertainty | u_c(y) | standard uncertainty of the result |+| Expanded uncertainty | U | k * u_c(y) |+| Coverage factor | k | multiplier chosen for a stated coverage probability |+| Coverage probability | p | probability that the interval contains the measurand |++"Error" and "uncertainty" are not synonyms. An error is a single unknowable difference+from the true value; an uncertainty is a dispersion. "Accuracy" and "precision" are+qualitative words in the GUM's vocabulary and never carry a number.++## Type A and Type B are methods, not qualities++The distinction is only about *how the uncertainty was evaluated*. Neither is more+reliable than the other, and both produce a standard uncertainty on the same footing.++**Type A** — evaluated from a statistical analysis of repeated observations.++For n independent readings with experimental standard deviation s(q):++```text+u(q_bar) = s(q) / sqrt(n) degrees of freedom: nu = n - 1+```++The standard uncertainty of the *mean* is what enters the budget when the reported+value is a mean. Using s(q) itself overstates it by sqrt(n); using `numpy.std` without+`ddof=1` understates s(q) itself. Both mistakes are common and neither is visible in+the output.++Pooling repeatability across several runs raises the degrees of freedom and is worth+doing when the same instrument and procedure produced them.++**Type B** — evaluated by any other means: a calibration certificate, a manufacturer's+specification, a handbook value, a previous measurement, or documented judgement.++The stated quantity is converted to a standard uncertainty by dividing by a factor that+depends on what the statement means:++| What the source states | Assumed density | Divisor | u |+| --- | --- | --- | --- |+| Expanded uncertainty U with coverage factor k | normal | k | U / k |+| 95% confidence interval, no k given | normal | 1.96 | half-width / 1.96 |+| A standard uncertainty | normal | 1 | as stated |+| Limits ±a, any value equally likely | rectangular | sqrt(3) | a / sqrt(3) |+| Limits ±a, centre far more likely | triangular | sqrt(6) | a / sqrt(6) |+| Limits ±a, extremes more likely (sinusoidal drift, cyclic error) | arcsine | sqrt(2) | a / sqrt(2) |++Rectangular is the default when a specification gives limits and says nothing about the+distribution inside them. Digital resolution of one least significant digit d gives+half-width a = d/2, so u = d / (2 sqrt(3)).++The most frequent Type B error is treating a certificate's expanded uncertainty as a+standard uncertainty: it silently doubles the reported interval.++## Law of propagation of uncertainty++For a model Y = f(X_1, ..., X_N) with uncorrelated inputs (JCGM 100:2008 equation 10):++```text+u_c(y)^2 = sum_i ( df/dx_i )^2 * u(x_i)^2+```++with correlated inputs (equation 13):++```text+u_c(y)^2 = sum_i ( df/dx_i )^2 u(x_i)^2+ + 2 * sum_i sum_{j>i} (df/dx_i)(df/dx_j) u(x_i) u(x_j) r(x_i, x_j)+```++The partial derivatives are the **sensitivity coefficients** c_i. They carry units, and+`c_i * u(x_i)` is the contribution of that input expressed in the units of the result.+Comparing contributions, not raw uncertainties, is what tells you where to spend effort.++Correlation is not exotic. It appears whenever two inputs were calibrated against the+same standard, corrected with the same reference value, measured with the same+instrument, or derived from a common fit. Ignoring a positive correlation understates+u_c; ignoring a negative one overstates it. In a difference of two similar quantities+measured the same way, the correlation is the whole point — it is what makes the+difference more precise than either term.++## Degrees of freedom and the coverage factor++k = 2 is a convention, not a law. It corresponds to p ≈ 95% only when the effective+degrees of freedom are large. The Welch-Satterthwaite formula (JCGM 100:2008 G.2b)+gives them:++```text+nu_eff = u_c(y)^4 / sum_i ( (c_i u(x_i))^4 / nu_i )+```++Components evaluated as Type B from a specification are conventionally assigned+infinite degrees of freedom and drop out of the denominator. A single Type A component+from a handful of readings can pull nu_eff low enough that k rises well above 2:++| nu_eff | k for p = 95% |+| --- | --- |+| 2 | 4.30 |+| 5 | 2.57 |+| 10 | 2.23 |+| 20 | 2.09 |+| 50 | 2.01 |+| infinite | 1.96 |++If the dominant component came from five readings, reporting k = 2 understates the+interval by about a quarter. The formula assumes uncorrelated inputs; with correlation+it is an approximation with no established validity.++## When the GUM framework is not applicable++The framework linearizes f about the estimates. That is fine when the model is close to+linear across the input uncertainties, and wrong when it is not. Specifically, it+breaks down when:++- the model is significantly nonlinear over ±2u of an input — squares, reciprocals,+ ratios of comparable quantities, exponentials;+- a single non-normal component dominates, so the output is not approximately normal+ and k from a t-distribution does not deliver the claimed coverage;+- the output distribution is asymmetric, which the symmetric interval y ± U cannot+ represent;+- an input's relative uncertainty is large (above roughly 20-30%), where the second-order+ terms the expansion drops are no longer negligible;+- the model has a bound the interval crosses — a variance, a concentration, or a+ squared quantity whose GUM interval extends below zero.++## Monte Carlo propagation (JCGM 101:2008)++The supplement propagates the input distributions rather than their standard+deviations. The procedure is:++1. assign a probability density to each input, not merely a standard uncertainty;+2. draw M samples from the joint density, respecting any correlation;+3. evaluate the model for each draw;+4. take the mean as the estimate and the standard deviation as u_c;+5. take a coverage interval from the sorted output.++Two intervals are defined and they differ for an asymmetric output. The+**probabilistically symmetric** interval cuts (1-p)/2 from each tail. The **shortest**+interval is the narrowest one containing the fraction p; it is the honest choice when+the output is skewed, and identical to the other when it is not.++M = 10^6 is the usual starting point for a 95% interval; JCGM 101 also defines an+adaptive procedure that keeps drawing until the results are stable to within the+numerical tolerance below. Fewer than 10^4 trials cannot resolve a 95% interval's+endpoints reliably.++## The validation test that decides which answer to report++JCGM 101 clause 8 is the reason to run both methods rather than choosing one. Write u_c+from the GUM framework to n_dig significant digits (1 or 2) as c × 10^L. The numerical+tolerance is half of that last digit:++```text+delta = 0.5 * 10^L+```++Compare the endpoints of the two coverage intervals:++```text+d_low = | (y - U) - y_low_MC |+d_high = | (y + U) - y_high_MC |+```++If both are at or below delta, the linearization is validated and the GUM framework+result may be reported. If either exceeds delta, the framework is not validated for+this model, and the Monte Carlo result is what should be reported.++`scripts/propagate_uncertainty.py` runs both methods and applies this test. Two+outcomes worth understanding:++**Rectangular inputs, linear model.** A model dominated by rectangular contributions+fails validation even though it is perfectly linear: the true output distribution is+closer to trapezoidal than normal, and k = 1.96 over-covers. The GUM value and u_c are+right; the interval is too wide.++**Nonlinear model.** For y = x^2 with x = 1.0 ± 0.5, the framework gives y = 1.0,+u_c = 1.0, and a 95% interval of [-0.96, 2.96] — an interval that is largely negative+for a squared quantity. Monte Carlo gives a mean of 1.25, u_c = 1.06, and a shortest+95% interval of [0, 3.32]. The framework result is not merely imprecise; it is outside+the range the model can produce.++## Order of operations++1. Write the measurement model explicitly, including every correction, even those whose+ value is zero. A correction with an estimated value of zero still has an uncertainty,+ and leaving it out of the model leaves its uncertainty out of the budget.+2. Assign each input an estimate, a standard uncertainty, a distribution, and degrees of+ freedom.+3. Identify correlations before combining anything.+4. Compute sensitivity coefficients and the budget.+5. Combine, and check the linearization against Monte Carlo.+6. Choose k from nu_eff, not by habit.+7. Round the uncertainty first, then the value (see `reporting-rules.md`).++## Recurring defects++- Reporting a standard deviation of readings as the uncertainty of their mean.+- Dividing a certificate's expanded uncertainty by nothing, or by 2 when the certificate+ states a different k.+- Omitting a correction from the model because its value is negligible, thereby omitting+ its uncertainty too.+- Combining relative and absolute uncertainties without converting.+- Treating resolution and repeatability as independent when the resolution is what+ limits the repeatability — double counting.+- Quoting k = 2 with an effective degrees of freedom below 10.+- Applying the framework to a strongly nonlinear model and never checking.+- Propagating uncertainty through a fitted model without using the fit's covariance+ matrix, which discards the correlation between the fitted parameters.diff --git a/skills/uncertainty-and-units/references/pint-recipes.md b/skills/uncertainty-and-units/references/pint-recipes.mdnew file mode 100644index 00000000..821f3ed7--- /dev/null+++ b/skills/uncertainty-and-units/references/pint-recipes.md@@ -0,0 +1,228 @@+# Pint recipes++Verified against pint 0.25.3 with NumPy 2.5.1. Every output below was produced by+running the snippet.++## One registry per process++A `Quantity` belongs to the registry that created it. Two registries produce quantities+that cannot interact, and the failure is a bare `ValueError` far from the cause:++```python+import pint++first = pint.UnitRegistry()+second = pint.UnitRegistry()+first.Quantity(1, "m") + second.Quantity(1, "m")+# ValueError: Cannot operate with Quantity and Quantity of different registries.+```++This bites hardest across module boundaries, where each module innocently creates its+own registry at import time, and after unpickling, because a pickled quantity is+restored against the *application* registry rather than the one that created it.++Build one registry and share it, or use the application registry everywhere:++```python+import pint++ureg = pint.UnitRegistry()+pint.set_application_registry(ureg)++# in every other module+ureg = pint.get_application_registry()+```++## Offset units++Degrees Celsius and Fahrenheit measure a point on a scale, not an amount, so+multiplication and addition are undefined for them. Pint refuses rather than guessing:++```python+Q = ureg.Quantity+Q(20, "degC") * 2+# OffsetUnitCalculusError: Ambiguous operation with offset unit (degree_Celsius).+Q(20, "degC") + Q(5, "degC")+# OffsetUnitCalculusError: Ambiguous operation with offset unit (...).+```++The delta units carry temperature *differences*, and mixed arithmetic works:++```python+Q(20, "degC") + Q(5, "delta_degC") # 25 degree_Celsius+Q(25, "degC") - Q(20, "degC") # 5 delta_degree_Celsius+```++Note the second line: subtracting two absolute temperatures yields a delta unit+automatically, which is correct and often surprising downstream.++An uncertainty on a temperature is always a difference. `u = 0.5 degC` means+`0.5 delta_degC`; converting it to Fahrenheit multiplies by 9/5 and applies no offset,+giving `0.9 delta_degF`. Converting the *value* 20 degC to Fahrenheit applies the+offset and gives 68 degF. Two different conversions on the same line of a report.++`pint.UnitRegistry(autoconvert_offset_to_baseunit=True)` makes arithmetic proceed by+converting to kelvin first. It removes the exception, not the ambiguity; enable it+deliberately, not to silence an error.++## Logarithmic units++Pint models dB, dBm, and friends as non-multiplicative units, and `+` on them means+what it means in log space — multiplication of the underlying linear quantities:++```python+Q(10, "dBm").to("mW") # 10.000000000000002 milliwatt+Q(10, "dBm") + Q(10, "dBm") # 0.00010000000000000005 kilogram**2 * meter**4 / second**6+```++The second line is 10 mW × 10 mW = 10^-4 W², not 20 mW and not 13 dBm. Nothing raises.+Convert to a linear unit, do the arithmetic, convert back.++## Contexts++Some conversions are physical relations rather than dimensional identities. Pint+performs them only inside a named context, which is a feature: it forces the physics to+be stated.++```python+Q(532, "nm").to("THz", "sp") # 563.5196578947367 terahertz+Q(532, "nm").to("1/cm", "sp") # 18796.992481203004 / centimeter+Q(532, "nm").to("eV", "sp") # 2.3305300457368467 electron_volt+Q(532, "nm").to("THz") # DimensionalityError++Q(1, "g").to("mol", "chemistry", mw=Q(180.156, "g/mol")) # 0.005550744909966918 mole+Q(298.15, "K").to("eV", "boltzmann") # 0.02569257912108585 electron_volt+Q(1, "gauss").to("T", "Gaussian") # 9.999999999338245e-05 tesla+```++The registry ships `spectroscopy` (`sp`), `chemistry` (`chem`), `boltzmann`, `energy`,+`textile`, `Gaussian` (`Gau`), and `ESU` (`esu`). `ureg.enable_contexts("sp")` turns one+on for every subsequent conversion and `ureg.disable_contexts()` turns it off again;+prefer passing the context per call so the assumption stays visible at the point of use.++The spectroscopy context accepts a refractive index `n`, defaulting to 1 (vacuum). It+matters more than it looks:++```python+Q(532, "nm").to("THz", "sp") # 563.5196578947367 terahertz+Q(532, "nm").to("THz", "sp", n=1.33) # 423.69899089829823 terahertz+```++Note that `gauss` fails without the Gaussian context: CGS electromagnetic units have+different *dimensions* from SI ones, not merely different scales.++## Stripping the unit++`.magnitude` returns whatever number the quantity happens to be carrying, in whatever+unit it happens to be in. That is the single most common way a unit error enters a+correct-looking program:++```python+length = (12.7 * ureg.mm).magnitude # 12.7 -- but of what?+length = (12.7 * ureg.mm).to("m").magnitude # 0.0127 metres, stated+length = (12.7 * ureg.mm).m_as("m") # same, shorter+```++Always name the unit at the point of extraction. `m_as` exists precisely so there is no+excuse.++## Boundary enforcement++Rather than sprinkling conversions through a function, convert once at its boundary:++```python[email protected]("J", ("N", "m"))+def work(force, distance):+ return force * distance++work(ureg.Quantity(2, "N"), ureg.Quantity(300, "cm")) # 6.0 joule+```++`wraps` strips the declared units on the way in and reattaches the result unit on the+way out, so the body is plain floats and stays fast. It defaults to `strict=True`,+which rejects bare numbers:++```python+work(2.0, 3.0)+# ValueError: A wrapped function using strict=True requires quantity or a string+# for all arguments with not None units.+```++`strict=False` accepts bare numbers and assumes they are already in the declared units.+That is convenient and it is also exactly the assumption that unit tracking exists to+avoid; use it only at an edge you control.++`check` validates dimensionality without converting:++```python[email protected]("[length]", "[time]")+def speed(distance, elapsed):+ return distance / elapsed++speed(ureg.Quantity(10, "kg"), ureg.Quantity(2, "s"))+# DimensionalityError: Cannot convert from '10 kilogram' ([mass]) to 'a quantity of' ([length])+```++## NumPy interoperability++A quantity can wrap an array, and most ufuncs and many array functions are supported:++```python+import numpy as np++a = ureg.Quantity(np.array([1.0, 2.0, 3.0]), "m")+np.mean(a) # 2.0 meter+np.concatenate([a, ureg.Quantity(np.array([100.0]), "cm")]) # [1.0 2.0 3.0 1.0] meter+np.concatenate([a, np.array([1.0])])+# DimensionalityError: Cannot convert from 'dimensionless' to 'meter'+```++Note that the mixed concatenation converted centimetres to metres correctly, and the+bare array was rejected rather than assumed. Both behaviours are what you want.++Wrapped arrays carry per-operation overhead. In an inner loop, convert at the boundary+with `wraps` or `m_as` and compute on raw arrays.++## Custom units and definitions++```python+ureg.define("cell = [cell_count] = cells")+ureg.define("od600 = [optical_density]")+ureg.define("percent_v_v = 0.01 = %v/v")+```++Defining a new base dimension in square brackets makes it dimensionally distinct from+everything else, which is the point: `cells / mL` will then refuse to be added to+`particles / mL`. Load a whole file of them with `ureg.load_definitions("units.txt")`.++## Formatting++```python+q = ureg.Quantity(1.2345, "kg*m/s**2")+f"{q}" # 1.2345 kilogram * meter / second ** 2+f"{q:~}" # 1.2345 kg * m / s ** 2+f"{q:.3f~P}" # 1.234 kg·m/s²+f"{q:~L}" # 1.2345\ \frac{\mathrm{kg} \cdot \mathrm{m}}{\mathrm{s}^{2}}+```++`~` gives short unit symbols, `P` pretty Unicode, `L` LaTeX, `C` compact ASCII. Numeric+format specs come first and behave as usual.++## With uncertainties++The two libraries compose: a `ufloat` magnitude inside a pint quantity converts and+formats correctly.++```python+from uncertainties import ufloat++q = ufloat(2.5, 0.1) * ureg.meter+q.to("cm") # 250+/-10 centimeter+f"{q:.2uS}" # 2.50(10) meter+```++## Related packages++`pint-pandas` provides a pandas extension dtype so a DataFrame column carries a unit;+`pint-xarray` does the same for xarray. Both are separate installs and both inherit the+one-registry rule.diff --git a/skills/uncertainty-and-units/references/plausibility-scales.md b/skills/uncertainty-and-units/references/plausibility-scales.mdnew file mode 100644index 00000000..1512cd1d--- /dev/null+++ b/skills/uncertainty-and-units/references/plausibility-scales.md@@ -0,0 +1,168 @@+# Plausibility: dimensionless groups, characteristic scales, and magnitude bands++Dimensional analysis proves a calculation is *consistent*. It cannot prove the answer is+*possible*. A cell 2 m across, a Reynolds number of 4×10⁷ in a capillary, and a diffusion+time of 300 years across a lipid bilayer are all dimensionally impeccable, and a+unit-checking library will pass every one of them.++The three checks below close that gap. `scripts/check_plausibility.py` runs all of them+and verifies dimensional consistency of each formula before reporting a number.++---++## 1. Choose the characteristic length first++The single most common error in this whole area is not an arithmetic slip — it is using+the wrong length. The dimensionless groups are only meaningful with the length the+correlation was fitted against.++| Geometry | Characteristic length |+| --- | --- |+| Flow in a circular pipe | inside **diameter**, not radius |+| Flow in a non-circular duct | hydraulic diameter `4A/P` |+| External flow over a plate | distance from the leading edge |+| Flow past a sphere or cylinder | diameter |+| Conduction in an irregular body (Biot) | volume / surface area |+| Packed bed | particle diameter |+| Open channel | hydraulic radius `A/P` — note: radius, not diameter |++Using radius where the correlation wants diameter puts every threshold out by a factor of+two, which is exactly the size of error that survives review.++## 2. Dimensionless groups and what they gate++Each threshold is a *modelling decision boundary*: past it, an assumption in your+analysis stops holding.++| Group | Definition | Threshold | What stops being true past it |+| --- | --- | --- | --- |+| Reynolds `Re` | `ρvL/μ` | 2300 / 4000 (pipe) | laminar solutions; above 4000 you need a turbulence model |+| Péclet `Pe` | `vL/D` | ≈ 1 | below 1 diffusion dominates, so stirring will not help |+| Damköhler `Da_I` | `kL/v` | 0.1 / 10 | above 10 the reagent is consumed at the inlet, so the reactor is transport-limited |+| Knudsen `Kn` | `λ/L` | 0.01 | the no-slip boundary condition, then the continuum assumption itself |+| Mach `Ma` | `v/c` | 0.3 | incompressibility, at about 5% density change |+| Womersley `Wo` | `R√(ωρ/μ)` | 1 / 10 | the parabolic (Poiseuille) profile; above 10 the core moves as a plug |+| Capillary `Ca` | `μv/σ` | ≈ 10⁻³ | an interface whose shape is set by surface tension alone |+| Weber `We` | `ρv²L/σ` | ≈ 12 | drop integrity — above it, aerodynamic breakup |+| Bond `Bo` | `Δρ g L²/σ` | 1 | surface tension holding a drop against gravity |+| Stokes `Stk` | `ρ_p d² v / (18 μ L)` | 0.1 | the tracer assumption behind PIV and aerosol sampling |+| Biot `Bi` | `hL/k` | 0.1 | lumped-capacitance (uniform internal temperature) |+| Fourier `Fo` | `αt/L²` | 0.05 / 1 | the semi-infinite solution; above 1 the body has equilibrated |+| Schmidt `Sc` | `μ/(ρD)` | — | ≈ 1 for gases, ≈ 10³ for small molecules in water |+| Deborah `De` | `t_relax/t_obs` | 1 | whether the material is a liquid or a solid *on your timescale* |++**Womersley takes angular frequency.** Pass `2πf`, not `f`. A resting human heart at+1.2 Hz gives `ω ≈ 7.5 rad/s`, and in the aorta `Wo ≈ 20` — firmly plug-like, which is why+Poiseuille's law is the wrong model for arterial flow and the right one for a capillary.++**The Reynolds thresholds are pipe-flow values.** Transition over a flat plate is around+`Re ≈ 5×10⁵`; for flow past a sphere the wake becomes unsteady near `Re ≈ 100`. The tool+reports the pipe classification and says so.++## 3. Characteristic scales++| Scale | Formula | Sanity anchor |+| --- | --- | --- |+| Diffusion time | `L²/D` | 10 µm at 10⁻⁹ m²/s → 0.1 s |+| Thermal diffusion time | `L²/α` | same form, thermal diffusivity |+| Thermal energy | `k_B T` | 4.14×10⁻²¹ J at 300 K |+| Molar thermal energy | `RT` | 2.49 kJ/mol at 300 K |+| Stokes settling velocity | `Δρ g d²/(18μ)` | 1 µm bead in water → ≈ 0.5 µm/s |+| Mean free path (gas) | `k_BT/(√2 π d² p)` | air at 1 atm → ≈ 68 nm |+| Debye length | `√(ε₀ε_r k_B T / (2 N_A e² I))` | 100 mM → 0.96 nm |+| Capillary length | `√(σ/(ρg))` | water → 2.7 mm |++**The L² in diffusion time is the whole story of cell biology.** Ten micrometres takes+0.1 s; one millimetre takes 1000 s; one centimetre takes 10⁵ s ≈ 28 hours. This is why+cells are small, why tissue thicker than ~200 µm needs a blood supply, and why a claim+that a molecule "diffuses across the tissue in seconds" is worth checking.++**Stokes settling is valid only while the particle Reynolds number stays below ≈ 0.1.**+Compute the settling velocity, then feed it back into the `reynolds` group with the+particle diameter as the length. If `Re_p > 0.1`, the drag law is wrong and the velocity+is an overestimate.++## 4. Magnitude bands++These are deliberately generous observed ranges. A value outside one is worth a second+look, not automatically wrong — the tool reports `questionable` inside one decade and+`implausible` beyond it.++| Band | Range | Source |+| --- | --- | --- |+| Bacterial cell diameter | 0.2–10 µm | Milo & Phillips, *Cell Biology by the Numbers*, ch. 1 |+| Eukaryotic cell diameter | 5–100 µm | Milo & Phillips, ch. 1 |+| Cell membrane thickness | 3–5 nm | Alberts et al., *MBoC* 7th ed., ch. 10 |+| DNA base-pair rise | 0.32–0.36 nm | Bloomfield et al., *Nucleic Acids* |+| Ribosome diameter | 20–30 nm | Milo & Phillips, ch. 1 |+| Protein molar mass | 5–1000 kDa | Milo & Phillips, ch. 1 |+| Human capillary diameter | 5–10 µm | Guyton & Hall, 14th ed., ch. 16 |+| Mammalian body temperature | 306–315 K | Guyton & Hall, ch. 74 |+| Resting heart rate | 0.7–3 Hz | Guyton & Hall, ch. 9 |+| Blood plasma osmolarity | 275–300 mol/m³ | Guyton & Hall, ch. 25 |+| Small-molecule diffusivity in water | 3×10⁻¹⁰–3×10⁻⁹ m²/s | Cussler, *Diffusion* 3rd ed., app. A |+| Protein diffusivity in water | 10⁻¹¹–1.5×10⁻¹⁰ m²/s | Cussler, app. A |+| Dynamic viscosity of water | 0.5–1.5 mPa·s | IAPWS R12-08 |+| Surface tension of water | 0.06–0.08 N/m | IAPWS R1-76 |+| Speed of sound in water | 1400–1560 m/s | Del Grosso & Mader, *JASA* 52:1442 (1972) |+| Speed of sound in air | 320–350 m/s | Cramer, *JASA* 93:2510 (1993) |+| Sea-level atmospheric pressure | 95–105 kPa | ISO 2533 |+| Earth surface gravity | 9.76–9.84 m/s² | WGS 84 normal gravity |+| Visible wavelength | 380–750 nm | CIE S 017:2020 |+| Non-covalent bond energy | 1–40 kJ/mol | Israelachvili 3rd ed., ch. 2 |+| Covalent bond energy | 150–1000 kJ/mol | Atkins & de Paula 12th ed. |+| ATP hydrolysis free energy | 40–60 kJ/mol | Milo & Phillips, ch. 4 |++**Compare binding energies against `RT`, not against zero.** At 300 K, `RT` is 2.5 kJ/mol.+A reported binding free energy of 1 kJ/mol is not a weak interaction; it is+indistinguishable from thermal noise.++## 5. The three errors this catches++**A quantity of the wrong kind.** Kinematic viscosity (m²/s) where the formula needs+dynamic (Pa·s) is the classic. Both are called "viscosity", both are tabulated for water,+and they differ by a factor of ρ ≈ 1000. The dimensionality check refuses it before any+number is computed:++```+error: viscosity must have dimensionality [mass] / ([length] * [time]),+ but m²/s is [length] ** 2 / [time]+```++**A unit prefix slip.** Micro for milli is three decades. The magnitude bands catch it+whenever the quantity is one the table knows.++**An assumption used outside its regime.** Applying Poiseuille's law at `Wo = 20`, the+lumped-capacitance model at `Bi = 5`, or Stokes drag at `Re_p = 30` all produce a number.+The group tells you the number is meaningless.++## 6. Caveats++- The thresholds are **conventions with soft edges**, not physical constants. `Re = 2400`+ in a very smooth pipe can stay laminar; `Re = 2000` with a disturbed inlet may not.+- Every group assumes the geometry its correlation was fitted for. Check §1 before+ trusting a classification.+- The bands describe **typical observed values**, not physical limits. Extremophiles,+ engineered materials, and pathological states legitimately sit outside them — which is+ why the tool warns rather than refuses.+- A `plausible` verdict means nothing contradicted the tables. It is not a correctness+ proof, and it says nothing about whether the *measurement* was any good — for that,+ see `references/gum-methodology.md`.++## Sources++Checked 2026-07-26:++- White, *Fluid Mechanics*, 8th ed. — Reynolds, Mach, pipe-flow transition.+- Deen, *Analysis of Transport Phenomena*, 2nd ed. — Péclet, Schmidt, boundary layers.+- Incropera et al., *Fundamentals of Heat and Mass Transfer* — Biot, Fourier.+- Bruus, *Theoretical Microfluidics* — capillary number, low-Reynolds flow.+- Berg, *Random Walks in Biology* — diffusion times, the L² scaling.+- Phillips et al., *Physical Biology of the Cell*, 2nd ed. — `k_BT` as the biological+ energy scale.+- Milo & Phillips, *Cell Biology by the Numbers* — biological magnitude bands;+ [bionumbers.hms.harvard.edu](https://bionumbers.hms.harvard.edu/).+- Israelachvili, *Intermolecular and Surface Forces*, 3rd ed. — Debye length, bond energies.+- Cussler, *Diffusion*, 3rd ed. — diffusivity tables.+- [CODATA internationally recommended values](https://physics.nist.gov/cuu/Constants/) —+ reached through `scipy.constants`, never typed as literals.diff --git a/skills/uncertainty-and-units/references/reporting-rules.md b/skills/uncertainty-and-units/references/reporting-rules.mdnew file mode 100644index 00000000..24d939f1--- /dev/null+++ b/skills/uncertainty-and-units/references/reporting-rules.md@@ -0,0 +1,133 @@+# Reporting rules++A number without its uncertainty and without a statement of what that uncertainty means+cannot be checked, compared, or reused. This file covers what has to accompany a+reported result.++## Round the uncertainty first++JCGM 100:2008 7.2.6. The uncertainty is rounded to one or two significant digits, and+the value is then rounded to that same decimal place — never the other way round, and+never independently.++```text+12.34567 +/- 0.02345 -> 12.346 +/- 0.023+12.34567 +/- 0.1 -> 12.35 +/- 0.10 (two digits, since 1 leads)+1234 +/- 250 -> 1230 +/- 250+```++Two digits are the safe default. One digit is acceptable when the leading digit is 3 or+more; rounding 0.14 to 0.1 changes it by 29%, which is why a leading 1 or 2 should keep+two digits. `scripts/format_result.py` applies the rule and warns on that case.++Trailing zeros in the uncertainty are significant and must be kept: `0.10`, not `0.1`.++## Notations++| Notation | Example | Where it is used |+| --- | --- | --- |+| Plus-minus | 12.346 ± 0.023 mm | prose, tables |+| Concise / parenthetic | 12.346(23) mm | physics, CODATA, high-precision tables |+| Scientific | (9.1093837139 ± 0.0000000028)e-31 kg | very large or small values |+| Concise scientific | 9.1093837139(28)e-31 kg | constants |++In concise notation the digits in parentheses apply to the last digits of the quoted+value, so `12.346(23)` is 12.346 ± 0.023 and `1234(25)` is 1234 ± 25. When the+uncertainty's last significant digit falls left of the decimal point the notation is+ambiguous and the scientific form must be used instead.++## What has to be stated alongside++A bare `±` is ambiguous. Readers cannot tell a standard uncertainty from an expanded+one, a standard deviation from a standard error, or a 95% interval from a 68% one. State:++1. **Which quantity the number is** — combined standard uncertainty u_c, expanded+ uncertainty U, standard deviation of a sample, standard error of a mean, or a+ confidence interval.+2. **The coverage factor k**, when U is reported.+3. **The coverage probability** and how it was arrived at — from a normal assumption or+ from effective degrees of freedom.+4. **The effective degrees of freedom**, when they are small enough to matter (below+ about 30).+5. **The method** — GUM framework, Monte Carlo, or both with the validation outcome.++The two standard forms:++> m = 100.02147 g with a combined standard uncertainty of u_c = 0.35 mg.++> m = (100.02147 ± 0.00079) g, where the number following the symbol ± is the numerical+> value of an expanded uncertainty U = k·u_c with U determined from a combined standard+> uncertainty u_c = 0.35 mg and a coverage factor k = 2.26 based on the t-distribution+> for ν_eff = 9 degrees of freedom, and defines an interval estimated to have a level of+> confidence of 95%.++The second is verbose because it has to be. `scripts/format_result.py --coverage-factor`+generates the sentence.++## SD, SEM, and CI in figures++An error bar is uninterpretable unless the caption says what it is, and the three+common choices differ by more than a factor of two for typical n:++| Bar | Answers | Shrinks with n |+| --- | --- | --- |+| Standard deviation | how much do individual observations scatter | no |+| Standard error of the mean | how precisely is the mean located | yes, as 1/√n |+| 95% confidence interval | plausible range for the population mean | yes |++Choosing SEM because it looks tighter is a misrepresentation when the question is about+spread. Every caption needs the bar's identity, n, and whether n counts biological or+technical replicates. Two SEM bars that do not overlap do not establish a significant+difference, and two 95% CIs that overlap slightly do not establish the absence of one.++## Relative and absolute++State which. A relative standard uncertainty is dimensionless and is written+`u_r(y) = 0.0035` or `0.35%`; multiplying it by the value gives the absolute one. Mixing+them in a single budget without conversion is a common arithmetic error — a "1%"+component and a "0.2 mg" component cannot be combined until they are in the same form.++For a product or quotient of independent quantities, relative uncertainties combine in+quadrature; for a sum or difference, absolute ones do. Using the wrong one is the single+most common propagation mistake, and it is why deriving the budget from sensitivity+coefficients rather than from remembered rules is worth the extra step.++## Results near zero or below a detection limit++- Do not report a value with an uncertainty larger than itself as though it were a+ measurement. Report the estimate and its uncertainty, and state that it is consistent+ with zero.+- Do not substitute zero, LOD, or LOD/2 for a non-detect without saying so; each choice+ biases downstream statistics differently.+- A negative estimate of a non-negative quantity is a legitimate measurement outcome and+ should be reported as measured, not truncated. Truncating biases any subsequent+ average.+- LOD and LOQ are defined by a stated procedure (typically 3σ and 10σ of the blank).+ Quote the procedure with the number.++## Significant figures in intermediate work++Round only at the point of reporting. Carrying rounded intermediates through a+calculation accumulates error that the uncertainty budget does not account for, and it+can shift the final digit. Keep full precision internally; apply+`scripts/format_result.py` at the end.++The same applies to constants: use `scipy.constants`, not a value typed from memory to+four digits.++## Conformity statements++Deciding whether a result passes a specification is a separate step from measuring it,+because a result within tolerance but with an uncertainty straddling the limit has not+demonstrated conformity. ISO/IEC 17025 requires a documented decision rule; ILAC-G8+describes guard-banded acceptance, where the acceptance limit is pulled inside the+specification limit by a multiple of u_c chosen for the false-accept risk you will+tolerate. Report the rule alongside the verdict.++## Sources++- JCGM 100:2008 (GUM), clause 7 — reporting uncertainty.+- JCGM 101:2008 (GUM Supplement 1), clause 8 — numerical tolerance and validation.+- NIST Technical Note 1297, *Guidelines for Evaluating and Expressing the Uncertainty of+ NIST Measurement Results* — the source of the two standard sentence forms above.+- ISO/IEC 17025:2017 clause 7.8.6 and ILAC-G8:09/2019 — decision rules and guard bands.diff --git a/skills/uncertainty-and-units/references/uncertainties-recipes.md b/skills/uncertainty-and-units/references/uncertainties-recipes.mdnew file mode 100644index 00000000..a160b55f--- /dev/null+++ b/skills/uncertainty-and-units/references/uncertainties-recipes.md@@ -0,0 +1,167 @@+# uncertainties recipes++Verified against uncertainties 3.2.3 with NumPy 2.5.1 and SciPy 1.18.0. The package+performs first-order (linear) propagation with analytic derivatives, tracking+correlations automatically through every operation. Everything it does is the GUM+uncertainty framework; it does not perform Monte Carlo propagation and does not check+whether linearization was appropriate.++## Variables and the correlation they carry++```python+from uncertainties import ufloat++x = ufloat(1.0, 0.1)+x - x # 0.0+/-0 the same variable, perfectly correlated+x - ufloat(1.0, 0.1) # 0.00+/-0.14 two independent variables+```++That pair of lines is the whole design. A `ufloat` is an *identity*, not a number with+an attached error bar, and the difference of a variable with itself is exactly zero.+This is the correct answer, and it is why arithmetic on ufloats beats manual quadrature+in any expression where a quantity appears more than once.++The corollary is the most common defect:++```python+copy = ufloat(x.nominal_value, x.std_dev) # a NEW, independent variable+x - copy # 0.00+/-0.14, not 0+```++Rebuilding a variable from `.nominal_value` and `.std_dev` throws away every correlation+it carried. So does serializing to JSON and back, storing in a DataFrame column of+floats, or passing through any interface that speaks in pairs of numbers.+`scripts/audit_units.py` flags this construction as `UNC004`.++## Correlated inputs++```python+import numpy as np+from uncertainties import correlated_values, correlation_matrix, covariance_matrix++cov = np.array([[0.04, 0.012],+ [0.012, 0.09]])+a, b = correlated_values([1.0, 2.0], cov)++a + b # 3.0+/-0.4+correlation_matrix([a, b])[0][1] # 0.19999999999999987+```++`correlated_values` is how a fit's covariance matrix enters the calculation intact.+`correlation_matrix` returns a NumPy array; `covariance_matrix` returns a nested list —+index it as `[i][j]`, not `[i, j]`.++`correlated_values_norm` takes `[(value, std_dev), ...]` plus a *correlation* matrix+instead of a covariance matrix, which is usually what a paper reports.++## Functions++Ordinary `math` and `numpy` functions have no derivative rule for these objects and+fail, loudly on scalars and inside the ufunc loop on arrays:++```python+import math, numpy as np+from uncertainties import umath, unumpy++math.sqrt(a) # TypeError: can't convert an affine function ... to float+np.sqrt(a) # TypeError: loop of ufunc does not support argument 0 of type AffineScalarFunc+umath.sqrt(a) # 1.00+/-0.10+```++`umath` mirrors `math`: sqrt, exp, log, log10, log1p, expm1, the trigonometric and+hyperbolic functions and their inverses, atan2, hypot, degrees, radians, fabs, erf.++For arrays, `unumpy` provides the wrapped versions plus constructors and accessors:++```python+arr = unumpy.uarray([1.0, 2.0], [0.1, 0.2])+unumpy.sqrt(arr) # [1.0+/-0.05 1.4142135623730951+/-0.07071067811865475]+unumpy.nominal_values(arr) # [1. 2.]+unumpy.std_devs(arr) # [0.1 0.2]+np.mean(arr) # 1.50+/-0.11 -- works: object-array reduction+np.sqrt(arr) # TypeError -- fails: ufunc loop+```++The split is worth internalizing: reductions written in terms of Python arithmetic+(`mean`, `sum`, `dot`) work on object arrays, while ufuncs do not. When in doubt use+`unumpy`.++## Formatting++The format spec extends the standard one. `u` counts significant digits *in the+uncertainty*, and the value is rounded to match:++```python+from uncertainties import ufloat+v = ufloat(12.3456, 0.0234)++f"{v:.2u}" # 12.346+/-0.023+f"{v:.2uS}" # 12.346(23) concise notation+f"{v:.1uP}" # 12.35±0.02 pretty Unicode+f"{v:.2uL}" # 12.346 \pm 0.023 LaTeX+f"{ufloat(0.00012345, 0.0000023):.2ue}" # (1.234+/-0.023)e-04+```++This handles the GUM rounding rule for you. `scripts/format_result.py` covers the cases+this does not: an expanded uncertainty with a stated k, the accompanying sentence, and+the warnings about reporting one digit when the leading digit is 1 or 2.++## Where the uncertainty came from++```python+result = a**2 + b+result.derivatives[a] # 2.0 -- the sensitivity coefficient+result.error_components() # {variable: contribution} for every input+```++`error_components` is the uncertainty budget, keyed by the original variables. Sorting+it descending tells you which input to improve.++## Fitted parameters++The covariance matrix from a fit is the correlation between parameters, and discarding+it is a routine error:++```python+import numpy as np+from scipy.optimize import curve_fit+from uncertainties import correlated_values++popt, pcov = curve_fit(model, x, y, sigma=sigma, absolute_sigma=True)+slope, intercept = correlated_values(popt, pcov) # keeps the correlation+```++Taking `np.sqrt(np.diag(pcov))` and building independent ufloats discards it, and for a+straight-line fit the slope-intercept correlation is strongly negative — predictions+near the centroid of the data come out far too uncertain.++`absolute_sigma` decides what `pcov` means, and the default is not what most users+assume:++```python+popt, pcov = curve_fit(f, x, y, sigma=sigma, absolute_sigma=False) # default+# pcov is rescaled by the reduced chi-square: parameter uncertainties absorb the+# goodness of fit, and are identical to what you get by passing no sigma at all.++popt, pcov = curve_fit(f, x, y, sigma=sigma, absolute_sigma=True)+# pcov reflects the standard uncertainties you supplied.+```++On one synthetic straight-line fit the two give parameter standard deviations of+`[0.0364, 0.2154]` and `[0.0477, 0.2820]` — a 31% difference, with no warning. Pass+`absolute_sigma=True` whenever `sigma` holds real standard uncertainties;+`scripts/audit_units.py` flags the omission as `UNC001`.++## Limits++- **First order only.** For a strongly nonlinear model the reported std_dev is the+ linearized one, and the package cannot tell you that. Cross-check with+ `scripts/propagate_uncertainty.py`, which runs Monte Carlo alongside.+- **No distribution.** A ufloat carries a standard deviation, not a shape. Rectangular+ and normal inputs with the same u are indistinguishable to it.+- **No degrees of freedom.** Coverage factors are your problem; use+ `scripts/uncertainty_budget.py`.+- **`float()` fails**, deliberately, on anything with an uncertainty. Comparison+ operators compare nominal values.+- **Object arrays are slow.** For large arrays, propagate analytically or by Monte Carlo+ rather than element-wise.diff --git a/skills/uncertainty-and-units/scripts/_common.py b/skills/uncertainty-and-units/scripts/_common.pynew file mode 100644index 00000000..af8f72da--- /dev/null+++ b/skills/uncertainty-and-units/scripts/_common.py@@ -0,0 +1,666 @@+#!/usr/bin/env python3+"""Shared, standard-library-first helpers for the uncertainty and unit CLIs."""++from __future__ import annotations++import ast+import json+import math+import os+import stat+import tempfile+from pathlib import Path+from typing import Any, Callable, Iterable+++MAX_INPUT_BYTES = 4 * 1024 * 1024+MAX_REPORT_BYTES = 4 * 1024 * 1024+MAX_COMPONENTS = 256+MAX_VARIABLES = 64+MAX_TRIALS = 5_000_000+# JCGM 101:2008 recommends at least 10**6 trials for a 95% coverage+# interval; below that the interval endpoints are dominated by sampling noise+# and the clause 8 comparison stops discriminating.+RECOMMENDED_TRIALS = 1_000_000+DEFAULT_TRIALS = RECOMMENDED_TRIALS+DEFAULT_SEED = 20_260_726+MAX_EXPRESSION_CHARS = 2_000+MAX_EXPRESSION_NODES = 400+MAX_EXPRESSION_DEPTH = 32+MAX_LITERAL_EXPONENT = 64++PINNED_INSTALL = (+ 'uv pip install "pint==0.25.3" "uncertainties==3.2.3" '+ '"numpy==2.5.1" "scipy==1.18.0"'+)++# Standard uncertainty of a Type B component equals its half-width divided by+# these factors (JCGM 100:2008 4.3.7-4.3.9).+DISTRIBUTION_DIVISORS: dict[str, float] = {+ "normal": 1.0,+ "rectangular": math.sqrt(3.0),+ "triangular": math.sqrt(6.0),+ "arcsine": math.sqrt(2.0),+ "exact": 1.0,+}++ALLOWED_FUNCTIONS = (+ "abs",+ "acos",+ "acosh",+ "asin",+ "asinh",+ "atan",+ "atan2",+ "atanh",+ "cos",+ "cosh",+ "degrees",+ "erf",+ "exp",+ "expm1",+ "fabs",+ "hypot",+ "log",+ "log10",+ "log1p",+ "radians",+ "sin",+ "sinh",+ "sqrt",+ "tan",+ "tanh",+)++ALLOWED_CONSTANTS = {"pi": math.pi, "e": math.e, "tau": math.tau}++_ALLOWED_NODES = (+ ast.Expression,+ ast.BinOp,+ ast.UnaryOp,+ ast.Call,+ ast.Name,+ ast.Constant,+ ast.Load,+ ast.Add,+ ast.Sub,+ ast.Mult,+ ast.Div,+ ast.Pow,+ ast.USub,+ ast.UAdd,+)+++class CliError(ValueError):+ """An expected command-line validation error."""+++def bounded_int(minimum: int, maximum: int) -> Callable[[str], int]:+ """Return an argparse converter for a bounded integer."""++ def convert(value: str) -> int:+ try:+ parsed = int(value)+ except ValueError as exc:+ raise CliError(f"expected an integer, got {value!r}") from exc+ if not minimum <= parsed <= maximum:+ raise CliError(+ f"expected an integer from {minimum} through {maximum}, got {parsed}"+ )+ return parsed++ return convert+++def finite_float(value: str) -> float:+ """Parse a finite floating-point value."""++ try:+ parsed = float(value)+ except (TypeError, ValueError) as exc:+ raise CliError(f"expected a number, got {value!r}") from exc+ if not math.isfinite(parsed):+ raise CliError("value must be finite")+ return parsed+++def non_negative_float(value: str) -> float:+ """Parse a finite, non-negative floating-point value."""++ parsed = finite_float(value)+ if parsed < 0:+ raise CliError("value must not be negative")+ return parsed+++def positive_float(value: str) -> float:+ """Parse a finite, strictly positive floating-point value."""++ parsed = finite_float(value)+ if parsed <= 0:+ raise CliError("value must be greater than zero")+ return parsed+++def probability(value: str) -> float:+ """Parse a coverage probability strictly between zero and one."""++ parsed = finite_float(value)+ if not 0 < parsed < 1:+ raise CliError("coverage probability must be strictly between 0 and 1")+ return parsed+++def as_finite(value: Any, *, label: str) -> float:+ """Coerce a JSON scalar to a finite float."""++ if isinstance(value, bool) or not isinstance(value, (int, float)):+ raise CliError(f"{label} must be a number")+ number = float(value)+ if not math.isfinite(number):+ raise CliError(f"{label} must be finite")+ return number+++def as_degrees_of_freedom(value: Any, *, label: str) -> float:+ """Coerce a degrees-of-freedom entry, treating null as infinite."""++ if value is None:+ return math.inf+ if isinstance(value, str) and value.strip().lower() in {"inf", "infinite"}:+ return math.inf+ number = as_finite(value, label=label)+ if number <= 0:+ raise CliError(f"{label} must be greater than zero")+ return number+++def normalize_distribution(value: Any, *, label: str) -> str:+ """Validate a supported probability-density shape."""++ if value is None:+ return "normal"+ if not isinstance(value, str):+ raise CliError(f"{label} must be a string")+ name = value.strip().lower()+ aliases = {"uniform": "rectangular", "u-shaped": "arcsine", "gaussian": "normal"}+ name = aliases.get(name, name)+ if name not in DISTRIBUTION_DIVISORS:+ allowed = ", ".join(sorted(DISTRIBUTION_DIVISORS))+ raise CliError(f"{label} must be one of: {allowed}")+ return name+++def checked_input_file(+ value: str | os.PathLike[str],+ *,+ suffixes: Iterable[str],+ max_bytes: int = MAX_INPUT_BYTES,+) -> Path:+ """Return a bounded regular local file, rejecting URLs and symlinks."""++ raw = os.fspath(value)+ if "://" in raw:+ raise CliError("network URLs are not accepted; provide a local file")+ path = Path(raw).expanduser()+ if path.is_symlink():+ raise CliError(f"input must not be a symlink: {path}")+ try:+ info = path.stat()+ except OSError as exc:+ raise CliError(f"cannot access input file {path}: {exc}") from exc+ if not stat.S_ISREG(info.st_mode):+ raise CliError(f"input is not a regular file: {path}")+ if info.st_size > max_bytes:+ raise CliError(f"input is {info.st_size} bytes; limit is {max_bytes} bytes")+ allowed = {suffix.lower() for suffix in suffixes}+ if path.suffix.lower() not in allowed:+ raise CliError(f"input suffix must be one of: {', '.join(sorted(allowed))}")+ return path.resolve()+++def checked_output_file(+ value: str | os.PathLike[str],+ *,+ suffixes: Iterable[str],+ force: bool = False,+) -> Path:+ """Validate an explicit local output without following symlinks."""++ raw = os.fspath(value)+ if "://" in raw:+ raise CliError("network URLs are not accepted as output paths")+ path = Path(raw).expanduser()+ if path.name in {"", ".", ".."}:+ raise CliError("output must name a file")+ if path.is_symlink():+ raise CliError(f"output must not be a symlink: {path}")+ allowed = {suffix.lower() for suffix in suffixes}+ if path.suffix.lower() not in allowed:+ raise CliError(f"output suffix must be one of: {', '.join(sorted(allowed))}")+ parent = path.parent+ if not parent.exists() or not parent.is_dir() or parent.is_symlink():+ raise CliError(f"output parent must be an existing regular directory: {parent}")+ if path.exists():+ if not path.is_file():+ raise CliError(f"output exists and is not a regular file: {path}")+ if not force:+ raise CliError(f"refusing to overwrite existing output: {path}")+ return parent.resolve() / path.name+++def atomic_write_bytes(path: Path, payload: bytes, *, force: bool = False) -> None:+ """Write bytes through a private same-directory temporary file."""++ destination = checked_output_file(path, suffixes={path.suffix.lower()}, force=force)+ descriptor, temporary_name = tempfile.mkstemp(+ prefix=f".{destination.name}.", suffix=".tmp", dir=destination.parent+ )+ temporary = Path(temporary_name)+ try:+ with os.fdopen(descriptor, "wb") as handle:+ handle.write(payload)+ handle.flush()+ os.fsync(handle.fileno())+ os.chmod(temporary, 0o600)+ if destination.exists() and not force:+ raise CliError(f"refusing to overwrite existing output: {destination}")+ os.replace(temporary, destination)+ finally:+ temporary.unlink(missing_ok=True)+++def _json_bytes(document: Any) -> bytes:+ """Serialize deterministic strict JSON."""++ try:+ payload = (+ json.dumps(+ document,+ indent=2,+ sort_keys=True,+ ensure_ascii=False,+ allow_nan=False,+ )+ + "\n"+ ).encode("utf-8")+ except (TypeError, ValueError) as exc:+ raise CliError(f"report is not strict JSON: {exc}") from exc+ if len(payload) > MAX_REPORT_BYTES:+ raise CliError(+ f"report is {len(payload)} bytes; limit is {MAX_REPORT_BYTES} bytes"+ )+ return payload+++def emit_json(+ document: Any,+ *,+ output: str | os.PathLike[str] | None = None,+ force: bool = False,+) -> None:+ """Print deterministic JSON or write it atomically."""++ payload = _json_bytes(document)+ if output is None:+ print(payload.decode("utf-8"), end="")+ return+ destination = checked_output_file(output, suffixes={".json"}, force=force)+ atomic_write_bytes(destination, payload, force=force)+++def emit_text(+ text: str,+ *,+ output: str | os.PathLike[str] | None = None,+ force: bool = False,+) -> None:+ """Print text or write it atomically to Markdown."""++ payload = text.encode("utf-8")+ if len(payload) > MAX_REPORT_BYTES:+ raise CliError(+ f"report is {len(payload)} bytes; limit is {MAX_REPORT_BYTES} bytes"+ )+ if output is None:+ print(text, end="" if text.endswith("\n") else "\n")+ return+ destination = checked_output_file(output, suffixes={".md"}, force=force)+ atomic_write_bytes(destination, payload, force=force)+++def load_json(value: str | os.PathLike[str]) -> Any:+ """Load bounded strict JSON from a local file."""++ path = checked_input_file(value, suffixes={".json"})++ def reject_constant(constant: str) -> None:+ raise CliError(f"non-standard JSON constant is not allowed: {constant}")++ try:+ with path.open("r", encoding="utf-8") as handle:+ return json.load(handle, parse_constant=reject_constant)+ except (OSError, json.JSONDecodeError) as exc:+ raise CliError(f"cannot read valid JSON from {path.name}: {exc}") from exc+++def read_text_file(+ value: str | os.PathLike[str], *, suffixes: Iterable[str] = (".py",)+) -> tuple[str, Path]:+ """Read a bounded local UTF-8 text file."""++ path = checked_input_file(value, suffixes=suffixes)+ try:+ return path.read_text(encoding="utf-8"), path+ except (OSError, UnicodeDecodeError) as exc:+ raise CliError(f"cannot read {path.name} as UTF-8 text: {exc}") from exc+++# --- bounded expression handling -------------------------------------------+#+# Measurement models arrive as text. Nothing here compiles or executes that+# text: the string is parsed to an AST, every node is checked against a+# whitelist, and the tree is reduced by an explicit walk over the six operators+# and the named functions below.+++def parse_expression(text: str) -> ast.Expression:+ """Parse a measurement model into a validated, bounded expression tree."""++ if not isinstance(text, str) or not text.strip():+ raise CliError("expression must be a non-empty string")+ if len(text) > MAX_EXPRESSION_CHARS:+ raise CliError(+ f"expression is {len(text)} characters; limit is {MAX_EXPRESSION_CHARS}"+ )+ try:+ tree = ast.parse(text, mode="eval")+ except (SyntaxError, ValueError) as exc:+ raise CliError(f"cannot parse expression: {exc}") from exc++ nodes = list(ast.walk(tree))+ if len(nodes) > MAX_EXPRESSION_NODES:+ raise CliError(+ f"expression has {len(nodes)} nodes; limit is {MAX_EXPRESSION_NODES}"+ )+ for node in nodes:+ if not isinstance(node, _ALLOWED_NODES):+ raise CliError(+ f"expression may not contain {type(node).__name__}; allowed syntax is "+ "names, numbers, + - * / **, and whitelisted functions"+ )+ if isinstance(node, ast.Constant) and not isinstance(node.value, (int, float)):+ raise CliError("expression constants must be numbers")+ if isinstance(node, ast.Name):+ if node.id.startswith("_"):+ raise CliError("expression names must not start with an underscore")+ if isinstance(node, ast.Call):+ if not isinstance(node.func, ast.Name):+ raise CliError("only direct calls to whitelisted functions are allowed")+ if node.func.id not in ALLOWED_FUNCTIONS:+ allowed = ", ".join(ALLOWED_FUNCTIONS)+ raise CliError(+ f"function {node.func.id!r} is not allowed; allowed: {allowed}"+ )+ if node.keywords:+ raise CliError("function calls may not use keyword arguments")+ if not 1 <= len(node.args) <= 2:+ raise CliError("functions accept one or two positional arguments")+ if isinstance(node, ast.BinOp) and isinstance(node.op, ast.Pow):+ exponent = node.right+ if isinstance(exponent, ast.Constant) and isinstance(+ exponent.value, (int, float)+ ):+ if abs(float(exponent.value)) > MAX_LITERAL_EXPONENT:+ raise CliError(+ f"literal exponents are limited to +/-{MAX_LITERAL_EXPONENT}"+ )++ _check_depth(tree.body, 1)+ return tree+++def _check_depth(node: ast.AST, depth: int) -> None:+ """Reject deeply nested expressions."""++ if depth > MAX_EXPRESSION_DEPTH:+ raise CliError(f"expression nesting exceeds {MAX_EXPRESSION_DEPTH} levels")+ for child in ast.iter_child_nodes(node):+ _check_depth(child, depth + 1)+++def expression_variables(tree: ast.Expression) -> list[str]:+ """Return the free variable names of a parsed expression, in sorted order."""++ called = {+ node.func.id+ for node in ast.walk(tree)+ if isinstance(node, ast.Call) and isinstance(node.func, ast.Name)+ }+ names = {+ node.id+ for node in ast.walk(tree)+ if isinstance(node, ast.Name)+ and node.id not in called+ and node.id not in ALLOWED_CONSTANTS+ }+ if len(names) > MAX_VARIABLES:+ raise CliError(f"expression uses more than {MAX_VARIABLES} variables")+ return sorted(names)+++def reduce_expression(+ tree: ast.Expression,+ variables: dict[str, Any],+ functions: dict[str, Callable[..., Any]],+) -> Any:+ """Reduce a validated expression tree against concrete values."""++ def walk(node: ast.AST) -> Any:+ if isinstance(node, ast.Expression):+ return walk(node.body)+ if isinstance(node, ast.Constant):+ return node.value+ if isinstance(node, ast.Name):+ if node.id in variables:+ return variables[node.id]+ if node.id in ALLOWED_CONSTANTS:+ return ALLOWED_CONSTANTS[node.id]+ raise CliError(f"no value supplied for variable {node.id!r}")+ if isinstance(node, ast.UnaryOp):+ operand = walk(node.operand)+ return -operand if isinstance(node.op, ast.USub) else +operand+ if isinstance(node, ast.BinOp):+ left = walk(node.left)+ right = walk(node.right)+ if isinstance(node.op, ast.Add):+ return left + right+ if isinstance(node.op, ast.Sub):+ return left - right+ if isinstance(node.op, ast.Mult):+ return left * right+ if isinstance(node.op, ast.Div):+ return left / right+ return left**right+ if isinstance(node, ast.Call):+ name = node.func.id # type: ignore[union-attr]+ handler = functions.get(name)+ if handler is None:+ raise CliError(f"function {name!r} is unavailable in this mode")+ return handler(*[walk(argument) for argument in node.args])+ raise CliError(f"unsupported expression node {type(node).__name__}")++ try:+ return walk(tree)+ except CliError:+ raise+ except ZeroDivisionError as exc:+ raise CliError("expression divides by zero at the supplied values") from exc+ except (ArithmeticError, ValueError, TypeError) as exc:+ raise CliError(f"cannot compute the expression: {exc}") from exc+++def scalar_functions() -> dict[str, Callable[..., Any]]:+ """Return uncertainty-aware scalar functions with analytic derivatives."""++ try:+ from uncertainties import umath+ except ImportError as exc:+ raise CliError(+ f"the uncertainties package is unavailable; install with `{PINNED_INSTALL}`"+ ) from exc+ mapping: dict[str, Callable[..., Any]] = {"abs": abs}+ for name in ALLOWED_FUNCTIONS:+ if name == "abs":+ continue+ handler = getattr(umath, name, None)+ if handler is not None:+ mapping[name] = handler+ return mapping+++def array_functions() -> dict[str, Callable[..., Any]]:+ """Return vectorized functions for Monte Carlo sampling."""++ try:+ import numpy as np+ except ImportError as exc:+ raise CliError(+ f"NumPy is unavailable; install with `{PINNED_INSTALL}`"+ ) from exc+ mapping: dict[str, Callable[..., Any]] = {+ "abs": np.abs,+ "acos": np.arccos,+ "acosh": np.arccosh,+ "asin": np.arcsin,+ "asinh": np.arcsinh,+ "atan": np.arctan,+ "atan2": np.arctan2,+ "atanh": np.arctanh,+ "cos": np.cos,+ "cosh": np.cosh,+ "degrees": np.degrees,+ "exp": np.exp,+ "expm1": np.expm1,+ "fabs": np.fabs,+ "hypot": np.hypot,+ "log": np.log,+ "log10": np.log10,+ "log1p": np.log1p,+ "radians": np.radians,+ "sin": np.sin,+ "sinh": np.sinh,+ "sqrt": np.sqrt,+ "tan": np.tan,+ "tanh": np.tanh,+ }++ def erf(value: Any) -> Any:+ try:+ from scipy.special import erf as scipy_erf+ except ImportError as exc:+ raise CliError(+ f"erf needs SciPy; install with `{PINNED_INSTALL}`"+ ) from exc+ return scipy_erf(value)++ mapping["erf"] = erf+ return mapping+++def json_dof(value: float | None) -> float | None:+ """Render infinite degrees of freedom as JSON null."""++ if value is None or not math.isfinite(value):+ return None+ return float(value)+++def format_dof(value: float | None) -> str:+ """Render degrees of freedom for a report table."""++ if value is None or not math.isfinite(value):+ return "inf"+ return f"{value:g}"+++def welch_satterthwaite(+ combined_uncertainty: float, terms: Iterable[tuple[float, float]]+) -> float:+ """Return effective degrees of freedom from |c_i u_i| and nu_i pairs.++ JCGM 100:2008 equation G.2b. Components with infinite degrees of freedom+ contribute nothing to the denominator.+ """++ if combined_uncertainty <= 0:+ return math.inf+ denominator = 0.0+ for contribution, dof in terms:+ if not math.isfinite(dof) or dof <= 0:+ continue+ denominator += (contribution**4) / dof+ if denominator <= 0:+ return math.inf+ return (combined_uncertainty**4) / denominator+++def coverage_factor(dof: float, coverage_probability: float) -> float:+ """Return the Student-t coverage factor for the given effective dof."""++ if not 0 < coverage_probability < 1:+ raise CliError("coverage probability must be strictly between 0 and 1")+ tail = 0.5 * (1.0 + coverage_probability)+ if not math.isfinite(dof):+ from statistics import NormalDist++ return float(NormalDist().inv_cdf(tail))+ try:+ from scipy.stats import t as student_t+ except ImportError as exc:+ raise CliError(+ "a Student-t coverage factor needs SciPy; install with "+ f"`{PINNED_INSTALL}`, or supply infinite degrees of freedom"+ ) from exc+ return float(student_t.ppf(tail, dof))+++def numerical_tolerance(combined_uncertainty: float, significant_digits: int) -> float:+ """Return the JCGM 101:2008 clause 8 numerical tolerance for u_c.++ Writing u_c to `significant_digits` digits as c x 10**exponent, the+ tolerance is half of the unit in that last retained digit.+ """++ if significant_digits not in (1, 2):+ raise CliError("numerical tolerance is defined for 1 or 2 significant digits")+ if combined_uncertainty <= 0 or not math.isfinite(combined_uncertainty):+ raise CliError("combined standard uncertainty must be finite and positive")+ # audit-units: ignore UNC003 -- u_c is a plain float here, never a ufloat+ decade = math.log10(combined_uncertainty)+ exponent = math.floor(decade) - (significant_digits - 1)+ return 0.5 * (10.0**exponent)+++def shortest_coverage_interval(+ sorted_sample: Any, coverage_probability: float+) -> tuple[float, float]:+ """Return the shortest coverage interval of a sorted Monte Carlo sample."""++ try:+ import numpy as np+ except ImportError as exc:+ raise CliError(+ f"NumPy is unavailable; install with `{PINNED_INSTALL}`"+ ) from exc+ total = int(sorted_sample.size)+ if total < 2:+ raise CliError("a coverage interval needs at least two Monte Carlo trials")+ inside = int(math.floor(coverage_probability * total))+ inside = min(max(inside, 1), total - 1)+ lower = sorted_sample[: total - inside]+ upper = sorted_sample[inside:]+ widths = upper - lower+ index = int(np.argmin(widths))+ return float(lower[index]), float(upper[index])diff --git a/skills/uncertainty-and-units/scripts/audit_units.py b/skills/uncertainty-and-units/scripts/audit_units.pynew file mode 100644index 00000000..ae05e699--- /dev/null+++ b/skills/uncertainty-and-units/scripts/audit_units.py@@ -0,0 +1,575 @@+#!/usr/bin/env python3+"""Scan Python source for the unit and uncertainty defects that stay silent.++Every rule here corresponds to code that runs, produces a plausible number, and+is wrong: a stripped unit, a rescaled covariance matrix, a destroyed+correlation, a population standard deviation used as a standard uncertainty.+The scan is static - it parses the file and never imports or runs it.+"""++# This module *is* the rule tables: it names every logarithmic unit and lists+# every CODATA value it looks for, so scanning it flags its own data. The+# directive below is the same mechanism any caller has, and suppressions are+# counted in the report rather than hidden.+# audit-units: ignore-file CONST001, UNIT004++from __future__ import annotations++import argparse+import ast+import math+import re+import sys+from decimal import Decimal+from typing import Any++import _common+from _common import CliError+++SEVERITY_ORDER = {"low": 1, "medium": 2, "high": 3}+MAX_FILES = 50++# `# audit-units: ignore [RULE, ...]` silences the rules on that line;+# `# audit-units: ignore-file RULE[, ...]` silences them for the whole module.+# Both forms are counted in the report, so a suppression stays visible.+SUPPRESSION = re.compile(+ r"#\s*audit-units:\s*ignore(?P<scope>-file)?(?P<rules>[\sA-Z0-9,]*)"+)++OFFSET_UNIT_TOKENS = {+ "degC",+ "degF",+ "celsius",+ "fahrenheit",+ "degree_Celsius",+ "degree_Fahrenheit",+ "degreeC",+ "degreeF",+}+LOGARITHMIC_UNIT_TOKENS = {+ "dB",+ "dBm",+ "dBW",+ "dBu",+ "dBV",+ "dBi",+ "decibel",+ "decibelmilliwatt",+ "decibelwatt",+}+ELEMENTARY_FUNCTIONS = {+ "acos",+ "acosh",+ "asin",+ "asinh",+ "atan",+ "atan2",+ "atanh",+ "cos",+ "cosh",+ "degrees",+ "erf",+ "exp",+ "expm1",+ "fabs",+ "hypot",+ "log",+ "log10",+ "log1p",+ "radians",+ "sin",+ "sinh",+ "sqrt",+ "tan",+ "tanh",+}+UNIT_PRESERVING_CALLS = {"to", "to_base_units", "to_reduced_units", "m_as", "ito"}++# CODATA 2022 recommended values, plus the constants the 2019 SI redefinition+# fixed exactly. A literal within one part in a thousand of one of these is+# almost always a constant somebody typed from memory. Every accessor below was+# checked against scipy.constants 1.18.0, whose default data set is CODATA 2022.+KNOWN_CONSTANTS: tuple[tuple[float, str, str], ...] = (+ (299792458.0, "speed of light in vacuum", "value('speed of light in vacuum')"),+ (6.62607015e-34, "Planck constant", "value('Planck constant')"),+ (1.054571817e-34, "reduced Planck constant", "value('reduced Planck constant')"),+ (1.602176634e-19, "elementary charge", "value('elementary charge')"),+ (1.380649e-23, "Boltzmann constant", "value('Boltzmann constant')"),+ (6.02214076e23, "Avogadro constant", "value('Avogadro constant')"),+ (8.314462618, "molar gas constant", "value('molar gas constant')"),+ (9.1093837139e-31, "electron mass", "value('electron mass')"),+ (1.67262192595e-27, "proton mass", "value('proton mass')"),+ (1.66053906892e-27, "atomic mass constant", "value('atomic mass constant')"),+ (5.670374419e-8, "Stefan-Boltzmann constant", "value('Stefan-Boltzmann constant')"),+ (+ 6.67430e-11,+ "Newtonian constant of gravitation",+ "value('Newtonian constant of gravitation')",+ ),+ (+ 9.80665,+ "standard acceleration of gravity",+ "value('standard acceleration of gravity')",+ ),+ (+ 8.8541878188e-12,+ "vacuum electric permittivity",+ "value('vacuum electric permittivity')",+ ),+ (+ 1.25663706127e-6,+ "vacuum magnetic permeability",+ "value('vacuum mag. permeability')",+ ),+ (5.29177210544e-11, "Bohr radius", "value('Bohr radius')"),+ (7.2973525643e-3, "fine-structure constant", "value('fine-structure constant')"),+ (10973731.568157, "Rydberg constant", "value('Rydberg constant')"),+ (96485.33212, "Faraday constant", "value('Faraday constant')"),+ (101325.0, "standard atmosphere", "value('standard atmosphere')"),+ (273.15, "zero of the Celsius scale", "zero_Celsius"),+)+++def significant_digits(value: float) -> int:+ """Count the significant digits in a float's shortest representation."""++ digits = list(Decimal(repr(abs(value))).as_tuple().digits)+ while len(digits) > 1 and digits[-1] == 0:+ digits.pop()+ while len(digits) > 1 and digits[0] == 0:+ digits.pop(0)+ return len(digits)+++def parse_suppressions(source: str) -> tuple[dict[int, set[str]], set[str]]:+ """Return per-line and whole-file rule suppressions.++ A trailing directive applies to its own line. A directive alone on a line+ applies to the next line, so a long statement does not have to carry the+ comment. Naming no rule, or `ALL`, suppresses every rule and is recorded as+ the `*` sentinel.+ """++ per_line: dict[int, set[str]] = {}+ whole_file: set[str] = set()+ for number, line in enumerate(source.splitlines(), start=1):+ match = SUPPRESSION.search(line)+ if match is None:+ continue+ rules = {+ item.strip()+ for item in match.group("rules").replace(" ", ",").split(",")+ if item.strip()+ }+ if not rules or "ALL" in rules:+ rules = {"*"}+ if match.group("scope"):+ whole_file |= rules+ continue+ target = number + 1 if line.lstrip().startswith("#") else number+ existing = per_line.get(target)+ if existing == {"*"} or rules == {"*"}:+ per_line[target] = {"*"}+ else:+ per_line[target] = (existing or set()) | rules+ return per_line, whole_file+++def _tokens(text: str) -> set[str]:+ """Split a unit string into candidate unit tokens."""++ current: list[str] = []+ found: set[str] = set()+ for character in text:+ if character.isalnum() or character == "_":+ current.append(character)+ else:+ if current:+ found.add("".join(current))+ current = []+ if current:+ found.add("".join(current))+ return found+++class Auditor(ast.NodeVisitor):+ """Collect unit and uncertainty findings from one parsed module."""++ def __init__(self, source: str, filename: str) -> None:+ self.filename = filename+ self.findings: list[dict[str, Any]] = []+ self.registry_sites: list[ast.AST] = []+ self.imports_uncertainties = False+ self.saw_delta_unit = False+ self.saw_offset_autoconvert = "autoconvert_offset_to_baseunit" in source+ self.line_suppressions, self.file_suppressions = parse_suppressions(source)+ self.suppressed = 0++ def report(+ self,+ node: ast.AST,+ rule: str,+ severity: str,+ message: str,+ remedy: str,+ ) -> None:+ self.findings.append(+ {+ "rule": rule,+ "severity": severity,+ "line": getattr(node, "lineno", 0),+ "column": getattr(node, "col_offset", 0) + 1,+ "message": message,+ "remedy": remedy,+ }+ )++ # --- imports -----------------------------------------------------------++ def visit_Import(self, node: ast.Import) -> None:+ for alias in node.names:+ if alias.name.split(".", 1)[0] == "uncertainties":+ self.imports_uncertainties = True+ self.generic_visit(node)++ def visit_ImportFrom(self, node: ast.ImportFrom) -> None:+ if node.module and node.module.split(".", 1)[0] == "uncertainties":+ self.imports_uncertainties = True+ self.generic_visit(node)++ # --- string literals ---------------------------------------------------++ def visit_Constant(self, node: ast.Constant) -> None:+ if isinstance(node.value, str):+ tokens = _tokens(node.value)+ if any(token.startswith("delta_") for token in tokens):+ self.saw_delta_unit = True+ if tokens & OFFSET_UNIT_TOKENS:+ self.report(+ node,+ "UNIT002",+ "medium",+ "offset temperature unit used; pint refuses to multiply or add "+ "degC/degF quantities because the operation is ambiguous",+ "express temperature differences in delta_degC, or build the "+ "registry with UnitRegistry(autoconvert_offset_to_baseunit=True)",+ )+ if tokens & LOGARITHMIC_UNIT_TOKENS:+ self.report(+ node,+ "UNIT004",+ "medium",+ "logarithmic unit used; adding two pint quantities in dB or dBm "+ "multiplies the underlying linear quantities instead of summing "+ "them, and reports the product in squared base units",+ "convert to a linear unit before arithmetic, then convert back",+ )+ elif isinstance(node.value, (int, float)) and not isinstance(node.value, bool):+ self._check_constant_literal(node, float(node.value))+ self.generic_visit(node)++ def _check_constant_literal(self, node: ast.Constant, value: float) -> None:+ if value == 0 or not math.isfinite(value):+ return+ if significant_digits(value) < 3:+ return+ for reference, description, accessor in KNOWN_CONSTANTS:+ if abs(value - reference) <= 1e-3 * abs(reference):+ self.report(+ node,+ "CONST001",+ "low",+ f"hard-coded literal matches the {description}; the recommended "+ "value and its uncertainty change between CODATA releases",+ f"use scipy.constants.{accessor}, with scipy.constants.precision "+ "for the relative standard uncertainty (0.0 when the SI fixes "+ "the constant exactly)",+ )+ return++ # --- attribute access --------------------------------------------------++ def visit_Attribute(self, node: ast.Attribute) -> None:+ if node.attr == "magnitude":+ source = node.value+ preserved = (+ isinstance(source, ast.Call)+ and isinstance(source.func, ast.Attribute)+ and source.func.attr in UNIT_PRESERVING_CALLS+ )+ if not preserved:+ self.report(+ node,+ "UNIT003",+ "high",+ ".magnitude strips the unit without stating which one, so the "+ "number that comes out depends on whatever the quantity happened "+ "to be carrying",+ "call .to('unit').magnitude or .m_as('unit') so the scale is "+ "fixed at the point of extraction",+ )+ self.generic_visit(node)++ # --- calls -------------------------------------------------------------++ def visit_Call(self, node: ast.Call) -> None:+ name = self._call_name(node)+ keywords = {keyword.arg for keyword in node.keywords if keyword.arg}++ if name == "UnitRegistry":+ self.registry_sites.append(node)++ if name == "curve_fit" and "absolute_sigma" not in keywords:+ self.report(+ node,+ "UNC001",+ "high",+ "curve_fit rescales the covariance matrix by the reduced chi-square "+ "unless absolute_sigma=True, so parameter uncertainties silently "+ "absorb the goodness of fit",+ "pass absolute_sigma=True when sigma holds real standard "+ "uncertainties; leave it False only for relative weights",+ )++ if name in {"std", "var", "nanstd", "nanvar"} and "ddof" not in keywords:+ if self._is_numpy_call(node):+ self.report(+ node,+ "UNC002",+ "medium",+ f"numpy {name} defaults to ddof=0, which is the population "+ "spread; a Type A standard uncertainty needs the sample "+ "estimate",+ "pass ddof=1, and divide by sqrt(n) as well when you want the "+ "standard uncertainty of the mean rather than of one reading",+ )++ if name == "ufloat":+ for argument in node.args + [kw.value for kw in node.keywords]:+ if any(+ isinstance(inner, ast.Attribute)+ and inner.attr in {"nominal_value", "std_dev", "n", "s"}+ for inner in ast.walk(argument)+ ):+ self.report(+ node,+ "UNC004",+ "high",+ "rebuilding a ufloat from another variable's nominal_value "+ "and std_dev creates an independent variable, discarding "+ "every correlation the original carried",+ "pass the existing variable through, or rebuild the whole "+ "set with correlated_values(values, covariance_matrix)",+ )+ break++ if self.imports_uncertainties and name in ELEMENTARY_FUNCTIONS:+ module = self._call_module(node)+ if module in {"math", "np", "numpy"}:+ self.report(+ node,+ "UNC003",+ "medium",+ f"{module}.{name} has no derivative rule for an uncertain value; "+ "on a scalar it raises TypeError, and on an object array it "+ "fails in the ufunc loop",+ "use uncertainties.umath for scalars and "+ "uncertainties.unumpy for arrays",+ )++ self.generic_visit(node)++ @staticmethod+ def _call_name(node: ast.Call) -> str | None:+ if isinstance(node.func, ast.Name):+ return node.func.id+ if isinstance(node.func, ast.Attribute):+ return node.func.attr+ return None++ @staticmethod+ def _call_module(node: ast.Call) -> str | None:+ if isinstance(node.func, ast.Attribute) and isinstance(node.func.value, ast.Name):+ return node.func.value.id+ return None++ @staticmethod+ def _is_numpy_call(node: ast.Call) -> bool:+ return (+ isinstance(node.func, ast.Attribute)+ and isinstance(node.func.value, ast.Name)+ and node.func.value.id in {"np", "numpy"}+ )++ def finish(self) -> list[dict[str, Any]]:+ """Emit the findings that need whole-module context."""++ if len(self.registry_sites) > 1:+ for node in self.registry_sites[1:]:+ self.report(+ node,+ "UNIT001",+ "medium",+ "a second UnitRegistry is constructed in this module; quantities "+ "from different registries raise ValueError on any shared "+ "operation",+ "build one registry and share it, or call "+ "pint.get_application_registry() everywhere",+ )+ if self.saw_offset_autoconvert or self.saw_delta_unit:+ self.findings = [+ finding for finding in self.findings if finding["rule"] != "UNIT002"+ ]+ kept = [finding for finding in self.findings if not self._suppressed(finding)]+ self.suppressed = len(self.findings) - len(kept)+ kept.sort(key=lambda item: (item["line"], item["column"], item["rule"]))+ self.findings = kept+ return kept++ def _suppressed(self, finding: dict[str, Any]) -> bool:+ """Report whether a directive comment silences this finding."""++ if "*" in self.file_suppressions or finding["rule"] in self.file_suppressions:+ return True+ rules = self.line_suppressions.get(finding["line"])+ if rules is None:+ return False+ return "*" in rules or finding["rule"] in rules+++def audit_source(source: str, filename: str) -> dict[str, Any]:+ """Parse one module and return its findings and suppression count."""++ try:+ tree = ast.parse(source, filename=filename)+ except SyntaxError as exc:+ raise CliError(f"{filename}: cannot parse as Python: {exc}") from exc+ auditor = Auditor(source, filename)+ auditor.visit(tree)+ findings = auditor.finish()+ return {"findings": findings, "suppressed": auditor.suppressed}+++def render_markdown(document: dict[str, Any]) -> str:+ """Render findings grouped by file."""++ lines = ["# Unit and uncertainty audit", ""]+ counts = document["counts"]+ lines.append(+ f"{counts['total']} findings across {document['files_scanned']} files "+ f"({counts['high']} high, {counts['medium']} medium, {counts['low']} low), "+ f"{counts['suppressed']} suppressed by directive comments."+ )+ for entry in document["results"]:+ lines += ["", f"## {entry['file']}", ""]+ if not entry["findings"]:+ lines.append(f"No findings ({entry['suppressed']} suppressed).")+ continue+ lines.append("| Line | Rule | Severity | Finding |")+ lines.append("| --- | --- | --- | --- |")+ for finding in entry["findings"]:+ lines.append(+ f"| {finding['line']} | {finding['rule']} | {finding['severity']} | "+ f"{finding['message']} |"+ )+ lines.append("")+ for finding in entry["findings"]:+ lines.append(+ f"- **{finding['rule']}** (line {finding['line']}): "+ f"{finding['remedy']}"+ )+ return "\n".join(lines) + "\n"+++def build_parser() -> argparse.ArgumentParser:+ parser = argparse.ArgumentParser(+ description=(+ "Statically audit Python sources for stripped units, offset-temperature "+ "arithmetic, broken uncertainty propagation, and hard-coded constants."+ )+ )+ parser.add_argument(+ "--input",+ action="append",+ default=[],+ required=True,+ help="Python file to audit; repeatable",+ )+ parser.add_argument(+ "--fail-on",+ choices=("none", "low", "medium", "high"),+ default="high",+ help="lowest severity that makes the command exit non-zero (default high)",+ )+ parser.add_argument("--format", choices=("json", "markdown"), default="json")+ parser.add_argument("--output", help="write the report to this file")+ parser.add_argument(+ "--force", action="store_true", help="overwrite an existing output file"+ )+ return parser+++def run(arguments: argparse.Namespace) -> dict[str, Any]:+ """Audit every requested file and summarize the findings."""++ if len(arguments.input) > MAX_FILES:+ raise CliError(f"at most {MAX_FILES} files can be audited in one run")+ results = []+ counts = {"total": 0, "low": 0, "medium": 0, "high": 0, "suppressed": 0}+ for item in arguments.input:+ source, path = _common.read_text_file(item, suffixes=(".py",))+ audited = audit_source(source, path.name)+ results.append(+ {+ "file": str(path),+ "findings": audited["findings"],+ "suppressed": audited["suppressed"],+ }+ )+ counts["suppressed"] += audited["suppressed"]+ for finding in audited["findings"]:+ counts["total"] += 1+ counts[finding["severity"]] += 1+ return {+ "files_scanned": len(results),+ "counts": counts,+ "results": results,+ }+++def exit_code(document: dict[str, Any], fail_on: str) -> int:+ """Return 1 when a finding meets the configured severity threshold."""++ if fail_on == "none":+ return 0+ threshold = SEVERITY_ORDER[fail_on]+ for entry in document["results"]:+ for finding in entry["findings"]:+ if SEVERITY_ORDER[finding["severity"]] >= threshold:+ return 1+ return 0+++def main(argv: list[str] | None = None) -> int:+ arguments = build_parser().parse_args(argv)+ try:+ document = run(arguments)+ if arguments.format == "markdown":+ _common.emit_text(+ render_markdown(document),+ output=arguments.output,+ force=arguments.force,+ )+ else:+ _common.emit_json(+ document, output=arguments.output, force=arguments.force+ )+ except CliError as error:+ print(f"error: {error}", file=sys.stderr)+ return 2+ return exit_code(document, arguments.fail_on)+++if __name__ == "__main__":+ raise SystemExit(main())diff --git a/skills/uncertainty-and-units/scripts/check_plausibility.py b/skills/uncertainty-and-units/scripts/check_plausibility.pynew file mode 100644index 00000000..1f735922--- /dev/null+++ b/skills/uncertainty-and-units/scripts/check_plausibility.py@@ -0,0 +1,894 @@+#!/usr/bin/env python3+"""Test a set of quantities against dimensionless groups and known physical scales.++Unit bookkeeping proves a calculation is dimensionally consistent. It cannot+say whether the answer is physically possible. A cell 2 m across, a Reynolds+number of 4e7 in a capillary, and a diffusion time of 300 years across a+membrane are all dimensionally impeccable.++This CLI closes that gap three ways. It evaluates named dimensionless groups+and reports the regime they place the system in; it computes characteristic+scales such as a diffusion time or a settling velocity; and it compares a+quantity against a curated band of values that quantity is actually observed to+take. Every expression is checked for dimensional consistency first, so passing+a kinematic viscosity where a dynamic one belongs is caught before any number+is reported.++Constants come from scipy.constants at run time rather than from literals in+this file, so they track the CODATA release SciPy ships.+"""++from __future__ import annotations++import argparse+import math+import sys+from typing import Any++import _common+from _common import CliError+from convert_units import build_registry, checked_unit+++MAX_ENTRIES = 64++# Available to every expression below without being supplied on the command+# line. The unit strings state what scipy.constants documents each value in.+CONSTANT_UNITS: tuple[tuple[str, str, str], ...] = (+ ("k_B", "k", "J/K"),+ ("N_A", "N_A", "1/mol"),+ ("R_gas", "R", "J/(mol*K)"),+ ("c_light", "c", "m/s"),+ ("h_planck", "h", "J*s"),+ ("e_charge", "e", "C"),+ ("epsilon_0", "epsilon_0", "F/m"),+ ("g_earth", "g", "m/s**2"),+ ("sigma_sb", "sigma", "W/(m**2*K**4)"),+ ("m_u", "m_u", "kg"),+)++# Each regime is [lower, upper, label]; null is an open end.+GROUPS: tuple[dict[str, Any], ...] = (+ {+ "name": "reynolds",+ "symbol": "Re",+ "expression": "density * velocity * length / viscosity",+ "inputs": {+ "density": "[mass] / [length] ** 3",+ "velocity": "[length] / [time]",+ "length": "[length]",+ "viscosity": "[mass] / ([length] * [time])",+ },+ "regimes": [+ [None, 2300.0, "laminar (circular pipe, length = diameter)"],+ [2300.0, 4000.0, "transitional"],+ [4000.0, None, "turbulent"],+ ],+ "note": "viscosity is dynamic; the thresholds are pipe-flow values and "+ "differ for external and open-channel flow",+ "source": "White, Fluid Mechanics, 8th ed., ch. 6",+ },+ {+ "name": "peclet",+ "symbol": "Pe",+ "expression": "velocity * length / diffusivity",+ "inputs": {+ "velocity": "[length] / [time]",+ "length": "[length]",+ "diffusivity": "[length] ** 2 / [time]",+ },+ "regimes": [+ [None, 1.0, "diffusion dominates transport"],+ [1.0, None, "advection dominates transport"],+ ],+ "note": "mass-transfer Peclet number; the thermal form replaces the "+ "diffusivity with the thermal diffusivity",+ "source": "Deen, Analysis of Transport Phenomena, 2nd ed., ch. 9",+ },+ {+ "name": "damkohler",+ "symbol": "Da",+ "expression": "rate_constant * length / velocity",+ "inputs": {+ "rate_constant": "1 / [time]",+ "length": "[length]",+ "velocity": "[length] / [time]",+ },+ "regimes": [+ [None, 0.1, "transport-limited; reaction barely proceeds in transit"],+ [0.1, 10.0, "reaction and transport comparable"],+ [10.0, None, "reaction-limited; reagent consumed near the inlet"],+ ],+ "note": "first-order Damkohler number Da_I, for a first-order rate constant",+ "source": "Fogler, Elements of Chemical Reaction Engineering, 6th ed.",+ },+ {+ "name": "knudsen",+ "symbol": "Kn",+ "expression": "mean_free_path / length",+ "inputs": {"mean_free_path": "[length]", "length": "[length]"},+ "regimes": [+ [None, 0.01, "continuum; Navier-Stokes with no-slip applies"],+ [0.01, 0.1, "slip flow; no-slip boundary condition fails"],+ [0.1, 10.0, "transition; continuum treatment invalid"],+ [10.0, None, "free molecular"],+ ],+ "note": "compute the mean free path with the mean_free_path_gas scale",+ "source": "Karniadakis et al., Microflows and Nanoflows, ch. 1",+ },+ {+ "name": "mach",+ "symbol": "Ma",+ "expression": "velocity / sound_speed",+ "inputs": {+ "velocity": "[length] / [time]",+ "sound_speed": "[length] / [time]",+ },+ "regimes": [+ [None, 0.3, "incompressible treatment valid to about 5% in density"],+ [0.3, 0.8, "subsonic compressible"],+ [0.8, 1.2, "transonic"],+ [1.2, None, "supersonic"],+ ],+ "note": "the 0.3 threshold is a 5% density-change criterion, not a hard limit",+ "source": "Anderson, Modern Compressible Flow, 4th ed., ch. 1",+ },+ {+ "name": "womersley",+ "symbol": "Wo",+ "expression": "radius * sqrt(angular_frequency * density / viscosity)",+ "inputs": {+ "radius": "[length]",+ "angular_frequency": "1 / [time]",+ "density": "[mass] / [length] ** 3",+ "viscosity": "[mass] / ([length] * [time])",+ },+ "regimes": [+ [None, 1.0, "quasi-steady; velocity profile stays parabolic"],+ [1.0, 10.0, "transitional; profile flattens and lags pressure"],+ [10.0, None, "inertia-dominated plug flow"],+ ],+ "note": "angular frequency, not frequency: use 2*pi*f",+ "source": "Womersley, J. Physiol. 127:553 (1955)",+ },+ {+ "name": "capillary",+ "symbol": "Ca",+ "expression": "viscosity * velocity / surface_tension",+ "inputs": {+ "viscosity": "[mass] / ([length] * [time])",+ "velocity": "[length] / [time]",+ "surface_tension": "[mass] / [time] ** 2",+ },+ "regimes": [+ [None, 0.001, "interface shape set by surface tension alone"],+ [0.001, None, "viscous stress deforms the interface"],+ ],+ "note": "governs droplet breakup and wetting in microfluidics",+ "source": "Bruus, Theoretical Microfluidics, ch. 7",+ },+ {+ "name": "weber",+ "symbol": "We",+ "expression": "density * velocity ** 2 * length / surface_tension",+ "inputs": {+ "density": "[mass] / [length] ** 3",+ "velocity": "[length] / [time]",+ "length": "[length]",+ "surface_tension": "[mass] / [time] ** 2",+ },+ "regimes": [+ [None, 1.0, "surface tension holds the drop together"],+ [1.0, 12.0, "deformation without breakup"],+ [12.0, None, "aerodynamic breakup"],+ ],+ "note": "the critical Weber number for bag breakup is about 12",+ "source": "Pilch and Erdman, Int. J. Multiphase Flow 13:741 (1987)",+ },+ {+ "name": "bond",+ "symbol": "Bo",+ "expression": "density_difference * g_earth * length ** 2 / surface_tension",+ "inputs": {+ "density_difference": "[mass] / [length] ** 3",+ "length": "[length]",+ "surface_tension": "[mass] / [time] ** 2",+ },+ "regimes": [+ [None, 1.0, "surface tension dominates gravity"],+ [1.0, None, "gravity dominates; the interface flattens"],+ ],+ "note": "also called the Eotvos number",+ "source": "de Gennes et al., Capillarity and Wetting Phenomena, ch. 2",+ },+ {+ "name": "stokes_number",+ "symbol": "Stk",+ "expression": (+ "particle_density * particle_diameter ** 2 * velocity "+ "/ (18 * viscosity * length)"+ ),+ "inputs": {+ "particle_density": "[mass] / [length] ** 3",+ "particle_diameter": "[length]",+ "velocity": "[length] / [time]",+ "viscosity": "[mass] / ([length] * [time])",+ "length": "[length]",+ },+ "regimes": [+ [None, 0.1, "particles follow streamlines; tracer assumption holds"],+ [0.1, 1.0, "partial slip; sampling bias likely"],+ [1.0, None, "ballistic; particles leave the flow and impact"],+ ],+ "note": "the tracer assumption behind particle image velocimetry needs "+ "Stk well below 0.1",+ "source": "Raffel et al., Particle Image Velocimetry, 3rd ed., ch. 2",+ },+ {+ "name": "biot",+ "symbol": "Bi",+ "expression": "heat_transfer_coefficient * length / conductivity",+ "inputs": {+ "heat_transfer_coefficient": "[mass] / ([time] ** 3 * [temperature])",+ "length": "[length]",+ "conductivity": "[length] * [mass] / ([time] ** 3 * [temperature])",+ },+ "regimes": [+ [None, 0.1, "lumped capacitance valid; body is nearly isothermal"],+ [0.1, None, "internal gradients matter; solve the conduction equation"],+ ],+ "note": "length is volume divided by surface area for an irregular body",+ "source": "Incropera et al., Fundamentals of Heat and Mass Transfer, ch. 5",+ },+ {+ "name": "fourier",+ "symbol": "Fo",+ "expression": "thermal_diffusivity * time / length ** 2",+ "inputs": {+ "thermal_diffusivity": "[length] ** 2 / [time]",+ "time": "[time]",+ "length": "[length]",+ },+ "regimes": [+ [None, 0.05, "early transient; semi-infinite solution applies"],+ [0.05, 1.0, "transient"],+ [1.0, None, "effectively equilibrated"],+ ],+ "note": "dimensionless time for conduction; the same form with a mass "+ "diffusivity governs diffusion",+ "source": "Incropera et al., Fundamentals of Heat and Mass Transfer, ch. 5",+ },+ {+ "name": "schmidt",+ "symbol": "Sc",+ "expression": "viscosity / (density * diffusivity)",+ "inputs": {+ "viscosity": "[mass] / ([length] * [time])",+ "density": "[mass] / [length] ** 3",+ "diffusivity": "[length] ** 2 / [time]",+ },+ "regimes": [+ [None, 10.0, "gases; momentum and mass diffuse comparably"],+ [10.0, None, "liquids; momentum diffuses far faster than solute"],+ ],+ "note": "about 1 for gases and 1e3 with small molecules in water",+ "source": "Cussler, Diffusion, 3rd ed., ch. 9",+ },+ {+ "name": "deborah",+ "symbol": "De",+ "expression": "relaxation_time / observation_time",+ "inputs": {"relaxation_time": "[time]", "observation_time": "[time]"},+ "regimes": [+ [None, 1.0, "material responds as a viscous liquid"],+ [1.0, None, "material responds elastically on this timescale"],+ ],+ "note": "the same material is a liquid or a solid depending on how long "+ "you watch it",+ "source": "Reiner, Physics Today 17:62 (1964)",+ },+)++SCALES: tuple[dict[str, Any], ...] = (+ {+ "name": "diffusion_time",+ "expression": "length ** 2 / diffusivity",+ "inputs": {"length": "[length]", "diffusivity": "[length] ** 2 / [time]"},+ "unit": "s",+ "note": "the L-squared scaling is why diffusion crosses a membrane in "+ "microseconds and a millimetre of tissue in minutes",+ "source": "Berg, Random Walks in Biology, ch. 2",+ },+ {+ "name": "thermal_diffusion_time",+ "expression": "length ** 2 / thermal_diffusivity",+ "inputs": {+ "length": "[length]",+ "thermal_diffusivity": "[length] ** 2 / [time]",+ },+ "unit": "s",+ "note": "same scaling with the thermal diffusivity k/(rho*c_p)",+ "source": "Incropera et al., Fundamentals of Heat and Mass Transfer, ch. 5",+ },+ {+ "name": "thermal_energy",+ "expression": "k_B * temperature",+ "inputs": {"temperature": "[temperature]"},+ "unit": "J",+ "note": "the energy scale every biological interaction is measured "+ "against; 4.14e-21 J at 300 K, or 2.5 kJ/mol",+ "source": "Phillips et al., Physical Biology of the Cell, 2nd ed., ch. 5",+ },+ {+ "name": "molar_thermal_energy",+ "expression": "R_gas * temperature",+ "inputs": {"temperature": "[temperature]"},+ "unit": "kJ/mol",+ "note": "a binding free energy below this is indistinguishable from "+ "thermal noise",+ "source": "Phillips et al., Physical Biology of the Cell, 2nd ed., ch. 6",+ },+ {+ "name": "stokes_settling_velocity",+ "expression": (+ "density_difference * g_earth * particle_diameter ** 2 "+ "/ (18 * viscosity)"+ ),+ "inputs": {+ "density_difference": "[mass] / [length] ** 3",+ "particle_diameter": "[length]",+ "viscosity": "[mass] / ([length] * [time])",+ },+ "unit": "m/s",+ "note": "valid only while the particle Reynolds number stays below about "+ "0.1; check it with the reynolds group",+ "source": "Batchelor, An Introduction to Fluid Dynamics, ch. 4",+ },+ {+ "name": "mean_free_path_gas",+ "expression": (+ "k_B * temperature / (sqrt(2) * pi * molecular_diameter ** 2 * pressure)"+ ),+ "inputs": {+ "temperature": "[temperature]",+ "molecular_diameter": "[length]",+ "pressure": "[mass] / ([length] * [time] ** 2)",+ },+ "unit": "m",+ "note": "hard-sphere estimate; the collision diameter of N2 is about 0.37 nm",+ "source": "Atkins and de Paula, Physical Chemistry, 12th ed., ch. 1",+ },+ {+ "name": "debye_length",+ "expression": (+ "sqrt(epsilon_0 * relative_permittivity * k_B * temperature "+ "/ (2 * N_A * e_charge ** 2 * ionic_strength))"+ ),+ "inputs": {+ "relative_permittivity": "",+ "temperature": "[temperature]",+ "ionic_strength": "[substance] / [length] ** 3",+ },+ "unit": "nm",+ "note": "for a symmetric monovalent electrolyte; about 0.96 nm at 100 mM "+ "and 0.7 nm at physiological ionic strength",+ "source": "Israelachvili, Intermolecular and Surface Forces, 3rd ed., ch. 14",+ },+ {+ "name": "capillary_length",+ "expression": "sqrt(surface_tension / (density * g_earth))",+ "inputs": {+ "surface_tension": "[mass] / [time] ** 2",+ "density": "[mass] / [length] ** 3",+ },+ "unit": "mm",+ "note": "below this size a drop is held by surface tension and does not "+ "puddle; about 2.7 mm for water",+ "source": "de Gennes et al., Capillarity and Wetting Phenomena, ch. 2",+ },+)++# Observed ranges, deliberately generous: a value outside one of these is worth+# a second look, not automatically wrong.+BANDS: tuple[dict[str, Any], ...] = (+ {"name": "bacterial_cell_diameter", "low": 0.2, "high": 10.0, "unit": "um",+ "source": "Milo and Phillips, Cell Biology by the Numbers, ch. 1"},+ {"name": "eukaryotic_cell_diameter", "low": 5.0, "high": 100.0, "unit": "um",+ "source": "Milo and Phillips, Cell Biology by the Numbers, ch. 1"},+ {"name": "cell_membrane_thickness", "low": 3.0, "high": 5.0, "unit": "nm",+ "source": "Alberts et al., Molecular Biology of the Cell, 7th ed., ch. 10"},+ {"name": "dna_base_pair_rise", "low": 0.32, "high": 0.36, "unit": "nm",+ "source": "Bloomfield et al., Nucleic Acids: Structures and Properties"},+ {"name": "ribosome_diameter", "low": 20.0, "high": 30.0, "unit": "nm",+ "source": "Milo and Phillips, Cell Biology by the Numbers, ch. 1"},+ {"name": "protein_molar_mass", "low": 5.0, "high": 1000.0, "unit": "kDa",+ "source": "Milo and Phillips, Cell Biology by the Numbers, ch. 1"},+ {"name": "human_capillary_diameter", "low": 5.0, "high": 10.0, "unit": "um",+ "source": "Guyton and Hall, Textbook of Medical Physiology, 14th ed., ch. 16"},+ {"name": "mammalian_body_temperature", "low": 306.0, "high": 315.0, "unit": "K",+ "source": "Guyton and Hall, Textbook of Medical Physiology, 14th ed., ch. 74"},+ {"name": "resting_heart_rate", "low": 0.7, "high": 3.0, "unit": "Hz",+ "source": "Guyton and Hall, Textbook of Medical Physiology, 14th ed., ch. 9"},+ {"name": "blood_plasma_osmolarity", "low": 275.0, "high": 300.0, "unit": "mol/m**3",+ "source": "Guyton and Hall, Textbook of Medical Physiology, 14th ed., ch. 25"},+ {"name": "diffusivity_small_molecule_water", "low": 3e-10, "high": 3e-9,+ "unit": "m**2/s", "source": "Cussler, Diffusion, 3rd ed., app. A"},+ {"name": "diffusivity_protein_water", "low": 1e-11, "high": 1.5e-10,+ "unit": "m**2/s", "source": "Cussler, Diffusion, 3rd ed., app. A"},+ {"name": "dynamic_viscosity_water", "low": 0.5, "high": 1.5, "unit": "mPa*s",+ "source": "IAPWS R12-08, viscosity of ordinary water substance"},+ {"name": "surface_tension_water", "low": 0.06, "high": 0.08, "unit": "N/m",+ "source": "IAPWS R1-76, surface tension of ordinary water substance"},+ {"name": "sound_speed_water", "low": 1400.0, "high": 1560.0, "unit": "m/s",+ "source": "Del Grosso and Mader, J. Acoust. Soc. Am. 52:1442 (1972)"},+ {"name": "sound_speed_air", "low": 320.0, "high": 350.0, "unit": "m/s",+ "source": "Cramer, J. Acoust. Soc. Am. 93:2510 (1993)"},+ {"name": "atmospheric_pressure_sea_level", "low": 95.0, "high": 105.0,+ "unit": "kPa", "source": "ISO 2533 standard atmosphere"},+ {"name": "earth_surface_gravity", "low": 9.76, "high": 9.84, "unit": "m/s**2",+ "source": "WGS 84 normal gravity at the ellipsoid"},+ {"name": "visible_wavelength", "low": 380.0, "high": 750.0, "unit": "nm",+ "source": "CIE S 017:2020, International Lighting Vocabulary"},+ {"name": "noncovalent_bond_energy", "low": 1.0, "high": 40.0, "unit": "kJ/mol",+ "source": "Israelachvili, Intermolecular and Surface Forces, 3rd ed., ch. 2"},+ {"name": "covalent_bond_energy", "low": 150.0, "high": 1000.0, "unit": "kJ/mol",+ "source": "Atkins and de Paula, Physical Chemistry, 12th ed., data section"},+ {"name": "atp_hydrolysis_energy", "low": 40.0, "high": 60.0, "unit": "kJ/mol",+ "source": "Milo and Phillips, Cell Biology by the Numbers, ch. 4"},+)++CATALOGUE = {+ "groups": {entry["name"]: entry for entry in GROUPS},+ "scales": {entry["name"]: entry for entry in SCALES},+ "bands": {entry["name"]: entry for entry in BANDS},+}+++def parse_quantity(text: str, registry: Any) -> tuple[str, Any]:+ """Parse `name=value[ unit]` into a named pint quantity."""++ name, separator, payload = text.partition("=")+ name = name.strip()+ if not separator or not name.isidentifier() or name.startswith("_"):+ raise CliError(+ f"quantity {text!r} must look like velocity=1.2 m/s, with a "+ "Python-identifier name"+ )+ fields = payload.strip().split(None, 1)+ if not fields:+ raise CliError(f"quantity {name!r} needs a magnitude")+ magnitude = _common.finite_float(fields[0])+ unit = checked_unit(fields[1], label=name) if len(fields) == 2 else "dimensionless"+ try:+ return name, registry.Quantity(magnitude, unit)+ except Exception as exc: # pint raises several unrelated types here+ raise CliError(f"cannot read {name!r} as a quantity: {exc}") from exc+++def constant_quantities(registry: Any) -> dict[str, Any]:+ """Return the physical constants every expression may use."""++ try:+ from scipy import constants+ except ImportError as exc:+ raise CliError(+ f"SciPy is unavailable; install with `{_common.PINNED_INSTALL}`"+ ) from exc+ resolved: dict[str, Any] = {}+ for name, attribute, unit in CONSTANT_UNITS:+ resolved[name] = registry.Quantity(float(getattr(constants, attribute)), unit)+ return resolved+++def check_dimensionality(name: str, quantity: Any, expected: str) -> None:+ """Reject an input whose dimensionality is not the one the formula needs."""++ if not expected:+ if not quantity.dimensionless:+ raise CliError(+ f"{name} must be dimensionless, but carries {quantity.units:~P}"+ )+ return+ if not quantity.check(expected):+ raise CliError(+ f"{name} must have dimensionality {expected}, but {quantity.units:~P} "+ f"is {quantity.dimensionality}"+ )+++def evaluate(+ entry: dict[str, Any], supplied: dict[str, Any], constants: dict[str, Any]+) -> Any:+ """Reduce one catalogue expression against the supplied quantities."""++ tree = _common.parse_expression(entry["expression"])+ needed = set(_common.expression_variables(tree))+ required = sorted(needed - set(constants))+ missing = [name for name in required if name not in supplied]+ if missing:+ wanted = ", ".join(+ f"{name} [{entry['inputs'].get(name) or 'dimensionless'}]"+ for name in missing+ )+ raise CliError(f"{entry['name']} needs: {wanted}")+ for name in required:+ check_dimensionality(name, supplied[name], entry["inputs"].get(name, ""))+ values = {name: supplied[name] for name in required}+ values.update({name: constants[name] for name in needed & set(constants)})+ return _common.reduce_expression(tree, values, {"sqrt": lambda q: q**0.5})+++def classify(value: float, regimes: list[list[Any]]) -> str:+ """Return the label of the regime a dimensionless value falls in."""++ for lower, upper, label in regimes:+ if (lower is None or value >= lower) and (upper is None or value < upper):+ return label+ return "outside every tabulated regime"+++def orders_of_magnitude(value: float, reference: float) -> float | None:+ """Return log10 of the ratio between a value and a reference.++ None when the ratio has no logarithm, which keeps the report strict JSON+ rather than emitting an infinity.+ """++ if value <= 0 or reference <= 0:+ return None+ return math.log10(value / reference)+++def _describe(quantity: Any) -> dict[str, Any]:+ """Report a quantity as a magnitude paired with its explicit unit string."""++ # audit-units: ignore UNIT003 -- the unit travels with the magnitude below+ value = float(quantity.magnitude)+ return {"value": value, "unit": f"{quantity.units:~P}"}+++def evaluate_group(+ entry: dict[str, Any], supplied: dict[str, Any], constants: dict[str, Any]+) -> dict[str, Any]:+ """Compute one dimensionless group and place it in a regime."""++ result = evaluate(entry, supplied, constants)+ if not getattr(result, "dimensionless", False):+ raise CliError(+ f"{entry['name']} evaluated to {result.units:~P}, which is not "+ "dimensionless; an input carries the wrong quantity"+ )+ value = float(result.m_as("dimensionless"))+ return {+ "name": entry["name"],+ "symbol": entry["symbol"],+ "expression": entry["expression"],+ "value": value,+ "regime": classify(value, entry["regimes"]),+ "note": entry["note"],+ "source": entry["source"],+ }+++def evaluate_scale(+ entry: dict[str, Any], supplied: dict[str, Any], constants: dict[str, Any]+) -> dict[str, Any]:+ """Compute one characteristic scale in its tabulated unit."""++ result = evaluate(entry, supplied, constants)+ try:+ magnitude = float(result.m_as(entry["unit"]))+ except Exception as exc: # pint raises DimensionalityError and friends+ raise CliError(+ f"{entry['name']} evaluated to {result.units:~P}, which cannot be "+ f"expressed in {entry['unit']}: {exc}"+ ) from exc+ return {+ "name": entry["name"],+ "expression": entry["expression"],+ "value": magnitude,+ "unit": entry["unit"],+ "note": entry["note"],+ "source": entry["source"],+ }+++def evaluate_band(+ entry: dict[str, Any], name: str, quantity: Any+) -> dict[str, Any]:+ """Compare a supplied quantity against a curated plausible range."""++ try:+ magnitude = float(quantity.m_as(entry["unit"]))+ except Exception as exc: # pint raises DimensionalityError and friends+ raise CliError(+ f"{name} is {quantity.units:~P}, which cannot be compared with the "+ f"{entry['name']} band in {entry['unit']}: {exc}"+ ) from exc+ if entry["low"] <= magnitude <= entry["high"]:+ verdict, decades = "plausible", 0.0+ else:+ edge = entry["low"] if magnitude < entry["low"] else entry["high"]+ decades = orders_of_magnitude(magnitude, edge)+ # A non-positive magnitude has no ratio to the band, and no physical+ # quantity in this table can take one.+ verdict = (+ "implausible" if decades is None or abs(decades) >= 1 else "questionable"+ )+ return {+ "quantity": name,+ "band": entry["name"],+ "value": magnitude,+ "unit": entry["unit"],+ "range": [entry["low"], entry["high"]],+ "verdict": verdict,+ "orders_of_magnitude_outside": decades,+ "source": entry["source"],+ }+++def overall_verdict(bands: list[dict[str, Any]]) -> str:+ """Reduce the band results to a single word."""++ verdicts = {item["verdict"] for item in bands}+ if "implausible" in verdicts:+ return "implausible"+ if "questionable" in verdicts:+ return "questionable"+ return "plausible"+++def exit_code(document: dict[str, Any], threshold: str) -> int:+ """Return 1 when the verdict meets the configured failure threshold."""++ order = {"plausible": 0, "questionable": 1, "implausible": 2}+ if threshold == "none":+ return 0+ return 1 if order[document["verdict"]] >= order[threshold] else 0+++def build_parser() -> argparse.ArgumentParser:+ parser = argparse.ArgumentParser(+ description=(+ "Evaluate dimensionless groups and characteristic scales from "+ "unit-bearing quantities, and compare quantities against curated "+ "physical ranges."+ )+ )+ parser.add_argument(+ "--quantity",+ action="append",+ default=[],+ metavar="NAME=VALUE UNIT",+ help="input quantity, e.g. 'velocity=1.2 m/s'; repeatable",+ )+ parser.add_argument(+ "--group",+ action="append",+ default=[],+ help="dimensionless group to evaluate; repeatable",+ )+ parser.add_argument(+ "--scale",+ action="append",+ default=[],+ help="characteristic scale to compute; repeatable",+ )+ parser.add_argument(+ "--band",+ action="append",+ default=[],+ metavar="BAND=NAME",+ help="compare a supplied quantity against a curated range, e.g. "+ "'eukaryotic_cell_diameter=diameter'; repeatable",+ )+ parser.add_argument(+ "--list",+ action="store_true",+ dest="list_catalogue",+ help="list the available groups, scales, and bands, then exit",+ )+ parser.add_argument(+ "--fail-on",+ choices=("none", "questionable", "implausible"),+ default="implausible",+ help="verdict at which the exit status becomes 1 (default implausible)",+ )+ parser.add_argument("--format", choices=("json", "markdown"), default="json")+ parser.add_argument("--output", help="write the report to this file")+ parser.add_argument(+ "--force", action="store_true", help="overwrite an existing output file"+ )+ return parser+++def _catalogue_document() -> dict[str, Any]:+ """Describe everything this tool knows how to compute."""++ return {+ "groups": [+ {+ "name": entry["name"],+ "symbol": entry["symbol"],+ "expression": entry["expression"],+ "inputs": entry["inputs"],+ }+ for entry in GROUPS+ ],+ "scales": [+ {+ "name": entry["name"],+ "expression": entry["expression"],+ "inputs": entry["inputs"],+ "unit": entry["unit"],+ }+ for entry in SCALES+ ],+ "bands": [+ {+ "name": entry["name"],+ "range": [entry["low"], entry["high"]],+ "unit": entry["unit"],+ }+ for entry in BANDS+ ],+ }+++def _lookup(kind: str, name: str) -> dict[str, Any]:+ """Fetch a catalogue entry or explain what is available."""++ entry = CATALOGUE[kind].get(name)+ if entry is None:+ available = ", ".join(sorted(CATALOGUE[kind]))+ raise CliError(f"unknown {kind[:-1]} {name!r}; available: {available}")+ return entry+++def run(arguments: argparse.Namespace) -> dict[str, Any]:+ """Evaluate every requested group, scale, and band."""++ if arguments.list_catalogue:+ return _catalogue_document()++ requested = len(arguments.group) + len(arguments.scale) + len(arguments.band)+ if requested == 0:+ raise CliError("supply at least one of --group, --scale, or --band")+ if max(requested, len(arguments.quantity)) > MAX_ENTRIES:+ raise CliError(f"at most {MAX_ENTRIES} entries are supported per run")++ registry = build_registry()+ supplied: dict[str, Any] = {}+ for item in arguments.quantity:+ name, quantity = parse_quantity(item, registry)+ if name in supplied:+ raise CliError(f"quantity {name!r} is supplied more than once")+ supplied[name] = quantity+ constants = constant_quantities(registry)+ shadowed = sorted(set(supplied) & set(constants))+ if shadowed:+ raise CliError(+ f"these names are reserved for physical constants: {', '.join(shadowed)}"+ )++ groups = [+ evaluate_group(_lookup("groups", name), supplied, constants)+ for name in arguments.group+ ]+ scales = [+ evaluate_scale(_lookup("scales", name), supplied, constants)+ for name in arguments.scale+ ]++ bands: list[dict[str, Any]] = []+ for item in arguments.band:+ band_name, separator, quantity_name = item.partition("=")+ quantity_name = quantity_name.strip()+ if not separator or not quantity_name:+ raise CliError(+ f"band {item!r} must look like eukaryotic_cell_diameter=diameter"+ )+ if quantity_name not in supplied:+ raise CliError(f"band {item!r} references unsupplied quantity "+ f"{quantity_name!r}")+ entry = _lookup("bands", band_name.strip())+ bands.append(evaluate_band(entry, quantity_name, supplied[quantity_name]))++ warnings: list[str] = []+ for item in bands:+ if item["verdict"] == "plausible":+ continue+ decades = item["orders_of_magnitude_outside"]+ distance = (+ "outside" if decades is None else f"{abs(decades):.1f} decades outside"+ )+ warnings.append(+ f"{item['quantity']} = {item['value']:.4g} {item['unit']} sits "+ f"{distance} the {item['band']} range "+ f"{item['range'][0]:.4g}-{item['range'][1]:.4g} {item['unit']}"+ )+ for item in groups:+ if item["value"] <= 0:+ warnings.append(+ f"{item['symbol']} is not positive, which no physical "+ "configuration produces; check the sign of an input"+ )++ return {+ "quantities": {+ name: _describe(quantity) for name, quantity in supplied.items()+ },+ "groups": groups,+ "scales": scales,+ "bands": bands,+ "verdict": overall_verdict(bands),+ "warnings": warnings,+ }+++def render_markdown(document: dict[str, Any]) -> str:+ """Render a human-readable plausibility report."""++ lines = ["# Plausibility check", ""]+ if document.get("groups"):+ lines += [+ "## Dimensionless groups",+ "",+ "| Group | Symbol | Value | Regime |",+ "| --- | --- | --- | --- |",+ ]+ for item in document["groups"]:+ lines.append(+ f"| {item['name']} | {item['symbol']} | {item['value']:.4g} | "+ f"{item['regime']} |"+ )+ lines.append("")+ if document.get("scales"):+ lines += ["## Characteristic scales", "", "| Scale | Value |", "| --- | --- |"]+ for item in document["scales"]:+ lines.append(f"| {item['name']} | {item['value']:.4g} {item['unit']} |")+ lines.append("")+ if document.get("bands"):+ lines += [+ "## Magnitude bands",+ "",+ "| Quantity | Band | Value | Range | Verdict |",+ "| --- | --- | --- | --- | --- |",+ ]+ for item in document["bands"]:+ lines.append(+ f"| {item['quantity']} | {item['band']} | "+ f"{item['value']:.4g} {item['unit']} | "+ f"{item['range'][0]:.4g}-{item['range'][1]:.4g} {item['unit']} | "+ f"{item['verdict']} |"+ )+ lines.append("")+ lines.append(f"**Verdict: {document['verdict']}**")+ if document.get("warnings"):+ lines += ["", "## Warnings", ""]+ lines += [f"- {message}" for message in document["warnings"]]+ return "\n".join(lines) + "\n"+++def main(argv: list[str] | None = None) -> int:+ arguments = build_parser().parse_args(argv)+ try:+ document = run(arguments)+ if arguments.format == "markdown" and not arguments.list_catalogue:+ _common.emit_text(+ render_markdown(document),+ output=arguments.output,+ force=arguments.force,+ )+ else:+ _common.emit_json(document, output=arguments.output, force=arguments.force)+ except CliError as error:+ print(f"error: {error}", file=sys.stderr)+ return 2+ if arguments.list_catalogue:+ return 0+ return exit_code(document, arguments.fail_on)+++if __name__ == "__main__":+ raise SystemExit(main())diff --git a/skills/uncertainty-and-units/scripts/convert_units.py b/skills/uncertainty-and-units/scripts/convert_units.pynew file mode 100644index 00000000..7fb2b94e--- /dev/null+++ b/skills/uncertainty-and-units/scripts/convert_units.py@@ -0,0 +1,280 @@+#!/usr/bin/env python3+"""Convert a quantity between units, including the conversions that need a context.++Wavelength to photon energy, mass to amount of substance, and energy to+temperature are not dimensional conversions - they are physical relations that+pint only performs inside a named context. This CLI makes the context explicit,+carries an uncertainty through the conversion's local derivative, and refuses+to hide the two unit families whose arithmetic does not mean what it looks+like: offset temperatures and logarithmic ratios.+"""++from __future__ import annotations++import argparse+import math+import sys+from typing import Any++import _common+from _common import CliError+++MAX_UNIT_CHARS = 120+OFFSET_HINT = (+ "an offset unit measures a point on a scale, not an amount; differences and "+ "uncertainties belong in the matching delta_ unit"+)+LOGARITHMIC_HINT = (+ "adding two quantities in a logarithmic unit multiplies the underlying linear "+ "quantities, and pint returns the product in squared base units"+)+++def checked_unit(text: str, *, label: str) -> str:+ """Reject unit strings that are empty, oversized, or obviously not units."""++ if not isinstance(text, str) or not text.strip():+ raise CliError(f"{label} must be a non-empty unit string")+ cleaned = text.strip()+ if len(cleaned) > MAX_UNIT_CHARS:+ raise CliError(f"{label} is longer than {MAX_UNIT_CHARS} characters")+ forbidden = set(";#\n\r\\'\"")+ if forbidden & set(cleaned):+ raise CliError(f"{label} contains characters that are not part of a unit")+ if "__" in cleaned or "lambda" in cleaned:+ raise CliError(f"{label} is not a valid unit expression")+ return cleaned+++def parse_context_parameter(text: str, registry: Any) -> tuple[str, Any]:+ """Parse `name=value[ unit]` into a context keyword argument."""++ name, _, payload = text.partition("=")+ name = name.strip()+ if not name.isidentifier() or not payload.strip():+ raise CliError(f"context parameter {text!r} must look like mw=180.16 g/mol")+ fields = payload.strip().split(None, 1)+ magnitude = _common.finite_float(fields[0])+ if len(fields) == 1:+ return name, magnitude+ return name, registry.Quantity(magnitude, checked_unit(fields[1], label=name))+++def build_registry() -> Any:+ """Create a default pint registry."""++ try:+ import pint+ except ImportError as exc:+ raise CliError(+ f"pint is unavailable; install with `{_common.PINNED_INSTALL}`"+ ) from exc+ return pint.UnitRegistry()+++def is_multiplicative(registry: Any, unit: str) -> bool:+ """Report whether a unit can take part in ordinary arithmetic."""++ import pint++ try:+ registry.Quantity(1.0, unit) * 2.0+ except pint.errors.OffsetUnitCalculusError:+ return False+ except pint.errors.UndefinedUnitError as exc:+ raise CliError(f"unknown unit {unit!r}") from exc+ return True+++def convert(+ registry: Any,+ value: float,+ unit: str,+ target: str,+ contexts: list[str],+ parameters: dict[str, Any],+) -> Any:+ """Convert one magnitude, applying any requested contexts."""++ import pint++ try:+ quantity = registry.Quantity(value, unit)+ except pint.errors.UndefinedUnitError as exc:+ raise CliError(f"unknown source unit {unit!r}") from exc+ try:+ return quantity.to(target, *contexts, **parameters)+ except pint.errors.UndefinedUnitError as exc:+ raise CliError(f"unknown target unit {target!r}") from exc+ except pint.errors.DimensionalityError as exc:+ raise CliError(+ f"{exc}. If the two units are related by a physical law rather than by "+ "dimensional analysis, name the context: --context spectroscopy for "+ "wavelength, frequency, wavenumber, and photon energy; "+ "--context chemistry --context-parameter 'mw=<molar mass>' for mass and "+ "amount of substance; --context boltzmann for energy and temperature"+ ) from exc+ except pint.errors.PintError as exc:+ raise CliError(f"pint could not perform the conversion: {exc}") from exc+++def propagate(+ registry: Any,+ value: float,+ uncertainty: float,+ unit: str,+ target: str,+ contexts: list[str],+ parameters: dict[str, Any],+) -> float:+ """Carry an uncertainty through the conversion's local derivative.++ A central difference is exact for the affine conversions (including offset+ temperatures) and accurate to second order for the reciprocal relations a+ context introduces.+ """++ step = 1e-6 * max(abs(value), 1.0)+ high = convert(registry, value + step, unit, target, contexts, parameters)+ low = convert(registry, value - step, unit, target, contexts, parameters)+ # audit-units: ignore UNIT003 -- both quantities were just converted to `target`+ derivative = (high.magnitude - low.magnitude) / (2.0 * step)+ if not math.isfinite(derivative):+ raise CliError("the conversion is not differentiable at this value")+ return abs(derivative) * uncertainty+++def run(arguments: argparse.Namespace) -> dict[str, Any]:+ """Perform the requested conversion and describe its hazards."""++ registry = build_registry()+ if arguments.list_contexts:+ defined = getattr(registry, "_contexts", {})+ return {+ "available_contexts": sorted(str(name) for name in defined),+ "note": (+ "spectroscopy (sp) relates wavelength, frequency, wavenumber, and "+ "photon energy; chemistry (chem) relates mass and amount of "+ "substance and needs mw; boltzmann relates energy and temperature"+ ),+ }++ if arguments.value is None or arguments.unit is None or arguments.to is None:+ raise CliError("--value, --unit, and --to are all required")++ unit = checked_unit(arguments.unit, label="--unit")+ target = checked_unit(arguments.to, label="--to")+ contexts = [checked_unit(item, label="--context") for item in arguments.context]+ parameters = dict(+ parse_context_parameter(item, registry)+ for item in arguments.context_parameter+ )++ result = convert(registry, arguments.value, unit, target, contexts, parameters)+ document: dict[str, Any] = {+ "input": {"value": arguments.value, "unit": unit},+ "target_unit": target,+ "contexts": contexts,+ # audit-units: ignore UNIT003 -- `result` is already in `target`+ "value": float(result.magnitude),+ "unit": str(result.units),+ "warnings": [],+ }++ source_multiplicative = is_multiplicative(registry, unit)+ target_multiplicative = is_multiplicative(registry, target)+ if source_multiplicative and target_multiplicative and not contexts:+ one = convert(registry, 1.0, unit, target, contexts, parameters)+ # audit-units: ignore UNIT003 -- `one` is already in `target`+ document["conversion_factor"] = float(one.magnitude)++ if not source_multiplicative or not target_multiplicative:+ document["warnings"].append(f"{OFFSET_HINT} (in {unit} or {target})")+ for candidate in (unit, target):+ # audit-units: ignore UNIT004 -- this line is the detector, not a usage+ if "dB" in candidate or "decibel" in candidate:+ document["warnings"].append(f"{LOGARITHMIC_HINT} (in {candidate})")++ if arguments.uncertainty is not None:+ converted = propagate(+ registry,+ arguments.value,+ arguments.uncertainty,+ unit,+ target,+ contexts,+ parameters,+ )+ document["input"]["uncertainty"] = arguments.uncertainty+ document["uncertainty"] = converted+ note = "propagated through the local derivative of the conversion"+ if not source_multiplicative or not target_multiplicative:+ note += (+ "; for an offset temperature that derivative is the scale factor "+ f"alone, so the result is {converted:.6g} delta_{target}, not a "+ "point on the scale"+ )+ document["uncertainty_note"] = note+ if contexts:+ document["warnings"].append(+ "a context conversion can be nonlinear, so the propagated "+ "uncertainty is a first-order approximation valid only while the "+ "uncertainty is small compared with the value"+ )+ return document+++def build_parser() -> argparse.ArgumentParser:+ parser = argparse.ArgumentParser(+ description=(+ "Convert a quantity between units with pint, including context-only "+ "conversions, and carry an uncertainty through the conversion."+ )+ )+ parser.add_argument("--value", type=_common.finite_float, help="magnitude")+ parser.add_argument("--unit", help="unit of the supplied magnitude")+ parser.add_argument("--to", help="target unit")+ parser.add_argument(+ "--uncertainty",+ type=_common.non_negative_float,+ help="standard uncertainty in the source unit",+ )+ parser.add_argument(+ "--context",+ action="append",+ default=[],+ help="pint context enabling the conversion, e.g. spectroscopy; repeatable",+ )+ parser.add_argument(+ "--context-parameter",+ action="append",+ default=[],+ metavar="NAME=VALUE[ UNIT]",+ help="context keyword such as 'mw=180.16 g/mol'; repeatable",+ )+ parser.add_argument(+ "--list-contexts",+ action="store_true",+ help="list the contexts the registry defines and exit",+ )+ parser.add_argument("--output", help="write JSON output to this file")+ parser.add_argument(+ "--force", action="store_true", help="overwrite an existing output file"+ )+ return parser+++def main(argv: list[str] | None = None) -> int:+ arguments = build_parser().parse_args(argv)+ try:+ document = run(arguments)+ _common.emit_json(document, output=arguments.output, force=arguments.force)+ except CliError as error:+ print(f"error: {error}", file=sys.stderr)+ return 2+ return 0+++if __name__ == "__main__":+ raise SystemExit(main())diff --git a/skills/uncertainty-and-units/scripts/format_result.py b/skills/uncertainty-and-units/scripts/format_result.pynew file mode 100644index 00000000..5da923f3--- /dev/null+++ b/skills/uncertainty-and-units/scripts/format_result.py@@ -0,0 +1,326 @@+#!/usr/bin/env python3+"""Round and render a measurement result the way JCGM 100:2008 7.2 requires.++The uncertainty is rounded to one or two significant digits first, and the+value is then rounded to that same decimal place. Doing it in the other order,+or not at all, produces the familiar `12.34567 +/- 0.1` that claims five digits+of resolution the measurement does not have.+"""++from __future__ import annotations++import argparse+import math+import sys+from decimal import Decimal, InvalidOperation, ROUND_HALF_EVEN, ROUND_HALF_UP+from typing import Any++import _common+from _common import CliError+++ROUNDING_MODES = {"half-even": ROUND_HALF_EVEN, "half-up": ROUND_HALF_UP}+++def _decimal(value: float, *, label: str) -> Decimal:+ """Convert a float to the shortest exact Decimal that reprs the same."""++ try:+ return Decimal(repr(float(value)))+ except (InvalidOperation, ValueError, OverflowError) as exc:+ raise CliError(f"{label} is not a finite decimal number") from exc+++def round_to_uncertainty(+ value: float,+ uncertainty: float,+ *,+ significant_digits: int = 2,+ rounding: str = "half-even",+) -> dict[str, Any]:+ """Round value and uncertainty to a common decimal place.++ Returns the rounded pair plus the decimal place they share, so callers can+ render any notation from the same numbers.+ """++ if significant_digits not in (1, 2):+ raise CliError("uncertainty is reported to one or two significant digits")+ mode = ROUNDING_MODES.get(rounding)+ if mode is None:+ raise CliError(f"rounding must be one of: {', '.join(sorted(ROUNDING_MODES))}")+ if not math.isfinite(value) or not math.isfinite(uncertainty):+ raise CliError("value and uncertainty must both be finite")+ if uncertainty < 0:+ raise CliError("uncertainty must not be negative")++ exact_value = _decimal(value, label="value")+ if uncertainty == 0:+ return {+ "exact": True,+ "decimal_place": None,+ "value": exact_value,+ "uncertainty": Decimal(0),+ }++ exact_uncertainty = _decimal(uncertainty, label="uncertainty")+ exponent = exact_uncertainty.adjusted()+ place = exponent - (significant_digits - 1)+ quantum = Decimal(1).scaleb(place)+ rounded_uncertainty = exact_uncertainty.quantize(quantum, rounding=mode)++ # Rounding 0.0996 to two digits gives 0.100, which now carries three+ # digits. Recompute the place once against the carried value.+ if rounded_uncertainty.adjusted() != exponent:+ exponent = rounded_uncertainty.adjusted()+ place = exponent - (significant_digits - 1)+ quantum = Decimal(1).scaleb(place)+ rounded_uncertainty = exact_uncertainty.quantize(quantum, rounding=mode)++ rounded_value = exact_value.quantize(quantum, rounding=mode)+ return {+ "exact": False,+ "decimal_place": place,+ "value": rounded_value,+ "uncertainty": rounded_uncertainty,+ }+++def _plain(number: Decimal) -> str:+ """Render a Decimal without exponent notation, keeping trailing zeros."""++ text = format(number, "f")+ return "-0" if text == "-0" else text+++# Outside this decade range, positional notation degenerates into a run of+# zeros, so every rendering switches to scientific form.+POSITIONAL_RANGE = range(-6, 16)+++def render(rounded: dict[str, Any], unit: str | None) -> dict[str, str]:+ """Produce the standard notations for an already-rounded pair."""++ suffix = f" {unit}" if unit else ""+ latex_unit = f"\\,\\mathrm{{{unit}}}" if unit else ""+ value = rounded["value"]+ uncertainty = rounded["uncertainty"]++ if rounded["exact"]:+ exponent = value.adjusted() if value != 0 else 0+ if exponent in POSITIONAL_RANGE:+ text = f"{_plain(value)}{suffix}"+ latex = f"${_plain(value)}${latex_unit}"+ else:+ mantissa = _plain(value.scaleb(-exponent))+ text = f"{mantissa}e{exponent:+03d}{suffix}"+ latex = (+ f"${mantissa} \\times 10^{{{exponent}}}${latex_unit}"+ )+ return {+ "plusminus": f"{text} (exact)",+ "ascii": f"{text} (exact)",+ "parenthetic": f"{text} (exact)",+ "scientific": f"{text} (exact)",+ "latex": latex,+ }++ place = rounded["decimal_place"]+ digits = int(uncertainty.scaleb(-place).to_integral_value())+ exponent = value.adjusted() if value != 0 else uncertainty.adjusted()+ mantissa_value = _plain(value.scaleb(-exponent))+ mantissa_uncertainty = _plain(uncertainty.scaleb(-exponent))+ scientific = f"({mantissa_value} ± {mantissa_uncertainty})e{exponent:+03d}{suffix}"+ scientific_concise = f"{mantissa_value}({digits})e{exponent:+03d}{suffix}"++ # `place > 0` puts the concise digits left of the decimal point, where they+ # are ambiguous; scientific notation removes the ambiguity.+ if exponent not in POSITIONAL_RANGE:+ return {+ "plusminus": scientific,+ "ascii": scientific.replace("±", "+/-"),+ "parenthetic": scientific_concise,+ "scientific": scientific,+ "latex": (+ f"$({mantissa_value} \\pm {mantissa_uncertainty}) "+ f"\\times 10^{{{exponent}}}${latex_unit}"+ ),+ }++ value_text = _plain(value)+ uncertainty_text = _plain(uncertainty)+ return {+ "plusminus": f"{value_text} ± {uncertainty_text}{suffix}",+ "ascii": f"{value_text} +/- {uncertainty_text}{suffix}",+ "parenthetic": (+ scientific_concise if place > 0 else f"{value_text}({digits}){suffix}"+ ),+ "scientific": scientific,+ "latex": f"$({value_text} \\pm {uncertainty_text})${latex_unit}",+ }+++def build_statement(+ renderings: dict[str, str],+ coverage_factor: float | None,+ coverage_probability: float | None,+) -> str:+ """Write the sentence that has to accompany the number."""++ if coverage_factor is None:+ return (+ f"{renderings['plusminus']}, where the stated uncertainty is a combined "+ "standard uncertainty (coverage factor k = 1)."+ )+ probability = ""+ if coverage_probability is not None:+ probability = (+ f", which corresponds to a coverage probability of approximately "+ f"{coverage_probability * 100:.0f}%"+ )+ return (+ f"{renderings['plusminus']}, where the stated uncertainty is an expanded "+ f"uncertainty U = k*u_c with a coverage factor k = {coverage_factor:g}"+ f"{probability}."+ )+++def collect_warnings(+ value: float, uncertainty: float, significant_digits: int, rounded: dict[str, Any]+) -> list[str]:+ """Flag reporting choices that mislead the reader."""++ warnings: list[str] = []+ if uncertainty == 0:+ return warnings+ leading = _decimal(uncertainty, label="uncertainty").as_tuple().digits[0]+ if significant_digits == 1 and leading in (1, 2):+ warnings.append(+ f"the uncertainty begins with {leading}; rounding it to one significant "+ "digit changes it by a large fraction, so report two digits"+ )+ if value != 0 and uncertainty / abs(value) > 1.0:+ warnings.append(+ "the standard uncertainty exceeds the estimate itself; report the result "+ "as consistent with zero rather than as a measured value"+ )+ if rounded["value"] == 0 and value != 0:+ warnings.append(+ "the estimate rounds to zero at the uncertainty's decimal place; the "+ "measurement does not resolve it from zero"+ )+ return warnings+++def build_parser() -> argparse.ArgumentParser:+ parser = argparse.ArgumentParser(+ description=(+ "Round a value and its uncertainty to a common decimal place and render "+ "the standard notations (JCGM 100:2008 7.2.6)."+ )+ )+ parser.add_argument(+ "--value", required=True, type=_common.finite_float, help="the estimate"+ )+ parser.add_argument(+ "--uncertainty",+ required=True,+ type=_common.non_negative_float,+ help="standard or expanded uncertainty, in the same unit as the value",+ )+ parser.add_argument("--unit", help="unit label appended to every rendering")+ parser.add_argument(+ "--significant-digits",+ type=_common.bounded_int(1, 2),+ default=2,+ help="significant digits retained in the uncertainty (default 2)",+ )+ parser.add_argument(+ "--rounding",+ choices=sorted(ROUNDING_MODES),+ default="half-even",+ help="rounding mode (default half-even)",+ )+ parser.add_argument(+ "--coverage-factor",+ type=_common.positive_float,+ help="k, when the uncertainty supplied is an expanded uncertainty",+ )+ parser.add_argument(+ "--coverage-probability",+ type=_common.probability,+ help="coverage probability associated with k",+ )+ parser.add_argument(+ "--style",+ choices=("plusminus", "ascii", "parenthetic", "scientific", "latex"),+ default="plusminus",+ help="which rendering --format text prints (default plusminus)",+ )+ parser.add_argument("--format", choices=("json", "text"), default="json")+ parser.add_argument("--output", help="write JSON output to this file")+ parser.add_argument(+ "--force", action="store_true", help="overwrite an existing output file"+ )+ return parser+++def run(arguments: argparse.Namespace) -> dict[str, Any]:+ """Round, render, and describe one measurement result."""++ if arguments.coverage_probability is not None and arguments.coverage_factor is None:+ raise CliError(+ "a coverage probability only means something alongside --coverage-factor"+ )+ rounded = round_to_uncertainty(+ arguments.value,+ arguments.uncertainty,+ significant_digits=arguments.significant_digits,+ rounding=arguments.rounding,+ )+ renderings = render(rounded, arguments.unit)+ document: dict[str, Any] = {+ "input": {+ "value": arguments.value,+ "uncertainty": arguments.uncertainty,+ "unit": arguments.unit,+ "significant_digits": arguments.significant_digits,+ "rounding": arguments.rounding,+ },+ "rounded_value": float(rounded["value"]),+ "rounded_uncertainty": float(rounded["uncertainty"]),+ "decimal_place": rounded["decimal_place"],+ "renderings": renderings,+ "statement": build_statement(+ renderings, arguments.coverage_factor, arguments.coverage_probability+ ),+ "warnings": collect_warnings(+ arguments.value,+ arguments.uncertainty,+ arguments.significant_digits,+ rounded,+ ),+ }+ if arguments.value != 0 and arguments.uncertainty > 0:+ document["relative_uncertainty"] = arguments.uncertainty / abs(arguments.value)+ return document+++def main(argv: list[str] | None = None) -> int:+ arguments = build_parser().parse_args(argv)+ try:+ document = run(arguments)+ if arguments.format == "text":+ print(document["renderings"][arguments.style])+ else:+ _common.emit_json(+ document, output=arguments.output, force=arguments.force+ )+ except CliError as error:+ print(f"error: {error}", file=sys.stderr)+ return 2+ return 0+++if __name__ == "__main__":+ raise SystemExit(main())diff --git a/skills/uncertainty-and-units/scripts/propagate_uncertainty.py b/skills/uncertainty-and-units/scripts/propagate_uncertainty.pynew file mode 100644index 00000000..53e09204--- /dev/null+++ b/skills/uncertainty-and-units/scripts/propagate_uncertainty.py@@ -0,0 +1,662 @@+#!/usr/bin/env python3+"""Propagate uncertainty through a measurement model two ways and compare them.++The GUM uncertainty framework (JCGM 100:2008) linearizes the model about the+best estimates. A Monte Carlo run (JCGM 101:2008) propagates the distributions+themselves. Clause 8 of JCGM 101 turns the difference between the two into a+pass/fail check on whether the linearization was allowed to begin with. This+CLI runs both and reports that check.+"""++from __future__ import annotations++import argparse+import math+import sys+from typing import Any++import _common+from _common import CliError+++def parse_variable(text: str) -> dict[str, Any]:+ """Parse `name=value,u[,distribution[,dof]]` into a variable record."""++ if "=" not in text:+ raise CliError(+ f"variable {text!r} must look like name=value,standard_uncertainty"+ )+ name, _, payload = text.partition("=")+ name = name.strip()+ fields = [item.strip() for item in payload.split(",")]+ if len(fields) < 2 or len(fields) > 4:+ raise CliError(+ f"variable {name!r} takes value,standard_uncertainty[,distribution[,dof]]"+ )+ record: dict[str, Any] = {+ "name": name,+ "value": _common.finite_float(fields[0]),+ "standard_uncertainty": _common.non_negative_float(fields[1]),+ }+ if len(fields) >= 3 and fields[2]:+ record["distribution"] = fields[2]+ if len(fields) == 4 and fields[3]:+ if fields[3].lower() in {"inf", "infinite"}:+ record["dof"] = None+ else:+ record["dof"] = _common.finite_float(fields[3])+ return record+++def parse_correlation(text: str) -> tuple[str, str, float]:+ """Parse `a,b=r` into a correlation coefficient between two inputs."""++ pair, _, value = text.partition("=")+ names = [item.strip() for item in pair.split(",")]+ if len(names) != 2 or not all(names) or not value.strip():+ raise CliError(f"correlation {text!r} must look like name_a,name_b=0.4")+ coefficient = _common.finite_float(value)+ if not -1.0 <= coefficient <= 1.0:+ raise CliError("correlation coefficients must lie in [-1, 1]")+ if names[0] == names[1]:+ raise CliError("a variable cannot be correlated with itself")+ return names[0], names[1], coefficient+++def normalize_variables(records: list[Any]) -> list[dict[str, Any]]:+ """Validate variable records from either the CLI or a JSON spec."""++ if not records:+ raise CliError("at least one input variable is required")+ if len(records) > _common.MAX_VARIABLES:+ raise CliError(f"at most {_common.MAX_VARIABLES} variables are supported")+ seen: set[str] = set()+ normalized: list[dict[str, Any]] = []+ for record in records:+ if not isinstance(record, dict):+ raise CliError("each variable must be a JSON object")+ name = record.get("name")+ if not isinstance(name, str) or not name.isidentifier():+ raise CliError(f"variable name {name!r} must be a Python identifier")+ if name.startswith("_"):+ raise CliError("variable names must not start with an underscore")+ if name in _common.ALLOWED_CONSTANTS or name in _common.ALLOWED_FUNCTIONS:+ raise CliError(f"variable name {name!r} shadows a built-in symbol")+ if name in seen:+ raise CliError(f"variable {name!r} is defined more than once")+ seen.add(name)+ uncertainty = record.get("standard_uncertainty", record.get("u"))+ entry = {+ "name": name,+ "value": _common.as_finite(record.get("value"), label=f"{name}.value"),+ "standard_uncertainty": _common.as_finite(+ uncertainty, label=f"{name}.standard_uncertainty"+ ),+ "distribution": _common.normalize_distribution(+ record.get("distribution"), label=f"{name}.distribution"+ ),+ "dof": _common.as_degrees_of_freedom(+ record.get("dof"), label=f"{name}.dof"+ ),+ "unit": record.get("unit") if isinstance(record.get("unit"), str) else None,+ }+ if entry["standard_uncertainty"] < 0:+ raise CliError(f"{name}: standard uncertainty must not be negative")+ if entry["distribution"] == "exact" and entry["standard_uncertainty"] != 0:+ raise CliError(f"{name}: an exact input must have zero uncertainty")+ normalized.append(entry)+ return normalized+++def gum_framework(+ tree: Any,+ variables: list[dict[str, Any]],+ correlations: dict[tuple[str, str], float],+ coverage_probability: float,+) -> dict[str, Any]:+ """Evaluate the model and propagate uncertainty by first-order expansion."""++ try:+ from uncertainties import ufloat+ except ImportError as exc:+ raise CliError(+ "the uncertainties package is unavailable; install with "+ f"`{_common.PINNED_INSTALL}`"+ ) from exc++ handles = {+ entry["name"]: ufloat(entry["value"], entry["standard_uncertainty"])+ for entry in variables+ }+ result = _common.reduce_expression(tree, handles, _common.scalar_functions())+ value = float(getattr(result, "nominal_value", result))+ derivatives = getattr(result, "derivatives", {})++ inputs: list[dict[str, Any]] = []+ for entry in variables:+ sensitivity = float(derivatives.get(handles[entry["name"]], 0.0))+ contribution = sensitivity * entry["standard_uncertainty"]+ inputs.append({**entry, "sensitivity": sensitivity, "contribution": contribution})++ independent = sum(item["contribution"] ** 2 for item in inputs)+ by_name = {item["name"]: item for item in inputs}+ covariance_term = 0.0+ for (first, second), coefficient in correlations.items():+ left = by_name[first]+ right = by_name[second]+ covariance_term += (+ 2.0+ * left["sensitivity"]+ * right["sensitivity"]+ * coefficient+ * left["standard_uncertainty"]+ * right["standard_uncertainty"]+ )+ variance = independent + covariance_term+ if variance < 0:+ raise CliError(+ "the supplied correlations give a negative combined variance; "+ "check the correlation matrix for consistency"+ )+ combined = math.sqrt(variance) # audit-units: ignore UNC003 -- plain float++ # Budget percentages are taken against the sum of squared contributions,+ # not against u_c**2. With correlated inputs the covariance term can make+ # u_c**2 smaller than that sum, and percentages of it would exceed 100.+ for item in inputs:+ item["variance_fraction"] = (+ (item["contribution"] ** 2) / independent if independent > 0 else 0.0+ )++ effective_dof = _common.welch_satterthwaite(+ combined, [(abs(item["contribution"]), item["dof"]) for item in inputs]+ )+ factor = _common.coverage_factor(effective_dof, coverage_probability)+ expanded = factor * combined+ for item in inputs:+ item["dof"] = _common.json_dof(item["dof"])+ return {+ "value": value,+ "combined_standard_uncertainty": combined,+ "independent_variance": independent,+ "covariance_term": covariance_term,+ "effective_degrees_of_freedom": (+ None if not math.isfinite(effective_dof) else effective_dof+ ),+ "coverage_probability": coverage_probability,+ "coverage_factor": factor,+ "expanded_uncertainty": expanded,+ "coverage_interval": [value - expanded, value + expanded],+ "inputs": inputs,+ }+++def _draw(rng: Any, entry: dict[str, Any], trials: int) -> Any:+ """Sample one input from its assigned probability density."""++ import numpy as np++ value = entry["value"]+ uncertainty = entry["standard_uncertainty"]+ if uncertainty == 0:+ return np.full(trials, value)+ shape = entry["distribution"]+ if shape == "normal":+ return rng.normal(value, uncertainty, trials)+ half_width = uncertainty * _common.DISTRIBUTION_DIVISORS[shape]+ if shape == "rectangular":+ return rng.uniform(value - half_width, value + half_width, trials)+ if shape == "triangular":+ return rng.triangular(value - half_width, value, value + half_width, trials)+ if shape == "arcsine":+ # audit-units: ignore UNC003 -- ordinary array of draws, not a ufloat+ return value + half_width * np.cos(rng.uniform(0.0, math.pi, trials))+ raise CliError(f"cannot sample distribution {shape!r}")+++def _draw_correlated(+ rng: Any,+ variables: list[dict[str, Any]],+ correlations: dict[tuple[str, str], float],+ trials: int,+) -> dict[str, Any]:+ """Sample jointly normal inputs from a correlation matrix."""++ import numpy as np++ involved = sorted({name for pair in correlations for name in pair})+ by_name = {entry["name"]: entry for entry in variables}+ for name in involved:+ entry = by_name[name]+ if entry["distribution"] != "normal":+ raise CliError(+ f"{name}: correlated Monte Carlo sampling requires a normal "+ "distribution; declare an uncorrelated model or supply normals"+ )+ if entry["standard_uncertainty"] <= 0:+ raise CliError(f"{name}: a correlated input needs a positive uncertainty")++ index = {name: position for position, name in enumerate(involved)}+ size = len(involved)+ matrix = np.eye(size)+ for (first, second), coefficient in correlations.items():+ matrix[index[first], index[second]] = coefficient+ matrix[index[second], index[first]] = coefficient+ try:+ factor = np.linalg.cholesky(matrix)+ except np.linalg.LinAlgError as exc:+ raise CliError(+ "the correlation matrix is not positive definite; the supplied "+ "coefficients cannot come from a single joint distribution"+ ) from exc++ standard = rng.standard_normal((size, trials))+ correlated = factor @ standard+ samples: dict[str, Any] = {}+ for name in involved:+ entry = by_name[name]+ samples[name] = (+ entry["value"] + entry["standard_uncertainty"] * correlated[index[name]]+ )+ for entry in variables:+ if entry["name"] not in samples:+ samples[entry["name"]] = _draw(rng, entry, trials)+ return samples+++def monte_carlo(+ tree: Any,+ variables: list[dict[str, Any]],+ correlations: dict[tuple[str, str], float],+ coverage_probability: float,+ trials: int,+ seed: int,+) -> dict[str, Any]:+ """Propagate the input distributions by Monte Carlo sampling."""++ try:+ import numpy as np+ except ImportError as exc:+ raise CliError(+ f"NumPy is unavailable; install with `{_common.PINNED_INSTALL}`"+ ) from exc++ if trials * max(len(variables), 1) > 40_000_000:+ raise CliError(+ f"{trials} trials across {len(variables)} inputs exceeds the sampling "+ "budget; lower --trials"+ )++ rng = np.random.default_rng(seed)+ if correlations:+ samples = _draw_correlated(rng, variables, correlations, trials)+ else:+ samples = {+ entry["name"]: _draw(rng, entry, trials) for entry in variables+ }++ drawn = _common.reduce_expression(tree, samples, _common.array_functions())+ drawn = np.asarray(drawn, dtype=float)+ if drawn.shape != (trials,):+ drawn = np.broadcast_to(drawn, (trials,)).astype(float)+ finite = int(np.count_nonzero(np.isfinite(drawn)))+ if finite < trials:+ raise CliError(+ f"{trials - finite} of {trials} Monte Carlo trials produced a "+ "non-finite result; the model is undefined over part of the input "+ "distribution"+ )++ tail = 0.5 * (1.0 - coverage_probability)+ ordered = np.sort(drawn)+ symmetric = (+ float(np.quantile(ordered, tail)),+ float(np.quantile(ordered, 1.0 - tail)),+ )+ shortest = _common.shortest_coverage_interval(ordered, coverage_probability)+ return {+ "trials": trials,+ "seed": seed,+ "mean": float(np.mean(drawn)),+ "standard_uncertainty": float(np.std(drawn, ddof=1)),+ "median": float(np.median(drawn)),+ "coverage_probability": coverage_probability,+ "probabilistically_symmetric_interval": list(symmetric),+ "shortest_coverage_interval": list(shortest),+ }+++def validate_linearization(+ framework: dict[str, Any], sampling: dict[str, Any], significant_digits: int+) -> dict[str, Any]:+ """Apply the JCGM 101:2008 clause 8 comparison of the two coverage intervals."""++ combined = framework["combined_standard_uncertainty"]+ if combined <= 0:+ return {+ "significant_digits": significant_digits,+ "numerical_tolerance": None,+ "gum_framework_validated": None,+ "note": "combined standard uncertainty is zero; no comparison is defined",+ }+ tolerance = _common.numerical_tolerance(combined, significant_digits)+ guf_low, guf_high = framework["coverage_interval"]+ mc_low, mc_high = sampling["probabilistically_symmetric_interval"]+ low_gap = abs(guf_low - mc_low)+ high_gap = abs(guf_high - mc_high)+ validated = low_gap <= tolerance and high_gap <= tolerance+ return {+ "significant_digits": significant_digits,+ "numerical_tolerance": tolerance,+ "endpoint_difference_low": low_gap,+ "endpoint_difference_high": high_gap,+ "gum_framework_validated": validated,+ "note": (+ "linearization reproduces the Monte Carlo coverage interval to within "+ "the numerical tolerance"+ if validated+ else "linearization does not reproduce the Monte Carlo coverage "+ "interval; report the Monte Carlo result"+ ),+ }+++def collect_warnings(+ framework: dict[str, Any],+ sampling: dict[str, Any] | None,+ correlations: dict[tuple[str, str], float],+) -> list[str]:+ """Flag conditions that make the headline numbers misleading."""++ warnings: list[str] = []+ dof = framework["effective_degrees_of_freedom"]+ if dof is not None and dof < 6:+ warnings.append(+ f"effective degrees of freedom is {dof:.1f}; the coverage factor is "+ "dominated by a small Type A sample and is unstable"+ )+ if correlations:+ warnings.append(+ "inputs are correlated, so the Welch-Satterthwaite effective degrees "+ "of freedom (JCGM 100:2008 annex G) does not strictly apply"+ )+ dominant = max(framework["inputs"], key=lambda item: item["variance_fraction"])+ if dominant["variance_fraction"] > 0.9 and len(framework["inputs"]) > 1:+ warnings.append(+ f"{dominant['name']} contributes "+ f"{dominant['variance_fraction'] * 100:.1f}% of the summed squared "+ "contributions; the other inputs barely affect the result"+ )+ if framework["covariance_term"] != 0:+ share = framework["covariance_term"] / framework["independent_variance"]+ warnings.append(+ f"the covariance term changes the combined variance by {share * 100:+.0f}% "+ "of the summed squared contributions, so the budget percentages below "+ "do not add up to u_c"+ )+ for item in framework["inputs"]:+ if item["value"] != 0:+ relative = item["standard_uncertainty"] / abs(item["value"])+ if relative > 0.3:+ warnings.append(+ f"{item['name']} has a relative standard uncertainty of "+ f"{relative * 100:.0f}%; first-order expansion is unreliable "+ "at that width"+ )+ if sampling is not None:+ if sampling["trials"] < _common.RECOMMENDED_TRIALS:+ warnings.append(+ f"{sampling['trials']} Monte Carlo trials is below the "+ f"{_common.RECOMMENDED_TRIALS} JCGM 101:2008 recommends for a 95% "+ "coverage interval; the clause 8 comparison is partly measuring "+ "sampling noise"+ )+ combined = framework["combined_standard_uncertainty"]+ if combined > 0:+ shift = abs(sampling["mean"] - framework["value"]) / combined+ if shift > 0.1:+ warnings.append(+ f"the Monte Carlo mean differs from the model value by "+ f"{shift:.2f} u_c, which indicates a nonlinear model"+ )+ return warnings+++def render_markdown(document: dict[str, Any]) -> str:+ """Render a human-readable propagation report."""++ framework = document["gum_framework"]+ unit = f" {document['unit']}" if document.get("unit") else ""+ lines = [+ f"# Uncertainty propagation: {document.get('measurand') or 'measurand'}",+ "",+ f"Model: `{document['expression']}`",+ "",+ "## GUM uncertainty framework (JCGM 100:2008)",+ "",+ f"- Value: {framework['value']:.6g}{unit}",+ f"- Combined standard uncertainty u_c: "+ f"{framework['combined_standard_uncertainty']:.6g}{unit}",+ f"- Effective degrees of freedom: "+ f"{_common.format_dof(framework['effective_degrees_of_freedom'])}",+ f"- Coverage factor k: {framework['coverage_factor']:.4g} "+ f"at p = {framework['coverage_probability']:.3g}",+ f"- Expanded uncertainty U: {framework['expanded_uncertainty']:.6g}{unit}",+ f"- Coverage interval: [{framework['coverage_interval'][0]:.6g}, "+ f"{framework['coverage_interval'][1]:.6g}]{unit}",+ "",+ "## Uncertainty budget",+ "",+ "| Input | Value | u(x) | Distribution | c = dy/dx | c*u(x) | % variance |",+ "| --- | --- | --- | --- | --- | --- | --- |",+ ]+ for item in framework["inputs"]:+ lines.append(+ f"| {item['name']} | {item['value']:.6g} | "+ f"{item['standard_uncertainty']:.4g} | {item['distribution']} | "+ f"{item['sensitivity']:.4g} | {item['contribution']:.4g} | "+ f"{item['variance_fraction'] * 100:.1f} |"+ )+ sampling = document.get("monte_carlo")+ if sampling:+ lines += [+ "",+ "## Monte Carlo propagation (JCGM 101:2008)",+ "",+ f"- Trials: {sampling['trials']} (seed {sampling['seed']})",+ f"- Mean: {sampling['mean']:.6g}{unit}",+ f"- Standard uncertainty: {sampling['standard_uncertainty']:.6g}{unit}",+ f"- Probabilistically symmetric interval: "+ f"[{sampling['probabilistically_symmetric_interval'][0]:.6g}, "+ f"{sampling['probabilistically_symmetric_interval'][1]:.6g}]{unit}",+ f"- Shortest coverage interval: "+ f"[{sampling['shortest_coverage_interval'][0]:.6g}, "+ f"{sampling['shortest_coverage_interval'][1]:.6g}]{unit}",+ ]+ validation = document.get("validation")+ if validation and validation.get("gum_framework_validated") is not None:+ verdict = "PASS" if validation["gum_framework_validated"] else "FAIL"+ lines += [+ "",+ "## Linearization check (JCGM 101:2008 clause 8)",+ "",+ f"- Numerical tolerance delta: {validation['numerical_tolerance']:.4g}",+ f"- Endpoint differences: "+ f"{validation['endpoint_difference_low']:.4g} (low), "+ f"{validation['endpoint_difference_high']:.4g} (high)",+ f"- Verdict: **{verdict}** - {validation['note']}",+ ]+ if document.get("warnings"):+ lines += ["", "## Warnings", ""]+ lines += [f"- {message}" for message in document["warnings"]]+ return "\n".join(lines) + "\n"+++def build_parser() -> argparse.ArgumentParser:+ parser = argparse.ArgumentParser(+ description=(+ "Propagate uncertainty through a measurement model with both the GUM "+ "framework and Monte Carlo sampling, then check the linearization."+ )+ )+ parser.add_argument(+ "--expression",+ help="measurement model, e.g. 'm / (pi * (d / 2) ** 2 * h)'",+ )+ parser.add_argument(+ "--variable",+ action="append",+ default=[],+ metavar="NAME=VALUE,U[,DIST[,DOF]]",+ help="input estimate and its standard uncertainty; repeatable",+ )+ parser.add_argument(+ "--correlation",+ action="append",+ default=[],+ metavar="A,B=R",+ help="correlation coefficient between two inputs; repeatable",+ )+ parser.add_argument("--spec", help="JSON file holding the whole model")+ parser.add_argument("--measurand", help="name of the output quantity")+ parser.add_argument("--unit", help="unit label for the report only")+ parser.add_argument(+ "--coverage",+ type=_common.probability,+ default=0.95,+ help="coverage probability for the expanded uncertainty (default 0.95)",+ )+ parser.add_argument(+ "--trials",+ type=_common.bounded_int(1_000, _common.MAX_TRIALS),+ default=_common.DEFAULT_TRIALS,+ help=f"Monte Carlo trials (default {_common.DEFAULT_TRIALS})",+ )+ parser.add_argument(+ "--seed",+ type=_common.bounded_int(0, 2**32 - 1),+ default=_common.DEFAULT_SEED,+ help="Monte Carlo seed",+ )+ parser.add_argument(+ "--significant-digits",+ type=_common.bounded_int(1, 2),+ default=2,+ help="significant digits retained in u_c for the clause 8 tolerance",+ )+ parser.add_argument(+ "--no-monte-carlo",+ action="store_true",+ help="skip sampling and report only the linearized result",+ )+ parser.add_argument("--format", choices=("json", "markdown"), default="json")+ parser.add_argument("--output", help="write the report to this file")+ parser.add_argument(+ "--force", action="store_true", help="overwrite an existing output file"+ )+ return parser+++def run(arguments: argparse.Namespace) -> dict[str, Any]:+ """Build the model from CLI flags or a spec file and propagate it."""++ spec: dict[str, Any] = {}+ if arguments.spec:+ loaded = _common.load_json(arguments.spec)+ if not isinstance(loaded, dict):+ raise CliError("the spec file must hold a JSON object")+ spec = loaded++ expression = arguments.expression or spec.get("expression")+ if not expression:+ raise CliError("supply --expression or a spec file containing 'expression'")++ records: list[Any] = list(spec.get("variables", []))+ records += [parse_variable(item) for item in arguments.variable]+ variables = normalize_variables(records)++ tree = _common.parse_expression(expression)+ required = set(_common.expression_variables(tree))+ supplied = {entry["name"] for entry in variables}+ missing = sorted(required - supplied)+ if missing:+ raise CliError(f"no value supplied for: {', '.join(missing)}")+ unused = sorted(supplied - required)+ if unused:+ raise CliError(+ f"these variables do not appear in the model: {', '.join(unused)}"+ )++ pairs: list[tuple[str, str, float]] = []+ for item in spec.get("correlations", []):+ if not isinstance(item, (list, tuple)) or len(item) != 3:+ raise CliError("each spec correlation must be [name_a, name_b, r]")+ pairs.append(+ (str(item[0]), str(item[1]), _common.as_finite(item[2], label="correlation"))+ )+ pairs += [parse_correlation(item) for item in arguments.correlation]++ correlations: dict[tuple[str, str], float] = {}+ for first, second, coefficient in pairs:+ for name in (first, second):+ if name not in supplied:+ raise CliError(f"correlation references unknown variable {name!r}")+ if not -1.0 <= coefficient <= 1.0:+ raise CliError("correlation coefficients must lie in [-1, 1]")+ key = (first, second) if first < second else (second, first)+ if key in correlations and correlations[key] != coefficient:+ raise CliError(f"conflicting correlations given for {key[0]} and {key[1]}")+ if coefficient != 0.0:+ correlations[key] = coefficient++ coverage = spec.get("coverage_probability", arguments.coverage)+ coverage = _common.as_finite(coverage, label="coverage_probability")+ if not 0 < coverage < 1:+ raise CliError("coverage probability must be strictly between 0 and 1")++ framework = gum_framework(tree, variables, correlations, coverage)+ document: dict[str, Any] = {+ "measurand": arguments.measurand or spec.get("measurand"),+ "unit": arguments.unit or spec.get("unit"),+ "expression": expression,+ "gum_framework": framework,+ "correlations": [+ {"inputs": list(key), "coefficient": value}+ for key, value in sorted(correlations.items())+ ],+ }+ sampling = None+ if not arguments.no_monte_carlo:+ sampling = monte_carlo(+ tree, variables, correlations, coverage, arguments.trials, arguments.seed+ )+ document["monte_carlo"] = sampling+ document["validation"] = validate_linearization(+ framework, sampling, arguments.significant_digits+ )+ document["warnings"] = collect_warnings(framework, sampling, correlations)+ return document+++def main(argv: list[str] | None = None) -> int:+ arguments = build_parser().parse_args(argv)+ try:+ document = run(arguments)+ if arguments.format == "markdown":+ _common.emit_text(+ render_markdown(document),+ output=arguments.output,+ force=arguments.force,+ )+ else:+ _common.emit_json(+ document, output=arguments.output, force=arguments.force+ )+ except CliError as error:+ print(f"error: {error}", file=sys.stderr)+ return 2+ return 0+++if __name__ == "__main__":+ raise SystemExit(main())diff --git a/skills/uncertainty-and-units/scripts/uncertainty_budget.py b/skills/uncertainty-and-units/scripts/uncertainty_budget.pynew file mode 100644index 00000000..3882fa4e--- /dev/null+++ b/skills/uncertainty-and-units/scripts/uncertainty_budget.py@@ -0,0 +1,363 @@+#!/usr/bin/env python3+"""Turn a list of uncertainty components into a GUM uncertainty budget.++Each component arrives the way it is actually stated on a certificate, a data+sheet, or a repeatability worksheet. The divisor that converts it to a standard+uncertainty depends on which of those it is, and getting that divisor wrong is+the most common defect in a real budget. This CLI applies JCGM 100:2008 4.3,+combines the components, and reports the Welch-Satterthwaite effective degrees+of freedom and the resulting coverage factor.+"""++from __future__ import annotations++import argparse+import math+import sys+from typing import Any++import _common+from _common import CliError+++TEMPLATE: dict[str, Any] = {+ "measurand": "example flow rate",+ "unit": "L/min",+ "value": 12.345,+ "coverage_probability": 0.95,+ "components": [+ {+ "label": "repeatability of ten readings",+ "type": "A",+ "distribution": "normal",+ "value": 0.052,+ "dof": 9,+ "note": "experimental standard deviation of the mean",+ },+ {+ "label": "calibration certificate",+ "type": "B",+ "distribution": "expanded",+ "value": 0.10,+ "coverage_factor": 2.0,+ },+ {+ "label": "display resolution",+ "type": "B",+ "distribution": "rectangular",+ "value": 0.005,+ "note": "half-width equals half of the last displayed digit",+ },+ {+ "label": "long-term drift since calibration",+ "type": "B",+ "distribution": "rectangular",+ "value": 0.04,+ },+ {+ "label": "temperature correction",+ "type": "B",+ "distribution": "triangular",+ "value": 0.03,+ "sensitivity": 0.8,+ },+ ],+}+++def normalize_component(raw: Any, index: int, measurand: float | None) -> dict[str, Any]:+ """Validate one budget component and reduce it to a standard uncertainty."""++ if not isinstance(raw, dict):+ raise CliError(f"component {index + 1} must be a JSON object")+ label = raw.get("label") or f"component {index + 1}"+ if not isinstance(label, str) or len(label) > 200:+ raise CliError(f"component {index + 1}: label must be a string under 200 chars")++ kind = str(raw.get("type", "B")).strip().upper()+ if kind not in {"A", "B"}:+ raise CliError(f"{label}: evaluation type must be 'A' or 'B'")++ shape = raw.get("distribution")+ shape = "expanded" if str(shape).strip().lower() == "expanded" else shape+ if shape == "expanded":+ distribution = "expanded"+ else:+ distribution = _common.normalize_distribution(+ shape, label=f"{label}.distribution"+ )++ value = _common.as_finite(raw.get("value"), label=f"{label}.value")+ if value < 0:+ raise CliError(f"{label}: value must not be negative")++ relative = bool(raw.get("relative", False))+ if relative:+ if measurand is None:+ raise CliError(+ f"{label}: a relative component needs a top-level measurand 'value'"+ )+ value = value * abs(measurand)++ if distribution == "expanded":+ factor = _common.as_finite(+ raw.get("coverage_factor", 2.0), label=f"{label}.coverage_factor"+ )+ if factor <= 0:+ raise CliError(f"{label}: coverage_factor must be greater than zero")+ divisor = factor+ else:+ divisor = _common.DISTRIBUTION_DIVISORS[distribution]++ if "divisor" in raw:+ divisor = _common.as_finite(raw["divisor"], label=f"{label}.divisor")+ if divisor <= 0:+ raise CliError(f"{label}: divisor must be greater than zero")++ sensitivity = _common.as_finite(+ raw.get("sensitivity", 1.0), label=f"{label}.sensitivity"+ )+ dof = _common.as_degrees_of_freedom(raw.get("dof"), label=f"{label}.dof")+ standard = value / divisor+ note = raw.get("note")+ if note is not None and (not isinstance(note, str) or len(note) > 500):+ raise CliError(f"{label}: note must be a string under 500 chars")++ return {+ "label": label,+ "type": kind,+ "distribution": distribution,+ "stated_value": value,+ "divisor": divisor,+ "standard_uncertainty": standard,+ "sensitivity": sensitivity,+ "contribution": sensitivity * standard,+ "dof": dof,+ "note": note,+ }+++def build_budget(spec: dict[str, Any], coverage_override: float | None) -> dict[str, Any]:+ """Combine components into u_c, effective degrees of freedom, and U."""++ raw_components = spec.get("components")+ if not isinstance(raw_components, list) or not raw_components:+ raise CliError("the spec must contain a non-empty 'components' list")+ if len(raw_components) > _common.MAX_COMPONENTS:+ raise CliError(f"at most {_common.MAX_COMPONENTS} components are supported")++ measurand_value = spec.get("value")+ if measurand_value is not None:+ measurand_value = _common.as_finite(measurand_value, label="value")++ components = [+ normalize_component(raw, index, measurand_value)+ for index, raw in enumerate(raw_components)+ ]++ variance = sum(item["contribution"] ** 2 for item in components)+ combined = math.sqrt(variance)+ for item in components:+ item["variance_fraction"] = (+ (item["contribution"] ** 2) / variance if variance > 0 else 0.0+ )++ coverage = coverage_override+ if coverage is None:+ coverage = _common.as_finite(+ spec.get("coverage_probability", 0.95), label="coverage_probability"+ )+ if not 0 < coverage < 1:+ raise CliError("coverage probability must be strictly between 0 and 1")++ effective_dof = _common.welch_satterthwaite(+ combined, [(abs(item["contribution"]), item["dof"]) for item in components]+ )+ factor = _common.coverage_factor(effective_dof, coverage)+ expanded = factor * combined++ result: dict[str, Any] = {+ "measurand": spec.get("measurand"),+ "unit": spec.get("unit"),+ "value": measurand_value,+ "components": components,+ "combined_standard_uncertainty": combined,+ "effective_degrees_of_freedom": (+ None if not math.isfinite(effective_dof) else effective_dof+ ),+ "coverage_probability": coverage,+ "coverage_factor": factor,+ "expanded_uncertainty": expanded,+ }+ if measurand_value is not None:+ result["coverage_interval"] = [+ measurand_value - expanded,+ measurand_value + expanded,+ ]+ if measurand_value != 0:+ result["relative_standard_uncertainty"] = combined / abs(measurand_value)+ result["warnings"] = collect_warnings(result)+ for item in components:+ item["dof"] = _common.json_dof(item["dof"])+ return result+++def collect_warnings(budget: dict[str, Any]) -> list[str]:+ """Flag budget defects that change how the result should be reported."""++ warnings: list[str] = []+ components = budget["components"]+ for item in components:+ if item["type"] == "A" and not math.isfinite(item["dof"]):+ warnings.append(+ f"{item['label']}: a Type A component evaluated from n readings has "+ "n-1 degrees of freedom; leaving it infinite understates the "+ "coverage factor"+ )+ if (+ item["type"] == "B"+ and item["distribution"] == "normal"+ and item["divisor"] == 1.0+ ):+ warnings.append(+ f"{item['label']}: a Type B component declared normal is divided by "+ "1, so the stated value is taken as a standard uncertainty; "+ "certificates normally quote an expanded U, which needs "+ "distribution 'expanded' with its coverage_factor"+ )+ dof = budget["effective_degrees_of_freedom"]+ if dof is not None and dof < 6:+ warnings.append(+ f"effective degrees of freedom is {dof:.1f}; k = "+ f"{budget['coverage_factor']:.2f} rather than the customary 2, and the "+ "interval is sensitive to one small sample"+ )+ if components:+ dominant = max(components, key=lambda item: item["variance_fraction"])+ if dominant["variance_fraction"] > 0.9:+ warnings.append(+ f"{dominant['label']} contributes "+ f"{dominant['variance_fraction'] * 100:.1f}% of the variance; "+ "improving any other component cannot change the result"+ )+ largest = max(abs(item["contribution"]) for item in components)+ negligible = [+ item["label"]+ for item in components+ if largest > 0 and abs(item["contribution"]) < largest / 3.0+ ]+ if negligible:+ warnings.append(+ "these components are below one third of the largest and change u_c "+ f"by under 6%: {', '.join(negligible)}"+ )+ return warnings+++def render_markdown(budget: dict[str, Any]) -> str:+ """Render the budget as a Markdown table plus the combined result."""++ unit = f" {budget['unit']}" if budget.get("unit") else ""+ lines = [+ f"# Uncertainty budget: {budget.get('measurand') or 'measurand'}",+ "",+ "| Component | Type | Distribution | Stated | Divisor | u(x) | c | "+ "c*u(x) | % variance | dof |",+ "| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |",+ ]+ for item in budget["components"]:+ dof = _common.format_dof(item["dof"])+ lines.append(+ f"| {item['label']} | {item['type']} | {item['distribution']} | "+ f"{item['stated_value']:.6g} | {item['divisor']:.4g} | "+ f"{item['standard_uncertainty']:.4g} | {item['sensitivity']:.4g} | "+ f"{item['contribution']:.4g} | {item['variance_fraction'] * 100:.1f} | "+ f"{dof} |"+ )+ effective = budget["effective_degrees_of_freedom"]+ lines += [+ "",+ "## Combined result",+ "",+ f"- Combined standard uncertainty u_c: "+ f"{budget['combined_standard_uncertainty']:.6g}{unit}",+ f"- Effective degrees of freedom: "+ f"{'infinite' if effective is None else f'{effective:.1f}'}",+ f"- Coverage factor k: {budget['coverage_factor']:.4g} at p = "+ f"{budget['coverage_probability']:.3g}",+ f"- Expanded uncertainty U: {budget['expanded_uncertainty']:.6g}{unit}",+ ]+ if budget.get("value") is not None:+ low, high = budget["coverage_interval"]+ lines.append(+ f"- Result: {budget['value']:.6g} +/- "+ f"{budget['expanded_uncertainty']:.6g}{unit} "+ f"[{low:.6g}, {high:.6g}]"+ )+ if budget.get("warnings"):+ lines += ["", "## Warnings", ""]+ lines += [f"- {message}" for message in budget["warnings"]]+ lines += [+ "",+ "Components are combined in quadrature, which assumes they are "+ "uncorrelated (JCGM 100:2008 equation 10). Use "+ "`propagate_uncertainty.py --correlation` when they are not.",+ ]+ return "\n".join(lines) + "\n"+++def build_parser() -> argparse.ArgumentParser:+ parser = argparse.ArgumentParser(+ description=(+ "Combine stated uncertainty components into a GUM budget with "+ "distribution-aware divisors and a Welch-Satterthwaite coverage factor."+ )+ )+ parser.add_argument("--spec", help="JSON file describing the budget")+ parser.add_argument(+ "--template",+ action="store_true",+ help="print a worked example spec and exit",+ )+ parser.add_argument(+ "--coverage",+ type=_common.probability,+ help="override the coverage probability in the spec",+ )+ parser.add_argument("--format", choices=("json", "markdown"), default="json")+ parser.add_argument("--output", help="write the report to this file")+ parser.add_argument(+ "--force", action="store_true", help="overwrite an existing output file"+ )+ return parser+++def main(argv: list[str] | None = None) -> int:+ arguments = build_parser().parse_args(argv)+ try:+ if arguments.template:+ _common.emit_json(+ TEMPLATE, output=arguments.output, force=arguments.force+ )+ return 0+ spec = TEMPLATE+ if arguments.spec:+ loaded = _common.load_json(arguments.spec)+ if not isinstance(loaded, dict):+ raise CliError("the spec file must hold a JSON object")+ spec = loaded+ budget = build_budget(spec, arguments.coverage)+ if arguments.format == "markdown":+ _common.emit_text(+ render_markdown(budget), output=arguments.output, force=arguments.force+ )+ else:+ _common.emit_json(budget, output=arguments.output, force=arguments.force)+ except CliError as error:+ print(f"error: {error}", file=sys.stderr)+ return 2+ return 0+++if __name__ == "__main__":+ raise SystemExit(main())diff --git a/tests/skill-requirements.toml b/tests/skill-requirements.tomlindex 33b53d1b..85a62ce3 100644--- a/tests/skill-requirements.toml+++ b/tests/skill-requirements.toml@@ -193,6 +193,9 @@ packages = []# --- core data, statistics, and plotting ----------------------------------+[skills.uncertainty-and-units]+packages = ["pint", "uncertainties", "numpy", "scipy"]+[skills.matplotlib]packages = ["matplotlib", "ipympl", "numpy", "pandas", "scipy"]diff --git a/tests/uncertainty-and-units/test_scripts.py b/tests/uncertainty-and-units/test_scripts.pynew file mode 100644index 00000000..35e51976--- /dev/null+++ b/tests/uncertainty-and-units/test_scripts.py@@ -0,0 +1,1077 @@+#!/usr/bin/env python3+"""Synthetic, network-free tests for the uncertainty-and-units helper CLIs."""++from __future__ import annotations++import ast+import json+import math+import stat+import sys+import tempfile+import unittest+from pathlib import Path+++SKILL_ROOT = Path(__file__).resolve().parents[2] / "skills" / "uncertainty-and-units"+SCRIPTS = SKILL_ROOT / "scripts"+sys.path.insert(0, str(SCRIPTS))++import _common # noqa: E402+import audit_units # noqa: E402+import check_plausibility # noqa: E402+import convert_units # noqa: E402+import format_result # noqa: E402+import propagate_uncertainty # noqa: E402+import uncertainty_budget # noqa: E402++MODULES = (+ audit_units,+ check_plausibility,+ convert_units,+ format_result,+ propagate_uncertainty,+ uncertainty_budget,+)+++def _available(name: str) -> bool:+ """Report whether an optional scientific package can be imported."""++ try:+ __import__(name)+ except ImportError:+ return False+ return True+++HAS_NUMPY = _available("numpy")+HAS_SCIPY = _available("scipy")+HAS_PINT = _available("pint")+HAS_UNCERTAINTIES = _available("uncertainties")+++HAZARD_SOURCE = '''+import math+import numpy as np+import pint+from scipy.optimize import curve_fit+from uncertainties import ufloat++ureg = pint.UnitRegistry()+spare = pint.UnitRegistry()++R_GAS = 8.314462618+++def analyse(readings, xs, ys, sigma):+ spread = np.std(readings)+ length = (12.7 * ureg.mm).magnitude+ safe = (12.7 * ureg.mm).to("m").magnitude+ bath = ureg.Quantity(37.0, "degC")+ gain = ureg.Quantity(3.0, "dB")+ popt, pcov = curve_fit(lambda x, a, b: a * x + b, xs, ys, sigma=sigma)+ slope = ufloat(popt[0], 0.1)+ copy = ufloat(slope.nominal_value, slope.std_dev)+ return math.log(2.0) + spread + length + safe + R_GAS + bath + gain + copy+'''++CLEAN_SOURCE = '''+import numpy as np+import pint+from scipy.optimize import curve_fit++ureg = pint.get_application_registry()+++def analyse(readings, xs, ys, sigma):+ spread = np.std(readings, ddof=1)+ length = (12.7 * ureg.mm).m_as("m")+ popt, pcov = curve_fit(lambda x, a, b: a * x + b, xs, ys, sigma=sigma,+ absolute_sigma=True)+ return spread + length + popt[0] + pcov[0, 0]+'''+++class CommonSafetyTests(unittest.TestCase):+ def test_json_output_is_private_and_refuses_overwrite(self) -> None:+ with tempfile.TemporaryDirectory() as directory:+ output = Path(directory) / "report.json"+ _common.emit_json({"ok": True}, output=output)+ self.assertEqual(json.loads(output.read_text()), {"ok": True})+ self.assertEqual(stat.S_IMODE(output.stat().st_mode), 0o600)+ with self.assertRaises(_common.CliError):+ _common.emit_json({"ok": False}, output=output)+ _common.emit_json({"ok": False}, output=output, force=True)+ self.assertEqual(json.loads(output.read_text()), {"ok": False})++ def test_local_paths_reject_urls_and_symlinks(self) -> None:+ with self.assertRaises(_common.CliError):+ _common.checked_input_file(+ "https://example.invalid/model.json", suffixes={".json"}+ )+ with tempfile.TemporaryDirectory() as directory:+ root = Path(directory)+ real = root / "model.json"+ real.write_text("{}", encoding="utf-8")+ link = root / "link.json"+ try:+ link.symlink_to(real)+ except OSError:+ self.skipTest("symlinks unavailable")+ with self.assertRaises(_common.CliError):+ _common.checked_input_file(link, suffixes={".json"})++ def test_no_shadow_modules_network_or_environment_reads(self) -> None:+ filenames = {path.name for path in SCRIPTS.glob("*.py")}+ for shadowed in ("pint.py", "uncertainties.py", "numpy.py", "scipy.py"):+ self.assertNotIn(shadowed, filenames)+ banned = {"aiohttp", "httpx", "requests", "socket", "urllib", "subprocess"}+ for path in SCRIPTS.glob("*.py"):+ tree = ast.parse(path.read_text(encoding="utf-8"), filename=str(path))+ for node in ast.walk(tree):+ if isinstance(node, ast.Import):+ roots = {alias.name.split(".", 1)[0] for alias in node.names}+ self.assertFalse(roots & banned, path.name)+ if isinstance(node, ast.ImportFrom) and node.module:+ self.assertNotIn(node.module.split(".", 1)[0], banned, path.name)+ if isinstance(node, ast.Attribute):+ self.assertFalse(+ isinstance(node.value, ast.Name)+ and node.value.id == "os"+ and node.attr in {"environ", "getenv"},+ path.name,+ )++ def test_skill_directory_ships_no_bytecode_or_tests(self) -> None:+ self.assertEqual(list(SKILL_ROOT.rglob("*.pyc")), [])+ self.assertEqual(list(SKILL_ROOT.rglob("__pycache__")), [])+ self.assertEqual(list(SKILL_ROOT.rglob("test_*.py")), [])+ self.assertFalse((SKILL_ROOT / "tests").exists())++ def test_help_is_available_without_running_workflows(self) -> None:+ for module in MODULES:+ with self.subTest(module=module.__name__):+ help_text = module.build_parser().format_help()+ self.assertIn("usage:", help_text)+ self.assertIn("--help", help_text)+++class ExpressionParsingTests(unittest.TestCase):+ def test_accepts_arithmetic_and_whitelisted_functions(self) -> None:+ tree = _common.parse_expression("sqrt(a**2 + b**2) / (pi * c)")+ self.assertEqual(_common.expression_variables(tree), ["a", "b", "c"])++ def test_rejects_everything_that_is_not_arithmetic(self) -> None:+ hostile = [+ "__import__('os').system('true')",+ "open('secret')",+ "a.b",+ "a if b else c",+ "[x for x in range(3)]",+ "lambda x: x",+ "a == b",+ "os.environ",+ "eval('1')",+ "a; b",+ "(a := 2)",+ ]+ for expression in hostile:+ with self.subTest(expression=expression):+ with self.assertRaises(_common.CliError):+ _common.parse_expression(expression)++ def test_rejects_unbounded_expressions(self) -> None:+ with self.assertRaises(_common.CliError):+ _common.parse_expression("x" * (_common.MAX_EXPRESSION_CHARS + 1))+ with self.assertRaises(_common.CliError):+ _common.parse_expression("x ** 1000")+ with self.assertRaises(_common.CliError):+ _common.parse_expression("sqrt(" * 40 + "x" + ")" * 40)+ with self.assertRaises(_common.CliError):+ _common.parse_expression("_hidden + 1")++ def test_reduction_uses_supplied_functions_only(self) -> None:+ tree = _common.parse_expression("sqrt(x) + 1")+ value = _common.reduce_expression(tree, {"x": 9.0}, {"sqrt": math.sqrt})+ self.assertAlmostEqual(value, 4.0)+ with self.assertRaises(_common.CliError):+ _common.reduce_expression(tree, {"x": 9.0}, {})+ with self.assertRaises(_common.CliError):+ _common.reduce_expression(tree, {}, {"sqrt": math.sqrt})+++class MetrologyMathTests(unittest.TestCase):+ def test_welch_satterthwaite_matches_the_published_formula(self) -> None:+ # Two components of 0.03 with 9 dof each: nu_eff = 4 * 9 / 2 = 18.+ combined = math.sqrt(0.03**2 + 0.03**2)+ dof = _common.welch_satterthwaite(combined, [(0.03, 9), (0.03, 9)])+ self.assertAlmostEqual(dof, 18.0)++ def test_infinite_degrees_of_freedom_drop_out(self) -> None:+ combined = math.sqrt(0.03**2 + 0.04**2)+ dof = _common.welch_satterthwaite(+ combined, [(0.03, math.inf), (0.04, math.inf)]+ )+ self.assertEqual(dof, math.inf)++ def test_coverage_factor_matches_the_normal_limit(self) -> None:+ self.assertAlmostEqual(_common.coverage_factor(math.inf, 0.95), 1.959964, 5)++ @unittest.skipUnless(HAS_SCIPY, "SciPy is required for Student-t quantiles")+ def test_coverage_factor_follows_the_t_distribution(self) -> None:+ self.assertAlmostEqual(_common.coverage_factor(9, 0.95), 2.262157, 5)+ self.assertAlmostEqual(_common.coverage_factor(2, 0.95), 4.302653, 5)++ def test_numerical_tolerance_is_half_the_last_retained_digit(self) -> None:+ self.assertAlmostEqual(_common.numerical_tolerance(0.0472, 2), 0.0005)+ self.assertAlmostEqual(_common.numerical_tolerance(0.0472, 1), 0.005)+ with self.assertRaises(_common.CliError):+ _common.numerical_tolerance(0.0, 2)++ @unittest.skipUnless(HAS_NUMPY, "NumPy is required")+ def test_shortest_interval_finds_the_narrowest_window(self) -> None:+ import numpy as np++ sample = np.sort(np.concatenate([np.linspace(0.0, 1.0, 99), [50.0]]))+ low, high = _common.shortest_coverage_interval(sample, 0.9)+ self.assertLess(high, 2.0)+ self.assertGreaterEqual(low, 0.0)++ def test_distribution_divisors_follow_the_gum(self) -> None:+ self.assertAlmostEqual(_common.DISTRIBUTION_DIVISORS["rectangular"], 3**0.5)+ self.assertAlmostEqual(_common.DISTRIBUTION_DIVISORS["triangular"], 6**0.5)+ self.assertAlmostEqual(_common.DISTRIBUTION_DIVISORS["arcsine"], 2**0.5)+++class FormatResultTests(unittest.TestCase):+ def test_value_is_rounded_to_the_uncertainty_decimal_place(self) -> None:+ rounded = format_result.round_to_uncertainty(12.34567, 0.02345)+ self.assertEqual(str(rounded["value"]), "12.346")+ self.assertEqual(str(rounded["uncertainty"]), "0.023")+ self.assertEqual(rounded["decimal_place"], -3)++ def test_carrying_uncertainty_recomputes_the_decimal_place(self) -> None:+ rounded = format_result.round_to_uncertainty(0.09996, 0.0996)+ self.assertEqual(str(rounded["uncertainty"]), "0.10")+ self.assertEqual(str(rounded["value"]), "0.10")+ self.assertEqual(rounded["decimal_place"], -2)++ def test_one_significant_digit_is_honoured(self) -> None:+ rounded = format_result.round_to_uncertainty(+ 12.3456, 0.15, significant_digits=1+ )+ self.assertEqual(str(rounded["uncertainty"]), "0.2")+ self.assertEqual(str(rounded["value"]), "12.3")++ def test_concise_notation_matches_the_plus_minus_form(self) -> None:+ renderings = format_result.render(+ format_result.round_to_uncertainty(12.34567, 0.02345), None+ )+ self.assertEqual(renderings["plusminus"], "12.346 ± 0.023")+ self.assertEqual(renderings["parenthetic"], "12.346(23)")+ self.assertEqual(renderings["ascii"], "12.346 +/- 0.023")++ def test_digits_left_of_the_point_switch_to_scientific(self) -> None:+ renderings = format_result.render(+ format_result.round_to_uncertainty(1234.0, 250.0), None+ )+ self.assertEqual(renderings["plusminus"], "1230 ± 250")+ self.assertEqual(renderings["parenthetic"], "1.23(25)e+03")++ def test_extreme_exponents_avoid_a_run_of_zeros(self) -> None:+ renderings = format_result.render(+ format_result.round_to_uncertainty(9.1093837139e-31, 2.8e-40), "kg"+ )+ self.assertEqual(renderings["parenthetic"], "9.1093837139(28)e-31 kg")++ def test_exact_values_are_labelled(self) -> None:+ renderings = format_result.render(+ format_result.round_to_uncertainty(299792458.0, 0.0), "m/s"+ )+ self.assertIn("exact", renderings["plusminus"])++ def test_warnings_cover_the_reporting_traps(self) -> None:+ rounded = format_result.round_to_uncertainty(+ 12.3456, 0.15, significant_digits=1+ )+ warnings = format_result.collect_warnings(12.3456, 0.15, 1, rounded)+ self.assertTrue(any("one significant digit" in item for item in warnings))+ rounded = format_result.round_to_uncertainty(0.0012, 0.004)+ warnings = format_result.collect_warnings(0.0012, 0.004, 2, rounded)+ self.assertTrue(any("consistent with zero" in item for item in warnings))++ def test_bad_arguments_are_rejected(self) -> None:+ with self.assertRaises(_common.CliError):+ format_result.round_to_uncertainty(1.0, -1.0)+ with self.assertRaises(_common.CliError):+ format_result.round_to_uncertainty(1.0, 0.1, significant_digits=3)+ with self.assertRaises(_common.CliError):+ format_result.round_to_uncertainty(1.0, 0.1, rounding="up")++ def test_statement_names_the_coverage_factor(self) -> None:+ renderings = format_result.render(+ format_result.round_to_uncertainty(1.0, 0.1), None+ )+ self.assertIn("k = 1", format_result.build_statement(renderings, None, None))+ statement = format_result.build_statement(renderings, 2.26, 0.95)+ self.assertIn("k = 2.26", statement)+ self.assertIn("95%", statement)+++class BudgetTests(unittest.TestCase):+ def _spec(self) -> dict:+ return {+ "measurand": "test",+ "unit": "mV",+ "value": 100.0,+ "coverage_probability": 0.95,+ "components": [+ {+ "label": "repeatability",+ "type": "A",+ "distribution": "normal",+ "value": 0.03,+ "dof": 9,+ },+ {+ "label": "certificate",+ "type": "B",+ "distribution": "expanded",+ "value": 0.06,+ "coverage_factor": 2.0,+ },+ ],+ }++ def test_divisors_follow_the_declared_distribution(self) -> None:+ component = uncertainty_budget.normalize_component(+ {"label": "resolution", "distribution": "rectangular", "value": 0.01},+ 0,+ None,+ )+ self.assertAlmostEqual(component["standard_uncertainty"], 0.01 / math.sqrt(3))+ component = uncertainty_budget.normalize_component(+ {+ "label": "certificate",+ "distribution": "expanded",+ "value": 0.10,+ "coverage_factor": 2.0,+ },+ 1,+ None,+ )+ self.assertAlmostEqual(component["standard_uncertainty"], 0.05)++ def test_relative_components_need_a_measurand_value(self) -> None:+ with self.assertRaises(_common.CliError):+ uncertainty_budget.normalize_component(+ {"label": "gain", "value": 0.01, "relative": True}, 0, None+ )+ component = uncertainty_budget.normalize_component(+ {"label": "gain", "value": 0.01, "relative": True}, 0, 250.0+ )+ self.assertAlmostEqual(component["standard_uncertainty"], 2.5)++ @unittest.skipUnless(HAS_SCIPY, "SciPy is required for Student-t quantiles")+ def test_combined_uncertainty_and_effective_dof(self) -> None:+ budget = uncertainty_budget.build_budget(self._spec(), None)+ self.assertAlmostEqual(+ budget["combined_standard_uncertainty"], math.sqrt(0.03**2 + 0.03**2)+ )+ self.assertAlmostEqual(budget["effective_degrees_of_freedom"], 36.0)+ self.assertGreater(budget["coverage_factor"], 2.0)+ self.assertLess(budget["coverage_factor"], 2.1)++ @unittest.skipUnless(HAS_SCIPY, "SciPy is required for Student-t quantiles")+ def test_small_sample_warning_and_type_a_dof_warning(self) -> None:+ spec = self._spec()+ spec["components"][0]["dof"] = 1+ budget = uncertainty_budget.build_budget(spec, None)+ self.assertLess(budget["effective_degrees_of_freedom"], 6.0)+ self.assertTrue(+ any("degrees of freedom" in item for item in budget["warnings"])+ )+ spec = self._spec()+ del spec["components"][0]["dof"]+ budget = uncertainty_budget.build_budget(spec, None)+ self.assertTrue(any("Type A" in item for item in budget["warnings"]))++ @unittest.skipUnless(HAS_SCIPY, "SciPy is required for Student-t quantiles")+ def test_undivided_type_b_normal_component_is_flagged(self) -> None:+ spec = self._spec()+ spec["components"][1] = {+ "label": "certificate",+ "type": "B",+ "distribution": "normal",+ "value": 0.06,+ }+ budget = uncertainty_budget.build_budget(spec, None)+ self.assertTrue(any("expanded" in item for item in budget["warnings"]))++ @unittest.skipUnless(HAS_SCIPY, "SciPy is required for Student-t quantiles")+ def test_report_is_strict_json_and_renders_markdown(self) -> None:+ budget = uncertainty_budget.build_budget(self._spec(), None)+ payload = json.dumps(budget, allow_nan=False)+ self.assertIn("combined_standard_uncertainty", payload)+ report = uncertainty_budget.render_markdown(budget)+ self.assertIn("| Component |", report)+ self.assertIn("Combined result", report)++ def test_malformed_specs_are_rejected(self) -> None:+ with self.assertRaises(_common.CliError):+ uncertainty_budget.build_budget({"components": []}, None)+ with self.assertRaises(_common.CliError):+ uncertainty_budget.build_budget({"components": ["nope"]}, None)+ with self.assertRaises(_common.CliError):+ uncertainty_budget.build_budget(+ {"components": [{"value": 1.0, "divisor": 0.0}]}, None+ )+++ HAS_NUMPY and HAS_SCIPY and HAS_UNCERTAINTIES,+ "NumPy, SciPy, and uncertainties are required",+)+class PropagationTests(unittest.TestCase):+ def _framework(self, expression: str, variables, correlations=None):+ tree = _common.parse_expression(expression)+ records = propagate_uncertainty.normalize_variables(variables)+ return propagate_uncertainty.gum_framework(+ tree, records, correlations or {}, 0.95+ )++ def test_linear_model_matches_quadrature(self) -> None:+ framework = self._framework(+ "a + b",+ [+ {"name": "a", "value": 1.0, "standard_uncertainty": 0.3},+ {"name": "b", "value": 2.0, "standard_uncertainty": 0.4},+ ],+ )+ self.assertAlmostEqual(framework["value"], 3.0)+ self.assertAlmostEqual(framework["combined_standard_uncertainty"], 0.5)+ self.assertEqual(framework["covariance_term"], 0.0)++ def test_sensitivity_coefficients_are_analytic_partials(self) -> None:+ framework = self._framework(+ "a * b",+ [+ {"name": "a", "value": 3.0, "standard_uncertainty": 0.1},+ {"name": "b", "value": 5.0, "standard_uncertainty": 0.2},+ ],+ )+ sensitivities = {+ item["name"]: item["sensitivity"] for item in framework["inputs"]+ }+ self.assertAlmostEqual(sensitivities["a"], 5.0)+ self.assertAlmostEqual(sensitivities["b"], 3.0)++ def test_correlation_reduces_the_uncertainty_of_a_difference(self) -> None:+ framework = self._framework(+ "a - b",+ [+ {"name": "a", "value": 10.0, "standard_uncertainty": 0.3},+ {"name": "b", "value": 9.5, "standard_uncertainty": 0.3},+ ],+ {("a", "b"): 0.9},+ )+ expected = math.sqrt(0.3**2 + 0.3**2 - 2 * 0.9 * 0.3 * 0.3)+ self.assertAlmostEqual(framework["combined_standard_uncertainty"], expected)+ self.assertLess(framework["covariance_term"], 0.0)++ def test_monte_carlo_agrees_with_the_framework_on_a_linear_model(self) -> None:+ tree = _common.parse_expression("a + b")+ records = propagate_uncertainty.normalize_variables(+ [+ {"name": "a", "value": 1.0, "standard_uncertainty": 0.3},+ {"name": "b", "value": 2.0, "standard_uncertainty": 0.4},+ ]+ )+ framework = propagate_uncertainty.gum_framework(tree, records, {}, 0.95)+ sampling = propagate_uncertainty.monte_carlo(+ tree, records, {}, 0.95, _common.RECOMMENDED_TRIALS, 7+ )+ self.assertAlmostEqual(sampling["standard_uncertainty"], 0.5, places=2)+ validation = propagate_uncertainty.validate_linearization(+ framework, sampling, 2+ )+ self.assertTrue(validation["gum_framework_validated"])+ self.assertEqual(+ propagate_uncertainty.collect_warnings(framework, sampling, {}), []+ )++ def test_nonlinear_model_fails_the_clause_eight_check(self) -> None:+ tree = _common.parse_expression("x ** 2")+ records = propagate_uncertainty.normalize_variables(+ [{"name": "x", "value": 1.0, "standard_uncertainty": 0.5}]+ )+ framework = propagate_uncertainty.gum_framework(tree, records, {}, 0.95)+ sampling = propagate_uncertainty.monte_carlo(+ tree, records, {}, 0.95, 200_000, 11+ )+ validation = propagate_uncertainty.validate_linearization(+ framework, sampling, 2+ )+ self.assertFalse(validation["gum_framework_validated"])+ self.assertLess(framework["coverage_interval"][0], 0.0)+ self.assertGreaterEqual(sampling["shortest_coverage_interval"][0], 0.0)+ warnings = propagate_uncertainty.collect_warnings(framework, sampling, {})+ self.assertTrue(any("nonlinear" in item for item in warnings))++ def test_correlated_sampling_needs_normal_inputs(self) -> None:+ tree = _common.parse_expression("a - b")+ records = propagate_uncertainty.normalize_variables(+ [+ {+ "name": "a",+ "value": 1.0,+ "standard_uncertainty": 0.1,+ "distribution": "rectangular",+ },+ {"name": "b", "value": 1.0, "standard_uncertainty": 0.1},+ ]+ )+ with self.assertRaises(_common.CliError):+ propagate_uncertainty.monte_carlo(+ tree, records, {("a", "b"): 0.5}, 0.95, 1_000, 3+ )++ def test_impossible_correlation_matrix_is_rejected(self) -> None:+ tree = _common.parse_expression("a + b + c")+ records = propagate_uncertainty.normalize_variables(+ [+ {"name": name, "value": 1.0, "standard_uncertainty": 0.1}+ for name in ("a", "b", "c")+ ]+ )+ with self.assertRaises(_common.CliError):+ propagate_uncertainty.monte_carlo(+ tree,+ records,+ {("a", "b"): 0.99, ("a", "c"): 0.99, ("b", "c"): -0.99},+ 0.95,+ 1_000,+ 3,+ )++ def test_variable_specification_parsing(self) -> None:+ record = propagate_uncertainty.parse_variable("d=20.0,0.02,rectangular,inf")+ self.assertEqual(record["name"], "d")+ self.assertEqual(record["distribution"], "rectangular")+ self.assertIsNone(record["dof"])+ with self.assertRaises(_common.CliError):+ propagate_uncertainty.parse_variable("d 20.0")+ with self.assertRaises(_common.CliError):+ propagate_uncertainty.parse_variable("d=20.0")+ with self.assertRaises(_common.CliError):+ propagate_uncertainty.normalize_variables(+ [{"name": "pi", "value": 1.0, "standard_uncertainty": 0.1}]+ )+ with self.assertRaises(_common.CliError):+ propagate_uncertainty.normalize_variables(+ [+ {"name": "a", "value": 1.0, "standard_uncertainty": 0.1},+ {"name": "a", "value": 2.0, "standard_uncertainty": 0.1},+ ]+ )++ def test_cli_rejects_a_model_with_missing_or_unused_inputs(self) -> None:+ parser = propagate_uncertainty.build_parser()+ arguments = parser.parse_args(+ ["--expression", "a + b", "--variable", "a=1.0,0.1"]+ )+ with self.assertRaises(_common.CliError):+ propagate_uncertainty.run(arguments)+ arguments = parser.parse_args(+ [+ "--expression",+ "a",+ "--variable",+ "a=1.0,0.1",+ "--variable",+ "b=1.0,0.1",+ ]+ )+ with self.assertRaises(_common.CliError):+ propagate_uncertainty.run(arguments)++ def test_end_to_end_report_is_strict_json(self) -> None:+ parser = propagate_uncertainty.build_parser()+ arguments = parser.parse_args(+ [+ "--expression",+ "m / (pi * (d / 2) ** 2 * h)",+ "--variable",+ "m=250.0,0.05",+ "--variable",+ "d=20.0,0.02,rectangular",+ "--variable",+ "h=40.0,0.05,rectangular",+ "--trials",+ "20000",+ ]+ )+ document = propagate_uncertainty.run(arguments)+ json.dumps(document, allow_nan=False)+ self.assertIn("validation", document)+ report = propagate_uncertainty.render_markdown(document)+ self.assertIn("Uncertainty budget", report)++[email protected](HAS_PINT, "pint is required")+class ConversionTests(unittest.TestCase):+ def setUp(self) -> None:+ self.registry = convert_units.build_registry()++ def test_plain_conversion_reports_a_factor(self) -> None:+ parser = convert_units.build_parser()+ arguments = parser.parse_args(["--value", "1", "--unit", "mm", "--to", "m"])+ document = convert_units.run(arguments)+ self.assertAlmostEqual(document["value"], 0.001)+ self.assertAlmostEqual(document["conversion_factor"], 0.001)++ def test_context_is_required_and_named_in_the_error(self) -> None:+ parser = convert_units.build_parser()+ arguments = parser.parse_args(["--value", "532", "--unit", "nm", "--to", "eV"])+ with self.assertRaises(_common.CliError) as caught:+ convert_units.run(arguments)+ self.assertIn("spectroscopy", str(caught.exception))++ def test_spectroscopy_context_converts_and_propagates(self) -> None:+ parser = convert_units.build_parser()+ arguments = parser.parse_args(+ [+ "--value",+ "532",+ "--unit",+ "nm",+ "--to",+ "eV",+ "--context",+ "spectroscopy",+ "--uncertainty",+ "0.5",+ ]+ )+ document = convert_units.run(arguments)+ self.assertAlmostEqual(document["value"], 2.3305300457, places=7)+ # E = hc/lambda, so u(E) = E * u(lambda) / lambda.+ self.assertAlmostEqual(+ document["uncertainty"], 2.3305300457 * 0.5 / 532.0, places=7+ )++ def test_offset_units_convert_the_value_and_the_difference_apart(self) -> None:+ parser = convert_units.build_parser()+ arguments = parser.parse_args(+ [+ "--value",+ "20",+ "--unit",+ "degC",+ "--to",+ "degF",+ "--uncertainty",+ "0.5",+ ]+ )+ document = convert_units.run(arguments)+ self.assertAlmostEqual(document["value"], 68.0, places=6)+ self.assertAlmostEqual(document["uncertainty"], 0.9, places=6)+ self.assertTrue(any("offset unit" in item for item in document["warnings"]))+ self.assertNotIn("conversion_factor", document)++ def test_unit_strings_are_bounded_and_screened(self) -> None:+ for hostile in ["", "m; import os", "m" * 200, "__class__", "lambda"]:+ with self.subTest(unit=hostile):+ with self.assertRaises(_common.CliError):+ convert_units.checked_unit(hostile, label="--unit")++ def test_unknown_units_raise_a_cli_error(self) -> None:+ parser = convert_units.build_parser()+ arguments = parser.parse_args(+ ["--value", "1", "--unit", "smoots", "--to", "m"]+ )+ with self.assertRaises(_common.CliError):+ convert_units.run(arguments)+++class AuditTests(unittest.TestCase):+ def _findings(self, source: str) -> list[dict]:+ return audit_units.audit_source(source, "sample.py")["findings"]++ def _rules(self, source: str) -> set[str]:+ return {finding["rule"] for finding in self._findings(source)}++ def test_every_rule_fires_on_the_hazard_fixture(self) -> None:+ rules = self._rules(HAZARD_SOURCE)+ for expected in (+ "UNIT001",+ "UNIT002",+ "UNIT003",+ "UNIT004",+ "UNC001",+ "UNC002",+ "UNC003",+ "UNC004",+ "CONST001",+ ):+ with self.subTest(rule=expected):+ self.assertIn(expected, rules)++ def test_clean_source_produces_no_findings(self) -> None:+ self.assertEqual(self._findings(CLEAN_SOURCE), [])++ def test_the_skills_own_scripts_are_clean(self) -> None:+ for path in sorted(SCRIPTS.glob("*.py")):+ with self.subTest(script=path.name):+ audited = audit_units.audit_source(+ path.read_text(encoding="utf-8"), path.name+ )+ self.assertEqual(audited["findings"], [])++ def test_magnitude_after_an_explicit_conversion_is_accepted(self) -> None:+ self.assertEqual(self._findings('q = thing.to("m").magnitude\n'), [])+ findings = self._findings("q = thing.magnitude\n")+ self.assertEqual([item["rule"] for item in findings], ["UNIT003"])++ def test_directive_comments_suppress_and_are_counted(self) -> None:+ audited = audit_units.audit_source(+ "q = thing.magnitude # audit-units: ignore UNIT003\n", "sample.py"+ )+ self.assertEqual(audited["findings"], [])+ self.assertEqual(audited["suppressed"], 1)++ audited = audit_units.audit_source(+ "q = thing.magnitude # audit-units: ignore CONST001\n", "sample.py"+ )+ self.assertEqual([item["rule"] for item in audited["findings"]], ["UNIT003"])+ self.assertEqual(audited["suppressed"], 0)++ audited = audit_units.audit_source(+ "q = thing.magnitude # audit-units: ignore\n", "sample.py"+ )+ self.assertEqual(audited["findings"], [])++ audited = audit_units.audit_source(+ "# audit-units: ignore-file UNIT003\nq = thing.magnitude\n", "sample.py"+ )+ self.assertEqual(audited["findings"], [])+ self.assertEqual(audited["suppressed"], 1)++ def test_suppression_parsing(self) -> None:+ per_line, whole_file = audit_units.parse_suppressions(+ "a = 1 # audit-units: ignore UNIT003, CONST001\n"+ "# audit-units: ignore-file UNC001\n"+ "b = 2 # audit-units: ignore\n"+ )+ self.assertEqual(per_line[1], {"UNIT003", "CONST001"})+ self.assertEqual(per_line[3], {"*"})+ self.assertEqual(whole_file, {"UNC001"})++ def test_a_standalone_directive_covers_the_next_line(self) -> None:+ audited = audit_units.audit_source(+ "# audit-units: ignore UNIT003\nq = thing.magnitude\n", "sample.py"+ )+ self.assertEqual(audited["findings"], [])+ self.assertEqual(audited["suppressed"], 1)+ audited = audit_units.audit_source(+ "# audit-units: ignore UNIT003\n\nq = thing.magnitude\n", "sample.py"+ )+ self.assertEqual([item["rule"] for item in audited["findings"]], ["UNIT003"])++ def test_delta_units_silence_the_offset_warning(self) -> None:+ self.assertIn("UNIT002", self._rules('bath = Q(37.0, "degC")\n'))+ self.assertNotIn(+ "UNIT002",+ self._rules('bath = Q(37.0, "degC") + Q(1.0, "delta_degC")\n'),+ )++ def test_constant_detection_needs_three_significant_digits(self) -> None:+ self.assertIn("CONST001", self._rules("g = 9.81\n"))+ self.assertNotIn("CONST001", self._rules("g = 9.8\n"))+ self.assertIn("CONST001", self._rules("c = 299792458\n"))+ self.assertNotIn("CONST001", self._rules("scale = 4.2\n"))++ def test_significant_digit_counting(self) -> None:+ self.assertEqual(audit_units.significant_digits(9.8), 2)+ self.assertEqual(audit_units.significant_digits(9.81), 3)+ self.assertEqual(audit_units.significant_digits(273.0), 3)+ self.assertEqual(audit_units.significant_digits(0.00012), 2)+ self.assertEqual(audit_units.significant_digits(299792458), 9)++ def test_findings_are_ordered_and_carry_a_remedy(self) -> None:+ findings = self._findings(HAZARD_SOURCE)+ lines = [finding["line"] for finding in findings]+ self.assertEqual(lines, sorted(lines))+ for finding in findings:+ self.assertIn(finding["severity"], {"low", "medium", "high"})+ self.assertTrue(finding["remedy"])+ self.assertGreaterEqual(finding["column"], 1)++ def test_exit_code_respects_the_threshold(self) -> None:+ with tempfile.TemporaryDirectory() as directory:+ target = Path(directory) / "hazards.py"+ target.write_text(HAZARD_SOURCE, encoding="utf-8")+ parser = audit_units.build_parser()+ arguments = parser.parse_args(["--input", str(target)])+ document = audit_units.run(arguments)+ self.assertEqual(audit_units.exit_code(document, "high"), 1)+ self.assertEqual(audit_units.exit_code(document, "none"), 0)+ report = audit_units.render_markdown(document)+ self.assertIn("Unit and uncertainty audit", report)+ json.dumps(document, allow_nan=False)++ def test_unparsable_source_is_reported_not_executed(self) -> None:+ with self.assertRaises(_common.CliError):+ audit_units.audit_source("def broken(:\n", "sample.py")+++class PlausibilityCatalogueTests(unittest.TestCase):+ """Structural checks that need no scientific packages."""++ def test_every_expression_declares_the_inputs_it_uses(self) -> None:+ constants = {name for name, _, _ in check_plausibility.CONSTANT_UNITS}+ entries = check_plausibility.GROUPS + check_plausibility.SCALES+ for entry in entries:+ with self.subTest(entry=entry["name"]):+ tree = _common.parse_expression(entry["expression"])+ used = set(_common.expression_variables(tree)) - constants+ self.assertEqual(+ used,+ set(entry["inputs"]),+ "declared inputs must match the free variables of the formula",+ )++ def test_regimes_are_ordered_and_cover_the_line(self) -> None:+ for entry in check_plausibility.GROUPS:+ with self.subTest(group=entry["name"]):+ regimes = entry["regimes"]+ self.assertIsNone(regimes[0][0])+ self.assertIsNone(regimes[-1][1])+ for lower, upper in zip(regimes, regimes[1:]):+ self.assertEqual(lower[1], upper[0], "regimes must be contiguous")++ def test_bands_are_ordered_positive_and_cited(self) -> None:+ for entry in check_plausibility.BANDS:+ with self.subTest(band=entry["name"]):+ self.assertGreater(entry["low"], 0)+ self.assertGreater(entry["high"], entry["low"])+ self.assertTrue(entry["source"])++ def test_catalogue_names_are_unique(self) -> None:+ for kind in ("groups", "scales", "bands"):+ table = getattr(check_plausibility, kind.upper())+ names = [entry["name"] for entry in table]+ self.assertEqual(len(names), len(set(names)), kind)++ def test_quantity_specifications_are_screened(self) -> None:+ for hostile in ["", "1 m", "_hidden=1 m", "a b=1 m", "x=", "x=nan m"]:+ with self.subTest(text=hostile):+ with self.assertRaises(_common.CliError):+ check_plausibility.parse_quantity(hostile, None)++ def test_orders_of_magnitude_has_no_infinities(self) -> None:+ self.assertIsNone(check_plausibility.orders_of_magnitude(0.0, 1.0))+ self.assertIsNone(check_plausibility.orders_of_magnitude(-2.0, 1.0))+ self.assertAlmostEqual(+ check_plausibility.orders_of_magnitude(100.0, 1.0), 2.0+ )++ def test_exit_code_respects_the_threshold(self) -> None:+ for verdict, threshold, expected in (+ ("implausible", "implausible", 1),+ ("questionable", "implausible", 0),+ ("questionable", "questionable", 1),+ ("plausible", "questionable", 0),+ ("implausible", "none", 0),+ ):+ with self.subTest(verdict=verdict, threshold=threshold):+ code = check_plausibility.exit_code({"verdict": verdict}, threshold)+ self.assertEqual(code, expected)++[email protected](HAS_PINT and HAS_SCIPY, "pint and SciPy are required")+class PlausibilityTests(unittest.TestCase):+ def _run(self, argv: list[str]) -> dict:+ return check_plausibility.run(check_plausibility.build_parser().parse_args(argv))++ def test_reynolds_matches_the_hand_computation(self) -> None:+ document = self._run(+ [+ "--quantity", "density=1060 kg/m**3",+ "--quantity", "velocity=0.5 mm/s",+ "--quantity", "length=8 um",+ "--quantity", "viscosity=3.5 mPa*s",+ "--group", "reynolds",+ ]+ )+ group = document["groups"][0]+ self.assertAlmostEqual(group["value"], 1060 * 5e-4 * 8e-6 / 3.5e-3)+ self.assertIn("laminar", group["regime"])+ self.assertEqual(document["verdict"], "plausible")+ json.dumps(document, allow_nan=False)++ def test_thermal_energy_tracks_scipy_constants(self) -> None:+ from scipy import constants++ document = self._run(+ ["--quantity", "temperature=300 K", "--scale", "thermal_energy"]+ )+ self.assertAlmostEqual(+ document["scales"][0]["value"] / (constants.k * 300.0), 1.0, places=9+ )++ def test_debye_length_matches_the_textbook_value(self) -> None:+ # 0.96 nm for a monovalent electrolyte at 100 mM and 25 C.+ document = self._run(+ [+ "--quantity", "temperature=298.15 K",+ "--quantity", "relative_permittivity=78.4",+ "--quantity", "ionic_strength=100 mol/m**3",+ "--scale", "debye_length",+ ]+ )+ self.assertAlmostEqual(document["scales"][0]["value"], 0.96, places=2)++ def test_wrong_dimensionality_is_caught_before_any_number(self) -> None:+ with self.assertRaises(_common.CliError) as caught:+ self._run(+ [+ "--quantity", "density=1000 kg/m**3",+ "--quantity", "velocity=1 m/s",+ "--quantity", "length=0.01 m",+ # kinematic viscosity where the formula needs dynamic+ "--quantity", "viscosity=1e-6 m**2/s",+ "--group", "reynolds",+ ]+ )+ self.assertIn("viscosity", str(caught.exception))+ self.assertIn("dimensionality", str(caught.exception))++ def test_a_dimensional_group_result_is_refused(self) -> None:+ registry = check_plausibility.build_registry()+ constants = check_plausibility.constant_quantities(registry)+ broken = {+ "name": "broken",+ "symbol": "X",+ "expression": "length",+ "inputs": {"length": "[length]"},+ "regimes": [[None, None, "any"]],+ "note": "",+ "source": "",+ }+ supplied = {"length": registry.Quantity(1.0, "m")}+ with self.assertRaises(_common.CliError) as caught:+ check_plausibility.evaluate_group(broken, supplied, constants)+ self.assertIn("dimensionless", str(caught.exception))++ def test_missing_inputs_name_their_dimensionality(self) -> None:+ with self.assertRaises(_common.CliError) as caught:+ self._run(["--quantity", "velocity=1 m/s", "--group", "reynolds"])+ message = str(caught.exception)+ for expected in ("density", "length", "viscosity"):+ self.assertIn(expected, message)++ def test_band_flags_an_order_of_magnitude_error(self) -> None:+ document = self._run(+ [+ "--quantity", "diameter=2 m",+ "--band", "eukaryotic_cell_diameter=diameter",+ ]+ )+ band = document["bands"][0]+ self.assertEqual(band["verdict"], "implausible")+ self.assertGreater(band["orders_of_magnitude_outside"], 4)+ self.assertEqual(document["verdict"], "implausible")+ self.assertTrue(document["warnings"])+ json.dumps(document, allow_nan=False)++ def test_band_accepts_a_value_inside_the_range(self) -> None:+ document = self._run(+ [+ "--quantity", "diameter=15 um",+ "--band", "eukaryotic_cell_diameter=diameter",+ ]+ )+ self.assertEqual(document["bands"][0]["verdict"], "plausible")+ self.assertEqual(document["verdict"], "plausible")++ def test_a_near_miss_is_questionable_not_implausible(self) -> None:+ document = self._run(+ [+ "--quantity", "diameter=140 um",+ "--band", "eukaryotic_cell_diameter=diameter",+ ]+ )+ self.assertEqual(document["bands"][0]["verdict"], "questionable")++ def test_a_non_positive_magnitude_stays_strict_json(self) -> None:+ document = self._run(+ [+ "--quantity", "diameter=-3 um",+ "--band", "eukaryotic_cell_diameter=diameter",+ ]+ )+ band = document["bands"][0]+ self.assertEqual(band["verdict"], "implausible")+ self.assertIsNone(band["orders_of_magnitude_outside"])+ json.dumps(document, allow_nan=False)++ def test_band_refuses_an_incomparable_dimension(self) -> None:+ with self.assertRaises(_common.CliError):+ self._run(+ [+ "--quantity", "mass=3 kg",+ "--band", "eukaryotic_cell_diameter=mass",+ ]+ )++ def test_constant_names_are_reserved(self) -> None:+ with self.assertRaises(_common.CliError) as caught:+ self._run(+ [+ "--quantity", "k_B=1 J/K",+ "--quantity", "temperature=300 K",+ "--scale", "thermal_energy",+ ]+ )+ self.assertIn("k_B", str(caught.exception))++ def test_unknown_names_list_the_alternatives(self) -> None:+ with self.assertRaises(_common.CliError) as caught:+ self._run(["--group", "renolds"])+ self.assertIn("reynolds", str(caught.exception))++ def test_an_empty_request_is_rejected(self) -> None:+ with self.assertRaises(_common.CliError):+ self._run(["--quantity", "velocity=1 m/s"])++ def test_every_catalogue_entry_is_listed(self) -> None:+ document = self._run(["--list"])+ self.assertEqual(len(document["groups"]), len(check_plausibility.GROUPS))+ self.assertEqual(len(document["scales"]), len(check_plausibility.SCALES))+ self.assertEqual(len(document["bands"]), len(check_plausibility.BANDS))+ json.dumps(document, allow_nan=False)++ def test_markdown_report_renders(self) -> None:+ document = self._run(+ [+ "--quantity", "temperature=300 K",+ "--quantity", "diameter=15 um",+ "--quantity", "length=10 um",+ "--quantity", "diffusivity=1e-9 m**2/s",+ "--scale", "diffusion_time",+ "--band", "eukaryotic_cell_diameter=diameter",+ ]+ )+ report = check_plausibility.render_markdown(document)+ self.assertIn("Plausibility check", report)+ self.assertIn("Characteristic scales", report)+ self.assertIn("Magnitude bands", report)+ # 10 um across at 1e-9 m^2/s is 0.1 s, not minutes and not microseconds.+ self.assertAlmostEqual(document["scales"][0]["value"], 0.1, places=6)+++if __name__ == "__main__":+ unittest.main()