SpharaPy 1.3.0__tar.gz → 1.3.1__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.
- {spharapy-1.3.0/spharapy.egg-info → spharapy-1.3.1}/PKG-INFO +5 -5
- {spharapy-1.3.0 → spharapy-1.3.1}/README.rst +4 -4
- {spharapy-1.3.0 → spharapy-1.3.1}/pyproject.toml +1 -1
- {spharapy-1.3.0 → spharapy-1.3.1}/spharapy/__init__.py +2 -3
- {spharapy-1.3.0 → spharapy-1.3.1}/spharapy/examples/plot_05_BaCI2019_TrainingCourse4_SpharaPy_01.py +0 -1
- {spharapy-1.3.0 → spharapy-1.3.1}/spharapy/examples/plot_05_BaCI2019_TrainingCourse4_SpharaPy_02.py +1 -2
- {spharapy-1.3.0 → spharapy-1.3.1}/spharapy/spharabasis.py +1 -3
- {spharapy-1.3.0 → spharapy-1.3.1}/spharapy/spharafilter.py +98 -31
- {spharapy-1.3.0 → spharapy-1.3.1}/spharapy/spharatransform.py +1 -0
- {spharapy-1.3.0 → spharapy-1.3.1}/spharapy/trimesh.py +1 -0
- {spharapy-1.3.0 → spharapy-1.3.1/spharapy.egg-info}/PKG-INFO +5 -5
- {spharapy-1.3.0 → spharapy-1.3.1}/spharapy.egg-info/SOURCES.txt +1 -0
- {spharapy-1.3.0 → spharapy-1.3.1}/tests/test_spectral_filters.py +7 -2
- spharapy-1.3.1/tests/test_spharafilter_transfer.py +143 -0
- {spharapy-1.3.0 → spharapy-1.3.1}/LICENSE +0 -0
- {spharapy-1.3.0 → spharapy-1.3.1}/MANIFEST.in +0 -0
- {spharapy-1.3.0 → spharapy-1.3.1}/setup.cfg +0 -0
- {spharapy-1.3.0 → spharapy-1.3.1}/spharapy/_types.py +0 -0
- {spharapy-1.3.0 → spharapy-1.3.1}/spharapy/datasets/__init__.py +0 -0
- {spharapy-1.3.0 → spharapy-1.3.1}/spharapy/datasets/base.py +0 -0
- {spharapy-1.3.0 → spharapy-1.3.1}/spharapy/datasets/data/eeg_256_channels_label.csv +0 -0
- {spharapy-1.3.0 → spharapy-1.3.1}/spharapy/datasets/data/eeg_256_channels_sep_data.csv +0 -0
- {spharapy-1.3.0 → spharapy-1.3.1}/spharapy/datasets/data/eeg_256_channels_tri.csv +0 -0
- {spharapy-1.3.0 → spharapy-1.3.1}/spharapy/datasets/data/eeg_256_channels_vert.csv +0 -0
- {spharapy-1.3.0 → spharapy-1.3.1}/spharapy/datasets/data/simple_mesh_tri.csv +0 -0
- {spharapy-1.3.0 → spharapy-1.3.1}/spharapy/datasets/data/simple_mesh_vert.csv +0 -0
- {spharapy-1.3.0 → spharapy-1.3.1}/spharapy/datasets/descr/eeg_256_channel_study.rst +0 -0
- {spharapy-1.3.0 → spharapy-1.3.1}/spharapy/datasets/descr/minimal_triangular_mesh.rst +0 -0
- {spharapy-1.3.0 → spharapy-1.3.1}/spharapy/datasets/descr/simple_triangular_mesh.rst +0 -0
- {spharapy-1.3.0 → spharapy-1.3.1}/spharapy/examples/SpharaPy_example.py +0 -0
- {spharapy-1.3.0 → spharapy-1.3.1}/spharapy/examples/plot_01_quick_start.py +0 -0
- {spharapy-1.3.0 → spharapy-1.3.1}/spharapy/examples/plot_02_sphara_basis_eeg.py +0 -0
- {spharapy-1.3.0 → spharapy-1.3.1}/spharapy/examples/plot_03_sphara_analysis_eeg.py +0 -0
- {spharapy-1.3.0 → spharapy-1.3.1}/spharapy/examples/plot_04_sphara_filter_eeg.py +0 -0
- {spharapy-1.3.0 → spharapy-1.3.1}/spharapy/spectral_filters.py +0 -0
- {spharapy-1.3.0 → spharapy-1.3.1}/spharapy.egg-info/dependency_links.txt +0 -0
- {spharapy-1.3.0 → spharapy-1.3.1}/spharapy.egg-info/requires.txt +0 -0
- {spharapy-1.3.0 → spharapy-1.3.1}/spharapy.egg-info/top_level.txt +0 -0
- {spharapy-1.3.0 → spharapy-1.3.1}/tests/test_spharabasis.py +0 -0
- {spharapy-1.3.0 → spharapy-1.3.1}/tests/test_spharafilter.py +0 -0
- {spharapy-1.3.0 → spharapy-1.3.1}/tests/test_spharatransform.py +0 -0
- {spharapy-1.3.0 → spharapy-1.3.1}/tests/test_trimesh.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: spharapy
|
|
3
|
-
Version: 1.3.
|
|
3
|
+
Version: 1.3.1
|
|
4
4
|
Summary: SPHARA-based spatial harmonic analysis and filtering
|
|
5
5
|
Author-email: Uwe Graichen <uwe.graichen@kl.ac.at>
|
|
6
6
|
License-Expression: BSD-3-Clause
|
|
@@ -43,7 +43,7 @@ Dynamic: license-file
|
|
|
43
43
|
:alt: Coverage
|
|
44
44
|
|
|
45
45
|
|
|
46
|
-
|
|
46
|
+
|
|
47
47
|
Sphara Implementation in Python
|
|
48
48
|
===============================
|
|
49
49
|
|
|
@@ -95,12 +95,12 @@ please consider citing the following publications:
|
|
|
95
95
|
|
|
96
96
|
- **Graichen, U., Eichardt, R., & Haueisen, J. (2019).**
|
|
97
97
|
*SpharaPy: A Python toolbox for spatial harmonic analysis of non-uniformly
|
|
98
|
-
sampled data.* SoftwareX, 10, Article 100289.
|
|
98
|
+
sampled data.* SoftwareX, 10, Article 100289.
|
|
99
99
|
https://doi.org/10.1016/j.softx.2019.100289
|
|
100
100
|
|
|
101
101
|
- **Graichen, U., Eichardt, R., Fiedler, P., Strohmeier, D., Zanow, F.,
|
|
102
102
|
& Haueisen, J. (2015).**
|
|
103
103
|
*SPHARA – A generalized spatial Fourier analysis for multi-sensor
|
|
104
|
-
systems with non-uniformly arranged sensors: Application to EEG.*
|
|
105
|
-
PLOS ONE, 10(4), e0121741.
|
|
104
|
+
systems with non-uniformly arranged sensors: Application to EEG.*
|
|
105
|
+
PLOS ONE, 10(4), e0121741.
|
|
106
106
|
https://doi.org/10.1371/journal.pone.0121741
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
:alt: Coverage
|
|
22
22
|
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
|
|
25
25
|
Sphara Implementation in Python
|
|
26
26
|
===============================
|
|
27
27
|
|
|
@@ -73,12 +73,12 @@ please consider citing the following publications:
|
|
|
73
73
|
|
|
74
74
|
- **Graichen, U., Eichardt, R., & Haueisen, J. (2019).**
|
|
75
75
|
*SpharaPy: A Python toolbox for spatial harmonic analysis of non-uniformly
|
|
76
|
-
sampled data.* SoftwareX, 10, Article 100289.
|
|
76
|
+
sampled data.* SoftwareX, 10, Article 100289.
|
|
77
77
|
https://doi.org/10.1016/j.softx.2019.100289
|
|
78
78
|
|
|
79
79
|
- **Graichen, U., Eichardt, R., Fiedler, P., Strohmeier, D., Zanow, F.,
|
|
80
80
|
& Haueisen, J. (2015).**
|
|
81
81
|
*SPHARA – A generalized spatial Fourier analysis for multi-sensor
|
|
82
|
-
systems with non-uniformly arranged sensors: Application to EEG.*
|
|
83
|
-
PLOS ONE, 10(4), e0121741.
|
|
82
|
+
systems with non-uniformly arranged sensors: Application to EEG.*
|
|
83
|
+
PLOS ONE, 10(4), e0121741.
|
|
84
84
|
https://doi.org/10.1371/journal.pone.0121741
|
|
@@ -59,7 +59,7 @@ build-backend = "setuptools.build_meta"
|
|
|
59
59
|
# ---------------------------------------------------------------------------
|
|
60
60
|
[project]
|
|
61
61
|
name = "spharapy"
|
|
62
|
-
version = "1.3.
|
|
62
|
+
version = "1.3.1" # Keep in sync with spharapy.__version__ (if defined)
|
|
63
63
|
description = "SPHARA-based spatial harmonic analysis and filtering"
|
|
64
64
|
|
|
65
65
|
# Long description shown on PyPI. This must be valid plain ReST (no Sphinx roles).
|
|
@@ -3,11 +3,10 @@ SpharaPy: SPHARA-based spatial harmonic analysis and filtering.
|
|
|
3
3
|
"""
|
|
4
4
|
|
|
5
5
|
__author__ = "Uwe Graichen"
|
|
6
|
-
__copyright__ = "Copyright 2018-
|
|
6
|
+
__copyright__ = "Copyright 2018-2026, Uwe Graichen"
|
|
7
7
|
__credits__ = ["Uwe Graichen"]
|
|
8
8
|
__license__ = "BSD-3-Clause"
|
|
9
|
-
__version__ = "1.3.
|
|
9
|
+
__version__ = "1.3.1"
|
|
10
10
|
__maintainer__ = "Uwe Graichen"
|
|
11
11
|
__email__ = "uwe.graichen@tu-ilmenau.de, uwe.graichen@kl.ac.at"
|
|
12
12
|
__status__ = "Release"
|
|
13
|
-
|
{spharapy-1.3.0 → spharapy-1.3.1}/spharapy/examples/plot_05_BaCI2019_TrainingCourse4_SpharaPy_02.py
RENAMED
|
@@ -327,7 +327,7 @@ def plot_scalar_on_mesh(mesh: tm.TriMesh, values: np.ndarray, title: str) -> Non
|
|
|
327
327
|
vmin=-vmax,
|
|
328
328
|
vmax=vmax
|
|
329
329
|
)
|
|
330
|
-
|
|
330
|
+
|
|
331
331
|
surf.set_array(val)
|
|
332
332
|
surf.set_clim(-2.0, 2.0)
|
|
333
333
|
|
|
@@ -389,4 +389,3 @@ plot_scalar_on_mesh(mesh_eeg, topo_filt, "Gaussian SPHARA LP filtered\n topograp
|
|
|
389
389
|
# filters tuned to specific spatial frequency bands) can be obtained by
|
|
390
390
|
# combining the low-pass transfer functions or using the helper
|
|
391
391
|
# functions provided in :mod:`spharapy.spectral_filters`.
|
|
392
|
-
|
|
@@ -5,10 +5,9 @@ are provided to determine basis functions using different discretization
|
|
|
5
5
|
schemes of the Laplace-Beltrami operator, as FEM, inverse euclidean and unit.
|
|
6
6
|
"""
|
|
7
7
|
|
|
8
|
-
from scipy import linalg
|
|
9
|
-
|
|
10
8
|
import numpy as np
|
|
11
9
|
from numpy.typing import NDArray
|
|
10
|
+
from scipy import linalg
|
|
12
11
|
|
|
13
12
|
import spharapy.trimesh as tm
|
|
14
13
|
|
|
@@ -300,4 +299,3 @@ class SpharaBasis:
|
|
|
300
299
|
wavelengths[zero_mask] = np.inf
|
|
301
300
|
wavelengths[nonzero_mask] = 2.0 * np.pi / omega[nonzero_mask]
|
|
302
301
|
return wavelengths
|
|
303
|
-
|
|
@@ -17,8 +17,9 @@ from spharapy.spharabasis import SpharaBasis
|
|
|
17
17
|
|
|
18
18
|
FloatArray = NDArray[np.floating]
|
|
19
19
|
|
|
20
|
+
|
|
20
21
|
class SpharaFilter(SpharaBasis):
|
|
21
|
-
"""SPHARA filter class
|
|
22
|
+
r"""SPHARA filter class
|
|
22
23
|
|
|
23
24
|
This class is used to design different types of filters and to
|
|
24
25
|
apply this filters to spatially irregularly sampled data.
|
|
@@ -38,28 +39,52 @@ class SpharaFilter(SpharaBasis):
|
|
|
38
39
|
inverse Euclidean distance of the edge lengths. The option
|
|
39
40
|
'fem' uses a FEM discretisation. The default weighting
|
|
40
41
|
function is 'fem'.
|
|
41
|
-
specification : integer or array, shape (
|
|
42
|
+
specification : integer or array, shape (n_points,)
|
|
42
43
|
If an integer value for specification is passed to the
|
|
43
|
-
constructor, it must be within the interval
|
|
44
|
-
n_points
|
|
45
|
-
points. If a positive integer value is passed,
|
|
44
|
+
constructor, it must be within the interval [-n_points,
|
|
45
|
+
n_points], where n_points is the number of spatial sample
|
|
46
|
+
points. If a positive integer value is passed, an ideal spatial
|
|
46
47
|
low-pass filter with the corresponding number of SPHARA basis
|
|
47
|
-
functions is created
|
|
48
|
-
spatial
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
48
|
+
functions is created; if a negative integer value is passed, an
|
|
49
|
+
ideal spatial high-pass filter with the corresponding number of
|
|
50
|
+
SPHARA basis functions is created. If a vector is passed, it is
|
|
51
|
+
interpreted as the transfer function :math:`\boldsymbol{h}` of
|
|
52
|
+
the filter in the SPHARA domain: the coefficient of the k-th
|
|
53
|
+
SPHARA basis function is multiplied by ``specification[k]``.
|
|
54
|
+
Binary vectors (0/1) define ideal filters, graded vectors (e.g.
|
|
55
|
+
from :mod:`spharapy.spectral_filters`) define smooth filters.
|
|
56
|
+
The default value of specification is 0, it means a neutral
|
|
52
57
|
all-pass filter is designed and applied.
|
|
53
58
|
|
|
59
|
+
Notes
|
|
60
|
+
-----
|
|
61
|
+
With the SPHARA basis :math:`\boldsymbol{\Phi}` (basis functions
|
|
62
|
+
column by column) and the transfer function :math:`\boldsymbol{h}`,
|
|
63
|
+
the filter applied to data stored row-wise (samples x channels) is
|
|
64
|
+
:math:`\tilde{\boldsymbol{D}} = \boldsymbol{D} \boldsymbol{F}` with
|
|
65
|
+
|
|
66
|
+
* :math:`\boldsymbol{F} = \boldsymbol{B} \boldsymbol{\Phi}
|
|
67
|
+
\operatorname{diag}(\boldsymbol{h}) \boldsymbol{\Phi}^\top`
|
|
68
|
+
for ``mode='fem'`` (:math:`\boldsymbol{B}` is the mass matrix,
|
|
69
|
+
:math:`\boldsymbol{\Phi}` is :math:`\boldsymbol{B}`-orthonormal),
|
|
70
|
+
* :math:`\boldsymbol{F} = \boldsymbol{\Phi}
|
|
71
|
+
\operatorname{diag}(\boldsymbol{h}) \boldsymbol{\Phi}^\top`
|
|
72
|
+
for ``mode='unit'`` and ``mode='inv_euclidean'``.
|
|
73
|
+
|
|
74
|
+
This is identical to a SPHARA analysis, a multiplication of the
|
|
75
|
+
coefficients by :math:`\boldsymbol{h}` and a SPHARA synthesis.
|
|
76
|
+
|
|
54
77
|
"""
|
|
55
78
|
|
|
56
79
|
def __init__(self, triangsamples=None, mode="fem", specification=0):
|
|
80
|
+
# cached filter matrix; must exist before the base class calls
|
|
81
|
+
# the (overridden) setters of triangsamples and mode
|
|
82
|
+
self._filtermatrix = None
|
|
57
83
|
SpharaBasis.__init__(self, triangsamples, mode)
|
|
58
84
|
# internal fields
|
|
59
85
|
self._basis = None
|
|
60
86
|
self._frequencies = None
|
|
61
87
|
self._massmatrix = None
|
|
62
|
-
self._filtermatrix = None
|
|
63
88
|
|
|
64
89
|
# default all-pass (ensures not-None even if someone bypasses the setter)
|
|
65
90
|
n = self._triangsamples.vertlist.shape[0]
|
|
@@ -68,6 +93,28 @@ class SpharaFilter(SpharaBasis):
|
|
|
68
93
|
# now apply the user-provided spec (calls the setter above)
|
|
69
94
|
self.specification = specification
|
|
70
95
|
|
|
96
|
+
# Changing the mesh or the discretisation changes the basis, so the
|
|
97
|
+
# cached filter matrix has to be discarded as well.
|
|
98
|
+
@property
|
|
99
|
+
def triangsamples(self):
|
|
100
|
+
"""Get or set the triangsamples object (see :class:`~spharapy.spharabasis.SpharaBasis`)."""
|
|
101
|
+
return self._triangsamples
|
|
102
|
+
|
|
103
|
+
@triangsamples.setter
|
|
104
|
+
def triangsamples(self, triangsamples):
|
|
105
|
+
SpharaBasis.triangsamples.fset(self, triangsamples) # type: ignore[attr-defined]
|
|
106
|
+
self._filtermatrix = None
|
|
107
|
+
|
|
108
|
+
@property
|
|
109
|
+
def mode(self):
|
|
110
|
+
"""Get or set the discretisation method (see :class:`~spharapy.spharabasis.SpharaBasis`)."""
|
|
111
|
+
return self._mode
|
|
112
|
+
|
|
113
|
+
@mode.setter
|
|
114
|
+
def mode(self, mode):
|
|
115
|
+
SpharaBasis.mode.fset(self, mode) # type: ignore[attr-defined]
|
|
116
|
+
self._filtermatrix = None
|
|
117
|
+
|
|
71
118
|
@property
|
|
72
119
|
def specification(self):
|
|
73
120
|
"""Get or set the specification of the filter.
|
|
@@ -97,7 +144,7 @@ class SpharaFilter(SpharaBasis):
|
|
|
97
144
|
v[specification:] = 1.0
|
|
98
145
|
self._specification = v
|
|
99
146
|
|
|
100
|
-
elif isinstance(specification, (list
|
|
147
|
+
elif isinstance(specification, (list | tuple | np.ndarray)):
|
|
101
148
|
arr = np.asarray(specification, dtype=float)
|
|
102
149
|
if arr.shape[0] != n:
|
|
103
150
|
raise IndexError(
|
|
@@ -109,6 +156,9 @@ class SpharaFilter(SpharaBasis):
|
|
|
109
156
|
else:
|
|
110
157
|
raise TypeError("The parameter specification has to be int or a vector.")
|
|
111
158
|
|
|
159
|
+
# a new transfer function requires a new filter matrix
|
|
160
|
+
self._filtermatrix = None
|
|
161
|
+
|
|
112
162
|
def _ensure_ready(self) -> None:
|
|
113
163
|
# compute basis lazily if needed
|
|
114
164
|
if self._basis is None or self._frequencies is None:
|
|
@@ -127,7 +177,14 @@ class SpharaFilter(SpharaBasis):
|
|
|
127
177
|
for data defined at spatially distributed sampling points
|
|
128
178
|
described by a triangular mesh. The filtering is
|
|
129
179
|
performed by matrix multiplication of the data matrix and a
|
|
130
|
-
precalculated filter matrix
|
|
180
|
+
precalculated filter matrix,
|
|
181
|
+
:math:`\tilde{\boldsymbol{D}} = \boldsymbol{D} \boldsymbol{F}`, with
|
|
182
|
+
:math:`\boldsymbol{F} = \boldsymbol{B} \boldsymbol{\Phi}
|
|
183
|
+
\operatorname{diag}(\boldsymbol{h}) \boldsymbol{\Phi}^\top`
|
|
184
|
+
for ``mode='fem'`` and :math:`\boldsymbol{F} = \boldsymbol{\Phi}
|
|
185
|
+
\operatorname{diag}(\boldsymbol{h}) \boldsymbol{\Phi}^\top`
|
|
186
|
+
otherwise. Each SPHARA basis function :math:`\boldsymbol{\phi}_k`
|
|
187
|
+
is thus mapped to :math:`h_k \boldsymbol{\phi}_k`.
|
|
131
188
|
|
|
132
189
|
Parameters
|
|
133
190
|
----------
|
|
@@ -186,13 +243,24 @@ class SpharaFilter(SpharaBasis):
|
|
|
186
243
|
else cast(FloatArray, np.asarray(self._massmatrix, dtype=float))
|
|
187
244
|
)
|
|
188
245
|
|
|
189
|
-
|
|
246
|
+
if H.shape[0] != basis.shape[1]:
|
|
247
|
+
raise ValueError(
|
|
248
|
+
f"Dimension mismatch: specification has {H.shape[0]} entries, "
|
|
249
|
+
f"basis has {basis.shape[1]} functions."
|
|
250
|
+
)
|
|
251
|
+
|
|
252
|
+
# Build filter matrix once (use broadcasting instead of np.diag for speed).
|
|
253
|
+
# F = B Phi diag(h) Phi^T (fem) or Phi diag(h) Phi^T (unit, inv_euclidean).
|
|
254
|
+
# Note: diag(h) must appear only once; (Phi diag(h)) (Phi diag(h))^T
|
|
255
|
+
# would apply h**2.
|
|
190
256
|
if self._filtermatrix is None:
|
|
191
|
-
|
|
192
|
-
if self._mode == "fem"
|
|
193
|
-
|
|
257
|
+
basis_weighted: FloatArray = basis * H # Phi diag(h), scales columns
|
|
258
|
+
if self._mode == "fem":
|
|
259
|
+
if M is None:
|
|
260
|
+
raise RuntimeError("Mass matrix not initialized for FEM mode.")
|
|
261
|
+
self._filtermatrix = M @ basis_weighted @ basis.T
|
|
194
262
|
else:
|
|
195
|
-
self._filtermatrix =
|
|
263
|
+
self._filtermatrix = basis_weighted @ basis.T
|
|
196
264
|
|
|
197
265
|
F: FloatArray = cast(FloatArray, self._filtermatrix)
|
|
198
266
|
|
|
@@ -213,19 +281,18 @@ class SpharaFilter(SpharaBasis):
|
|
|
213
281
|
data_filtered = cast(FloatArray, data_filtered[0])
|
|
214
282
|
return data_filtered
|
|
215
283
|
|
|
216
|
-
def spectral_grid(self) -> FloatArray:
|
|
217
|
-
|
|
284
|
+
def spectral_grid(self) -> FloatArray:
|
|
285
|
+
"""Return the SPHARA spectral grid used by this filter.
|
|
218
286
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
287
|
+
This is equivalent to
|
|
288
|
+
:meth:`~spharapy.spharabasis.SpharaBasis.eigenvalues` and can be
|
|
289
|
+
used together with the transfer-function helpers in
|
|
290
|
+
:mod:`spharapy.spectral_filters`.
|
|
223
291
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
292
|
+
Returns
|
|
293
|
+
-------
|
|
294
|
+
numpy.ndarray
|
|
295
|
+
Eigenvalues :math:`τ_i` associated with the SPHARA basis.
|
|
228
296
|
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
297
|
+
"""
|
|
298
|
+
return self.eigenvalues()
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: spharapy
|
|
3
|
-
Version: 1.3.
|
|
3
|
+
Version: 1.3.1
|
|
4
4
|
Summary: SPHARA-based spatial harmonic analysis and filtering
|
|
5
5
|
Author-email: Uwe Graichen <uwe.graichen@kl.ac.at>
|
|
6
6
|
License-Expression: BSD-3-Clause
|
|
@@ -43,7 +43,7 @@ Dynamic: license-file
|
|
|
43
43
|
:alt: Coverage
|
|
44
44
|
|
|
45
45
|
|
|
46
|
-
|
|
46
|
+
|
|
47
47
|
Sphara Implementation in Python
|
|
48
48
|
===============================
|
|
49
49
|
|
|
@@ -95,12 +95,12 @@ please consider citing the following publications:
|
|
|
95
95
|
|
|
96
96
|
- **Graichen, U., Eichardt, R., & Haueisen, J. (2019).**
|
|
97
97
|
*SpharaPy: A Python toolbox for spatial harmonic analysis of non-uniformly
|
|
98
|
-
sampled data.* SoftwareX, 10, Article 100289.
|
|
98
|
+
sampled data.* SoftwareX, 10, Article 100289.
|
|
99
99
|
https://doi.org/10.1016/j.softx.2019.100289
|
|
100
100
|
|
|
101
101
|
- **Graichen, U., Eichardt, R., Fiedler, P., Strohmeier, D., Zanow, F.,
|
|
102
102
|
& Haueisen, J. (2015).**
|
|
103
103
|
*SPHARA – A generalized spatial Fourier analysis for multi-sensor
|
|
104
|
-
systems with non-uniformly arranged sensors: Application to EEG.*
|
|
105
|
-
PLOS ONE, 10(4), e0121741.
|
|
104
|
+
systems with non-uniformly arranged sensors: Application to EEG.*
|
|
105
|
+
PLOS ONE, 10(4), e0121741.
|
|
106
106
|
https://doi.org/10.1371/journal.pone.0121741
|
|
@@ -27,19 +27,21 @@ def grid(n=257, fmax=1.0):
|
|
|
27
27
|
# Basic helpers
|
|
28
28
|
# -------------------------------------------------------------------------
|
|
29
29
|
|
|
30
|
+
|
|
30
31
|
def test_as_highpass_and_bandpass_identities():
|
|
31
32
|
lp = np.array([1.0, 0.5, 0.0])
|
|
32
33
|
hp = as_highpass(lp)
|
|
33
34
|
np.testing.assert_allclose(hp, np.array([0.0, 0.5, 1.0]))
|
|
34
35
|
|
|
35
|
-
bp = as_bandpass(np.array([0
|
|
36
|
-
np.testing.assert_allclose(bp, np.array([0
|
|
36
|
+
bp = as_bandpass(np.array([0.0, 0.3, 1.0]), np.array([0.0, 1.0, 1.0]))
|
|
37
|
+
np.testing.assert_allclose(bp, np.array([0.0, 0.7, 0.0]))
|
|
37
38
|
|
|
38
39
|
|
|
39
40
|
# -------------------------------------------------------------------------
|
|
40
41
|
# Ideal filters
|
|
41
42
|
# -------------------------------------------------------------------------
|
|
42
43
|
|
|
44
|
+
|
|
43
45
|
def test_ideal_lowpass_relative_equals_absolute():
|
|
44
46
|
f = grid()
|
|
45
47
|
lp_abs = transfer_func_ideal_lowpass(f, fc=0.5)
|
|
@@ -66,6 +68,7 @@ def test_ideal_bandpass_matches_difference():
|
|
|
66
68
|
# Gaussian filters
|
|
67
69
|
# -------------------------------------------------------------------------
|
|
68
70
|
|
|
71
|
+
|
|
69
72
|
def test_gaussian_hits_target_at_fc():
|
|
70
73
|
fc = 0.3
|
|
71
74
|
dB = -3.01029995664
|
|
@@ -97,6 +100,7 @@ def test_gaussian_high_and_bandpass_rel():
|
|
|
97
100
|
# Butterworth filters
|
|
98
101
|
# -------------------------------------------------------------------------
|
|
99
102
|
|
|
103
|
+
|
|
100
104
|
@pytest.mark.parametrize("order", [1, 2, 4, 8])
|
|
101
105
|
def test_butterworth_half_power(order):
|
|
102
106
|
fc = 0.4
|
|
@@ -135,6 +139,7 @@ def test_butterworth_rel_high_and_bandpass():
|
|
|
135
139
|
# Error conditions
|
|
136
140
|
# -------------------------------------------------------------------------
|
|
137
141
|
|
|
142
|
+
|
|
138
143
|
def test_invalid_params():
|
|
139
144
|
f = grid()
|
|
140
145
|
with pytest.raises(ValueError):
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"""Regression tests: SpharaFilter must apply the transfer function H, not H**2.
|
|
2
|
+
|
|
3
|
+
A SPHARA filter with transfer function H maps the k-th basis vector
|
|
4
|
+
psi_k to H[k] * psi_k. Data are stored row-wise (samples x channels), so
|
|
5
|
+
for mode 'fem' the filter matrix is F = B Psi diag(H) Psi^T and for the
|
|
6
|
+
modes 'unit' and 'inv_euclidean' it is F = Psi diag(H) Psi^T.
|
|
7
|
+
|
|
8
|
+
Up to and including SpharaPy 1.3.0 the filter matrix was built as
|
|
9
|
+
B Psi diag(H)^2 Psi^T, i.e. graded transfer functions were applied
|
|
10
|
+
squared. Ideal (0/1) filters were not affected because H**2 == H there.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
import numpy as np
|
|
14
|
+
import pytest
|
|
15
|
+
from scipy.spatial import Delaunay
|
|
16
|
+
|
|
17
|
+
from spharapy import spectral_filters as spf
|
|
18
|
+
from spharapy import spharabasis as sb
|
|
19
|
+
from spharapy import spharafilter as sf
|
|
20
|
+
from spharapy import spharatransform as st
|
|
21
|
+
from spharapy import trimesh as tm
|
|
22
|
+
|
|
23
|
+
MODES = ["fem", "unit", "inv_euclidean"]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _hemisphere_mesh(n_inner=120, n_boundary=30):
|
|
27
|
+
"""Triangulated unit hemisphere (Fibonacci points plus a boundary ring)."""
|
|
28
|
+
golden = (1 + 5**0.5) / 2
|
|
29
|
+
i = np.arange(n_inner)
|
|
30
|
+
z = 1 - (i + 0.5) / n_inner * 0.98
|
|
31
|
+
r = np.sqrt(1 - z**2)
|
|
32
|
+
theta = 2 * np.pi * i / golden
|
|
33
|
+
inner = np.c_[r * np.cos(theta), r * np.sin(theta), z]
|
|
34
|
+
phi = 2 * np.pi * np.arange(n_boundary) / n_boundary
|
|
35
|
+
ring = np.c_[np.cos(phi), np.sin(phi), np.zeros(n_boundary)]
|
|
36
|
+
points = np.vstack([inner, ring])
|
|
37
|
+
# azimuthal equidistant projection keeps the triangulation manifold
|
|
38
|
+
colat = np.arccos(np.clip(points[:, 2], -1, 1))
|
|
39
|
+
azim = np.arctan2(points[:, 1], points[:, 0])
|
|
40
|
+
rho = 2 * np.sin(colat / 2)
|
|
41
|
+
triangles = Delaunay(np.c_[rho * np.cos(azim), rho * np.sin(azim)]).simplices
|
|
42
|
+
return tm.TriMesh(triangles.tolist(), points.tolist()), points.shape[0]
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
@pytest.fixture(scope="module")
|
|
46
|
+
def hemisphere():
|
|
47
|
+
return _hemisphere_mesh()
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
@pytest.mark.parametrize("mode", MODES)
|
|
51
|
+
def test_filter_applies_transfer_function(hemisphere, mode):
|
|
52
|
+
"""Filtering psi_k yields H_k psi_k (and not H_k**2 psi_k)."""
|
|
53
|
+
mesh, n = hemisphere
|
|
54
|
+
basis, _ = sb.SpharaBasis(mesh, mode=mode).basis()
|
|
55
|
+
H = np.exp(-np.linspace(0.0, 3.0, n)) # graded weights
|
|
56
|
+
flt = sf.SpharaFilter(mesh, mode=mode, specification=H)
|
|
57
|
+
out = flt.filter(basis.T) # row k = filtered psi_k
|
|
58
|
+
np.testing.assert_allclose(out, (basis * H).T, atol=1e-10)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
@pytest.mark.parametrize("mode", MODES)
|
|
62
|
+
def test_binary_specification_is_projection(hemisphere, mode):
|
|
63
|
+
"""Ideal filters: applying the filter twice changes nothing."""
|
|
64
|
+
mesh, n = hemisphere
|
|
65
|
+
rng = np.random.default_rng(0)
|
|
66
|
+
data = rng.standard_normal((5, n))
|
|
67
|
+
flt = sf.SpharaFilter(mesh, mode=mode, specification=20)
|
|
68
|
+
once = flt.filter(data)
|
|
69
|
+
twice = flt.filter(once)
|
|
70
|
+
np.testing.assert_allclose(twice, once, atol=1e-10)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
@pytest.mark.parametrize("mode", MODES)
|
|
74
|
+
def test_filter_matches_transform_weighting_synthesis(hemisphere, mode):
|
|
75
|
+
"""filter() equals analysis -> multiplication by H -> synthesis."""
|
|
76
|
+
mesh, n = hemisphere
|
|
77
|
+
rng = np.random.default_rng(1)
|
|
78
|
+
data = rng.standard_normal((7, n))
|
|
79
|
+
H = np.linspace(1.0, 0.0, n) ** 3
|
|
80
|
+
flt = sf.SpharaFilter(mesh, mode=mode, specification=H)
|
|
81
|
+
trafo = st.SpharaTransform(mesh, mode=mode)
|
|
82
|
+
expected = trafo.synthesis(trafo.analysis(data) * H)
|
|
83
|
+
np.testing.assert_allclose(flt.filter(data), expected, atol=1e-10)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
@pytest.mark.parametrize("mode", MODES)
|
|
87
|
+
def test_lowpass_preserves_constant_field(hemisphere, mode):
|
|
88
|
+
"""A low-pass with H_0 = 1 leaves a constant field unchanged."""
|
|
89
|
+
mesh, n = hemisphere
|
|
90
|
+
H = np.exp(-np.linspace(0.0, 5.0, n))
|
|
91
|
+
assert H[0] == 1.0
|
|
92
|
+
flt = sf.SpharaFilter(mesh, mode=mode, specification=H)
|
|
93
|
+
const = np.full((1, n), 3.7)
|
|
94
|
+
np.testing.assert_allclose(flt.filter(const), const, atol=1e-10)
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
@pytest.mark.parametrize("design", ["gaussian", "butterworth"])
|
|
98
|
+
def test_attenuation_at_cutoff_matches_specification(hemisphere, design):
|
|
99
|
+
"""End to end: a basis function at f = f_c is attenuated by exactly dB."""
|
|
100
|
+
mesh, n = hemisphere
|
|
101
|
+
flt = sf.SpharaFilter(mesh, mode="fem")
|
|
102
|
+
basis, _ = flt.basis()
|
|
103
|
+
freqs = flt.spatial_frequencies()
|
|
104
|
+
k = n // 4
|
|
105
|
+
fc = float(freqs[k])
|
|
106
|
+
dB = -20 * np.log10(np.sqrt(2.0)) # half power
|
|
107
|
+
if design == "gaussian":
|
|
108
|
+
H = spf.transfer_func_gaussian_lowpass(freqs, fc=fc, dB=dB)
|
|
109
|
+
else:
|
|
110
|
+
H = spf.transfer_func_butterworth_lowpass(freqs, fc=fc, order=4, dB=dB)
|
|
111
|
+
flt.specification = H
|
|
112
|
+
out = flt.filter(basis[:, k])
|
|
113
|
+
gain = np.linalg.norm(out) / np.linalg.norm(basis[:, k])
|
|
114
|
+
np.testing.assert_allclose(20 * np.log10(gain), dB, atol=1e-8)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
@pytest.mark.parametrize("mode", MODES)
|
|
118
|
+
def test_changing_specification_invalidates_cached_filter(hemisphere, mode):
|
|
119
|
+
"""A new specification must take effect after filter() was called."""
|
|
120
|
+
mesh, n = hemisphere
|
|
121
|
+
basis, _ = sb.SpharaBasis(mesh, mode=mode).basis()
|
|
122
|
+
flt = sf.SpharaFilter(mesh, mode=mode, specification=0) # all-pass
|
|
123
|
+
np.testing.assert_allclose(flt.filter(basis.T), basis.T, atol=1e-10)
|
|
124
|
+
H = np.linspace(1.0, 0.0, n)
|
|
125
|
+
flt.specification = H
|
|
126
|
+
np.testing.assert_allclose(flt.filter(basis.T), (basis * H).T, atol=1e-10)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def test_changing_mode_invalidates_cached_filter(hemisphere):
|
|
130
|
+
"""Switching the discretisation must rebuild the filter matrix."""
|
|
131
|
+
mesh, n = hemisphere
|
|
132
|
+
H = np.exp(-np.linspace(0.0, 3.0, n))
|
|
133
|
+
flt = sf.SpharaFilter(mesh, mode="fem", specification=H)
|
|
134
|
+
flt.filter(np.ones(n))
|
|
135
|
+
flt.mode = "unit"
|
|
136
|
+
basis, _ = sb.SpharaBasis(mesh, mode="unit").basis()
|
|
137
|
+
np.testing.assert_allclose(flt.filter(basis.T), (basis * H).T, atol=1e-10)
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def test_spectral_grid_is_a_method(hemisphere):
|
|
141
|
+
mesh, _ = hemisphere
|
|
142
|
+
flt = sf.SpharaFilter(mesh, mode="fem")
|
|
143
|
+
np.testing.assert_array_equal(flt.spectral_grid(), flt.eigenvalues())
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|