dycw-utilities 0.109.14__py3-none-any.whl → 0.109.16__py3-none-any.whl

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dycw-utilities
3
- Version: 0.109.14
3
+ Version: 0.109.16
4
4
  Author-email: Derek Wan <d.wan@icloud.com>
5
5
  License-File: LICENSE
6
6
  Requires-Python: >=3.12
@@ -1,4 +1,4 @@
1
- utilities/__init__.py,sha256=9sl1XiZAUQGAGfCyJEHhTC7Kfmx8TtcYJHtVOZdUugw,61
1
+ utilities/__init__.py,sha256=4AFXsQvJMy-nibpq-XnsnHXbkRajpf73GAF_okhFQgY,61
2
2
  utilities/altair.py,sha256=Gpja-flOo-Db0PIPJLJsgzAlXWoKUjPU1qY-DQ829ek,9156
3
3
  utilities/astor.py,sha256=xuDUkjq0-b6fhtwjhbnebzbqQZAjMSHR1IIS5uOodVg,777
4
4
  utilities/asyncio.py,sha256=41oQUurWMvadFK5gFnaG21hMM0Vmfn2WS6OpC0R9mas,14757
@@ -36,7 +36,7 @@ utilities/math.py,sha256=TexfvLCI12d9Sw5_W4pKVBZ3nRr3zk2iPkcEU7xdEWU,26771
36
36
  utilities/memory_profiler.py,sha256=tf2C51P2lCujPGvRt2Rfc7VEw5LDXmVPCG3z_AvBmbU,962
37
37
  utilities/modules.py,sha256=SnhsRHRUS1po_acejrINauihGQpPvVsp8RDNCei1OLQ,3173
38
38
  utilities/more_itertools.py,sha256=CPUxrMAcTwRxbzbhiqPKi3Xx9hxqI0t6gkWjutaibGk,5534
39
- utilities/numpy.py,sha256=rA1b0_GkBUSMjnv77tinRM70KRnkcmZxI9xbrsXFDRg,21819
39
+ utilities/numpy.py,sha256=-VCZZBUs9GaLxcPOHGVc_iLkVP_SEKKRL7YjV05jrS4,22961
40
40
  utilities/operator.py,sha256=0M2yZJ0PODH47ogFEnkGMBe_cfxwZR02T_92LZVZvHo,3715
41
41
  utilities/optuna.py,sha256=loyJGWTzljgdJaoLhP09PT8Jz6o_pwBOwehY33lHkhw,1923
42
42
  utilities/orjson.py,sha256=Wj5pzG_VdgoAy14a7Luhem-BgYrRtRFvvl_POiszRd0,36930
@@ -47,7 +47,7 @@ utilities/period.py,sha256=ikHXsWtDLr553cfH6p9mMaiCnIAP69B7q84ckWV3HaA,10884
47
47
  utilities/pickle.py,sha256=Bhvd7cZl-zQKQDFjUerqGuSKlHvnW1K2QXeU5UZibtg,657
48
48
  utilities/platform.py,sha256=NU7ycTvAXAG-fdYmDXaM1m4EOml2cGiaYwaUzfzSqyU,1767
49
49
  utilities/polars.py,sha256=aOQNVyV04qYZjg7Exi6zYERhSQoCMzBP74oufxqANFY,52167
50
- utilities/polars_ols.py,sha256=AQe3RFOMv8CEI_ZCoscb_-PxB4JWjO0TAEmk8DKLeaI,2138
50
+ utilities/polars_ols.py,sha256=Uc9V5kvlWZ5cU93lKZ-cfAKdVFFw81tqwLW9PxtUvMs,5618
51
51
  utilities/pqdm.py,sha256=foRytQybmOQ05pjt5LF7ANyzrIa--4ScDE3T2wd31a4,3118
52
52
  utilities/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
53
53
  utilities/pydantic.py,sha256=f6qtR5mO2YMuyvNmbaEj5YeD9eGA4YYfb7Bjzh9jUs0,1845
@@ -86,7 +86,7 @@ utilities/warnings.py,sha256=yUgjnmkCRf6QhdyAXzl7u0qQFejhQG3PrjoSwxpbHrs,1819
86
86
  utilities/whenever.py,sha256=TjoTAJ1R27-rKXiXzdE4GzPidmYqm0W58XydDXp-QZM,17786
87
87
  utilities/zipfile.py,sha256=24lQc9ATcJxHXBPc_tBDiJk48pWyRrlxO2fIsFxU0A8,699
88
88
  utilities/zoneinfo.py,sha256=-DQz5a0Ikw9jfSZtL0BEQkXOMC9yGn_xiJYNCLMiqEc,1989
89
- dycw_utilities-0.109.14.dist-info/METADATA,sha256=sjPmXifZcBMY4js2lgCXzweOgdeXLZ1qihJdZ8lFsTA,13005
90
- dycw_utilities-0.109.14.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
91
- dycw_utilities-0.109.14.dist-info/licenses/LICENSE,sha256=gppZp16M6nSVpBbUBrNL6JuYfvKwZiKgV7XoKKsHzqo,1066
92
- dycw_utilities-0.109.14.dist-info/RECORD,,
89
+ dycw_utilities-0.109.16.dist-info/METADATA,sha256=u1Fl275S55bRfV6fTrMT8qHsS8_bu4aispHkIR6QNz8,13005
90
+ dycw_utilities-0.109.16.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
91
+ dycw_utilities-0.109.16.dist-info/licenses/LICENSE,sha256=gppZp16M6nSVpBbUBrNL6JuYfvKwZiKgV7XoKKsHzqo,1066
92
+ dycw_utilities-0.109.16.dist-info/RECORD,,
utilities/__init__.py CHANGED
@@ -1,3 +1,3 @@
1
1
  from __future__ import annotations
2
2
 
3
- __version__ = "0.109.14"
3
+ __version__ = "0.109.16"
utilities/numpy.py CHANGED
@@ -1,19 +1,22 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  from dataclasses import dataclass
4
- from functools import reduce
4
+ from functools import partial, reduce
5
5
  from itertools import repeat
6
6
  from typing import TYPE_CHECKING, Any, overload, override
7
7
 
8
8
  import numpy as np
9
9
  from numpy import (
10
+ argsort,
10
11
  array,
11
12
  bool_,
13
+ complex128,
12
14
  digitize,
13
15
  dtype,
14
16
  errstate,
15
17
  flatnonzero,
16
18
  float64,
19
+ floating,
17
20
  full_like,
18
21
  inf,
19
22
  int64,
@@ -30,6 +33,7 @@ from numpy import (
30
33
  roll,
31
34
  where,
32
35
  )
36
+ from numpy.fft import fft, fftfreq, ifft
33
37
  from numpy.linalg import det, eig
34
38
  from numpy.random import default_rng
35
39
  from numpy.typing import NDArray
@@ -37,7 +41,7 @@ from numpy.typing import NDArray
37
41
  from utilities.iterables import is_iterable_not_str
38
42
 
39
43
  if TYPE_CHECKING:
40
- from collections.abc import Iterable
44
+ from collections.abc import Callable, Iterable
41
45
 
42
46
 
43
47
  ##
@@ -160,6 +164,33 @@ def fillna(array: NDArrayF, /, *, value: float = 0.0) -> NDArrayF:
160
164
  ##
161
165
 
162
166
 
167
+ def filter_frequencies(
168
+ array: NDArrayF,
169
+ /,
170
+ *filters: Callable[[NDArray[floating[Any]]], NDArrayB],
171
+ d: int = 1,
172
+ ) -> NDArrayF:
173
+ """Filter an array by the frequencies of its FFT."""
174
+ (n,) = array.shape
175
+ fft_vals = fft(array)
176
+ freqs = fftfreq(n, d=d)
177
+ reduced = reduce(partial(_filter_frequencies_one, freqs=freqs), filters, fft_vals)
178
+ return ifft(reduced).real
179
+
180
+
181
+ def _filter_frequencies_one(
182
+ acc: NDArray[complex128],
183
+ el: Callable[[NDArray[floating[Any]]], NDArrayB],
184
+ /,
185
+ *,
186
+ freqs: NDArray[floating[Any]],
187
+ ) -> NDArray[complex128]:
188
+ return where(el(freqs), acc, 0.0)
189
+
190
+
191
+ ##
192
+
193
+
163
194
  def flatn0(array: NDArrayB, /) -> int:
164
195
  """Return the index of the unique True element."""
165
196
  if not array.any():
@@ -193,6 +224,19 @@ class FlatN0MultipleError(FlatN0Error):
193
224
  ##
194
225
 
195
226
 
227
+ def get_frequency_spectrum(array: NDArrayF, /, *, d: int = 1) -> NDArray[floating[Any]]:
228
+ """Get the frequency spectrum."""
229
+ (n,) = array.shape
230
+ fft_vals = fft(array)
231
+ freqs = fftfreq(n, d=d)
232
+ amplitudes = np.abs(fft_vals)
233
+ data = np.hstack([freqs.reshape(-1, 1), amplitudes.reshape(-1, 1)])
234
+ return data[argsort(data[:, 0])]
235
+
236
+
237
+ ##
238
+
239
+
196
240
  def has_dtype(x: Any, dtype: Any, /) -> bool:
197
241
  """Check if an object has the required dtype."""
198
242
  if is_iterable_not_str(dtype):
@@ -851,7 +895,9 @@ __all__ = [
851
895
  "datetime64us",
852
896
  "discretize",
853
897
  "fillna",
898
+ "filter_frequencies",
854
899
  "flatn0",
900
+ "get_frequency_spectrum",
855
901
  "has_dtype",
856
902
  "is_at_least",
857
903
  "is_at_least_or_nan",
utilities/polars_ols.py CHANGED
@@ -1,19 +1,22 @@
1
1
  from __future__ import annotations
2
2
 
3
- from typing import TYPE_CHECKING
3
+ from typing import TYPE_CHECKING, overload
4
4
 
5
- from polars import struct
5
+ from polars import Expr, Series, struct
6
6
  from polars_ols import RollingKwargs, compute_rolling_least_squares
7
7
 
8
- from utilities.polars import ensure_expr_or_series
8
+ from utilities.errors import ImpossibleCaseError
9
+ from utilities.functions import is_sequence_of
10
+ from utilities.polars import concat_series, ensure_expr_or_series
9
11
 
10
12
  if TYPE_CHECKING:
11
- from polars import Expr
13
+ from polars._typing import IntoExprColumn
12
14
  from polars_ols import NullPolicy
13
15
 
14
16
  from utilities.polars import ExprLike
15
17
 
16
18
 
19
+ @overload
17
20
  def compute_rolling_ols(
18
21
  target: ExprLike,
19
22
  *features: ExprLike,
@@ -24,9 +27,106 @@ def compute_rolling_ols(
24
27
  min_periods: int | None = None,
25
28
  use_woodbury: bool | None = None,
26
29
  alpha: float | None = None,
30
+ ) -> Expr: ...
31
+ @overload
32
+ def compute_rolling_ols(
33
+ target: Series,
34
+ *features: Series,
35
+ sample_weights: Series | None = None,
36
+ add_intercept: bool = False,
37
+ null_policy: NullPolicy = "drop_window",
38
+ window_size: int = 1000000,
39
+ min_periods: int | None = None,
40
+ use_woodbury: bool | None = None,
41
+ alpha: float | None = None,
42
+ ) -> Series: ...
43
+ @overload
44
+ def compute_rolling_ols(
45
+ target: IntoExprColumn,
46
+ *features: IntoExprColumn,
47
+ sample_weights: IntoExprColumn | None = None,
48
+ add_intercept: bool = False,
49
+ null_policy: NullPolicy = "drop_window",
50
+ window_size: int = 1000000,
51
+ min_periods: int | None = None,
52
+ use_woodbury: bool | None = None,
53
+ alpha: float | None = None,
54
+ ) -> Expr | Series: ...
55
+ def compute_rolling_ols(
56
+ target: IntoExprColumn,
57
+ *features: IntoExprColumn,
58
+ sample_weights: IntoExprColumn | None = None,
59
+ add_intercept: bool = False,
60
+ null_policy: NullPolicy = "drop_window",
61
+ window_size: int = 1000000,
62
+ min_periods: int | None = None,
63
+ use_woodbury: bool | None = None,
64
+ alpha: float | None = None,
65
+ ) -> Expr | Series:
66
+ """Compute a rolling OLS."""
67
+ target = ensure_expr_or_series(target)
68
+ features2 = tuple(map(ensure_expr_or_series, features))
69
+ sample_weights = (
70
+ None if sample_weights is None else ensure_expr_or_series(sample_weights)
71
+ )
72
+ if (
73
+ isinstance(target, Expr)
74
+ and is_sequence_of(features2, Expr)
75
+ and ((sample_weights is None) or isinstance(sample_weights, Expr))
76
+ ):
77
+ return _compute_rolling_ols_expr(
78
+ target,
79
+ *features2,
80
+ sample_weights=sample_weights,
81
+ add_intercept=add_intercept,
82
+ null_policy=null_policy,
83
+ window_size=window_size,
84
+ min_periods=min_periods,
85
+ use_woodbury=use_woodbury,
86
+ alpha=alpha,
87
+ )
88
+ if (
89
+ isinstance(target, Series)
90
+ and is_sequence_of(features2, Series)
91
+ and ((sample_weights is None) or isinstance(sample_weights, Series))
92
+ ):
93
+ return concat_series(
94
+ target, *features2, *([] if sample_weights is None else [sample_weights])
95
+ ).with_columns(
96
+ _compute_rolling_ols_expr(
97
+ target.name,
98
+ *(f.name for f in features2),
99
+ sample_weights=None if sample_weights is None else sample_weights.name,
100
+ add_intercept=add_intercept,
101
+ null_policy=null_policy,
102
+ window_size=window_size,
103
+ min_periods=min_periods,
104
+ use_woodbury=use_woodbury,
105
+ alpha=alpha,
106
+ )
107
+ )["ols"]
108
+ raise ImpossibleCaseError( # pragma: no cover
109
+ case=[f"{target=}", f"{features2=}", f"{sample_weights=}"]
110
+ )
111
+
112
+
113
+ def _compute_rolling_ols_expr(
114
+ target: ExprLike,
115
+ *features: ExprLike,
116
+ sample_weights: ExprLike | None = None,
117
+ add_intercept: bool = False,
118
+ null_policy: NullPolicy = "drop_window",
119
+ window_size: int = 1000000,
120
+ min_periods: int | None = None,
121
+ use_woodbury: bool | None = None,
122
+ alpha: float | None = None,
27
123
  ) -> Expr:
28
124
  """Compute a rolling OLS."""
29
125
  target = ensure_expr_or_series(target)
126
+ features2 = tuple(map(ensure_expr_or_series, features))
127
+ sample_weights = (
128
+ None if sample_weights is None else ensure_expr_or_series(sample_weights)
129
+ )
30
130
  rolling_kwargs = RollingKwargs(
31
131
  null_policy=null_policy,
32
132
  window_size=window_size,
@@ -36,7 +136,7 @@ def compute_rolling_ols(
36
136
  )
37
137
  coefficients = compute_rolling_least_squares(
38
138
  target,
39
- *features,
139
+ *features2,
40
140
  sample_weights=sample_weights,
41
141
  add_intercept=add_intercept,
42
142
  mode="coefficients",
@@ -44,7 +144,7 @@ def compute_rolling_ols(
44
144
  ).alias("coefficients")
45
145
  predictions = compute_rolling_least_squares(
46
146
  target,
47
- *features,
147
+ *features2,
48
148
  sample_weights=sample_weights,
49
149
  add_intercept=add_intercept,
50
150
  mode="predictions",
@@ -52,7 +152,7 @@ def compute_rolling_ols(
52
152
  ).alias("predictions")
53
153
  residuals = compute_rolling_least_squares(
54
154
  target,
55
- *features,
155
+ *features2,
56
156
  sample_weights=sample_weights,
57
157
  add_intercept=add_intercept,
58
158
  mode="residuals",