""" Interactive Crystallization Component Predictor =============================================== Streamlit app for Hugging Face Hub deployment Predicts crystallization components using Simple Baseline and Advanced Baseline models """ import streamlit as st import pandas as pd import numpy as np import joblib import json import os import warnings warnings.filterwarnings('ignore') # Page config st.set_page_config( page_title="Crystallization Predictor", page_icon="🔬", layout="wide", initial_sidebar_state="expanded" ) # Get the directory of this script BASE_DIR = os.path.dirname(os.path.abspath(__file__)) # Title and Introduction st.title("🔬 Crystallization Component Predictor") st.markdown(""" ### Predict crystallization components using Machine Learning This app uses trained machine learning models to predict the optimal components for protein crystallization based on your experimental parameters. """) st.markdown("---") # Sidebar st.sidebar.header("⚙️ Model Selection") approach = st.sidebar.radio( "Choose Approach:", ["Advanced Baseline (Recommended)", "Simple Baseline"], help="Advanced has concentration parsing and better accuracy" ) st.sidebar.markdown("---") st.sidebar.markdown("### 📊 Model Performance") # Display performance metrics try: simple_results_path = os.path.join(BASE_DIR, 'models', 'simple_baseline', 'training_results.json') advanced_results_path = os.path.join(BASE_DIR, 'models', 'advanced_baseline', 'training_results.json') if os.path.exists(simple_results_path): with open(simple_results_path, 'r') as f: simple_results = json.load(f) if os.path.exists(advanced_results_path): with open(advanced_results_path, 'r') as f: advanced_results = json.load(f) if "Simple" in approach: st.sidebar.metric("Name Accuracy", "61.12%") st.sidebar.metric("pH R²", "95.58%") st.sidebar.warning("⚠️ Conc: N/A") else: st.sidebar.metric("Name Accuracy", "64.18%") st.sidebar.metric("Conc R²", "47.33%") st.sidebar.metric("pH R²", "99.34%") st.sidebar.success("✅ All metrics working!") except Exception as e: st.sidebar.info(f"Using default metrics") st.sidebar.markdown("---") st.sidebar.markdown(""" ### ℹ️ About This tool predicts three key crystallization parameters: - **Component Name**: The chemical compound - **Concentration**: Amount in solution (M) - **pH**: Acidity/basicity level **Recommended:** Advanced Baseline for complete predictions """) # Input Form st.header("🎯 Input Crystallization Parameters") col1, col2 = st.columns(2) with col1: st.markdown("#### Crystallization Setup") cryst_method = st.selectbox( "Crystallization Method", [ "VAPOR DIFFUSION, SITTING DROP", "VAPOR DIFFUSION, HANGING DROP", "VAPOR DIFFUSION", "BATCH MODE", "MICROBATCH" ], help="Select the crystallization technique you're using" ) temp = st.slider( "Temperature (K)", 250.0, 320.0, 293.0, 1.0, help="Typical room temperature is ~293K (20°C)" ) ph = st.slider( "pH", 0.0, 14.0, 7.0, 0.1, help="Initial pH of your crystallization solution" ) with col2: st.markdown("#### Crystal Properties") matthews = st.slider( "Matthews Coefficient", 1.0, 4.5, 2.2, 0.1, help="Ratio of unit cell volume to protein molecular weight (ų/Da)" ) solvent = st.slider( "Percent Solvent Content (%)", 0.0, 100.0, 45.0, 1.0, help="Percentage of solvent in the crystal" ) st.markdown("---") # Predict button if st.button("🚀 Predict Components", type="primary", use_container_width=True): try: with st.spinner("🔄 Loading models and making predictions..."): if "Advanced" in approach: # Load advanced models model_name = joblib.load(os.path.join(BASE_DIR, 'models', 'advanced_baseline', 'model_component_name.pkl')) model_conc = joblib.load(os.path.join(BASE_DIR, 'models', 'advanced_baseline', 'model_component_conc.pkl')) model_ph = joblib.load(os.path.join(BASE_DIR, 'models', 'advanced_baseline', 'model_component_ph.pkl')) le = joblib.load(os.path.join(BASE_DIR, 'models', 'advanced_baseline', 'label_encoder_name.pkl')) scaler = joblib.load(os.path.join(BASE_DIR, 'models', 'advanced_baseline', 'scaler.pkl')) tfidf = joblib.load(os.path.join(BASE_DIR, 'models', 'advanced_baseline', 'tfidf.pkl')) # Feature engineering (Advanced Baseline needs 8 features) temp_ph_int = temp * ph matthews_solvent_int = matthews * solvent ph_diff = 0 # Unknown for new prediction solvent_ratio = solvent / (matthews + 1e-6) numerical = np.array([[temp, ph, matthews, solvent, temp_ph_int, matthews_solvent_int, ph_diff, solvent_ratio]]) else: # Load simple models model_name = joblib.load(os.path.join(BASE_DIR, 'models', 'simple_baseline', 'model_component_name.pkl')) model_ph = joblib.load(os.path.join(BASE_DIR, 'models', 'simple_baseline', 'model_component_ph.pkl')) le = joblib.load(os.path.join(BASE_DIR, 'models', 'simple_baseline', 'label_encoder_name.pkl')) scaler = joblib.load(os.path.join(BASE_DIR, 'models', 'simple_baseline', 'scaler.pkl')) tfidf = joblib.load(os.path.join(BASE_DIR, 'models', 'simple_baseline', 'tfidf.pkl')) # Simple baseline: only 4 features numerical = np.array([[temp, ph, matthews, solvent]]) # Scale numerical features numerical_scaled = scaler.transform(numerical) # TF-IDF for crystallization method method_tfidf = tfidf.transform([cryst_method.upper()]).toarray() # Combine features X_pred = np.concatenate([numerical_scaled, method_tfidf], axis=1) # Make predictions pred_name_idx = model_name.predict(X_pred)[0] pred_name = le.inverse_transform([pred_name_idx])[0] pred_name_proba = model_name.predict_proba(X_pred)[0] top_5_idx = np.argsort(pred_name_proba)[-5:][::-1] top_5_names = le.inverse_transform(top_5_idx) top_5_proba = pred_name_proba[top_5_idx] pred_ph = model_ph.predict(X_pred)[0] if "Advanced" in approach: pred_conc = model_conc.predict(X_pred)[0] # Display Results st.success("✅ Predictions Complete!") st.markdown("---") st.header("📊 Prediction Results") # Component Name st.subheader("1️⃣ Component_1_Name") st.markdown("**Most likely chemical component for crystallization:**") col1, col2 = st.columns([1, 2]) with col1: st.metric("Predicted Component", pred_name) st.caption("Top prediction from the model") with col2: st.markdown("**Top 5 Predictions (with confidence):**") top5_df = pd.DataFrame({ 'Rank': range(1, 6), 'Component': top_5_names, 'Probability': [f"{p:.2%}" for p in top_5_proba] }) st.dataframe(top5_df, hide_index=True, use_container_width=True) st.markdown("---") # Concentration st.subheader("2️⃣ Component_1_Conc") if "Advanced" in approach: col1, col2 = st.columns(2) with col1: st.metric("Predicted Concentration (log-scale)", f"{pred_conc:.4f}") with col2: actual_molarity = 10**pred_conc st.metric("Actual Molarity", f"{actual_molarity:.6f} M") st.info(f"💡 Use approximately **{actual_molarity:.6f} M** of {pred_name} in your crystallization trials") else: st.warning("⚠️ Not available in Simple Baseline - use Advanced Baseline for concentration predictions") st.markdown("---") # pH st.subheader("3️⃣ Component_1_pH") col1, col2 = st.columns([1, 2]) with col1: st.metric("Predicted pH", f"{pred_ph:.2f}") # pH classification if pred_ph < 6: ph_class = "Acidic" ph_emoji = "🔴" elif pred_ph < 8: ph_class = "Neutral" ph_emoji = "🟢" else: ph_class = "Basic" ph_emoji = "🔵" st.caption(f"{ph_emoji} {ph_class} solution") with col2: # pH visualization ph_percent = (pred_ph / 14) * 100 ph_color = "red" if pred_ph < 6 else ("green" if pred_ph < 8 else "blue") st.markdown(f"""
🔬 Crystallization Component Prediction System
Advanced Baseline achieves: 64% Name Accuracy | 47% Conc R² | 99% pH R²
Built with Scikit-learn, XGBoost, LightGBM, CatBoost & Streamlit
For research and educational purposes. Validate predictions experimentally.