# ============================================================================== # FASTAPI APPLICATION WITH INTEGRATED UNCERTAINTY SYSTEM # ============================================================================== # This application replaces the simple prediction system with an advanced # uncertainty quantification framework, providing engineers not only predictions # but also calibrated confidence intervals for informed decision making from fastapi import FastAPI, HTTPException, Request, Depends, Query from fastapi.responses import JSONResponse, HTMLResponse from fastapi.middleware.cors import CORSMiddleware from fastapi.staticfiles import StaticFiles import pandas as pd import joblib import numpy as np import os import time import pickle from datetime import datetime from sklearn.ensemble import RandomForestRegressor # Required for deserialization from pydantic import BaseModel, ValidationError, Field, field_validator, model_validator from typing import Any, Dict, List, Optional, Union from scipy import stats import json # ============================================================================== # FASTAPI APPLICATION CONFIGURATION # ============================================================================== app = FastAPI( title="UCS Prediction API with Uncertainty Quantification", description=""" **Advanced API for predicting Unconfined Compressive Strength (UCS) of cement-stabilized soils** This application implements the uncertainty quantification system developed in the research "Prediction of Unconfined Compressive Strength in Cement-Treated Soil: A Machine Learning Approach". **Main features:** - Accurate UCS predictions using optimized Random Forest - Complete uncertainty quantification with calibrated confidence intervals - Sensitivity analysis for parameter optimization - Interpretability through feature importance analysis **Developed by:** Research Team - Technical University Gheorghe Asachi of IaΘi """, version="2.0.0", contact={ "name": "UCS Development Team", "email": "iancu-bogdan.teodoru@academic.tuiasi.ro", } ) # CORS configuration for web interface app.add_middleware( CORSMiddleware, allow_origins=[ "http://www.bi4e-at.tuiasi.ro", "https://www.bi4e-at.tuiasi.ro" # "http://localhost:3000", # For local development # "http://localhost:8000" # For local testing ], allow_credentials=True, allow_methods=["GET", "POST", "OPTIONS"], allow_headers=["*"], ) # ============================================================================== # MODEL CONFIGURATION AND SYSTEM LOADING # ============================================================================== # Paths to serialized models MODELS_DIR = "./models_for_deployment" PRIMARY_MODEL_PATH = os.path.join(MODELS_DIR, "rf_primary_model.joblib") UNCERTAINTY_MODEL_PATH = os.path.join(MODELS_DIR, "rf_uncertainty_model.joblib") METADATA_PATH = os.path.join(MODELS_DIR, "system_metadata.pkl") # Feature order (critical for compatibility) DEFAULT_FEATURE_ORDER = ['cement_percent', 'curing_period', 'compaction_rate'] # Global variables for system primary_model = None uncertainty_model = None system_metadata = None FEATURE_ORDER = None def load_uncertainty_system(): """ Loads and validates the entire uncertainty system. This function orchestrates the loading of all system components and performs basic validations to ensure proper operation. The process is designed to be robust and provide detailed information about any issues encountered during loading. """ global primary_model, uncertainty_model, system_metadata, FEATURE_ORDER print("π Loading uncertainty system...") start_time = time.time() try: # Load primary model if os.path.exists(PRIMARY_MODEL_PATH): primary_model = joblib.load(PRIMARY_MODEL_PATH) print(f"β Primary model loaded: {type(primary_model).__name__}") else: raise FileNotFoundError(f"Primary model not found at: {PRIMARY_MODEL_PATH}") # Load uncertainty model if os.path.exists(UNCERTAINTY_MODEL_PATH): uncertainty_model = joblib.load(UNCERTAINTY_MODEL_PATH) print(f"β Uncertainty model loaded: {type(uncertainty_model).__name__}") else: raise FileNotFoundError(f"Uncertainty model not found at: {UNCERTAINTY_MODEL_PATH}") # Load system metadata if os.path.exists(METADATA_PATH): with open(METADATA_PATH, 'rb') as f: system_metadata = pickle.load(f) print(f"β System metadata loaded: {len(system_metadata)} keys") else: print("β οΈ System metadata not found, using default values") system_metadata = {"feature_names": DEFAULT_FEATURE_ORDER} # Determine feature order if hasattr(primary_model, 'feature_names_in_'): FEATURE_ORDER = primary_model.feature_names_in_ elif system_metadata and 'feature_names' in system_metadata: FEATURE_ORDER = np.array(system_metadata['feature_names']) else: FEATURE_ORDER = np.array(DEFAULT_FEATURE_ORDER) # Validate model compatibility validation_result = validate_models_compatibility() if not validation_result: raise ValueError("Models are not compatible with each other") load_time = time.time() - start_time print(f"π Uncertainty system loaded successfully in {load_time:.2f} seconds!") print(f"π Features: {FEATURE_ORDER.tolist()}") return True except Exception as e: print(f"β Error loading system: {str(e)}") import traceback print(traceback.format_exc()) return False def validate_models_compatibility(): """ Validates that models are compatible and work together. This validation includes dimensional compatibility tests, data type checks and a complete functional test. """ try: # Test with synthetic data test_input = np.array([[5.0, 14.0, 1.0]]) # cement, curing, compaction # Test primary model primary_pred = primary_model.predict(test_input)[0] # Test uncertainty model with feature augmentation uncertainty_input = np.column_stack([test_input, [[primary_pred]]]) uncertainty_pred = uncertainty_model.predict(uncertainty_input)[0] # Check that results are numeric and reasonable assert isinstance(primary_pred, (int, float, np.number)) assert isinstance(uncertainty_pred, (int, float, np.number)) assert primary_pred > 0 assert uncertainty_pred > 0 print(f"β Compatibility test: UCS={primary_pred:.1f} kPa, Ο={uncertainty_pred:.1f} kPa") return True except Exception as e: print(f"β Compatibility test failed: {str(e)}") return False # Load system at application startup system_loaded = load_uncertainty_system() # ============================================================================== # PYDANTIC MODELS FOR INPUT AND OUTPUT # ============================================================================== class SoilInput(BaseModel): """ Model for soil input data. This class defines and validates input parameters, ensuring values are within validated experimental ranges. """ cement_perecent: float = Field( ..., description="Cement percentage in mixture", ge=0, le=15, example=5.0 ) curing_period: float = Field( ..., description="Curing period in days", ge=0, le=90, example=28.0 ) compaction_rate: float = Field( ..., description="Compaction rate in mm/min", ge=0.5, le=2.0, example=1.0 ) @model_validator(mode="after") def validate_cement_curing_relationship(self): """ Validates the relationship between cement content and curing period. For untreated soil (0% cement), curing period is forced to 0 because there is no cement hydration process. """ if self.cement_perecent == 0: self.curing_period = 0 elif self.cement_perecent > 0 and self.curing_period < 1: raise ValueError("For cement-treated soil, curing period must be β₯ 1 day") return self class Config: json_schema_extra = { "example": { "cement_perecent": 5.0, "curing_period": 28.0, "compaction_rate": 1.0 } } class ConfidenceInterval(BaseModel): """Model for a confidence interval.""" lower: float = Field(..., description="Lower bound of the interval") upper: float = Field(..., description="Upper bound of the interval") width: float = Field(..., description="Width of the interval") class UncertaintyPredictionResponse(BaseModel): """ Complete response with uncertainty quantification. This extended structure provides the engineer with a complete picture of the prediction, including not only the estimated value but also confidence in that estimate through calibrated intervals. """ success: bool = Field(..., description="Request processing status") # Central prediction central_prediction: float = Field(..., description="Most probable UCS prediction") units: str = Field(default="kPa", description="Units of measurement") # Uncertainty information uncertainty_estimate: float = Field(..., description="Absolute uncertainty estimate (1-sigma)") relative_uncertainty: float = Field(..., description="Relative uncertainty as percentage") # Confidence intervals confidence_intervals: Dict[str, ConfidenceInterval] = Field( ..., description="Confidence intervals for multiple probability levels" ) # User interpretation interpretation: Dict[str, str] = Field(..., description="Interpretation guide for results") # Metadata input_parameters: Dict[str, float] = Field(..., description="Input parameters used") prediction_time_ms: Optional[float] = Field(None, description="Processing time in milliseconds") model_info: Optional[Dict[str, Any]] = Field(None, description="Information about models used") class SensitivityAnalysisRequest(BaseModel): """Request for sensitivity analysis.""" base_parameters: SoilInput parameter_to_vary: str = Field(..., pattern="^(cement_perecent|curing_period|compaction_rate)$") variation_range: float = Field(default=10.0, ge=1.0, le=50.0, description="Variation range in percentage") num_points: int = Field(default=11, ge=5, le=21, description="Number of points for analysis") # ============================================================================== # CORE FUNCTIONS FOR UNCERTAINTY PREDICTION # ============================================================================== def predict_with_uncertainty(input_data: np.ndarray, confidence_levels: List[float] = [0.68, 0.80, 0.90, 0.95]) -> Dict[str, Any]: """ Performs complete prediction with uncertainty quantification. This function implements the two-stage algorithm developed in research: 1. Primary model generates central UCS prediction 2. Uncertainty model estimates magnitude of probable error 3. Confidence intervals are constructed assuming normal distribution Args: input_data: Numpy array with features [cement%, curing_days, compaction_rate] confidence_levels: List of confidence levels for which to calculate intervals Returns: Dictionary with central prediction, uncertainty estimation and confidence intervals """ # Stage 1: Central prediction with primary model central_prediction = primary_model.predict(input_data)[0] # Stage 2: Preparing input for uncertainty model # Uncertainty model uses feature augmentation: # original features + central prediction uncertainty_input = np.column_stack([input_data, [[central_prediction]]]) # Stage 3: Uncertainty prediction (magnitude of expected error) uncertainty_estimate = uncertainty_model.predict(uncertainty_input)[0] # Stage 4: Calculating confidence intervals confidence_intervals = {} for conf_level in confidence_levels: # Z-score corresponding to confidence level # For normal distribution: 68% β zβ1.0, 90% β zβ1.645, 95% β zβ1.96 z_score = stats.norm.ppf((1 + conf_level) / 2) # Margin of error = z-score Γ uncertainty estimate margin = z_score * uncertainty_estimate confidence_intervals[f'{conf_level:.0%}'] = ConfidenceInterval( lower=float(central_prediction - margin), upper=float(central_prediction + margin), width=float(2 * margin) ) # Calculating relative uncertainty relative_uncertainty = (uncertainty_estimate / central_prediction) * 100 if central_prediction != 0 else 0 return { 'central_prediction': float(central_prediction), 'uncertainty_estimate': float(uncertainty_estimate), 'relative_uncertainty': float(relative_uncertainty), 'confidence_intervals': confidence_intervals } def generate_interpretation_guide(central_prediction: float, uncertainty_estimate: float, confidence_intervals: Dict[str, ConfidenceInterval]) -> Dict[str, str]: """ Generates a personalized interpretation guide for prediction results. This function translates statistical results into practical language for engineers, providing the necessary context for informed decision making in projects. """ # Calculate 95% interval for interpretation interval_95 = confidence_intervals.get('95%') # Confidence classification based on relative uncertainty relative_unc = (uncertainty_estimate / central_prediction) * 100 if relative_unc <= 10: confidence_level = "very high" reliability_desc = "The prediction is very reliable for design decision making." elif relative_unc <= 20: confidence_level = "high" reliability_desc = "The prediction is reliable, we recommend validation through limited testing." elif relative_unc <= 30: confidence_level = "moderate" reliability_desc = "The prediction provides a useful estimate, but additional testing is recommended." else: confidence_level = "limited" reliability_desc = "The prediction is indicative, extensive testing is recommended for validation." interpretation = { "central_prediction": f"The most probable UCS value is {central_prediction:.0f} kPa, based on the input parameters.", "uncertainty": f"The estimated uncertainty is Β±{uncertainty_estimate:.0f} kPa ({relative_unc:.1f}%), " f"indicating {confidence_level} confidence in the prediction.", "confidence_95": f"We have 95% confidence that the actual UCS value is between " f"{interval_95.lower:.0f} and {interval_95.upper:.0f} kPa." if interval_95 else "", "reliability": reliability_desc, "practical_guidance": f"For applications with UCS requirements > {central_prediction + uncertainty_estimate:.0f} kPa, " f"consider increasing cement content or extending the curing period." } return interpretation async def validate_models_loaded(): """Dependency function for validating model loading.""" if not system_loaded or primary_model is None or uncertainty_model is None: raise HTTPException( status_code=503, detail="Model system is not loaded correctly. Contact administrator." ) return True # ============================================================================== # API ENDPOINTS # ============================================================================== @app.get("/", response_class=HTMLResponse, summary="Main page") async def root(): """ Returns the main page with API information. """ return """
Advanced API for predicting unconfined compressive strength of cement-stabilized soils.