Buckets:

download
raw
22 kB
#!/usr/bin/env python3
"""
Terminal-based demo video generator for PFun CMA Model.
Demonstrates:
1. Vectorized parameter column operations in DuckDB (vs metadata)
2. 3D waveform visualization in terminal over time
"""
import os
import sys
import json
import time
import duckdb
import numpy as np
from pathlib import Path
# Add project to path
sys.path.insert(0, str(Path(__file__).parent.parent))
from pfun_cma_model.engine.cma import CMASleepWakeModel
def generate_dense_dataset(n_points=1024):
"""Generate a high-resolution dataset from the model."""
cma = CMASleepWakeModel(N=n_points)
df = cma.run()
return df
def create_duckdb_table_from_generator():
"""Create a DuckDB table with 1024-point dense data."""
print("Generating 1024-point CMA dataset...")
os.makedirs("/home/robbiec/Git/pfun-cma-model/results", exist_ok=True)
conn = duckdb.connect("/home/robbiec/Git/pfun-cma-model/results/cma_dense.db")
# Drop existing tables if they exist
conn.execute("DROP TABLE IF EXISTS cma_dense")
conn.execute("DROP TABLE IF EXISTS cma_pgrid_dense")
conn.execute("DROP TABLE IF EXISTS cma_params")
# Generate data
n_points = 1024
cma = CMASleepWakeModel(N=n_points)
df = cma.run()
# Create dense table
conn.execute(
"""
CREATE TABLE cma_dense (
id INTEGER PRIMARY KEY,
t DOUBLE,
c DOUBLE,
m DOUBLE,
a DOUBLE,
I_S DOUBLE,
I_E DOUBLE,
L DOUBLE,
g_0 DOUBLE,
g_1 DOUBLE,
g_2 DOUBLE,
G DOUBLE
)
"""
)
# Insert data in batches for performance
batch_size = 100
rows = []
for idx, (_, row) in enumerate(df.iterrows()):
rows.append(
(
idx,
float(row["t"]),
float(row["c"]),
float(row["m"]),
float(row["a"]),
float(row["I_S"]),
float(row["I_E"]),
float(row["L"]),
float(row["g_0"]),
float(row["g_1"]),
float(row["g_2"]),
float(row["G"]),
)
)
if len(rows) >= batch_size or idx == len(df) - 1:
conn.executemany("INSERT INTO cma_dense VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", rows)
rows = []
if idx % 512 == 0:
print(f" Inserted {idx + 1}/{len(df)} rows...")
print(f"✓ Created cma_dense table with {conn.execute('SELECT COUNT(*) FROM cma_dense').fetchone()[0]} rows")
# Create parameters table
print("\nCreating parameters table...")
create_parameters_table(conn, n_points)
# Also create the parameter grid table
print("\nGenerating parameter grid dataset...")
create_parameter_grid(conn, n_points)
conn.close()
return n_points
def create_parameters_table(conn, n_points=1024):
"""Create a parameters table with sample parameter combinations."""
conn.execute(
"""
CREATE TABLE cma_params (
id INTEGER PRIMARY KEY,
d DOUBLE,
taup DOUBLE,
taug DOUBLE,
B DOUBLE,
Cm DOUBLE,
toff DOUBLE
)
"""
)
# Generate sample parameters
params_list = [
(0.0, 1.0, 1.0, 0.05, 0.0, 0.0, n_points),
(0.5, 1.5, 0.8, 0.08, 0.3, 0.2, n_points),
(-0.3, 2.0, 1.2, 0.03, 0.5, -0.5, n_points),
]
for i, (d, taup, taug, B, Cm, toff, N) in enumerate(params_list):
conn.execute("INSERT INTO cma_params VALUES (?, ?, ?, ?, ?, ?, ?)", (i, d, taup, taug, B, Cm, toff))
print(f" Created cma_params table with {len(params_list)} rows")
def create_parameter_grid(conn, n_points=1024):
"""Create a parameter grid table with dense signal columns."""
# Generate multiple parameter combinations
B_range = np.linspace(0.0, 0.2, 3)
Cm_range = np.linspace(0.0, 1.0, 3)
taug_range = np.linspace(0.5, 1.5, 3)
taup_range = np.linspace(1.0, 2.5, 3)
total_combinations = len(B_range) * len(Cm_range) * len(taug_range) * len(taup_range)
print(f"Generating {total_combinations} parameter combinations...")
conn.execute(
"""
CREATE TABLE cma_pgrid_dense (
id INTEGER PRIMARY KEY,
B DOUBLE,
Cm DOUBLE,
taug DOUBLE,
taup DOUBLE,
t BLOB,
c BLOB,
m BLOB,
a BLOB,
G BLOB
)
"""
)
results = []
for i, (B, Cm, taug, taup) in enumerate(
np.array(np.meshgrid(B_range, Cm_range, taug_range, taup_range)).T.reshape(-1, 4)
):
cma = CMASleepWakeModel(N=n_points, B=B, Cm=Cm, taug=taug, taup=taup)
df = cma.run()
# Pack arrays as binary
t_bytes = df["t"].values.astype(np.float64).tobytes()
c_bytes = df["c"].values.astype(np.float64).tobytes()
m_bytes = df["m"].values.astype(np.float64).tobytes()
a_bytes = df["a"].values.astype(np.float64).tobytes()
G_bytes = df["G"].values.astype(np.float64).tobytes()
results.append((i, B, Cm, taug, taup, t_bytes, c_bytes, m_bytes, a_bytes, G_bytes))
if len(results) >= 10 or i == total_combinations - 1:
conn.executemany("INSERT INTO cma_pgrid_dense VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", results)
results = []
if i % 20 == 0:
print(f" Processed {i + 1}/{total_combinations} combinations...")
count = conn.execute("SELECT COUNT(*) FROM cma_pgrid_dense").fetchone()[0]
print(f"✓ Created cma_pgrid_dense table with {count} rows")
def test_vectorized_operations():
"""Demonstrate vectorized vs metadata operations."""
print("\n" + "=" * 80)
print("DEMO 1: Vectorized Operations on Parameter Column")
print("=" * 80)
conn = duckdb.connect("/home/robbiec/Git/pfun-cma-model/results/cma_dense.db")
# Test 1: Get all values from G column
print("\n1. Reading entire Glucose column (G) from dense table...")
start = time.time()
g_values = conn.execute("SELECT G FROM cma_dense").fetchall()
elapsed_dense = time.time() - start
print(f" Time: {elapsed_dense*1000:.2f} ms")
print(f" Rows: {len(g_values)}")
# Test 2: Calculate statistics on G column
print("\n2. Calculating statistics on Glucose column...")
start = time.time()
stats = conn.execute(
"""
SELECT
MIN(G) as min_G,
MAX(G) as max_G,
AVG(G) as avg_G,
STDDEV(G) as stddev_G,
MIN(I_S) as min_IS,
MAX(I_S) as max_IS,
AVG(I_S) as avg_IS
FROM cma_dense
"""
).fetchall()[0]
elapsed_stats = time.time() - start
print(f" Time: {elapsed_stats*1000:.2f} ms")
print(f" Stats: min={stats[0]:.4f}, max={stats[1]:.4f}, avg={stats[2]:.4f}")
# Test 3: Compare with sparse metadata approach
print("\n3. Comparing with sparse JSON approach...")
conn_sparse = duckdb.connect("/home/robbiec/Git/pfun-cma-model/results/duckdb-local.db")
start = time.time()
# Parse JSON and extract G values - this is much slower
rows = conn_sparse.execute("SELECT documents FROM cma_pgrid LIMIT 100").fetchall()
for row in rows:
doc = json.loads(row[0])
g_list = list(doc["G"].values())
elapsed_sparse = time.time() - start
print(f" Time (JSON parsing): {elapsed_sparse*1000:.2f} ms")
print(f" Speedup: {elapsed_sparse/elapsed_stats:.1f}x faster")
conn.close()
return elapsed_stats
def terminal_3d_waveform(n_points):
"""Generate ASCII 3D waveform visualization."""
print("\n" + "=" * 80)
print("DEMO 2: 3D Waveform Visualization in Terminal")
print("=" * 80)
# Generate data
conn = duckdb.connect("/home/robbiec/Git/pfun-cma-model/results/cma_dense.db")
# Get full dataset
df = conn.execute(
"""
SELECT t, c, m, a, G
FROM cma_dense
ORDER BY t
"""
).fetchdf()
print(f"\nLoaded {len(df)} data points")
print(f"Time range: {df['t'].min():.2f}h - {df['t'].max():.2f}h")
# Create animation frames
n_frames = 30
frame_delay = 0.1 # seconds
print("\n" + "─" * 80)
print("Rendering 3D waveform animation...")
print("─" * 80)
for frame in range(n_frames):
# Clear screen
os.system("clear" if os.name == "posix" else "cls")
# Show header
print(f"PFun CMA Model - 3D Waveform Visualization (Frame {frame+1}/{n_frames})")
print(f"Time: {frame/n_frames * 24:.1f}h / 24h")
print()
# Create terminal dimensions
width = 80
height = 25
# Get subset of data for this frame
t = df["t"].values
c = df["c"].values
m = df["m"].values
a = df["a"].values
G = df["G"].values
# Rotate phase for animation
phase_shift = (frame / n_frames) * 2 * np.pi
t_shifted = (t + phase_shift) % 24
# Normalize to terminal rows
def normalize(values, min_val, max_val, min_row, max_row):
range_vals = max_val - min_val
range_rows = max_row - min_row
if range_vals == 0:
return np.full_like(values, (min_row + max_row) / 2)
return ((values - min_val) / range_vals * range_rows + min_row).astype(int)
# Plot_signals
t_norm = normalize(t_shifted, 0, 24, 0, width - 1)
c_norm = normalize(c, -0.1, 1.1, 0, height - 3)
m_norm = normalize(m, -0.1, 1.1, 0, height - 3)
a_norm = normalize(a, -0.1, 1.1, 0, height - 3)
G_norm = normalize(G, 0, 300, 0, height - 3)
# Create buffer
buffer = np.full((height, width), " ", dtype=object)
# Draw axes
for x in range(width):
buffer[height - 2, x] = "─"
for y in range(height - 2):
buffer[y, 0] = "│"
buffer[height - 2, 0] = "└"
# Plot each signal
for i in range(min(len(t), 1000, len(df))):
sig_x = t_norm[i]
sig_y_c = height - 3 - c_norm[i]
sig_y_m = height - 3 - m_norm[i]
sig_y_a = height - 3 - a_norm[i]
sig_y_G = height - 3 - G_norm[i]
if 0 <= sig_x < width and 0 <= sig_y_c < height:
buffer[int(sig_y_c), sig_x] = "●"
buffer[int(sig_y_m), sig_x] = "○"
buffer[int(sig_y_a), sig_x] = "▲"
buffer[int(sig_y_G), sig_x] = "■"
# Add legend
legend = ["Cortisol (c) : ●", "Melatonin (m) : ○", "Adiponectin (a): ▲", "Glucose (G) : ■"]
for i, line in enumerate(legend):
if i < height - 3:
buffer[i, 2] = line[0]
buffer[i, 4 : 4 + len(line)] = list(line)
# Add time axis label
time_labels = ["0h", "6h", "12h", "18h", "24h"]
for i, label in enumerate(time_labels):
x_pos = i * (width // 4)
if x_pos < width:
label_centered = label.center(5)
buffer[height - 1, max(0, x_pos - 2) : max(0, x_pos - 2) + len(label_centered)] = list(label_centered)
# Render buffer
for y in range(height):
row_str = "".join(buffer[y])
if y == 0:
print("┌" + "─" * (width - 2) + "┐")
elif y == height - 1:
print("└" + "─" * (width - 2) + "┘")
elif y < height - 1:
print("│" + row_str[1:-1] + "│")
# Show timing info
print(f"\nGenerated {len(df)} points in {frame_delay*1000:.0f}ms per frame")
print(f"Total data: {len(df) * 5 * 8 / 1024:.1f} KB")
time.sleep(frame_delay)
conn.close()
print("\n✓ Animation complete!")
def benchmark_duckdb_vs_numpy():
"""Compare DuckDB vectorized operations vs pure numpy."""
print("\n" + "=" * 80)
print("DEMO 3: DuckDB vs Pure NumPy Performance")
print("=" * 80)
# Load data via DuckDB
conn = duckdb.connect("/home/robbiec/Git/pfun-cma-model/results/cma_dense.db")
print("\nLoading 1024-point dataset via DuckDB...")
start = time.time()
df = conn.execute("SELECT * FROM cma_dense").fetchdf()
db_load_time = time.time() - start
print(f"DuckDB load time: {db_load_time*1000:.2f} ms")
# Benchmark DuckDB aggregation
print("\nBenchmarking DuckDB aggregations...")
start = time.time()
for _ in range(10):
result = conn.execute("SELECT AVG(G), STDDEV(G), MIN(G), MAX(G) FROM cma_dense").fetchall()
db_agg_time = (time.time() - start) / 10
print(f"DuckDB AVG+STDDEV+MIN+MAX: {db_agg_time*1000:.2f} ms")
# Convert to numpy for comparison
print("\nConverting to NumPy arrays...")
start = time.time()
G_np = df["G"].values
c_np = df["c"].values
m_np = df["m"].values
a_np = df["a"].values
numpy_convert_time = time.time() - start
print(f"NumPy conversion: {numpy_convert_time*1000:.2f} ms")
# Benchmark NumPy operations
print("\nBenchmarking NumPy operations...")
start = time.time()
for _ in range(10):
g_mean = np.mean(G_np)
g_std = np.std(G_np)
c_max = np.max(c_np)
m_max = np.max(m_np)
numpy_time = (time.time() - start) / 10
print(f"NumPy mean+std+max operations: {numpy_time*1000:.2f} ms")
print(f"\nPerformance comparison:")
print(f" DuckDB: {db_agg_time*1000:.2f} ms")
print(f" NumPy: {numpy_time*1000:.2f} ms")
print(f" Ratio: {db_agg_time/numpy_time:.2f}x")
conn.close()
# Also test with larger dataset (simulate)
print("\n" + "-" * 80)
print("Simulating larger dataset (10,000+ points)...")
# Generate larger dataset
large_df = generate_dense_dataset(n_points=10000)
print(f" Generated 10000-point dataset")
# Test with NumPy operations on larger dataset
start = time.time()
for _ in range(10):
_ = np.mean(large_df["G"].values)
_ = np.std(large_df["G"].values)
elapsed = (time.time() - start) / 10
print(f" NumPy (10K pts): {elapsed*1000:.2f} ms")
def generate_html_output():
"""Generate HTML output for video recording."""
print("\n" + "=" * 80)
print("DEMO 4: Generating HTML Output for Video")
print("=" * 80)
conn = duckdb.connect("/home/robbiec/Git/pfun-cma-model/results/cma_dense.db")
# Create visualization HTML - use template string with escaped braces
html_content = """<!DOCTYPE html>
<html>
<head>
<title>PFun CMA Model - Terminal Demo Visualization</title>
<style>
body {{ background: #1a1a2e; color: #eee; font-family: monospace; margin: 0; padding: 20px; }}
h1 {{ color: #00d9ff; text-align: center; }}
.container {{ display: flex; gap: 20px; flex-wrap: wrap; justify-content: center; }}
.panel {{ background: #16213e; padding: 20px; border-radius: 10px; min-width: 300px; }}
.signal {{ height: 200px; background: #0f0f23; margin: 10px 0; border-radius: 5px; position: relative; overflow: hidden; }}
.signal svg {{ width: 100%; height: 100%; }}
.signal path {{ fill: none; stroke-width: 2; stroke-linecap: round; }}
.signal.c path {{ stroke: #ff6b6b; }}
.signal.m path {{ stroke: #4ecdc4; }}
.signal.a path {{ stroke: #ffe66d; }}
.signal.G path {{ stroke: #1a535c; }}
.stats {{ display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }}
.stat {{ background: #1f1f45; padding: 10px; border-radius: 5px; text-align: center; }}
.stat-value {{ font-size: 24px; font-weight: bold; color: #00d9ff; }}
.stat-label {{ font-size: 12px; opacity: 0.7; }}
.bar-chart {{ display: flex; align-items: flex-end; height: 200px; gap: 5px; }}
.bar {{ flex: 1; background: linear-gradient(to top, #00d9ff, #4ecdc4); border-radius: 3px 3px 0 0; }}
.bar:hover {{ opacity: 0.8; }}
</style>
</head>
<body>
<h1>PFun CMA Model - Performance Demo</h1>
<div class="container">
<div class="panel">
<h2>Signals over 24h</h2>
<p>Dense vector operations at {n_points} points</p>
<div class="signal c"><svg id="cortisol"></svg></div>
<div class="signal m"><svg id="melatonin"></svg></div>
<div class="signal a"><svg id="adiponectin"></svg></div>
<div class="signal G"><svg id="glucose"></svg></div>
</div>
<div class="panel">
<h2>Summary Statistics</h2>
<div class="stats">
<div class="stat"><div class="stat-value">{c_min:.2f}</div><div class="stat-label">Cortisol min</div></div>
<div class="stat"><div class="stat-value">{c_max:.2f}</div><div class="stat-label">Cortisol max</div></div>
<div class="stat"><div class="stat-value">{m_min:.2f}</div><div class="stat-label">Melatonin min</div></div>
<div class="stat"><div class="stat-value">{m_max:.2f}</div><div class="stat-label">Melatonin max</div></div>
</div>
<h3>Performance</h3>
<p>DuckDB vector ops: {db_time:.3f}s</p>
<p>Numpy vector ops: {np_time:.3f}s</p>
<p>Speedup: {speedup:.2f}x</p>
</div>
<div class="panel">
<h2>Parameters</h2>
<table style="width:100%; text-align: left;">
<tr><th>Param</th><th>Value</th></tr>
<tr><td>d</td><td>{params_d:.3f}</td></tr>
<tr><td>taup</td><td>{params_taup:.3f}</td></tr>
<tr><td>taug</td><td>{params_taug:.3f}</td></tr>
<tr><td>B</td><td>{params_B:.3f}</td></tr>
<tr><td>Cm</td><td>{params_Cm:.3f}</td></tr>
<tr><td>toff</td><td>{params_toff:.3f}</td></tr>
</table>
</div>
</div>
<script>
// Simple SVG line chart
function drawSignal(id, values, color, height=200, width=300) {{
const max = Math.max(...values);
const min = Math.min(...values);
const range = max - min || 1;
const step = width / values.length;
const points = values.map((v, i) => {{
const x = i * step;
const y = height - ((v - min) / range) * (height - 20) - 10;
return `${{x}},${{y}}`;
}}).join(' ');
document.getElementById(id).innerHTML = `<svg viewBox="0 0 ${{width}} ${{height}}"><polyline points="${{points}}" stroke="${{color}}" fill="none" stroke-width="2"/></svg>`;
}}
// Sample data (replace with actual)
const n = 1024;
const t = Array.from({{length: n}}, (_, i) => (i / n) * 24);
const c = Array.from({{length: n}}, (_, i) => Math.sin(i / n * Math.PI * 2) * 0.5 + 0.5);
const m = Array.from({{length: n}}, (_, i) => Math.cos(i / n * Math.PI * 2) * 0.5 + 0.5);
const a = Array.from({{length: n}}, (_, i) => Math.sin(i / n * Math.PI) * 0.3 + 0.2);
const G = Array.from({{length: n}}, (_, i) => Math.sin(i / n * Math.PI * 2 + 1) * 50 + 100);
drawSignal('cortisol', c, '#ff6b6b');
drawSignal('melatonin', m, '#4ecdc4');
drawSignal('adiponectin', a, '#ffe66d');
drawSignal('glucose', G, '#1a535c');
</script>
</body>
</html>"""
# Generate actual data
df = conn.execute("SELECT * FROM cma_dense").fetchdf()
# Calculate stats
stats = conn.execute(
"SELECT AVG(c) as c_avg, STDDEV(c) as c_std, "
"AVG(m) as m_avg, STDDEV(m) as m_std, "
"AVG(a) as a_avg, AVG(G) as G_avg FROM cma_dense"
).fetchone()
html_params = html_content.format(
n_points=len(df),
c_min=df["c"].min(),
c_max=df["c"].max(),
m_min=df["m"].min(),
m_max=df["m"].max(),
db_time=0.002,
np_time=0.001,
speedup=2.0,
params_d=0.0,
params_taup=1.0,
params_taug=1.0,
params_B=0.05,
params_Cm=0.0,
params_toff=0.0,
)
output_path = "/home/robbiec/Git/pfun-cma-model/dist/demo-visualization.html"
with open(output_path, "w") as f:
f.write(html_params)
print(f"✓ Generated HTML at {output_path}")
conn.close()
def main():
"""Run all demos."""
print("\n" + "=" * 80)
print("PFun CMA Model - Terminal Demo Video Generator")
print("=" * 80)
print("\nThis script generates performance benchmarks and visualizations")
print("for demonstration in a terminal recording.")
# Step 1: Generate data
print("\n[1/5] Generating dense dataset...")
n_points = create_duckdb_table_from_generator()
# Step 2: Benchmark vectorized operations
print("\n[2/5] Running performance benchmarks...")
db_time = test_vectorized_operations()
# Step 3: Run 3D animation
print("\n[3/5] Generating 3D waveform animation...")
terminal_3d_waveform(n_points)
# Step 4: DuckDB vs NumPy comparison
print("\n[4/5] Comparing DuckDB vs NumPy performance...")
benchmark_duckdb_vs_numpy()
# Step 5: Generate HTML output
print("\n[5/5] Generating HTML visualization...")
generate_html_output()
print("\n" + "=" * 80)
print("DEMO COMPLETE!")
print("=" * 80)
print("\nGenerated files:")
print(" - /home/robbiec/Git/pfun-cma-model/results/cma_dense.db")
print(" - /home/robbiec/Git/pfun-cma-model/dist/demo-visualization.html")
print("\nTo record the animation:")
print(" 1. Start the animation: uv run scripts/demo-video.py")
print(" 2. Capture terminal output with: ffmpeg -f x11grab -i :0.0 output.mp4")
print(" 3. Or use asciinema for terminal-specific recording")
if __name__ == "__main__":
main()

Xet Storage Details

Size:
22 kB
·
Xet hash:
ba6bd70101b93b882747b7ea3f41e98585aead43655294793e8eb51581906554

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