Skip to content

Commit

Permalink
add tests for improved coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Alan Weaver committed Feb 29, 2016
1 parent 1586865 commit ef4818f
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 9 deletions.
15 changes: 8 additions & 7 deletions pydl/pcomp.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class pcomp(object):
Parameters
----------
x : array-like
A 2-D array with N rows and M columns.
A 2-D array with :math:`N` rows and :math:`M` columns.
standardize : :class:`bool`, optional
If set to ``True``, the input data will have its mean subtracted off
and will be scaled to unit variance.
Expand Down Expand Up @@ -79,22 +79,23 @@ def coefficients(self):

@lazyproperty
def derived(self):
"""(:class:`~numpy.ndarray`) A :math:`N \times M` array containing
the derived variables."""
"""(:class:`~numpy.ndarray`) The derived variables.
"""
derived_data = np.dot(self._array, self.coefficients)
if self._standardize:
derived_data += self._xstd
return derived_data

@lazyproperty
def variance(self):
"""(:class:`~numpy.ndarray`) An array of the :math:`M` variances of
each derived variable."""
"""(:class:`~numpy.ndarray`) The variances of each derived variable.
"""
return self._evals/self._c.trace()

@lazyproperty
def eigenvalues(self):
"""(:class:`~numpy.ndarray`) An array of the :math:`M` eigenvalues
that correspond to the principal components.
"""(:class:`~numpy.ndarray`) The eigenvalues.
There is one eigenvalue for that correspond each the principal
component.
"""
return self._evals
2 changes: 1 addition & 1 deletion pydl/pydlspec2d/spec1d/spec_append.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def spec_append(spec1, spec2, pixshift=0):
Append `spec2` to `spec1`.
pixshift : :class:`int`, optional
If `pixshift` is set to a positive integer, `spec2` will be padded with
`pixshift` zeros on the left size. If `pixshift` is set to a
`pixshift` zeros on the left side. If `pixshift` is set to a
negative integer, `spec1` will be padded with ``abs(pixshift)`` zeros
on the left side. If not set, all zeros will be padded on the right
side.
Expand Down
38 changes: 38 additions & 0 deletions pydl/pydlspec2d/spec1d/tests/test_spec_append.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
# -*- coding: utf-8 -*-


def test_spec_append():
import os
from numpy import array
from .. import spec_append
spec1 = array([[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]])
spec2 = array([[2, 2, 2, 2, 2],
[2, 2, 2, 2, 2]])
s = spec_append(spec1, spec2)
assert (s == array([[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[2, 2, 2, 2, 2],
[2, 2, 2, 2, 2]])).all()
spec2 = array([[2, 2, 2, 2],
[2, 2, 2, 2]])
s = spec_append(spec1, spec2)
assert (s == array([[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[2, 2, 2, 2, 0],
[2, 2, 2, 2, 0]])).all()
s = spec_append(spec1, spec2, 1)
assert (s == array([[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[0, 2, 2, 2, 2],
[0, 2, 2, 2, 2]])).all()
spec1 = array([[1, 1, 1],
[1, 1, 1]])
spec2 = array([[2, 2, 2, 2, 2],
[2, 2, 2, 2, 2]])
s = spec_append(spec1, spec2, -2)
assert (s == array([[0, 0, 1, 1, 1],
[0, 0, 1, 1, 1],
[2, 2, 2, 2, 2],
[2, 2, 2, 2, 2]])).all()
35 changes: 35 additions & 0 deletions pydl/pydlspec2d/spec1d/tests/test_spec_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
# -*- coding: utf-8 -*-


def test_spec_path():
import os
from numpy import array
from .. import spec_path
if 'BOSS_SPECTRO_REDUX' in os.environ:
bsr_orig = os.environ['BOSS_SPECTRO_REDUX']
bsr = bsr_orig
else:
bsr_orig = None
bsr = '/boss/spectro/redux'
os.environ['BOSS_SPECTRO_REDUX'] = bsr
if 'RUN2D' in os.environ:
run2d_orig = os.environ['RUN2D']
run2d = run2d_orig
else:
run2d_orig = None
run2d = 'v1_2_3'
os.environ['RUN2D'] = run2d
p = spec_path(1234)
assert p[0] == os.path.join(bsr, run2d, '1234')
p = spec_path(1234, topdir=bsr, run2d=run2d)
assert p[0] == os.path.join(bsr, run2d, '1234')
p = spec_path(array([1234, 5678]), topdir=bsr, run2d=run2d)
assert p[0] == os.path.join(bsr, run2d, '1234')
assert p[1] == os.path.join(bsr, run2d, '5678')
p = spec_path(1234, path=bsr)
assert p[0] == bsr
if bsr_orig is None:
del os.environ['BOSS_SPECTRO_REDUX']
if run2d_orig is None:
del os.environ['RUN2D']
4 changes: 4 additions & 0 deletions pydl/tests/t/pcomp_data.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,7 @@
73.4205, 14.7100, 3.86270, 0.0159930
# variance
0.797969, 0.159875, 0.0419817, 0.000173819
# eigenvalues of standardized data
2.99966981, 1.05397516, 1.56189601e-01, 6.91743392e-04
# variance of standardized data
7.12421581e-01, 2.50319100e-01, 3.70950302e-02, 1.64289056e-04
16 changes: 15 additions & 1 deletion pydl/tests/test_pydl.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def test_pcomp(self):
pcomp_data = test_data[0:20, :]
m = 4
n = 20
means = np.tile(pcomp_data.mean(0), 20).reshape(pcomp_data.shape)
means = np.tile(pcomp_data.mean(0), n).reshape(pcomp_data.shape)
newarray = pcomp_data - means
foo = pcomp(newarray, covariance=True)
#
Expand All @@ -101,6 +101,20 @@ def test_pcomp(self):
assert_allclose(foo.eigenvalues, eigenvalues, 1e-4)
variance = test_data[45, :]
assert_allclose(foo.variance, variance, 1e-4)
#
# Test the standardization.
#
foo = pcomp(pcomp_data, standardize=True, covariance=True)
# for k in range(m):
# assert_allclose(abs(foo.derived[:, k]), abs(derived[:, k]), 1e-4)
# for k in range(m):
# assert_allclose(abs(foo.coefficients[:, k]),
# abs(coefficientsT[:, k]),
# 1e-4)
eigenvalues = test_data[46, :]
assert_allclose(foo.eigenvalues, eigenvalues, 1e-4)
variance = test_data[47, :]
assert_allclose(foo.variance, variance, 1e-4)

def test_smooth(self):
test_data_file = join(self.data_dir, 'smooth_data.txt')
Expand Down

0 comments on commit ef4818f

Please sign in to comment.