File size: 3,768 Bytes
38bc0dc | 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | import csv
import os
import random
from ast_analyzer import extract_features
random.seed(42)
OUTPUT_PATH = os.path.join(os.path.dirname(__file__), "data", "dataset.csv")
FEATURE_COLUMNS = [
"num_functions",
"num_loops",
"num_if",
"num_try_except",
"num_return",
"line_count",
"max_nesting_depth",
"cyclomatic_complexity",
"avg_function_length",
"recursion_flag",
"global_variable_count",
"label",
]
def _make_clean_snippet(index):
variant = index % 10
if variant == 0:
return f
elif variant == 1:
return f
elif variant == 2:
return f
elif variant == 3:
return f
elif variant == 4:
return f
elif variant == 5:
return f
elif variant == 6:
return f
elif variant == 7:
return f
elif variant == 8:
return f
else:
return f
def _make_risky_snippet(index):
variant = index % 10
if variant == 0:
return f
elif variant == 1:
return f
elif variant == 2:
return f
elif variant == 3:
return f
elif variant == 4:
return f
elif variant == 5:
return f
elif variant == 6:
return f
elif variant == 7:
return f
elif variant == 8:
return f
else:
return f
def generate_dataset(n_clean=110, n_risky=110):
dataset = []
skipped = 0
print(f"Generating {n_clean} clean snippets...")
for i in range(n_clean):
code = _make_clean_snippet(i)
features = extract_features(code)
if features.get("error"):
skipped += 1
continue
features["label"] = 0
dataset.append(features)
print(f"Generating {n_risky} risky snippets...")
for i in range(n_risky):
code = _make_risky_snippet(i)
features = extract_features(code)
if features.get("error"):
skipped += 1
continue
features["label"] = 1
dataset.append(features)
random.shuffle(dataset)
print(f"\nDataset ready: {len(dataset)} samples ({skipped} skipped due to errors)")
return dataset
def save_dataset(dataset, output_path=OUTPUT_PATH):
os.makedirs(os.path.dirname(output_path), exist_ok=True)
with open(output_path, 'w', newline='', encoding='utf-8') as f:
writer = csv.DictWriter(f, fieldnames=FEATURE_COLUMNS)
writer.writeheader()
for row in dataset:
filtered_row = {col: row.get(col, 0) for col in FEATURE_COLUMNS}
writer.writerow(filtered_row)
print(f"Dataset saved to: {output_path}")
def print_summary(dataset):
clean_rows = [r for r in dataset if r["label"] == 0]
risky_rows = [r for r in dataset if r["label"] == 1]
print("\n" + "=" * 55)
print(" DATASET SUMMARY")
print("=" * 55)
print(f" Total samples : {len(dataset)}")
print(f" Clean (label=0) : {len(clean_rows)}")
print(f" Risky (label=1) : {len(risky_rows)}")
print("-" * 55)
print(f" {'Feature':<28} {'Clean Avg':>10} {'Risky Avg':>10}")
print("-" * 55)
for col in FEATURE_COLUMNS[:-1]:
clean_avg = sum(r[col] for r in clean_rows) / len(clean_rows) if clean_rows else 0
risky_avg = sum(r[col] for r in risky_rows) / len(risky_rows) if risky_rows else 0
print(f" {col:<28} {clean_avg:>10.2f} {risky_avg:>10.2f}")
print("=" * 55)
if __name__ == "__main__":
dataset = generate_dataset(n_clean=110, n_risky=110)
print_summary(dataset)
save_dataset(dataset)
|