Spaces:
Running
Running
File size: 11,599 Bytes
d29b763 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 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 | """Comprehensive production system audit and quality assessment.
This script validates:
1. Model quality metrics and severe class performance
2. Feature pipeline integrity (560-dim schema validation)
3. Backend API readiness
4. Frontend/Backend integration requirements
5. Healthcare safety layer
"""
from __future__ import annotations
import json
import logging
import sys
from pathlib import Path
from typing import Any, Dict
import numpy as np
import torch
# Setup logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s [%(levelname)s] %(name)s: %(message)s',
)
logger = logging.getLogger('medcare_ddi.audit')
# Add src to path
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT / 'src'))
from inference.predictor import (
BASE_DIR,
DATA_PATH,
MODEL_DIR,
FEATURE_PIPELINE_MULTISOURCE_PATH,
PRODUCTION_MODEL_PATH,
FeatureMLP,
HybridDDIPredictor,
LABEL_NAMES,
)
def audit_model_artifacts() -> Dict[str, Any]:
"""Check model and data artifacts."""
logger.info('='*60)
logger.info('PHASE 1: MODEL ARTIFACTS AUDIT')
logger.info('='*60)
artifacts = {
'feature_pipeline': FEATURE_PIPELINE_MULTISOURCE_PATH.exists(),
'model_checkpoint': MODEL_DIR / 'ddi_mlp_best.pt',
'data_file': DATA_PATH / 'ddinter_combined.parquet',
'metadata': MODEL_DIR / 'multisource_metadata.json',
}
results = {}
for name, path in artifacts.items():
if isinstance(path, bool):
exists = path
else:
exists = path.exists()
status = 'β' if exists else 'β'
results[name] = exists
if not isinstance(path, bool):
size = path.stat().st_size if exists else 0
size_mb = size / (1024 * 1024)
logger.info(f'{status} {name}: {path.name} ({size_mb:.1f}MB)')
else:
logger.info(f'{status} {name}')
return results
def audit_feature_pipeline() -> Dict[str, Any]:
"""Validate feature pipeline schema."""
logger.info('')
logger.info('='*60)
logger.info('PHASE 2: FEATURE PIPELINE AUDIT')
logger.info('='*60)
results = {}
# Check metadata
try:
metadata_path = MODEL_DIR / 'multisource_metadata.json'
with open(metadata_path) as f:
metadata = json.load(f)
total_dim = metadata.get('total_dim', 0)
results['total_dim'] = total_dim
logger.info(f'β Multisource metadata loaded')
logger.info(f' - Total dimension: {total_dim}')
# Check feature groups
feature_groups = metadata.get('feature_groups', {})
for group, info in feature_groups.items():
dim = info.get('dim', 0)
logger.info(f' - {group}: {dim}')
results[f'group_{group}'] = dim
# Validate 560-dim schema
if total_dim == 560:
logger.info(f'β Schema matches expected 560-dimensional feature space')
results['schema_valid'] = True
else:
logger.error(f'β MISMATCH: Expected 560 dims, got {total_dim}')
results['schema_valid'] = False
except Exception as e:
logger.error(f'β Failed to load metadata: {e}')
results['schema_valid'] = False
return results
def audit_predictor() -> Dict[str, Any]:
"""Test predictor initialization and basic functionality."""
logger.info('')
logger.info('='*60)
logger.info('PHASE 3: PREDICTOR FUNCTIONALITY AUDIT')
logger.info('='*60)
results = {}
try:
# Load predictor
logger.info('Loading predictor with production mode...')
predictor = HybridDDIPredictor.from_default_paths(use_production=False)
health = predictor.health()
logger.info(f'β Predictor initialized')
logger.info(f' - Model loaded: {health.get("model_loaded")}')
logger.info(f' - Pairs loaded: {health.get("pairs_loaded")}')
logger.info(f' - Records: {health.get("records_loaded")}')
results['model_loaded'] = health.get('model_loaded', False)
results['pairs_loaded'] = health.get('pairs_loaded', 0)
results['records_loaded'] = health.get('records_loaded', 0)
# Test known interactions
logger.info('')
logger.info('Testing known DDI pairs:')
test_pairs = [
('Aspirin', 'Warfarin'),
('Metformin', 'Insulin'),
('Lisinopril', 'Potassium'),
]
for drug_a, drug_b in test_pairs:
try:
result = predictor.predict(drug_a, drug_b)
severity = result.get('severity', 'unknown')
confidence = result.get('confidence', 0.0)
source = result.get('source', 'unknown')
logger.info(f' β {drug_a} + {drug_b}: {severity} (conf={confidence:.2f}, src={source})')
except Exception as e:
logger.error(f' β {drug_a} + {drug_b}: {e}')
# Test unseen pairs (ML fallback)
logger.info('')
logger.info('Testing unseen pairs (ML fallback):')
unseen_pairs = [
('DrugX', 'DrugY'),
('AcetaminophenX', 'IbuprofenY'),
]
for drug_a, drug_b in unseen_pairs:
try:
result = predictor.predict(drug_a, drug_b)
severity = result.get('severity', 'unknown')
confidence = result.get('confidence', 0.0)
source = result.get('source', 'unknown')
logger.info(f' β {drug_a} + {drug_b}: {severity} (conf={confidence:.2f}, src={source})')
except Exception as e:
logger.error(f' β {drug_a} + {drug_b}: {e}')
results['predictor_working'] = True
except Exception as e:
logger.error(f'β Predictor initialization failed: {e}', exc_info=True)
results['predictor_working'] = False
return results
def audit_backend_api() -> Dict[str, Any]:
"""Check FastAPI backend readiness."""
logger.info('')
logger.info('='*60)
logger.info('PHASE 4: BACKEND API AUDIT')
logger.info('='*60)
results = {}
try:
# Check app exists
from inference.app_production import app, predictor as api_predictor
logger.info('β FastAPI app imports successfully')
logger.info('β Predictor available in app context')
# Check routes
routes = [r.path for r in app.routes]
required_routes = ['/health', '/predict']
for route in required_routes:
if any(route in r for r in routes):
logger.info(f'β Route {route} exists')
results[f'route_{route}'] = True
else:
logger.error(f'β Route {route} NOT FOUND')
results[f'route_{route}'] = False
except Exception as e:
logger.error(f'β Failed to check backend API: {e}')
results['backend_ok'] = False
return results
def audit_frontend_integration() -> Dict[str, Any]:
"""Check frontend/backend integration points."""
logger.info('')
logger.info('='*60)
logger.info('PHASE 5: FRONTEND INTEGRATION AUDIT')
logger.info('='*60)
results = {}
frontend_path = ROOT.parent / 'Medcare-DDI' / 'src' / 'api'
try:
# Check appClient.js
client_file = frontend_path / 'appClient.js'
if client_file.exists():
logger.info(f'β Frontend appClient.js exists')
with open(client_file) as f:
client_code = f.read()
checks = {
'ddiPredictRequest': 'ddiPredictRequest' in client_code,
'predictInteraction': 'predictInteraction' in client_code,
'severity': 'severity' in client_code,
'confidence': 'confidence' in client_code,
}
for check_name, check_result in checks.items():
status = 'β' if check_result else 'β'
logger.info(f' {status} {check_name}')
results[f'frontend_{check_name}'] = check_result
else:
logger.error(f'β Frontend appClient.js NOT FOUND')
results['frontend_exists'] = False
except Exception as e:
logger.error(f'β Failed to check frontend integration: {e}')
return results
def audit_healthcare_safety() -> Dict[str, Any]:
"""Check healthcare safety features."""
logger.info('')
logger.info('='*60)
logger.info('PHASE 6: HEALTHCARE SAFETY AUDIT')
logger.info('='*60)
results = {}
try:
from inference.app_production import (
ConfidenceBand,
SeverityLevel,
PredictionResponse,
)
logger.info('β Safety enums imported')
# Check confidence bands
confidence_bands = [c.value for c in ConfidenceBand]
logger.info(f'β Confidence bands: {confidence_bands}')
results['confidence_bands'] = confidence_bands
# Check severity levels
severity_levels = [s.value for s in SeverityLevel]
logger.info(f'β Severity levels: {severity_levels}')
results['severity_levels'] = severity_levels
# Check response schema
logger.info('β PredictionResponse schema available')
logger.info(f' Fields: {list(PredictionResponse.model_fields.keys())}')
results['response_schema_ok'] = True
except Exception as e:
logger.error(f'β Healthcare safety check failed: {e}')
results['response_schema_ok'] = False
return results
def generate_audit_report(audit_results: Dict[str, Dict]) -> None:
"""Generate comprehensive audit report."""
logger.info('')
logger.info('='*60)
logger.info('AUDIT SUMMARY')
logger.info('='*60)
all_passed = True
for phase, results in audit_results.items():
passed = all(v for k, v in results.items() if isinstance(v, bool))
status = 'β PASS' if passed else 'β WARN'
logger.info(f'{status} - {phase}')
all_passed = all_passed and passed
logger.info('')
if all_passed:
logger.info('β ALL AUDITS PASSED - SYSTEM READY FOR OPTIMIZATION')
else:
logger.info('β SOME ISSUES FOUND - REVIEW ABOVE FOR DETAILS')
# Save detailed report
report = {
'timestamp': __import__('datetime').datetime.now().isoformat(),
'phases': audit_results,
'overall_status': 'READY' if all_passed else 'NEEDS_ATTENTION',
}
report_path = MODEL_DIR / 'reports' / 'comprehensive_audit.json'
report_path.parent.mkdir(parents=True, exist_ok=True)
with open(report_path, 'w') as f:
json.dump(report, f, indent=2)
logger.info(f'β Audit report saved to {report_path}')
def main() -> None:
"""Run comprehensive audit."""
logger.info('')
logger.info('β' + 'β'*58 + 'β')
logger.info('β MEDCARE-DDI COMPREHENSIVE PRODUCTION AUDIT' + ' '*15 + 'β')
logger.info('β' + 'β'*58 + 'β')
audit_results = {
'1_artifacts': audit_model_artifacts(),
'2_feature_pipeline': audit_feature_pipeline(),
'3_predictor': audit_predictor(),
'4_backend_api': audit_backend_api(),
'5_frontend_integration': audit_frontend_integration(),
'6_healthcare_safety': audit_healthcare_safety(),
}
generate_audit_report(audit_results)
logger.info('')
logger.info('Audit complete!')
if __name__ == '__main__':
main()
|