Download benchmarks/bug_detection_benchmark.py from vinsblack/CodeReality: direct link, hf CLI and curl.
- Browser
- Download file 9.27 kB
-
https://huggingface.co/datasets/vinsblack/CodeReality/resolve/main/benchmarks/bug_detection_benchmark.py
- Command line
-
hf download hf://datasets/vinsblack/CodeReality/benchmarks/bug_detection_benchmark.py
-
curl -L -o bug_detection_benchmark.py https://huggingface.co/datasets/vinsblack/CodeReality/resolve/main/benchmarks/bug_detection_benchmark.py
9.27 kB
| #!/usr/bin/env python3 | |
| """ | |
| Bug Detection Benchmark for CodeReality-1T Dataset | |
| This benchmark evaluates bug detection systems on deliberately noisy code data. | |
| Analyzes commit pairs to identify potential bugs and fixes in real-world repositories. | |
| Status: PLANNED - Framework scaffold for future implementation | |
| """ | |
| import json | |
| import os | |
| import re | |
| from typing import Dict, List, Tuple, Any | |
| from collections import defaultdict | |
| import random | |
| def load_dataset_sample(data_dir: str, sample_size: int = 500) -> List[Dict]: | |
| """ | |
| Load sample of repositories with commit history for bug detection analysis. | |
| Args: | |
| data_dir: Path to CodeReality-1T unified dataset | |
| sample_size: Number of repositories to sample | |
| Returns: | |
| List of repository data with commit pairs | |
| """ | |
| # TODO: Implement repository loading with commit history | |
| # Focus on repositories with: | |
| # - Multiple commits with bug-fix indicators | |
| # - Before/after code changes | |
| # - Issue tracking data | |
| print(f"Loading {sample_size} repositories for bug detection analysis...") | |
| return [] | |
| def extract_bug_fix_patterns(repositories: List[Dict]) -> List[Dict]: | |
| """ | |
| Extract potential bug-fix commit pairs from repository history. | |
| Args: | |
| repositories: List of repository data | |
| Returns: | |
| List of bug-fix patterns with before/after code | |
| """ | |
| # TODO: Implement bug-fix pattern extraction | |
| # Look for: | |
| # - Commit messages with "fix", "bug", "issue" keywords | |
| # - Code changes that add null checks, exception handling | |
| # - Revert patterns and subsequent fixes | |
| patterns = [] | |
| bug_keywords = ["fix", "bug", "issue", "error", "crash", "null", "exception"] | |
| for repo in repositories: | |
| # Extract commit pairs where bug-fix indicators are present | |
| pass | |
| return patterns | |
| def simple_bug_detector(code_before: str, code_after: str) -> Dict[str, Any]: | |
| """ | |
| Simple rule-based bug detection for demonstration purposes. | |
| This is a baseline implementation - real bug detection would use | |
| sophisticated ML models, static analysis, or dynamic testing. | |
| Args: | |
| code_before: Code before the fix | |
| code_after: Code after the fix | |
| Returns: | |
| Detection results with confidence scores | |
| """ | |
| # TODO: Implement simple pattern-based bug detection | |
| # Examples: | |
| # - Missing null checks | |
| # - Array bounds issues | |
| # - Resource leaks | |
| # - Logic errors | |
| results = { | |
| "bug_detected": False, | |
| "bug_type": "unknown", | |
| "confidence": 0.0, | |
| "patterns_matched": [], | |
| "fix_applied": False | |
| } | |
| # Simple pattern matching for demonstration | |
| null_check_added = "!= null" in code_after and "!= null" not in code_before | |
| bounds_check_added = "length" in code_after and "length" not in code_before | |
| if null_check_added: | |
| results["bug_detected"] = True | |
| results["bug_type"] = "null_pointer" | |
| results["confidence"] = 0.7 | |
| results["patterns_matched"].append("null_check_added") | |
| results["fix_applied"] = True | |
| return results | |
| def evaluate_bug_detection(bug_patterns: List[Dict]) -> Dict[str, Any]: | |
| """ | |
| Evaluate bug detection accuracy on commit pairs. | |
| Args: | |
| bug_patterns: List of bug-fix patterns | |
| Returns: | |
| Evaluation metrics including precision, recall, F1 | |
| """ | |
| # TODO: Implement comprehensive evaluation | |
| # Metrics: | |
| # - True positive rate (bugs correctly identified) | |
| # - False positive rate (false alarms) | |
| # - Precision, Recall, F1 score | |
| # - Bug type classification accuracy | |
| total_patterns = len(bug_patterns) | |
| detected_bugs = 0 | |
| correct_detections = 0 | |
| false_positives = 0 | |
| for pattern in bug_patterns: | |
| # Apply simple bug detector | |
| result = simple_bug_detector(pattern.get("code_before", ""), | |
| pattern.get("code_after", "")) | |
| if result["bug_detected"]: | |
| detected_bugs += 1 | |
| # In real scenario, would compare against ground truth | |
| # For demo, assume 60% accuracy | |
| if random.random() < 0.6: | |
| correct_detections += 1 | |
| else: | |
| false_positives += 1 | |
| precision = correct_detections / detected_bugs if detected_bugs > 0 else 0 | |
| recall = correct_detections / total_patterns if total_patterns > 0 else 0 | |
| f1_score = 2 * (precision * recall) / (precision + recall) if (precision + recall) > 0 else 0 | |
| return { | |
| "total_patterns": total_patterns, | |
| "detected_bugs": detected_bugs, | |
| "correct_detections": correct_detections, | |
| "false_positives": false_positives, | |
| "precision": precision, | |
| "recall": recall, | |
| "f1_score": f1_score, | |
| "detection_rate": detected_bugs / total_patterns if total_patterns > 0 else 0 | |
| } | |
| def run_benchmark(repositories: List[Dict]) -> Dict[str, Any]: | |
| """ | |
| Run complete bug detection benchmark. | |
| Args: | |
| repositories: List of repository data | |
| Returns: | |
| Complete benchmark results | |
| """ | |
| print("Extracting bug-fix patterns...") | |
| bug_patterns = extract_bug_fix_patterns(repositories) | |
| print("Evaluating bug detection...") | |
| metrics = evaluate_bug_detection(bug_patterns) | |
| print("Analyzing bug types...") | |
| bug_type_distribution = defaultdict(int) | |
| for pattern in bug_patterns: | |
| bug_type = pattern.get("bug_type", "unknown") | |
| bug_type_distribution[bug_type] += 1 | |
| return { | |
| "benchmark_info": { | |
| "name": "Bug Detection Benchmark", | |
| "dataset": "CodeReality-1T", | |
| "version": "1.0.0", | |
| "description": "Evaluates bug detection on commit pairs", | |
| "status": "PLANNED - Framework scaffold" | |
| }, | |
| "dataset_stats": { | |
| "total_repositories": len(repositories), | |
| "total_bug_patterns": len(bug_patterns), | |
| "avg_patterns_per_repo": len(bug_patterns) / len(repositories) if repositories else 0 | |
| }, | |
| "detection_metrics": metrics, | |
| "bug_type_distribution": dict(bug_type_distribution), | |
| "insights": [ | |
| "This is a planned benchmark - implementation needed", | |
| "Real bug detection requires sophisticated analysis", | |
| "CodeReality-1T provides rich commit history for training", | |
| "Noisy dataset challenges standard detection methods" | |
| ], | |
| "recommendations": [ | |
| "Implement advanced static analysis tools", | |
| "Use ML models trained on commit patterns", | |
| "Validate with manual inspection of detected bugs", | |
| "Consider temporal patterns in bug introduction/fixing" | |
| ] | |
| } | |
| def print_benchmark_results(results: Dict[str, Any]): | |
| """Print formatted benchmark results.""" | |
| print("\n" + "="*60) | |
| print("BUG DETECTION BENCHMARK RESULTS") | |
| print("="*60) | |
| info = results["benchmark_info"] | |
| print(f"Benchmark: {info['name']}") | |
| print(f"Dataset: {info['dataset']}") | |
| print(f"Status: {info['status']}") | |
| print(f"Description: {info['description']}") | |
| print("\nDataset Statistics:") | |
| stats = results["dataset_stats"] | |
| print(f" Total Repositories: {stats['total_repositories']}") | |
| print(f" Bug Patterns Found: {stats['total_bug_patterns']}") | |
| print(f" Avg Patterns/Repo: {stats['avg_patterns_per_repo']:.2f}") | |
| print("\nDetection Metrics:") | |
| metrics = results["detection_metrics"] | |
| print(f" Precision: {metrics['precision']:.3f}") | |
| print(f" Recall: {metrics['recall']:.3f}") | |
| print(f" F1 Score: {metrics['f1_score']:.3f}") | |
| print(f" Detection Rate: {metrics['detection_rate']:.3f}") | |
| print("\nBug Type Distribution:") | |
| for bug_type, count in results["bug_type_distribution"].items(): | |
| print(f" {bug_type}: {count}") | |
| print("\nKey Insights:") | |
| for insight in results["insights"]: | |
| print(f" • {insight}") | |
| print("\nRecommendations:") | |
| for rec in results["recommendations"]: | |
| print(f" • {rec}") | |
| def main(): | |
| """Run bug detection benchmark on CodeReality-1T dataset.""" | |
| # Configuration | |
| data_dir = "/mnt/z/CodeReality_Final/unified_dataset" | |
| sample_size = 100 # Reduced for planning phase | |
| print("CodeReality-1T Bug Detection Benchmark") | |
| print("Status: PLANNED - Framework scaffold only") | |
| print(f"Data directory: {data_dir}") | |
| print(f"Sample size: {sample_size}") | |
| # Load dataset sample | |
| print("\nLoading dataset sample...") | |
| repositories = load_dataset_sample(data_dir, sample_size) | |
| if not repositories: | |
| print("No repositories loaded - using mock data for demonstration") | |
| # Create mock data for demonstration | |
| repositories = [{"name": f"mock_repo_{i}", "commits": []} for i in range(10)] | |
| # Run benchmark | |
| results = run_benchmark(repositories) | |
| # Print results | |
| print_benchmark_results(results) | |
| # Save results | |
| output_file = "bug_detection_results.json" | |
| with open(output_file, 'w') as f: | |
| json.dump(results, f, indent=2) | |
| print(f"\nResults saved to: {output_file}") | |
| print("Note: This is a framework scaffold - full implementation needed") | |
| if __name__ == "__main__": | |
| main() |