Repository navigation
Expand file tree
/
Copy pathbase.py
More file actions
130 lines (105 loc) · 4.1 KB
/
Copy pathbase.py
File metadata and controls
130 lines (105 loc) · 4.1 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
"""
Base utilities for tiling.
- Orchestrator works in pure pandas
- Engine-specific nodes do direct DataFrame conversions.
"""
from dataclasses import dataclass
from typing import Any, List, Optional, Tuple
import numpy as np
from feast.aggregation import Aggregation
@dataclass
class IRMetadata:
"""
Metadata about intermediate representations for an aggregation.
Attributes:
type: "algebraic" (sum, count, max, min) or "holistic" (avg, std, var)
ir_columns: List of IR column names (e.g., ["sum", "count"] for avg)
computation: String describing how to compute final value from IRs
"""
type: str # "algebraic" or "holistic"
ir_columns: Optional[List[str]] = None
computation: Optional[str] = None
def get_ir_metadata_for_aggregation(
agg: Aggregation, feature_name: str
) -> Tuple[List[Tuple[str, Any]], IRMetadata]:
"""
Get intermediate representation metadata for an aggregation.
This function determines what intermediate values need to be stored
to correctly compute holistic aggregations when merging tiles.
For example:
- avg: store sum and count (not just final average)
- std: store sum, count, and sum_of_squares
- sum: just store sum (algebraic, no IRs needed)
Args:
agg: Aggregation specification
feature_name: Name of the feature being aggregated
Returns:
Tuple of (expression_list, metadata)
"""
agg_type = agg.function.lower()
# Algebraic aggregations
if agg_type in ["sum", "count", "max", "min"]:
return ([], IRMetadata(type="algebraic"))
# Holistic aggregations (need intermediate representations)
elif agg_type in ["avg", "mean"]:
# Average needs sum and count
ir_cols = [
f"_tail_{feature_name}_sum",
f"_tail_{feature_name}_count",
]
return (
[],
IRMetadata(
type="avg", # Mark as avg for special handling
ir_columns=ir_cols,
computation="sum / count",
),
)
elif agg_type in ["std", "stddev", "var", "variance"]:
# Std/Var need sum, count, and sum of squares
ir_cols = [
f"_tail_{feature_name}_sum",
f"_tail_{feature_name}_count",
f"_tail_{feature_name}_sum_sq",
]
return (
[],
IRMetadata(
type="holistic",
ir_columns=ir_cols,
computation="sqrt((sum_sq - sum^2/count) / (count-1))",
),
)
elif agg_type == "count_distinct":
raise ValueError(
"count_distinct does not support tiling. "
"Use enable_tiling=False or choose an algebraic aggregation (sum, count, min, max)."
)
else:
# Unknown aggregation: treat as algebraic
return ([], IRMetadata(type="algebraic"))
def compute_holistic_value_from_irs(
agg: Aggregation, ir_sum: Any, ir_count: Any, ir_sum_sq: Any = None
) -> Any:
"""
Compute the final value of a holistic aggregation from its IR components.
Works on scalars as well as array-likes (e.g. pandas Series).
- avg/mean: sum / count (0 when count is 0)
- var/variance: sample variance (sum_sq - sum^2 / count) / (count - 1)
- std/stddev: square root of the sample variance
Sample variance and standard deviation are NaN when count < 2, matching
the non-tiled aggregation.
"""
agg_type = agg.function.lower()
count = np.asarray(ir_count, dtype="float64")
total = np.asarray(ir_sum, dtype="float64")
with np.errstate(divide="ignore", invalid="ignore"):
if agg_type in ["std", "stddev", "var", "variance"]:
sum_sq = np.asarray(ir_sum_sq, dtype="float64")
variance = (sum_sq - total**2 / count) / (count - 1)
# Clamp tiny negative values caused by floating point cancellation
variance = np.where(count > 1, np.maximum(variance, 0.0), np.nan)
if agg_type in ["std", "stddev"]:
return np.sqrt(variance)
return variance
return np.where(count > 0, total / count, 0)