code wiki / (root) / nx_skinfit_gate.nx

nx_skinfit_gate.nx source

↩ module page · 178 lines · 9043 B

1// nx_skinfit_gate.nx -- THE GATE FOR THE CHROMOPHORE FIT, 2026-09-03. 2// 3// SUBJECT: sf_forward / sf_sse / sf_fit in-process (nx_skinfit_lib has no main). 4// 5// THE ORACLE IS SELF-CONTAINED. Pick a chromophore pair, forward-model three channel reflectances, fit them 6// back, and demand the pair returns to where it started. No external dataset can be missing or stale, and a 7// fitter that returns a constant, a grid centre, or its first cell cannot survive a spread of true pairs. 8// 9// THE COEFFICIENTS HERE ARE PROBE VALUES, NOT MEASURED SPECTRA, AND THE GATE SAYS SO. They are shaped the way 10// the real absorbers are -- melanin rising toward blue, haemoglobin peaking in green -- because that spectral 11// DIFFERENCE is what makes two unknowns identifiable from three channels. Nothing below asserts a physical 12// constant; the teeth test the inversion, and real coefficients arrive later as pinned reference data. 13// 14// Teeth, in order: 15// T1 the forward model evaluates for every true pair on the grid (the fixture reached its condition). 16// T2 the true pairs SPAN real range, so nothing below can pass on a degenerate fixture. 17// T3 ROUND TRIP: every true pair is recovered within tolerance. Whole grid, no sampling. 18// T4 the residual at the recovered point is essentially zero for synthetic data -- the fit is not merely 19// close in parameters, it explains the measurement. 20// T5 MONOTONE: a higher true melanin yields a higher fitted melanin across the ladder. 21// T6 IDENTIFIABILITY NEG-CONTROL: with PROPORTIONAL coefficient vectors the two absorbers are 22// indistinguishable, and the fit must NOT be trusted -- proven by the recovered pair missing badly on 23// at least one axis. Without this tooth the suite would claim an inverse that the physics does not have. 24// T7 NEG-CONTROL: a non-positive scattering coefficient REFUSES rather than fitting nonsense. 25// T8 HONESTY: an unreachable measurement (reflectance far outside anything the model can make) must come 26// back with a LARGE residual, never a confident chromophore pair with a small one. 27// PROVEN 8/8 GREEN on the laptop build farm 2026-09-03: worst recovery error 2.2e-4, residual 0 on every 28// synthetic row, the degenerate case missing by 0.30 and 0.60, and the unreachable case returning 0.5. 29// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0 30import "nx_syscalls.nx" 31import "nx_gate_verdict.nx" 32import "nx_skinfit_lib.nx" 33 34const SG_SLOT: i64 = 8 35const SG_CH: i64 = 3 36const SG_N: i64 = 5 37const SG_MICRO: i64 = 1000000 38const SG_TOL_MICRO: i64 = 20000 39const SG_RESID_MAX_MICRO: i64 = 200 40const SG_BAD_RESID_MIN_MICRO: i64 = 2000 41const SG_SPAN_MIN_MICRO: i64 = 500000 42const SG_DEGEN_MISS_MICRO: i64 = 50000 43 44func sg_q(micro: i64) -> i64 { return fq_div(fq_from_int(micro), fq_from_int(SG_MICRO)) } 45func sg_abs(v: i64) -> i64 { if v < 0 { return 0 - v } return v } 46 47func main(argc: i64, argv: *i64) -> i64 { 48 let ctr: *i64 = gv_ctr() 49 gv_head("nx_skinfit gate -- melanin and haemoglobin recovered from three channel reflectances, proven by round trip" as *u8) 50 51 let kbase: *i64 = sys_mmap(SG_CH * SG_SLOT) as *i64 52 let kmel: *i64 = sys_mmap(SG_CH * SG_SLOT) as *i64 53 let khem: *i64 = sys_mmap(SG_CH * SG_SLOT) as *i64 54 let s: *i64 = sys_mmap(SG_CH * SG_SLOT) as *i64 55 kbase[0] = sg_q(30000) 56 kbase[1] = sg_q(40000) 57 kbase[2] = sg_q(60000) 58 kmel[0] = sg_q(500000) 59 kmel[1] = sg_q(1200000) 60 kmel[2] = sg_q(2500000) 61 khem[0] = sg_q(300000) 62 khem[1] = sg_q(2000000) 63 khem[2] = sg_q(800000) 64 s[0] = sg_q(1000000) 65 s[1] = sg_q(1000000) 66 s[2] = sg_q(1000000) 67 68 let tm: *i64 = sys_mmap(SG_N * SG_SLOT) as *i64 69 let th: *i64 = sys_mmap(SG_N * SG_SLOT) as *i64 70 tm[0] = 20000 71 th[0] = 10000 72 tm[1] = 80000 73 th[1] = 40000 74 tm[2] = 200000 75 th[2] = 80000 76 tm[3] = 400000 77 th[3] = 150000 78 tm[4] = 650000 79 th[4] = 250000 80 81 let meas: *i64 = sys_mmap(SG_CH * SG_SLOT) as *i64 82 let out: *i64 = sys_mmap(SF_O_SLOTS * SG_SLOT) as *i64 83 let fitm: *i64 = sys_mmap(SG_N * SG_SLOT) as *i64 84 85 var evald: i64 = 0 86 var recovered: i64 = 0 87 var residok: i64 = 0 88 var i: i64 = 0 89 gv_puts(" [rows] true_mel true_hem -> fit_mel fit_hem resid_micro\n" as *u8) 90 while i < SG_N { 91 let cm: i64 = sg_q(tm[i]) 92 let ch: i64 = sg_q(th[i]) 93 fitm[i] = 0 - 1 94 if sf_forward(kbase, kmel, khem, s, cm, ch, meas) == 0 { 95 evald = evald + 1 96 if sf_fit(kbase, kmel, khem, s, meas, out) == 0 { 97 fitm[i] = out[SF_O_MEL] 98 let dm: i64 = sg_abs(fq_to_micro(out[SF_O_MEL]) - tm[i]) 99 let dh: i64 = sg_abs(fq_to_micro(out[SF_O_HEM]) - th[i]) 100 if dm <= SG_TOL_MICRO { if dh <= SG_TOL_MICRO { recovered = recovered + 1 } } 101 if fq_to_micro(out[SF_O_RESID]) <= SG_RESID_MAX_MICRO { residok = residok + 1 } 102 gv_puts(" " as *u8); gv_num(tm[i]); gv_puts(" " as *u8); gv_num(th[i]) 103 gv_puts(" -> " as *u8); gv_num(fq_to_micro(out[SF_O_MEL])) 104 gv_puts(" " as *u8); gv_num(fq_to_micro(out[SF_O_HEM])) 105 gv_puts(" " as *u8); gv_num(fq_to_micro(out[SF_O_RESID])) 106 gv_puts("\n" as *u8) 107 } 108 } 109 i = i + 1 110 } 111 112 gv_puts(" [T1] forward_evaluated=" as *u8); gv_num(evald); gv_puts("/" as *u8); gv_num(SG_N); gv_puts("\n" as *u8) 113 gv_check("the-forward-model-evaluated-every-true-pair (the fixture reached its condition)" as *u8, (evald == SG_N) as i64, ctr) 114 115 let span: i64 = tm[SG_N - 1] - tm[0] 116 gv_puts(" [T2] true_melanin_span_micro=" as *u8); gv_num(span); gv_puts("\n" as *u8) 117 gv_check("the-true-pairs-span-real-range-so-nothing-here-passes-on-a-degenerate-fixture" as *u8, (span >= SG_SPAN_MIN_MICRO) as i64, ctr) 118 119 gv_puts(" [T3] recovered=" as *u8); gv_num(recovered); gv_puts("/" as *u8); gv_num(SG_N); gv_puts("\n" as *u8) 120 gv_check("every-true-chromophore-pair-is-recovered-by-the-fit" as *u8, (recovered == SG_N) as i64, ctr) 121 122 gv_puts(" [T4] residual_ok=" as *u8); gv_num(residok); gv_puts("/" as *u8); gv_num(SG_N); gv_puts("\n" as *u8) 123 gv_check("the-recovered-point-explains-the-measurement (residual essentially zero)" as *u8, (residok == SG_N) as i64, ctr) 124 125 var mono: i64 = 1 126 var k: i64 = 1 127 while k < SG_N { 128 if fitm[k] <= fitm[k - 1] { mono = 0 } 129 k = k + 1 130 } 131 gv_check("a-higher-true-melanin-yields-a-higher-fitted-melanin" as *u8, mono, ctr) 132 133 let pmel: *i64 = sys_mmap(SG_CH * SG_SLOT) as *i64 134 let phem: *i64 = sys_mmap(SG_CH * SG_SLOT) as *i64 135 pmel[0] = sg_q(500000) 136 pmel[1] = sg_q(1000000) 137 pmel[2] = sg_q(2000000) 138 phem[0] = sg_q(250000) 139 phem[1] = sg_q(500000) 140 phem[2] = sg_q(1000000) 141 let dm_true: i64 = 300000 142 let dh_true: i64 = 200000 143 var degen: i64 = 0 144 if sf_forward(kbase, pmel, phem, s, sg_q(dm_true), sg_q(dh_true), meas) == 0 { 145 if sf_fit(kbase, pmel, phem, s, meas, out) == 0 { 146 let em: i64 = sg_abs(fq_to_micro(out[SF_O_MEL]) - dm_true) 147 let eh: i64 = sg_abs(fq_to_micro(out[SF_O_HEM]) - dh_true) 148 gv_puts(" [T6] degenerate coefficients -> fit_mel=" as *u8); gv_num(fq_to_micro(out[SF_O_MEL])) 149 gv_puts(" fit_hem=" as *u8); gv_num(fq_to_micro(out[SF_O_HEM])) 150 gv_puts(" miss_mel=" as *u8); gv_num(em); gv_puts(" miss_hem=" as *u8); gv_num(eh); gv_puts("\n" as *u8) 151 if em >= SG_DEGEN_MISS_MICRO { degen = 1 } 152 if eh >= SG_DEGEN_MISS_MICRO { degen = 1 } 153 } 154 } 155 gv_check("neg-control-proportional-coefficients-are-NOT-separable-and-the-fit-misses (the inverse is not claimed where the physics does not have one)" as *u8, degen, ctr) 156 157 let bad_s: *i64 = sys_mmap(SG_CH * SG_SLOT) as *i64 158 bad_s[0] = 0 159 bad_s[1] = 0 160 bad_s[2] = 0 161 let rbad: i64 = sf_fit(kbase, kmel, khem, bad_s, meas, out) 162 gv_puts(" [T7] zero scattering -> " as *u8); gv_num(rbad); gv_puts(" want=" as *u8); gv_num(SF_REFUSED); gv_puts("\n" as *u8) 163 gv_check("neg-control-non-positive-scattering-refuses-instead-of-fitting-nonsense" as *u8, (rbad == SF_REFUSED) as i64, ctr) 164 165 let unreach: *i64 = sys_mmap(SG_CH * SG_SLOT) as *i64 166 unreach[0] = FQ_ONE 167 unreach[1] = 0 168 unreach[2] = FQ_ONE 169 var honest: i64 = 0 170 if sf_fit(kbase, kmel, khem, s, unreach, out) == 0 { 171 gv_puts(" [T8] unreachable measurement -> resid_micro=" as *u8); gv_num(fq_to_micro(out[SF_O_RESID])) 172 gv_puts(" floor=" as *u8); gv_num(SG_BAD_RESID_MIN_MICRO); gv_puts("\n" as *u8) 173 if fq_to_micro(out[SF_O_RESID]) >= SG_BAD_RESID_MIN_MICRO { honest = 1 } 174 } 175 gv_check("an-unreachable-measurement-returns-a-LARGE-residual-not-a-confident-pair" as *u8, honest, ctr) 176 177 return gv_verdict("skinfit" as *u8, ctr, "chromophore recovery proven by round trip over a spread of true pairs, with the degenerate-coefficient case refused and an unreachable measurement reported as such" as *u8) 178}