Skip to content

Commit a805109

Browse files
authored
fix: typing_extensions only with python < 3.10 (#134)
1 parent c0e6cb3 commit a805109

File tree

16 files changed

+64
-58
lines changed

16 files changed

+64
-58
lines changed

docs/code/feature_engineering.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
1-
import polars as pl
21
import numpy as np
3-
from functime.feature_extractors import FeatureExtractor, binned_entropy
2+
import polars as pl
3+
4+
from functime.feature_extractors import binned_entropy
45

56
# Load commodities price data
6-
y = pl.read_parquet("https://github.com/TracecatHQ/functime/raw/main/data/commodities.parquet")
7+
y = pl.read_parquet(
8+
"https://github.com/TracecatHQ/functime/raw/main/data/commodities.parquet"
9+
)
710

811
# Get column names ("commodity_type", "time", "price")
912
entity_col, time_col, value_col = y.columns
1013

1114
# Extract a single feature from a single time-series
1215
binned_entropy = binned_entropy(
13-
pl.Series(np.random.normal(0, 1, size=10)),
14-
bin_count=10
16+
pl.Series(np.random.normal(0, 1, size=10)), bin_count=10
1517
)
1618

1719
# 🔥 Also works on LazyFrames with query optimization
1820
features = (
19-
pl.LazyFrame({
20-
"index": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
21-
"value": np.random.normal(0, 1, size=10)
22-
})
21+
pl.LazyFrame(
22+
{
23+
"index": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
24+
"value": np.random.normal(0, 1, size=10),
25+
}
26+
)
2327
.select(
2428
binned_entropy=pl.col("value").ts.binned_entropy(bin_count=10),
2529
lempel_ziv_complexity=pl.col("value").ts.lempel_ziv_complexity(threshold=3),
@@ -30,13 +34,10 @@
3034

3135
# 🚄 Extract features blazingly fast on many
3236
# stacked time-series using `group_by`
33-
features = (
34-
y.group_by(entity_col)
35-
.agg(
36-
binned_entropy=pl.col(value_col).ts.binned_entropy(bin_count=10),
37-
lempel_ziv_complexity=pl.col(value_col).ts.lempel_ziv_complexity(threshold=3),
38-
longest_streak_above_mean=pl.col(value_col).ts.longest_streak_above_mean(),
39-
)
37+
features = y.group_by(entity_col).agg(
38+
binned_entropy=pl.col(value_col).ts.binned_entropy(bin_count=10),
39+
lempel_ziv_complexity=pl.col(value_col).ts.lempel_ziv_complexity(threshold=3),
40+
longest_streak_above_mean=pl.col(value_col).ts.longest_streak_above_mean(),
4041
)
4142

4243
# 🚄 Extract features blazingly fast on windows
@@ -47,8 +48,7 @@
4748
time_col,
4849
every="12mo",
4950
by=entity_col,
50-
)
51-
.agg(
51+
).agg(
5252
binned_entropy=pl.col(value_col).ts.binned_entropy(bin_count=10),
5353
lempel_ziv_complexity=pl.col(value_col).ts.lempel_ziv_complexity(threshold=3),
5454
longest_streak_above_mean=pl.col(value_col).ts.longest_streak_above_mean(),

docs/notebooks/benchmarks.ipynb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@
3030
"outputs": [],
3131
"source": [
3232
"from typing import Callable\n",
33+
"\n",
3334
"import pandas as pd\n",
3435
"import perfplot\n",
3536
"import polars as pl\n",
3637
"from tsfresh.feature_extraction import feature_calculators as tsfresh\n",
38+
"\n",
3739
"from functime import feature_extractors as fe"
3840
]
3941
},
@@ -104,7 +106,12 @@
104106
" (fe.absolute_energy, tsfresh.abs_energy, {}, {}),\n",
105107
" (fe.absolute_maximum, tsfresh.absolute_maximum, {}, {}),\n",
106108
" (fe.absolute_sum_of_changes, tsfresh.absolute_sum_of_changes, {}, {}),\n",
107-
" (fe.lempel_ziv_complexity, tsfresh.lempel_ziv_complexity, {\"threshold\": (pl.col(\"value\").max() - pl.col(\"value\").min()) / 2}, {\"bins\": 2}),\n",
109+
" (\n",
110+
" fe.lempel_ziv_complexity,\n",
111+
" tsfresh.lempel_ziv_complexity,\n",
112+
" {\"threshold\": (pl.col(\"value\").max() - pl.col(\"value\").min()) / 2},\n",
113+
" {\"bins\": 2},\n",
114+
" ),\n",
108115
" (\n",
109116
" fe.approximate_entropy,\n",
110117
" tsfresh.approximate_entropy,\n",
@@ -279,7 +286,11 @@
279286
"):\n",
280287
" if f_feat.__name__ == \"approximate_entropy\":\n",
281288
" n_range = [10_000]\n",
282-
" elif f_feat.__name__ in (\"number_cwt_peaks\", \"sample_entropy\", \"lempel_ziv_complexity\"):\n",
289+
" elif f_feat.__name__ in (\n",
290+
" \"number_cwt_peaks\",\n",
291+
" \"sample_entropy\",\n",
292+
" \"lempel_ziv_complexity\",\n",
293+
" ):\n",
283294
" n_range = [10_000, 100_000]\n",
284295
" else:\n",
285296
" n_range = [10_000, 100_000, 1_000_000, 9_000_000]\n",

functime/base/forecaster.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import logging
2+
import sys
23
from dataclasses import dataclass
3-
from typing import Callable, List, Mapping, Optional, Tuple, TypeVar, Union
4+
from typing import Callable, List, Literal, Mapping, Optional, Tuple, TypeVar, Union
45

56
import polars as pl
6-
from typing_extensions import Literal, ParamSpec
77

88
from functime.base.model import Model, ModelState
99
from functime.base.transformer import Transformer
1010
from functime.ranges import make_future_ranges
1111

12+
if sys.version_info < (3, 10):
13+
from typing_extensions import ParamSpec
14+
else:
15+
from typing import ParamSpec
16+
1217
# The parameters of the Model
1318
P = ParamSpec("P")
1419
# The return type of the esimator's curried function

functime/base/transformer.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import inspect
2+
import sys
23
from functools import cached_property, wraps
34
from typing import Callable, Tuple, TypeVar, Union
45

56
import polars as pl
6-
from typing_extensions import ParamSpec
77

88
from functime.base.model import ModelState
99

10+
if sys.version_info < (3, 10):
11+
from typing_extensions import ParamSpec
12+
else:
13+
from typing import ParamSpec
14+
15+
1016
P = ParamSpec("P") # The parameters of the Model
1117
R = TypeVar("R")
1218

functime/evaluation.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
from functools import partial
2-
from typing import List, Optional
2+
from typing import List, Literal, Optional
33

44
import polars as pl
55
import polars.selectors as cs
66
from scipy.stats import norm, normaltest
7-
from typing_extensions import Literal
87

98
from functime.base.metric import METRIC_TYPE
109
from functime.metrics import (

functime/forecasting/_ar.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import logging
2-
from typing import Any, Callable, List, Mapping, Optional, Union
2+
from typing import Any, Callable, List, Literal, Mapping, Optional, Union
33

44
import numpy as np
55
import polars as pl
66
from tqdm import tqdm, trange
7-
from typing_extensions import Literal
87

98
from functime.cross_validation import expanding_window_split
109
from functime.forecasting._evaluate import evaluate

functime/forecasting/_regressors.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
Fit-predict regressors with special needs.
33
"""
44

5-
from typing import Callable, Optional, Union
5+
from typing import Callable, Literal, Optional, Union
66

77
import numpy as np
88
import polars as pl
99
import sklearn
10-
from typing_extensions import Literal
1110

1211
from functime.conversion import X_to_numpy, y_to_numpy
1312
from functime.preprocessing import PL_NUMERIC_COLS

functime/forecasting/automl.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
from abc import abstractmethod
22
from functools import partial
3-
from typing import Any, Mapping, Optional, Union
3+
from typing import Any, Literal, Mapping, Optional, Union
44

55
import polars as pl
66
from flaml import tune
7-
from typing_extensions import Literal
87

98
from functime.base.forecaster import FORECAST_STRATEGIES, SUPPORTED_FREQ, Forecaster
109
from functime.base.transformer import Transformer

functime/forecasting/elite.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
from functools import partial
2-
from typing import Any, Mapping, Optional, Union
2+
from typing import Any, Literal, Mapping, Optional, Union
33

44
import polars as pl
55
import polars.selectors as cs
66
from sklearn.linear_model import LassoLarsIC
77
from tqdm import tqdm
8-
from typing_extensions import Literal
98

109
from functime.backtesting import backtest
1110
from functime.base.forecaster import Forecaster

functime/forecasting/lance.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
from typing import Optional
1+
from typing import Literal, Optional
22

33
import lance
44
import numpy as np
55
import polars as pl
66
from tqdm import tqdm
7-
from typing_extensions import Literal
87

98
from functime.base import Forecaster
109
from functime.forecasting._ar import fit_autoreg

0 commit comments

Comments
 (0)