-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
273 lines (215 loc) · 9.21 KB
/
main.py
File metadata and controls
273 lines (215 loc) · 9.21 KB
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
"""
Customer Shopping Behavior Simulation - Main Application
======================================================
Main entry point for running the customer shopping behavior simulation.
Author: dshail
Date: 2025
Usage:
python main.py --days 30 --customers 1000
python main.py --config config/custom_personas.yaml
python main.py --help
"""
import argparse
import sys
import os
import json
from pathlib import Path
from datetime import datetime
# Add src to path
sys.path.append('src')
from simulator import CustomerBehaviorSimulator
import logging
def setup_logging(log_level: str = 'INFO') -> None:
"""Setup application logging"""
# Create logs directory if it doesn't exist
Path('logs').mkdir(exist_ok=True)
# Configure logging
log_format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
logging.basicConfig(
level=getattr(logging, log_level.upper()),
format=log_format,
handlers=[
logging.FileHandler(f'logs/simulation_{datetime.now().strftime("%Y%m%d_%H%M%S")}.log'),
logging.StreamHandler(sys.stdout)
]
)
def print_banner() -> None:
"""Print application banner"""
banner = """
╔══════════════════════════════════════════════════════════════════╗
║ ║
║ 🛒 CUSTOMER SHOPPING BEHAVIOR SIMULATION SYSTEM ║
║ ║
║ Built for: Stimulation ║
║ Author: dshail ║
║ Date: August 2025 ║
║ ║
╚══════════════════════════════════════════════════════════════════╝
"""
print(banner)
def print_simulation_summary(summary: dict) -> None:
"""Print formatted simulation summary"""
print("\n" + "="*70)
print("📊 SIMULATION RESULTS SUMMARY")
print("="*70)
# Overview
overview = summary['overview']
print(f"\n🔍 OVERVIEW:")
print(f" Total Customers: {overview['total_customers']:,}")
print(f" Total Transactions: {overview['total_transactions']:,}")
print(f" Simulation Period: {overview['simulation_period']}")
print(f" Total Revenue: ₹{overview['total_revenue']:,.2f}")
print(f" Average Transaction Value: ₹{overview['avg_transaction_value']:,.2f}")
# Persona breakdown
print(f"\n👥 PERSONA PERFORMANCE:")
for persona_name, stats in summary['persona_breakdown'].items():
print(f" {persona_name}:")
print(f" Revenue: ₹{stats['total_revenue']:,.2f}")
print(f" Transactions: {stats['transaction_count']:,}")
print(f" Avg Transaction: ₹{stats['avg_transaction']:,.2f}")
print(f" Avg Items/Basket: {stats['avg_items_per_basket']:.1f}")
# Temporal analysis
temporal = summary['temporal_analysis']
print(f"\n📅 TEMPORAL ANALYSIS:")
festival_data = temporal['festival_vs_regular']
print(f" Festival vs Regular Days:")
print(f" Festival Revenue: ₹{festival_data['festival_revenue']:,.2f}")
print(f" Regular Revenue: ₹{festival_data['regular_revenue']:,.2f}")
print(f" Festival Avg: ₹{festival_data['festival_avg_transaction']:,.2f}")
print(f" Regular Avg: ₹{festival_data['regular_avg_transaction']:,.2f}")
weekend_data = temporal['weekend_vs_weekday']
print(f" Weekend vs Weekday:")
print(f" Weekend Revenue: ₹{weekend_data['weekend_revenue']:,.2f}")
print(f" Weekday Revenue: ₹{weekend_data['weekday_revenue']:,.2f}")
print(f" Weekend Avg: ₹{weekend_data['weekend_avg_transaction']:,.2f}")
print(f" Weekday Avg: ₹{weekend_data['weekday_avg_transaction']:,.2f}")
# Performance metrics
perf = summary['performance_metrics']
print(f"\n⚡ PERFORMANCE METRICS:")
print(f" Execution Time: {perf['execution_time_seconds']:.2f} seconds")
print(f" Transactions/Second: {perf['transactions_per_second']:.2f}")
print(f" Errors Encountered: {perf['errors_encountered']}")
print("\n" + "="*70)
def validate_arguments(args) -> None:
"""Validate command line arguments"""
if args.days <= 0:
raise ValueError("Simulation days must be positive")
if args.customers <= 0:
raise ValueError("Customers per persona must be positive")
if not Path(args.config).exists():
raise FileNotFoundError(f"Configuration file not found: {args.config}")
def main() -> None:
"""Main application entry point"""
# Parse command line arguments
parser = argparse.ArgumentParser(
description="Customer Shopping Behavior Simulation System",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python main.py # Run with default settings
python main.py --days 60 --customers 2000 # Custom simulation parameters
python main.py --config custom.yaml # Use custom configuration
python main.py --output results/ # Custom output directory
python main.py --log-level DEBUG # Enable debug logging
"""
)
parser.add_argument(
'--days',
type=int,
default=30,
help='Number of days to simulate (default: 30)'
)
parser.add_argument(
'--customers',
type=int,
default=1000,
help='Number of customers per persona (default: 1000)'
)
parser.add_argument(
'--config',
type=str,
default='config/personas.yaml',
help='Path to persona configuration file (default: config/personas.yaml)'
)
parser.add_argument(
'--output',
type=str,
default='data/output',
help='Output directory for results (default: data/output)'
)
parser.add_argument(
'--seed',
type=int,
default=42,
help='Random seed for reproducibility (default: 42)'
)
parser.add_argument(
'--log-level',
type=str,
default='INFO',
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR'],
help='Logging level (default: INFO)'
)
parser.add_argument(
'--export-summary',
action='store_true',
help='Export detailed summary to JSON file'
)
args = parser.parse_args()
try:
# Setup logging
setup_logging(args.log_level)
# Print banner
print_banner()
# Validate arguments
validate_arguments(args)
print(f"🔧 Configuration:")
print(f" Config File: {args.config}")
print(f" Simulation Days: {args.days}")
print(f" Customers per Persona: {args.customers}")
print(f" Random Seed: {args.seed}")
print(f" Output Directory: {args.output}")
print(f" Log Level: {args.log_level}")
# Initialize and run simulation
print(f"\n🚀 Initializing simulation...")
simulator = CustomerBehaviorSimulator(
config_path=args.config,
random_seed=args.seed
)
# Override customers per persona if specified
if args.customers != 1000:
simulator.config.setdefault('simulation', {})['customers_per_persona'] = args.customers
# Run simulation
print(f"\n⏳ Running {args.days}-day simulation...")
transactions_df, customers_df = simulator.run_simulation(simulation_days=args.days)
# Generate summary
summary = simulator.get_simulation_summary(transactions_df, customers_df)
# Print results
print_simulation_summary(summary)
# Export data
print(f"\n💾 Exporting data to {args.output}/...")
simulator.export_data(transactions_df, customers_df, args.output)
# Export summary if requested
if args.export_summary:
summary_file = f"{args.output}/detailed_summary.json"
with open(summary_file, 'w') as f:
json.dump(summary, f, indent=2, default=str)
print(f" 📄 Detailed summary exported to {summary_file}")
print(f"\n✅ Simulation completed successfully!")
print(f"📁 Output files:")
print(f" 📊 transactions.csv - {len(transactions_df):,} transaction records")
print(f" 👥 customers.csv - {len(customers_df):,} customer profiles")
print(f" 📈 simulation_metrics.json - Performance metrics")
print(f" ⚙️ simulation_config.json - Configuration used")
if args.export_summary:
print(f" 📋 detailed_summary.json - Comprehensive analysis")
print(f"\n🎯 Ready for analysis and presentation!")
except KeyboardInterrupt:
print(f"\n⚠️ Simulation interrupted by user")
sys.exit(1)
except Exception as e:
logging.error(f"Simulation failed: {e}")
print(f"\n❌ Simulation failed: {e}")
sys.exit(1)
if __name__ == "__main__":
main()