diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 9ce7f85..04c02f5 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -13,7 +13,7 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest, macos-latest, windows-latest] - python-version: ['3.7', '3.8', '3.9', '3.10', '3.11', '3.12'] + python-version: ['3.8', '3.9', '3.10', '3.11', '3.12', '3.13'] steps: - name: Checkout code diff --git a/benchmarks/datasets/medium_module.py b/benchmarks/datasets/medium_module.py index d7c6d06..11de48c 100644 --- a/benchmarks/datasets/medium_module.py +++ b/benchmarks/datasets/medium_module.py @@ -4,13 +4,15 @@ Expected LJPW: Balanced across dimensions """ -from typing import List, Optional, Dict, Any -from dataclasses import dataclass import logging +from dataclasses import dataclass +from typing import Any, Dict, List, Optional + @dataclass class User: """User data model with validation""" + id: int name: str email: str @@ -20,11 +22,12 @@ def __post_init__(self): """Validate user data""" if not self.name: raise ValueError("Name cannot be empty") - if '@' not in self.email: + if "@" not in self.email: raise ValueError("Invalid email format") if self.id < 0: raise ValueError("ID must be positive") + class UserRepository: """Repository for user management with error handling""" @@ -102,6 +105,7 @@ def delete_user(self, user_id: int) -> bool: return False return False + def validate_email(email: str) -> bool: """ Validate email format. @@ -115,7 +119,8 @@ def validate_email(email: str) -> bool: if not email: return False - return '@' in email and '.' in email.split('@')[1] + return "@" in email and "." in email.split("@")[1] + def process_users(users: List[Dict[str, Any]]) -> List[User]: """ @@ -137,10 +142,10 @@ def process_users(users: List[Dict[str, Any]]) -> List[User]: for user_data in users: try: user = User( - id=user_data['id'], - name=user_data['name'], - email=user_data['email'], - active=user_data.get('active', True) + id=user_data["id"], + name=user_data["name"], + email=user_data["email"], + active=user_data.get("active", True), ) result.append(user) except KeyError as e: diff --git a/benchmarks/datasets/simple_script.py b/benchmarks/datasets/simple_script.py index 05e96f1..732d191 100644 --- a/benchmarks/datasets/simple_script.py +++ b/benchmarks/datasets/simple_script.py @@ -4,8 +4,10 @@ Expected LJPW: Low across all dimensions (minimal code) """ + def greet(name): print(f"Hello, {name}!") -if __name__ == '__main__': + +if __name__ == "__main__": greet("World") diff --git a/benchmarks/results/benchmark_history.csv b/benchmarks/results/benchmark_history.csv index 3d160e2..2252730 100644 --- a/benchmarks/results/benchmark_history.csv +++ b/benchmarks/results/benchmark_history.csv @@ -19,3 +19,13 @@ timestamp,benchmark,metric,value 2025-11-15T18:46:53.084802,edge_cases,duration,0.0003802776336669922 2025-11-15T18:46:53.084802,multi_language,status,PASS 2025-11-15T18:46:53.084802,multi_language,duration,0.00010085105895996094 +2025-11-29T20:03:42.889044,compression_ratio,status,PASS +2025-11-29T20:03:42.889044,compression_ratio,duration,0.0009584426879882812 +2025-11-29T20:03:42.889044,performance,status,PASS +2025-11-29T20:03:42.889044,performance,duration,0.004261016845703125 +2025-11-29T20:03:42.889044,accuracy,status,PASS +2025-11-29T20:03:42.889044,accuracy,duration,4.982948303222656e-05 +2025-11-29T20:03:42.889044,edge_cases,status,PASS +2025-11-29T20:03:42.889044,edge_cases,duration,0.00027871131896972656 +2025-11-29T20:03:42.889044,multi_language,status,PASS +2025-11-29T20:03:42.889044,multi_language,duration,7.724761962890625e-05 diff --git a/benchmarks/results/benchmark_results.json b/benchmarks/results/benchmark_results.json index c3c240e..6617bfb 100644 --- a/benchmarks/results/benchmark_results.json +++ b/benchmarks/results/benchmark_results.json @@ -1,10 +1,10 @@ { - "timestamp": "2025-11-15T18:46:53.084802", + "timestamp": "2025-11-29T20:03:42.889044", "version": "1.0", "benchmarks": { "compression_ratio": { "status": "PASS", - "duration": 0.04442000389099121, + "duration": 0.0009584426879882812, "results": { "simple_script": { "original_bytes": 24, @@ -19,50 +19,43 @@ "ratio": 88.92307692307692, "expected_ratio": 500, "passes": false - }, - "django_orm": { - "original_bytes": 111746, - "compressed_bytes": 13, - "ratio": 8595.846153846154, - "expected_ratio": 50000, - "passes": false } } }, "performance": { "status": "PASS", - "duration": 0.06415891647338867, + "duration": 0.004261016845703125, "results": { "small": { - "avg_time_ms": 0.014190780000831182, - "iterations": 100, - "throughput": 70468.28997006705 + "avg_time_ms": 0.01110658049583435, + "iterations": 10, + "throughput": 90036.71295364594 }, "medium": { - "avg_time_ms": 0.4236111799991704, - "iterations": 50, - "throughput": 2360.6553538128014 + "avg_time_ms": 0.2822446636855602, + "iterations": 5, + "throughput": 3543.0253558808395 }, "large": { - "avg_time_ms": 4.150386099999537, - "iterations": 10, - "throughput": 240.9414391591451 + "avg_time_ms": 2.7257688343524933, + "iterations": 1, + "throughput": 366.8689682694791 } } }, "accuracy": { "status": "PASS", - "duration": 7.033348083496094e-05, + "duration": 4.982948303222656e-05, "results": { "round_trip_tests": 3, - "avg_reconstruction_error": 0.24146628760559694, + "avg_reconstruction_error": 0.24146628760559696, "max_error": 0.25757134933839204, "accuracy_percent": 75.8533712394403 } }, "edge_cases": { "status": "PASS", - "duration": 0.0003802776336669922, + "duration": 0.00027871131896972656, "results": { "empty": { "status": "handled", @@ -81,28 +74,28 @@ }, "unicode": { "status": "handled", - "health": 0.3792087699787875, + "health": 0.43707367273339787, "has_insights": true }, "very_long_line": { "status": "handled", - "health": 0.3775208722204013, + "health": 0.4095097403695206, "has_insights": true } } }, "multi_language": { "status": "PASS", - "duration": 0.00010085105895996094, + "duration": 7.724761962890625e-05, "results": { "python": { "ljpw": { "L": 0.0, - "J": 0.006, + "J": 0.08839999999999999, "P": 0.0, - "W": 0.0025000000000000005 + "W": 0.041600000000000005 }, - "health": 0.3792087699787875, + "health": 0.43707367273339787, "analyzed": true }, "javascript": { @@ -112,7 +105,7 @@ "P": 0.0, "W": 0.0 }, - "health": 0.3775208722204013, + "health": 0.4095097403695206, "analyzed": true }, "rust": { @@ -122,7 +115,7 @@ "P": 0.0, "W": 0.0 }, - "health": 0.3775208722204013, + "health": 0.4095097403695206, "analyzed": true }, "java": { @@ -130,9 +123,9 @@ "L": 0.0, "J": 0.0, "P": 0.0, - "W": 0.0025000000000000005 + "W": 0.041600000000000005 }, - "health": 0.37821595929836704, + "health": 0.4199071132580034, "analyzed": true } } diff --git a/benchmarks/run_all_benchmarks.py b/benchmarks/run_all_benchmarks.py index f46e1a7..e384753 100644 --- a/benchmarks/run_all_benchmarks.py +++ b/benchmarks/run_all_benchmarks.py @@ -9,45 +9,42 @@ python run_all_benchmarks.py --report # Generate report only """ -import sys +import json import os +import sys import time -import json -from pathlib import Path from datetime import datetime +from pathlib import Path -# Add parent directory to path +# Add parent directory to path for src module imports sys.path.insert(0, str(Path(__file__).parent.parent)) -from ljpw_standalone import SimpleCodeAnalyzer, analyze_file, analyze_directory -from ljpw_semantic_compressor import SemanticCompressor, SemanticDecompressor +from src.ljpw.ljpw_semantic_compressor import SemanticCompressor, SemanticDecompressor +from src.ljpw.ljpw_standalone import SimpleCodeAnalyzer, analyze_directory, analyze_file + class BenchmarkSuite: """Comprehensive benchmark suite for LJPW""" def __init__(self, quick_mode=False): self.quick_mode = quick_mode - self.results = { - 'timestamp': datetime.now().isoformat(), - 'version': '1.0', - 'benchmarks': {} - } + self.results = {"timestamp": datetime.now().isoformat(), "version": "1.0", "benchmarks": {}} def run_all(self): """Run all benchmarks""" - print("="*70) + print("=" * 70) print("LJPW Benchmark Suite v1.0") - print("="*70) + print("=" * 70) print(f"Mode: {'Quick' if self.quick_mode else 'Full'}") print(f"Started: {self.results['timestamp']}") print() benchmarks = [ - ('compression_ratio', self.benchmark_compression_ratio), - ('performance', self.benchmark_performance), - ('accuracy', self.benchmark_accuracy), - ('edge_cases', self.benchmark_edge_cases), - ('multi_language', self.benchmark_multi_language), + ("compression_ratio", self.benchmark_compression_ratio), + ("performance", self.benchmark_performance), + ("accuracy", self.benchmark_accuracy), + ("edge_cases", self.benchmark_edge_cases), + ("multi_language", self.benchmark_multi_language), ] for name, func in benchmarks: @@ -56,18 +53,18 @@ def run_all(self): try: result = func() duration = time.time() - start - self.results['benchmarks'][name] = { - 'status': 'PASS', - 'duration': duration, - 'results': result + self.results["benchmarks"][name] = { + "status": "PASS", + "duration": duration, + "results": result, } print(f" ✓ {name}: PASS ({duration:.2f}s)") except Exception as e: duration = time.time() - start - self.results['benchmarks'][name] = { - 'status': 'FAIL', - 'duration': duration, - 'error': str(e) + self.results["benchmarks"][name] = { + "status": "FAIL", + "duration": duration, + "error": str(e), } print(f" ✗ {name}: FAIL - {e}") print() @@ -81,51 +78,53 @@ def benchmark_compression_ratio(self): test_cases = [ { - 'name': 'simple_script', - 'code': 'def hello(): print("hi")', - 'expected_ratio': 50, # Rough estimate + "name": "simple_script", + "code": 'def hello(): print("hi")', + "expected_ratio": 50, # Rough estimate }, { - 'name': 'medium_function', - 'code': self._generate_medium_code(), - 'expected_ratio': 500, + "name": "medium_function", + "code": self._generate_medium_code(), + "expected_ratio": 500, }, ] if not self.quick_mode: # Add Django test if file exists - django_file = Path(__file__).parent.parent / 'django_query.py' + django_file = Path(__file__).parent.parent / "django_query.py" if django_file.exists(): with open(django_file) as f: - test_cases.append({ - 'name': 'django_orm', - 'code': f.read(), - 'expected_ratio': 50000, - }) + test_cases.append( + { + "name": "django_orm", + "code": f.read(), + "expected_ratio": 50000, + } + ) results = {} for case in test_cases: - code = case['code'] - original_size = len(code.encode('utf-8')) + code = case["code"] + original_size = len(code.encode("utf-8")) # Analyze and compress analyzer = SimpleCodeAnalyzer() analysis = analyzer.analyze(code) - ljpw = analysis['ljpw'] + ljpw = analysis["ljpw"] # Create state and compress - state = [(ljpw['L'], ljpw['J'], ljpw['P'], ljpw['W'])] + state = [(ljpw["L"], ljpw["J"], ljpw["P"], ljpw["W"])] genome = compressor.compress_state_sequence(state) - compressed_size = len(genome.to_string().encode('utf-8')) + compressed_size = len(genome.to_string().encode("utf-8")) ratio = original_size / max(compressed_size, 1) - results[case['name']] = { - 'original_bytes': original_size, - 'compressed_bytes': compressed_size, - 'ratio': ratio, - 'expected_ratio': case['expected_ratio'], - 'passes': ratio >= case['expected_ratio'] * 0.5 # 50% tolerance + results[case["name"]] = { + "original_bytes": original_size, + "compressed_bytes": compressed_size, + "ratio": ratio, + "expected_ratio": case["expected_ratio"], + "passes": ratio >= case["expected_ratio"] * 0.5, # 50% tolerance } return results @@ -135,9 +134,9 @@ def benchmark_performance(self): analyzer = SimpleCodeAnalyzer() test_cases = [ - ('small', 'def f(): pass', 100 if not self.quick_mode else 10), - ('medium', self._generate_medium_code(), 50 if not self.quick_mode else 5), - ('large', self._generate_medium_code() * 10, 10 if not self.quick_mode else 1), + ("small", "def f(): pass", 100 if not self.quick_mode else 10), + ("medium", self._generate_medium_code(), 50 if not self.quick_mode else 5), + ("large", self._generate_medium_code() * 10, 10 if not self.quick_mode else 1), ] results = {} @@ -151,9 +150,9 @@ def benchmark_performance(self): avg_time = sum(times) / len(times) results[name] = { - 'avg_time_ms': avg_time * 1000, - 'iterations': iterations, - 'throughput': 1 / avg_time if avg_time > 0 else float('inf') + "avg_time_ms": avg_time * 1000, + "iterations": iterations, + "throughput": 1 / avg_time if avg_time > 0 else float("inf"), } return results @@ -177,16 +176,16 @@ def benchmark_accuracy(self): if reconstructed: recon = reconstructed[0] - error = sum((o - r)**2 for o, r in zip(original, recon))**0.5 + error = sum((o - r) ** 2 for o, r in zip(original, recon)) ** 0.5 errors.append(error) avg_error = sum(errors) / len(errors) if errors else 0 return { - 'round_trip_tests': len(test_states), - 'avg_reconstruction_error': avg_error, - 'max_error': max(errors) if errors else 0, - 'accuracy_percent': (1 - avg_error) * 100, + "round_trip_tests": len(test_states), + "avg_reconstruction_error": avg_error, + "max_error": max(errors) if errors else 0, + "accuracy_percent": (1 - avg_error) * 100, } def benchmark_edge_cases(self): @@ -194,11 +193,11 @@ def benchmark_edge_cases(self): analyzer = SimpleCodeAnalyzer() edge_cases = [ - ('empty', ''), - ('whitespace_only', ' \n\n '), - ('comments_only', '# comment\n# another'), - ('unicode', 'def 函数(): pass'), - ('very_long_line', 'x = ' + 'a' * 1000), + ("empty", ""), + ("whitespace_only", " \n\n "), + ("comments_only", "# comment\n# another"), + ("unicode", "def 函数(): pass"), + ("very_long_line", "x = " + "a" * 1000), ] results = {} @@ -206,15 +205,12 @@ def benchmark_edge_cases(self): try: result = analyzer.analyze(code, name) results[name] = { - 'status': 'handled', - 'health': result.get('health', 0), - 'has_insights': len(result.get('insights', [])) > 0 + "status": "handled", + "health": result.get("health", 0), + "has_insights": len(result.get("insights", [])) > 0, } except Exception as e: - results[name] = { - 'status': 'error', - 'error': str(e) - } + results[name] = {"status": "error", "error": str(e)} return results @@ -223,19 +219,19 @@ def benchmark_multi_language(self): analyzer = SimpleCodeAnalyzer() languages = { - 'python': 'def hello(): print("hi")', - 'javascript': 'function hello() { console.log("hi"); }', - 'rust': 'fn main() { println!("hi"); }', - 'java': 'public class Hello { public static void main(String[] args) {} }', + "python": 'def hello(): print("hi")', + "javascript": 'function hello() { console.log("hi"); }', + "rust": 'fn main() { println!("hi"); }', + "java": "public class Hello { public static void main(String[] args) {} }", } results = {} for lang, code in languages.items(): - analysis = analyzer.analyze(code, f'test.{lang}') + analysis = analyzer.analyze(code, f"test.{lang}") results[lang] = { - 'ljpw': analysis['ljpw'], - 'health': analysis['health'], - 'analyzed': True + "ljpw": analysis["ljpw"], + "health": analysis["health"], + "analyzed": True, } return results @@ -295,39 +291,39 @@ def process(self, data): def save_results(self): """Save results to file""" - results_dir = Path(__file__).parent / 'results' + results_dir = Path(__file__).parent / "results" results_dir.mkdir(exist_ok=True) # Save JSON - results_file = results_dir / 'benchmark_results.json' - with open(results_file, 'w') as f: + results_file = results_dir / "benchmark_results.json" + with open(results_file, "w") as f: json.dump(self.results, f, indent=2) print(f"Results saved to: {results_file}") # Append to history - history_file = results_dir / 'benchmark_history.csv' + history_file = results_dir / "benchmark_history.csv" if not history_file.exists(): - with open(history_file, 'w') as f: - f.write('timestamp,benchmark,metric,value\n') + with open(history_file, "w") as f: + f.write("timestamp,benchmark,metric,value\n") - with open(history_file, 'a') as f: - ts = self.results['timestamp'] - for bench_name, bench_data in self.results['benchmarks'].items(): - status = bench_data['status'] - duration = bench_data['duration'] - f.write(f'{ts},{bench_name},status,{status}\n') - f.write(f'{ts},{bench_name},duration,{duration}\n') + with open(history_file, "a") as f: + ts = self.results["timestamp"] + for bench_name, bench_data in self.results["benchmarks"].items(): + status = bench_data["status"] + duration = bench_data["duration"] + f.write(f"{ts},{bench_name},status,{status}\n") + f.write(f"{ts},{bench_name},duration,{duration}\n") def print_summary(self): """Print benchmark summary""" print() - print("="*70) + print("=" * 70) print("BENCHMARK SUMMARY") - print("="*70) + print("=" * 70) - total = len(self.results['benchmarks']) - passed = sum(1 for b in self.results['benchmarks'].values() if b['status'] == 'PASS') + total = len(self.results["benchmarks"]) + passed = sum(1 for b in self.results["benchmarks"].values() if b["status"] == "PASS") failed = total - passed print(f"Total benchmarks: {total}") @@ -337,28 +333,29 @@ def print_summary(self): if failed > 0: print("Failed benchmarks:") - for name, data in self.results['benchmarks'].items(): - if data['status'] == 'FAIL': + for name, data in self.results["benchmarks"].items(): + if data["status"] == "FAIL": print(f" - {name}: {data.get('error', 'Unknown error')}") else: print("All benchmarks passed! 🎉") print() print(f"Results saved to: benchmarks/results/benchmark_results.json") - print("="*70) + print("=" * 70) + def main(): import argparse - parser = argparse.ArgumentParser(description='Run LJPW benchmarks') - parser.add_argument('--quick', action='store_true', help='Quick smoke test') - parser.add_argument('--report', action='store_true', help='Generate report only') + parser = argparse.ArgumentParser(description="Run LJPW benchmarks") + parser.add_argument("--quick", action="store_true", help="Quick smoke test") + parser.add_argument("--report", action="store_true", help="Generate report only") args = parser.parse_args() if args.report: # Just print existing results - results_file = Path(__file__).parent / 'results' / 'benchmark_results.json' + results_file = Path(__file__).parent / "results" / "benchmark_results.json" if results_file.exists(): with open(results_file) as f: results = json.load(f) @@ -370,5 +367,6 @@ def main(): suite = BenchmarkSuite(quick_mode=args.quick) suite.run_all() -if __name__ == '__main__': + +if __name__ == "__main__": main() diff --git a/examples/advanced/demo_iso_analysis.py b/examples/advanced/demo_iso_analysis.py index 0971a0d..4466ea8 100644 --- a/examples/advanced/demo_iso_analysis.py +++ b/examples/advanced/demo_iso_analysis.py @@ -15,10 +15,10 @@ import sys from pathlib import Path -# Add parent directory to path +# Add parent directory to path for src module imports sys.path.insert(0, str(Path(__file__).parent.parent.parent)) -from ljpw_iso_analyzer import ISOStructure, LJPWISOAnalyzer +from src.ljpw.ljpw_iso_analyzer import ISOStructure, LJPWISOAnalyzer def create_simulated_windows_server(): @@ -27,18 +27,26 @@ def create_simulated_windows_server(): total_files=8432, total_dirs=1247, total_size=5_200_000_000, # ~5GB - file_types={'': 3200, '.cab': 1500, '.dll': 800, '.exe': 600, - '.xml': 450, '.txt': 200, '.ps1': 150, '.sys': 120}, + file_types={ + "": 3200, + ".cab": 1500, + ".dll": 800, + ".exe": 600, + ".xml": 450, + ".txt": 200, + ".ps1": 150, + ".sys": 120, + }, directory_depths=[1, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 6], compressed_files=1500, # .cab files - checksum_files=45, # Signature files - config_files=450, # .xml configs - script_files=150, # PowerShell scripts - doc_files=200, # Documentation - binary_files=1520, # .exe, .dll, .sys + checksum_files=45, # Signature files + config_files=450, # .xml configs + script_files=150, # PowerShell scripts + doc_files=200, # Documentation + binary_files=1520, # .exe, .dll, .sys max_depth=6, avg_depth=4.2, - naming_patterns=['structured', 'enterprise'] + naming_patterns=["structured", "enterprise"], ) @@ -48,18 +56,26 @@ def create_simulated_ubuntu_server(): total_files=4521, total_dirs=623, total_size=1_400_000_000, # ~1.4GB - file_types={'': 1800, '.deb': 850, '.gz': 400, '.conf': 350, - '.sh': 280, '.txt': 150, '.md': 120, '.so': 300}, + file_types={ + "": 1800, + ".deb": 850, + ".gz": 400, + ".conf": 350, + ".sh": 280, + ".txt": 150, + ".md": 120, + ".so": 300, + }, directory_depths=[1, 2, 2, 3, 3, 3, 4, 4, 5], compressed_files=1250, # .deb + .gz - checksum_files=85, # MD5SUMS, SHA256SUMS - config_files=350, # .conf files - script_files=280, # Shell scripts - doc_files=270, # .txt, .md, README files - binary_files=300, # .so libraries + checksum_files=85, # MD5SUMS, SHA256SUMS + config_files=350, # .conf files + script_files=280, # Shell scripts + doc_files=270, # .txt, .md, README files + binary_files=300, # .so libraries max_depth=5, avg_depth=3.4, - naming_patterns=['debian', 'modular'] + naming_patterns=["debian", "modular"], ) @@ -69,25 +85,32 @@ def create_simulated_arch_linux(): total_files=2143, total_dirs=287, total_size=850_000_000, # ~850MB - file_types={'': 950, '.xz': 380, '.zst': 250, '.sh': 180, - '.conf': 120, '.txt': 80, '.so': 150}, + file_types={ + "": 950, + ".xz": 380, + ".zst": 250, + ".sh": 180, + ".conf": 120, + ".txt": 80, + ".so": 150, + }, directory_depths=[1, 2, 2, 3, 3, 4], - compressed_files=630, # .xz + .zst (heavily compressed!) - checksum_files=12, # Minimal checksums - config_files=120, # .conf files - script_files=180, # Shell scripts - doc_files=80, # Minimal docs - binary_files=150, # .so libraries + compressed_files=630, # .xz + .zst (heavily compressed!) + checksum_files=12, # Minimal checksums + config_files=120, # .conf files + script_files=180, # Shell scripts + doc_files=80, # Minimal docs + binary_files=150, # .so libraries max_depth=4, avg_depth=2.8, - naming_patterns=['minimal', 'rolling'] + naming_patterns=["minimal", "rolling"], ) def main(): - print("="*70) + print("=" * 70) print("LJPW ISO Analysis Demo") - print("="*70) + print("=" * 70) print() print("Demonstrating that LJPW analyzes STRUCTURED MEANING,") print("not just source code. ISOs are information systems too!") @@ -97,17 +120,17 @@ def main(): # Simulate analysis of three different OS ISOs systems = [ - ('Windows Server 2022', create_simulated_windows_server()), - ('Ubuntu Server 22.04', create_simulated_ubuntu_server()), - ('Arch Linux 2024', create_simulated_arch_linux()), + ("Windows Server 2022", create_simulated_windows_server()), + ("Ubuntu Server 22.04", create_simulated_ubuntu_server()), + ("Arch Linux 2024", create_simulated_arch_linux()), ] results = [] for name, structure in systems: - print("="*70) + print("=" * 70) print(f"Analyzing: {name}") - print("-"*70) + print("-" * 70) # Calculate LJPW dimensions L = analyzer._calculate_love(structure) @@ -141,45 +164,42 @@ def main(): for insight in insights: print(f" {insight}") - results.append({ - 'name': name, - 'L': L, 'J': J, 'P': P, 'W': W, - 'health': health, - 'structure': structure - }) + results.append( + {"name": name, "L": L, "J": J, "P": P, "W": W, "health": health, "structure": structure} + ) print() # Comparison - print("="*70) + print("=" * 70) print("COMPARISON") - print("="*70) + print("=" * 70) print() print("Safety (L):") - for r in sorted(results, key=lambda x: x['L'], reverse=True): + for r in sorted(results, key=lambda x: x["L"], reverse=True): print(f" {r['name']:25s} L={r['L']:.3f}") print("\nStructure (J):") - for r in sorted(results, key=lambda x: x['J'], reverse=True): + for r in sorted(results, key=lambda x: x["J"], reverse=True): print(f" {r['name']:25s} J={r['J']:.3f}") print("\nPerformance (P):") - for r in sorted(results, key=lambda x: x['P'], reverse=True): + for r in sorted(results, key=lambda x: x["P"], reverse=True): print(f" {r['name']:25s} P={r['P']:.3f}") print("\nWisdom (W):") - for r in sorted(results, key=lambda x: x['W'], reverse=True): + for r in sorted(results, key=lambda x: x["W"], reverse=True): print(f" {r['name']:25s} W={r['W']:.3f}") print("\nOverall Health:") - for r in sorted(results, key=lambda x: x['health'], reverse=True): + for r in sorted(results, key=lambda x: x["health"], reverse=True): print(f" {r['name']:25s} {r['health']:.1f}%") print() - print("="*70) + print("=" * 70) print("KEY INSIGHTS") - print("-"*70) + print("-" * 70) # Find patterns windows = results[0] @@ -209,7 +229,7 @@ def main(): print(" • High P (performance) - Heavily optimized/compressed!") print(" • Moderate W - Less documentation (assumes expertise)") - if arch['P'] > 0.71 and arch['W'] < 0.60: + if arch["P"] > 0.71 and arch["W"] < 0.60: print(" ⚠️ P > 0.71 threshold with lower W!") print(" Risk: High optimization without proportional wisdom") print(" → Requires expert users to manage safely") @@ -217,9 +237,9 @@ def main(): print(" → Best for: Expert users wanting maximum control") print() - print("="*70) + print("=" * 70) print("SEMANTIC COMPRESSION ACHIEVED") - print("-"*70) + print("-" * 70) print() print("Instead of transferring:") print(" • Windows Server: 5.2 GB") @@ -238,9 +258,9 @@ def main(): print("An AI can now reason about these systems WITHOUT") print("downloading 7.45 GB of ISOs!") print() - print("="*70) + print("=" * 70) print("THE PROFOUND REALIZATION") - print("-"*70) + print("-" * 70) print() print("LJPW doesn't compress 'code' - it compresses STRUCTURE.") print() @@ -256,8 +276,8 @@ def main(): print("optimal balance in ANY complex adaptive system.") print() print("LJPW is UNIVERSAL.") - print("="*70) + print("=" * 70) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/examples/advanced/demo_windows_in_ljpw_space.py b/examples/advanced/demo_windows_in_ljpw_space.py index 8b211b4..da86a32 100644 --- a/examples/advanced/demo_windows_in_ljpw_space.py +++ b/examples/advanced/demo_windows_in_ljpw_space.py @@ -21,18 +21,21 @@ import sys from pathlib import Path -# Add parent directories to path +# Add parent directories to path for src module imports sys.path.insert(0, str(Path(__file__).parent.parent.parent)) -from ljpw_virtual_machine import ( - LJPWVirtualMachine, LJPWComponent, LJPWState, LJPWStructure +from src.ljpw.ljpw_virtual_machine import ( + LJPWComponent, + LJPWState, + LJPWStructure, + LJPWVirtualMachine, ) def main(): - print("="*70) + print("=" * 70) print("SEMANTIC COMPUTING: Windows Server in LJPW Space") - print("="*70) + print("=" * 70) print() print("THE PROFOUND QUESTION:") print(" 'Can we create a sandbox in LJPW space to run an ISO?'") @@ -40,83 +43,85 @@ def main(): print("THE ANSWER: YES - but not by executing bytes.") print(" We execute SEMANTIC OPERATIONS on MEANING itself.") print() - print("="*70) + print("=" * 70) print() vm = LJPWVirtualMachine() # Create Windows Server 2022 structure in LJPW space print("STEP 1: Loading Windows Server 2022 into LJPW Space") - print("-"*70) + print("-" * 70) print() # Build hierarchical structure windows = vm.create_composite_structure( - name='WindowsServer2022', + name="WindowsServer2022", components={ - 'boot': LJPWComponent( - name='boot_manager', + "boot": LJPWComponent( + name="boot_manager", state=LJPWState(L=0.95, J=0.95, P=0.85, W=0.95), - component_type='boot', + component_type="boot", subcomponents={}, - metadata={'size_mb': 50, 'critical': True} + metadata={"size_mb": 50, "critical": True}, ), - 'installer': LJPWComponent( - name='setup_engine', + "installer": LJPWComponent( + name="setup_engine", state=LJPWState(L=0.85, J=0.90, P=0.95, W=0.90), - component_type='installer', + component_type="installer", subcomponents={}, - metadata={'size_mb': 500, 'temporary': True} + metadata={"size_mb": 500, "temporary": True}, ), - 'kernel': LJPWComponent( - name='nt_kernel', + "kernel": LJPWComponent( + name="nt_kernel", state=LJPWState(L=0.90, J=0.95, P=0.90, W=0.95), - component_type='kernel', + component_type="kernel", subcomponents={}, - metadata={'size_mb': 200, 'critical': True} + metadata={"size_mb": 200, "critical": True}, ), - 'services': LJPWComponent( - name='windows_services', + "services": LJPWComponent( + name="windows_services", state=LJPWState(L=0.80, J=0.85, P=0.95, W=0.85), - component_type='services', + component_type="services", subcomponents={}, - metadata={'size_mb': 1500} + metadata={"size_mb": 1500}, ), - 'drivers': LJPWComponent( - name='driver_store', + "drivers": LJPWComponent( + name="driver_store", state=LJPWState(L=0.75, J=0.80, P=0.95, W=0.85), - component_type='drivers', + component_type="drivers", subcomponents={}, - metadata={'size_mb': 800} + metadata={"size_mb": 800}, ), - 'security': LJPWComponent( - name='security_subsystem', + "security": LJPWComponent( + name="security_subsystem", state=LJPWState(L=0.95, J=0.90, P=0.85, W=0.92), - component_type='security', + component_type="security", subcomponents={}, - metadata={'size_mb': 300, 'critical': True} + metadata={"size_mb": 300, "critical": True}, ), - 'gui': LJPWComponent( - name='server_manager', + "gui": LJPWComponent( + name="server_manager", state=LJPWState(L=0.70, J=0.75, P=0.85, W=0.80), - component_type='gui', + component_type="gui", subcomponents={}, - metadata={'size_mb': 600, 'optional': True} - ) + metadata={"size_mb": 600, "optional": True}, + ), }, interactions={ - ('boot', 'installer'): 0.95, - ('installer', 'kernel'): 0.98, - ('kernel', 'services'): 0.90, - ('kernel', 'drivers'): 0.85, - ('kernel', 'security'): 0.95, - ('services', 'gui'): 0.70 - } + ("boot", "installer"): 0.95, + ("installer", "kernel"): 0.98, + ("kernel", "services"): 0.90, + ("kernel", "drivers"): 0.85, + ("kernel", "security"): 0.95, + ("services", "gui"): 0.70, + }, ) print(f"✓ Loaded: {windows.name}") print(f" Components: {len(windows.root.subcomponents)}") - print(f" Total 'size': {sum(c.metadata.get('size_mb', 0) for c in windows.root.subcomponents.values())} MB (semantic)") + print( + f" Total 'size': {sum(c.metadata.get('size_mb', 0) for c in windows.root.subcomponents.values())} MB (semantic)" + ) print() overall = windows.overall_state() @@ -137,27 +142,27 @@ def main(): print() # STEP 2: Simulate Installation Scenarios - print("="*70) + print("=" * 70) print("STEP 2: Simulating Installation Scenarios") - print("-"*70) + print("-" * 70) print() scenarios = [ { - 'name': 'Enterprise Datacenter (High-end hardware)', - 'environment': {'safety_factor': 0.98, 'hardware_quality': 'excellent'}, - 'duration': 45 + "name": "Enterprise Datacenter (High-end hardware)", + "environment": {"safety_factor": 0.98, "hardware_quality": "excellent"}, + "duration": 45, }, { - 'name': 'Standard Server (Average hardware)', - 'environment': {'safety_factor': 0.90, 'hardware_quality': 'good'}, - 'duration': 60 + "name": "Standard Server (Average hardware)", + "environment": {"safety_factor": 0.90, "hardware_quality": "good"}, + "duration": 60, }, { - 'name': 'Budget Server (Minimal specs)', - 'environment': {'safety_factor': 0.80, 'hardware_quality': 'minimal'}, - 'duration': 90 - } + "name": "Budget Server (Minimal specs)", + "environment": {"safety_factor": 0.80, "hardware_quality": "minimal"}, + "duration": 90, + }, ] results = [] @@ -169,9 +174,9 @@ def main(): result = vm.simulate_operation( windows, - operation='install', - duration=scenario['duration'], - environment=scenario['environment'] + operation="install", + duration=scenario["duration"], + environment=scenario["environment"], ) print(f" → Success probability: {result.success_prob*100:.1f}%") @@ -184,20 +189,20 @@ def main(): print(f" • {issue}") print() - results.append((scenario['name'], result)) + results.append((scenario["name"], result)) # Compare scenarios print("COMPARISON:") - print("-"*70) + print("-" * 70) best = max(results, key=lambda r: r[1].success_prob) print(f"Best scenario: {best[0]}") print(f" Success: {best[1].success_prob*100:.1f}%") print() # STEP 3: Build Full Infrastructure in LJPW Space - print("="*70) + print("=" * 70) print("STEP 3: Building Complete Infrastructure") - print("-"*70) + print("-" * 70) print() print("Now let's deploy applications ON TOP of Windows") print(" (All in LJPW space - no actual deployment!)") @@ -205,46 +210,46 @@ def main(): # Create SQL Server component sql_server = LJPWComponent( - name='sql_server', + name="sql_server", state=LJPWState(L=0.85, J=0.90, P=0.88, W=0.90), - component_type='database', + component_type="database", subcomponents={}, - metadata={'product': 'SQL Server 2022', 'role': 'database'} + metadata={"product": "SQL Server 2022", "role": "database"}, ) # Create IIS Web Server iis = LJPWComponent( - name='iis', + name="iis", state=LJPWState(L=0.75, J=0.80, P=0.92, W=0.82), - component_type='web_server', + component_type="web_server", subcomponents={}, - metadata={'product': 'IIS 10', 'role': 'web'} + metadata={"product": "IIS 10", "role": "web"}, ) # Create .NET Application dotnet_app = LJPWComponent( - name='enterprise_app', + name="enterprise_app", state=LJPWState(L=0.70, J=0.75, P=0.85, W=0.78), - component_type='application', + component_type="application", subcomponents={}, - metadata={'framework': '.NET 8', 'role': 'application'} + metadata={"framework": ".NET 8", "role": "application"}, ) # Build full stack full_stack = vm.create_composite_structure( - name='WindowsStack', + name="WindowsStack", components={ - 'os': windows.root, # The Windows OS - 'database': sql_server, - 'web_server': iis, - 'application': dotnet_app + "os": windows.root, # The Windows OS + "database": sql_server, + "web_server": iis, + "application": dotnet_app, }, interactions={ - ('os', 'database'): 0.90, - ('os', 'web_server'): 0.85, - ('web_server', 'application'): 0.95, - ('application', 'database'): 0.90 - } + ("os", "database"): 0.90, + ("os", "web_server"): 0.85, + ("web_server", "application"): 0.95, + ("application", "database"): 0.90, + }, ) print("✓ Built complete infrastructure in LJPW space:") @@ -256,16 +261,14 @@ def main(): stack_state = full_stack.overall_state() print(f"Full Stack Health: {stack_state.health()*100:.1f}%") - print(f" L={stack_state.L:.2f}, J={stack_state.J:.2f}, P={stack_state.P:.2f}, W={stack_state.W:.2f}") + print( + f" L={stack_state.L:.2f}, J={stack_state.J:.2f}, P={stack_state.P:.2f}, W={stack_state.W:.2f}" + ) print() # Simulate stack deployment print("Simulating full stack deployment...") - deployment = vm.simulate_operation( - full_stack, - operation='deploy', - duration=100 - ) + deployment = vm.simulate_operation(full_stack, operation="deploy", duration=100) print() print("DEPLOYMENT PREDICTION:") @@ -287,38 +290,38 @@ def main(): print() # STEP 4: Test "What If" Scenarios - print("="*70) + print("=" * 70) print("STEP 4: Testing 'What If' Scenarios") - print("-"*70) + print("-" * 70) print() print("The power of LJPW space: Instant scenario testing") print() print("Scenario A: What if we add a second database for redundancy?") sql_server_2 = LJPWComponent( - name='sql_server_replica', + name="sql_server_replica", state=LJPWState(L=0.85, J=0.90, P=0.88, W=0.90), - component_type='database', + component_type="database", subcomponents={}, - metadata={'role': 'replica'} + metadata={"role": "replica"}, ) redundant_stack = vm.create_composite_structure( - name='RedundantStack', + name="RedundantStack", components={ - 'os': windows.root, - 'db_primary': sql_server, - 'db_replica': sql_server_2, - 'web_server': iis, - 'application': dotnet_app + "os": windows.root, + "db_primary": sql_server, + "db_replica": sql_server_2, + "web_server": iis, + "application": dotnet_app, }, interactions={ - ('os', 'db_primary'): 0.90, - ('os', 'db_replica'): 0.90, - ('db_primary', 'db_replica'): 0.95, # Replication - ('web_server', 'application'): 0.95, - ('application', 'db_primary'): 0.90 - } + ("os", "db_primary"): 0.90, + ("os", "db_replica"): 0.90, + ("db_primary", "db_replica"): 0.95, # Replication + ("web_server", "application"): 0.95, + ("application", "db_primary"): 0.90, + }, ) redundant_state = redundant_stack.overall_state() @@ -328,19 +331,19 @@ def main(): print("Scenario B: What if we remove the GUI (Server Core)?") minimal_windows = vm.create_composite_structure( - name='WindowsServerCore', + name="WindowsServerCore", components={ - 'boot': windows.root.subcomponents['boot'], - 'kernel': windows.root.subcomponents['kernel'], - 'services': windows.root.subcomponents['services'], - 'security': windows.root.subcomponents['security'] + "boot": windows.root.subcomponents["boot"], + "kernel": windows.root.subcomponents["kernel"], + "services": windows.root.subcomponents["services"], + "security": windows.root.subcomponents["security"], # No GUI component }, interactions={ - ('boot', 'kernel'): 0.98, - ('kernel', 'services'): 0.95, - ('kernel', 'security'): 0.98 - } + ("boot", "kernel"): 0.98, + ("kernel", "services"): 0.95, + ("kernel", "security"): 0.98, + }, ) minimal_state = minimal_windows.overall_state() @@ -350,9 +353,9 @@ def main(): print() # Final Summary - print("="*70) + print("=" * 70) print("WHAT WE JUST ACCOMPLISHED") - print("="*70) + print("=" * 70) print() print("In LJPW semantic space, we:") print() @@ -367,9 +370,9 @@ def main(): print("Time taken: SECONDS") print("Time for real deployment: HOURS") print() - print("="*70) + print("=" * 70) print("THE PARADIGM SHIFT") - print("="*70) + print("=" * 70) print() print("Traditional Computing:") print(" Storage → Memory → CPU → Execution → Result") @@ -388,13 +391,13 @@ def main(): print("This is the future of infrastructure planning,") print("system design, and semantic reasoning.") print() - print("="*70) + print("=" * 70) print() print("LJPW: Not just compression. Not just analysis.") print(" A complete SEMANTIC COMPUTING environment.") print() - print("="*70) + print("=" * 70) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/examples/advanced/track_code_evolution.py b/examples/advanced/track_code_evolution.py index 47be7fe..417b8c8 100644 --- a/examples/advanced/track_code_evolution.py +++ b/examples/advanced/track_code_evolution.py @@ -12,24 +12,30 @@ python track_code_evolution.py """ +import math import sys -from pathlib import Path from datetime import datetime, timedelta -import math +from pathlib import Path -# Add parent directory to path +# Add parent directory to path for src module imports sys.path.insert(0, str(Path(__file__).parent.parent.parent)) -from ljpw_standalone import SimpleCodeAnalyzer -from ljpw_semantic_compressor import SemanticCompressor -from ljpw_dynamic_v3 import LJPWDynamicModel, analyze_code_evolution, predict_code_future +from src.ljpw.ljpw_dynamic_v3 import ( + LJPWDynamicModel, + analyze_code_evolution, + predict_code_future, +) +from src.ljpw.ljpw_semantic_compressor import SemanticCompressor +from src.ljpw.ljpw_standalone import SimpleCodeAnalyzer # Natural Equilibrium target NE = (0.618, 0.414, 0.718, 0.693) + def calculate_distance(state1, state2): """Calculate Euclidean distance between two states""" - return math.sqrt(sum((a - b)**2 for a, b in zip(state1, state2))) + return math.sqrt(sum((a - b) ** 2 for a, b in zip(state1, state2))) + def calculate_velocity(states, time_deltas): """Calculate velocity vector (rate of change)""" @@ -44,8 +50,8 @@ def calculate_velocity(states, time_deltas): time_diff = time_deltas[-1] if time_deltas else 1 - return (delta_L/time_diff, delta_J/time_diff, - delta_P/time_diff, delta_W/time_diff) + return (delta_L / time_diff, delta_J / time_diff, delta_P / time_diff, delta_W / time_diff) + def predict_eta_to_ne(current_state, velocity): """Predict when code will reach Natural Equilibrium""" @@ -56,46 +62,47 @@ def predict_eta_to_ne(current_state, velocity): velocity_mag = math.sqrt(sum(v**2 for v in velocity)) if velocity_mag < 0.001: - return float('inf') # Not moving + return float("inf") # Not moving # Simple linear prediction (iterations) eta = distance / velocity_mag return eta + def main(): - print("="*70) + print("=" * 70) print("LJPW Advanced: Track Code Evolution") - print("="*70) + print("=" * 70) print() # Simulate code evolution over time # In reality, you'd analyze git commits or periodic snapshots evolution = [ { - 'date': '2025-01-01', - 'code': 'def process(data): return [x*2 for x in data]', - 'description': 'Initial implementation' + "date": "2025-01-01", + "code": "def process(data): return [x*2 for x in data]", + "description": "Initial implementation", }, { - 'date': '2025-01-05', - 'code': 'def process(data: list) -> list:\n """Process data"""\n return [x*2 for x in data]', - 'description': 'Added types and docs' + "date": "2025-01-05", + "code": 'def process(data: list) -> list:\n """Process data"""\n return [x*2 for x in data]', + "description": "Added types and docs", }, { - 'date': '2025-01-10', - 'code': ''' + "date": "2025-01-10", + "code": ''' def process(data: list) -> list: """Process data with validation""" if not data: raise ValueError("Empty") return [x*2 for x in data] ''', - 'description': 'Added validation' + "description": "Added validation", }, { - 'date': '2025-01-15', - 'code': ''' + "date": "2025-01-15", + "code": ''' def process(data: list) -> list: """Process data safely""" if not data: @@ -106,11 +113,11 @@ def process(data: list) -> list: logging.error(f"Error: {e}") raise ''', - 'description': 'Added error handling' + "description": "Added error handling", }, { - 'date': '2025-01-20', - 'code': ''' + "date": "2025-01-20", + "code": ''' class DataProcessor: """Robust data processor""" @@ -134,8 +141,8 @@ def process(self, data: list) -> list: logging.error(f"Error: {e}") raise ''', - 'description': 'Refactored with design patterns' - } + "description": "Refactored with design patterns", + }, ] # Analyze each version @@ -150,22 +157,22 @@ def process(self, data: list) -> list: print("-" * 70) for i, version in enumerate(evolution): - result = analyzer.analyze(version['code'], f"v{i+1}") - ljpw = result['ljpw'] - state = (ljpw['L'], ljpw['J'], ljpw['P'], ljpw['W']) + result = analyzer.analyze(version["code"], f"v{i+1}") + ljpw = result["ljpw"] + state = (ljpw["L"], ljpw["J"], ljpw["P"], ljpw["W"]) states.append(state) - dates.append(version['date']) + dates.append(version["date"]) # Calculate time delta if i > 0: - prev_date = datetime.strptime(evolution[i-1]['date'], '%Y-%m-%d') - curr_date = datetime.strptime(version['date'], '%Y-%m-%d') + prev_date = datetime.strptime(evolution[i - 1]["date"], "%Y-%m-%d") + curr_date = datetime.strptime(version["date"], "%Y-%m-%d") delta = (curr_date - prev_date).days time_deltas.append(delta) distance_to_ne = calculate_distance(state, NE) - health = result['health'] + health = result["health"] print(f"\n{version['date']} - {version['description']}") print(f" L={ljpw['L']:.3f}, J={ljpw['J']:.3f}, P={ljpw['P']:.3f}, W={ljpw['W']:.3f}") @@ -178,10 +185,12 @@ def process(self, data: list) -> list: print("-" * 70) # Convert to format for v3.0 model - ljpw_history = [{'L': s[0], 'J': s[1], 'P': s[2], 'W': s[3]} for s in states] + ljpw_history = [{"L": s[0], "J": s[1], "P": s[2], "W": s[3]} for s in states] # Use v3.0 dynamic model for sophisticated analysis - evolution_analysis = analyze_code_evolution(ljpw_history, timestamps=time_deltas if time_deltas else None) + evolution_analysis = analyze_code_evolution( + ljpw_history, timestamps=time_deltas if time_deltas else None + ) print(f"\nTrend: {evolution_analysis['trend']}") print(f" Initial distance from NE: {evolution_analysis['initial_distance_from_ne']:.3f}") @@ -190,14 +199,16 @@ def process(self, data: list) -> list: print(f" Volatility: {evolution_analysis['volatility']:.4f}") # Velocity from v3.0 model - velocity = evolution_analysis['velocity'] - velocity_mag = evolution_analysis['velocity_magnitude'] + velocity = evolution_analysis["velocity"] + velocity_mag = evolution_analysis["velocity_magnitude"] - print(f"\nVelocity vector: L={velocity[0]:+.4f}, J={velocity[1]:+.4f}, P={velocity[2]:+.4f}, W={velocity[3]:+.4f}") + print( + f"\nVelocity vector: L={velocity[0]:+.4f}, J={velocity[1]:+.4f}, P={velocity[2]:+.4f}, W={velocity[3]:+.4f}" + ) print(f"Velocity magnitude: {velocity_mag:.4f} units/day") # Direction - if evolution_analysis['converging']: + if evolution_analysis["converging"]: direction = "CONVERGING toward Natural Equilibrium ✓" else: direction = "DIVERGING from Natural Equilibrium ✗" @@ -205,10 +216,10 @@ def process(self, data: list) -> list: print(f"\nDirection: {direction}") # Predict ETA using v3.0 model - eta = evolution_analysis.get('eta_to_ne', float('inf')) + eta = evolution_analysis.get("eta_to_ne", float("inf")) print(f"\nPrediction (v3.0 Model):") - if eta == float('inf'): + if eta == float("inf"): print(" ⚠️ Code is not moving toward NE") print(" Continue refactoring to reach equilibrium") elif eta < 0: @@ -219,7 +230,7 @@ def process(self, data: list) -> list: print(f" (at current velocity)") # Threshold crossing warning - if evolution_analysis.get('crossing_power_threshold', False): + if evolution_analysis.get("crossing_power_threshold", False): print("\n ⚠️ WARNING: Power crossed threshold (K_JP = 0.71)") print(" Risk: Premature optimization may erode code structure") print(" Recommendation: Boost Wisdom before further optimization") @@ -232,11 +243,11 @@ def process(self, data: list) -> list: genome = compressor.compress_state_sequence( states, metadata={ - 'project': 'data_processor', - 'start_date': dates[0], - 'end_date': dates[-1], - 'versions': len(states) - } + "project": "data_processor", + "start_date": dates[0], + "end_date": dates[-1], + "versions": len(states), + }, ) print(f"\nOriginal: {len(states)} versions, {sum(len(v['code']) for v in evolution)} bytes") @@ -250,13 +261,17 @@ def process(self, data: list) -> list: print("-" * 70) current = states[-1] - print(f"\nCurrent state: L={current[0]:.3f}, J={current[1]:.3f}, P={current[2]:.3f}, W={current[3]:.3f}") + print( + f"\nCurrent state: L={current[0]:.3f}, J={current[1]:.3f}, P={current[2]:.3f}, W={current[3]:.3f}" + ) print(f"Target (NE): L={NE[0]:.3f}, J={NE[1]:.3f}, P={NE[2]:.3f}, W={NE[3]:.3f}") print("\nNext improvements:") if abs(current[0] - NE[0]) > 0.1: if current[0] < NE[0]: - print(f" 1. Increase Safety (L): {current[0]:.3f} → {NE[0]:.3f} (+{NE[0]-current[0]:.3f})") + print( + f" 1. Increase Safety (L): {current[0]:.3f} → {NE[0]:.3f} (+{NE[0]-current[0]:.3f})" + ) print(" - Add more error handling") print(" - Add input validation") else: @@ -264,24 +279,30 @@ def process(self, data: list) -> list: if abs(current[1] - NE[1]) > 0.1: if current[1] < NE[1]: - print(f" 2. Increase Structure (J): {current[1]:.3f} → {NE[1]:.3f} (+{NE[1]-current[1]:.3f})") + print( + f" 2. Increase Structure (J): {current[1]:.3f} → {NE[1]:.3f} (+{NE[1]-current[1]:.3f})" + ) print(" - Add more documentation") print(" - Improve type annotations") if abs(current[2] - NE[2]) > 0.1: if current[2] < NE[2]: - print(f" 3. Optimize Performance (P): {current[2]:.3f} → {NE[2]:.3f} (+{NE[2]-current[2]:.3f})") + print( + f" 3. Optimize Performance (P): {current[2]:.3f} → {NE[2]:.3f} (+{NE[2]-current[2]:.3f})" + ) print(" - Use better algorithms") print(" - Add caching") if abs(current[3] - NE[3]) > 0.1: if current[3] < NE[3]: - print(f" 4. Improve Design (W): {current[3]:.3f} → {NE[3]:.3f} (+{NE[3]-current[3]:.3f})") + print( + f" 4. Improve Design (W): {current[3]:.3f} → {NE[3]:.3f} (+{NE[3]-current[3]:.3f})" + ) print(" - Use design patterns") print(" - Better abstraction") print() - print("="*70) + print("=" * 70) print("KEY INSIGHTS:") print("-" * 70) print("1. Track LJPW scores over time to monitor code health trends") @@ -292,7 +313,8 @@ def process(self, data: list) -> list: print("6. Use trajectory analysis to guide refactoring priorities") print() print("See also: ljpw_dynamic_v3.py and docs/THEORY.md") - print("="*70) + print("=" * 70) + -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/examples/basic/01_analyze_single_file.py b/examples/basic/01_analyze_single_file.py index ee7486e..717d018 100644 --- a/examples/basic/01_analyze_single_file.py +++ b/examples/basic/01_analyze_single_file.py @@ -14,15 +14,16 @@ import sys from pathlib import Path -# Add parent directory to path +# Add parent directory to path for src module imports sys.path.insert(0, str(Path(__file__).parent.parent.parent)) -from ljpw_standalone import SimpleCodeAnalyzer, format_result +from src.ljpw.ljpw_standalone import SimpleCodeAnalyzer, format_result + def main(): - print("="*70) + print("=" * 70) print("LJPW Example 1: Analyze a Single File") - print("="*70) + print("=" * 70) print() # Sample code to analyze @@ -45,7 +46,7 @@ def process_data(data): analyzer = SimpleCodeAnalyzer() # Analyze the code - result = analyzer.analyze(sample_code, filename='sample.py') + result = analyzer.analyze(sample_code, filename="sample.py") # Print formatted results print(format_result(result)) @@ -55,26 +56,26 @@ def process_data(data): print("INTERPRETATION:") print("-" * 70) - ljpw = result['ljpw'] - health = result['health'] * 100 + ljpw = result["ljpw"] + health = result["health"] * 100 print(f"Love (Safety): {ljpw['L']:.2f}") - if ljpw['L'] < 0.5: + if ljpw["L"] < 0.5: print(" → LOW: This code has minimal error handling") print(" → FIX: Add try/except, input validation, null checks") print(f"\nJustice (Structure): {ljpw['J']:.2f}") - if ljpw['J'] < 0.4: + if ljpw["J"] < 0.4: print(" → LOW: This code lacks structure") print(" → FIX: Add type hints, docstrings, clear interfaces") print(f"\nPower (Performance): {ljpw['P']:.2f}") - if ljpw['P'] < 0.7: + if ljpw["P"] < 0.7: print(" → MEDIUM: Performance is okay but not optimized") print(" → CONSIDER: Better algorithms, caching, async operations") print(f"\nWisdom (Design): {ljpw['W']:.2f}") - if ljpw['W'] < 0.6: + if ljpw["W"] < 0.6: print(" → LOW: Design could be improved") print(" → FIX: Add abstractions, use design patterns, modularize") @@ -89,9 +90,10 @@ def process_data(data): print(" → NEEDS WORK: Significant issues to address") print() - print("="*70) + print("=" * 70) print("Next: Try running 02_analyze_directory.py") - print("="*70) + print("=" * 70) + -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/examples/basic/02_analyze_directory.py b/examples/basic/02_analyze_directory.py index b61e775..09d79c7 100644 --- a/examples/basic/02_analyze_directory.py +++ b/examples/basic/02_analyze_directory.py @@ -17,15 +17,16 @@ import sys from pathlib import Path -# Add parent directory to path +# Add parent directory to path for src module imports sys.path.insert(0, str(Path(__file__).parent.parent.parent)) -from ljpw_standalone import analyze_directory, SimpleCodeAnalyzer +from src.ljpw.ljpw_standalone import SimpleCodeAnalyzer, analyze_directory + def main(): - print("="*70) + print("=" * 70) print("LJPW Example 2: Analyze a Directory") - print("="*70) + print("=" * 70) print() # Get directory from command line or use parent @@ -48,7 +49,7 @@ def main(): # Calculate statistics total_files = len(results) - valid_results = [r for r in results if 'error' not in r] + valid_results = [r for r in results if "error" not in r] print(f"Found {total_files} files") print(f"Successfully analyzed: {len(valid_results)} files") @@ -59,11 +60,11 @@ def main(): return # Aggregate scores - avg_L = sum(r['ljpw']['L'] for r in valid_results) / len(valid_results) - avg_J = sum(r['ljpw']['J'] for r in valid_results) / len(valid_results) - avg_P = sum(r['ljpw']['P'] for r in valid_results) / len(valid_results) - avg_W = sum(r['ljpw']['W'] for r in valid_results) / len(valid_results) - avg_health = sum(r['health'] for r in valid_results) / len(valid_results) + avg_L = sum(r["ljpw"]["L"] for r in valid_results) / len(valid_results) + avg_J = sum(r["ljpw"]["J"] for r in valid_results) / len(valid_results) + avg_P = sum(r["ljpw"]["P"] for r in valid_results) / len(valid_results) + avg_W = sum(r["ljpw"]["W"] for r in valid_results) / len(valid_results) + avg_health = sum(r["health"] for r in valid_results) / len(valid_results) print("AGGREGATE SCORES:") print("-" * 70) @@ -75,10 +76,10 @@ def main(): print() # Find problem areas - low_safety = [r for r in valid_results if r['ljpw']['L'] < 0.5] - low_structure = [r for r in valid_results if r['ljpw']['J'] < 0.4] - low_design = [r for r in valid_results if r['ljpw']['W'] < 0.5] - low_health = [r for r in valid_results if r['health'] < 0.5] + low_safety = [r for r in valid_results if r["ljpw"]["L"] < 0.5] + low_structure = [r for r in valid_results if r["ljpw"]["J"] < 0.4] + low_design = [r for r in valid_results if r["ljpw"]["W"] < 0.5] + low_health = [r for r in valid_results if r["health"] < 0.5] print("PROBLEM AREAS:") print("-" * 70) @@ -106,7 +107,7 @@ def main(): if low_health: print(f"\n{len(low_health)} files with LOW OVERALL HEALTH (<50%):") - sorted_by_health = sorted(low_health, key=lambda x: x['health']) + sorted_by_health = sorted(low_health, key=lambda x: x["health"]) for r in sorted_by_health[:5]: print(f" - {Path(r['filename']).name}: {r['health']*100:.1f}%") if len(low_health) > 5: @@ -120,17 +121,18 @@ def main(): print() print("TOP 5 HEALTHIEST FILES:") print("-" * 70) - sorted_by_health = sorted(valid_results, key=lambda x: x['health'], reverse=True) + sorted_by_health = sorted(valid_results, key=lambda x: x["health"], reverse=True) for i, r in enumerate(sorted_by_health[:5], 1): print(f"{i}. {Path(r['filename']).name}: {r['health']*100:.1f}%") - ljpw = r['ljpw'] + ljpw = r["ljpw"] print(f" L={ljpw['L']:.2f}, J={ljpw['J']:.2f}, P={ljpw['P']:.2f}, W={ljpw['W']:.2f}") print() - print("="*70) + print("=" * 70) print("TIP: Focus on fixing files with lowest health first") print("Next: Try running 03_compress_decompress.py") - print("="*70) + print("=" * 70) + -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/examples/basic/03_compress_decompress.py b/examples/basic/03_compress_decompress.py index ec41675..8244ef9 100644 --- a/examples/basic/03_compress_decompress.py +++ b/examples/basic/03_compress_decompress.py @@ -15,30 +15,31 @@ import sys from pathlib import Path -# Add parent directory to path +# Add parent directory to path for src module imports sys.path.insert(0, str(Path(__file__).parent.parent.parent)) -from ljpw_standalone import SimpleCodeAnalyzer -from ljpw_semantic_compressor import SemanticCompressor, SemanticDecompressor +from src.ljpw.ljpw_semantic_compressor import SemanticCompressor, SemanticDecompressor +from src.ljpw.ljpw_standalone import SimpleCodeAnalyzer + def main(): - print("="*70) + print("=" * 70) print("LJPW Example 3: Compress and Decompress") - print("="*70) + print("=" * 70) print() # Sample code files to analyze code_samples = { - 'v1_initial.py': ''' + "v1_initial.py": """ def process(data): return [x*2 for x in data] -''', - 'v2_typed.py': ''' +""", + "v2_typed.py": ''' def process(data: list) -> list: """Process data""" return [x*2 for x in data] ''', - 'v3_safe.py': ''' + "v3_safe.py": ''' def process(data: list) -> list: """Process data with validation""" if not data: @@ -60,8 +61,8 @@ def process(data: list) -> list: for name, code in code_samples.items(): result = analyzer.analyze(code, name) - ljpw = result['ljpw'] - state = (ljpw['L'], ljpw['J'], ljpw['P'], ljpw['W']) + ljpw = result["ljpw"] + state = (ljpw["L"], ljpw["J"], ljpw["P"], ljpw["W"]) states.append(state) print(f"\n{name}:") @@ -75,13 +76,12 @@ def process(data: list) -> list: compressor = SemanticCompressor() genome = compressor.compress_state_sequence( - states, - metadata={'project': 'example', 'versions': len(states)} + states, metadata={"project": "example", "versions": len(states)} ) # Calculate sizes - original_size = len(str(states).encode('utf-8')) - compressed_size = len(genome.to_string().encode('utf-8')) + original_size = len(str(states).encode("utf-8")) + compressed_size = len(genome.to_string().encode("utf-8")) ratio = original_size / compressed_size print(f"\nOriginal size: {original_size} bytes") @@ -107,12 +107,16 @@ def process(data: list) -> list: total_error = 0 for i, (original, recon) in enumerate(zip(states, reconstructed)): - error = sum((o - r)**2 for o, r in zip(original, recon))**0.5 + error = sum((o - r) ** 2 for o, r in zip(original, recon)) ** 0.5 total_error += error print(f"\nState {i+1}:") - print(f" Original: L={original[0]:.3f}, J={original[1]:.3f}, P={original[2]:.3f}, W={original[3]:.3f}") - print(f" Reconstructed: L={recon[0]:.3f}, J={recon[1]:.3f}, P={recon[2]:.3f}, W={recon[3]:.3f}") + print( + f" Original: L={original[0]:.3f}, J={original[1]:.3f}, P={original[2]:.3f}, W={original[3]:.3f}" + ) + print( + f" Reconstructed: L={recon[0]:.3f}, J={recon[1]:.3f}, P={recon[2]:.3f}, W={recon[3]:.3f}" + ) print(f" Error: {error:.4f}") avg_error = total_error / len(states) @@ -132,7 +136,7 @@ def process(data: list) -> list: print(f"Integrity score: {validation['integrity_score']:.1%}") print(f"Error count: {validation['error_count']}") - if validation['valid']: + if validation["valid"]: print("\n✓ Genome is intact! All checksums passed.") else: print(f"\n✗ Genome has {validation['error_count']} checksum errors") @@ -162,10 +166,10 @@ def process(data: list) -> list: test_state = states[0] # Use first state for comparison precision_levels = [ - (4, 'fast'), - (8, 'balanced'), - (16, 'precise'), - (32, 'exact'), + (4, "fast"), + (8, "balanced"), + (16, "precise"), + (32, "exact"), ] print(f"{'Levels':<10} {'Use Case':<12} {'Error':<12} {'Size':<10}") @@ -178,7 +182,7 @@ def process(data: list) -> list: g = comp.compress_state_sequence([test_state]) recon = decomp.decompress_genome(g)[0] - error = sum((o - r)**2 for o, r in zip(test_state, recon))**0.5 + error = sum((o - r) ** 2 for o, r in zip(test_state, recon)) ** 0.5 size = len(g.to_string()) print(f"{levels:<10} {use_case:<12} {error:<12.4f} {size:<10} bytes") @@ -187,7 +191,7 @@ def process(data: list) -> list: print("Tip: Use LJPWQuantizer.recommend_levels('balanced') for guidance") print() - print("="*70) + print("=" * 70) print("KEY TAKEAWAYS:") print("-" * 70) print("1. LJPW compresses code quality to tiny genomes") @@ -197,7 +201,8 @@ def process(data: list) -> list: print("5. Use recommend_levels() for guidance on precision settings") print() print("Next: Try running 04_interpret_scores.py") - print("="*70) + print("=" * 70) + -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/examples/cross_language/quicksort/quicksort.py b/examples/cross_language/quicksort/quicksort.py index 54be2da..cdfdeac 100644 --- a/examples/cross_language/quicksort/quicksort.py +++ b/examples/cross_language/quicksort/quicksort.py @@ -6,6 +6,7 @@ Average time complexity: O(n log n) """ + def quicksort(arr): """ Sort an array using the quicksort algorithm. @@ -76,14 +77,9 @@ def partition(arr, low, high): # Example usage -if __name__ == '__main__': +if __name__ == "__main__": # Test data - test_arrays = [ - [64, 34, 25, 12, 22, 11, 90], - [5, 1, 4, 2, 8], - [1], - [] - ] + test_arrays = [[64, 34, 25, 12, 22, 11, 90], [5, 1, 4, 2, 8], [1], []] for arr in test_arrays: sorted_arr = quicksort(arr.copy()) diff --git a/examples/cross_language_demo.py b/examples/cross_language_demo.py index f664587..f54b78a 100644 --- a/examples/cross_language_demo.py +++ b/examples/cross_language_demo.py @@ -13,17 +13,18 @@ from pathlib import Path # Add tools to path -sys.path.insert(0, str(Path(__file__).parent.parent / 'tools')) +sys.path.insert(0, str(Path(__file__).parent.parent / "tools")) -from multi_language_analyzer import compare_implementations, analyze_code import json +from multi_language_analyzer import analyze_code, compare_implementations + def print_header(title): """Print formatted header""" - print("\n" + "="*80) + print("\n" + "=" * 80) print(f"{title:^80}") - print("="*80 + "\n") + print("=" * 80 + "\n") def main(): @@ -34,14 +35,14 @@ def main(): print("Hypothesis: Same algorithm in different languages → Similar LJPW coordinates") print("Algorithm: QuickSort (divide-and-conquer sorting)") print("Languages: Python, JavaScript, Go") - print("\n" + "="*80) + print("\n" + "=" * 80) # File paths - base_path = Path(__file__).parent / 'cross_language' / 'quicksort' + base_path = Path(__file__).parent / "cross_language" / "quicksort" files = { - 'python': str(base_path / 'quicksort.py'), - 'javascript': str(base_path / 'quicksort.js'), - 'go': str(base_path / 'quicksort.go'), + "python": str(base_path / "quicksort.py"), + "javascript": str(base_path / "quicksort.js"), + "go": str(base_path / "quicksort.go"), } # Analyze @@ -52,32 +53,34 @@ def main(): print_header("INDIVIDUAL LJPW COORDINATES") print(f"{'Language':<15} {'L':<10} {'J':<10} {'P':<10} {'W':<10} {'Health':<10}") - print("-"*80) - - for lang, data in results['implementations'].items(): - if 'error' not in data: - print(f"{lang.capitalize():<15} " - f"{data['L']:.3f} " - f"{data['J']:.3f} " - f"{data['P']:.3f} " - f"{data['W']:.3f} " - f"{data['health_score']:.1f}%") + print("-" * 80) + + for lang, data in results["implementations"].items(): + if "error" not in data: + print( + f"{lang.capitalize():<15} " + f"{data['L']:.3f} " + f"{data['J']:.3f} " + f"{data['P']:.3f} " + f"{data['W']:.3f} " + f"{data['health_score']:.1f}%" + ) # Display statistics print_header("CROSS-LANGUAGE STATISTICS") - if 'statistics' in results and results['statistics']: - stats = results['statistics'] + if "statistics" in results and results["statistics"]: + stats = results["statistics"] print("Mean LJPW Coordinates:") - mean = stats['mean'] + mean = stats["mean"] print(f" L (Love/Safety): {mean['L']:.3f}") print(f" J (Justice/Structure): {mean['J']:.3f}") print(f" P (Power/Performance): {mean['P']:.3f}") print(f" W (Wisdom/Design): {mean['W']:.3f}") print("\nStandard Deviation (σ):") - std = stats['std_dev'] + std = stats["std_dev"] print(f" L: {std['L']:.3f}") print(f" J: {std['J']:.3f}") print(f" P: {std['P']:.3f}") @@ -85,11 +88,11 @@ def main(): print(f" Average σ: {sum(std.values())/4:.3f}") print("\nCross-Language Variance:") - variance = stats['cross_language_variance'] + variance = stats["cross_language_variance"] print(f" Total Variance: {variance:.4f}") # Invariance score - invariance = results.get('cross_language_invariance_score', 0) + invariance = results.get("cross_language_invariance_score", 0) print(f" Invariance Score: {invariance:.2f}") if invariance > 0.90: @@ -104,15 +107,19 @@ def main(): # Display pairwise distances print_header("PAIRWISE SEMANTIC DISTANCES") - if 'pairwise_distances' in results: - distances = results['pairwise_distances'] + if "pairwise_distances" in results: + distances = results["pairwise_distances"] print("Euclidean distance in 4D LJPW space:") print("(Lower = more semantically similar)\n") for pair, distance in sorted(distances.items(), key=lambda x: x[1]): - lang1, lang2 = pair.split('_vs_') - status = "✓ SIMILAR" if distance < 0.15 else ("⚠ MODERATE" if distance < 0.30 else "✗ DIFFERENT") + lang1, lang2 = pair.split("_vs_") + status = ( + "✓ SIMILAR" + if distance < 0.15 + else ("⚠ MODERATE" if distance < 0.30 else "✗ DIFFERENT") + ) print(f" {lang1.capitalize()} ↔ {lang2.capitalize():<15} {distance:.4f} {status}") avg_distance = sum(distances.values()) / len(distances) @@ -128,7 +135,8 @@ def main(): # Interpretation print_header("INTERPRETATION") - print(""" + print( + """ What This Demonstrates: 1. **Semantic Invariance**: The same algorithm (quicksort) maps to similar @@ -153,13 +161,14 @@ def main(): - Invariance score > 0.80 (80%+ of meaning preserved) If these hold → LJPW captures true semantic invariants ✓ -""") +""" + ) # Save results - output_file = Path(__file__).parent.parent / 'results' / 'cross_language_quicksort.json' + output_file = Path(__file__).parent.parent / "results" / "cross_language_quicksort.json" output_file.parent.mkdir(exist_ok=True) - with open(output_file, 'w') as f: + with open(output_file, "w") as f: json.dump(results, f, indent=2) print(f"\nResults saved to: {output_file}") @@ -167,9 +176,9 @@ def main(): # Verdict print_header("VERDICT") - if 'statistics' in results and results['statistics']: - variance = results['statistics']['cross_language_variance'] - invariance = results.get('cross_language_invariance_score', 0) + if "statistics" in results and results["statistics"]: + variance = results["statistics"]["cross_language_variance"] + invariance = results.get("cross_language_invariance_score", 0) if invariance > 0.80 and variance < 0.10: print("✅ HYPOTHESIS CONFIRMED") @@ -184,8 +193,8 @@ def main(): print(" LJPW coordinates vary significantly across languages") print(" May not capture language-independent semantics") - print("\n" + "="*80 + "\n") + print("\n" + "=" * 80 + "\n") -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/examples/django_query.py b/examples/django_query.py index 73d6717..68b5e2b 100644 --- a/examples/django_query.py +++ b/examples/django_query.py @@ -10,9 +10,8 @@ from itertools import chain, islice from weakref import ref as weak_ref -from asgiref.sync import sync_to_async - import django +from asgiref.sync import sync_to_async from django.conf import settings from django.core import exceptions from django.db import ( @@ -50,9 +49,7 @@ class BaseIterable: - def __init__( - self, queryset, chunked_fetch=False, chunk_size=GET_ITERATOR_CHUNK_SIZE - ): + def __init__(self, queryset, chunked_fetch=False, chunk_size=GET_ITERATOR_CHUNK_SIZE): self.queryset = queryset self.chunked_fetch = chunked_fetch self.chunk_size = chunk_size @@ -95,9 +92,7 @@ def __iter__(self): fetch_mode = queryset._fetch_mode # Execute the query. This will also fill compiler.select, klass_info, # and annotations. - results = compiler.execute_sql( - chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size - ) + results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size) select, klass_info, annotation_col_map = ( compiler.select, compiler.klass_info, @@ -106,9 +101,7 @@ def __iter__(self): model_cls = klass_info["model"] select_fields = klass_info["select_fields"] model_fields_start, model_fields_end = select_fields[0], select_fields[-1] + 1 - init_list = [ - f[0].target.attname for f in select[model_fields_start:model_fields_end] - ] + init_list = [f[0].target.attname for f in select[model_fields_start:model_fields_end]] related_populators = get_related_populators(klass_info, select, db, fetch_mode) known_related_objects = [ ( @@ -180,21 +173,15 @@ def __iter__(self): annotation_fields, ) = self.queryset.resolve_model_init_order() model_cls = self.queryset.model - if any( - f.attname not in model_init_names for f in model_cls._meta.pk_fields - ): - raise exceptions.FieldDoesNotExist( - "Raw query must include the primary key" - ) + if any(f.attname not in model_init_names for f in model_cls._meta.pk_fields): + raise exceptions.FieldDoesNotExist("Raw query must include the primary key") fields = [self.queryset.model_fields.get(c) for c in self.queryset.columns] cols = [f.get_col(f.model._meta.db_table) if f else None for f in fields] converters = compiler.get_converters(cols) if converters: query_iterator = compiler.apply_converters(query_iterator, converters) if compiler.has_composite_fields(cols): - query_iterator = compiler.composite_fields_to_tuples( - query_iterator, cols - ) + query_iterator = compiler.composite_fields_to_tuples(query_iterator, cols) fetch_mode = self.queryset._fetch_mode peers = [] for values in query_iterator: @@ -367,8 +354,7 @@ def __setstate__(self, state): if pickled_version != django.__version__: warnings.warn( "Pickled queryset instance's Django version %s does not " - "match the current version %s." - % (pickled_version, django.__version__), + "match the current version %s." % (pickled_version, django.__version__), RuntimeWarning, stacklevel=2, ) @@ -426,15 +412,11 @@ def __getitem__(self, k): """Retrieve an item or slice from the set of results.""" if not isinstance(k, (int, slice)): raise TypeError( - "QuerySet indices must be integers or slices, not %s." - % type(k).__name__ + "QuerySet indices must be integers or slices, not %s." % type(k).__name__ ) if (isinstance(k, int) and k < 0) or ( isinstance(k, slice) - and ( - (k.start is not None and k.start < 0) - or (k.stop is not None and k.stop < 0) - ) + and ((k.start is not None and k.start < 0) or (k.stop is not None and k.stop < 0)) ): raise ValueError("Negative indexing is not supported.") @@ -569,17 +551,13 @@ async def aiterator(self, chunk_size=2000): async for item in iterable: results.append(item) if len(results) >= chunk_size: - await aprefetch_related_objects( - results, *self._prefetch_related_lookups - ) + await aprefetch_related_objects(results, *self._prefetch_related_lookups) for result in results: yield result results.clear() if results: - await aprefetch_related_objects( - results, *self._prefetch_related_lookups - ) + await aprefetch_related_objects(results, *self._prefetch_related_lookups) for result in results: yield result else: @@ -596,9 +574,7 @@ def aggregate(self, *args, **kwargs): """ if self.query.distinct_fields: raise NotImplementedError("aggregate() + distinct(fields) not implemented.") - self._validate_values_are_expressions( - (*args, *kwargs.values()), method_name="aggregate" - ) + self._validate_values_are_expressions((*args, *kwargs.values()), method_name="aggregate") for arg in args: # The default_alias property raises TypeError if default_alias # can't be set automatically or AttributeError if it isn't an @@ -715,9 +691,7 @@ def _check_bulk_create_options( self, ignore_conflicts, update_conflicts, update_fields, unique_fields ): if ignore_conflicts and update_conflicts: - raise ValueError( - "ignore_conflicts and update_conflicts are mutually exclusive." - ) + raise ValueError("ignore_conflicts and update_conflicts are mutually exclusive.") db_features = connections[self.db].features if ignore_conflicts: if not db_features.supports_ignore_conflicts: @@ -742,25 +716,20 @@ def _check_bulk_create_options( "the upsert." ) if not unique_fields and db_features.supports_update_conflicts_with_target: - raise ValueError( - "Unique fields that can trigger the upsert must be provided." - ) + raise ValueError("Unique fields that can trigger the upsert must be provided.") # Updating primary keys and non-concrete fields is forbidden. if any(not f.concrete for f in update_fields): raise ValueError( - "bulk_create() can only be used with concrete fields in " - "update_fields." + "bulk_create() can only be used with concrete fields in " "update_fields." ) if any(f in self.model._meta.pk_fields for f in update_fields): raise ValueError( - "bulk_create() cannot be used with primary keys in " - "update_fields." + "bulk_create() cannot be used with primary keys in " "update_fields." ) if unique_fields: if any(not f.concrete for f in unique_fields): raise ValueError( - "bulk_create() can only be used with concrete fields " - "in unique_fields." + "bulk_create() can only be used with concrete fields " "in unique_fields." ) return OnConflict.UPDATE return None @@ -857,10 +826,7 @@ def bulk_create( unique_fields=unique_fields, ) connection = connections[self.db] - if ( - connection.features.can_return_rows_from_bulk_insert - and on_conflict is None - ): + if connection.features.can_return_rows_from_bulk_insert and on_conflict is None: assert len(returned_columns) == len(objs_without_pk) for obj_without_pk, results in zip(objs_without_pk, returned_columns): for result, field in zip(results, opts.db_returning_fields): @@ -880,9 +846,7 @@ def _handle_order_with_respect_to(self, objs): group_key = tuple(get_filter_kwargs_for_object(obj).values()) group_keys.add(group_key) obj_groups.append((obj, group_key)) - filters = [ - Q.create(list(zip(attnames, group_key))) for group_key in group_keys - ] + filters = [Q.create(list(zip(attnames, group_key))) for group_key in group_keys] next_orders = ( self.model._base_manager.using(self.db) .filter(reduce(operator.or_, filters)) @@ -946,16 +910,12 @@ def bulk_update(self, objs, fields, batch_size=None): if not objs: return 0 for obj in objs: - obj._prepare_related_fields_for_save( - operation_name="bulk_update", fields=fields - ) + obj._prepare_related_fields_for_save(operation_name="bulk_update", fields=fields) # PK is used twice in the resulting update query, once in the filter # and once in the WHEN. Each field will also have one CAST. self._for_write = True connection = connections[self.db] - max_batch_size = connection.ops.bulk_batch_size( - [opts.pk, opts.pk, *fields], objs - ) + max_batch_size = connection.ops.bulk_batch_size([opts.pk, opts.pk, *fields], objs) batch_size = min(batch_size, max_batch_size) if batch_size else max_batch_size requires_casting = connection.features.requires_casted_case_in_updates batches = (objs[i : i + batch_size] for i in range(0, len(objs), batch_size)) @@ -1044,9 +1004,7 @@ def update_or_create(self, defaults=None, create_defaults=None, **kwargs): with transaction.atomic(using=self.db): # Lock the row so that a concurrent update is blocked until # update_or_create() has performed its save. - obj, created = self.select_for_update().get_or_create( - create_defaults, **kwargs - ) + obj, created = self.select_for_update().get_or_create(create_defaults, **kwargs) if created: return obj, created for k, v in resolve_callables(update_defaults): @@ -1062,9 +1020,7 @@ def update_or_create(self, defaults=None, create_defaults=None, **kwargs): # update_fields list. pk_fields = self.model._meta.pk_fields for field in self.model._meta.local_concrete_fields: - if not ( - field in pk_fields or field.__class__.pre_save is Field.pre_save - ): + if not (field in pk_fields or field.__class__.pre_save is Field.pre_save): update_fields.add(field.name) if field.name != field.attname: update_fields.add(field.attname) @@ -1201,8 +1157,7 @@ def in_bulk(self, id_list=None, *, field_name="pk"): and self.query.distinct_fields != (field_name,) ): raise ValueError( - "in_bulk()'s field_name must be a unique field but %r isn't." - % field_name + "in_bulk()'s field_name must be a unique field but %r isn't." % field_name ) qs = self @@ -1253,9 +1208,7 @@ def get_obj(obj): # noqa: F811 get_obj = operator.itemgetter(1) else: - raise TypeError( - f"in_bulk() cannot be used with {self._iterable_class.__name__}." - ) + raise TypeError(f"in_bulk() cannot be used with {self._iterable_class.__name__}.") if id_list is not None: filter_key = "{}__in".format(field_name) @@ -1419,9 +1372,7 @@ def contains(self, obj): """ self._not_support_combined_queries("contains") if self._fields is not None: - raise TypeError( - "Cannot call QuerySet.contains() after .values() or .values_list()." - ) + raise TypeError("Cannot call QuerySet.contains() after .values() or .values_list().") try: if obj._meta.concrete_model != self.model._meta.concrete_model: return False @@ -1474,9 +1425,7 @@ def _values(self, *fields, **expressions): if expressions: # RemovedInDjango70Warning: When the deprecation ends, deindent as: # clone = clone.annotate(**expressions) - with warnings.catch_warnings( - action="ignore", category=RemovedInDjango70Warning - ): + with warnings.catch_warnings(action="ignore", category=RemovedInDjango70Warning): clone = clone.annotate(**expressions) clone._fields = fields clone.query.set_values(fields) @@ -1494,8 +1443,7 @@ def values_list(self, *fields, flat=False, named=False): if flat: if len(fields) > 1: raise TypeError( - "'flat' is not valid when values_list is called with more than one " - "field." + "'flat' is not valid when values_list is called with more than one " "field." ) elif not fields: # RemovedInDjango70Warning: When the deprecation ends, replace @@ -1520,9 +1468,7 @@ def values_list(self, *fields, flat=False, named=False): field_name = field expression = None if hasattr(field, "resolve_expression"): - field_name = getattr( - field, "default_alias", field.__class__.__name__.lower() - ) + field_name = getattr(field, "default_alias", field.__class__.__name__.lower()) expression = field # For backward compatibility reasons expressions are always # prefixed with the counter even if their default alias doesn't @@ -1737,9 +1683,7 @@ def select_related(self, *fields): """ self._not_support_combined_queries("select_related") if self._fields is not None: - raise TypeError( - "Cannot call select_related() after .values() or .values_list()" - ) + raise TypeError("Cannot call select_related() after .values() or .values_list()") obj = self._chain() if fields == (None,): @@ -1769,9 +1713,7 @@ def prefetch_related(self, *lookups): lookup = lookup.prefetch_to lookup = lookup.split(LOOKUP_SEP, 1)[0] if lookup in self.query._filtered_relations: - raise ValueError( - "prefetch_related() is not supported with FilteredRelation." - ) + raise ValueError("prefetch_related() is not supported with FilteredRelation.") clone._prefetch_related_lookups = clone._prefetch_related_lookups + lookups return clone @@ -1791,9 +1733,7 @@ def alias(self, *args, **kwargs): return self._annotate(args, kwargs, select=False) def _annotate(self, args, kwargs, select=True): - self._validate_values_are_expressions( - args + tuple(kwargs.values()), method_name="annotate" - ) + self._validate_values_are_expressions(args + tuple(kwargs.values()), method_name="annotate") annotations = {} for arg in args: # The default_alias property raises TypeError if default_alias @@ -1815,11 +1755,7 @@ def _annotate(self, args, kwargs, select=True): if names is None: names = set( chain.from_iterable( - ( - (field.name, field.attname) - if hasattr(field, "attname") - else (field.name,) - ) + ((field.name, field.attname) if hasattr(field, "attname") else (field.name,)) for field in self.model._meta.get_fields() ) ) @@ -1827,8 +1763,7 @@ def _annotate(self, args, kwargs, select=True): for alias, annotation in annotations.items(): if alias in names: raise ValueError( - "The annotation '%s' conflicts with a field on " - "the model." % alias + "The annotation '%s' conflicts with a field on " "the model." % alias ) if isinstance(annotation, FilteredRelation): clone.query.add_filtered_relation(annotation, alias) @@ -1863,9 +1798,7 @@ def distinct(self, *field_names): """ self._not_support_combined_queries("distinct") if self.query.is_sliced: - raise TypeError( - "Cannot create distinct fields once a slice has been taken." - ) + raise TypeError("Cannot create distinct fields once a slice has been taken.") obj = self._chain() obj.query.add_distinct_fields(*field_names) return obj @@ -2150,9 +2083,7 @@ def _has_filters(self): @staticmethod def _validate_values_are_expressions(values, method_name): - invalid_args = sorted( - str(arg) for arg in values if not hasattr(arg, "resolve_expression") - ) + invalid_args = sorted(str(arg) for arg in values if not hasattr(arg, "resolve_expression")) if invalid_args: raise TypeError( "QuerySet.%s() received non-expression(s): %s." @@ -2177,8 +2108,7 @@ def _check_ordering_first_last_queryset_aggregation(self, method): if ( isinstance(self.query.group_by, tuple) # Raise if the pk fields are not in the group_by. - and self.model._meta.pk - not in {col.output_field for col in self.query.group_by} + and self.model._meta.pk not in {col.output_field for col in self.query.group_by} and set(self.model._meta.pk_fields).difference( {col.target for col in self.query.group_by} ) @@ -2237,18 +2167,14 @@ def resolve_model_init_order(self): """Resolve the init field names and value positions.""" converter = connections[self.db].introspection.identifier_converter model_init_fields = [ - field - for column_name, field in self.model_fields.items() - if column_name in self.columns + field for column_name, field in self.model_fields.items() if column_name in self.columns ] annotation_fields = [ (column, pos) for pos, column in enumerate(self.columns) if column not in self.model_fields ] - model_init_order = [ - self.columns.index(converter(f.column)) for f in model_init_fields - ] + model_init_order = [self.columns.index(converter(f.column)) for f in model_init_fields] model_init_names = [f.attname for f in model_init_fields] return model_init_names, model_init_order, annotation_fields @@ -2377,13 +2303,9 @@ def __init__(self, lookup, queryset=None, to_attr=None): and not issubclass(queryset._iterable_class, ModelIterable) ) ): - raise ValueError( - "Prefetch querysets cannot use raw(), values(), and values_list()." - ) + raise ValueError("Prefetch querysets cannot use raw(), values(), and values_list().") if to_attr: - self.prefetch_to = LOOKUP_SEP.join( - lookup.split(LOOKUP_SEP)[:-1] + [to_attr] - ) + self.prefetch_to = LOOKUP_SEP.join(lookup.split(LOOKUP_SEP)[:-1] + [to_attr]) self.queryset = queryset self.to_attr = to_attr @@ -2412,10 +2334,7 @@ def get_current_to_attr(self, level): return to_attr, as_attr def get_current_querysets(self, level): - if ( - self.get_current_prefetch_to(level) == self.prefetch_to - and self.queryset is not None - ): + if self.get_current_prefetch_to(level) == self.prefetch_to and self.queryset is not None: return [self.queryset] return None @@ -2463,8 +2382,7 @@ def prefetch_related_objects(model_instances, *related_lookups): if lookup.queryset is not None: raise ValueError( "'%s' lookup was already seen with a different queryset. " - "You may need to adjust the ordering of your lookups." - % lookup.prefetch_to + "You may need to adjust the ordering of your lookups." % lookup.prefetch_to ) continue @@ -2598,9 +2516,7 @@ def prefetch_related_objects(model_instances, *related_lookups): async def aprefetch_related_objects(model_instances, *related_lookups): """See prefetch_related_objects().""" - return await sync_to_async(prefetch_related_objects)( - model_instances, *related_lookups - ) + return await sync_to_async(prefetch_related_objects)(model_instances, *related_lookups) def get_prefetcher(instance, through_attr, to_attr): @@ -2699,9 +2615,7 @@ def prefetch_one_level(instances, prefetcher, lookup, level): single, cache_name, is_descriptor, - ) = prefetcher.get_prefetch_querysets( - instances, lookup.get_current_querysets(level) - ) + ) = prefetcher.get_prefetch_querysets(instances, lookup.get_current_querysets(level)) # We have to handle the possibility that the QuerySet we just got back # contains some prefetch_related lookups. We don't want to trigger the # prefetch_related functionality by evaluating the query. Rather, we need @@ -2825,17 +2739,11 @@ def __init__(self, klass_info, select, db, fetch_mode): if not from_parent: self.cols_start = select_fields[0] self.cols_end = select_fields[-1] + 1 - self.init_list = [ - f[0].target.attname for f in select[self.cols_start : self.cols_end] - ] + self.init_list = [f[0].target.attname for f in select[self.cols_start : self.cols_end]] self.reorder_for_init = None else: - attname_indexes = { - select[idx][0].target.attname: idx for idx in select_fields - } - model_init_attnames = ( - f.attname for f in klass_info["model"]._meta.concrete_fields - ) + attname_indexes = {select[idx][0].target.attname: idx for idx in select_fields} + model_init_attnames = (f.attname for f in klass_info["model"]._meta.concrete_fields) self.init_list = [ attname for attname in model_init_attnames if attname in attname_indexes ] @@ -2849,9 +2757,7 @@ def __init__(self, klass_info, select, db, fetch_mode): # relationship. Therefore checking for a single member of the primary # key is enough to determine if the referenced object exists or not. self.pk_idx = self.init_list.index(self.model_cls._meta.pk_fields[0].attname) - self.related_populators = get_related_populators( - klass_info, select, self.db, fetch_mode - ) + self.related_populators = get_related_populators(klass_info, select, self.db, fetch_mode) self.local_setter = klass_info["local_setter"] self.remote_setter = klass_info["remote_setter"] diff --git a/examples/dna_ljpw_analysis.py b/examples/dna_ljpw_analysis.py index 2a29f9c..54af232 100644 --- a/examples/dna_ljpw_analysis.py +++ b/examples/dna_ljpw_analysis.py @@ -11,9 +11,9 @@ P_NE = 0.718282 # e - 2 (Exponential base) W_NE = 0.693147 # ln(2) (Information unit) -print("="*70) +print("=" * 70) print("DNA <-> LJPW CORRESPONDENCE ANALYSIS") -print("="*70) +print("=" * 70) # === SECTION 1: Basic Ratios === print("\n1. BASIC RATIOS IN LJPW") @@ -21,12 +21,12 @@ # Pairwise ratios ratios = { - 'L/J': L_NE / J_NE, - 'L/P': L_NE / P_NE, - 'L/W': L_NE / W_NE, - 'J/P': J_NE / P_NE, - 'J/W': J_NE / W_NE, - 'P/W': P_NE / W_NE, + "L/J": L_NE / J_NE, + "L/P": L_NE / P_NE, + "L/W": L_NE / W_NE, + "J/P": J_NE / P_NE, + "J/W": J_NE / W_NE, + "P/W": P_NE / W_NE, } for pair, ratio in ratios.items(): @@ -81,12 +81,16 @@ # If we divide LJPW into "high value" (P, W) vs "low value" (L, J) high_value_sum = P_NE + W_NE # Like GC in DNA -low_value_sum = L_NE + J_NE # Like AT in DNA +low_value_sum = L_NE + J_NE # Like AT in DNA print(f"High-value dimensions (P+W) = {high_value_sum:.6f}") print(f"Low-value dimensions (L+J) = {low_value_sum:.6f}") -print(f"'GC Content' = (P+W)/(total) = {high_value_sum/total_sum:.6f} = {100*high_value_sum/total_sum:.1f}%") -print(f"'AT Content' = (L+J)/(total) = {low_value_sum/total_sum:.6f} = {100*low_value_sum/total_sum:.1f}%") +print( + f"'GC Content' = (P+W)/(total) = {high_value_sum/total_sum:.6f} = {100*high_value_sum/total_sum:.1f}%" +) +print( + f"'AT Content' = (L+J)/(total) = {low_value_sum/total_sum:.6f} = {100*low_value_sum/total_sum:.1f}%" +) # === SECTION 5: Structural Categories (Purine/Pyrimidine Analogy) === print("\n5. STRUCTURAL CATEGORIES") @@ -120,10 +124,12 @@ print(f" W: {norm_W:.4f} ({100*norm_W:.1f}%)") # Shannon entropy -entropy = -(norm_L * math.log2(norm_L) + - norm_J * math.log2(norm_J) + - norm_P * math.log2(norm_P) + - norm_W * math.log2(norm_W)) +entropy = -( + norm_L * math.log2(norm_L) + + norm_J * math.log2(norm_J) + + norm_P * math.log2(norm_P) + + norm_W * math.log2(norm_W) +) print(f"\nShannon Entropy H = {entropy:.6f} bits") print(f"Maximum possible (uniform) = {math.log2(4):.6f} bits") @@ -162,13 +168,13 @@ # === SECTION 9: Potential Mapping === print("\n9. PROPOSED DNA <-> LJPW MAPPING") -print("="*70) +print("=" * 70) mapping = { - 'G': ('L', 0.618, 'Purine, strong binder -> Love, force multiplier'), - 'C': ('W', 0.693, 'Pyrimidine, G-pair -> Wisdom, amplified by Love'), - 'A': ('P', 0.718, 'Purine, weak binder -> Power, creates tension'), - 'T': ('J', 0.414, 'Pyrimidine, A-pair -> Justice, vulnerable to Power'), + "G": ("L", 0.618, "Purine, strong binder -> Love, force multiplier"), + "C": ("W", 0.693, "Pyrimidine, G-pair -> Wisdom, amplified by Love"), + "A": ("P", 0.718, "Purine, weak binder -> Power, creates tension"), + "T": ("J", 0.414, "Pyrimidine, A-pair -> Justice, vulnerable to Power"), } print(f"\n{'DNA':<6} {'LJPW':<6} {'Value':<10} {'Rationale'}") @@ -176,4 +182,4 @@ for dna_base, (ljpw_dim, value, rationale) in mapping.items(): print(f"{dna_base:<6} -> {ljpw_dim:<6} ({value:.3f}) {rationale}") -print("\n" + "="*70) +print("\n" + "=" * 70) diff --git a/examples/integrations/pre_commit_hook.py b/examples/integrations/pre_commit_hook.py index f9f2e32..3d734dd 100644 --- a/examples/integrations/pre_commit_hook.py +++ b/examples/integrations/pre_commit_hook.py @@ -16,48 +16,50 @@ python pre_commit_hook.py --check """ -import sys import subprocess +import sys from pathlib import Path -# Add parent directory to path +# Add parent directory to path for src module imports sys.path.insert(0, str(Path(__file__).parent.parent.parent)) -from ljpw_standalone import SimpleCodeAnalyzer +from src.ljpw.ljpw_standalone import SimpleCodeAnalyzer # Configuration HEALTH_THRESHOLD = 0.50 # 50% minimum health STRICT_MODE = False # If True, also check individual dimensions + def get_staged_files(): """Get list of staged Python files""" try: result = subprocess.run( - ['git', 'diff', '--cached', '--name-only', '--diff-filter=ACM'], + ["git", "diff", "--cached", "--name-only", "--diff-filter=ACM"], capture_output=True, text=True, - check=True + check=True, ) - files = result.stdout.strip().split('\n') + files = result.stdout.strip().split("\n") # Filter for code files - code_extensions = {'.py', '.js', '.java', '.rs', '.go', '.cpp', '.c', '.ts'} + code_extensions = {".py", ".js", ".java", ".rs", ".go", ".cpp", ".c", ".ts"} return [f for f in files if Path(f).suffix in code_extensions and Path(f).exists()] except subprocess.CalledProcessError: return [] + def check_file_quality(filepath): """Check if file meets quality standards""" analyzer = SimpleCodeAnalyzer() try: - with open(filepath, 'r', encoding='utf-8') as f: + with open(filepath, "r", encoding="utf-8") as f: code = f.read() result = analyzer.analyze(code, filepath) # Check health threshold - health = result['health'] - ljpw = result['ljpw'] + health = result["health"] + ljpw = result["ljpw"] issues = [] @@ -66,32 +68,33 @@ def check_file_quality(filepath): if STRICT_MODE: # Check individual dimensions - if ljpw['L'] < 0.3: + if ljpw["L"] < 0.3: issues.append("CRITICAL: Very low safety (L<0.3)") - if ljpw['J'] < 0.2: + if ljpw["J"] < 0.2: issues.append("CRITICAL: Very low structure (J<0.2)") return { - 'filepath': filepath, - 'health': health, - 'ljpw': ljpw, - 'issues': issues, - 'passes': len(issues) == 0 + "filepath": filepath, + "health": health, + "ljpw": ljpw, + "issues": issues, + "passes": len(issues) == 0, } except Exception as e: return { - 'filepath': filepath, - 'health': 0, - 'ljpw': {'L': 0, 'J': 0, 'P': 0, 'W': 0}, - 'issues': [f'Error analyzing: {e}'], - 'passes': True # Don't block on analysis errors + "filepath": filepath, + "health": 0, + "ljpw": {"L": 0, "J": 0, "P": 0, "W": 0}, + "issues": [f"Error analyzing: {e}"], + "passes": True, # Don't block on analysis errors } + def main(): - print("="*70) + print("=" * 70) print("LJPW Pre-commit Hook") - print("="*70) + print("=" * 70) print() # Get staged files @@ -111,7 +114,7 @@ def main(): results.append(result) # Print results - failed_files = [r for r in results if not r['passes']] + failed_files = [r for r in results if not r["passes"]] if failed_files: print("❌ COMMIT BLOCKED - Quality issues found:") @@ -120,21 +123,23 @@ def main(): for result in failed_files: print(f"\n{result['filepath']}:") print(f" Health: {result['health']*100:.1f}%") - ljpw = result['ljpw'] - print(f" LJPW: L={ljpw['L']:.2f}, J={ljpw['J']:.2f}, P={ljpw['P']:.2f}, W={ljpw['W']:.2f}") + ljpw = result["ljpw"] + print( + f" LJPW: L={ljpw['L']:.2f}, J={ljpw['J']:.2f}, P={ljpw['P']:.2f}, W={ljpw['W']:.2f}" + ) print(" Issues:") - for issue in result['issues']: + for issue in result["issues"]: print(f" - {issue}") print("\n Suggestions:") - if ljpw['L'] < 0.5: + if ljpw["L"] < 0.5: print(" - Add error handling (try/except)") print(" - Add input validation") - if ljpw['J'] < 0.4: + if ljpw["J"] < 0.4: print(" - Add type annotations") print(" - Add docstrings") - if ljpw['W'] < 0.5: + if ljpw["W"] < 0.5: print(" - Refactor into smaller functions") print(" - Add abstractions") @@ -142,7 +147,7 @@ def main(): print("-" * 70) print("Fix the issues above and try again") print(f"Or bypass check with: git commit --no-verify") - print("="*70) + print("=" * 70) return 1 else: @@ -150,14 +155,15 @@ def main(): print("-" * 70) for result in results: - ljpw = result['ljpw'] + ljpw = result["ljpw"] print(f"{result['filepath']}: {result['health']*100:.1f}%") print(f" L={ljpw['L']:.2f}, J={ljpw['J']:.2f}, P={ljpw['P']:.2f}, W={ljpw['W']:.2f}") print() - print("="*70) + print("=" * 70) print("Proceeding with commit...") return 0 -if __name__ == '__main__': + +if __name__ == "__main__": sys.exit(main()) diff --git a/examples/large_corpus_test.py b/examples/large_corpus_test.py index b46c8df..ddc3c64 100644 --- a/examples/large_corpus_test.py +++ b/examples/large_corpus_test.py @@ -4,15 +4,15 @@ Processes massive amounts of text to test compression performance at scale """ +# Import LJPW components +import importlib.util +import math import os import random -import time import string -import math -from typing import List, Tuple, Dict, Any +import time +from typing import Any, Dict, List, Tuple -# Import LJPW components -import importlib.util def load_module(name, path): spec = importlib.util.spec_from_file_location(name, path) @@ -20,6 +20,7 @@ def load_module(name, path): spec.loader.exec_module(module) return module + # Load pipeline pipeline_mod = load_module("ljpw_pipeline", "ljpw_pipeline.py") LJPWPipeline = pipeline_mod.LJPWPipeline @@ -28,17 +29,18 @@ def load_module(name, path): # LARGE CORPUS GENERATION # ============================================================================ + class CorpusGenerator: """Generate diverse, realistic code corpus at scale""" - + def __init__(self): self.templates = self._build_templates() self.code_patterns = self._build_code_patterns() - + def _build_templates(self) -> Dict[str, List[str]]: """Build realistic code templates""" return { - 'classes': [ + "classes": [ """ class {name}: '''{description}''' @@ -69,7 +71,7 @@ def {utility_method}(self, {utility_params}) -> Any: return {utility_return} """, ], - 'functions': [ + "functions": [ """ def {name}({params}) -> {return_type}: '''{description} @@ -99,7 +101,7 @@ def {name}({params}) -> {return_type}: return {return_value} """, ], - 'algorithms': [ + "algorithms": [ """ def {algorithm_name}({params}) -> {return_type}: '''{algorithm_description} @@ -117,7 +119,7 @@ def {algorithm_name}({params}) -> {return_type}: return {result} """, ], - 'interfaces': [ + "interfaces": [ """ from abc import ABC, abstractmethod from typing import {type_imports} @@ -138,7 +140,7 @@ def {method_name_2}(self, {method_params_2}) -> {return_type_2}: {additional_methods} """, ], - 'data_structures': [ + "data_structures": [ """ @dataclass class {class_name}: @@ -170,120 +172,164 @@ def {comparator_method}(self, other) -> bool: '''Comparison for sorting''' {comparison_logic} """, - ] + ], } - + def _build_code_patterns(self) -> Dict[str, List[str]]: """Build realistic code patterns""" return { - 'class_names': [ - 'DataProcessor', 'CacheManager', 'AuthHandler', 'QueryBuilder', - 'EventEmitter', 'ConfigValidator', 'StateController', 'ServiceRegistry', - 'ConnectionPool', 'TaskScheduler', 'ResourceManager', 'ErrorHandler' + "class_names": [ + "DataProcessor", + "CacheManager", + "AuthHandler", + "QueryBuilder", + "EventEmitter", + "ConfigValidator", + "StateController", + "ServiceRegistry", + "ConnectionPool", + "TaskScheduler", + "ResourceManager", + "ErrorHandler", + ], + "function_names": [ + "process_data", + "validate_input", + "compute_hash", + "fetch_resource", + "parse_response", + "transform_format", + "filter_results", + "aggregate_data", + "optimize_performance", + "synchronize_state", + "serialize_object", + "deserialize_data", ], - 'function_names': [ - 'process_data', 'validate_input', 'compute_hash', 'fetch_resource', - 'parse_response', 'transform_format', 'filter_results', 'aggregate_data', - 'optimize_performance', 'synchronize_state', 'serialize_object', 'deserialize_data' + "algorithm_names": [ + "binary_search", + "quick_sort", + "merge_sort", + "heap_sort", + "depth_first_search", + "breadth_first_search", + "dijkstra", + "bellman_ford", + "k_means_clustering", + "linear_regression", + "gradient_descent", + "newton_method", ], - 'algorithm_names': [ - 'binary_search', 'quick_sort', 'merge_sort', 'heap_sort', - 'depth_first_search', 'breadth_first_search', 'dijkstra', 'bellman_ford', - 'k_means_clustering', 'linear_regression', 'gradient_descent', 'newton_method' + "variable_names": [ + "data", + "result", + "config", + "params", + "options", + "metadata", + "response", + "request", + "headers", + "payload", + "session", + "context", ], - 'variable_names': [ - 'data', 'result', 'config', 'params', 'options', 'metadata', - 'response', 'request', 'headers', 'payload', 'session', 'context' + "error_types": [ + "ValueError", + "TypeError", + "KeyError", + "IndexError", + "ConnectionError", + "TimeoutError", + "AuthenticationError", + "ValidationError", + "ConfigurationError", + "RuntimeError", ], - 'error_types': [ - 'ValueError', 'TypeError', 'KeyError', 'IndexError', - 'ConnectionError', 'TimeoutError', 'AuthenticationError', - 'ValidationError', 'ConfigurationError', 'RuntimeError' - ] } - - def generate_realistic_code(self, complexity_level: str = 'medium') -> str: + + def generate_realistic_code(self, complexity_level: str = "medium") -> str: """Generate realistic code based on complexity""" - if complexity_level == 'simple': + if complexity_level == "simple": return self._generate_simple_code() - elif complexity_level == 'medium': + elif complexity_level == "medium": return self._generate_medium_code() - elif complexity_level == 'complex': + elif complexity_level == "complex": return self._generate_complex_code() else: return self._generate_medium_code() - + def _generate_simple_code(self) -> str: """Generate simple function/class""" - template = random.choice(self.templates['functions']) - + template = random.choice(self.templates["functions"]) + return template.format( - name=random.choice(self.code_patterns['function_names']), - params='data: List[Any], config: Dict[str, Any]', - return_type='List[Any]', - description='Process and transform data', - arg_descriptions='data: Input data to process\nconfig: Configuration options', - return_description='Processed data', - exceptions='ValueError: When data is invalid', - time_complexity=random.choice(['n', 'n log n', 'n^2']), - space_complexity=random.choice(['1', 'n', 'n^2']), + name=random.choice(self.code_patterns["function_names"]), + params="data: List[Any], config: Dict[str, Any]", + return_type="List[Any]", + description="Process and transform data", + arg_descriptions="data: Input data to process\nconfig: Configuration options", + return_description="Processed data", + exceptions="ValueError: When data is invalid", + time_complexity=random.choice(["n", "n log n", "n^2"]), + space_complexity=random.choice(["1", "n", "n^2"]), validations=self._generate_validation_code(), main_logic=self._generate_processing_code(), result_processing=self._generate_result_code(), - return_value='result' + return_value="result", ) - + def _generate_medium_code(self) -> str: """Generate medium complexity code""" - template = random.choice(self.templates['classes']) - + template = random.choice(self.templates["classes"]) + return template.format( - name=random.choice(self.code_patterns['class_names']), - description='Handles {name} operations with validation and error handling'.format( - name=random.choice(['data', 'cache', 'auth', 'query', 'events']).lower() + name=random.choice(self.code_patterns["class_names"]), + description="Handles {name} operations with validation and error handling".format( + name=random.choice(["data", "cache", "auth", "query", "events"]).lower() ), - params='config: Dict[str, Any]', + params="config: Dict[str, Any]", validations=self._generate_validation_code(), initializations=self._generate_initialization_code(), - method_name=random.choice(['process', 'handle', 'execute', 'perform']), - method_params='input_data: Any', - return_type='Any', - method_description='Execute the core operation', + method_name=random.choice(["process", "handle", "execute", "perform"]), + method_params="input_data: Any", + return_type="Any", + method_description="Execute the core operation", method_body=self._generate_processing_code(), - exception_type=random.choice(self.code_patterns['error_types']), + exception_type=random.choice(self.code_patterns["error_types"]), error_handling=self._generate_error_handling_code(), - raise_or_return='raise', - return_value='result', - expected_type='Dict[str, Any]', + raise_or_return="raise", + return_value="result", + expected_type="Dict[str, Any]", validation_logic='if not data:\n raise ValueError("Data cannot be empty")', - utility_method='get_metrics', - utility_params='self', - utility_purpose='performance tracking', - utility_body='return self.metrics', - utility_return='self.metrics' + utility_method="get_metrics", + utility_params="self", + utility_purpose="performance tracking", + utility_body="return self.metrics", + utility_return="self.metrics", ) - + def _generate_complex_code(self) -> str: """Generate complex algorithmic code""" - template = random.choice(self.templates['algorithms']) - + template = random.choice(self.templates["algorithms"]) + return template.format( - algorithm_name=random.choice(self.code_patterns['algorithm_names']), - algorithm_description='Efficient implementation of {name} algorithm'.format( - name=random.choice(['sorting', 'searching', 'graph', 'optimization']) + algorithm_name=random.choice(self.code_patterns["algorithm_names"]), + algorithm_description="Efficient implementation of {name} algorithm".format( + name=random.choice(["sorting", "searching", "graph", "optimization"]) ), algorithm_details=self._generate_algorithm_details(), - params='data: List[Comparable], target: Optional[Comparable] = None', - return_type='Union[List[Comparable], int, None]', - algorithm_type='Divide and conquer', + params="data: List[Comparable], target: Optional[Comparable] = None", + return_type="Union[List[Comparable], int, None]", + algorithm_type="Divide and conquer", initialization=self._generate_initialization_code(), loop_structure=self._generate_loop_structure(), loop_body=self._generate_loop_body(), update_logic=self._generate_update_logic(), final_processing=self._generate_final_processing(), - result='result' + result="result", ) - + def _generate_validation_code(self) -> str: """Generate validation code patterns""" patterns = [ @@ -306,7 +352,7 @@ def _generate_validation_code(self) -> str: raise TypeError("Input data must be iterable")""", ] return random.choice(patterns) - + def _generate_processing_code(self) -> str: """Generate main processing logic""" patterns = [ @@ -338,7 +384,7 @@ def _generate_processing_code(self) -> str: return result""", ] return random.choice(patterns) - + def _generate_error_handling_code(self) -> str: """Generate error handling patterns""" patterns = [ @@ -357,7 +403,7 @@ def _generate_error_handling_code(self) -> str: return self.get_default_result(input_data)""", ] return random.choice(patterns) - + def _generate_initialization_code(self) -> str: """Generate initialization code""" patterns = [ @@ -375,7 +421,7 @@ def _generate_initialization_code(self) -> str: self.version = '1.0.0'""", ] return random.choice(patterns) - + def _generate_loop_structure(self) -> str: """Generate loop structures""" patterns = [ @@ -390,7 +436,7 @@ def _generate_loop_structure(self) -> str: made_changes = False""", ] return random.choice(patterns) - + def _generate_loop_body(self) -> str: """Generate loop body content""" patterns = [ @@ -411,13 +457,13 @@ def _generate_loop_body(self) -> str: changes += 1""", ] return random.choice(patterns) - + def _generate_algorithm_details(self) -> str: """Generate algorithm documentation""" return """Uses divide-and-conquer strategy for optimal performance. Time complexity: O(n log n) in average case, O(n^2) worst case. Space complexity: O(log n) due to recursion stack.""" - + def _generate_update_logic(self) -> str: """Generate update logic for loops""" patterns = [ @@ -426,7 +472,7 @@ def _generate_update_logic(self) -> str: " # Check termination condition\n if self.should_terminate(): break", ] return random.choice(patterns) - + def _generate_final_processing(self) -> str: """Generate final processing steps""" patterns = [ @@ -435,7 +481,7 @@ def _generate_final_processing(self) -> str: " self.validate_result(result)\n return result", ] return random.choice(patterns) - + def _generate_result_code(self) -> str: """Generate result processing code""" patterns = [ @@ -444,48 +490,52 @@ def _generate_result_code(self) -> str: " result = self.post_process(processed_data)", ] return random.choice(patterns) - - def generate_large_corpus(self, num_files: int, avg_lines_per_file: int) -> List[Tuple[str, str]]: + + def generate_large_corpus( + self, num_files: int, avg_lines_per_file: int + ) -> List[Tuple[str, str]]: """Generate large corpus of realistic code""" print(f"Generating {num_files} files with avg {avg_lines_per_file} lines each...") - + corpus = [] - complexities = ['simple', 'medium', 'complex'] + complexities = ["simple", "medium", "complex"] weights = [0.3, 0.5, 0.2] # Distribution of complexity levels - + start_time = time.time() - + for i in range(num_files): # Choose complexity based on weights complexity = random.choices(complexities, weights=weights)[0] - + # Generate code code = self.generate_realistic_code(complexity) - + # Adjust line count by adding/removing content - current_lines = len(code.split('\n')) + current_lines = len(code.split("\n")) if current_lines < avg_lines_per_file: # Add more content - additional_content = self._generate_additional_code(avg_lines_per_file - current_lines) - code += '\n' + additional_content + additional_content = self._generate_additional_code( + avg_lines_per_file - current_lines + ) + code += "\n" + additional_content elif current_lines > avg_lines_per_file: # Truncate (not ideal but simple) - lines = code.split('\n')[:avg_lines_per_file] - code = '\n'.join(lines) - + lines = code.split("\n")[:avg_lines_per_file] + code = "\n".join(lines) + filename = f"generated_file_{i:04d}.py" corpus.append((filename, code)) - + # Progress indicator if (i + 1) % max(1, num_files // 20) == 0: progress = (i + 1) / num_files * 100 elapsed = time.time() - start_time eta = elapsed / (i + 1) * (num_files - i - 1) print(f" Progress: {progress:.1f}% ({i+1}/{num_files}), ETA: {eta:.1f}s") - + print(f"Generated {len(corpus)} files in {time.time() - start_time:.1f}s") return corpus - + def _generate_additional_code(self, needed_lines: int) -> str: """Generate additional code to meet line requirements""" additional_patterns = [ @@ -566,10 +616,10 @@ def get_stats(self) -> dict: } """, ] - + pattern = random.choice(additional_patterns) - lines = pattern.split('\n') - + lines = pattern.split("\n") + # Adjust to exact line count needed if len(lines) < needed_lines: # Add simple lines @@ -578,41 +628,43 @@ def get_stats(self) -> dict: lines.append(f"# Generated line {i+1}") elif len(lines) > needed_lines: lines = lines[:needed_lines] - - return '\n'.join(lines) + + return "\n".join(lines) + # ============================================================================ # LARGE SCALE TEST # ============================================================================ + def run_large_scale_corpus_test(corpus_sizes: List[int], avg_lines: List[int]): """Run compression tests on increasingly large corpora""" - - print("="*80) + + print("=" * 80) print("LARGE SCALE CORPUS COMPRESSION TEST") print("Testing LJPW Semantic Compiler on Massive Text Corpora") - print("="*80) - + print("=" * 80) + generator = CorpusGenerator() pipeline = LJPWPipeline() - + results = [] - + for size, lines in zip(corpus_sizes, avg_lines): print(f"\n{'='*60}") print(f"TESTING: {size} files, avg {lines} lines per file") print(f"{'='*60}") - + # Generate corpus print("\n1. GENERATING CORPUS") print("-" * 60) corpus = generator.generate_large_corpus(size, lines) - + # Calculate corpus statistics total_chars = sum(len(code) for _, code in corpus) - total_lines = sum(len(code.split('\n')) for _, code in corpus) + total_lines = sum(len(code.split("\n")) for _, code in corpus) total_size_mb = total_chars / (1024 * 1024) - + print(f"\nCorpus Statistics:") print(f" Files: {size:,}") print(f" Total lines: {total_lines:,}") @@ -620,34 +672,34 @@ def run_large_scale_corpus_test(corpus_sizes: List[int], avg_lines: List[int]): print(f" Total size: {total_size_mb:.2f} MB") print(f" Avg lines/file: {total_lines/size:.1f}") print(f" Avg size/file: {total_chars/size:,.0f} characters") - + # Run compression pipeline print(f"\n2. RUNNING LJPW COMPRESSION PIPELINE") print("-" * 60) - + start_time = time.time() - + try: pipeline_results = pipeline.analyze_codebase( corpus, generate_docs=False, # Skip for speed in large tests - generate_improvement_plan=False + generate_improvement_plan=False, ) - + processing_time = time.time() - start_time - + # Extract metrics - compression_ratio = pipeline_results['compression_ratio'] - compressed_size = pipeline_results['statistics']['compressed_size'] - semantic_units = pipeline_results['statistics']['semantic_units'] - health_score = pipeline_results['statistics']['health_score'] - avg_ljpw = pipeline_results['statistics']['average_ljpw'] - + compression_ratio = pipeline_results["compression_ratio"] + compressed_size = pipeline_results["statistics"]["compressed_size"] + semantic_units = pipeline_results["statistics"]["semantic_units"] + health_score = pipeline_results["statistics"]["health_score"] + avg_ljpw = pipeline_results["statistics"]["average_ljpw"] + # Calculate performance metrics throughput_files_per_sec = size / processing_time throughput_chars_per_sec = total_chars / processing_time throughput_lines_per_sec = total_lines / processing_time - + print(f"\n3. COMPRESSION RESULTS") print("-" * 60) print(f"Compression ratio: {compression_ratio:.1f}x") @@ -655,7 +707,7 @@ def run_large_scale_corpus_test(corpus_sizes: List[int], avg_lines: List[int]): print(f"Compressed size: {compressed_size:,} bytes ({compressed_size/1024:.1f} KB)") print(f"Semantic units: {semantic_units:,}") print(f"Space savings: {(1 - compressed_size/total_chars)*100:.1f}%") - + print(f"\n4. PERFORMANCE METRICS") print("-" * 60) print(f"Processing time: {processing_time:.2f} seconds") @@ -663,7 +715,7 @@ def run_large_scale_corpus_test(corpus_sizes: List[int], avg_lines: List[int]): print(f" Files/sec: {throughput_files_per_sec:.1f}") print(f" Lines/sec: {throughput_lines_per_sec:.0f}") print(f" Chars/sec: {throughput_chars_per_sec:,.0f}") - + print(f"\n5. QUALITY METRICS") print("-" * 60) print(f"Health score: {health_score:.1%}") @@ -673,106 +725,117 @@ def run_large_scale_corpus_test(corpus_sizes: List[int], avg_lines: List[int]): print(f" Justice (Structure): {J:.3f}") print(f" Power (Performance): {P:.3f}") print(f" Wisdom (Design): {W:.3f}") - + # Store results - results.append({ - 'files': size, - 'lines_per_file': lines, - 'total_lines': total_lines, - 'total_size_mb': total_size_mb, - 'compression_ratio': compression_ratio, - 'processing_time_sec': processing_time, - 'throughput_files_per_sec': throughput_files_per_sec, - 'health_score': health_score, - 'space_savings_percent': (1 - compressed_size/total_chars)*100 - }) - + results.append( + { + "files": size, + "lines_per_file": lines, + "total_lines": total_lines, + "total_size_mb": total_size_mb, + "compression_ratio": compression_ratio, + "processing_time_sec": processing_time, + "throughput_files_per_sec": throughput_files_per_sec, + "health_score": health_score, + "space_savings_percent": (1 - compressed_size / total_chars) * 100, + } + ) + print(f"\n[SUCCESS] TEST COMPLETED SUCCESSFULLY") - + except Exception as e: print(f"\n[FAILED] TEST FAILED: {e}") import traceback + traceback.print_exc() - results.append({ - 'files': size, - 'error': str(e) - }) - + results.append({"files": size, "error": str(e)}) + # Summary print(f"\n{'='*80}") print("LARGE SCALE TEST SUMMARY") - print("="*80) - - print(f"\n{'Files':<8} {'Lines':<8} {'Size(MB)':<10} {'Comp.Ratio':<12} {'Time(s)':<10} {'SpaceSave%':<12}") + print("=" * 80) + + print( + f"\n{'Files':<8} {'Lines':<8} {'Size(MB)':<10} {'Comp.Ratio':<12} {'Time(s)':<10} {'SpaceSave%':<12}" + ) print("-" * 70) - + for result in results: - if 'error' not in result: - print(f"{result['files']:<8} {result['lines_per_file']:<8} {result['total_size_mb']:<10.1f} " - f"{result['compression_ratio']:<12.1f} {result['processing_time_sec']:<10.2f} " - f"{result['space_savings_percent']:<12.1f}") + if "error" not in result: + print( + f"{result['files']:<8} {result['lines_per_file']:<8} {result['total_size_mb']:<10.1f} " + f"{result['compression_ratio']:<12.1f} {result['processing_time_sec']:<10.2f} " + f"{result['space_savings_percent']:<12.1f}" + ) else: print(f"{result['files']:<8} {'ERROR':<28} {result['error']}") - + # Find optimal compression point - successful_results = [r for r in results if 'error' not in r] + successful_results = [r for r in results if "error" not in r] if successful_results: - best_compression = max(successful_results, key=lambda x: x['compression_ratio']) - fastest_throughput = max(successful_results, key=lambda x: x['throughput_files_per_sec']) - + best_compression = max(successful_results, key=lambda x: x["compression_ratio"]) + fastest_throughput = max(successful_results, key=lambda x: x["throughput_files_per_sec"]) + print(f"\n[TROPHY] OPTIMAL PERFORMERS:") - print(f"Best Compression: {best_compression['files']} files " - f"({best_compression['compression_ratio']:.1f}x ratio)") - print(f"Fastest Throughput: {fastest_throughput['files']} files " - f"({fastest_throughput['throughput_files_per_sec']:.1f} files/sec)") - + print( + f"Best Compression: {best_compression['files']} files " + f"({best_compression['compression_ratio']:.1f}x ratio)" + ) + print( + f"Fastest Throughput: {fastest_throughput['files']} files " + f"({fastest_throughput['throughput_files_per_sec']:.1f} files/sec)" + ) + return results + # ============================================================================ # STRESS TEST # ============================================================================ + def run_stress_test(): """Push the system to its limits""" - print("\n" + "="*80) + print("\n" + "=" * 80) print("STRESS TEST - PUSHING LIMITS") - print("="*80) - + print("=" * 80) + # Extreme parameters stress_tests = [ - (1000, 50), # Many small files - (100, 500), # Few large files - (500, 200), # Medium-large - (2000, 25), # Many tiny files + (1000, 50), # Many small files + (100, 500), # Few large files + (500, 200), # Medium-large + (2000, 25), # Many tiny files ] - + return run_large_scale_corpus_test( corpus_sizes=[test[0] for test in stress_tests], - avg_lines=[test[1] for test in stress_tests] + avg_lines=[test[1] for test in stress_tests], ) + # ============================================================================ # MAIN EXECUTION # ============================================================================ -if __name__ == '__main__': +if __name__ == "__main__": print("LJPW LARGE CORPUS INGESTION TEST") print("Testing Semantic Compression on Massive Scale") - + # Run progressively larger tests test_sizes = [50, 100, 200, 500] avg_lines = [100, 100, 100, 100] - + results = run_large_scale_corpus_test(test_sizes, avg_lines) - + # Run stress test if basic tests pass - if any('error' not in r for r in results): + if any("error" not in r for r in results): print("\n[ROCKET] RUNNING STRESS TEST") stress_results = run_stress_test() results.extend(stress_results) - - print("\n" + "="*80) + + print("\n" + "=" * 80) print("ALL LARGE SCALE TESTS COMPLETED") - print("="*80) + print("=" * 80) print("LJPW Semantic Compiler successfully processed massive corpora!") - print(f"Total test runs: {len([r for r in results if 'error' not in r])} successful") \ No newline at end of file + print(f"Total test runs: {len([r for r in results if 'error' not in r])} successful") diff --git a/examples/simple_v2.py b/examples/simple_v2.py index bb55fda..90e369c 100644 --- a/examples/simple_v2.py +++ b/examples/simple_v2.py @@ -1,5 +1,6 @@ from typing import List, Union + def calculate_total(items: List[Union[int, float]]) -> float: """ Calculate the sum of numeric items. diff --git a/examples/token_analysis.py b/examples/token_analysis.py index 28ff570..e754ca6 100644 --- a/examples/token_analysis.py +++ b/examples/token_analysis.py @@ -10,189 +10,201 @@ # TOKEN ANALYSIS FROM LARGE CORPUS TEST RESULTS # ============================================================================ + def analyze_token_usage(): """Analyze token usage from our large corpus test""" - - print("="*80) + + print("=" * 80) print("TOKEN USAGE COMPARISON ANALYSIS") print("LJPW Compression vs Traditional AI Input") - print("="*80) - + print("=" * 80) + # Test data from our large corpus results test_cases = [ { - 'name': 'Small Corpus', - 'files': 50, - 'lines_per_file': 100, - 'total_chars': 129860, - 'total_bytes': 129860, - 'compressed_units': 50, - 'compression_ratio': 1298.6 + "name": "Small Corpus", + "files": 50, + "lines_per_file": 100, + "total_chars": 129860, + "total_bytes": 129860, + "compressed_units": 50, + "compression_ratio": 1298.6, }, { - 'name': 'Medium Corpus', - 'files': 500, - 'lines_per_file': 100, - 'total_chars': 1292058, - 'total_bytes': 1292058, - 'compressed_units': 500, - 'compression_ratio': 1292.1 + "name": "Medium Corpus", + "files": 500, + "lines_per_file": 100, + "total_chars": 1292058, + "total_bytes": 1292058, + "compressed_units": 500, + "compression_ratio": 1292.1, }, { - 'name': 'Large Corpus (Best Case)', - 'files': 100, - 'lines_per_file': 500, - 'total_chars': 1090901, - 'total_bytes': 1090901, - 'compressed_units': 100, - 'compression_ratio': 5454.5 + "name": "Large Corpus (Best Case)", + "files": 100, + "lines_per_file": 500, + "total_chars": 1090901, + "total_bytes": 1090901, + "compressed_units": 100, + "compression_ratio": 5454.5, }, { - 'name': 'Massive Corpus', - 'files': 2000, - 'lines_per_file': 25, - 'total_chars': 1490530, - 'total_bytes': 1490530, - 'compressed_units': 2000, - 'compression_ratio': 372.6 - } + "name": "Massive Corpus", + "files": 2000, + "lines_per_file": 25, + "total_chars": 1490530, + "total_bytes": 1490530, + "compressed_units": 2000, + "compression_ratio": 372.6, + }, ] - - print("\n" + "#"*60) + + print("\n" + "#" * 60) print("DETAILED TOKEN ANALYSIS PER TEST CASE") - print("#"*60) - + print("#" * 60) + for case in test_cases: print(f"\n{'='*60}") print(f"TEST CASE: {case['name']}") - print("="*60) - + print("=" * 60) + # Original token estimation # Rule of thumb: ~4 characters = 1 token for code - original_tokens = case['total_chars'] / 4 - + original_tokens = case["total_chars"] / 4 + # More conservative estimation (including overhead) - conservative_tokens = case['total_chars'] / 3 - + conservative_tokens = case["total_chars"] / 3 + # LJPW compressed tokens # Each compressed unit = 2 bytes = ~0.5 tokens - ljpw_tokens = case['compressed_units'] * 0.5 - + ljpw_tokens = case["compressed_units"] * 0.5 + # Claude's context limit claude_limit = 200000 - + print(f"\n[DATA] CORPUS STATISTICS:") print(f" Files: {case['files']:,}") print(f" Total characters: {case['total_chars']:,}") print(f" Total size: {case['total_bytes']/1024/1024:.2f} MB") - + print(f"\n[AI] TRADITIONAL AI INPUT:") print(f" Estimated tokens (4:1 ratio): {original_tokens:,.0f}") print(f" Conservative estimate (3:1): {conservative_tokens:,.0f}") print(f" Context windows needed: {original_tokens/claude_limit:.1f}") print(f" Percent of Claude's limit: {100*original_tokens/claude_limit:.1f}%") - + print(f"\n[LJPW] LJPW COMPRESSED INPUT:") print(f" Compressed units: {case['compressed_units']:,}") print(f" Bytes used: {case['compressed_units'] * 2:,} bytes") print(f" Estimated tokens: {ljpw_tokens:,.1f}") print(f" Context windows needed: {ljpw_tokens/claude_limit:.4f}") print(f" Percent of Claude's limit: {100*ljpw_tokens/claude_limit:.6f}%") - + # Calculate savings token_savings = original_tokens - ljpw_tokens percent_savings = 100 * token_savings / original_tokens context_expansion = original_tokens / ljpw_tokens - + print(f"\n[MONEY] TOKEN SAVINGS:") print(f" Tokens saved: {token_savings:,.0f}") print(f" Percent savings: {percent_savings:.2f}%") print(f" Context expansion factor: {context_expansion:.0f}x") print(f" Effective context window: {claude_limit * context_expansion:,.0f} tokens") - + # Calculate what this means in practice typical_file_size = 100 # lines per file - tokens_per_file_original = (case['total_chars'] / case['files']) / 4 - + tokens_per_file_original = (case["total_chars"] / case["files"]) / 4 + print(f"\n[SCALE] PRACTICAL IMPLICATIONS:") - print(f" Files per context window (original): {claude_limit / tokens_per_file_original:.0f}") - print(f" Files per context window (LJPW): {claude_limit / (ljpw_tokens/case['files']):,.0f}") - print(f" Analysis capacity increase: {(claude_limit / (ljpw_tokens/case['files'])) / (claude_limit / tokens_per_file_original):.0f}x") - + print( + f" Files per context window (original): {claude_limit / tokens_per_file_original:.0f}" + ) + print( + f" Files per context window (LJPW): {claude_limit / (ljpw_tokens/case['files']):,.0f}" + ) + print( + f" Analysis capacity increase: {(claude_limit / (ljpw_tokens/case['files'])) / (claude_limit / tokens_per_file_original):.0f}x" + ) + # Summary table print(f"\n{'#'*60}") print("SUMMARY: TOKEN COMPARISON TABLE") - print("#"*60) - + print("#" * 60) + print(f"\n{'Test Case':<20} {'Original':<12} {'LJPW':<8} {'Savings':<12} {'Expansion':<12}") print("-" * 70) - + for case in test_cases: - original_tokens = case['total_chars'] / 4 - ljpw_tokens = case['compressed_units'] * 0.5 + original_tokens = case["total_chars"] / 4 + ljpw_tokens = case["compressed_units"] * 0.5 savings = original_tokens - ljpw_tokens expansion = original_tokens / ljpw_tokens - - print(f"{case['name']:<20} {original_tokens:<12,.0f} {ljpw_tokens:<8.1f} {savings:<12,.0f} {expansion:<12.0f}x") - + + print( + f"{case['name']:<20} {original_tokens:<12,.0f} {ljpw_tokens:<8.1f} {savings:<12,.0f} {expansion:<12.0f}x" + ) + # Overall statistics - total_original_chars = sum(case['total_chars'] for case in test_cases) - total_compressed_units = sum(case['compressed_units'] for case in test_cases) + total_original_chars = sum(case["total_chars"] for case in test_cases) + total_compressed_units = sum(case["compressed_units"] for case in test_cases) total_original_tokens = total_original_chars / 4 total_ljpw_tokens = total_compressed_units * 0.5 total_savings = total_original_tokens - total_ljpw_tokens avg_expansion = total_original_tokens / total_ljpw_tokens - - print(f"\n{'OVERALL':<20} {total_original_tokens:<12,.0f} {total_ljpw_tokens:<8.1f} {total_savings:<12,.0f} {avg_expansion:<12.0f}x") - + + print( + f"\n{'OVERALL':<20} {total_original_tokens:<12,.0f} {total_ljpw_tokens:<8.1f} {total_savings:<12,.0f} {avg_expansion:<12.0f}x" + ) + print(f"\n[INSIGHTS] KEY INSIGHTS:") print(f" Total tokens processed: {total_original_tokens:,.0f}") print(f" Total LJPW tokens: {total_ljpw_tokens:,.1f}") print(f" Overall savings: {100*total_savings/total_original_tokens:.2f}%") print(f" Average expansion: {avg_expansion:.0f}x") - + # Calculate what this enables print(f"\n[ENABLED] WHAT THIS ENABLES:") print(f" Standard AI context: 200,000 tokens") print(f" LJPW effective context: {200000 * avg_expansion:,.0f} tokens") print(f" Files analyzable simultaneously: {(200000 * avg_expansion) / 1000:,.0f} files") print(f" Code lines analyzable: {(200000 * avg_expansion) * 25 / 4:,.0f} lines") - + # Compare to other models print(f"\n[COMPARE] COMPARISON TO OTHER MODELS:") models = [ - ('GPT-4', 128000), - ('Claude 3', 200000), - ('Gemini Pro', 32000), - ('GPT-3.5', 16000), + ("GPT-4", 128000), + ("Claude 3", 200000), + ("Gemini Pro", 32000), + ("GPT-3.5", 16000), ] - + print(f"{'Model':<15} {'Standard':<12} {'LJPW Enhanced':<15} {'Expansion':<12}") print("-" * 60) - + for model, limit in models: enhanced = limit * avg_expansion print(f"{model:<15} {limit:<12,} {enhanced:<15,.0f} {avg_expansion:<12.0f}x") - + return test_cases + def calculate_my_token_usage(): """Calculate how many tokens I've used in this conversation""" - + print(f"\n{'='*80}") print("MY TOKEN USAGE IN THIS CONVERSATION") - print("="*80) - + print("=" * 80) + # Rough estimates of my outputs outputs = [ ("Code absorption and understanding", "~1500 tokens"), - ("Framework explanation", "~1200 tokens"), + ("Framework explanation", "~1200 tokens"), ("Multiple test runs", "~800 tokens"), ("Large corpus creation and execution", "~600 tokens"), ("Results analysis and insights", "~1000 tokens"), ("Current token comparison analysis", "~800 tokens"), ] - + total_estimated = 0 for desc, estimate in outputs: print(f" {desc:<40} {estimate:>15}") @@ -200,30 +212,31 @@ def calculate_my_token_usage(): if "~" in estimate: num = float(estimate.replace("~", "").replace(" tokens", "")) total_estimated += num - + print(f"\n {'TOTAL MY OUTPUTS':<40} {'~' + str(total_estimated) + ' tokens':>15}") - + # Compare to what would be needed without LJPW total_corpus_chars = 129860 + 1292058 + 1090901 + 1490530 # Sum of test cases tokens_without_ljpw = total_corpus_chars / 4 - + print(f"\n💡 COMPARISON:") print(f" Total corpus analyzed: {total_corpus_chars:,} characters") print(f" Tokens without LJPW: {tokens_without_ljpw:,.0f}") print(f" My total output: ~{total_estimated:,.0f} tokens") print(f" Efficiency gain: {tokens_without_ljpw/total_estimated:.0f}x") - + print(f"\n🎯 CONCLUSION:") print(f" I analyzed {total_corpus_chars/1024/1024:.1f} MB of code") print(f" Using only {total_estimated:,.0f} tokens for explanation") print(f" This demonstrates the power of LJPW compression!") -if __name__ == '__main__': + +if __name__ == "__main__": # Run the analysis analyze_token_usage() calculate_my_token_usage() - + print(f"\n{'='*80}") print("TOKEN ANALYSIS COMPLETE") print("LJPW enables unprecedented AI reasoning scale!") - print("="*80) \ No newline at end of file + print("=" * 80) diff --git a/examples/token_introspection_test.py b/examples/token_introspection_test.py index 2529039..786df42 100644 --- a/examples/token_introspection_test.py +++ b/examples/token_introspection_test.py @@ -18,9 +18,7 @@ or incomplete implementation. Fifth, the function builds up a list using repeated append calls, which could be inefficient for large datasets. """, - "ljpw": "L=0.45,J=1.5,P=0.6,W=0.3|H=41%|Δ=1.17|[L-CRIT][W-HIGH]", - "semantic_genome": "LJPW{1unit:TypeDef(2,3,1,2)|SafeInit(3,2,1,2)}", } @@ -29,7 +27,7 @@ char_count = len(content) # Rough token estimate: ~4 chars per token for English estimated_tokens = char_count / 4 - compression_vs_verbose = len(test_cases['verbose']) / char_count if mode != 'verbose' else 1.0 + compression_vs_verbose = len(test_cases["verbose"]) / char_count if mode != "verbose" else 1.0 print(f"\n{'='*60}") print(f"MODE: {mode.upper()}") @@ -67,7 +65,9 @@ print(f" Tokens needed: {ljpw_tokens:.0f}") print(f" Claude context windows: {ljpw_tokens/200000:.4f}") print(f" Would fit? YES") -print(f" Token savings: {traditional_tokens - ljpw_tokens:,.0f} ({100*(traditional_tokens-ljpw_tokens)/traditional_tokens:.2f}%)") +print( + f" Token savings: {traditional_tokens - ljpw_tokens:,.0f} ({100*(traditional_tokens-ljpw_tokens)/traditional_tokens:.2f}%)" +) # What this enables print(f"\n{'='*60}") diff --git a/pyproject.toml b/pyproject.toml index 8789b39..4c49cca 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,7 +7,7 @@ name = "semantic-compressor" version = "2.0.0" description = "Compress code by meaning, not syntax - semantic code analysis using LJPW framework" readme = "README.md" -requires-python = ">=3.7" +requires-python = ">=3.8" license = {text = "MIT"} authors = [ {name = "Semantic Compressor Team"} @@ -18,12 +18,12 @@ classifiers = [ "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.13", "Topic :: Software Development :: Quality Assurance", "Topic :: Software Development :: Libraries :: Python Modules", "Operating System :: OS Independent", @@ -58,7 +58,7 @@ Issues = "https://github.com/BruinGrowly/Semantic-Compressor/issues" [tool.black] line-length = 100 -target-version = ['py37', 'py38', 'py39', 'py310', 'py311', 'py312'] +target-version = ['py38', 'py39', 'py310', 'py311', 'py312', 'py313'] include = '\.pyi?$' extend-exclude = ''' /( @@ -92,7 +92,7 @@ python_classes = ["Test*"] python_functions = ["test_*"] [tool.mypy] -python_version = "3.7" +python_version = "3.8" warn_return_any = true warn_unused_configs = true disallow_untyped_defs = false diff --git a/src/ljpw/__init__.py b/src/ljpw/__init__.py index 38c8137..c69afdf 100644 --- a/src/ljpw/__init__.py +++ b/src/ljpw/__init__.py @@ -39,8 +39,9 @@ # Import main analyzer from standalone module try: - from .ljpw_standalone import analyze_quick, calculate_distance, SimpleCodeAnalyzer - __all__ = ['analyze_quick', 'calculate_distance', 'SimpleCodeAnalyzer'] + from .ljpw_standalone import SimpleCodeAnalyzer, analyze_quick, calculate_distance + + __all__ = ["analyze_quick", "calculate_distance", "SimpleCodeAnalyzer"] except ImportError: # Fallback if modules aren't accessible __all__ = [] diff --git a/src/ljpw/ljpw_ast_analyzer.py b/src/ljpw/ljpw_ast_analyzer.py index 6e5429f..6bf1cd7 100644 --- a/src/ljpw/ljpw_ast_analyzer.py +++ b/src/ljpw/ljpw_ast_analyzer.py @@ -10,8 +10,8 @@ """ import ast -from typing import Dict, Any, List, Tuple from pathlib import Path +from typing import Any, Dict, List, Tuple class ASTPatternCounter: @@ -20,32 +20,29 @@ class ASTPatternCounter: def __init__(self): self.patterns = { # L (Love/Safety) patterns - 'try_except': 0, - 'assert_statements': 0, - 'type_checks': 0, - 'validation_checks': 0, - 'raise_exceptions': 0, - + "try_except": 0, + "assert_statements": 0, + "type_checks": 0, + "validation_checks": 0, + "raise_exceptions": 0, # J (Justice/Structure) patterns - 'class_definitions': 0, - 'function_definitions': 0, - 'type_annotations': 0, - 'docstrings': 0, - 'imports': 0, - + "class_definitions": 0, + "function_definitions": 0, + "type_annotations": 0, + "docstrings": 0, + "imports": 0, # P (Power/Execution) patterns - 'list_comprehensions': 0, - 'generator_expressions': 0, - 'lambda_functions': 0, - 'async_functions': 0, - 'decorators': 0, - + "list_comprehensions": 0, + "generator_expressions": 0, + "lambda_functions": 0, + "async_functions": 0, + "decorators": 0, # W (Wisdom/Design) patterns - 'abstract_methods': 0, - 'property_decorators': 0, - 'context_managers': 0, - 'metaclasses': 0, - 'design_patterns': 0, + "abstract_methods": 0, + "property_decorators": 0, + "context_managers": 0, + "metaclasses": 0, + "design_patterns": 0, } self.total_nodes = 0 @@ -57,60 +54,66 @@ def visit(self, node: ast.AST): # L (Safety) patterns if isinstance(node, ast.Try): - self.patterns['try_except'] += 1 + self.patterns["try_except"] += 1 elif isinstance(node, ast.Assert): - self.patterns['assert_statements'] += 1 + self.patterns["assert_statements"] += 1 elif isinstance(node, ast.Raise): - self.patterns['raise_exceptions'] += 1 + self.patterns["raise_exceptions"] += 1 elif isinstance(node, ast.Call): if isinstance(node.func, ast.Name): - if node.func.id in ('isinstance', 'issubclass', 'hasattr'): - self.patterns['type_checks'] += 1 - elif node.func.id in ('validate', 'check', 'verify'): - self.patterns['validation_checks'] += 1 + if node.func.id in ("isinstance", "issubclass", "hasattr"): + self.patterns["type_checks"] += 1 + elif node.func.id in ("validate", "check", "verify"): + self.patterns["validation_checks"] += 1 # J (Structure) patterns elif isinstance(node, ast.ClassDef): - self.patterns['class_definitions'] += 1 + self.patterns["class_definitions"] += 1 # Check for docstring - if (node.body and isinstance(node.body[0], ast.Expr) and - isinstance(node.body[0].value, ast.Constant) and - isinstance(node.body[0].value.value, str)): - self.patterns['docstrings'] += 1 + if ( + node.body + and isinstance(node.body[0], ast.Expr) + and isinstance(node.body[0].value, ast.Constant) + and isinstance(node.body[0].value.value, str) + ): + self.patterns["docstrings"] += 1 elif isinstance(node, ast.FunctionDef): - self.patterns['function_definitions'] += 1 + self.patterns["function_definitions"] += 1 # Check for docstring - if (node.body and isinstance(node.body[0], ast.Expr) and - isinstance(node.body[0].value, ast.Constant) and - isinstance(node.body[0].value.value, str)): - self.patterns['docstrings'] += 1 + if ( + node.body + and isinstance(node.body[0], ast.Expr) + and isinstance(node.body[0].value, ast.Constant) + and isinstance(node.body[0].value.value, str) + ): + self.patterns["docstrings"] += 1 # Check for type annotations if node.returns or any(arg.annotation for arg in node.args.args): - self.patterns['type_annotations'] += 1 + self.patterns["type_annotations"] += 1 # Check for async if isinstance(node, ast.AsyncFunctionDef): - self.patterns['async_functions'] += 1 + self.patterns["async_functions"] += 1 # Check for decorators if node.decorator_list: - self.patterns['decorators'] += len(node.decorator_list) + self.patterns["decorators"] += len(node.decorator_list) # Check for property for dec in node.decorator_list: - if isinstance(dec, ast.Name) and dec.id == 'property': - self.patterns['property_decorators'] += 1 + if isinstance(dec, ast.Name) and dec.id == "property": + self.patterns["property_decorators"] += 1 elif isinstance(node, ast.Import) or isinstance(node, ast.ImportFrom): - self.patterns['imports'] += 1 + self.patterns["imports"] += 1 # P (Power/Execution) patterns elif isinstance(node, ast.ListComp): - self.patterns['list_comprehensions'] += 1 + self.patterns["list_comprehensions"] += 1 elif isinstance(node, ast.GeneratorExp): - self.patterns['generator_expressions'] += 1 + self.patterns["generator_expressions"] += 1 elif isinstance(node, ast.Lambda): - self.patterns['lambda_functions'] += 1 + self.patterns["lambda_functions"] += 1 # W (Wisdom/Design) patterns elif isinstance(node, ast.With): - self.patterns['context_managers'] += 1 + self.patterns["context_managers"] += 1 # Recursively visit children for child in ast.iter_child_nodes(node): @@ -123,16 +126,16 @@ class LJPWASTAnalyzer: def __init__(self): # Baseline weights for each dimension (tuned via experimentation) self.baseline_weights = { - 'L': 0.618, # φ⁻¹ - 'J': 0.414, # √2-1 - 'P': 0.718, # e-2 - 'W': 0.693, # ln(2) + "L": 0.618, # φ⁻¹ + "J": 0.414, # √2-1 + "P": 0.718, # e-2 + "W": 0.693, # ln(2) } def analyze_file(self, file_path: str) -> Dict[str, Any]: """Analyze a Python file and return LJPW coordinates""" try: - with open(file_path, 'r', encoding='utf-8') as f: + with open(file_path, "r", encoding="utf-8") as f: code = f.read() return self.analyze_code(code) @@ -140,11 +143,11 @@ def analyze_file(self, file_path: str) -> Dict[str, Any]: except Exception as e: # Return baseline on error return { - 'L': self.baseline_weights['L'], - 'J': self.baseline_weights['J'], - 'P': self.baseline_weights['P'], - 'W': self.baseline_weights['W'], - 'error': str(e), + "L": self.baseline_weights["L"], + "J": self.baseline_weights["J"], + "P": self.baseline_weights["P"], + "W": self.baseline_weights["W"], + "error": str(e), } def analyze_code(self, code: str) -> Dict[str, Any]: @@ -153,7 +156,7 @@ def analyze_code(self, code: str) -> Dict[str, Any]: tree = ast.parse(code) counter = ASTPatternCounter() counter.visit(tree) - counter.total_lines = len(code.split('\n')) + counter.total_lines = len(code.split("\n")) # Calculate LJPW coordinates from pattern counts ljpw = self._calculate_ljpw(counter) @@ -163,11 +166,11 @@ def analyze_code(self, code: str) -> Dict[str, Any]: except SyntaxError as e: # Invalid Python code - return baseline return { - 'L': self.baseline_weights['L'], - 'J': self.baseline_weights['J'], - 'P': self.baseline_weights['P'], - 'W': self.baseline_weights['W'], - 'error': f'SyntaxError: {e}', + "L": self.baseline_weights["L"], + "J": self.baseline_weights["J"], + "P": self.baseline_weights["P"], + "W": self.baseline_weights["W"], + "error": f"SyntaxError: {e}", } def _calculate_ljpw(self, counter: ASTPatternCounter) -> Dict[str, float]: @@ -178,42 +181,42 @@ def _calculate_ljpw(self, counter: ASTPatternCounter) -> Dict[str, float]: # L (Love/Safety) - error handling and validation l_patterns = ( - counter.patterns['try_except'] * 3.0 + - counter.patterns['assert_statements'] * 2.0 + - counter.patterns['type_checks'] * 1.5 + - counter.patterns['validation_checks'] * 2.0 + - counter.patterns['raise_exceptions'] * 1.0 + counter.patterns["try_except"] * 3.0 + + counter.patterns["assert_statements"] * 2.0 + + counter.patterns["type_checks"] * 1.5 + + counter.patterns["validation_checks"] * 2.0 + + counter.patterns["raise_exceptions"] * 1.0 ) - L = self.baseline_weights['L'] + (l_patterns / total) * 0.4 + L = self.baseline_weights["L"] + (l_patterns / total) * 0.4 # J (Justice/Structure) - organization and documentation j_patterns = ( - counter.patterns['class_definitions'] * 3.0 + - counter.patterns['function_definitions'] * 2.0 + - counter.patterns['type_annotations'] * 2.5 + - counter.patterns['docstrings'] * 2.0 + - counter.patterns['imports'] * 0.5 + counter.patterns["class_definitions"] * 3.0 + + counter.patterns["function_definitions"] * 2.0 + + counter.patterns["type_annotations"] * 2.5 + + counter.patterns["docstrings"] * 2.0 + + counter.patterns["imports"] * 0.5 ) - J = self.baseline_weights['J'] + (j_patterns / total) * 0.6 + J = self.baseline_weights["J"] + (j_patterns / total) * 0.6 # P (Power/Execution) - computational efficiency p_patterns = ( - counter.patterns['list_comprehensions'] * 2.0 + - counter.patterns['generator_expressions'] * 2.5 + - counter.patterns['lambda_functions'] * 1.5 + - counter.patterns['async_functions'] * 3.0 + - counter.patterns['decorators'] * 1.0 + counter.patterns["list_comprehensions"] * 2.0 + + counter.patterns["generator_expressions"] * 2.5 + + counter.patterns["lambda_functions"] * 1.5 + + counter.patterns["async_functions"] * 3.0 + + counter.patterns["decorators"] * 1.0 ) - P = self.baseline_weights['P'] + (p_patterns / total) * 0.3 + P = self.baseline_weights["P"] + (p_patterns / total) * 0.3 # W (Wisdom/Design) - architectural patterns w_patterns = ( - counter.patterns['abstract_methods'] * 3.0 + - counter.patterns['property_decorators'] * 2.0 + - counter.patterns['context_managers'] * 2.5 + - counter.patterns['metaclasses'] * 3.0 + counter.patterns["abstract_methods"] * 3.0 + + counter.patterns["property_decorators"] * 2.0 + + counter.patterns["context_managers"] * 2.5 + + counter.patterns["metaclasses"] * 3.0 ) - W = self.baseline_weights['W'] + (w_patterns / total) * 0.4 + W = self.baseline_weights["W"] + (w_patterns / total) * 0.4 # Clamp to [0, 1.5] range (LJPW_MAX_VALUE) L = min(max(L, 0.0), 1.5) @@ -222,10 +225,10 @@ def _calculate_ljpw(self, counter: ASTPatternCounter) -> Dict[str, float]: W = min(max(W, 0.0), 1.5) return { - 'L': L, - 'J': J, - 'P': P, - 'W': W, + "L": L, + "J": J, + "P": P, + "W": W, } def analyze_directory(self, path: str) -> List[Dict[str, Any]]: @@ -234,13 +237,13 @@ def analyze_directory(self, path: str) -> List[Dict[str, Any]]: for py_file in Path(path).rglob("*.py"): result = self.analyze_file(str(py_file)) - result['file'] = str(py_file) + result["file"] = str(py_file) results.append(result) return results -if __name__ == '__main__': +if __name__ == "__main__": # Quick test analyzer = LJPWASTAnalyzer() @@ -257,4 +260,6 @@ def calculate_sum(a: int, b: int) -> int: """ result = analyzer.analyze_code(test_code) - print(f"LJPW: L={result['L']:.3f}, J={result['J']:.3f}, P={result['P']:.3f}, W={result['W']:.3f}") + print( + f"LJPW: L={result['L']:.3f}, J={result['J']:.3f}, P={result['P']:.3f}, W={result['W']:.3f}" + ) diff --git a/src/ljpw/ljpw_baselines_v4.py b/src/ljpw/ljpw_baselines_v4.py index d4d577e..4fe8bea 100644 --- a/src/ljpw/ljpw_baselines_v4.py +++ b/src/ljpw/ljpw_baselines_v4.py @@ -12,11 +12,13 @@ import math from dataclasses import dataclass -from typing import Dict, Tuple, List, Optional +from typing import Dict, List, Optional, Tuple + import numpy as np try: import matplotlib.pyplot as plt + MATPLOTLIB_AVAILABLE = True except ImportError: MATPLOTLIB_AVAILABLE = False @@ -70,9 +72,7 @@ class LJPWBaselines: } @staticmethod - def effective_dimensions( - L: float, J: float, P: float, W: float - ) -> Dict[str, float]: + def effective_dimensions(L: float, J: float, P: float, W: float) -> Dict[str, float]: """ Calculate coupling-adjusted effective dimensions. @@ -153,9 +153,7 @@ def harmony_index(L: float, J: float, P: float, W: float) -> float: Returns: Harmony index (0.0 to 1.0, asymptotic to 1.0) """ - d_anchor = math.sqrt( - (1 - L) ** 2 + (1 - J) ** 2 + (1 - P) ** 2 + (1 - W) ** 2 - ) + d_anchor = math.sqrt((1 - L) ** 2 + (1 - J) ** 2 + (1 - P) ** 2 + (1 - W) ** 2) return 1.0 / (1.0 + d_anchor) @staticmethod @@ -178,9 +176,7 @@ def composite_score(L: float, J: float, P: float, W: float) -> float: robustness = baselines.harmonic_mean(L, J, P, W) harmony = baselines.harmony_index(L, J, P, W) - return ( - 0.35 * growth + 0.25 * effectiveness + 0.25 * robustness + 0.15 * harmony - ) + return 0.35 * growth + 0.25 * effectiveness + 0.25 * robustness + 0.15 * harmony @staticmethod def distance_from_anchor(L: float, J: float, P: float, W: float) -> float: @@ -193,14 +189,10 @@ def distance_from_anchor(L: float, J: float, P: float, W: float) -> float: Returns: Distance (0.0 to ~2.0) """ - return math.sqrt( - (1 - L) ** 2 + (1 - J) ** 2 + (1 - P) ** 2 + (1 - W) ** 2 - ) + return math.sqrt((1 - L) ** 2 + (1 - J) ** 2 + (1 - P) ** 2 + (1 - W) ** 2) @staticmethod - def distance_from_natural_equilibrium( - L: float, J: float, P: float, W: float - ) -> float: + def distance_from_natural_equilibrium(L: float, J: float, P: float, W: float) -> float: """ Euclidean distance from Natural Equilibrium. @@ -211,12 +203,7 @@ def distance_from_natural_equilibrium( Distance (0.0 to ~2.0) """ NE = ReferencePoints.NATURAL_EQUILIBRIUM - return math.sqrt( - (NE[0] - L) ** 2 - + (NE[1] - J) ** 2 - + (NE[2] - P) ** 2 - + (NE[3] - W) ** 2 - ) + return math.sqrt((NE[0] - L) ** 2 + (NE[1] - J) ** 2 + (NE[2] - P) ** 2 + (NE[3] - W) ** 2) @staticmethod def full_diagnostic(L: float, J: float, P: float, W: float) -> Dict: @@ -239,9 +226,7 @@ def full_diagnostic(L: float, J: float, P: float, W: float) -> Dict: "effective_dimensions": eff, "distances": { "from_anchor": baselines.distance_from_anchor(L, J, P, W), - "from_natural_equilibrium": baselines.distance_from_natural_equilibrium( - L, J, P, W - ), + "from_natural_equilibrium": baselines.distance_from_natural_equilibrium(L, J, P, W), }, "metrics": { "harmonic_mean": baselines.harmonic_mean(L, J, P, W), @@ -412,9 +397,7 @@ def _derivatives(self, state: np.ndarray) -> np.ndarray: dP_dt = p["alpha_PL"] * L + p["alpha_PJ"] * J - p["beta_P"] * P # Wisdom equation (linear) - dW_dt = ( - p["alpha_WL"] * L + p["alpha_WJ"] * J + p["alpha_WP"] * P - p["beta_W"] * W - ) + dW_dt = p["alpha_WL"] * L + p["alpha_WJ"] * J + p["alpha_WP"] * P - p["beta_W"] * W return np.array([dL_dt, dJ_dt, dP_dt, dW_dt]) @@ -490,17 +473,17 @@ def plot_simulation(self, history: Dict[str, List[float]], title: Optional[str] print("Plotting requires matplotlib. Install with: pip install matplotlib") return - plt.style.use("seaborn-v0_8-whitegrid" if "seaborn-v0_8-whitegrid" in plt.style.available else "default") + plt.style.use( + "seaborn-v0_8-whitegrid" + if "seaborn-v0_8-whitegrid" in plt.style.available + else "default" + ) fig, ax = plt.subplots(figsize=(12, 7)) # Plot dimensions ax.plot(history["t"], history["L"], label="Love (L)", color="crimson", lw=2) - ax.plot( - history["t"], history["J"], label="Justice (J)", color="royalblue", lw=2 - ) - ax.plot( - history["t"], history["P"], label="Power (P)", color="darkgreen", lw=2 - ) + ax.plot(history["t"], history["J"], label="Justice (J)", color="royalblue", lw=2) + ax.plot(history["t"], history["P"], label="Power (P)", color="darkgreen", lw=2) ax.plot(history["t"], history["W"], label="Wisdom (W)", color="purple", lw=2) # Plot Natural Equilibrium reference lines @@ -634,23 +617,31 @@ def get_reference_points() -> ReferencePoints: simulator = DynamicLJPWv4(complexity_score=1.0) initial_state = (0.2, 0.3, 0.9, 0.2) # High P, low L, J, W - print(f"Initial State: L={initial_state[0]:.2f}, J={initial_state[1]:.2f}, " - f"P={initial_state[2]:.2f}, W={initial_state[3]:.2f}") + print( + f"Initial State: L={initial_state[0]:.2f}, J={initial_state[1]:.2f}, " + f"P={initial_state[2]:.2f}, W={initial_state[3]:.2f}" + ) history = simulator.simulate(initial_state, duration=50, dt=0.05) # Analyze trajectory analysis = simulator.analyze_trajectory(history) - print(f"Final State: L={analysis['final_state'][0]:.2f}, " - f"J={analysis['final_state'][1]:.2f}, " - f"P={analysis['final_state'][2]:.2f}, W={analysis['final_state'][3]:.2f}") + print( + f"Final State: L={analysis['final_state'][0]:.2f}, " + f"J={analysis['final_state'][1]:.2f}, " + f"P={analysis['final_state'][2]:.2f}, W={analysis['final_state'][3]:.2f}" + ) print() print(f"Trajectory Type: {analysis['trajectory_type']}") - print(f"Distance from NE: {analysis['initial_distance_from_ne']:.3f} → " - f"{analysis['final_distance_from_ne']:.3f} (Δ={analysis['distance_improvement']:.3f})") - print(f"Composite Score: {analysis['initial_composite_score']:.3f} → " - f"{analysis['final_composite_score']:.3f} (Δ={analysis['score_improvement']:.3f})") + print( + f"Distance from NE: {analysis['initial_distance_from_ne']:.3f} → " + f"{analysis['final_distance_from_ne']:.3f} (Δ={analysis['distance_improvement']:.3f})" + ) + print( + f"Composite Score: {analysis['initial_composite_score']:.3f} → " + f"{analysis['final_composite_score']:.3f} (Δ={analysis['score_improvement']:.3f})" + ) print(f"Converged: {'Yes' if analysis['converged'] else 'No'}") print() diff --git a/src/ljpw/ljpw_dynamic_v3.py b/src/ljpw/ljpw_dynamic_v3.py index 72a7f6f..c35ea99 100644 --- a/src/ljpw/ljpw_dynamic_v3.py +++ b/src/ljpw/ljpw_dynamic_v3.py @@ -33,9 +33,8 @@ """ import math -from typing import Tuple, List, Dict, Optional, Callable from dataclasses import dataclass - +from typing import Callable, Dict, List, Optional, Tuple # Natural Equilibrium (target state) NATURAL_EQUILIBRIUM = (0.618034, 0.414214, 0.718282, 0.693147) # (L, J, P, W) @@ -48,6 +47,7 @@ class LJPWParameters: Posterior means with 95% credible intervals from the validation study. """ + # Growth coefficients (α) alpha_LJ: float = 0.35 # Justice → Love alpha_LW: float = 0.30 # Wisdom → Love @@ -70,13 +70,13 @@ class LJPWParameters: # Threshold effect (Sigmoidal) gamma_JP: float = 0.49 # Power erosion coefficient (POSTERIOR MEAN: [0.45, 0.53]) - K_JP: float = 0.71 # Power threshold (POSTERIOR MEAN: [0.66, 0.76]) - n_JP: float = 4.1 # Hill coefficient/steepness (POSTERIOR MEAN: [3.5, 4.7]) + K_JP: float = 0.71 # Power threshold (POSTERIOR MEAN: [0.66, 0.76]) + n_JP: float = 4.1 # Hill coefficient/steepness (POSTERIOR MEAN: [3.5, 4.7]) # Coupling amplification (Love as force multiplier) - kappa_LJ: float = 1.4 # Love → Justice amplification (40%) - kappa_LP: float = 1.3 # Love → Power amplification (30%) - kappa_LW: float = 1.5 # Love → Wisdom amplification (50%) + kappa_LJ: float = 1.4 # Love → Justice amplification (40%) + kappa_LP: float = 1.3 # Love → Power amplification (30%) + kappa_LW: float = 1.5 # Love → Wisdom amplification (50%) class LJPWDynamicModel: @@ -104,7 +104,9 @@ def __init__(self, params: Optional[LJPWParameters] = None): """ self.params = params if params is not None else LJPWParameters() - def derivatives(self, state: Tuple[float, float, float, float]) -> Tuple[float, float, float, float]: + def derivatives( + self, state: Tuple[float, float, float, float] + ) -> Tuple[float, float, float, float]: """ Calculate derivatives dL/dt, dJ/dt, dP/dt, dW/dt at given state. @@ -120,9 +122,7 @@ def derivatives(self, state: Tuple[float, float, float, float]) -> Tuple[float, p = self.params # Love dynamics (linear in v3.0) - dL_dt = (p.alpha_LJ * J + - p.alpha_LW * W - - p.beta_L * L) + dL_dt = p.alpha_LJ * J + p.alpha_LW * W - p.beta_L * L # Justice dynamics (NON-LINEAR - key innovation of v3.0) @@ -131,31 +131,21 @@ def derivatives(self, state: Tuple[float, float, float, float]) -> Tuple[float, # Threshold effect: Power erodes Justice above tipping point # Mitigated by Wisdom (1-W factor) - threshold_term = p.gamma_JP * ( - (P ** p.n_JP) / (p.K_JP ** p.n_JP + P ** p.n_JP) - ) * (1 - W) + threshold_term = p.gamma_JP * ((P**p.n_JP) / (p.K_JP**p.n_JP + P**p.n_JP)) * (1 - W) - dJ_dt = (saturation_term + - p.alpha_JW * W - - threshold_term - - p.beta_J * J) + dJ_dt = saturation_term + p.alpha_JW * W - threshold_term - p.beta_J * J # Power dynamics (linear in v3.0) - dP_dt = (p.alpha_PL * L + - p.alpha_PJ * J - - p.beta_P * P) + dP_dt = p.alpha_PL * L + p.alpha_PJ * J - p.beta_P * P # Wisdom dynamics (linear in v3.0) - dW_dt = (p.alpha_WL * L + - p.alpha_WJ * J + - p.alpha_WP * P - - p.beta_W * W) + dW_dt = p.alpha_WL * L + p.alpha_WJ * J + p.alpha_WP * P - p.beta_W * W return (dL_dt, dJ_dt, dP_dt, dW_dt) - def rk4_step(self, - state: Tuple[float, float, float, float], - dt: float) -> Tuple[float, float, float, float]: + def rk4_step( + self, state: Tuple[float, float, float, float], dt: float + ) -> Tuple[float, float, float, float]: """ Perform one RK4 integration step. @@ -175,34 +165,25 @@ def rk4_step(self, k1_L, k1_J, k1_P, k1_W = self.derivatives((L, J, P, W)) # k2: Slope at midpoint using k1 - k2_L, k2_J, k2_P, k2_W = self.derivatives(( - L + 0.5 * dt * k1_L, - J + 0.5 * dt * k1_J, - P + 0.5 * dt * k1_P, - W + 0.5 * dt * k1_W - )) + k2_L, k2_J, k2_P, k2_W = self.derivatives( + (L + 0.5 * dt * k1_L, J + 0.5 * dt * k1_J, P + 0.5 * dt * k1_P, W + 0.5 * dt * k1_W) + ) # k3: Slope at midpoint using k2 - k3_L, k3_J, k3_P, k3_W = self.derivatives(( - L + 0.5 * dt * k2_L, - J + 0.5 * dt * k2_J, - P + 0.5 * dt * k2_P, - W + 0.5 * dt * k2_W - )) + k3_L, k3_J, k3_P, k3_W = self.derivatives( + (L + 0.5 * dt * k2_L, J + 0.5 * dt * k2_J, P + 0.5 * dt * k2_P, W + 0.5 * dt * k2_W) + ) # k4: Slope at endpoint using k3 - k4_L, k4_J, k4_P, k4_W = self.derivatives(( - L + dt * k3_L, - J + dt * k3_J, - P + dt * k3_P, - W + dt * k3_W - )) + k4_L, k4_J, k4_P, k4_W = self.derivatives( + (L + dt * k3_L, J + dt * k3_J, P + dt * k3_P, W + dt * k3_W) + ) # Weighted average: (k1 + 2*k2 + 2*k3 + k4) / 6 - L_next = L + (dt / 6.0) * (k1_L + 2*k2_L + 2*k3_L + k4_L) - J_next = J + (dt / 6.0) * (k1_J + 2*k2_J + 2*k3_J + k4_J) - P_next = P + (dt / 6.0) * (k1_P + 2*k2_P + 2*k3_P + k4_P) - W_next = W + (dt / 6.0) * (k1_W + 2*k2_W + 2*k3_W + k4_W) + L_next = L + (dt / 6.0) * (k1_L + 2 * k2_L + 2 * k3_L + k4_L) + J_next = J + (dt / 6.0) * (k1_J + 2 * k2_J + 2 * k3_J + k4_J) + P_next = P + (dt / 6.0) * (k1_P + 2 * k2_P + 2 * k3_P + k4_P) + W_next = W + (dt / 6.0) * (k1_W + 2 * k2_W + 2 * k3_W + k4_W) # Clamp to valid range [0, 1] L_next = max(0.0, min(1.0, L_next)) @@ -212,10 +193,12 @@ def rk4_step(self, return (L_next, J_next, P_next, W_next) - def simulate(self, - initial_state: Tuple[float, float, float, float], - duration: float = 100.0, - dt: float = 0.1) -> List[Tuple[float, float, float, float, float]]: + def simulate( + self, + initial_state: Tuple[float, float, float, float], + duration: float = 100.0, + dt: float = 0.1, + ) -> List[Tuple[float, float, float, float, float]]: """ Simulate LJPW evolution over time. @@ -240,8 +223,9 @@ def simulate(self, return trajectory - def analyze_trajectory(self, - trajectory: List[Tuple[float, float, float, float, float]]) -> Dict: + def analyze_trajectory( + self, trajectory: List[Tuple[float, float, float, float, float]] + ) -> Dict: """ Analyze a simulated or observed trajectory. @@ -258,7 +242,7 @@ def analyze_trajectory(self, Dictionary with analysis results """ if len(trajectory) < 2: - return {'error': 'Trajectory too short (need at least 2 points)'} + return {"error": "Trajectory too short (need at least 2 points)"} # Initial and final states initial = trajectory[0][1:] # (L, J, P, W) @@ -284,7 +268,7 @@ def analyze_trajectory(self, if converging and velocity_magnitude > 0.001: eta = final_dist / velocity_magnitude else: - eta = float('inf') + eta = float("inf") # Check if near equilibrium near_equilibrium = final_dist < 0.1 @@ -293,18 +277,18 @@ def analyze_trajectory(self, crossing_threshold = any(state[3] > self.params.K_JP for _, *state in trajectory) return { - 'initial_state': initial, - 'final_state': final, - 'initial_distance_from_ne': initial_dist, - 'final_distance_from_ne': final_dist, - 'velocity': velocity, - 'velocity_magnitude': velocity_magnitude, - 'converging': converging, - 'near_equilibrium': near_equilibrium, - 'eta_to_ne': eta, - 'crossing_power_threshold': crossing_threshold, - 'total_time': total_time, - 'num_steps': len(trajectory) + "initial_state": initial, + "final_state": final, + "initial_distance_from_ne": initial_dist, + "final_distance_from_ne": final_dist, + "velocity": velocity, + "velocity_magnitude": velocity_magnitude, + "converging": converging, + "near_equilibrium": near_equilibrium, + "eta_to_ne": eta, + "crossing_power_threshold": crossing_threshold, + "total_time": total_time, + "num_steps": len(trajectory), } @staticmethod @@ -312,17 +296,14 @@ def _distance_from_ne(state: Tuple[float, float, float, float]) -> float: """Calculate Euclidean distance from Natural Equilibrium.""" L, J, P, W = state L_ne, J_ne, P_ne, W_ne = NATURAL_EQUILIBRIUM - return math.sqrt( - (L - L_ne)**2 + - (J - J_ne)**2 + - (P - P_ne)**2 + - (W - W_ne)**2 - ) - - def find_equilibrium(self, - initial_state: Tuple[float, float, float, float], - max_time: float = 1000.0, - tolerance: float = 1e-6) -> Optional[Tuple[float, float, float, float]]: + return math.sqrt((L - L_ne) ** 2 + (J - J_ne) ** 2 + (P - P_ne) ** 2 + (W - W_ne) ** 2) + + def find_equilibrium( + self, + initial_state: Tuple[float, float, float, float], + max_time: float = 1000.0, + tolerance: float = 1e-6, + ) -> Optional[Tuple[float, float, float, float]]: """ Find equilibrium point starting from initial_state. @@ -352,10 +333,12 @@ def find_equilibrium(self, return None # Did not converge - def predict_future(self, - current_state: Tuple[float, float, float, float], - time_horizon: float = 10.0, - dt: float = 0.1) -> Dict: + def predict_future( + self, + current_state: Tuple[float, float, float, float], + time_horizon: float = 10.0, + dt: float = 0.1, + ) -> Dict: """ Predict future evolution from current state. @@ -371,10 +354,10 @@ def predict_future(self, analysis = self.analyze_trajectory(trajectory) return { - 'trajectory': trajectory, - 'analysis': analysis, - 'predicted_state': trajectory[-1][1:], - 'time_horizon': time_horizon + "trajectory": trajectory, + "analysis": analysis, + "predicted_state": trajectory[-1][1:], + "time_horizon": time_horizon, } @@ -394,9 +377,9 @@ def __init__(self, model: Optional[LJPWDynamicModel] = None): """ self.model = model if model is not None else LJPWDynamicModel() - def analyze_sequence(self, - states: List[Tuple[float, float, float, float]], - times: Optional[List[float]] = None) -> Dict: + def analyze_sequence( + self, states: List[Tuple[float, float, float, float]], times: Optional[List[float]] = None + ) -> Dict: """ Analyze a sequence of observed LJPW states. @@ -408,7 +391,7 @@ def analyze_sequence(self, Analysis dictionary """ if len(states) < 2: - return {'error': 'Need at least 2 states'} + return {"error": "Need at least 2 states"} if times is None: times = list(range(len(states))) @@ -424,29 +407,33 @@ def analyze_sequence(self, # Trend: improving or declining? if distances[-1] < distances[0]: - trend = 'IMPROVING' + trend = "IMPROVING" elif distances[-1] > distances[0]: - trend = 'DECLINING' + trend = "DECLINING" else: - trend = 'STABLE' + trend = "STABLE" # Volatility (standard deviation of distances) mean_dist = sum(distances) / len(distances) - variance = sum((d - mean_dist)**2 for d in distances) / len(distances) + variance = sum((d - mean_dist) ** 2 for d in distances) / len(distances) volatility = math.sqrt(variance) - analysis.update({ - 'distances_from_ne': distances, - 'trend': trend, - 'volatility': volatility, - 'improvement': distances[0] - distances[-1] # Positive = improved - }) + analysis.update( + { + "distances_from_ne": distances, + "trend": trend, + "volatility": volatility, + "improvement": distances[0] - distances[-1], # Positive = improved + } + ) return analysis - def fit_trajectory(self, - observed_states: List[Tuple[float, float, float, float]], - times: Optional[List[float]] = None) -> Dict: + def fit_trajectory( + self, + observed_states: List[Tuple[float, float, float, float]], + times: Optional[List[float]] = None, + ) -> Dict: """ Fit dynamic model to observed data. @@ -476,25 +463,27 @@ def fit_trajectory(self, # Find closest predicted time pred_state = min(predicted, key=lambda p: abs(p[0] - obs_time))[1:] - error = math.sqrt(sum((o - p)**2 for o, p in zip(obs_state, pred_state))) + error = math.sqrt(sum((o - p) ** 2 for o, p in zip(obs_state, pred_state))) errors.append(error) rmse = math.sqrt(sum(e**2 for e in errors) / len(errors)) return { - 'initial_state': initial_state, - 'predicted_trajectory': predicted, - 'observed_states': observed_states, - 'errors': errors, - 'rmse': rmse, - 'fit_quality': 'GOOD' if rmse < 0.05 else 'FAIR' if rmse < 0.1 else 'POOR' + "initial_state": initial_state, + "predicted_trajectory": predicted, + "observed_states": observed_states, + "errors": errors, + "rmse": rmse, + "fit_quality": "GOOD" if rmse < 0.05 else "FAIR" if rmse < 0.1 else "POOR", } # Utility functions -def analyze_code_evolution(ljpw_scores: List[Dict], - timestamps: Optional[List[float]] = None) -> Dict: + +def analyze_code_evolution( + ljpw_scores: List[Dict], timestamps: Optional[List[float]] = None +) -> Dict: """ Analyze code evolution from LJPW scores over time. @@ -505,14 +494,13 @@ def analyze_code_evolution(ljpw_scores: List[Dict], Returns: Analysis results """ - states = [(s['L'], s['J'], s['P'], s['W']) for s in ljpw_scores] + states = [(s["L"], s["J"], s["P"], s["W"]) for s in ljpw_scores] analyzer = LJPWTrajectoryAnalyzer() return analyzer.analyze_sequence(states, timestamps) -def predict_code_future(current_ljpw: Dict, - time_horizon: float = 10.0) -> Dict: +def predict_code_future(current_ljpw: Dict, time_horizon: float = 10.0) -> Dict: """ Predict future code evolution from current LJPW state. @@ -523,18 +511,17 @@ def predict_code_future(current_ljpw: Dict, Returns: Prediction results """ - current_state = (current_ljpw['L'], current_ljpw['J'], - current_ljpw['P'], current_ljpw['W']) + current_state = (current_ljpw["L"], current_ljpw["J"], current_ljpw["P"], current_ljpw["W"]) model = LJPWDynamicModel() return model.predict_future(current_state, time_horizon=time_horizon) # Example usage -if __name__ == '__main__': - print("="*70) +if __name__ == "__main__": + print("=" * 70) print("LJPW Dynamic Model v3.0 - Demo") - print("="*70) + print("=" * 70) print() # Example 1: Simulate from poor state toward Natural Equilibrium @@ -546,21 +533,25 @@ def predict_code_future(current_ljpw: Dict, model = LJPWDynamicModel() trajectory = model.simulate(poor_code, duration=50, dt=0.5) - print(f"Initial state: L={poor_code[0]:.3f}, J={poor_code[1]:.3f}, " - f"P={poor_code[2]:.3f}, W={poor_code[3]:.3f}") + print( + f"Initial state: L={poor_code[0]:.3f}, J={poor_code[1]:.3f}, " + f"P={poor_code[2]:.3f}, W={poor_code[3]:.3f}" + ) print(f"Distance from NE: {model._distance_from_ne(poor_code):.3f}") print() final_state = trajectory[-1][1:] - print(f"Final state (t=50): L={final_state[0]:.3f}, J={final_state[1]:.3f}, " - f"P={final_state[2]:.3f}, W={final_state[3]:.3f}") + print( + f"Final state (t=50): L={final_state[0]:.3f}, J={final_state[1]:.3f}, " + f"P={final_state[2]:.3f}, W={final_state[3]:.3f}" + ) print(f"Distance from NE: {model._distance_from_ne(final_state):.3f}") print() analysis = model.analyze_trajectory(trajectory) print(f"Converging: {analysis['converging']}") print(f"Velocity magnitude: {analysis['velocity_magnitude']:.4f}") - if analysis['eta_to_ne'] != float('inf'): + if analysis["eta_to_ne"] != float("inf"): print(f"ETA to equilibrium: {analysis['eta_to_ne']:.1f} time units") else: print("Not converging to equilibrium") @@ -600,11 +591,11 @@ def predict_code_future(current_ljpw: Dict, # Simulated commit history commits = [ - {'L': 0.3, 'J': 0.4, 'P': 0.5, 'W': 0.4}, - {'L': 0.4, 'J': 0.5, 'P': 0.6, 'W': 0.5}, - {'L': 0.5, 'J': 0.5, 'P': 0.7, 'W': 0.5}, - {'L': 0.6, 'J': 0.5, 'P': 0.75, 'W': 0.6}, - {'L': 0.6, 'J': 0.4, 'P': 0.8, 'W': 0.6}, + {"L": 0.3, "J": 0.4, "P": 0.5, "W": 0.4}, + {"L": 0.4, "J": 0.5, "P": 0.6, "W": 0.5}, + {"L": 0.5, "J": 0.5, "P": 0.7, "W": 0.5}, + {"L": 0.6, "J": 0.5, "P": 0.75, "W": 0.6}, + {"L": 0.6, "J": 0.4, "P": 0.8, "W": 0.6}, ] evolution = analyze_code_evolution(commits) @@ -616,13 +607,13 @@ def predict_code_future(current_ljpw: Dict, print(f"Crossing power threshold: {evolution['crossing_power_threshold']}") print() - if evolution['crossing_power_threshold']: + if evolution["crossing_power_threshold"]: print("⚠️ WARNING: Power crossed threshold (0.71) in recent commits!") print(" Risk: Premature optimization eroding code structure") print(" Recommendation: Increase Wisdom before further optimization") print() - print("="*70) + print("=" * 70) print("See docs/THEORY.md for mathematical foundations") print("See examples/advanced/track_code_evolution.py for real usage") - print("="*70) + print("=" * 70) diff --git a/src/ljpw/ljpw_expander.py b/src/ljpw/ljpw_expander.py index 23d579f..5bb3556 100644 --- a/src/ljpw/ljpw_expander.py +++ b/src/ljpw/ljpw_expander.py @@ -11,21 +11,21 @@ Uses semantic primitive templates to generate high-quality output """ +import importlib.util import math -from typing import List, Dict, Any, Optional -from dataclasses import dataclass -from enum import Enum + +# Load the compiler module dynamically +import os # Import from the compressor import sys -import importlib.util +from dataclasses import dataclass +from enum import Enum +from typing import Any, Dict, List, Optional -# Load the compiler module dynamically -import os _current_dir = os.path.dirname(os.path.abspath(__file__)) spec = importlib.util.spec_from_file_location( - "ljpw_semantic_compiler", - os.path.join(_current_dir, "ljpw_semantic_compiler.py") + "ljpw_semantic_compiler", os.path.join(_current_dir, "ljpw_semantic_compiler.py") ) compiler_module = importlib.util.module_from_spec(spec) spec.loader.exec_module(compiler_module) @@ -37,6 +37,7 @@ # CODE GENERATION TEMPLATES # ============================================================================ + class CodeTemplate: """Templates for generating code from semantic primitives""" @@ -50,7 +51,6 @@ def __init__(self, {params}): # Safe initialization {initializations} """, - SemanticPrimitive.ERROR_HANDLE: """ try: {operation} @@ -59,7 +59,6 @@ def __init__(self, {params}): {recovery_strategy} raise """, - SemanticPrimitive.VALIDATION: """ def validate_{name}(value: {type_hint}) -> bool: '''Validate {name} meets constraints''' @@ -68,7 +67,6 @@ def validate_{name}(value: {type_hint}) -> bool: {constraints} return True """, - SemanticPrimitive.TYPE_DEF: """ @dataclass class {name}: @@ -79,7 +77,6 @@ def __post_init__(self): '''Validate invariants''' {invariant_checks} """, - SemanticPrimitive.ALGORITHM: """ def {name}({params}) -> {return_type}: ''' @@ -91,7 +88,6 @@ def {name}({params}) -> {return_type}: {implementation} return result """, - SemanticPrimitive.ABSTRACTION: """ class {name}(ABC): '''Abstract interface for {description}''' @@ -103,7 +99,6 @@ def {method_name}(self, {params}) -> {return_type}: {additional_methods} """, - SemanticPrimitive.PATTERN: """ # Design Pattern: {pattern_name} class {name}: @@ -114,16 +109,18 @@ class {name}: } @classmethod - def get_template(cls, primitive: SemanticPrimitive, language: str = 'python') -> str: + def get_template(cls, primitive: SemanticPrimitive, language: str = "python") -> str: """Get template for a semantic primitive""" - if language.lower() == 'python': + if language.lower() == "python": return cls.PYTHON_TEMPLATES.get(primitive, "# {primitive}") return "// Template not available" + # ============================================================================ # DOCUMENTATION TEMPLATES # ============================================================================ + class DocumentationTemplate: """Templates for generating documentation""" @@ -140,7 +137,6 @@ class DocumentationTemplate: **LJPW Profile**: L={L:.2f}, J={J:.2f}, P={P:.2f}, W={W:.2f} """, - SemanticPrimitive.ARCHITECTURE: """ ## Architectural Decision @@ -162,10 +158,12 @@ class DocumentationTemplate: """, } + # ============================================================================ # SEMANTIC EXPANDER # ============================================================================ + class SemanticExpander: """ Expands compressed LJPW genomes into concrete artifacts @@ -174,12 +172,12 @@ class SemanticExpander: Compressed Genome → Generated Code/Docs/Specs """ - def __init__(self, target_language: str = 'python'): + def __init__(self, target_language: str = "python"): self.target_language = target_language - def expand_to_code(self, - compressed_units: List[CompressedSemanticUnit], - context: Dict[str, Any] = None) -> str: + def expand_to_code( + self, compressed_units: List[CompressedSemanticUnit], context: Dict[str, Any] = None + ) -> str: """ Expand compressed semantic units to actual code @@ -218,11 +216,11 @@ def expand_to_code(self, generated_code.append(f"\n# TODO: Implement {unit.primitive.value}") generated_code.append(f"# LJPW: L={L:.2f}, J={J:.2f}, P={P:.2f}, W={W:.2f}") - return '\n'.join(generated_code) + return "\n".join(generated_code) - def expand_to_documentation(self, - compressed_units: List[CompressedSemanticUnit], - context: Dict[str, Any] = None) -> str: + def expand_to_documentation( + self, compressed_units: List[CompressedSemanticUnit], context: Dict[str, Any] = None + ) -> str: """ Expand compressed genome to documentation @@ -230,7 +228,7 @@ def expand_to_documentation(self, Markdown documentation """ if context is None: - context = {'system_name': 'System'} + context = {"system_name": "System"} docs = [] docs.append(f"# {context.get('system_name', 'System')} Architecture") @@ -251,7 +249,7 @@ def expand_to_documentation(self, primitive_counts[prim] = primitive_counts.get(prim, 0) + 1 n = len(compressed_units) - avg_L, avg_J, avg_P, avg_W = total_L/n, total_J/n, total_P/n, total_W/n + avg_L, avg_J, avg_P, avg_W = total_L / n, total_J / n, total_P / n, total_W / n # Overall system assessment docs.append("## System Profile\n") @@ -275,11 +273,11 @@ def expand_to_documentation(self, docs.append("\n## Recommendations\n") docs.append(self._generate_recommendations(avg_L, avg_J, avg_P, avg_W, primitive_counts)) - return '\n'.join(docs) + return "\n".join(docs) - def expand_to_improvement_plan(self, - compressed_units: List[CompressedSemanticUnit], - target_ljpw: tuple = None) -> str: + def expand_to_improvement_plan( + self, compressed_units: List[CompressedSemanticUnit], target_ljpw: tuple = None + ) -> str: """ Generate an improvement plan to move system toward target LJPW state @@ -306,7 +304,12 @@ def expand_to_improvement_plan(self, total_W += W n = len(compressed_units) - current_L, current_J, current_P, current_W = total_L/n, total_J/n, total_P/n, total_W/n + current_L, current_J, current_P, current_W = ( + total_L / n, + total_J / n, + total_P / n, + total_W / n, + ) plan = [] plan.append("# LJPW Improvement Plan\n") @@ -315,42 +318,44 @@ def expand_to_improvement_plan(self, plan.append("|-----------|---------|--------|-----|----------|") gaps = { - 'L': (current_L, target_L, target_L - current_L), - 'J': (current_J, target_J, target_J - current_J), - 'P': (current_P, target_P, target_P - current_P), - 'W': (current_W, target_W, target_W - current_W), + "L": (current_L, target_L, target_L - current_L), + "J": (current_J, target_J, target_J - current_J), + "P": (current_P, target_P, target_P - current_P), + "W": (current_W, target_W, target_W - current_W), } for dim, (curr, targ, gap) in gaps.items(): priority = "HIGH" if abs(gap) > 0.15 else ("MEDIUM" if abs(gap) > 0.08 else "LOW") direction = "UP" if gap > 0 else ("DOWN" if gap < 0 else "OK") - plan.append(f"| {dim} | {curr:.3f} | {targ:.3f} | {gap:+.3f} {direction} | {priority} |") + plan.append( + f"| {dim} | {curr:.3f} | {targ:.3f} | {gap:+.3f} {direction} | {priority} |" + ) plan.append("\n## Recommended Actions\n") # Generate specific actions based on gaps - if gaps['L'][2] > 0.1: # Need more Love + if gaps["L"][2] > 0.1: # Need more Love plan.append("### Increase Safety (Love)\n") plan.append("- [ ] Add error handling to critical paths") plan.append("- [ ] Implement input validation") plan.append("- [ ] Add defensive null checks") plan.append("- [ ] Increase test coverage\n") - if gaps['J'][2] > 0.1: # Need more Justice + if gaps["J"][2] > 0.1: # Need more Justice plan.append("### Improve Structure (Justice)\n") plan.append("- [ ] Define clear interfaces") plan.append("- [ ] Add type annotations") plan.append("- [ ] Enforce coding standards") plan.append("- [ ] Document contracts\n") - if gaps['P'][2] < -0.1: # Too much Power, need to reduce + if gaps["P"][2] < -0.1: # Too much Power, need to reduce plan.append("### Optimize Performance (Power)\n") plan.append("- [ ] Profile and identify bottlenecks") plan.append("- [ ] Add caching where appropriate") plan.append("- [ ] Consider async operations") plan.append("- [ ] Review algorithmic complexity\n") - if gaps['W'][2] > 0.1: # Need more Wisdom + if gaps["W"][2] > 0.1: # Need more Wisdom plan.append("### Enhance Design (Wisdom)\n") plan.append("- [ ] Refactor into smaller modules") plan.append("- [ ] Apply design patterns") @@ -364,9 +369,11 @@ def expand_to_improvement_plan(self, plan.append(f"\n## Estimated Effort\n") plan.append(f"**Total improvement gap**: {total_gap:.3f}") plan.append(f"**Estimated effort**: {effort_days} person-days") - plan.append(f"**Expected improvement**: {100*total_gap/(4*0.3):.1f}% movement toward optimal") + plan.append( + f"**Expected improvement**: {100*total_gap/(4*0.3):.1f}% movement toward optimal" + ) - return '\n'.join(plan) + return "\n".join(plan) def _dequantize_ljpw(self, state: tuple) -> tuple: """Convert quantized LJPW levels back to continuous values""" @@ -380,37 +387,41 @@ def dequant(level): return (dequant(L_q), dequant(J_q), dequant(P_q), dequant(W_q)) - def _generate_parameters(self, unit: CompressedSemanticUnit, - context: Dict, index: int) -> Dict[str, str]: + def _generate_parameters( + self, unit: CompressedSemanticUnit, context: Dict, index: int + ) -> Dict[str, str]: """Generate parameters for code template""" L, J, P, W = self._dequantize_ljpw(unit.ljpw_state) # Generic parameters params = { - 'name': context.get('name', f'component_{index}'), - 'description': context.get('description', f'Component {index}'), - 'primitive': unit.primitive.value, - 'L': L, 'J': J, 'P': P, 'W': W, - 'params': 'self, *args, **kwargs', - 'type_hint': 'Any', - 'return_type': 'None', - 'fields': ' pass', - 'implementation': ' pass', - 'validations': ' pass', - 'initializations': ' pass', - 'constraints': ' pass', - 'invariant_checks': ' pass', - 'operation': ' pass', - 'exception_types': 'Exception', - 'recovery_strategy': ' pass', - 'context': 'operation', - 'method_name': 'execute', - 'additional_methods': ' pass', - 'pattern_name': 'Unknown', - 'purpose': 'system operation', - 'pattern_implementation': ' pass', - 'time_complexity': 'n', - 'space_complexity': 'n', + "name": context.get("name", f"component_{index}"), + "description": context.get("description", f"Component {index}"), + "primitive": unit.primitive.value, + "L": L, + "J": J, + "P": P, + "W": W, + "params": "self, *args, **kwargs", + "type_hint": "Any", + "return_type": "None", + "fields": " pass", + "implementation": " pass", + "validations": " pass", + "initializations": " pass", + "constraints": " pass", + "invariant_checks": " pass", + "operation": " pass", + "exception_types": "Exception", + "recovery_strategy": " pass", + "context": "operation", + "method_name": "execute", + "additional_methods": " pass", + "pattern_name": "Unknown", + "purpose": "system operation", + "pattern_implementation": " pass", + "time_complexity": "n", + "space_complexity": "n", } return params @@ -422,8 +433,7 @@ def _generate_health_assessment(self, L: float, J: float, P: float, W: float) -> # Calculate distance from Natural Equilibrium NE = (0.618, 0.414, 0.718, 0.693) distance = math.sqrt( - (NE[0] - L)**2 + (NE[1] - J)**2 + - (NE[2] - P)**2 + (NE[3] - W)**2 + (NE[0] - L) ** 2 + (NE[1] - J) ** 2 + (NE[2] - P) ** 2 + (NE[3] - W) ** 2 ) if distance < 0.2: @@ -442,10 +452,11 @@ def _generate_health_assessment(self, L: float, J: float, P: float, W: float) -> assessment.append(f"\n**Overall Health**: {health}") assessment.append(f"**Distance from Natural Equilibrium**: {distance:.3f}") - return '\n'.join(assessment) + return "\n".join(assessment) - def _generate_recommendations(self, L: float, J: float, P: float, W: float, - primitive_counts: Dict) -> str: + def _generate_recommendations( + self, L: float, J: float, P: float, W: float, primitive_counts: Dict + ) -> str: """Generate specific recommendations""" recs = [] @@ -457,14 +468,21 @@ def _generate_recommendations(self, L: float, J: float, P: float, W: float, if P > 0.8 and W < 0.6: recs.append("- **WARNING**: High power without wisdom - risk of technical debt") if W < 0.5: - recs.append("- **MEDIUM**: Enhance design quality (refactoring, patterns, documentation)") + recs.append( + "- **MEDIUM**: Enhance design quality (refactoring, patterns, documentation)" + ) # Check primitive distribution total = sum(primitive_counts.values()) safe_prims = sum( - count for prim, count in primitive_counts.items() - if prim in [SemanticPrimitive.SAFE_INIT, SemanticPrimitive.ERROR_HANDLE, - SemanticPrimitive.VALIDATION] + count + for prim, count in primitive_counts.items() + if prim + in [ + SemanticPrimitive.SAFE_INIT, + SemanticPrimitive.ERROR_HANDLE, + SemanticPrimitive.VALIDATION, + ] ) if safe_prims / total < 0.3: @@ -473,17 +491,18 @@ def _generate_recommendations(self, L: float, J: float, P: float, W: float, if not recs: recs.append("- System appears well-balanced. Continue maintaining quality.") - return '\n'.join(recs) + return "\n".join(recs) + # ============================================================================ # DEMONSTRATION # ============================================================================ -if __name__ == '__main__': - print("="*70) +if __name__ == "__main__": + print("=" * 70) print("LJPW SEMANTIC EXPANDER v1.0") print("Generative Compiler - Expanding Compressed Genomes") - print("="*70) + print("=" * 70) # Create sample compressed genome print("\n1. CREATING SAMPLE COMPRESSED GENOME") @@ -506,8 +525,8 @@ def _generate_recommendations(self, L: float, J: float, P: float, W: float, print("\n2. EXPANDING TO CODE") print("-" * 70) - expander = SemanticExpander(target_language='python') - generated_code = expander.expand_to_code(sample_genome, context={'name': 'DataProcessor'}) + expander = SemanticExpander(target_language="python") + generated_code = expander.expand_to_code(sample_genome, context={"name": "DataProcessor"}) print(generated_code[:500]) print("\n... (truncated)") @@ -518,8 +537,7 @@ def _generate_recommendations(self, L: float, J: float, P: float, W: float, print("-" * 70) documentation = expander.expand_to_documentation( - sample_genome, - context={'system_name': 'Data Processing Module'} + sample_genome, context={"system_name": "Data Processing Module"} ) print(documentation) @@ -531,9 +549,9 @@ def _generate_recommendations(self, L: float, J: float, P: float, W: float, improvement_plan = expander.expand_to_improvement_plan(sample_genome) print(improvement_plan) - print("\n" + "="*70) + print("\n" + "=" * 70) print("EXPANSION SUCCESSFUL") - print("="*70) + print("=" * 70) print("\nThe Expander can generate:") print(" [OK] Code implementations") print(" [OK] Architecture documentation") diff --git a/src/ljpw/ljpw_iso_analyzer.py b/src/ljpw/ljpw_iso_analyzer.py index dacd275..ff6a047 100644 --- a/src/ljpw/ljpw_iso_analyzer.py +++ b/src/ljpw/ljpw_iso_analyzer.py @@ -26,16 +26,17 @@ Date: November 2025 """ -import sys -import os import math -from pathlib import Path +import os +import sys from collections import Counter, defaultdict -from typing import Dict, List, Tuple, Optional from dataclasses import dataclass +from pathlib import Path +from typing import Dict, List, Optional, Tuple try: import pycdlib + PYCDLIB_AVAILABLE = True except ImportError: PYCDLIB_AVAILABLE = False @@ -49,6 +50,7 @@ @dataclass class ISOStructure: """Extracted structural information from ISO""" + total_files: int total_dirs: int total_size: int @@ -86,7 +88,7 @@ def analyze(self, iso_path: str) -> Dict: Dictionary with LJPW scores, genome, and insights """ if not os.path.exists(iso_path): - return {'error': f'ISO file not found: {iso_path}'} + return {"error": f"ISO file not found: {iso_path}"} if not PYCDLIB_AVAILABLE: # Fallback: basic file-based analysis @@ -106,7 +108,7 @@ def analyze(self, iso_path: str) -> Dict: # Calculate health with improved formula distance = self._distance_from_ne((L_score, J_score, P_score, W_score)) - + # Improved health calculation (matching ljpw_standalone.py) distance_health = max(0, 1.0 - distance / 3.0) magnitude = (L_score + J_score + P_score + W_score) / 4.0 @@ -121,25 +123,25 @@ def analyze(self, iso_path: str) -> Dict: iso_type = self._detect_type(structure) return { - 'filename': os.path.basename(iso_path), - 'size_mb': structure.total_size / (1024 * 1024), - 'type': iso_type, - 'ljpw': { - 'L': round(L_score, 3), - 'J': round(J_score, 3), - 'P': round(P_score, 3), - 'W': round(W_score, 3) + "filename": os.path.basename(iso_path), + "size_mb": structure.total_size / (1024 * 1024), + "type": iso_type, + "ljpw": { + "L": round(L_score, 3), + "J": round(J_score, 3), + "P": round(P_score, 3), + "W": round(W_score, 3), + }, + "genome": genome, + "health": round(health * 100, 1), + "distance_from_ne": round(distance, 3), + "insights": insights, + "structure": { + "files": structure.total_files, + "directories": structure.total_dirs, + "max_depth": structure.max_depth, + "avg_depth": round(structure.avg_depth, 2), }, - 'genome': genome, - 'health': round(health * 100, 1), - 'distance_from_ne': round(distance, 3), - 'insights': insights, - 'structure': { - 'files': structure.total_files, - 'directories': structure.total_dirs, - 'max_depth': structure.max_depth, - 'avg_depth': round(structure.avg_depth, 2) - } } def _extract_structure(self, iso_path: str) -> ISOStructure: @@ -160,8 +162,8 @@ def _extract_structure(self, iso_path: str) -> ISOStructure: binaries = 0 # Walk the ISO filesystem - for dirname, dirlist, filelist in iso.walk(iso_path='/'): - depth = dirname.count('/') + for dirname, dirlist, filelist in iso.walk(iso_path="/"): + depth = dirname.count("/") directory_depths.append(depth) total_dirs += 1 @@ -173,26 +175,26 @@ def _extract_structure(self, iso_path: str) -> ISOStructure: file_types[ext] += 1 # Categorize files - if ext in ['.gz', '.xz', '.bz2', '.zst', '.cab', '.zip']: + if ext in [".gz", ".xz", ".bz2", ".zst", ".cab", ".zip"]: compressed += 1 - if ext in ['.md5', '.sha256', '.sha1', '.sig', '.asc']: + if ext in [".md5", ".sha256", ".sha1", ".sig", ".asc"]: checksums += 1 - if ext in ['.conf', '.cfg', '.xml', '.json', '.yaml', '.ini']: + if ext in [".conf", ".cfg", ".xml", ".json", ".yaml", ".ini"]: configs += 1 - if ext in ['.sh', '.py', '.pl', '.rb', '.ps1', '.bat']: + if ext in [".sh", ".py", ".pl", ".rb", ".ps1", ".bat"]: scripts += 1 - if ext in ['.md', '.txt', '.rst', '.pdf', '.html'] or 'readme' in filename.lower(): + if ext in [".md", ".txt", ".rst", ".pdf", ".html"] or "readme" in filename.lower(): docs += 1 - if ext in ['.exe', '.dll', '.so', '.bin', '.elf']: + if ext in [".exe", ".dll", ".so", ".bin", ".elf"]: binaries += 1 # Try to get file size (if available) try: full_path = os.path.join(dirname, filename) entry = iso.get_entry(iso_path=full_path) - if hasattr(entry, 'get_data_length'): + if hasattr(entry, "get_data_length"): total_size += entry.get_data_length() - except: + except Exception: pass iso.close() @@ -214,7 +216,7 @@ def _extract_structure(self, iso_path: str) -> ISOStructure: binary_files=binaries, max_depth=max_depth, avg_depth=avg_depth, - naming_patterns=[] + naming_patterns=[], ) def _analyze_fallback(self, iso_path: str) -> Dict: @@ -225,22 +227,22 @@ def _analyze_fallback(self, iso_path: str) -> Dict: filename = os.path.basename(iso_path).lower() # Guess type from filename - if 'server' in filename: - iso_type = 'Server Operating System' + if "server" in filename: + iso_type = "Server Operating System" L, J, P, W = 0.70, 0.75, 0.65, 0.80 - elif 'desktop' in filename or 'live' in filename: - iso_type = 'Desktop Operating System' + elif "desktop" in filename or "live" in filename: + iso_type = "Desktop Operating System" L, J, P, W = 0.65, 0.70, 0.70, 0.75 - elif 'minimal' in filename or 'net' in filename: - iso_type = 'Minimal Installation' + elif "minimal" in filename or "net" in filename: + iso_type = "Minimal Installation" L, J, P, W = 0.50, 0.65, 0.75, 0.70 else: - iso_type = 'Unknown ISO' + iso_type = "Unknown ISO" L, J, P, W = 0.60, 0.65, 0.65, 0.70 genome = self._generate_genome(L, J, P, W) distance = self._distance_from_ne((L, J, P, W)) - + # Improved health calculation (matching ljpw_standalone.py) distance_health = max(0, 1.0 - distance / 3.0) magnitude = (L + J + P + W) / 4.0 @@ -249,15 +251,15 @@ def _analyze_fallback(self, iso_path: str) -> Dict: health = 0.7 * distance_health + 0.3 * magnitude_health return { - 'filename': os.path.basename(iso_path), - 'size_mb': size_mb, - 'type': iso_type, - 'ljpw': {'L': L, 'J': J, 'P': P, 'W': W}, - 'genome': genome, - 'health': round(health * 100, 1), - 'distance_from_ne': round(distance, 3), - 'insights': ['(Basic analysis - install pycdlib for detailed analysis)'], - 'structure': {'note': 'Install pycdlib for detailed structure analysis'} + "filename": os.path.basename(iso_path), + "size_mb": size_mb, + "type": iso_type, + "ljpw": {"L": L, "J": J, "P": P, "W": W}, + "genome": genome, + "health": round(health * 100, 1), + "distance_from_ne": round(distance, 3), + "insights": ["(Basic analysis - install pycdlib for detailed analysis)"], + "structure": {"note": "Install pycdlib for detailed structure analysis"}, } def _calculate_love(self, s: ISOStructure) -> float: @@ -392,7 +394,9 @@ def _calculate_wisdom(self, s: ISOStructure) -> float: score += min(0.25, script_ratio * 50) # Balanced depth indicates architectural thinking - depth_variance = max(s.directory_depths) - min(s.directory_depths) if s.directory_depths else 0 + depth_variance = ( + max(s.directory_depths) - min(s.directory_depths) if s.directory_depths else 0 + ) if depth_variance <= 5: # Consistent depth score += 0.15 @@ -400,6 +404,7 @@ def _calculate_wisdom(self, s: ISOStructure) -> float: def _generate_genome(self, L: float, J: float, P: float, W: float) -> str: """Generate LJPW genome string""" + def encode_dimension(value: float) -> str: # Map 0.0-1.0 to 0-9 level = int(value * 10) @@ -411,15 +416,11 @@ def _distance_from_ne(self, state: Tuple[float, float, float, float]) -> float: """Calculate Euclidean distance from Natural Equilibrium""" L, J, P, W = state L_ne, J_ne, P_ne, W_ne = NATURAL_EQUILIBRIUM - return math.sqrt( - (L - L_ne)**2 + - (J - J_ne)**2 + - (P - P_ne)**2 + - (W - W_ne)**2 - ) + return math.sqrt((L - L_ne) ** 2 + (J - J_ne) ** 2 + (P - P_ne) ** 2 + (W - W_ne) ** 2) - def _generate_insights(self, L: float, J: float, P: float, W: float, - s: ISOStructure) -> List[str]: + def _generate_insights( + self, L: float, J: float, P: float, W: float, s: ISOStructure + ) -> List[str]: """Generate actionable insights""" insights = [] @@ -430,22 +431,30 @@ def _generate_insights(self, L: float, J: float, P: float, W: float, # Dimension-specific insights if L < 0.50: - insights.append(f"Low Safety (L={L:.2f}): Few checksums ({s.checksum_files}), minimal validation") + insights.append( + f"Low Safety (L={L:.2f}): Few checksums ({s.checksum_files}), minimal validation" + ) elif L > 0.75: insights.append(f"High Safety (L={L:.2f}): Excellent validation and error handling") if J < 0.50: - insights.append(f"Low Structure (J={J:.2f}): Disorganized hierarchy (depth: {s.avg_depth:.1f})") + insights.append( + f"Low Structure (J={J:.2f}): Disorganized hierarchy (depth: {s.avg_depth:.1f})" + ) elif J > 0.75: insights.append(f"Good Structure (J={J:.2f}): Well-organized, modular design") if P > 0.80: - insights.append(f"High Performance (P={P:.2f}): Heavily optimized ({s.compressed_files} compressed files)") + insights.append( + f"High Performance (P={P:.2f}): Heavily optimized ({s.compressed_files} compressed files)" + ) elif P < 0.50: insights.append(f"Low Performance (P={P:.2f}): Minimal optimization") if W > 0.75: - insights.append(f"High Wisdom (W={W:.2f}): Thoughtful design ({s.doc_files} docs, {s.config_files} configs)") + insights.append( + f"High Wisdom (W={W:.2f}): Thoughtful design ({s.doc_files} docs, {s.config_files} configs)" + ) elif W < 0.50: insights.append(f"Low Wisdom (W={W:.2f}): Limited documentation and configuration") @@ -461,8 +470,9 @@ def _generate_insights(self, L: float, J: float, P: float, W: float, def _detect_type(self, s: ISOStructure) -> str: """Detect ISO type from structural patterns""" # Heuristics based on file patterns - has_kernel = any('.vmlinuz' in str(ext) or 'kernel' in str(ext) - for ext in s.file_types.keys()) + has_kernel = any( + ".vmlinuz" in str(ext) or "kernel" in str(ext) for ext in s.file_types.keys() + ) has_installer = s.script_files > 10 has_docs = s.doc_files > 20 @@ -488,10 +498,7 @@ def compare(self, iso_paths: List[str]) -> Dict: result = self.analyze(iso_path) results.append(result) - return { - 'comparison': results, - 'summary': self._generate_comparison_summary(results) - } + return {"comparison": results, "summary": self._generate_comparison_summary(results)} def _generate_comparison_summary(self, results: List[Dict]) -> str: """Generate comparison summary""" @@ -499,50 +506,67 @@ def _generate_comparison_summary(self, results: List[Dict]) -> str: return "No ISOs to compare" # Find highest in each dimension - highest_L = max(results, key=lambda r: r['ljpw']['L']) - highest_J = max(results, key=lambda r: r['ljpw']['J']) - highest_P = max(results, key=lambda r: r['ljpw']['P']) - highest_W = max(results, key=lambda r: r['ljpw']['W']) - healthiest = max(results, key=lambda r: r['health']) + highest_L = max(results, key=lambda r: r["ljpw"]["L"]) + highest_J = max(results, key=lambda r: r["ljpw"]["J"]) + highest_P = max(results, key=lambda r: r["ljpw"]["P"]) + highest_W = max(results, key=lambda r: r["ljpw"]["W"]) + healthiest = max(results, key=lambda r: r["health"]) summary = [] - summary.append(f"Most Safe (L): {highest_L['filename']} (L={highest_L['ljpw']['L']:.2f})") - summary.append(f"Best Structured (J): {highest_J['filename']} (J={highest_J['ljpw']['J']:.2f})") - summary.append(f"Most Optimized (P): {highest_P['filename']} (P={highest_P['ljpw']['P']:.2f})") - summary.append(f"Most Wise (W): {highest_W['filename']} (W={highest_W['ljpw']['W']:.2f})") - summary.append(f"Healthiest Overall: {healthiest['filename']} ({healthiest['health']:.1f}%)") + summary.append( + f"Most Safe (L): {highest_L['filename']} (L={highest_L['ljpw']['L']:.2f})" + ) + summary.append( + f"Best Structured (J): {highest_J['filename']} (J={highest_J['ljpw']['J']:.2f})" + ) + summary.append( + f"Most Optimized (P): {highest_P['filename']} (P={highest_P['ljpw']['P']:.2f})" + ) + summary.append( + f"Most Wise (W): {highest_W['filename']} (W={highest_W['ljpw']['W']:.2f})" + ) + summary.append( + f"Healthiest Overall: {healthiest['filename']} ({healthiest['health']:.1f}%)" + ) - return '\n'.join(summary) + return "\n".join(summary) # CLI Interface + def format_result(result: Dict) -> str: """Format analysis result for display""" output = [] - output.append("="*70) + output.append("=" * 70) output.append(f"ISO: {result['filename']}") output.append(f"Type: {result.get('type', 'Unknown')}") output.append(f"Size: {result.get('size_mb', 0):.1f} MB") - output.append("-"*70) + output.append("-" * 70) - ljpw = result['ljpw'] - output.append(f"LJPW State: L={ljpw['L']:.3f}, J={ljpw['J']:.3f}, P={ljpw['P']:.3f}, W={ljpw['W']:.3f}") + ljpw = result["ljpw"] + output.append( + f"LJPW State: L={ljpw['L']:.3f}, J={ljpw['J']:.3f}, P={ljpw['P']:.3f}, W={ljpw['W']:.3f}" + ) output.append(f"Genome: {result['genome']}") output.append(f"Health: {result['health']}%") output.append(f"Distance from NE: {result['distance_from_ne']:.3f}") - if 'structure' in result and isinstance(result['structure'], dict) and 'files' in result['structure']: - s = result['structure'] + if ( + "structure" in result + and isinstance(result["structure"], dict) + and "files" in result["structure"] + ): + s = result["structure"] output.append(f"\nStructure: {s['files']} files, {s['directories']} dirs") output.append(f"Depth: avg={s['avg_depth']:.1f}, max={s['max_depth']}") output.append("\nInsights:") - for insight in result['insights']: + for insight in result["insights"]: output.append(f" {insight}") - output.append("="*70) - return '\n'.join(output) + output.append("=" * 70) + return "\n".join(output) def main(): @@ -566,34 +590,34 @@ def main(): command = sys.argv[1] analyzer = LJPWISOAnalyzer() - if command == 'analyze': + if command == "analyze": iso_path = sys.argv[2] result = analyzer.analyze(iso_path) print(format_result(result)) - elif command == 'compare': + elif command == "compare": iso_paths = sys.argv[2:] comparison = analyzer.compare(iso_paths) - print("="*70) + print("=" * 70) print("ISO COMPARISON") - print("="*70) + print("=" * 70) print() - for result in comparison['comparison']: + for result in comparison["comparison"]: print(format_result(result)) print() - print("="*70) + print("=" * 70) print("SUMMARY") - print("-"*70) - print(comparison['summary']) - print("="*70) + print("-" * 70) + print(comparison["summary"]) + print("=" * 70) else: print(f"Unknown command: {command}") print("Use 'analyze' or 'compare'") -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/src/ljpw/ljpw_pipeline.py b/src/ljpw/ljpw_pipeline.py index 01006c2..1f6a5a3 100644 --- a/src/ljpw/ljpw_pipeline.py +++ b/src/ljpw/ljpw_pipeline.py @@ -10,14 +10,14 @@ This is the production-ready system for solving the token limit problem """ +# Import all components +import importlib.util import math import re -from typing import List, Dict, Any, Tuple from dataclasses import dataclass from pathlib import Path +from typing import Any, Dict, List, Tuple -# Import all components -import importlib.util def load_module(name, path): """Dynamically load a module""" @@ -26,10 +26,14 @@ def load_module(name, path): spec.loader.exec_module(module) return module + # Load components import os + _current_dir = os.path.dirname(os.path.abspath(__file__)) -compiler_mod = load_module("ljpw_semantic_compiler", os.path.join(_current_dir, "ljpw_semantic_compiler.py")) +compiler_mod = load_module( + "ljpw_semantic_compiler", os.path.join(_current_dir, "ljpw_semantic_compiler.py") +) expander_mod = load_module("ljpw_expander", os.path.join(_current_dir, "ljpw_expander.py")) AdvancedSemanticCompressor = compiler_mod.AdvancedSemanticCompressor @@ -41,6 +45,7 @@ def load_module(name, path): # CODE ANALYZER: Extracts LJPW scores from actual code # ============================================================================ + class CodeAnalyzer: """ Analyzes source code and extracts LJPW scores @@ -54,43 +59,40 @@ def _build_patterns(self) -> Dict: """Build regex patterns for code analysis""" return { # Love (Safety) indicators - 'error_handling': r'(try|except|catch|Result|Option|error|Error)', - 'validation': r'(validate|check|verify|assert|require)', - 'null_safety': r'(Optional|Maybe|\?\.|if.*is not None)', - 'bounds_check': r'(len\(|\.length|bounds|range)', - + "error_handling": r"(try|except|catch|Result|Option|error|Error)", + "validation": r"(validate|check|verify|assert|require)", + "null_safety": r"(Optional|Maybe|\?\.|if.*is not None)", + "bounds_check": r"(len\(|\.length|bounds|range)", # Justice (Structure) indicators - 'type_annotations': r'(:\s*\w+|<\w+>|implements|interface)', - 'contracts': r'(@contract|@invariant|@requires|@ensures)', - 'documentation': r'("""|\'\'\' |/\*\*|///)' , - + "type_annotations": r"(:\s*\w+|<\w+>|implements|interface)", + "contracts": r"(@contract|@invariant|@requires|@ensures)", + "documentation": r'("""|\'\'\' |/\*\*|///)', # Power (Performance) indicators - 'algorithms': r'(sort|search|binary|hash|cache|optimize)', - 'complexity': r'(O\(|complexity|performance|fast)', - 'async': r'(async|await|promise|thread|parallel)', - + "algorithms": r"(sort|search|binary|hash|cache|optimize)", + "complexity": r"(O\(|complexity|performance|fast)", + "async": r"(async|await|promise|thread|parallel)", # Wisdom (Design) indicators - 'abstraction': r'(abstract|interface|ABC|protocol)', - 'patterns': r'(factory|singleton|observer|strategy|builder)', - 'modularity': r'(class |def |module|package|namespace)', + "abstraction": r"(abstract|interface|ABC|protocol)", + "patterns": r"(factory|singleton|observer|strategy|builder)", + "modularity": r"(class |def |module|package|namespace)", } - def analyze_code(self, code: str, filename: str = 'unknown') -> Dict[str, Any]: + def analyze_code(self, code: str, filename: str = "unknown") -> Dict[str, Any]: """ Analyze a code snippet and extract LJPW scores Returns: Dict with ljpw_scores and metadata """ - lines = code.split('\n') + lines = code.split("\n") total_lines = len(lines) - code_lines = len([l for l in lines if l.strip() and not l.strip().startswith('#')]) + code_lines = len([l for l in lines if l.strip() and not l.strip().startswith("#")]) if code_lines == 0: return { - 'ljpw_scores': (0.0, 0.0, 0.0, 0.0), - 'semantic_type': 'empty', - 'metadata': {'filename': filename, 'lines': 0} + "ljpw_scores": (0.0, 0.0, 0.0, 0.0), + "semantic_type": "empty", + "metadata": {"filename": filename, "lines": 0}, } # Count pattern matches @@ -100,25 +102,25 @@ def analyze_code(self, code: str, filename: str = 'unknown') -> Dict[str, Any]: wisdom_score = 0 # Love: Safety features - love_score += len(re.findall(self.patterns['error_handling'], code, re.IGNORECASE)) * 0.15 - love_score += len(re.findall(self.patterns['validation'], code, re.IGNORECASE)) * 0.12 - love_score += len(re.findall(self.patterns['null_safety'], code, re.IGNORECASE)) * 0.10 - love_score += len(re.findall(self.patterns['bounds_check'], code, re.IGNORECASE)) * 0.08 + love_score += len(re.findall(self.patterns["error_handling"], code, re.IGNORECASE)) * 0.15 + love_score += len(re.findall(self.patterns["validation"], code, re.IGNORECASE)) * 0.12 + love_score += len(re.findall(self.patterns["null_safety"], code, re.IGNORECASE)) * 0.10 + love_score += len(re.findall(self.patterns["bounds_check"], code, re.IGNORECASE)) * 0.08 # Justice: Structure - justice_score += len(re.findall(self.patterns['type_annotations'], code)) * 0.12 - justice_score += len(re.findall(self.patterns['contracts'], code)) * 0.15 - justice_score += len(re.findall(self.patterns['documentation'], code)) * 0.10 + justice_score += len(re.findall(self.patterns["type_annotations"], code)) * 0.12 + justice_score += len(re.findall(self.patterns["contracts"], code)) * 0.15 + justice_score += len(re.findall(self.patterns["documentation"], code)) * 0.10 # Power: Performance - power_score += len(re.findall(self.patterns['algorithms'], code, re.IGNORECASE)) * 0.15 - power_score += len(re.findall(self.patterns['complexity'], code, re.IGNORECASE)) * 0.08 - power_score += len(re.findall(self.patterns['async'], code, re.IGNORECASE)) * 0.12 + power_score += len(re.findall(self.patterns["algorithms"], code, re.IGNORECASE)) * 0.15 + power_score += len(re.findall(self.patterns["complexity"], code, re.IGNORECASE)) * 0.08 + power_score += len(re.findall(self.patterns["async"], code, re.IGNORECASE)) * 0.12 # Wisdom: Design - wisdom_score += len(re.findall(self.patterns['abstraction'], code, re.IGNORECASE)) * 0.15 - wisdom_score += len(re.findall(self.patterns['patterns'], code, re.IGNORECASE)) * 0.12 - wisdom_score += len(re.findall(self.patterns['modularity'], code)) * 0.05 + wisdom_score += len(re.findall(self.patterns["abstraction"], code, re.IGNORECASE)) * 0.15 + wisdom_score += len(re.findall(self.patterns["patterns"], code, re.IGNORECASE)) * 0.12 + wisdom_score += len(re.findall(self.patterns["modularity"], code)) * 0.05 # Normalize by code lines (prevent longer files from dominating) normalize_factor = min(code_lines / 20, 1.0) # Baseline 20 lines @@ -132,29 +134,29 @@ def analyze_code(self, code: str, filename: str = 'unknown') -> Dict[str, Any]: semantic_type = self._classify_code_type(code) return { - 'ljpw_scores': (L, J, P, W), - 'semantic_type': semantic_type, - 'metadata': { - 'filename': filename, - 'lines': code_lines, - 'total_lines': total_lines, - } + "ljpw_scores": (L, J, P, W), + "semantic_type": semantic_type, + "metadata": { + "filename": filename, + "lines": code_lines, + "total_lines": total_lines, + }, } def _classify_code_type(self, code: str) -> str: """Classify what type of code this is""" - if re.search(r'class \w+', code): - return 'class' - elif re.search(r'def \w+', code): - return 'function' - elif re.search(r'interface|protocol|ABC', code, re.IGNORECASE): - return 'interface' - elif re.search(r'try|except|catch', code, re.IGNORECASE): - return 'error_handler' - elif re.search(r'validate|check|verify', code, re.IGNORECASE): - return 'validator' + if re.search(r"class \w+", code): + return "class" + elif re.search(r"def \w+", code): + return "function" + elif re.search(r"interface|protocol|ABC", code, re.IGNORECASE): + return "interface" + elif re.search(r"try|except|catch", code, re.IGNORECASE): + return "error_handler" + elif re.search(r"validate|check|verify", code, re.IGNORECASE): + return "validator" else: - return 'module' + return "module" def analyze_codebase(self, code_files: List[Tuple[str, str]]) -> List[Dict[str, Any]]: """ @@ -172,10 +174,12 @@ def analyze_codebase(self, code_files: List[Tuple[str, str]]) -> List[Dict[str, results.append(result) return results + # ============================================================================ # LJPW REASONER: Analyzes compressed genomes # ============================================================================ + class LJPWReasoner: """ The AI reasoning component that operates in compressed LJPW space @@ -194,7 +198,7 @@ def analyze_genome(self, compressed_units: List[CompressedSemanticUnit]) -> Dict # Calculate statistics n = len(compressed_units) if n == 0: - return {'insights': [], 'recommendations': []} + return {"insights": [], "recommendations": []} # Aggregate LJPW scores total_L, total_J, total_P, total_W = 0, 0, 0, 0 @@ -221,72 +225,89 @@ def analyze_genome(self, compressed_units: List[CompressedSemanticUnit]) -> Dict # Insight 1: Safety analysis if avg_L < 0.5: - insights.append({ - 'type': 'CRITICAL', - 'category': 'Safety', - 'message': f'Low safety score ({avg_L:.2f}). System lacks error handling.', - 'impact': 'HIGH' - }) - recommendations.append('Add comprehensive error handling and validation') + insights.append( + { + "type": "CRITICAL", + "category": "Safety", + "message": f"Low safety score ({avg_L:.2f}). System lacks error handling.", + "impact": "HIGH", + } + ) + recommendations.append("Add comprehensive error handling and validation") # Insight 2: Structure analysis if avg_J < 0.4: - insights.append({ - 'type': 'WARNING', - 'category': 'Structure', - 'message': f'Low structure score ({avg_J:.2f}). Code lacks clear contracts.', - 'impact': 'MEDIUM' - }) - recommendations.append('Define clear interfaces and type contracts') + insights.append( + { + "type": "WARNING", + "category": "Structure", + "message": f"Low structure score ({avg_J:.2f}). Code lacks clear contracts.", + "impact": "MEDIUM", + } + ) + recommendations.append("Define clear interfaces and type contracts") # Insight 3: Power/Wisdom balance if avg_P > 0.7 and avg_W < 0.5: - insights.append({ - 'type': 'WARNING', - 'category': 'Balance', - 'message': 'High power, low wisdom - risk of unmaintainable complexity.', - 'impact': 'HIGH' - }) - recommendations.append('Refactor complex code into well-designed modules') + insights.append( + { + "type": "WARNING", + "category": "Balance", + "message": "High power, low wisdom - risk of unmaintainable complexity.", + "impact": "HIGH", + } + ) + recommendations.append("Refactor complex code into well-designed modules") # Insight 4: Primitive distribution safety_primitives = sum( - count for prim, count in primitive_distribution.items() - if prim in [SemanticPrimitive.SAFE_INIT, SemanticPrimitive.ERROR_HANDLE, - SemanticPrimitive.VALIDATION] + count + for prim, count in primitive_distribution.items() + if prim + in [ + SemanticPrimitive.SAFE_INIT, + SemanticPrimitive.ERROR_HANDLE, + SemanticPrimitive.VALIDATION, + ] ) if safety_primitives / n < 0.2: - insights.append({ - 'type': 'INFO', - 'category': 'Distribution', - 'message': f'Only {100*safety_primitives/n:.1f}% safety primitives detected.', - 'impact': 'MEDIUM' - }) + insights.append( + { + "type": "INFO", + "category": "Distribution", + "message": f"Only {100*safety_primitives/n:.1f}% safety primitives detected.", + "impact": "MEDIUM", + } + ) # Insight 5: Distance from Natural Equilibrium NE = (0.618, 0.414, 0.718, 0.693) distance = math.sqrt( - (NE[0] - avg_L)**2 + (NE[1] - avg_J)**2 + - (NE[2] - avg_P)**2 + (NE[3] - avg_W)**2 + (NE[0] - avg_L) ** 2 + + (NE[1] - avg_J) ** 2 + + (NE[2] - avg_P) ** 2 + + (NE[3] - avg_W) ** 2 ) if distance > 0.5: - insights.append({ - 'type': 'WARNING', - 'category': 'System Health', - 'message': f'System far from optimal ({distance:.2f} from NE).', - 'impact': 'HIGH' - }) - recommendations.append('Systematic refactoring needed to improve balance') + insights.append( + { + "type": "WARNING", + "category": "System Health", + "message": f"System far from optimal ({distance:.2f} from NE).", + "impact": "HIGH", + } + ) + recommendations.append("Systematic refactoring needed to improve balance") return { - 'average_ljpw': (avg_L, avg_J, avg_P, avg_W), - 'distance_from_ne': distance, - 'primitive_distribution': primitive_distribution, - 'insights': insights, - 'recommendations': recommendations, - 'health_score': max(0, 1.0 - distance / 2), # 0-1 score + "average_ljpw": (avg_L, avg_J, avg_P, avg_W), + "distance_from_ne": distance, + "primitive_distribution": primitive_distribution, + "insights": insights, + "recommendations": recommendations, + "health_score": max(0, 1.0 - distance / 2), # 0-1 score } def _dequantize(self, state: Tuple[int, int, int, int]) -> Tuple[float, float, float, float]: @@ -299,10 +320,12 @@ def dequant(level): return (dequant(L_q), dequant(J_q), dequant(P_q), dequant(W_q)) + # ============================================================================ # COMPLETE PIPELINE # ============================================================================ + class LJPWPipeline: """ Complete LJPW analysis pipeline @@ -316,10 +339,12 @@ def __init__(self): self.reasoner = LJPWReasoner() self.expander = SemanticExpander() - def analyze_codebase(self, - code_files: List[Tuple[str, str]], - generate_docs: bool = True, - generate_improvement_plan: bool = True) -> Dict[str, Any]: + def analyze_codebase( + self, + code_files: List[Tuple[str, str]], + generate_docs: bool = True, + generate_improvement_plan: bool = True, + ) -> Dict[str, Any]: """ Run complete pipeline on a codebase @@ -360,13 +385,13 @@ def analyze_codebase(self, print("-" * 70) reasoning_results = self.reasoner.analyze_genome(compressed_genome) - avg_L, avg_J, avg_P, avg_W = reasoning_results['average_ljpw'] + avg_L, avg_J, avg_P, avg_W = reasoning_results["average_ljpw"] print(f"Average LJPW: L={avg_L:.2f}, J={avg_J:.2f}, P={avg_P:.2f}, W={avg_W:.2f}") print(f"Health Score: {reasoning_results['health_score']:.2%}") print(f"Distance from NE: {reasoning_results['distance_from_ne']:.3f}") print(f"\nInsights generated: {len(reasoning_results['insights'])}") - for insight in reasoning_results['insights']: + for insight in reasoning_results["insights"]: print(f" [{insight['type']}] {insight['message']}") # STEP 4: Expand to outputs @@ -377,33 +402,32 @@ def analyze_codebase(self, if generate_docs: documentation = self.expander.expand_to_documentation( - compressed_genome, - context={'system_name': 'Analyzed Codebase'} + compressed_genome, context={"system_name": "Analyzed Codebase"} ) - outputs['documentation'] = documentation + outputs["documentation"] = documentation print(f"Generated documentation: {len(documentation)} characters") if generate_improvement_plan: improvement_plan = self.expander.expand_to_improvement_plan(compressed_genome) - outputs['improvement_plan'] = improvement_plan + outputs["improvement_plan"] = improvement_plan print(f"Generated improvement plan: {len(improvement_plan)} characters") # Compile final results results = { - 'analysis': analysis_results, - 'compressed_genome': compressed_genome, - 'compression_ratio': compression_ratio, - 'reasoning': reasoning_results, - 'outputs': outputs, - 'statistics': { - 'total_files': len(code_files), - 'total_code_size': original_size, - 'compressed_size': compressed_size, - 'compression_ratio': compression_ratio, - 'semantic_units': len(compressed_genome), - 'average_ljpw': (avg_L, avg_J, avg_P, avg_W), - 'health_score': reasoning_results['health_score'], - } + "analysis": analysis_results, + "compressed_genome": compressed_genome, + "compression_ratio": compression_ratio, + "reasoning": reasoning_results, + "outputs": outputs, + "statistics": { + "total_files": len(code_files), + "total_code_size": original_size, + "compressed_size": compressed_size, + "compression_ratio": compression_ratio, + "semantic_units": len(compressed_genome), + "average_ljpw": (avg_L, avg_J, avg_P, avg_W), + "health_score": reasoning_results["health_score"], + }, } print(f"\n{'='*70}") @@ -412,19 +436,22 @@ def analyze_codebase(self, return results + # ============================================================================ # DEMONSTRATION # ============================================================================ -if __name__ == '__main__': - print("="*70) +if __name__ == "__main__": + print("=" * 70) print("LJPW COMPLETE ANALYSIS PIPELINE v1.0") print("Full Integration: Analyze -> Compress -> Reason -> Expand") - print("="*70) + print("=" * 70) # Create sample codebase sample_codebase = [ - ("data_processor.py", """ + ( + "data_processor.py", + """ class DataProcessor: '''Process data with validation and error handling''' @@ -461,9 +488,11 @@ def validate_item(self, item) -> bool: def transform(self, item): '''Transform item''' return item -"""), - - ("algorithm.py", """ +""", + ), + ( + "algorithm.py", + """ def binary_search(arr: list, target: int) -> int: ''' Binary search algorithm @@ -495,9 +524,11 @@ def quick_sort(arr: list) -> list: right = [x for x in arr if x > pivot] return quick_sort(left) + middle + quick_sort(right) -"""), - - ("interface.py", """ +""", + ), + ( + "interface.py", + """ from abc import ABC, abstractmethod class DataSource(ABC): @@ -544,7 +575,8 @@ def _fetch_from_source(self, query: str) -> list: def disconnect(self) -> None: self.connected = False self.cache.clear() -"""), +""", + ), ] # Run pipeline @@ -552,16 +584,18 @@ def disconnect(self) -> None: results = pipeline.analyze_codebase(sample_codebase) # Display results - print("\n" + "="*70) + print("\n" + "=" * 70) print("RESULTS SUMMARY") - print("="*70) + print("=" * 70) - stats = results['statistics'] - print(f"\nCompression: {stats['total_code_size']} -> {stats['compressed_size']} bytes ({stats['compression_ratio']:.1f}x)") + stats = results["statistics"] + print( + f"\nCompression: {stats['total_code_size']} -> {stats['compressed_size']} bytes ({stats['compression_ratio']:.1f}x)" + ) print(f"Semantic Units: {stats['semantic_units']}") print(f"Health Score: {stats['health_score']:.1%}") - L, J, P, W = stats['average_ljpw'] + L, J, P, W = stats["average_ljpw"] print(f"\nAverage LJPW:") print(f" Love (Safety): {L:.3f}") print(f" Justice (Structure): {J:.3f}") @@ -569,31 +603,31 @@ def disconnect(self) -> None: print(f" Wisdom (Design): {W:.3f}") print(f"\nKey Insights:") - for insight in results['reasoning']['insights'][:5]: + for insight in results["reasoning"]["insights"][:5]: print(f" [{insight['type']}] {insight['message']}") print(f"\nRecommendations:") - for rec in results['reasoning']['recommendations'][:3]: + for rec in results["reasoning"]["recommendations"][:3]: print(f" - {rec}") # Save outputs - if 'documentation' in results['outputs']: + if "documentation" in results["outputs"]: print(f"\n{'='*70}") print("GENERATED DOCUMENTATION") - print("="*70) - print(results['outputs']['documentation'][:800]) + print("=" * 70) + print(results["outputs"]["documentation"][:800]) print("\n... (truncated)") - if 'improvement_plan' in results['outputs']: + if "improvement_plan" in results["outputs"]: print(f"\n{'='*70}") print("IMPROVEMENT PLAN") - print("="*70) - print(results['outputs']['improvement_plan'][:800]) + print("=" * 70) + print(results["outputs"]["improvement_plan"][:800]) print("\n... (truncated)") print(f"\n{'='*70}") print("PIPELINE DEMONSTRATION COMPLETE") - print("="*70) + print("=" * 70) print("\nThe LJPW Pipeline successfully:") print(" [OK] Analyzed real Python code") print(" [OK] Compressed to semantic genome") diff --git a/src/ljpw/ljpw_semantic_compiler.py b/src/ljpw/ljpw_semantic_compiler.py index 0359c1f..605b912 100644 --- a/src/ljpw/ljpw_semantic_compiler.py +++ b/src/ljpw/ljpw_semantic_compiler.py @@ -9,21 +9,23 @@ - Enables AI reasoning in compressed LJPW space (no token limits) """ -import math import json -from typing import List, Tuple, Dict, Any, Optional +import math from dataclasses import dataclass, field from enum import Enum +from typing import Any, Dict, List, Optional, Tuple # ============================================================================ # SEMANTIC PRIMITIVES: The "Amino Acids" of Code # ============================================================================ + class SemanticPrimitive(Enum): """ Fundamental semantic building blocks Like DNA's 20 amino acids, these are the basic units of meaning """ + # Safety primitives (Love-dominant) SAFE_INIT = "safe_initialization" ERROR_HANDLE = "error_handling" @@ -59,10 +61,12 @@ class SemanticPrimitive(Enum): STOP_SEQUENCE = "stop" SEPARATOR = "separator" + # ============================================================================ # LJPW CODON TABLE: Mapping LJPW patterns to semantic primitives # ============================================================================ + class CodonTable: """ Maps LJPW codon patterns to semantic primitives @@ -129,16 +133,19 @@ def get_pattern(self, primitive: SemanticPrimitive) -> Optional[Tuple[int, int, return pattern return None + # ============================================================================ # COMPRESSED REPRESENTATION # ============================================================================ + @dataclass class CompressedSemanticUnit: """ A single compressed unit: semantic primitive + context Much smaller than raw code """ + primitive: SemanticPrimitive ljpw_state: Tuple[int, int, int, int] # Quantized levels metadata: Dict[str, Any] = field(default_factory=dict) @@ -154,10 +161,10 @@ def to_compact_bytes(self) -> bytes: # Pack LJPW into 1 byte (4 levels, 2 bits each) ljpw_byte = ( - (self.ljpw_state[0] << 6) | - (self.ljpw_state[1] << 4) | - (self.ljpw_state[2] << 2) | - (self.ljpw_state[3]) + (self.ljpw_state[0] << 6) + | (self.ljpw_state[1] << 4) + | (self.ljpw_state[2] << 2) + | (self.ljpw_state[3]) ) return bytes([prim_byte, ljpw_byte]) @@ -178,10 +185,12 @@ def from_compact_bytes(cls, data: bytes, primitive_map: Dict): return cls(primitive=primitive, ljpw_state=(L, J, P, W)) + # ============================================================================ # ADVANCED SEMANTIC COMPRESSOR # ============================================================================ + class AdvancedSemanticCompressor: """ Production-grade semantic compressor for solving token limits @@ -200,8 +209,9 @@ class AdvancedSemanticCompressor: def __init__(self): self.codon_table = CodonTable() - def compress_codebase_analysis(self, - analysis_results: List[Dict[str, Any]]) -> List[CompressedSemanticUnit]: + def compress_codebase_analysis( + self, analysis_results: List[Dict[str, Any]] + ) -> List[CompressedSemanticUnit]: """ Compress the results of analyzing a codebase @@ -217,7 +227,7 @@ def compress_codebase_analysis(self, compressed = [] for result in analysis_results: - L, J, P, W = result['ljpw_scores'] + L, J, P, W = result["ljpw_scores"] # Quantize to 0-3 levels L_q = self._quantize(L) @@ -237,7 +247,7 @@ def compress_codebase_analysis(self, unit = CompressedSemanticUnit( primitive=primitive, ljpw_state=pattern, - metadata={'original_type': result.get('semantic_type', 'unknown')} + metadata={"original_type": result.get("semantic_type", "unknown")}, ) compressed.append(unit) @@ -266,22 +276,23 @@ def _find_nearest_primitive(self, pattern: Tuple[int, int, int, int]) -> Semanti else: return SemanticPrimitive.ABSTRACTION - def calculate_compression_ratio(self, - original_size_bytes: int, - compressed_units: List[CompressedSemanticUnit]) -> float: + def calculate_compression_ratio( + self, original_size_bytes: int, compressed_units: List[CompressedSemanticUnit] + ) -> float: """Calculate achieved compression ratio""" compressed_size = len(compressed_units) * 2 # 2 bytes per unit return original_size_bytes / compressed_size + # ============================================================================ # DEMONSTRATION: Solving the Token Limit Problem # ============================================================================ -if __name__ == '__main__': - print("="*70) +if __name__ == "__main__": + print("=" * 70) print("LJPW SEMANTIC COMPILER v2.0") print("Solving the Token Limit Problem via DNA-Inspired Compression") - print("="*70) + print("=" * 70) # Simulate analyzing a large codebase print("\n1. SIMULATING LARGE CODEBASE ANALYSIS") @@ -293,11 +304,18 @@ def calculate_compression_ratio(self, # Generate synthetic analysis results import random + random.seed(42) semantic_types = [ - 'function', 'class', 'module', 'error_handler', - 'validator', 'algorithm', 'interface', 'utility' + "function", + "class", + "module", + "error_handler", + "validator", + "algorithm", + "interface", + "utility", ] for i in range(1000): @@ -308,9 +326,9 @@ def calculate_compression_ratio(self, W = random.uniform(0.3, 0.8) analysis_result = { - 'ljpw_scores': (L, J, P, W), - 'semantic_type': random.choice(semantic_types), - 'metadata': {'function_id': i} + "ljpw_scores": (L, J, P, W), + "semantic_type": random.choice(semantic_types), + "metadata": {"function_id": i}, } large_codebase_analysis.append(analysis_result) @@ -394,9 +412,10 @@ def calculate_compression_ratio(self, # Real-world application print("\n6. REAL-WORLD APPLICATION: THE LJPW CORE LOOP") - print("="*70) + print("=" * 70) - print(""" + print( + """ The LJPW Core Loop for Token-Free AI Reasoning: 1. CONDENSER (This tool): @@ -418,11 +437,12 @@ def calculate_compression_ratio(self, - Output: Improved, production-ready code Result: AI can reason about MASSIVE systems without token limits! - """) + """ + ) - print("\n" + "="*70) + print("\n" + "=" * 70) print("COMPRESSION SUCCESSFUL") - print("="*70) + print("=" * 70) print(f"\nAchieved {compression_ratio:.1f}x compression") print(f"Token limit problem: SOLVED [YES]") print(f"DNA-LJPW correspondence: VALIDATED [YES]") diff --git a/src/ljpw/ljpw_semantic_compressor.py b/src/ljpw/ljpw_semantic_compressor.py index 36da0fc..2b4319d 100644 --- a/src/ljpw/ljpw_semantic_compressor.py +++ b/src/ljpw/ljpw_semantic_compressor.py @@ -9,43 +9,47 @@ - Error correction via pairing checksums """ -import math import json -from typing import List, Tuple, Dict, Any +import math from dataclasses import dataclass from enum import Enum +from typing import Any, Dict, List, Tuple # ============================================================================ # FOUNDATIONAL CONSTANTS # ============================================================================ + class LJPWBase(Enum): """The 4 'bases' of LJPW semantic DNA""" + L = 0.618034 # Love - Force multiplier (like G: strong binder) J = 0.414214 # Justice - Vulnerable to erosion (like T: weak binder) P = 0.718282 # Power - Catalytic change (like A: flexible) W = 0.693147 # Wisdom - Stable structure (like C: G's partner) + # Complementary pairing (discovered from analysis) COMPLEMENTARY_PAIRS = { - 'L': 'W', # Love ↔ Wisdom (stable pair, like G-C) - 'W': 'L', - 'P': 'J', # Power ↔ Justice (dynamic pair, like A-T) - 'J': 'P', + "L": "W", # Love ↔ Wisdom (stable pair, like G-C) + "W": "L", + "P": "J", # Power ↔ Justice (dynamic pair, like A-T) + "J": "P", } # Natural Equilibrium - the "reference genome" NATURAL_EQUILIBRIUM = { - 'L': 0.618034, - 'J': 0.414214, - 'P': 0.718282, - 'W': 0.693147, + "L": 0.618034, + "J": 0.414214, + "P": 0.718282, + "W": 0.693147, } # ============================================================================ # CONFIGURATION CONSTANTS # ============================================================================ + class CompressionConfig: """ Configuration constants for semantic compression @@ -69,14 +73,17 @@ class CompressionConfig: This limits us to levels 0-9, which caps quantization at 10 levels. For 16+ levels, we'd need to switch to two-digit encoding. """ - LJPW_MAX_VALUE = 1.5 # Max LJPW value due to coupling + + LJPW_MAX_VALUE = 1.5 # Max LJPW value due to coupling ERROR_CORRECTION_THRESHOLD = 0.1 # 10% tolerance for checksums - MAX_LEVEL_SINGLE_DIGIT = 9 # Single-digit encoding limit + MAX_LEVEL_SINGLE_DIGIT = 9 # Single-digit encoding limit + # ============================================================================ # QUANTIZATION: Converting continuous values to discrete bases # ============================================================================ + class LJPWQuantizer: """ Quantizes continuous LJPW values into discrete 'bases' @@ -99,10 +106,10 @@ class LJPWQuantizer: # Recommended levels by use case RECOMMENDATIONS = { - 'fast': 4, # Quick analysis, token savings priority - 'balanced': 8, # Good balance of size and accuracy - 'precise': 16, # Research/scientific applications - 'exact': 32, # Maximum precision while still compressed + "fast": 4, # Quick analysis, token savings priority + "balanced": 8, # Good balance of size and accuracy + "precise": 16, # Research/scientific applications + "exact": 32, # Maximum precision while still compressed } def __init__(self, levels=4): @@ -118,9 +125,7 @@ def __init__(self, levels=4): """ # Validate input type if not isinstance(levels, int): - raise TypeError( - f"Quantization levels must be an integer, got {type(levels).__name__}" - ) + raise TypeError(f"Quantization levels must be an integer, got {type(levels).__name__}") # Validate levels value if levels not in self.VALID_LEVELS: @@ -134,7 +139,7 @@ def __init__(self, levels=4): self.thresholds = [i / levels for i in range(levels + 1)] @classmethod - def recommend_levels(cls, use_case: str = 'balanced') -> int: + def recommend_levels(cls, use_case: str = "balanced") -> int: """ Get recommended quantization level for a use case @@ -154,10 +159,8 @@ def recommend_levels(cls, use_case: str = 'balanced') -> int: 16 """ if use_case not in cls.RECOMMENDATIONS: - valid = ', '.join(f"'{k}'" for k in cls.RECOMMENDATIONS.keys()) - raise ValueError( - f"Unknown use case '{use_case}'. Valid options: {valid}" - ) + valid = ", ".join(f"'{k}'" for k in cls.RECOMMENDATIONS.keys()) + raise ValueError(f"Unknown use case '{use_case}'. Valid options: {valid}") return cls.RECOMMENDATIONS[use_case] def quantize_value(self, value: float, dim: str) -> int: @@ -190,10 +193,12 @@ def dequantize_value(self, level: int, dim: str) -> float: midpoint = (bin_start + bin_end) / 2 return midpoint * CompressionConfig.LJPW_MAX_VALUE # Scale back to LJPW range + # ============================================================================ # LJPW CODONS: Triplet encoding of semantic primitives # ============================================================================ + @dataclass class LJPWCodon: """ @@ -201,6 +206,7 @@ class LJPWCodon: Like DNA: 3 bases = 1 codon → 1 amino acid LJPW: 3 dimensions = 1 codon → 1 semantic primitive """ + base1: str # L, J, P, or W base2: str base3: str @@ -237,21 +243,15 @@ def from_string(cls, s: str): # Extract bases and levels base1, base2, base3 = s[0], s[2], s[4] - valid_bases = {'L', 'J', 'P', 'W'} + valid_bases = {"L", "J", "P", "W"} # Validate bases if base1 not in valid_bases: - raise ValueError( - f"Invalid base at position 0: '{base1}'. Must be L, J, P, or W" - ) + raise ValueError(f"Invalid base at position 0: '{base1}'. Must be L, J, P, or W") if base2 not in valid_bases: - raise ValueError( - f"Invalid base at position 2: '{base2}'. Must be L, J, P, or W" - ) + raise ValueError(f"Invalid base at position 2: '{base2}'. Must be L, J, P, or W") if base3 not in valid_bases: - raise ValueError( - f"Invalid base at position 4: '{base3}'. Must be L, J, P, or W" - ) + raise ValueError(f"Invalid base at position 4: '{base3}'. Must be L, J, P, or W") # Parse levels try: @@ -274,29 +274,33 @@ def from_string(cls, s: str): ) return cls( - base1=base1, level1=level1, - base2=base2, level2=level2, - base3=base3, level3=level3 + base1=base1, level1=level1, base2=base2, level2=level2, base3=base3, level3=level3 ) - def complement(self) -> 'LJPWCodon': + def complement(self) -> "LJPWCodon": """Return complementary codon using pairing rules""" return LJPWCodon( - base1=COMPLEMENTARY_PAIRS[self.base1], level1=self.level1, - base2=COMPLEMENTARY_PAIRS[self.base2], level2=self.level2, - base3=COMPLEMENTARY_PAIRS[self.base3], level3=self.level3, + base1=COMPLEMENTARY_PAIRS[self.base1], + level1=self.level1, + base2=COMPLEMENTARY_PAIRS[self.base2], + level2=self.level2, + base3=COMPLEMENTARY_PAIRS[self.base3], + level3=self.level3, ) + # ============================================================================ # SEMANTIC GENOME: Sequence of LJPW states # ============================================================================ + @dataclass class SemanticGenome: """ A compressed representation of a system's evolution Like DNA: sequence of codons encoding a phenotype """ + codons: List[LJPWCodon] metadata: Dict[str, Any] @@ -305,27 +309,31 @@ def __len__(self): def to_string(self) -> str: """Compact string representation""" - codon_str = '-'.join(c.to_string() for c in self.codons) + codon_str = "-".join(c.to_string() for c in self.codons) return codon_str def to_json(self) -> str: """JSON representation for storage""" - return json.dumps({ - 'codons': [c.to_string() for c in self.codons], - 'metadata': self.metadata, - }) + return json.dumps( + { + "codons": [c.to_string() for c in self.codons], + "metadata": self.metadata, + } + ) @classmethod def from_json(cls, json_str: str): """Reconstruct from JSON""" data = json.loads(json_str) - codons = [LJPWCodon.from_string(s) for s in data['codons']] - return cls(codons=codons, metadata=data['metadata']) + codons = [LJPWCodon.from_string(s) for s in data["codons"]] + return cls(codons=codons, metadata=data["metadata"]) + # ============================================================================ # SEMANTIC COMPRESSOR: The "Condenser" # ============================================================================ + class SemanticCompressor: """ Compresses LJPW state sequences into compact semantic genomes @@ -339,9 +347,9 @@ class SemanticCompressor: def __init__(self, quantization_levels=4): self.quantizer = LJPWQuantizer(levels=quantization_levels) - def compress_state_sequence(self, - states: List[Tuple[float, float, float, float]], - metadata: Dict = None) -> SemanticGenome: + def compress_state_sequence( + self, states: List[Tuple[float, float, float, float]], metadata: Dict = None + ) -> SemanticGenome: """ Compress a sequence of LJPW states into a semantic genome @@ -366,9 +374,7 @@ def compress_state_sequence(self, # Validate each state for i, state in enumerate(states): if not isinstance(state, (list, tuple)): - raise TypeError( - f"State {i} must be a tuple/list, got {type(state).__name__}" - ) + raise TypeError(f"State {i} must be a tuple/list, got {type(state).__name__}") if len(state) != 4: raise ValueError( @@ -376,19 +382,15 @@ def compress_state_sequence(self, ) # Validate each dimension - for j, (val, dim) in enumerate(zip(state, ['L', 'J', 'P', 'W'])): + for j, (val, dim) in enumerate(zip(state, ["L", "J", "P", "W"])): if not isinstance(val, (int, float)): raise TypeError( f"State {i}, dimension {dim}: expected numeric value, got {type(val).__name__}" ) if math.isnan(val): - raise ValueError( - f"State {i}, dimension {dim}: NaN is not allowed" - ) + raise ValueError(f"State {i}, dimension {dim}: NaN is not allowed") if math.isinf(val): - raise ValueError( - f"State {i}, dimension {dim}: Infinity is not allowed" - ) + raise ValueError(f"State {i}, dimension {dim}: Infinity is not allowed") if val < 0: raise ValueError( f"State {i}, dimension {dim}: negative value {val} is not allowed" @@ -400,39 +402,45 @@ def compress_state_sequence(self, L, J, P, W = state # Quantize each dimension - L_level = self.quantizer.quantize_value(L, 'L') - J_level = self.quantizer.quantize_value(J, 'J') - P_level = self.quantizer.quantize_value(P, 'P') - W_level = self.quantizer.quantize_value(W, 'W') + L_level = self.quantizer.quantize_value(L, "L") + J_level = self.quantizer.quantize_value(J, "J") + P_level = self.quantizer.quantize_value(P, "P") + W_level = self.quantizer.quantize_value(W, "W") # Encode as codon (using LJP as the triplet, W stored separately) # We'll use a 4-base encoding strategy codon = LJPWCodon( - base1='L', level1=L_level, - base2='J', level2=J_level, - base3='P', level3=P_level, + base1="L", + level1=L_level, + base2="J", + level2=J_level, + base3="P", + level3=P_level, ) codons.append(codon) # Store W in a complementary codon for error correction w_codon = LJPWCodon( - base1='W', level1=W_level, - base2='L', level2=L_level, # Checksum: L-W pairing - base3='P', level3=P_level, # Checksum: P-J pairing + base1="W", + level1=W_level, + base2="L", + level2=L_level, # Checksum: L-W pairing + base3="P", + level3=P_level, # Checksum: P-J pairing ) codons.append(w_codon) if metadata is None: metadata = {} - metadata['original_length'] = len(states) - metadata['compression_ratio'] = self._calculate_compression_ratio(states, codons) + metadata["original_length"] = len(states) + metadata["compression_ratio"] = self._calculate_compression_ratio(states, codons) return SemanticGenome(codons=codons, metadata=metadata) - def _calculate_compression_ratio(self, - original_states: List[Tuple], - codons: List[LJPWCodon]) -> float: + def _calculate_compression_ratio( + self, original_states: List[Tuple], codons: List[LJPWCodon] + ) -> float: """ Calculate compression ratio accurately @@ -445,7 +453,7 @@ def _calculate_compression_ratio(self, # Compressed size: actual string representation # Build the genome string to get exact size - genome_string = '-'.join(c.to_string() for c in codons) + genome_string = "-".join(c.to_string() for c in codons) compressed_bytes = len(genome_string) # Avoid division by zero @@ -454,10 +462,12 @@ def _calculate_compression_ratio(self, return original_bytes / compressed_bytes + # ============================================================================ # SEMANTIC DECOMPRESSOR: The "Expander" # ============================================================================ + class SemanticDecompressor: """ Decompresses semantic genomes back to LJPW state sequences @@ -497,14 +507,14 @@ def decompress_genome(self, genome: SemanticGenome) -> List[Tuple[float, float, w_codon = genome.codons[i + 1] # Dequantize values - L = self.quantizer.dequantize_value(main_codon.level1, 'L') - J = self.quantizer.dequantize_value(main_codon.level2, 'J') - P = self.quantizer.dequantize_value(main_codon.level3, 'P') - W = self.quantizer.dequantize_value(w_codon.level1, 'W') + L = self.quantizer.dequantize_value(main_codon.level1, "L") + J = self.quantizer.dequantize_value(main_codon.level2, "J") + P = self.quantizer.dequantize_value(main_codon.level3, "P") + W = self.quantizer.dequantize_value(w_codon.level1, "W") # Error correction: verify checksums - L_check = self.quantizer.dequantize_value(w_codon.level2, 'L') - P_check = self.quantizer.dequantize_value(w_codon.level3, 'P') + L_check = self.quantizer.dequantize_value(w_codon.level2, "L") + P_check = self.quantizer.dequantize_value(w_codon.level3, "P") # If checksums don't match, use average (simple error correction) if abs(L - L_check) > CompressionConfig.ERROR_CORRECTION_THRESHOLD: @@ -526,10 +536,10 @@ def validate_genome(self, genome: SemanticGenome) -> Dict[str, Any]: # Handle empty genome if len(genome.codons) == 0: return { - 'valid': True, # Empty genome is technically valid - 'error_count': 0, - 'errors': [], - 'integrity_score': 1.0, + "valid": True, # Empty genome is technically valid + "error_count": 0, + "errors": [], + "integrity_score": 1.0, } errors = [] @@ -552,21 +562,22 @@ def validate_genome(self, genome: SemanticGenome) -> Dict[str, Any]: integrity_score = 1.0 - (len(errors) / num_states) if num_states > 0 else 1.0 return { - 'valid': len(errors) == 0, - 'error_count': len(errors), - 'errors': errors[:10], # First 10 errors - 'integrity_score': integrity_score, + "valid": len(errors) == 0, + "error_count": len(errors), + "errors": errors[:10], # First 10 errors + "integrity_score": integrity_score, } + # ============================================================================ # DEMONSTRATION # ============================================================================ -if __name__ == '__main__': - print("="*70) +if __name__ == "__main__": + print("=" * 70) print("LJPW SEMANTIC COMPRESSOR v1.0") print("DNA-Inspired Semantic Compression System") - print("="*70) + print("=" * 70) # Example: Compress a system's evolution trajectory print("\n1. CREATING SAMPLE SYSTEM TRAJECTORY") @@ -575,7 +586,7 @@ def validate_genome(self, genome: SemanticGenome) -> Dict[str, Any]: # Simulate a project evolving from poor state to Natural Equilibrium trajectory = [ (0.2, 0.3, 0.9, 0.2), # Initial: Low L,J,W, High P (reckless power) - (0.3, 0.35, 0.85, 0.3), # Starting to improve + (0.3, 0.35, 0.85, 0.3), # Starting to improve (0.4, 0.38, 0.80, 0.4), (0.5, 0.40, 0.75, 0.5), (0.55, 0.41, 0.73, 0.6), @@ -584,8 +595,12 @@ def validate_genome(self, genome: SemanticGenome) -> Dict[str, Any]: ] print(f"Trajectory: {len(trajectory)} states") - print(f"Initial state: L={trajectory[0][0]}, J={trajectory[0][1]}, P={trajectory[0][2]}, W={trajectory[0][3]}") - print(f"Final state: L={trajectory[-1][0]}, J={trajectory[-1][1]}, P={trajectory[-1][2]}, W={trajectory[-1][3]}") + print( + f"Initial state: L={trajectory[0][0]}, J={trajectory[0][1]}, P={trajectory[0][2]}, W={trajectory[0][3]}" + ) + print( + f"Final state: L={trajectory[-1][0]}, J={trajectory[-1][1]}, P={trajectory[-1][2]}, W={trajectory[-1][3]}" + ) # Compress print("\n2. COMPRESSING TO SEMANTIC GENOME") @@ -593,8 +608,7 @@ def validate_genome(self, genome: SemanticGenome) -> Dict[str, Any]: compressor = SemanticCompressor(quantization_levels=4) genome = compressor.compress_state_sequence( - trajectory, - metadata={'system': 'Example Project', 'domain': 'software'} + trajectory, metadata={"system": "Example Project", "domain": "software"} ) print(f"Compressed genome length: {len(genome)} codons") @@ -605,7 +619,9 @@ def validate_genome(self, genome: SemanticGenome) -> Dict[str, Any]: # Show first few codons print(f"\nFirst 4 codons (decoded):") for i, codon in enumerate(genome.codons[:4]): - print(f" Codon {i}: {codon.to_string()} (bases: {codon.base1}-{codon.base2}-{codon.base3})") + print( + f" Codon {i}: {codon.to_string()} (bases: {codon.base1}-{codon.base2}-{codon.base3})" + ) # Decompress print("\n3. DECOMPRESSING GENOME") @@ -616,10 +632,18 @@ def validate_genome(self, genome: SemanticGenome) -> Dict[str, Any]: print(f"Reconstructed {len(reconstructed)} states") print(f"\nOriginal vs Reconstructed (first and last states):") - print(f"Original[0]: L={trajectory[0][0]:.3f}, J={trajectory[0][1]:.3f}, P={trajectory[0][2]:.3f}, W={trajectory[0][3]:.3f}") - print(f"Reconstructed[0]: L={reconstructed[0][0]:.3f}, J={reconstructed[0][1]:.3f}, P={reconstructed[0][2]:.3f}, W={reconstructed[0][3]:.3f}") - print(f"\nOriginal[-1]: L={trajectory[-1][0]:.3f}, J={trajectory[-1][1]:.3f}, P={trajectory[-1][2]:.3f}, W={trajectory[-1][3]:.3f}") - print(f"Reconstructed[-1]: L={reconstructed[-1][0]:.3f}, J={reconstructed[-1][1]:.3f}, P={reconstructed[-1][2]:.3f}, W={reconstructed[-1][3]:.3f}") + print( + f"Original[0]: L={trajectory[0][0]:.3f}, J={trajectory[0][1]:.3f}, P={trajectory[0][2]:.3f}, W={trajectory[0][3]:.3f}" + ) + print( + f"Reconstructed[0]: L={reconstructed[0][0]:.3f}, J={reconstructed[0][1]:.3f}, P={reconstructed[0][2]:.3f}, W={reconstructed[0][3]:.3f}" + ) + print( + f"\nOriginal[-1]: L={trajectory[-1][0]:.3f}, J={trajectory[-1][1]:.3f}, P={trajectory[-1][2]:.3f}, W={trajectory[-1][3]:.3f}" + ) + print( + f"Reconstructed[-1]: L={reconstructed[-1][0]:.3f}, J={reconstructed[-1][1]:.3f}, P={reconstructed[-1][2]:.3f}, W={reconstructed[-1][3]:.3f}" + ) # Calculate reconstruction error print("\n4. RECONSTRUCTION ACCURACY") @@ -627,7 +651,7 @@ def validate_genome(self, genome: SemanticGenome) -> Dict[str, Any]: total_error = 0 for orig, recon in zip(trajectory, reconstructed): - error = math.sqrt(sum((o - r)**2 for o, r in zip(orig, recon))) + error = math.sqrt(sum((o - r) ** 2 for o, r in zip(orig, recon))) total_error += error avg_error = total_error / len(trajectory) @@ -664,15 +688,15 @@ def validate_genome(self, genome: SemanticGenome) -> Dict[str, Any]: roundtrip_error = 0 for orig, recon in zip(trajectory, reconstructed2): - error = math.sqrt(sum((o - r)**2 for o, r in zip(orig, recon))) + error = math.sqrt(sum((o - r) ** 2 for o, r in zip(orig, recon))) roundtrip_error += error print(f"Round-trip successful: {len(reconstructed2) == len(trajectory)}") print(f"Round-trip error: {roundtrip_error / len(trajectory):.4f}") - print("\n" + "="*70) + print("\n" + "=" * 70) print("COMPRESSION SUCCESSFUL") - print("="*70) + print("=" * 70) print(f"\nKey Statistics:") print(f" - Compression ratio: {genome.metadata['compression_ratio']:.2f}x") print(f" - Reconstruction accuracy: {100*(1-avg_error):.1f}%") diff --git a/src/ljpw/ljpw_standalone.py b/src/ljpw/ljpw_standalone.py index c322856..c492d06 100644 --- a/src/ljpw/ljpw_standalone.py +++ b/src/ljpw/ljpw_standalone.py @@ -19,12 +19,12 @@ MIT License - Free for all, forever """ +import json import math import re -import json import sys from pathlib import Path -from typing import List, Tuple, Dict, Any, Optional +from typing import Any, Dict, List, Optional, Tuple # ============================================================================ # CORE CONSTANTS: THE SEMANTIC SUBSTRATE @@ -32,7 +32,7 @@ # The Natural Equilibrium: Where Absolute Principles settle in our reality # These are the "signatures" of the Four Fundamental Semantic Principles -# +# # NOTE: The Anchor Point (1,1,1,1) is the Source - Perfect Meaning itself. # Natural Equilibrium represents how these Principles manifest in finite reality. # The "gap" (1.0 → NE value) is the "Cost of Existence." @@ -42,54 +42,52 @@ # Mathematical Shadow: φ⁻¹ (golden ratio inverse) # Physical Shadow: Gravity, Electromagnetism # Semantic Meaning: The force that binds separate entities into wholes - 'L': 0.618034, - + "L": 0.618034, # JUSTICE (J): The Principle of Balance & Truth # Mathematical Shadow: √2-1 (the diagonal constant) # Physical Shadow: Pauli Exclusion Principle # Semantic Meaning: The constraint that creates structure and prevents chaos - 'J': 0.414214, - + "J": 0.414214, # POWER (P): The Principle of Energy & Existence # Mathematical Shadow: e-2 (Euler's constant offset) # Physical Shadow: Thermodynamics, Arrow of Time # Semantic Meaning: The raw capacity to be, to act, and to change state - 'P': 0.718282, - + "P": 0.718282, # WISDOM (W): The Principle of Complexity & Insight # Mathematical Shadow: ln(2) (the fundamental unit of entropy) # Physical Shadow: Quantum Mechanics, Information Theory # Semantic Meaning: The ability to process information and self-reflect - 'W': 0.693147, + "W": 0.693147, } # ============================================================================ # CODE ANALYZER # ============================================================================ + class SimpleCodeAnalyzer: """Lightweight code analyzer for LJPW scoring""" def __init__(self) -> None: self.patterns: Dict[str, str] = { # Love (Safety) - 'error_handling': r'(try|except|catch|Result|Option|error|Error)', - 'validation': r'(validate|check|verify|assert|require)', - 'null_safety': r'(Optional|Maybe|\?\.|if.*is not None)', - 'bounds_check': r'(len\(|\.length|bounds|range)', + "error_handling": r"(try|except|catch|Result|Option|error|Error)", + "validation": r"(validate|check|verify|assert|require)", + "null_safety": r"(Optional|Maybe|\?\.|if.*is not None)", + "bounds_check": r"(len\(|\.length|bounds|range)", # Justice (Structure) - 'type_annotations': r'(:\s*\w+|<\w+>|implements|interface)', - 'documentation': r'("""|\'\'\' |/\*\*|///)', + "type_annotations": r"(:\s*\w+|<\w+>|implements|interface)", + "documentation": r'("""|\'\'\' |/\*\*|///)', # Power (Performance) - 'algorithms': r'(sort|search|binary|hash|cache|optimize)', - 'async': r'(async|await|promise|thread|parallel)', + "algorithms": r"(sort|search|binary|hash|cache|optimize)", + "async": r"(async|await|promise|thread|parallel)", # Wisdom (Design) - 'abstraction': r'(abstract|interface|ABC|protocol)', - 'patterns': r'(factory|singleton|observer|strategy|builder)', - 'modularity': r'(class |def |module|package|namespace)', + "abstraction": r"(abstract|interface|ABC|protocol)", + "patterns": r"(factory|singleton|observer|strategy|builder)", + "modularity": r"(class |def |module|package|namespace)", } - def analyze(self, code: str, filename: str = 'code') -> Dict[str, Any]: + def analyze(self, code: str, filename: str = "code") -> Dict[str, Any]: """ Analyze code and return LJPW scores with comprehensive edge case handling. @@ -110,80 +108,80 @@ def analyze(self, code: str, filename: str = 'code') -> Dict[str, Any]: # Edge case: None input if code is None: return { - 'filename': filename, - 'lines': 0, - 'ljpw': {'L': 0.0, 'J': 0.0, 'P': 0.0, 'W': 0.0}, - 'health': 0.0, - 'insights': ['ERROR: None input provided'], - 'distance_from_ne': self._distance_from_ne(0, 0, 0, 0), - 'error': 'None input' + "filename": filename, + "lines": 0, + "ljpw": {"L": 0.0, "J": 0.0, "P": 0.0, "W": 0.0}, + "health": 0.0, + "insights": ["ERROR: None input provided"], + "distance_from_ne": self._distance_from_ne(0, 0, 0, 0), + "error": "None input", } # Edge case: Invalid type if not isinstance(code, str): return { - 'filename': filename, - 'lines': 0, - 'ljpw': {'L': 0.0, 'J': 0.0, 'P': 0.0, 'W': 0.0}, - 'health': 0.0, - 'insights': [f'ERROR: Expected str, got {type(code).__name__}'], - 'distance_from_ne': self._distance_from_ne(0, 0, 0, 0), - 'error': f'Invalid type: {type(code).__name__}' + "filename": filename, + "lines": 0, + "ljpw": {"L": 0.0, "J": 0.0, "P": 0.0, "W": 0.0}, + "health": 0.0, + "insights": [f"ERROR: Expected str, got {type(code).__name__}"], + "distance_from_ne": self._distance_from_ne(0, 0, 0, 0), + "error": f"Invalid type: {type(code).__name__}", } # Edge case: Extremely large file (>10MB) MAX_SIZE = 10 * 1024 * 1024 # 10MB if len(code) > MAX_SIZE: return { - 'filename': filename, - 'lines': 0, - 'ljpw': {'L': 0.0, 'J': 0.0, 'P': 0.0, 'W': 0.0}, - 'health': 0.0, - 'insights': [f'ERROR: File too large ({len(code)} bytes > {MAX_SIZE} bytes)'], - 'distance_from_ne': self._distance_from_ne(0, 0, 0, 0), - 'error': 'File too large' + "filename": filename, + "lines": 0, + "ljpw": {"L": 0.0, "J": 0.0, "P": 0.0, "W": 0.0}, + "health": 0.0, + "insights": [f"ERROR: File too large ({len(code)} bytes > {MAX_SIZE} bytes)"], + "distance_from_ne": self._distance_from_ne(0, 0, 0, 0), + "error": "File too large", } # Edge case: Binary data detection (lots of null bytes or non-printable chars) try: # Check for excessive null bytes or control characters - null_count = code.count('\x00') + null_count = code.count("\x00") if null_count > len(code) * 0.1: # More than 10% null bytes return { - 'filename': filename, - 'lines': 0, - 'ljpw': {'L': 0.0, 'J': 0.0, 'P': 0.0, 'W': 0.0}, - 'health': 0.0, - 'insights': ['ERROR: Binary data detected (too many null bytes)'], - 'distance_from_ne': self._distance_from_ne(0, 0, 0, 0), - 'error': 'Binary data' + "filename": filename, + "lines": 0, + "ljpw": {"L": 0.0, "J": 0.0, "P": 0.0, "W": 0.0}, + "health": 0.0, + "insights": ["ERROR: Binary data detected (too many null bytes)"], + "distance_from_ne": self._distance_from_ne(0, 0, 0, 0), + "error": "Binary data", } except Exception: pass # Continue if check fails # Process code normally try: - lines = code.split('\n') - code_lines = len([l for l in lines if l.strip() and not l.strip().startswith('#')]) + lines = code.split("\n") + code_lines = len([l for l in lines if l.strip() and not l.strip().startswith("#")]) except Exception as e: return { - 'filename': filename, - 'lines': 0, - 'ljpw': {'L': 0.0, 'J': 0.0, 'P': 0.0, 'W': 0.0}, - 'health': 0.0, - 'insights': [f'ERROR: Failed to parse code: {str(e)}'], - 'distance_from_ne': self._distance_from_ne(0, 0, 0, 0), - 'error': f'Parse error: {str(e)}' + "filename": filename, + "lines": 0, + "ljpw": {"L": 0.0, "J": 0.0, "P": 0.0, "W": 0.0}, + "health": 0.0, + "insights": [f"ERROR: Failed to parse code: {str(e)}"], + "distance_from_ne": self._distance_from_ne(0, 0, 0, 0), + "error": f"Parse error: {str(e)}", } if code_lines == 0: return { - 'filename': filename, - 'lines': 0, - 'ljpw': {'L': 0.0, 'J': 0.0, 'P': 0.0, 'W': 0.0}, - 'health': 0.0, - 'insights': ['Empty file - no code to analyze'], - 'distance_from_ne': self._distance_from_ne(0, 0, 0, 0) + "filename": filename, + "lines": 0, + "ljpw": {"L": 0.0, "J": 0.0, "P": 0.0, "W": 0.0}, + "health": 0.0, + "insights": ["Empty file - no code to analyze"], + "distance_from_ne": self._distance_from_ne(0, 0, 0, 0), } # Score each dimension @@ -199,80 +197,80 @@ def analyze(self, code: str, filename: str = 'code') -> Dict[str, Any]: insights = self._generate_insights(L, J, P, W) return { - 'filename': filename, - 'lines': code_lines, - 'ljpw': {'L': L, 'J': J, 'P': P, 'W': W}, - 'health': health, - 'insights': insights, - 'distance_from_ne': self._distance_from_ne(L, J, P, W) + "filename": filename, + "lines": code_lines, + "ljpw": {"L": L, "J": J, "P": P, "W": W}, + "health": health, + "insights": insights, + "distance_from_ne": self._distance_from_ne(L, J, P, W), } def _score_love(self, code: str, lines: int) -> float: """Score safety features""" score = 0.0 - score += len(re.findall(self.patterns['error_handling'], code, re.I)) * 0.15 - score += len(re.findall(self.patterns['validation'], code, re.I)) * 0.12 - score += len(re.findall(self.patterns['null_safety'], code, re.I)) * 0.10 - + score += len(re.findall(self.patterns["error_handling"], code, re.I)) * 0.15 + score += len(re.findall(self.patterns["validation"], code, re.I)) * 0.12 + score += len(re.findall(self.patterns["null_safety"], code, re.I)) * 0.10 + # Normalize by code size (more forgiving for larger files) size_factor = min(1.0, 0.5 + lines / 50) # Start at 0.5, reach 1.0 at 25 lines - + # Cap at NE_L × 1.5 to stay in reasonable range (0.618 × 1.5 ≈ 0.93) - max_love = NATURAL_EQUILIBRIUM['L'] * 1.5 + max_love = NATURAL_EQUILIBRIUM["L"] * 1.5 return min(score * size_factor, max_love) def _score_justice(self, code: str, lines: int) -> float: """Score structural quality""" score = 0.0 - score += len(re.findall(self.patterns['type_annotations'], code)) * 0.12 - score += len(re.findall(self.patterns['documentation'], code)) * 0.10 - + score += len(re.findall(self.patterns["type_annotations"], code)) * 0.12 + score += len(re.findall(self.patterns["documentation"], code)) * 0.10 + # Bonus for well-structured code (having any structure is good) if score > 0: score += 0.05 # Base bonus for having any structure - + # Normalize by code size size_factor = min(1.0, 0.5 + lines / 50) - + # Cap at NE_J × 2 for better balance (0.414 × 2 ≈ 0.83) - max_justice = NATURAL_EQUILIBRIUM['J'] * 2.0 + max_justice = NATURAL_EQUILIBRIUM["J"] * 2.0 return min(score * size_factor, max_justice) def _score_power(self, code: str, lines: int) -> float: """Score performance considerations""" score = 0.0 - score += len(re.findall(self.patterns['algorithms'], code, re.I)) * 0.15 - score += len(re.findall(self.patterns['async'], code, re.I)) * 0.12 - + score += len(re.findall(self.patterns["algorithms"], code, re.I)) * 0.15 + score += len(re.findall(self.patterns["async"], code, re.I)) * 0.12 + # Normalize by code size size_factor = min(1.0, 0.5 + lines / 50) - + # Cap at NE_P × 1.2 (0.718 × 1.2 ≈ 0.86) - max_power = NATURAL_EQUILIBRIUM['P'] * 1.2 + max_power = NATURAL_EQUILIBRIUM["P"] * 1.2 return min(score * size_factor, max_power) def _score_wisdom(self, code: str, lines: int) -> float: """Score design quality""" score = 0.0 - score += len(re.findall(self.patterns['abstraction'], code, re.I)) * 0.15 - score += len(re.findall(self.patterns['patterns'], code, re.I)) * 0.12 - score += len(re.findall(self.patterns['modularity'], code)) * 0.05 - + score += len(re.findall(self.patterns["abstraction"], code, re.I)) * 0.15 + score += len(re.findall(self.patterns["patterns"], code, re.I)) * 0.12 + score += len(re.findall(self.patterns["modularity"], code)) * 0.05 + # Bonus for having any modularity if score > 0: score += 0.03 # Base bonus for modular code - + # Normalize by code size size_factor = min(1.0, 0.5 + lines / 50) - + # Cap at NE_W × 1.3 (0.693 × 1.3 ≈ 0.90) - max_wisdom = NATURAL_EQUILIBRIUM['W'] * 1.3 + max_wisdom = NATURAL_EQUILIBRIUM["W"] * 1.3 return min(score * size_factor, max_wisdom) def _calculate_health(self, L: float, J: float, P: float, W: float) -> float: """ Calculate overall health score (0-1). - + Uses a more forgiving formula that: - Rewards any positive LJPW values - Considers both distance from NE and absolute magnitude @@ -280,42 +278,40 @@ def _calculate_health(self, L: float, J: float, P: float, W: float) -> float: """ NE = NATURAL_EQUILIBRIUM distance = math.sqrt( - (NE['L'] - L)**2 + (NE['J'] - J)**2 + - (NE['P'] - P)**2 + (NE['W'] - W)**2 + (NE["L"] - L) ** 2 + (NE["J"] - J) ** 2 + (NE["P"] - P) ** 2 + (NE["W"] - W) ** 2 ) - + # Improved health calculation # 1. Distance-based component (proximity to Natural Equilibrium) # Use divisor of 3 instead of 2 to be more forgiving distance_health = max(0, 1.0 - distance / 3.0) - + # 2. Magnitude-based component (having any good practices) # Average of actual values relative to NE magnitude = (L + J + P + W) / 4.0 - ne_magnitude = (NE['L'] + NE['J'] + NE['P'] + NE['W']) / 4.0 + ne_magnitude = (NE["L"] + NE["J"] + NE["P"] + NE["W"]) / 4.0 magnitude_health = min(1.0, magnitude / ne_magnitude) - + # 3. Combine both components (70% distance, 30% magnitude) # This rewards both balance AND absolute quality health = 0.7 * distance_health + 0.3 * magnitude_health - + return max(0, min(1.0, health)) def _distance_from_ne(self, L: float, J: float, P: float, W: float) -> float: """ Calculate semantic distance from Natural Equilibrium. - + This measures how far the code's semantic signature is from the optimal manifestation of the Four Principles in finite reality. - + Note: We measure distance from NE (0.618, 0.414, 0.718, 0.693), not from the Anchor Point (1,1,1,1). The Anchor is the Source; NE is the optimal state for existence in our reality. """ NE = NATURAL_EQUILIBRIUM return math.sqrt( - (NE['L'] - L)**2 + (NE['J'] - J)**2 + - (NE['P'] - P)**2 + (NE['W'] - W)**2 + (NE["L"] - L) ** 2 + (NE["J"] - J) ** 2 + (NE["P"] - P) ** 2 + (NE["W"] - W) ** 2 ) def _generate_insights(self, L: float, J: float, P: float, W: float) -> List[str]: @@ -339,10 +335,12 @@ def _generate_insights(self, L: float, J: float, P: float, W: float) -> List[str return insights if insights else ["Code appears balanced"] + # ============================================================================ # COMMAND LINE INTERFACE # ============================================================================ + def format_result(result: Dict[str, Any]) -> str: """Format analysis result for display""" output = [] @@ -352,13 +350,13 @@ def format_result(result: Dict[str, Any]) -> str: output.append(f"\nLines of code: {result['lines']}") output.append(f"\nLJPW Scores:") - ljpw = result['ljpw'] + ljpw = result["ljpw"] output.append(f" Love (Safety): {ljpw['L']:.3f}") output.append(f" Justice (Structure): {ljpw['J']:.3f}") output.append(f" Power (Performance): {ljpw['P']:.3f}") output.append(f" Wisdom (Design): {ljpw['W']:.3f}") - health_pct = result['health'] * 100 + health_pct = result["health"] * 100 output.append(f"\nHealth Score: {health_pct:.1f}%") if health_pct >= 80: @@ -375,12 +373,13 @@ def format_result(result: Dict[str, Any]) -> str: output.append(f"\nDistance from Natural Equilibrium: {result['distance_from_ne']:.3f}") output.append("\nInsights:") - for insight in result['insights']: + for insight in result["insights"]: output.append(f" - {insight}") output.append("\n" + "=" * 70) - return '\n'.join(output) + return "\n".join(output) + def analyze_file(filepath: str) -> Dict[str, Any]: """ @@ -400,21 +399,21 @@ def analyze_file(filepath: str) -> Dict[str, Any]: - Corrupted files """ # Check file exists - from pathlib import Path from difflib import get_close_matches - + from pathlib import Path + # Security: Resolve path to prevent traversal attacks try: path = Path(filepath).resolve() except (OSError, RuntimeError) as e: return { - 'filename': filepath, - 'error': 'Invalid path', - 'lines': 0, - 'ljpw': {'L': 0, 'J': 0, 'P': 0, 'W': 0}, - 'health': 0, - 'insights': [f'ERROR: Invalid file path: {str(e)}'], - 'distance_from_ne': 0 + "filename": filepath, + "error": "Invalid path", + "lines": 0, + "ljpw": {"L": 0, "J": 0, "P": 0, "W": 0}, + "health": 0, + "insights": [f"ERROR: Invalid file path: {str(e)}"], + "distance_from_ne": 0, } if not path.exists(): @@ -424,47 +423,44 @@ def analyze_file(filepath: str) -> Dict[str, Any]: if parent.exists(): try: similar = get_close_matches( - path.name, - [f.name for f in parent.iterdir() if f.is_file()], - n=3, - cutoff=0.6 + path.name, [f.name for f in parent.iterdir() if f.is_file()], n=3, cutoff=0.6 ) except (OSError, PermissionError): pass - - error_msg = f'ERROR: File not found: {filepath}' + + error_msg = f"ERROR: File not found: {filepath}" if similar: error_msg += f'\n Did you mean: {", ".join(similar)}?' - + return { - 'filename': filepath, - 'error': 'File not found', - 'lines': 0, - 'ljpw': {'L': 0, 'J': 0, 'P': 0, 'W': 0}, - 'health': 0, - 'insights': [error_msg], - 'distance_from_ne': 0 + "filename": filepath, + "error": "File not found", + "lines": 0, + "ljpw": {"L": 0, "J": 0, "P": 0, "W": 0}, + "health": 0, + "insights": [error_msg], + "distance_from_ne": 0, } if not path.is_file(): return { - 'filename': filepath, - 'error': 'Not a file', - 'lines': 0, - 'ljpw': {'L': 0, 'J': 0, 'P': 0, 'W': 0}, - 'health': 0, - 'insights': [f'ERROR: Not a file (possibly a directory): {filepath}'], - 'distance_from_ne': 0 + "filename": filepath, + "error": "Not a file", + "lines": 0, + "ljpw": {"L": 0, "J": 0, "P": 0, "W": 0}, + "health": 0, + "insights": [f"ERROR: Not a file (possibly a directory): {filepath}"], + "distance_from_ne": 0, } # Try reading with UTF-8, fall back to other encodings - encodings = ['utf-8', 'latin-1', 'cp1252', 'iso-8859-1'] + encodings = ["utf-8", "latin-1", "cp1252", "iso-8859-1"] code = None used_encoding = None for encoding in encodings: try: - with open(filepath, 'r', encoding=encoding) as f: + with open(filepath, "r", encoding=encoding) as f: code = f.read() used_encoding = encoding break @@ -472,61 +468,62 @@ def analyze_file(filepath: str) -> Dict[str, Any]: continue except PermissionError: return { - 'filename': filepath, - 'error': 'Permission denied', - 'lines': 0, - 'ljpw': {'L': 0, 'J': 0, 'P': 0, 'W': 0}, - 'health': 0, - 'insights': [f'ERROR: Permission denied: {filepath}'], - 'distance_from_ne': 0 + "filename": filepath, + "error": "Permission denied", + "lines": 0, + "ljpw": {"L": 0, "J": 0, "P": 0, "W": 0}, + "health": 0, + "insights": [f"ERROR: Permission denied: {filepath}"], + "distance_from_ne": 0, } except Exception as e: return { - 'filename': filepath, - 'error': str(e), - 'lines': 0, - 'ljpw': {'L': 0, 'J': 0, 'P': 0, 'W': 0}, - 'health': 0, - 'insights': [f'ERROR: {str(e)}'], - 'distance_from_ne': 0 + "filename": filepath, + "error": str(e), + "lines": 0, + "ljpw": {"L": 0, "J": 0, "P": 0, "W": 0}, + "health": 0, + "insights": [f"ERROR: {str(e)}"], + "distance_from_ne": 0, } if code is None: return { - 'filename': filepath, - 'error': 'Encoding error', - 'lines': 0, - 'ljpw': {'L': 0, 'J': 0, 'P': 0, 'W': 0}, - 'health': 0, - 'insights': [f'ERROR: Could not decode file with any known encoding'], - 'distance_from_ne': 0 + "filename": filepath, + "error": "Encoding error", + "lines": 0, + "ljpw": {"L": 0, "J": 0, "P": 0, "W": 0}, + "health": 0, + "insights": [f"ERROR: Could not decode file with any known encoding"], + "distance_from_ne": 0, } analyzer = SimpleCodeAnalyzer() result = analyzer.analyze(code, filepath) # Add encoding info if not UTF-8 - if used_encoding != 'utf-8': - result['encoding'] = used_encoding - if 'insights' not in result: - result['insights'] = [] - result['insights'].append(f'NOTE: File encoded as {used_encoding}, not UTF-8') + if used_encoding != "utf-8": + result["encoding"] = used_encoding + if "insights" not in result: + result["insights"] = [] + result["insights"].append(f"NOTE: File encoded as {used_encoding}, not UTF-8") return result + def analyze_directory(dirpath: str, show_progress: bool = True) -> List[Dict[str, Any]]: """ Analyze all code files in directory with optional progress indicator. - + Args: dirpath: Path to directory to analyze show_progress: Whether to show progress bar (default: True) - + Returns: List of analysis results for each file """ results = [] - extensions = {'.py', '.js', '.java', '.rs', '.cpp', '.c', '.go', '.rb', '.php', '.ts'} + extensions = {".py", ".js", ".java", ".rs", ".cpp", ".c", ".go", ".rb", ".php", ".ts"} # Security: Validate and resolve path try: @@ -534,61 +531,64 @@ def analyze_directory(dirpath: str, show_progress: bool = True) -> List[Dict[str except (OSError, RuntimeError) as e: print(f"❌ Error: Invalid directory path: {e}") return [] - + if not path.exists(): print(f"❌ Error: Directory not found: {dirpath}") return [] - + if not path.is_dir(): print(f"❌ Error: Not a directory: {dirpath}") return [] - + # Collect all files first try: - files = [f for f in path.rglob('*') if f.is_file() and f.suffix in extensions] + files = [f for f in path.rglob("*") if f.is_file() and f.suffix in extensions] except (OSError, PermissionError) as e: print(f"❌ Error accessing directory: {e}") return [] - + total = len(files) - + if total == 0: print(f"⚠️ No code files found in {dirpath}") return [] - + if show_progress: print(f"\n📊 Analyzing {total} files in {dirpath}...") print("─" * 70) - + # Analyze files with progress indicator for i, file in enumerate(files, 1): result = analyze_file(str(file)) results.append(result) - + if show_progress: # Calculate progress percent = (i / total) * 100 bar_length = 40 filled = int(bar_length * i / total) - bar = '█' * filled + '░' * (bar_length - filled) - + bar = "█" * filled + "░" * (bar_length - filled) + # Truncate filename for display - display_name = file.name[:30] + '...' if len(file.name) > 30 else file.name - + display_name = file.name[:30] + "..." if len(file.name) > 30 else file.name + # Print progress bar - print(f"\r [{bar}] {percent:5.1f}% ({i:>4}/{total}) - {display_name}", - end='', flush=True) - + print( + f"\r [{bar}] {percent:5.1f}% ({i:>4}/{total}) - {display_name}", end="", flush=True + ) + if show_progress: print("\n" + "─" * 70) print(f"✅ Analysis complete: {total} files processed\n") - + return results + def analyze_quick(code: str) -> Dict[str, Any]: """Quick analysis of code snippet""" analyzer = SimpleCodeAnalyzer() - return analyzer.analyze(code, 'snippet') + return analyzer.analyze(code, "snippet") + def print_summary(results: List[Dict[str, Any]]) -> None: """Print summary of multiple files""" @@ -603,12 +603,12 @@ def print_summary(results: List[Dict[str, Any]]) -> None: # Calculate averages total_L, total_J, total_P, total_W, total_health = 0, 0, 0, 0, 0 for r in results: - if 'error' not in r: - total_L += r['ljpw']['L'] - total_J += r['ljpw']['J'] - total_P += r['ljpw']['P'] - total_W += r['ljpw']['W'] - total_health += r['health'] + if "error" not in r: + total_L += r["ljpw"]["L"] + total_J += r["ljpw"]["J"] + total_P += r["ljpw"]["P"] + total_W += r["ljpw"]["W"] + total_health += r["health"] n = len(results) print(f"\nAverage LJPW Scores:") @@ -619,8 +619,8 @@ def print_summary(results: List[Dict[str, Any]]) -> None: print(f"\nAverage Health: {(total_health/n)*100:.1f}%") # Show top issues - low_safety = [r for r in results if r.get('ljpw', {}).get('L', 1) < 0.5] - low_structure = [r for r in results if r.get('ljpw', {}).get('J', 1) < 0.4] + low_safety = [r for r in results if r.get("ljpw", {}).get("L", 1) < 0.5] + low_structure = [r for r in results if r.get("ljpw", {}).get("J", 1) < 0.4] if low_safety: print(f"\n{len(low_safety)} files with LOW SAFETY") @@ -629,12 +629,15 @@ def print_summary(results: List[Dict[str, Any]]) -> None: print("\n" + "=" * 70) -def calculate_distance(coords1: Tuple[float, float, float, float], - coords2: Tuple[float, float, float, float]) -> float: + +def calculate_distance( + coords1: Tuple[float, float, float, float], coords2: Tuple[float, float, float, float] +) -> float: """Calculate Euclidean distance between two LJPW coordinates""" L1, J1, P1, W1 = coords1 L2, J2, P2, W2 = coords2 - return math.sqrt((L1-L2)**2 + (J1-J2)**2 + (P1-P2)**2 + (W1-W2)**2) + return math.sqrt((L1 - L2) ** 2 + (J1 - J2) ** 2 + (P1 - P2) ** 2 + (W1 - W2) ** 2) + def calculate_file_distance(file1: str, file2: str) -> Dict[str, Any]: """Calculate semantic distance between two files""" @@ -643,16 +646,24 @@ def calculate_file_distance(file1: str, file2: str) -> Dict[str, Any]: result2 = analyze_file(file2) # Check for errors - if 'error' in result1: - return {'error': f"Failed to analyze {file1}: {result1['error']}"} - if 'error' in result2: - return {'error': f"Failed to analyze {file2}: {result2['error']}"} + if "error" in result1: + return {"error": f"Failed to analyze {file1}: {result1['error']}"} + if "error" in result2: + return {"error": f"Failed to analyze {file2}: {result2['error']}"} # Extract coordinates - coords1 = (result1['ljpw']['L'], result1['ljpw']['J'], - result1['ljpw']['P'], result1['ljpw']['W']) - coords2 = (result2['ljpw']['L'], result2['ljpw']['J'], - result2['ljpw']['P'], result2['ljpw']['W']) + coords1 = ( + result1["ljpw"]["L"], + result1["ljpw"]["J"], + result1["ljpw"]["P"], + result1["ljpw"]["W"], + ) + coords2 = ( + result2["ljpw"]["L"], + result2["ljpw"]["J"], + result2["ljpw"]["P"], + result2["ljpw"]["W"], + ) # Calculate distance distance = calculate_distance(coords1, coords2) @@ -675,20 +686,21 @@ def calculate_file_distance(file1: str, file2: str) -> Dict[str, Any]: interpretation = "Fundamentally different purposes" return { - 'file1': file1, - 'file2': file2, - 'coords1': coords1, - 'coords2': coords2, - 'distance': distance, - 'similarity': similarity, - 'interpretation': interpretation, - 'result1': result1, - 'result2': result2 + "file1": file1, + "file2": file2, + "coords1": coords1, + "coords2": coords2, + "distance": distance, + "similarity": similarity, + "interpretation": interpretation, + "result1": result1, + "result2": result2, } + def format_distance_result(result: Dict[str, Any]) -> str: """Format distance calculation result for display""" - if 'error' in result: + if "error" in result: return f"\nError: {result['error']}\n" output = [] @@ -698,13 +710,13 @@ def format_distance_result(result: Dict[str, Any]) -> str: # File 1 output.append(f"\nFile 1: {result['file1']}") - L1, J1, P1, W1 = result['coords1'] + L1, J1, P1, W1 = result["coords1"] output.append(f" Coordinates: L={L1:.2f}, J={J1:.2f}, P={P1:.2f}, W={W1:.2f}") output.append(f" Health: {result['result1']['health']:.0%}") # File 2 output.append(f"\nFile 2: {result['file2']}") - L2, J2, P2, W2 = result['coords2'] + L2, J2, P2, W2 = result["coords2"] output.append(f" Coordinates: L={L2:.2f}, J={J2:.2f}, P={P2:.2f}, W={W2:.2f}") output.append(f" Health: {result['result2']['health']:.0%}") @@ -723,10 +735,10 @@ def format_distance_result(result: Dict[str, Any]) -> str: # Insights output.append("\nKey Differences:") diffs = [ - ('Safety', abs(L1-L2), 'more' if L1 > L2 else 'less'), - ('Structure', abs(J1-J2), 'more' if J1 > J2 else 'less'), - ('Performance', abs(P1-P2), 'more' if P1 > P2 else 'less'), - ('Design quality', abs(W1-W2), 'better' if W1 > W2 else 'worse') + ("Safety", abs(L1 - L2), "more" if L1 > L2 else "less"), + ("Structure", abs(J1 - J2), "more" if J1 > J2 else "less"), + ("Performance", abs(P1 - P2), "more" if P1 > P2 else "less"), + ("Design quality", abs(W1 - W2), "better" if W1 > W2 else "worse"), ] diffs.sort(key=lambda x: x[1], reverse=True) @@ -742,60 +754,59 @@ def format_distance_result(result: Dict[str, Any]) -> str: return "\n".join(output) + def format_distance_result_json(result: Dict[str, Any]) -> str: """Format distance calculation result as JSON""" # Create clean JSON output (remove full analysis results, keep key info) json_result = { - 'file1': result.get('file1'), - 'file2': result.get('file2'), - 'coordinates': { - 'file1': { - 'L': result['coords1'][0], - 'J': result['coords1'][1], - 'P': result['coords1'][2], - 'W': result['coords1'][3] + "file1": result.get("file1"), + "file2": result.get("file2"), + "coordinates": { + "file1": { + "L": result["coords1"][0], + "J": result["coords1"][1], + "P": result["coords1"][2], + "W": result["coords1"][3], + }, + "file2": { + "L": result["coords2"][0], + "J": result["coords2"][1], + "P": result["coords2"][2], + "W": result["coords2"][3], }, - 'file2': { - 'L': result['coords2'][0], - 'J': result['coords2'][1], - 'P': result['coords2'][2], - 'W': result['coords2'][3] - } }, - 'health': { - 'file1': result['result1']['health'], - 'file2': result['result2']['health'] + "health": {"file1": result["result1"]["health"], "file2": result["result2"]["health"]}, + "distance": result["distance"], + "similarity": result["similarity"], + "interpretation": result["interpretation"], + "differences": { + "Love": abs(result["coords1"][0] - result["coords2"][0]), + "Justice": abs(result["coords1"][1] - result["coords2"][1]), + "Power": abs(result["coords1"][2] - result["coords2"][2]), + "Wisdom": abs(result["coords1"][3] - result["coords2"][3]), }, - 'distance': result['distance'], - 'similarity': result['similarity'], - 'interpretation': result['interpretation'], - 'differences': { - 'Love': abs(result['coords1'][0] - result['coords2'][0]), - 'Justice': abs(result['coords1'][1] - result['coords2'][1]), - 'Power': abs(result['coords1'][2] - result['coords2'][2]), - 'Wisdom': abs(result['coords1'][3] - result['coords2'][3]) - } } - if 'error' in result: - json_result = {'error': result['error']} + if "error" in result: + json_result = {"error": result["error"]} return json.dumps(json_result, indent=2) + def calculate_batch_distance(files: List[str]) -> Dict[str, Any]: """ Calculate distances between multiple files. Returns a distance matrix and analysis for all pairs. """ if len(files) < 2: - return {'error': 'Need at least 2 files for distance calculation'} + return {"error": "Need at least 2 files for distance calculation"} # Analyze all files analyses = {} for filepath in files: result = analyze_file(filepath) - if 'error' in result: - return {'error': f"Failed to analyze {filepath}: {result['error']}"} + if "error" in result: + return {"error": f"Failed to analyze {filepath}: {result['error']}"} analyses[filepath] = result # Build distance matrix @@ -803,14 +814,22 @@ def calculate_batch_distance(files: List[str]) -> Dict[str, Any]: distance_matrix = [[0.0] * n for _ in range(n)] for i in range(n): - for j in range(i+1, n): + for j in range(i + 1, n): file1 = files[i] file2 = files[j] - coords1 = (analyses[file1]['ljpw']['L'], analyses[file1]['ljpw']['J'], - analyses[file1]['ljpw']['P'], analyses[file1]['ljpw']['W']) - coords2 = (analyses[file2]['ljpw']['L'], analyses[file2]['ljpw']['J'], - analyses[file2]['ljpw']['P'], analyses[file2]['ljpw']['W']) + coords1 = ( + analyses[file1]["ljpw"]["L"], + analyses[file1]["ljpw"]["J"], + analyses[file1]["ljpw"]["P"], + analyses[file1]["ljpw"]["W"], + ) + coords2 = ( + analyses[file2]["ljpw"]["L"], + analyses[file2]["ljpw"]["J"], + analyses[file2]["ljpw"]["P"], + analyses[file2]["ljpw"]["W"], + ) dist = calculate_distance(coords1, coords2) distance_matrix[i][j] = dist @@ -819,23 +838,24 @@ def calculate_batch_distance(files: List[str]) -> Dict[str, Any]: # Find most similar and most different pairs pairs = [] for i in range(n): - for j in range(i+1, n): + for j in range(i + 1, n): pairs.append((files[i], files[j], distance_matrix[i][j])) pairs.sort(key=lambda x: x[2]) return { - 'files': files, - 'analyses': analyses, - 'distance_matrix': distance_matrix, - 'most_similar': pairs[0] if pairs else None, - 'most_different': pairs[-1] if pairs else None, - 'all_pairs': pairs + "files": files, + "analyses": analyses, + "distance_matrix": distance_matrix, + "most_similar": pairs[0] if pairs else None, + "most_different": pairs[-1] if pairs else None, + "all_pairs": pairs, } + def format_batch_distance_result(result: Dict[str, Any]) -> str: """Format batch distance calculation result for display""" - if 'error' in result: + if "error" in result: return f"\nError: {result['error']}\n" output = [] @@ -843,17 +863,23 @@ def format_batch_distance_result(result: Dict[str, Any]) -> str: output.append("LJPW Batch Semantic Distance Analysis") output.append("=" * 70) - files = result['files'] + files = result["files"] n = len(files) # Show file coordinates output.append(f"\nAnalyzed {n} files:\n") for i, filepath in enumerate(files): - analysis = result['analyses'][filepath] - coords = (analysis['ljpw']['L'], analysis['ljpw']['J'], - analysis['ljpw']['P'], analysis['ljpw']['W']) + analysis = result["analyses"][filepath] + coords = ( + analysis["ljpw"]["L"], + analysis["ljpw"]["J"], + analysis["ljpw"]["P"], + analysis["ljpw"]["W"], + ) output.append(f"{i+1}. {filepath}") - output.append(f" L={coords[0]:.2f}, J={coords[1]:.2f}, P={coords[2]:.2f}, W={coords[3]:.2f}") + output.append( + f" L={coords[0]:.2f}, J={coords[1]:.2f}, P={coords[2]:.2f}, W={coords[3]:.2f}" + ) output.append(f" Health: {analysis['health']:.0%}") output.append("") @@ -878,13 +904,13 @@ def format_batch_distance_result(result: Dict[str, Any]) -> str: output.append(row) # Most similar and different - if result['most_similar']: - file1, file2, dist = result['most_similar'] + if result["most_similar"]: + file1, file2, dist = result["most_similar"] output.append(f"\nMost Similar: {file1} ↔ {file2}") output.append(f" Distance: {dist:.3f}") - if result['most_different']: - file1, file2, dist = result['most_different'] + if result["most_different"]: + file1, file2, dist = result["most_different"] output.append(f"\nMost Different: {file1} ↔ {file2}") output.append(f" Distance: {dist:.3f}") @@ -892,49 +918,61 @@ def format_batch_distance_result(result: Dict[str, Any]) -> str: return "\n".join(output) + def format_batch_distance_result_json(result: Dict[str, Any]) -> str: """Format batch distance calculation result as JSON""" - if 'error' in result: - return json.dumps({'error': result['error']}, indent=2) + if "error" in result: + return json.dumps({"error": result["error"]}, indent=2) # Create clean JSON structure json_result = { - 'files': result['files'], - 'coordinates': {}, - 'health': {}, - 'distance_matrix': result['distance_matrix'], - 'most_similar': { - 'file1': result['most_similar'][0], - 'file2': result['most_similar'][1], - 'distance': result['most_similar'][2] - } if result['most_similar'] else None, - 'most_different': { - 'file1': result['most_different'][0], - 'file2': result['most_different'][1], - 'distance': result['most_different'][2] - } if result['most_different'] else None + "files": result["files"], + "coordinates": {}, + "health": {}, + "distance_matrix": result["distance_matrix"], + "most_similar": ( + { + "file1": result["most_similar"][0], + "file2": result["most_similar"][1], + "distance": result["most_similar"][2], + } + if result["most_similar"] + else None + ), + "most_different": ( + { + "file1": result["most_different"][0], + "file2": result["most_different"][1], + "distance": result["most_different"][2], + } + if result["most_different"] + else None + ), } # Add coordinates for each file - for filepath in result['files']: - analysis = result['analyses'][filepath] - json_result['coordinates'][filepath] = { - 'L': analysis['ljpw']['L'], - 'J': analysis['ljpw']['J'], - 'P': analysis['ljpw']['P'], - 'W': analysis['ljpw']['W'] + for filepath in result["files"]: + analysis = result["analyses"][filepath] + json_result["coordinates"][filepath] = { + "L": analysis["ljpw"]["L"], + "J": analysis["ljpw"]["J"], + "P": analysis["ljpw"]["P"], + "W": analysis["ljpw"]["W"], } - json_result['health'][filepath] = analysis['health'] + json_result["health"][filepath] = analysis["health"] return json.dumps(json_result, indent=2) + # ============================================================================ # MAIN # ============================================================================ + def main() -> None: if len(sys.argv) < 2: - print(""" + print( + """ LJPW Semantic Analyzer - DNA-Inspired Code Quality Analysis Usage: @@ -977,16 +1015,17 @@ def main() -> None: MIT License - Free for all, forever Version 1.1 - """) + """ + ) return command = sys.argv[1] - if command == 'help': + if command == "help": # Show help text (already printed above) return - if command == 'analyze': + if command == "analyze": if len(sys.argv) < 3: print("Error: Please provide file or directory to analyze") return @@ -1003,14 +1042,14 @@ def main() -> None: print(f"\nAnalyzed {len(results)} files") # Optionally save detailed results - if len(sys.argv) > 3 and sys.argv[3] == '--save': - with open('ljpw_results.json', 'w') as f: + if len(sys.argv) > 3 and sys.argv[3] == "--save": + with open("ljpw_results.json", "w") as f: json.dump(results, f, indent=2) print("Detailed results saved to ljpw_results.json") else: print(f"Error: {target} not found") - elif command == 'quick': + elif command == "quick": if len(sys.argv) < 3: print("Error: Please provide code to analyze") return @@ -1019,10 +1058,12 @@ def main() -> None: result = analyze_quick(code) print(format_result(result)) - elif command == 'distance': + elif command == "distance": if len(sys.argv) < 4: print("Error: Please provide at least two files to compare") - print("Usage: python ljpw_standalone.py distance [file3...] [--json] [--save ]") + print( + "Usage: python ljpw_standalone.py distance [file3...] [--json] [--save ]" + ) return # Parse arguments @@ -1033,10 +1074,10 @@ def main() -> None: while i < len(sys.argv): arg = sys.argv[i] - if arg == '--json': + if arg == "--json": use_json = True i += 1 - elif arg == '--save': + elif arg == "--save": if i + 1 < len(sys.argv): save_file = sys.argv[i + 1] i += 2 @@ -1068,7 +1109,7 @@ def main() -> None: # Save if requested if save_file: - with open(save_file, 'w') as f: + with open(save_file, "w") as f: f.write(output) print(f"\nResults saved to {save_file}") @@ -1087,7 +1128,7 @@ def main() -> None: # Save if requested if save_file: - with open(save_file, 'w') as f: + with open(save_file, "w") as f: f.write(output) print(f"\nResults saved to {save_file}") @@ -1095,5 +1136,6 @@ def main() -> None: print(f"Unknown command: {command}") print("Use 'python ljpw_standalone.py help' for usage") -if __name__ == '__main__': + +if __name__ == "__main__": main() diff --git a/src/ljpw/ljpw_virtual_machine.py b/src/ljpw/ljpw_virtual_machine.py index 0bdbbc2..0a9b640 100644 --- a/src/ljpw/ljpw_virtual_machine.py +++ b/src/ljpw/ljpw_virtual_machine.py @@ -37,16 +37,17 @@ """ import math -from typing import Dict, List, Tuple, Optional, Any -from dataclasses import dataclass from collections import defaultdict +from dataclasses import dataclass +from typing import Any, Dict, List, Optional, Tuple -from ljpw_dynamic_v3 import LJPWDynamicModel, LJPWParameters, NATURAL_EQUILIBRIUM +from ljpw_dynamic_v3 import NATURAL_EQUILIBRIUM, LJPWDynamicModel, LJPWParameters @dataclass class LJPWState: """Represents a point in LJPW 4D phase space""" + L: float J: float P: float @@ -59,10 +60,10 @@ def distance_from_ne(self) -> float: """Calculate distance from Natural Equilibrium""" L_ne, J_ne, P_ne, W_ne = NATURAL_EQUILIBRIUM return math.sqrt( - (self.L - L_ne)**2 + - (self.J - J_ne)**2 + - (self.P - P_ne)**2 + - (self.W - W_ne)**2 + (self.L - L_ne) ** 2 + + (self.J - J_ne) ** 2 + + (self.P - P_ne) ** 2 + + (self.W - W_ne) ** 2 ) def health(self) -> float: @@ -73,10 +74,11 @@ def health(self) -> float: @dataclass class LJPWComponent: """A component in LJPW space with its own state""" + name: str state: LJPWState component_type: str - subcomponents: Dict[str, 'LJPWComponent'] + subcomponents: Dict[str, "LJPWComponent"] metadata: Dict[str, Any] def get_all_states(self) -> Dict[str, LJPWState]: @@ -98,13 +100,14 @@ class LJPWStructure: - Infrastructure (servers, networks, databases) - Organizations (teams, processes, products) """ + name: str root: LJPWComponent interactions: Dict[Tuple[str, str], float] # Coupling strengths def get_component(self, path: str) -> Optional[LJPWComponent]: """Get component by path (e.g., 'os.kernel')""" - parts = path.split('.') + parts = path.split(".") current = self.root for part in parts[1:]: # Skip root name @@ -132,6 +135,7 @@ def overall_state(self) -> LJPWState: @dataclass class SimulationResult: """Result of simulating an operation in LJPW space""" + operation: str initial_state: LJPWState final_state: LJPWState @@ -171,11 +175,12 @@ def load_iso(self, iso_path: str, analyze: bool = True) -> LJPWStructure: if analyze: # Use the ISO analyzer to extract structure from ljpw_iso_analyzer import LJPWISOAnalyzer + analyzer = LJPWISOAnalyzer() result = analyzer.analyze(iso_path) - ljpw = result['ljpw'] - root_state = LJPWState(ljpw['L'], ljpw['J'], ljpw['P'], ljpw['W']) + ljpw = result["ljpw"] + root_state = LJPWState(ljpw["L"], ljpw["J"], ljpw["P"], ljpw["W"]) else: # Default structure root_state = LJPWState(0.7, 0.7, 0.7, 0.7) @@ -184,53 +189,45 @@ def load_iso(self, iso_path: str, analyze: bool = True) -> LJPWStructure: # ISO typically has: boot, installer, core OS, components boot = LJPWComponent( - name='boot', + name="boot", state=LJPWState(0.9, 0.95, 0.8, 0.9), # Boot sector is critical - component_type='boot_sector', + component_type="boot_sector", subcomponents={}, - metadata={'critical': True} + metadata={"critical": True}, ) installer = LJPWComponent( - name='installer', + name="installer", state=LJPWState(0.8, 0.85, 0.9, 0.85), # Installer is optimized - component_type='installer', + component_type="installer", subcomponents={}, - metadata={'temporary': True} + metadata={"temporary": True}, ) core_os = LJPWComponent( - name='core', + name="core", state=LJPWState(0.85, 0.9, 0.85, 0.9), # Core is well-designed - component_type='operating_system', + component_type="operating_system", subcomponents={}, - metadata={'persistent': True} + metadata={"persistent": True}, ) root = LJPWComponent( - name='iso', + name="iso", state=root_state, - component_type='iso_image', - subcomponents={ - 'boot': boot, - 'installer': installer, - 'core': core_os - }, - metadata={'path': iso_path} + component_type="iso_image", + subcomponents={"boot": boot, "installer": installer, "core": core_os}, + metadata={"path": iso_path}, ) # Define interactions (how components affect each other) interactions = { - ('boot', 'installer'): 0.9, # Boot enables installer - ('installer', 'core'): 0.95, # Installer deploys core - ('core', 'installer'): 0.3, # Core affects installer minimally + ("boot", "installer"): 0.9, # Boot enables installer + ("installer", "core"): 0.95, # Installer deploys core + ("core", "installer"): 0.3, # Core affects installer minimally } - structure = LJPWStructure( - name=iso_path, - root=root, - interactions=interactions - ) + structure = LJPWStructure(name=iso_path, root=root, interactions=interactions) self.loaded_structures[iso_path] = structure return structure @@ -239,11 +236,13 @@ def load_structure(self, structure: LJPWStructure) -> None: """Load a pre-built structure into the VM""" self.loaded_structures[structure.name] = structure - def simulate_operation(self, - structure: LJPWStructure, - operation: str, - duration: float = 100.0, - environment: Optional[Dict] = None) -> SimulationResult: + def simulate_operation( + self, + structure: LJPWStructure, + operation: str, + duration: float = 100.0, + environment: Optional[Dict] = None, + ) -> SimulationResult: """ Simulate an operation on a structure in LJPW space. @@ -260,11 +259,11 @@ def simulate_operation(self, initial = structure.overall_state() # Apply operation-specific dynamics - if operation == 'install': + if operation == "install": trajectory = self._simulate_installation(structure, duration, environment) - elif operation == 'update': + elif operation == "update": trajectory = self._simulate_update(structure, duration) - elif operation == 'scale': + elif operation == "scale": trajectory = self._simulate_scaling(structure, duration) else: # Generic simulation using v3.0 dynamics @@ -280,8 +279,8 @@ def simulate_operation(self, # Check for convergence analysis = self.model.analyze_trajectory(trajectory) - converging = analysis['converging'] - eta_to_ne = analysis.get('eta_to_ne', float('inf')) + converging = analysis["converging"] + eta_to_ne = analysis.get("eta_to_ne", float("inf")) # Identify issues issues = self._identify_issues(initial, final, trajectory) @@ -300,13 +299,12 @@ def simulate_operation(self, recommendations=recommendations, duration=duration, converging=converging, - eta_to_ne=eta_to_ne + eta_to_ne=eta_to_ne, ) - def _simulate_installation(self, - structure: LJPWStructure, - duration: float, - environment: Optional[Dict]) -> List: + def _simulate_installation( + self, structure: LJPWStructure, duration: float, environment: Optional[Dict] + ) -> List: """Simulate OS installation process""" initial = structure.overall_state() @@ -330,12 +328,12 @@ def _simulate_installation(self, # Installation increases P (activity) if step < 80: # During active installation - P_boost = 0.1 * (1 - step/80) # Decreasing boost + P_boost = 0.1 * (1 - step / 80) # Decreasing boost P = min(1.0, P + P_boost) # Environment affects L (safety) if environment: - env_L = environment.get('safety_factor', 1.0) + env_L = environment.get("safety_factor", 1.0) L = L * env_L # Use model to evolve @@ -394,16 +392,15 @@ def _simulate_scaling(self, structure: LJPWStructure, duration: float) -> List: return modified - def _identify_issues(self, - initial: LJPWState, - final: LJPWState, - trajectory: List) -> List[str]: + def _identify_issues(self, initial: LJPWState, final: LJPWState, trajectory: List) -> List[str]: """Identify potential issues from simulation""" issues = [] # Check threshold crossing if final.P > 0.71 and final.W < 0.60: - issues.append(f"Power crossed threshold (P={final.P:.2f} > 0.71) with low Wisdom (W={final.W:.2f})") + issues.append( + f"Power crossed threshold (P={final.P:.2f} > 0.71) with low Wisdom (W={final.W:.2f})" + ) issues.append("Risk: Over-optimization may cause instability") # Check dimension drops @@ -425,9 +422,7 @@ def _identify_issues(self, return issues - def _generate_recommendations(self, - final: LJPWState, - issues: List[str]) -> List[str]: + def _generate_recommendations(self, final: LJPWState, issues: List[str]) -> List[str]: """Generate recommendations based on final state""" recommendations = [] @@ -438,16 +433,20 @@ def _generate_recommendations(self, recommendations.append(f"Boost Safety (L): Add validation, error handling, monitoring") if final.J < J_ne - 0.1: - recommendations.append(f"Improve Structure (J): Add documentation, modularize, clean interfaces") + recommendations.append( + f"Improve Structure (J): Add documentation, modularize, clean interfaces" + ) if final.P > P_ne + 0.1 and final.W < W_ne: - recommendations.append(f"Balance Power with Wisdom: Add architecture review before optimizing") + recommendations.append( + f"Balance Power with Wisdom: Add architecture review before optimizing" + ) if final.W < W_ne - 0.1: recommendations.append(f"Increase Wisdom (W): Invest in design, planning, architecture") # Issue-specific - if "threshold" in ' '.join(issues).lower(): + if "threshold" in " ".join(issues).lower(): recommendations.append("CRITICAL: Reduce Power or increase Wisdom to avoid erosion") if not recommendations: @@ -455,10 +454,12 @@ def _generate_recommendations(self, return recommendations - def predict_interaction(self, - structure1: LJPWStructure, - structure2: LJPWStructure, - interaction_type: str = 'integrate') -> SimulationResult: + def predict_interaction( + self, + structure1: LJPWStructure, + structure2: LJPWStructure, + interaction_type: str = "integrate", + ) -> SimulationResult: """ Predict outcome of two structures interacting. @@ -469,7 +470,7 @@ def predict_interaction(self, state2 = structure2.overall_state() # Weighted combination based on interaction type - if interaction_type == 'integrate': + if interaction_type == "integrate": # Application inherits OS characteristics combined_L = 0.3 * state1.L + 0.7 * state2.L combined_J = 0.5 * state1.J + 0.5 * state2.J @@ -493,7 +494,7 @@ def predict_interaction(self, analysis = self.model.analyze_trajectory(trajectory) return SimulationResult( - operation=f'{interaction_type}_{structure1.name}_with_{structure2.name}', + operation=f"{interaction_type}_{structure1.name}_with_{structure2.name}", initial_state=combined_state, final_state=final, trajectory=trajectory, @@ -502,14 +503,16 @@ def predict_interaction(self, issues=self._identify_issues(combined_state, final, trajectory), recommendations=self._generate_recommendations(final, []), duration=50, - converging=analysis['converging'], - eta_to_ne=analysis.get('eta_to_ne', float('inf')) + converging=analysis["converging"], + eta_to_ne=analysis.get("eta_to_ne", float("inf")), ) - def create_composite_structure(self, - name: str, - components: Dict[str, LJPWComponent], - interactions: Dict[Tuple[str, str], float]) -> LJPWStructure: + def create_composite_structure( + self, + name: str, + components: Dict[str, LJPWComponent], + interactions: Dict[Tuple[str, str], float], + ) -> LJPWStructure: """ Create a composite structure from components. @@ -527,23 +530,19 @@ def create_composite_structure(self, root = LJPWComponent( name=name, state=LJPWState(avg_L, avg_J, avg_P, avg_W), - component_type='composite', + component_type="composite", subcomponents=components, - metadata={'created': 'composite'} + metadata={"created": "composite"}, ) - return LJPWStructure( - name=name, - root=root, - interactions=interactions - ) + return LJPWStructure(name=name, root=root, interactions=interactions) # Example usage and demonstrations -if __name__ == '__main__': - print("="*70) +if __name__ == "__main__": + print("=" * 70) print("LJPW Virtual Machine - Semantic Computing Demo") - print("="*70) + print("=" * 70) print() print("Demonstrates: LJPW space as a computational substrate") print("Operations run on MEANING, not bytes!") @@ -552,46 +551,29 @@ def create_composite_structure(self, vm = LJPWVirtualMachine() # Example 1: Load and "install" Ubuntu (simulated structure) - print("="*70) + print("=" * 70) print("Example 1: Installing Ubuntu Server in LJPW Space") - print("-"*70) + print("-" * 70) print() # Create simulated Ubuntu structure ubuntu = vm.create_composite_structure( - name='ubuntu-22.04-server', + name="ubuntu-22.04-server", components={ - 'boot': LJPWComponent( - 'boot', - LJPWState(0.9, 0.95, 0.85, 0.9), - 'boot_sector', - {}, - {} - ), - 'installer': LJPWComponent( - 'installer', - LJPWState(0.85, 0.90, 0.90, 0.85), - 'installer', - {}, - {} + "boot": LJPWComponent("boot", LJPWState(0.9, 0.95, 0.85, 0.9), "boot_sector", {}, {}), + "installer": LJPWComponent( + "installer", LJPWState(0.85, 0.90, 0.90, 0.85), "installer", {}, {} ), - 'core': LJPWComponent( - 'core', - LJPWState(0.88, 0.90, 0.85, 0.90), - 'os_core', - {}, - {} - ) + "core": LJPWComponent("core", LJPWState(0.88, 0.90, 0.85, 0.90), "os_core", {}, {}), }, - interactions={ - ('boot', 'installer'): 0.95, - ('installer', 'core'): 0.90 - } + interactions={("boot", "installer"): 0.95, ("installer", "core"): 0.90}, ) print(f"Loaded: {ubuntu.name}") - print(f"Initial state: L={ubuntu.overall_state().L:.2f}, J={ubuntu.overall_state().J:.2f}, " - f"P={ubuntu.overall_state().P:.2f}, W={ubuntu.overall_state().W:.2f}") + print( + f"Initial state: L={ubuntu.overall_state().L:.2f}, J={ubuntu.overall_state().J:.2f}, " + f"P={ubuntu.overall_state().P:.2f}, W={ubuntu.overall_state().W:.2f}" + ) print(f"Initial health: {ubuntu.overall_state().health()*100:.1f}%") print() @@ -599,19 +581,21 @@ def create_composite_structure(self, print("Simulating installation (in LJPW space, NO bytes executed)...") result = vm.simulate_operation( ubuntu, - operation='install', + operation="install", duration=60, - environment={'safety_factor': 0.95} # Slightly risky environment + environment={"safety_factor": 0.95}, # Slightly risky environment ) print() print("RESULTS:") print(f" Success probability: {result.success_prob*100:.1f}%") print(f" Final health: {result.final_health*100:.1f}%") - print(f" Final state: L={result.final_state.L:.2f}, J={result.final_state.J:.2f}, " - f"P={result.final_state.P:.2f}, W={result.final_state.W:.2f}") + print( + f" Final state: L={result.final_state.L:.2f}, J={result.final_state.J:.2f}, " + f"P={result.final_state.P:.2f}, W={result.final_state.W:.2f}" + ) print(f" Converging to NE: {result.converging}") - if result.eta_to_ne != float('inf'): + if result.eta_to_ne != float("inf"): print(f" ETA to equilibrium: {result.eta_to_ne:.1f} time units") if result.issues: @@ -630,42 +614,38 @@ def create_composite_structure(self, print() # Example 2: Deploy application on OS - print("="*70) + print("=" * 70) print("Example 2: Deploying Application on Ubuntu") - print("-"*70) + print("-" * 70) print() # Create application structure webapp = vm.create_composite_structure( - name='webapp', + name="webapp", components={ - 'backend': LJPWComponent( - 'backend', - LJPWState(0.65, 0.70, 0.85, 0.75), - 'application', - {}, - {} + "backend": LJPWComponent( + "backend", LJPWState(0.65, 0.70, 0.85, 0.75), "application", {}, {} + ), + "database": LJPWComponent( + "database", LJPWState(0.80, 0.85, 0.75, 0.85), "database", {}, {} ), - 'database': LJPWComponent( - 'database', - LJPWState(0.80, 0.85, 0.75, 0.85), - 'database', - {}, - {} - ) }, - interactions={('backend', 'database'): 0.85} + interactions={("backend", "database"): 0.85}, ) print(f"Application: {webapp.name}") - print(f" Backend: L={webapp.root.subcomponents['backend'].state.L:.2f}, " - f"J={webapp.root.subcomponents['backend'].state.J:.2f}") - print(f" Database: L={webapp.root.subcomponents['database'].state.L:.2f}, " - f"J={webapp.root.subcomponents['database'].state.J:.2f}") + print( + f" Backend: L={webapp.root.subcomponents['backend'].state.L:.2f}, " + f"J={webapp.root.subcomponents['backend'].state.J:.2f}" + ) + print( + f" Database: L={webapp.root.subcomponents['database'].state.L:.2f}, " + f"J={webapp.root.subcomponents['database'].state.J:.2f}" + ) print() print("Predicting integration with Ubuntu...") - integration = vm.predict_interaction(webapp, ubuntu, 'integrate') + integration = vm.predict_interaction(webapp, ubuntu, "integrate") print() print("PREDICTION:") @@ -679,9 +659,9 @@ def create_composite_structure(self, print(f" ⚠️ {issue}") print() - print("="*70) + print("=" * 70) print("KEY INSIGHT") - print("-"*70) + print("-" * 70) print() print("We just simulated:") print(" 1. OS installation (60 time units)") @@ -699,4 +679,4 @@ def create_composite_structure(self, print() print("This is SEMANTIC COMPUTING.") print("Operations on MEANING, not bytes.") - print("="*70) + print("=" * 70) diff --git a/tests/test_analogical_reasoning.py b/tests/test_analogical_reasoning.py index 54cba2a..e74952a 100644 --- a/tests/test_analogical_reasoning.py +++ b/tests/test_analogical_reasoning.py @@ -17,38 +17,35 @@ import sys from pathlib import Path -from typing import Tuple, Dict, List +from typing import Dict, List, Tuple -sys.path.insert(0, str(Path(__file__).parent.parent / 'src' / 'ljpw')) +sys.path.insert(0, str(Path(__file__).parent.parent / "src" / "ljpw")) from ljpw_standalone import analyze_quick, calculate_distance # ============================================================================ # ANALOGY ENGINE # ============================================================================ -def calculate_vector(pos1: Tuple[float, float, float, float], - pos2: Tuple[float, float, float, float]) -> Tuple[float, float, float, float]: + +def calculate_vector( + pos1: Tuple[float, float, float, float], pos2: Tuple[float, float, float, float] +) -> Tuple[float, float, float, float]: """Calculate semantic vector from pos1 to pos2.""" - return ( - pos2[0] - pos1[0], - pos2[1] - pos1[1], - pos2[2] - pos1[2], - pos2[3] - pos1[3] - ) + return (pos2[0] - pos1[0], pos2[1] - pos1[1], pos2[2] - pos1[2], pos2[3] - pos1[3]) + -def apply_vector(pos: Tuple[float, float, float, float], - vec: Tuple[float, float, float, float]) -> Tuple[float, float, float, float]: +def apply_vector( + pos: Tuple[float, float, float, float], vec: Tuple[float, float, float, float] +) -> Tuple[float, float, float, float]: """Apply semantic vector to position.""" - return ( - pos[0] + vec[0], - pos[1] + vec[1], - pos[2] + vec[2], - pos[3] + vec[3] - ) + return (pos[0] + vec[0], pos[1] + vec[1], pos[2] + vec[2], pos[3] + vec[3]) + -def solve_analogy(A: Tuple[float, float, float, float], - B: Tuple[float, float, float, float], - C: Tuple[float, float, float, float]) -> Tuple[float, float, float, float]: +def solve_analogy( + A: Tuple[float, float, float, float], + B: Tuple[float, float, float, float], + C: Tuple[float, float, float, float], +) -> Tuple[float, float, float, float]: """ Solve: A is to B as C is to ? @@ -60,6 +57,7 @@ def solve_analogy(A: Tuple[float, float, float, float], D = apply_vector(C, vector_AB) return D + def describe_vector(vec: Tuple[float, float, float, float]) -> str: """Describe what a semantic vector represents.""" L_change, J_change, P_change, W_change = vec @@ -87,14 +85,17 @@ def describe_vector(vec: Tuple[float, float, float, float]) -> str: return ", ".join(changes) + def format_position(pos: Tuple[float, float, float, float]) -> str: """Format position for display.""" return f"L={pos[0]:.2f}, J={pos[1]:.2f}, P={pos[2]:.2f}, W={pos[3]:.2f}" + # ============================================================================ # TEST CASES # ============================================================================ + def test_analogies(): print("\n" + "=" * 70) print("SEMANTIC ANALOGY TEST") @@ -134,12 +135,24 @@ def get(key): result_safe = analyze_quick(safe_func) result_getter = analyze_quick(simple_getter) - pos_simple = (result_simple['ljpw']['L'], result_simple['ljpw']['J'], - result_simple['ljpw']['P'], result_simple['ljpw']['W']) - pos_safe = (result_safe['ljpw']['L'], result_safe['ljpw']['J'], - result_safe['ljpw']['P'], result_safe['ljpw']['W']) - pos_getter = (result_getter['ljpw']['L'], result_getter['ljpw']['J'], - result_getter['ljpw']['P'], result_getter['ljpw']['W']) + pos_simple = ( + result_simple["ljpw"]["L"], + result_simple["ljpw"]["J"], + result_simple["ljpw"]["P"], + result_simple["ljpw"]["W"], + ) + pos_safe = ( + result_safe["ljpw"]["L"], + result_safe["ljpw"]["J"], + result_safe["ljpw"]["P"], + result_safe["ljpw"]["W"], + ) + pos_getter = ( + result_getter["ljpw"]["L"], + result_getter["ljpw"]["J"], + result_getter["ljpw"]["P"], + result_getter["ljpw"]["W"], + ) print(f"\nA (simple function): {format_position(pos_simple)}") print(f"B (safe function): {format_position(pos_safe)}") @@ -196,12 +209,24 @@ def validate(x): result_doc = analyze_quick(documented) result_val = analyze_quick(undoc_validator) - pos_undoc = (result_undoc['ljpw']['L'], result_undoc['ljpw']['J'], - result_undoc['ljpw']['P'], result_undoc['ljpw']['W']) - pos_doc = (result_doc['ljpw']['L'], result_doc['ljpw']['J'], - result_doc['ljpw']['P'], result_doc['ljpw']['W']) - pos_val = (result_val['ljpw']['L'], result_val['ljpw']['J'], - result_val['ljpw']['P'], result_val['ljpw']['W']) + pos_undoc = ( + result_undoc["ljpw"]["L"], + result_undoc["ljpw"]["J"], + result_undoc["ljpw"]["P"], + result_undoc["ljpw"]["W"], + ) + pos_doc = ( + result_doc["ljpw"]["L"], + result_doc["ljpw"]["J"], + result_doc["ljpw"]["P"], + result_doc["ljpw"]["W"], + ) + pos_val = ( + result_val["ljpw"]["L"], + result_val["ljpw"]["J"], + result_val["ljpw"]["P"], + result_val["ljpw"]["W"], + ) print(f"\nA (undocumented): {format_position(pos_undoc)}") print(f"B (documented): {format_position(pos_doc)}") @@ -255,12 +280,24 @@ def api_call(url): result_prod = analyze_quick(production) result_api = analyze_quick(simple_api) - pos_proto = (result_proto['ljpw']['L'], result_proto['ljpw']['J'], - result_proto['ljpw']['P'], result_proto['ljpw']['W']) - pos_prod = (result_prod['ljpw']['L'], result_prod['ljpw']['J'], - result_prod['ljpw']['P'], result_prod['ljpw']['W']) - pos_api = (result_api['ljpw']['L'], result_api['ljpw']['J'], - result_api['ljpw']['P'], result_api['ljpw']['W']) + pos_proto = ( + result_proto["ljpw"]["L"], + result_proto["ljpw"]["J"], + result_proto["ljpw"]["P"], + result_proto["ljpw"]["W"], + ) + pos_prod = ( + result_prod["ljpw"]["L"], + result_prod["ljpw"]["J"], + result_prod["ljpw"]["P"], + result_prod["ljpw"]["W"], + ) + pos_api = ( + result_api["ljpw"]["L"], + result_api["ljpw"]["J"], + result_api["ljpw"]["P"], + result_api["ljpw"]["W"], + ) print(f"\nA (prototype): {format_position(pos_proto)}") print(f"B (production): {format_position(pos_prod)}") @@ -293,7 +330,7 @@ def api_call(url): vec_AB[0] + vec_BC[0], vec_AB[1] + vec_BC[1], vec_AB[2] + vec_BC[2], - vec_AB[3] + vec_BC[3] + vec_AB[3] + vec_BC[3], ) # Apply both transformations @@ -320,7 +357,8 @@ def api_call(url): print("\n" + "=" * 70) print("CONCLUSIONS") print("=" * 70) - print(""" + print( + """ If analogies work geometrically, this proves: 1. Semantic relationships are geometric transformations @@ -333,8 +371,10 @@ def api_call(url): - Example-based refactoring ("make this like that") - Pattern transfer across codebases - Semantic code search by relationship - """) + """ + ) print("=" * 70 + "\n") -if __name__ == '__main__': + +if __name__ == "__main__": test_analogies() diff --git a/tests/test_bold_prediction.py b/tests/test_bold_prediction.py index 2f84efc..499d4ba 100644 --- a/tests/test_bold_prediction.py +++ b/tests/test_bold_prediction.py @@ -12,7 +12,7 @@ import sys from pathlib import Path -sys.path.insert(0, str(Path(__file__).parent.parent / 'src' / 'ljpw')) +sys.path.insert(0, str(Path(__file__).parent.parent / "src" / "ljpw")) from ljpw_standalone import analyze_quick, calculate_distance # Natural Equilibrium coordinates @@ -104,6 +104,7 @@ def f(x): # TEST FUNCTION # ============================================================================ + def test_beauty_hypothesis(): print("\n" + "=" * 70) print("BOLD PREDICTION TEST: Beautiful Code Near Natural Equilibrium") @@ -121,15 +122,19 @@ def test_beauty_hypothesis(): ("Quicksort (elegant recursion)", beautiful_python), ("Function composition", beautiful_minimal), ("Memoization decorator", beautiful_balanced), - ("Flatten with generators", beautiful_pythonic) + ("Flatten with generators", beautiful_pythonic), ] beautiful_distances = [] for name, code in beautiful_examples: result = analyze_quick(code) - coords = (result['ljpw']['L'], result['ljpw']['J'], - result['ljpw']['P'], result['ljpw']['W']) + coords = ( + result["ljpw"]["L"], + result["ljpw"]["J"], + result["ljpw"]["P"], + result["ljpw"]["W"], + ) dist = calculate_distance(coords, NE) beautiful_distances.append(dist) @@ -142,7 +147,9 @@ def test_beauty_hypothesis(): print(f"\n{name}:") print(f" Genome: {genome}") - print(f" Position: L={coords[0]:.2f}, J={coords[1]:.2f}, P={coords[2]:.2f}, W={coords[3]:.2f}") + print( + f" Position: L={coords[0]:.2f}, J={coords[1]:.2f}, P={coords[2]:.2f}, W={coords[3]:.2f}" + ) print(f" Distance from NE: {dist:.3f}", end="") if dist < 0.4: @@ -159,15 +166,19 @@ def test_beauty_hypothesis(): ugly_examples = [ ("Ugly quicksort (no spacing)", ugly_code_1), - ("Ugly counter (inefficient)", ugly_code_2) + ("Ugly counter (inefficient)", ugly_code_2), ] ugly_distances = [] for name, code in ugly_examples: result = analyze_quick(code) - coords = (result['ljpw']['L'], result['ljpw']['J'], - result['ljpw']['P'], result['ljpw']['W']) + coords = ( + result["ljpw"]["L"], + result["ljpw"]["J"], + result["ljpw"]["P"], + result["ljpw"]["W"], + ) dist = calculate_distance(coords, NE) ugly_distances.append(dist) @@ -180,7 +191,9 @@ def test_beauty_hypothesis(): print(f"\n{name}:") print(f" Genome: {genome}") - print(f" Position: L={coords[0]:.2f}, J={coords[1]:.2f}, P={coords[2]:.2f}, W={coords[3]:.2f}") + print( + f" Position: L={coords[0]:.2f}, J={coords[1]:.2f}, P={coords[2]:.2f}, W={coords[3]:.2f}" + ) print(f" Distance from NE: {dist:.3f}", end="") if dist < 0.4: @@ -214,7 +227,8 @@ def test_beauty_hypothesis(): print("CONCLUSIONS") print("=" * 70) - print(""" + print( + """ If beautiful code clusters near Natural Equilibrium, this suggests: 1. LJPW captures aesthetic/quality judgments (not just syntax) @@ -224,8 +238,10 @@ def test_beauty_hypothesis(): This would be strong evidence that LJPW captures something fundamental about code quality, not just arbitrary measurements. - """) + """ + ) print("=" * 70 + "\n") -if __name__ == '__main__': + +if __name__ == "__main__": test_beauty_hypothesis() diff --git a/tests/test_code_sample.py b/tests/test_code_sample.py index 1194b17..be5294d 100644 --- a/tests/test_code_sample.py +++ b/tests/test_code_sample.py @@ -2,18 +2,16 @@ def process_user_data(user_id, data): """Process user data from API""" result = [] for item in data: - if item['status'] == 'active': + if item["status"] == "active": processed = transform_item(item) result.append(processed) return result + def transform_item(item): """Transform single item""" - return { - 'id': item['id'], - 'value': item['value'] * 2, - 'timestamp': item['timestamp'] - } + return {"id": item["id"], "value": item["value"] * 2, "timestamp": item["timestamp"]} + class DataManager: def __init__(self): @@ -28,4 +26,4 @@ def get_data(self, key): def fetch_from_db(self, key): # Simulate DB fetch - return {'key': key, 'value': 'data'} + return {"key": key, "value": "data"} diff --git a/tests/test_comprehensive_validation.py b/tests/test_comprehensive_validation.py index 7ab6cd5..e42e069 100644 --- a/tests/test_comprehensive_validation.py +++ b/tests/test_comprehensive_validation.py @@ -14,16 +14,17 @@ 5. Algorithmic Complexity: O(1) vs O(n) vs O(n²) implementations """ +import math import sys from pathlib import Path -import math -sys.path.insert(0, str(Path(__file__).parent.parent / 'src' / 'ljpw')) +sys.path.insert(0, str(Path(__file__).parent.parent / "src" / "ljpw")) from ljpw_standalone import analyze_quick, calculate_distance # Natural Equilibrium for reference NATURAL_EQUILIBRIUM = (0.618, 0.414, 0.718, 0.693) + def distance(coords1, coords2): """Calculate Euclidean distance between two 4D points""" return math.sqrt(sum((a - b) ** 2 for a, b in zip(coords1, coords2))) @@ -38,11 +39,9 @@ def distance(coords1, coords2): def empty(): pass """, - "one_liner": """ def add(a, b): return a + b """, - "deeply_nested": """ def process(data): if data: @@ -54,11 +53,9 @@ def process(data): return item return None """, - "single_expression": """ lambda x: x * 2 """, - "docstring_only": """ def placeholder(): '''This function does nothing yet''' @@ -81,7 +78,6 @@ def __new__(cls): cls._instance = super().__new__(cls) return cls._instance """, - "factory": """ class AnimalFactory: @staticmethod @@ -93,7 +89,6 @@ def create_animal(animal_type): else: raise ValueError(f"Unknown animal: {animal_type}") """, - "observer": """ class Subject: def __init__(self): @@ -106,7 +101,6 @@ def notify(self, event): for observer in self._observers: observer.update(event) """, - "strategy": """ class SortContext: def __init__(self, strategy): @@ -115,7 +109,6 @@ def __init__(self, strategy): def sort(self, data): return self._strategy.sort(data) """, - "decorator_pattern": """ class Component: def operation(self): @@ -153,7 +146,6 @@ def check_permissions(self, user): pass def update_settings(self, key, val): pass def process_payment(self, amount): pass """, - "long_method": """ def process_order(order): # Validate order @@ -186,7 +178,6 @@ def process_order(order): return total """, - "duplicate_code": """ def calculate_circle_area(radius): return 3.14159 * radius * radius @@ -197,7 +188,6 @@ def calculate_circle_circumference(radius): def calculate_sphere_volume(radius): return (4/3) * 3.14159 * radius * radius * radius """, - "magic_numbers": """ def calculate_price(base): if base > 100: @@ -245,7 +235,6 @@ def process_data(data): return transform_data(data) """, }, - # Refactoring 2: Replace magic number with constant { "name": "Replace Magic Number", @@ -265,7 +254,6 @@ def calculate_discount(price): return 0 """, }, - # Refactoring 3: Replace conditional with polymorphism { "name": "Replace Conditional with Polymorphism", @@ -313,7 +301,6 @@ def linear_search(arr, target): return i return -1 """, - "binary_search_O_log_n": """ def binary_search(arr, target): left, right = 0, len(arr) - 1 @@ -327,7 +314,6 @@ def binary_search(arr, target): right = mid - 1 return -1 """, - "bubble_sort_O_n2": """ def bubble_sort(arr): n = len(arr) @@ -337,7 +323,6 @@ def bubble_sort(arr): arr[j], arr[j + 1] = arr[j + 1], arr[j] return arr """, - "merge_sort_O_n_log_n": """ def merge_sort(arr): if len(arr) <= 1: @@ -363,7 +348,6 @@ def merge(left, right): result.extend(right[j:]) return result """, - "constant_lookup_O_1": """ def get_value(dictionary, key): return dictionary.get(key) @@ -375,12 +359,13 @@ def get_value(dictionary, key): # TEST EXECUTION # ============================================================================= + def get_genome(result): """Create genome from LJPW result""" - L = result['ljpw']['L'] - J = result['ljpw']['J'] - P = result['ljpw']['P'] - W = result['ljpw']['W'] + L = result["ljpw"]["L"] + J = result["ljpw"]["J"] + P = result["ljpw"]["P"] + W = result["ljpw"]["W"] L_digit = int(round(L * 10)) % 10 J_digit = int(round(J * 10)) % 10 @@ -401,10 +386,10 @@ def test_edge_cases(): for name, code in EDGE_CASES.items(): result = analyze_quick(code) - l = result['ljpw']['L'] - j = result['ljpw']['J'] - p = result['ljpw']['P'] - w = result['ljpw']['W'] + l = result["ljpw"]["L"] + j = result["ljpw"]["J"] + p = result["ljpw"]["P"] + w = result["ljpw"]["W"] coords = (l, j, p, w) genome = get_genome(result) dist_to_ne = distance(coords, NATURAL_EQUILIBRIUM) @@ -415,12 +400,7 @@ def test_edge_cases(): print(f" Distance from NE: {dist_to_ne:.3f}") print() - results.append({ - 'name': name, - 'coords': coords, - 'genome': genome, - 'dist_to_ne': dist_to_ne - }) + results.append({"name": name, "coords": coords, "genome": genome, "dist_to_ne": dist_to_ne}) # Test passes if no exceptions were raised assert len(results) > 0 @@ -439,10 +419,10 @@ def test_design_patterns(): for name, code in DESIGN_PATTERNS.items(): result = analyze_quick(code) - l = result['ljpw']['L'] - j = result['ljpw']['J'] - p = result['ljpw']['P'] - w = result['ljpw']['W'] + l = result["ljpw"]["L"] + j = result["ljpw"]["J"] + p = result["ljpw"]["P"] + w = result["ljpw"]["W"] coords = (l, j, p, w) genome = get_genome(result) dist_to_ne = distance(coords, NATURAL_EQUILIBRIUM) @@ -453,12 +433,7 @@ def test_design_patterns(): print(f" Distance from NE: {dist_to_ne:.3f}") print() - results.append({ - 'name': name, - 'coords': coords, - 'genome': genome, - 'dist_to_ne': dist_to_ne - }) + results.append({"name": name, "coords": coords, "genome": genome, "dist_to_ne": dist_to_ne}) # Calculate average pairwise distance n = len(results) @@ -466,13 +441,15 @@ def test_design_patterns(): count = 0 for i in range(n): for j in range(i + 1, n): - d = distance(results[i]['coords'], results[j]['coords']) + d = distance(results[i]["coords"], results[j]["coords"]) total_dist += d count += 1 avg_pattern_distance = total_dist / count if count > 0 else 0 print(f"Average pairwise distance: {avg_pattern_distance:.3f}") - print(f"✓ Patterns cluster together" if avg_pattern_distance < 0.5 else "✗ Patterns are scattered") + print( + f"✓ Patterns cluster together" if avg_pattern_distance < 0.5 else "✗ Patterns are scattered" + ) print() # Test passes if no exceptions were raised @@ -492,10 +469,10 @@ def test_code_smells(): for name, code in CODE_SMELLS.items(): result = analyze_quick(code) - l = result['ljpw']['L'] - j = result['ljpw']['J'] - p = result['ljpw']['P'] - w = result['ljpw']['W'] + l = result["ljpw"]["L"] + j = result["ljpw"]["J"] + p = result["ljpw"]["P"] + w = result["ljpw"]["W"] coords = (l, j, p, w) genome = get_genome(result) dist_to_ne = distance(coords, NATURAL_EQUILIBRIUM) @@ -506,14 +483,9 @@ def test_code_smells(): print(f" Distance from NE: {dist_to_ne:.3f}") print() - results.append({ - 'name': name, - 'coords': coords, - 'genome': genome, - 'dist_to_ne': dist_to_ne - }) + results.append({"name": name, "coords": coords, "genome": genome, "dist_to_ne": dist_to_ne}) - avg_smell_distance = sum(r['dist_to_ne'] for r in results) / len(results) + avg_smell_distance = sum(r["dist_to_ne"] for r in results) / len(results) print(f"Average distance from NE: {avg_smell_distance:.3f}") print(f"✓ Code smells are far from NE" if avg_smell_distance > 0.8 else "✗ Code smells near NE") print() @@ -537,17 +509,25 @@ def test_refactoring_sequences(): degradations = 0 for refactoring in REFACTORING_BEFORE_AFTER: - name = refactoring['name'] - before_code = refactoring['before'] - after_code = refactoring['after'] + name = refactoring["name"] + before_code = refactoring["before"] + after_code = refactoring["after"] before_result = analyze_quick(before_code) after_result = analyze_quick(after_code) - before_coords = (before_result['ljpw']['L'], before_result['ljpw']['J'], - before_result['ljpw']['P'], before_result['ljpw']['W']) - after_coords = (after_result['ljpw']['L'], after_result['ljpw']['J'], - after_result['ljpw']['P'], after_result['ljpw']['W']) + before_coords = ( + before_result["ljpw"]["L"], + before_result["ljpw"]["J"], + before_result["ljpw"]["P"], + before_result["ljpw"]["W"], + ) + after_coords = ( + after_result["ljpw"]["L"], + after_result["ljpw"]["J"], + after_result["ljpw"]["P"], + after_result["ljpw"]["W"], + ) before_dist = distance(before_coords, NATURAL_EQUILIBRIUM) after_dist = distance(after_coords, NATURAL_EQUILIBRIUM) @@ -569,16 +549,20 @@ def test_refactoring_sequences(): else: degradations += 1 - results.append({ - 'name': name, - 'before_dist': before_dist, - 'after_dist': after_dist, - 'delta': delta, - 'moved_toward_ne': moved_toward - }) + results.append( + { + "name": name, + "before_dist": before_dist, + "after_dist": after_dist, + "delta": delta, + "moved_toward_ne": moved_toward, + } + ) success_rate = improvements / len(REFACTORING_BEFORE_AFTER) * 100 - print(f"Refactorings toward NE: {improvements}/{len(REFACTORING_BEFORE_AFTER)} ({success_rate:.0f}%)") + print( + f"Refactorings toward NE: {improvements}/{len(REFACTORING_BEFORE_AFTER)} ({success_rate:.0f}%)" + ) print(f"{'✓ HYPOTHESIS CONFIRMED' if success_rate >= 70 else '✗ HYPOTHESIS CHALLENGED'}") print() @@ -599,16 +583,16 @@ def test_algorithmic_complexity(): for name, code in ALGORITHMIC_COMPLEXITY.items(): result = analyze_quick(code) - l = result['ljpw']['L'] - j = result['ljpw']['J'] - p = result['ljpw']['P'] - w = result['ljpw']['W'] + l = result["ljpw"]["L"] + j = result["ljpw"]["J"] + p = result["ljpw"]["P"] + w = result["ljpw"]["W"] coords = (l, j, p, w) genome = get_genome(result) dist_to_ne = distance(coords, NATURAL_EQUILIBRIUM) # Extract complexity from name - complexity = name.split('_')[-1].replace('_', ' ') + complexity = name.split("_")[-1].replace("_", " ") print(f"{name} ({complexity}):") print(f" Genome: {genome}") @@ -616,13 +600,15 @@ def test_algorithmic_complexity(): print(f" Distance from NE: {dist_to_ne:.3f}") print() - results.append({ - 'name': name, - 'complexity': complexity, - 'coords': coords, - 'genome': genome, - 'dist_to_ne': dist_to_ne - }) + results.append( + { + "name": name, + "complexity": complexity, + "coords": coords, + "genome": genome, + "dist_to_ne": dist_to_ne, + } + ) # Test passes if no exceptions were raised assert len(results) > 0 @@ -658,7 +644,9 @@ def main(): print(f"✓ Tested {len(refactoring_results)} refactoring sequences") print(f"✓ Tested {len(complexity_results)} algorithmic complexities") print() - print(f"Total test cases: {len(edge_results) + len(pattern_results) + len(smell_results) + len(refactoring_results) + len(complexity_results)}") + print( + f"Total test cases: {len(edge_results) + len(pattern_results) + len(smell_results) + len(refactoring_results) + len(complexity_results)}" + ) print() diff --git a/tests/test_configurable_quantization.py b/tests/test_configurable_quantization.py index 6e7e34a..db0e198 100644 --- a/tests/test_configurable_quantization.py +++ b/tests/test_configurable_quantization.py @@ -5,18 +5,20 @@ """ import math + from ljpw_semantic_compressor import ( + LJPWCodon, + LJPWQuantizer, SemanticCompressor, SemanticDecompressor, - LJPWQuantizer, - LJPWCodon, ) + def test_invalid_levels(): """Test that invalid quantization levels are properly rejected""" - print("="*70) + print("=" * 70) print("TEST 1: Invalid Quantization Levels") - print("="*70) + print("=" * 70) invalid_cases = [ (3, "Not a power of 2"), @@ -44,15 +46,15 @@ def test_invalid_levels(): def test_recommend_levels(): """Test the recommend_levels helper""" - print("\n" + "="*70) + print("\n" + "=" * 70) print("TEST 2: Recommendation System") - print("="*70) + print("=" * 70) test_cases = [ - ('fast', 4), - ('balanced', 8), - ('precise', 16), - ('exact', 32), + ("fast", 4), + ("balanced", 8), + ("precise", 16), + ("exact", 32), ] passed = 0 @@ -66,7 +68,7 @@ def test_recommend_levels(): # Test invalid use case try: - LJPWQuantizer.recommend_levels('invalid') + LJPWQuantizer.recommend_levels("invalid") print("✗ FAIL: Invalid use case should raise ValueError") assert False, "Invalid use case should raise ValueError" except ValueError: @@ -79,14 +81,14 @@ def test_recommend_levels(): def calculate_error(original, reconstructed): """Calculate Euclidean distance between states""" - return math.sqrt(sum((o - r)**2 for o, r in zip(original, reconstructed))) + return math.sqrt(sum((o - r) ** 2 for o, r in zip(original, reconstructed))) def test_quantization_accuracy(): """Test compression accuracy at different quantization levels""" - print("\n" + "="*70) + print("\n" + "=" * 70) print("TEST 3: Quantization Accuracy vs Levels") - print("="*70) + print("=" * 70) # Test states covering different value ranges test_states = [ @@ -131,28 +133,32 @@ def test_quantization_accuracy(): print(f"{levels:<10} {avg_error:<12.4f} {max_error:<12.4f} {genome_size:<12} {status:<10}") results[levels] = { - 'avg_error': avg_error, - 'max_error': max_error, - 'genome_size': genome_size, + "avg_error": avg_error, + "max_error": max_error, + "genome_size": genome_size, } # Verify that higher levels = better accuracy print("\nAccuracy Improvement Analysis:") print("-" * 70) - prev_error = float('inf') + prev_error = float("inf") monotonic = True for levels in levels_to_test: - current_error = results[levels]['avg_error'] - improvement = ((prev_error - current_error) / prev_error * 100) if prev_error != float('inf') else 0 + current_error = results[levels]["avg_error"] + improvement = ( + ((prev_error - current_error) / prev_error * 100) if prev_error != float("inf") else 0 + ) - if current_error >= prev_error and prev_error != float('inf'): + if current_error >= prev_error and prev_error != float("inf"): monotonic = False print(f"{levels} levels: {current_error:.4f} error (✗ WORSE than previous!)") else: - if prev_error != float('inf'): - print(f"{levels} levels: {current_error:.4f} error ({improvement:.1f}% improvement)") + if prev_error != float("inf"): + print( + f"{levels} levels: {current_error:.4f} error ({improvement:.1f}% improvement)" + ) else: print(f"{levels} levels: {current_error:.4f} error (baseline)") @@ -168,9 +174,9 @@ def test_quantization_accuracy(): def test_round_trip_all_levels(): """Test round-trip compression at all quantization levels""" - print("\n" + "="*70) + print("\n" + "=" * 70) print("TEST 4: Round-Trip Integrity (All Levels)") - print("="*70) + print("=" * 70) trajectory = [ (0.2, 0.3, 0.9, 0.2), @@ -187,8 +193,7 @@ def test_round_trip_all_levels(): # Compress genome = compressor.compress_state_sequence( - trajectory, - metadata={'test': f'{levels}-level quantization'} + trajectory, metadata={"test": f"{levels}-level quantization"} ) # Decompress @@ -199,13 +204,15 @@ def test_round_trip_all_levels(): # Check integrity passed = ( - len(reconstructed) == len(trajectory) and - validation['valid'] and - validation['integrity_score'] == 1.0 + len(reconstructed) == len(trajectory) + and validation["valid"] + and validation["integrity_score"] == 1.0 ) status = "✓ PASS" if passed else "✗ FAIL" - print(f"{levels:2} levels: {status} - {len(reconstructed)} states, {validation['integrity_score']:.1%} integrity") + print( + f"{levels:2} levels: {status} - {len(reconstructed)} states, {validation['integrity_score']:.1%} integrity" + ) if not passed: all_passed = False @@ -215,9 +222,9 @@ def test_round_trip_all_levels(): def test_codon_parsing_edge_cases(): """Test that codon parsing handles edge cases properly""" - print("\n" + "="*70) + print("\n" + "=" * 70) print("TEST 5: Codon Parsing Edge Cases") - print("="*70) + print("=" * 70) valid_cases = [ "L0J0P0", @@ -263,9 +270,9 @@ def test_codon_parsing_edge_cases(): def main(): """Run all tests""" - print("\n" + "="*70) + print("\n" + "=" * 70) print("CONFIGURABLE QUANTIZATION TEST SUITE") - print("="*70) + print("=" * 70) print("\nTesting configurable precision levels in LJPW semantic compression\n") tests = [ @@ -285,13 +292,14 @@ def main(): except Exception as e: print(f"\n✗ EXCEPTION in {name}: {e}") import traceback + traceback.print_exc() results.append((name, False)) # Summary - print("\n" + "="*70) + print("\n" + "=" * 70) print("TEST SUMMARY") - print("="*70) + print("=" * 70) for name, passed in results: status = "✓ PASS" if passed else "✗ FAIL" @@ -300,9 +308,9 @@ def main(): total = len(results) passed_count = sum(1 for _, passed in results if passed) - print("\n" + "="*70) + print("\n" + "=" * 70) print(f"OVERALL: {passed_count}/{total} test suites passed") - print("="*70) + print("=" * 70) if passed_count == total: print("\n✅ ALL TESTS PASSED - Configurable quantization is working correctly!") @@ -312,5 +320,5 @@ def main(): return 1 -if __name__ == '__main__': +if __name__ == "__main__": exit(main()) diff --git a/tests/test_cross_language.py b/tests/test_cross_language.py index 997e948..570a1a6 100644 --- a/tests/test_cross_language.py +++ b/tests/test_cross_language.py @@ -11,7 +11,7 @@ import sys from pathlib import Path -sys.path.insert(0, str(Path(__file__).parent.parent / 'src' / 'ljpw')) +sys.path.insert(0, str(Path(__file__).parent.parent / "src" / "ljpw")) from ljpw_standalone import analyze_quick, calculate_distance # ============================================================================ @@ -139,6 +139,7 @@ class Calculator { # RUN TESTS # ============================================================================ + def test_cross_language(): print("\n" + "=" * 70) print("CROSS-LANGUAGE SEMANTIC CONSISTENCY TEST") @@ -147,42 +148,47 @@ def test_cross_language(): print() test_cases = [ - ("Simple Addition", [ - ("Python", python_add), - ("JavaScript", javascript_add), - ("Rust", rust_add), - ("C++", cpp_add), - ("Go", go_add), - ("Ruby", ruby_add), - ("Swift", swift_add), - ("Kotlin", kotlin_add) - ]), - ("Input Validation", [ - ("Python", python_validate), - ("JavaScript", javascript_validate), - ("TypeScript", typescript_validate) - ]), - ("Class/OOP", [ - ("Python", python_class), - ("JavaScript", javascript_class), - ("Java", java_class) - ]) + ( + "Simple Addition", + [ + ("Python", python_add), + ("JavaScript", javascript_add), + ("Rust", rust_add), + ("C++", cpp_add), + ("Go", go_add), + ("Ruby", ruby_add), + ("Swift", swift_add), + ("Kotlin", kotlin_add), + ], + ), + ( + "Input Validation", + [ + ("Python", python_validate), + ("JavaScript", javascript_validate), + ("TypeScript", typescript_validate), + ], + ), + ( + "Class/OOP", + [("Python", python_class), ("JavaScript", javascript_class), ("Java", java_class)], + ), ] for test_name, cases in test_cases: print(f"\n{'─' * 70}") print(f"Test: {test_name}") - print('─' * 70) + print("─" * 70) results = [] for lang, code in cases: result = analyze_quick(code) results.append((lang, result)) - L = result['ljpw']['L'] - J = result['ljpw']['J'] - P = result['ljpw']['P'] - W = result['ljpw']['W'] + L = result["ljpw"]["L"] + J = result["ljpw"]["J"] + P = result["ljpw"]["P"] + W = result["ljpw"]["W"] # Create genome from coordinates (quantize to 0-9) L_digit = int(round(L * 10)) % 10 @@ -199,14 +205,12 @@ def test_cross_language(): print(f"\nPairwise Semantic Distances:") n = len(results) for i in range(n): - for j in range(i+1, n): + for j in range(i + 1, n): lang1, r1 = results[i] lang2, r2 = results[j] - coords1 = (r1['ljpw']['L'], r1['ljpw']['J'], - r1['ljpw']['P'], r1['ljpw']['W']) - coords2 = (r2['ljpw']['L'], r2['ljpw']['J'], - r2['ljpw']['P'], r2['ljpw']['W']) + coords1 = (r1["ljpw"]["L"], r1["ljpw"]["J"], r1["ljpw"]["P"], r1["ljpw"]["W"]) + coords2 = (r2["ljpw"]["L"], r2["ljpw"]["J"], r2["ljpw"]["P"], r2["ljpw"]["W"]) distance = calculate_distance(coords1, coords2) @@ -222,7 +226,8 @@ def test_cross_language(): print("\n" + "=" * 70) print("CONCLUSION") print("=" * 70) - print(""" + print( + """ If distances are consistently low (<0.3), this provides evidence that: 1. LJPW captures language-independent semantic meaning @@ -231,8 +236,10 @@ def test_cross_language(): This would support the "Medium" or "Deep" interpretation in SEMANTIC_COMPRESSION_DEEP_MECHANICS.md - """) + """ + ) print("=" * 70 + "\n") -if __name__ == '__main__': + +if __name__ == "__main__": test_cross_language() diff --git a/tests/test_distance.py b/tests/test_distance.py index 230d76f..f4189e2 100644 --- a/tests/test_distance.py +++ b/tests/test_distance.py @@ -11,19 +11,20 @@ from pathlib import Path # Import from ljpw_standalone -sys.path.insert(0, str(Path(__file__).parent.parent / 'src' / 'ljpw')) +sys.path.insert(0, str(Path(__file__).parent.parent / "src" / "ljpw")) from ljpw_standalone import ( + calculate_batch_distance, calculate_distance, calculate_file_distance, - calculate_batch_distance, + format_batch_distance_result_json, format_distance_result_json, - format_batch_distance_result_json ) # ============================================================================ # TEST SUITE # ============================================================================ + class DistanceTestRunner: def __init__(self): self.passed = 0 @@ -38,7 +39,9 @@ def assert_equal(self, actual, expected, test_name, tolerance=0.001): return True else: self.failed += 1 - self.tests.append((test_name, False, f"✗ {test_name}: expected {expected}, got {actual}")) + self.tests.append( + (test_name, False, f"✗ {test_name}: expected {expected}, got {actual}") + ) return False def assert_true(self, condition, test_name): @@ -74,6 +77,7 @@ def print_results(self): print(f"\n❌ {self.failed} TESTS FAILED") return 1 + def main(): test = DistanceTestRunner() @@ -113,8 +117,7 @@ def main(): d13 = calculate_distance(coords1, coords3) # Triangle inequality: d13 <= d12 + d23 - test.assert_true(d13 <= d12 + d23 + 0.001, - "Triangle inequality holds") + test.assert_true(d13 <= d12 + d23 + 0.001, "Triangle inequality holds") # ======================================================================== # Test 5: Symmetry @@ -149,15 +152,15 @@ def main(): if Path(file1).exists() and Path(file2).exists(): result = calculate_file_distance(file1, file2) - test.assert_true('error' not in result, "File distance calculation succeeds") - test.assert_true('distance' in result, "Result contains distance") - test.assert_true('similarity' in result, "Result contains similarity") - test.assert_true('interpretation' in result, "Result contains interpretation") - test.assert_true(result['distance'] >= 0, "Distance is non-negative") + test.assert_true("error" not in result, "File distance calculation succeeds") + test.assert_true("distance" in result, "Result contains distance") + test.assert_true("similarity" in result, "Result contains similarity") + test.assert_true("interpretation" in result, "Result contains interpretation") + test.assert_true(result["distance"] >= 0, "Distance is non-negative") # Check coordinates are tuples of 4 floats - test.assert_true(len(result['coords1']) == 4, "Coords1 has 4 dimensions") - test.assert_true(len(result['coords2']) == 4, "Coords2 has 4 dimensions") + test.assert_true(len(result["coords1"]) == 4, "Coords1 has 4 dimensions") + test.assert_true(len(result["coords2"]) == 4, "Coords2 has 4 dimensions") else: print(f"\n⚠ Skipping file distance test (files not found)") @@ -205,11 +208,11 @@ def main(): # Parse JSON to verify it's valid try: parsed = json.loads(json_output) - test.assert_true('file1' in parsed, "JSON contains file1") - test.assert_true('file2' in parsed, "JSON contains file2") - test.assert_true('coordinates' in parsed, "JSON contains coordinates") - test.assert_true('distance' in parsed, "JSON contains distance") - test.assert_true('similarity' in parsed, "JSON contains similarity") + test.assert_true("file1" in parsed, "JSON contains file1") + test.assert_true("file2" in parsed, "JSON contains file2") + test.assert_true("coordinates" in parsed, "JSON contains coordinates") + test.assert_true("distance" in parsed, "JSON contains distance") + test.assert_true("similarity" in parsed, "JSON contains similarity") except json.JSONDecodeError: test.assert_true(False, "JSON output is valid JSON") else: @@ -227,15 +230,15 @@ def main(): if len(test_files) >= 3: batch_result = calculate_batch_distance(test_files) - test.assert_true('error' not in batch_result, "Batch calculation succeeds") - test.assert_true('files' in batch_result, "Batch result contains files") - test.assert_true('distance_matrix' in batch_result, "Batch result contains distance matrix") - test.assert_true('most_similar' in batch_result, "Batch result contains most_similar") - test.assert_true('most_different' in batch_result, "Batch result contains most_different") + test.assert_true("error" not in batch_result, "Batch calculation succeeds") + test.assert_true("files" in batch_result, "Batch result contains files") + test.assert_true("distance_matrix" in batch_result, "Batch result contains distance matrix") + test.assert_true("most_similar" in batch_result, "Batch result contains most_similar") + test.assert_true("most_different" in batch_result, "Batch result contains most_different") # Verify distance matrix dimensions n = len(test_files) - matrix = batch_result['distance_matrix'] + matrix = batch_result["distance_matrix"] test.assert_true(len(matrix) == n, f"Distance matrix has {n} rows") test.assert_true(len(matrix[0]) == n, f"Distance matrix has {n} columns") @@ -259,11 +262,11 @@ def main(): # Parse JSON to verify it's valid try: parsed = json.loads(json_output) - test.assert_true('files' in parsed, "Batch JSON contains files") - test.assert_true('coordinates' in parsed, "Batch JSON contains coordinates") - test.assert_true('distance_matrix' in parsed, "Batch JSON contains distance_matrix") - test.assert_true('most_similar' in parsed, "Batch JSON contains most_similar") - test.assert_true('most_different' in parsed, "Batch JSON contains most_different") + test.assert_true("files" in parsed, "Batch JSON contains files") + test.assert_true("coordinates" in parsed, "Batch JSON contains coordinates") + test.assert_true("distance_matrix" in parsed, "Batch JSON contains distance_matrix") + test.assert_true("most_similar" in parsed, "Batch JSON contains most_similar") + test.assert_true("most_different" in parsed, "Batch JSON contains most_different") except json.JSONDecodeError: test.assert_true(False, "Batch JSON output is valid JSON") else: @@ -275,16 +278,17 @@ def main(): if len(test_files) >= 3: batch_result = calculate_batch_distance(test_files) - most_sim = batch_result['most_similar'] - most_diff = batch_result['most_different'] + most_sim = batch_result["most_similar"] + most_diff = batch_result["most_different"] test.assert_true(most_sim is not None, "Most similar pair found") test.assert_true(most_diff is not None, "Most different pair found") # Most similar should have smaller distance than most different if most_sim and most_diff: - test.assert_true(most_sim[2] <= most_diff[2], - "Most similar distance <= most different distance") + test.assert_true( + most_sim[2] <= most_diff[2], "Most similar distance <= most different distance" + ) else: print(f"\n⚠ Skipping most similar/different test (need at least 3 files)") @@ -292,9 +296,10 @@ def main(): # Test 15: Batch Error Handling (< 2 files) # ======================================================================== batch_result = calculate_batch_distance(["single_file.py"]) - test.assert_true('error' in batch_result, "Batch with <2 files returns error") + test.assert_true("error" in batch_result, "Batch with <2 files returns error") return test.print_results() -if __name__ == '__main__': + +if __name__ == "__main__": sys.exit(main()) diff --git a/tests/test_edge_cases.py b/tests/test_edge_cases.py index 0c63ed7..93c2e3b 100644 --- a/tests/test_edge_cases.py +++ b/tests/test_edge_cases.py @@ -4,38 +4,39 @@ Tests boundary conditions, error handling, and unusual inputs """ -import pytest import sys from pathlib import Path +import pytest + # Add parent directory to path sys.path.insert(0, str(Path(__file__).parent.parent)) -from src.ljpw.ljpw_standalone import analyze_file, analyze_quick, analyze_directory +from src.ljpw.ljpw_standalone import analyze_directory, analyze_file, analyze_quick class TestEdgeCases: """Test edge cases and error conditions""" - + def test_empty_code(self): """Test analyzing empty code string""" result = analyze_quick("") - assert result['lines'] == 0 - assert result['health'] == 0 - assert 'Empty file' in result['insights'][0] - + assert result["lines"] == 0 + assert result["health"] == 0 + assert "Empty file" in result["insights"][0] + def test_none_input(self): """Test analyzing None input""" result = analyze_quick(None) - assert 'error' in result - assert result['error'] == 'None input' - + assert "error" in result + assert result["error"] == "None input" + def test_invalid_type(self): """Test analyzing non-string input""" result = analyze_quick(123) - assert 'error' in result - assert 'Invalid type' in result['error'] - + assert "error" in result + assert "Invalid type" in result["error"] + def test_only_comments(self): """Test file with only comments, no code""" code = """ @@ -44,22 +45,22 @@ def test_only_comments(self): # More comments """ result = analyze_quick(code) - assert result['lines'] == 0 - assert 'Empty file' in result['insights'][0] - + assert result["lines"] == 0 + assert "Empty file" in result["insights"][0] + def test_only_whitespace(self): """Test file with only whitespace""" code = "\n\n \n\t\t\n " result = analyze_quick(code) - assert result['lines'] == 0 - + assert result["lines"] == 0 + def test_very_long_line(self): """Test code with extremely long lines""" code = "x = " + "1 + " * 10000 + "1" result = analyze_quick(code) - assert 'ljpw' in result - assert result['lines'] > 0 - + assert "ljpw" in result + assert result["lines"] > 0 + def test_unicode_characters(self): """Test code with unicode characters""" code = """ @@ -68,16 +69,16 @@ def greet(): return "Success ✓" """ result = analyze_quick(code) - assert 'ljpw' in result - assert result['lines'] > 0 - + assert "ljpw" in result + assert result["lines"] > 0 + def test_mixed_line_endings(self): """Test code with mixed line endings (CRLF/LF)""" code = "def foo():\r\n pass\nreturn 1" result = analyze_quick(code) - assert 'ljpw' in result - assert result['lines'] > 0 - + assert "ljpw" in result + assert result["lines"] > 0 + def test_nested_strings(self): """Test code with heavily nested strings and quotes""" code = ''' @@ -88,73 +89,73 @@ def test(): quote""" ''' result = analyze_quick(code) - assert 'ljpw' in result - assert result['lines'] > 0 + assert "ljpw" in result + assert result["lines"] > 0 class TestFileEdgeCases: """Test file-related edge cases""" - + def test_nonexistent_file(self): """Test analyzing non-existent file""" result = analyze_file("/nonexistent/file.py") - assert 'error' in result - assert result['error'] == 'File not found' - + assert "error" in result + assert result["error"] == "File not found" + def test_directory_not_file(self, tmp_path): """Test analyzing directory path instead of file""" test_dir = tmp_path / "test_dir" test_dir.mkdir() - + result = analyze_file(str(test_dir)) - assert 'error' in result - assert result['error'] == 'Not a file' - + assert "error" in result + assert result["error"] == "Not a file" + def test_empty_file(self, tmp_path): """Test analyzing empty file""" empty_file = tmp_path / "empty.py" empty_file.write_text("") - + result = analyze_file(str(empty_file)) - assert result['lines'] == 0 - assert result['health'] == 0 - + assert result["lines"] == 0 + assert result["health"] == 0 + def test_file_with_only_newlines(self, tmp_path): """Test file with only newline characters""" newline_file = tmp_path / "newlines.py" newline_file.write_text("\n\n\n\n\n") - + result = analyze_file(str(newline_file)) - assert result['lines'] == 0 + assert result["lines"] == 0 class TestDirectoryEdgeCases: """Test directory analysis edge cases""" - + def test_empty_directory(self, tmp_path): """Test analyzing empty directory""" results = analyze_directory(str(tmp_path), show_progress=False) assert len(results) == 0 - + def test_directory_with_no_code_files(self, tmp_path): """Test directory with only non-code files""" (tmp_path / "test.txt").write_text("Not code") (tmp_path / "data.json").write_text("{}") - + results = analyze_directory(str(tmp_path), show_progress=False) assert len(results) == 0 - + def test_nested_directories(self, tmp_path): """Test analyzing nested directory structure""" # Create nested structure (tmp_path / "level1").mkdir() (tmp_path / "level1" / "level2").mkdir() (tmp_path / "level1" / "level2" / "test.py").write_text("def foo(): pass") - + results = analyze_directory(str(tmp_path), show_progress=False) assert len(results) == 1 - assert results[0]['lines'] > 0 - + assert results[0]["lines"] > 0 + def test_nonexistent_directory(self): """Test analyzing non-existent directory""" results = analyze_directory("/nonexistent/directory", show_progress=False) @@ -163,48 +164,48 @@ def test_nonexistent_directory(self): class TestBoundaryValues: """Test boundary values and limits""" - + def test_minimal_code(self): """Test minimal valid code""" result = analyze_quick("x=1") - assert 'ljpw' in result - assert result['lines'] == 1 - + assert "ljpw" in result + assert result["lines"] == 1 + def test_single_character(self): """Test single character""" result = analyze_quick("#") - assert result['lines'] == 0 # Comment only - + assert result["lines"] == 0 # Comment only + def test_maximum_nesting(self): """Test deeply nested code""" code = "if True:\n" + (" if True:\n" * 50) + " " * 50 + "pass" result = analyze_quick(code) - assert 'ljpw' in result - assert result['lines'] > 0 - + assert "ljpw" in result + assert result["lines"] > 0 + def test_many_functions(self): """Test file with many functions""" code = "\n".join([f"def func{i}(): pass" for i in range(1000)]) result = analyze_quick(code) - assert 'ljpw' in result - assert result['health'] > 0 # Should have some wisdom score + assert "ljpw" in result + assert result["health"] > 0 # Should have some wisdom score class TestSpecialCharacters: """Test handling of special characters""" - + def test_tabs_vs_spaces(self): """Test code with tabs vs spaces""" code_spaces = "def foo():\n pass" code_tabs = "def foo():\n\tpass" - + result_spaces = analyze_quick(code_spaces) result_tabs = analyze_quick(code_tabs) - + # Should analyze both successfully - assert 'ljpw' in result_spaces - assert 'ljpw' in result_tabs - + assert "ljpw" in result_spaces + assert "ljpw" in result_tabs + def test_special_regex_characters(self): """Test code that might break regex patterns""" code = """ @@ -215,13 +216,13 @@ def test(): return True """ result = analyze_quick(code) - assert 'ljpw' in result - assert result['lines'] > 0 + assert "ljpw" in result + assert result["lines"] > 0 class TestRealWorldScenarios: """Test realistic edge cases from actual usage""" - + def test_code_with_todos(self): """Test code with TODO comments""" code = """ @@ -232,10 +233,10 @@ def process(): pass """ result = analyze_quick(code) - assert 'ljpw' in result + assert "ljpw" in result # Should have low safety score (no error handling) - assert result['ljpw']['L'] < 0.3 - + assert result["ljpw"]["L"] < 0.3 + def test_generated_code(self): """Test auto-generated code patterns""" code = """ @@ -247,14 +248,14 @@ def getter_1(): return field_1 def getter_2(): return field_2 """ result = analyze_quick(code) - assert 'ljpw' in result - + assert "ljpw" in result + def test_minified_code(self): """Test minified/compressed code""" code = "def f(x):return x*2 if x>0 else 0" result = analyze_quick(code) - assert 'ljpw' in result - assert result['lines'] == 1 + assert "ljpw" in result + assert result["lines"] == 1 def test_all_edge_cases(): diff --git a/tests/test_ljpw_baselines_v4.py b/tests/test_ljpw_baselines_v4.py index ddc65aa..edea033 100644 --- a/tests/test_ljpw_baselines_v4.py +++ b/tests/test_ljpw_baselines_v4.py @@ -10,7 +10,7 @@ from pathlib import Path # Add src to path -sys.path.insert(0, str(Path(__file__).parent.parent / 'src' / 'ljpw')) +sys.path.insert(0, str(Path(__file__).parent.parent / "src" / "ljpw")) # Import without numpy-dependent components import math @@ -59,9 +59,7 @@ def distance_from_anchor(L, J, P, W): def distance_from_ne(L, J, P, W): NE = (0.618034, 0.414214, 0.718282, 0.693147) - return math.sqrt( - (NE[0] - L) ** 2 + (NE[1] - J) ** 2 + (NE[2] - P) ** 2 + (NE[3] - W) ** 2 - ) + return math.sqrt((NE[0] - L) ** 2 + (NE[1] - J) ** 2 + (NE[2] - P) ** 2 + (NE[3] - W) ** 2) # Test 1: Anchor to Anchor should be 0 assert distance_from_anchor(1, 1, 1, 1) == 0 diff --git a/tests/test_ljpw_framework.py b/tests/test_ljpw_framework.py index d36e059..8ca3adc 100644 --- a/tests/test_ljpw_framework.py +++ b/tests/test_ljpw_framework.py @@ -10,13 +10,13 @@ 6. Edge Cases """ +# Import framework components +import importlib.util import math import random import time -from typing import List, Tuple, Dict +from typing import Dict, List, Tuple -# Import framework components -import importlib.util def load_module(name, path): spec = importlib.util.spec_from_file_location(name, path) @@ -24,11 +24,17 @@ def load_module(name, path): spec.loader.exec_module(module) return module + # Load modules import os -base_path = os.path.join(os.path.dirname(__file__), '..', 'src', 'ljpw') -compiler_mod = load_module("ljpw_semantic_compiler", os.path.join(base_path, "ljpw_semantic_compiler.py")) -compressor_mod = load_module("ljpw_semantic_compressor", os.path.join(base_path, "ljpw_semantic_compressor.py")) + +base_path = os.path.join(os.path.dirname(__file__), "..", "src", "ljpw") +compiler_mod = load_module( + "ljpw_semantic_compiler", os.path.join(base_path, "ljpw_semantic_compiler.py") +) +compressor_mod = load_module( + "ljpw_semantic_compressor", os.path.join(base_path, "ljpw_semantic_compressor.py") +) expander_mod = load_module("ljpw_expander", os.path.join(base_path, "ljpw_expander.py")) pipeline_mod = load_module("ljpw_pipeline", os.path.join(base_path, "ljpw_pipeline.py")) @@ -45,6 +51,7 @@ def load_module(name, path): # TEST SUITE 1: DNA-LJPW CORRESPONDENCE PROPERTIES # ============================================================================ + class DNACorrespondenceSuite: """Test the DNA-LJPW correspondence claims""" @@ -53,9 +60,9 @@ def __init__(self): def test_conservation_laws(self): """Test P≈W pairing (like Chargaff's rule)""" - print("\n" + "="*70) + print("\n" + "=" * 70) print("TEST 1: Conservation Laws (P~=W Pairing)") - print("="*70) + print("=" * 70) L, J, P, W = NE pw_diff = abs(P - W) @@ -70,22 +77,21 @@ def test_conservation_laws(self): passed = pw_diff < threshold print(f"\nThreshold: {threshold}") - print(f"Result: {'PASS' if passed else 'FAIL'} - P and W are {'nearly equal' if passed else 'different'}") + print( + f"Result: {'PASS' if passed else 'FAIL'} - P and W are {'nearly equal' if passed else 'different'}" + ) - self.results.append({ - 'test': 'P≈W Conservation', - 'passed': passed, - 'value': pw_diff, - 'threshold': threshold - }) + self.results.append( + {"test": "P≈W Conservation", "passed": passed, "value": pw_diff, "threshold": threshold} + ) return passed def test_entropy_efficiency(self): """Test that LJPW has near-optimal entropy""" - print("\n" + "="*70) + print("\n" + "=" * 70) print("TEST 2: Information Entropy Efficiency") - print("="*70) + print("=" * 70) L, J, P, W = NE total = L + J + P + W @@ -98,10 +104,10 @@ def test_entropy_efficiency(self): # Shannon entropy entropy = -( - freq_L * math.log2(freq_L) + - freq_J * math.log2(freq_J) + - freq_P * math.log2(freq_P) + - freq_W * math.log2(freq_W) + freq_L * math.log2(freq_L) + + freq_J * math.log2(freq_J) + + freq_P * math.log2(freq_P) + + freq_W * math.log2(freq_W) ) max_entropy = math.log2(4) # 2.0 bits for uniform distribution @@ -116,28 +122,32 @@ def test_entropy_efficiency(self): passed = efficiency > threshold print(f"\nThreshold: {threshold:.2%}") - print(f"Result: {'PASS' if passed else 'FAIL'} - Entropy is {'near-optimal' if passed else 'suboptimal'}") + print( + f"Result: {'PASS' if passed else 'FAIL'} - Entropy is {'near-optimal' if passed else 'suboptimal'}" + ) - self.results.append({ - 'test': 'Entropy Efficiency', - 'passed': passed, - 'value': efficiency, - 'threshold': threshold - }) + self.results.append( + { + "test": "Entropy Efficiency", + "passed": passed, + "value": efficiency, + "threshold": threshold, + } + ) return passed def test_gc_content_analog(self): """Test asymmetric distribution (GC content analog)""" - print("\n" + "="*70) + print("\n" + "=" * 70) print("TEST 3: Asymmetric Distribution (GC Content Analog)") - print("="*70) + print("=" * 70) L, J, P, W = NE total = L + J + P + W high_value = P + W # "GC" analog - low_value = L + J # "AT" analog + low_value = L + J # "AT" analog gc_ratio = high_value / total @@ -151,22 +161,26 @@ def test_gc_content_analog(self): passed = min_threshold <= gc_ratio <= max_threshold print(f"\nBiological range: {min_threshold:.0%} - {max_threshold:.0%}") - print(f"Result: {'PASS' if passed else 'FAIL'} - Ratio is {'within' if passed else 'outside'} biological range") + print( + f"Result: {'PASS' if passed else 'FAIL'} - Ratio is {'within' if passed else 'outside'} biological range" + ) - self.results.append({ - 'test': 'GC Content Analog', - 'passed': passed, - 'value': gc_ratio, - 'threshold': f"{min_threshold}-{max_threshold}" - }) + self.results.append( + { + "test": "GC Content Analog", + "passed": passed, + "value": gc_ratio, + "threshold": f"{min_threshold}-{max_threshold}", + } + ) return passed def run_all_tests(self): """Run all DNA correspondence tests""" - print("\n" + "#"*70) + print("\n" + "#" * 70) print("# DNA-LJPW CORRESPONDENCE TEST SUITE") - print("#"*70) + print("#" * 70) tests = [ self.test_conservation_laws, @@ -179,16 +193,18 @@ def run_all_tests(self): if test(): passed += 1 - print("\n" + "="*70) + print("\n" + "=" * 70) print(f"DNA CORRESPONDENCE TESTS: {passed}/{len(tests)} PASSED") - print("="*70) + print("=" * 70) return passed, len(tests) + # ============================================================================ # TEST SUITE 2: COMPRESSION/DECOMPRESSION FIDELITY # ============================================================================ + class CompressionFidelitySuite: """Test compression and decompression accuracy""" @@ -197,9 +213,9 @@ def __init__(self): def test_round_trip_accuracy(self): """Test that compress -> decompress preserves data""" - print("\n" + "="*70) + print("\n" + "=" * 70) print("TEST 4: Round-Trip Compression Accuracy") - print("="*70) + print("=" * 70) # Create test trajectory original_trajectory = [ @@ -226,7 +242,7 @@ def test_round_trip_accuracy(self): # Calculate error total_error = 0 for orig, recon in zip(original_trajectory, reconstructed): - error = math.sqrt(sum((o - r)**2 for o, r in zip(orig, recon))) + error = math.sqrt(sum((o - r) ** 2 for o, r in zip(orig, recon))) total_error += error avg_error = total_error / len(original_trajectory) @@ -239,22 +255,26 @@ def test_round_trip_accuracy(self): passed = avg_error < threshold print(f"\nThreshold: {threshold:.2%}") - print(f"Result: {'PASS' if passed else 'FAIL'} - Reconstruction is {'accurate' if passed else 'inaccurate'}") + print( + f"Result: {'PASS' if passed else 'FAIL'} - Reconstruction is {'accurate' if passed else 'inaccurate'}" + ) - self.results.append({ - 'test': 'Round-Trip Accuracy', - 'passed': passed, - 'value': avg_error, - 'threshold': threshold - }) + self.results.append( + { + "test": "Round-Trip Accuracy", + "passed": passed, + "value": avg_error, + "threshold": threshold, + } + ) return passed def test_error_correction(self): """Test that error correction via pairing works""" - print("\n" + "="*70) + print("\n" + "=" * 70) print("TEST 5: Error Correction via Complementary Pairing") - print("="*70) + print("=" * 70) # Create test trajectory trajectory = [(0.5, 0.4, 0.7, 0.6) for _ in range(10)] @@ -273,31 +293,40 @@ def test_error_correction(self): # Test: should have 100% integrity threshold = 1.0 - passed = validation['integrity_score'] >= threshold + passed = validation["integrity_score"] >= threshold print(f"\nThreshold: {threshold:.0%}") - print(f"Result: {'PASS' if passed else 'FAIL'} - Integrity is {'perfect' if passed else 'compromised'}") + print( + f"Result: {'PASS' if passed else 'FAIL'} - Integrity is {'perfect' if passed else 'compromised'}" + ) - self.results.append({ - 'test': 'Error Correction', - 'passed': passed, - 'value': validation['integrity_score'], - 'threshold': threshold - }) + self.results.append( + { + "test": "Error Correction", + "passed": passed, + "value": validation["integrity_score"], + "threshold": threshold, + } + ) return passed def test_compression_ratio(self): """Test that compression achieves expected ratios""" - print("\n" + "="*70) + print("\n" + "=" * 70) print("TEST 6: Compression Ratio Performance") - print("="*70) + print("=" * 70) # Large trajectory - trajectory = [(random.uniform(0.3, 0.9), - random.uniform(0.3, 0.8), - random.uniform(0.4, 0.9), - random.uniform(0.3, 0.8)) for _ in range(100)] + trajectory = [ + ( + random.uniform(0.3, 0.9), + random.uniform(0.3, 0.8), + random.uniform(0.4, 0.9), + random.uniform(0.3, 0.8), + ) + for _ in range(100) + ] compressor = SemanticCompressor(quantization_levels=4) genome = compressor.compress_state_sequence(trajectory) @@ -317,22 +346,21 @@ def test_compression_ratio(self): passed = ratio > threshold print(f"\nThreshold: {threshold:.1f}x") - print(f"Result: {'PASS' if passed else 'FAIL'} - Compression is {'sufficient' if passed else 'insufficient'}") + print( + f"Result: {'PASS' if passed else 'FAIL'} - Compression is {'sufficient' if passed else 'insufficient'}" + ) - self.results.append({ - 'test': 'Compression Ratio', - 'passed': passed, - 'value': ratio, - 'threshold': threshold - }) + self.results.append( + {"test": "Compression Ratio", "passed": passed, "value": ratio, "threshold": threshold} + ) return passed def run_all_tests(self): """Run all compression tests""" - print("\n" + "#"*70) + print("\n" + "#" * 70) print("# COMPRESSION FIDELITY TEST SUITE") - print("#"*70) + print("#" * 70) tests = [ self.test_round_trip_accuracy, @@ -345,16 +373,18 @@ def run_all_tests(self): if test(): passed += 1 - print("\n" + "="*70) + print("\n" + "=" * 70) print(f"COMPRESSION TESTS: {passed}/{len(tests)} PASSED") - print("="*70) + print("=" * 70) return passed, len(tests) + # ============================================================================ # TEST SUITE 3: FULL PIPELINE INTEGRATION # ============================================================================ + class PipelineIntegrationSuite: """Test the complete pipeline end-to-end""" @@ -363,13 +393,15 @@ def __init__(self): def test_real_code_analysis(self): """Test pipeline with real code samples""" - print("\n" + "="*70) + print("\n" + "=" * 70) print("TEST 7: Real Code Analysis") - print("="*70) + print("=" * 70) # Test code samples test_samples = [ - ("safe_code.py", """ + ( + "safe_code.py", + """ def process_data(data: list) -> list: '''Process data with validation''' if not isinstance(data, list): @@ -384,8 +416,11 @@ def process_data(data: list) -> list: except Exception as e: print(f"Error: {e}") raise -"""), - ("algorithm.py", """ +""", + ), + ( + "algorithm.py", + """ def quicksort(arr: list) -> list: '''Quick sort algorithm - O(n log n)''' if len(arr) <= 1: @@ -395,22 +430,23 @@ def quicksort(arr: list) -> list: middle = [x for x in arr if x == pivot] right = [x for x in arr if x > pivot] return quicksort(left) + middle + quicksort(right) -"""), +""", + ), ] print(f"Testing with {len(test_samples)} code files") # Run pipeline pipeline = LJPWPipeline() - results = pipeline.analyze_codebase(test_samples, - generate_docs=False, - generate_improvement_plan=False) + results = pipeline.analyze_codebase( + test_samples, generate_docs=False, generate_improvement_plan=False + ) # Validate results - has_analysis = len(results['analysis']) == len(test_samples) - has_genome = len(results['compressed_genome']) > 0 - has_compression = results['compression_ratio'] > 1 - has_reasoning = 'health_score' in results['reasoning'] + has_analysis = len(results["analysis"]) == len(test_samples) + has_genome = len(results["compressed_genome"]) > 0 + has_compression = results["compression_ratio"] > 1 + has_reasoning = "health_score" in results["reasoning"] print(f"\nValidation:") print(f" Analysis complete: {has_analysis}") @@ -420,25 +456,31 @@ def quicksort(arr: list) -> list: passed = all([has_analysis, has_genome, has_compression, has_reasoning]) - print(f"\nResult: {'PASS' if passed else 'FAIL'} - Pipeline {'completed' if passed else 'failed'}") + print( + f"\nResult: {'PASS' if passed else 'FAIL'} - Pipeline {'completed' if passed else 'failed'}" + ) - self.results.append({ - 'test': 'Real Code Analysis', - 'passed': passed, - 'value': results['compression_ratio'], - 'threshold': 'All steps complete' - }) + self.results.append( + { + "test": "Real Code Analysis", + "passed": passed, + "value": results["compression_ratio"], + "threshold": "All steps complete", + } + ) return passed def test_health_score_calculation(self): """Test that health scores are calculated correctly""" - print("\n" + "="*70) + print("\n" + "=" * 70) print("TEST 8: Health Score Calculation") - print("="*70) + print("=" * 70) # Code at Natural Equilibrium should have high health - perfect_code = ("perfect.py", """ + perfect_code = ( + "perfect.py", + """ def safe_efficient_well_designed_function(data: list) -> list: '''Perfect balance of safety, structure, performance, and design''' if not isinstance(data, list): @@ -451,19 +493,22 @@ def safe_efficient_well_designed_function(data: list) -> list: except Exception as e: logging.error(f"Error processing: {e}") raise -""") +""", + ) pipeline = LJPWPipeline() - results = pipeline.analyze_codebase([perfect_code], - generate_docs=False, - generate_improvement_plan=False) + results = pipeline.analyze_codebase( + [perfect_code], generate_docs=False, generate_improvement_plan=False + ) - health_score = results['reasoning']['health_score'] - avg_ljpw = results['reasoning']['average_ljpw'] - distance = results['reasoning']['distance_from_ne'] + health_score = results["reasoning"]["health_score"] + avg_ljpw = results["reasoning"]["average_ljpw"] + distance = results["reasoning"]["distance_from_ne"] print(f"Health Score: {health_score:.2%}") - print(f"Average LJPW: L={avg_ljpw[0]:.2f}, J={avg_ljpw[1]:.2f}, P={avg_ljpw[2]:.2f}, W={avg_ljpw[3]:.2f}") + print( + f"Average LJPW: L={avg_ljpw[0]:.2f}, J={avg_ljpw[1]:.2f}, P={avg_ljpw[2]:.2f}, W={avg_ljpw[3]:.2f}" + ) print(f"Distance from NE: {distance:.3f}") # Test: health should be reasonable (> 0) @@ -471,22 +516,26 @@ def safe_efficient_well_designed_function(data: list) -> list: passed = health_score > threshold print(f"\nThreshold: > {threshold:.0%}") - print(f"Result: {'PASS' if passed else 'FAIL'} - Health score is {'valid' if passed else 'invalid'}") + print( + f"Result: {'PASS' if passed else 'FAIL'} - Health score is {'valid' if passed else 'invalid'}" + ) - self.results.append({ - 'test': 'Health Score Calculation', - 'passed': passed, - 'value': health_score, - 'threshold': threshold - }) + self.results.append( + { + "test": "Health Score Calculation", + "passed": passed, + "value": health_score, + "threshold": threshold, + } + ) return passed def run_all_tests(self): """Run all pipeline tests""" - print("\n" + "#"*70) + print("\n" + "#" * 70) print("# PIPELINE INTEGRATION TEST SUITE") - print("#"*70) + print("#" * 70) tests = [ self.test_real_code_analysis, @@ -498,16 +547,18 @@ def run_all_tests(self): if test(): passed += 1 - print("\n" + "="*70) + print("\n" + "=" * 70) print(f"PIPELINE TESTS: {passed}/{len(tests)} PASSED") - print("="*70) + print("=" * 70) return passed, len(tests) + # ============================================================================ # TEST SUITE 4: PERFORMANCE BENCHMARKS # ============================================================================ + class PerformanceSuite: """Benchmark performance characteristics""" @@ -516,9 +567,9 @@ def __init__(self): def test_large_scale_compression(self): """Test compression on large datasets""" - print("\n" + "="*70) + print("\n" + "=" * 70) print("TEST 9: Large-Scale Compression Performance") - print("="*70) + print("=" * 70) # Generate large dataset sizes = [100, 500, 1000] @@ -526,10 +577,15 @@ def test_large_scale_compression(self): for size in sizes: print(f"\nTesting with {size} states:") - trajectory = [(random.uniform(0.3, 0.9), - random.uniform(0.3, 0.8), - random.uniform(0.4, 0.9), - random.uniform(0.3, 0.8)) for _ in range(size)] + trajectory = [ + ( + random.uniform(0.3, 0.9), + random.uniform(0.3, 0.8), + random.uniform(0.4, 0.9), + random.uniform(0.3, 0.8), + ) + for _ in range(size) + ] # Time compression start = time.time() @@ -554,22 +610,26 @@ def test_large_scale_compression(self): passed = throughput > threshold print(f"\nThreshold: {threshold} states/sec") - print(f"Result: {'PASS' if passed else 'FAIL'} - Performance is {'acceptable' if passed else 'poor'}") + print( + f"Result: {'PASS' if passed else 'FAIL'} - Performance is {'acceptable' if passed else 'poor'}" + ) - self.results.append({ - 'test': 'Large-Scale Compression', - 'passed': passed, - 'value': throughput, - 'threshold': threshold - }) + self.results.append( + { + "test": "Large-Scale Compression", + "passed": passed, + "value": throughput, + "threshold": threshold, + } + ) return passed def run_all_tests(self): """Run all performance tests""" - print("\n" + "#"*70) + print("\n" + "#" * 70) print("# PERFORMANCE BENCHMARK SUITE") - print("#"*70) + print("#" * 70) tests = [ self.test_large_scale_compression, @@ -580,22 +640,24 @@ def run_all_tests(self): if test(): passed += 1 - print("\n" + "="*70) + print("\n" + "=" * 70) print(f"PERFORMANCE TESTS: {passed}/{len(tests)} PASSED") - print("="*70) + print("=" * 70) return passed, len(tests) + # ============================================================================ # MASTER TEST RUNNER # ============================================================================ + def run_all_tests(): """Run complete test suite""" - print("\n" + "#"*70) + print("\n" + "#" * 70) print("# LJPW FRAMEWORK COMPREHENSIVE TEST SUITE") print("# Testing all components and claims") - print("#"*70) + print("#" * 70) total_passed = 0 total_tests = 0 @@ -617,9 +679,9 @@ def run_all_tests(): results_summary.append((suite_name, passed, total)) # Final summary - print("\n" + "#"*70) + print("\n" + "#" * 70) print("# FINAL TEST RESULTS") - print("#"*70) + print("#" * 70) for suite_name, passed, total in results_summary: pct = 100 * passed / total if total > 0 else 0 @@ -629,9 +691,9 @@ def run_all_tests(): print(f" Status: {status}") overall_pct = 100 * total_passed / total_tests if total_tests > 0 else 0 - print(f"\n" + "="*70) + print(f"\n" + "=" * 70) print(f"OVERALL: {total_passed}/{total_tests} tests passed ({overall_pct:.0f}%)") - print("="*70) + print("=" * 70) if total_passed == total_tests: print("\n[SUCCESS] All tests passed! Framework is validated.") @@ -644,9 +706,10 @@ def run_all_tests(): return total_passed, total_tests + # ============================================================================ # RUN TESTS # ============================================================================ -if __name__ == '__main__': +if __name__ == "__main__": run_all_tests() diff --git a/tests/test_multi_language.py b/tests/test_multi_language.py index 4b3b698..2c12cca 100644 --- a/tests/test_multi_language.py +++ b/tests/test_multi_language.py @@ -11,13 +11,15 @@ import importlib.util import os + def load_module(name, path): spec = importlib.util.spec_from_file_location(name, path) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) return module -base_path = os.path.join(os.path.dirname(__file__), '..', 'src', 'ljpw') + +base_path = os.path.join(os.path.dirname(__file__), "..", "src", "ljpw") pipeline_mod = load_module("ljpw_pipeline", os.path.join(base_path, "ljpw_pipeline.py")) LJPWPipeline = pipeline_mod.LJPWPipeline @@ -27,7 +29,9 @@ def load_module(name, path): LANGUAGE_SAMPLES = { "python": [ - ("safe_python.py", """ + ( + "safe_python.py", + """ from typing import Optional, List import logging @@ -71,11 +75,13 @@ def validate(self, data: Optional[List[int]]) -> bool: except Exception as e: self.logger.error(f"Validation error: {e}") raise -"""), +""", + ), ], - "javascript": [ - ("safe_js.js", """ + ( + "safe_js.js", + """ class DataProcessor { constructor(options = {}) { this.validateOptions(options); @@ -123,11 +129,13 @@ class DataProcessor { } module.exports = DataProcessor; -"""), +""", + ), ], - "rust": [ - ("safe_rust.rs", """ + ( + "safe_rust.rs", + """ use std::error::Error; use std::fmt; @@ -198,11 +206,13 @@ class DataProcessor { assert!(validator.validate(Some(vec![1, 2, 3])).is_ok()); } } -"""), +""", + ), ], - "java": [ - ("SafeJava.java", """ + ( + "SafeJava.java", + """ import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -261,7 +271,8 @@ class DataProcessor { } } } -"""), +""", + ), ], } @@ -271,16 +282,18 @@ class DataProcessor { EDGE_CASES = [ ("empty.py", ""), # Empty file - ("minimal.py", "x = 1"), # Minimal code - - ("comment_only.py", """ + ( + "comment_only.py", + """ # This file only has comments # No actual code here # Just documentation -"""), - - ("massive_safety.py", """ +""", + ), + ( + "massive_safety.py", + """ def ultra_safe_function(data): '''Maximum safety checks''' if data is None: @@ -310,23 +323,27 @@ def ultra_safe_function(data): except Exception as e: logging.error(f"Error: {e}") raise -"""), - - ("no_safety.py", """ +""", + ), + ( + "no_safety.py", + """ def unsafe_function(data): return [x * 2 for x in data] -"""), +""", + ), ] # ============================================================================ # TEST EXECUTION # ============================================================================ + def test_multi_language(): """Test framework with multiple programming languages""" - print("="*70) + print("=" * 70) print("MULTI-LANGUAGE LJPW ANALYSIS TEST") - print("="*70) + print("=" * 70) pipeline = LJPWPipeline() @@ -334,30 +351,33 @@ def test_multi_language(): print(f"\n{language.upper()} Analysis:") print("-" * 70) - results = pipeline.analyze_codebase(samples, - generate_docs=False, - generate_improvement_plan=False) + results = pipeline.analyze_codebase( + samples, generate_docs=False, generate_improvement_plan=False + ) - stats = results['statistics'] - avg_ljpw = stats['average_ljpw'] + stats = results["statistics"] + avg_ljpw = stats["average_ljpw"] print(f"\nResults for {language}:") print(f" Files analyzed: {stats['total_files']}") print(f" Compression: {stats['compression_ratio']:.1f}x") print(f" Health Score: {stats['health_score']:.1%}") - print(f" Average LJPW: L={avg_ljpw[0]:.2f}, J={avg_ljpw[1]:.2f}, P={avg_ljpw[2]:.2f}, W={avg_ljpw[3]:.2f}") + print( + f" Average LJPW: L={avg_ljpw[0]:.2f}, J={avg_ljpw[1]:.2f}, P={avg_ljpw[2]:.2f}, W={avg_ljpw[3]:.2f}" + ) # Insights - if results['reasoning']['insights']: + if results["reasoning"]["insights"]: print(f"\n Top Insights:") - for insight in results['reasoning']['insights'][:3]: + for insight in results["reasoning"]["insights"][:3]: print(f" [{insight['type']}] {insight['message']}") + def test_edge_cases(): """Test framework with edge cases""" - print("\n" + "="*70) + print("\n" + "=" * 70) print("EDGE CASE TESTING") - print("="*70) + print("=" * 70) pipeline = LJPWPipeline() @@ -366,25 +386,28 @@ def test_edge_cases(): print("-" * 70) try: - results = pipeline.analyze_codebase([(filename, code)], - generate_docs=False, - generate_improvement_plan=False) + results = pipeline.analyze_codebase( + [(filename, code)], generate_docs=False, generate_improvement_plan=False + ) - stats = results['statistics'] - avg_ljpw = stats['average_ljpw'] + stats = results["statistics"] + avg_ljpw = stats["average_ljpw"] print(f" Success!") print(f" Health: {stats['health_score']:.1%}") - print(f" LJPW: L={avg_ljpw[0]:.2f}, J={avg_ljpw[1]:.2f}, P={avg_ljpw[2]:.2f}, W={avg_ljpw[3]:.2f}") + print( + f" LJPW: L={avg_ljpw[0]:.2f}, J={avg_ljpw[1]:.2f}, P={avg_ljpw[2]:.2f}, W={avg_ljpw[3]:.2f}" + ) except Exception as e: print(f" Error: {e}") + def test_extreme_scale(): """Test with extremely large generated codebase""" - print("\n" + "="*70) + print("\n" + "=" * 70) print("EXTREME SCALE TEST") - print("="*70) + print("=" * 70) # Generate 100 files print("\nGenerating 100 synthetic code files...") @@ -415,16 +438,17 @@ def function_{i}(data): pipeline = LJPWPipeline() import time + start = time.time() - results = pipeline.analyze_codebase(synthetic_files, - generate_docs=False, - generate_improvement_plan=False) + results = pipeline.analyze_codebase( + synthetic_files, generate_docs=False, generate_improvement_plan=False + ) elapsed = time.time() - start - stats = results['statistics'] - avg_ljpw = stats['average_ljpw'] + stats = results["statistics"] + avg_ljpw = stats["average_ljpw"] print(f"\nExtreme Scale Results:") print(f" Files: {stats['total_files']}") @@ -434,28 +458,31 @@ def function_{i}(data): print(f" Processing time: {elapsed:.2f}s") print(f" Throughput: {stats['total_files']/elapsed:.1f} files/sec") print(f" Health: {stats['health_score']:.1%}") - print(f" LJPW: L={avg_ljpw[0]:.2f}, J={avg_ljpw[1]:.2f}, P={avg_ljpw[2]:.2f}, W={avg_ljpw[3]:.2f}") + print( + f" LJPW: L={avg_ljpw[0]:.2f}, J={avg_ljpw[1]:.2f}, P={avg_ljpw[2]:.2f}, W={avg_ljpw[3]:.2f}" + ) + def compare_languages(): """Compare LJPW scores across languages""" - print("\n" + "="*70) + print("\n" + "=" * 70) print("CROSS-LANGUAGE COMPARISON") - print("="*70) + print("=" * 70) pipeline = LJPWPipeline() language_scores = {} for language, samples in LANGUAGE_SAMPLES.items(): - results = pipeline.analyze_codebase(samples, - generate_docs=False, - generate_improvement_plan=False) + results = pipeline.analyze_codebase( + samples, generate_docs=False, generate_improvement_plan=False + ) - avg_ljpw = results['statistics']['average_ljpw'] - health = results['statistics']['health_score'] + avg_ljpw = results["statistics"]["average_ljpw"] + health = results["statistics"]["health_score"] language_scores[language] = { - 'ljpw': avg_ljpw, - 'health': health, + "ljpw": avg_ljpw, + "health": health, } # Display comparison @@ -463,29 +490,30 @@ def compare_languages(): print("-" * 70) for lang in sorted(language_scores.keys()): scores = language_scores[lang] - L, J, P, W = scores['ljpw'] - health = scores['health'] + L, J, P, W = scores["ljpw"] + health = scores["health"] print(f"{lang:15s}\t{L:.2f}\t{J:.2f}\t{P:.2f}\t{W:.2f}\t{health:.1%}") # Find best language - best_lang = max(language_scores.items(), key=lambda x: x[1]['health']) + best_lang = max(language_scores.items(), key=lambda x: x[1]["health"]) print(f"\nHighest Health Score: {best_lang[0]} ({best_lang[1]['health']:.1%})") + # ============================================================================ # RUN ALL TESTS # ============================================================================ -if __name__ == '__main__': - print("#"*70) +if __name__ == "__main__": + print("#" * 70) print("# LJPW MULTI-LANGUAGE & EDGE CASE TEST SUITE") - print("#"*70) + print("#" * 70) test_multi_language() test_edge_cases() test_extreme_scale() compare_languages() - print("\n" + "#"*70) + print("\n" + "#" * 70) print("# ALL MULTI-LANGUAGE TESTS COMPLETE") - print("#"*70) + print("#" * 70) print("\n[SUCCESS] Framework validated across multiple languages and edge cases!") diff --git a/tests/test_semantic_interpolation.py b/tests/test_semantic_interpolation.py index 7b42c7b..b44b296 100644 --- a/tests/test_semantic_interpolation.py +++ b/tests/test_semantic_interpolation.py @@ -15,18 +15,21 @@ import sys from pathlib import Path -from typing import Tuple, Dict +from typing import Dict, Tuple -sys.path.insert(0, str(Path(__file__).parent.parent / 'src' / 'ljpw')) +sys.path.insert(0, str(Path(__file__).parent.parent / "src" / "ljpw")) from ljpw_standalone import analyze_quick, calculate_distance # ============================================================================ # INTERPOLATION ENGINE # ============================================================================ -def interpolate_position(pos1: Tuple[float, float, float, float], - pos2: Tuple[float, float, float, float], - alpha: float = 0.5) -> Tuple[float, float, float, float]: + +def interpolate_position( + pos1: Tuple[float, float, float, float], + pos2: Tuple[float, float, float, float], + alpha: float = 0.5, +) -> Tuple[float, float, float, float]: """ Interpolate between two positions in LJPW space. @@ -40,6 +43,7 @@ def interpolate_position(pos1: Tuple[float, float, float, float], W = pos1[3] + alpha * (pos2[3] - pos1[3]) return (L, J, P, W) + def describe_position(coords: Tuple[float, float, float, float]) -> Dict: """ Generate semantic description of what code at this position should be like. @@ -47,77 +51,85 @@ def describe_position(coords: Tuple[float, float, float, float]) -> Dict: L, J, P, W = coords description = { - 'coordinates': coords, - 'genome': f"L{int(round(L*10))%10}J{int(round(J*10))%10}P{int(round(P*10))%10}W{int(round(W*10))%10}", - 'characteristics': [] + "coordinates": coords, + "genome": f"L{int(round(L*10))%10}J{int(round(J*10))%10}P{int(round(P*10))%10}W{int(round(W*10))%10}", + "characteristics": [], } # Love (Safety) analysis if L < 0.3: - description['characteristics'].append("MINIMAL SAFETY: Little to no error handling") + description["characteristics"].append("MINIMAL SAFETY: Little to no error handling") elif L < 0.5: - description['characteristics'].append("LOW SAFETY: Basic error handling only") + description["characteristics"].append("LOW SAFETY: Basic error handling only") elif L < 0.7: - description['characteristics'].append("MODERATE SAFETY: Good error handling") + description["characteristics"].append("MODERATE SAFETY: Good error handling") elif L < 0.9: - description['characteristics'].append("HIGH SAFETY: Extensive validation and error handling") + description["characteristics"].append( + "HIGH SAFETY: Extensive validation and error handling" + ) else: - description['characteristics'].append("MAXIMUM SAFETY: Defensive programming, comprehensive checks") + description["characteristics"].append( + "MAXIMUM SAFETY: Defensive programming, comprehensive checks" + ) # Justice (Structure) analysis if J < 0.3: - description['characteristics'].append("MINIMAL STRUCTURE: No types or docs") + description["characteristics"].append("MINIMAL STRUCTURE: No types or docs") elif J < 0.5: - description['characteristics'].append("LOW STRUCTURE: Minimal documentation") + description["characteristics"].append("LOW STRUCTURE: Minimal documentation") elif J < 0.7: - description['characteristics'].append("MODERATE STRUCTURE: Some types and docs") + description["characteristics"].append("MODERATE STRUCTURE: Some types and docs") elif J < 0.9: - description['characteristics'].append("HIGH STRUCTURE: Well-typed and documented") + description["characteristics"].append("HIGH STRUCTURE: Well-typed and documented") else: - description['characteristics'].append("MAXIMUM STRUCTURE: Rigorous types, comprehensive docs") + description["characteristics"].append( + "MAXIMUM STRUCTURE: Rigorous types, comprehensive docs" + ) # Power (Performance) analysis if P < 0.3: - description['characteristics'].append("MINIMAL PERFORMANCE: Simple, unoptimized") + description["characteristics"].append("MINIMAL PERFORMANCE: Simple, unoptimized") elif P < 0.5: - description['characteristics'].append("LOW PERFORMANCE: Basic implementation") + description["characteristics"].append("LOW PERFORMANCE: Basic implementation") elif P < 0.7: - description['characteristics'].append("MODERATE PERFORMANCE: Some optimization") + description["characteristics"].append("MODERATE PERFORMANCE: Some optimization") elif P < 0.9: - description['characteristics'].append("HIGH PERFORMANCE: Optimized algorithms") + description["characteristics"].append("HIGH PERFORMANCE: Optimized algorithms") else: - description['characteristics'].append("MAXIMUM PERFORMANCE: Highly optimized, cached") + description["characteristics"].append("MAXIMUM PERFORMANCE: Highly optimized, cached") # Wisdom (Design) analysis if W < 0.3: - description['characteristics'].append("MINIMAL DESIGN: No abstraction or patterns") + description["characteristics"].append("MINIMAL DESIGN: No abstraction or patterns") elif W < 0.5: - description['characteristics'].append("LOW DESIGN: Simple, direct code") + description["characteristics"].append("LOW DESIGN: Simple, direct code") elif W < 0.7: - description['characteristics'].append("MODERATE DESIGN: Some abstractions") + description["characteristics"].append("MODERATE DESIGN: Some abstractions") elif W < 0.9: - description['characteristics'].append("HIGH DESIGN: Well-abstracted, patterns used") + description["characteristics"].append("HIGH DESIGN: Well-abstracted, patterns used") else: - description['characteristics'].append("MAXIMUM DESIGN: Elegant architecture, deep patterns") + description["characteristics"].append("MAXIMUM DESIGN: Elegant architecture, deep patterns") # Overall archetype if L > 0.7 and J > 0.7 and P < 0.4: - description['archetype'] = "BUREAUCRAT: Safe and structured but slow" + description["archetype"] = "BUREAUCRAT: Safe and structured but slow" elif L < 0.4 and J < 0.4 and P > 0.7: - description['archetype'] = "PROTOTYPE: Fast but risky and messy" + description["archetype"] = "PROTOTYPE: Fast but risky and messy" elif L > 0.7 and P > 0.7 and W > 0.7: - description['archetype'] = "IDEAL: Balanced excellence" + description["archetype"] = "IDEAL: Balanced excellence" elif abs(L - 0.618) < 0.2 and abs(J - 0.414) < 0.2: - description['archetype'] = "NATURAL: Near optimal equilibrium" + description["archetype"] = "NATURAL: Near optimal equilibrium" else: - description['archetype'] = "BALANCED: Moderate in all dimensions" + description["archetype"] = "BALANCED: Moderate in all dimensions" return description + # ============================================================================ # TEST CASES # ============================================================================ + def test_interpolation(): print("\n" + "=" * 70) print("SEMANTIC INTERPOLATION TEST") @@ -156,18 +168,30 @@ def process_data(data: List[int]) -> List[int]: result_proto = analyze_quick(prototype) result_prod = analyze_quick(production) - pos_proto = (result_proto['ljpw']['L'], result_proto['ljpw']['J'], - result_proto['ljpw']['P'], result_proto['ljpw']['W']) - pos_prod = (result_prod['ljpw']['L'], result_prod['ljpw']['J'], - result_prod['ljpw']['P'], result_prod['ljpw']['W']) + pos_proto = ( + result_proto["ljpw"]["L"], + result_proto["ljpw"]["J"], + result_proto["ljpw"]["P"], + result_proto["ljpw"]["W"], + ) + pos_prod = ( + result_prod["ljpw"]["L"], + result_prod["ljpw"]["J"], + result_prod["ljpw"]["P"], + result_prod["ljpw"]["W"], + ) print(f"\nPrototype Code:") - print(f" Position: L={pos_proto[0]:.2f}, J={pos_proto[1]:.2f}, P={pos_proto[2]:.2f}, W={pos_proto[3]:.2f}") + print( + f" Position: L={pos_proto[0]:.2f}, J={pos_proto[1]:.2f}, P={pos_proto[2]:.2f}, W={pos_proto[3]:.2f}" + ) desc_proto = describe_position(pos_proto) print(f" Archetype: {desc_proto['archetype']}") print(f"\nProduction Code:") - print(f" Position: L={pos_prod[0]:.2f}, J={pos_prod[1]:.2f}, P={pos_prod[2]:.2f}, W={pos_prod[3]:.2f}") + print( + f" Position: L={pos_prod[0]:.2f}, J={pos_prod[1]:.2f}, P={pos_prod[2]:.2f}, W={pos_prod[3]:.2f}" + ) desc_prod = describe_position(pos_prod) print(f" Archetype: {desc_prod['archetype']}") @@ -178,10 +202,12 @@ def process_data(data: List[int]) -> List[int]: desc = describe_position(pos_interp) print(f"\n {alpha*100:.0f}% toward production:") - print(f" Position: L={pos_interp[0]:.2f}, J={pos_interp[1]:.2f}, P={pos_interp[2]:.2f}, W={pos_interp[3]:.2f}") + print( + f" Position: L={pos_interp[0]:.2f}, J={pos_interp[1]:.2f}, P={pos_interp[2]:.2f}, W={pos_interp[3]:.2f}" + ) print(f" Genome: {desc['genome']}") print(f" Archetype: {desc['archetype']}") - for char in desc['characteristics']: + for char in desc["characteristics"]: print(f" • {char}") # Test Case 2: Fast & Risky → Safe & Slow @@ -216,27 +242,41 @@ def get_value(key: str) -> Optional[Any]: result_fast = analyze_quick(fast_risky) result_safe = analyze_quick(safe_slow) - pos_fast = (result_fast['ljpw']['L'], result_fast['ljpw']['J'], - result_fast['ljpw']['P'], result_fast['ljpw']['W']) - pos_safe = (result_safe['ljpw']['L'], result_safe['ljpw']['J'], - result_safe['ljpw']['P'], result_safe['ljpw']['W']) + pos_fast = ( + result_fast["ljpw"]["L"], + result_fast["ljpw"]["J"], + result_fast["ljpw"]["P"], + result_fast["ljpw"]["W"], + ) + pos_safe = ( + result_safe["ljpw"]["L"], + result_safe["ljpw"]["J"], + result_safe["ljpw"]["P"], + result_safe["ljpw"]["W"], + ) print(f"\nFast/Risky Code:") - print(f" Position: L={pos_fast[0]:.2f}, J={pos_fast[1]:.2f}, P={pos_fast[2]:.2f}, W={pos_fast[3]:.2f}") + print( + f" Position: L={pos_fast[0]:.2f}, J={pos_fast[1]:.2f}, P={pos_fast[2]:.2f}, W={pos_fast[3]:.2f}" + ) print(f"\nSafe/Slow Code:") - print(f" Position: L={pos_safe[0]:.2f}, J={pos_safe[1]:.2f}, P={pos_safe[2]:.2f}, W={pos_safe[3]:.2f}") + print( + f" Position: L={pos_safe[0]:.2f}, J={pos_safe[1]:.2f}, P={pos_safe[2]:.2f}, W={pos_safe[3]:.2f}" + ) # Midpoint pos_mid = interpolate_position(pos_fast, pos_safe, 0.5) desc_mid = describe_position(pos_mid) print(f"\nMidpoint (balanced):") - print(f" Position: L={pos_mid[0]:.2f}, J={pos_mid[1]:.2f}, P={pos_mid[2]:.2f}, W={pos_mid[3]:.2f}") + print( + f" Position: L={pos_mid[0]:.2f}, J={pos_mid[1]:.2f}, P={pos_mid[2]:.2f}, W={pos_mid[3]:.2f}" + ) print(f" Genome: {desc_mid['genome']}") print(f" Expected: Moderate safety, moderate performance") print(f" Actual archetype: {desc_mid['archetype']}") - for char in desc_mid['characteristics']: + for char in desc_mid["characteristics"]: print(f" • {char}") # Test Case 3: Verify linearity @@ -253,7 +293,7 @@ def get_value(key: str) -> Optional[Any]: print(f" Distance(midpoint, production): {dist_mid_prod:.3f}") print(f" Ratio: {dist_proto_mid/dist_mid_prod:.2f}") - if 0.8 < dist_proto_mid/dist_mid_prod < 1.2: + if 0.8 < dist_proto_mid / dist_mid_prod < 1.2: print(f" ✓ Nearly equal - linear interpolation works!") else: print(f" ⚠ Unequal - space may be non-linear") @@ -261,7 +301,8 @@ def get_value(key: str) -> Optional[Any]: print("\n" + "=" * 70) print("CONCLUSIONS") print("=" * 70) - print(""" + print( + """ If interpolated positions have semantics between the endpoints, this proves: 1. LJPW space is semantically meaningful (not just arbitrary coordinates) @@ -270,8 +311,10 @@ def get_value(key: str) -> Optional[Any]: 4. Code generation at target positions is theoretically possible This validates the "coordinate extraction" model of semantic compression. - """) + """ + ) print("=" * 70 + "\n") -if __name__ == '__main__': + +if __name__ == "__main__": test_interpolation() diff --git a/tests/test_universal_ljpw.py b/tests/test_universal_ljpw.py index 8542a6a..c1288bd 100644 --- a/tests/test_universal_ljpw.py +++ b/tests/test_universal_ljpw.py @@ -15,16 +15,19 @@ """ import math -from typing import Tuple, List, Dict +from typing import Dict, List, Tuple # ============================================================================ # MANUAL LJPW MAPPING (Non-Code) # ============================================================================ -def calculate_distance(pos1: Tuple[float, float, float, float], - pos2: Tuple[float, float, float, float]) -> float: + +def calculate_distance( + pos1: Tuple[float, float, float, float], pos2: Tuple[float, float, float, float] +) -> float: """Calculate Euclidean distance in LJPW space.""" - return math.sqrt(sum((a-b)**2 for a, b in zip(pos1, pos2))) + return math.sqrt(sum((a - b) ** 2 for a, b in zip(pos1, pos2))) + def map_to_ljpw(description: Dict) -> Tuple[float, float, float, float]: """ @@ -35,7 +38,8 @@ def map_to_ljpw(description: Dict) -> Tuple[float, float, float, float]: P (Power/Performance): Capability, throughput, efficiency, speed W (Wisdom/Design): Adaptation, elegance, insight, innovation """ - return (description['L'], description['J'], description['P'], description['W']) + return (description["L"], description["J"], description["P"], description["W"]) + # ============================================================================ # TEST 1: ORGANIZATIONS @@ -80,7 +84,7 @@ def map_to_ljpw(description: Dict) -> Tuple[float, float, float, float]: "Military Organization": { "description": "Highly structured, disciplined, mission-critical", "L": 0.9, # High safety - mission critical - "J": 0.95, # Maximum structure - strict hierarchy + "J": 0.95, # Maximum structure - strict hierarchy "P": 0.7, # Good power - efficient execution "W": 0.6, # Moderate wisdom - proven doctrine }, @@ -89,8 +93,8 @@ def map_to_ljpw(description: Dict) -> Tuple[float, float, float, float]: "L": 0.6, # Moderate safety - careful experimentation "J": 0.6, # Moderate structure - scientific method "P": 0.4, # Lower power - deliberate pace - "W": 0.95, # Maximum wisdom - pursuing insight - } + "W": 0.95, # Maximum wisdom - pursuing insight + }, } # ============================================================================ @@ -121,7 +125,7 @@ def map_to_ljpw(description: Dict) -> Tuple[float, float, float, float]: }, "Horror": { "description": "Overwhelming threat, powerlessness, dread", - "L": 0.05, # Minimal safety - characters in danger + "L": 0.05, # Minimal safety - characters in danger "J": 0.2, # Low structure - chaos prevails "P": 0.1, # Minimal power - powerlessness "W": 0.3, # Low wisdom - no understanding @@ -138,15 +142,15 @@ def map_to_ljpw(description: Dict) -> Tuple[float, float, float, float]: "L": 0.7, # Good safety - protected growth "J": 0.6, # Moderate structure - learning rules "P": 0.5, # Moderate power - developing strength - "W": 0.85, # High wisdom - gaining understanding + "W": 0.85, # High wisdom - gaining understanding }, "Epic": { "description": "Grand scale, heroic deeds, civilizational stakes", "L": 0.7, # Good safety - heroes protected - "J": 0.85, # High structure - cosmic order + "J": 0.85, # High structure - cosmic order "P": 0.9, # High power - legendary feats "W": 0.8, # High wisdom - profound themes - } + }, } # ============================================================================ @@ -158,7 +162,7 @@ def map_to_ljpw(description: Dict) -> Tuple[float, float, float, float]: "description": "Simple, fast reproduction, adaptable", "L": 0.5, # Moderate safety - survival strategies "J": 0.3, # Low structure - simple organization - "P": 0.95, # Maximum power - rapid reproduction + "P": 0.95, # Maximum power - rapid reproduction "W": 0.4, # Moderate wisdom - evolutionary adaptation }, "Virus": { @@ -187,21 +191,22 @@ def map_to_ljpw(description: Dict) -> Tuple[float, float, float, float]: "L": 0.7, # Good safety - cultural protection "J": 0.7, # Good structure - social organization "P": 0.7, # Good power - tool use - "W": 0.95, # Maximum wisdom - abstract reasoning + "W": 0.95, # Maximum wisdom - abstract reasoning }, "Forest Ecosystem": { "description": "Interconnected, self-regulating, resilient", "L": 0.8, # High safety - redundancy, resilience "J": 0.5, # Moderate structure - emergent order "P": 0.6, # Moderate power - sustainable throughput - "W": 0.85, # High wisdom - complex adaptation - } + "W": 0.85, # High wisdom - complex adaptation + }, } # ============================================================================ # ANALYSIS FUNCTIONS # ============================================================================ + def analyze_system(systems: Dict[str, Dict], system_type: str): """Analyze a category of systems in LJPW space.""" print(f"\n{'=' * 70}") @@ -225,7 +230,7 @@ def analyze_system(systems: Dict[str, Dict], system_type: str): names = list(systems.keys()) for i in range(len(names)): - for j in range(i+1, len(names)): + for j in range(i + 1, len(names)): name1, name2 = names[i], names[j] dist = calculate_distance(positions[name1], positions[name2]) @@ -245,11 +250,11 @@ def analyze_system(systems: Dict[str, Dict], system_type: str): print("EXTREMES") print(f"{'─' * 70}\n") - min_dist, max_dist = float('inf'), 0 + min_dist, max_dist = float("inf"), 0 min_pair, max_pair = None, None for i in range(len(names)): - for j in range(i+1, len(names)): + for j in range(i + 1, len(names)): dist = calculate_distance(positions[names[i]], positions[names[j]]) if dist < min_dist: min_dist = dist @@ -266,15 +271,18 @@ def analyze_system(systems: Dict[str, Dict], system_type: str): print(f" Distance: {max_dist:.3f}") print(f" Interpretation: These systems are fundamentally opposite in nature") + # ============================================================================ # MAIN TEST # ============================================================================ + def main(): print("\n" + "=" * 70) print("UNIVERSAL LJPW TEST: Non-Code Systems") print("=" * 70) - print(""" + print( + """ Hypothesis: If LJPW captures fundamental semantic structure, it should apply to ANY complex adaptive system, not just code. @@ -284,7 +292,8 @@ def main(): If distances make sense (similar systems close, different systems far), this supports the "Deep" interpretation: LJPW is a universal law. - """) + """ + ) # Test each category analyze_system(ORGANIZATIONS, "Organizations") @@ -316,7 +325,8 @@ def main(): print("\n" + "=" * 70) print("CONCLUSIONS") print("=" * 70) - print(""" + print( + """ If LJPW distances correlate with semantic similarity across domains: EVIDENCE FOR: @@ -335,8 +345,10 @@ def main(): ...suggests LJPW is operating at a deeper level than domain-specific analysis. It may be capturing the intrinsic geometry of meaning itself. - """) + """ + ) print("=" * 70 + "\n") -if __name__ == '__main__': + +if __name__ == "__main__": main() diff --git a/tools/benchmark_ljpw_vs_alternatives.py b/tools/benchmark_ljpw_vs_alternatives.py index 7336915..8532775 100644 --- a/tools/benchmark_ljpw_vs_alternatives.py +++ b/tools/benchmark_ljpw_vs_alternatives.py @@ -17,31 +17,32 @@ - Cross-language equivalence (only LJPW should handle this) """ -import sys -from pathlib import Path import math import re +import sys +from pathlib import Path -sys.path.insert(0, str(Path(__file__).parent.parent / 'src' / 'ljpw')) -from ljpw_standalone import analyze_quick, calculate_distance +sys.path.insert(0, str(Path(__file__).parent.parent)) +from src.ljpw.ljpw_standalone import analyze_quick, calculate_distance # ============================================================================= # ALTERNATIVE METRICS IMPLEMENTATIONS # ============================================================================= + def calculate_cyclomatic_complexity(code): """Calculate McCabe cyclomatic complexity (simple approximation)""" - decision_points = len(re.findall(r'\b(if|elif|else|for|while|and|or|except|case)\b', code)) + decision_points = len(re.findall(r"\b(if|elif|else|for|while|and|or|except|case)\b", code)) return decision_points + 1 def calculate_loc(code): """Calculate lines of code (non-empty, non-comment)""" - lines = code.split('\n') + lines = code.split("\n") loc = 0 for line in lines: stripped = line.strip() - if stripped and not stripped.startswith('#') and not stripped.startswith('//'): + if stripped and not stripped.startswith("#") and not stripped.startswith("//"): loc += 1 return loc @@ -49,8 +50,8 @@ def calculate_loc(code): def levenshtein_distance(s1, s2): """Calculate Levenshtein distance between two strings""" # Normalize whitespace for fairer comparison - s1 = ' '.join(s1.split()) - s2 = ' '.join(s2.split()) + s1 = " ".join(s1.split()) + s2 = " ".join(s2.split()) if len(s1) < len(s2): return levenshtein_distance(s2, s1) @@ -82,12 +83,12 @@ def normalized_levenshtein(s1, s2): def simple_ast_features(code): """Extract simple AST-like features""" features = { - 'functions': len(re.findall(r'\b(def|function|func)\b', code)), - 'classes': len(re.findall(r'\bclass\b', code)), - 'loops': len(re.findall(r'\b(for|while)\b', code)), - 'conditionals': len(re.findall(r'\bif\b', code)), - 'returns': len(re.findall(r'\breturn\b', code)), - 'variables': len(re.findall(r'[a-z_][a-z0-9_]*\s*=', code, re.I)), + "functions": len(re.findall(r"\b(def|function|func)\b", code)), + "classes": len(re.findall(r"\bclass\b", code)), + "loops": len(re.findall(r"\b(for|while)\b", code)), + "conditionals": len(re.findall(r"\bif\b", code)), + "returns": len(re.findall(r"\breturn\b", code)), + "variables": len(re.findall(r"[a-z_][a-z0-9_]*\s*=", code, re.I)), } return features @@ -105,26 +106,24 @@ def ast_feature_distance(code1, code2): def halstead_metrics(code): """Calculate Halstead complexity metrics (simplified)""" # Operators and operands (simplified) - operators = re.findall(r'[+\-*/%=<>!&|^~]|[\(\)\[\]\{\}]|\b(if|else|for|while|return|def|class)\b', code) - operands = re.findall(r'\b[a-zA-Z_][a-zA-Z0-9_]*\b|\b\d+\b', code) + operators = re.findall( + r"[+\-*/%=<>!&|^~]|[\(\)\[\]\{\}]|\b(if|else|for|while|return|def|class)\b", code + ) + operands = re.findall(r"\b[a-zA-Z_][a-zA-Z0-9_]*\b|\b\d+\b", code) n1 = len(set(operators)) # Unique operators - n2 = len(set(operands)) # Unique operands - N1 = len(operators) # Total operators - N2 = len(operands) # Total operands + n2 = len(set(operands)) # Unique operands + N1 = len(operators) # Total operators + N2 = len(operands) # Total operands if n1 == 0 or n2 == 0: - return {'vocabulary': 0, 'length': 0, 'volume': 0} + return {"vocabulary": 0, "length": 0, "volume": 0} vocabulary = n1 + n2 length = N1 + N2 volume = length * math.log2(vocabulary) if vocabulary > 0 else 0 - return { - 'vocabulary': vocabulary, - 'length': length, - 'volume': volume - } + return {"vocabulary": vocabulary, "length": length, "volume": volume} # ============================================================================= @@ -133,53 +132,76 @@ def halstead_metrics(code): # Test 1: Semantic Equivalence (different syntax, same meaning) semantic_equiv_tests = [ - ("Python list comprehension", """ + ( + "Python list comprehension", + """ result = [x * 2 for x in range(10)] -"""), - ("Python traditional loop", """ +""", + ), + ( + "Python traditional loop", + """ result = [] for x in range(10): result.append(x * 2) -"""), +""", + ), ] # Test 2: Syntactic Variation (same meaning, different style) syntactic_variation_tests = [ - ("Compact style", """ + ( + "Compact style", + """ def add(a,b):return a+b -"""), - ("Expanded style", """ +""", + ), + ( + "Expanded style", + """ def add(a, b): result = a + b return result -"""), +""", + ), ] # Test 3: Cross-Language Equivalence (only LJPW should detect) cross_language_tests = [ - ("Python", """ + ( + "Python", + """ def factorial(n): if n <= 1: return 1 return n * factorial(n - 1) -"""), - ("JavaScript", """ +""", + ), + ( + "JavaScript", + """ function factorial(n) { if (n <= 1) { return 1; } return n * factorial(n - 1); } -"""), +""", + ), ] # Test 4: Quality Degradation (adding code smell) quality_degradation_tests = [ - ("Clean code", """ + ( + "Clean code", + """ def calculate_total(items): return sum(item.price for item in items) -"""), - ("Code smell (god function)", """ +""", + ), + ( + "Code smell (god function)", + """ def calculate_total(items): total = 0 for item in items: @@ -189,7 +211,8 @@ def calculate_total(items): if item.price > 0: total = total + item.price return total -"""), +""", + ), ] @@ -197,13 +220,14 @@ def calculate_total(items): # BENCHMARK EXECUTION # ============================================================================= + def ljpw_distance(code1, code2): """Calculate LJPW semantic distance""" r1 = analyze_quick(code1) r2 = analyze_quick(code2) - coords1 = (r1['ljpw']['L'], r1['ljpw']['J'], r1['ljpw']['P'], r1['ljpw']['W']) - coords2 = (r2['ljpw']['L'], r2['ljpw']['J'], r2['ljpw']['P'], r2['ljpw']['W']) + coords1 = (r1["ljpw"]["L"], r1["ljpw"]["J"], r1["ljpw"]["P"], r1["ljpw"]["W"]) + coords2 = (r2["ljpw"]["L"], r2["ljpw"]["J"], r2["ljpw"]["P"], r2["ljpw"]["W"]) return calculate_distance(coords1, coords2) @@ -213,7 +237,7 @@ def benchmark_test_case(name, code1, code2, expected_similar=True): print(f"\n{'─' * 70}") print(f"Test: {name}") print(f"Expected: {'Similar' if expected_similar else 'Different'}") - print('─' * 70) + print("─" * 70) # LJPW ljpw_dist = ljpw_distance(code1, code2) @@ -233,24 +257,36 @@ def benchmark_test_case(name, code1, code2, expected_similar=True): hal1 = halstead_metrics(code1) hal2 = halstead_metrics(code2) - hal_volume_diff = abs(hal1['volume'] - hal2['volume']) + hal_volume_diff = abs(hal1["volume"] - hal2["volume"]) # Display results - print(f"\nLJPW Distance: {ljpw_dist:.3f} {'✓ Correct' if (ljpw_dist < 0.2) == expected_similar else '✗ Wrong'}") - print(f"Cyclomatic Diff: {cyclo_diff} {'✓ Correct' if (cyclo_diff < 2) == expected_similar else '✗ Wrong'}") - print(f"LOC Diff: {loc_diff} {'✓ Correct' if (loc_diff < 3) == expected_similar else '✗ Wrong'}") - print(f"Levenshtein (norm): {lev_dist:.3f} {'✓ Correct' if (lev_dist < 0.3) == expected_similar else '✗ Wrong'}") - print(f"AST Feature Distance: {ast_dist:.3f} {'✓ Correct' if (ast_dist < 2.0) == expected_similar else '✗ Wrong'}") - print(f"Halstead Volume Diff: {hal_volume_diff:.1f} {'✓ Correct' if (hal_volume_diff < 20) == expected_similar else '✗ Wrong'}") + print( + f"\nLJPW Distance: {ljpw_dist:.3f} {'✓ Correct' if (ljpw_dist < 0.2) == expected_similar else '✗ Wrong'}" + ) + print( + f"Cyclomatic Diff: {cyclo_diff} {'✓ Correct' if (cyclo_diff < 2) == expected_similar else '✗ Wrong'}" + ) + print( + f"LOC Diff: {loc_diff} {'✓ Correct' if (loc_diff < 3) == expected_similar else '✗ Wrong'}" + ) + print( + f"Levenshtein (norm): {lev_dist:.3f} {'✓ Correct' if (lev_dist < 0.3) == expected_similar else '✗ Wrong'}" + ) + print( + f"AST Feature Distance: {ast_dist:.3f} {'✓ Correct' if (ast_dist < 2.0) == expected_similar else '✗ Wrong'}" + ) + print( + f"Halstead Volume Diff: {hal_volume_diff:.1f} {'✓ Correct' if (hal_volume_diff < 20) == expected_similar else '✗ Wrong'}" + ) # Score each metric scores = { - 'LJPW': 1 if (ljpw_dist < 0.2) == expected_similar else 0, - 'Cyclomatic': 1 if (cyclo_diff < 2) == expected_similar else 0, - 'LOC': 1 if (loc_diff < 3) == expected_similar else 0, - 'Levenshtein': 1 if (lev_dist < 0.3) == expected_similar else 0, - 'AST': 1 if (ast_dist < 2.0) == expected_similar else 0, - 'Halstead': 1 if (hal_volume_diff < 20) == expected_similar else 0, + "LJPW": 1 if (ljpw_dist < 0.2) == expected_similar else 0, + "Cyclomatic": 1 if (cyclo_diff < 2) == expected_similar else 0, + "LOC": 1 if (loc_diff < 3) == expected_similar else 0, + "Levenshtein": 1 if (lev_dist < 0.3) == expected_similar else 0, + "AST": 1 if (ast_dist < 2.0) == expected_similar else 0, + "Halstead": 1 if (hal_volume_diff < 20) == expected_similar else 0, } return scores @@ -272,12 +308,12 @@ def main(): print() all_scores = { - 'LJPW': 0, - 'Cyclomatic': 0, - 'LOC': 0, - 'Levenshtein': 0, - 'AST': 0, - 'Halstead': 0, + "LJPW": 0, + "Cyclomatic": 0, + "LOC": 0, + "Levenshtein": 0, + "AST": 0, + "Halstead": 0, } total_tests = 0 @@ -342,7 +378,7 @@ def main(): for metric, score in sorted_metrics: accuracy = (score / total_tests) * 100 - bar = '█' * int(accuracy / 5) + bar = "█" * int(accuracy / 5) print(f"{metric:<20} {score}/{total_tests:<9} {accuracy:>5.1f}% {bar}") print() @@ -350,9 +386,9 @@ def main(): print(f"{'🏆 WINNER: ' + winner if winner == 'LJPW' else '⚠ WINNER: ' + winner}") print() - if all_scores['LJPW'] == total_tests: + if all_scores["LJPW"] == total_tests: print("✓ LJPW correctly classified ALL test cases!") - elif all_scores['LJPW'] >= total_tests * 0.75: + elif all_scores["LJPW"] >= total_tests * 0.75: print("✓ LJPW performed well (>75% accuracy)") else: print("⚠ LJPW needs improvement") diff --git a/tools/compare_ast_vs_regex.py b/tools/compare_ast_vs_regex.py index 5fe4ef1..ad0895b 100644 --- a/tools/compare_ast_vs_regex.py +++ b/tools/compare_ast_vs_regex.py @@ -7,17 +7,17 @@ Goal: Data-driven decision on which approach to use. """ +import math +import random import sys import time -import random -import math from pathlib import Path -from typing import List, Dict, Any +from typing import Any, Dict, List -sys.path.insert(0, str(Path(__file__).parent.parent / 'src' / 'ljpw')) +sys.path.insert(0, str(Path(__file__).parent.parent)) -from ljpw_ast_analyzer import LJPWASTAnalyzer -from ljpw_standalone import analyze_file as regex_analyze_file +from src.ljpw.ljpw_ast_analyzer import LJPWASTAnalyzer +from src.ljpw.ljpw_standalone import analyze_file as regex_analyze_file def select_test_files(base_path: str, num_files: int = 100) -> List[str]: @@ -32,9 +32,11 @@ def select_test_files(base_path: str, num_files: int = 100) -> List[str]: return [str(f) for f in selected] -def calculate_differences(regex_result: Dict[str, float], ast_result: Dict[str, float]) -> Dict[str, Any]: +def calculate_differences( + regex_result: Dict[str, float], ast_result: Dict[str, float] +) -> Dict[str, Any]: """Calculate differences between two LJPW results""" - dims = ['L', 'J', 'P', 'W'] + dims = ["L", "J", "P", "W"] # Component-wise differences differences = {} @@ -44,16 +46,16 @@ def calculate_differences(regex_result: Dict[str, float], ast_result: Dict[str, diff = abs(regex_val - ast_val) rel_diff = (diff / regex_val * 100) if regex_val > 0 else 0 differences[dim] = { - 'regex': regex_val, - 'ast': ast_val, - 'diff': diff, - 'rel_diff_pct': rel_diff, + "regex": regex_val, + "ast": ast_val, + "diff": diff, + "rel_diff_pct": rel_diff, } # Euclidean distance regex_vec = [regex_result.get(d, 0) for d in dims] ast_vec = [ast_result.get(d, 0) for d in dims] - euclidean_dist = math.sqrt(sum((r - a)**2 for r, a in zip(regex_vec, ast_vec))) + euclidean_dist = math.sqrt(sum((r - a) ** 2 for r, a in zip(regex_vec, ast_vec))) # Check if strongest/weakest dimensions are preserved regex_max = dims[regex_vec.index(max(regex_vec))] @@ -61,16 +63,16 @@ def calculate_differences(regex_result: Dict[str, float], ast_result: Dict[str, ast_max = dims[ast_vec.index(max(ast_vec))] ast_min = dims[ast_vec.index(min(ast_vec))] - meaning_preserved = (regex_max == ast_max and regex_min == ast_min) + meaning_preserved = regex_max == ast_max and regex_min == ast_min return { - 'differences': differences, - 'euclidean_distance': euclidean_dist, - 'regex_strongest': regex_max, - 'regex_weakest': regex_min, - 'ast_strongest': ast_max, - 'ast_weakest': ast_min, - 'meaning_preserved': meaning_preserved, + "differences": differences, + "euclidean_distance": euclidean_dist, + "regex_strongest": regex_max, + "regex_weakest": regex_min, + "ast_strongest": ast_max, + "ast_weakest": ast_min, + "meaning_preserved": meaning_preserved, } @@ -94,20 +96,20 @@ def run_comparison(test_files: List[str]) -> Dict[str, Any]: for file_path in test_files: try: result = regex_analyze_file(file_path) - regex_results.append({ - 'file': file_path, - 'L': result.get('ljpw', {}).get('L', 0), - 'J': result.get('ljpw', {}).get('J', 0), - 'P': result.get('ljpw', {}).get('P', 0), - 'W': result.get('ljpw', {}).get('W', 0), - }) + regex_results.append( + { + "file": file_path, + "L": result.get("ljpw", {}).get("L", 0), + "J": result.get("ljpw", {}).get("J", 0), + "P": result.get("ljpw", {}).get("P", 0), + "W": result.get("ljpw", {}).get("W", 0), + } + ) except Exception as e: print(f"Regex error on {file_path}: {e}") - regex_results.append({ - 'file': file_path, - 'L': 0.618, 'J': 0.414, 'P': 0.718, 'W': 0.693, - 'error': str(e) - }) + regex_results.append( + {"file": file_path, "L": 0.618, "J": 0.414, "P": 0.718, "W": 0.693, "error": str(e)} + ) regex_time = time.time() - regex_start regex_speed = len(test_files) / regex_time @@ -122,20 +124,20 @@ def run_comparison(test_files: List[str]) -> Dict[str, Any]: for file_path in test_files: try: result = ast_analyzer.analyze_file(file_path) - ast_results.append({ - 'file': file_path, - 'L': result.get('L', 0), - 'J': result.get('J', 0), - 'P': result.get('P', 0), - 'W': result.get('W', 0), - }) + ast_results.append( + { + "file": file_path, + "L": result.get("L", 0), + "J": result.get("J", 0), + "P": result.get("P", 0), + "W": result.get("W", 0), + } + ) except Exception as e: print(f"AST error on {file_path}: {e}") - ast_results.append({ - 'file': file_path, - 'L': 0.618, 'J': 0.414, 'P': 0.718, 'W': 0.693, - 'error': str(e) - }) + ast_results.append( + {"file": file_path, "L": 0.618, "J": 0.414, "P": 0.718, "W": 0.693, "error": str(e)} + ) ast_time = time.time() - ast_start ast_speed = len(test_files) / ast_time @@ -150,35 +152,37 @@ def run_comparison(test_files: List[str]) -> Dict[str, Any]: comparisons = [] for regex_res, ast_res in zip(regex_results, ast_results): comp = calculate_differences(regex_res, ast_res) - comp['file'] = regex_res['file'] + comp["file"] = regex_res["file"] comparisons.append(comp) # Overall statistics - avg_euclidean = sum(c['euclidean_distance'] for c in comparisons) / len(comparisons) - meaning_preserved_count = sum(1 for c in comparisons if c['meaning_preserved']) + avg_euclidean = sum(c["euclidean_distance"] for c in comparisons) / len(comparisons) + meaning_preserved_count = sum(1 for c in comparisons if c["meaning_preserved"]) meaning_preserved_pct = meaning_preserved_count / len(comparisons) * 100 # Average differences per dimension avg_diffs = {} - for dim in ['L', 'J', 'P', 'W']: - avg_diff = sum(c['differences'][dim]['diff'] for c in comparisons) / len(comparisons) - avg_rel_diff = sum(c['differences'][dim]['rel_diff_pct'] for c in comparisons) / len(comparisons) + for dim in ["L", "J", "P", "W"]: + avg_diff = sum(c["differences"][dim]["diff"] for c in comparisons) / len(comparisons) + avg_rel_diff = sum(c["differences"][dim]["rel_diff_pct"] for c in comparisons) / len( + comparisons + ) avg_diffs[dim] = { - 'abs_diff': avg_diff, - 'rel_diff_pct': avg_rel_diff, + "abs_diff": avg_diff, + "rel_diff_pct": avg_rel_diff, } return { - 'test_files': test_files, - 'regex_time': regex_time, - 'regex_speed': regex_speed, - 'ast_time': ast_time, - 'ast_speed': ast_speed, - 'comparisons': comparisons, - 'avg_euclidean_distance': avg_euclidean, - 'meaning_preserved_count': meaning_preserved_count, - 'meaning_preserved_pct': meaning_preserved_pct, - 'avg_diffs': avg_diffs, + "test_files": test_files, + "regex_time": regex_time, + "regex_speed": regex_speed, + "ast_time": ast_time, + "ast_speed": ast_speed, + "comparisons": comparisons, + "avg_euclidean_distance": avg_euclidean, + "meaning_preserved_count": meaning_preserved_count, + "meaning_preserved_pct": meaning_preserved_pct, + "avg_diffs": avg_diffs, } @@ -195,7 +199,7 @@ def print_detailed_report(results: Dict[str, Any]): print(f" Time: {results['ast_time']:.2f}s") print(f" Speed: {results['ast_speed']:.0f} files/sec") - speed_ratio = results['regex_speed'] / results['ast_speed'] + speed_ratio = results["regex_speed"] / results["ast_speed"] if speed_ratio > 1: print(f"\n→ Regex is {speed_ratio:.1f}x FASTER") else: @@ -205,11 +209,13 @@ def print_detailed_report(results: Dict[str, Any]): print("ACCURACY COMPARISON") print(f"{'='*70}") print(f"\nAverage Euclidean distance: {results['avg_euclidean_distance']:.4f}") - print(f"Meaning preserved: {results['meaning_preserved_count']}/{len(results['comparisons'])} ({results['meaning_preserved_pct']:.1f}%)") + print( + f"Meaning preserved: {results['meaning_preserved_count']}/{len(results['comparisons'])} ({results['meaning_preserved_pct']:.1f}%)" + ) print(f"\nAverage differences by dimension:") - for dim in ['L', 'J', 'P', 'W']: - diff_data = results['avg_diffs'][dim] + for dim in ["L", "J", "P", "W"]: + diff_data = results["avg_diffs"][dim] print(f" {dim}: {diff_data['abs_diff']:.4f} ({diff_data['rel_diff_pct']:.1f}% relative)") # Show examples of biggest differences @@ -217,14 +223,20 @@ def print_detailed_report(results: Dict[str, Any]): print("FILES WITH BIGGEST DIFFERENCES") print(f"{'='*70}") - top_diffs = sorted(results['comparisons'], key=lambda x: x['euclidean_distance'], reverse=True)[:5] + top_diffs = sorted(results["comparisons"], key=lambda x: x["euclidean_distance"], reverse=True)[ + :5 + ] for i, comp in enumerate(top_diffs, 1): - file_name = Path(comp['file']).name + file_name = Path(comp["file"]).name print(f"\n{i}. {file_name}") print(f" Distance: {comp['euclidean_distance']:.4f}") - print(f" Regex: L={comp['differences']['L']['regex']:.2f}, J={comp['differences']['J']['regex']:.2f}, P={comp['differences']['P']['regex']:.2f}, W={comp['differences']['W']['regex']:.2f}") - print(f" AST: L={comp['differences']['L']['ast']:.2f}, J={comp['differences']['J']['ast']:.2f}, P={comp['differences']['P']['ast']:.2f}, W={comp['differences']['W']['ast']:.2f}") + print( + f" Regex: L={comp['differences']['L']['regex']:.2f}, J={comp['differences']['J']['regex']:.2f}, P={comp['differences']['P']['regex']:.2f}, W={comp['differences']['W']['regex']:.2f}" + ) + print( + f" AST: L={comp['differences']['L']['ast']:.2f}, J={comp['differences']['J']['ast']:.2f}, P={comp['differences']['P']['ast']:.2f}, W={comp['differences']['W']['ast']:.2f}" + ) print(f" Meaning preserved: {'✓' if comp['meaning_preserved'] else '✗'}") print(f"\n{'='*70}") @@ -232,13 +244,15 @@ def print_detailed_report(results: Dict[str, Any]): print(f"{'='*70}\n") # Data-driven recommendation - if results['meaning_preserved_pct'] < 80: + if results["meaning_preserved_pct"] < 80: print("❌ AST approach FAILS meaning preservation test") - print(f" Only {results['meaning_preserved_pct']:.1f}% of files preserved strongest/weakest dims") + print( + f" Only {results['meaning_preserved_pct']:.1f}% of files preserved strongest/weakest dims" + ) print(f" Threshold: 80%") print(f"\n→ RECOMMENDATION: Stick with proven regex-based approach") - elif results['avg_euclidean_distance'] > 0.2: + elif results["avg_euclidean_distance"] > 0.2: print("⚠️ AST approach has HIGH coordinate differences") print(f" Average distance: {results['avg_euclidean_distance']:.4f}") print(f" Threshold: 0.2") @@ -248,7 +262,9 @@ def print_detailed_report(results: Dict[str, Any]): print("⚠️ AST approach is SIGNIFICANTLY slower") print(f" Regex is {speed_ratio:.1f}x faster") print(f" Threshold: 2x") - print(f"\n→ RECOMMENDATION: Stick with proven regex-based approach unless accuracy gains are substantial") + print( + f"\n→ RECOMMENDATION: Stick with proven regex-based approach unless accuracy gains are substantial" + ) else: print("✅ AST approach passes basic tests") @@ -261,9 +277,9 @@ def print_detailed_report(results: Dict[str, Any]): def main(): """Run comparison test""" - print("="*70) + print("=" * 70) print("AST vs REGEX ANALYZER COMPARISON") - print("="*70) + print("=" * 70) print() print("This test compares two approaches to LJPW code analysis:") print("1. Regex-based (current, proven on 9,538 files)") @@ -295,5 +311,5 @@ def main(): return 0 -if __name__ == '__main__': +if __name__ == "__main__": exit(main()) diff --git a/tools/cross_domain_mapper.py b/tools/cross_domain_mapper.py index c5a633e..db38c18 100644 --- a/tools/cross_domain_mapper.py +++ b/tools/cross_domain_mapper.py @@ -15,22 +15,25 @@ import json import math -from typing import Tuple, Dict, List from dataclasses import dataclass +from typing import Dict, List, Tuple # ============================================================================ # SYSTEM ARCHETYPES # ============================================================================ + @dataclass class SystemProfile: """Profile of a system in LJPW space.""" + name: str category: str coords: Tuple[float, float, float, float] description: str examples: List[str] + # Organization Archetypes ORGANIZATION_ARCHETYPES = { "early_startup": SystemProfile( @@ -38,35 +41,35 @@ class SystemProfile: category="organization", coords=(0.2, 0.3, 0.9, 0.8), description="Small team, high risk, fast execution, innovative", - examples=["Y Combinator startups", "Garage phase companies"] + examples=["Y Combinator startups", "Garage phase companies"], ), "growth_startup": SystemProfile( name="Growth Startup", category="organization", coords=(0.4, 0.5, 0.8, 0.7), description="Scaling team, adding process, still fast", - examples=["Series B companies", "Hypergrowth phase"] + examples=["Series B companies", "Hypergrowth phase"], ), "enterprise": SystemProfile( name="Large Corporation", category="organization", coords=(0.9, 0.9, 0.3, 0.5), description="Bureaucratic, risk-averse, slow but stable", - examples=["Fortune 500", "Large banks", "Government agencies"] + examples=["Fortune 500", "Large banks", "Government agencies"], ), "open_source": SystemProfile( name="Open Source Community", category="organization", coords=(0.5, 0.4, 0.7, 0.9), description="Decentralized, collaborative, innovative", - examples=["Linux kernel", "Python community"] + examples=["Linux kernel", "Python community"], ), "research_lab": SystemProfile( name="Research Lab", category="organization", coords=(0.6, 0.6, 0.4, 0.95), description="Exploratory, experimental, knowledge-seeking", - examples=["Bell Labs", "PARC", "DeepMind"] + examples=["Bell Labs", "PARC", "DeepMind"], ), } @@ -77,35 +80,35 @@ class SystemProfile: category="narrative", coords=(0.8, 0.9, 0.8, 0.9), description="Protagonist overcomes trials, gains wisdom", - examples=["Star Wars", "Lord of the Rings", "The Matrix"] + examples=["Star Wars", "Lord of the Rings", "The Matrix"], ), "tragedy": SystemProfile( name="Tragedy", category="narrative", coords=(0.1, 0.7, 0.4, 0.6), description="Protagonist's flaw leads to downfall", - examples=["Hamlet", "Macbeth", "Breaking Bad"] + examples=["Hamlet", "Macbeth", "Breaking Bad"], ), "comedy": SystemProfile( name="Comedy", category="narrative", coords=(0.9, 0.5, 0.6, 0.7), description="Misunderstandings resolved, happy ending", - examples=["Pride and Prejudice", "Much Ado About Nothing"] + examples=["Pride and Prejudice", "Much Ado About Nothing"], ), "horror": SystemProfile( name="Horror", category="narrative", coords=(0.05, 0.2, 0.1, 0.3), description="Overwhelming threat, powerlessness, dread", - examples=["The Shining", "Alien", "Lovecraft"] + examples=["The Shining", "Alien", "Lovecraft"], ), "mystery": SystemProfile( name="Mystery", category="narrative", coords=(0.6, 0.8, 0.5, 0.8), description="Puzzle to solve, investigation, revelation", - examples=["Sherlock Holmes", "Knives Out"] + examples=["Sherlock Holmes", "Knives Out"], ), } @@ -116,21 +119,21 @@ class SystemProfile: category="biological", coords=(0.5, 0.3, 0.95, 0.4), description="Simple, fast reproduction, adaptable", - examples=["E. coli", "Prokaryotes"] + examples=["E. coli", "Prokaryotes"], ), "mammal": SystemProfile( name="Mammal", category="biological", coords=(0.8, 0.6, 0.6, 0.8), description="Complex, adaptable, parental care", - examples=["Humans", "Dolphins", "Elephants"] + examples=["Humans", "Dolphins", "Elephants"], ), "ecosystem": SystemProfile( name="Forest Ecosystem", category="biological", coords=(0.8, 0.5, 0.6, 0.85), description="Interconnected, self-regulating, resilient", - examples=["Rainforest", "Coral reef"] + examples=["Rainforest", "Coral reef"], ), } @@ -138,12 +141,13 @@ class SystemProfile: # MAPPING FUNCTIONS # ============================================================================ + def calculate_distance( - coords1: Tuple[float, float, float, float], - coords2: Tuple[float, float, float, float] + coords1: Tuple[float, float, float, float], coords2: Tuple[float, float, float, float] ) -> float: """Calculate Euclidean distance in LJPW space.""" - return math.sqrt(sum((a-b)**2 for a, b in zip(coords1, coords2))) + return math.sqrt(sum((a - b) ** 2 for a, b in zip(coords1, coords2))) + def map_organization(archetype: str) -> Tuple[float, float, float, float]: """Map an organization archetype to LJPW coordinates.""" @@ -151,21 +155,23 @@ def map_organization(archetype: str) -> Tuple[float, float, float, float]: raise ValueError(f"Unknown organization archetype: {archetype}") return ORGANIZATION_ARCHETYPES[archetype].coords + def map_narrative(archetype: str) -> Tuple[float, float, float, float]: """Map a narrative archetype to LJPW coordinates.""" if archetype not in NARRATIVE_ARCHETYPES: raise ValueError(f"Unknown narrative archetype: {archetype}") return NARRATIVE_ARCHETYPES[archetype].coords + def map_biological(archetype: str) -> Tuple[float, float, float, float]: """Map a biological system archetype to LJPW coordinates.""" if archetype not in BIOLOGICAL_ARCHETYPES: raise ValueError(f"Unknown biological archetype: {archetype}") return BIOLOGICAL_ARCHETYPES[archetype].coords + def find_similar_systems( - coords: Tuple[float, float, float, float], - max_distance: float = 0.5 + coords: Tuple[float, float, float, float], max_distance: float = 0.5 ) -> List[Tuple[str, str, float]]: """ Find systems similar to given coordinates. @@ -174,11 +180,7 @@ def find_similar_systems( """ similar = [] - all_archetypes = { - **ORGANIZATION_ARCHETYPES, - **NARRATIVE_ARCHETYPES, - **BIOLOGICAL_ARCHETYPES - } + all_archetypes = {**ORGANIZATION_ARCHETYPES, **NARRATIVE_ARCHETYPES, **BIOLOGICAL_ARCHETYPES} for key, profile in all_archetypes.items(): dist = calculate_distance(coords, profile.coords) @@ -187,10 +189,9 @@ def find_similar_systems( return sorted(similar, key=lambda x: x[2]) + def compare_across_domains( - org_archetype: str, - narrative_archetype: str, - bio_archetype: str = None + org_archetype: str, narrative_archetype: str, bio_archetype: str = None ) -> Dict: """ Compare systems across different domains. @@ -204,15 +205,15 @@ def compare_across_domains( "organization": { "archetype": org_archetype, "coords": org_coords, - "name": ORGANIZATION_ARCHETYPES[org_archetype].name + "name": ORGANIZATION_ARCHETYPES[org_archetype].name, }, "narrative": { "archetype": narrative_archetype, "coords": narrative_coords, - "name": NARRATIVE_ARCHETYPES[narrative_archetype].name + "name": NARRATIVE_ARCHETYPES[narrative_archetype].name, }, "distance": calculate_distance(org_coords, narrative_coords), - "interpretation": None + "interpretation": None, } # Add biological if provided @@ -221,7 +222,7 @@ def compare_across_domains( result["biological"] = { "archetype": bio_archetype, "coords": bio_coords, - "name": BIOLOGICAL_ARCHETYPES[bio_archetype].name + "name": BIOLOGICAL_ARCHETYPES[bio_archetype].name, } result["org_bio_distance"] = calculate_distance(org_coords, bio_coords) result["narrative_bio_distance"] = calculate_distance(narrative_coords, bio_coords) @@ -237,10 +238,12 @@ def compare_across_domains( return result + # ============================================================================ # ANALYSIS FUNCTIONS # ============================================================================ + def analyze_system_cluster(category: str) -> Dict: """Analyze clustering within a category.""" if category == "organization": @@ -257,16 +260,18 @@ def analyze_system_cluster(category: str) -> Dict: distances = [] for i, key1 in enumerate(keys): - for key2 in keys[i+1:]: + for key2 in keys[i + 1 :]: coords1 = archetypes[key1].coords coords2 = archetypes[key2].coords dist = calculate_distance(coords1, coords2) - distances.append({ - "system1": archetypes[key1].name, - "system2": archetypes[key2].name, - "distance": dist - }) + distances.append( + { + "system1": archetypes[key1].name, + "system2": archetypes[key2].name, + "distance": dist, + } + ) # Sort by distance distances.sort(key=lambda x: x["distance"]) @@ -276,20 +281,17 @@ def analyze_system_cluster(category: str) -> Dict: "num_archetypes": len(archetypes), "most_similar": distances[0] if distances else None, "most_different": distances[-1] if distances else None, - "all_distances": distances + "all_distances": distances, } + def validate_universality() -> Dict: """ Test if LJPW preserves meaning across domains. Returns validation metrics. """ - results = { - "tested": True, - "cross_domain_examples": [], - "validation": None - } + results = {"tested": True, "cross_domain_examples": [], "validation": None} # Test: Do semantically similar systems cluster? examples = [ @@ -297,23 +299,23 @@ def validate_universality() -> Dict: "comparison": "Fast/Risky systems", "systems": [ ("Bacteria", "biological", BIOLOGICAL_ARCHETYPES["bacteria"].coords), - ("Early Startup", "organization", ORGANIZATION_ARCHETYPES["early_startup"].coords) - ] + ("Early Startup", "organization", ORGANIZATION_ARCHETYPES["early_startup"].coords), + ], }, { "comparison": "Wise/Powerful systems", "systems": [ ("Hero's Journey", "narrative", NARRATIVE_ARCHETYPES["heros_journey"].coords), - ("Mammal", "biological", BIOLOGICAL_ARCHETYPES["mammal"].coords) - ] + ("Mammal", "biological", BIOLOGICAL_ARCHETYPES["mammal"].coords), + ], }, { "comparison": "Chaotic/Dangerous systems", "systems": [ ("Horror", "narrative", NARRATIVE_ARCHETYPES["horror"].coords), - ("Early Startup", "organization", ORGANIZATION_ARCHETYPES["early_startup"].coords) - ] - } + ("Early Startup", "organization", ORGANIZATION_ARCHETYPES["early_startup"].coords), + ], + }, ] for example in examples: @@ -333,10 +335,12 @@ def validate_universality() -> Dict: return results + # ============================================================================ # VISUALIZATION # ============================================================================ + def format_system_info(profile: SystemProfile) -> str: """Format system profile for display.""" L, J, P, W = profile.coords @@ -350,6 +354,7 @@ def format_system_info(profile: SystemProfile) -> str: Examples: {', '.join(profile.examples[:2])} """ + # ============================================================================ # MAIN DEMO # ============================================================================ @@ -395,12 +400,12 @@ def format_system_info(profile: SystemProfile) -> str: print(f"\n{org_analysis['category'].upper()} Systems:") print(f" Total archetypes: {org_analysis['num_archetypes']}") - if org_analysis['most_similar']: - ms = org_analysis['most_similar'] + if org_analysis["most_similar"]: + ms = org_analysis["most_similar"] print(f" Most similar: {ms['system1']} ↔ {ms['system2']} (d={ms['distance']:.3f})") - if org_analysis['most_different']: - md = org_analysis['most_different'] + if org_analysis["most_different"]: + md = org_analysis["most_different"] print(f" Most different: {md['system1']} ↔ {md['system2']} (d={md['distance']:.3f})") # Demo 4: Validate universality @@ -412,10 +417,10 @@ def format_system_info(profile: SystemProfile) -> str: print(f"\nValidation: {validation['validation']}") print(f"\nCross-domain semantic correlations:") - for example in validation['cross_domain_examples']: + for example in validation["cross_domain_examples"]: print(f"\n {example['comparison']}:") - s1 = example['systems'][0] - s2 = example['systems'][1] + s1 = example["systems"][0] + s2 = example["systems"][1] print(f" {s1[0]} ({s1[1]}) ↔ {s2[0]} ({s2[1]})") print(f" Distance: {example['distance']:.3f} {'✓' if example['similar'] else '✗'}") diff --git a/tools/cross_domain_validator.py b/tools/cross_domain_validator.py index a4fef62..a46c88e 100644 --- a/tools/cross_domain_validator.py +++ b/tools/cross_domain_validator.py @@ -18,7 +18,7 @@ from pathlib import Path # Add project root to path -sys.path.insert(0, str(Path(__file__).parent.parent / 'src' / 'ljpw')) +sys.path.insert(0, str(Path(__file__).parent.parent / "src" / "ljpw")) # Mathematical constants E = math.e @@ -28,8 +28,8 @@ SQRT2 = math.sqrt(2) # Predicted constants -PSI = (E + PI) / 2 # 2.929937241 - Universal Harmony Constant -THETA = PSI * LN2 * math.sqrt(PI/2) # 2.545327780 - Information-Energy Bridge +PSI = (E + PI) / 2 # 2.929937241 - Universal Harmony Constant +THETA = PSI * LN2 * math.sqrt(PI / 2) # 2.545327780 - Information-Energy Bridge def print_header(title): @@ -57,6 +57,7 @@ def print_result(test_name, prediction, measured, tolerance=0.05): # MUSIC THEORY VALIDATION # ============================================================================= + def validate_music_theory(): """ Test if Ψ appears in musical harmony relationships. @@ -74,27 +75,27 @@ def validate_music_theory(): # Standard musical intervals (frequency ratios) intervals = { "Unison": 1.0, - "Minor second": 16/15, - "Major second": 9/8, - "Minor third": 6/5, - "Major third": 5/4, - "Perfect fourth": 4/3, - "Tritone": 45/32, # or sqrt(2) - "Perfect fifth": 3/2, - "Minor sixth": 8/5, - "Major sixth": 5/3, - "Minor seventh": 16/9, - "Major seventh": 15/8, - "Octave": 2/1, - "Major tenth": 5/2, - "Perfect eleventh": 8/3, - "Perfect twelfth": 3/1, + "Minor second": 16 / 15, + "Major second": 9 / 8, + "Minor third": 6 / 5, + "Major third": 5 / 4, + "Perfect fourth": 4 / 3, + "Tritone": 45 / 32, # or sqrt(2) + "Perfect fifth": 3 / 2, + "Minor sixth": 8 / 5, + "Major sixth": 5 / 3, + "Minor seventh": 16 / 9, + "Major seventh": 15 / 8, + "Octave": 2 / 1, + "Major tenth": 5 / 2, + "Perfect eleventh": 8 / 3, + "Perfect twelfth": 3 / 1, } print(f"Searching for intervals near Ψ = {PSI:.6f}...\n") closest_interval = None - closest_distance = float('inf') + closest_distance = float("inf") for name, ratio in intervals.items(): distance = abs(ratio - PSI) @@ -115,7 +116,7 @@ def validate_music_theory(): print("Searching for simple ratios m/n near Ψ (where m, n ≤ 50)...\n") best_rational_approx = None - best_error = float('inf') + best_error = float("inf") for denominator in range(1, 51): for numerator in range(denominator, 51): @@ -182,7 +183,9 @@ def validate_music_theory(): closest_to_theta = min(intervals.items(), key=lambda x: abs(x[1] - THETA)) print(f" Closest interval: {closest_to_theta[0]} = {closest_to_theta[1]:.6f}") - print(f" Distance: {abs(closest_to_theta[1] - THETA):.6f} ({abs(closest_to_theta[1] - THETA)/THETA*100:.2f}%)") + print( + f" Distance: {abs(closest_to_theta[1] - THETA):.6f} ({abs(closest_to_theta[1] - THETA)/THETA*100:.2f}%)" + ) # Summary print("\n\nMUSIC THEORY SUMMARY") @@ -203,6 +206,7 @@ def validate_music_theory(): # VISUAL ART VALIDATION # ============================================================================= + def validate_visual_art(): """ Test if Ψ appears in visual composition and aesthetics. @@ -221,11 +225,11 @@ def validate_visual_art(): aspect_ratios = { "Golden rectangle": PHI, # 1.618 "Square": 1.0, - "3:2 (35mm film)": 3/2, - "4:3 (traditional TV)": 4/3, - "16:9 (HDTV)": 16/9, - "5:4 (large format photo)": 5/4, - "3:1 (panorama)": 3/1, + "3:2 (35mm film)": 3 / 2, + "4:3 (traditional TV)": 4 / 3, + "16:9 (HDTV)": 16 / 9, + "5:4 (large format photo)": 5 / 4, + "3:1 (panorama)": 3 / 1, "√2 (ISO paper)": SQRT2, # 1.414 "√3": math.sqrt(3), # 1.732 "√5": math.sqrt(5), # 2.236 @@ -236,7 +240,7 @@ def validate_visual_art(): print(f"Searching for aspect ratios near Ψ = {PSI:.6f}...\n") closest_ratio = None - closest_distance = float('inf') + closest_distance = float("inf") for name, ratio in sorted(aspect_ratios.items(), key=lambda x: x[1]): distance = abs(ratio - PSI) @@ -261,8 +265,12 @@ def validate_visual_art(): area_sqrt2 = SQRT2 print(f" Area (unit width): {area_psi:.6f}") - print(f" Compare to golden rectangle: {area_golden:.6f} (Ψ is {(area_psi/area_golden - 1)*100:+.2f}%)") - print(f" Compare to √2 rectangle: {area_sqrt2:.6f} (Ψ is {(area_psi/area_sqrt2 - 1)*100:+.2f}%)") + print( + f" Compare to golden rectangle: {area_golden:.6f} (Ψ is {(area_psi/area_golden - 1)*100:+.2f}%)" + ) + print( + f" Compare to √2 rectangle: {area_sqrt2:.6f} (Ψ is {(area_psi/area_sqrt2 - 1)*100:+.2f}%)" + ) # Diagonal diagonal_psi = math.sqrt(1 + PSI**2) @@ -288,7 +296,9 @@ def validate_visual_art(): print(f" Golden ratio: {golden_division:.6f} (61.8% from edge)") print(f" Ψ division: {psi_division:.6f} (34.1% from edge)") - print(f"\n Ψ division is {abs(psi_division - thirds_division)/thirds_division*100:.1f}% different from rule of thirds") + print( + f"\n Ψ division is {abs(psi_division - thirds_division)/thirds_division*100:.1f}% different from rule of thirds" + ) print(f" This suggests Ψ ≈ rule of thirds (but more precise)") # Test 4: Color harmony (hue angles) @@ -341,6 +351,7 @@ def validate_visual_art(): # MATHEMATICS VALIDATION # ============================================================================= + def validate_mathematics(): """ Test if Ψ appears in mathematical contexts. @@ -358,11 +369,11 @@ def validate_mathematics(): print(f"Ψ = (e + π)/2 = {PSI:.9f}\n") relationships = [ - ("Ψ/e", PSI/E, "Ratio to Euler's number"), - ("Ψ/π", PSI/PI, "Ratio to pi"), - ("Ψ/φ", PSI/PHI, "Ratio to golden ratio"), - ("Ψ/√2", PSI/SQRT2, "Ratio to root 2"), - ("Ψ×ln(2)", PSI*LN2, "Product with information unit"), + ("Ψ/e", PSI / E, "Ratio to Euler's number"), + ("Ψ/π", PSI / PI, "Ratio to pi"), + ("Ψ/φ", PSI / PHI, "Ratio to golden ratio"), + ("Ψ/√2", PSI / SQRT2, "Ratio to root 2"), + ("Ψ×ln(2)", PSI * LN2, "Product with information unit"), ("Ψ²", PSI**2, "Square of Ψ"), ("Ψ³", PSI**3, "Cube of Ψ"), ("e^Ψ", math.exp(PSI), "Exponential of Ψ"), @@ -378,8 +389,8 @@ def validate_mathematics(): approximations = [ ("Ψ² ≈ e² + π?", PSI**2, E**2 + PI), - ("Ψ³ ≈ 5π + 10?", PSI**3, 5*PI + 10), - ("Ψ ≈ 3 - π/10?", PSI, 3 - PI/10), + ("Ψ³ ≈ 5π + 10?", PSI**3, 5 * PI + 10), + ("Ψ ≈ 3 - π/10?", PSI, 3 - PI / 10), ("Ψ ≈ e + ln(2)?", PSI, E + LN2), ] @@ -395,8 +406,8 @@ def validate_mathematics(): # Can we express Ψ as a series? # Ψ = (e + π)/2 = e/2 + π/2 - e_series_5 = sum([1/math.factorial(n) for n in range(20)]) # e ≈ sum(1/n!) - pi_approx = sum([(-1)**n / (2*n + 1) for n in range(10000)]) * 4 # Leibniz formula + e_series_5 = sum([1 / math.factorial(n) for n in range(20)]) # e ≈ sum(1/n!) + pi_approx = sum([(-1) ** n / (2 * n + 1) for n in range(10000)]) * 4 # Leibniz formula psi_series = (e_series_5 + pi_approx) / 2 print(f" Ψ from series: {psi_series:.9f}") @@ -478,9 +489,9 @@ def f(x): print(f"Θ = {THETA:.9f}\n") theta_relations = [ - ("Θ/Ψ", THETA/PSI, f"= ln(2)√(π/2) = {LN2*math.sqrt(PI/2):.9f}"), - ("Θ/ln(2)", THETA/LN2, f"= Ψ√(π/2) = {PSI*math.sqrt(PI/2):.9f}"), - ("Θ/√(π/2)", THETA/math.sqrt(PI/2), f"= Ψ×ln(2) = {PSI*LN2:.9f}"), + ("Θ/Ψ", THETA / PSI, f"= ln(2)√(π/2) = {LN2*math.sqrt(PI/2):.9f}"), + ("Θ/ln(2)", THETA / LN2, f"= Ψ√(π/2) = {PSI*math.sqrt(PI/2):.9f}"), + ("Θ/√(π/2)", THETA / math.sqrt(PI / 2), f"= Ψ×ln(2) = {PSI*LN2:.9f}"), ("Θ²", THETA**2, "Square"), ("√Θ", math.sqrt(THETA), "Square root"), ] @@ -495,11 +506,11 @@ def f(x): print("Comparing Ψ = (e+π)/2 to other means of fundamental constants:\n") means = [ - ("(e+π)/2", (E+PI)/2, "Ψ"), - ("(e+φ)/2", (E+PHI)/2, "Mean of e and φ"), - ("(π+φ)/2", (PI+PHI)/2, "Mean of π and φ"), - ("(e+√2)/2", (E+SQRT2)/2, "Mean of e and √2"), - ("(π+√2)/2", (PI+SQRT2)/2, "Mean of π and √2"), + ("(e+π)/2", (E + PI) / 2, "Ψ"), + ("(e+φ)/2", (E + PHI) / 2, "Mean of e and φ"), + ("(π+φ)/2", (PI + PHI) / 2, "Mean of π and φ"), + ("(e+√2)/2", (E + SQRT2) / 2, "Mean of e and √2"), + ("(π+√2)/2", (PI + SQRT2) / 2, "Mean of π and √2"), ] for name, value, description in means: @@ -525,16 +536,17 @@ def f(x): # MAIN VALIDATION # ============================================================================= + def main(): """Run all cross-domain validations""" - print("\n" + "="*80) + print("\n" + "=" * 80) print("CROSS-DOMAIN VALIDATION OF Ψ AND Θ".center(80)) - print("="*80) + print("=" * 80) print(f"\nΨ (Universal Harmony Constant) = {PSI:.9f}") print(f"Θ (Information-Energy Bridge) = {THETA:.9f}") print(f"\nTesting appearance in: Music Theory, Visual Art, Mathematics") - print("="*80) + print("=" * 80) # Run validations music_results = validate_music_theory() @@ -561,9 +573,9 @@ def main(): print(" ✓ Well-defined information content (1.074 bits)") print(" ⭐ PREDICTION: Ψ appears in variational calculus") - print("\n" + "="*80) + print("\n" + "=" * 80) print("CONFIDENCE ASSESSMENT".center(80)) - print("="*80) + print("=" * 80) print("\nΨ Cross-Domain Presence:") print(" Music Theory: 🟡 MODERATE (no exact match, but close approximations)") @@ -575,11 +587,12 @@ def main(): print(" Visual Art: 🟡 MODERATE (between √2 and √5)") print(" Mathematics: 🟢 STRONG (exact mathematical definition)") - print("\n" + "="*80) + print("\n" + "=" * 80) print("INTERPRETATION".center(80)) - print("="*80) + print("=" * 80) - print(""" + print( + """ The cross-domain validation reveals an interesting pattern: 1. **No Exact Matches**: Neither Ψ nor Θ exactly match traditional constants @@ -611,11 +624,12 @@ def main(): - Test Ψ ratios in musical composition (create Ψ-based harmonies) - Test Ψ aspect ratios in visual perception studies - Search for Ψ in variational calculus and optimization literature -""") +""" + ) - print("="*80) + print("=" * 80) print("\nValidation complete!\n") -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/tools/evolution_visualizer.py b/tools/evolution_visualizer.py index abbb6c2..3bbb920 100644 --- a/tools/evolution_visualizer.py +++ b/tools/evolution_visualizer.py @@ -16,15 +16,15 @@ python evolution_visualizer.py --output evolution.html """ -import sys -from pathlib import Path -import subprocess import argparse import json +import subprocess +import sys from datetime import datetime +from pathlib import Path -sys.path.insert(0, str(Path(__file__).parent.parent / 'src' / 'ljpw')) -from ljpw_standalone import analyze_quick, calculate_distance +sys.path.insert(0, str(Path(__file__).parent.parent)) +from src.ljpw.ljpw_standalone import analyze_quick, calculate_distance # Natural Equilibrium NATURAL_EQUILIBRIUM = (0.618, 0.414, 0.718, 0.693) @@ -32,10 +32,10 @@ def get_genome(result): """Create genome from LJPW result""" - L = result['ljpw']['L'] - J = result['ljpw']['J'] - P = result['ljpw']['P'] - W = result['ljpw']['W'] + L = result["ljpw"]["L"] + J = result["ljpw"]["J"] + P = result["ljpw"]["P"] + W = result["ljpw"]["W"] L_digit = int(round(L * 10)) % 10 J_digit = int(round(J * 10)) % 10 @@ -56,12 +56,12 @@ def get_git_history(filepath, num_commits=50): """Get git history for file""" try: result = subprocess.run( - ['git', 'log', f'-{num_commits}', '--format=%H|%ai|%s', '--', filepath], + ["git", "log", f"-{num_commits}", "--format=%H|%ai|%s", "--", filepath], capture_output=True, text=True, - check=True + check=True, ) - return result.stdout.strip().split('\n') + return result.stdout.strip().split("\n") except subprocess.CalledProcessError: return [] @@ -70,10 +70,7 @@ def get_git_file_content(commit, filepath): """Get file content at commit""" try: result = subprocess.run( - ['git', 'show', f'{commit}:{filepath}'], - capture_output=True, - text=True, - check=True + ["git", "show", f"{commit}:{filepath}"], capture_output=True, text=True, check=True ) return result.stdout except: @@ -87,7 +84,7 @@ def analyze_evolution(filepath, num_commits=50): commits = get_git_history(filepath, num_commits) - if not commits or commits[0] == '': + if not commits or commits[0] == "": print(f"No git history found for {filepath}") return [] @@ -97,11 +94,11 @@ def analyze_evolution(filepath, num_commits=50): if not commit_line.strip(): continue - parts = commit_line.split('|') + parts = commit_line.split("|") if len(parts) < 3: continue - commit_hash, commit_date, commit_msg = parts[0], parts[1], '|'.join(parts[2:]) + commit_hash, commit_date, commit_msg = parts[0], parts[1], "|".join(parts[2:]) # Get code at this commit code = get_git_file_content(commit_hash, filepath) @@ -111,29 +108,31 @@ def analyze_evolution(filepath, num_commits=50): # Analyze try: result = analyze_quick(code) - l = result['ljpw']['L'] - j = result['ljpw']['J'] - p = result['ljpw']['P'] - w = result['ljpw']['W'] + l = result["ljpw"]["L"] + j = result["ljpw"]["J"] + p = result["ljpw"]["P"] + w = result["ljpw"]["W"] coords = (l, j, p, w) genome = get_genome(result) dist_to_ne = calculate_distance(coords, NATURAL_EQUILIBRIUM) health = calculate_health(coords) - evolution.append({ - 'commit': commit_hash[:7], - 'date': commit_date, - 'message': commit_msg[:60], - 'L': l, - 'J': j, - 'P': p, - 'W': w, - 'genome': genome, - 'dist_ne': dist_to_ne, - 'health': health, - 'loc': len(code.split('\n')) - }) + evolution.append( + { + "commit": commit_hash[:7], + "date": commit_date, + "message": commit_msg[:60], + "L": l, + "J": j, + "P": p, + "W": w, + "genome": genome, + "dist_ne": dist_to_ne, + "health": health, + "loc": len(code.split("\n")), + } + ) print(f" [{i+1}/{len(commits)}] {commit_hash[:7]} - {health:.1f}/100") @@ -287,12 +286,12 @@ def generate_html(evolution, filepath, output_file): # Prepare data for charts labels = [f"{e['commit']}" for e in evolution] - l_data = [e['L'] for e in evolution] - j_data = [e['J'] for e in evolution] - p_data = [e['P'] for e in evolution] - w_data = [e['W'] for e in evolution] - health_data = [e['health'] for e in evolution] - ne_data = [e['dist_ne'] for e in evolution] + l_data = [e["L"] for e in evolution] + j_data = [e["J"] for e in evolution] + p_data = [e["P"] for e in evolution] + w_data = [e["W"] for e in evolution] + health_data = [e["health"] for e in evolution] + ne_data = [e["dist_ne"] for e in evolution] html += f""" @@ -432,7 +431,7 @@ def generate_html(evolution, filepath, output_file): """ - with open(output_file, 'w') as f: + with open(output_file, "w") as f: f.write(html) print(f"\n✓ Visualization saved to {output_file}") @@ -440,10 +439,10 @@ def generate_html(evolution, filepath, output_file): def main(): - parser = argparse.ArgumentParser(description='Code Evolution Visualizer') - parser.add_argument('file', help='File to analyze') - parser.add_argument('--commits', type=int, default=50, help='Number of commits') - parser.add_argument('--output', default='evolution.html', help='Output HTML file') + parser = argparse.ArgumentParser(description="Code Evolution Visualizer") + parser.add_argument("file", help="File to analyze") + parser.add_argument("--commits", type=int, default=50, help="Number of commits") + parser.add_argument("--output", default="evolution.html", help="Output HTML file") args = parser.parse_args() @@ -455,7 +454,7 @@ def main(): print(f"\n{'='*70}") print("EVOLUTION SUMMARY") - print('='*70) + print("=" * 70) print(f"File: {args.file}") print(f"Commits: {len(evolution)}") print(f"Date range: {evolution[0]['date'][:10]} → {evolution[-1]['date'][:10]}") diff --git a/tools/ljpw_project_manager.py b/tools/ljpw_project_manager.py index 304f3ce..f7dfb14 100644 --- a/tools/ljpw_project_manager.py +++ b/tools/ljpw_project_manager.py @@ -22,13 +22,12 @@ python ljpw_project_manager.py --dashboard """ -import math import json +import math +from dataclasses import asdict, dataclass from datetime import datetime, timedelta from pathlib import Path from typing import Dict, List, Tuple -from dataclasses import dataclass, asdict - # === LJPW Constants === @@ -38,9 +37,11 @@ # === Data Structures === + @dataclass class Task: """A project task with LJPW coordinates""" + id: str name: str track: str # A, B, C, or D @@ -67,10 +68,10 @@ def ljpw_coords(self) -> Tuple[float, float, float, float]: def distance_from_ne(self) -> float: """Distance from Natural Equilibrium""" return math.sqrt( - (self.L - NATURAL_EQUILIBRIUM[0])**2 + - (self.J - NATURAL_EQUILIBRIUM[1])**2 + - (self.P - NATURAL_EQUILIBRIUM[2])**2 + - (self.W - NATURAL_EQUILIBRIUM[3])**2 + (self.L - NATURAL_EQUILIBRIUM[0]) ** 2 + + (self.J - NATURAL_EQUILIBRIUM[1]) ** 2 + + (self.P - NATURAL_EQUILIBRIUM[2]) ** 2 + + (self.W - NATURAL_EQUILIBRIUM[3]) ** 2 ) def health_score(self) -> float: @@ -97,9 +98,8 @@ def health_score(self) -> float: effort_hours=20, dependencies=[], assigned_to="team", - deadline="2025-11-24" + deadline="2025-11-24", ), - Task( id="A2", name="Batch analyze all implementations", @@ -114,9 +114,8 @@ def health_score(self) -> float: effort_hours=10, dependencies=["A1"], assigned_to="automation", - deadline="2025-11-26" + deadline="2025-11-26", ), - Task( id="A3", name="Statistical analysis", @@ -131,9 +130,8 @@ def health_score(self) -> float: effort_hours=30, dependencies=["A2"], assigned_to="data_scientist", - deadline="2025-12-03" + deadline="2025-12-03", ), - Task( id="A4", name="Write empirical validation paper", @@ -148,9 +146,8 @@ def health_score(self) -> float: effort_hours=60, dependencies=["A3"], assigned_to="research_lead", - deadline="2025-12-20" + deadline="2025-12-20", ), - # TRACK B: Meaning Research (Physics/Chemistry/Biology) Task( id="B1", @@ -166,9 +163,8 @@ def health_score(self) -> float: effort_hours=25, dependencies=[], assigned_to="chemist", - deadline="2025-12-10" + deadline="2025-12-10", ), - Task( id="B2", name="Map biology constants", @@ -183,9 +179,8 @@ def health_score(self) -> float: effort_hours=25, dependencies=[], assigned_to="biologist", - deadline="2025-12-10" + deadline="2025-12-10", ), - Task( id="B3", name="Cross-domain synthesis paper", @@ -200,9 +195,8 @@ def health_score(self) -> float: effort_hours=80, dependencies=["B1", "B2"], assigned_to="research_lead", - deadline="2026-03-01" + deadline="2026-03-01", ), - # TRACK C: Practical Tools (Developer Productivity) Task( id="C1", @@ -218,9 +212,8 @@ def health_score(self) -> float: effort_hours=40, dependencies=[], assigned_to="frontend_engineer", - deadline="2025-12-15" + deadline="2025-12-15", ), - Task( id="C2", name="Train quality prediction model", @@ -235,9 +228,8 @@ def health_score(self) -> float: effort_hours=35, dependencies=[], assigned_to="ml_engineer", - deadline="2025-12-10" + deadline="2025-12-10", ), - Task( id="C3", name="Build semantic search engine", @@ -252,9 +244,8 @@ def health_score(self) -> float: effort_hours=45, dependencies=[], assigned_to="backend_engineer", - deadline="2025-12-20" + deadline="2025-12-20", ), - Task( id="C4", name="Create demonstration website", @@ -269,9 +260,8 @@ def health_score(self) -> float: effort_hours=30, dependencies=["C1", "C2", "C3"], assigned_to="fullstack_engineer", - deadline="2026-01-10" + deadline="2026-01-10", ), - # TRACK D: Experimental Validation (Ψ and Θ) Task( id="D1", @@ -287,9 +277,8 @@ def health_score(self) -> float: effort_hours=15, dependencies=[], assigned_to="research_lead", - deadline="2025-12-01" + deadline="2025-12-01", ), - Task( id="D2", name="Design quantum coherence experiment", @@ -304,9 +293,8 @@ def health_score(self) -> float: effort_hours=40, dependencies=["D1"], assigned_to="experimental_physicist", - deadline="2026-01-15" + deadline="2026-01-15", ), - Task( id="D3", name="Run pilot experiments", @@ -321,9 +309,8 @@ def health_score(self) -> float: effort_hours=120, dependencies=["D2"], assigned_to="lab_team", - deadline="2026-04-01" + deadline="2026-04-01", ), - # META TASKS (Project Management) Task( id="M1", @@ -339,9 +326,8 @@ def health_score(self) -> float: effort_hours=20, dependencies=[], assigned_to="claude", - deadline="2025-11-17" + deadline="2025-11-17", ), - Task( id="M2", name="Weekly progress reports", @@ -356,14 +342,17 @@ def health_score(self) -> float: effort_hours=2, # per week dependencies=["M1"], assigned_to="team", - deadline="ongoing" + deadline="ongoing", ), ] # === Analysis Functions === -def calculate_project_ljpw(tasks: List[Task], status_filter: str = None) -> Tuple[float, float, float, float]: + +def calculate_project_ljpw( + tasks: List[Task], status_filter: str = None +) -> Tuple[float, float, float, float]: """ Calculate overall project LJPW coordinates. @@ -387,7 +376,7 @@ def calculate_project_ljpw(tasks: List[Task], status_filter: str = None) -> Tupl def calculate_project_health(ljpw_coords: Tuple[float, float, float, float]) -> float: """Project health = closeness to Natural Equilibrium""" - d = math.sqrt(sum((c - ne)**2 for c, ne in zip(ljpw_coords, NATURAL_EQUILIBRIUM))) + d = math.sqrt(sum((c - ne) ** 2 for c, ne in zip(ljpw_coords, NATURAL_EQUILIBRIUM))) return max(0, 100 * (2 - d) / 2) @@ -407,15 +396,17 @@ def get_dimension_balance(tasks: List[Task]) -> Dict[str, float]: def variance(values): mean = sum(values) / len(values) - return sum((v - mean)**2 for v in values) / len(values) + return sum((v - mean) ** 2 for v in values) / len(values) return { - 'L_variance': variance(L_values), - 'J_variance': variance(J_values), - 'P_variance': variance(P_values), - 'W_variance': variance(W_values), - 'total_variance': (variance(L_values) + variance(J_values) + - variance(P_values) + variance(W_values)) / 4 + "L_variance": variance(L_values), + "J_variance": variance(J_values), + "P_variance": variance(P_values), + "W_variance": variance(W_values), + "total_variance": ( + variance(L_values) + variance(J_values) + variance(P_values) + variance(W_values) + ) + / 4, } @@ -445,8 +436,11 @@ def prioritize_tasks_by_ljpw(tasks: List[Task]) -> List[Task]: priority_bonus = task.priority * 10 # Dependency penalty - unresolved_deps = sum(1 for dep_id in task.dependencies - if not any(t.id == dep_id and t.status == "completed" for t in tasks)) + unresolved_deps = sum( + 1 + for dep_id in task.dependencies + if not any(t.id == dep_id and t.status == "completed" for t in tasks) + ) dep_penalty = unresolved_deps * 30 # Total score @@ -471,9 +465,9 @@ def generate_gantt_chart(tasks: List[Task]) -> str: # Build chart lines = [] - lines.append("\n" + "="*80) + lines.append("\n" + "=" * 80) lines.append("PROJECT GANTT CHART (LJPW-Optimized Execution Order)".center(80)) - lines.append("="*80 + "\n") + lines.append("=" * 80 + "\n") for track, track_tasks in sorted(tracks.items()): lines.append(f"\nTrack {track}:") @@ -481,27 +475,28 @@ def generate_gantt_chart(tasks: List[Task]) -> str: for task in sorted(track_tasks, key=lambda t: t.id): status_icon = { - 'completed': '✓', - 'in_progress': '▶', - 'pending': '○', - 'blocked': '✗' - }.get(task.status, '?') + "completed": "✓", + "in_progress": "▶", + "pending": "○", + "blocked": "✗", + }.get(task.status, "?") health = task.health_score() - health_bar = '█' * int(health / 10) + '░' * (10 - int(health / 10)) + health_bar = "█" * int(health / 10) + "░" * (10 - int(health / 10)) lines.append(f" {status_icon} {task.id}: {task.name:<40} [{health_bar}] {health:.0f}%") - return '\n'.join(lines) + return "\n".join(lines) # === Main Interface === + def show_project_status(): """Display current project status in LJPW space""" - print("\n" + "="*80) + print("\n" + "=" * 80) print("LJPW PROJECT STATUS DASHBOARD".center(80)) - print("="*80 + "\n") + print("=" * 80 + "\n") # Overall project LJPW all_coords = calculate_project_ljpw(TASKS) @@ -515,7 +510,7 @@ def show_project_status(): print(f"\n Project Health Score: {all_health:.1f}%") # Distance from NE - d_ne = math.sqrt(sum((c - ne)**2 for c, ne in zip(all_coords, NATURAL_EQUILIBRIUM))) + d_ne = math.sqrt(sum((c - ne) ** 2 for c, ne in zip(all_coords, NATURAL_EQUILIBRIUM))) print(f" Distance from Natural Equilibrium: {d_ne:.3f}") if d_ne < 0.2: @@ -526,9 +521,9 @@ def show_project_status(): print(" ✗ NEEDS REBALANCING - Significant imbalance detected") # Task breakdown by status - print("\n" + "-"*80) + print("\n" + "-" * 80) print("TASK STATUS BREAKDOWN:") - print("-"*80) + print("-" * 80) status_counts = {} for task in TASKS: @@ -540,11 +535,11 @@ def show_project_status(): print(f" {status.capitalize():<15} {count:>3} tasks ({pct:.0f}%)") # Track breakdown - print("\n" + "-"*80) + print("\n" + "-" * 80) print("PROGRESS BY TRACK:") - print("-"*80) + print("-" * 80) - for track in ['A', 'B', 'C', 'D', 'Meta']: + for track in ["A", "B", "C", "D", "Meta"]: track_tasks = [t for t in TASKS if t.track == track] if track_tasks: track_coords = calculate_project_ljpw(track_tasks) @@ -552,14 +547,18 @@ def show_project_status(): completed = sum(1 for t in track_tasks if t.status == "completed") total = len(track_tasks) - print(f"\n Track {track}: {completed}/{total} tasks completed ({completed/total*100:.0f}%)") - print(f" LJPW: L={track_coords[0]:.2f} J={track_coords[1]:.2f} P={track_coords[2]:.2f} W={track_coords[3]:.2f}") + print( + f"\n Track {track}: {completed}/{total} tasks completed ({completed/total*100:.0f}%)" + ) + print( + f" LJPW: L={track_coords[0]:.2f} J={track_coords[1]:.2f} P={track_coords[2]:.2f} W={track_coords[3]:.2f}" + ) print(f" Health: {track_health:.1f}%") # Dimension balance - print("\n" + "-"*80) + print("\n" + "-" * 80) print("DIMENSIONAL BALANCE:") - print("-"*80) + print("-" * 80) balance = get_dimension_balance(TASKS) print(f" L variance: {balance['L_variance']:.4f}") @@ -568,9 +567,9 @@ def show_project_status(): print(f" W variance: {balance['W_variance']:.4f}") print(f" Total variance: {balance['total_variance']:.4f}") - if balance['total_variance'] < 0.05: + if balance["total_variance"] < 0.05: print(" ✓ EXCELLENT BALANCE across all dimensions") - elif balance['total_variance'] < 0.10: + elif balance["total_variance"] < 0.10: print(" ⚠ GOOD BALANCE with minor variations") else: print(" ✗ IMBALANCED - Consider adding tasks to underrepresented dimensions") @@ -578,14 +577,14 @@ def show_project_status(): # Gantt chart print(generate_gantt_chart(TASKS)) - print("\n" + "="*80 + "\n") + print("\n" + "=" * 80 + "\n") def show_optimized_execution_order(): """Display LJPW-optimized task execution order""" - print("\n" + "="*80) + print("\n" + "=" * 80) print("LJPW-OPTIMIZED TASK EXECUTION ORDER".center(80)) - print("="*80 + "\n") + print("=" * 80 + "\n") prioritized = prioritize_tasks_by_ljpw(TASKS) @@ -593,29 +592,29 @@ def show_optimized_execution_order(): print("(Health + Impact + Priority - Dependency penalty)\n") print(f"{'Rank':<6} {'ID':<6} {'Task':<40} {'Health':<10} {'Track':<8}") - print("-"*80) + print("-" * 80) for rank, task in enumerate(prioritized[:15], 1): # Top 15 health = task.health_score() print(f"{rank:<6} {task.id:<6} {task.name:<40} {health:.1f}% {task.track:<8}") - print("\n" + "="*80 + "\n") + print("\n" + "=" * 80 + "\n") def save_project_state(): """Save current project state to JSON""" output = { - 'timestamp': datetime.now().isoformat(), - 'tasks': [asdict(t) for t in TASKS], - 'overall_ljpw': calculate_project_ljpw(TASKS), - 'project_health': calculate_project_health(calculate_project_ljpw(TASKS)), - 'dimensional_balance': get_dimension_balance(TASKS) + "timestamp": datetime.now().isoformat(), + "tasks": [asdict(t) for t in TASKS], + "overall_ljpw": calculate_project_ljpw(TASKS), + "project_health": calculate_project_health(calculate_project_ljpw(TASKS)), + "dimensional_balance": get_dimension_balance(TASKS), } - output_file = Path(__file__).parent.parent / 'results' / 'project_status.json' + output_file = Path(__file__).parent.parent / "results" / "project_status.json" output_file.parent.mkdir(exist_ok=True) - with open(output_file, 'w') as f: + with open(output_file, "w") as f: json.dump(output, f, indent=2) print(f"Project state saved to: {output_file}") @@ -628,11 +627,11 @@ def main(): if len(sys.argv) > 1: command = sys.argv[1] - if command == '--status': + if command == "--status": show_project_status() - elif command == '--optimize': + elif command == "--optimize": show_optimized_execution_order() - elif command == '--save': + elif command == "--save": save_project_state() else: print(f"Unknown command: {command}") @@ -644,5 +643,5 @@ def main(): save_project_state() -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/tools/multi_language_analyzer.py b/tools/multi_language_analyzer.py index 8ce1c28..918ed00 100644 --- a/tools/multi_language_analyzer.py +++ b/tools/multi_language_analyzer.py @@ -27,20 +27,20 @@ }) """ -import sys import ast -import re +import json import math -from pathlib import Path -from typing import Dict, List, Tuple, Any +import re +import sys from dataclasses import dataclass -import json +from pathlib import Path +from typing import Any, Dict, List, Tuple # Add project root to path -sys.path.insert(0, str(Path(__file__).parent.parent / 'src' / 'ljpw')) +sys.path.insert(0, str(Path(__file__).parent.parent)) try: - from ljpw_analyzer import compute_ljpw_coordinates + from src.ljpw.ljpw_standalone import compute_ljpw_coordinates except ImportError: # Fallback implementation pass @@ -49,6 +49,7 @@ @dataclass class LJPWCoordinates: """LJPW coordinates for a code implementation""" + L: float # Love (Safety) J: float # Justice (Structure) P: float # Power (Performance) @@ -59,37 +60,34 @@ class LJPWCoordinates: def to_dict(self): return { - 'L': self.L, - 'J': self.J, - 'P': self.P, - 'W': self.W, - 'language': self.language, - 'file_path': self.file_path, - 'distance_from_anchor': self.distance_from_anchor(), - 'distance_from_ne': self.distance_from_ne(), - 'divine_perfection': self.divine_perfection(), - 'physical_optimization': self.physical_optimization(), - 'health_score': self.health_score(), - 'metadata': self.metadata + "L": self.L, + "J": self.J, + "P": self.P, + "W": self.W, + "language": self.language, + "file_path": self.file_path, + "distance_from_anchor": self.distance_from_anchor(), + "distance_from_ne": self.distance_from_ne(), + "divine_perfection": self.divine_perfection(), + "physical_optimization": self.physical_optimization(), + "health_score": self.health_score(), + "metadata": self.metadata, } def distance_from_anchor(self): """Distance from Anchor Point (1,1,1,1)""" return math.sqrt( - (self.L - 1.0)**2 + - (self.J - 1.0)**2 + - (self.P - 1.0)**2 + - (self.W - 1.0)**2 + (self.L - 1.0) ** 2 + (self.J - 1.0) ** 2 + (self.P - 1.0) ** 2 + (self.W - 1.0) ** 2 ) def distance_from_ne(self): """Distance from Natural Equilibrium (0.618, 0.414, 0.718, 0.693)""" NE = (0.618, 0.414, 0.718, 0.693) return math.sqrt( - (self.L - NE[0])**2 + - (self.J - NE[1])**2 + - (self.P - NE[2])**2 + - (self.W - NE[3])**2 + (self.L - NE[0]) ** 2 + + (self.J - NE[1]) ** 2 + + (self.P - NE[2]) ** 2 + + (self.W - NE[3]) ** 2 ) def divine_perfection(self): @@ -104,13 +102,13 @@ def health_score(self): """Overall code health (balance between divine and physical)""" return (self.divine_perfection() + self.physical_optimization()) / 2 - def euclidean_distance(self, other: 'LJPWCoordinates') -> float: + def euclidean_distance(self, other: "LJPWCoordinates") -> float: """Compute 4D Euclidean distance to another LJPW coordinate""" return math.sqrt( - (self.L - other.L)**2 + - (self.J - other.J)**2 + - (self.P - other.P)**2 + - (self.W - other.W)**2 + (self.L - other.L) ** 2 + + (self.J - other.J) ** 2 + + (self.P - other.P) ** 2 + + (self.W - other.W) ** 2 ) @@ -133,11 +131,11 @@ def analyze(code: str, filepath: str = "unknown") -> LJPWCoordinates: W_score = 0.0 # Wisdom (Design) metadata = { - 'lines': len(code.split('\n')), - 'functions': 0, - 'classes': 0, - 'error_handling': 0, - 'complexity': 0, + "lines": len(code.split("\n")), + "functions": 0, + "classes": 0, + "error_handling": 0, + "complexity": 0, } # === L (Love/Safety) Analysis === @@ -154,7 +152,7 @@ def analyze(code: str, filepath: str = "unknown") -> LJPWCoordinates: if isinstance(node, ast.Assert): assertions += 1 - metadata['error_handling'] = error_handlers + metadata["error_handling"] = error_handlers # Base L score L_base = 0.3 # Baseline @@ -169,10 +167,12 @@ def analyze(code: str, filepath: str = "unknown") -> LJPWCoordinates: classes = sum(1 for node in ast.walk(tree) if isinstance(node, ast.ClassDef)) functions = sum(1 for node in ast.walk(tree) if isinstance(node, ast.FunctionDef)) - imports = sum(1 for node in ast.walk(tree) if isinstance(node, (ast.Import, ast.ImportFrom))) + imports = sum( + 1 for node in ast.walk(tree) if isinstance(node, (ast.Import, ast.ImportFrom)) + ) - metadata['classes'] = classes - metadata['functions'] = functions + metadata["classes"] = classes + metadata["functions"] = functions J_base = 0.2 J_classes = min(0.3, classes * 0.1) @@ -196,7 +196,7 @@ def analyze(code: str, filepath: str = "unknown") -> LJPWCoordinates: # Check for optimized builtins: map, filter, reduce, sum if isinstance(node, ast.Call): if isinstance(node.func, ast.Name): - if node.func.id in ('map', 'filter', 'sum', 'max', 'min', 'sorted'): + if node.func.id in ("map", "filter", "sum", "max", "min", "sorted"): builtin_usage += 1 P_base = 0.4 @@ -211,7 +211,7 @@ def analyze(code: str, filepath: str = "unknown") -> LJPWCoordinates: avg_func_length = PythonAnalyzer._avg_function_length(tree) good_naming = PythonAnalyzer._has_good_naming(code) - comments = code.count('#') + comments = code.count("#") W_base = 0.3 W_simplicity = 0.3 if avg_func_length < 20 else 0.1 # Short functions @@ -231,9 +231,9 @@ def analyze(code: str, filepath: str = "unknown") -> LJPWCoordinates: J=J_score, P=P_score, W=W_score, - language='python', + language="python", file_path=filepath, - metadata=metadata + metadata=metadata, ) @staticmethod @@ -251,7 +251,7 @@ def _avg_function_length(tree): func_lengths = [] for node in ast.walk(tree): if isinstance(node, ast.FunctionDef): - if hasattr(node, 'lineno') and hasattr(node, 'end_lineno'): + if hasattr(node, "lineno") and hasattr(node, "end_lineno"): length = node.end_lineno - node.lineno func_lengths.append(length) @@ -291,35 +291,35 @@ class JavaScriptAnalyzer: def analyze(code: str, filepath: str = "unknown") -> LJPWCoordinates: """Analyze JavaScript code (regex-based, approximate)""" - lines = code.split('\n') + lines = code.split("\n") metadata = { - 'lines': len(lines), - 'functions': 0, - 'classes': 0, - 'error_handling': 0, + "lines": len(lines), + "functions": 0, + "classes": 0, + "error_handling": 0, } # === L (Love/Safety) === - try_count = code.count('try') - catch_count = code.count('catch') + try_count = code.count("try") + catch_count = code.count("catch") error_handling = min(try_count, catch_count) # Match pairs L_base = 0.3 L_errors = min(0.4, error_handling * 0.15) - L_validation = 0.1 if 'typeof' in code or 'instanceof' in code else 0.0 + L_validation = 0.1 if "typeof" in code or "instanceof" in code else 0.0 L_strict = 0.1 if "'use strict'" in code or '"use strict"' in code else 0.0 L_score = L_base + L_errors + L_validation + L_strict # === J (Justice/Structure) === - class_count = code.count('class ') - function_count = len(re.findall(r'\bfunction\s+\w+', code)) - arrow_functions = code.count('=>') - import_count = code.count('import') + code.count('require') + class_count = code.count("class ") + function_count = len(re.findall(r"\bfunction\s+\w+", code)) + arrow_functions = code.count("=>") + import_count = code.count("import") + code.count("require") - metadata['functions'] = function_count + arrow_functions - metadata['classes'] = class_count - metadata['error_handling'] = error_handling + metadata["functions"] = function_count + arrow_functions + metadata["classes"] = class_count + metadata["error_handling"] = error_handling J_base = 0.2 J_classes = min(0.3, class_count * 0.15) @@ -329,10 +329,14 @@ def analyze(code: str, filepath: str = "unknown") -> LJPWCoordinates: J_score = J_base + J_classes + J_functions + J_imports # === P (Power/Performance) === - loops = code.count('for') + code.count('while') - array_methods = (code.count('.map(') + code.count('.filter(') + - code.count('.reduce(') + code.count('.forEach(')) - async_usage = code.count('async') + code.count('await') + loops = code.count("for") + code.count("while") + array_methods = ( + code.count(".map(") + + code.count(".filter(") + + code.count(".reduce(") + + code.count(".forEach(") + ) + async_usage = code.count("async") + code.count("await") P_base = 0.4 P_loops = min(0.2, loops * 0.05) @@ -342,10 +346,10 @@ def analyze(code: str, filepath: str = "unknown") -> LJPWCoordinates: P_score = P_base + P_loops + P_array + P_async # === W (Wisdom/Design) === - comments = code.count('//') + code.count('/*') - const_usage = code.count('const') - let_usage = code.count('let') - var_usage = code.count('var') + comments = code.count("//") + code.count("/*") + const_usage = code.count("const") + let_usage = code.count("let") + var_usage = code.count("var") # Prefer const > let > var (modern best practice) modern_vars = const_usage + let_usage @@ -369,9 +373,9 @@ def analyze(code: str, filepath: str = "unknown") -> LJPWCoordinates: J=J_score, P=P_score, W=W_score, - language='javascript', + language="javascript", file_path=filepath, - metadata=metadata + metadata=metadata, ) @@ -382,21 +386,21 @@ class GoAnalyzer: def analyze(code: str, filepath: str = "unknown") -> LJPWCoordinates: """Analyze Go code (regex-based, approximate)""" - lines = code.split('\n') + lines = code.split("\n") metadata = { - 'lines': len(lines), - 'functions': 0, - 'structs': 0, - 'error_handling': 0, + "lines": len(lines), + "functions": 0, + "structs": 0, + "error_handling": 0, } # === L (Love/Safety) === # Go: explicit error handling via "if err != nil" - error_checks = len(re.findall(r'if\s+err\s*!=\s*nil', code)) - panics = code.count('panic(') - recovers = code.count('recover(') + error_checks = len(re.findall(r"if\s+err\s*!=\s*nil", code)) + panics = code.count("panic(") + recovers = code.count("recover(") - metadata['error_handling'] = error_checks + metadata["error_handling"] = error_checks L_base = 0.4 # Go is inherently safe (type-safe, memory-safe) L_errors = min(0.4, error_checks * 0.08) @@ -406,13 +410,13 @@ def analyze(code: str, filepath: str = "unknown") -> LJPWCoordinates: L_score = L_base + L_errors + L_panic + L_recover # === J (Justice/Structure) === - structs = len(re.findall(r'\btype\s+\w+\s+struct', code)) - interfaces = len(re.findall(r'\btype\s+\w+\s+interface', code)) - functions = len(re.findall(r'\bfunc\s+(\w+|\(\w+\s+\*?\w+\))', code)) - packages = code.count('package ') + structs = len(re.findall(r"\btype\s+\w+\s+struct", code)) + interfaces = len(re.findall(r"\btype\s+\w+\s+interface", code)) + functions = len(re.findall(r"\bfunc\s+(\w+|\(\w+\s+\*?\w+\))", code)) + packages = code.count("package ") - metadata['structs'] = structs - metadata['functions'] = functions + metadata["structs"] = structs + metadata["functions"] = functions J_base = 0.3 # Go has strong structural conventions J_structs = min(0.3, structs * 0.1) @@ -422,10 +426,10 @@ def analyze(code: str, filepath: str = "unknown") -> LJPWCoordinates: J_score = J_base + J_structs + J_interfaces + J_functions # === P (Power/Performance) === - goroutines = code.count('go ') - channels = code.count('chan ') - loops = code.count('for ') - defers = code.count('defer ') + goroutines = code.count("go ") + channels = code.count("chan ") + loops = code.count("for ") + defers = code.count("defer ") P_base = 0.5 # Go is performant by design P_concurrent = min(0.3, (goroutines + channels) * 0.1) @@ -435,7 +439,7 @@ def analyze(code: str, filepath: str = "unknown") -> LJPWCoordinates: P_score = P_base + P_concurrent + P_loops + P_defer # === W (Wisdom/Design) === - comments = code.count('//') + comments = code.count("//") avg_line_length = sum(len(line) for line in lines) / max(len(lines), 1) gofmt_compliant = avg_line_length < 100 # Rough heuristic @@ -457,14 +461,15 @@ def analyze(code: str, filepath: str = "unknown") -> LJPWCoordinates: J=J_score, P=P_score, W=W_score, - language='go', + language="go", file_path=filepath, - metadata=metadata + metadata=metadata, ) # === Main API === + def analyze_code(filepath: str, language: str = None) -> LJPWCoordinates: """ Analyze code file and return LJPW coordinates @@ -481,25 +486,25 @@ def analyze_code(filepath: str, language: str = None) -> LJPWCoordinates: if language is None: ext = Path(filepath).suffix.lower() language_map = { - '.py': 'python', - '.js': 'javascript', - '.ts': 'typescript', - '.go': 'go', - '.rs': 'rust', - '.java': 'java', + ".py": "python", + ".js": "javascript", + ".ts": "typescript", + ".go": "go", + ".rs": "rust", + ".java": "java", } - language = language_map.get(ext, 'unknown') + language = language_map.get(ext, "unknown") # Read file - with open(filepath, 'r', encoding='utf-8') as f: + with open(filepath, "r", encoding="utf-8") as f: code = f.read() # Dispatch to appropriate analyzer analyzers = { - 'python': PythonAnalyzer, - 'javascript': JavaScriptAnalyzer, - 'typescript': JavaScriptAnalyzer, # Treat TS as JS for now - 'go': GoAnalyzer, + "python": PythonAnalyzer, + "javascript": JavaScriptAnalyzer, + "typescript": JavaScriptAnalyzer, # Treat TS as JS for now + "go": GoAnalyzer, } if language not in analyzers: @@ -531,14 +536,14 @@ def compare_implementations(files: Dict[str, str]) -> Dict[str, Any]: coordinates[lang] = ljpw results[lang] = ljpw.to_dict() except Exception as e: - results[lang] = {'error': str(e)} + results[lang] = {"error": str(e)} # Compute pairwise distances languages = list(coordinates.keys()) distances = {} for i, lang1 in enumerate(languages): - for lang2 in languages[i+1:]: + for lang2 in languages[i + 1 :]: coord1 = coordinates[lang1] coord2 = coordinates[lang2] distance = coord1.euclidean_distance(coord2) @@ -551,30 +556,30 @@ def compare_implementations(files: Dict[str, str]) -> Dict[str, Any]: avg_P = sum(c.P for c in coordinates.values()) / len(coordinates) avg_W = sum(c.W for c in coordinates.values()) / len(coordinates) - var_L = sum((c.L - avg_L)**2 for c in coordinates.values()) / len(coordinates) - var_J = sum((c.J - avg_J)**2 for c in coordinates.values()) / len(coordinates) - var_P = sum((c.P - avg_P)**2 for c in coordinates.values()) / len(coordinates) - var_W = sum((c.W - avg_W)**2 for c in coordinates.values()) / len(coordinates) + var_L = sum((c.L - avg_L) ** 2 for c in coordinates.values()) / len(coordinates) + var_J = sum((c.J - avg_J) ** 2 for c in coordinates.values()) / len(coordinates) + var_P = sum((c.P - avg_P) ** 2 for c in coordinates.values()) / len(coordinates) + var_W = sum((c.W - avg_W) ** 2 for c in coordinates.values()) / len(coordinates) statistics = { - 'mean': {'L': avg_L, 'J': avg_J, 'P': avg_P, 'W': avg_W}, - 'variance': {'L': var_L, 'J': var_J, 'P': var_P, 'W': var_W}, - 'std_dev': { - 'L': math.sqrt(var_L), - 'J': math.sqrt(var_J), - 'P': math.sqrt(var_P), - 'W': math.sqrt(var_W), + "mean": {"L": avg_L, "J": avg_J, "P": avg_P, "W": avg_W}, + "variance": {"L": var_L, "J": var_J, "P": var_P, "W": var_W}, + "std_dev": { + "L": math.sqrt(var_L), + "J": math.sqrt(var_J), + "P": math.sqrt(var_P), + "W": math.sqrt(var_W), }, - 'cross_language_variance': sum([var_L, var_J, var_P, var_W]) / 4, + "cross_language_variance": sum([var_L, var_J, var_P, var_W]) / 4, } else: statistics = {} return { - 'implementations': results, - 'pairwise_distances': distances, - 'statistics': statistics, - 'cross_language_invariance_score': 1.0 - statistics.get('cross_language_variance', 1.0) + "implementations": results, + "pairwise_distances": distances, + "statistics": statistics, + "cross_language_invariance_score": 1.0 - statistics.get("cross_language_variance", 1.0), } @@ -595,13 +600,13 @@ def batch_analyze(directory: str, languages: List[str] = None) -> List[LJPWCoord # Extension map ext_map = { - '.py': 'python', - '.js': 'javascript', - '.ts': 'typescript', - '.go': 'go', + ".py": "python", + ".js": "javascript", + ".ts": "typescript", + ".go": "go", } - for filepath in dir_path.rglob('*'): + for filepath in dir_path.rglob("*"): if filepath.is_file(): ext = filepath.suffix.lower() if ext in ext_map: @@ -618,15 +623,16 @@ def batch_analyze(directory: str, languages: List[str] = None) -> List[LJPWCoord # === CLI === + def main(): """CLI interface""" import argparse - parser = argparse.ArgumentParser(description='Multi-language LJPW analyzer') - parser.add_argument('file', help='File to analyze') - parser.add_argument('-l', '--language', help='Language (auto-detect if not specified)') - parser.add_argument('-o', '--output', help='Output JSON file') - parser.add_argument('--compare', nargs='+', help='Compare multiple files') + parser = argparse.ArgumentParser(description="Multi-language LJPW analyzer") + parser.add_argument("file", help="File to analyze") + parser.add_argument("-l", "--language", help="Language (auto-detect if not specified)") + parser.add_argument("-o", "--output", help="Output JSON file") + parser.add_argument("--compare", nargs="+", help="Compare multiple files") args = parser.parse_args() @@ -634,14 +640,14 @@ def main(): # Compare mode files = {} for f in args.compare: - lang = Path(f).suffix.lstrip('.') + lang = Path(f).suffix.lstrip(".") files[lang] = f results = compare_implementations(files) print(json.dumps(results, indent=2)) if args.output: - with open(args.output, 'w') as f: + with open(args.output, "w") as f: json.dump(results, f, indent=2) else: # Single file mode @@ -660,9 +666,9 @@ def main(): print(f" Health Score: {ljpw.health_score():.1f}%") if args.output: - with open(args.output, 'w') as f: + with open(args.output, "w") as f: json.dump(ljpw.to_dict(), f, indent=2) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/tools/particle_physics_mapper.py b/tools/particle_physics_mapper.py index da45b9d..663bcaa 100644 --- a/tools/particle_physics_mapper.py +++ b/tools/particle_physics_mapper.py @@ -11,14 +11,14 @@ python particle_physics_mapper.py """ +import json import math import sys from pathlib import Path from typing import Dict, List, Tuple -import json # Add project root to path -sys.path.insert(0, str(Path(__file__).parent.parent / 'src' / 'ljpw')) +sys.path.insert(0, str(Path(__file__).parent.parent / "src" / "ljpw")) # Mathematical constants E = math.e @@ -36,45 +36,45 @@ QUARKS = { "Up (u)": { "mass_MeV": 2.2, # MeV/c² - "charge": 2/3, + "charge": 2 / 3, "generation": 1, "coords": (0.650, 0.620, 0.680, 0.660), - "description": "Lightest quark, 1st generation" + "description": "Lightest quark, 1st generation", }, "Down (d)": { "mass_MeV": 4.7, - "charge": -1/3, + "charge": -1 / 3, "generation": 1, "coords": (0.640, 0.610, 0.670, 0.650), - "description": "2nd lightest quark, 1st generation" + "description": "2nd lightest quark, 1st generation", }, "Charm (c)": { "mass_MeV": 1275, - "charge": 2/3, + "charge": 2 / 3, "generation": 2, "coords": (0.780, 0.750, 0.810, 0.790), - "description": "Medium mass, 2nd generation" + "description": "Medium mass, 2nd generation", }, "Strange (s)": { "mass_MeV": 95, - "charge": -1/3, + "charge": -1 / 3, "generation": 2, "coords": (0.720, 0.690, 0.750, 0.730), - "description": "Medium-light mass, 2nd generation" + "description": "Medium-light mass, 2nd generation", }, "Top (t)": { "mass_MeV": 173000, # 173 GeV - "charge": 2/3, + "charge": 2 / 3, "generation": 3, "coords": (0.920, 0.900, 0.950, 0.930), - "description": "Heaviest quark, 3rd generation, most 'perfect'" + "description": "Heaviest quark, 3rd generation, most 'perfect'", }, "Bottom (b)": { "mass_MeV": 4180, # 4.18 GeV - "charge": -1/3, + "charge": -1 / 3, "generation": 3, "coords": (0.850, 0.820, 0.880, 0.860), - "description": "Heavy quark, 3rd generation" + "description": "Heavy quark, 3rd generation", }, } @@ -84,42 +84,42 @@ "charge": -1, "generation": 1, "coords": (0.730, 0.710, 0.760, 0.740), - "description": "Stable lepton, 1st generation" + "description": "Stable lepton, 1st generation", }, "Electron Neutrino (νe)": { "mass_MeV": 0.000001, # < 1 eV "charge": 0, "generation": 1, "coords": (0.500, 0.480, 0.520, 0.510), - "description": "Nearly massless, 1st generation" + "description": "Nearly massless, 1st generation", }, "Muon (μ)": { "mass_MeV": 105.7, "charge": -1, "generation": 2, "coords": (0.800, 0.780, 0.830, 0.810), - "description": "Unstable, 2nd generation" + "description": "Unstable, 2nd generation", }, "Muon Neutrino (νμ)": { "mass_MeV": 0.0002, # < 0.2 eV "charge": 0, "generation": 2, "coords": (0.520, 0.500, 0.540, 0.530), - "description": "Nearly massless, 2nd generation" + "description": "Nearly massless, 2nd generation", }, "Tau (τ)": { "mass_MeV": 1776.9, "charge": -1, "generation": 3, "coords": (0.860, 0.840, 0.890, 0.870), - "description": "Heavy lepton, 3rd generation" + "description": "Heavy lepton, 3rd generation", }, "Tau Neutrino (ντ)": { "mass_MeV": 0.002, # < 2 eV "charge": 0, "generation": 3, "coords": (0.540, 0.520, 0.560, 0.550), - "description": "Nearly massless, 3rd generation" + "description": "Nearly massless, 3rd generation", }, } @@ -129,49 +129,50 @@ "charge": 0, "force": "electromagnetic", "coords": (0.950, 0.920, 0.980, 0.940), - "description": "Massless EM force carrier, infinite range" + "description": "Massless EM force carrier, infinite range", }, "W± Boson": { "mass_MeV": 80379, # 80.4 GeV "charge": "±1", "force": "weak", "coords": (0.880, 0.850, 0.910, 0.890), - "description": "Massive weak force carrier, charged" + "description": "Massive weak force carrier, charged", }, "Z⁰ Boson": { "mass_MeV": 91188, # 91.2 GeV "charge": 0, "force": "weak", "coords": (0.890, 0.860, 0.920, 0.900), - "description": "Massive weak force carrier, neutral" + "description": "Massive weak force carrier, neutral", }, "Gluon (g)": { "mass_MeV": 0, "charge": 0, "force": "strong", "coords": (0.970, 0.940, 0.990, 0.960), - "description": "Massless strong force carrier, color charge" + "description": "Massless strong force carrier, color charge", }, "Higgs (H)": { "mass_MeV": 125100, # 125.1 GeV "charge": 0, "force": "mass generation", "coords": (0.900, 0.870, 0.930, 0.910), - "description": "Mass-giving field, completes Standard Model" + "description": "Mass-giving field, completes Standard Model", }, } # === Analysis Functions === + def distance_from_anchor(coords): """Calculate distance from Anchor Point (Divine Perfection)""" - return math.sqrt(sum((c - 1.0)**2 for c in coords)) + return math.sqrt(sum((c - 1.0) ** 2 for c in coords)) def distance_from_ne(coords): """Calculate distance from Natural Equilibrium""" - return math.sqrt(sum((c - ne)**2 for c, ne in zip(coords, NATURAL_EQUILIBRIUM))) + return math.sqrt(sum((c - ne) ** 2 for c, ne in zip(coords, NATURAL_EQUILIBRIUM))) def divine_perfection(coords): @@ -191,15 +192,15 @@ def validate_mass_perfection_correlation(): Test Prediction: Higher mass → Higher divine perfection (Heavier particles are closer to fundamental/perfect realm) """ - print("="*80) + print("=" * 80) print("PREDICTION 1: Mass-Divine Perfection Correlation (Quarks)") - print("="*80) + print("=" * 80) # Collect quark data quark_data = [] for name, data in QUARKS.items(): - mass = data['mass_MeV'] - coords = data['coords'] + mass = data["mass_MeV"] + coords = data["coords"] divine = divine_perfection(coords) quark_data.append((name, mass, divine)) @@ -207,7 +208,7 @@ def validate_mass_perfection_correlation(): quark_data.sort(key=lambda x: x[1]) print(f"\n{'Quark':<20} {'Mass (MeV)':<15} {'Divine %':<15}") - print("-"*80) + print("-" * 80) for name, mass, divine in quark_data: print(f"{name:<20} {mass:<15.1f} {divine:<15.2f}") @@ -221,8 +222,8 @@ def validate_mass_perfection_correlation(): mean_divine = sum(divines) / n cov = sum((m - mean_mass) * (d - mean_divine) for m, d in zip(masses, divines)) / n - std_mass = math.sqrt(sum((m - mean_mass)**2 for m in masses) / n) - std_divine = math.sqrt(sum((d - mean_divine)**2 for d in divines) / n) + std_mass = math.sqrt(sum((m - mean_mass) ** 2 for m in masses) / n) + std_divine = math.sqrt(sum((d - mean_divine) ** 2 for d in divines) / n) correlation = cov / (std_mass * std_divine) if std_mass * std_divine > 0 else 0 @@ -236,8 +237,12 @@ def validate_mass_perfection_correlation(): print(" ✗ WEAK/NO CORRELATION - Prediction not supported") print(f"\nInterpretation:") - print(f" Heavier quarks (Top: 173 GeV) have higher divine perfection ({divine_perfection(QUARKS['Top (t)']['coords']):.1f}%)") - print(f" Lighter quarks (Up: 2.2 MeV) have lower divine perfection ({divine_perfection(QUARKS['Up (u)']['coords']):.1f}%)") + print( + f" Heavier quarks (Top: 173 GeV) have higher divine perfection ({divine_perfection(QUARKS['Top (t)']['coords']):.1f}%)" + ) + print( + f" Lighter quarks (Up: 2.2 MeV) have lower divine perfection ({divine_perfection(QUARKS['Up (u)']['coords']):.1f}%)" + ) print(f" This suggests mass is a measure of 'fundamentalness' in semantic space") return correlation @@ -248,16 +253,16 @@ def validate_generation_hierarchy(): Test Prediction: 3rd generation > 2nd generation > 1st generation (Later generations are more 'perfect' but less stable) """ - print("\n" + "="*80) + print("\n" + "=" * 80) print("PREDICTION 2: Generation Hierarchy (Divine Perfection)") - print("="*80) + print("=" * 80) # Group by generation generations = {1: [], 2: [], 3: []} for name, data in QUARKS.items(): - gen = data['generation'] - divine = divine_perfection(data['coords']) + gen = data["generation"] + divine = divine_perfection(data["coords"]) generations[gen].append((name, divine)) # Calculate averages @@ -267,18 +272,20 @@ def validate_generation_hierarchy(): gen_averages[gen] = avg print(f"\n{'Generation':<15} {'Quarks':<30} {'Avg Divine %':<15}") - print("-"*80) + print("-" * 80) for gen in sorted(gen_averages.keys()): particles = generations[gen] - particle_names = ', '.join([name.split()[0] for name, _ in particles]) + particle_names = ", ".join([name.split()[0] for name, _ in particles]) avg = gen_averages[gen] print(f"{gen:<15} {particle_names:<30} {avg:<15.2f}") # Test monotonic increase if gen_averages[3] > gen_averages[2] > gen_averages[1]: print(f"\n ✓ PERFECT MONOTONIC HIERARCHY - Prediction confirmed!") - print(f" 3rd gen ({gen_averages[3]:.1f}%) > 2nd gen ({gen_averages[2]:.1f}%) > 1st gen ({gen_averages[1]:.1f}%)") + print( + f" 3rd gen ({gen_averages[3]:.1f}%) > 2nd gen ({gen_averages[2]:.1f}%) > 1st gen ({gen_averages[1]:.1f}%)" + ) result = True else: print(f"\n ✗ NOT MONOTONIC - Prediction not supported") @@ -299,15 +306,15 @@ def validate_massless_perfection(): Test Prediction: Massless particles (photon, gluon) have highest divine perfection (Perfect symmetry → massless → highest divine%) """ - print("\n" + "="*80) + print("\n" + "=" * 80) print("PREDICTION 3: Massless Particles Have Highest Divine Perfection") - print("="*80) + print("=" * 80) # Collect boson data boson_data = [] for name, data in BOSONS.items(): - mass = data['mass_MeV'] - coords = data['coords'] + mass = data["mass_MeV"] + coords = data["coords"] divine = divine_perfection(coords) boson_data.append((name, mass, divine, mass == 0)) @@ -315,7 +322,7 @@ def validate_massless_perfection(): boson_data.sort(key=lambda x: x[2], reverse=True) print(f"\n{'Boson':<20} {'Mass (MeV)':<15} {'Divine %':<15} {'Massless':<10}") - print("-"*80) + print("-" * 80) for name, mass, divine, massless in boson_data: massless_str = "✓" if massless else "" print(f"{name:<20} {mass:<15.1f} {divine:<15.2f} {massless_str:<10}") @@ -345,34 +352,44 @@ def validate_higgs_position(): Test Prediction: Higgs boson occupies special semantic position (Gives mass to all particles → bridges massless perfection and massive reality) """ - print("\n" + "="*80) + print("\n" + "=" * 80) print("PREDICTION 4: Higgs Boson as Bridge Between Realms") - print("="*80) + print("=" * 80) - higgs_coords = BOSONS['Higgs (H)']['coords'] + higgs_coords = BOSONS["Higgs (H)"]["coords"] higgs_divine = divine_perfection(higgs_coords) higgs_physical = physical_optimization(higgs_coords) # Compare to massless bosons (high divine, low physical) - photon_divine = divine_perfection(BOSONS['Photon (γ)']['coords']) - photon_physical = physical_optimization(BOSONS['Photon (γ)']['coords']) + photon_divine = divine_perfection(BOSONS["Photon (γ)"]["coords"]) + photon_physical = physical_optimization(BOSONS["Photon (γ)"]["coords"]) # Compare to quarks (moderate divine, moderate physical) - avg_quark_divine = sum(divine_perfection(q['coords']) for q in QUARKS.values()) / len(QUARKS) - avg_quark_physical = sum(physical_optimization(q['coords']) for q in QUARKS.values()) / len(QUARKS) + avg_quark_divine = sum(divine_perfection(q["coords"]) for q in QUARKS.values()) / len(QUARKS) + avg_quark_physical = sum(physical_optimization(q["coords"]) for q in QUARKS.values()) / len( + QUARKS + ) print(f"\n{'Particle':<20} {'Divine %':<15} {'Physical %':<15} {'Balance':<15}") - print("-"*80) - print(f"{'Photon (massless)':<20} {photon_divine:<15.2f} {photon_physical:<15.2f} {abs(photon_divine - photon_physical):<15.2f}") - print(f"{'Higgs (bridge)':<20} {higgs_divine:<15.2f} {higgs_physical:<15.2f} {abs(higgs_divine - higgs_physical):<15.2f}") - print(f"{'Avg Quark (matter)':<20} {avg_quark_divine:<15.2f} {avg_quark_physical:<15.2f} {abs(avg_quark_divine - avg_quark_physical):<15.2f}") + print("-" * 80) + print( + f"{'Photon (massless)':<20} {photon_divine:<15.2f} {photon_physical:<15.2f} {abs(photon_divine - photon_physical):<15.2f}" + ) + print( + f"{'Higgs (bridge)':<20} {higgs_divine:<15.2f} {higgs_physical:<15.2f} {abs(higgs_divine - higgs_physical):<15.2f}" + ) + print( + f"{'Avg Quark (matter)':<20} {avg_quark_divine:<15.2f} {avg_quark_physical:<15.2f} {abs(avg_quark_divine - avg_quark_physical):<15.2f}" + ) # Test if Higgs is between photon and quarks - higgs_between = (photon_divine > higgs_divine > avg_quark_divine) + higgs_between = photon_divine > higgs_divine > avg_quark_divine if higgs_between: print(f"\n ✓ HIGGS IS BRIDGE - Prediction confirmed!") - print(f" Divine%: Photon (94.3%) > Higgs ({higgs_divine:.1f}%) > Quarks ({avg_quark_divine:.1f}%)") + print( + f" Divine%: Photon (94.3%) > Higgs ({higgs_divine:.1f}%) > Quarks ({avg_quark_divine:.1f}%)" + ) result = True else: print(f"\n ⚠ HIGGS POSITION UNCLEAR - Needs refinement") @@ -389,97 +406,118 @@ def validate_higgs_position(): def create_particle_hierarchy_map(): """Create visual hierarchy of all particles in LJPW space""" - print("\n" + "="*80) + print("\n" + "=" * 80) print("COMPLETE PARTICLE PHYSICS HIERARCHY IN LJPW SPACE") - print("="*80) + print("=" * 80) all_particles = [] # Collect all particles for name, data in BOSONS.items(): - all_particles.append({ - 'name': name, - 'type': 'Boson', - 'coords': data['coords'], - 'mass': data['mass_MeV'], - 'divine': divine_perfection(data['coords']), - 'physical': physical_optimization(data['coords']) - }) + all_particles.append( + { + "name": name, + "type": "Boson", + "coords": data["coords"], + "mass": data["mass_MeV"], + "divine": divine_perfection(data["coords"]), + "physical": physical_optimization(data["coords"]), + } + ) for name, data in QUARKS.items(): - all_particles.append({ - 'name': name, - 'type': 'Quark', - 'coords': data['coords'], - 'mass': data['mass_MeV'], - 'divine': divine_perfection(data['coords']), - 'physical': physical_optimization(data['coords']) - }) + all_particles.append( + { + "name": name, + "type": "Quark", + "coords": data["coords"], + "mass": data["mass_MeV"], + "divine": divine_perfection(data["coords"]), + "physical": physical_optimization(data["coords"]), + } + ) for name, data in LEPTONS.items(): - all_particles.append({ - 'name': name, - 'type': 'Lepton', - 'coords': data['coords'], - 'mass': data['mass_MeV'], - 'divine': divine_perfection(data['coords']), - 'physical': physical_optimization(data['coords']) - }) + all_particles.append( + { + "name": name, + "type": "Lepton", + "coords": data["coords"], + "mass": data["mass_MeV"], + "divine": divine_perfection(data["coords"]), + "physical": physical_optimization(data["coords"]), + } + ) # Sort by divine perfection - all_particles.sort(key=lambda x: x['divine'], reverse=True) + all_particles.sort(key=lambda x: x["divine"], reverse=True) - print(f"\n{'Rank':<6} {'Particle':<25} {'Type':<10} {'Divine %':<12} {'Physical %':<12} {'Mass (MeV)':<15}") - print("-"*100) + print( + f"\n{'Rank':<6} {'Particle':<25} {'Type':<10} {'Divine %':<12} {'Physical %':<12} {'Mass (MeV)':<15}" + ) + print("-" * 100) for rank, p in enumerate(all_particles, 1): - print(f"{rank:<6} {p['name']:<25} {p['type']:<10} {p['divine']:<12.2f} {p['physical']:<12.2f} {p['mass']:<15.1f}") + print( + f"{rank:<6} {p['name']:<25} {p['type']:<10} {p['divine']:<12.2f} {p['physical']:<12.2f} {p['mass']:<15.1f}" + ) return all_particles # === Main Execution === + def main(): """Run all particle physics validations""" - print("\n" + "="*80) + print("\n" + "=" * 80) print("PARTICLE PHYSICS CONSTANTS IN LJPW SEMANTIC SPACE".center(80)) - print("="*80) + print("=" * 80) print("\nExtending cosmic architecture validation to Standard Model particles") print("Quarks: 6 flavors (up, down, charm, strange, top, bottom)") print("Leptons: 6 flavors (electron, muon, tau + neutrinos)") print("Bosons: 5 force carriers (photon, W, Z, gluon, Higgs)") - print("="*80) + print("=" * 80) # Run validations results = {} - results['mass_correlation'] = validate_mass_perfection_correlation() - results['generation_hierarchy'] = validate_generation_hierarchy() - results['massless_perfection'] = validate_massless_perfection() - results['higgs_bridge'] = validate_higgs_position() + results["mass_correlation"] = validate_mass_perfection_correlation() + results["generation_hierarchy"] = validate_generation_hierarchy() + results["massless_perfection"] = validate_massless_perfection() + results["higgs_bridge"] = validate_higgs_position() # Create complete hierarchy particle_hierarchy = create_particle_hierarchy_map() # Summary - print("\n" + "="*80) + print("\n" + "=" * 80) print("VALIDATION SUMMARY".center(80)) - print("="*80) + print("=" * 80) print("\nPredictions Tested:") - print(f" 1. Mass-Divine Correlation: r = {results['mass_correlation']:.3f} {'✓' if results['mass_correlation'] > 0.7 else '✗'}") - print(f" 2. Generation Hierarchy: {'✓ CONFIRMED' if results['generation_hierarchy'] else '✗ NOT SUPPORTED'}") - print(f" 3. Massless Highest Perfection: {'✓ CONFIRMED' if results['massless_perfection'] else '✗ NOT SUPPORTED'}") - print(f" 4. Higgs as Bridge: {'✓ CONFIRMED' if results['higgs_bridge'] else '⚠ NEEDS REFINEMENT'}") - - confirmed = sum([ - results['mass_correlation'] > 0.7, - results['generation_hierarchy'], - results['massless_perfection'], - results['higgs_bridge'] - ]) + print( + f" 1. Mass-Divine Correlation: r = {results['mass_correlation']:.3f} {'✓' if results['mass_correlation'] > 0.7 else '✗'}" + ) + print( + f" 2. Generation Hierarchy: {'✓ CONFIRMED' if results['generation_hierarchy'] else '✗ NOT SUPPORTED'}" + ) + print( + f" 3. Massless Highest Perfection: {'✓ CONFIRMED' if results['massless_perfection'] else '✗ NOT SUPPORTED'}" + ) + print( + f" 4. Higgs as Bridge: {'✓ CONFIRMED' if results['higgs_bridge'] else '⚠ NEEDS REFINEMENT'}" + ) + + confirmed = sum( + [ + results["mass_correlation"] > 0.7, + results["generation_hierarchy"], + results["massless_perfection"], + results["higgs_bridge"], + ] + ) print(f"\nOverall: {confirmed}/4 predictions confirmed ({confirmed/4*100:.0f}%)") @@ -490,11 +528,12 @@ def main(): else: print("\n✗ WEAK VALIDATION - LJPW may not apply to particle physics") - print("\n" + "="*80) + print("\n" + "=" * 80) print("KEY INSIGHTS".center(80)) - print("="*80) + print("=" * 80) - print(""" + print( + """ 1. **Scale-Perfection Extends to Particle Physics** - Gluon (strong force, 97.0% divine) > Photon (EM, 94.3%) > Quarks (65-92%) - Fundamental particles are closer to divine perfection @@ -524,36 +563,37 @@ def main(): - Just like we predicted Ψ and Θ... - Could we predict undiscovered particles? - Gap analysis could extend to particle physics! -""") +""" + ) - print("="*80) + print("=" * 80) print("\nValidation complete! Particle physics confirms LJPW architecture.\n") # Save results - output_file = Path(__file__).parent.parent / 'results' / 'particle_physics_validation.json' + output_file = Path(__file__).parent.parent / "results" / "particle_physics_validation.json" output_file.parent.mkdir(exist_ok=True) output_data = { - 'validation_results': results, - 'particle_hierarchy': [ + "validation_results": results, + "particle_hierarchy": [ { - 'rank': i+1, - 'name': p['name'], - 'type': p['type'], - 'divine_perfection': p['divine'], - 'physical_optimization': p['physical'], - 'mass_MeV': p['mass'], - 'ljpw_coords': p['coords'] + "rank": i + 1, + "name": p["name"], + "type": p["type"], + "divine_perfection": p["divine"], + "physical_optimization": p["physical"], + "mass_MeV": p["mass"], + "ljpw_coords": p["coords"], } for i, p in enumerate(particle_hierarchy) - ] + ], } - with open(output_file, 'w') as f: + with open(output_file, "w") as f: json.dump(output_data, f, indent=2) print(f"Results saved to: {output_file}") -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/tools/psi_theta_validator.py b/tools/psi_theta_validator.py index dd505df..2b3bf78 100644 --- a/tools/psi_theta_validator.py +++ b/tools/psi_theta_validator.py @@ -14,23 +14,23 @@ - Θ (Theta) = Ψ×ln(2)×√(π/2) ≈ 2.545 - Information-Energy Bridge """ +import math import sys from pathlib import Path -import math -sys.path.insert(0, str(Path(__file__).parent.parent / 'src' / 'ljpw')) -from ljpw_standalone import calculate_distance +sys.path.insert(0, str(Path(__file__).parent.parent)) +from src.ljpw.ljpw_standalone import calculate_distance # Mathematical Constants -E = math.e # 2.718281828... -PI = math.pi # 3.141592654... +E = math.e # 2.718281828... +PI = math.pi # 3.141592654... PHI = (1 + math.sqrt(5)) / 2 # 1.618033989... (golden ratio) -SQRT2 = math.sqrt(2) # 1.414213562... -LN2 = math.log(2) # 0.693147181... +SQRT2 = math.sqrt(2) # 1.414213562... +LN2 = math.log(2) # 0.693147181... # Predicted New Constants -PSI = (E + PI) / 2 # 2.929937241 - Universal Harmony Constant -THETA = PSI * LN2 * math.sqrt(PI/2) # 2.545327778 - Information-Energy Bridge +PSI = (E + PI) / 2 # 2.929937241 - Universal Harmony Constant +THETA = PSI * LN2 * math.sqrt(PI / 2) # 2.545327778 - Information-Energy Bridge # Reference Points ANCHOR_POINT = (1.000, 1.000, 1.000, 1.000) @@ -85,11 +85,11 @@ def validate_psi_mathematical_properties(): # Test 4: Relationship to known constants print("Test 4: Relationships to Known Constants") relationships = { - "Ψ/e": PSI/E, - "Ψ/π": PSI/PI, - "Ψ/φ": PSI/PHI, - "Ψ/√2": PSI/SQRT2, - "Ψ×ln(2)": PSI*LN2, + "Ψ/e": PSI / E, + "Ψ/π": PSI / PI, + "Ψ/φ": PSI / PHI, + "Ψ/√2": PSI / SQRT2, + "Ψ×ln(2)": PSI * LN2, } for name, value in relationships.items(): @@ -97,10 +97,10 @@ def validate_psi_mathematical_properties(): print() return { - 'value': PSI, - 'in_range': in_range, - 'dist_anchor': dist_to_anchor, - 'dist_ne': dist_to_ne + "value": PSI, + "in_range": in_range, + "dist_anchor": dist_to_anchor, + "dist_ne": dist_to_ne, } @@ -158,10 +158,10 @@ def validate_theta_mathematical_properties(): print() return { - 'value': THETA, - 'in_range': in_initial_range, - 'dist_anchor': dist_to_anchor, - 'dist_ne': dist_to_ne + "value": THETA, + "in_range": in_initial_range, + "dist_anchor": dist_to_anchor, + "dist_ne": dist_to_ne, } @@ -177,7 +177,7 @@ def test_quantum_classical_interface(): # Simulated quantum coherence metrics baseline_coherence = 0.75 # 75% coherence - psi_coherence = baseline_coherence * (1 + (PSI - E)/E) # Ψ-enhanced + psi_coherence = baseline_coherence * (1 + (PSI - E) / E) # Ψ-enhanced improvement = (psi_coherence - baseline_coherence) / baseline_coherence @@ -261,7 +261,7 @@ def test_information_processing(): # Shannon capacity simulation signal_to_noise = 10.0 # 10 dB baseline_capacity = math.log2(1 + signal_to_noise) - psi_capacity = math.log2(1 + signal_to_noise * PSI/E) + psi_capacity = math.log2(1 + signal_to_noise * PSI / E) improvement = (psi_capacity - baseline_capacity) / baseline_capacity diff --git a/tools/semantic_code_generator.py b/tools/semantic_code_generator.py index a7d69da..0cccce2 100644 --- a/tools/semantic_code_generator.py +++ b/tools/semantic_code_generator.py @@ -15,18 +15,24 @@ import sys from pathlib import Path -from typing import Tuple, Dict +from typing import Dict, Tuple -sys.path.insert(0, str(Path(__file__).parent.parent / 'src' / 'ljpw')) -from ljpw_standalone import analyze_quick +sys.path.insert(0, str(Path(__file__).parent.parent)) +from src.ljpw.ljpw_standalone import analyze_quick # ============================================================================ # CODE GENERATION ENGINE # ============================================================================ -def generate_code(L: float, J: float, P: float, W: float, - domain: str = "data processing", - function_name: str = "process") -> str: + +def generate_code( + L: float, + J: float, + P: float, + W: float, + domain: str = "data processing", + function_name: str = "process", +) -> str: """ Generate code at specified LJPW coordinates. @@ -50,17 +56,17 @@ def generate_code(L: float, J: float, P: float, W: float, # High structure: Full docstring + type annotations code_parts.append(f"def {function_name}(data: List[Any]) -> List[Any]:") code_parts.append(' """') - code_parts.append(f' {domain.capitalize()} with comprehensive validation.') - code_parts.append(' ') - code_parts.append(' Args:') - code_parts.append(' data: Input data to process') - code_parts.append(' ') - code_parts.append(' Returns:') - code_parts.append(' Processed results') - code_parts.append(' ') - code_parts.append(' Raises:') - code_parts.append(' ValueError: If data is invalid') - code_parts.append(' TypeError: If data type is incorrect') + code_parts.append(f" {domain.capitalize()} with comprehensive validation.") + code_parts.append(" ") + code_parts.append(" Args:") + code_parts.append(" data: Input data to process") + code_parts.append(" ") + code_parts.append(" Returns:") + code_parts.append(" Processed results") + code_parts.append(" ") + code_parts.append(" Raises:") + code_parts.append(" ValueError: If data is invalid") + code_parts.append(" TypeError: If data type is incorrect") code_parts.append(' """') elif J >= 0.4: # Medium structure: Simple docstring + some types @@ -76,32 +82,32 @@ def generate_code(L: float, J: float, P: float, W: float, if L >= 0.8: # High safety: Comprehensive validation + try/except + logging - code_parts.append(' # Input validation') - code_parts.append(' if data is None:') + code_parts.append(" # Input validation") + code_parts.append(" if data is None:") code_parts.append(' raise ValueError("Data cannot be None")') - code_parts.append(' if not data:') + code_parts.append(" if not data:") code_parts.append(' raise ValueError("Data cannot be empty")') - code_parts.append(' if not isinstance(data, list):') + code_parts.append(" if not isinstance(data, list):") code_parts.append(' raise TypeError("Data must be a list")') - code_parts.append(' ') - code_parts.append(' try:') - indent = ' ' + code_parts.append(" ") + code_parts.append(" try:") + indent = " " elif L >= 0.5: # Medium safety: Basic validation + try/except - code_parts.append(' if not data:') + code_parts.append(" if not data:") code_parts.append(' raise ValueError("Data required")') - code_parts.append(' ') - code_parts.append(' try:') - indent = ' ' + code_parts.append(" ") + code_parts.append(" try:") + indent = " " elif L >= 0.3: # Low safety: Minimal check - code_parts.append(' if not data:') - code_parts.append(' return []') - code_parts.append(' ') - indent = '' + code_parts.append(" if not data:") + code_parts.append(" return []") + code_parts.append(" ") + indent = "" else: # Minimal safety: No checks - indent = '' + indent = "" # ======================================================================== # P (Power/Performance) + W (Wisdom/Design): Implementation @@ -111,30 +117,30 @@ def generate_code(L: float, J: float, P: float, W: float, # High wisdom: Use abstraction and design patterns if P >= 0.7: # High wisdom + high performance: Strategy pattern + optimization - code_parts.append(f'{indent} # Use strategy pattern for flexible processing') - code_parts.append(f'{indent} strategy = ProcessingStrategy.select(data)') - code_parts.append(f'{indent} result = strategy.execute(data)') + code_parts.append(f"{indent} # Use strategy pattern for flexible processing") + code_parts.append(f"{indent} strategy = ProcessingStrategy.select(data)") + code_parts.append(f"{indent} result = strategy.execute(data)") else: # High wisdom + low performance: Clean abstraction - code_parts.append(f'{indent} # Delegate to processor for clean separation') - code_parts.append(f'{indent} processor = DataProcessor(data)') - code_parts.append(f'{indent} result = processor.process()') + code_parts.append(f"{indent} # Delegate to processor for clean separation") + code_parts.append(f"{indent} processor = DataProcessor(data)") + code_parts.append(f"{indent} result = processor.process()") elif P >= 0.7: # Low wisdom + high performance: Optimized but direct if P >= 0.8: # Very high performance: List comprehension + caching - code_parts.append(f'{indent} # Optimized processing with comprehension') - code_parts.append(f'{indent} result = [transform_cached(item) for item in data]') + code_parts.append(f"{indent} # Optimized processing with comprehension") + code_parts.append(f"{indent} result = [transform_cached(item) for item in data]") else: # High performance: List comprehension - code_parts.append(f'{indent} # Efficient list comprehension') - code_parts.append(f'{indent} result = [transform(item) for item in data]') + code_parts.append(f"{indent} # Efficient list comprehension") + code_parts.append(f"{indent} result = [transform(item) for item in data]") else: # Low wisdom + low performance: Simple loop - code_parts.append(f'{indent} # Basic processing') - code_parts.append(f'{indent} result = []') - code_parts.append(f'{indent} for item in data:') - code_parts.append(f'{indent} result.append(transform(item))') + code_parts.append(f"{indent} # Basic processing") + code_parts.append(f"{indent} result = []") + code_parts.append(f"{indent} for item in data:") + code_parts.append(f"{indent} result.append(transform(item))") # ======================================================================== # L (Love/Safety): Error Handling Completion @@ -142,32 +148,34 @@ def generate_code(L: float, J: float, P: float, W: float, if L >= 0.8: # High safety: Comprehensive error handling - code_parts.append(' return result') - code_parts.append(' except TypeError as e:') + code_parts.append(" return result") + code_parts.append(" except TypeError as e:") code_parts.append(' logger.error(f"Type error during processing: {e}")') - code_parts.append(' raise') - code_parts.append(' except ValueError as e:') + code_parts.append(" raise") + code_parts.append(" except ValueError as e:") code_parts.append(' logger.error(f"Value error during processing: {e}")') - code_parts.append(' raise') - code_parts.append(' except Exception as e:') + code_parts.append(" raise") + code_parts.append(" except Exception as e:") code_parts.append(' logger.error(f"Unexpected error: {e}")') - code_parts.append(' raise') + code_parts.append(" raise") elif L >= 0.5: # Medium safety: Basic error handling - code_parts.append(' return result') - code_parts.append(' except Exception as e:') + code_parts.append(" return result") + code_parts.append(" except Exception as e:") code_parts.append(' logger.error(f"Processing failed: {e}")') - code_parts.append(' raise') + code_parts.append(" raise") else: # Low safety: Direct return - code_parts.append(' return result') + code_parts.append(" return result") + + return "\n".join(code_parts) - return '\n'.join(code_parts) # ============================================================================ # VALIDATION # ============================================================================ + def validate_generation(target_genome: str, generated_code: str) -> Dict: """ Validate that generated code matches target genome. @@ -187,30 +195,37 @@ def validate_generation(target_genome: str, generated_code: str) -> Dict: # Analyze generated code result = analyze_quick(generated_code) - actual_pos = (result['ljpw']['L'], result['ljpw']['J'], - result['ljpw']['P'], result['ljpw']['W']) + actual_pos = ( + result["ljpw"]["L"], + result["ljpw"]["J"], + result["ljpw"]["P"], + result["ljpw"]["W"], + ) # Calculate distance import math - distance = math.sqrt(sum((a-b)**2 for a, b in zip(target_pos, actual_pos))) + + distance = math.sqrt(sum((a - b) ** 2 for a, b in zip(target_pos, actual_pos))) # Create actual genome actual_genome = f"L{int(actual_pos[0]*10)%10}J{int(actual_pos[1]*10)%10}P{int(actual_pos[2]*10)%10}W{int(actual_pos[3]*10)%10}" return { - 'target_genome': target_genome, - 'target_pos': target_pos, - 'actual_genome': actual_genome, - 'actual_pos': actual_pos, - 'distance': distance, - 'match': distance < 0.3, # Close enough - 'similarity': 'MATCH' if distance < 0.2 else 'CLOSE' if distance < 0.4 else 'DIFFERENT' + "target_genome": target_genome, + "target_pos": target_pos, + "actual_genome": actual_genome, + "actual_pos": actual_pos, + "distance": distance, + "match": distance < 0.3, # Close enough + "similarity": "MATCH" if distance < 0.2 else "CLOSE" if distance < 0.4 else "DIFFERENT", } + # ============================================================================ # TEST CASES # ============================================================================ + def test_generator(): print("\n" + "=" * 70) print("SEMANTIC CODE GENERATOR TEST") @@ -219,41 +234,41 @@ def test_generator(): test_cases = [ { - 'name': 'Minimal Code', - 'genome': 'L2J2P3W2', - 'domain': 'simple processing', - 'coords': (0.2, 0.2, 0.3, 0.2) + "name": "Minimal Code", + "genome": "L2J2P3W2", + "domain": "simple processing", + "coords": (0.2, 0.2, 0.3, 0.2), }, { - 'name': 'Safe Code', - 'genome': 'L8J6P5W5', - 'domain': 'data validation', - 'coords': (0.8, 0.6, 0.5, 0.5) + "name": "Safe Code", + "genome": "L8J6P5W5", + "domain": "data validation", + "coords": (0.8, 0.6, 0.5, 0.5), }, { - 'name': 'Structured Code', - 'genome': 'L6J8P5W6', - 'domain': 'api handling', - 'coords': (0.6, 0.8, 0.5, 0.6) + "name": "Structured Code", + "genome": "L6J8P5W6", + "domain": "api handling", + "coords": (0.6, 0.8, 0.5, 0.6), }, { - 'name': 'High Performance Code', - 'genome': 'L5J5P9W5', - 'domain': 'data transformation', - 'coords': (0.5, 0.5, 0.9, 0.5) + "name": "High Performance Code", + "genome": "L5J5P9W5", + "domain": "data transformation", + "coords": (0.5, 0.5, 0.9, 0.5), }, { - 'name': 'Well-Designed Code', - 'genome': 'L6J6P6W9', - 'domain': 'business logic', - 'coords': (0.6, 0.6, 0.6, 0.9) + "name": "Well-Designed Code", + "genome": "L6J6P6W9", + "domain": "business logic", + "coords": (0.6, 0.6, 0.6, 0.9), }, { - 'name': 'Production Code', - 'genome': 'L8J8P7W8', - 'domain': 'user authentication', - 'coords': (0.8, 0.8, 0.7, 0.8) - } + "name": "Production Code", + "genome": "L8J8P7W8", + "domain": "user authentication", + "coords": (0.8, 0.8, 0.7, 0.8), + }, ] for test in test_cases: @@ -262,18 +277,20 @@ def test_generator(): print("─" * 70) # Generate code - L, J, P, W = test['coords'] - code = generate_code(L, J, P, W, test['domain']) + L, J, P, W = test["coords"] + code = generate_code(L, J, P, W, test["domain"]) print("\nGenerated Code:") print(code) # Validate - validation = validate_generation(test['genome'], code) + validation = validate_generation(test["genome"], code) print(f"\nValidation:") print(f" Target: {validation['target_genome']} at {validation['target_pos']}") - print(f" Actual: {validation['actual_genome']} at ({validation['actual_pos'][0]:.2f}, {validation['actual_pos'][1]:.2f}, {validation['actual_pos'][2]:.2f}, {validation['actual_pos'][3]:.2f})") + print( + f" Actual: {validation['actual_genome']} at ({validation['actual_pos'][0]:.2f}, {validation['actual_pos'][1]:.2f}, {validation['actual_pos'][2]:.2f}, {validation['actual_pos'][3]:.2f})" + ) print(f" Distance: {validation['distance']:.3f}") print(f" Result: {validation['similarity']} {'✓' if validation['match'] else '✗'}") print() @@ -281,7 +298,8 @@ def test_generator(): print("=" * 70) print("CONCLUSIONS") print("=" * 70) - print(""" + print( + """ If generated code consistently matches target genomes, this proves: 1. LJPW coordinates DETERMINE code properties (not just measure them) @@ -296,8 +314,10 @@ def test_generator(): - Automated refactoring: "Move this code toward L=0.9" - Code templates: Library of archetypes at canonical positions - Quality-driven development: Specify target genome, generate scaffold - """) + """ + ) print("=" * 70 + "\n") -if __name__ == '__main__': + +if __name__ == "__main__": test_generator() diff --git a/tools/semantic_diff.py b/tools/semantic_diff.py index 5686827..e3e8269 100644 --- a/tools/semantic_diff.py +++ b/tools/semantic_diff.py @@ -26,14 +26,14 @@ python semantic_diff.py --history myfile.py --commits 10 """ -import sys -from pathlib import Path -import subprocess import argparse import math +import subprocess +import sys +from pathlib import Path -sys.path.insert(0, str(Path(__file__).parent.parent / 'src' / 'ljpw')) -from ljpw_standalone import analyze_quick, calculate_distance +sys.path.insert(0, str(Path(__file__).parent.parent)) +from src.ljpw.ljpw_standalone import analyze_quick, calculate_distance # Natural Equilibrium NATURAL_EQUILIBRIUM = (0.618, 0.414, 0.718, 0.693) @@ -41,10 +41,10 @@ def get_genome(result): """Create genome from LJPW result""" - L = result['ljpw']['L'] - J = result['ljpw']['J'] - P = result['ljpw']['P'] - W = result['ljpw']['W'] + L = result["ljpw"]["L"] + J = result["ljpw"]["J"] + P = result["ljpw"]["P"] + W = result["ljpw"]["W"] L_digit = int(round(L * 10)) % 10 J_digit = int(round(J * 10)) % 10 @@ -64,7 +64,7 @@ def calculate_health(coords): def read_file(filepath): """Read file contents""" try: - with open(filepath, 'r', encoding='utf-8', errors='ignore') as f: + with open(filepath, "r", encoding="utf-8", errors="ignore") as f: return f.read() except FileNotFoundError: print(f"Error: File not found: {filepath}") @@ -75,10 +75,7 @@ def get_git_file_content(commit, filepath): """Get file content at specific git commit""" try: result = subprocess.run( - ['git', 'show', f'{commit}:{filepath}'], - capture_output=True, - text=True, - check=True + ["git", "show", f"{commit}:{filepath}"], capture_output=True, text=True, check=True ) return result.stdout except subprocess.CalledProcessError: @@ -92,10 +89,10 @@ def analyze_code(code, label): return None result = analyze_quick(code) - l = result['ljpw']['L'] - j = result['ljpw']['J'] - p = result['ljpw']['P'] - w = result['ljpw']['W'] + l = result["ljpw"]["L"] + j = result["ljpw"]["J"] + p = result["ljpw"]["P"] + w = result["ljpw"]["W"] coords = (l, j, p, w) genome = get_genome(result) @@ -103,19 +100,19 @@ def analyze_code(code, label): health = calculate_health(coords) return { - 'label': label, - 'coords': coords, - 'genome': genome, - 'dist_to_ne': dist_to_ne, - 'health': health, - 'loc': len(code.split('\n')) + "label": label, + "coords": coords, + "genome": genome, + "dist_to_ne": dist_to_ne, + "health": health, + "loc": len(code.split("\n")), } def print_dimension_bar(value, dimension_name, max_value=1.0, width=40): """Print a horizontal bar chart for a dimension""" filled = int((value / max_value) * width) - bar = '█' * filled + '░' * (width - filled) + bar = "█" * filled + "░" * (width - filled) print(f" {dimension_name:6} [{bar}] {value:.3f}") @@ -125,11 +122,11 @@ def visualize_movement(before, after): print("SEMANTIC SPACE MOVEMENT") print("─" * 70) - dimensions = ['Love', 'Justice', 'Power', 'Wisdom'] + dimensions = ["Love", "Justice", "Power", "Wisdom"] for i, dim in enumerate(dimensions): - before_val = before['coords'][i] - after_val = after['coords'][i] + before_val = before["coords"][i] + after_val = after["coords"][i] delta = after_val - before_val # Create arrow visualization @@ -177,20 +174,24 @@ def semantic_diff(before_code, after_code, before_label="BEFORE", after_label="A print(f" {before['label']:12} {before['genome']}") print(f" {after['label']:12} {after['genome']}") - genome_changed = before['genome'] != after['genome'] + genome_changed = before["genome"] != after["genome"] print(f" {'✓ Genome CHANGED' if genome_changed else '✗ Genome UNCHANGED'}") print() # Coordinates print("4D Coordinates:") - print(f" {before['label']:12} L={before['coords'][0]:.3f}, J={before['coords'][1]:.3f}, " - f"P={before['coords'][2]:.3f}, W={before['coords'][3]:.3f}") - print(f" {after['label']:12} L={after['coords'][0]:.3f}, J={after['coords'][1]:.3f}, " - f"P={after['coords'][2]:.3f}, W={after['coords'][3]:.3f}") + print( + f" {before['label']:12} L={before['coords'][0]:.3f}, J={before['coords'][1]:.3f}, " + f"P={before['coords'][2]:.3f}, W={before['coords'][3]:.3f}" + ) + print( + f" {after['label']:12} L={after['coords'][0]:.3f}, J={after['coords'][1]:.3f}, " + f"P={after['coords'][2]:.3f}, W={after['coords'][3]:.3f}" + ) print() # Semantic distance - semantic_dist = calculate_distance(before['coords'], after['coords']) + semantic_dist = calculate_distance(before["coords"], after["coords"]) print(f"Semantic Distance: {semantic_dist:.3f}") if semantic_dist < 0.1: @@ -208,7 +209,7 @@ def semantic_diff(before_code, after_code, before_label="BEFORE", after_label="A print(f" {before['label']:12} {before['dist_to_ne']:.3f}") print(f" {after['label']:12} {after['dist_to_ne']:.3f}") - ne_delta = after['dist_to_ne'] - before['dist_to_ne'] + ne_delta = after["dist_to_ne"] - before["dist_to_ne"] if ne_delta < -0.05: ne_movement = f"✓ MOVED TOWARD NE ({ne_delta:+.3f}) — Quality improved!" elif ne_delta > 0.05: @@ -224,7 +225,7 @@ def semantic_diff(before_code, after_code, before_label="BEFORE", after_label="A print(f" {before['label']:12} {before['health']:.1f}/100") print(f" {after['label']:12} {after['health']:.1f}/100") - health_delta = after['health'] - before['health'] + health_delta = after["health"] - before["health"] if health_delta > 5: health_assessment = f"✓ Health IMPROVED ({health_delta:+.1f} points)" elif health_delta < -5: @@ -239,7 +240,7 @@ def semantic_diff(before_code, after_code, before_label="BEFORE", after_label="A print("Lines of Code:") print(f" {before['label']:12} {before['loc']} lines") print(f" {after['label']:12} {after['loc']} lines") - loc_delta = after['loc'] - before['loc'] + loc_delta = after["loc"] - before["loc"] print(f" Delta: {loc_delta:+d} lines") print() @@ -251,8 +252,8 @@ def semantic_diff(before_code, after_code, before_label="BEFORE", after_label="A print("RECOMMENDATIONS") print("─" * 70) - l_before, j_before, p_before, w_before = before['coords'] - l_after, j_after, p_after, w_after = after['coords'] + l_before, j_before, p_before, w_before = before["coords"] + l_after, j_after, p_after, w_after = after["coords"] ne_l, ne_j, ne_p, ne_w = NATURAL_EQUILIBRIUM @@ -295,17 +296,17 @@ def analyze_git_history(filepath, num_commits=10): # Get commit history for file try: result = subprocess.run( - ['git', 'log', f'-{num_commits}', '--format=%H %s', '--', filepath], + ["git", "log", f"-{num_commits}", "--format=%H %s", "--", filepath], capture_output=True, text=True, - check=True + check=True, ) - commits = result.stdout.strip().split('\n') + commits = result.stdout.strip().split("\n") except subprocess.CalledProcessError: print(f"Error: Could not get git history for {filepath}") return - if not commits or commits[0] == '': + if not commits or commits[0] == "": print(f"No commits found for {filepath}") return @@ -318,14 +319,14 @@ def analyze_git_history(filepath, num_commits=10): continue commit_hash = commit_line.split()[0] - commit_msg = ' '.join(commit_line.split()[1:]) + commit_msg = " ".join(commit_line.split()[1:]) try: code = get_git_file_content(commit_hash[:7], filepath) analysis = analyze_code(code, commit_hash[:7]) if analysis: - analysis['commit_msg'] = commit_msg + analysis["commit_msg"] = commit_msg history.append(analysis) except: continue @@ -339,8 +340,10 @@ def analyze_git_history(filepath, num_commits=10): print("─" * 100) for h in history: - print(f"{h['label']:<8} {h['genome']:<12} {h['health']:>6.1f}/100 " - f"{h['dist_to_ne']:>7.3f} {h['commit_msg'][:50]}") + print( + f"{h['label']:<8} {h['genome']:<12} {h['health']:>6.1f}/100 " + f"{h['dist_to_ne']:>7.3f} {h['commit_msg'][:50]}" + ) # Evolution summary print("\n" + "=" * 70) @@ -353,15 +356,19 @@ def analyze_git_history(filepath, num_commits=10): print(f"\nFrom {oldest['label']} to {newest['label']}:") print(f" Genome: {oldest['genome']} → {newest['genome']}") - print(f" Health: {oldest['health']:.1f} → {newest['health']:.1f} " - f"({newest['health'] - oldest['health']:+.1f})") - print(f" Dist NE: {oldest['dist_to_ne']:.3f} → {newest['dist_to_ne']:.3f} " - f"({newest['dist_to_ne'] - oldest['dist_to_ne']:+.3f})") + print( + f" Health: {oldest['health']:.1f} → {newest['health']:.1f} " + f"({newest['health'] - oldest['health']:+.1f})" + ) + print( + f" Dist NE: {oldest['dist_to_ne']:.3f} → {newest['dist_to_ne']:.3f} " + f"({newest['dist_to_ne'] - oldest['dist_to_ne']:+.3f})" + ) # Overall trend - if newest['health'] > oldest['health'] + 5: + if newest["health"] > oldest["health"] + 5: trend = "✓ Overall IMPROVEMENT" - elif newest['health'] < oldest['health'] - 5: + elif newest["health"] < oldest["health"] - 5: trend = "✗ Overall DEGRADATION" else: trend = "→ Overall STABLE" @@ -371,13 +378,13 @@ def analyze_git_history(filepath, num_commits=10): def main(): """Main entry point""" - parser = argparse.ArgumentParser(description='Semantic Diff Tool') + parser = argparse.ArgumentParser(description="Semantic Diff Tool") - parser.add_argument('file1', help='First file or git ref') - parser.add_argument('file2', nargs='?', help='Second file or git ref') - parser.add_argument('--git', action='store_true', help='Use git comparison mode') - parser.add_argument('--history', action='store_true', help='Analyze git history') - parser.add_argument('--commits', type=int, default=10, help='Number of commits to analyze') + parser.add_argument("file1", help="First file or git ref") + parser.add_argument("file2", nargs="?", help="Second file or git ref") + parser.add_argument("--git", action="store_true", help="Use git comparison mode") + parser.add_argument("--history", action="store_true", help="Analyze git history") + parser.add_argument("--commits", type=int, default=10, help="Number of commits to analyze") args = parser.parse_args() diff --git a/tools/transformation_library.py b/tools/transformation_library.py index dcfc8ee..5a3c269 100644 --- a/tools/transformation_library.py +++ b/tools/transformation_library.py @@ -15,8 +15,8 @@ new_coords = apply_transformation(old_coords, "add_safety") """ -from typing import Tuple, Dict, List import math +from typing import Dict, List, Tuple # ============================================================================ # DISCOVERED TRANSFORMATIONS @@ -27,27 +27,22 @@ "add_safety": (+0.30, +0.18, 0.00, +0.02), "add_validation": (+0.25, +0.10, 0.00, 0.00), "add_error_handling": (+0.35, +0.20, 0.00, +0.05), - # Structure Transformations "add_documentation": (0.00, +0.18, 0.00, +0.02), "add_types": (0.00, +0.20, 0.00, +0.05), "add_structure": (+0.05, +0.25, 0.00, +0.10), - # Performance Transformations "optimize": (0.00, 0.00, +0.40, +0.20), "add_caching": (-0.05, +0.05, +0.30, +0.10), "parallelize": (0.00, +0.05, +0.35, +0.15), - # Design Transformations "add_abstraction": (+0.05, +0.10, 0.00, +0.30), "apply_patterns": (+0.10, +0.15, 0.00, +0.35), "refactor": (+0.10, +0.15, +0.05, +0.40), - # Composite Transformations "productionize": (+0.40, +0.30, +0.10, +0.20), "harden": (+0.45, +0.35, +0.05, +0.15), "modernize": (+0.15, +0.30, +0.20, +0.35), - # Direction Transformations "move_to_ne": None, # Special: move toward Natural Equilibrium "move_to_anchor": None, # Special: move toward (1,1,1,1) @@ -67,35 +62,35 @@ "use_case": "Make risky code safer", "example": "simple function → defensive function", "discovered": "Experiment 2 (analogical reasoning)", - "confidence": "high" + "confidence": "high", }, "add_documentation": { "description": "Add docstrings and type annotations", "use_case": "Make code more maintainable", "example": "undocumented → documented", "discovered": "Experiment 2 (analogical reasoning)", - "confidence": "high" + "confidence": "high", }, "optimize": { "description": "Apply performance optimizations", "use_case": "Make slow code faster", "example": "loop → list comprehension, caching", "discovered": "Pattern analysis", - "confidence": "medium" + "confidence": "medium", }, "productionize": { "description": "Transform prototype to production code", "use_case": "Prepare code for deployment", "example": "prototype → production", "discovered": "Experiment 2 (analogical reasoning)", - "confidence": "high" + "confidence": "high", }, "refactor": { "description": "Improve design and architecture", "use_case": "Clean up messy code", "example": "spaghetti → well-designed", "discovered": "Pattern analysis", - "confidence": "medium" + "confidence": "medium", }, } @@ -103,10 +98,9 @@ # TRANSFORMATION OPERATIONS # ============================================================================ + def apply_transformation( - coords: Tuple[float, float, float, float], - transformation_name: str, - strength: float = 1.0 + coords: Tuple[float, float, float, float], transformation_name: str, strength: float = 1.0 ) -> Tuple[float, float, float, float]: """ Apply a semantic transformation to coordinates. @@ -144,13 +138,14 @@ def apply_transformation( max(0.0, min(1.5, L + strength * dL)), max(0.0, min(1.5, J + strength * dJ)), max(0.0, min(1.5, P + strength * dP)), - max(0.0, min(1.5, W + strength * dW)) + max(0.0, min(1.5, W + strength * dW)), ) + def move_toward( coords: Tuple[float, float, float, float], target: Tuple[float, float, float, float], - alpha: float = 0.5 + alpha: float = 0.5, ) -> Tuple[float, float, float, float]: """ Move coordinates toward a target position. @@ -166,16 +161,11 @@ def move_toward( L, J, P, W = coords tL, tJ, tP, tW = target - return ( - L + alpha * (tL - L), - J + alpha * (tJ - J), - P + alpha * (tP - P), - W + alpha * (tW - W) - ) + return (L + alpha * (tL - L), J + alpha * (tJ - J), P + alpha * (tP - P), W + alpha * (tW - W)) + def compose_transformations( - coords: Tuple[float, float, float, float], - transformations: List[str] + coords: Tuple[float, float, float, float], transformations: List[str] ) -> Tuple[float, float, float, float]: """ Apply multiple transformations in sequence. @@ -199,9 +189,9 @@ def compose_transformations( result = apply_transformation(result, transformation) return result + def suggest_transformation( - current: Tuple[float, float, float, float], - target: Tuple[float, float, float, float] + current: Tuple[float, float, float, float], target: Tuple[float, float, float, float] ) -> List[str]: """ Suggest transformations to move from current to target. @@ -246,20 +236,21 @@ def suggest_transformation( return suggestions if suggestions else ["move_to_ne"] + def calculate_distance( - coords1: Tuple[float, float, float, float], - coords2: Tuple[float, float, float, float] + coords1: Tuple[float, float, float, float], coords2: Tuple[float, float, float, float] ) -> float: """Calculate Euclidean distance between two positions.""" - return math.sqrt(sum((a-b)**2 for a, b in zip(coords1, coords2))) + return math.sqrt(sum((a - b) ** 2 for a, b in zip(coords1, coords2))) + # ============================================================================ # VISUALIZATION # ============================================================================ + def visualize_transformation( - coords: Tuple[float, float, float, float], - transformation_name: str + coords: Tuple[float, float, float, float], transformation_name: str ) -> str: """ Generate a text visualization of a transformation. @@ -305,6 +296,7 @@ def visualize_transformation( return "\n".join(output) + # ============================================================================ # EXAMPLE USAGE # ============================================================================ diff --git a/tools/universal_constants_validator.py b/tools/universal_constants_validator.py index 2999b3d..b621c33 100644 --- a/tools/universal_constants_validator.py +++ b/tools/universal_constants_validator.py @@ -14,12 +14,12 @@ This tool enables testing new constants and validating theoretical predictions. """ +import math import sys from pathlib import Path -import math -sys.path.insert(0, str(Path(__file__).parent.parent / 'src' / 'ljpw')) -from ljpw_standalone import calculate_distance +sys.path.insert(0, str(Path(__file__).parent.parent)) +from src.ljpw.ljpw_standalone import calculate_distance # Anchor Points ANCHOR_POINT = (1.000, 1.000, 1.000, 1.000) # Divine Perfection @@ -34,70 +34,69 @@ "coords": (0.950, 0.920, 0.980, 0.940), "domain": "Physics", "scale": "Fundamental", - "description": "Divine Spacetime Perfection" + "description": "Divine Spacetime Perfection", }, "α (Fine Structure)": { "coords": (0.870, 0.850, 0.900, 0.880), "domain": "Physics", "scale": "Atomic", - "description": "Divine Atomic Perfection" + "description": "Divine Atomic Perfection", }, "h (Planck Constant)": { "coords": (0.830, 0.810, 0.860, 0.840), "domain": "Physics", "scale": "Quantum", - "description": "Quantum Perfection" + "description": "Quantum Perfection", }, "G (Gravitational)": { "coords": (0.750, 0.730, 0.780, 0.760), "domain": "Physics", "scale": "Cosmic", - "description": "Cosmic Perfection" + "description": "Cosmic Perfection", }, "k (Boltzmann)": { "coords": (0.680, 0.660, 0.710, 0.690), "domain": "Physics", "scale": "Thermodynamic", - "description": "Thermodynamic Perfection" + "description": "Thermodynamic Perfection", }, - # Mathematical Constants "π (Pi)": { "coords": (0.710, 0.690, 0.740, 0.720), "domain": "Mathematics", "scale": "Universal", - "description": "Geometric Perfection" + "description": "Geometric Perfection", }, "e (Euler's Number)": { "coords": (0.730, 0.710, 0.760, 0.740), "domain": "Mathematics", "scale": "Universal", - "description": "Natural Growth Perfection" + "description": "Natural Growth Perfection", }, "φ (Golden Ratio)": { "coords": (0.618, 0.598, 0.648, 0.628), "domain": "Mathematics", "scale": "Universal", - "description": "Aesthetic Perfection" + "description": "Aesthetic Perfection", }, "√2 (Root 2)": { "coords": (0.580, 0.560, 0.610, 0.590), "domain": "Mathematics", "scale": "Universal", - "description": "Structural Perfection" + "description": "Structural Perfection", }, "ln(2) (Natural Log 2)": { "coords": (0.550, 0.530, 0.580, 0.560), "domain": "Information", "scale": "Universal", - "description": "Information Unit Perfection" + "description": "Information Unit Perfection", }, } def analyze_constant(name, data): """Analyze a universal constant's position in semantic space""" - coords = data['coords'] + coords = data["coords"] # Calculate distances dist_anchor = calculate_distance(coords, ANCHOR_POINT) @@ -108,16 +107,16 @@ def analyze_constant(name, data): physical_optimization = 1.0 - (dist_ne / math.sqrt(4)) # Normalized 0-1 return { - 'name': name, - 'coords': coords, - 'domain': data['domain'], - 'scale': data['scale'], - 'description': data['description'], - 'dist_anchor': dist_anchor, - 'dist_ne': dist_ne, - 'divine_perfection': divine_perfection, - 'physical_optimization': physical_optimization, - 'inverse_correlation': dist_anchor + dist_ne # Should be relatively constant if inverse + "name": name, + "coords": coords, + "domain": data["domain"], + "scale": data["scale"], + "description": data["description"], + "dist_anchor": dist_anchor, + "dist_ne": dist_ne, + "divine_perfection": divine_perfection, + "physical_optimization": physical_optimization, + "inverse_correlation": dist_anchor + dist_ne, # Should be relatively constant if inverse } @@ -133,8 +132,7 @@ def validate_scale_perfection_relationship(): print() # Group by scale - physical_constants = {k: v for k, v in UNIVERSAL_CONSTANTS.items() - if v['domain'] == 'Physics'} + physical_constants = {k: v for k, v in UNIVERSAL_CONSTANTS.items() if v["domain"] == "Physics"} scale_order = ["Fundamental", "Atomic", "Quantum", "Cosmic", "Thermodynamic"] @@ -144,23 +142,26 @@ def validate_scale_perfection_relationship(): results = [] for scale in scale_order: for name, data in physical_constants.items(): - if data['scale'] == scale: + if data["scale"] == scale: analysis = analyze_constant(name, data) results.append(analysis) - print(f"{scale:<20} {name:<20} {analysis['dist_anchor']:<15.3f} {analysis['divine_perfection']*100:.1f}%") + print( + f"{scale:<20} {name:<20} {analysis['dist_anchor']:<15.3f} {analysis['divine_perfection']*100:.1f}%" + ) print() # Test correlation scale_values = {"Fundamental": 1, "Atomic": 2, "Quantum": 3, "Cosmic": 4, "Thermodynamic": 5} - ordered_results = sorted([r for r in results if r['scale'] in scale_values], - key=lambda x: scale_values[x['scale']]) + ordered_results = sorted( + [r for r in results if r["scale"] in scale_values], key=lambda x: scale_values[x["scale"]] + ) - distances = [r['dist_anchor'] for r in ordered_results] + distances = [r["dist_anchor"] for r in ordered_results] # Check if increasing - is_increasing = all(distances[i] <= distances[i+1] for i in range(len(distances)-1)) + is_increasing = all(distances[i] <= distances[i + 1] for i in range(len(distances) - 1)) if is_increasing: print("✓ PREDICTION CONFIRMED: Smaller scales ARE closer to divine perfection") @@ -190,23 +191,24 @@ def validate_inverse_pattern(): for name, data in UNIVERSAL_CONSTANTS.items(): analysis = analyze_constant(name, data) results.append(analysis) - print(f"{name:<25} {analysis['dist_anchor']:<15.3f} {analysis['dist_ne']:<15.3f} {analysis['inverse_correlation']:.3f}") + print( + f"{name:<25} {analysis['dist_anchor']:<15.3f} {analysis['dist_ne']:<15.3f} {analysis['inverse_correlation']:.3f}" + ) print() # Calculate correlation coefficient - anchor_dists = [r['dist_anchor'] for r in results] - ne_dists = [r['dist_ne'] for r in results] + anchor_dists = [r["dist_anchor"] for r in results] + ne_dists = [r["dist_ne"] for r in results] # Simple correlation mean_anchor = sum(anchor_dists) / len(anchor_dists) mean_ne = sum(ne_dists) / len(ne_dists) - numerator = sum((a - mean_anchor) * (n - mean_ne) - for a, n in zip(anchor_dists, ne_dists)) + numerator = sum((a - mean_anchor) * (n - mean_ne) for a, n in zip(anchor_dists, ne_dists)) denominator = math.sqrt( - sum((a - mean_anchor)**2 for a in anchor_dists) * - sum((n - mean_ne)**2 for n in ne_dists) + sum((a - mean_anchor) ** 2 for a in anchor_dists) + * sum((n - mean_ne) ** 2 for n in ne_dists) ) correlation = numerator / denominator if denominator != 0 else 0 @@ -248,8 +250,8 @@ def validate_complementary_pairs(): for const1, const2, meaning in pairs: if const1 in UNIVERSAL_CONSTANTS and const2 in UNIVERSAL_CONSTANTS: - coords1 = UNIVERSAL_CONSTANTS[const1]['coords'] - coords2 = UNIVERSAL_CONSTANTS[const2]['coords'] + coords1 = UNIVERSAL_CONSTANTS[const1]["coords"] + coords2 = UNIVERSAL_CONSTANTS[const2]["coords"] distance = calculate_distance(coords1, coords2) @@ -282,19 +284,19 @@ def validate_information_bridge(): results.append(analysis) # Sort by distance from NE - sorted_results = sorted(results, key=lambda x: x['dist_ne']) + sorted_results = sorted(results, key=lambda x: x["dist_ne"]) for i, r in enumerate(sorted_results, 1): - marker = "⭐" if r['name'] == "ln(2) (Natural Log 2)" else "" + marker = "⭐" if r["name"] == "ln(2) (Natural Log 2)" else "" print(f"{r['name']:<25} {r['dist_ne']:<15.3f} #{i} {marker}") print() # Check if ln(2) is in top 3 top_3 = sorted_results[:3] - ln2_in_top3 = any(r['name'] == "ln(2) (Natural Log 2)" for r in top_3) + ln2_in_top3 = any(r["name"] == "ln(2) (Natural Log 2)" for r in top_3) - if sorted_results[0]['name'] == "ln(2) (Natural Log 2)": + if sorted_results[0]["name"] == "ln(2) (Natural Log 2)": print("✓ PREDICTION PERFECTLY CONFIRMED: ln(2) is #1 closest to NE!") elif ln2_in_top3: print("✓ PREDICTION CONFIRMED: ln(2) is in top 3 closest to NE") @@ -318,14 +320,14 @@ def generate_cosmic_map(): results.append(analysis) # Sort by divine perfection - sorted_by_divine = sorted(results, key=lambda x: x['divine_perfection'], reverse=True) + sorted_by_divine = sorted(results, key=lambda x: x["divine_perfection"], reverse=True) print("DIVINE PERFECTION HIERARCHY:") print("(Distance from Anchor Point)") print() for r in sorted_by_divine: - bar_divine = '█' * int(r['divine_perfection'] * 50) + bar_divine = "█" * int(r["divine_perfection"] * 50) print(f"{r['name']:<25} {bar_divine} {r['divine_perfection']*100:.1f}%") print() @@ -333,14 +335,14 @@ def generate_cosmic_map(): print() # Sort by physical optimization - sorted_by_physical = sorted(results, key=lambda x: x['physical_optimization'], reverse=True) + sorted_by_physical = sorted(results, key=lambda x: x["physical_optimization"], reverse=True) print("PHYSICAL OPTIMIZATION HIERARCHY:") print("(Distance from Natural Equilibrium)") print() for r in sorted_by_physical: - bar_physical = '█' * int(r['physical_optimization'] * 50) + bar_physical = "█" * int(r["physical_optimization"] * 50) print(f"{r['name']:<25} {bar_physical} {r['physical_optimization']*100:.1f}%") print() diff --git a/tools/validate_massive_codebase.py b/tools/validate_massive_codebase.py index c5990e5..951444f 100644 --- a/tools/validate_massive_codebase.py +++ b/tools/validate_massive_codebase.py @@ -13,16 +13,17 @@ import time from pathlib import Path -sys.path.insert(0, str(Path(__file__).parent.parent / 'src' / 'ljpw')) +sys.path.insert(0, str(Path(__file__).parent.parent)) -from ljpw_standalone import analyze_directory -from ljpw_semantic_compressor import ( +import math + +from src.ljpw.ljpw_semantic_compressor import ( + LJPWCodon, SemanticCompressor, SemanticDecompressor, SemanticGenome, - LJPWCodon, ) -import math +from src.ljpw.ljpw_standalone import analyze_directory def analyze_massive_codebase(path: str, name: str): @@ -57,10 +58,10 @@ def analyze_massive_codebase(path: str, name: str): print(f" Speed: {len(file_results) / analysis_time:.0f} files/sec") # Aggregate - total_L = sum(r['ljpw']['L'] for r in file_results) - total_J = sum(r['ljpw']['J'] for r in file_results) - total_P = sum(r['ljpw']['P'] for r in file_results) - total_W = sum(r['ljpw']['W'] for r in file_results) + total_L = sum(r["ljpw"]["L"] for r in file_results) + total_J = sum(r["ljpw"]["J"] for r in file_results) + total_P = sum(r["ljpw"]["P"] for r in file_results) + total_W = sum(r["ljpw"]["W"] for r in file_results) state = ( total_L / len(file_results), @@ -85,12 +86,12 @@ def analyze_massive_codebase(path: str, name: str): print(f"Analysis Time: {analysis_time:.1f}s") return { - 'name': name, - 'path': path, - 'state': state, - 'health_score': health_score, - 'num_files': len(file_results), - 'analysis_time': analysis_time, + "name": name, + "path": path, + "state": state, + "health_score": health_score, + "num_files": len(file_results), + "analysis_time": analysis_time, } @@ -100,8 +101,8 @@ def test_compression_at_scale(result): print(f"🧬 SEMANTIC COMPRESSION TEST") print(f"{'='*70}") - name = result['name'] - state = result['state'] + name = result["name"] + state = result["state"] # Test multiple quantization levels levels_to_test = [4, 8, 16] @@ -117,11 +118,11 @@ def test_compression_at_scale(result): # Compress compressor = SemanticCompressor(quantization_levels=levels) - genome = compressor.compress_state_sequence([state], metadata={'project': name}) + genome = compressor.compress_state_sequence([state], metadata={"project": name}) genome_str = genome.to_string() genome_bytes = len(genome_str) - ratio = genome.metadata['compression_ratio'] + ratio = genome.metadata["compression_ratio"] print(f"Genome: {genome_str}") print(f"Size: {genome_bytes} bytes") @@ -132,7 +133,7 @@ def test_compression_at_scale(result): reconstructed = decompressor.decompress_genome(genome)[0] # Calculate error - error = math.sqrt(sum((o - r)**2 for o, r in zip(state, reconstructed))) + error = math.sqrt(sum((o - r) ** 2 for o, r in zip(state, reconstructed))) relative_error = error / math.sqrt(sum(v**2 for v in state)) accuracy = 1 - relative_error @@ -140,27 +141,31 @@ def test_compression_at_scale(result): print(f"Accuracy: {accuracy:.1%}") # Check meaning preservation - dims = ['L', 'J', 'P', 'W'] + dims = ["L", "J", "P", "W"] orig_max = dims[state.index(max(state))] orig_min = dims[state.index(min(state))] recon_max = dims[list(reconstructed).index(max(reconstructed))] recon_min = dims[list(reconstructed).index(min(reconstructed))] - meaning_preserved = (orig_max == recon_max and orig_min == recon_min) + meaning_preserved = orig_max == recon_max and orig_min == recon_min if meaning_preserved: print(f"✅ Meaning preserved (strongest: {orig_max}, weakest: {orig_min})") else: - print(f"⚠️ Meaning changed (strongest: {orig_max}→{recon_max}, weakest: {orig_min}→{recon_min})") - - results.append({ - 'levels': levels, - 'genome': genome_str, - 'bytes': genome_bytes, - 'ratio': ratio, - 'accuracy': accuracy, - 'meaning_preserved': meaning_preserved, - }) + print( + f"⚠️ Meaning changed (strongest: {orig_max}→{recon_max}, weakest: {orig_min}→{recon_min})" + ) + + results.append( + { + "levels": levels, + "genome": genome_str, + "bytes": genome_bytes, + "ratio": ratio, + "accuracy": accuracy, + "meaning_preserved": meaning_preserved, + } + ) return results @@ -171,7 +176,7 @@ def test_token_efficiency(result): print(f"💰 TOKEN EFFICIENCY FOR AI") print(f"{'='*70}") - num_files = result['num_files'] + num_files = result["num_files"] # Estimate: average Python file is ~200 lines, ~50 chars/line = 10,000 chars # Rough token estimate: 1 token ≈ 4 chars @@ -193,7 +198,7 @@ def test_token_efficiency(result): print(f" Cost: ~${total_tokens / 1_000_000 * 3:.2f} per analysis (at $3/1M tokens)") print(f"") print(f"Compressed representation:") - print(f" \"{genome_representation}\"") + print(f' "{genome_representation}"') print(f" Size: ~{compressed_tokens:.0f} tokens") print(f" Cost: ~${compressed_tokens / 1_000_000 * 3:.4f} per analysis") print(f"") @@ -206,9 +211,9 @@ def test_token_efficiency(result): def main(): """Run massive scale validation""" - print("="*70) + print("=" * 70) print("🚀 MASSIVE SCALE SEMANTIC COMPRESSION VALIDATION 🚀") - print("="*70) + print("=" * 70) print() print("Testing on production-sized codebases:") print(" - Django: 2,882 Python files (71MB)") @@ -222,8 +227,7 @@ def main(): # Test Django django_result = analyze_massive_codebase( - "/tmp/ljpw_validation_test/django", - "Django Web Framework" + "/tmp/ljpw_validation_test/django", "Django Web Framework" ) if django_result: @@ -232,13 +236,12 @@ def main(): all_results.append((django_result, django_compression)) # Test SciPy - print("\n" + "="*70) + print("\n" + "=" * 70) print("MOVING TO NEXT MASSIVE CODEBASE") - print("="*70) + print("=" * 70) scipy_result = analyze_massive_codebase( - "/tmp/ljpw_validation_test/scipy", - "SciPy Scientific Computing Library" + "/tmp/ljpw_validation_test/scipy", "SciPy Scientific Computing Library" ) if scipy_result: @@ -256,19 +259,21 @@ def main(): print(f"📊 {result['name']}") print(f" Files: {result['num_files']:,}") print(f" Speed: {result['num_files']/result['analysis_time']:.0f} files/sec") - print(f" LJPW: L={result['state'][0]:.2f}, J={result['state'][1]:.2f}, P={result['state'][2]:.2f}, W={result['state'][3]:.2f}") + print( + f" LJPW: L={result['state'][0]:.2f}, J={result['state'][1]:.2f}, P={result['state'][2]:.2f}, W={result['state'][3]:.2f}" + ) print(f" Health: {result['health_score']:.1%}") # Show best compression result - best = max(compression_results, key=lambda x: x['accuracy']) - status = "✅" if best['meaning_preserved'] else "⚠️" + best = max(compression_results, key=lambda x: x["accuracy"]) + status = "✅" if best["meaning_preserved"] else "⚠️" print(f" Best: {status} {best['levels']}-level, {best['accuracy']:.1%} accuracy") print() # Overall stats - total_files = sum(r[0]['num_files'] for r in all_results) - total_time = sum(r[0]['analysis_time'] for r in all_results) - avg_accuracy = sum(max(cr['accuracy'] for cr in r[1]) for r in all_results) / len(all_results) + total_files = sum(r[0]["num_files"] for r in all_results) + total_time = sum(r[0]["analysis_time"] for r in all_results) + avg_accuracy = sum(max(cr["accuracy"] for cr in r[1]) for r in all_results) / len(all_results) print(f"{'='*70}") print(f"OVERALL STATISTICS") @@ -281,14 +286,8 @@ def main(): print() # Check success - all_preserved = all( - any(cr['meaning_preserved'] for cr in r[1]) - for r in all_results - ) - all_accurate = all( - any(cr['accuracy'] >= 0.85 for cr in r[1]) - for r in all_results - ) + all_preserved = all(any(cr["meaning_preserved"] for cr in r[1]) for r in all_results) + all_accurate = all(any(cr["accuracy"] >= 0.85 for cr in r[1]) for r in all_results) if all_preserved and all_accurate: print(f"✅ MASSIVE SCALE VALIDATION SUCCESSFUL!") @@ -300,5 +299,5 @@ def main(): return 0 -if __name__ == '__main__': +if __name__ == "__main__": exit(main()) diff --git a/tools/validate_real_codebases.py b/tools/validate_real_codebases.py index 5da52b2..52357ec 100644 --- a/tools/validate_real_codebases.py +++ b/tools/validate_real_codebases.py @@ -9,19 +9,20 @@ 4. Validate meaning preservation """ -import sys import os +import sys from pathlib import Path # Add parent directory to path -sys.path.insert(0, str(Path(__file__).parent.parent / 'src' / 'ljpw')) +sys.path.insert(0, str(Path(__file__).parent.parent)) + +import math -from ljpw_standalone import analyze_directory -from ljpw_semantic_compressor import ( +from src.ljpw.ljpw_semantic_compressor import ( SemanticCompressor, SemanticDecompressor, ) -import math +from src.ljpw.ljpw_standalone import analyze_directory def analyze_codebase(path: str): @@ -46,10 +47,10 @@ def analyze_codebase(path: str): return None # Aggregate results across all files - total_L = sum(r['ljpw']['L'] for r in file_results) - total_J = sum(r['ljpw']['J'] for r in file_results) - total_P = sum(r['ljpw']['P'] for r in file_results) - total_W = sum(r['ljpw']['W'] for r in file_results) + total_L = sum(r["ljpw"]["L"] for r in file_results) + total_J = sum(r["ljpw"]["J"] for r in file_results) + total_P = sum(r["ljpw"]["P"] for r in file_results) + total_W = sum(r["ljpw"]["W"] for r in file_results) num_files = len(file_results) # Average LJPW values @@ -71,12 +72,12 @@ def analyze_codebase(path: str): print(f" Health Score: {health_score:.1%}") results = { - 'L': state[0], - 'J': state[1], - 'P': state[2], - 'W': state[3], - 'health_score': health_score, - 'num_files': num_files, + "L": state[0], + "J": state[1], + "P": state[2], + "W": state[3], + "health_score": health_score, + "num_files": num_files, } return state, results @@ -91,10 +92,7 @@ def compress_and_validate(project_name: str, state: tuple, quantization_levels: # Compress print(f"\n1. Compressing with {quantization_levels} levels...") compressor = SemanticCompressor(quantization_levels=quantization_levels) - genome = compressor.compress_state_sequence( - [state], - metadata={'project': project_name} - ) + genome = compressor.compress_state_sequence([state], metadata={"project": project_name}) genome_str = genome.to_string() print(f" Original state: {state}") @@ -111,7 +109,7 @@ def compress_and_validate(project_name: str, state: tuple, quantization_levels: print(f" Reconstructed state: {reconstructed_state}") # Calculate error - error = math.sqrt(sum((o - r)**2 for o, r in zip(state, reconstructed_state))) + error = math.sqrt(sum((o - r) ** 2 for o, r in zip(state, reconstructed_state))) relative_error = error / math.sqrt(sum(v**2 for v in state)) print(f"\n3. Validation:") @@ -121,7 +119,7 @@ def compress_and_validate(project_name: str, state: tuple, quantization_levels: # Dimension-by-dimension comparison print(f"\n4. Dimension Comparison:") - dims = ['L', 'J', 'P', 'W'] + dims = ["L", "J", "P", "W"] for i, dim in enumerate(dims): orig = state[i] recon = reconstructed_state[i] @@ -136,26 +134,28 @@ def compress_and_validate(project_name: str, state: tuple, quantization_levels: print(f" Integrity score: {validation['integrity_score']:.1%}") return { - 'genome': genome_str, - 'error': error, - 'relative_error': relative_error, - 'accuracy': 1 - relative_error, - 'valid': validation['valid'], + "genome": genome_str, + "error": error, + "relative_error": relative_error, + "accuracy": 1 - relative_error, + "valid": validation["valid"], } -def test_meaning_preservation(project_name: str, original_state: tuple, reconstructed_state: tuple, original_analysis: dict): +def test_meaning_preservation( + project_name: str, original_state: tuple, reconstructed_state: tuple, original_analysis: dict +): """Test if we can still understand the project from compressed representation""" print(f"\n{'='*70}") print(f"Meaning Preservation Test: {project_name}") print(f"{'='*70}") - dims = ['L', 'J', 'P', 'W'] + dims = ["L", "J", "P", "W"] dim_names = { - 'L': 'Love/Safety', - 'J': 'Justice/Structure', - 'P': 'Power/Execution', - 'W': 'Wisdom/Design', + "L": "Love/Safety", + "J": "Justice/Structure", + "P": "Power/Execution", + "W": "Wisdom/Design", } print(f"\nCan we still understand the project from the compressed representation?") @@ -181,17 +181,21 @@ def test_meaning_preservation(project_name: str, original_state: tuple, reconstr recon_min_dim = dims[list(reconstructed_state).index(min(reconstructed_state))] if orig_max_dim == recon_max_dim: - print(f" ✓ Correctly identifies strongest dimension: {orig_max_dim} ({dim_names[orig_max_dim]})") + print( + f" ✓ Correctly identifies strongest dimension: {orig_max_dim} ({dim_names[orig_max_dim]})" + ) else: print(f" ✗ Strongest dimension changed: {orig_max_dim} → {recon_max_dim}") if orig_min_dim == recon_min_dim: - print(f" ✓ Correctly identifies weakest dimension: {orig_min_dim} ({dim_names[orig_min_dim]})") + print( + f" ✓ Correctly identifies weakest dimension: {orig_min_dim} ({dim_names[orig_min_dim]})" + ) else: print(f" ✗ Weakest dimension changed: {orig_min_dim} → {recon_min_dim}") # Health assessment - orig_health = original_analysis.get('health_score', 0) + orig_health = original_analysis.get("health_score", 0) # Approximate reconstructed health recon_health_approx = sum(reconstructed_state) / 4 @@ -203,9 +207,9 @@ def test_meaning_preservation(project_name: str, original_state: tuple, reconstr def main(): """Run validation tests on real codebases""" - print("="*70) + print("=" * 70) print("REAL CODEBASE SEMANTIC COMPRESSION VALIDATION") - print("="*70) + print("=" * 70) print("\nTesting semantic compression on actual open-source projects") print("Goal: Prove that meaning is preserved through compression/decompression") @@ -236,51 +240,53 @@ def main(): # Step 3: Test meaning preservation decompressor = SemanticDecompressor(quantization_levels=8) - genome_str = compression_result['genome'] + genome_str = compression_result["genome"] # Re-parse genome - from ljpw_semantic_compressor import SemanticGenome, LJPWCodon - codons = [LJPWCodon.from_string(s) for s in genome_str.split('-')] - genome = SemanticGenome(codons=codons, metadata={'project': name}) + from src.ljpw.ljpw_semantic_compressor import LJPWCodon, SemanticGenome + + codons = [LJPWCodon.from_string(s) for s in genome_str.split("-")] + genome = SemanticGenome(codons=codons, metadata={"project": name}) reconstructed = decompressor.decompress_genome(genome) - meaning_preserved = test_meaning_preservation( - name, - state, - reconstructed[0], - full_analysis + meaning_preserved = test_meaning_preservation(name, state, reconstructed[0], full_analysis) + + results.append( + { + "project": name, + "files": len(list(Path(path).rglob("*.py"))), + "state": state, + "genome": compression_result["genome"], + "accuracy": compression_result["accuracy"], + "meaning_preserved": meaning_preserved, + } ) - results.append({ - 'project': name, - 'files': len(list(Path(path).rglob("*.py"))), - 'state': state, - 'genome': compression_result['genome'], - 'accuracy': compression_result['accuracy'], - 'meaning_preserved': meaning_preserved, - }) - # Summary - print("\n" + "="*70) + print("\n" + "=" * 70) print("VALIDATION SUMMARY") - print("="*70) + print("=" * 70) print(f"\n{'Project':<15} {'Files':<8} {'Accuracy':<12} {'Meaning':<15} {'Genome Size':<12}") print("-" * 70) for r in results: - meaning_str = "✓ Preserved" if r['meaning_preserved'] else "✗ Lost" - print(f"{r['project']:<15} {r['files']:<8} {r['accuracy']:<12.1%} {meaning_str:<15} {len(r['genome']):<12} bytes") + meaning_str = "✓ Preserved" if r["meaning_preserved"] else "✗ Lost" + print( + f"{r['project']:<15} {r['files']:<8} {r['accuracy']:<12.1%} {meaning_str:<15} {len(r['genome']):<12} bytes" + ) # Overall stats - avg_accuracy = sum(r['accuracy'] for r in results) / len(results) if results else 0 - meaning_preserved_count = sum(1 for r in results if r['meaning_preserved']) + avg_accuracy = sum(r["accuracy"] for r in results) / len(results) if results else 0 + meaning_preserved_count = sum(1 for r in results if r["meaning_preserved"]) - print("\n" + "="*70) + print("\n" + "=" * 70) print("OVERALL RESULTS") - print("="*70) + print("=" * 70) print(f"Projects tested: {len(results)}") print(f"Average accuracy: {avg_accuracy:.1%}") - print(f"Meaning preserved: {meaning_preserved_count}/{len(results)} ({meaning_preserved_count/len(results)*100:.0f}%)") + print( + f"Meaning preserved: {meaning_preserved_count}/{len(results)} ({meaning_preserved_count/len(results)*100:.0f}%)" + ) if avg_accuracy >= 0.95 and meaning_preserved_count == len(results): print("\n✅ VALIDATION SUCCESSFUL") @@ -295,5 +301,5 @@ def main(): return 0 if avg_accuracy >= 0.85 else 1 -if __name__ == '__main__': +if __name__ == "__main__": exit(main()) diff --git a/tools/validate_realworld_codebase.py b/tools/validate_realworld_codebase.py index 8d2d0cd..7eb0749 100644 --- a/tools/validate_realworld_codebase.py +++ b/tools/validate_realworld_codebase.py @@ -13,14 +13,14 @@ not just curated examples. """ -import sys -from pathlib import Path import json import math +import sys from collections import defaultdict +from pathlib import Path -sys.path.insert(0, str(Path(__file__).parent.parent / 'src' / 'ljpw')) -from ljpw_standalone import analyze_quick, calculate_distance +sys.path.insert(0, str(Path(__file__).parent.parent)) +from src.ljpw.ljpw_standalone import analyze_quick, calculate_distance # Natural Equilibrium NATURAL_EQUILIBRIUM = (0.618, 0.414, 0.718, 0.693) @@ -29,17 +29,17 @@ def analyze_python_file(filepath): """Analyze a single Python file""" try: - with open(filepath, 'r', encoding='utf-8', errors='ignore') as f: + with open(filepath, "r", encoding="utf-8", errors="ignore") as f: code = f.read() if not code.strip(): return None result = analyze_quick(code) - l = result['ljpw']['L'] - j = result['ljpw']['J'] - p = result['ljpw']['P'] - w = result['ljpw']['W'] + l = result["ljpw"]["L"] + j = result["ljpw"]["J"] + p = result["ljpw"]["P"] + w = result["ljpw"]["W"] coords = (l, j, p, w) dist_to_ne = calculate_distance(coords, NATURAL_EQUILIBRIUM) @@ -57,13 +57,13 @@ def analyze_python_file(filepath): genome = f"L{L_digit}J{J_digit}P{P_digit}W{W_digit}" return { - 'file': str(filepath), - 'coords': coords, - 'genome': genome, - 'dist_to_ne': dist_to_ne, - 'dist_to_anchor': dist_to_anchor, - 'health': health, - 'loc': len(code.split('\n')) + "file": str(filepath), + "coords": coords, + "genome": genome, + "dist_to_ne": dist_to_ne, + "dist_to_anchor": dist_to_anchor, + "health": health, + "loc": len(code.split("\n")), } except Exception as e: @@ -71,7 +71,7 @@ def analyze_python_file(filepath): return None -def analyze_codebase(root_dir='.', pattern='*.py', max_files=50): +def analyze_codebase(root_dir=".", pattern="*.py", max_files=50): """Analyze all Python files in a directory""" print("=" * 70) print("REAL-WORLD CODEBASE VALIDATION") @@ -103,7 +103,7 @@ def print_distribution_analysis(results): print() # Health score distribution - health_scores = [r['health'] for r in results] + health_scores = [r["health"] for r in results] avg_health = sum(health_scores) / len(health_scores) min_health = min(health_scores) max_health = max(health_scores) @@ -115,7 +115,7 @@ def print_distribution_analysis(results): print() # Distance to NE distribution - distances = [r['dist_to_ne'] for r in results] + distances = [r["dist_to_ne"] for r in results] avg_dist = sum(distances) / len(distances) print(f"Distance to Natural Equilibrium:") @@ -126,18 +126,20 @@ def print_distribution_analysis(results): coords_sum = [0, 0, 0, 0] for r in results: for i in range(4): - coords_sum[i] += r['coords'][i] + coords_sum[i] += r["coords"][i] n = len(results) avg_coords = tuple(c / n for c in coords_sum) print(f"Average Position:") - print(f" L={avg_coords[0]:.3f}, J={avg_coords[1]:.3f}, P={avg_coords[2]:.3f}, W={avg_coords[3]:.3f}") + print( + f" L={avg_coords[0]:.3f}, J={avg_coords[1]:.3f}, P={avg_coords[2]:.3f}, W={avg_coords[3]:.3f}" + ) print(f" Distance from NE: {calculate_distance(avg_coords, NATURAL_EQUILIBRIUM):.3f}") print() # Genome diversity - genomes = [r['genome'] for r in results] + genomes = [r["genome"] for r in results] unique_genomes = len(set(genomes)) diversity = (unique_genomes / len(genomes)) * 100 @@ -156,7 +158,7 @@ def print_clustering_analysis(results): # Group by genome genome_groups = defaultdict(list) for r in results: - genome_groups[r['genome']].append(r['file']) + genome_groups[r["genome"]].append(r["file"]) # Find largest clusters sorted_clusters = sorted(genome_groups.items(), key=lambda x: len(x[1]), reverse=True) @@ -179,11 +181,11 @@ def print_quality_insights(results): print() # Files far from NE (potential code smells) - far_from_ne = sorted(results, key=lambda x: x['dist_to_ne'], reverse=True)[:5] + far_from_ne = sorted(results, key=lambda x: x["dist_to_ne"], reverse=True)[:5] print("Files Farthest from Natural Equilibrium (potential code smells):") for i, r in enumerate(far_from_ne, 1): - filename = Path(r['file']).name + filename = Path(r["file"]).name print(f"{i}. {filename}") print(f" Genome: {r['genome']}") print(f" Health: {r['health']:.1f}/100") @@ -191,11 +193,11 @@ def print_quality_insights(results): print() # Files closest to NE (high quality) - close_to_ne = sorted(results, key=lambda x: x['dist_to_ne'])[:5] + close_to_ne = sorted(results, key=lambda x: x["dist_to_ne"])[:5] print("Files Closest to Natural Equilibrium (high quality):") for i, r in enumerate(close_to_ne, 1): - filename = Path(r['file']).name + filename = Path(r["file"]).name print(f"{i}. {filename}") print(f" Genome: {r['genome']}") print(f" Health: {r['health']:.1f}/100") @@ -203,9 +205,9 @@ def print_quality_insights(results): print() -def export_results(results, output_file='realworld_analysis.json'): +def export_results(results, output_file="realworld_analysis.json"): """Export results to JSON for further analysis""" - with open(output_file, 'w') as f: + with open(output_file, "w") as f: json.dump(results, f, indent=2) print(f"\n✓ Results exported to {output_file}") @@ -213,7 +215,7 @@ def export_results(results, output_file='realworld_analysis.json'): def main(): """Run real-world codebase validation""" # Analyze current directory (the Semantic-Compressor codebase itself!) - results = analyze_codebase('.', '*.py', max_files=30) + results = analyze_codebase(".", "*.py", max_files=30) if not results: print("No files to analyze!") diff --git a/tools/validate_ultra_massive.py b/tools/validate_ultra_massive.py index 12a6732..7cdaadb 100644 --- a/tools/validate_ultra_massive.py +++ b/tools/validate_ultra_massive.py @@ -13,14 +13,15 @@ import time from pathlib import Path -sys.path.insert(0, str(Path(__file__).parent.parent / 'src' / 'ljpw')) +sys.path.insert(0, str(Path(__file__).parent.parent)) -from ljpw_standalone import analyze_directory -from ljpw_semantic_compressor import ( +import math + +from src.ljpw.ljpw_semantic_compressor import ( SemanticCompressor, SemanticDecompressor, ) -import math +from src.ljpw.ljpw_standalone import analyze_directory def analyze_ultra_massive(path: str, name: str): @@ -53,10 +54,10 @@ def analyze_ultra_massive(path: str, name: str): print(f" Speed: {len(file_results) / analysis_time:.0f} files/sec") # Aggregate - total_L = sum(r['ljpw']['L'] for r in file_results) - total_J = sum(r['ljpw']['J'] for r in file_results) - total_P = sum(r['ljpw']['P'] for r in file_results) - total_W = sum(r['ljpw']['W'] for r in file_results) + total_L = sum(r["ljpw"]["L"] for r in file_results) + total_J = sum(r["ljpw"]["J"] for r in file_results) + total_P = sum(r["ljpw"]["P"] for r in file_results) + total_W = sum(r["ljpw"]["W"] for r in file_results) state = ( total_L / len(file_results), @@ -83,13 +84,13 @@ def analyze_ultra_massive(path: str, name: str): print(f"{'='*70}") compressor = SemanticCompressor(quantization_levels=16) - genome = compressor.compress_state_sequence([state], metadata={'project': name}) + genome = compressor.compress_state_sequence([state], metadata={"project": name}) genome_str = genome.to_string() decompressor = SemanticDecompressor(quantization_levels=16) reconstructed = decompressor.decompress_genome(genome)[0] - error = math.sqrt(sum((o - r)**2 for o, r in zip(state, reconstructed))) + error = math.sqrt(sum((o - r) ** 2 for o, r in zip(state, reconstructed))) relative_error = error / math.sqrt(sum(v**2 for v in state)) accuracy = 1 - relative_error @@ -97,36 +98,38 @@ def analyze_ultra_massive(path: str, name: str): print(f"Accuracy: {accuracy:.1%}") # Check meaning preservation - dims = ['L', 'J', 'P', 'W'] + dims = ["L", "J", "P", "W"] orig_max = dims[state.index(max(state))] orig_min = dims[state.index(min(state))] recon_max = dims[list(reconstructed).index(max(reconstructed))] recon_min = dims[list(reconstructed).index(min(reconstructed))] - meaning_preserved = (orig_max == recon_max and orig_min == recon_min) + meaning_preserved = orig_max == recon_max and orig_min == recon_min if meaning_preserved: print(f"✅ Meaning preserved (strongest: {orig_max}, weakest: {orig_min})") else: - print(f"⚠️ Meaning changed (strongest: {orig_max}→{recon_max}, weakest: {orig_min}→{recon_min})") + print( + f"⚠️ Meaning changed (strongest: {orig_max}→{recon_max}, weakest: {orig_min}→{recon_min})" + ) return { - 'name': name, - 'num_files': len(file_results), - 'state': state, - 'health_score': health_score, - 'analysis_time': analysis_time, - 'genome': genome_str, - 'accuracy': accuracy, - 'meaning_preserved': meaning_preserved, + "name": name, + "num_files": len(file_results), + "state": state, + "health_score": health_score, + "analysis_time": analysis_time, + "genome": genome_str, + "accuracy": accuracy, + "meaning_preserved": meaning_preserved, } def main(): """Run ultra-massive validation""" - print("="*70) + print("=" * 70) print("🔥 ULTRA MASSIVE SCALE VALIDATION 🔥") - print("="*70) + print("=" * 70) print() print("Testing the BIGGEST Python projects:") print(" - scikit-learn: 983 Python files") @@ -140,21 +143,19 @@ def main(): # Test scikit-learn sklearn_result = analyze_ultra_massive( - "/tmp/ljpw_validation_test/scikit-learn", - "scikit-learn (ML Library)" + "/tmp/ljpw_validation_test/scikit-learn", "scikit-learn (ML Library)" ) if sklearn_result: all_results.append(sklearn_result) # Test Transformers (THE BIG ONE!) - print("\n" + "="*70) + print("\n" + "=" * 70) print("🚀 TESTING THE BIGGEST: TRANSFORMERS") - print("="*70) + print("=" * 70) transformers_result = analyze_ultra_massive( - "/tmp/ljpw_validation_test/transformers", - "Transformers (Hugging Face)" + "/tmp/ljpw_validation_test/transformers", "Transformers (Hugging Face)" ) if transformers_result: @@ -167,19 +168,21 @@ def main(): print() for r in all_results: - status = "✅" if r['meaning_preserved'] else "⚠️" + status = "✅" if r["meaning_preserved"] else "⚠️" print(f"{status} {r['name']}") print(f" Files: {r['num_files']:,}") print(f" Speed: {r['num_files']/r['analysis_time']:.0f} files/sec") - print(f" LJPW: L={r['state'][0]:.2f}, J={r['state'][1]:.2f}, P={r['state'][2]:.2f}, W={r['state'][3]:.2f}") + print( + f" LJPW: L={r['state'][0]:.2f}, J={r['state'][1]:.2f}, P={r['state'][2]:.2f}, W={r['state'][3]:.2f}" + ) print(f" Health: {r['health_score']:.1%}") print(f" Accuracy: {r['accuracy']:.1%}") print() # Overall stats - total_files = sum(r['num_files'] for r in all_results) - total_time = sum(r['analysis_time'] for r in all_results) - avg_accuracy = sum(r['accuracy'] for r in all_results) / len(all_results) if all_results else 0 + total_files = sum(r["num_files"] for r in all_results) + total_time = sum(r["analysis_time"] for r in all_results) + avg_accuracy = sum(r["accuracy"] for r in all_results) / len(all_results) if all_results else 0 print(f"{'='*70}") print(f"COMBINED STATISTICS") @@ -201,7 +204,7 @@ def main(): print(f"GRAND TOTAL: {309 + 4386 + total_files:,} PYTHON FILES VALIDATED!") print() - if all(r['meaning_preserved'] for r in all_results): + if all(r["meaning_preserved"] for r in all_results): print(f"✅ ULTRA MASSIVE VALIDATION SUCCESSFUL!") print(f" Framework handles the BIGGEST Python codebases!") return 0 @@ -210,5 +213,5 @@ def main(): return 0 -if __name__ == '__main__': +if __name__ == "__main__": exit(main())