code wiki / _hdl_build / nx_analyst_report_gate.nx
nx_analyst_report_gate.nx source
↩ module page · 103 lines · 5333 B
1// nx_analyst_report_gate.nx -- GATE for the dataset-analysis capstone (nx_analyst_report). A realistic small
2// dataset with KNOWN relationships (price strongly driven by size, weakly by age) must produce a report that
3// (a) profiles every column, (b) reports the correct correlation direction/strength to the target, (c) names
4// the correct strongest ASSOCIATION (renamed from KEY DRIVER in F1005: the number is associational and
5// 'driver' is a causal word). The report substrings are the load-bearing asserts. expect_exit: 0 license_tier: ORIGINAL
6import "nx_syscalls.nx"
7import "_hdl_build/nx_analyst_report.nx"
8
9func rp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
10func rn(v: i64) -> i64 { var m: i64=v; if m<0{rp("-" 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 }
11func rhas(hay: *u8, n: i64, needle: *u8) -> i64 {
12 var nl: i64 = 0
13 while needle[nl] != (0 as u8) { nl = nl + 1 }
14 var i: i64 = 0
15 while i + nl <= n { var j: i64 = 0; var eq: i64 = 1; while j < nl { if hay[i+j] != needle[j] { eq = 0; break } j = j + 1 } if eq == 1 { return 1 } i = i + 1 }
16 return 0
17}
18func lcg(s: i64) -> i64 { return (s * 1103515245 + 12345) & 0x7fffffff }
19
20func main() -> i64 {
21 rp("=== nx_analyst_report_gate ===\n" as *u8)
22 var pass: i64 = 0
23 var fail: i64 = 0
24 let n: i64 = 300
25
26 // dataset: size (driver), age (weak inverse), price = 5*size - 2*age + noise (price is the target)
27 let size: *i64 = sys_mmap(n * 8) as *i64
28 let age: *i64 = sys_mmap(n * 8) as *i64
29 let price: *i64 = sys_mmap(n * 8) as *i64
30 var s: i64 = 11
31 var i: i64 = 0
32 while i < n {
33 s = lcg(s)
34 size[i] = 50 + (s % 150) // 50..199
35 s = lcg(s)
36 age[i] = s % 40 // 0..39 (independent of size)
37 s = lcg(s)
38 price[i] = 5 * size[i] - 2 * age[i] + (s % 20) // strongly driven by size, weakly (neg) by age
39 i = i + 1
40 }
41
42 let cols: *i64 = sys_mmap(3 * 8) as *i64
43 let names: *i64 = sys_mmap(3 * 8) as *i64
44 cols[0] = size as i64
45 cols[1] = age as i64
46 cols[2] = price as i64
47 names[0] = "size" as *u8 as i64
48 names[1] = "age" as *u8 as i64
49 names[2] = "price" as *u8 as i64
50
51 let out: *u8 = sys_mmap(16384)
52 let w: i64 = ar_analyze(cols, names, 3, n, 2, out, 16384) // target = price (idx 2)
53 rp(out)
54 rp("\n---\n" as *u8)
55
56 // T1 structure: header + per-column profiles + relationships + key driver sections
57 var t1: i64 = 1
58 if w <= 0 { t1 = 0 }
59 if rhas(out, w, "DATASET ANALYSIS" as *u8) == 0 { t1 = 0 }
60 if rhas(out, w, "PER-COLUMN PROFILES" as *u8) == 0 { t1 = 0 }
61 if rhas(out, w, "RELATIONSHIPS TO price" as *u8) == 0 { t1 = 0 }
62 if rhas(out, w, "STRONGEST ASSOCIATION with price" as *u8) == 0 { t1 = 0 }
63 if t1 == 1 { pass = pass + 1 } else { fail = fail + 1; rp("T1 FAIL structure\n" as *u8) }
64
65 // T2 every column profiled
66 var t2: i64 = 1
67 if rhas(out, w, "DATA PROFILE: size" as *u8) == 0 { t2 = 0 }
68 if rhas(out, w, "DATA PROFILE: age" as *u8) == 0 { t2 = 0 }
69 if rhas(out, w, "DATA PROFILE: price" as *u8) == 0 { t2 = 0 }
70 if t2 == 1 { pass = pass + 1 } else { fail = fail + 1; rp("T2 FAIL per-column profiles\n" as *u8) }
71
72 // T3 correct relationships: size -> strong positive to price; age -> weak/negligible (and negative if any)
73 var t3: i64 = 1
74 if rhas(out, w, "size: r=" as *u8) == 0 { t3 = 0 }
75 if rhas(out, w, "positive strong" as *u8) == 0 { if rhas(out, w, "positive very strong" as *u8) == 0 { t3 = 0 } }
76 if t3 == 1 { pass = pass + 1 } else { fail = fail + 1; rp("T3 FAIL relationships\n" as *u8) }
77
78 // T4 the strongest association correctly identified as size (not age)
79 var t4: i64 = 1
80 if rhas(out, w, "size has the strongest association" as *u8) == 0 { t4 = 0 }
81 if t4 == 1 { pass = pass + 1 } else { fail = fail + 1; rp("T4 FAIL key driver not size\n" as *u8) }
82
83 // T5 bounds guard: target_idx out of range -> 0 (no crash)
84 var t5: i64 = 1
85 if ar_analyze(cols, names, 3, n, 9, out, 16384) != 0 { t5 = 0 }
86 if ar_analyze(cols, names, 0, n, 0, out, 16384) != 0 { t5 = 0 }
87 if t5 == 1 { pass = pass + 1 } else { fail = fail + 1; rp("T5 FAIL bounds guard\n" as *u8) }
88
89 rp("pass=" as *u8); rn(pass); rp(" fail=" as *u8); rn(fail); rp("\n" as *u8)
90 let log: *u8 = sys_mmap(128)
91 var lo: i64 = 0
92 let pre: *u8 = "ANALYSTREPORT authored=organ verdict=" as *u8
93 var pi: i64 = 0
94 while pre[pi] != (0 as u8) { log[lo] = pre[pi]; lo = lo + 1; pi = pi + 1 }
95 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 } }
96 let fd: i64 = sys_openat_wr("knowledge/status/analyst_report.log" as *u8, 0x1A4)
97 if fd >= 0 { sys_write(fd, log, lo); sys_close(fd) }
98
99 if fail == 0 { rp("=== ANALYST-REPORT-GATE verdict=GREEN ===\n" as *u8); sys_exit(0); return 0 }
100 rp("=== ANALYST-REPORT-GATE verdict=RED ===\n" as *u8)
101 sys_exit(1)
102 return 1
103}