upgini 1.2.62a3818.dev3__tar.gz → 1.2.62a3818.dev4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of upgini might be problematic. Click here for more details.
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/PKG-INFO +1 -1
- upgini-1.2.62a3818.dev4/src/upgini/__about__.py +1 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/autofe/all_operands.py +1 -1
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/autofe/operator.py +5 -3
- upgini-1.2.62a3818.dev4/src/upgini/autofe/timeseries/__init__.py +23 -0
- upgini-1.2.62a3818.dev4/src/upgini/autofe/timeseries/base.py +105 -0
- upgini-1.2.62a3818.dev4/src/upgini/autofe/timeseries/cross.py +130 -0
- upgini-1.2.62a3818.dev4/src/upgini/autofe/timeseries/delta.py +125 -0
- upgini-1.2.62a3818.dev4/src/upgini/autofe/timeseries/lag.py +68 -0
- upgini-1.2.62a3818.dev4/src/upgini/autofe/timeseries/roll.py +92 -0
- upgini-1.2.62a3818.dev4/src/upgini/autofe/timeseries/trend.py +61 -0
- upgini-1.2.62a3818.dev4/src/upgini/autofe/timeseries/volatility.py +259 -0
- upgini-1.2.62a3818.dev3/src/upgini/__about__.py +0 -1
- upgini-1.2.62a3818.dev3/src/upgini/autofe/timeseries.py +0 -200
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/.gitignore +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/LICENSE +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/README.md +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/pyproject.toml +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/__init__.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/ads.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/ads_management/__init__.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/ads_management/ads_manager.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/autofe/__init__.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/autofe/binary.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/autofe/date.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/autofe/feature.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/autofe/groupby.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/autofe/unary.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/autofe/vector.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/data_source/__init__.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/data_source/data_source_publisher.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/dataset.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/errors.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/features_enricher.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/http.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/lazy_import.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/mdc/__init__.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/mdc/context.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/metadata.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/metrics.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/normalizer/__init__.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/normalizer/normalize_utils.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/resource_bundle/__init__.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/resource_bundle/exceptions.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/resource_bundle/strings.properties +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/resource_bundle/strings_widget.properties +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/sampler/__init__.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/sampler/base.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/sampler/random_under_sampler.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/sampler/utils.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/search_task.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/spinner.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/utils/Roboto-Regular.ttf +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/utils/__init__.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/utils/base_search_key_detector.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/utils/blocked_time_series.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/utils/country_utils.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/utils/custom_loss_utils.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/utils/cv_utils.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/utils/datetime_utils.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/utils/deduplicate_utils.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/utils/display_utils.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/utils/email_utils.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/utils/fallback_progress_bar.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/utils/feature_info.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/utils/features_validator.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/utils/format.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/utils/ip_utils.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/utils/mstats.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/utils/phone_utils.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/utils/postal_code_utils.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/utils/progress_bar.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/utils/sklearn_ext.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/utils/sort.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/utils/target_utils.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/utils/track_info.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/utils/ts_utils.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/utils/warning_counter.py +0 -0
- {upgini-1.2.62a3818.dev3 → upgini-1.2.62a3818.dev4}/src/upgini/version_validator.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: upgini
|
|
3
|
-
Version: 1.2.62a3818.
|
|
3
|
+
Version: 1.2.62a3818.dev4
|
|
4
4
|
Summary: Intelligent data search & enrichment for Machine Learning
|
|
5
5
|
Project-URL: Bug Reports, https://github.com/upgini/upgini/issues
|
|
6
6
|
Project-URL: Homepage, https://upgini.com/
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "1.2.62a3818.dev4"
|
|
@@ -28,15 +28,17 @@ class OperatorRegistry(type(BaseModel)):
|
|
|
28
28
|
try:
|
|
29
29
|
instance = new_class()
|
|
30
30
|
cls._registry[instance.name] = new_class
|
|
31
|
+
if instance.alias:
|
|
32
|
+
cls._registry[instance.alias] = new_class
|
|
31
33
|
except Exception:
|
|
32
34
|
pass
|
|
33
35
|
return new_class
|
|
34
36
|
|
|
35
37
|
@classmethod
|
|
36
|
-
def
|
|
38
|
+
def get_operator(cls, name: str) -> Optional["Operator"]:
|
|
37
39
|
# First try to resolve as a parametrized operand formula
|
|
38
|
-
for
|
|
39
|
-
resolved =
|
|
40
|
+
for operator_cls in cls._parametrized_registry:
|
|
41
|
+
resolved = operator_cls.from_formula(name)
|
|
40
42
|
if resolved is not None:
|
|
41
43
|
return resolved
|
|
42
44
|
# Fall back to direct registry lookup
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"""Time series feature engineering operators."""
|
|
2
|
+
|
|
3
|
+
from upgini.autofe.timeseries.base import TimeSeriesBase
|
|
4
|
+
from upgini.autofe.timeseries.roll import Roll
|
|
5
|
+
from upgini.autofe.timeseries.lag import Lag
|
|
6
|
+
from upgini.autofe.timeseries.delta import Delta, Delta2
|
|
7
|
+
from upgini.autofe.timeseries.trend import TrendCoefficient
|
|
8
|
+
from upgini.autofe.timeseries.volatility import EWMAVolatility, RollingVolatility, RollingVolatility2, VolatilityRatio
|
|
9
|
+
from upgini.autofe.timeseries.cross import CrossSeriesInteraction
|
|
10
|
+
|
|
11
|
+
__all__ = [
|
|
12
|
+
"TimeSeriesBase",
|
|
13
|
+
"Roll",
|
|
14
|
+
"Lag",
|
|
15
|
+
"Delta",
|
|
16
|
+
"Delta2",
|
|
17
|
+
"TrendCoefficient",
|
|
18
|
+
"EWMAVolatility",
|
|
19
|
+
"RollingVolatility",
|
|
20
|
+
"RollingVolatility2",
|
|
21
|
+
"VolatilityRatio",
|
|
22
|
+
"CrossSeriesInteraction",
|
|
23
|
+
]
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import abc
|
|
2
|
+
from typing import Dict, List, Optional
|
|
3
|
+
|
|
4
|
+
import pandas as pd
|
|
5
|
+
from upgini.autofe.operator import PandasOperator
|
|
6
|
+
|
|
7
|
+
# Used in derived classes
|
|
8
|
+
try:
|
|
9
|
+
from pydantic import field_validator as validator # V2
|
|
10
|
+
except ImportError:
|
|
11
|
+
from pydantic import validator # V1
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class TimeSeriesBase(PandasOperator, abc.ABC):
|
|
15
|
+
is_vector: bool = True
|
|
16
|
+
date_unit: Optional[str] = None
|
|
17
|
+
offset_size: int = 0
|
|
18
|
+
offset_unit: str = "D"
|
|
19
|
+
|
|
20
|
+
def get_params(self) -> Dict[str, Optional[str]]:
|
|
21
|
+
res = super().get_params()
|
|
22
|
+
res.update(
|
|
23
|
+
{
|
|
24
|
+
"date_unit": self.date_unit,
|
|
25
|
+
"offset_size": self.offset_size,
|
|
26
|
+
"offset_unit": self.offset_unit,
|
|
27
|
+
}
|
|
28
|
+
)
|
|
29
|
+
return res
|
|
30
|
+
|
|
31
|
+
def calculate_vector(self, data: List[pd.Series]) -> pd.Series:
|
|
32
|
+
# assuming first is date, last is value, rest is group columns
|
|
33
|
+
date = pd.to_datetime(data[0], unit=self.date_unit, errors="coerce")
|
|
34
|
+
ts = pd.concat([date] + data[1:], axis=1)
|
|
35
|
+
ts.drop_duplicates(subset=ts.columns[:-1], keep="first", inplace=True)
|
|
36
|
+
ts.set_index(date.name, inplace=True)
|
|
37
|
+
ts = ts[ts.index.notna()].sort_index()
|
|
38
|
+
ts = (
|
|
39
|
+
ts.groupby([c.name for c in data[1:-1]], group_keys=True)
|
|
40
|
+
.apply(self._shift)[data[-1].name]
|
|
41
|
+
.to_frame()
|
|
42
|
+
.reset_index()
|
|
43
|
+
.set_index(date.name)
|
|
44
|
+
.groupby([c.name for c in data[1:-1]], group_keys=True)
|
|
45
|
+
if len(data) > 2
|
|
46
|
+
else self._shift(ts)
|
|
47
|
+
)
|
|
48
|
+
ts = self._aggregate(ts)
|
|
49
|
+
ts = ts.reindex(data[1:-1] + [date] if len(data) > 2 else date).reset_index()
|
|
50
|
+
ts.index = date.index
|
|
51
|
+
|
|
52
|
+
return ts.iloc[:, -1]
|
|
53
|
+
|
|
54
|
+
def _shift(self, ts: pd.DataFrame) -> pd.DataFrame:
|
|
55
|
+
if self.offset_size > 0:
|
|
56
|
+
return ts.iloc[:, :-1].merge(
|
|
57
|
+
ts.iloc[:, -1].shift(freq=f"{self.offset_size}{self.offset_unit}"),
|
|
58
|
+
left_index=True,
|
|
59
|
+
right_index=True,
|
|
60
|
+
)
|
|
61
|
+
return ts
|
|
62
|
+
|
|
63
|
+
@abc.abstractmethod
|
|
64
|
+
def _aggregate(self, ts: pd.DataFrame) -> pd.DataFrame:
|
|
65
|
+
pass
|
|
66
|
+
|
|
67
|
+
def _add_offset_to_formula(self, base_formula: str) -> str:
|
|
68
|
+
if self.offset_size > 0:
|
|
69
|
+
return f"{base_formula}_offset_{self.offset_size}{self.offset_unit}"
|
|
70
|
+
return base_formula
|
|
71
|
+
|
|
72
|
+
@classmethod
|
|
73
|
+
def _parse_offset_from_formula(cls, formula: str, base_regex: str) -> tuple[Optional[dict], Optional[str]]:
|
|
74
|
+
"""
|
|
75
|
+
Parse the offset component from a formula.
|
|
76
|
+
|
|
77
|
+
Args:
|
|
78
|
+
formula: The formula to parse
|
|
79
|
+
base_regex: The regex pattern for the base formula (without offset)
|
|
80
|
+
|
|
81
|
+
Returns:
|
|
82
|
+
A tuple with:
|
|
83
|
+
- Dictionary with offset parameters if found, None otherwise
|
|
84
|
+
- Remaining part of the formula after removing offset component (for further parsing)
|
|
85
|
+
"""
|
|
86
|
+
import re
|
|
87
|
+
|
|
88
|
+
offset_regex = f"{base_regex}_offset_(\\d+)([a-zA-Z])"
|
|
89
|
+
match = re.match(offset_regex, formula)
|
|
90
|
+
|
|
91
|
+
if match:
|
|
92
|
+
# Get groups from the offset part
|
|
93
|
+
offset_size = int(match.group(match.lastindex - 1))
|
|
94
|
+
offset_unit = match.group(match.lastindex)
|
|
95
|
+
|
|
96
|
+
# Return the parameters and the base formula for further parsing if needed
|
|
97
|
+
# Extract the base formula by using the match object
|
|
98
|
+
base_formula = formula[: match.start(match.lastindex - 1) - len("_offset_")]
|
|
99
|
+
return {"offset_size": offset_size, "offset_unit": offset_unit}, base_formula
|
|
100
|
+
|
|
101
|
+
# Check if it matches the base regex (no offset)
|
|
102
|
+
if re.match(f"^{base_regex}$", formula) or re.match(f"^{base_regex}_", formula):
|
|
103
|
+
return None, formula
|
|
104
|
+
|
|
105
|
+
return None, None
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
from typing import Dict, List, Optional
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
import pandas as pd
|
|
5
|
+
|
|
6
|
+
try:
|
|
7
|
+
from pydantic import field_validator as validator # V2
|
|
8
|
+
except ImportError:
|
|
9
|
+
from pydantic import validator # V1
|
|
10
|
+
|
|
11
|
+
from upgini.autofe.all_operands import find_op
|
|
12
|
+
from upgini.autofe.operator import PandasOperator, ParametrizedOperator
|
|
13
|
+
from upgini.autofe.timeseries.base import TimeSeriesBase
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class CrossSeriesInteraction(TimeSeriesBase, ParametrizedOperator):
|
|
17
|
+
base_name: str = "cross"
|
|
18
|
+
interaction_op: PandasOperator
|
|
19
|
+
descriptor_indices: List[int] = []
|
|
20
|
+
left_descriptor: List[str] = []
|
|
21
|
+
right_descriptor: List[str] = []
|
|
22
|
+
|
|
23
|
+
@validator("descriptor_indices")
|
|
24
|
+
@classmethod
|
|
25
|
+
def validate_descriptor_indices(cls, v):
|
|
26
|
+
if not v:
|
|
27
|
+
raise ValueError("descriptor_indices cannot be empty for CrossSeriesInteraction")
|
|
28
|
+
return v
|
|
29
|
+
|
|
30
|
+
def __init__(self, **data):
|
|
31
|
+
super().__init__(**data)
|
|
32
|
+
indices = self.descriptor_indices
|
|
33
|
+
left = self.left_descriptor
|
|
34
|
+
right = self.right_descriptor
|
|
35
|
+
|
|
36
|
+
if len(left) != len(indices):
|
|
37
|
+
raise ValueError(
|
|
38
|
+
f"left_descriptor length ({len(left)}) " f"must match descriptor_indices length ({len(indices)})"
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
if len(right) != len(indices):
|
|
42
|
+
raise ValueError(
|
|
43
|
+
f"right_descriptor length ({len(right)}) " f"must match descriptor_indices length ({len(indices)})"
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
def to_formula(self) -> str:
|
|
47
|
+
base_formula = f"{self.base_name}_{self._get_interaction_op_name()}"
|
|
48
|
+
return self._add_offset_to_formula(base_formula)
|
|
49
|
+
|
|
50
|
+
@classmethod
|
|
51
|
+
def from_formula(cls, formula: str) -> Optional["CrossSeriesInteraction"]:
|
|
52
|
+
base_regex = r"cross_(.+)"
|
|
53
|
+
|
|
54
|
+
offset_params, remaining_formula = cls._parse_offset_from_formula(formula, base_regex)
|
|
55
|
+
|
|
56
|
+
if remaining_formula is None:
|
|
57
|
+
return None
|
|
58
|
+
|
|
59
|
+
import re
|
|
60
|
+
|
|
61
|
+
match = re.match(f"^{base_regex}$", remaining_formula)
|
|
62
|
+
|
|
63
|
+
if not match:
|
|
64
|
+
return None
|
|
65
|
+
|
|
66
|
+
# Extract the operator formula
|
|
67
|
+
op_formula = match.group(1)
|
|
68
|
+
|
|
69
|
+
op = find_op(op_formula)
|
|
70
|
+
if op is None or not op.is_binary:
|
|
71
|
+
return None
|
|
72
|
+
|
|
73
|
+
# Include default values to pass validation
|
|
74
|
+
params = {
|
|
75
|
+
"interaction_op": op,
|
|
76
|
+
"descriptor_indices": [0], # Default index
|
|
77
|
+
"left_descriptor": ["default"], # Default left descriptor
|
|
78
|
+
"right_descriptor": ["default"], # Default right descriptor
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if offset_params:
|
|
82
|
+
params.update(offset_params)
|
|
83
|
+
|
|
84
|
+
return cls(**params)
|
|
85
|
+
|
|
86
|
+
def get_params(self) -> Dict[str, str | None]:
|
|
87
|
+
res = super().get_params()
|
|
88
|
+
res.update(
|
|
89
|
+
{
|
|
90
|
+
"interaction_op": self._get_interaction_op_name(),
|
|
91
|
+
"descriptor_indices": self.descriptor_indices,
|
|
92
|
+
"left_descriptor": self.left_descriptor,
|
|
93
|
+
"right_descriptor": self.right_descriptor,
|
|
94
|
+
}
|
|
95
|
+
)
|
|
96
|
+
return res
|
|
97
|
+
|
|
98
|
+
def _get_interaction_op_name(self) -> str:
|
|
99
|
+
return self.interaction_op.alias or self.interaction_op.to_formula()
|
|
100
|
+
|
|
101
|
+
def calculate_vector(self, data: List[pd.Series]) -> pd.Series:
|
|
102
|
+
left_mask = self._get_mask(data, self.left_descriptor)
|
|
103
|
+
left = self._extract_series(data, left_mask)
|
|
104
|
+
|
|
105
|
+
right_mask = self._get_mask(data, self.right_descriptor)
|
|
106
|
+
right = self._extract_series(data, right_mask)
|
|
107
|
+
|
|
108
|
+
interaction: pd.Series = self.interaction_op.calculate_binary(left, right)
|
|
109
|
+
interaction = interaction.reindex(self._get_index(data))
|
|
110
|
+
res = pd.Series(np.nan, index=data[-1].index, name=data[-1].name)
|
|
111
|
+
res.loc[left_mask] = interaction[left_mask].values
|
|
112
|
+
res.loc[right_mask] = interaction[right_mask].values
|
|
113
|
+
return res
|
|
114
|
+
|
|
115
|
+
def _get_mask(self, data: List[pd.Series], descriptor: List[str]) -> pd.Series:
|
|
116
|
+
mask = np.logical_and.reduce([data[i] == v for i, v in zip(self.descriptor_indices, descriptor)])
|
|
117
|
+
return mask
|
|
118
|
+
|
|
119
|
+
def _extract_series(self, data: List[pd.Series], mask: pd.Series) -> pd.Series:
|
|
120
|
+
masked_data = [d[mask] for d in data]
|
|
121
|
+
shifted = super().calculate_vector(masked_data)
|
|
122
|
+
shifted.index = self._get_index(masked_data)
|
|
123
|
+
return shifted
|
|
124
|
+
|
|
125
|
+
def _get_index(self, data: List[pd.Series]) -> pd.Series:
|
|
126
|
+
index = [d for i, d in enumerate(data[:-1]) if i not in self.descriptor_indices]
|
|
127
|
+
return index if len(index) > 1 else index[0]
|
|
128
|
+
|
|
129
|
+
def _aggregate(self, ts: pd.DataFrame) -> pd.DataFrame:
|
|
130
|
+
return ts.apply(lambda x: x).iloc[:, [-1]]
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import pandas as pd
|
|
2
|
+
from typing import Dict, Optional
|
|
3
|
+
|
|
4
|
+
from upgini.autofe.operator import ParametrizedOperator
|
|
5
|
+
from upgini.autofe.timeseries.base import TimeSeriesBase
|
|
6
|
+
from upgini.autofe.timeseries.lag import Lag
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Delta(TimeSeriesBase, ParametrizedOperator):
|
|
10
|
+
delta_size: int
|
|
11
|
+
delta_unit: str = "D"
|
|
12
|
+
|
|
13
|
+
def to_formula(self) -> str:
|
|
14
|
+
base_formula = f"delta_{self.delta_size}{self.delta_unit}"
|
|
15
|
+
return self._add_offset_to_formula(base_formula)
|
|
16
|
+
|
|
17
|
+
@classmethod
|
|
18
|
+
def from_formula(cls, formula: str) -> Optional["Delta"]:
|
|
19
|
+
# Base regex for Delta class
|
|
20
|
+
base_regex = r"delta_(\d+)([a-zA-Z])"
|
|
21
|
+
|
|
22
|
+
# Parse offset first
|
|
23
|
+
offset_params, remaining_formula = cls._parse_offset_from_formula(formula, base_regex)
|
|
24
|
+
|
|
25
|
+
if remaining_formula is None:
|
|
26
|
+
return None
|
|
27
|
+
|
|
28
|
+
# Now parse the delta part
|
|
29
|
+
import re
|
|
30
|
+
|
|
31
|
+
match = re.match(f"^{base_regex}$", remaining_formula)
|
|
32
|
+
|
|
33
|
+
if not match:
|
|
34
|
+
return None
|
|
35
|
+
|
|
36
|
+
delta_size = int(match.group(1))
|
|
37
|
+
delta_unit = match.group(2)
|
|
38
|
+
|
|
39
|
+
# Create instance with appropriate parameters
|
|
40
|
+
params = {
|
|
41
|
+
"delta_size": delta_size,
|
|
42
|
+
"delta_unit": delta_unit,
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if offset_params:
|
|
46
|
+
params.update(offset_params)
|
|
47
|
+
|
|
48
|
+
return cls(**params)
|
|
49
|
+
|
|
50
|
+
def get_params(self) -> Dict[str, Optional[str]]:
|
|
51
|
+
res = super().get_params()
|
|
52
|
+
res.update(
|
|
53
|
+
{
|
|
54
|
+
"delta_size": self.delta_size,
|
|
55
|
+
"delta_unit": self.delta_unit,
|
|
56
|
+
"offset_size": self.offset_size,
|
|
57
|
+
"offset_unit": self.offset_unit,
|
|
58
|
+
}
|
|
59
|
+
)
|
|
60
|
+
return res
|
|
61
|
+
|
|
62
|
+
def _aggregate(self, ts: pd.DataFrame) -> pd.DataFrame:
|
|
63
|
+
lag0 = Lag(lag_size=0, lag_unit=self.delta_unit)
|
|
64
|
+
lag = Lag(lag_size=self.delta_size, lag_unit=self.delta_unit)
|
|
65
|
+
return lag0._aggregate(ts) - lag._aggregate(ts)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class Delta2(TimeSeriesBase, ParametrizedOperator):
|
|
69
|
+
delta_size: int
|
|
70
|
+
delta_unit: str = "D"
|
|
71
|
+
|
|
72
|
+
def to_formula(self) -> str:
|
|
73
|
+
base_formula = f"delta2_{self.delta_size}{self.delta_unit}"
|
|
74
|
+
return self._add_offset_to_formula(base_formula)
|
|
75
|
+
|
|
76
|
+
@classmethod
|
|
77
|
+
def from_formula(cls, formula: str) -> Optional["Delta2"]:
|
|
78
|
+
# Base regex for Delta2 class
|
|
79
|
+
base_regex = r"delta2_(\d+)([a-zA-Z])"
|
|
80
|
+
|
|
81
|
+
# Parse offset first
|
|
82
|
+
offset_params, remaining_formula = cls._parse_offset_from_formula(formula, base_regex)
|
|
83
|
+
|
|
84
|
+
if remaining_formula is None:
|
|
85
|
+
return None
|
|
86
|
+
|
|
87
|
+
# Now parse the delta part
|
|
88
|
+
import re
|
|
89
|
+
|
|
90
|
+
match = re.match(f"^{base_regex}$", remaining_formula)
|
|
91
|
+
|
|
92
|
+
if not match:
|
|
93
|
+
return None
|
|
94
|
+
|
|
95
|
+
delta_size = int(match.group(1))
|
|
96
|
+
delta_unit = match.group(2)
|
|
97
|
+
|
|
98
|
+
# Create instance with appropriate parameters
|
|
99
|
+
params = {
|
|
100
|
+
"delta_size": delta_size,
|
|
101
|
+
"delta_unit": delta_unit,
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if offset_params:
|
|
105
|
+
params.update(offset_params)
|
|
106
|
+
|
|
107
|
+
return cls(**params)
|
|
108
|
+
|
|
109
|
+
def get_params(self) -> Dict[str, Optional[str]]:
|
|
110
|
+
res = super().get_params()
|
|
111
|
+
res.update(
|
|
112
|
+
{
|
|
113
|
+
"delta_size": self.delta_size,
|
|
114
|
+
"delta_unit": self.delta_unit,
|
|
115
|
+
}
|
|
116
|
+
)
|
|
117
|
+
return res
|
|
118
|
+
|
|
119
|
+
def _aggregate(self, ts: pd.DataFrame) -> pd.DataFrame:
|
|
120
|
+
# Calculate first delta
|
|
121
|
+
delta1 = Delta(delta_size=self.delta_size, delta_unit=self.delta_unit)
|
|
122
|
+
first_delta = delta1._aggregate(ts)
|
|
123
|
+
|
|
124
|
+
# Calculate delta of delta (second derivative)
|
|
125
|
+
return delta1._aggregate(first_delta)
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import pandas as pd
|
|
3
|
+
from typing import Dict, Optional
|
|
4
|
+
|
|
5
|
+
from upgini.autofe.operator import ParametrizedOperator
|
|
6
|
+
from upgini.autofe.timeseries.base import TimeSeriesBase
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Lag(TimeSeriesBase, ParametrizedOperator):
|
|
10
|
+
lag_size: int
|
|
11
|
+
lag_unit: str = "D"
|
|
12
|
+
|
|
13
|
+
def to_formula(self) -> str:
|
|
14
|
+
base_formula = f"lag_{self.lag_size}{self.lag_unit}"
|
|
15
|
+
return self._add_offset_to_formula(base_formula)
|
|
16
|
+
|
|
17
|
+
@classmethod
|
|
18
|
+
def from_formula(cls, formula: str) -> Optional["Lag"]:
|
|
19
|
+
# Base regex for Lag class
|
|
20
|
+
base_regex = r"lag_(\d+)([a-zA-Z])"
|
|
21
|
+
|
|
22
|
+
# Parse offset first
|
|
23
|
+
offset_params, remaining_formula = cls._parse_offset_from_formula(formula, base_regex)
|
|
24
|
+
|
|
25
|
+
if remaining_formula is None:
|
|
26
|
+
return None
|
|
27
|
+
|
|
28
|
+
# Now parse the lag part
|
|
29
|
+
import re
|
|
30
|
+
|
|
31
|
+
match = re.match(f"^{base_regex}$", remaining_formula)
|
|
32
|
+
|
|
33
|
+
if not match:
|
|
34
|
+
return None
|
|
35
|
+
|
|
36
|
+
lag_size = int(match.group(1))
|
|
37
|
+
lag_unit = match.group(2)
|
|
38
|
+
|
|
39
|
+
# Create instance with appropriate parameters
|
|
40
|
+
params = {
|
|
41
|
+
"lag_size": lag_size,
|
|
42
|
+
"lag_unit": lag_unit,
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if offset_params:
|
|
46
|
+
params.update(offset_params)
|
|
47
|
+
|
|
48
|
+
return cls(**params)
|
|
49
|
+
|
|
50
|
+
def get_params(self) -> Dict[str, Optional[str]]:
|
|
51
|
+
res = super().get_params()
|
|
52
|
+
res.update(
|
|
53
|
+
{
|
|
54
|
+
"lag_size": self.lag_size,
|
|
55
|
+
"lag_unit": self.lag_unit,
|
|
56
|
+
}
|
|
57
|
+
)
|
|
58
|
+
return res
|
|
59
|
+
|
|
60
|
+
def _aggregate(self, ts: pd.DataFrame) -> pd.DataFrame:
|
|
61
|
+
lag_window = self.lag_size + 1
|
|
62
|
+
return ts.rolling(f"{lag_window}{self.lag_unit}", min_periods=1).agg(self._lag)
|
|
63
|
+
|
|
64
|
+
def _lag(self, x):
|
|
65
|
+
if x.index.min() > (x.index.max() - pd.Timedelta(self.lag_size, self.lag_unit)):
|
|
66
|
+
return np.nan
|
|
67
|
+
else:
|
|
68
|
+
return x[0]
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import pandas as pd
|
|
2
|
+
from typing import Dict, Optional
|
|
3
|
+
|
|
4
|
+
from upgini.autofe.operator import ParametrizedOperator
|
|
5
|
+
from upgini.autofe.timeseries.base import TimeSeriesBase
|
|
6
|
+
|
|
7
|
+
# Roll aggregation functions
|
|
8
|
+
roll_aggregations = {
|
|
9
|
+
"norm_mean": lambda x: x[-1] / x.mean(),
|
|
10
|
+
"q25": lambda x: x.quantile(0.25),
|
|
11
|
+
"q75": lambda x: x.quantile(0.75),
|
|
12
|
+
"iqr": lambda x: x.quantile(0.75) - x.quantile(0.25),
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
try:
|
|
16
|
+
from pydantic import field_validator as validator # V2
|
|
17
|
+
except ImportError:
|
|
18
|
+
from pydantic import validator # V1
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class Roll(TimeSeriesBase, ParametrizedOperator):
|
|
22
|
+
aggregation: str
|
|
23
|
+
window_size: int = 1
|
|
24
|
+
window_unit: str = "D"
|
|
25
|
+
|
|
26
|
+
@validator("window_unit")
|
|
27
|
+
@classmethod
|
|
28
|
+
def validate_window_unit(cls, v: str) -> str:
|
|
29
|
+
try:
|
|
30
|
+
pd.tseries.frequencies.to_offset(v)
|
|
31
|
+
return v
|
|
32
|
+
except ValueError:
|
|
33
|
+
raise ValueError(
|
|
34
|
+
f"Invalid window_unit: {v}. Must be a valid pandas frequency string (e.g. 'D', 'H', 'T', etc)"
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
def to_formula(self) -> str:
|
|
38
|
+
# First add window size and unit, then add aggregation, then add offset
|
|
39
|
+
base_formula = f"roll_{self.window_size}{self.window_unit}"
|
|
40
|
+
formula_with_agg = f"{base_formula}_{self.aggregation}"
|
|
41
|
+
return self._add_offset_to_formula(formula_with_agg)
|
|
42
|
+
|
|
43
|
+
@classmethod
|
|
44
|
+
def from_formula(cls, formula: str) -> Optional["Roll"]:
|
|
45
|
+
import re
|
|
46
|
+
|
|
47
|
+
# Base regex for Roll class (with aggregation)
|
|
48
|
+
base_regex = r"roll_(\d+)([a-zA-Z])_(\w+)"
|
|
49
|
+
|
|
50
|
+
# Parse offset first - this removes the offset part if present
|
|
51
|
+
offset_params, remaining_formula = cls._parse_offset_from_formula(formula, base_regex)
|
|
52
|
+
|
|
53
|
+
if remaining_formula is None:
|
|
54
|
+
return None
|
|
55
|
+
|
|
56
|
+
# Parse the window part and aggregation
|
|
57
|
+
match = re.match(f"^{base_regex}$", remaining_formula)
|
|
58
|
+
|
|
59
|
+
if not match:
|
|
60
|
+
return None
|
|
61
|
+
|
|
62
|
+
window_size = int(match.group(1))
|
|
63
|
+
window_unit = match.group(2)
|
|
64
|
+
aggregation = match.group(3)
|
|
65
|
+
|
|
66
|
+
# Create instance with appropriate parameters
|
|
67
|
+
params = {
|
|
68
|
+
"window_size": window_size,
|
|
69
|
+
"window_unit": window_unit,
|
|
70
|
+
"aggregation": aggregation,
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if offset_params:
|
|
74
|
+
params.update(offset_params)
|
|
75
|
+
|
|
76
|
+
return cls(**params)
|
|
77
|
+
|
|
78
|
+
def get_params(self) -> Dict[str, Optional[str]]:
|
|
79
|
+
res = super().get_params()
|
|
80
|
+
res.update(
|
|
81
|
+
{
|
|
82
|
+
"window_size": self.window_size,
|
|
83
|
+
"window_unit": self.window_unit,
|
|
84
|
+
"aggregation": self.aggregation,
|
|
85
|
+
}
|
|
86
|
+
)
|
|
87
|
+
return res
|
|
88
|
+
|
|
89
|
+
def _aggregate(self, ts: pd.DataFrame) -> pd.DataFrame:
|
|
90
|
+
return ts.rolling(f"{self.window_size}{self.window_unit}", min_periods=1).agg(
|
|
91
|
+
roll_aggregations.get(self.aggregation, self.aggregation)
|
|
92
|
+
)
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
from typing import Dict, Optional, Union
|
|
2
|
+
import numpy as np
|
|
3
|
+
import pandas as pd
|
|
4
|
+
|
|
5
|
+
from upgini.autofe.timeseries.base import TimeSeriesBase
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class TrendCoefficient(TimeSeriesBase):
|
|
9
|
+
name: str = "trend_coef"
|
|
10
|
+
step_size: int = 1
|
|
11
|
+
step_unit: str = "D"
|
|
12
|
+
|
|
13
|
+
def to_formula(self) -> str:
|
|
14
|
+
base_formula = "trend_coef"
|
|
15
|
+
return self._add_offset_to_formula(base_formula)
|
|
16
|
+
|
|
17
|
+
@classmethod
|
|
18
|
+
def from_formula(cls, formula: str) -> Optional["TrendCoefficient"]:
|
|
19
|
+
# Base regex for TrendCoefficient class
|
|
20
|
+
base_regex = r"trend_coef"
|
|
21
|
+
|
|
22
|
+
# Parse offset first
|
|
23
|
+
offset_params, remaining_formula = cls._parse_offset_from_formula(formula, base_regex)
|
|
24
|
+
|
|
25
|
+
if remaining_formula is None:
|
|
26
|
+
return None
|
|
27
|
+
|
|
28
|
+
# Basic pattern (no offset)
|
|
29
|
+
if remaining_formula == "trend_coef":
|
|
30
|
+
params = {}
|
|
31
|
+
if offset_params:
|
|
32
|
+
params.update(offset_params)
|
|
33
|
+
return cls(**params)
|
|
34
|
+
|
|
35
|
+
return None
|
|
36
|
+
|
|
37
|
+
def get_params(self) -> Dict[str, Optional[str]]:
|
|
38
|
+
res = super().get_params()
|
|
39
|
+
res.update(
|
|
40
|
+
{
|
|
41
|
+
"step_size": self.step_size,
|
|
42
|
+
"step_unit": self.step_unit,
|
|
43
|
+
"offset_size": self.offset_size,
|
|
44
|
+
"offset_unit": self.offset_unit,
|
|
45
|
+
}
|
|
46
|
+
)
|
|
47
|
+
return res
|
|
48
|
+
|
|
49
|
+
def _aggregate(self, ts: pd.DataFrame) -> pd.DataFrame:
|
|
50
|
+
return ts.apply(self._trend_coef).iloc[:, [-1]].fillna(0)
|
|
51
|
+
|
|
52
|
+
def _trend_coef(self, x: Union[pd.DataFrame, pd.Series]) -> Union[pd.DataFrame, pd.Series]:
|
|
53
|
+
return_series = isinstance(x, pd.Series)
|
|
54
|
+
x = pd.DataFrame(x)
|
|
55
|
+
resampled = (
|
|
56
|
+
x.iloc[:, -1].resample(f"{self.step_size}{self.step_unit}").fillna(method="ffill").fillna(method="bfill")
|
|
57
|
+
)
|
|
58
|
+
idx = np.arange(len(resampled))
|
|
59
|
+
coeffs = np.polyfit(idx, resampled, 1)
|
|
60
|
+
x.iloc[:, -1] = coeffs[0]
|
|
61
|
+
return x.iloc[:, -1] if return_series else x
|