code wiki / _hdl_build / nx_analyst_data_gate.nx
nx_analyst_data_gate.nx source
↩ module page · 133 lines · 6063 B
1// nx_analyst_data_gate.nx -- GATE for the general data analyst (nx_analyst_data). Proves the profiler
2// computes correct stats on hand-built columns AND that the data-driven EXPLANATION correctly classifies
3// each column shape (constant / categorical / near-unique-id / right-skewed / has-outliers). Known-answer;
4// the report substrings are the load-bearing asserts. expect_exit: 0 license_tier: ORIGINAL
5import "nx_syscalls.nx"
6import "_hdl_build/nx_analyst_data.nx"
7
8func gp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
9func gnn(v: i64) -> i64 { var m: i64=v; if m<0{gp("-" as *u8);m=0-m} let t:*u8=sys_mmap(24); var k:i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} let o:*u8=sys_mmap(24); var i:i64=0; while i<k{o[i]=t[k-1-i];i=i+1} sys_write(1,o,k); return 0 }
10
11func ghas(hay: *u8, n: i64, needle: *u8) -> i64 {
12 var nl: i64 = 0
13 while needle[nl] != (0 as u8) { nl = nl + 1 }
14 if nl == 0 { return 0 }
15 var i: i64 = 0
16 while i + nl <= n {
17 var j: i64 = 0
18 var eq: i64 = 1
19 while j < nl { if hay[i + j] != needle[j] { eq = 0; break } j = j + 1 }
20 if eq == 1 { return 1 }
21 i = i + 1
22 }
23 return 0
24}
25
26func lcg(s: i64) -> i64 { return (s * 1103515245 + 12345) & 0x7fffffff }
27
28func main() -> i64 {
29 gp("=== nx_analyst_data_gate ===\n" as *u8)
30 var pass: i64 = 0
31 var fail: i64 = 0
32 let prof: *i64 = sys_mmap(16 * 8) as *i64
33 let rep: *u8 = sys_mmap(4096)
34
35 // ---- C1 categorical: 1000 rows, 5 classes (values 0..4) ----
36 let c1: *i64 = sys_mmap(1000 * 8) as *i64
37 var s: i64 = 3
38 var i: i64 = 0
39 while i < 1000 { s = lcg(s); c1[i] = s % 5; i = i + 1 }
40 ad_profile(c1, 1000, 10, prof)
41 var t1: i64 = 1
42 if prof[AD_COUNT] != 1000 { t1 = 0 }
43 if prof[AD_MIN] != 0 { t1 = 0 }
44 if prof[AD_MAX] != 4 { t1 = 0 }
45 let r1: i64 = ad_report("c1_categorical" as *u8, prof, rep, 4096)
46 if ghas(rep, r1, "categorical" as *u8) == 0 { t1 = 0 }
47 gp(rep)
48 if t1 == 1 { pass = pass + 1 } else { fail = fail + 1; gp("C1 FAIL categorical\n" as *u8) }
49
50 // ---- C2 near-unique id: 500 rows all distinct (i*7+1) ----
51 let c2: *i64 = sys_mmap(500 * 8) as *i64
52 i = 0
53 while i < 500 { c2[i] = i * 7 + 1; i = i + 1 }
54 ad_profile(c2, 500, 10, prof)
55 var t2: i64 = 1
56 if prof[AD_COUNT] != 500 { t2 = 0 }
57 // distinct is approx (HLL); distinct_pct should be high (near 100)
58 if prof[AD_DISTINCT_PCT] < 85 { t2 = 0 }
59 let r2: i64 = ad_report("c2_id" as *u8, prof, rep, 4096)
60 if ghas(rep, r2, "id/key" as *u8) == 0 { t2 = 0 }
61 gp(rep)
62 if t2 == 1 { pass = pass + 1 } else { fail = fail + 1; gp("C2 FAIL near-unique id\n" as *u8) }
63
64 // ---- C3 constant: 300 rows all = 42 ----
65 let c3: *i64 = sys_mmap(300 * 8) as *i64
66 i = 0
67 while i < 300 { c3[i] = 42; i = i + 1 }
68 ad_profile(c3, 300, 10, prof)
69 var t3: i64 = 1
70 if prof[AD_MIN] != 42 { t3 = 0 }
71 if prof[AD_MAX] != 42 { t3 = 0 }
72 if prof[AD_MEAN] != 42 { t3 = 0 }
73 if prof[AD_STDDEV] != 0 { t3 = 0 }
74 let r3: i64 = ad_report("c3_const" as *u8, prof, rep, 4096)
75 if ghas(rep, r3, "CONSTANT" as *u8) == 0 { t3 = 0 }
76 gp(rep)
77 if t3 == 1 { pass = pass + 1 } else { fail = fail + 1; gp("C3 FAIL constant\n" as *u8) }
78
79 // ---- C4 right-skewed with outliers: 1000 rows mostly ~100, a few HUGE ----
80 let c4: *i64 = sys_mmap(1000 * 8) as *i64
81 s = 99
82 i = 0
83 while i < 1000 { s = lcg(s); c4[i] = 90 + (s % 20); i = i + 1 } // ~90..110
84 c4[10] = 100000
85 c4[500] = 120000
86 c4[900] = 140000 // 3 huge -> right skew + outliers
87 ad_profile(c4, 1000, 10, prof)
88 var t4: i64 = 1
89 if prof[AD_MEAN] <= prof[AD_MEDIAN] { t4 = 0 } // mean pulled above median by the tail
90 if (prof[AD_OUT_HI] + prof[AD_OUT_LO]) < 3 { t4 = 0 } // at least the 3 planted outliers
91 let r4: i64 = ad_report("c4_skew" as *u8, prof, rep, 4096)
92 if ghas(rep, r4, "RIGHT-SKEWED" as *u8) == 0 { t4 = 0 }
93 if ghas(rep, r4, "outliers beyond 3 sigma" as *u8) == 0 { t4 = 0 }
94 gp(rep)
95 if t4 == 1 { pass = pass + 1 } else { fail = fail + 1; gp("C4 FAIL skew+outliers\n" as *u8) }
96
97 // ---- C5 clean continuous: uniform, no outliers, symmetric ----
98 let c5: *i64 = sys_mmap(2000 * 8) as *i64
99 s = 555
100 i = 0
101 while i < 2000 { s = lcg(s); c5[i] = s % 1000; i = i + 1 }
102 ad_profile(c5, 2000, 10, prof)
103 let r5: i64 = ad_report("c5_uniform" as *u8, prof, rep, 4096)
104 var t5: i64 = 1
105 if ghas(rep, r5, "continuous numeric" as *u8) == 0 { t5 = 0 }
106 if ghas(rep, r5, "No 3-sigma outliers" as *u8) == 0 { t5 = 0 }
107 gp(rep)
108 if t5 == 1 { pass = pass + 1 } else { fail = fail + 1; gp("C5 FAIL clean continuous\n" as *u8) }
109
110 // ---- C6 empty-column safety ----
111 let ec: *i64 = sys_mmap(8) as *i64
112 ad_profile(ec, 0, 10, prof)
113 var t6: i64 = 1
114 if prof[AD_COUNT] != 0 { t6 = 0 }
115 let r6: i64 = ad_report("c6_empty" as *u8, prof, rep, 4096)
116 if r6 <= 0 { t6 = 0 }
117 if t6 == 1 { pass = pass + 1 } else { fail = fail + 1; gp("C6 FAIL empty safety\n" as *u8) }
118
119 gp("pass=" as *u8); gnn(pass); gp(" fail=" as *u8); gnn(fail); gp("\n" as *u8)
120 let log: *u8 = sys_mmap(128)
121 var lo: i64 = 0
122 let pre: *u8 = "ANALYSTDATA authored=organ verdict=" as *u8
123 var pi: i64 = 0
124 while pre[pi] != (0 as u8) { log[lo] = pre[pi]; lo = lo + 1; pi = pi + 1 }
125 if fail == 0 { let g: *u8 = "GREEN\n" as *u8; var gi: i64 = 0; while g[gi] != (0 as u8) { log[lo] = g[gi]; lo = lo + 1; gi = gi + 1 } } else { let r: *u8 = "RED\n" as *u8; var ri: i64 = 0; while r[ri] != (0 as u8) { log[lo] = r[ri]; lo = lo + 1; ri = ri + 1 } }
126 let fd: i64 = sys_openat_wr("knowledge/status/analyst_data.log" as *u8, 0x1A4)
127 if fd >= 0 { sys_write(fd, log, lo); sys_close(fd) }
128
129 if fail == 0 { gp("=== ANALYST-DATA-GATE verdict=GREEN ===\n" as *u8); sys_exit(0); return 0 }
130 gp("=== ANALYST-DATA-GATE verdict=RED ===\n" as *u8)
131 sys_exit(1)
132 return 1
133}