Skip to content

Commit

Permalink
Merge pull request spacetelescope#7 from mcara/add-unit-tests
Browse files Browse the repository at this point in the history
Add unit tests
  • Loading branch information
mcara authored Aug 19, 2019
2 parents c6126a6 + 5673b76 commit 0d8304b
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[run]
branch = True
source = wiimatch

[report]
exclude_lines =
if self.debug:
pragma: no cover
raise NotImplementedError
if __name__ == .__main__.:
ignore_errors = True
omit =
conftest.py
setup.py
tests/*
wiimatch/tests/*
Empty file added wiimatch/tests/__init__.py
Empty file.
78 changes: 78 additions & 0 deletions wiimatch/tests/test_match.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""
A module containing unit tests for the `wcsutil` module.
Licensed under a 3-clause BSD style license - see LICENSE.rst
"""
import pytest
import numpy as np

from wiimatch import match


@pytest.mark.parametrize('solver', ['RLU', 'PINV'])
def test_match_lsq_solver(solver):
# simulate background image data:
c = [1.32, 0.15, 0.62, 0, 0.74, 0, 0, 0]
im1 = np.zeros((5, 5, 4), dtype=np.float)
cbg = c[0] * np.ones_like(im1) # constand background level image

# add slope:
ind = np.indices(im1.shape, dtype=np.float)
im3 = cbg + c[1] * ind[0] + c[2] * ind[1] + c[4] * ind[2]

mask = np.ones_like(im1, dtype=np.int8)
sigma = np.ones_like(im1, dtype=np.float)

p = match.match_lsq(
[im1, im3], [mask, mask], [sigma, sigma],
degree=(1, 1, 1), center=(0, 0, 0), solver=solver
)

assert np.allclose(-p[0], p[1], rtol=1.e-8, atol=1.e-12)
assert np.allclose(c, 2 * np.abs(p[0]), rtol=1.e-8, atol=1.e-12)


def test_match_lsq_extended_return():
# simulate background image data:
c = [1.32, 0.15, 0.62, 0, 0.74, 0, 0, 0]
im1 = np.zeros((5, 5, 4), dtype=np.float)
cbg = c[0] * np.ones_like(im1) # constand background level image

# add slope:
ind = np.indices(im1.shape, dtype=np.float)
im3 = cbg + c[1] * ind[0] + c[2] * ind[1] + c[4] * ind[2]

mask = np.ones_like(im1, dtype=np.int8)
sigma = np.ones_like(im1, dtype=np.float)

p, a, b, coord_arrays, eff_center, coord_system = match.match_lsq(
[im1, im3], [mask, mask], [sigma, sigma],
degree=1, center=(0, 0, 0), ext_return=True
)

assert np.allclose(-p[0], p[1], rtol=1.e-8, atol=1.e-12)
assert np.allclose(c, 2 * np.abs(p[0]), rtol=1.e-8, atol=1.e-12)


@pytest.mark.parametrize('degree', [1, (1, 1, 1)])
def test_match_lsq_num_degree(degree):
# simulate background image data:
c = [1.32, 0.15, 0.62, 0, 0.74, 0, 0, 0]
im1 = np.zeros((5, 5, 4), dtype=np.float)
cbg = c[0] * np.ones_like(im1) # constand background level image

# add slope:
ind = np.indices(im1.shape, dtype=np.float)
im3 = cbg + c[1] * ind[0] + c[2] * ind[1] + c[4] * ind[2]

mask = np.ones_like(im1, dtype=np.int8)
sigma = np.ones_like(im1, dtype=np.float)

p = match.match_lsq(
[im1, im3], [mask, mask], [sigma, sigma],
degree=degree, center=(0, 0, 0), solver='RLU', ext_return=False
)

assert np.allclose(-p[0], p[1], rtol=1.e-8, atol=1.e-12)
assert np.allclose(c, 2 * np.abs(p[0]), rtol=1.e-8, atol=1.e-12)
32 changes: 32 additions & 0 deletions wiimatch/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
A module containing unit tests for the `wcsutil` module.
Licensed under a 3-clause BSD style license - see LICENSE.rst
"""
import pytest
import numpy as np

from wiimatch import utils


def test_utils_coordinates():
image_shape = (3, 5, 4)
center = (0, 0, 0)
c = utils.create_coordinate_arrays(image_shape, center=center)
ind = np.indices(image_shape, dtype=np.float)[::-1]

assert np.allclose(c[0], ind, rtol=1.e-8, atol=1.e-12)
assert np.allclose(c[1], center, rtol=1.e-8, atol=1.e-12)


def test_utils_coordinates_no_center():
image_shape = (3, 5, 4)
c = utils.create_coordinate_arrays(image_shape, center=None)
ind = np.indices(image_shape, dtype=np.float)[::-1]

center = tuple(i // 2 for i in image_shape)
for orig, cc, i in zip(center, c[0], ind):
assert(i - orig, cc)

assert np.allclose(c[1], center, rtol=1.e-8, atol=1.e-12)

0 comments on commit 0d8304b

Please sign in to comment.