Buckets:
| #!/usr/bin/env python3 | |
| """ | |
| Complete Notebook Execution Script for CA12 - Tree of Thought | |
| Executes cells one by one, captures outputs, handles errors, and enhances the notebook | |
| """ | |
| import os | |
| import sys | |
| import json | |
| import traceback | |
| import warnings | |
| from io import StringIO | |
| from contextlib import redirect_stdout, redirect_stderr | |
| # Add parent directory to path | |
| sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) | |
| warnings.filterwarnings('ignore') | |
| # Configure matplotlib for non-interactive backend | |
| import matplotlib | |
| matplotlib.use('Agg') | |
| import matplotlib.pyplot as plt | |
| import numpy as np | |
| import networkx as nx | |
| from typing import List, Dict, Optional, Tuple, Any, Set | |
| from dataclasses import dataclass, field | |
| from enum import Enum | |
| import heapq | |
| import copy | |
| from collections import deque | |
| import random | |
| import time | |
| def execute_cell(cell_code, global_namespace, cell_index): | |
| """Execute a single cell and capture output""" | |
| print(f"\n{'='*80}") | |
| print(f"Executing Cell {cell_index + 1}") | |
| print(f"{'='*80}") | |
| # Capture stdout and stderr | |
| stdout_capture = StringIO() | |
| stderr_capture = StringIO() | |
| result = { | |
| 'success': False, | |
| 'output': '', | |
| 'error': None, | |
| 'figures': [] | |
| } | |
| try: | |
| with redirect_stdout(stdout_capture), redirect_stderr(stderr_capture): | |
| # Execute the cell | |
| exec(cell_code, global_namespace) | |
| # Capture any matplotlib figures | |
| if plt.get_fignums(): | |
| result['figures'] = plt.get_fignums().copy() | |
| # Save figures | |
| for i, fig_num in enumerate(plt.get_fignums()): | |
| fig = plt.figure(fig_num) | |
| filename = f'output_figures/cell_{cell_index + 1}_fig_{i + 1}.png' | |
| os.makedirs('output_figures', exist_ok=True) | |
| fig.savefig(filename, dpi=100, bbox_inches='tight') | |
| print(f"Saved figure: {filename}") | |
| plt.close('all') | |
| # Get captured output | |
| output = stdout_capture.getvalue() | |
| error_output = stderr_capture.getvalue() | |
| if output: | |
| result['output'] = output | |
| print(output) | |
| if error_output: | |
| result['output'] += '\n' + error_output | |
| print(error_output) | |
| result['success'] = True | |
| print(f"โ Cell {cell_index + 1} executed successfully") | |
| except Exception as e: | |
| error_msg = f"Error in cell {cell_index + 1}:\n{traceback.format_exc()}" | |
| result['error'] = error_msg | |
| print(f"โ {error_msg}") | |
| return result | |
| def main(): | |
| """Main execution function""" | |
| print("๐ Starting CA12 Notebook Execution") | |
| print("=" * 80) | |
| notebook_path = 'notebooks/CA12.ipynb' | |
| if not os.path.exists(notebook_path): | |
| print(f"โ Notebook not found: {notebook_path}") | |
| return | |
| # Load notebook | |
| with open(notebook_path, 'r') as f: | |
| notebook = json.load(f) | |
| print(f"๐ Loaded notebook with {len(notebook['cells'])} cells") | |
| # Create global namespace for execution | |
| global_namespace = { | |
| '__name__': '__main__', | |
| '__file__': notebook_path, | |
| } | |
| # Track execution results | |
| execution_log = [] | |
| successful_cells = 0 | |
| failed_cells = 0 | |
| # Execute each cell | |
| for idx, cell in enumerate(notebook['cells']): | |
| if cell['cell_type'] == 'code': | |
| # Get cell source | |
| source = ''.join(cell['source']) | |
| if source.strip(): # Only execute non-empty cells | |
| result = execute_cell(source, global_namespace, idx) | |
| execution_log.append({ | |
| 'cell_index': idx, | |
| 'cell_type': 'code', | |
| 'success': result['success'], | |
| 'output_length': len(result['output']), | |
| 'has_error': result['error'] is not None, | |
| 'num_figures': len(result['figures']) | |
| }) | |
| if result['success']: | |
| successful_cells += 1 | |
| else: | |
| failed_cells += 1 | |
| else: | |
| print(f"\nSkipping empty code cell {idx + 1}") | |
| else: | |
| print(f"\nSkipping markdown cell {idx + 1}") | |
| execution_log.append({ | |
| 'cell_index': idx, | |
| 'cell_type': 'markdown', | |
| 'success': True | |
| }) | |
| # Print summary | |
| print(f"\n{'='*80}") | |
| print(f"๐ EXECUTION SUMMARY") | |
| print(f"{'='*80}") | |
| print(f"Total cells: {len(notebook['cells'])}") | |
| print(f"Code cells executed: {successful_cells + failed_cells}") | |
| print(f"โ Successful: {successful_cells}") | |
| print(f"โ Failed: {failed_cells}") | |
| print(f"Success rate: {successful_cells / max(1, successful_cells + failed_cells) * 100:.1f}%") | |
| # Save execution log | |
| with open('execution_log_ca12.json', 'w') as f: | |
| json.dump(execution_log, f, indent=2) | |
| print(f"\n๐พ Execution log saved to: execution_log_ca12.json") | |
| print(f"๐จ Figures saved to: output_figures/") | |
| print(f"\n{'='*80}") | |
| print("๐ Notebook execution completed!") | |
| print(f"{'='*80}") | |
| if __name__ == '__main__': | |
| main() | |
Xet Storage Details
- Size:
- 5.4 kB
- Xet hash:
- 5e31e8178415d2b6efce606f8cffd4ade7ff4b3c6d38156c73d2c277efceaa87
ยท
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.