-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats_io.py
More file actions
40 lines (32 loc) · 1.31 KB
/
Copy pathstats_io.py
File metadata and controls
40 lines (32 loc) · 1.31 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
"""Shared helper for writing per-lead-window outputs.
Both ``compute_stats`` (per-stat accumulator dicts) and the downloaders'
``extract_forecast`` (per-lead arrays) iterate ``(start, end)`` windows and
write a combined output for each window where every lead is present. The
combine step differs (sum of accumulator dicts vs mean of arrays); the
iteration shape is shared.
"""
from __future__ import annotations
from typing import Callable, Iterable, TypeVar
T = TypeVar("T")
def write_lead_windows(
per_lead: dict[int, T],
windows: Iterable[tuple[int, int]],
*,
write_fn: Callable[[int, int, T], None],
combine_fn: Callable[[list[T]], T],
) -> int:
"""For each complete window, combine its leads and call ``write_fn``.
A window is "complete" iff every lead from ``start`` to ``end`` (inclusive)
appears in ``per_lead``. Incomplete windows are silently skipped.
Returns the number of windows written.
"""
sorted_leads = sorted(per_lead)
written = 0
for start, end in windows:
leads_in_window = [ld for ld in sorted_leads if start <= ld <= end]
if len(leads_in_window) != end - start + 1:
continue
combined = combine_fn([per_lead[ld] for ld in leads_in_window])
write_fn(start, end, combined)
written += 1
return written