Buckets:
tahamajs/Sysmem2_in_AI / ComputerAssignments /CA18_memory_systems /integrations /advanced_visualization.py
| #!/usr/bin/env python3 | |
| import numpy as np | |
| import pandas as pd | |
| import matplotlib.pyplot as plt | |
| import matplotlib.animation as animation | |
| from mpl_toolkits.mplot3d import Axes3D | |
| import seaborn as sns | |
| import plotly.graph_objects as go | |
| import plotly.express as px | |
| from plotly.subplots import make_subplots | |
| import plotly.figure_factory as ff | |
| import networkx as nx | |
| from sklearn.decomposition import PCA | |
| from sklearn.manifold import TSNE | |
| import warnings | |
| warnings.filterwarnings('ignore') | |
| from typing import Dict, List, Tuple, Any, Optional, Union | |
| import json | |
| import pickle | |
| from dataclasses import dataclass | |
| from datetime import datetime, timedelta | |
| import time | |
| import threading | |
| class VisualizationConfig: | |
| figure_size: Tuple[int, int] = (12, 8) | |
| dpi: int = 300 | |
| style: str = 'seaborn-v0_8' | |
| color_palette: str = 'husl' | |
| enable_interactivity: bool = True | |
| animation_speed: float = 1.0 | |
| update_interval: int = 1000 | |
| save_format: str = 'png' | |
| save_dpi: int = 300 | |
| show_plots: bool = True | |
| enable_3d: bool = True | |
| enable_animations: bool = True | |
| enable_real_time: bool = False | |
| class MemoryArchitectureVisualizer: | |
| def __init__(self, config: VisualizationConfig = None): | |
| self.config = config or VisualizationConfig() | |
| self.fig = None | |
| self.ax = None | |
| def create_3d_memory_layout(self, memory_systems: Dict[str, Any]) -> go.Figure: | |
| fig = go.Figure() | |
| positions = { | |
| 'Sequential': {'x': 0, 'y': 0, 'z': 0, 'size': 20, 'color': 'red'}, | |
| 'Associative': {'x': 2, 'y': 0, 'z': 0, 'size': 25, 'color': 'blue'}, | |
| 'Content-Addressable': {'x': 0, 'y': 2, 'z': 0, 'size': 30, 'color': 'green'}, | |
| 'LRU Cache': {'x': 2, 'y': 2, 'z': 0, 'size': 22, 'color': 'orange'}, | |
| 'Neural': {'x': 1, 'y': 1, 'z': 2, 'size': 35, 'color': 'purple'}, | |
| 'Compressed': {'x': 0, 'y': 0, 'z': 2, 'size': 28, 'color': 'brown'}, | |
| 'Hierarchical': {'x': 2, 'y': 0, 'z': 2, 'size': 40, 'color': 'pink'} | |
| } | |
| for system_name, props in positions.items(): | |
| fig.add_trace(go.Scatter3d( | |
| x=[props['x']], | |
| y=[props['y']], | |
| z=[props['z']], | |
| mode='markers', | |
| marker=dict( | |
| size=props['size'], | |
| color=props['color'], | |
| opacity=0.8, | |
| line=dict(width=2, color='black') | |
| ), | |
| name=system_name, | |
| text=[f"{system_name}<br>Size: {props['size']}<br>Position: ({props['x']}, {props['y']}, {props['z']})"], | |
| hovertemplate='<b>%{text}</b><extra></extra>' | |
| )) | |
| connections = [ | |
| ('Sequential', 'Associative'), | |
| ('Associative', 'Content-Addressable'), | |
| ('Content-Addressable', 'Neural'), | |
| ('LRU Cache', 'Hierarchical'), | |
| ('Compressed', 'Hierarchical') | |
| ] | |
| for start, end in connections: | |
| start_pos = positions[start] | |
| end_pos = positions[end] | |
| fig.add_trace(go.Scatter3d( | |
| x=[start_pos['x'], end_pos['x']], | |
| y=[start_pos['y'], end_pos['y']], | |
| z=[start_pos['z'], end_pos['z']], | |
| mode='lines', | |
| line=dict(color='gray', width=3, dash='dash'), | |
| showlegend=False, | |
| hoverinfo='skip' | |
| )) | |
| fig.update_layout( | |
| title='3D Memory System Architecture', | |
| scene=dict( | |
| xaxis_title='Complexity', | |
| yaxis_title='Performance', | |
| zaxis_title='Memory Efficiency', | |
| camera=dict( | |
| eye=dict(x=1.5, y=1.5, z=1.5) | |
| ) | |
| ), | |
| width=800, | |
| height=600 | |
| ) | |
| return fig | |
| def create_memory_hierarchy_diagram(self) -> go.Figure: | |
| fig = go.Figure(data=[go.Sankey( | |
| node=dict( | |
| pad=15, | |
| thickness=20, | |
| line=dict(color="black", width=0.5), | |
| label=["CPU Cache", "RAM", "SSD", "HDD", "Sequential", "Associative", | |
| "Content-Addr", "LRU", "Neural", "Compressed", "Hierarchical"], | |
| color=["blue", "green", "orange", "red", "purple", "cyan", | |
| "magenta", "yellow", "brown", "pink", "gray"] | |
| ), | |
| link=dict( | |
| source=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], | |
| target=[4, 5, 6, 7, 8, 9, 10, 10, 10, 10], | |
| value=[10, 15, 20, 25, 30, 35, 40, 45, 50, 55] | |
| ) | |
| )]) | |
| fig.update_layout( | |
| title_text="Memory System Hierarchy Flow", | |
| font_size=12, | |
| width=800, | |
| height=600 | |
| ) | |
| return fig | |
| class PerformanceDashboard: | |
| def __init__(self, config: VisualizationConfig = None): | |
| self.config = config or VisualizationConfig() | |
| self.data_history = [] | |
| self.max_history = 100 | |
| def create_real_time_dashboard(self, current_data: Dict[str, float]) -> go.Figure: | |
| current_data['timestamp'] = datetime.now() | |
| self.data_history.append(current_data) | |
| if len(self.data_history) > self.max_history: | |
| self.data_history = self.data_history[-self.max_history:] | |
| df = pd.DataFrame(self.data_history) | |
| fig = make_subplots( | |
| rows=2, cols=2, | |
| subplot_titles=('Memory Usage', 'Access Time', 'Hit Rate', 'Throughput'), | |
| specs=[[{"secondary_y": False}, {"secondary_y": False}], | |
| [{"secondary_y": False}, {"secondary_y": False}]] | |
| ) | |
| fig.add_trace( | |
| go.Scatter(x=df['timestamp'], y=df.get('memory_usage', []), | |
| name='Memory Usage', line=dict(color='blue')), | |
| row=1, col=1 | |
| ) | |
| fig.add_trace( | |
| go.Scatter(x=df['timestamp'], y=df.get('access_time', []), | |
| name='Access Time', line=dict(color='red')), | |
| row=1, col=2 | |
| ) | |
| fig.add_trace( | |
| go.Scatter(x=df['timestamp'], y=df.get('hit_rate', []), | |
| name='Hit Rate', line=dict(color='green')), | |
| row=2, col=1 | |
| ) | |
| fig.add_trace( | |
| go.Scatter(x=df['timestamp'], y=df.get('throughput', []), | |
| name='Throughput', line=dict(color='orange')), | |
| row=2, col=2 | |
| ) | |
| fig.update_layout( | |
| title_text="Real-time Memory System Performance Dashboard", | |
| height=600, | |
| showlegend=True | |
| ) | |
| return fig | |
| def create_performance_heatmap(self, performance_data: np.ndarray, | |
| systems: List[str], metrics: List[str]) -> go.Figure: | |
| fig = go.Figure(data=go.Heatmap( | |
| z=performance_data, | |
| x=metrics, | |
| y=systems, | |
| colorscale='RdYlBu_r', | |
| hoverongaps=False, | |
| text=performance_data, | |
| texttemplate="%{text:.2f}", | |
| textfont={"size": 10} | |
| )) | |
| fig.update_layout( | |
| title='Memory System Performance Heatmap', | |
| xaxis_title='Metrics', | |
| yaxis_title='Memory Systems', | |
| width=800, | |
| height=600 | |
| ) | |
| return fig | |
| class NetworkGraphVisualizer: | |
| def __init__(self, config: VisualizationConfig = None): | |
| self.config = config or VisualizationConfig() | |
| def create_memory_network_graph(self, relationships: Dict[str, List[str]]) -> go.Figure: | |
| G = nx.Graph() | |
| for system, connections in relationships.items(): | |
| G.add_node(system) | |
| for connection in connections: | |
| G.add_edge(system, connection) | |
| pos = nx.spring_layout(G, k=3, iterations=50) | |
| edge_x = [] | |
| edge_y = [] | |
| for edge in G.edges(): | |
| x0, y0 = pos[edge[0]] | |
| x1, y1 = pos[edge[1]] | |
| edge_x.extend([x0, x1, None]) | |
| edge_y.extend([y0, y1, None]) | |
| edge_trace = go.Scatter( | |
| x=edge_x, y=edge_y, | |
| line=dict(width=2, color='gray'), | |
| hoverinfo='none', | |
| mode='lines' | |
| ) | |
| node_x = [] | |
| node_y = [] | |
| node_text = [] | |
| node_info = [] | |
| for node in G.nodes(): | |
| x, y = pos[node] | |
| node_x.append(x) | |
| node_y.append(y) | |
| node_text.append(node) | |
| node_info.append(f"System: {node}<br>Connections: {G.degree[node]}") | |
| node_trace = go.Scatter( | |
| x=node_x, y=node_y, | |
| mode='markers+text', | |
| hoverinfo='text', | |
| text=node_text, | |
| textposition="middle center", | |
| hovertext=node_info, | |
| marker=dict( | |
| size=30, | |
| color=[G.degree[node] for node in G.nodes()], | |
| colorscale='Viridis', | |
| line=dict(width=2, color='black') | |
| ) | |
| ) | |
| fig = go.Figure(data=[edge_trace, node_trace], | |
| layout=go.Layout( | |
| title='Memory System Network Graph', | |
| titlefont_size=16, | |
| showlegend=False, | |
| hovermode='closest', | |
| margin=dict(b=20,l=5,r=5,t=40), | |
| annotations=[ dict( | |
| text="Node size represents number of connections", | |
| showarrow=False, | |
| xref="paper", yref="paper", | |
| x=0.005, y=-0.002, | |
| xanchor='left', yanchor='bottom', | |
| font=dict(color='gray', size=12) | |
| )], | |
| xaxis=dict(showgrid=False, zeroline=False, showticklabels=False), | |
| yaxis=dict(showgrid=False, zeroline=False, showticklabels=False), | |
| width=800, | |
| height=600 | |
| )) | |
| return fig | |
| class CorrelationMatrixVisualizer: | |
| def __init__(self, config: VisualizationConfig = None): | |
| self.config = config or VisualizationConfig() | |
| def create_interactive_correlation_matrix(self, data: pd.DataFrame) -> go.Figure: | |
| corr_matrix = data.corr() | |
| fig = go.Figure(data=go.Heatmap( | |
| z=corr_matrix.values, | |
| x=corr_matrix.columns, | |
| y=corr_matrix.columns, | |
| colorscale='RdBu', | |
| zmid=0, | |
| text=corr_matrix.values, | |
| texttemplate="%{text:.2f}", | |
| textfont={"size": 10}, | |
| hoverongaps=False | |
| )) | |
| fig.update_layout( | |
| title='Interactive Correlation Matrix', | |
| xaxis_title='Variables', | |
| yaxis_title='Variables', | |
| width=800, | |
| height=600 | |
| ) | |
| return fig | |
| def create_correlation_network(self, data: pd.DataFrame, threshold: float = 0.5) -> go.Figure: | |
| corr_matrix = data.corr() | |
| G = nx.Graph() | |
| for col in corr_matrix.columns: | |
| G.add_node(col) | |
| for i, col1 in enumerate(corr_matrix.columns): | |
| for j, col2 in enumerate(corr_matrix.columns): | |
| if i < j and abs(corr_matrix.loc[col1, col2]) > threshold: | |
| G.add_edge(col1, col2, weight=abs(corr_matrix.loc[col1, col2])) | |
| pos = nx.spring_layout(G, k=2, iterations=50) | |
| edge_x = [] | |
| edge_y = [] | |
| edge_info = [] | |
| for edge in G.edges(): | |
| x0, y0 = pos[edge[0]] | |
| x1, y1 = pos[edge[1]] | |
| edge_x.extend([x0, x1, None]) | |
| edge_y.extend([y0, y1, None]) | |
| corr_val = corr_matrix.loc[edge[0], edge[1]] | |
| edge_info.append(f"{edge[0]} โ {edge[1]}<br>Correlation: {corr_val:.3f}") | |
| edge_trace = go.Scatter( | |
| x=edge_x, y=edge_y, | |
| line=dict(width=2, color='gray'), | |
| hoverinfo='none', | |
| mode='lines' | |
| ) | |
| node_x = [] | |
| node_y = [] | |
| node_text = [] | |
| node_info = [] | |
| for node in G.nodes(): | |
| x, y = pos[node] | |
| node_x.append(x) | |
| node_y.append(y) | |
| node_text.append(node) | |
| node_info.append(f"Variable: {node}<br>Connections: {G.degree[node]}") | |
| node_trace = go.Scatter( | |
| x=node_x, y=node_y, | |
| mode='markers+text', | |
| hoverinfo='text', | |
| text=node_text, | |
| textposition="middle center", | |
| hovertext=node_info, | |
| marker=dict( | |
| size=30, | |
| color=[G.degree[node] for node in G.nodes()], | |
| colorscale='Viridis', | |
| line=dict(width=2, color='black') | |
| ) | |
| ) | |
| fig = go.Figure(data=[edge_trace, node_trace], | |
| layout=go.Layout( | |
| title=f'Correlation Network (threshold: {threshold})', | |
| titlefont_size=16, | |
| showlegend=False, | |
| hovermode='closest', | |
| margin=dict(b=20,l=5,r=5,t=40), | |
| xaxis=dict(showgrid=False, zeroline=False, showticklabels=False), | |
| yaxis=dict(showgrid=False, zeroline=False, showticklabels=False), | |
| width=800, | |
| height=600 | |
| )) | |
| return fig | |
| class TimeSeriesAnimator: | |
| def __init__(self, config: VisualizationConfig = None): | |
| self.config = config or VisualizationConfig() | |
| def create_animated_performance_plot(self, time_series_data: Dict[str, np.ndarray]) -> go.Figure: | |
| fig = go.Figure() | |
| colors = ['blue', 'red', 'green', 'orange', 'purple'] | |
| for i, (metric, data) in enumerate(time_series_data.items()): | |
| fig.add_trace(go.Scatter( | |
| x=list(range(len(data))), | |
| y=data, | |
| mode='lines', | |
| name=metric, | |
| line=dict(color=colors[i % len(colors)]) | |
| )) | |
| frames = [] | |
| for frame_idx in range(len(list(time_series_data.values())[0])): | |
| frame_data = [] | |
| for metric, data in time_series_data.items(): | |
| frame_data.append(go.Scatter( | |
| x=list(range(frame_idx + 1)), | |
| y=data[:frame_idx + 1], | |
| mode='lines', | |
| name=metric | |
| )) | |
| frames.append(go.Frame(data=frame_data, name=str(frame_idx))) | |
| fig.frames = frames | |
| fig.update_layout( | |
| title='Animated Memory System Performance', | |
| xaxis_title='Time', | |
| yaxis_title='Performance Value', | |
| updatemenus=[{ | |
| 'type': 'buttons', | |
| 'showactive': False, | |
| 'buttons': [ | |
| { | |
| 'label': 'Play', | |
| 'method': 'animate', | |
| 'args': [None, {'frame': {'duration': 100, 'redraw': True}}] | |
| }, | |
| { | |
| 'label': 'Pause', | |
| 'method': 'animate', | |
| 'args': [[None], {'frame': {'duration': 0, 'redraw': False}}] | |
| } | |
| ] | |
| }], | |
| width=800, | |
| height=600 | |
| ) | |
| return fig | |
| class MultiDimensionalVisualizer: | |
| def __init__(self, config: VisualizationConfig = None): | |
| self.config = config or VisualizationConfig() | |
| def create_parallel_coordinates_plot(self, data: pd.DataFrame) -> go.Figure: | |
| fig = go.Figure(data= | |
| go.Parcoords( | |
| line=dict(color=data.iloc[:, -1] if len(data.columns) > 1 else 'blue'), | |
| dimensions=list([ | |
| dict(label=col, values=data[col]) for col in data.columns | |
| ]) | |
| ) | |
| ) | |
| fig.update_layout( | |
| title='Parallel Coordinates Plot', | |
| width=800, | |
| height=600 | |
| ) | |
| return fig | |
| def create_radar_chart(self, data: Dict[str, List[float]], | |
| categories: List[str]) -> go.Figure: | |
| fig = go.Figure() | |
| colors = ['blue', 'red', 'green', 'orange', 'purple'] | |
| for i, (system_name, values) in enumerate(data.items()): | |
| fig.add_trace(go.Scatterpolar( | |
| r=values, | |
| theta=categories, | |
| fill='toself', | |
| name=system_name, | |
| line_color=colors[i % len(colors)] | |
| )) | |
| fig.update_layout( | |
| polar=dict( | |
| radialaxis=dict( | |
| visible=True, | |
| range=[0, 1] | |
| )), | |
| title='Memory System Performance Radar Chart', | |
| width=800, | |
| height=600 | |
| ) | |
| return fig | |
| class ComprehensiveVisualizer: | |
| def __init__(self, config: VisualizationConfig = None): | |
| self.config = config or VisualizationConfig() | |
| self.architecture_viz = MemoryArchitectureVisualizer(config) | |
| self.dashboard = PerformanceDashboard(config) | |
| self.network_viz = NetworkGraphVisualizer(config) | |
| self.correlation_viz = CorrelationMatrixVisualizer(config) | |
| self.animator = TimeSeriesAnimator(config) | |
| self.multidim_viz = MultiDimensionalVisualizer(config) | |
| def create_comprehensive_dashboard(self, data: Dict[str, Any]) -> List[go.Figure]: | |
| figures = [] | |
| if 'memory_systems' in data: | |
| arch_fig = self.architecture_viz.create_3d_memory_layout(data['memory_systems']) | |
| figures.append(arch_fig) | |
| if 'performance_matrix' in data: | |
| perf_fig = self.dashboard.create_performance_heatmap( | |
| data['performance_matrix'], | |
| data.get('systems', []), | |
| data.get('metrics', []) | |
| ) | |
| figures.append(perf_fig) | |
| if 'relationships' in data: | |
| network_fig = self.network_viz.create_memory_network_graph(data['relationships']) | |
| figures.append(network_fig) | |
| if 'correlation_data' in data: | |
| corr_fig = self.correlation_viz.create_interactive_correlation_matrix(data['correlation_data']) | |
| figures.append(corr_fig) | |
| if 'time_series' in data: | |
| anim_fig = self.animator.create_animated_performance_plot(data['time_series']) | |
| figures.append(anim_fig) | |
| if 'radar_data' in data: | |
| radar_fig = self.multidim_viz.create_radar_chart( | |
| data['radar_data'], | |
| data.get('categories', []) | |
| ) | |
| figures.append(radar_fig) | |
| return figures | |
| def save_visualizations(self, figures: List[go.Figure], output_dir: str = "visualizations"): | |
| import os | |
| if not os.path.exists(output_dir): | |
| os.makedirs(output_dir) | |
| for i, fig in enumerate(figures): | |
| filename = f"{output_dir}/visualization_{i+1}.html" | |
| fig.write_html(filename) | |
| print(f"๐พ Saved visualization {i+1} to {filename}") | |
| def create_interactive_report(self, figures: List[go.Figure]) -> str: | |
| html_content = | |
| </div> | |
| <div class="section"> | |
| <h2>Visualization {i+1}</h2> | |
| <div class="plot-container"> | |
| {fig.to_html(include_plotlyjs='cdn', div_id=f"plot_{i+1}")} | |
| </div> | |
| </div> | |
| </body> | |
| </html> | |
| print("๐จ Advanced Visualization System Demo") | |
| print("=" * 50) | |
| np.random.seed(42) | |
| systems = ['Sequential', 'Associative', 'Content-Addr', 'LRU', 'Neural', 'Compressed', 'Hierarchical'] | |
| metrics = ['Memory Usage', 'Access Time', 'Hit Rate', 'Throughput', 'Latency'] | |
| performance_matrix = np.random.rand(len(systems), len(metrics)) | |
| time_points = 50 | |
| time_series_data = { | |
| 'Memory Usage': np.random.randn(time_points).cumsum() + 100, | |
| 'Access Time': np.random.exponential(0.1, time_points), | |
| 'Hit Rate': np.random.beta(2, 1, time_points), | |
| 'Throughput': np.random.normal(1000, 100, time_points) | |
| } | |
| correlation_data = pd.DataFrame({ | |
| 'Memory Usage': np.random.randn(100), | |
| 'Access Time': np.random.randn(100), | |
| 'Hit Rate': np.random.randn(100), | |
| 'Throughput': np.random.randn(100), | |
| 'Latency': np.random.randn(100) | |
| }) | |
| correlation_data['Memory Usage'] += correlation_data['Access Time'] * 0.3 | |
| correlation_data['Hit Rate'] -= correlation_data['Latency'] * 0.2 | |
| relationships = { | |
| 'Sequential': ['Associative'], | |
| 'Associative': ['Content-Addr', 'LRU'], | |
| 'Content-Addr': ['Neural'], | |
| 'LRU': ['Hierarchical'], | |
| 'Neural': ['Compressed'], | |
| 'Compressed': ['Hierarchical'], | |
| 'Hierarchical': [] | |
| } | |
| radar_data = { | |
| 'Sequential': [0.8, 0.6, 0.7, 0.5, 0.9], | |
| 'Associative': [0.9, 0.8, 0.8, 0.7, 0.6], | |
| 'Neural': [0.7, 0.9, 0.9, 0.8, 0.7], | |
| 'Hierarchical': [0.9, 0.9, 0.9, 0.9, 0.8] | |
| } | |
| data = { | |
| 'memory_systems': {}, | |
| 'performance_matrix': performance_matrix, | |
| 'systems': systems, | |
| 'metrics': metrics, | |
| 'relationships': relationships, | |
| 'correlation_data': correlation_data, | |
| 'time_series': time_series_data, | |
| 'radar_data': radar_data, | |
| 'categories': ['Speed', 'Efficiency', 'Accuracy', 'Scalability', 'Reliability'] | |
| } | |
| config = VisualizationConfig() | |
| visualizer = ComprehensiveVisualizer(config) | |
| print("๐จ Creating visualizations...") | |
| figures = visualizer.create_comprehensive_dashboard(data) | |
| print("๐พ Saving visualizations...") | |
| visualizer.save_visualizations(figures, "advanced_visualizations") | |
| print("๐ Creating interactive report...") | |
| report_html = visualizer.create_interactive_report(figures) | |
| with open("advanced_visualizations/interactive_report.html", "w") as f: | |
| f.write(report_html) | |
| print("โ Advanced visualization demo completed!") | |
| print(f"๐ Visualizations saved to: advanced_visualizations/") | |
| print(f"๐ Interactive report: advanced_visualizations/interactive_report.html") | |
| return figures | |
| if __name__ == "__main__": | |
| run_advanced_visualization_demo() | |
Xet Storage Details
- Size:
- 23.4 kB
- Xet hash:
- 397fa2e5396d6e632a7aa957be6f08e0e3c2e9c752eeeeff51d44d1635e9f034
ยท
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.