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46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
162
163
164
165
166
167
168
169
170
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
253
254
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
281
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# In 2_Enhanced_Analysis.py
"""
Enhanced Analysis Page
Advanced multi-modal spectroscopy analysis with modern ML architecture
"""

import streamlit as st
import torch
import numpy as np
import matplotlib.pyplot as plt
from pathlib import Path
import io
from PIL import Image
import sys
import os

sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), "modules"))

from modules.transparent_ai import TransparentAIEngine, PredictionExplanation
from modules.enhanced_data import (
    EnhancedDataManager,
    ContextualSpectrum,
    SpectralMetadata,
)
from modules.advanced_spectroscopy import MultiModalSpectroscopyEngine
from modules.modern_ml_architecture import (
    ModernMLPipeline,
)
from modules.enhanced_data_pipeline import EnhancedDataPipeline
from core_logic import load_model
from utils.multifile import parse_spectrum_data
from models.registry import choices
from config import TARGET_LEN

# Removed unused preprocess_spectrum import


def init_enhanced_analysis():
    """Initialize enhanced analysis session state with new components"""
    if "data_manager" not in st.session_state:
        st.session_state.data_manager = EnhancedDataManager()

    if "spectroscopy_engine" not in st.session_state:
        st.session_state.spectroscopy_engine = MultiModalSpectroscopyEngine()

    if "ml_pipeline" not in st.session_state:
        st.session_state.ml_pipeline = ModernMLPipeline()
        st.session_state.ml_pipeline.initialize_models()

    if "data_pipeline" not in st.session_state:
        st.session_state.data_pipeline = EnhancedDataPipeline()

    if "transparent_ai" not in st.session_state:
        st.session_state.transparent_ai = None

    if "current_model" not in st.session_state:
        st.session_state.current_model = None

    if "analysis_results" not in st.session_state:
        st.session_state.analysis_results = None


def load_enhanced_model(model_name: str):
    """Load model and initialize transparent AI engine"""
    try:
        model = load_model(model_name)
        if model is not None:
            st.session_state.current_model = model
            st.session_state.transparent_ai = TransparentAIEngine(model)
            return True
        return False
    except Exception as e:
        st.error(f"Error loading model: {e}")
        st.error("Please check the model name and ensure the model file is accessible.")
        st.error("Detailed traceback has been logged for debugging.")
        import traceback

        traceback.print_exc()
        return False


def render_enhanced_file_upload():
    """Render enhanced file upload with metadata extraction"""
    st.subheader("Deeper Analysis on Spectrum  with AI Insights")

    uploaded_file = st.file_uploader(
        "Upload spectrum file (.txt)",
        type=["txt"],
        help="Upload a Raman or FTIR spectrum in text format",
    )

    if uploaded_file is not None:
        # Parse spectrum data
        try:
            content = uploaded_file.read().decode("utf-8")
            x_data, y_data = parse_spectrum_data(content)

            # Create enhanced spectrum with metadata
            metadata = SpectralMetadata(
                filename=uploaded_file.name,
                instrument_type="Raman",  # Default, could be detected from filename
                data_quality_score=None,
            )

            spectrum = ContextualSpectrum(x_data, y_data, metadata)

            # Get data quality assessment
            data_manager = st.session_state.data_manager
            quality_score = data_manager.assess_data_quality(y_data)
            spectrum.metadata.data_quality_score = quality_score

            # Display quality assessment
            col1, col2, col3 = st.columns(3)
            with col1:
                st.metric("Data Points", len(x_data))
            with col2:
                st.metric("Quality Score", f"{quality_score:.2f}")
            with col3:
                quality_color = (
                    "🟒"
                    if quality_score > 0.7
                    else "🟑" if quality_score > 0.4 else "πŸ”΄"
                )
                st.metric("Quality", f"{quality_color}")

            # Get preprocessing recommendations
            recommendations = data_manager.get_preprocessing_recommendations(spectrum)

            st.subheader("Intelligent Preprocessing Recommendations")
            rec_col1, rec_col2 = st.columns(2)

            with rec_col1:
                st.write("**Recommended settings:**")
                for param, value in recommendations.items():
                    st.write(f"β€’ {param}: {value}")

            with rec_col2:
                st.write("**Manual override:**")
                do_baseline = st.checkbox(
                    "Baseline correction",
                    value=recommendations.get("do_baseline", True),
                )
                do_smooth = st.checkbox(
                    "Smoothing", value=recommendations.get("do_smooth", True)
                )
                do_normalize = st.checkbox(
                    "Normalization", value=recommendations.get("do_normalize", True)
                )

            # Apply preprocessing with tracking
            preprocessing_params = {
                "do_baseline": do_baseline,
                "do_smooth": do_smooth,
                "do_normalize": do_normalize,
                "target_len": TARGET_LEN,
            }

            if st.button("Process and Analyze"):
                with st.spinner("Processing spectrum with provenance tracking..."):
                    # Apply preprocessing with full tracking
                    processed_spectrum = data_manager.preprocess_with_tracking(
                        spectrum, **preprocessing_params
                    )

                    # Store processed spectrum
                    st.session_state.processed_spectrum = processed_spectrum
                    st.success("Spectrum processed with full provenance tracking!")

                    # Display provenance information
                    st.subheader("Processing Provenance")
                    for record in processed_spectrum.provenance:
                        with st.expander(f"Operation: {record.operation}"):
                            st.write(f"**Timestamp:** {record.timestamp}")
                            st.write(f"**Parameters:** {record.parameters}")
                            st.write(f"**Input hash:** {record.input_hash}")
                            st.write(f"**Output hash:** {record.output_hash}")

        except Exception as e:
            st.error(f"Error processing file: {e}")


def render_transparent_analysis():
    """Render transparent AI analysis with explanations"""
    if "processed_spectrum" not in st.session_state:
        st.info("Please upload and process a spectrum first.")
        return

    st.header("🧠 Transparent AI Analysis")

    # Model selection
    model_names = choices()
    selected_model = st.selectbox("Select AI model:", model_names)

    if st.session_state.current_model is None or st.button("Load Model"):
        with st.spinner(f"Loading {selected_model} model..."):
            if load_enhanced_model(selected_model):
                st.success(f"Model {selected_model} loaded successfully!")
            else:
                st.error("Failed to load model")
                return

    if st.session_state.transparent_ai is not None:
        spectrum = st.session_state.processed_spectrum

        if st.button("Run Transparent Analysis"):
            with st.spinner("Running comprehensive analysis..."):
                # Prepare input tensor
                y_processed = spectrum.y_data
                x_input = torch.tensor(y_processed, dtype=torch.float32).unsqueeze(0)

                # Get transparent explanation
                explanation = st.session_state.transparent_ai.predict_with_explanation(
                    x_input, wavenumbers=spectrum.x_data
                )

                # Generate hypotheses
                hypotheses = st.session_state.transparent_ai.generate_hypotheses(
                    explanation
                )

                # Store results
                st.session_state.analysis_results = {
                    "explanation": explanation,
                    "hypotheses": hypotheses,
                }

                # Display results
                render_analysis_results(explanation, hypotheses)


def render_analysis_results(explanation: PredictionExplanation, hypotheses: list):
    """Render comprehensive analysis results"""
    st.subheader("🎯 Prediction Results")

    # Main prediction
    class_names = ["Stable", "Weathered"]
    predicted_class = class_names[explanation.prediction]

    col1, col2, col3 = st.columns(3)
    with col1:
        st.metric("Prediction", predicted_class)
    with col2:
        st.metric("Confidence", f"{explanation.confidence:.3f}")
    with col3:
        confidence_emoji = (
            "🟒"
            if explanation.confidence_level == "HIGH"
            else "🟑" if explanation.confidence_level == "MEDIUM" else "πŸ”΄"
        )
        st.metric("Level", f"{confidence_emoji} {explanation.confidence_level}")

    # Probability distribution
    st.subheader("πŸ“Š Probability Distribution")
    prob_data = {"Class": class_names, "Probability": explanation.probabilities}

    fig, ax = plt.subplots(figsize=(8, 5))
    bars = ax.bar(prob_data["Class"], prob_data["Probability"])
    ax.set_ylabel("Probability")
    ax.set_title("Class Probabilities")
    ax.set_ylim(0, 1)

    # Color bars based on prediction
    for i, bar in enumerate(bars):
        if i == explanation.prediction:
            bar.set_color("steelblue")
        else:
            bar.set_color("lightgray")

    st.pyplot(fig)

    # Reasoning chain
    st.subheader("πŸ” AI Reasoning Chain")
    for i, reasoning in enumerate(explanation.reasoning_chain):
        st.write(f"{i+1}. {reasoning}")

    # Feature importance
    if explanation.feature_importance:
        st.subheader("🎯 Feature Importance Analysis")

        # Create feature importance plot
        features = list(explanation.feature_importance.keys())
        importances = list(explanation.feature_importance.values())

        fig, ax = plt.subplots(figsize=(10, 6))
        bars = ax.barh(features, importances)
        ax.set_xlabel("Importance Score")
        ax.set_title("Spectral Region Importance")

        # Color bars based on importance
        for bar, importance in zip(bars, importances):
            if abs(importance) > 0.5:
                bar.set_color("red")
            elif abs(importance) > 0.3:
                bar.set_color("orange")
            else:
                bar.set_color("lightblue")

        plt.tight_layout()
        st.pyplot(fig)

    # Uncertainty analysis
    st.subheader("πŸ€” Uncertainty Analysis")
    for source in explanation.uncertainty_sources:
        st.write(f"β€’ {source}")

    # Confidence intervals
    if explanation.confidence_intervals:
        st.subheader("πŸ“ˆ Confidence Intervals")
        for class_name, (lower, upper) in explanation.confidence_intervals.items():
            st.write(f"**{class_name}:** [{lower:.3f}, {upper:.3f}]")

    # AI-generated hypotheses
    if hypotheses:
        st.subheader("πŸ§ͺ AI-Generated Scientific Hypotheses")

        for i, hypothesis in enumerate(hypotheses):
            with st.expander(f"Hypothesis {i+1}: {hypothesis.statement}"):
                st.write(f"**Confidence:** {hypothesis.confidence:.3f}")

                st.write("**Supporting Evidence:**")
                for evidence in hypothesis.supporting_evidence:
                    st.write(f"β€’ {evidence}")

                st.write("**Testable Predictions:**")
                for prediction in hypothesis.testable_predictions:
                    st.write(f"β€’ {prediction}")

                st.write("**Suggested Experiments:**")
                for experiment in hypothesis.suggested_experiments:
                    st.write(f"β€’ {experiment}")


def render_data_provenance():
    """Render data provenance and quality information"""
    if "processed_spectrum" not in st.session_state:
        st.info("No processed spectrum available.")
        return

    st.header("πŸ“‹ Data Provenance & Quality")

    spectrum = st.session_state.processed_spectrum

    # Metadata display
    st.subheader("πŸ“„ Spectrum Metadata")
    metadata = spectrum.metadata

    col1, col2 = st.columns(2)
    with col1:
        st.write(f"**Filename:** {metadata.filename}")
        st.write(f"**Instrument:** {metadata.instrument_type}")
        st.write(f"**Quality Score:** {metadata.data_quality_score:.3f}")

    with col2:
        if metadata.laser_wavelength:
            st.write(f"**Laser Wavelength:** {metadata.laser_wavelength} nm")
        if metadata.acquisition_date:
            st.write(f"**Acquisition Date:** {metadata.acquisition_date}")
        st.write(f"**Data Hash:** {spectrum.data_hash}")

    # Provenance timeline
    st.subheader("πŸ•’ Processing Timeline")

    if spectrum.provenance:
        for i, record in enumerate(spectrum.provenance):
            with st.expander(
                f"Step {i+1}: {record.operation} ({record.timestamp[:19]})"
            ):
                st.write(f"**Operation:** {record.operation}")
                st.write(f"**Operator:** {record.operator}")
                st.write(f"**Parameters:**")
                for param, value in record.parameters.items():
                    st.write(f"  - {param}: {value}")
                st.write(f"**Input Hash:** {record.input_hash}")
                st.write(f"**Output Hash:** {record.output_hash}")
    else:
        st.info("No processing operations recorded yet.")

    # Quality assessment details
    st.subheader("πŸ” Quality Assessment Details")

    if hasattr(spectrum, "quality_metrics"):
        metrics = spectrum.quality_metrics
        for metric, value in metrics.items():
            st.write(f"**{metric}:** {value}")
    else:
        st.info("Run quality assessment to see detailed metrics.")


def main():
    """Main enhanced analysis interface"""
    st.set_page_config(
        page_title="ML Polymer Enhanced Analysis", page_icon="πŸ”¬", layout="wide"
    )

    st.title("Enhanced Spectrum Analysis")
    st.markdown("**Transparent AI with Explainability and Hypothesis Generation**")

    # Initialize session
    init_enhanced_analysis()

    # Sidebar navigation
    st.sidebar.title("πŸ§ͺ Analysis Tools")
    analysis_mode = st.sidebar.selectbox(
        "Select analysis mode:",
        [
            "Spectrum Upload & Processing",
            "Transparent AI Analysis",
            "Data Provenance & Quality",
        ],
    )

    # Render selected mode
    if analysis_mode == "Spectrum Upload & Processing":
        render_enhanced_file_upload()
    elif analysis_mode == "Transparent AI Analysis":
        render_transparent_analysis()
    elif analysis_mode == "Data Provenance & Quality":
        render_data_provenance()

    # Additional information
    st.sidebar.markdown("---")
    st.sidebar.markdown("**Enhanced Features:**")
    st.sidebar.markdown("β€’ Complete provenance tracking")
    st.sidebar.markdown("β€’ Intelligent preprocessing")
    st.sidebar.markdown("β€’ Uncertainty quantification")
    st.sidebar.markdown("β€’ AI hypothesis generation")
    st.sidebar.markdown("β€’ Explainable predictions")

    # Display current analysis status
    if st.session_state.analysis_results:
        st.sidebar.success("βœ… Analysis completed")
    elif "processed_spectrum" in st.session_state:
        st.sidebar.info("πŸ“Š Spectrum processed")
    else:
        st.sidebar.info("πŸ“ Ready for upload")


if __name__ == "__main__":
    main()