Spaces:
Sleeping
(FEAT)[Enhanced Analysis Page]: Add Enhanced Analysis page for advanced multi-modal spectroscopy
Browse files- Created `pages/Enhanced_Analysis.py` for advanced polymer spectroscopy analysis using modern ML architectures.
- Integrated session state initialization for enhanced data manager, multi-modal engine, ML pipeline, and transparent AI engine.
- Implemented spectrum upload and processing workflow with intelligent preprocessing recommendations and provenance tracking.
- Added transparent AI analysis with model selection, explainable predictions, confidence metrics, and hypothesis generation.
- Rendered comprehensive analysis results including probability distributions, reasoning chains, feature importance, uncertainty analysis, and confidence intervals.
- Provided data provenance and quality assessment views with metadata, processing timeline, and quality metrics.
- Structured sidebar navigation for switching between spectrum upload, AI analysis, and provenance views.
- Improved error handling, user feedback, and UI responsiveness throughout.
- Enhanced documentation and
- pages/Enhanced_Analysis.py +434 -433
|
@@ -1,433 +1,434 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Enhanced Analysis Page
|
| 3 |
-
Advanced multi-modal spectroscopy analysis with modern ML architecture
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
-
import streamlit as st
|
| 7 |
-
import torch
|
| 8 |
-
import numpy as np
|
| 9 |
-
import matplotlib.pyplot as plt
|
| 10 |
-
from pathlib import Path
|
| 11 |
-
import io
|
| 12 |
-
from PIL import Image
|
| 13 |
-
|
| 14 |
-
# Import POLYMEROS components
|
| 15 |
-
import sys
|
| 16 |
-
import os
|
| 17 |
-
|
| 18 |
-
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), "modules"))
|
| 19 |
-
|
| 20 |
-
from modules.transparent_ai import TransparentAIEngine, PredictionExplanation
|
| 21 |
-
from modules.enhanced_data import (
|
| 22 |
-
EnhancedDataManager,
|
| 23 |
-
ContextualSpectrum,
|
| 24 |
-
SpectralMetadata,
|
| 25 |
-
)
|
| 26 |
-
from modules.advanced_spectroscopy import MultiModalSpectroscopyEngine
|
| 27 |
-
from modules.modern_ml_architecture import (
|
| 28 |
-
ModernMLPipeline,
|
| 29 |
-
)
|
| 30 |
-
from modules.enhanced_data_pipeline import EnhancedDataPipeline
|
| 31 |
-
from core_logic import load_model, parse_spectrum_data
|
| 32 |
-
from
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
st.session_state.ml_pipeline
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
st.session_state.
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 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 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
"
|
| 148 |
-
"
|
| 149 |
-
"
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
st.
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
st.write(f"**
|
| 169 |
-
st.write(f"**
|
| 170 |
-
st.write(f"**
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
"
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
ax.
|
| 253 |
-
ax.
|
| 254 |
-
ax.
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
ax.
|
| 281 |
-
ax.
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
st.write(f"**
|
| 345 |
-
st.write(f"**
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
st.write(f"**
|
| 364 |
-
st.write(f"**
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
st.write(f"**
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
st.
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
"
|
| 402 |
-
"
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
st.sidebar.markdown("
|
| 417 |
-
st.sidebar.markdown("
|
| 418 |
-
st.sidebar.markdown("β’
|
| 419 |
-
st.sidebar.markdown("β’
|
| 420 |
-
st.sidebar.markdown("β’
|
| 421 |
-
st.sidebar.markdown("β’
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Enhanced Analysis Page
|
| 3 |
+
Advanced multi-modal spectroscopy analysis with modern ML architecture
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import streamlit as st
|
| 7 |
+
import torch
|
| 8 |
+
import numpy as np
|
| 9 |
+
import matplotlib.pyplot as plt
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
import io
|
| 12 |
+
from PIL import Image
|
| 13 |
+
|
| 14 |
+
# Import POLYMEROS components
|
| 15 |
+
import sys
|
| 16 |
+
import os
|
| 17 |
+
|
| 18 |
+
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), "modules"))
|
| 19 |
+
|
| 20 |
+
from modules.transparent_ai import TransparentAIEngine, PredictionExplanation
|
| 21 |
+
from modules.enhanced_data import (
|
| 22 |
+
EnhancedDataManager,
|
| 23 |
+
ContextualSpectrum,
|
| 24 |
+
SpectralMetadata,
|
| 25 |
+
)
|
| 26 |
+
from modules.advanced_spectroscopy import MultiModalSpectroscopyEngine
|
| 27 |
+
from modules.modern_ml_architecture import (
|
| 28 |
+
ModernMLPipeline,
|
| 29 |
+
)
|
| 30 |
+
from modules.enhanced_data_pipeline import EnhancedDataPipeline
|
| 31 |
+
from core_logic import load_model, parse_spectrum_data
|
| 32 |
+
from models.registry import choices
|
| 33 |
+
from config import TARGET_LEN
|
| 34 |
+
|
| 35 |
+
# Removed unused preprocess_spectrum import
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def init_enhanced_analysis():
|
| 39 |
+
"""Initialize enhanced analysis session state with new components"""
|
| 40 |
+
if "data_manager" not in st.session_state:
|
| 41 |
+
st.session_state.data_manager = EnhancedDataManager()
|
| 42 |
+
|
| 43 |
+
if "spectroscopy_engine" not in st.session_state:
|
| 44 |
+
st.session_state.spectroscopy_engine = MultiModalSpectroscopyEngine()
|
| 45 |
+
|
| 46 |
+
if "ml_pipeline" not in st.session_state:
|
| 47 |
+
st.session_state.ml_pipeline = ModernMLPipeline()
|
| 48 |
+
st.session_state.ml_pipeline.initialize_models()
|
| 49 |
+
|
| 50 |
+
if "data_pipeline" not in st.session_state:
|
| 51 |
+
st.session_state.data_pipeline = EnhancedDataPipeline()
|
| 52 |
+
|
| 53 |
+
if "transparent_ai" not in st.session_state:
|
| 54 |
+
st.session_state.transparent_ai = None
|
| 55 |
+
|
| 56 |
+
if "current_model" not in st.session_state:
|
| 57 |
+
st.session_state.current_model = None
|
| 58 |
+
|
| 59 |
+
if "analysis_results" not in st.session_state:
|
| 60 |
+
st.session_state.analysis_results = None
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def load_enhanced_model(model_name: str):
|
| 64 |
+
"""Load model and initialize transparent AI engine"""
|
| 65 |
+
try:
|
| 66 |
+
model = load_model(model_name)
|
| 67 |
+
if model is not None:
|
| 68 |
+
st.session_state.current_model = model
|
| 69 |
+
st.session_state.transparent_ai = TransparentAIEngine(model)
|
| 70 |
+
return True
|
| 71 |
+
return False
|
| 72 |
+
except Exception as e:
|
| 73 |
+
st.error(f"Error loading model: {e}")
|
| 74 |
+
return False
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def render_enhanced_file_upload():
|
| 78 |
+
"""Render enhanced file upload with metadata extraction"""
|
| 79 |
+
st.header("π Enhanced Spectrum Analysis")
|
| 80 |
+
|
| 81 |
+
uploaded_file = st.file_uploader(
|
| 82 |
+
"Upload spectrum file (.txt)",
|
| 83 |
+
type=["txt"],
|
| 84 |
+
help="Upload a Raman or FTIR spectrum in text format",
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
if uploaded_file is not None:
|
| 88 |
+
# Parse spectrum data
|
| 89 |
+
try:
|
| 90 |
+
content = uploaded_file.read().decode("utf-8")
|
| 91 |
+
x_data, y_data = parse_spectrum_data(content)
|
| 92 |
+
|
| 93 |
+
# Create enhanced spectrum with metadata
|
| 94 |
+
metadata = SpectralMetadata(
|
| 95 |
+
filename=uploaded_file.name,
|
| 96 |
+
instrument_type="Raman", # Default, could be detected from filename
|
| 97 |
+
data_quality_score=None,
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
spectrum = ContextualSpectrum(x_data, y_data, metadata)
|
| 101 |
+
|
| 102 |
+
# Get data quality assessment
|
| 103 |
+
data_manager = st.session_state.data_manager
|
| 104 |
+
quality_score = data_manager._assess_data_quality(y_data)
|
| 105 |
+
spectrum.metadata.data_quality_score = quality_score
|
| 106 |
+
|
| 107 |
+
# Display quality assessment
|
| 108 |
+
col1, col2, col3 = st.columns(3)
|
| 109 |
+
with col1:
|
| 110 |
+
st.metric("Data Points", len(x_data))
|
| 111 |
+
with col2:
|
| 112 |
+
st.metric("Quality Score", f"{quality_score:.2f}")
|
| 113 |
+
with col3:
|
| 114 |
+
quality_color = (
|
| 115 |
+
"π’"
|
| 116 |
+
if quality_score > 0.7
|
| 117 |
+
else "π‘" if quality_score > 0.4 else "π΄"
|
| 118 |
+
)
|
| 119 |
+
st.metric("Quality", f"{quality_color}")
|
| 120 |
+
|
| 121 |
+
# Get preprocessing recommendations
|
| 122 |
+
recommendations = data_manager.get_preprocessing_recommendations(spectrum)
|
| 123 |
+
|
| 124 |
+
st.subheader("Intelligent Preprocessing Recommendations")
|
| 125 |
+
rec_col1, rec_col2 = st.columns(2)
|
| 126 |
+
|
| 127 |
+
with rec_col1:
|
| 128 |
+
st.write("**Recommended settings:**")
|
| 129 |
+
for param, value in recommendations.items():
|
| 130 |
+
st.write(f"β’ {param}: {value}")
|
| 131 |
+
|
| 132 |
+
with rec_col2:
|
| 133 |
+
st.write("**Manual override:**")
|
| 134 |
+
do_baseline = st.checkbox(
|
| 135 |
+
"Baseline correction",
|
| 136 |
+
value=recommendations.get("do_baseline", True),
|
| 137 |
+
)
|
| 138 |
+
do_smooth = st.checkbox(
|
| 139 |
+
"Smoothing", value=recommendations.get("do_smooth", True)
|
| 140 |
+
)
|
| 141 |
+
do_normalize = st.checkbox(
|
| 142 |
+
"Normalization", value=recommendations.get("do_normalize", True)
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
# Apply preprocessing with tracking
|
| 146 |
+
preprocessing_params = {
|
| 147 |
+
"do_baseline": do_baseline,
|
| 148 |
+
"do_smooth": do_smooth,
|
| 149 |
+
"do_normalize": do_normalize,
|
| 150 |
+
"target_len": TARGET_LEN,
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
if st.button("Process and Analyze"):
|
| 154 |
+
with st.spinner("Processing spectrum with provenance tracking..."):
|
| 155 |
+
# Apply preprocessing with full tracking
|
| 156 |
+
processed_spectrum = data_manager.preprocess_with_tracking(
|
| 157 |
+
spectrum, **preprocessing_params
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
# Store processed spectrum
|
| 161 |
+
st.session_state.processed_spectrum = processed_spectrum
|
| 162 |
+
st.success("Spectrum processed with full provenance tracking!")
|
| 163 |
+
|
| 164 |
+
# Display provenance information
|
| 165 |
+
st.subheader("Processing Provenance")
|
| 166 |
+
for record in processed_spectrum.provenance:
|
| 167 |
+
with st.expander(f"Operation: {record.operation}"):
|
| 168 |
+
st.write(f"**Timestamp:** {record.timestamp}")
|
| 169 |
+
st.write(f"**Parameters:** {record.parameters}")
|
| 170 |
+
st.write(f"**Input hash:** {record.input_hash}")
|
| 171 |
+
st.write(f"**Output hash:** {record.output_hash}")
|
| 172 |
+
|
| 173 |
+
except Exception as e:
|
| 174 |
+
st.error(f"Error processing file: {e}")
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
def render_transparent_analysis():
|
| 178 |
+
"""Render transparent AI analysis with explanations"""
|
| 179 |
+
if "processed_spectrum" not in st.session_state:
|
| 180 |
+
st.info("Please upload and process a spectrum first.")
|
| 181 |
+
return
|
| 182 |
+
|
| 183 |
+
st.header("π§ Transparent AI Analysis")
|
| 184 |
+
|
| 185 |
+
# Model selection
|
| 186 |
+
model_names = choices()
|
| 187 |
+
selected_model = st.selectbox("Select AI model:", model_names)
|
| 188 |
+
|
| 189 |
+
if st.session_state.current_model is None or st.button("Load Model"):
|
| 190 |
+
with st.spinner(f"Loading {selected_model} model..."):
|
| 191 |
+
if load_enhanced_model(selected_model):
|
| 192 |
+
st.success(f"Model {selected_model} loaded successfully!")
|
| 193 |
+
else:
|
| 194 |
+
st.error("Failed to load model")
|
| 195 |
+
return
|
| 196 |
+
|
| 197 |
+
if st.session_state.transparent_ai is not None:
|
| 198 |
+
spectrum = st.session_state.processed_spectrum
|
| 199 |
+
|
| 200 |
+
if st.button("Run Transparent Analysis"):
|
| 201 |
+
with st.spinner("Running comprehensive analysis..."):
|
| 202 |
+
# Prepare input tensor
|
| 203 |
+
y_processed = spectrum.y_data
|
| 204 |
+
x_input = torch.tensor(y_processed, dtype=torch.float32).unsqueeze(0)
|
| 205 |
+
|
| 206 |
+
# Get transparent explanation
|
| 207 |
+
explanation = st.session_state.transparent_ai.predict_with_explanation(
|
| 208 |
+
x_input, wavenumbers=spectrum.x_data
|
| 209 |
+
)
|
| 210 |
+
|
| 211 |
+
# Generate hypotheses
|
| 212 |
+
hypotheses = st.session_state.transparent_ai.generate_hypotheses(
|
| 213 |
+
explanation
|
| 214 |
+
)
|
| 215 |
+
|
| 216 |
+
# Store results
|
| 217 |
+
st.session_state.analysis_results = {
|
| 218 |
+
"explanation": explanation,
|
| 219 |
+
"hypotheses": hypotheses,
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
# Display results
|
| 223 |
+
render_analysis_results(explanation, hypotheses)
|
| 224 |
+
|
| 225 |
+
|
| 226 |
+
def render_analysis_results(explanation: PredictionExplanation, hypotheses: list):
|
| 227 |
+
"""Render comprehensive analysis results"""
|
| 228 |
+
st.subheader("π― Prediction Results")
|
| 229 |
+
|
| 230 |
+
# Main prediction
|
| 231 |
+
class_names = ["Stable", "Weathered"]
|
| 232 |
+
predicted_class = class_names[explanation.prediction]
|
| 233 |
+
|
| 234 |
+
col1, col2, col3 = st.columns(3)
|
| 235 |
+
with col1:
|
| 236 |
+
st.metric("Prediction", predicted_class)
|
| 237 |
+
with col2:
|
| 238 |
+
st.metric("Confidence", f"{explanation.confidence:.3f}")
|
| 239 |
+
with col3:
|
| 240 |
+
confidence_emoji = (
|
| 241 |
+
"π’"
|
| 242 |
+
if explanation.confidence_level == "HIGH"
|
| 243 |
+
else "π‘" if explanation.confidence_level == "MEDIUM" else "π΄"
|
| 244 |
+
)
|
| 245 |
+
st.metric("Level", f"{confidence_emoji} {explanation.confidence_level}")
|
| 246 |
+
|
| 247 |
+
# Probability distribution
|
| 248 |
+
st.subheader("π Probability Distribution")
|
| 249 |
+
prob_data = {"Class": class_names, "Probability": explanation.probabilities}
|
| 250 |
+
|
| 251 |
+
fig, ax = plt.subplots(figsize=(8, 5))
|
| 252 |
+
bars = ax.bar(prob_data["Class"], prob_data["Probability"])
|
| 253 |
+
ax.set_ylabel("Probability")
|
| 254 |
+
ax.set_title("Class Probabilities")
|
| 255 |
+
ax.set_ylim(0, 1)
|
| 256 |
+
|
| 257 |
+
# Color bars based on prediction
|
| 258 |
+
for i, bar in enumerate(bars):
|
| 259 |
+
if i == explanation.prediction:
|
| 260 |
+
bar.set_color("steelblue")
|
| 261 |
+
else:
|
| 262 |
+
bar.set_color("lightgray")
|
| 263 |
+
|
| 264 |
+
st.pyplot(fig)
|
| 265 |
+
|
| 266 |
+
# Reasoning chain
|
| 267 |
+
st.subheader("π AI Reasoning Chain")
|
| 268 |
+
for i, reasoning in enumerate(explanation.reasoning_chain):
|
| 269 |
+
st.write(f"{i+1}. {reasoning}")
|
| 270 |
+
|
| 271 |
+
# Feature importance
|
| 272 |
+
if explanation.feature_importance:
|
| 273 |
+
st.subheader("π― Feature Importance Analysis")
|
| 274 |
+
|
| 275 |
+
# Create feature importance plot
|
| 276 |
+
features = list(explanation.feature_importance.keys())
|
| 277 |
+
importances = list(explanation.feature_importance.values())
|
| 278 |
+
|
| 279 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
| 280 |
+
bars = ax.barh(features, importances)
|
| 281 |
+
ax.set_xlabel("Importance Score")
|
| 282 |
+
ax.set_title("Spectral Region Importance")
|
| 283 |
+
|
| 284 |
+
# Color bars based on importance
|
| 285 |
+
for bar, importance in zip(bars, importances):
|
| 286 |
+
if abs(importance) > 0.5:
|
| 287 |
+
bar.set_color("red")
|
| 288 |
+
elif abs(importance) > 0.3:
|
| 289 |
+
bar.set_color("orange")
|
| 290 |
+
else:
|
| 291 |
+
bar.set_color("lightblue")
|
| 292 |
+
|
| 293 |
+
plt.tight_layout()
|
| 294 |
+
st.pyplot(fig)
|
| 295 |
+
|
| 296 |
+
# Uncertainty analysis
|
| 297 |
+
st.subheader("π€ Uncertainty Analysis")
|
| 298 |
+
for source in explanation.uncertainty_sources:
|
| 299 |
+
st.write(f"β’ {source}")
|
| 300 |
+
|
| 301 |
+
# Confidence intervals
|
| 302 |
+
if explanation.confidence_intervals:
|
| 303 |
+
st.subheader("π Confidence Intervals")
|
| 304 |
+
for class_name, (lower, upper) in explanation.confidence_intervals.items():
|
| 305 |
+
st.write(f"**{class_name}:** [{lower:.3f}, {upper:.3f}]")
|
| 306 |
+
|
| 307 |
+
# AI-generated hypotheses
|
| 308 |
+
if hypotheses:
|
| 309 |
+
st.subheader("π§ͺ AI-Generated Scientific Hypotheses")
|
| 310 |
+
|
| 311 |
+
for i, hypothesis in enumerate(hypotheses):
|
| 312 |
+
with st.expander(f"Hypothesis {i+1}: {hypothesis.statement}"):
|
| 313 |
+
st.write(f"**Confidence:** {hypothesis.confidence:.3f}")
|
| 314 |
+
|
| 315 |
+
st.write("**Supporting Evidence:**")
|
| 316 |
+
for evidence in hypothesis.supporting_evidence:
|
| 317 |
+
st.write(f"β’ {evidence}")
|
| 318 |
+
|
| 319 |
+
st.write("**Testable Predictions:**")
|
| 320 |
+
for prediction in hypothesis.testable_predictions:
|
| 321 |
+
st.write(f"β’ {prediction}")
|
| 322 |
+
|
| 323 |
+
st.write("**Suggested Experiments:**")
|
| 324 |
+
for experiment in hypothesis.suggested_experiments:
|
| 325 |
+
st.write(f"β’ {experiment}")
|
| 326 |
+
|
| 327 |
+
|
| 328 |
+
def render_data_provenance():
|
| 329 |
+
"""Render data provenance and quality information"""
|
| 330 |
+
if "processed_spectrum" not in st.session_state:
|
| 331 |
+
st.info("No processed spectrum available.")
|
| 332 |
+
return
|
| 333 |
+
|
| 334 |
+
st.header("π Data Provenance & Quality")
|
| 335 |
+
|
| 336 |
+
spectrum = st.session_state.processed_spectrum
|
| 337 |
+
|
| 338 |
+
# Metadata display
|
| 339 |
+
st.subheader("π Spectrum Metadata")
|
| 340 |
+
metadata = spectrum.metadata
|
| 341 |
+
|
| 342 |
+
col1, col2 = st.columns(2)
|
| 343 |
+
with col1:
|
| 344 |
+
st.write(f"**Filename:** {metadata.filename}")
|
| 345 |
+
st.write(f"**Instrument:** {metadata.instrument_type}")
|
| 346 |
+
st.write(f"**Quality Score:** {metadata.data_quality_score:.3f}")
|
| 347 |
+
|
| 348 |
+
with col2:
|
| 349 |
+
if metadata.laser_wavelength:
|
| 350 |
+
st.write(f"**Laser Wavelength:** {metadata.laser_wavelength} nm")
|
| 351 |
+
if metadata.acquisition_date:
|
| 352 |
+
st.write(f"**Acquisition Date:** {metadata.acquisition_date}")
|
| 353 |
+
st.write(f"**Data Hash:** {spectrum.data_hash}")
|
| 354 |
+
|
| 355 |
+
# Provenance timeline
|
| 356 |
+
st.subheader("π Processing Timeline")
|
| 357 |
+
|
| 358 |
+
if spectrum.provenance:
|
| 359 |
+
for i, record in enumerate(spectrum.provenance):
|
| 360 |
+
with st.expander(
|
| 361 |
+
f"Step {i+1}: {record.operation} ({record.timestamp[:19]})"
|
| 362 |
+
):
|
| 363 |
+
st.write(f"**Operation:** {record.operation}")
|
| 364 |
+
st.write(f"**Operator:** {record.operator}")
|
| 365 |
+
st.write(f"**Parameters:**")
|
| 366 |
+
for param, value in record.parameters.items():
|
| 367 |
+
st.write(f" - {param}: {value}")
|
| 368 |
+
st.write(f"**Input Hash:** {record.input_hash}")
|
| 369 |
+
st.write(f"**Output Hash:** {record.output_hash}")
|
| 370 |
+
else:
|
| 371 |
+
st.info("No processing operations recorded yet.")
|
| 372 |
+
|
| 373 |
+
# Quality assessment details
|
| 374 |
+
st.subheader("π Quality Assessment Details")
|
| 375 |
+
|
| 376 |
+
if hasattr(spectrum, "quality_metrics"):
|
| 377 |
+
metrics = spectrum.quality_metrics
|
| 378 |
+
for metric, value in metrics.items():
|
| 379 |
+
st.write(f"**{metric}:** {value}")
|
| 380 |
+
else:
|
| 381 |
+
st.info("Run quality assessment to see detailed metrics.")
|
| 382 |
+
|
| 383 |
+
|
| 384 |
+
def main():
|
| 385 |
+
"""Main enhanced analysis interface"""
|
| 386 |
+
st.set_page_config(
|
| 387 |
+
page_title="POLYMEROS Enhanced Analysis", page_icon="π¬", layout="wide"
|
| 388 |
+
)
|
| 389 |
+
|
| 390 |
+
st.title("π¬ POLYMEROS Enhanced Analysis")
|
| 391 |
+
st.markdown("**Transparent AI with Explainability and Hypothesis Generation**")
|
| 392 |
+
|
| 393 |
+
# Initialize session
|
| 394 |
+
init_enhanced_analysis()
|
| 395 |
+
|
| 396 |
+
# Sidebar navigation
|
| 397 |
+
st.sidebar.title("π§ͺ Analysis Tools")
|
| 398 |
+
analysis_mode = st.sidebar.selectbox(
|
| 399 |
+
"Select analysis mode:",
|
| 400 |
+
[
|
| 401 |
+
"Spectrum Upload & Processing",
|
| 402 |
+
"Transparent AI Analysis",
|
| 403 |
+
"Data Provenance & Quality",
|
| 404 |
+
],
|
| 405 |
+
)
|
| 406 |
+
|
| 407 |
+
# Render selected mode
|
| 408 |
+
if analysis_mode == "Spectrum Upload & Processing":
|
| 409 |
+
render_enhanced_file_upload()
|
| 410 |
+
elif analysis_mode == "Transparent AI Analysis":
|
| 411 |
+
render_transparent_analysis()
|
| 412 |
+
elif analysis_mode == "Data Provenance & Quality":
|
| 413 |
+
render_data_provenance()
|
| 414 |
+
|
| 415 |
+
# Additional information
|
| 416 |
+
st.sidebar.markdown("---")
|
| 417 |
+
st.sidebar.markdown("**Enhanced Features:**")
|
| 418 |
+
st.sidebar.markdown("β’ Complete provenance tracking")
|
| 419 |
+
st.sidebar.markdown("β’ Intelligent preprocessing")
|
| 420 |
+
st.sidebar.markdown("β’ Uncertainty quantification")
|
| 421 |
+
st.sidebar.markdown("β’ AI hypothesis generation")
|
| 422 |
+
st.sidebar.markdown("β’ Explainable predictions")
|
| 423 |
+
|
| 424 |
+
# Display current analysis status
|
| 425 |
+
if st.session_state.analysis_results:
|
| 426 |
+
st.sidebar.success("β
Analysis completed")
|
| 427 |
+
elif "processed_spectrum" in st.session_state:
|
| 428 |
+
st.sidebar.info("π Spectrum processed")
|
| 429 |
+
else:
|
| 430 |
+
st.sidebar.info("π Ready for upload")
|
| 431 |
+
|
| 432 |
+
|
| 433 |
+
if __name__ == "__main__":
|
| 434 |
+
main()
|