File size: 3,115 Bytes
7c34b66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
import cv2
import glob
import pandas as pd
import numpy as np
import sys

# Ensure parent directory is in path to import features
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from features import extract_features

def audit_features():
    groups = {
        'dataset_real': 'dataset/my_photos/real/*',
        'dataset_screen': 'dataset/my_photos/screen/*',
        'failure_real': 'manual_test/failure_cases/real/*',
        'failure_screen': 'manual_test/failure_cases/screen/*',
    }

    results = []

    for group_name, path_pattern in groups.items():
        base_path = os.path.join(os.path.dirname(__file__), '..', path_pattern)
        for filepath in glob.glob(base_path):
            if not os.path.isfile(filepath):
                continue
            
            img = cv2.imread(filepath)
            if img is None:
                continue
            
            _, features_dict = extract_features(img)
            features_dict['image'] = os.path.basename(filepath)
            features_dict['group'] = group_name
            
            # Simple ground truth 0 or 1
            if 'real' in group_name:
                features_dict['label'] = 0
            else:
                features_dict['label'] = 1
                
            results.append(features_dict)

    if not results:
        print("No images found to audit.")
        return

    df = pd.DataFrame(results)
    
    # Save raw audit
    os.makedirs(os.path.join(os.path.dirname(__file__), '..', 'reports'), exist_ok=True)
    df.to_csv(os.path.join(os.path.dirname(__file__), '..', 'reports', 'feature_audit.csv'), index=False)

    # Group stats
    feature_cols = [c for c in df.columns if c not in ['image', 'group', 'label']]
    
    stats = []
    for f in feature_cols:
        real_vals = df[df['label'] == 0][f]
        screen_vals = df[df['label'] == 1][f]
        
        real_mean = real_vals.mean()
        screen_mean = screen_vals.mean()
        
        stats.append({
            'feature': f,
            'real_mean': real_mean,
            'real_std': real_vals.std(),
            'screen_mean': screen_mean,
            'screen_std': screen_vals.std(),
            'diff': screen_mean - real_mean,
            'abs_diff': abs(screen_mean - real_mean)
        })

    stats_df = pd.DataFrame(stats)
    stats_df = stats_df.sort_values('abs_diff', ascending=False)
    stats_df.to_csv(os.path.join(os.path.dirname(__file__), '..', 'reports', 'feature_group_stats.csv'), index=False)
    
    # Generate summary markdown
    with open(os.path.join(os.path.dirname(__file__), '..', 'reports', 'feature_audit_summary.md'), 'w') as f:
        f.write("# Feature Audit Summary\n\n")
        f.write("## Top Separating Features (High Abs Diff)\n")
        f.write(stats_df.head(10).to_markdown(index=False))
        f.write("\n\n")
        
        f.write("## Suspicious Features (Real > Screen)\n")
        suspicious = stats_df[stats_df['diff'] < 0]
        f.write(suspicious.to_markdown(index=False))
        f.write("\n")

if __name__ == "__main__":
    audit_features()