-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompute_features_demo.py
More file actions
333 lines (285 loc) · 9.9 KB
/
compute_features_demo.py
File metadata and controls
333 lines (285 loc) · 9.9 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
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
#!/usr/bin/env python
"""Demo script for feature engineering computation.
Usage:
uv run python examples/compute_features_demo.py
This script demonstrates how to:
1. Configure feature engineering with various feature types
2. Compute time-safe features via the API
3. Preview features for debugging
Requirements:
- API server running: uv run uvicorn app.main:app --port 8123
- Database seeded with sales data
"""
import argparse
import json
import sys
from datetime import date
import httpx
# API configuration
API_BASE = "http://localhost:8123"
FEATURES_ENDPOINT = f"{API_BASE}/featuresets"
def create_sample_config() -> dict:
"""Create a sample feature configuration.
Returns:
FeatureSetConfig as a dictionary.
"""
return {
"name": "retail_forecast_v1",
"description": "Standard retail forecasting features",
"entity_columns": ["store_id", "product_id"],
"date_column": "date",
"target_column": "quantity",
"lag_config": {
"lags": [1, 7, 14, 28],
"target_column": "quantity",
"fill_value": None,
},
"rolling_config": {
"windows": [7, 14, 28],
"aggregations": ["mean", "std", "min", "max"],
"target_column": "quantity",
"min_periods": 7,
},
"calendar_config": {
"include_day_of_week": True,
"include_month": True,
"include_quarter": True,
"include_year": False,
"include_is_weekend": True,
"include_is_month_end": False,
"include_is_holiday": False,
"use_cyclical_encoding": True,
},
"imputation_config": {
"strategies": {
"quantity": "zero",
"unit_price": "ffill",
}
},
}
def create_phase2_config() -> dict:
"""Create a Phase 2-enabled feature configuration.
Extends create_sample_config() with the three Phase 2 sub-configs
(lifecycle, replenishment, promotion). Use this with the same
/featuresets/compute and /featuresets/preview endpoints.
Returns:
FeatureSetConfig as a dictionary with Phase 2 sub-configs set.
"""
cfg = create_sample_config()
cfg["name"] = "retail_forecast_phase2_v1"
cfg["lifecycle_config"] = {
"include_days_since_launch": True,
"include_days_since_discontinue": True,
"lag_days": 1,
}
cfg["replenishment_config"] = {
"include_days_since_last": True,
"include_count_window": True,
"lag_days": 1,
"count_window_days": 14,
}
cfg["promotion_config"] = {
"kinds_to_track": ["markdown"],
"include_active": True,
"include_intensity": True,
"lag_days": 1,
}
return cfg
def compute_features(
store_id: int,
product_id: int,
cutoff_date: date,
lookback_days: int = 365,
) -> dict:
"""Compute features for a single series.
Args:
store_id: Store identifier.
product_id: Product identifier.
cutoff_date: Date up to which features are computed.
lookback_days: Number of days of history to use.
Returns:
API response with computed features.
"""
request_body = {
"store_id": store_id,
"product_id": product_id,
"cutoff_date": cutoff_date.isoformat(),
"lookback_days": lookback_days,
"config": create_sample_config(),
}
with httpx.Client(timeout=30.0) as client:
response = client.post(
f"{FEATURES_ENDPOINT}/compute",
json=request_body,
)
response.raise_for_status()
return response.json()
def preview_features(
store_id: int,
product_id: int,
cutoff_date: date,
sample_rows: int = 10,
) -> dict:
"""Preview features for debugging.
Args:
store_id: Store identifier.
product_id: Product identifier.
cutoff_date: Date up to which features are computed.
sample_rows: Number of sample rows to return.
Returns:
API response with sample feature rows.
"""
request_body = {
"store_id": store_id,
"product_id": product_id,
"cutoff_date": cutoff_date.isoformat(),
"sample_rows": sample_rows,
"config": create_sample_config(),
}
with httpx.Client(timeout=30.0) as client:
response = client.post(
f"{FEATURES_ENDPOINT}/preview",
json=request_body,
)
response.raise_for_status()
return response.json()
def print_phase2_columns(
store_id: int = 1,
product_id: int = 1,
cutoff_date: date = date(2024, 1, 31),
) -> None:
"""Run a /featuresets/preview call with Phase 2 sub-configs and print
the Phase 2 columns from the response.
"""
cfg = create_phase2_config()
print("Phase 2 feature configuration constructed:")
print(f" Lifecycle: {cfg['lifecycle_config']}")
print(f" Replenishment: {cfg['replenishment_config']}")
print(f" Promotion: {cfg['promotion_config']}")
print()
body = {
"store_id": store_id,
"product_id": product_id,
"cutoff_date": cutoff_date.isoformat(),
"sample_rows": 3,
"config": cfg,
}
with httpx.Client(timeout=30.0) as client:
try:
response = client.post(f"{FEATURES_ENDPOINT}/preview", json=body)
response.raise_for_status()
except httpx.ConnectError:
print("ERROR: Cannot connect to API server.", file=sys.stderr)
print(
"Please start the server with: uv run uvicorn app.main:app --port 8123",
file=sys.stderr,
)
return
except httpx.HTTPStatusError as e:
print(
f" Phase 2 preview returned HTTP {e.response.status_code}: {e.response.text}",
file=sys.stderr,
)
return
result = response.json()
phase2_cols = [
c
for c in result["feature_columns"]
if c.startswith(("days_since_", "replenishment_", "promo_"))
]
print(f" Phase 2 columns returned ({len(phase2_cols)}):")
for col in phase2_cols:
print(f" - {col}")
def main() -> None:
"""Run the feature engineering demo."""
parser = argparse.ArgumentParser(description="Feature engineering demo")
parser.add_argument(
"--phase2",
action="store_true",
help="Run the Phase 2 (lifecycle/replenishment/promotion) preview after the Phase 1 demo",
)
args, _ = parser.parse_known_args()
print("ForecastLabAI - Feature Engineering Demo")
print("=" * 50)
print()
# Demo parameters
store_id = 1
product_id = 1
cutoff_date = date(2024, 1, 31)
print(f"Store ID: {store_id}")
print(f"Product ID: {product_id}")
print(f"Cutoff Date: {cutoff_date}")
print()
# Show configuration
print("Feature Configuration:")
print("-" * 30)
config = create_sample_config()
print(f" Name: {config['name']}")
print(f" Lag features: {config['lag_config']['lags']}")
print(f" Rolling windows: {config['rolling_config']['windows']}")
print(f" Rolling aggregations: {config['rolling_config']['aggregations']}")
print(f" Cyclical encoding: {config['calendar_config']['use_cyclical_encoding']}")
print()
try:
# Preview features
print("Previewing features (10 sample rows)...")
print("-" * 30)
result = preview_features(
store_id=store_id,
product_id=product_id,
cutoff_date=cutoff_date,
sample_rows=10,
)
print(f" Config hash: {result['config_hash']}")
print(f" Row count: {result['row_count']}")
print(f" Feature columns: {len(result['feature_columns'])}")
print(f" Duration: {result['duration_ms']:.2f}ms")
print()
print("Feature columns:")
for col in result["feature_columns"]:
print(f" - {col}")
print()
print("Sample rows (last 3):")
for row in result["rows"][-3:]:
print(f" Date: {row['date']}")
print(f" Features: {json.dumps(row['features'], indent=6)}")
print()
# Compute full features
print("Computing full features...")
print("-" * 30)
full_result = compute_features(
store_id=store_id,
product_id=product_id,
cutoff_date=cutoff_date,
lookback_days=365,
)
print(f" Total rows: {full_result['row_count']}")
print(f" Duration: {full_result['duration_ms']:.2f}ms")
print()
# Show null counts
if full_result["null_counts"]:
print("Null counts per feature:")
for col, count in sorted(full_result["null_counts"].items()):
if count > 0:
print(f" {col}: {count}")
print()
if args.phase2:
print()
print("=" * 50)
print("Phase 2 feature demo")
print("=" * 50)
print_phase2_columns(
store_id=store_id,
product_id=product_id,
cutoff_date=cutoff_date,
)
print("Demo completed successfully!")
except httpx.ConnectError:
print("ERROR: Cannot connect to API server.")
print("Please start the server with:")
print(" uv run uvicorn app.main:app --port 8123")
except httpx.HTTPStatusError as e:
print(f"ERROR: API returned status {e.response.status_code}")
print(f"Response: {e.response.text}")
if __name__ == "__main__":
main()