File size: 4,329 Bytes
4a15f9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
"""Build a conservative, auditable exclusion list from manually reviewed overlap candidates."""
from pathlib import Path
from collections import defaultdict,Counter
import csv,json
ROOT=Path(__file__).resolve().parents[1];AUDIT=ROOT/"dataset_audit"
meta={r["filename"]:r for r in csv.DictReader(open(ROOT/"dataset_metadata.csv",newline="",encoding="utf-8-sig"))}
pairs=list(csv.DictReader(open(AUDIT/"full_phash_scored_pairs.csv",newline="",encoding="utf-8")))
edges=[]
for r in pairs:
 if float(r["ssim"])<.60:continue
 a,b=r["file_a"],r["file_b"];ma,mb=meta[a],meta[b]
 if ma.get("classes")!=mb.get("classes") or ma.get("fabric_type")!=mb.get("fabric_type") or ma.get("factory_id")!=mb.get("factory_id"):continue
 edges.append((a,b,float(r["ssim"]),int(r["distance"])))
adj=defaultdict(set)
for a,b,_,_ in edges:adj[a].add(b);adj[b].add(a)
seen=set();components=[]
for start in adj:
 if start in seen:continue
 stack=[start];seen.add(start);comp=[]
 while stack:
  x=stack.pop();comp.append(x)
  for y in adj[x]:
   if y not in seen:seen.add(y);stack.append(y)
 if len(comp)>1:components.append(sorted(comp))
components=sorted(components,key=lambda c:(min(c),len(c)))
component_of={f:f"OVL{i:02d}" for i,c in enumerate(components,1) for f in c}
edge_set={frozenset((a,b)) for a,b,_,_ in edges}
# Register every visually reviewed candidate; class/factory/fabric mismatches are
# retained as screened false positives rather than exclusion edges.
register=[]
for r in pairs:
 if float(r["ssim"])<.60:continue
 a,b=r["file_a"],r["file_b"];ma,mb=meta[a],meta[b]
 keep=frozenset((a,b)) in edge_set
 register.append({
  "file_a":a,"split_a":r["split_a"],"file_b":b,"split_b":r["split_b"],
  "phash_distance":r["distance"],"ssim":r["ssim"],
  "same_class":r["same_class"],"same_fabric":r["same_fabric"],"same_factory":r["same_factory"],
  "component_id":component_of.get(a,component_of.get(b,"")),
  "visual_review":"completed",
  "review_outcome":"probable_same_specimen_or_capture_component" if keep else "screening_false_positive_or_mismatched_context",
  "note":"Conservative visual grouping; similarity does not establish byte-identical images."
 })
with (AUDIT/"manual_review_register.csv").open("w",newline="",encoding="utf-8") as f:
 w=csv.DictWriter(f,fieldnames=list(register[0]));w.writeheader();w.writerows(register)
exclusions=[]
for ci,comp in enumerate(components,1):
 has_test=any(meta[f]["split"]=="test" for f in comp)
 for f in comp:
  split=meta[f]["split"]
  exclude=(split!="test") if has_test else (split=="train")
  if not exclude:continue
  exclusions.append({
   "split":split,"filename":f,"component_id":f"OVL{ci:02d}",
   "class":meta[f].get("classes",""),"fabric_type":meta[f].get("fabric_type",""),
   "factory_id":meta[f].get("factory_id",""),"linked_files":";".join(g for g in comp if g!=f),
   "decision":"exclude_from_fit_or_validation",
   "basis":"Manual visual review of high-SSIM cross-split candidate component; conservative same-specimen/scene overlap control."
  })
with (AUDIT/"cross_split_exclusions.csv").open("w",newline="",encoding="utf-8") as f:
 w=csv.DictWriter(f,fieldnames=list(exclusions[0]));w.writeheader();w.writerows(exclusions)
summary={
 "method":"All 1,088 images; 64-bit DCT pHash candidate search at Hamming distance <=12; SSIM scoring of 324 cross-split pairs; all 36 pairs with SSIM >=0.60 visually reviewed.",
 "candidate_pairs_scored":len(pairs),"visual_shortlist_pairs":len(register),
 "retained_same_class_fabric_factory_edges":len(edges),"overlap_components":len(components),
 "excluded_files_by_split":dict(Counter(r["split"] for r in exclusions)),"excluded_total":len(exclusions),
 "policy":"Original dataset and official split membership are unchanged. Keep test files untouched. Exclude linked train/validation members of components that include test images. For train-validation-only overlap, retain validation and exclude train.",
 "limitations":"These are probable same-specimen/scene overlaps, not confirmed byte-identical files. Exclusions are a conservative leakage-control analysis cohort, not a rewrite of the public dataset."
}
(AUDIT/"cross_split_exclusion_summary.json").write_text(json.dumps(summary,indent=2))
print(json.dumps(summary,indent=2))
for r in exclusions:print(r["split"],r["filename"],r["component_id"],r["class"])