Graph Signal Processing Utilities
The GSP module provides specialized tools for spectral analysis on graphs. This includes functions for computing graph spectral bases (eigenmodes), performing graph Fourier transforms (encoding), and inverse transforms (decoding).
spectranorm.utils.gsp
utils/gsp.py
Graph Signal Processing (GSP) functions for the Spectranorm package.
EigenmodeBasis
dataclass
Data class to hold eigenmode basis information.
Attributes:
| Name | Type | Description |
|---|---|---|
eigenvalues |
NDArray[floating[Any]]
|
np.ndarray Eigenvalues of the Basis (n_modes,). |
eigenvectors |
NDArray[floating[Any]]
|
np.ndarray
Eigenvectors corresponding to the eigenvalues (n_features, n_modes).
This is the matrix :math: |
mass_matrix |
NDArray[floating[Any]] | csr_matrix | None
|
np.ndarray | sparse.csr_matrix | None
Mass matrix associated with the eigenmodes (optional). This can be used
in generalized eigenvalue problems (e.g. random walk Laplacian), in which
case the eigenmodes satisfy :math: |
Source code in src/spectranorm/utils/gsp.py
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 | |
__repr__() -> str
decode(encoded_signals: npt.NDArray[np.floating[Any]], n_modes: int | None = None) -> npt.NDArray[np.floating[Any]]
Decode encoded signals :math:ilde{x} using the eigenmode basis.
Given an eigenmode set :math:\Psi where :math:L = \Psi \Lambda \Psi^{-1}
and a list of encoded signals :math:ilde{x}, the decoded signals
:math:\hat{x} are given by:
.. math:: \hat{x} = \Psi \tilde{x}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encoded_signals
|
NDArray[floating[Any]]
|
np.ndarray Encoded signals in the eigenmode basis (n_signals, n_modes). |
required |
n_modes
|
int | None
|
int | None Number of modes used for decoding. If None, use all available modes. This should not exceed the number of modes in the encoded_signals nor the number of modes in the eigenmode basis. |
None
|
Returns:
| Type | Description |
|---|---|
NDArray[floating[Any]]
|
np.ndarray Decoded signal in the original feature space (n_signals, n_features). |
Source code in src/spectranorm/utils/gsp.py
encode(signals: npt.NDArray[np.floating[Any]], n_modes: int | None = None) -> npt.NDArray[np.floating[Any]]
Encode a signal using the eigenmode basis.
Given an eigenmode set :math:\Psi where :math:L = \Psi \Lambda \Psi^{-1}
and a list of signals :math:x, the encoded signals :math:ilde{x} are given
by:
.. math:: \tilde{x} = \Psi^{-1} x = \Psi^T M x
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signals
|
NDArray[floating[Any]]
|
np.ndarray Signals to encode (n_signals, n_features). |
required |
n_modes
|
int | None
|
int | None Number of modes to use for encoding. If None, use all available modes. |
None
|
Returns:
| Type | Description |
|---|---|
NDArray[floating[Any]]
|
np.ndarray Encoded signal in the eigenmode basis (n_signals, n_modes). |
Source code in src/spectranorm/utils/gsp.py
inverted_eigenvectors() -> npt.NDArray[np.floating[Any]]
Compute the inverse of the eigenvector matrix.
.. math:: \Psi^{-1} = \Psi^T M
Returns:
| Type | Description |
|---|---|
NDArray[floating[Any]]
|
np.ndarray Inverse of the eigenvector matrix (n_modes, n_features). |
Source code in src/spectranorm/utils/gsp.py
load(filepath: str, mmap_mode: MmapMode | None = 'r') -> EigenmodeBasis
classmethod
Load an EigenmodeBasis instance from a joblib file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
str Path to the joblib file. |
required |
mmap_mode
|
MmapMode | None
|
MmapMode | None Memory mapping mode for joblib (default: "r"). You can set this to None to disable memory-mapping. |
'r'
|
Returns:
| Type | Description |
|---|---|
EigenmodeBasis
|
EigenmodeBasis instance |
Source code in src/spectranorm/utils/gsp.py
reduce(n_modes: int, *, inplace: bool = True) -> EigenmodeBasis
Reduce the EigenmodeBasis to only contain the first n_modes.
This method is useful to reduce the size of the basis for efficiency (e.g., to remove less important modes/degenerate modes before further processing).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_modes
|
int
|
int Number of modes to retain. This must be less than or equal to the current number of modes. |
required |
inplace
|
bool
|
bool Whether to modify the current instance or return a new one. By default, this is True (modify in place, optimizing memory usage). |
True
|
Returns:
| Type | Description |
|---|---|
EigenmodeBasis
|
EigenmodeBasis A new EigenmodeBasis instance with reduced modes. |
Source code in src/spectranorm/utils/gsp.py
save(filepath: str, compress: int = 0, *, overwrite: bool = False) -> None
Save the EigenmodeBasis instance to a joblib file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
str Path to save the joblib file. |
required |
compress
|
int
|
int Compression level for joblib (default: 0). Note: Compression is disabled by default to enable support for memory-mapped arrays. However, if you do not need memory-mapping, and want to reduce file size, you can enable compression by setting the compress parameter to a positive integer. |
0
|
overwrite
|
bool
|
bool Whether to overwrite an existing file (default: False). |
False
|
Source code in src/spectranorm/utils/gsp.py
compute_random_walk_laplacian_eigenmodes(adjacency_matrix: sparse.csr_matrix | npt.NDArray[np.floating[Any]], num_eigenvalues: int = 100) -> tuple[npt.NDArray[np.floating[Any]], npt.NDArray[np.floating[Any]], npt.NDArray[np.floating[Any]]]
Compute the eigenvalues of the random walk Laplacian.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adjacency_matrix
|
csr_matrix | NDArray[floating[Any]]
|
sparse.csr_matrix The adjacency matrix of the graph. |
required |
num_eigenvalues
|
int
|
int Number of eigenvalues to compute. |
100
|
Returns:
| Type | Description |
|---|---|
tuple[NDArray[floating[Any]], NDArray[floating[Any]], NDArray[floating[Any]]]
|
eigenvalues, eigenvectors: (np.ndarray, np.ndarray) Eigenvalues and eigenvectors of the random walk Laplacian. |
Source code in src/spectranorm/utils/gsp.py
compute_symmetric_normalized_laplacian_eigenmodes(adjacency_matrix: sparse.spmatrix | npt.NDArray[np.floating[Any]], num_eigenvalues: int = 100) -> tuple[npt.NDArray[np.floating[Any]], npt.NDArray[np.floating[Any]]]
Compute the eigenvalues of the symmetric normalized Laplacian.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adjacency_matrix
|
spmatrix | NDArray[floating[Any]]
|
sparse.spmatrix The adjacency matrix of the graph. |
required |
num_eigenvalues
|
int
|
int Number of eigenvalues to compute. |
100
|
Returns:
| Type | Description |
|---|---|
tuple[NDArray[floating[Any]], NDArray[floating[Any]]]
|
eigenvalues, eigenvectors: (np.ndarray, np.ndarray) Eigenvalues and eigenvectors of the symmetric normalized Laplacian. |
Source code in src/spectranorm/utils/gsp.py
compute_symmetric_normalized_laplacian_eigenmodes_amg_lobpcg(adjacency_matrix: sparse.csr_matrix | npt.NDArray[np.floating[Any]], num_eigenvalues: int = 100, amg_cycles: int = 1) -> tuple[npt.NDArray[np.floating[Any]], npt.NDArray[np.floating[Any]], npt.NDArray[np.floating[Any]]]
Compute the eigenvalues and eigenvectors of the Symmetric Normalized
Laplacian (:math:L_{ ext{sym}} = I - D^{-1/2} A D^{-1/2}) using LOBPCG
with Algebraic Multigrid (AMG) preconditioning.
Note: this is a more advanced and efficient method for large graphs compared
to using scipy.sparse.linalg.eigsh.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adjacency_matrix
|
csr_matrix | NDArray[floating[Any]]
|
The adjacency matrix of the graph. |
required |
num_eigenvalues
|
int
|
Number of eigenvalues (modes) to compute. |
100
|
amg_cycles
|
int
|
Number of V-cycles to use in the AMG preconditioner solve. |
1
|
Returns:
| Type | Description |
|---|---|
tuple[NDArray[floating[Any]], NDArray[floating[Any]], NDArray[floating[Any]]]
|
eigenvalues, eigenvectors, degrees: (np.ndarray, np.ndarray, np.ndarray) Eigenvalues and eigenvectors of L_sym, and the node degrees. |
Source code in src/spectranorm/utils/gsp.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | |
convert_adjacency_to_transition_matrix(adjacency_matrix: sparse.spmatrix) -> sparse.csr_matrix
Convert an adjacency matrix to a transition matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adjacency_matrix
|
spmatrix
|
sparse.spmatrix The adjacency matrix of the graph. |
required |
Returns:
| Type | Description |
|---|---|
csr_matrix
|
sparse.spmatrix Transition matrix. |
Source code in src/spectranorm/utils/gsp.py
make_csr_matrix(matrix: sparse.spmatrix | npt.NDArray[np.floating[Any]]) -> sparse.csr_matrix
Ensure the input matrix is in CSR format.
Source code in src/spectranorm/utils/gsp.py
perform_symmetric_normalization(adjacency_matrix: sparse.spmatrix) -> sparse.csr_matrix
Perform symmetric normalization on the adjacency matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adjacency_matrix
|
spmatrix
|
sparse.spmatrix The adjacency matrix of the graph. |
required |
Returns:
| Type | Description |
|---|---|
csr_matrix
|
sparse.spmatrix Symmetrically normalized adjacency matrix. |