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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
from __future__ import print_function
# STDLIB
import functools
import itertools
import math
import os
import random
import sys
# THIRD-PARTY
import numpy as np
from numpy.testing import assert_array_almost_equal
# LOCAL
from sphere import polygon
GRAPH_MODE = False
ROOT_DIR = os.path.join(os.path.dirname(__file__), 'data')
class union_test:
def __init__(self, lon_0, lat_0, proj='ortho'):
self._lon_0 = lon_0
self._lat_0 = lat_0
self._proj = proj
def __call__(self, func):
@functools.wraps(func)
def run(*args, **kwargs):
polys = func(*args, **kwargs)
unions = []
num_permutations = math.factorial(len(polys))
step_size = int(max(float(num_permutations) / 20.0, 1.0))
if GRAPH_MODE:
print("%d permutations" % num_permutations)
for method in ('parallel', 'serial'):
for i, permutation in enumerate(
itertools.islice(
itertools.permutations(polys),
None, None, step_size)):
filename = '%s_%s_union_%04d.svg' % (
func.__name__, method, i)
print(filename)
union = polygon.SphericalPolygon.multi_union(
permutation, method=method)
unions.append(union)
areas = [x.area() for x in permutation]
union_area = union.area()
assert np.all(union_area >= areas)
if GRAPH_MODE:
fig = plt.figure()
m = Basemap(projection=self._proj,
lon_0=self._lon_0,
lat_0=self._lat_0)
m.drawmapboundary(fill_color='white')
m.drawparallels(np.arange(-90., 90., 20.))
m.drawmeridians(np.arange(0., 420., 20.))
union.draw(m, color='red', linewidth=3)
for poly in permutation:
poly.draw(m, color='blue', alpha=0.5)
plt.savefig(filename)
fig.clear()
lengths = np.array([len(x._points) for x in unions])
assert np.all(lengths == [lengths[0]])
areas = np.array([x.area() for x in unions])
# assert_array_almost_equal(areas, areas[0], 1)
return run
@union_test(0, 90)
def test1():
import pyfits
fits = pyfits.open(os.path.join(ROOT_DIR, '1904-66_TAN.fits'))
header = fits[0].header
poly1 = polygon.SphericalPolygon.from_wcs(
header, 1, crval=[0, 87])
poly2 = polygon.SphericalPolygon.from_wcs(
header, 1, crval=[20, 89])
poly3 = polygon.SphericalPolygon.from_wcs(
header, 1, crval=[175, 89])
poly4 = polygon.SphericalPolygon.from_cone(
90, 70, 10, steps=50)
return [poly1, poly2, poly3, poly4]
@union_test(0, 90)
def test2():
poly1 = polygon.SphericalPolygon.from_cone(0, 60, 7, steps=16)
poly2 = polygon.SphericalPolygon.from_cone(0, 72, 7, steps=16)
poly3 = polygon.SphericalPolygon.from_cone(20, 60, 7, steps=16)
poly4 = polygon.SphericalPolygon.from_cone(20, 72, 7, steps=16)
poly5 = polygon.SphericalPolygon.from_cone(35, 55, 7, steps=16)
poly6 = polygon.SphericalPolygon.from_cone(60, 60, 3, steps=16)
return [poly1, poly2, poly3, poly4, poly5, poly6]
@union_test(0, 90)
def test3():
random.seed(0)
polys = []
for i in range(10):
polys.append(polygon.SphericalPolygon.from_cone(
random.randrange(-180, 180),
random.randrange(20, 90),
random.randrange(5, 16),
steps=16))
return polys
@union_test(0, 15)
def test4():
random.seed(64)
polys = []
for i in range(10):
polys.append(polygon.SphericalPolygon.from_cone(
random.randrange(-30, 30),
random.randrange(-15, 60),
random.randrange(5, 16),
steps=16))
return polys
def test5():
import pyfits
import pywcs
A = pyfits.open(os.path.join(ROOT_DIR, '2chipA.fits.gz'))
wcs = pywcs.WCS(A[1].header, fobj=A)
chipA1 = polygon.SphericalPolygon.from_wcs(wcs)
wcs = pywcs.WCS(A[4].header, fobj=A)
chipA2 = polygon.SphericalPolygon.from_wcs(wcs)
null_union = chipA1.union(chipA2)
def test6():
import pyfits
import pywcs
A = pyfits.open(os.path.join(ROOT_DIR, '2chipC.fits.gz'))
wcs = pywcs.WCS(A[1].header, fobj=A)
chipA1 = polygon.SphericalPolygon.from_wcs(wcs)
wcs = pywcs.WCS(A[4].header, fobj=A)
chipA2 = polygon.SphericalPolygon.from_wcs(wcs)
null_union = chipA1.union(chipA2)
if __name__ == '__main__':
if '--profile' not in sys.argv:
GRAPH_MODE = True
from mpl_toolkits.basemap import Basemap
from matplotlib import pyplot as plt
functions = [(k, v) for k, v in globals().items() if k.startswith('test')]
functions.sort()
for k, v in functions:
v()
|