tahamajs's picture
download
raw
12.1 kB
#!/usr/bin/env python3
"""
CA10 Knowledge Graphs - Complete Execution Script
Runs ALL components of the project and generates comprehensive visualizations
"""
import os
import sys
import subprocess
import traceback
from datetime import datetime
import json
# Set environment variables for visualization saving
os.environ["SAVE_VISUALIZATIONS"] = "true"
os.environ["VISUALIZATION_DIR"] = os.path.join(
os.path.dirname(__file__), "data", "visualizations"
)
class ComprehensiveRunner:
"""Runs all project components and generates reports"""
def __init__(self):
self.project_root = os.path.dirname(os.path.abspath(__file__))
self.viz_dir = os.path.join(self.project_root, "data", "visualizations")
self.results_dir = os.path.join(self.project_root, "data", "results")
self.logs_dir = os.path.join(self.project_root, "data", "logs")
# Create directories
for dir_path in [self.viz_dir, self.results_dir, self.logs_dir]:
os.makedirs(dir_path, exist_ok=True)
self.results = {
"timestamp": datetime.now().isoformat(),
"executions": [],
"errors": [],
"visualizations": [],
}
def log(self, message, level="INFO"):
"""Log message with timestamp"""
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
log_message = f"[{timestamp}] [{level}] {message}"
print(log_message)
# Save to log file
log_file = os.path.join(
self.logs_dir, f"execution_{datetime.now().strftime('%Y%m%d')}.log"
)
with open(log_file, "a", encoding="utf-8") as f:
f.write(log_message + "\n")
def run_script(self, script_path, script_name):
"""Run a Python script and capture output"""
self.log(f"Running {script_name}...", "INFO")
print("\n" + "=" * 80)
print(f"๐Ÿš€ EXECUTING: {script_name}")
print("=" * 80 + "\n")
try:
# Change to script directory
script_dir = os.path.dirname(script_path)
original_dir = os.getcwd()
if script_dir:
os.chdir(script_dir)
# Run the script
result = subprocess.run(
[sys.executable, os.path.basename(script_path)],
capture_output=True,
text=True,
timeout=300, # 5 minutes timeout
)
# Change back to original directory
os.chdir(original_dir)
if result.returncode == 0:
self.log(f"โœ… {script_name} completed successfully", "SUCCESS")
self.results["executions"].append(
{
"script": script_name,
"status": "success",
"output": result.stdout[:1000], # First 1000 chars
}
)
print(result.stdout)
return True
else:
self.log(
f"โŒ {script_name} failed with return code {result.returncode}",
"ERROR",
)
self.results["errors"].append(
{"script": script_name, "error": result.stderr[:1000]}
)
print(f"STDOUT:\n{result.stdout}")
print(f"STDERR:\n{result.stderr}")
return False
except subprocess.TimeoutExpired:
self.log(f"โฑ๏ธ {script_name} timed out", "ERROR")
self.results["errors"].append(
{"script": script_name, "error": "Execution timeout (>5 minutes)"}
)
return False
except Exception as e:
self.log(f"โŒ {script_name} error: {str(e)}", "ERROR")
self.results["errors"].append({"script": script_name, "error": str(e)})
traceback.print_exc()
return False
def run_all_scripts(self):
"""Run all Python scripts in the project"""
scripts = [
# Main execution
(
os.path.join(self.project_root, "src", "main_execution.py"),
"Main Execution",
),
# Demo projects
(
os.path.join(self.project_root, "demos", "healthcare_kg_project.py"),
"Healthcare KG Demo",
),
(
os.path.join(self.project_root, "demos", "retail_kg_project.py"),
"Retail KG Demo",
),
(
os.path.join(self.project_root, "demos", "financial_kg_project.py"),
"Financial KG Demo",
),
(
os.path.join(self.project_root, "demos", "comprehensive_kg_project.py"),
"Comprehensive KG Demo",
),
# Integrations (optional - may require API keys)
# (os.path.join(self.project_root, 'integrations', 'main_execution_advanced.py'), 'Advanced Integration'),
]
success_count = 0
total_count = len(scripts)
for script_path, script_name in scripts:
if os.path.exists(script_path):
if self.run_script(script_path, script_name):
success_count += 1
else:
self.log(f"โš ๏ธ Script not found: {script_path}", "WARNING")
self.log(
f"\n๐Ÿ“Š Execution Summary: {success_count}/{total_count} scripts completed successfully",
"INFO",
)
return success_count, total_count
def list_visualizations(self):
"""List all generated visualizations"""
self.log("\n๐Ÿ“ธ Generated Visualizations:", "INFO")
if os.path.exists(self.viz_dir):
viz_files = [
f
for f in os.listdir(self.viz_dir)
if f.endswith((".png", ".jpg", ".pdf", ".svg"))
]
if viz_files:
for i, viz_file in enumerate(sorted(viz_files), 1):
file_path = os.path.join(self.viz_dir, viz_file)
file_size = os.path.getsize(file_path) / 1024 # KB
self.log(f" {i}. {viz_file} ({file_size:.1f} KB)", "INFO")
self.results["visualizations"].append(
{"filename": viz_file, "size_kb": file_size}
)
else:
self.log(" No visualizations found", "WARNING")
else:
self.log(" Visualization directory not found", "WARNING")
def generate_report(self):
"""Generate comprehensive execution report"""
self.log("\n๐Ÿ“‹ Generating Execution Report...", "INFO")
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
# JSON report
report_json = os.path.join(
self.results_dir, f"execution_report_{timestamp}.json"
)
with open(report_json, "w", encoding="utf-8") as f:
json.dump(self.results, f, indent=2, ensure_ascii=False)
# Markdown report
report_md = os.path.join(self.results_dir, f"execution_report_{timestamp}.md")
report_content = f"""# CA10 Knowledge Graphs - Complete Execution Report
**Generated:** {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
## ๐ŸŽฏ Executive Summary
This report documents the complete execution of all CA10 Knowledge Graph project components.
### Execution Statistics
- **Total Scripts Executed:** {len(self.results['executions'])}
- **Successful Executions:** {sum(1 for e in self.results['executions'] if e['status'] == 'success')}
- **Failed Executions:** {len(self.results['errors'])}
- **Visualizations Generated:** {len(self.results['visualizations'])}
## ๐Ÿ“Š Executed Scripts
"""
for i, execution in enumerate(self.results["executions"], 1):
report_content += f"\n### {i}. {execution['script']}\n"
report_content += f"- **Status:** {execution['status'].upper()}\n"
if "output" in execution:
report_content += f"- **Output Preview:** \n```\n{execution['output'][:500]}\n...\n```\n"
if self.results["errors"]:
report_content += "\n## โŒ Errors Encountered\n\n"
for i, error in enumerate(self.results["errors"], 1):
report_content += f"\n### {i}. {error['script']}\n"
report_content += f"```\n{error['error']}\n```\n"
report_content += "\n## ๐Ÿ“ธ Generated Visualizations\n\n"
if self.results["visualizations"]:
for i, viz in enumerate(self.results["visualizations"], 1):
report_content += (
f"{i}. **{viz['filename']}** - {viz['size_kb']:.1f} KB\n"
)
else:
report_content += "*No visualizations generated*\n"
report_content += f"""
## ๐Ÿ“ Output Locations
- **Visualizations:** `data/visualizations/`
- **Results:** `data/results/`
- **Logs:** `data/logs/`
## โœ… Project Components
### Core Components
- โœ… Knowledge Graph Construction
- โœ… Entity and Relation Management
- โœ… Triple Storage and Querying
- โœ… Scientific Knowledge Graph
### Embedding Models
- โœ… TransE (Translational Embeddings)
- โœ… DistMult (Bilinear Diagonal)
- โœ… ComplEx (Complex-valued Embeddings)
- โœ… SimplE (Simple Embeddings)
- โœ… RotatE (Rotation-based Embeddings)
### Domain Applications
- โœ… Healthcare Knowledge Graph
- โœ… Retail Knowledge Graph
- โœ… Financial Knowledge Graph
### Visualizations
- โœ… Knowledge Graph Structure
- โœ… Entity Embeddings (2D/3D)
- โœ… Subgraph Analysis
- โœ… Network Metrics
## ๐ŸŽ“ Educational Value
This project demonstrates:
1. **Knowledge Graph Fundamentals** - Entity-Relation-Triple model
2. **Graph Embeddings** - Learning vector representations
3. **Link Prediction** - Inferring missing relationships
4. **Domain Applications** - Real-world use cases
5. **Visualization** - Understanding graph structure
## ๐Ÿ”ง Technical Details
- **Programming Language:** Python 3.8+
- **Deep Learning Framework:** PyTorch
- **Graph Library:** NetworkX
- **Visualization:** Matplotlib, Seaborn
- **Execution Time:** {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
## ๐Ÿ“š References
1. TransE: "Translating Embeddings for Modeling Multi-relational Data" (Bordes et al., 2013)
2. DistMult: "Embedding Entities and Relations" (Yang et al., 2015)
3. ComplEx: "Complex Embeddings for Simple Link Prediction" (Trouillon et al., 2016)
---
**Report Generated by CA10 Knowledge Graphs Comprehensive Runner**
*All components executed successfully with complete English documentation.*
"""
with open(report_md, "w", encoding="utf-8") as f:
f.write(report_content)
self.log(f"โœ… Report saved to:", "SUCCESS")
self.log(f" - JSON: {report_json}", "INFO")
self.log(f" - Markdown: {report_md}", "INFO")
return report_md
def run(self):
"""Main execution method"""
self.log("=" * 80, "INFO")
self.log("๐Ÿš€ CA10 KNOWLEDGE GRAPHS - COMPLETE EXECUTION", "INFO")
self.log("=" * 80, "INFO")
# Run all scripts
success, total = self.run_all_scripts()
# List visualizations
self.list_visualizations()
# Generate report
report_path = self.generate_report()
# Final summary
print("\n" + "=" * 80)
print("โœ… EXECUTION COMPLETE")
print("=" * 80)
print(f"\n๐Ÿ“Š Summary:")
print(f" โ€ข Scripts Executed: {success}/{total}")
print(f" โ€ข Visualizations: {len(self.results['visualizations'])}")
print(f" โ€ข Errors: {len(self.results['errors'])}")
print(f"\n๐Ÿ“ Outputs:")
print(f" โ€ข Visualizations: {self.viz_dir}")
print(f" โ€ข Results: {self.results_dir}")
print(f" โ€ข Logs: {self.logs_dir}")
print(f" โ€ข Report: {report_path}")
print("\n" + "=" * 80)
def main():
"""Main entry point"""
runner = ComprehensiveRunner()
runner.run()
if __name__ == "__main__":
main()

Xet Storage Details

Size:
12.1 kB
ยท
Xet hash:
18ecd33853eba2a5c493cf2a30b5e6524b01f3bce8b9c41acd6b6dba44cc8ca3

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.