Skip to content

Commit 42c0425

Browse files
committed
principal_components now also accepts a GaussianStats object.
1 parent 24681a1 commit 42c0425

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

spectral/algorithms/algorithms.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ def principal_components(image):
448448
449449
Arguments:
450450
451-
`image` (ndarray or :class:`spectral.Image`):
451+
`image` (ndarray, :class:`spectral.Image`, :class:`GaussianStats`):
452452
453453
An `MxNxB` image
454454
@@ -478,9 +478,10 @@ def principal_components(image):
478478
'''
479479
from numpy import sqrt, sum
480480

481-
(M, N, B) = image.shape
482-
483-
stats = calc_stats(image)
481+
if isinstance(image, GaussianStats):
482+
stats = image
483+
else:
484+
stats = calc_stats(image)
484485

485486
(L, V) = numpy.linalg.eig(stats.cov)
486487

spectral/tests/dimensionality.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ def test_ppi_centered(self):
8888
p2 = spy.ppi(data_centered, 4)
8989
np.all(p == p2)
9090

91+
def test_pca_runs(self):
92+
'''Should be able to compute PCs and transform data.'''
93+
data = self.data
94+
xdata = spy.principal_components(data).transform(data)
95+
96+
def test_pca_runs_from_stats(self):
97+
'''Should be able to pass image stats to PCA function.'''
98+
data = self.data
99+
stats = spy.calc_stats(data)
100+
xdata = spy.principal_components(stats).transform(data)
101+
91102
def run():
92103
print('\n' + '-' * 72)
93104
print('Running dimensionality tests.')

0 commit comments

Comments
 (0)