anky2002 commited on
Commit
4c5518a
·
verified ·
1 Parent(s): 7acadc5

Upload agents/statistical_agent.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. agents/statistical_agent.py +27 -24
agents/statistical_agent.py CHANGED
@@ -137,15 +137,12 @@ def t09_lbp_distribution(img):
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})"
@@ -276,13 +273,26 @@ def t21_joint_histogram(img):
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}"
@@ -295,14 +305,7 @@ ALL_TESTS=[t01_dct_kurtosis,t02_benford,t03_gradient_sparsity,t04_local_kurtosis
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)
 
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
141
+ # Vectorized GLCM — horizontal adjacency
142
  glcm=np.zeros((16,16))
143
+ np.add.at(glcm, (gray[:,:-1].ravel(), gray[:,1:].ravel()), 1)
 
 
144
  glcm/=(glcm.sum()+1e-9)
 
145
  energy=float(np.sum(glcm**2))
 
146
  I,J=np.mgrid[0:16,0:16]; homog=float(np.sum(glcm/(1+np.abs(I-J))))
147
  if energy<0.05 and homog>0.5: s,n=-0.2,f"Natural texture (E={energy:.4f}, H={homog:.3f})"
148
  elif energy>0.2: s,n=0.3,f"Flat/repetitive (E={energy:.4f})"
 
273
  return {"test":"Joint Color Histogram","mi":round(mi,4),"score":s,"note":n}
274
 
275
  def t22_run_length(img):
276
+ gray=np.array(img.convert("L")); h,w=gray.shape
277
+ # Sample 10 rows and 10 columns spread across the image
278
+ all_runs=[]
279
+ row_indices = np.linspace(0, h-1, min(10, h), dtype=int)
280
+ col_indices = np.linspace(0, w-1, min(10, w), dtype=int)
281
+ for ri in row_indices:
282
+ row=gray[ri,:]; cur=1
283
+ for i in range(1,len(row)):
284
+ if row[i]==row[i-1]: cur+=1
285
+ else: all_runs.append(cur); cur=1
286
+ all_runs.append(cur)
287
+ for ci in col_indices:
288
+ col=gray[:,ci]; cur=1
289
+ for i in range(1,len(col)):
290
+ if col[i]==col[i-1]: cur+=1
291
+ else: all_runs.append(cur); cur=1
292
+ all_runs.append(cur)
293
+ runs=np.array(all_runs)
294
+ if len(runs)<10: return {"test":"Run Length Analysis","score":0.0,"note":"Insufficient data"}
295
+ avg_run=float(np.mean(runs))
296
  if 1<avg_run<5: s,n=-0.2,f"Natural run lengths (avg={avg_run:.2f})"
297
  elif avg_run>10: s,n=0.3,f"Long runs ({avg_run:.2f}) — flat patches"
298
  else: s,n=0.0,f"Run avg={avg_run:.2f}"
 
305
  t19_power_law_fit,t20_contrast_distribution,t21_joint_histogram,t22_run_length]
306
 
307
  def run_statistical_agent(img):
308
+ from agents.utils import run_agent_tests
309
+ from agents.optical_agent import AgentEvidence
310
+ findings, avg, conf, fail, rat = run_agent_tests(ALL_TESTS, img, "Statistical Priors Agent")
311
+ return AgentEvidence("Statistical Priors Agent", np.clip(avg,-1,1), conf, fail, rat, findings)