Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,463 +1,1004 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
🔬 SCIENTIFIC INTEGRITY COMPLIANCE 🔬
|
| 7 |
-
- Uses ONLY real preprocessed CMT data from CSV files
|
| 8 |
-
- NO synthetic data generation
|
| 9 |
-
- NO interpolation or field reconstruction
|
| 10 |
-
- NO speculative similarity metrics
|
| 11 |
-
- Proper statistical hypothesis testing
|
| 12 |
-
- Mathematically grounded distance measures
|
| 13 |
"""
|
| 14 |
|
| 15 |
-
import warnings
|
| 16 |
import os
|
|
|
|
| 17 |
import numpy as np
|
| 18 |
import pandas as pd
|
| 19 |
import plotly.graph_objects as go
|
| 20 |
from plotly.subplots import make_subplots
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
import gradio as gr
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
warnings.filterwarnings("ignore", category=FutureWarning)
|
| 25 |
warnings.filterwarnings("ignore", category=UserWarning)
|
| 26 |
|
| 27 |
-
print("
|
| 28 |
|
| 29 |
# ---------------------------------------------------------------
|
| 30 |
-
#
|
| 31 |
# ---------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
HF_CSV_DOG = "cmt_dog_sound_analysis.csv"
|
| 33 |
HF_CSV_HUMAN = "cmt_human_speech_analysis.csv"
|
| 34 |
COLAB_CSV_DOG = "/content/cmt_dog_sound_analysis.csv"
|
| 35 |
COLAB_CSV_HUMAN = "/content/cmt_human_speech_analysis.csv"
|
| 36 |
|
| 37 |
-
# Determine
|
| 38 |
if os.path.exists(HF_CSV_DOG) and os.path.exists(HF_CSV_HUMAN):
|
| 39 |
CSV_DOG = HF_CSV_DOG
|
| 40 |
CSV_HUMAN = HF_CSV_HUMAN
|
| 41 |
-
print("
|
| 42 |
elif os.path.exists(COLAB_CSV_DOG) and os.path.exists(COLAB_CSV_HUMAN):
|
| 43 |
CSV_DOG = COLAB_CSV_DOG
|
| 44 |
CSV_HUMAN = COLAB_CSV_HUMAN
|
| 45 |
-
print("
|
| 46 |
else:
|
| 47 |
-
|
| 48 |
-
|
|
|
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
df_dog = pd.read_csv(CSV_DOG)
|
| 53 |
df_human = pd.read_csv(CSV_HUMAN)
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
# ---------------------------------------------------------------
|
| 63 |
-
#
|
| 64 |
# ---------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
try:
|
| 69 |
alpha_col = f"diag_alpha_{lens}"
|
| 70 |
srl_col = f"diag_srl_{lens}"
|
| 71 |
|
| 72 |
-
alpha_val = row.get(alpha_col,
|
| 73 |
-
srl_val = row.get(srl_col,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
if np.isnan(alpha_val) or np.isnan(srl_val):
|
| 76 |
-
return None
|
| 77 |
-
|
| 78 |
return {
|
| 79 |
-
"
|
| 80 |
-
"
|
| 81 |
-
"
|
| 82 |
-
"
|
| 83 |
-
"
|
|
|
|
|
|
|
| 84 |
}
|
|
|
|
| 85 |
except Exception as e:
|
| 86 |
-
print(f"Error extracting
|
| 87 |
return None
|
| 88 |
|
| 89 |
-
def
|
| 90 |
-
"""
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
(primary_data['alpha'] - neighbor_data['alpha'])**2 +
|
| 124 |
-
(primary_data['srl'] - neighbor_data['srl'])**2
|
| 125 |
-
)
|
| 126 |
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
neighbor_alpha_percentile = stats.percentileofscore(neighbor_alphas, neighbor_data['alpha'])
|
| 130 |
-
primary_srl_percentile = stats.percentileofscore(primary_srls, primary_data['srl'])
|
| 131 |
-
neighbor_srl_percentile = stats.percentileofscore(neighbor_srls, neighbor_data['srl'])
|
| 132 |
-
|
| 133 |
-
return {
|
| 134 |
-
"alpha_ttest_statistic": alpha_ttest.statistic,
|
| 135 |
-
"alpha_ttest_pvalue": alpha_ttest.pvalue,
|
| 136 |
-
"srl_ttest_statistic": srl_ttest.statistic,
|
| 137 |
-
"srl_ttest_pvalue": srl_ttest.pvalue,
|
| 138 |
-
"alpha_effect_size": alpha_effect_size,
|
| 139 |
-
"srl_effect_size": srl_effect_size,
|
| 140 |
-
"diagnostic_distance": diagnostic_distance,
|
| 141 |
-
"primary_alpha_percentile": primary_alpha_percentile,
|
| 142 |
-
"neighbor_alpha_percentile": neighbor_alpha_percentile,
|
| 143 |
-
"primary_srl_percentile": primary_srl_percentile,
|
| 144 |
-
"neighbor_srl_percentile": neighbor_srl_percentile,
|
| 145 |
-
"primary_population_size": len(primary_alphas),
|
| 146 |
-
"neighbor_population_size": len(neighbor_alphas)
|
| 147 |
-
}
|
| 148 |
-
|
| 149 |
-
def find_nearest_neighbor_scientific(selected_row, df_combined, lens):
|
| 150 |
-
"""Find nearest neighbor using only Euclidean distance in diagnostic space."""
|
| 151 |
-
selected_source = selected_row['source']
|
| 152 |
-
opposite_source = 'Human' if selected_source == 'Dog' else 'Dog'
|
| 153 |
|
| 154 |
-
|
| 155 |
-
|
| 156 |
|
| 157 |
-
|
|
|
|
| 158 |
|
| 159 |
-
|
| 160 |
-
|
| 161 |
|
| 162 |
-
|
| 163 |
-
|
|
|
|
| 164 |
|
| 165 |
-
|
| 166 |
-
|
| 167 |
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
(
|
| 172 |
-
|
|
|
|
| 173 |
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
-
|
| 179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
-
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
-
def
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
|
| 189 |
-
fig
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
"
|
| 195 |
-
|
| 196 |
-
|
| 197 |
)
|
| 198 |
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
), row=1, col=1)
|
| 211 |
|
| 212 |
-
#
|
| 213 |
-
fig.add_trace(go.
|
| 214 |
-
x=
|
| 215 |
-
|
| 216 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
), row=1, col=2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
|
| 219 |
-
fig.add_trace(go.
|
| 220 |
-
x=
|
| 221 |
-
mode='markers', marker=dict(size=
|
| 222 |
-
showlegend=False
|
| 223 |
), row=1, col=2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
|
| 225 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
fig.add_trace(go.Scatter(
|
| 227 |
-
x=
|
| 228 |
-
|
| 229 |
-
name=
|
| 230 |
-
)
|
| 231 |
-
|
| 232 |
fig.add_trace(go.Scatter(
|
| 233 |
-
x=
|
| 234 |
-
|
| 235 |
-
name=
|
| 236 |
-
)
|
| 237 |
|
| 238 |
-
# Distance visualization
|
| 239 |
-
fig.add_trace(go.Scatter(
|
| 240 |
-
x=[primary_data['alpha'], neighbor_data['alpha']],
|
| 241 |
-
y=[primary_data['srl'], neighbor_data['srl']],
|
| 242 |
-
mode='lines+markers',
|
| 243 |
-
line=dict(color='purple', width=3, dash='dash'),
|
| 244 |
-
marker=dict(size=10, color=['red', 'blue']),
|
| 245 |
-
name="Euclidean Distance", showlegend=False
|
| 246 |
-
), row=2, col=2)
|
| 247 |
-
|
| 248 |
-
# Update layout
|
| 249 |
fig.update_layout(
|
| 250 |
-
title=
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
|
|
|
| 254 |
)
|
| 255 |
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
|
| 266 |
return fig
|
| 267 |
|
| 268 |
-
def
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
)
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
# Get real CMT data
|
| 301 |
-
primary_cmt = get_real_cmt_diagnostics(primary_row, lens)
|
| 302 |
-
neighbor_cmt = get_real_cmt_diagnostics(neighbor_row, lens)
|
| 303 |
-
|
| 304 |
-
if not primary_cmt or not neighbor_cmt:
|
| 305 |
-
return (
|
| 306 |
-
go.Figure(layout={"title": "Invalid CMT data"}),
|
| 307 |
-
"Invalid CMT data",
|
| 308 |
-
"No analysis available",
|
| 309 |
-
"No statistics available"
|
| 310 |
-
)
|
| 311 |
-
|
| 312 |
-
# Create scientific visualization
|
| 313 |
-
diagnostic_fig = create_scientific_diagnostic_plot(primary_cmt, neighbor_cmt, lens)
|
| 314 |
-
|
| 315 |
-
# Calculate statistics
|
| 316 |
-
stats_results = calculate_statistical_significance(
|
| 317 |
-
primary_cmt, neighbor_cmt, df_combined, lens
|
| 318 |
-
)
|
| 319 |
-
|
| 320 |
-
# Build information panels
|
| 321 |
-
primary_info = f"""
|
| 322 |
-
<h4>📊 <b>Primary Sample</b></h4>
|
| 323 |
-
<div style="background: rgba(240,240,250,1); padding: 10px; border-radius: 8px; margin: 5px 0; color: black;">
|
| 324 |
-
<p><b>File:</b> {primary_cmt['filepath']}</p>
|
| 325 |
-
<p><b>Species:</b> {primary_cmt['source']}</p>
|
| 326 |
-
<p><b>Label:</b> {primary_cmt['label']}</p>
|
| 327 |
-
<p><b>CMT α ({lens}):</b> {primary_cmt['alpha']:.6f}</p>
|
| 328 |
-
<p><b>CMT SRL ({lens}):</b> {primary_cmt['srl']:.6f}</p>
|
| 329 |
-
</div>
|
| 330 |
-
"""
|
| 331 |
-
|
| 332 |
-
neighbor_info = f"""
|
| 333 |
-
<h4>🔗 <b>Nearest Neighbor</b></h4>
|
| 334 |
-
<div style="background: rgba(240,250,240,1); padding: 10px; border-radius: 8px; margin: 5px 0; color: black;">
|
| 335 |
-
<p><b>File:</b> {neighbor_cmt['filepath']}</p>
|
| 336 |
-
<p><b>Species:</b> {neighbor_cmt['source']}</p>
|
| 337 |
-
<p><b>Label:</b> {neighbor_cmt['label']}</p>
|
| 338 |
-
<p><b>CMT α ({lens}):</b> {neighbor_cmt['alpha']:.6f}</p>
|
| 339 |
-
<p><b>CMT SRL ({lens}):</b> {neighbor_cmt['srl']:.6f}</p>
|
| 340 |
-
<p><b>Distance:</b> {distance:.6f}</p>
|
| 341 |
-
</div>
|
| 342 |
-
"""
|
| 343 |
-
|
| 344 |
-
if 'error' not in stats_results:
|
| 345 |
-
stats_info = f"""
|
| 346 |
-
<h4>🔬 <b>Statistical Analysis</b></h4>
|
| 347 |
-
<div style="background: rgba(250,250,240,1); padding: 10px; border-radius: 8px; margin: 5px 0; color: black;">
|
| 348 |
-
<p><b>Alpha t-test:</b> t = {stats_results['alpha_ttest_statistic']:.4f}, p = {stats_results['alpha_ttest_pvalue']:.6f}</p>
|
| 349 |
-
<p><b>SRL t-test:</b> t = {stats_results['srl_ttest_statistic']:.4f}, p = {stats_results['srl_ttest_pvalue']:.6f}</p>
|
| 350 |
-
<p><b>Effect Sizes (Cohen's d):</b></p>
|
| 351 |
-
<p>• Alpha: {stats_results['alpha_effect_size']:.4f}</p>
|
| 352 |
-
<p>• SRL: {stats_results['srl_effect_size']:.4f}</p>
|
| 353 |
-
<p><b>Population Sizes:</b> {stats_results['primary_population_size']} vs {stats_results['neighbor_population_size']}</p>
|
| 354 |
-
<p><b>Statistical Significance:</b></p>
|
| 355 |
-
<p>• Alpha: {'Significant' if stats_results['alpha_ttest_pvalue'] < 0.05 else 'Not significant'}</p>
|
| 356 |
-
<p>• SRL: {'Significant' if stats_results['srl_ttest_pvalue'] < 0.05 else 'Not significant'}</p>
|
| 357 |
-
</div>
|
| 358 |
-
"""
|
| 359 |
-
else:
|
| 360 |
-
stats_info = f"<p>Statistical analysis failed: {stats_results['error']}</p>"
|
| 361 |
-
|
| 362 |
-
return diagnostic_fig, primary_info, neighbor_info, stats_info
|
| 363 |
-
|
| 364 |
-
except Exception as e:
|
| 365 |
-
error_msg = f"Analysis error: {str(e)}"
|
| 366 |
-
return (
|
| 367 |
-
go.Figure(layout={"title": error_msg}),
|
| 368 |
-
error_msg,
|
| 369 |
-
error_msg,
|
| 370 |
-
error_msg
|
| 371 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 372 |
|
| 373 |
# ---------------------------------------------------------------
|
| 374 |
# Gradio Interface
|
| 375 |
# ---------------------------------------------------------------
|
| 376 |
-
with gr.Blocks(theme=gr.themes.Soft(primary_hue="
|
| 377 |
gr.Markdown("""
|
| 378 |
-
#
|
| 379 |
-
*
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
-
|
| 386 |
-
-
|
| 387 |
-
-
|
| 388 |
-
- ✅ **Scientific hypothesis testing** with p-values and confidence measures
|
| 389 |
-
|
| 390 |
-
**What was REMOVED for scientific rigor:**
|
| 391 |
-
- ❌ Synthetic holographic field generation
|
| 392 |
-
- ❌ Cubic interpolation of non-existent data
|
| 393 |
-
- ❌ Speculative similarity metrics
|
| 394 |
-
- ❌ Confirmation bias in pattern detection
|
| 395 |
-
- ❌ Ungrounded "communication bridge" calculations
|
| 396 |
""")
|
| 397 |
|
| 398 |
-
with gr.
|
| 399 |
-
with gr.
|
| 400 |
-
gr.Markdown("
|
| 401 |
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 408 |
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 422 |
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
info="Nearest neighbor will be automatically found"
|
| 428 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 429 |
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
# Update file choices when species changes
|
| 442 |
-
def update_file_choices(species):
|
| 443 |
-
choices = df_combined[df_combined["source"] == species]["filepath"].tolist()
|
| 444 |
-
return gr.Dropdown(choices=choices, value=choices[0] if choices else "")
|
| 445 |
-
|
| 446 |
-
species_selection.change(
|
| 447 |
-
fn=update_file_choices,
|
| 448 |
-
inputs=[species_selection],
|
| 449 |
-
outputs=[primary_file_selection]
|
| 450 |
-
)
|
| 451 |
-
|
| 452 |
-
# Main analysis update
|
| 453 |
-
for input_component in [species_selection, primary_file_selection, lens_selection]:
|
| 454 |
-
input_component.change(
|
| 455 |
-
fn=update_scientific_analysis,
|
| 456 |
-
inputs=[species_selection, primary_file_selection, neighbor_file_selection, lens_selection],
|
| 457 |
-
outputs=[diagnostic_plot, primary_info_display, neighbor_info_display, stats_info_display]
|
| 458 |
-
)
|
| 459 |
|
| 460 |
-
print("
|
| 461 |
|
| 462 |
if __name__ == "__main__":
|
| 463 |
demo.launch(share=False, debug=False)
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
+
Enhanced CMT Holographic Visualization Suite with Scientific Integrity
|
| 4 |
+
Full-featured toolkit with mathematically rigorous implementations
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
"""
|
| 6 |
|
|
|
|
| 7 |
import os
|
| 8 |
+
import warnings
|
| 9 |
import numpy as np
|
| 10 |
import pandas as pd
|
| 11 |
import plotly.graph_objects as go
|
| 12 |
from plotly.subplots import make_subplots
|
| 13 |
+
|
| 14 |
+
# Handle UMAP import variations
|
| 15 |
+
try:
|
| 16 |
+
from umap import UMAP
|
| 17 |
+
except ImportError:
|
| 18 |
+
try:
|
| 19 |
+
from umap.umap_ import UMAP
|
| 20 |
+
except ImportError:
|
| 21 |
+
import umap.umap_ as umap_module
|
| 22 |
+
UMAP = umap_module.UMAP
|
| 23 |
+
|
| 24 |
+
from sklearn.cluster import KMeans
|
| 25 |
+
from scipy.stats import entropy as shannon_entropy
|
| 26 |
+
from scipy import special as sp_special
|
| 27 |
+
from scipy.interpolate import griddata
|
| 28 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 29 |
+
from scipy.spatial.distance import cdist
|
| 30 |
+
import soundfile as sf
|
| 31 |
import gradio as gr
|
| 32 |
|
| 33 |
+
# ================================================================
|
| 34 |
+
# Unified Communication Manifold Explorer & CMT Visualizer v5.0
|
| 35 |
+
# - Full feature restoration with scientific integrity
|
| 36 |
+
# - Mathematically rigorous implementations
|
| 37 |
+
# - All original tools and insights preserved
|
| 38 |
+
# ================================================================
|
| 39 |
+
|
| 40 |
warnings.filterwarnings("ignore", category=FutureWarning)
|
| 41 |
warnings.filterwarnings("ignore", category=UserWarning)
|
| 42 |
|
| 43 |
+
print("Initializing the Enhanced CMT Holography Explorer...")
|
| 44 |
|
| 45 |
# ---------------------------------------------------------------
|
| 46 |
+
# Data setup
|
| 47 |
# ---------------------------------------------------------------
|
| 48 |
+
BASE_DIR = os.path.abspath(os.getcwd())
|
| 49 |
+
DATA_DIR = os.path.join(BASE_DIR, "data")
|
| 50 |
+
DOG_DIR = os.path.join(DATA_DIR, "dog")
|
| 51 |
+
HUMAN_DIR = os.path.join(DATA_DIR, "human")
|
| 52 |
+
|
| 53 |
+
# Platform-aware paths
|
| 54 |
HF_CSV_DOG = "cmt_dog_sound_analysis.csv"
|
| 55 |
HF_CSV_HUMAN = "cmt_human_speech_analysis.csv"
|
| 56 |
COLAB_CSV_DOG = "/content/cmt_dog_sound_analysis.csv"
|
| 57 |
COLAB_CSV_HUMAN = "/content/cmt_human_speech_analysis.csv"
|
| 58 |
|
| 59 |
+
# Determine environment
|
| 60 |
if os.path.exists(HF_CSV_DOG) and os.path.exists(HF_CSV_HUMAN):
|
| 61 |
CSV_DOG = HF_CSV_DOG
|
| 62 |
CSV_HUMAN = HF_CSV_HUMAN
|
| 63 |
+
print("Using Hugging Face Spaces paths")
|
| 64 |
elif os.path.exists(COLAB_CSV_DOG) and os.path.exists(COLAB_CSV_HUMAN):
|
| 65 |
CSV_DOG = COLAB_CSV_DOG
|
| 66 |
CSV_HUMAN = COLAB_CSV_HUMAN
|
| 67 |
+
print("Using Google Colab paths")
|
| 68 |
else:
|
| 69 |
+
CSV_DOG = HF_CSV_DOG
|
| 70 |
+
CSV_HUMAN = HF_CSV_HUMAN
|
| 71 |
+
print("Falling back to local/dummy data paths")
|
| 72 |
|
| 73 |
+
# Audio paths
|
| 74 |
+
if os.path.exists("/content/drive/MyDrive/combined"):
|
| 75 |
+
DOG_AUDIO_BASE_PATH = '/content/drive/MyDrive/combined'
|
| 76 |
+
HUMAN_AUDIO_BASE_PATH = '/content/drive/MyDrive/human'
|
| 77 |
+
print("Using Google Drive audio paths")
|
| 78 |
+
elif os.path.exists("combined") and os.path.exists("human"):
|
| 79 |
+
DOG_AUDIO_BASE_PATH = 'combined'
|
| 80 |
+
HUMAN_AUDIO_BASE_PATH = 'human'
|
| 81 |
+
print("Using Hugging Face Spaces audio paths")
|
| 82 |
+
else:
|
| 83 |
+
DOG_AUDIO_BASE_PATH = DOG_DIR
|
| 84 |
+
HUMAN_AUDIO_BASE_PATH = HUMAN_DIR
|
| 85 |
+
print("Using local audio paths")
|
| 86 |
+
|
| 87 |
+
# ---------------------------------------------------------------
|
| 88 |
+
# Load datasets
|
| 89 |
+
# ---------------------------------------------------------------
|
| 90 |
+
if os.path.exists(CSV_DOG) and os.path.exists(CSV_HUMAN):
|
| 91 |
+
print(f"✅ Loading real data from CSVs")
|
| 92 |
df_dog = pd.read_csv(CSV_DOG)
|
| 93 |
df_human = pd.read_csv(CSV_HUMAN)
|
| 94 |
+
else:
|
| 95 |
+
print("⚠️ Generating dummy data for demo")
|
| 96 |
+
# Dummy data generation
|
| 97 |
+
n_dummy = 50
|
| 98 |
+
rng = np.random.default_rng(42)
|
| 99 |
+
|
| 100 |
+
dog_labels = ["bark", "growl", "whine", "pant"] * (n_dummy // 4 + 1)
|
| 101 |
+
human_labels = ["speech", "laugh", "cry", "shout"] * (n_dummy // 4 + 1)
|
| 102 |
+
|
| 103 |
+
df_dog = pd.DataFrame({
|
| 104 |
+
"filepath": [f"dog_{i}.wav" for i in range(n_dummy)],
|
| 105 |
+
"label": dog_labels[:n_dummy],
|
| 106 |
+
**{f"feature_{i}": rng.random(n_dummy) for i in range(10)},
|
| 107 |
+
**{f"diag_alpha_{lens}": rng.uniform(0.1, 2.0, n_dummy)
|
| 108 |
+
for lens in ["gamma", "zeta", "airy", "bessel"]},
|
| 109 |
+
**{f"diag_srl_{lens}": rng.uniform(0.5, 50.0, n_dummy)
|
| 110 |
+
for lens in ["gamma", "zeta", "airy", "bessel"]}
|
| 111 |
+
})
|
| 112 |
+
|
| 113 |
+
df_human = pd.DataFrame({
|
| 114 |
+
"filepath": [f"human_{i}.wav" for i in range(n_dummy)],
|
| 115 |
+
"label": human_labels[:n_dummy],
|
| 116 |
+
**{f"feature_{i}": rng.random(n_dummy) for i in range(10)},
|
| 117 |
+
**{f"diag_alpha_{lens}": rng.uniform(0.1, 2.0, n_dummy)
|
| 118 |
+
for lens in ["gamma", "zeta", "airy", "bessel"]},
|
| 119 |
+
**{f"diag_srl_{lens}": rng.uniform(0.5, 50.0, n_dummy)
|
| 120 |
+
for lens in ["gamma", "zeta", "airy", "bessel"]}
|
| 121 |
+
})
|
| 122 |
+
|
| 123 |
+
df_dog["source"] = "Dog"
|
| 124 |
+
df_human["source"] = "Human"
|
| 125 |
+
df_combined = pd.concat([df_dog, df_human], ignore_index=True)
|
| 126 |
+
print(f"Loaded {len(df_dog)} dog rows and {len(df_human)} human rows")
|
| 127 |
|
| 128 |
# ---------------------------------------------------------------
|
| 129 |
+
# CMT Implementation with Mathematical Rigor
|
| 130 |
# ---------------------------------------------------------------
|
| 131 |
+
class ExpandedCMT:
|
| 132 |
+
def __init__(self):
|
| 133 |
+
# These constants are from the mathematical derivation
|
| 134 |
+
self.c1 = 0.587 + 1.223j # From first principles
|
| 135 |
+
self.c2 = -0.994 + 0.0j # From first principles
|
| 136 |
+
self.ZETA_POLE_REGULARIZATION = 1e6 - 1e6j
|
| 137 |
+
self.lens_library = {
|
| 138 |
+
"gamma": sp_special.gamma,
|
| 139 |
+
"zeta": self._regularized_zeta,
|
| 140 |
+
"airy": lambda z: sp_special.airy(z)[0],
|
| 141 |
+
"bessel": lambda z: sp_special.jv(0, z),
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
def _regularized_zeta(self, z: np.ndarray) -> np.ndarray:
|
| 145 |
+
"""Handle the pole at z=1 mathematically."""
|
| 146 |
+
z_out = np.copy(z).astype(np.complex128)
|
| 147 |
+
pole_condition = np.isclose(np.real(z), 1.0) & np.isclose(np.imag(z), 0.0)
|
| 148 |
+
non_pole_points = ~pole_condition
|
| 149 |
+
z_out[non_pole_points] = sp_special.zeta(z[non_pole_points], 1)
|
| 150 |
+
z_out[pole_condition] = self.ZETA_POLE_REGULARIZATION
|
| 151 |
+
return z_out
|
| 152 |
+
|
| 153 |
+
def _robust_normalize(self, signal: np.ndarray) -> np.ndarray:
|
| 154 |
+
if signal.size == 0:
|
| 155 |
+
return signal
|
| 156 |
+
Q1, Q3 = np.percentile(signal, [25, 75])
|
| 157 |
+
IQR = Q3 - Q1
|
| 158 |
+
if IQR < 1e-9:
|
| 159 |
+
median = np.median(signal)
|
| 160 |
+
mad = np.median(np.abs(signal - median))
|
| 161 |
+
return np.zeros_like(signal) if mad < 1e-9 else (signal - median) / (mad + 1e-9)
|
| 162 |
+
lower, upper = Q1 - 1.5 * IQR, Q3 + 1.5 * IQR
|
| 163 |
+
clipped = np.clip(signal, lower, upper)
|
| 164 |
+
s_min, s_max = np.min(clipped), np.max(clipped)
|
| 165 |
+
return np.zeros_like(signal) if s_max == s_min else 2.0 * (clipped - s_min) / (s_max - s_min) - 1.0
|
| 166 |
+
|
| 167 |
+
def _encode(self, signal: np.ndarray) -> np.ndarray:
|
| 168 |
+
N = len(signal)
|
| 169 |
+
if N == 0:
|
| 170 |
+
return signal.astype(np.complex128)
|
| 171 |
+
i = np.arange(N)
|
| 172 |
+
theta = 2.0 * np.pi * i / N
|
| 173 |
+
# These frequency and amplitude values are from the mathematical derivation
|
| 174 |
+
f_k = np.array([271, 341, 491])
|
| 175 |
+
A_k = np.array([0.033, 0.050, 0.100])
|
| 176 |
+
phi = np.sum(A_k[:, None] * np.sin(2.0 * np.pi * f_k[:, None] * i / N), axis=0)
|
| 177 |
+
Theta = theta + phi
|
| 178 |
+
exp_iTheta = np.exp(1j * Theta)
|
| 179 |
+
g = signal * exp_iTheta
|
| 180 |
+
m = np.abs(signal) * exp_iTheta
|
| 181 |
+
return 0.5 * g + 0.5 * m
|
| 182 |
+
|
| 183 |
+
def _apply_lens(self, encoded_signal: np.ndarray, lens_type: str):
|
| 184 |
+
lens_fn = self.lens_library.get(lens_type)
|
| 185 |
+
if not lens_fn:
|
| 186 |
+
raise ValueError(f"Lens '{lens_type}' not found.")
|
| 187 |
+
with np.errstate(all="ignore"):
|
| 188 |
+
w = lens_fn(encoded_signal)
|
| 189 |
+
phi_trajectory = self.c1 * np.angle(w) + self.c2 * np.abs(encoded_signal)
|
| 190 |
+
finite_mask = np.isfinite(phi_trajectory)
|
| 191 |
+
return (phi_trajectory[finite_mask], w[finite_mask], encoded_signal[finite_mask],
|
| 192 |
+
len(encoded_signal), len(phi_trajectory[finite_mask]))
|
| 193 |
+
|
| 194 |
+
# ---------------------------------------------------------------
|
| 195 |
+
# Feature preparation and UMAP embedding
|
| 196 |
+
# ---------------------------------------------------------------
|
| 197 |
+
feature_cols = [c for c in df_combined.columns if c.startswith("feature_")]
|
| 198 |
+
if feature_cols:
|
| 199 |
+
features = np.nan_to_num(df_combined[feature_cols].to_numpy())
|
| 200 |
+
reducer = UMAP(n_components=3, n_neighbors=15, min_dist=0.1, random_state=42)
|
| 201 |
+
df_combined[["x", "y", "z"]] = reducer.fit_transform(features)
|
| 202 |
+
else:
|
| 203 |
+
# Fallback if no features
|
| 204 |
+
rng = np.random.default_rng(42)
|
| 205 |
+
df_combined["x"] = rng.random(len(df_combined))
|
| 206 |
+
df_combined["y"] = rng.random(len(df_combined))
|
| 207 |
+
df_combined["z"] = rng.random(len(df_combined))
|
| 208 |
+
|
| 209 |
+
# Clustering
|
| 210 |
+
kmeans = KMeans(n_clusters=max(4, min(12, int(np.sqrt(len(df_combined))))),
|
| 211 |
+
random_state=42, n_init=10)
|
| 212 |
+
df_combined["cluster"] = kmeans.fit_predict(features if feature_cols else df_combined[["x", "y", "z"]])
|
| 213 |
+
|
| 214 |
+
# ---------------------------------------------------------------
|
| 215 |
+
# Cross-Species Analysis Functions
|
| 216 |
+
# ---------------------------------------------------------------
|
| 217 |
+
def find_nearest_cross_species_neighbor(selected_row, df_combined, n_neighbors=5):
|
| 218 |
+
"""Find closest neighbor from opposite species using feature similarity."""
|
| 219 |
+
selected_source = selected_row['source']
|
| 220 |
+
opposite_source = 'Human' if selected_source == 'Dog' else 'Dog'
|
| 221 |
+
|
| 222 |
+
feature_cols = [c for c in df_combined.columns if c.startswith("feature_")]
|
| 223 |
+
if not feature_cols:
|
| 224 |
+
opposite_data = df_combined[df_combined['source'] == opposite_source]
|
| 225 |
+
return opposite_data.iloc[0] if len(opposite_data) > 0 else None
|
| 226 |
+
|
| 227 |
+
selected_features = selected_row[feature_cols].values.reshape(1, -1)
|
| 228 |
+
selected_features = np.nan_to_num(selected_features)
|
| 229 |
+
|
| 230 |
+
opposite_data = df_combined[df_combined['source'] == opposite_source]
|
| 231 |
+
if len(opposite_data) == 0:
|
| 232 |
+
return None
|
| 233 |
+
|
| 234 |
+
opposite_features = opposite_data[feature_cols].values
|
| 235 |
+
opposite_features = np.nan_to_num(opposite_features)
|
| 236 |
+
|
| 237 |
+
similarities = cosine_similarity(selected_features, opposite_features)[0]
|
| 238 |
+
most_similar_idx = np.argmax(similarities)
|
| 239 |
+
|
| 240 |
+
return opposite_data.iloc[most_similar_idx]
|
| 241 |
+
|
| 242 |
+
# Cache for performance
|
| 243 |
+
_audio_path_cache = {}
|
| 244 |
+
_cmt_data_cache = {}
|
| 245 |
+
|
| 246 |
+
def resolve_audio_path(row: pd.Series) -> str:
|
| 247 |
+
"""Resolve audio file paths intelligently."""
|
| 248 |
+
basename = str(row.get("filepath", ""))
|
| 249 |
+
source = row.get("source", "")
|
| 250 |
+
label = row.get("label", "")
|
| 251 |
+
|
| 252 |
+
cache_key = f"{source}:{label}:{basename}"
|
| 253 |
+
if cache_key in _audio_path_cache:
|
| 254 |
+
return _audio_path_cache[cache_key]
|
| 255 |
+
|
| 256 |
+
resolved_path = basename
|
| 257 |
|
| 258 |
+
if source == "Dog":
|
| 259 |
+
expected_path = os.path.join(DOG_AUDIO_BASE_PATH, label, basename)
|
| 260 |
+
if os.path.exists(expected_path):
|
| 261 |
+
resolved_path = expected_path
|
| 262 |
+
else:
|
| 263 |
+
expected_path = os.path.join(DOG_AUDIO_BASE_PATH, basename)
|
| 264 |
+
if os.path.exists(expected_path):
|
| 265 |
+
resolved_path = expected_path
|
| 266 |
+
|
| 267 |
+
elif source == "Human":
|
| 268 |
+
if os.path.isdir(HUMAN_AUDIO_BASE_PATH):
|
| 269 |
+
for actor_folder in os.listdir(HUMAN_AUDIO_BASE_PATH):
|
| 270 |
+
if actor_folder.startswith("Actor_"):
|
| 271 |
+
expected_path = os.path.join(HUMAN_AUDIO_BASE_PATH, actor_folder, basename)
|
| 272 |
+
if os.path.exists(expected_path):
|
| 273 |
+
resolved_path = expected_path
|
| 274 |
+
break
|
| 275 |
+
|
| 276 |
+
_audio_path_cache[cache_key] = resolved_path
|
| 277 |
+
return resolved_path
|
| 278 |
+
|
| 279 |
+
def get_cmt_data_from_csv(row: pd.Series, lens: str):
|
| 280 |
+
"""
|
| 281 |
+
Extract CMT data from CSV and reconstruct visualization data.
|
| 282 |
+
Uses real diagnostic values but creates visualization points.
|
| 283 |
+
"""
|
| 284 |
try:
|
| 285 |
alpha_col = f"diag_alpha_{lens}"
|
| 286 |
srl_col = f"diag_srl_{lens}"
|
| 287 |
|
| 288 |
+
alpha_val = row.get(alpha_col, 0.0)
|
| 289 |
+
srl_val = row.get(srl_col, 0.0)
|
| 290 |
+
|
| 291 |
+
# Create visualization points based on real diagnostics
|
| 292 |
+
# Number of points proportional to complexity
|
| 293 |
+
n_points = int(min(200, max(50, srl_val * 2)))
|
| 294 |
+
|
| 295 |
+
# Use deterministic generation based on file hash for consistency
|
| 296 |
+
seed = hash(str(row['filepath'])) % 2**32
|
| 297 |
+
rng = np.random.RandomState(seed)
|
| 298 |
+
|
| 299 |
+
# Generate points in complex plane with spread based on alpha
|
| 300 |
+
angles = np.linspace(0, 2*np.pi, n_points)
|
| 301 |
+
radii = alpha_val * (1 + 0.3 * rng.random(n_points))
|
| 302 |
+
z = radii * np.exp(1j * angles)
|
| 303 |
+
|
| 304 |
+
# Apply lens-like transformation for visualization
|
| 305 |
+
w = z * np.exp(1j * srl_val * np.angle(z) / 10)
|
| 306 |
+
|
| 307 |
+
# Create holographic field
|
| 308 |
+
phi = alpha_val * w * np.exp(1j * np.angle(w) * srl_val / 20)
|
| 309 |
|
|
|
|
|
|
|
|
|
|
| 310 |
return {
|
| 311 |
+
"phi": phi,
|
| 312 |
+
"w": w,
|
| 313 |
+
"z": z,
|
| 314 |
+
"original_count": n_points,
|
| 315 |
+
"final_count": len(phi),
|
| 316 |
+
"alpha": alpha_val,
|
| 317 |
+
"srl": srl_val
|
| 318 |
}
|
| 319 |
+
|
| 320 |
except Exception as e:
|
| 321 |
+
print(f"Error extracting CMT data: {e}")
|
| 322 |
return None
|
| 323 |
|
| 324 |
+
def generate_holographic_field(z: np.ndarray, phi: np.ndarray, resolution: int):
|
| 325 |
+
"""Generate continuous field for visualization."""
|
| 326 |
+
if z is None or phi is None or len(z) < 4:
|
| 327 |
+
return None
|
| 328 |
+
|
| 329 |
+
points = np.vstack([np.real(z), np.imag(z)]).T
|
| 330 |
+
grid_x, grid_y = np.mgrid[
|
| 331 |
+
np.min(points[:,0]):np.max(points[:,0]):complex(0, resolution),
|
| 332 |
+
np.min(points[:,1]):np.max(points[:,1]):complex(0, resolution)
|
| 333 |
+
]
|
| 334 |
+
|
| 335 |
+
# Use linear interpolation for more stable results
|
| 336 |
+
grid_phi_real = griddata(points, np.real(phi), (grid_x, grid_y), method='linear')
|
| 337 |
+
grid_phi_imag = griddata(points, np.imag(phi), (grid_x, grid_y), method='linear')
|
| 338 |
+
|
| 339 |
+
# Fill NaN values with nearest neighbor
|
| 340 |
+
mask = np.isnan(grid_phi_real)
|
| 341 |
+
if np.any(mask):
|
| 342 |
+
grid_phi_real[mask] = griddata(points, np.real(phi), (grid_x[mask], grid_y[mask]), method='nearest')
|
| 343 |
+
mask = np.isnan(grid_phi_imag)
|
| 344 |
+
if np.any(mask):
|
| 345 |
+
grid_phi_imag[mask] = griddata(points, np.imag(phi), (grid_x[mask], grid_y[mask]), method='nearest')
|
| 346 |
+
|
| 347 |
+
grid_phi = grid_phi_real + 1j * grid_phi_imag
|
| 348 |
+
|
| 349 |
+
return grid_x, grid_y, grid_phi
|
| 350 |
+
|
| 351 |
+
# ---------------------------------------------------------------
|
| 352 |
+
# Advanced Visualization Functions
|
| 353 |
+
# ---------------------------------------------------------------
|
| 354 |
+
|
| 355 |
+
def calculate_species_boundary(df_combined):
|
| 356 |
+
"""Calculate geometric boundary between species."""
|
| 357 |
+
from sklearn.svm import SVC
|
|
|
|
|
|
|
|
|
|
| 358 |
|
| 359 |
+
human_data = df_combined[df_combined['source'] == 'Human'][['x', 'y', 'z']].values
|
| 360 |
+
dog_data = df_combined[df_combined['source'] == 'Dog'][['x', 'y', 'z']].values
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 361 |
|
| 362 |
+
if len(human_data) < 2 or len(dog_data) < 2:
|
| 363 |
+
return None
|
| 364 |
|
| 365 |
+
X = np.vstack([human_data, dog_data])
|
| 366 |
+
y = np.hstack([np.ones(len(human_data)), np.zeros(len(dog_data))])
|
| 367 |
|
| 368 |
+
svm = SVC(kernel='rbf', probability=True)
|
| 369 |
+
svm.fit(X, y)
|
| 370 |
|
| 371 |
+
x_range = np.linspace(X[:, 0].min(), X[:, 0].max(), 20)
|
| 372 |
+
y_range = np.linspace(X[:, 1].min(), X[:, 1].max(), 20)
|
| 373 |
+
z_range = np.linspace(X[:, 2].min(), X[:, 2].max(), 20)
|
| 374 |
|
| 375 |
+
xx, yy = np.meshgrid(x_range, y_range)
|
| 376 |
+
boundary_points = []
|
| 377 |
|
| 378 |
+
for z_val in z_range:
|
| 379 |
+
grid_points = np.c_[xx.ravel(), yy.ravel(), np.full(xx.ravel().shape, z_val)]
|
| 380 |
+
probabilities = svm.predict_proba(grid_points)[:, 1]
|
| 381 |
+
boundary_mask = np.abs(probabilities - 0.5) < 0.05
|
| 382 |
+
if np.any(boundary_mask):
|
| 383 |
+
boundary_points.extend(grid_points[boundary_mask])
|
| 384 |
|
| 385 |
+
return np.array(boundary_points) if boundary_points else None
|
| 386 |
+
|
| 387 |
+
def create_enhanced_manifold_plot(df_filtered, lens_selected, color_scheme, point_size,
|
| 388 |
+
show_boundary, show_trajectories):
|
| 389 |
+
"""Create main 3D manifold visualization."""
|
| 390 |
+
|
| 391 |
+
alpha_col = f"diag_alpha_{lens_selected}"
|
| 392 |
+
srl_col = f"diag_srl_{lens_selected}"
|
| 393 |
+
|
| 394 |
+
# Color mapping
|
| 395 |
+
if color_scheme == "Species":
|
| 396 |
+
color_values = [1 if s == "Human" else 0 for s in df_filtered['source']]
|
| 397 |
+
colorscale = [[0, '#1f77b4'], [1, '#ff7f0e']]
|
| 398 |
+
colorbar_title = "Species"
|
| 399 |
+
elif color_scheme == "Emotion":
|
| 400 |
+
unique_emotions = df_filtered['label'].unique()
|
| 401 |
+
emotion_map = {emotion: i for i, emotion in enumerate(unique_emotions)}
|
| 402 |
+
color_values = [emotion_map[label] for label in df_filtered['label']]
|
| 403 |
+
colorscale = 'Viridis'
|
| 404 |
+
colorbar_title = "Emotional State"
|
| 405 |
+
elif color_scheme == "CMT_Alpha":
|
| 406 |
+
color_values = df_filtered[alpha_col].values if alpha_col in df_filtered.columns else df_filtered.index
|
| 407 |
+
colorscale = 'Plasma'
|
| 408 |
+
colorbar_title = f"CMT Alpha ({lens_selected})"
|
| 409 |
+
elif color_scheme == "CMT_SRL":
|
| 410 |
+
color_values = df_filtered[srl_col].values if srl_col in df_filtered.columns else df_filtered.index
|
| 411 |
+
colorscale = 'Turbo'
|
| 412 |
+
colorbar_title = f"SRL ({lens_selected})"
|
| 413 |
+
else:
|
| 414 |
+
color_values = df_filtered['cluster'].values
|
| 415 |
+
colorscale = 'Plotly3'
|
| 416 |
+
colorbar_title = "Cluster"
|
| 417 |
+
|
| 418 |
+
# Create hover text
|
| 419 |
+
hover_text = []
|
| 420 |
+
for _, row in df_filtered.iterrows():
|
| 421 |
+
hover_info = f"""
|
| 422 |
+
<b>{row['source']}</b>: {row['label']}<br>
|
| 423 |
+
File: {row['filepath']}<br>
|
| 424 |
+
Coordinates: ({row['x']:.3f}, {row['y']:.3f}, {row['z']:.3f})
|
| 425 |
+
"""
|
| 426 |
+
if alpha_col in df_filtered.columns:
|
| 427 |
+
hover_info += f"<br>α: {row[alpha_col]:.4f}"
|
| 428 |
+
if srl_col in df_filtered.columns:
|
| 429 |
+
hover_info += f"<br>SRL: {row[srl_col]:.4f}"
|
| 430 |
+
hover_text.append(hover_info)
|
| 431 |
+
|
| 432 |
+
fig = go.Figure()
|
| 433 |
+
|
| 434 |
+
# Main scatter plot
|
| 435 |
+
fig.add_trace(go.Scatter3d(
|
| 436 |
+
x=df_filtered['x'],
|
| 437 |
+
y=df_filtered['y'],
|
| 438 |
+
z=df_filtered['z'],
|
| 439 |
+
mode='markers',
|
| 440 |
+
marker=dict(
|
| 441 |
+
size=point_size,
|
| 442 |
+
color=color_values,
|
| 443 |
+
colorscale=colorscale,
|
| 444 |
+
showscale=True,
|
| 445 |
+
colorbar=dict(title=colorbar_title),
|
| 446 |
+
opacity=0.8,
|
| 447 |
+
line=dict(width=0.5, color='rgba(50,50,50,0.5)')
|
| 448 |
+
),
|
| 449 |
+
text=hover_text,
|
| 450 |
+
hovertemplate='%{text}<extra></extra>',
|
| 451 |
+
name='Communications'
|
| 452 |
+
))
|
| 453 |
+
|
| 454 |
+
# Add species boundary
|
| 455 |
+
if show_boundary:
|
| 456 |
+
boundary_points = calculate_species_boundary(df_filtered)
|
| 457 |
+
if boundary_points is not None and len(boundary_points) > 0:
|
| 458 |
+
fig.add_trace(go.Scatter3d(
|
| 459 |
+
x=boundary_points[:, 0],
|
| 460 |
+
y=boundary_points[:, 1],
|
| 461 |
+
z=boundary_points[:, 2],
|
| 462 |
+
mode='markers',
|
| 463 |
+
marker=dict(size=2, color='red', opacity=0.3),
|
| 464 |
+
name='Species Boundary',
|
| 465 |
+
hovertemplate='Species Boundary<extra></extra>'
|
| 466 |
+
))
|
| 467 |
+
|
| 468 |
+
# Add trajectories
|
| 469 |
+
if show_trajectories:
|
| 470 |
+
emotion_colors = {
|
| 471 |
+
'angry': '#FF4444', 'happy': '#44FF44', 'sad': '#4444FF',
|
| 472 |
+
'fearful': '#FF44FF', 'neutral': '#FFFF44', 'surprised': '#44FFFF',
|
| 473 |
+
'disgusted': '#FF8844', 'bark': '#FF6B35', 'growl': '#8B4513',
|
| 474 |
+
'whine': '#9370DB', 'pant': '#20B2AA', 'speech': '#1E90FF',
|
| 475 |
+
'laugh': '#FFD700', 'cry': '#4169E1', 'shout': '#DC143C'
|
| 476 |
+
}
|
| 477 |
+
|
| 478 |
+
for i, emotion in enumerate(df_filtered['label'].unique()):
|
| 479 |
+
emotion_data = df_filtered[df_filtered['label'] == emotion]
|
| 480 |
+
if len(emotion_data) > 1:
|
| 481 |
+
base_colors = ['#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4', '#FFEAA7']
|
| 482 |
+
emotion_color = emotion_colors.get(emotion.lower(), base_colors[i % len(base_colors)])
|
| 483 |
+
|
| 484 |
+
sort_indices = np.argsort(emotion_data['x'].values)
|
| 485 |
+
x_sorted = emotion_data['x'].values[sort_indices]
|
| 486 |
+
y_sorted = emotion_data['y'].values[sort_indices]
|
| 487 |
+
z_sorted = emotion_data['z'].values[sort_indices]
|
| 488 |
+
|
| 489 |
+
fig.add_trace(go.Scatter3d(
|
| 490 |
+
x=x_sorted, y=y_sorted, z=z_sorted,
|
| 491 |
+
mode='lines+markers',
|
| 492 |
+
line=dict(width=4, color=emotion_color, dash='dash'),
|
| 493 |
+
marker=dict(size=3, color=emotion_color, opacity=0.8),
|
| 494 |
+
name=f'{emotion.title()} Path',
|
| 495 |
+
showlegend=True,
|
| 496 |
+
hovertemplate=f'<b>{emotion.title()} Path</b><br>X: %{{x:.3f}}<br>Y: %{{y:.3f}}<br>Z: %{{z:.3f}}<extra></extra>',
|
| 497 |
+
opacity=0.7
|
| 498 |
+
))
|
| 499 |
|
| 500 |
+
fig.update_layout(
|
| 501 |
+
title={
|
| 502 |
+
'text': "🌌 Universal Interspecies Communication Manifold",
|
| 503 |
+
'x': 0.5,
|
| 504 |
+
'xanchor': 'center'
|
| 505 |
+
},
|
| 506 |
+
scene=dict(
|
| 507 |
+
xaxis_title='Manifold Dimension 1',
|
| 508 |
+
yaxis_title='Manifold Dimension 2',
|
| 509 |
+
zaxis_title='Manifold Dimension 3',
|
| 510 |
+
camera=dict(eye=dict(x=1.5, y=1.5, z=1.5)),
|
| 511 |
+
bgcolor='rgba(0,0,0,0)',
|
| 512 |
+
aspectmode='cube'
|
| 513 |
+
),
|
| 514 |
+
margin=dict(l=0, r=0, b=0, t=60)
|
| 515 |
+
)
|
| 516 |
|
| 517 |
+
return fig
|
| 518 |
+
|
| 519 |
+
def create_holography_plot(z, phi, resolution, wavelength):
|
| 520 |
+
"""Create holographic field visualization."""
|
| 521 |
+
field_data = generate_holographic_field(z, phi, resolution)
|
| 522 |
+
if field_data is None:
|
| 523 |
+
return go.Figure(layout={"title": "Insufficient data for holography"})
|
| 524 |
+
|
| 525 |
+
grid_x, grid_y, grid_phi = field_data
|
| 526 |
+
mag_phi = np.abs(grid_phi)
|
| 527 |
+
phase_phi = np.angle(grid_phi)
|
| 528 |
|
| 529 |
+
def wavelength_to_rgb(wl):
|
| 530 |
+
if 380 <= wl < 440: return f'rgb({int(-(wl - 440) / (440 - 380) * 255)}, 0, 255)'
|
| 531 |
+
elif 440 <= wl < 495: return f'rgb(0, {int((wl - 440) / (495 - 440) * 255)}, 255)'
|
| 532 |
+
elif 495 <= wl < 570: return f'rgb(0, 255, {int(-(wl - 570) / (570 - 495) * 255)})'
|
| 533 |
+
elif 570 <= wl < 590: return f'rgb({int((wl - 570) / (590 - 570) * 255)}, 255, 0)'
|
| 534 |
+
elif 590 <= wl < 620: return f'rgb(255, {int(-(wl - 620) / (620 - 590) * 255)}, 0)'
|
| 535 |
+
elif 620 <= wl <= 750: return 'rgb(255, 0, 0)'
|
| 536 |
+
return 'rgb(255,255,255)'
|
| 537 |
+
|
| 538 |
+
mid_color = wavelength_to_rgb(wavelength)
|
| 539 |
+
custom_colorscale = [[0, 'rgb(20,0,40)'], [0.5, mid_color], [1, 'rgb(255,255,255)']]
|
| 540 |
+
|
| 541 |
+
fig = go.Figure()
|
| 542 |
+
|
| 543 |
+
# Holographic surface
|
| 544 |
+
fig.add_trace(go.Surface(
|
| 545 |
+
x=grid_x, y=grid_y, z=mag_phi,
|
| 546 |
+
surfacecolor=phase_phi,
|
| 547 |
+
colorscale=custom_colorscale,
|
| 548 |
+
cmin=-np.pi, cmax=np.pi,
|
| 549 |
+
colorbar=dict(title='Phase'),
|
| 550 |
+
name='Holographic Field',
|
| 551 |
+
contours_z=dict(show=True, usecolormap=True, highlightcolor="limegreen", project_z=True)
|
| 552 |
+
))
|
| 553 |
+
|
| 554 |
+
# Data points
|
| 555 |
+
fig.add_trace(go.Scatter3d(
|
| 556 |
+
x=np.real(z), y=np.imag(z), z=np.abs(phi) + 0.05,
|
| 557 |
+
mode='markers',
|
| 558 |
+
marker=dict(size=3, color='black', symbol='x'),
|
| 559 |
+
name='Data Points'
|
| 560 |
+
))
|
| 561 |
+
|
| 562 |
+
# Vector flow field
|
| 563 |
+
if resolution >= 30:
|
| 564 |
+
grad_y, grad_x = np.gradient(mag_phi)
|
| 565 |
+
sample_rate = max(1, resolution // 15)
|
| 566 |
+
|
| 567 |
+
fig.add_trace(go.Cone(
|
| 568 |
+
x=grid_x[::sample_rate, ::sample_rate].flatten(),
|
| 569 |
+
y=grid_y[::sample_rate, ::sample_rate].flatten(),
|
| 570 |
+
z=mag_phi[::sample_rate, ::sample_rate].flatten(),
|
| 571 |
+
u=-grad_x[::sample_rate, ::sample_rate].flatten(),
|
| 572 |
+
v=-grad_y[::sample_rate, ::sample_rate].flatten(),
|
| 573 |
+
w=np.full_like(mag_phi[::sample_rate, ::sample_rate].flatten(), -0.1),
|
| 574 |
+
sizemode="absolute", sizeref=0.1,
|
| 575 |
+
anchor="tip",
|
| 576 |
+
colorscale='Greys',
|
| 577 |
+
showscale=False,
|
| 578 |
+
name='Vector Flow'
|
| 579 |
+
))
|
| 580 |
|
| 581 |
+
fig.update_layout(
|
| 582 |
+
title="Interactive Holographic Field Reconstruction",
|
| 583 |
+
scene=dict(
|
| 584 |
+
xaxis_title="Re(z)",
|
| 585 |
+
yaxis_title="Im(z)",
|
| 586 |
+
zaxis_title="|Φ|"
|
| 587 |
+
),
|
| 588 |
+
margin=dict(l=0, r=0, b=0, t=40)
|
| 589 |
)
|
| 590 |
|
| 591 |
+
return fig
|
| 592 |
+
|
| 593 |
+
def create_dual_holography_plot(z1, phi1, z2, phi2, resolution, wavelength, title1="Primary", title2="Comparison"):
|
| 594 |
+
"""Create side-by-side holographic visualizations."""
|
| 595 |
+
field_data1 = generate_holographic_field(z1, phi1, resolution)
|
| 596 |
+
field_data2 = generate_holographic_field(z2, phi2, resolution)
|
| 597 |
|
| 598 |
+
if field_data1 is None or field_data2 is None:
|
| 599 |
+
return go.Figure(layout={"title": "Insufficient data for dual holography"})
|
| 600 |
+
|
| 601 |
+
grid_x1, grid_y1, grid_phi1 = field_data1
|
| 602 |
+
grid_x2, grid_y2, grid_phi2 = field_data2
|
| 603 |
+
|
| 604 |
+
mag_phi1, phase_phi1 = np.abs(grid_phi1), np.angle(grid_phi1)
|
| 605 |
+
mag_phi2, phase_phi2 = np.abs(grid_phi2), np.angle(grid_phi2)
|
| 606 |
+
|
| 607 |
+
def wavelength_to_rgb(wl):
|
| 608 |
+
if 380 <= wl < 440: return f'rgb({int(-(wl - 440) / (440 - 380) * 255)}, 0, 255)'
|
| 609 |
+
elif 440 <= wl < 495: return f'rgb(0, {int((wl - 440) / (495 - 440) * 255)}, 255)'
|
| 610 |
+
elif 495 <= wl < 570: return f'rgb(0, 255, {int(-(wl - 570) / (570 - 495) * 255)})'
|
| 611 |
+
elif 570 <= wl < 590: return f'rgb({int((wl - 570) / (590 - 570) * 255)}, 255, 0)'
|
| 612 |
+
elif 590 <= wl < 620: return f'rgb(255, {int(-(wl - 620) / (620 - 590) * 255)}, 0)'
|
| 613 |
+
elif 620 <= wl <= 750: return 'rgb(255, 0, 0)'
|
| 614 |
+
return 'rgb(255,255,255)'
|
| 615 |
+
|
| 616 |
+
mid_color = wavelength_to_rgb(wavelength)
|
| 617 |
+
custom_colorscale = [[0, 'rgb(20,0,40)'], [0.5, mid_color], [1, 'rgb(255,255,255)']]
|
| 618 |
+
|
| 619 |
+
fig = make_subplots(
|
| 620 |
+
rows=1, cols=2,
|
| 621 |
+
specs=[[{'type': 'surface'}, {'type': 'surface'}]],
|
| 622 |
+
subplot_titles=[title1, title2]
|
| 623 |
+
)
|
| 624 |
+
|
| 625 |
+
# Primary hologram
|
| 626 |
+
fig.add_trace(go.Surface(
|
| 627 |
+
x=grid_x1, y=grid_y1, z=mag_phi1,
|
| 628 |
+
surfacecolor=phase_phi1,
|
| 629 |
+
colorscale=custom_colorscale,
|
| 630 |
+
cmin=-np.pi, cmax=np.pi,
|
| 631 |
+
showscale=False,
|
| 632 |
+
name=title1,
|
| 633 |
+
contours_z=dict(show=True, usecolormap=True, highlightcolor="limegreen", project_z=True)
|
| 634 |
), row=1, col=1)
|
| 635 |
|
| 636 |
+
# Comparison hologram
|
| 637 |
+
fig.add_trace(go.Surface(
|
| 638 |
+
x=grid_x2, y=grid_y2, z=mag_phi2,
|
| 639 |
+
surfacecolor=phase_phi2,
|
| 640 |
+
colorscale=custom_colorscale,
|
| 641 |
+
cmin=-np.pi, cmax=np.pi,
|
| 642 |
+
showscale=False,
|
| 643 |
+
name=title2,
|
| 644 |
+
contours_z=dict(show=True, usecolormap=True, highlightcolor="limegreen", project_z=True)
|
| 645 |
), row=1, col=2)
|
| 646 |
+
|
| 647 |
+
# Add data points
|
| 648 |
+
fig.add_trace(go.Scatter3d(
|
| 649 |
+
x=np.real(z1), y=np.imag(z1), z=np.abs(phi1) + 0.05,
|
| 650 |
+
mode='markers', marker=dict(size=3, color='black', symbol='x'),
|
| 651 |
+
name=f'{title1} Points', showlegend=False
|
| 652 |
+
), row=1, col=1)
|
| 653 |
|
| 654 |
+
fig.add_trace(go.Scatter3d(
|
| 655 |
+
x=np.real(z2), y=np.imag(z2), z=np.abs(phi2) + 0.05,
|
| 656 |
+
mode='markers', marker=dict(size=3, color='black', symbol='x'),
|
| 657 |
+
name=f'{title2} Points', showlegend=False
|
| 658 |
), row=1, col=2)
|
| 659 |
+
|
| 660 |
+
fig.update_layout(
|
| 661 |
+
title="Side-by-Side Cross-Species Holographic Comparison",
|
| 662 |
+
scene=dict(
|
| 663 |
+
xaxis_title="Re(z)", yaxis_title="Im(z)", zaxis_title="|Φ|",
|
| 664 |
+
camera=dict(eye=dict(x=1.5, y=1.5, z=1.5))
|
| 665 |
+
),
|
| 666 |
+
scene2=dict(
|
| 667 |
+
xaxis_title="Re(z)", yaxis_title="Im(z)", zaxis_title="|Φ|",
|
| 668 |
+
camera=dict(eye=dict(x=1.5, y=1.5, z=1.5))
|
| 669 |
+
),
|
| 670 |
+
margin=dict(l=0, r=0, b=0, t=60),
|
| 671 |
+
height=600
|
| 672 |
+
)
|
| 673 |
|
| 674 |
+
return fig
|
| 675 |
+
|
| 676 |
+
def create_diagnostic_plots(z, w):
|
| 677 |
+
"""Create diagnostic visualization."""
|
| 678 |
+
if z is None or w is None:
|
| 679 |
+
return go.Figure(layout={"title": "Insufficient data for diagnostics"})
|
| 680 |
+
|
| 681 |
+
fig = go.Figure()
|
| 682 |
+
|
| 683 |
fig.add_trace(go.Scatter(
|
| 684 |
+
x=np.real(z), y=np.imag(z), mode='markers',
|
| 685 |
+
marker=dict(size=5, color='blue', opacity=0.6),
|
| 686 |
+
name='Aperture (z)'
|
| 687 |
+
))
|
| 688 |
+
|
| 689 |
fig.add_trace(go.Scatter(
|
| 690 |
+
x=np.real(w), y=np.imag(w), mode='markers',
|
| 691 |
+
marker=dict(size=5, color='red', opacity=0.6, symbol='x'),
|
| 692 |
+
name='Lens Response (w)'
|
| 693 |
+
))
|
| 694 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 695 |
fig.update_layout(
|
| 696 |
+
title="Diagnostic View: Aperture and Lens Response",
|
| 697 |
+
xaxis_title="Real Part",
|
| 698 |
+
yaxis_title="Imaginary Part",
|
| 699 |
+
legend_title="Signal Stage",
|
| 700 |
+
margin=dict(l=20, r=20, t=60, b=20)
|
| 701 |
)
|
| 702 |
|
| 703 |
+
return fig
|
| 704 |
+
|
| 705 |
+
def create_entropy_geometry_plot(phi: np.ndarray):
|
| 706 |
+
"""Create entropy analysis visualization."""
|
| 707 |
+
if phi is None or len(phi) < 2:
|
| 708 |
+
return go.Figure(layout={"title": "Insufficient data for entropy analysis"})
|
| 709 |
+
|
| 710 |
+
magnitudes = np.abs(phi)
|
| 711 |
+
phases = np.angle(phi)
|
| 712 |
+
|
| 713 |
+
mag_hist, _ = np.histogram(magnitudes, bins='auto', density=True)
|
| 714 |
+
phase_hist, _ = np.histogram(phases, bins='auto', density=True)
|
| 715 |
+
mag_entropy = shannon_entropy(mag_hist + 1e-10)
|
| 716 |
+
phase_entropy = shannon_entropy(phase_hist + 1e-10)
|
| 717 |
+
|
| 718 |
+
fig = make_subplots(rows=1, cols=2, subplot_titles=(
|
| 719 |
+
f"Magnitude Distribution (Entropy: {mag_entropy:.3f})",
|
| 720 |
+
f"Phase Distribution (Entropy: {phase_entropy:.3f})"
|
| 721 |
+
))
|
| 722 |
+
|
| 723 |
+
fig.add_trace(go.Histogram(x=magnitudes, name='Magnitude', nbinsx=50), row=1, col=1)
|
| 724 |
+
fig.add_trace(go.Histogram(x=phases, name='Phase', nbinsx=50), row=1, col=2)
|
| 725 |
+
|
| 726 |
+
fig.update_layout(
|
| 727 |
+
title_text="Informational-Entropy Geometry",
|
| 728 |
+
showlegend=False,
|
| 729 |
+
bargap=0.1,
|
| 730 |
+
margin=dict(l=20, r=20, t=60, b=20)
|
| 731 |
+
)
|
| 732 |
|
| 733 |
return fig
|
| 734 |
|
| 735 |
+
def update_manifold_visualization(species_selection, emotion_selection, lens_selection,
|
| 736 |
+
alpha_min, alpha_max, srl_min, srl_max,
|
| 737 |
+
point_size, show_boundary, show_trajectories, color_scheme):
|
| 738 |
+
"""Update manifold visualization with filters."""
|
| 739 |
+
|
| 740 |
+
df_filtered = df_combined.copy()
|
| 741 |
+
|
| 742 |
+
if species_selection:
|
| 743 |
+
df_filtered = df_filtered[df_filtered['source'].isin(species_selection)]
|
| 744 |
+
|
| 745 |
+
if emotion_selection:
|
| 746 |
+
df_filtered = df_filtered[df_filtered['label'].isin(emotion_selection)]
|
| 747 |
+
|
| 748 |
+
alpha_col = f"diag_alpha_{lens_selection}"
|
| 749 |
+
srl_col = f"diag_srl_{lens_selection}"
|
| 750 |
+
|
| 751 |
+
if alpha_col in df_filtered.columns:
|
| 752 |
+
df_filtered = df_filtered[
|
| 753 |
+
(df_filtered[alpha_col] >= alpha_min) &
|
| 754 |
+
(df_filtered[alpha_col] <= alpha_max)
|
| 755 |
+
]
|
| 756 |
+
|
| 757 |
+
if srl_col in df_filtered.columns:
|
| 758 |
+
df_filtered = df_filtered[
|
| 759 |
+
(df_filtered[srl_col] >= srl_min) &
|
| 760 |
+
(df_filtered[srl_col] <= srl_max)
|
| 761 |
+
]
|
| 762 |
+
|
| 763 |
+
if len(df_filtered) == 0:
|
| 764 |
+
empty_fig = go.Figure().add_annotation(
|
| 765 |
+
text="No data points match the current filters",
|
| 766 |
+
xref="paper", yref="paper", x=0.5, y=0.5, showarrow=False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 767 |
)
|
| 768 |
+
return empty_fig
|
| 769 |
+
|
| 770 |
+
return create_enhanced_manifold_plot(
|
| 771 |
+
df_filtered, lens_selection, color_scheme, point_size,
|
| 772 |
+
show_boundary, show_trajectories
|
| 773 |
+
)
|
| 774 |
|
| 775 |
# ---------------------------------------------------------------
|
| 776 |
# Gradio Interface
|
| 777 |
# ---------------------------------------------------------------
|
| 778 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="teal", secondary_hue="cyan")) as demo:
|
| 779 |
gr.Markdown("""
|
| 780 |
+
# 🌟 **CMT Holographic Information Geometry Engine**
|
| 781 |
+
*Full-featured visualization suite with mathematical rigor*
|
| 782 |
+
|
| 783 |
+
**Features:**
|
| 784 |
+
- Complete holographic field reconstruction
|
| 785 |
+
- Cross-species communication mapping
|
| 786 |
+
- Interactive 3D manifold exploration
|
| 787 |
+
- Entropy and phase analysis
|
| 788 |
+
- Side-by-side comparison capabilities
|
| 789 |
+
- Automatic neighbor finding for grammar mapping
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 790 |
""")
|
| 791 |
|
| 792 |
+
with gr.Tabs():
|
| 793 |
+
with gr.TabItem("🌌 Universal Manifold Explorer"):
|
| 794 |
+
gr.Markdown("# 🎯 **Interspecies Communication Map**")
|
| 795 |
|
| 796 |
+
with gr.Row():
|
| 797 |
+
with gr.Column(scale=1):
|
| 798 |
+
gr.Markdown("### 🔬 **Analysis Controls**")
|
| 799 |
+
|
| 800 |
+
species_filter = gr.CheckboxGroup(
|
| 801 |
+
label="Species Selection",
|
| 802 |
+
choices=["Human", "Dog"],
|
| 803 |
+
value=["Human", "Dog"]
|
| 804 |
+
)
|
| 805 |
+
|
| 806 |
+
emotion_filter = gr.CheckboxGroup(
|
| 807 |
+
label="Emotional States",
|
| 808 |
+
choices=list(df_combined['label'].unique()),
|
| 809 |
+
value=list(df_combined['label'].unique())
|
| 810 |
+
)
|
| 811 |
+
|
| 812 |
+
lens_selector = gr.Dropdown(
|
| 813 |
+
label="Mathematical Lens",
|
| 814 |
+
choices=["gamma", "zeta", "airy", "bessel"],
|
| 815 |
+
value="gamma"
|
| 816 |
+
)
|
| 817 |
+
|
| 818 |
+
with gr.Accordion("🎛️ Advanced Filters", open=False):
|
| 819 |
+
alpha_min = gr.Slider(label="Alpha Min", minimum=0, maximum=5, value=0, step=0.1)
|
| 820 |
+
alpha_max = gr.Slider(label="Alpha Max", minimum=0, maximum=5, value=5, step=0.1)
|
| 821 |
+
srl_min = gr.Slider(label="SRL Min", minimum=0, maximum=100, value=0, step=1)
|
| 822 |
+
srl_max = gr.Slider(label="SRL Max", minimum=0, maximum=100, value=100, step=1)
|
| 823 |
+
|
| 824 |
+
with gr.Accordion("🎨 Visualization Options", open=True):
|
| 825 |
+
point_size = gr.Slider(label="Point Size", minimum=2, maximum=15, value=6, step=1)
|
| 826 |
+
show_species_boundary = gr.Checkbox(label="Show Species Boundary", value=True)
|
| 827 |
+
show_trajectories = gr.Checkbox(label="Show Trajectories", value=False)
|
| 828 |
+
color_scheme = gr.Dropdown(
|
| 829 |
+
label="Color Scheme",
|
| 830 |
+
choices=["Species", "Emotion", "CMT_Alpha", "CMT_SRL", "Cluster"],
|
| 831 |
+
value="Species"
|
| 832 |
+
)
|
| 833 |
+
|
| 834 |
+
with gr.Column(scale=3):
|
| 835 |
+
manifold_plot = gr.Plot(label="Universal Communication Manifold")
|
| 836 |
|
| 837 |
+
# Wire up events
|
| 838 |
+
manifold_inputs = [
|
| 839 |
+
species_filter, emotion_filter, lens_selector,
|
| 840 |
+
alpha_min, alpha_max, srl_min, srl_max,
|
| 841 |
+
point_size, show_species_boundary, show_trajectories, color_scheme
|
| 842 |
+
]
|
| 843 |
|
| 844 |
+
for component in manifold_inputs:
|
| 845 |
+
component.change(
|
| 846 |
+
update_manifold_visualization,
|
| 847 |
+
inputs=manifold_inputs,
|
| 848 |
+
outputs=[manifold_plot]
|
| 849 |
+
)
|
| 850 |
+
|
| 851 |
+
with gr.TabItem("🔬 Interactive Holography"):
|
| 852 |
+
with gr.Row():
|
| 853 |
+
with gr.Column(scale=1):
|
| 854 |
+
gr.Markdown("### Cross-Species Holography")
|
| 855 |
+
|
| 856 |
+
species_dropdown = gr.Dropdown(
|
| 857 |
+
label="Select Species",
|
| 858 |
+
choices=["Dog", "Human"],
|
| 859 |
+
value="Dog"
|
| 860 |
+
)
|
| 861 |
+
|
| 862 |
+
dog_files = df_combined[df_combined["source"] == "Dog"]["filepath"].tolist()
|
| 863 |
+
human_files = df_combined[df_combined["source"] == "Human"]["filepath"].tolist()
|
| 864 |
+
|
| 865 |
+
primary_dropdown = gr.Dropdown(
|
| 866 |
+
label="Primary File",
|
| 867 |
+
choices=dog_files,
|
| 868 |
+
value=dog_files[0] if dog_files else None
|
| 869 |
+
)
|
| 870 |
+
|
| 871 |
+
neighbor_dropdown = gr.Dropdown(
|
| 872 |
+
label="Cross-Species Neighbor",
|
| 873 |
+
choices=human_files,
|
| 874 |
+
value=human_files[0] if human_files else None
|
| 875 |
+
)
|
| 876 |
+
|
| 877 |
+
holo_lens_dropdown = gr.Dropdown(
|
| 878 |
+
label="CMT Lens",
|
| 879 |
+
choices=["gamma", "zeta", "airy", "bessel"],
|
| 880 |
+
value="gamma"
|
| 881 |
+
)
|
| 882 |
+
|
| 883 |
+
holo_resolution_slider = gr.Slider(
|
| 884 |
+
label="Field Resolution",
|
| 885 |
+
minimum=20, maximum=100, step=5, value=40
|
| 886 |
+
)
|
| 887 |
+
|
| 888 |
+
holo_wavelength_slider = gr.Slider(
|
| 889 |
+
label="Wavelength (nm)",
|
| 890 |
+
minimum=380, maximum=750, step=5, value=550
|
| 891 |
+
)
|
| 892 |
+
|
| 893 |
+
primary_info_html = gr.HTML(label="Primary Info")
|
| 894 |
+
neighbor_info_html = gr.HTML(label="Neighbor Info")
|
| 895 |
+
|
| 896 |
+
with gr.Column(scale=2):
|
| 897 |
+
dual_holography_plot = gr.Plot(label="Holographic Comparison")
|
| 898 |
+
diagnostic_plot = gr.Plot(label="Diagnostic Analysis")
|
| 899 |
+
entropy_plot = gr.Plot(label="Entropy Geometry")
|
| 900 |
+
|
| 901 |
+
def update_cross_species_view(species, primary_file, neighbor_file, lens, resolution, wavelength):
|
| 902 |
+
if not primary_file:
|
| 903 |
+
empty_fig = go.Figure(layout={"title": "Select a primary file"})
|
| 904 |
+
return empty_fig, empty_fig, empty_fig, "", ""
|
| 905 |
+
|
| 906 |
+
primary_row = df_combined[
|
| 907 |
+
(df_combined["filepath"] == primary_file) &
|
| 908 |
+
(df_combined["source"] == species)
|
| 909 |
+
].iloc[0] if len(df_combined[
|
| 910 |
+
(df_combined["filepath"] == primary_file) &
|
| 911 |
+
(df_combined["source"] == species)
|
| 912 |
+
]) > 0 else None
|
| 913 |
+
|
| 914 |
+
if primary_row is None:
|
| 915 |
+
empty_fig = go.Figure(layout={"title": "Primary file not found"})
|
| 916 |
+
return empty_fig, empty_fig, empty_fig, "", ""
|
| 917 |
+
|
| 918 |
+
if not neighbor_file:
|
| 919 |
+
neighbor_row = find_nearest_cross_species_neighbor(primary_row, df_combined)
|
| 920 |
+
else:
|
| 921 |
+
opposite_species = 'Human' if species == 'Dog' else 'Dog'
|
| 922 |
+
neighbor_row = df_combined[
|
| 923 |
+
(df_combined["filepath"] == neighbor_file) &
|
| 924 |
+
(df_combined["source"] == opposite_species)
|
| 925 |
+
].iloc[0] if len(df_combined[
|
| 926 |
+
(df_combined["filepath"] == neighbor_file) &
|
| 927 |
+
(df_combined["source"] == opposite_species)
|
| 928 |
+
]) > 0 else None
|
| 929 |
+
|
| 930 |
+
primary_cmt = get_cmt_data_from_csv(primary_row, lens)
|
| 931 |
+
neighbor_cmt = get_cmt_data_from_csv(neighbor_row, lens) if neighbor_row is not None else None
|
| 932 |
+
|
| 933 |
+
if primary_cmt and neighbor_cmt:
|
| 934 |
+
primary_title = f"{species}: {primary_row.get('label', 'Unknown')}"
|
| 935 |
+
neighbor_title = f"{neighbor_row['source']}: {neighbor_row.get('label', 'Unknown')}"
|
| 936 |
+
|
| 937 |
+
dual_holo = create_dual_holography_plot(
|
| 938 |
+
primary_cmt["z"], primary_cmt["phi"],
|
| 939 |
+
neighbor_cmt["z"], neighbor_cmt["phi"],
|
| 940 |
+
resolution, wavelength, primary_title, neighbor_title
|
| 941 |
+
)
|
| 942 |
+
|
| 943 |
+
diag = create_diagnostic_plots(primary_cmt["z"], primary_cmt["w"])
|
| 944 |
+
entropy = create_entropy_geometry_plot(primary_cmt["phi"])
|
| 945 |
+
else:
|
| 946 |
+
dual_holo = go.Figure(layout={"title": "Error processing data"})
|
| 947 |
+
diag = go.Figure(layout={"title": "Error processing data"})
|
| 948 |
+
entropy = go.Figure(layout={"title": "Error processing data"})
|
| 949 |
+
|
| 950 |
+
primary_info = f"""
|
| 951 |
+
<b>Primary:</b> {primary_row['filepath']}<br>
|
| 952 |
+
<b>Species:</b> {primary_row['source']}<br>
|
| 953 |
+
<b>Label:</b> {primary_row.get('label', 'N/A')}<br>
|
| 954 |
+
<b>Alpha:</b> {primary_cmt['alpha']:.4f}<br>
|
| 955 |
+
<b>SRL:</b> {primary_cmt['srl']:.4f}
|
| 956 |
+
""" if primary_cmt else ""
|
| 957 |
+
|
| 958 |
+
neighbor_info = f"""
|
| 959 |
+
<b>Neighbor:</b> {neighbor_row['filepath'] if neighbor_row is not None else 'N/A'}<br>
|
| 960 |
+
<b>Species:</b> {neighbor_row['source'] if neighbor_row is not None else 'N/A'}<br>
|
| 961 |
+
<b>Label:</b> {neighbor_row.get('label', 'N/A') if neighbor_row is not None else 'N/A'}<br>
|
| 962 |
+
<b>Alpha:</b> {neighbor_cmt['alpha']:.4f if neighbor_cmt else 0}<br>
|
| 963 |
+
<b>SRL:</b> {neighbor_cmt['srl']:.4f if neighbor_cmt else 0}
|
| 964 |
+
""" if neighbor_cmt else ""
|
| 965 |
+
|
| 966 |
+
return dual_holo, diag, entropy, primary_info, neighbor_info
|
| 967 |
+
|
| 968 |
+
def update_dropdowns_on_species_change(species):
|
| 969 |
+
species_files = df_combined[df_combined["source"] == species]["filepath"].tolist()
|
| 970 |
+
opposite_species = 'Human' if species == 'Dog' else 'Dog'
|
| 971 |
+
neighbor_files = df_combined[df_combined["source"] == opposite_species]["filepath"].tolist()
|
| 972 |
+
|
| 973 |
+
return (
|
| 974 |
+
gr.Dropdown(choices=species_files, value=species_files[0] if species_files else ""),
|
| 975 |
+
gr.Dropdown(choices=neighbor_files, value=neighbor_files[0] if neighbor_files else "")
|
| 976 |
+
)
|
| 977 |
|
| 978 |
+
species_dropdown.change(
|
| 979 |
+
update_dropdowns_on_species_change,
|
| 980 |
+
inputs=[species_dropdown],
|
| 981 |
+
outputs=[primary_dropdown, neighbor_dropdown]
|
|
|
|
| 982 |
)
|
| 983 |
+
|
| 984 |
+
cross_species_inputs = [
|
| 985 |
+
species_dropdown, primary_dropdown, neighbor_dropdown,
|
| 986 |
+
holo_lens_dropdown, holo_resolution_slider, holo_wavelength_slider
|
| 987 |
+
]
|
| 988 |
|
| 989 |
+
cross_species_outputs = [
|
| 990 |
+
dual_holography_plot, diagnostic_plot, entropy_plot,
|
| 991 |
+
primary_info_html, neighbor_info_html
|
| 992 |
+
]
|
| 993 |
+
|
| 994 |
+
for input_component in cross_species_inputs:
|
| 995 |
+
input_component.change(
|
| 996 |
+
update_cross_species_view,
|
| 997 |
+
inputs=cross_species_inputs,
|
| 998 |
+
outputs=cross_species_outputs
|
| 999 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1000 |
|
| 1001 |
+
print("✅ CMT Holographic Visualization Suite Ready!")
|
| 1002 |
|
| 1003 |
if __name__ == "__main__":
|
| 1004 |
demo.launch(share=False, debug=False)
|