-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path_sorted_schur.py
409 lines (345 loc) · 14.3 KB
/
_sorted_schur.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
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
# This file is part of pyGPCCA.
#
# Copyright (c) 2020 Bernhard Reuter.
# With contributions of Marius Lange, Michal Klein and Alexander Sikorski.
# Based on the original MATLAB GPCCA code authored by Bernhard Reuter, Susanna Roeblitz and Marcus Weber,
# Zuse Institute Berlin, Takustrasse 7, 14195 Berlin
# We like to thank A. Sikorski and M. Weber for pointing us to SLEPc for partial Schur decompositions of
# sparse matrices.
# Further parts of sorted_krylov_schur were developed based on the function krylov_schur
# https://github.com/zib-cmd/cmdtools/blob/1c6b6d8e1c35bb487fcf247c5c1c622b4b665b0a/src/cmdtools/analysis/pcca.py#L64,
# written by Alexander Sikorski.
# --------------------------------------------------------------------------------------------------------------------
# If you use this code or parts of it, cite the following reference:
# --------------------------------------------------------------------------------------------------------------------
# Bernhard Reuter, Konstantin Fackeldey, and Marcus Weber,
# Generalized Markov modeling of nonreversible molecular kinetics,
# The Journal of Chemical Physics, 150(17):174103, 2019.
# https://doi.org/10.1063/1.5064530
# --------------------------------------------------------------------------------------------------------------------
# pyGPCCA is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser
# General Public License as published by the Free Software Foundation, either version 3 of the License,
# or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along with this program.
# If not, see <http://www.gnu.org/licenses/>.
# --------------------------------------------------------------------------------------------------------------------
from typing import Tuple, Union, Literal
import sys
if not sys.warnoptions:
import os
import warnings
warnings.simplefilter("always", category=UserWarning) # Change the filter in this process
os.environ["PYTHONWARNINGS"] = "always::UserWarning" # Also affect subprocesses
from scipy.linalg import schur, rsf2csf, subspace_angles
from scipy.sparse import issparse, spmatrix, csr_matrix, isspmatrix_csr
import numpy as np
from pygpcca._types import ArrayLike
from pygpcca.utils._docs import d
from pygpcca.utils._checks import assert_petsc_real_scalar_type
from pygpcca._sort_real_schur import sort_real_schur
from pygpcca.utils._constants import EPS, DEFAULT_SCHUR_METHOD, NO_PETSC_SLEPC_FOUND_MSG
try:
import petsc4py
import slepc4py
assert_petsc_real_scalar_type()
except (ImportError, TypeError):
petsc4py = slepc4py = None
__all__ = ["sorted_schur"]
def _initialize_matrix(M: "petsc4py.PETSc.Mat", P: Union[ArrayLike, spmatrix]) -> None:
"""
Initialize PETSc matrix.
Parameters
----------
M
:mod:`petsc4py` matrix to initialize.
P
:mod:`numpy` array or :mod:`scipy` sparse matrix from which we take the data.
Returns
-------
Nothing, just initializes `M`. If `P` is an :class:`numpy.ndarray`,
`M` will also be dense. If `P` is a :class:`scipy.sparse.spmatrix`,
`M` will become a CSR matrix regardless of `P`'s sparse format.
"""
if issparse(P):
if not isspmatrix_csr(P):
warnings.warn("Only CSR sparse matrices are supported, converting.", stacklevel=2)
P = csr_matrix(P)
M.createAIJ(size=P.shape, csr=(P.indptr, P.indices, P.data)) # type: ignore[union-attr]
else:
M.createDense(list(np.shape(P)), array=P)
@d.dedent
def _check_conj_split(eigenvalues: ArrayLike) -> bool:
"""
Check whether using m eigenvalues cuts through a block of complex conjugates.
If the last (`m`th) eigenvalue is not real, check whether it
forms a complex conjugate pair with the second-last eigenvalue.
If that is not the case, then choosing `m` clusters would cut through a
block of complex conjugates.
Parameters
----------
eigenvalues
%(eigenvalues_m)s
Returns
-------
``True`` if a block of complex conjugate eigenvalues is split, ``False`` otherwise.
"""
last_eigenvalue, second_last_eigenvalue = eigenvalues[-1], eigenvalues[-2]
splits_block = False
if last_eigenvalue.imag > EPS:
splits_block = not np.isclose(last_eigenvalue, np.conj(second_last_eigenvalue))
return splits_block
@d.dedent
def _check_schur(P: ArrayLike, Q: ArrayLike, R: ArrayLike, eigenvalues: ArrayLike, method: str) -> None:
"""
Run a number of checks on the sorted Schur decomposition.
Parameters
----------
%(P)s
Q
%(Q_sort)s
R
%(R_sort)s
eigenvalues
%(eigenvalues_m)s
%(method)s
Returns
-------
Nothing.
"""
m = len(eigenvalues)
# check the dimensions
if Q.shape[1] != len(eigenvalues):
raise ValueError(f"Number of Schur vectors does not match number of eigenvalues for `method={method!r}`.")
if R.shape[0] != R.shape[1]:
raise ValueError(f"R is not rectangular for `method={method!r}`.")
if P.shape[0] != Q.shape[0]:
raise ValueError(f"First dimension in P does not match first dimension in Q for `method={method!r}`.")
if R.shape[0] != Q.shape[1]:
raise ValueError(f"First dimension in R does not match second dimension in Q for `method={method!r}`.")
# check whether things are real
if not np.all(np.isreal(Q)):
raise TypeError(
f"The orthonormal basis of the subspace returned by `method={method!r}` is not real. "
"G-PCCA needs real basis vectors to work."
)
dummy = np.dot(P, csr_matrix(Q) if issparse(P) else Q)
if issparse(dummy):
dummy = dummy.toarray()
dummy1 = np.dot(Q, np.diag(eigenvalues))
# dummy2 = np.concatenate((dummy, dummy1), axis=1)
dummy3 = subspace_angles(dummy, dummy1)
# test1 = ( ( matrix_rank(dummy2) - matrix_rank(dummy) ) == 0 )
test2 = np.allclose(dummy3, 0.0, atol=1e-8, rtol=1e-5)
test3 = dummy3.shape[0] == m
dummy4 = subspace_angles(dummy, Q)
test4 = np.allclose(dummy4, 0.0, atol=1e-6, rtol=1e-5)
if not test4:
raise ValueError(
f"According to `scipy.linalg.subspace_angles()`, `{method}` didn't "
f"return an invariant subspace of P. The subspace angles are: `{dummy4}`."
)
if not test2:
warnings.warn(
f"According to `scipy.linalg.subspace_angles()`, `{method}` didn't "
f"return the invariant subspace associated with the top k eigenvalues, "
f"since the subspace angles between the column spaces of P*Q and Q*L "
f"aren't near zero (L is a diagonal matrix with the "
f"sorted top eigenvalues on the diagonal). The subspace angles are: `{dummy3}`.",
stacklevel=2,
)
if not test3:
warnings.warn(
f"According to `scipy.linalg.subspace_angles()`, the dimension of the "
f"column space of P*Q and/or Q*L is not equal to m (L is a diagonal "
f"matrix with the sorted top eigenvalues on the diagonal), method=`{method}`.",
stacklevel=2,
)
@d.dedent
def sorted_krylov_schur(
P: Union[spmatrix, ArrayLike], k: int, z: Literal["LM", "LR"] = "LM", tol: float = 1e-16
) -> Tuple[ArrayLike, ArrayLike, ArrayLike, ArrayLike]:
r"""
Calculate an orthonormal basis of the subspace associated with the `k`
dominant eigenvalues of `P` using the Krylov-Schur method as implemented in SLEPc.
This functions requires :mod:`petsc4py` and :mod:`slepc4py`.
Parameters
----------
%(P)s
%(k)s
%(z)s
tol
Convergence criterion used by SLEPc internally. If you are dealing
with ill-conditioned matrices, consider decreasing this value to
get accurate results.
Returns
-------
Tuple of the following:
R
%(R_sort)s
Q
%(Q_sort)s
eigenvalues
%(eigenvalues_k)s
eigenvalues_error
Array of shape `(k,)` containing the error, based on the residual
norm, of the `i`th eigenpair at index `i`.
""" # noqa: D205, D400
# We like to thank A. Sikorski and M. Weber for pointing us to SLEPc for partial Schur decompositions of
# sparse matrices.
# Further parts of sorted_krylov_schur were developed based on the function krylov_schur
# https://github.com/zib-cmd/cmdtools/blob/1c6b6d8e1c35bb487fcf247c5c1c622b4b665b0a/src/cmdtools/analysis/pcca.py#L64,
# written by Alexander Sikorski.
from petsc4py import PETSc
from slepc4py import SLEPc
M = PETSc.Mat().create()
_initialize_matrix(M, P)
# Creates EPS object.
E = SLEPc.EPS()
E.create()
# Set the matrix associated with the eigenvalue problem.
E.setOperators(M)
# Select the particular solver to be used in the EPS object: Krylov-Schur
E.setType(SLEPc.EPS.Type.KRYLOVSCHUR)
# Set the number of eigenvalues to compute and the dimension of the subspace.
E.setDimensions(nev=k)
# set the tolerance used in the convergence criterion
E.setTolerances(tol=tol)
# Specify which portion of the spectrum is to be sought.
# LARGEST_MAGNITUDE: Largest magnitude (default).
# LARGEST_REAL: Largest real parts.
# All possible Options can be found here:
# (see: https://slepc.upv.es/slepc4py-current/docs/apiref/slepc4py.SLEPc.EPS.Which-class.html)
if z == "LM":
E.setWhichEigenpairs(E.Which.LARGEST_MAGNITUDE)
elif z == "LR":
E.setWhichEigenpairs(E.Which.LARGEST_REAL)
else:
raise ValueError(f"Invalid spectrum sorting options `{z}`. Valid options are: 'LM', 'LR'.")
# Solve the eigensystem.
E.solve()
# getInvariantSubspace() gets an orthonormal basis of the computed invariant subspace.
# It returns a list of vectors.
# The returned real vectors span an invariant subspace associated with the computed eigenvalues.
# We take the sequence of 1-D arrays and stack them as columns to make a single 2-D array.
Q = np.column_stack([x.array for x in E.getInvariantSubspace()])
try:
# otherwise, R would be of shape `(k + 1, k)`
E.getDS().setExtraRow(False)
except AttributeError:
pass
# Get the schur form
ds = E.getDS()
A = ds.getMat(SLEPc.DS.MatType.A)
R = A.getDenseArray().astype(np.float64)
ds.restoreMat(SLEPc.DS.MatType.A, A)
# Gets the number of converged eigenpairs.
nconv = E.getConverged()
# Warn, if nconv smaller than k.
if nconv < k:
warnings.warn(f"The number of converged eigenpairs is `{nconv}`, but `{k}` were requested.", stacklevel=2)
# Collect the k dominant eigenvalues.
eigenvalues = []
eigenvalues_error = []
for i in range(nconv):
# Get the i-th eigenvalue as computed by solve().
eigenval = E.getEigenvalue(i)
eigenvalues.append(eigenval)
# Computes the error (based on the residual norm) associated with the i-th computed eigenpair.
eigenval_error = E.computeError(i)
eigenvalues_error.append(eigenval_error)
# convert lists with eigenvalues and errors to arrays (while keeping excess eigenvalues and errors)
eigenvalues = np.asarray(eigenvalues) # type: ignore[assignment]
eigenvalues_error = np.asarray(eigenvalues_error) # type: ignore[assignment]
return R, Q, eigenvalues, eigenvalues_error # type: ignore[return-value]
@d.dedent
def sorted_brandts_schur(P: ArrayLike, k: int, z: Literal["LM", "LR"] = "LM") -> Tuple[ArrayLike, ArrayLike, ArrayLike]:
"""
Compute a sorted Schur decomposition.
This function uses :mod:`scipy` for the decomposition and Brandts'
method (see [Brandts02]_) for the sorting.
Parameters
----------
%(P)s
%(k)s
%(z)s
Returns
-------
Tuple of the following:
R
%(R_sort)s
Q
%(Q_sort)s
eigenvalues
%(eigenvalues_k)s
"""
# Make a Schur decomposition of P.
R, Q = schur(P, output="real")
# Sort the Schur matrix and vectors.
Q, R, ap = sort_real_schur(Q, R, z=z, b=k)
# Warnings
if np.any(np.array(ap) > 1.0):
warnings.warn("Reordering of Schur matrix was inaccurate.", stacklevel=2)
# compute eigenvalues
T, _ = rsf2csf(R, Q)
eigenvalues = np.diag(T)[:k]
return R, Q, eigenvalues
@d.dedent
def sorted_schur(
P: Union[ArrayLike, spmatrix],
m: int,
z: Literal["LM", "LR"] = "LM",
method: str = DEFAULT_SCHUR_METHOD,
tol_krylov: float = 1e-16,
) -> Tuple[ArrayLike, ArrayLike, ArrayLike]:
"""
Return ``m`` dominant real Schur vectors or an orthonormal basis spanning the same invariant subspace.
Parameters
----------
%(P)s
%(m)s
%(z)s
%(method)s
%(tol_krylov)s
Returns
-------
Tuple of the following:
R
%(R_sort)s
Q
%(Q_sort)s
eigenvalues
%(eigenvalues_m)s
"""
if method == "krylov":
if petsc4py is None or slepc4py is None:
method = DEFAULT_SCHUR_METHOD
warnings.warn(NO_PETSC_SLEPC_FOUND_MSG, stacklevel=2)
if method != "krylov" and issparse(P):
raise ValueError("Sparse implementation is only available for `method='krylov'`.")
# make sure we have enough eigenvalues to check for block splitting
n = P.shape[0]
if m > n:
raise ValueError(f"Requested more groups than states: {m} > {n}.")
# compute the sorted schur decomposition
if method == "brandts":
R, Q, eigenvalues = sorted_brandts_schur(P=P, k=m, z=z)
elif method == "krylov":
R, Q, eigenvalues, _ = sorted_krylov_schur(P=P, k=m, z=z, tol=tol_krylov)
else:
raise ValueError(f"Unknown method `{method!r}`.")
# check for splitting pairs of complex conjugates
if m < n:
if _check_conj_split(eigenvalues[:m]):
raise ValueError(
f"Clustering into {m} clusters will split complex conjugate eigenvalues. "
"Request one cluster more or less."
)
Q, R, eigenvalues = Q[:, :m], R[:m, :m], eigenvalues[:m]
# check the returned schur decomposition
_check_schur(P=P, Q=Q, R=R, eigenvalues=eigenvalues, method=method)
return R, Q, eigenvalues