def test_matrix_sqrt_eigs(self): import spectral as spy from spectral.algorithms.spymath import matrix_sqrt stats = spy.calc_stats(self.data) (evals, evecs) = np.linalg.eig(stats.cov) S = matrix_sqrt(eigs=(evals, evecs)) assert_allclose(S.dot(S), self.C, atol=1e-8)
def set_background(self, stats): '''Sets background statistics to be used when applying the detector.''' from spectral.algorithms.spymath import matrix_sqrt self.background = stats self.u_b = stats.mean # Matrix square root of inverse of cov: C**(-1/2) self.C_1_2 = matrix_sqrt(np.linalg.inv(stats.cov))
def whiten(self, X): '''Transforms data to the whitened space of the background. Arguments: `X` (ndarray): Size (M,N,K) or (M*N,K) array of length K vectors to transform. Returns an array of same size as `X` but linearly transformed to the whitened space of the filter. ''' import math from spectral.algorithms.transforms import LinearTransform from spectral.algorithms.spymath import matrix_sqrt if self._whitening_transform is None: A = math.sqrt(self.coef) * matrix_sqrt(self.C_1, True) self._whitening_transform = LinearTransform(A, pre=-self.u_b) return self._whitening_transform(X)
def get_whitening_transform(self): '''Returns transform that centers and whitens data for these stats.''' from spectral.algorithms.transforms import LinearTransform from spectral.algorithms.spymath import matrix_sqrt C_1 = np.linalg.inv(self.cov) return LinearTransform(matrix_sqrt(C_1, True), pre=-self.mean)
def test_matrix_sqrt_eigs(self): stats = spy.calc_stats(self.data) (evals, evecs) = np.linalg.eig(stats.cov) S = matrix_sqrt(eigs=(evals, evecs)) assert_allclose(S.dot(S), self.C, atol=1e-8)
def test_matrix_sqrt_sym_inv(self): S = matrix_sqrt(self.C, symmetric=True, inverse=True) assert_allclose(S.dot(S), np.linalg.inv(self.C), atol=1e-8)
def test_matrix_sqrt_sym(self): S = matrix_sqrt(self.C, symmetric=True) assert_allclose(S.dot(S), self.C, atol=1e-8)
def test_matrix_sqrt_inv(self): S = matrix_sqrt(self.X, inverse=True) assert_allclose(S.dot(S), np.linalg.inv(self.X))
def test_matrix_sqrt(self): S = matrix_sqrt(self.X) assert_allclose(S.dot(S), self.X)
def test_matrix_sqrt_sym_inv(self): from spectral.algorithms.spymath import matrix_sqrt S = matrix_sqrt(self.C, symmetric=True, inverse=True) assert_allclose(S.dot(S), np.linalg.inv(self.C), atol=1e-8)
def test_matrix_sqrt_sym(self): from spectral.algorithms.spymath import matrix_sqrt S = matrix_sqrt(self.C, symmetric=True) assert_allclose(S.dot(S), self.C, atol=1e-8)
def test_matrix_sqrt_inv(self): from spectral.algorithms.spymath import matrix_sqrt S = matrix_sqrt(self.X, inverse=True) assert_allclose(S.dot(S), np.linalg.inv(self.X))
def test_matrix_sqrt(self): from spectral.algorithms.spymath import matrix_sqrt S = matrix_sqrt(self.X) assert_allclose(S.dot(S), self.X)