Modal-Decomposition 0.2.0__cp310-cp310-win_amd64.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.
- Modal_Decomposition/Base/Cache.py +420 -0
- Modal_Decomposition/Base/ClassDefine.py +129 -0
- Modal_Decomposition/Base/ConfigDefine.py +22 -0
- Modal_Decomposition/Base/ConstDefine.py +19 -0
- Modal_Decomposition/Base/PathDefine.py +11 -0
- Modal_Decomposition/Base/TextDefine.py +43 -0
- Modal_Decomposition/Base/__init__.py +21 -0
- Modal_Decomposition/CEEFD.py +161 -0
- Modal_Decomposition/CEEMD.py +149 -0
- Modal_Decomposition/CEEMDAN.py +150 -0
- Modal_Decomposition/EEMD.py +117 -0
- Modal_Decomposition/EFD.py +144 -0
- Modal_Decomposition/EMD.py +102 -0
- Modal_Decomposition/EMD_new.py +267 -0
- Modal_Decomposition/EWT.py +110 -0
- Modal_Decomposition/Error/RealizationError.py +12 -0
- Modal_Decomposition/Error/__init__.py +7 -0
- Modal_Decomposition/FMD.py +418 -0
- Modal_Decomposition/ICEEMDAN.py +253 -0
- Modal_Decomposition/LMD.py +411 -0
- Modal_Decomposition/MEMD.py +285 -0
- Modal_Decomposition/RPSEMD.py +147 -0
- Modal_Decomposition/SSA.py +238 -0
- Modal_Decomposition/SVMD.py +209 -0
- Modal_Decomposition/Utils/Check.py +346 -0
- Modal_Decomposition/Utils/Chunk.py +488 -0
- Modal_Decomposition/Utils/Envelope.py +148 -0
- Modal_Decomposition/Utils/Hilbert.py +42 -0
- Modal_Decomposition/Utils/Memory.py +160 -0
- Modal_Decomposition/Utils/Monotonicity.py +407 -0
- Modal_Decomposition/Utils/Peaks.py +220 -0
- Modal_Decomposition/Utils/Seed.py +104 -0
- Modal_Decomposition/Utils/Spline.py +382 -0
- Modal_Decomposition/Utils/_Hilbert/_C/_fht/am_sysdep.c +112 -0
- Modal_Decomposition/Utils/_Hilbert/_C/_fht/am_sysdep.h +54 -0
- Modal_Decomposition/Utils/_Hilbert/_C/_fht/math_const.h +35 -0
- Modal_Decomposition/Utils/_Hilbert/_C/_fht/transforms.c +1406 -0
- Modal_Decomposition/Utils/_Hilbert/_C/_fht/transforms.h +38 -0
- Modal_Decomposition/Utils/_Hilbert/__init__.py +101 -0
- Modal_Decomposition/Utils/_Hilbert/_fft.py +0 -0
- Modal_Decomposition/Utils/_Hilbert/_fht.py +191 -0
- Modal_Decomposition/Utils/_Hilbert/_fht_native.cp310-win_amd64.pyd +0 -0
- Modal_Decomposition/Utils/_Hilbert/_fht_native.pyx +103 -0
- Modal_Decomposition/Utils/_Hilbert/_fir.py +0 -0
- Modal_Decomposition/Utils/_Hilbert/_forth_order_wave.py +0 -0
- Modal_Decomposition/Utils/_Hilbert/_kramers_kronig.py +0 -0
- Modal_Decomposition/Utils/_Hilbert/_scipy_backend.py +6 -0
- Modal_Decomposition/Utils/_Hilbert/_wave.py +0 -0
- Modal_Decomposition/Utils/__init__.py +199 -0
- Modal_Decomposition/VMD.py +99 -0
- Modal_Decomposition/_Registry.py +39 -0
- Modal_Decomposition/_SVMD_numba.py +17 -0
- Modal_Decomposition/__init__.py +154 -0
- modal_decomposition-0.2.0.dist-info/LICENSE +202 -0
- modal_decomposition-0.2.0.dist-info/METADATA +267 -0
- modal_decomposition-0.2.0.dist-info/RECORD +58 -0
- modal_decomposition-0.2.0.dist-info/WHEEL +5 -0
- modal_decomposition-0.2.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
"""
|
|
2
|
+
store of the import cache
|
|
3
|
+
|
|
4
|
+
Process-wide, thread-safe registry for imported third-party submodules
|
|
5
|
+
(e.g. ``scipy.signal`` / ``scipy.interpolate``). A module is imported and
|
|
6
|
+
registered at most once per process; afterwards every consumer fetches it
|
|
7
|
+
through :meth:`cache.get`.
|
|
8
|
+
|
|
9
|
+
Uniqueness contract
|
|
10
|
+
-------------------
|
|
11
|
+
- Keys are **case-sensitive, non-empty strings** (module-style names such as
|
|
12
|
+
``"scipy.signal"`` are recommended). ``""``, whitespace-only names and
|
|
13
|
+
non-string names are rejected up front.
|
|
14
|
+
- :meth:`cache.add` **never overwrites**: registering an already-known name
|
|
15
|
+
returns ``False`` and keeps the original api. Replacement is only possible
|
|
16
|
+
via an explicit :meth:`cache.remove` followed by a new :meth:`cache.add`.
|
|
17
|
+
- The backing store is shared by the class and by every instance, so the
|
|
18
|
+
uniqueness guarantee holds process-wide no matter who registers.
|
|
19
|
+
- Every registered entry carries a description string (see
|
|
20
|
+
:meth:`cache.describe`); the description is documented in a comment next to
|
|
21
|
+
each ``cache.add(...)`` call site.
|
|
22
|
+
|
|
23
|
+
Thread safety
|
|
24
|
+
-------------
|
|
25
|
+
All mutating and reading operations are guarded by a single re-entrant lock,
|
|
26
|
+
so concurrent registration of distinct names is safe and concurrent
|
|
27
|
+
registration of the *same* name succeeds exactly once.
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
import functools
|
|
31
|
+
import threading
|
|
32
|
+
import types
|
|
33
|
+
from typing import Any, Dict, List, Optional, Tuple
|
|
34
|
+
|
|
35
|
+
__all__ = ["cache", "Cache"]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class cache:
|
|
39
|
+
"""
|
|
40
|
+
Import cache: unique-name registry of imported modules.
|
|
41
|
+
|
|
42
|
+
The public API is implemented with classmethods, so both spellings are
|
|
43
|
+
equivalent and share one process-wide store::
|
|
44
|
+
|
|
45
|
+
cache.add("scipy.signal", scipy.signal) # class-level call
|
|
46
|
+
cache().add("scipy.signal", scipy.signal) # instance-level call
|
|
47
|
+
|
|
48
|
+
Examples
|
|
49
|
+
--------
|
|
50
|
+
>>> from Modal_Decomposition.Base.Cache import cache
|
|
51
|
+
>>> import scipy.signal as ss
|
|
52
|
+
>>> cache.add("scipy.signal", ss, description="signal processing submodule")
|
|
53
|
+
True
|
|
54
|
+
>>> cache.check("scipy.signal")
|
|
55
|
+
True
|
|
56
|
+
>>> cache.get("scipy.signal") is ss
|
|
57
|
+
True
|
|
58
|
+
>>> cache.describe("scipy.signal")
|
|
59
|
+
'signal processing submodule'
|
|
60
|
+
>>> cache.add("scipy.signal", ss) # duplicate: never overwrites
|
|
61
|
+
False
|
|
62
|
+
"""
|
|
63
|
+
|
|
64
|
+
# --- process-wide shared state (class attributes; instances share it) ---
|
|
65
|
+
_entries: Dict[str, Any] = {}
|
|
66
|
+
_descriptions: Dict[str, str] = {}
|
|
67
|
+
_lock = threading.RLock()
|
|
68
|
+
|
|
69
|
+
# ------------------------------------------------------------------ #
|
|
70
|
+
# helpers
|
|
71
|
+
# ------------------------------------------------------------------ #
|
|
72
|
+
@staticmethod
|
|
73
|
+
def _validate_name(name) -> str:
|
|
74
|
+
"""
|
|
75
|
+
Validate a registry key.
|
|
76
|
+
|
|
77
|
+
Returns the name unchanged (keys are kept verbatim: exact string,
|
|
78
|
+
case-sensitive) after rejecting the extreme cases.
|
|
79
|
+
|
|
80
|
+
Raises
|
|
81
|
+
------
|
|
82
|
+
TypeError
|
|
83
|
+
On a non-string name (None, int, bytes, ...).
|
|
84
|
+
ValueError
|
|
85
|
+
On an empty or whitespace-only name.
|
|
86
|
+
"""
|
|
87
|
+
if not isinstance(name, str):
|
|
88
|
+
raise TypeError(
|
|
89
|
+
f"cache name must be a str, got {type(name).__name__}"
|
|
90
|
+
)
|
|
91
|
+
if not name.strip():
|
|
92
|
+
raise ValueError("cache name must not be empty or whitespace")
|
|
93
|
+
return name
|
|
94
|
+
|
|
95
|
+
@staticmethod
|
|
96
|
+
def _validate_api(api) -> None:
|
|
97
|
+
"""Reject None apis: caching None is ambiguous with a missing entry."""
|
|
98
|
+
if api is None:
|
|
99
|
+
raise TypeError("cache api must not be None")
|
|
100
|
+
|
|
101
|
+
@staticmethod
|
|
102
|
+
def _validate_description(description: Optional[str]) -> Optional[str]:
|
|
103
|
+
"""Description must be a str (or None); empty/whitespace means None."""
|
|
104
|
+
if description is None:
|
|
105
|
+
return None
|
|
106
|
+
if not isinstance(description, str):
|
|
107
|
+
raise TypeError(
|
|
108
|
+
f"cache description must be a str or None, "
|
|
109
|
+
f"got {type(description).__name__}"
|
|
110
|
+
)
|
|
111
|
+
stripped = description.strip()
|
|
112
|
+
return stripped if stripped else None
|
|
113
|
+
|
|
114
|
+
@staticmethod
|
|
115
|
+
def _api_label(api) -> str:
|
|
116
|
+
"""Human-readable label of a cached api (module name or type name)."""
|
|
117
|
+
if isinstance(api, types.ModuleType):
|
|
118
|
+
return getattr(api, "__name__", str(api))
|
|
119
|
+
return f"{type(api).__module__}.{type(api).__qualname__}"
|
|
120
|
+
|
|
121
|
+
# ------------------------------------------------------------------ #
|
|
122
|
+
# public API (classmethods: usable as ``cache.add(...)`` and
|
|
123
|
+
# ``cache().add(...)`` alike)
|
|
124
|
+
# ------------------------------------------------------------------ #
|
|
125
|
+
@classmethod
|
|
126
|
+
def add(
|
|
127
|
+
cls,
|
|
128
|
+
name,
|
|
129
|
+
api,
|
|
130
|
+
verbose: bool = False,
|
|
131
|
+
description: Optional[str] = None,
|
|
132
|
+
) -> bool:
|
|
133
|
+
"""
|
|
134
|
+
add the api of the module or function or class to the cache
|
|
135
|
+
|
|
136
|
+
Parameters
|
|
137
|
+
----------
|
|
138
|
+
name : str
|
|
139
|
+
Unique registry key (case-sensitive, non-empty).
|
|
140
|
+
api : object
|
|
141
|
+
The imported module (or any object) to cache; must not be None.
|
|
142
|
+
verbose : bool
|
|
143
|
+
When True, print a line for every registration and for every
|
|
144
|
+
rejected duplicate.
|
|
145
|
+
description : str, optional
|
|
146
|
+
One-line description of the registered module. Retrieved later
|
|
147
|
+
with :meth:`cache.describe`. The same text is documented in a
|
|
148
|
+
comment next to this ``add`` call. When omitted, ``describe``
|
|
149
|
+
falls back to an auto-generated notice.
|
|
150
|
+
|
|
151
|
+
Returns
|
|
152
|
+
-------
|
|
153
|
+
bool
|
|
154
|
+
True when the entry was registered; False when ``name`` was
|
|
155
|
+
already registered (the existing api is kept untouched).
|
|
156
|
+
"""
|
|
157
|
+
name = cls._validate_name(name)
|
|
158
|
+
cls._validate_api(api)
|
|
159
|
+
description = cls._validate_description(description)
|
|
160
|
+
|
|
161
|
+
with cls._lock:
|
|
162
|
+
if name in cls._entries:
|
|
163
|
+
if verbose:
|
|
164
|
+
print(
|
|
165
|
+
f"[cache] '{name}' is already registered; "
|
|
166
|
+
f"duplicate registration ignored"
|
|
167
|
+
)
|
|
168
|
+
return False
|
|
169
|
+
|
|
170
|
+
cls._entries[name] = api
|
|
171
|
+
cls._descriptions[name] = description if description is not None \
|
|
172
|
+
else f"<no description provided at registration of '{name}'>"
|
|
173
|
+
|
|
174
|
+
if verbose:
|
|
175
|
+
print(f"[cache] registered '{name}' ({cls._api_label(api)})")
|
|
176
|
+
return True
|
|
177
|
+
|
|
178
|
+
@classmethod
|
|
179
|
+
def import_module(
|
|
180
|
+
cls,
|
|
181
|
+
name: str,
|
|
182
|
+
description: Optional[str] = None,
|
|
183
|
+
verbose: bool = False,
|
|
184
|
+
):
|
|
185
|
+
"""
|
|
186
|
+
抽象并集中管理经典惰性导入样板::
|
|
187
|
+
|
|
188
|
+
if <flag> is None:
|
|
189
|
+
try:
|
|
190
|
+
import <name> as <flag>
|
|
191
|
+
except ImportError:
|
|
192
|
+
raise ImportError(...)
|
|
193
|
+
|
|
194
|
+
- 首次调用: import 目标模块, 以模块名 ``name`` 注册进统一缓存
|
|
195
|
+
(持 ``RLock``, 恰好执行一次);
|
|
196
|
+
- 之后调用: 直接返回缓存实例, 不再执行 import;
|
|
197
|
+
- ``ImportError`` 原样传播且**不缓存** (标志保持"未就绪", 下次调用
|
|
198
|
+
自动重试) —— try/except 只存在于这一处, 调用方零样板;
|
|
199
|
+
- 键已存在时 (可能由另一条 import 路径先注册) 直接返回既有实例,
|
|
200
|
+
维持"永不覆盖"契约。
|
|
201
|
+
|
|
202
|
+
Parameters
|
|
203
|
+
----------
|
|
204
|
+
name : str
|
|
205
|
+
Module name to import, doubling as the registry key
|
|
206
|
+
(sys.modules style, case-sensitive, non-empty).
|
|
207
|
+
description : str, optional
|
|
208
|
+
One-line description stored for :meth:`cache.describe`.
|
|
209
|
+
verbose : bool
|
|
210
|
+
Print a line when the entry is newly registered.
|
|
211
|
+
|
|
212
|
+
Returns
|
|
213
|
+
-------
|
|
214
|
+
module
|
|
215
|
+
The imported (and registered) module instance.
|
|
216
|
+
"""
|
|
217
|
+
name = cls._validate_name(name)
|
|
218
|
+
description = cls._validate_description(description)
|
|
219
|
+
|
|
220
|
+
with cls._lock:
|
|
221
|
+
if name in cls._entries:
|
|
222
|
+
return cls._entries[name]
|
|
223
|
+
|
|
224
|
+
from importlib import import_module as _import_module
|
|
225
|
+
|
|
226
|
+
api = _import_module(name) # ImportError propagates, nothing cached
|
|
227
|
+
cls._validate_api(api)
|
|
228
|
+
cls._entries[name] = api
|
|
229
|
+
cls._descriptions[name] = (
|
|
230
|
+
description if description is not None
|
|
231
|
+
else f"<imported module '{name}'>"
|
|
232
|
+
)
|
|
233
|
+
|
|
234
|
+
if verbose:
|
|
235
|
+
print(f"[cache] registered '{name}' ({cls._api_label(api)})")
|
|
236
|
+
return api
|
|
237
|
+
|
|
238
|
+
@classmethod
|
|
239
|
+
def lazy(cls, name: str, description: Optional[str] = None, verbose: bool = False):
|
|
240
|
+
"""
|
|
241
|
+
装饰器: 把零参加载函数变成"进程内恰好执行一次"的惰性取用器。
|
|
242
|
+
|
|
243
|
+
对调用方完全透明 (``functools.wraps`` 保留签名与 docstring), 缓存
|
|
244
|
+
藏在背后: 调用者只看到一个普通函数调用。
|
|
245
|
+
|
|
246
|
+
- 首次调用: 持 ``RLock`` 执行原函数, 结果以 ``name`` 注册进缓存并返回;
|
|
247
|
+
- 之后调用: 返回缓存实例, 原函数不再执行;
|
|
248
|
+
- 原函数抛异常: 原样传播且不缓存, 下次调用可重试;
|
|
249
|
+
- 键已存在: 不执行原函数, 返回既有实例 (永不覆盖);
|
|
250
|
+
- 带参调用或原函数返回 ``None``: 抛 ``TypeError``。
|
|
251
|
+
|
|
252
|
+
Parameters
|
|
253
|
+
----------
|
|
254
|
+
name : str
|
|
255
|
+
Unique registry key (case-sensitive, non-empty).
|
|
256
|
+
description : str, optional
|
|
257
|
+
One-line description stored for :meth:`cache.describe`.
|
|
258
|
+
verbose : bool
|
|
259
|
+
Print a line when the entry is newly registered.
|
|
260
|
+
"""
|
|
261
|
+
name = cls._validate_name(name)
|
|
262
|
+
description = cls._validate_description(description)
|
|
263
|
+
|
|
264
|
+
def dec(fn):
|
|
265
|
+
@functools.wraps(fn)
|
|
266
|
+
def wrapper(*args, **kwargs):
|
|
267
|
+
if args or kwargs:
|
|
268
|
+
raise TypeError(
|
|
269
|
+
f"cache.lazy loader {fn.__qualname__!r} takes no "
|
|
270
|
+
f"arguments (got {len(args)} positional, "
|
|
271
|
+
f"{sorted(kwargs)} keyword)"
|
|
272
|
+
)
|
|
273
|
+
with cls._lock:
|
|
274
|
+
if name in cls._entries:
|
|
275
|
+
return cls._entries[name]
|
|
276
|
+
api = fn()
|
|
277
|
+
cls._validate_api(api)
|
|
278
|
+
cls._entries[name] = api
|
|
279
|
+
cls._descriptions[name] = (
|
|
280
|
+
description if description is not None
|
|
281
|
+
else f"<lazy '{name}' via {fn.__qualname__}>"
|
|
282
|
+
)
|
|
283
|
+
if verbose:
|
|
284
|
+
print(
|
|
285
|
+
f"[cache] registered '{name}' "
|
|
286
|
+
f"via lazy loader {fn.__qualname__} "
|
|
287
|
+
f"({cls._api_label(api)})"
|
|
288
|
+
)
|
|
289
|
+
return api
|
|
290
|
+
|
|
291
|
+
return wrapper
|
|
292
|
+
|
|
293
|
+
return dec
|
|
294
|
+
|
|
295
|
+
@classmethod
|
|
296
|
+
def get(cls, name):
|
|
297
|
+
"""
|
|
298
|
+
return the cached api
|
|
299
|
+
|
|
300
|
+
Raises
|
|
301
|
+
------
|
|
302
|
+
KeyError
|
|
303
|
+
When ``name`` is not registered.
|
|
304
|
+
"""
|
|
305
|
+
name = cls._validate_name(name)
|
|
306
|
+
with cls._lock:
|
|
307
|
+
if name not in cls._entries:
|
|
308
|
+
raise KeyError(
|
|
309
|
+
f"'{name}' is not registered in the cache; "
|
|
310
|
+
f"registered entries: {sorted(cls._entries)}"
|
|
311
|
+
)
|
|
312
|
+
return cls._entries[name]
|
|
313
|
+
|
|
314
|
+
@classmethod
|
|
315
|
+
def check(cls, name) -> bool:
|
|
316
|
+
"""
|
|
317
|
+
return the result of the query to cache
|
|
318
|
+
"""
|
|
319
|
+
name = cls._validate_name(name)
|
|
320
|
+
with cls._lock:
|
|
321
|
+
return name in cls._entries
|
|
322
|
+
|
|
323
|
+
@classmethod
|
|
324
|
+
def describe(cls, name) -> str:
|
|
325
|
+
"""
|
|
326
|
+
Return the description of a registered module.
|
|
327
|
+
|
|
328
|
+
Every entry registered with a ``description`` answers with exactly
|
|
329
|
+
that text (mirrored in the comment next to the ``cache.add`` call);
|
|
330
|
+
entries registered without one answer with an auto-generated notice.
|
|
331
|
+
|
|
332
|
+
Raises
|
|
333
|
+
------
|
|
334
|
+
KeyError
|
|
335
|
+
When ``name`` is not registered.
|
|
336
|
+
"""
|
|
337
|
+
name = cls._validate_name(name)
|
|
338
|
+
with cls._lock:
|
|
339
|
+
if name not in cls._descriptions:
|
|
340
|
+
raise KeyError(
|
|
341
|
+
f"'{name}' is not registered in the cache; "
|
|
342
|
+
f"registered entries: {sorted(cls._entries)}"
|
|
343
|
+
)
|
|
344
|
+
return cls._descriptions[name]
|
|
345
|
+
|
|
346
|
+
@classmethod
|
|
347
|
+
def remove(cls, name) -> bool:
|
|
348
|
+
"""
|
|
349
|
+
Remove one entry (api and description).
|
|
350
|
+
|
|
351
|
+
Returns True when the entry existed. Removal is the only sanctioned
|
|
352
|
+
way to make room for a *replacement* registration.
|
|
353
|
+
"""
|
|
354
|
+
name = cls._validate_name(name)
|
|
355
|
+
with cls._lock:
|
|
356
|
+
existed = name in cls._entries
|
|
357
|
+
cls._entries.pop(name, None)
|
|
358
|
+
cls._descriptions.pop(name, None)
|
|
359
|
+
return existed
|
|
360
|
+
|
|
361
|
+
@classmethod
|
|
362
|
+
def clear(cls) -> None:
|
|
363
|
+
"""Remove every registered entry."""
|
|
364
|
+
with cls._lock:
|
|
365
|
+
cls._entries.clear()
|
|
366
|
+
cls._descriptions.clear()
|
|
367
|
+
|
|
368
|
+
@classmethod
|
|
369
|
+
def names(cls) -> List[str]:
|
|
370
|
+
"""Sorted list of all registered keys."""
|
|
371
|
+
with cls._lock:
|
|
372
|
+
return sorted(cls._entries)
|
|
373
|
+
|
|
374
|
+
@classmethod
|
|
375
|
+
def keys(cls) -> List[str]:
|
|
376
|
+
"""Alias of :meth:`cache.names`."""
|
|
377
|
+
return cls.names()
|
|
378
|
+
|
|
379
|
+
@classmethod
|
|
380
|
+
def values(cls) -> List[Any]:
|
|
381
|
+
"""List of cached apis in sorted-key order."""
|
|
382
|
+
with cls._lock:
|
|
383
|
+
return [cls._entries[k] for k in sorted(cls._entries)]
|
|
384
|
+
|
|
385
|
+
@classmethod
|
|
386
|
+
def items(cls) -> List[Tuple[str, Any]]:
|
|
387
|
+
"""List of ``(name, api)`` pairs in sorted-key order."""
|
|
388
|
+
with cls._lock:
|
|
389
|
+
return [(k, cls._entries[k]) for k in sorted(cls._entries)]
|
|
390
|
+
|
|
391
|
+
@classmethod
|
|
392
|
+
def descriptions(cls) -> Dict[str, str]:
|
|
393
|
+
"""Snapshot dict of ``{name: description}``."""
|
|
394
|
+
with cls._lock:
|
|
395
|
+
return dict(cls._descriptions)
|
|
396
|
+
|
|
397
|
+
# ------------------------------------------------------------------ #
|
|
398
|
+
# convenience protocol (instance level: len(cache()), 'x' in cache(),
|
|
399
|
+
# cache()['x'])
|
|
400
|
+
# ------------------------------------------------------------------ #
|
|
401
|
+
def __contains__(self, name) -> bool:
|
|
402
|
+
return self.check(name)
|
|
403
|
+
|
|
404
|
+
def __len__(self) -> int:
|
|
405
|
+
with self._lock:
|
|
406
|
+
return len(self._entries)
|
|
407
|
+
|
|
408
|
+
def __getitem__(self, name):
|
|
409
|
+
return self.get(name)
|
|
410
|
+
|
|
411
|
+
def __repr__(self) -> str:
|
|
412
|
+
with self._lock:
|
|
413
|
+
return (
|
|
414
|
+
f"<cache entries={sorted(self._entries)} "
|
|
415
|
+
f"n={len(self._entries)}>"
|
|
416
|
+
)
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
# 首字母大写别名: 与 Base 包其余类(Config / Decomposer)风格一致。
|
|
420
|
+
Cache = cache
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Core contracts: the decomposition result type and the decomposer base class.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
from abc import ABC, abstractmethod
|
|
7
|
+
from dataclasses import dataclass, field
|
|
8
|
+
from typing import Any, ClassVar
|
|
9
|
+
|
|
10
|
+
from .ConfigDefine import Config
|
|
11
|
+
from .TextDefine import Name, Reference
|
|
12
|
+
from .PathDefine import TEMP_DIR
|
|
13
|
+
|
|
14
|
+
__all__ = ["DecompositionResult", "Decomposer"]
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@dataclass(frozen=True)
|
|
18
|
+
class DecompositionResult:
|
|
19
|
+
"""
|
|
20
|
+
Unified return type of every decomposition method.
|
|
21
|
+
|
|
22
|
+
Attributes
|
|
23
|
+
----------
|
|
24
|
+
IMFs : np.ndarray
|
|
25
|
+
Decomposed modes. Shape (K, N) for univariate methods and
|
|
26
|
+
(K, d, N) for multivariate methods.
|
|
27
|
+
Res : np.ndarray | None
|
|
28
|
+
Residual. None for methods without a residual concept.
|
|
29
|
+
info : dict[str, Any]
|
|
30
|
+
Method-specific diagnostics. Always a dict.
|
|
31
|
+
config : Config
|
|
32
|
+
Effective parameter snapshot of this run. Never None.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
IMFs: np.ndarray
|
|
36
|
+
Res: np.ndarray | None
|
|
37
|
+
info: dict[str, Any] = field(default_factory=dict)
|
|
38
|
+
config: Config = field(default_factory=Config)
|
|
39
|
+
|
|
40
|
+
@property
|
|
41
|
+
def n_imfs(self) -> int:
|
|
42
|
+
"""
|
|
43
|
+
Number of decomposed modes.
|
|
44
|
+
"""
|
|
45
|
+
return int(self.IMFs.shape[0])
|
|
46
|
+
|
|
47
|
+
@property
|
|
48
|
+
def shape(self) -> tuple:
|
|
49
|
+
"""
|
|
50
|
+
Shape of the IMFs array.
|
|
51
|
+
"""
|
|
52
|
+
return tuple(self.IMFs.shape)
|
|
53
|
+
|
|
54
|
+
def reconstruct(self) -> np.ndarray:
|
|
55
|
+
"""
|
|
56
|
+
Reconstruct the signal as the sum of all modes plus the residual.
|
|
57
|
+
|
|
58
|
+
A None residual contributes zero.
|
|
59
|
+
"""
|
|
60
|
+
recon = np.sum(self.IMFs, axis=0)
|
|
61
|
+
if self.Res is not None:
|
|
62
|
+
recon = recon + self.Res
|
|
63
|
+
return recon
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class Decomposer(ABC):
|
|
67
|
+
"""
|
|
68
|
+
Base class of all decomposition methods.
|
|
69
|
+
|
|
70
|
+
Subclasses must set the ``name`` class attribute to their registry key
|
|
71
|
+
and implement ``decompose``.
|
|
72
|
+
"""
|
|
73
|
+
|
|
74
|
+
name: ClassVar[str]
|
|
75
|
+
|
|
76
|
+
@abstractmethod
|
|
77
|
+
def decompose(self, S, T=None) -> DecompositionResult:
|
|
78
|
+
"""
|
|
79
|
+
Decompose the signal.
|
|
80
|
+
|
|
81
|
+
Parameters
|
|
82
|
+
----------
|
|
83
|
+
S : array-like
|
|
84
|
+
Signal.
|
|
85
|
+
T : array-like, optional
|
|
86
|
+
Time axis. If None, a default index axis is used.
|
|
87
|
+
|
|
88
|
+
Returns
|
|
89
|
+
-------
|
|
90
|
+
DecompositionResult
|
|
91
|
+
Unified decomposition result.
|
|
92
|
+
"""
|
|
93
|
+
|
|
94
|
+
def __call__(self, S, T=None) -> DecompositionResult:
|
|
95
|
+
"""
|
|
96
|
+
Alias for ``decompose``.
|
|
97
|
+
"""
|
|
98
|
+
return self.decompose(S, T)
|
|
99
|
+
|
|
100
|
+
def chunk(self, S: np.ndarray, chunk_size: int) -> np.ndarray:
|
|
101
|
+
_dtype = S.dtype
|
|
102
|
+
|
|
103
|
+
L = S.shape[0]
|
|
104
|
+
chunk_num = L // chunk_size
|
|
105
|
+
if L % chunk_size != 0:
|
|
106
|
+
last = True
|
|
107
|
+
last_len = L - chunk_num * chunk_size
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
@property
|
|
112
|
+
def full_name(self) -> str:
|
|
113
|
+
"""
|
|
114
|
+
Full descriptive name of the method from the Name table.
|
|
115
|
+
"""
|
|
116
|
+
try:
|
|
117
|
+
return Name[self.name]
|
|
118
|
+
except KeyError:
|
|
119
|
+
raise ValueError(f"No full name defined for method {self.name!r}")
|
|
120
|
+
|
|
121
|
+
@property
|
|
122
|
+
def reference(self) -> str:
|
|
123
|
+
"""
|
|
124
|
+
Bibliographic reference (DOI) from the Reference table.
|
|
125
|
+
"""
|
|
126
|
+
try:
|
|
127
|
+
return Reference[self.name]
|
|
128
|
+
except KeyError:
|
|
129
|
+
raise ValueError(f"No reference defined for method {self.name!r}")
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Base class for per-method configuration snapshots.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from dataclasses import asdict
|
|
6
|
+
|
|
7
|
+
__all__ = ["Config"]
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Config:
|
|
11
|
+
"""
|
|
12
|
+
Immutable parameter snapshot of a decomposition run.
|
|
13
|
+
|
|
14
|
+
Subclasses are frozen, keyword-only dataclasses defined next to their
|
|
15
|
+
method. Instances record the effective parameter values of one run.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
def to_dict(self) -> dict:
|
|
19
|
+
"""
|
|
20
|
+
Return the configuration as a plain dict.
|
|
21
|
+
"""
|
|
22
|
+
return asdict(self)
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Size constants shared by the whole package.
|
|
3
|
+
|
|
4
|
+
所有涉及分块 / 内存策略的大小常量统一从本字典取用
|
|
5
|
+
(见 ``Utils.Chunk`` / ``Utils.Check`` / ``Utils.Memory`` /
|
|
6
|
+
``Utils.Monotonicity``), 避免各模块各自书写魔数。
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
__all__ = ["SIZE"]
|
|
10
|
+
|
|
11
|
+
#: 基本字节大小字典: 1KB / 1MB / 1GB 三类基本常量。
|
|
12
|
+
#: 值为字节数 (int); 以"元素数"为单位的常量同样可用其倍数表达
|
|
13
|
+
#: (如 8M 元素 = ``8 * SIZE["1MB"]``)。
|
|
14
|
+
SIZE = \
|
|
15
|
+
{
|
|
16
|
+
"1KB": 1024, # 2**10 bytes
|
|
17
|
+
"1MB": 1024 ** 2, # 2**20 bytes
|
|
18
|
+
"1GB": 1024 ** 3, # 2**30 bytes
|
|
19
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import os
|
|
2
|
+
|
|
3
|
+
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
4
|
+
BASE_DIR = os.path.join(ROOT_DIR, "Base")
|
|
5
|
+
C_METHOD_DIR = os.path.join(BASE_DIR, "C_method")
|
|
6
|
+
ERROR_DIR = os.path.join(BASE_DIR, "Error")
|
|
7
|
+
UTILS_DIR = os.path.join(BASE_DIR, "Utils")
|
|
8
|
+
TEMP_DIR = os.path.join(BASE_DIR, "_temp")
|
|
9
|
+
|
|
10
|
+
if __name__ == "__main__":
|
|
11
|
+
print(ROOT_DIR)
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Method metadata tables: full names and references.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
__all__ = ["Name", "Reference"]
|
|
6
|
+
|
|
7
|
+
Name = \
|
|
8
|
+
{
|
|
9
|
+
"CEEMDAN": "Complete Ensemble Empirical Mode Decomposition with Adaptive Noise",
|
|
10
|
+
"CEEFD": "Cyclic Envelop Empirical Fourier Decomposition",
|
|
11
|
+
"CEEMD": "Complementary Ensemble Empirical Mode Decomposition",
|
|
12
|
+
"EEMD": "Ensemble Empirical Mode Decomposition",
|
|
13
|
+
"EFD": "Empirical Fourier Decomposition",
|
|
14
|
+
"EMD": "Empirical Mode Decomposition",
|
|
15
|
+
"EWT": "Empirical Wavelet Transform",
|
|
16
|
+
"FMD": "Filtered Mode Decomposition",
|
|
17
|
+
"ICEEMDAN": "Improved Complete Ensemble Empirical Mode Decomposition with Adaptive Noise",
|
|
18
|
+
"LMD": "Local Mean Decomposition",
|
|
19
|
+
"MEMD": "Multivariate Empirical Mode Decomposition",
|
|
20
|
+
"RPSEMD": "Random Phase Sinusoidal Assisted Empirical Mode Decomposition",
|
|
21
|
+
"SSA": "Singular Spectrum Analysis",
|
|
22
|
+
"SVMD": "Successive Variational Mode Decomposition",
|
|
23
|
+
"VMD": "Variational Mode Decomposition",
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
Reference = \
|
|
27
|
+
{
|
|
28
|
+
"CEEMDAN": "10.1109/ICASSP.2011.5947265",
|
|
29
|
+
"CEEFD": "10.3969/j.issn.1001-4551.2023.07.001",
|
|
30
|
+
"CEEMD": "10.1016/j.jhydrol.2020.124647",
|
|
31
|
+
"EEMD": "10.1142/S1793536909000047",
|
|
32
|
+
"EFD": "10.1016/j.ymssp.2021.108155",
|
|
33
|
+
"EMD": "10.1098/rspa.1998.0193",
|
|
34
|
+
"EWT": "10.48550/arXiv.2304.06274",
|
|
35
|
+
"FMD": "10.1109/TIE.2022.3156156",
|
|
36
|
+
"ICEEMDAN": "10.1007/s10470-021-01901-3",
|
|
37
|
+
"LMD": "10.1098/rsif.2005.0058",
|
|
38
|
+
"MEMD": "10.48550/arXiv.2206.00926",
|
|
39
|
+
"RPSEMD": "10.1109/LSP.2016.2537376",
|
|
40
|
+
"SSA": "10.1016/j.mex.2020.101015",
|
|
41
|
+
"SVMD": "10.1016/j.sigpro.2020.107610",
|
|
42
|
+
"VMD": "10.1109/TSP.2013.2288675",
|
|
43
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Base package: contracts, metadata tables, the config base class, and the
|
|
3
|
+
import cache.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from .Cache import Cache, cache
|
|
7
|
+
from .ClassDefine import Decomposer, DecompositionResult
|
|
8
|
+
from .ConfigDefine import Config
|
|
9
|
+
from .ConstDefine import SIZE
|
|
10
|
+
from .TextDefine import Name, Reference
|
|
11
|
+
|
|
12
|
+
__all__ = [
|
|
13
|
+
"Decomposer",
|
|
14
|
+
"DecompositionResult",
|
|
15
|
+
"Config",
|
|
16
|
+
"Name",
|
|
17
|
+
"Reference",
|
|
18
|
+
"SIZE",
|
|
19
|
+
"cache",
|
|
20
|
+
"Cache",
|
|
21
|
+
]
|