/
test_common.py
74 lines (52 loc) · 2.32 KB
/
test_common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Licensed under a 3-clause BSD style license - see LICENSE.rst
# Base class for all shape tests
from __future__ import absolute_import, division, print_function, unicode_literals
import numpy as np
from numpy.testing import assert_equal, assert_allclose
import pytest
from ...core import PixCoord, BoundingBox
class BaseTestRegion(object):
def test_repr(self):
assert repr(self.reg).replace(" ","") == self.expected_repr.replace(" ","")
def test_str(self):
assert str(self.reg).replace(" ","") == self.expected_str.replace(" ","")
class BaseTestPixelRegion(BaseTestRegion):
def test_area(self):
assert_allclose(self.reg.area, self.expected_area)
def test_mask_area(self):
try:
mask = self.reg.to_mask(mode='exact')
assert_allclose(np.sum(mask.data), self.expected_area)
except NotImplementedError:
try:
mask = self.reg.to_mask(mode='subpixels', subpixels=30)
assert_allclose(np.sum(mask.data), self.expected_area, rtol=0.005)
except NotImplementedError:
pytest.skip()
def test_contains_scalar(self):
if len(self.inside) > 0:
pixcoord = PixCoord(*self.inside[0])
assert self.reg.contains(pixcoord)
assert pixcoord in self.reg
if len(self.outside) > 0:
pixcoord = PixCoord(*self.outside[0])
assert not self.reg.contains(pixcoord)
assert pixcoord not in self.reg
def test_contains_array_1d(self):
pixcoord = PixCoord(*zip(*(self.inside + self.outside)))
actual = self.reg.contains(pixcoord)
assert_equal(actual[:len(self.inside)], True)
assert_equal(actual[len(self.inside):], False)
with pytest.raises(ValueError) as excinfo:
pixcoord in self.reg
assert 'coord must be scalar' in str(excinfo.value)
def test_contains_array_2d(self):
x, y = zip(*(self.inside + self.outside))
pixcoord = PixCoord([x] * 3, [y] * 3)
actual = self.reg.contains(pixcoord)
assert actual.shape == (3, len(x))
assert_equal(actual[:, :len(self.inside)], True)
assert_equal(actual[:, len(self.inside):], False)
class BaseTestSkyRegion(BaseTestRegion):
# TODO: here we should add inside/outside tests as above
pass