File size: 14,121 Bytes
7affe31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f14291
7affe31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f14291
 
 
 
 
7affe31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
"""Small timeout-risk model for Huaxiang ChaPanda orders.

The first dataset only contains delayed orders, so the model supports two modes:

* labeled_empirical_bayes: used when both timeout and non-timeout samples exist.
* positive_only_time_density: used when only timeout samples exist. This is a
  relative risk estimate by order time, not a calibrated probability.
"""

from __future__ import annotations

import csv
import json
import math
from collections import Counter, defaultdict
from dataclasses import dataclass
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, Callable, Iterable

MODEL_VERSION = 1
CSV_FIELDS = [
    "product_name",
    "order_time",
    "timeout",
    "is_timeout",
    "expected_latest_time",
    "actual_arrival_time",
    "submitted_at",
    "notes",
]


@dataclass(frozen=True)
class TrainingRecord:
    product_name: str
    order_time: datetime
    is_timeout: bool
    source: str


def parse_datetime(value: Any) -> datetime | None:
    if value is None:
        return None
    text = str(value).strip()
    if not text:
        return None

    normalized = text.replace("/", "-").replace("T", " ")
    if len(normalized) == 16:
        normalized = f"{normalized}:00"

    formats = (
        "%Y-%m-%d %H:%M:%S",
        "%Y-%m-%d %H:%M",
        "%Y-%m-%d",
    )
    for fmt in formats:
        try:
            return datetime.strptime(normalized, fmt)
        except ValueError:
            pass

    try:
        return datetime.fromisoformat(normalized)
    except ValueError:
        return None


def normalize_row(row: dict[str, Any]) -> dict[str, str]:
    return {str(key).strip(): str(value).strip() for key, value in row.items() if key is not None}


def coerce_bool(value: Any) -> bool | None:
    if value is None:
        return None
    text = str(value).strip().lower()
    if not text:
        return None
    true_values = {"1", "true", "yes", "y", "timeout", "late", "超时", "是", "yes_timeout"}
    false_values = {"0", "false", "no", "n", "on_time", "not_timeout", "未超时", "否"}
    if text in true_values:
        return True
    if text in false_values:
        return False
    if "未超时" in text or "准时" in text:
        return False
    if "超时" in text or "延迟" in text:
        return True
    return None


def infer_timeout_label(row: dict[str, str], forced_timeout: bool | None = None) -> bool | None:
    if forced_timeout is not None:
        return forced_timeout

    for key in ("is_timeout", "timeout", "timeout_status", "status"):
        parsed = coerce_bool(row.get(key))
        if parsed is not None:
            return parsed

    expected = parse_datetime(row.get("expected_latest_time"))
    actual = parse_datetime(row.get("actual_arrival_time"))
    if expected and actual:
        return actual > expected
    return None


def read_csv_records(path: Path, forced_timeout: bool | None = None) -> list[TrainingRecord]:
    if not path.exists():
        return []

    records: list[TrainingRecord] = []
    with path.open("r", encoding="utf-8-sig", newline="") as handle:
        reader = csv.DictReader(handle)
        for raw_row in reader:
            row = normalize_row(raw_row)
            order_time = parse_datetime(row.get("order_time"))
            is_timeout = infer_timeout_label(row, forced_timeout=forced_timeout)
            if not order_time or is_timeout is None:
                continue

            product_name = row.get("product_name", "").strip() or "unknown"
            records.append(
                TrainingRecord(
                    product_name=product_name,
                    order_time=order_time,
                    is_timeout=is_timeout,
                    source=str(path),
                )
            )
    return records


def load_dataset_records(dataset_dir: str | Path) -> list[TrainingRecord]:
    dataset_path = Path(dataset_dir)
    records_path = dataset_path / "records.csv"
    if records_path.exists():
        records = read_csv_records(records_path, forced_timeout=None)
    else:
        records = read_csv_records(dataset_path / "timeout_records.csv", forced_timeout=True)

    tmp_dir = dataset_path / "tmp"
    if tmp_dir.exists():
        for tmp_file in sorted(tmp_dir.glob("tmp_records_*.csv")):
            records.extend(read_csv_records(tmp_file, forced_timeout=None))

    return records


def _time_bucket(dt: datetime) -> str:
    hour = dt.hour
    if 6 <= hour < 11:
        return "morning"
    if 11 <= hour < 14:
        return "lunch"
    if 14 <= hour < 17:
        return "afternoon"
    if 17 <= hour < 20:
        return "dinner"
    if 20 <= hour < 24:
        return "night"
    return "late_night"


def _sigmoid(value: float) -> float:
    return 1.0 / (1.0 + math.exp(-value))


def _logit(probability: float) -> float:
    clipped = min(max(probability, 0.02), 0.98)
    return math.log(clipped / (1.0 - clipped))


def _clip_probability(value: float) -> float:
    return round(min(max(value, 0.02), 0.98), 4)


def _empirical_probabilities(
    records: list[TrainingRecord],
    key_fn: Callable[[TrainingRecord], str],
    base_probability: float,
    prior_strength: float,
) -> dict[str, dict[str, float | int]]:
    grouped: dict[str, list[TrainingRecord]] = defaultdict(list)
    for record in records:
        grouped[key_fn(record)].append(record)

    stats: dict[str, dict[str, float | int]] = {}
    for key, items in grouped.items():
        total = len(items)
        timeouts = sum(1 for item in items if item.is_timeout)
        probability = (timeouts + prior_strength * base_probability) / (total + prior_strength)
        stats[str(key)] = {
            "total": total,
            "timeouts": timeouts,
            "probability": _clip_probability(probability),
        }
    return stats


def _positive_density_probabilities(
    records: list[TrainingRecord],
    key_fn: Callable[[TrainingRecord], str],
    keys: Iterable[str],
    low: float,
    high: float,
) -> dict[str, dict[str, float | int]]:
    counts = Counter(key_fn(record) for record in records if record.is_timeout)
    max_count = max(counts.values(), default=0)
    output: dict[str, dict[str, float | int]] = {}
    for key in keys:
        count = counts.get(key, 0)
        density = count / max_count if max_count else 0.0
        probability = low + (high - low) * density
        output[str(key)] = {
            "positive_count": count,
            "probability": _clip_probability(probability),
        }
    return output


def _smoothed_hour_density(records: list[TrainingRecord]) -> dict[str, dict[str, float | int]]:
    raw_counts = Counter(record.order_time.hour for record in records if record.is_timeout)
    smoothed: dict[int, float] = {}
    for hour in range(24):
        smoothed[hour] = (
            raw_counts.get(hour, 0)
            + 0.45 * raw_counts.get((hour - 1) % 24, 0)
            + 0.45 * raw_counts.get((hour + 1) % 24, 0)
            + 0.15 * raw_counts.get((hour - 2) % 24, 0)
            + 0.15 * raw_counts.get((hour + 2) % 24, 0)
        )
    max_score = max(smoothed.values(), default=0.0)

    output: dict[str, dict[str, float | int]] = {}
    for hour in range(24):
        density = smoothed[hour] / max_score if max_score else 0.0
        probability = 0.12 + 0.68 * density
        output[str(hour)] = {
            "positive_count": raw_counts.get(hour, 0),
            "smoothed_density": round(density, 4),
            "probability": _clip_probability(probability),
        }
    return output


def train_timeout_model(records: list[TrainingRecord]) -> dict[str, Any]:
    total = len(records)
    positives = sum(1 for record in records if record.is_timeout)
    negatives = total - positives
    generated_at = datetime.now(timezone.utc).isoformat()

    if total == 0:
        return {
            "model_version": MODEL_VERSION,
            "mode": "empty_default",
            "generated_at": generated_at,
            "base_probability": 0.35,
            "hour_probabilities": {str(hour): {"probability": 0.35, "total": 0, "timeouts": 0} for hour in range(24)},
            "weekday_probabilities": {str(day): {"probability": 0.35, "total": 0, "timeouts": 0} for day in range(7)},
            "time_bucket_probabilities": {},
            "product_probabilities": {},
            "metadata": {
                "training_records": 0,
                "timeout_records": 0,
                "non_timeout_records": 0,
                "calibration": "No valid records were found. Default probability is a placeholder.",
            },
        }

    if positives > 0 and negatives > 0:
        base_probability = (positives + 2.0) / (total + 4.0)
        mode = "labeled_empirical_bayes"
        calibration = "Calibrated from user-labeled timeout and non-timeout records with Bayesian smoothing."
        hour_probabilities = _empirical_probabilities(records, lambda r: str(r.order_time.hour), base_probability, 6.0)
        weekday_probabilities = _empirical_probabilities(records, lambda r: str(r.order_time.weekday()), base_probability, 8.0)
        time_bucket_probabilities = _empirical_probabilities(records, lambda r: _time_bucket(r.order_time), base_probability, 5.0)
        product_probabilities = _empirical_probabilities(records, lambda r: r.product_name, base_probability, 8.0)
    else:
        base_probability = 0.35
        mode = "positive_only_time_density"
        calibration = (
            "Only timeout records were available. Output is a relative time-risk estimate, "
            "not a fully calibrated timeout probability."
        )
        positive_records = [record for record in records if record.is_timeout]
        hour_probabilities = _smoothed_hour_density(positive_records)
        weekday_probabilities = _positive_density_probabilities(
            positive_records,
            lambda r: str(r.order_time.weekday()),
            [str(day) for day in range(7)],
            low=0.18,
            high=0.62,
        )
        time_bucket_probabilities = _positive_density_probabilities(
            positive_records,
            lambda r: _time_bucket(r.order_time),
            ["morning", "lunch", "afternoon", "dinner", "night", "late_night"],
            low=0.16,
            high=0.70,
        )
        product_probabilities = {}

    return {
        "model_version": MODEL_VERSION,
        "mode": mode,
        "generated_at": generated_at,
        "base_probability": _clip_probability(base_probability),
        "hour_probabilities": hour_probabilities,
        "weekday_probabilities": weekday_probabilities,
        "time_bucket_probabilities": time_bucket_probabilities,
        "product_probabilities": product_probabilities,
        "metadata": {
            "training_records": total,
            "timeout_records": positives,
            "non_timeout_records": negatives,
            "calibration": calibration,
            "features": ["order hour", "weekday", "time bucket", "optional product name"],
        },
    }


def load_model(path: str | Path) -> dict[str, Any]:
    with Path(path).open("r", encoding="utf-8") as handle:
        return json.load(handle)


def save_model(model: dict[str, Any], path: str | Path) -> None:
    output_path = Path(path)
    output_path.parent.mkdir(parents=True, exist_ok=True)
    with output_path.open("w", encoding="utf-8") as handle:
        json.dump(model, handle, ensure_ascii=False, indent=2, sort_keys=True)
        handle.write("\n")


def predict_timeout_probability(
    model: dict[str, Any],
    order_time: str | datetime,
    product_name: str = "",
) -> dict[str, Any]:
    dt = order_time if isinstance(order_time, datetime) else parse_datetime(order_time)
    if dt is None:
        raise ValueError("order_time must look like '2026-05-24 18:30'.")

    base = float(model.get("base_probability", 0.35))
    mode = model.get("mode", "unknown")

    def component_probability(section: str, key: str, fallback: float) -> float:
        value = model.get(section, {}).get(str(key), {})
        try:
            return float(value.get("probability", fallback))
        except (TypeError, ValueError):
            return fallback

    hour_probability = component_probability("hour_probabilities", str(dt.hour), base)
    weekday_probability = component_probability("weekday_probabilities", str(dt.weekday()), base)
    bucket_probability = component_probability("time_bucket_probabilities", _time_bucket(dt), base)
    product_probability = None
    if product_name:
        product_probability = component_probability("product_probabilities", product_name.strip(), base)

    if mode == "labeled_empirical_bayes":
        combined_logit = _logit(base)
        combined_logit += 0.58 * (_logit(hour_probability) - _logit(base))
        combined_logit += 0.18 * (_logit(weekday_probability) - _logit(base))
        combined_logit += 0.16 * (_logit(bucket_probability) - _logit(base))
        if product_probability is not None:
            combined_logit += 0.08 * (_logit(product_probability) - _logit(base))
        probability = _sigmoid(combined_logit)
    else:
        combined_logit = _logit(base)
        combined_logit += 0.70 * (_logit(hour_probability) - _logit(base))
        combined_logit += 0.18 * (_logit(bucket_probability) - _logit(base))
        combined_logit += 0.12 * (_logit(weekday_probability) - _logit(base))
        probability = _sigmoid(combined_logit)

    clipped = _clip_probability(probability)
    if clipped >= 0.65:
        level = "high"
    elif clipped >= 0.4:
        level = "medium"
    else:
        level = "low"

    return {
        "probability": clipped,
        "risk_level": level,
        "mode": mode,
        "order_time": dt.strftime("%Y-%m-%d %H:%M"),
        "components": {
            "base": base,
            "hour": hour_probability,
            "weekday": weekday_probability,
            "time_bucket": bucket_probability,
            "product": product_probability,
        },
        "calibration": model.get("metadata", {}).get("calibration", ""),
    }