File size: 10,863 Bytes
edb09e8 |
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 |
"""
Quick Start Test Script
Run this script to verify everything is set up correctly before deployment.
This performs basic smoke tests on the model and inference API.
"""
import sys
from pathlib import Path
from typing import Any
def test_imports():
"""Test that all required packages are installed."""
print("=" * 70)
print("TEST 1: Checking Required Packages")
print("=" * 70)
required_packages = {
'sklearn': 'scikit-learn',
'pandas': 'pandas',
'numpy': 'numpy',
'joblib': 'joblib'
}
missing: list[str] = []
for module_name, package_name in required_packages.items():
try:
__import__(module_name)
print(f"β
{package_name} is installed")
except ImportError:
print(f"β {package_name} is NOT installed")
missing.append(package_name)
if missing:
print(f"\nβ οΈ Missing packages: {', '.join(missing)}")
print("Install them with: pip install -r requirements.txt")
return False
print("\nβ
All required packages are installed!\n")
return True
def test_files():
"""Test that all required files exist."""
print("=" * 70)
print("TEST 2: Checking Required Files")
print("=" * 70)
required_files = [
'house_price_model.joblib',
'preprocessing_pipeline.joblib',
'inference.py',
'README.md',
'requirements.txt',
'LICENSE',
'.gitattributes',
'example_usage.py'
]
missing: list[str] = []
for filename in required_files:
filepath = Path(filename)
if filepath.exists():
size = filepath.stat().st_size
size_str = f"{size:,} bytes" if size < 1024 else f"{size/1024:.1f} KB" if size < 1024*1024 else f"{size/(1024*1024):.1f} MB"
print(f"β
{filename:35s} ({size_str})")
else:
print(f"β {filename:35s} (MISSING)")
missing.append(filename)
if missing:
print(f"\nβ οΈ Missing files: {', '.join(missing)}")
return False
print("\nβ
All required files exist!\n")
return True
def test_model_loading() -> tuple[bool, Any]:
"""Test that the model can be loaded."""
print("=" * 70)
print("TEST 3: Loading Model and Pipeline")
print("=" * 70)
try:
from inference import HousePricePredictor
predictor = HousePricePredictor()
predictor.load()
print("β
Model and pipeline loaded successfully!\n")
return True, predictor
except Exception as e:
print(f"β Failed to load model: {e}\n")
return False, None
def test_prediction(predictor: Any) -> bool:
"""Test that predictions work correctly."""
print("=" * 70)
print("TEST 4: Making Test Predictions")
print("=" * 70)
test_cases: list[dict[str, Any]] = [
{
'name': 'Expensive Bay Area house',
'data': {
'longitude': -122.23, 'latitude': 37.88,
'housing_median_age': 41.0, 'total_rooms': 880.0,
'total_bedrooms': 129.0, 'population': 322.0,
'households': 126.0, 'median_income': 8.3252,
'ocean_proximity': 'NEAR BAY'
},
'expected_range': (300000, 600000)
},
{
'name': 'Inland moderate house',
'data': {
'longitude': -119.56, 'latitude': 36.78,
'housing_median_age': 15.0, 'total_rooms': 4500.0,
'total_bedrooms': 800.0, 'population': 1800.0,
'households': 750.0, 'median_income': 3.2,
'ocean_proximity': 'INLAND'
},
'expected_range': (100000, 300000)
},
{
'name': 'Coastal high-income house',
'data': {
'longitude': -118.40, 'latitude': 34.07,
'housing_median_age': 35.0, 'total_rooms': 2500.0,
'total_bedrooms': 500.0, 'population': 1200.0,
'households': 450.0, 'median_income': 7.5,
'ocean_proximity': '<1H OCEAN'
},
'expected_range': (250000, 550000)
}
]
all_passed = True
for i, test in enumerate(test_cases, 1):
print(f"\nTest case {i}: {test['name']}")
print("-" * 70)
try:
prediction = predictor.predict(test['data'])
price = prediction[0]
min_price, max_price = test['expected_range']
print(f"Input: Income=${test['data']['median_income']*10000:,.0f}, "
f"Location=({test['data']['longitude']}, {test['data']['latitude']}), "
f"Proximity={test['data']['ocean_proximity']}")
print(f"Predicted price: ${price:,.2f}")
if min_price <= price <= max_price:
print(f"β
Prediction is within expected range (${min_price:,} - ${max_price:,})")
else:
print(f"β οΈ Prediction outside expected range (${min_price:,} - ${max_price:,})")
print(" (This might be okay, just flagging for review)")
except Exception as e:
print(f"β Prediction failed: {e}")
all_passed = False
if all_passed:
print("\nβ
All predictions completed successfully!\n")
else:
print("\nβ οΈ Some predictions had issues\n")
return all_passed
def test_batch_prediction(predictor: Any) -> bool:
"""Test batch predictions."""
print("=" * 70)
print("TEST 5: Batch Prediction")
print("=" * 70)
try:
import pandas as pd
# Create batch data
batch_data = pd.DataFrame([
{
'longitude': -122.23, 'latitude': 37.88,
'housing_median_age': 41.0, 'total_rooms': 880.0,
'total_bedrooms': 129.0, 'population': 322.0,
'households': 126.0, 'median_income': 8.3252,
'ocean_proximity': 'NEAR BAY'
},
{
'longitude': -119.56, 'latitude': 36.78,
'housing_median_age': 15.0, 'total_rooms': 4500.0,
'total_bedrooms': 800.0, 'population': 1800.0,
'households': 750.0, 'median_income': 3.2,
'ocean_proximity': 'INLAND'
}
])
predictions = predictor.predict(batch_data)
print(f"β
Successfully predicted {len(predictions)} houses in batch:")
for i, price in enumerate(predictions, 1):
print(f" House {i}: ${price:,.2f}")
print("\nβ
Batch prediction works!\n")
return True
except Exception as e:
print(f"β Batch prediction failed: {e}\n")
return False
def test_validation(predictor: Any) -> bool:
"""Test input validation."""
print("=" * 70)
print("TEST 6: Input Validation")
print("=" * 70)
# Test with missing feature
print("\nTest: Missing required feature...")
try:
import pandas as pd
invalid_data = pd.DataFrame([{
'longitude': -122.23,
'latitude': 37.88,
# Missing other required features
}])
predictor.predict(invalid_data)
print("β Should have raised an error for missing features")
return False
except ValueError as e:
print(f"β
Correctly caught missing features: {e}")
# Test with invalid ocean_proximity
print("\nTest: Invalid ocean_proximity value...")
try:
import pandas as pd
invalid_data = pd.DataFrame([{
'longitude': -122.23, 'latitude': 37.88,
'housing_median_age': 41.0, 'total_rooms': 880.0,
'total_bedrooms': 129.0, 'population': 322.0,
'households': 126.0, 'median_income': 8.3252,
'ocean_proximity': 'INVALID_VALUE'
}])
predictor.predict(invalid_data)
print("β Should have raised an error for invalid ocean_proximity")
return False
except ValueError as e:
print(f"β
Correctly caught invalid value: {e}")
print("\nβ
Input validation works correctly!\n")
return True
def main() -> None:
"""Run all tests."""
print("\n" + "=" * 70)
print("π CALIFORNIA HOUSE PRICE PREDICTION - DEPLOYMENT READINESS CHECK")
print("=" * 70 + "\n")
results: list[tuple[str, bool]] = []
# Test 1: Imports
results.append(("Required packages", test_imports()))
if not results[-1][1]:
print("\nβ Cannot continue without required packages. Install them first.")
sys.exit(1)
# Test 2: Files
results.append(("Required files", test_files()))
if not results[-1][1]:
print("\nβ Cannot continue without required files.")
sys.exit(1)
# Test 3: Model loading
success, predictor = test_model_loading()
results.append(("Model loading", success))
if not success:
print("\nβ Cannot continue without loading the model.")
sys.exit(1)
# Test 4: Predictions
results.append(("Predictions", test_prediction(predictor)))
# Test 5: Batch prediction
results.append(("Batch prediction", test_batch_prediction(predictor)))
# Test 6: Validation
results.append(("Input validation", test_validation(predictor)))
# Summary
print("=" * 70)
print("SUMMARY")
print("=" * 70)
for test_name, passed in results:
status = "β
PASS" if passed else "β FAIL"
print(f"{test_name:25s} {status}")
all_passed = all(result[1] for result in results)
print("\n" + "=" * 70)
if all_passed:
print("π ALL TESTS PASSED! Your model is ready for deployment!")
print("=" * 70)
print("\nNext steps:")
print("1. Review the DEPLOYMENT_GUIDE.md file")
print("2. Set up Git LFS: git lfs install")
print("3. Create a Hugging Face account if you don't have one")
print("4. Follow the deployment steps in DEPLOYMENT_GUIDE.md")
print("\n⨠Your model will be live on Hugging Face Model Hub soon!")
else:
print("β οΈ SOME TESTS FAILED - Please fix the issues above")
print("=" * 70)
sys.exit(1)
print()
if __name__ == "__main__":
main()
|