LOOS  v2.3.2
subspace.py
1 """
2 Functions related to subspace calculations...
3 """
4 
5 
6 import numpy
7 import math
8 
9 
10 def subspaceOverlap(A, B, nmodes=0):
11  """
12  Compute the subspace overlap between two sets of eigenvectors.
13  If the # of modes is left at 0, then the first 25% of modes are used.
14  A and B are numpy matrices with the eigenvectors stored in columns.
15 
16  >>> subspaceOverlap(b2ar_U, rhod_U, 50)
17  """
18 
19 
20  (am, an) = A.shape
21  (bm, bn) = B.shape
22  if am != bm:
23  raise RuntimeError('Eigenvectors must have the same number of rows')
24 
25  if nmodes == 0:
26  nmodes = int(0.25 * min(an, bn))
27 
28  U = A[:,0:nmodes]
29  V = B[:,0:nmodes]
30  D = numpy.dot(numpy.transpose(U), V)
31  D = numpy.multiply(D,D)
32  return(numpy.sum(D) / nmodes)
33 
34 
35 
36 def covarianceOverlap(ls, lU, rs, rU, nmodes = 0):
37  """
38  Computes the covariance overlap between two subspaces.
39  Requires two sets of eigenvalues and corresponding
40  eigenvector matrices (eigenvectors in the columns).
41 
42  Be sure that the eigenvalues are scaled appropriately
43  and that any zero modes are removed, such as when comparing
44  an ENM result with a PCA result.
45 
46  Leaving nmodes as 0 will use ALL modes in the overlap.
47 
48  >>> covarianceOverlap(b2ar_s, b2ar_U, rhod_s, rhod_U)
49  """
50  (lm, ln) = lU.shape
51  (rm, rn) = rU.shape
52 
53  if lm != rm :
54  raise RuntimeError('Eigenvectors must have the same number of rows')
55  if nmodes == 0:
56  nmodes = min(ln, rn)
57 
58  X = numpy.absolute(numpy.dot(numpy.transpose(rU[:, 0:nmodes]), lU[:, 0:nmodes]))
59 
60  m = ls.shape
61  if len(m) == 1:
62  ls=numpy.reshape(ls, (m[0], 1))
63  rs=numpy.reshape(rs, (m[0], 1))
64 
65  L = numpy.dot(rs[0:nmodes, :], numpy.transpose(ls[0:nmodes, :]))
66  X2 = numpy.multiply(X, X)
67  y = numpy.sum(numpy.multiply(numpy.sqrt(L), X2))
68 
69  e = numpy.sum(numpy.add(ls[0:nmodes], rs[0:nmodes]))
70 
71  num = e - 2.0 * y
72  co = 1.0 - math.sqrt( math.fabs(num) / e)
73 
74  return(co)
75 
76 
def covarianceOverlap(ls, lU, rs, rU, nmodes=0)
Definition: subspace.py:36
def subspaceOverlap(A, B, nmodes=0)
Definition: subspace.py:10