anky2002 commited on
Commit
d5f3cd5
Β·
verified Β·
1 Parent(s): 23e2106

Upload agents/statistical_agent.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. agents/statistical_agent.py +301 -355
agents/statistical_agent.py CHANGED
@@ -1,362 +1,308 @@
1
- """
2
- FORENSIQ β€” Statistical Priors Agent
3
- Tests natural image statistics violations:
4
- - DCT coefficient distribution (Laplacian vs Gaussian)
5
- - Benford's law on first digits of DCT coefficients
6
- - Gradient sparsity (kurtosis > 3 for natural images)
7
- """
8
-
9
  import numpy as np
10
  from PIL import Image
11
  from scipy.fftpack import dct
12
- from scipy.stats import kurtosis as scipy_kurtosis, entropy
13
- from scipy.ndimage import gaussian_filter
14
  from typing import Dict, Any
15
-
16
  from agents.optical_agent import AgentEvidence
17
 
18
-
19
- # ─── DCT Coefficient Distribution ───────────────────────────────────
20
- def analyze_dct_distribution(img: Image.Image) -> Dict[str, Any]:
21
- """
22
- Natural image DCT coefficients follow a Laplacian (heavy-tailed)
23
- distribution. AI-generated images often follow a Gaussian.
24
- """
25
- gray = np.array(img.convert("L")).astype(np.float64)
26
- h, w = gray.shape
27
- h_crop, w_crop = (h // 8) * 8, (w // 8) * 8
28
- gray = gray[:h_crop, :w_crop]
29
-
30
- coeffs = []
31
- for i in range(0, h_crop, 8):
32
- for j in range(0, w_crop, 8):
33
- block = gray[i:i + 8, j:j + 8]
34
- dct_block = dct(dct(block.T, norm="ortho").T, norm="ortho")
35
- # Skip DC coefficient
36
- ac = dct_block.copy()
37
- ac[0, 0] = 0
38
- coeffs.extend(ac.flatten().tolist())
39
-
40
- coeffs = np.array(coeffs)
41
- coeffs = coeffs[coeffs != 0]
42
-
43
- if len(coeffs) < 100:
44
- return {"test": "DCT Distribution", "score": 0.0, "note": "Insufficient data"}
45
-
46
- # Kurtosis: Laplacian β‰ˆ 6, Gaussian β‰ˆ 3
47
- kurt = float(scipy_kurtosis(coeffs, fisher=True))
48
-
49
- if kurt > 4.5:
50
- score = -0.4
51
- note = f"DCT kurtosis={kurt:.2f} (Laplacian-like, consistent with natural images)"
52
- elif kurt < 2.0:
53
- score = 0.5
54
- note = f"DCT kurtosis={kurt:.2f} (Gaussian-like, inconsistent with natural images)"
55
- elif kurt < 3.5:
56
- score = 0.2
57
- note = f"DCT kurtosis={kurt:.2f} (borderline, mildly Gaussian)"
58
- else:
59
- score = -0.1
60
- note = f"DCT kurtosis={kurt:.2f} (near-natural)"
61
-
62
- return {
63
- "test": "DCT Distribution",
64
- "kurtosis": round(kurt, 4),
65
- "mean": round(float(np.mean(coeffs)), 4),
66
- "std": round(float(np.std(coeffs)), 4),
67
- "score": score,
68
- "note": note,
69
- }
70
-
71
-
72
- # ─── Benford's Law ──────────────────────────────────────────────────
73
- def analyze_benford(img: Image.Image) -> Dict[str, Any]:
74
- """
75
- First-digit distribution of DCT coefficients should follow
76
- Benford's Law in natural images. AI images deviate.
77
- """
78
- gray = np.array(img.convert("L")).astype(np.float64)
79
- h, w = gray.shape
80
- h_crop, w_crop = (h // 8) * 8, (w // 8) * 8
81
- gray = gray[:h_crop, :w_crop]
82
-
83
- coeffs = []
84
- for i in range(0, h_crop, 8):
85
- for j in range(0, w_crop, 8):
86
- block = gray[i:i + 8, j:j + 8]
87
- dct_block = dct(dct(block.T, norm="ortho").T, norm="ortho")
88
- coeffs.extend(np.abs(dct_block.flatten()).tolist())
89
-
90
- coeffs = np.array(coeffs)
91
- nonzero = coeffs[coeffs > 0]
92
-
93
- if len(nonzero) < 100:
94
- return {"test": "Benford's Law", "score": 0.0, "note": "Insufficient data"}
95
-
96
- # Extract first digits
97
- log_vals = np.floor(np.log10(nonzero + 1e-12))
98
- first_digits = np.floor(nonzero / (10 ** log_vals)).astype(int)
99
- first_digits = first_digits[(first_digits >= 1) & (first_digits <= 9)]
100
-
101
- observed = np.array([np.sum(first_digits == d) for d in range(1, 10)], dtype=np.float64)
102
- observed = observed / (observed.sum() + 1e-9)
103
-
104
- # Benford's expected distribution
105
- benford = np.log10(1 + 1.0 / np.arange(1, 10))
106
-
107
- # Chi-squared statistic
108
- chi2 = float(np.sum((observed - benford) ** 2 / (benford + 1e-9)))
109
-
110
- # KL divergence
111
- kl_div = float(np.sum(observed * np.log((observed + 1e-9) / (benford + 1e-9))))
112
-
113
- if chi2 < 0.005:
114
- score = -0.4
115
- note = f"Excellent Benford's law fit (χ²={chi2:.5f}, natural image)"
116
- elif chi2 < 0.02:
117
- score = -0.1
118
- note = f"Good Benford's law fit (χ²={chi2:.5f})"
119
- elif chi2 < 0.05:
120
- score = 0.3
121
- note = f"Moderate Benford's deviation (χ²={chi2:.5f})"
122
- else:
123
- score = 0.6
124
- note = f"Strong Benford's law violation (χ²={chi2:.5f}, AI-like)"
125
-
126
- return {
127
- "test": "Benford's Law",
128
- "chi_squared": round(chi2, 6),
129
- "kl_divergence": round(kl_div, 6),
130
- "observed": observed.tolist(),
131
- "benford_expected": benford.tolist(),
132
- "score": score,
133
- "note": note,
134
- }
135
-
136
-
137
- # ─── Gradient Sparsity ──────────────────────────────────────────────
138
- def analyze_gradient_sparsity(img: Image.Image) -> Dict[str, Any]:
139
- """
140
- Natural images have sparse gradients (kurtosis > 3).
141
- AI images often have smoother gradients with lower kurtosis.
142
- """
143
- gray = np.array(img.convert("L")).astype(np.float64)
144
-
145
- # Compute gradients
146
- gx = np.diff(gray, axis=1)
147
- gy = np.diff(gray, axis=0)
148
-
149
- # Combine
150
- gx_flat = gx.ravel()
151
- gy_flat = gy.ravel()
152
- all_grads = np.concatenate([gx_flat, gy_flat])
153
-
154
- kurt_val = float(scipy_kurtosis(all_grads, fisher=True))
155
-
156
- # Sparsity: fraction of near-zero gradients
157
- threshold = np.std(all_grads) * 0.1
158
- sparsity = float(np.mean(np.abs(all_grads) < threshold))
159
-
160
- if kurt_val > 5.0 and sparsity > 0.4:
161
- score = -0.4
162
- note = f"Sparse gradients (kurtosis={kurt_val:.2f}, sparsity={sparsity:.2f}, natural)"
163
- elif kurt_val < 2.0:
164
- score = 0.5
165
- note = f"Low gradient kurtosis ({kurt_val:.2f}), unnaturally smooth"
166
- elif kurt_val < 3.5:
167
- score = 0.2
168
- note = f"Borderline gradient statistics (kurtosis={kurt_val:.2f})"
169
- else:
170
- score = -0.1
171
- note = f"Normal gradient statistics (kurtosis={kurt_val:.2f})"
172
-
173
- return {
174
- "test": "Gradient Sparsity",
175
- "kurtosis": round(kurt_val, 4),
176
- "sparsity": round(sparsity, 4),
177
- "gradient_mean": round(float(np.mean(np.abs(all_grads))), 4),
178
- "score": score,
179
- "note": note,
180
- }
181
-
182
-
183
- # ─── Local Kurtosis Map ──────────────────────────────────────────────
184
- def analyze_local_kurtosis(img: Image.Image) -> Dict[str, Any]:
185
- """
186
- Natural images have spatially varying kurtosis (textured vs smooth).
187
- AI images often have unnaturally uniform local statistics.
188
- """
189
- gray = np.array(img.convert("L")).astype(np.float64)
190
- h, w = gray.shape
191
- block_size = 32
192
- h_crop, w_crop = (h // block_size) * block_size, (w // block_size) * block_size
193
- gray = gray[:h_crop, :w_crop]
194
-
195
- local_kurts = []
196
- for i in range(0, h_crop, block_size):
197
- for j in range(0, w_crop, block_size):
198
- block = gray[i:i + block_size, j:j + block_size].ravel()
199
- if np.std(block) > 1:
200
- local_kurts.append(float(scipy_kurtosis(block, fisher=True)))
201
-
202
- if len(local_kurts) < 10:
203
- return {"test": "Local Kurtosis Map", "score": 0.0, "note": "Insufficient blocks"}
204
-
205
- local_kurts = np.array(local_kurts)
206
- kurt_std = float(np.std(local_kurts))
207
- kurt_mean = float(np.mean(local_kurts))
208
-
209
- # Natural images: high variation in local kurtosis
210
- if kurt_std > 3.0:
211
- score = -0.3
212
- note = f"High local kurtosis variation (Οƒ={kurt_std:.2f}, natural spatial statistics)"
213
- elif kurt_std < 1.0:
214
- score = 0.4
215
- note = f"Unnaturally uniform local statistics (Οƒ={kurt_std:.2f}, AI-like)"
216
- else:
217
- score = 0.0
218
- note = f"Moderate local kurtosis variation (Οƒ={kurt_std:.2f})"
219
-
220
- return {
221
- "test": "Local Kurtosis Map",
222
- "kurtosis_std": round(kurt_std, 4),
223
- "kurtosis_mean": round(kurt_mean, 4),
224
- "score": score,
225
- "note": note,
226
- }
227
-
228
-
229
- # ─── Color Histogram Analysis ───────────────────────────────────────
230
- def analyze_color_histogram(img: Image.Image) -> Dict[str, Any]:
231
- """
232
- Natural images have smooth, continuous color histograms.
233
- AI/GAN images can show comb-like gaps or unusual peaks in histograms.
234
- """
235
- rgb = np.array(img.convert("RGB"))
236
-
237
- anomaly_scores = []
238
- for c, name in enumerate(["Red", "Green", "Blue"]):
239
- hist, _ = np.histogram(rgb[:, :, c].ravel(), bins=256, range=(0, 256))
240
- hist = hist.astype(np.float64)
241
-
242
- # Check for gaps (zero bins surrounded by non-zero)
243
- zero_bins = np.sum(hist == 0)
244
-
245
- # Check for comb pattern (alternating zero/nonzero)
246
- diffs = np.diff((hist > 0).astype(int))
247
- transitions = int(np.sum(np.abs(diffs)))
248
-
249
- # Smoothness: ratio of histogram derivative to histogram
250
- hist_smooth = gaussian_filter(hist.astype(np.float64), sigma=2)
251
- smoothness = float(np.mean(np.abs(hist - hist_smooth)) / (np.mean(hist) + 1e-9))
252
-
253
- anomaly_scores.append(smoothness)
254
-
255
- avg_smoothness = float(np.mean(anomaly_scores))
256
-
257
- if avg_smoothness < 0.3:
258
- score = -0.2
259
- note = f"Smooth color histograms (smoothness={avg_smoothness:.3f}, natural)"
260
- elif avg_smoothness > 0.8:
261
- score = 0.4
262
- note = f"Irregular color histograms (smoothness={avg_smoothness:.3f}, manipulation artifact)"
263
- else:
264
- score = 0.0
265
- note = f"Normal histogram smoothness ({avg_smoothness:.3f})"
266
-
267
- return {
268
- "test": "Color Histogram Analysis",
269
- "histogram_smoothness": round(avg_smoothness, 4),
270
- "score": score,
271
- "note": note,
272
- }
273
-
274
-
275
- # ─── Wavelet Coefficient Distribution ───────────────────────────────
276
- def analyze_wavelet_coefficients(img: Image.Image) -> Dict[str, Any]:
277
- """
278
- Natural image wavelet coefficients follow generalized Gaussian.
279
- AI images show deviations, especially at high-frequency subbands.
280
- Uses Haar wavelet (simple, no pywt dependency needed).
281
- """
282
- gray = np.array(img.convert("L")).astype(np.float64)
283
- h, w = gray.shape
284
- h2, w2 = h // 2 * 2, w // 2 * 2
285
- gray = gray[:h2, :w2]
286
-
287
- # Simple Haar wavelet decomposition
288
- # Level 1
289
- ll = (gray[0::2, 0::2] + gray[0::2, 1::2] + gray[1::2, 0::2] + gray[1::2, 1::2]) / 4
290
- lh = (gray[0::2, 0::2] + gray[0::2, 1::2] - gray[1::2, 0::2] - gray[1::2, 1::2]) / 4
291
- hl = (gray[0::2, 0::2] - gray[0::2, 1::2] + gray[1::2, 0::2] - gray[1::2, 1::2]) / 4
292
- hh = (gray[0::2, 0::2] - gray[0::2, 1::2] - gray[1::2, 0::2] + gray[1::2, 1::2]) / 4
293
-
294
- # Analyze high-frequency subbands
295
- hf_coeffs = np.concatenate([lh.ravel(), hl.ravel(), hh.ravel()])
296
- hf_coeffs = hf_coeffs[hf_coeffs != 0]
297
-
298
- if len(hf_coeffs) < 100:
299
- return {"test": "Wavelet Coefficients", "score": 0.0, "note": "Insufficient data"}
300
-
301
- kurt = float(scipy_kurtosis(hf_coeffs, fisher=True))
302
- # Generalized Gaussian: natural images have kurtosis > 3
303
- # AI images: often lower kurtosis (more Gaussian-like)
304
-
305
- if kurt > 5.0:
306
- score = -0.3
307
- note = f"Heavy-tailed wavelet coefficients (kurtosis={kurt:.2f}, natural)"
308
- elif kurt < 1.5:
309
- score = 0.4
310
- note = f"Gaussian-like wavelet coefficients (kurtosis={kurt:.2f}, AI-like)"
311
- else:
312
- score = 0.0
313
- note = f"Wavelet kurtosis={kurt:.2f}"
314
-
315
- return {
316
- "test": "Wavelet Coefficients",
317
- "hf_kurtosis": round(kurt, 4),
318
- "score": score,
319
- "note": note,
320
- }
321
-
322
-
323
- # ─── Main Agent Entry Point ─────────────────────────────────────────
324
- def run_statistical_agent(img: Image.Image) -> AgentEvidence:
325
- """Run all statistical priors tests."""
326
- findings = []
327
- scores = []
328
-
329
- for fn in [analyze_dct_distribution, analyze_benford, analyze_gradient_sparsity,
330
- analyze_local_kurtosis, analyze_color_histogram, analyze_wavelet_coefficients]:
331
- try:
332
- result = fn(img)
333
- findings.append(result)
334
- scores.append(result["score"])
335
- except Exception as e:
336
- findings.append({"test": fn.__name__, "error": str(e), "score": 0})
337
-
338
- avg_score = float(np.mean(scores)) if scores else 0.0
339
- confidence = min(1.0, 0.5 + 0.5 * abs(avg_score))
340
-
341
- violations = [f["test"] for f in findings if f.get("score", 0) > 0.2]
342
- compliant = [f["test"] for f in findings if f.get("score", 0) < -0.1]
343
-
344
- if violations:
345
- rationale = f"Statistical violations: {', '.join(violations)}."
346
- elif compliant:
347
- rationale = f"Natural statistics confirmed: {', '.join(compliant)}."
348
- else:
349
- rationale = "Statistical analysis inconclusive."
350
-
351
  for f in findings:
352
- if f.get("note"):
353
- rationale += f" [{f['test']}]: {f['note']}."
354
-
355
- return AgentEvidence(
356
- agent_name="Statistical Priors Agent",
357
- violation_score=np.clip(avg_score, -1, 1),
358
- confidence=confidence,
359
- failure_prob=max(0.0, 1.0 - len(scores) / 6),
360
- rationale=rationale,
361
- sub_findings=findings,
362
- )
 
1
+ """FORENSIQ β€” Statistical Priors Agent (22 features)"""
 
 
 
 
 
 
 
2
  import numpy as np
3
  from PIL import Image
4
  from scipy.fftpack import dct
5
+ from scipy.stats import kurtosis as sp_kurt, skew as sp_skew, entropy as sp_entropy
6
+ from scipy.ndimage import gaussian_filter, sobel, uniform_filter
7
  from typing import Dict, Any
 
8
  from agents.optical_agent import AgentEvidence
9
 
10
+ def _g(img): return np.array(img.convert("L")).astype(np.float64)
11
+ def _rgb(img): return np.array(img.convert("RGB")).astype(np.float64)
12
+
13
+ def t01_dct_kurtosis(img):
14
+ gray=_g(img); h,w=gray.shape; hc,wc=(h//8)*8,(w//8)*8; gray=gray[:hc,:wc]
15
+ coeffs=[]
16
+ for i in range(0,hc,8):
17
+ for j in range(0,wc,8):
18
+ b=gray[i:i+8,j:j+8]; d=dct(dct(b.T,norm="ortho").T,norm="ortho"); ac=d.copy(); ac[0,0]=0
19
+ coeffs.extend(ac.ravel().tolist())
20
+ c=np.array(coeffs); c=c[c!=0]
21
+ if len(c)<100: return {"test":"DCT Kurtosis","score":0.0,"note":"Insufficient data"}
22
+ k=float(sp_kurt(c,fisher=True))
23
+ if k>4.5: s,n=-0.4,f"Laplacian DCT (ΞΊ={k:.2f})"
24
+ elif k<2.0: s,n=0.5,f"Gaussian DCT (ΞΊ={k:.2f})"
25
+ elif k<3.5: s,n=0.2,f"Borderline (ΞΊ={k:.2f})"
26
+ else: s,n=-0.1,f"Near-natural (ΞΊ={k:.2f})"
27
+ return {"test":"DCT Kurtosis","kurtosis":round(k,4),"score":s,"note":n}
28
+
29
+ def t02_benford(img):
30
+ gray=_g(img); h,w=gray.shape; hc,wc=(h//8)*8,(w//8)*8; gray=gray[:hc,:wc]
31
+ coeffs=[]
32
+ for i in range(0,hc,8):
33
+ for j in range(0,wc,8):
34
+ coeffs.extend(np.abs(dct(dct(gray[i:i+8,j:j+8].T,norm="ortho").T,norm="ortho").ravel()).tolist())
35
+ c=np.array(coeffs); nz=c[c>0]
36
+ if len(nz)<100: return {"test":"Benford's Law","score":0.0,"note":"Insufficient"}
37
+ lv=np.floor(np.log10(nz+1e-12)); fd=np.floor(nz/(10**lv)).astype(int); fd=fd[(fd>=1)&(fd<=9)]
38
+ obs=np.array([np.sum(fd==d) for d in range(1,10)],dtype=float); obs/=(obs.sum()+1e-9)
39
+ ben=np.log10(1+1.0/np.arange(1,10))
40
+ chi2=float(np.sum((obs-ben)**2/(ben+1e-9)))
41
+ if chi2<0.005: s,n=-0.4,f"Excellent Benford fit (χ²={chi2:.5f})"
42
+ elif chi2<0.02: s,n=-0.1,f"Good fit (χ²={chi2:.5f})"
43
+ elif chi2<0.05: s,n=0.3,f"Moderate deviation (χ²={chi2:.5f})"
44
+ else: s,n=0.6,f"Strong violation (χ²={chi2:.5f})"
45
+ return {"test":"Benford's Law","chi2":round(chi2,6),"observed":obs.tolist(),"benford_expected":ben.tolist(),"score":s,"note":n}
46
+
47
+ def t03_gradient_sparsity(img):
48
+ gray=_g(img); gx=np.diff(gray,axis=1).ravel(); gy=np.diff(gray,axis=0).ravel()
49
+ ag=np.concatenate([gx,gy]); k=float(sp_kurt(ag,fisher=True))
50
+ thr=np.std(ag)*0.1; sp=float(np.mean(np.abs(ag)<thr))
51
+ if k>5 and sp>0.4: s,n=-0.4,f"Sparse gradients (ΞΊ={k:.2f}, sp={sp:.2f})"
52
+ elif k<2: s,n=0.5,f"Low kurtosis ({k:.2f})"
53
+ elif k<3.5: s,n=0.2,f"Borderline ({k:.2f})"
54
+ else: s,n=-0.1,f"Normal (ΞΊ={k:.2f})"
55
+ return {"test":"Gradient Sparsity","kurtosis":round(k,4),"sparsity":round(sp,4),"score":s,"note":n}
56
+
57
+ def t04_local_kurtosis(img):
58
+ gray=_g(img); h,w=gray.shape; bs=32; hc,wc=(h//bs)*bs,(w//bs)*bs; gray=gray[:hc,:wc]
59
+ lk=[]
60
+ for i in range(0,hc,bs):
61
+ for j in range(0,wc,bs):
62
+ b=gray[i:i+bs,j:j+bs].ravel()
63
+ if np.std(b)>1: lk.append(float(sp_kurt(b,fisher=True)))
64
+ if len(lk)<10: return {"test":"Local Kurtosis Map","score":0.0,"note":"Insufficient"}
65
+ std=float(np.std(lk))
66
+ if std>3: s,n=-0.3,f"High kurtosis variation (Οƒ={std:.2f})"
67
+ elif std<1: s,n=0.4,f"Uniform statistics (Οƒ={std:.2f})"
68
+ else: s,n=0.0,f"Moderate (Οƒ={std:.2f})"
69
+ return {"test":"Local Kurtosis Map","kurtosis_std":round(std,4),"score":s,"note":n}
70
+
71
+ def t05_color_histogram(img):
72
+ rgb=np.array(img.convert("RGB")); scores=[]
73
+ for c in range(3):
74
+ h,_=np.histogram(rgb[:,:,c].ravel(),bins=256,range=(0,256))
75
+ sm=gaussian_filter(h.astype(float),2)
76
+ scores.append(float(np.mean(np.abs(h-sm))/(np.mean(h)+1e-9)))
77
+ avg=float(np.mean(scores))
78
+ if avg<0.3: s,n=-0.2,f"Smooth histograms ({avg:.3f})"
79
+ elif avg>0.8: s,n=0.4,f"Irregular histograms ({avg:.3f})"
80
+ else: s,n=0.0,f"Histogram smoothness={avg:.3f}"
81
+ return {"test":"Color Histogram","smoothness":round(avg,4),"score":s,"note":n}
82
+
83
+ def t06_wavelet_kurtosis(img):
84
+ gray=_g(img); h,w=gray.shape; h2,w2=h//2*2,w//2*2; gray=gray[:h2,:w2]
85
+ lh=(gray[0::2,0::2]+gray[0::2,1::2]-gray[1::2,0::2]-gray[1::2,1::2])/4
86
+ hl=(gray[0::2,0::2]-gray[0::2,1::2]+gray[1::2,0::2]-gray[1::2,1::2])/4
87
+ hh=(gray[0::2,0::2]-gray[0::2,1::2]-gray[1::2,0::2]+gray[1::2,1::2])/4
88
+ hf=np.concatenate([lh.ravel(),hl.ravel(),hh.ravel()]); hf=hf[hf!=0]
89
+ if len(hf)<100: return {"test":"Wavelet Kurtosis","score":0.0,"note":"Insufficient"}
90
+ k=float(sp_kurt(hf,fisher=True))
91
+ if k>5: s,n=-0.3,f"Heavy-tailed wavelets (ΞΊ={k:.2f})"
92
+ elif k<1.5: s,n=0.4,f"Gaussian wavelets (ΞΊ={k:.2f})"
93
+ else: s,n=0.0,f"Wavelet ΞΊ={k:.2f}"
94
+ return {"test":"Wavelet Kurtosis","kurtosis":round(k,4),"score":s,"note":n}
95
+
96
+ def t07_entropy_map(img):
97
+ gray=_g(img); h,w=gray.shape; bs=32; ents=[]
98
+ for i in range(0,h-bs,bs):
99
+ for j in range(0,w-bs,bs):
100
+ b=gray[i:i+bs,j:j+bs].ravel().astype(int)
101
+ h_,_=np.histogram(b,bins=64,range=(0,256)); h_=h_.astype(float); h_/=(h_.sum()+1e-9)
102
+ ents.append(-float(np.sum(h_*np.log2(h_+1e-12))))
103
+ if len(ents)<4: return {"test":"Entropy Map","score":0.0,"note":"Too small"}
104
+ std=float(np.std(ents)); mn=float(np.mean(ents))
105
+ if std>0.5: s,n=-0.2,f"Varied local entropy (Οƒ={std:.2f})"
106
+ elif std<0.15: s,n=0.3,f"Uniform entropy (Οƒ={std:.2f})"
107
+ else: s,n=0.0,f"Entropy Οƒ={std:.2f}"
108
+ return {"test":"Entropy Map","entropy_std":round(std,4),"mean":round(mn,4),"score":s,"note":n}
109
+
110
+ def t08_edge_orientation(img):
111
+ gray=_g(img); gx=sobel(gray,1); gy=sobel(gray,0); mag=np.hypot(gx,gy)
112
+ strong=mag>np.percentile(mag,80); angles=np.arctan2(gy[strong],gx[strong])
113
+ hist,_=np.histogram(angles,bins=36,range=(-np.pi,np.pi)); hist=hist.astype(float); hist/=(hist.sum()+1e-9)
114
+ ent=-float(np.sum(hist*np.log(hist+1e-9)))
115
+ max_ent=np.log(36)
116
+ norm_ent=ent/max_ent
117
+ if norm_ent<0.85: s,n=-0.2,f"Directional edges (entropy={norm_ent:.3f})"
118
+ elif norm_ent>0.95: s,n=0.2,f"Isotropic edges ({norm_ent:.3f})"
119
+ else: s,n=0.0,f"Edge entropy={norm_ent:.3f}"
120
+ return {"test":"Edge Orientation","entropy":round(norm_ent,4),"score":s,"note":n}
121
+
122
+ def t09_lbp_distribution(img):
123
+ gray=np.array(img.convert("L")); h,w=gray.shape
124
+ # Simplified LBP
125
+ lbp=np.zeros((h-2,w-2),dtype=int)
126
+ for dy,dx,bit in [(-1,-1,0),(-1,0,1),(-1,1,2),(0,1,3),(1,1,4),(1,0,5),(1,-1,6),(0,-1,7)]:
127
+ lbp|=((gray[1+dy:h-1+dy,1+dx:w-1+dx]>=gray[1:h-1,1:w-1]).astype(int)<<bit)
128
+ hist,_=np.histogram(lbp.ravel(),bins=256,range=(0,256)); hist=hist.astype(float); hist/=(hist.sum()+1e-9)
129
+ # Uniform LBP patterns (≀2 transitions) dominate in natural images
130
+ uniform=0
131
+ for v in range(256):
132
+ b=format(v,'08b'); t=sum(1 for i in range(7) if b[i]!=b[i+1])+int(b[0]!=b[7])
133
+ if t<=2: uniform+=hist[v]
134
+ if uniform>0.6: s,n=-0.2,f"Natural LBP (uniform={uniform:.2%})"
135
+ elif uniform<0.3: s,n=0.3,f"Non-uniform LBP ({uniform:.2%})"
136
+ else: s,n=0.0,f"LBP uniform={uniform:.2%}"
137
+ return {"test":"LBP Distribution","uniform_ratio":round(uniform,4),"score":s,"note":n}
138
+
139
+ def t10_cooccurrence(img):
140
+ gray=(np.array(img.convert("L"))//16).astype(int); h,w=gray.shape # Quantize to 16 levels
141
+ glcm=np.zeros((16,16))
142
+ for i in range(h):
143
+ for j in range(w-1):
144
+ glcm[gray[i,j],gray[i,j+1]]+=1
145
+ glcm/=(glcm.sum()+1e-9)
146
+ # Energy (angular second moment)
147
+ energy=float(np.sum(glcm**2))
148
+ # Homogeneity
149
+ I,J=np.mgrid[0:16,0:16]; homog=float(np.sum(glcm/(1+np.abs(I-J))))
150
+ if energy<0.05 and homog>0.5: s,n=-0.2,f"Natural texture (E={energy:.4f}, H={homog:.3f})"
151
+ elif energy>0.2: s,n=0.3,f"Flat/repetitive (E={energy:.4f})"
152
+ else: s,n=0.0,f"GLCM E={energy:.4f}, H={homog:.3f}"
153
+ return {"test":"Co-occurrence Matrix","energy":round(energy,4),"homogeneity":round(homog,4),"score":s,"note":n}
154
+
155
+ def t11_block_variance(img):
156
+ gray=_g(img); h,w=gray.shape; bs=8; hc,wc=(h//bs)*bs,(w//bs)*bs
157
+ gray=gray[:hc,:wc]; bvars=[]
158
+ for i in range(0,hc,bs):
159
+ for j in range(0,wc,bs):
160
+ bvars.append(float(np.var(gray[i:i+bs,j:j+bs])))
161
+ bv=np.array(bvars)
162
+ # ANOVA-like test: variance of variances
163
+ vov=float(np.std(bv))/(float(np.mean(bv))+1e-9)
164
+ if vov>1: s,n=-0.2,f"Varied block variance (VoV={vov:.3f})"
165
+ elif vov<0.3: s,n=0.3,f"Uniform block variance ({vov:.3f})"
166
+ else: s,n=0.0,f"VoV={vov:.3f}"
167
+ return {"test":"Block Variance ANOVA","vov":round(vov,4),"score":s,"note":n}
168
+
169
+ def t12_gradient_magnitude(img):
170
+ gray=_g(img); gm=np.hypot(sobel(gray,0),sobel(gray,1))
171
+ k=float(sp_kurt(gm.ravel(),fisher=True)); sk=float(sp_skew(gm.ravel()))
172
+ if k>5: s,n=-0.2,f"Heavy-tailed gradients (ΞΊ={k:.2f})"
173
+ elif k<2: s,n=0.3,f"Light-tailed ({k:.2f})"
174
+ else: s,n=0.0,f"Gradient ΞΊ={k:.2f}"
175
+ return {"test":"Gradient Magnitude Dist","kurtosis":round(k,3),"skewness":round(sk,3),"score":s,"note":n}
176
+
177
+ def t13_spatial_correlation(img):
178
+ gray=_g(img); h,w=gray.shape; step=max(1,h*w//200000)
179
+ ac1=float(np.corrcoef(gray[:,:-1].ravel()[::step],gray[:,1:].ravel()[::step])[0,1])
180
+ ac5=float(np.corrcoef(gray[:,:-5].ravel()[::step],gray[:,5:].ravel()[::step])[0,1])
181
+ decay=ac1-ac5
182
+ if 0.05<decay<0.3: s,n=-0.2,f"Natural correlation decay ({decay:.3f})"
183
+ elif decay<0.01: s,n=0.3,f"Flat correlation ({decay:.3f})"
184
+ else: s,n=0.0,f"Decay={decay:.3f}"
185
+ return {"test":"Spatial Correlation Decay","decay":round(decay,4),"score":s,"note":n}
186
+
187
+ def t14_dct_skewness(img):
188
+ gray=_g(img); h,w=gray.shape; hc,wc=(h//8)*8,(w//8)*8; gray=gray[:hc,:wc]
189
+ coeffs=[]
190
+ for i in range(0,hc,8):
191
+ for j in range(0,wc,8):
192
+ d=dct(dct(gray[i:i+8,j:j+8].T,norm="ortho").T,norm="ortho"); ac=d.copy(); ac[0,0]=0
193
+ coeffs.extend(ac.ravel().tolist())
194
+ c=np.array(coeffs); c=c[c!=0]
195
+ if len(c)<100: return {"test":"DCT Skewness","score":0.0,"note":"Insufficient"}
196
+ sk=float(sp_skew(c))
197
+ if abs(sk)<0.1: s,n=-0.2,f"Symmetric DCT (skew={sk:.3f})"
198
+ elif abs(sk)>0.5: s,n=0.3,f"Skewed DCT ({sk:.3f})"
199
+ else: s,n=0.0,f"DCT skew={sk:.3f}"
200
+ return {"test":"DCT Skewness","skewness":round(sk,4),"score":s,"note":n}
201
+
202
+ def t15_saturation_distribution(img):
203
+ rgb=np.array(img.convert("RGB")).astype(float)
204
+ mx=np.max(rgb,axis=-1); mn=np.min(rgb,axis=-1)
205
+ sat=(mx-mn)/(mx+1e-9); sat_flat=sat.ravel()
206
+ k=float(sp_kurt(sat_flat,fisher=True))
207
+ if k>3: s,n=-0.2,f"Natural saturation (ΞΊ={k:.2f})"
208
+ elif k<1: s,n=0.3,f"Unusual saturation ({k:.2f})"
209
+ else: s,n=0.0,f"Saturation ΞΊ={k:.2f}"
210
+ return {"test":"Saturation Distribution","kurtosis":round(k,3),"score":s,"note":n}
211
+
212
+ def t16_luminance_gradient_ratio(img):
213
+ gray=_g(img); gx=np.abs(np.diff(gray,axis=1)); gy=np.abs(np.diff(gray,axis=0))
214
+ hg=float(np.mean(gx)); vg=float(np.mean(gy))
215
+ ratio=hg/(vg+1e-9)
216
+ if 0.7<ratio<1.4: s,n=-0.1,f"Balanced H/V gradients ({ratio:.3f})"
217
+ elif ratio>2 or ratio<0.5: s,n=0.2,f"Extreme H/V bias ({ratio:.3f})"
218
+ else: s,n=0.0,f"H/V ratio={ratio:.3f}"
219
+ return {"test":"H/V Gradient Ratio","ratio":round(ratio,3),"score":s,"note":n}
220
+
221
+ def t17_pixel_uniqueness(img):
222
+ gray=np.array(img.convert("L")); total=gray.size; unique=len(np.unique(gray))
223
+ ratio=unique/256
224
+ if ratio>0.9: s,n=-0.1,f"Full tonal range ({unique} levels)"
225
+ elif ratio<0.5: s,n=0.2,f"Limited range ({unique} levels)"
226
+ else: s,n=0.0,f"{unique} levels"
227
+ return {"test":"Pixel Uniqueness","levels":unique,"score":s,"note":n}
228
+
229
+ def t18_global_entropy(img):
230
+ gray=np.array(img.convert("L")); hist,_=np.histogram(gray,bins=256,range=(0,256))
231
+ hist=hist.astype(float); hist/=(hist.sum()+1e-9)
232
+ ent=-float(np.sum(hist*np.log2(hist+1e-12)))
233
+ if 6<ent<7.8: s,n=-0.2,f"Natural entropy ({ent:.3f})"
234
+ elif ent<5: s,n=0.3,f"Low entropy ({ent:.3f})"
235
+ else: s,n=0.0,f"Entropy={ent:.3f}"
236
+ return {"test":"Global Entropy","entropy":round(ent,4),"score":s,"note":n}
237
+
238
+ def t19_power_law_fit(img):
239
+ gray=_g(img); gm=np.hypot(sobel(gray,0),sobel(gray,1)).ravel()
240
+ gm=gm[gm>1]; hist,edges=np.histogram(gm,bins=50); hist=hist.astype(float)+1
241
+ centers=(edges[:-1]+edges[1:])/2; valid=hist>1
242
+ if np.sum(valid)<5: return {"test":"Power Law Gradient","score":0.0,"note":"Insufficient"}
243
+ try:
244
+ c=np.polyfit(np.log(centers[valid]),np.log(hist[valid]),1); slope=float(c[0])
245
+ except: slope=0
246
+ if -3<slope<-1: s,n=-0.2,f"Power-law gradients (Ξ±={slope:.2f})"
247
+ elif slope>-0.5: s,n=0.3,f"Non-power-law ({slope:.2f})"
248
+ else: s,n=0.0,f"Slope={slope:.2f}"
249
+ return {"test":"Power Law Gradient","slope":round(slope,3),"score":s,"note":n}
250
+
251
+ def t20_contrast_distribution(img):
252
+ gray=_g(img); h,w=gray.shape; bs=16
253
+ contrasts=[]
254
+ for i in range(0,h-bs,bs):
255
+ for j in range(0,w-bs,bs):
256
+ b=gray[i:i+bs,j:j+bs]; contrasts.append(float(np.max(b)-np.min(b)))
257
+ c=np.array(contrasts)
258
+ if len(c)<10: return {"test":"Contrast Distribution","score":0.0,"note":"Insufficient"}
259
+ k=float(sp_kurt(c,fisher=True))
260
+ if k>2: s,n=-0.2,f"Natural contrast variation (ΞΊ={k:.2f})"
261
+ elif k<0.5: s,n=0.2,f"Uniform contrast ({k:.2f})"
262
+ else: s,n=0.0,f"Contrast ΞΊ={k:.2f}"
263
+ return {"test":"Contrast Distribution","kurtosis":round(k,3),"score":s,"note":n}
264
+
265
+ def t21_joint_histogram(img):
266
+ rgb=np.array(img.convert("RGB")); r,g=rgb[:,:,0].ravel(),rgb[:,:,1].ravel()
267
+ step=max(1,len(r)//100000)
268
+ h2d,_,_=np.histogram2d(r[::step],g[::step],bins=32,range=[[0,256],[0,256]])
269
+ h2d/=(h2d.sum()+1e-9)
270
+ # Mutual information
271
+ hr=np.sum(h2d,axis=1); hg=np.sum(h2d,axis=0)
272
+ mi=float(np.sum(h2d*np.log2(h2d/(np.outer(hr,hg)+1e-12)+1e-12)))
273
+ if mi>0.5: s,n=-0.2,f"Natural color correlation (MI={mi:.3f})"
274
+ elif mi<0.1: s,n=0.2,f"Weak color correlation ({mi:.3f})"
275
+ else: s,n=0.0,f"MI={mi:.3f}"
276
+ return {"test":"Joint Color Histogram","mi":round(mi,4),"score":s,"note":n}
277
+
278
+ def t22_run_length(img):
279
+ gray=np.array(img.convert("L")); row=gray[gray.shape[0]//2,:]
280
+ runs=[]; cur=1
281
+ for i in range(1,len(row)):
282
+ if row[i]==row[i-1]: cur+=1
283
+ else: runs.append(cur); cur=1
284
+ runs.append(cur); runs=np.array(runs)
285
+ avg_run=float(np.mean(runs)); max_run=int(np.max(runs))
286
+ if 1<avg_run<5: s,n=-0.2,f"Natural run lengths (avg={avg_run:.2f})"
287
+ elif avg_run>10: s,n=0.3,f"Long runs ({avg_run:.2f}) β€” flat patches"
288
+ else: s,n=0.0,f"Run avg={avg_run:.2f}"
289
+ return {"test":"Run Length Analysis","avg_run":round(avg_run,3),"score":s,"note":n}
290
+
291
+ ALL_TESTS=[t01_dct_kurtosis,t02_benford,t03_gradient_sparsity,t04_local_kurtosis,t05_color_histogram,
292
+ t06_wavelet_kurtosis,t07_entropy_map,t08_edge_orientation,t09_lbp_distribution,t10_cooccurrence,
293
+ t11_block_variance,t12_gradient_magnitude,t13_spatial_correlation,t14_dct_skewness,
294
+ t15_saturation_distribution,t16_luminance_gradient_ratio,t17_pixel_uniqueness,t18_global_entropy,
295
+ t19_power_law_fit,t20_contrast_distribution,t21_joint_histogram,t22_run_length]
296
+
297
+ def run_statistical_agent(img):
298
+ findings,scores=[],[]
299
+ for fn in ALL_TESTS:
300
+ try: r=fn(img); findings.append(r); scores.append(r["score"])
301
+ except Exception as e: findings.append({"test":fn.__name__,"error":str(e),"score":0})
302
+ avg=float(np.mean(scores)) if scores else 0.0; conf=min(1.0,0.5+0.5*abs(avg))
303
+ viol=[f["test"] for f in findings if f.get("score",0)>0.2]
304
+ comp=[f["test"] for f in findings if f.get("score",0)<-0.1]
305
+ rat=f"Statistical violations: {', '.join(viol)}." if viol else f"Natural statistics: {', '.join(comp)}." if comp else "Statistical inconclusive."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
306
  for f in findings:
307
+ if f.get("note"): rat+=f" [{f['test']}]: {f['note']}."
308
+ return AgentEvidence("Statistical Priors Agent",np.clip(avg,-1,1),conf,max(0,1-len(scores)/len(ALL_TESTS)),rat,findings)