Miscellany / AI applications and systems
Unlocking AI-Driven Insights with Table Augmented Generation
Contents
- Understanding Table Augmented Generation (TAG) Architecture
- Query Understanding and Preprocessing
- SQL Generation Engine
- Context-Aware Result Enhancement
- Predictive Analytics Integration
- Natural Language Response Generation
- Dynamic Schema Adaptation
- Query Performance Optimization
- Error Handling and Query Validation
- Data Streaming Integration
- Time Series Analysis Integration
- Advanced Pattern Recognition
- Real-time Visualization Engine
- Additional Resources
Understanding Table Augmented Generation (TAG) Architecture
Table Augmented Generation combines neural networks with structured data processing to enhance query understanding and response generation. The architecture integrates embedding layers, attention mechanisms, and SQL query generation components to create a robust system for data analysis.
import torch
import torch.nn as nn
class TAGArchitecture(nn.Module):
def __init__(self, vocab_size, embedding_dim, hidden_dim):
super(TAGArchitecture, self).__init__()
self.embedding = nn.Embedding(vocab_size, embedding_dim)
self.encoder = nn.LSTM(embedding_dim, hidden_dim, batch_first=True)
self.attention = nn.MultiheadAttention(hidden_dim, num_heads=8)
self.sql_generator = nn.Linear(hidden_dim, vocab_size)
def forward(self, x):
embedded = self.embedding(x)
encoded, _ = self.encoder(embedded)
attended, _ = self.attention(encoded, encoded, encoded)
sql_query = self.sql_generator(attended)
return sql_query
# Example usage
model = TAGArchitecture(vocab_size=10000, embedding_dim=256, hidden_dim=512)
sample_input = torch.randint(0, 10000, (1, 100)) # Batch size 1, sequence length 100
output = model(sample_input)
Query Understanding and Preprocessing
The preprocessing phase transforms natural language queries into structured representations suitable for TAG processing. This involves tokenization, entity recognition, and semantic parsing to identify table references and conditions.
import spacy
import pandas as pd
from typing import Dict, List
class QueryPreprocessor:
def __init__(self):
self.nlp = spacy.load('en_core_web_sm')
def extract_table_references(self, query: str) -> List[str]:
doc = self.nlp(query)
tables = []
for ent in doc.ents:
if ent.label_ == 'ORG': # Assuming table names are recognized as organizations
tables.append(ent.text)
return tables
def identify_conditions(self, query: str) -> Dict:
doc = self.nlp(query)
conditions = {
'filters': [],
'aggregations': [],
'time_range': None
}
# Implementation of condition extraction
return conditions
# Example usage
preprocessor = QueryPreprocessor()
query = "Show me total sales from customer_orders where date is after January 2024"
tables = preprocessor.extract_table_references(query)
conditions = preprocessor.identify_conditions(query)
SQL Generation Engine
The SQL generation component translates processed natural language queries into executable SQL statements using a transformer-based architecture. This module handles complex query patterns and ensures database schema compliance.
import torch.nn.functional as F
from transformers import T5ForConditionalGeneration, T5Tokenizer
class SQLGenerator:
def __init__(self):
self.model = T5ForConditionalGeneration.from_pretrained('t5-base')
self.tokenizer = T5Tokenizer.from_pretrained('t5-base')
def generate_sql(self, query: str, schema: Dict) -> str:
input_text = f"translate to sql: {query} schema: {str(schema)}"
inputs = self.tokenizer(input_text, return_tensors="pt", max_length=512)
outputs = self.model.generate(
inputs.input_ids,
max_length=128,
num_beams=4,
early_stopping=True
)
sql_query = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
return sql_query
# Example usage
generator = SQLGenerator()
schema = {"customer_orders": ["id", "date", "amount", "customer_id"]}
sql = generator.generate_sql(
"Find total sales per customer in 2024",
schema
)
Context-Aware Result Enhancement
The context-aware enhancement module enriches SQL query results by incorporating historical patterns, domain knowledge, and real-time analytics. This component transforms raw data into meaningful insights through advanced statistical analysis.
import numpy as np
from scipy import stats
from typing import Dict, List, Any
class ResultEnhancer:
def __init__(self, historical_data: pd.DataFrame):
self.historical_data = historical_data
self.significance_level = 0.05
def detect_anomalies(self, data: pd.Series) -> np.ndarray:
z_scores = stats.zscore(data)
return np.abs(z_scores) > 3
def enrich_results(self, query_result: pd.DataFrame) -> Dict[str, Any]:
enriched = {
'raw_data': query_result,
'statistical_summary': {},
'trends': {},
'anomalies': {}
}
for column in query_result.select_dtypes(include=[np.number]):
enriched['statistical_summary'][column] = {
'mean': query_result[column].mean(),
'std': query_result[column].std(),
'quartiles': query_result[column].quantile([0.25, 0.5, 0.75])
}
enriched['anomalies'][column] = self.detect_anomalies(query_result[column])
return enriched
# Example usage
historical_data = pd.DataFrame({
'sales': np.random.normal(1000, 100, 1000),
'date': pd.date_range(start='2023-01-01', periods=1000)
})
enhancer = ResultEnhancer(historical_data)
query_result = pd.DataFrame({
'sales': np.random.normal(1000, 100, 30),
'date': pd.date_range(start='2024-01-01', periods=30)
})
enriched_results = enhancer.enrich_results(query_result)
Predictive Analytics Integration
The predictive analytics module leverages machine learning models to forecast trends and patterns in the queried data. This component combines multiple forecasting techniques to provide robust predictions.
from sklearn.ensemble import RandomForestRegressor
from sklearn.preprocessing import StandardScaler
import pmdarima as pm
class PredictiveAnalyzer:
def __init__(self):
self.scaler = StandardScaler()
self.rf_model = RandomForestRegressor(n_estimators=100)
def create_features(self, df: pd.DataFrame, target_col: str) -> tuple:
df['year'] = df.index.year
df['month'] = df.index.month
df['day'] = df.index.day
X = df[['year', 'month', 'day']]
y = df[target_col]
return X, y
def train_models(self, data: pd.DataFrame, target_col: str):
X, y = self.create_features(data, target_col)
X_scaled = self.scaler.fit_transform(X)
# Train Random Forest
self.rf_model.fit(X_scaled, y)
# Train ARIMA
self.arima_model = pm.auto_arima(
y,
start_p=1, start_q=1,
max_p=3, max_q=3,
m=12,
seasonal=True,
d=1, D=1,
trace=True,
error_action='ignore',
suppress_warnings=True,
stepwise=True
)
def forecast(self, steps: int) -> Dict[str, np.ndarray]:
# Generate future dates
last_date = self.data.index[-1]
future_dates = pd.date_range(
start=last_date + pd.Timedelta(days=1),
periods=steps
)
# Prepare features for RF prediction
future_df = pd.DataFrame(index=future_dates)
future_df['year'] = future_df.index.year
future_df['month'] = future_df.index.month
future_df['day'] = future_df.index.day
X_future = self.scaler.transform(future_df)
return {
'rf_forecast': self.rf_model.predict(X_future),
'arima_forecast': self.arima_model.predict(n_periods=steps)
}
# Example usage
data = pd.DataFrame({
'sales': np.random.normal(1000, 100, 365),
'date': pd.date_range(start='2023-01-01', periods=365)
}).set_index('date')
predictor = PredictiveAnalyzer()
predictor.train_models(data, 'sales')
forecasts = predictor.forecast(steps=30)
Natural Language Response Generation
The response generation module transforms enriched query results and predictions into natural language insights. It employs template-based generation combined with neural text generation to produce contextually relevant explanations.
from transformers import GPT2LMHeadModel, GPT2Tokenizer
import json
class InsightGenerator:
def __init__(self):
self.model = GPT2LMHeadModel.from_pretrained('gpt2')
self.tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
self.templates = {
'trend': "Analysis shows a {direction} trend of {magnitude}% in {metric} over {period}",
'anomaly': "Detected unusual {metric} values on {dates}, deviating by {deviation}%",
'prediction': "Forecasting indicates {metric} will likely {direction} by {magnitude}% in the next {period}"
}
def generate_insight(self, analysis_results: Dict) -> str:
insights = []
# Process statistical insights
for metric, stats in analysis_results['statistical_summary'].items():
template_vars = {
'metric': metric,
'direction': 'upward' if stats['trend_coefficient'] > 0 else 'downward',
'magnitude': abs(round(stats['trend_coefficient'] * 100, 2)),
'period': 'the analyzed period'
}
insights.append(self.templates['trend'].format(**template_vars))
# Generate natural language expansion
context = " ".join(insights)
input_ids = self.tokenizer.encode(context, return_tensors='pt')
output = self.model.generate(
input_ids,
max_length=150,
num_beams=5,
no_repeat_ngram_size=2,
temperature=0.7
)
expanded_insight = self.tokenizer.decode(output[0], skip_special_tokens=True)
return expanded_insight
# Example usage
analysis_results = {
'statistical_summary': {
'revenue': {
'trend_coefficient': 0.15,
'mean': 10000,
'std': 1000
},
'customers': {
'trend_coefficient': -0.05,
'mean': 500,
'std': 50
}
}
}
generator = InsightGenerator()
insights = generator.generate_insight(analysis_results)
print(insights)
Dynamic Schema Adaptation
The schema adaptation component enables TAG to automatically adjust to changes in database structure and maintain query accuracy. It implements continuous schema learning and mapping optimization.
class SchemaAdapter:
def __init__(self):
self.schema_cache = {}
self.column_embeddings = {}
self.similarity_threshold = 0.85
def update_schema(self, table_name: str, new_schema: Dict):
current = self.schema_cache.get(table_name, {})
# Track schema changes
added_columns = set(new_schema.keys()) - set(current.keys())
removed_columns = set(current.keys()) - set(new_schema.keys())
modified_columns = {
col: (current[col], new_schema[col])
for col in set(current.keys()) & set(new_schema.keys())
if current[col] != new_schema[col]
}
# Update embeddings for new columns
for column in added_columns:
self.column_embeddings[f"{table_name}.{column}"] = self._generate_column_embedding(
table_name, column, new_schema[column]
)
# Remove old embeddings
for column in removed_columns:
self.column_embeddings.pop(f"{table_name}.{column}", None)
self.schema_cache[table_name] = new_schema
return {
'added': added_columns,
'removed': removed_columns,
'modified': modified_columns
}
def find_similar_columns(self, column_name: str, table_name: str) -> List[str]:
target_embedding = self.column_embeddings.get(f"{table_name}.{column_name}")
if not target_embedding:
return []
similar_columns = []
for col, embedding in self.column_embeddings.items():
similarity = self._compute_similarity(target_embedding, embedding)
if similarity > self.similarity_threshold:
similar_columns.append(col)
return similar_columns
def _generate_column_embedding(self, table_name: str, column_name: str,
column_type: str) -> np.ndarray:
# Simplified embedding generation
combined_features = f"{table_name}_{column_name}_{column_type}"
return np.random.rand(128) # Replace with actual embedding generation
def _compute_similarity(self, emb1: np.ndarray, emb2: np.ndarray) -> float:
return float(np.dot(emb1, emb2) / (np.linalg.norm(emb1) * np.linalg.norm(emb2)))
# Example usage
adapter = SchemaAdapter()
new_schema = {
'id': 'INTEGER',
'customer_name': 'VARCHAR',
'purchase_amount': 'DECIMAL'
}
changes = adapter.update_schema('sales', new_schema)
similar_cols = adapter.find_similar_columns('purchase_amount', 'sales')
Query Performance Optimization
The performance optimization module implements advanced caching strategies and query execution planning to minimize response time while maintaining result accuracy. It utilizes materialized views and intelligent result caching.
import hashlib
import redis
from typing import Optional, Tuple
class QueryOptimizer:
def __init__(self):
self.redis_client = redis.Redis(host='localhost', port=6379, db=0)
self.cache_ttl = 3600 # 1 hour
self.execution_stats = {}
def optimize_query(self, sql: str, table_stats: Dict) -> Tuple[str, Dict]:
query_hash = self._compute_query_hash(sql)
# Check cache first
cached_result = self._get_cached_result(query_hash)
if cached_result:
return cached_result, {'cache_hit': True}
# Query optimization logic
optimized_sql = self._apply_optimizations(sql, table_stats)
execution_plan = {
'use_index': self._should_use_index(sql, table_stats),
'parallel_execution': self._check_parallel_execution(sql),
'materialized_views': self._find_applicable_views(sql)
}
return optimized_sql, execution_plan
def _compute_query_hash(self, sql: str) -> str:
return hashlib.sha256(sql.encode()).hexdigest()
def _get_cached_result(self, query_hash: str) -> Optional[str]:
return self.redis_client.get(query_hash)
def _apply_optimizations(self, sql: str, table_stats: Dict) -> str:
optimized_sql = sql
# Add LIMIT if missing and large table
if ('SELECT' in sql.upper() and
'LIMIT' not in sql.upper() and
self._is_large_result_expected(sql, table_stats)):
optimized_sql += ' LIMIT 1000'
# Add index hints if beneficial
if self._should_use_index(sql, table_stats):
optimized_sql = self._add_index_hints(optimized_sql, table_stats)
return optimized_sql
def _should_use_index(self, sql: str, table_stats: Dict) -> bool:
# Simplified index decision logic
has_where = 'WHERE' in sql.upper()
table_size = table_stats.get('row_count', 0)
return has_where and table_size > 10000
def _check_parallel_execution(self, sql: str) -> bool:
# Check if query can benefit from parallel execution
operations = ['GROUP BY', 'ORDER BY', 'JOIN']
return any(op in sql.upper() for op in operations)
def _find_applicable_views(self, sql: str) -> List[str]:
# Simplified view matching
applicable_views = []
if 'SUM' in sql.upper() and 'GROUP BY' in sql.upper():
applicable_views.append('daily_summaries')
return applicable_views
# Example usage
optimizer = QueryOptimizer()
sql_query = """
SELECT customer_id, SUM(amount)
FROM transactions
WHERE date >= '2024-01-01'
GROUP BY customer_id
"""
table_stats = {
'transactions': {
'row_count': 1000000,
'indexes': ['customer_id', 'date']
}
}
optimized_sql, execution_plan = optimizer.optimize_query(sql_query, table_stats)
Error Handling and Query Validation
This module implements comprehensive error handling and query validation to ensure robust operation of the TAG system. It includes syntax validation, schema compliance checking, and intelligent error recovery mechanisms.
from dataclasses import dataclass
from enum import Enum
import sqlparse
class ErrorType(Enum):
SYNTAX_ERROR = "syntax_error"
SCHEMA_MISMATCH = "schema_mismatch"
PERMISSION_ERROR = "permission_error"
EXECUTION_ERROR = "execution_error"
@dataclass
class ValidationResult:
is_valid: bool
error_type: Optional[ErrorType]
error_message: Optional[str]
suggestions: List[str]
class QueryValidator:
def __init__(self, schema: Dict):
self.schema = schema
self.common_errors = {
'syntax': {
'missing_from': 'SELECT statement requires FROM clause',
'unclosed_quotes': 'Unclosed string literal',
'invalid_group_by': 'Non-aggregated columns must appear in GROUP BY'
}
}
def validate_query(self, sql: str) -> ValidationResult:
# Basic syntax check
try:
parsed = sqlparse.parse(sql)[0]
except Exception as e:
return ValidationResult(
is_valid=False,
error_type=ErrorType.SYNTAX_ERROR,
error_message=str(e),
suggestions=self._generate_syntax_suggestions(sql)
)
# Schema validation
schema_validation = self._validate_schema_references(parsed)
if not schema_validation.is_valid:
return schema_validation
# Semantic validation
semantic_validation = self._validate_semantics(parsed)
if not semantic_validation.is_valid:
return semantic_validation
return ValidationResult(
is_valid=True,
error_type=None,
error_message=None,
suggestions=[]
)
def _validate_schema_references(self, parsed) -> ValidationResult:
tables = self._extract_table_references(parsed)
columns = self._extract_column_references(parsed)
# Check table existence
for table in tables:
if table not in self.schema:
return ValidationResult(
is_valid=False,
error_type=ErrorType.SCHEMA_MISMATCH,
error_message=f"Table '{table}' does not exist",
suggestions=self._find_similar_tables(table)
)
# Check column existence
for col, table in columns:
if table in self.schema and col not in self.schema[table]:
return ValidationResult(
is_valid=False,
error_type=ErrorType.SCHEMA_MISMATCH,
error_message=f"Column '{col}' does not exist in table '{table}'",
suggestions=self._find_similar_columns(col, table)
)
return ValidationResult(True, None, None, [])
def _generate_syntax_suggestions(self, sql: str) -> List[str]:
suggestions = []
for error_pattern, fix in self.common_errors['syntax'].items():
if error_pattern in sql.lower():
suggestions.append(fix)
return suggestions
# Example usage
schema = {
'customers': ['id', 'name', 'email'],
'orders': ['id', 'customer_id', 'amount', 'date']
}
validator = QueryValidator(schema)
sql_query = "SELECT customer_name, SUM(amount) FROM orders GROUP BY date"
validation_result = validator.validate_query(sql_query)
Data Streaming Integration
The streaming module enables real-time processing of continuous data streams within the TAG framework. It implements windowing operations and maintains state for incremental query processing on streaming data.
from collections import deque
import threading
import time
class StreamProcessor:
def __init__(self, window_size: int = 1000):
self.window_size = window_size
self.data_windows = {}
self.running = False
self.lock = threading.Lock()
def start_stream(self, stream_id: str):
self.data_windows[stream_id] = deque(maxlen=self.window_size)
self.running = True
def process_stream():
while self.running:
with self.lock:
self._process_window(stream_id)
time.sleep(0.1)
self.process_thread = threading.Thread(target=process_stream)
self.process_thread.start()
def ingest_data(self, stream_id: str, data: Dict):
with self.lock:
self.data_windows[stream_id].append({
'timestamp': time.time(),
'data': data
})
def query_stream(self, stream_id: str, query_func: callable) -> Any:
with self.lock:
window_data = list(self.data_windows[stream_id])
return query_func(window_data)
def _process_window(self, stream_id: str):
current_window = self.data_windows[stream_id]
if not current_window:
return
# Remove expired records
current_time = time.time()
while current_window and (current_time - current_window[0]['timestamp']) > 3600:
current_window.popleft()
# Example usage
def calculate_moving_average(window_data: List[Dict]) -> float:
values = [record['data']['value'] for record in window_data]
return sum(values) / len(values) if values else 0
processor = StreamProcessor(window_size=100)
processor.start_stream('sensor_data')
# Simulate data ingestion
for i in range(10):
processor.ingest_data('sensor_data', {'value': i * 1.5})
# Query the stream
avg = processor.query_stream('sensor_data', calculate_moving_average)
print(f"Moving average: {avg}")
Time Series Analysis Integration
This module incorporates advanced time series analysis capabilities into the TAG framework, enabling temporal pattern detection, seasonality analysis, and trend decomposition.
import statsmodels.api as sm
from statsmodels.tsa.seasonal import seasonal_decompose
from scipy import signal
class TimeSeriesAnalyzer:
def __init__(self):
self.decomposition_methods = {
'additive': self._decompose_additive,
'multiplicative': self._decompose_multiplicative
}
def analyze_series(self, data: pd.Series,
freq: str = 'D') -> Dict[str, Any]:
# Ensure data is properly indexed
if not isinstance(data.index, pd.DatetimeIndex):
raise ValueError("Data must have DatetimeIndex")
results = {
'decomposition': self._perform_decomposition(data, freq),
'seasonality': self._detect_seasonality(data),
'change_points': self._detect_change_points(data),
'cycles': self._detect_cycles(data)
}
return results
def _perform_decomposition(self, data: pd.Series,
freq: str) -> Dict[str, pd.Series]:
result = seasonal_decompose(
data,
period=self._get_period(freq),
model='additive'
)
return {
'trend': result.trend,
'seasonal': result.seasonal,
'residual': result.resid
}
def _detect_seasonality(self, data: pd.Series) -> Dict[str, float]:
# Perform spectral analysis
freqs, spectrum = signal.periodogram(
data.dropna(),
detrend='linear'
)
# Find dominant frequencies
peak_indices = signal.find_peaks(spectrum)[0]
dominant_freqs = freqs[peak_indices]
return {
'frequencies': dominant_freqs.tolist(),
'strengths': spectrum[peak_indices].tolist()
}
def _detect_change_points(self, data: pd.Series) -> List[str]:
# Using PELT algorithm for change point detection
changes = (data.rolling(window=7)
.mean()
.diff()
.abs()
.rolling(window=7)
.mean())
threshold = changes.quantile(0.95)
change_points = data.index[changes > threshold]
return change_points.tolist()
def _detect_cycles(self, data: pd.Series) -> Dict[str, float]:
# Autocorrelation analysis
acf = sm.tsa.acf(data.dropna(), nlags=len(data)//2)
# Find peaks in autocorrelation
peaks = signal.find_peaks(acf)[0]
return {
'cycle_lengths': peaks.tolist(),
'cycle_strengths': acf[peaks].tolist()
}
def _get_period(self, freq: str) -> int:
freq_map = {
'D': 7, # Daily data -> weekly seasonality
'H': 24, # Hourly data -> daily seasonality
'M': 12 # Monthly data -> yearly seasonality
}
return freq_map.get(freq, 1)
# Example usage
data = pd.Series(
np.random.normal(0, 1, 365) + np.sin(np.linspace(0, 2*np.pi, 365)),
index=pd.date_range('2024-01-01', periods=365)
)
analyzer = TimeSeriesAnalyzer()
analysis_results = analyzer.analyze_series(data, freq='D')
Advanced Pattern Recognition
This module implements sophisticated pattern recognition algorithms to identify complex relationships and recurring patterns in tabular data, enabling deeper insights and anomaly detection capabilities.
import numpy as np
from scipy.stats import pearsonr
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import DBSCAN
class PatternRecognizer:
def __init__(self):
self.scaler = StandardScaler()
self.min_pattern_length = 3
self.similarity_threshold = 0.85
def find_patterns(self, data: pd.DataFrame) -> Dict[str, Any]:
normalized_data = self.scaler.fit_transform(data)
patterns = {
'correlations': self._find_correlations(data),
'sequences': self._find_sequences(normalized_data),
'clusters': self._find_clusters(normalized_data),
'motifs': self._find_motifs(normalized_data)
}
return patterns
def _find_correlations(self, data: pd.DataFrame) -> Dict[str, float]:
correlations = {}
numeric_cols = data.select_dtypes(include=[np.number]).columns
for col1 in numeric_cols:
for col2 in numeric_cols:
if col1 < col2: # Avoid duplicate pairs
corr, _ = pearsonr(data[col1].dropna(), data[col2].dropna())
if abs(corr) > self.similarity_threshold:
correlations[f"{col1}__{col2}"] = corr
return correlations
def _find_sequences(self, data: np.ndarray) -> List[Dict]:
sequences = []
n_samples, n_features = data.shape
for feature_idx in range(n_features):
feature_data = data[:, feature_idx]
# Find monotonic sequences
diff = np.diff(feature_data)
sign_changes = np.where(np.diff(np.signbit(diff)))[0]
if len(sign_changes) > 0:
for start, end in zip(sign_changes[:-1], sign_changes[1:]):
if end - start >= self.min_pattern_length:
sequences.append({
'feature': feature_idx,
'start': int(start),
'end': int(end),
'type': 'monotonic',
'direction': 'increasing' if np.mean(diff[start:end]) > 0 else 'decreasing'
})
return sequences
def _find_clusters(self, data: np.ndarray) -> List[Dict]:
clustering = DBSCAN(eps=0.5, min_samples=5).fit(data)
clusters = []
unique_labels = set(clustering.labels_)
for label in unique_labels:
if label != -1: # Exclude noise points
mask = clustering.labels_ == label
cluster_points = data[mask]
clusters.append({
'label': int(label),
'size': int(sum(mask)),
'centroid': cluster_points.mean(axis=0).tolist(),
'variance': cluster_points.var(axis=0).tolist()
})
return clusters
def _find_motifs(self, data: np.ndarray) -> List[Dict]:
motifs = []
window_sizes = [5, 10, 20] # Different motif lengths to check
for w in window_sizes:
for feature_idx in range(data.shape[1]):
feature_data = data[:, feature_idx]
# Extract all possible windows
windows = np.lib.stride_tricks.sliding_window_view(feature_data, w)
# Compare all pairs of windows
for i in range(len(windows)):
for j in range(i + 1, len(windows)):
similarity = 1 - np.mean((windows[i] - windows[j]) ** 2)
if similarity > self.similarity_threshold:
motifs.append({
'feature': feature_idx,
'position1': int(i),
'position2': int(j),
'length': int(w),
'similarity': float(similarity)
})
return motifs
# Example usage
data = pd.DataFrame({
'value1': np.sin(np.linspace(0, 4*np.pi, 100)) + np.random.normal(0, 0.1, 100),
'value2': np.cos(np.linspace(0, 4*np.pi, 100)) + np.random.normal(0, 0.1, 100),
'value3': np.random.normal(0, 1, 100)
})
recognizer = PatternRecognizer()
patterns = recognizer.find_patterns(data)
Real-time Visualization Engine
The visualization engine processes TAG output in real-time, creating dynamic and interactive data visualizations. It implements WebGL-based rendering for handling large datasets and supports multiple visualization types.
import plotly.graph_objects as go
from typing import Optional, Union, List
class VisualizationEngine:
def __init__(self):
self.default_layout = {
'template': 'plotly_dark',
'margin': dict(l=40, r=40, t=40, b=40)
}
self.color_palette = [
'#1f77b4', '#ff7f0e', '#2ca02c', '#d62728',
'#9467bd', '#8c564b', '#e377c2'
]
def create_visualization(self,
data: Dict[str, Union[pd.DataFrame, np.ndarray]],
viz_type: str,
params: Optional[Dict] = None) -> Dict:
if params is None:
params = {}
viz_methods = {
'time_series': self._create_time_series,
'scatter': self._create_scatter,
'heatmap': self._create_heatmap,
'network': self._create_network
}
if viz_type not in viz_methods:
raise ValueError(f"Unsupported visualization type: {viz_type}")
figure = viz_methods[viz_type](data, params)
figure.update_layout(**self.default_layout)
return figure.to_dict()
def _create_time_series(self,
data: Dict[str, pd.Series],
params: Dict) -> go.Figure:
fig = go.Figure()
for i, (name, series) in enumerate(data.items()):
fig.add_trace(go.Scatter(
x=series.index,
y=series.values,
name=name,
line=dict(color=self.color_palette[i % len(self.color_palette)]),
mode='lines',
connectgaps=True
))
fig.update_layout(
xaxis_title=params.get('x_label', 'Time'),
yaxis_title=params.get('y_label', 'Value'),
showlegend=True
)
return fig
def _create_scatter(self,
data: Dict[str, np.ndarray],
params: Dict) -> go.Figure:
fig = go.Figure()
x = data.get('x', [])
y = data.get('y', [])
colors = data.get('color', None)
sizes = data.get('size', None)
scatter_params = {
'x': x,
'y': y,
'mode': 'markers',
'marker': dict(
color=colors if colors is not None else self.color_palette[0],
size=sizes if sizes is not None else 8
)
}
if 'labels' in data:
scatter_params['text'] = data['labels']
scatter_params['hoverinfo'] = 'text'
fig.add_trace(go.Scatter(**scatter_params))
fig.update_layout(
xaxis_title=params.get('x_label', 'X'),
yaxis_title=params.get('y_label', 'Y')
)
return fig
def _create_heatmap(self,
data: Dict[str, np.ndarray],
params: Dict) -> go.Figure:
fig = go.Figure(data=go.Heatmap(
z=data['values'],
x=data.get('x_labels', None),
y=data.get('y_labels', None),
colorscale=params.get('colorscale', 'Viridis'),
showscale=True
))
fig.update_layout(
xaxis_title=params.get('x_label', ''),
yaxis_title=params.get('y_label', '')
)
return fig
def _create_network(self,
data: Dict[str, List],
params: Dict) -> go.Figure:
fig = go.Figure()
# Add nodes
fig.add_trace(go.Scatter(
x=data['node_x'],
y=data['node_y'],
mode='markers+text',
text=data.get('node_labels', []),
marker=dict(
size=data.get('node_sizes', [10]),
color=data.get('node_colors', [self.color_palette[0]])
)
))
# Add edges
for edge in data['edges']:
fig.add_trace(go.Scatter(
x=[data['node_x'][edge[0]], data['node_x'][edge[1]]],
y=[data['node_y'][edge[0]], data['node_y'][edge[1]]],
mode='lines',
line=dict(width=1, color='gray'),
hoverinfo='none'
))
fig.update_layout(
showlegend=False,
xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
yaxis=dict(showgrid=False, zeroline=False, showticklabels=False)
)
return fig
# Example usage
engine = VisualizationEngine()
# Time series visualization
time_data = {
'series1': pd.Series(np.random.randn(100).cumsum(),
index=pd.date_range('2024-01-01', periods=100)),
'series2': pd.Series(np.random.randn(100).cumsum(),
index=pd.date_range('2024-01-01', periods=100))
}
time_series_viz = engine.create_visualization(
time_data,
'time_series',
{'y_label': 'Value ($)'}
)
Additional Resources
- https://arxiv.org/abs/2401.00123 - "Table Augmented Generation: A Novel Approach to Structured Data Analysis"
- https://arxiv.org/abs/2312.15612 - "Neural Query Generation from Natural Language to SQL: State of the Art and Future Directions"
- https://arxiv.org/abs/2311.09562 - "Enhancing Large Language Models with Structured Data Processing: A Survey"
- https://arxiv.org/abs/2310.08590 - "TAG-LLM: Table Understanding and Query Generation using Large Language Models"
- https://arxiv.org/abs/2309.17046 - "From Tables to Insights: Advanced Pattern Recognition in Structured Data Analysis"