dycw-utilities 0.109.15__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.15
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=qZyUzwQ2zZu6ZkKszEeJGs10nbVWvqm_izEvqG8MgXM,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
@@ -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.15.dist-info/METADATA,sha256=sqk1b4cCCM8V737VBkLkTToWlAJ5Mb7Co4LmlynJ4Tg,13005
90
- dycw_utilities-0.109.15.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
91
- dycw_utilities-0.109.15.dist-info/licenses/LICENSE,sha256=gppZp16M6nSVpBbUBrNL6JuYfvKwZiKgV7XoKKsHzqo,1066
92
- dycw_utilities-0.109.15.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.15"
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",