File size: 6,574 Bytes
0162f5e |
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 |
"""
Test ensemble model training and prediction
"""
import sys
from pathlib import Path
# Add parent directory to path
parent_dir = str(Path(__file__).parent.parent)
if parent_dir not in sys.path:
sys.path.insert(0, parent_dir)
from SelfTrainService.config import CONFIG
from SelfTrainService.trainer import ModelTrainer
from SelfTrainService.data_store import ScheduleDataStore
from SelfTrainService.feature_extractor import FeatureExtractor
from DataService.metro_data_generator import MetroDataGenerator
from DataService.schedule_optimizer import MetroScheduleOptimizer
def test_config():
"""Test configuration"""
print("Testing Configuration...")
print(f" Model Types: {CONFIG.MODEL_TYPES}")
print(f" Use Ensemble: {CONFIG.USE_ENSEMBLE}")
print(f" Retrain Interval: {CONFIG.RETRAIN_INTERVAL_HOURS} hours")
print(f" Features: {len(CONFIG.FEATURES)} features")
print(" β Config OK")
def test_model_initialization():
"""Test model initialization"""
print("\nTesting Model Initialization...")
trainer = ModelTrainer()
for model_name in CONFIG.MODEL_TYPES:
model = trainer._get_model(model_name)
if model is not None:
print(f" β {model_name}: {type(model).__name__}")
else:
print(f" β {model_name}: Failed to initialize")
print(" β Model initialization OK")
def test_data_generation():
"""Test data generation"""
print("\nTesting Data Generation...")
from datetime import datetime
num_trains = 30
generator = MetroDataGenerator(num_trains=num_trains)
route = generator.generate_route()
train_health = generator.generate_train_health_statuses()
optimizer = MetroScheduleOptimizer(
date=datetime.now().strftime("%Y-%m-%d"),
num_trains=num_trains,
route=route,
train_health=train_health
)
schedule = optimizer.optimize_schedule()
print(f" Generated schedule with {len(schedule.trainsets)} trains")
print(f" Total service blocks: {sum(len(t.service_blocks) for t in schedule.trainsets)}")
print(" β Data generation OK")
def test_feature_extraction():
"""Test feature extraction"""
print("\nTesting Feature Extraction...")
from datetime import datetime
num_trains = 30
generator = MetroDataGenerator(num_trains=num_trains)
route = generator.generate_route()
train_health = generator.generate_train_health_statuses()
optimizer = MetroScheduleOptimizer(
date=datetime.now().strftime("%Y-%m-%d"),
num_trains=num_trains,
route=route,
train_health=train_health
)
feature_extractor = FeatureExtractor()
schedule = optimizer.optimize_schedule()
schedule_dict = schedule.model_dump()
features = feature_extractor.extract_from_schedule(schedule_dict)
print(f" Extracted {len(features)} features")
print(f" Feature names: {list(features.keys())[:5]}...")
quality = feature_extractor.calculate_target(schedule_dict)
print(f" Quality score: {quality:.2f}")
print(" β Feature extraction OK")
def test_training():
"""Test model training"""
print("\nTesting Model Training...")
from datetime import datetime
# Generate small dataset
data_store = ScheduleDataStore()
print(" Generating 20 sample schedules...")
for i in range(20):
num_trains = 25 + i
generator = MetroDataGenerator(num_trains=num_trains)
route = generator.generate_route()
train_health = generator.generate_train_health_statuses()
optimizer = MetroScheduleOptimizer(
date=datetime.now().strftime("%Y-%m-%d"),
num_trains=num_trains,
route=route,
train_health=train_health
)
schedule = optimizer.optimize_schedule()
data_store.save_schedule(schedule.model_dump())
# Try training (will fail due to insufficient data, but tests the pipeline)
trainer = ModelTrainer()
result = trainer.train(force=True)
if result["success"]:
print(f" β Training successful")
print(f" Models: {result['models_trained']}")
print(f" Best: {result['best_model']}")
else:
print(f" β Training skipped: {result['reason']}")
print(" (This is expected with small dataset)")
print(" β Training pipeline OK")
def test_prediction():
"""Test model prediction"""
print("\nTesting Model Prediction...")
trainer = ModelTrainer()
# Try to load existing model
if trainer.load_model():
print(" β Loaded existing model")
# Test prediction
test_features = {
"num_trains": 30,
"num_available": 28,
"avg_readiness_score": 85.0,
"total_mileage": 150000,
"mileage_variance": 5000,
"maintenance_count": 3,
"certificate_expiry_count": 1,
"branding_priority_sum": 15,
"time_of_day": 12,
"day_of_week": 3
}
prediction, confidence = trainer.predict(test_features, use_ensemble=True)
print(f" Ensemble Prediction: {prediction:.2f}")
print(f" Confidence: {confidence:.2f}")
prediction_single, confidence_single = trainer.predict(test_features, use_ensemble=False)
print(f" Single Model Prediction: {prediction_single:.2f}")
print(f" Confidence: {confidence_single:.2f}")
print(" β Prediction OK")
else:
print(" β No trained model available (run train_model.py first)")
def main():
"""Run all tests"""
print("=" * 60)
print("Ensemble Model System Tests")
print("=" * 60)
try:
test_config()
test_model_initialization()
test_data_generation()
test_feature_extraction()
test_training()
test_prediction()
print("\n" + "=" * 60)
print("All Tests Completed!")
print("=" * 60)
print("\nNext Steps:")
print("1. Install remaining dependencies: pip install -r requirements.txt")
print("2. Generate training data: python SelfTrainService/train_model.py")
print("3. Start retraining service: python SelfTrainService/start_retraining.py")
except Exception as e:
print(f"\nβ Test failed with error: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
main()
|