1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | #TEST |
---|
4 | import sys |
---|
5 | import unittest |
---|
6 | from math import sqrt |
---|
7 | |
---|
8 | |
---|
9 | from spike_least_squares import * |
---|
10 | from Numeric import allclose, array, transpose |
---|
11 | from Numeric import zeros, take, compress, array, Float, Int, dot, transpose, concatenate, ArrayType |
---|
12 | from utilities.sparse import Sparse, Sparse_CSR |
---|
13 | |
---|
14 | from coordinate_transforms.geo_reference import Geo_reference |
---|
15 | |
---|
16 | def distance(x, y): |
---|
17 | return sqrt( sum( (array(x)-array(y))**2 )) |
---|
18 | |
---|
19 | def linear_function(point): |
---|
20 | point = array(point) |
---|
21 | return point[:,0]+point[:,1] |
---|
22 | |
---|
23 | |
---|
24 | class Test_Least_Squares(unittest.TestCase): |
---|
25 | |
---|
26 | def setUp(self): |
---|
27 | pass |
---|
28 | |
---|
29 | def tearDown(self): |
---|
30 | pass |
---|
31 | |
---|
32 | def test_smooth_attributes_to_mesh(self): |
---|
33 | a = [0.0, 0.0] |
---|
34 | b = [0.0, 5.0] |
---|
35 | c = [5.0, 0.0] |
---|
36 | points = [a, b, c] |
---|
37 | triangles = [ [1,0,2] ] #bac |
---|
38 | |
---|
39 | d1 = [1.0, 1.0] |
---|
40 | d2 = [1.0, 3.0] |
---|
41 | d3 = [3.0,1.0] |
---|
42 | z1 = 2 |
---|
43 | z2 = 4 |
---|
44 | z3 = 4 |
---|
45 | data_coords = [d1, d2, d3] |
---|
46 | |
---|
47 | z = [z1, z2, z3] |
---|
48 | interp = Interpolation(points, triangles, z,data_coords, alpha=0) |
---|
49 | #print "interp.get_A()", interp.get_A() |
---|
50 | A = interp.A |
---|
51 | #print "A",A |
---|
52 | #print "z",z |
---|
53 | Atz = A.trans_mult(z) |
---|
54 | #print "Atz",Atz |
---|
55 | |
---|
56 | f = interp.fit(z) |
---|
57 | answer = [0, 5., 5.] |
---|
58 | |
---|
59 | #print "f\n",f |
---|
60 | #print "answer\n",answer |
---|
61 | |
---|
62 | assert allclose(f, answer, atol=1e-7) |
---|
63 | |
---|
64 | |
---|
65 | def test_smooth_attributes_to_mesh_one_point(self): |
---|
66 | a = [0.0, 0.0] |
---|
67 | b = [0.0, 5.0] |
---|
68 | c = [5.0, 0.0] |
---|
69 | points = [a, b, c] |
---|
70 | triangles = [ [1,0,2] ] #bac |
---|
71 | |
---|
72 | d1 = [1.0, 1.0] |
---|
73 | d2 = [1.0, 3.0] |
---|
74 | d3 = [3.0,1.0] |
---|
75 | z1 = 2 |
---|
76 | z2 = 4 |
---|
77 | z3 = 4 |
---|
78 | data_coords = [d1] |
---|
79 | |
---|
80 | z = [z1] |
---|
81 | interp = Interpolation(points, triangles, z,data_coords, alpha=0) |
---|
82 | #print "interp.get_A()", interp.get_A() |
---|
83 | A = interp.A |
---|
84 | #print "A",A |
---|
85 | #print "z",z |
---|
86 | Atz_actual = A.trans_mult(z) |
---|
87 | #Atz = interp.Atz.todense() |
---|
88 | |
---|
89 | |
---|
90 | #print "Atz",Atz |
---|
91 | #print "Atz_actual",Atz_actual |
---|
92 | |
---|
93 | |
---|
94 | #assert allclose(Atz_actual, Atz, atol=1e-7) |
---|
95 | |
---|
96 | def ytest_chewin_the_fat(self): |
---|
97 | |
---|
98 | A = Sparse(2,2) |
---|
99 | A[0,0] = 1 |
---|
100 | A[1,0] = 1 |
---|
101 | A[0,1] = 0 |
---|
102 | A[1,1] = 1 |
---|
103 | z = [1,1] |
---|
104 | m = 2 |
---|
105 | n = 2 |
---|
106 | Atz = zeros((n), Float) |
---|
107 | r = A.trans_mult(z) |
---|
108 | for i in range(m): |
---|
109 | for j in range(n): |
---|
110 | Atz[i] += A[m-i-1,n-j-1]*z[i] |
---|
111 | print "A.trans_mult(z)",A.trans_mult(z) |
---|
112 | print "Atz",Atz |
---|
113 | print "A*z", A*z |
---|
114 | |
---|
115 | |
---|
116 | #print "answer\n",answer |
---|
117 | |
---|
118 | assert allclose(f, answer, atol=1e-7) |
---|
119 | |
---|
120 | |
---|
121 | def test_smooth_att_to_meshII(self): |
---|
122 | |
---|
123 | a = [0.0, 0.0] |
---|
124 | b = [0.0, 5.0] |
---|
125 | c = [5.0, 0.0] |
---|
126 | points = [a, b, c] |
---|
127 | triangles = [ [1,0,2] ] #bac |
---|
128 | |
---|
129 | d1 = [1.0, 1.0] |
---|
130 | d2 = [1.0, 2.0] |
---|
131 | d3 = [3.0,1.0] |
---|
132 | data_coords = [d1, d2, d3] |
---|
133 | z = linear_function(data_coords) |
---|
134 | #print "z",z |
---|
135 | |
---|
136 | interp = Interpolation(points, triangles, z, |
---|
137 | data_coords, alpha=0.0) |
---|
138 | f = interp.fit(z) |
---|
139 | answer = linear_function(points) |
---|
140 | #print "f\n",f |
---|
141 | #print "answer\n",answer |
---|
142 | |
---|
143 | assert allclose(f, answer) |
---|
144 | |
---|
145 | def test_smooth_attributes_to_meshIII(self): |
---|
146 | |
---|
147 | a = [-1.0, 0.0] |
---|
148 | b = [3.0, 4.0] |
---|
149 | c = [4.0,1.0] |
---|
150 | d = [-3.0, 2.0] #3 |
---|
151 | e = [-1.0,-2.0] |
---|
152 | f = [1.0, -2.0] #5 |
---|
153 | |
---|
154 | vertices = [a, b, c, d,e,f] |
---|
155 | triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc |
---|
156 | |
---|
157 | point_coords = [[-2.0, 2.0], |
---|
158 | [-1.0, 1.0], |
---|
159 | [0.0,2.0], |
---|
160 | [1.0, 1.0], |
---|
161 | [2.0, 1.0], |
---|
162 | [0.0,0.0], |
---|
163 | [1.0, 0.0], |
---|
164 | [0.0, -1.0], |
---|
165 | [-0.2,-0.5], |
---|
166 | [-0.9, -1.5], |
---|
167 | [0.5, -1.9], |
---|
168 | [3.0,1.0]] |
---|
169 | |
---|
170 | z = linear_function(point_coords) |
---|
171 | interp = Interpolation(vertices, triangles, z, |
---|
172 | point_coords, alpha=0.0) |
---|
173 | |
---|
174 | #print 'z',z |
---|
175 | f = interp.fit(z) |
---|
176 | answer = linear_function(vertices) |
---|
177 | #print "f\n",f |
---|
178 | #print "answer\n",answer |
---|
179 | assert allclose(f, answer) |
---|
180 | |
---|
181 | def test_smooth_attributes_to_meshIV(self): |
---|
182 | """ Testing 2 attributes smoothed to the mesh |
---|
183 | """ |
---|
184 | |
---|
185 | a = [0.0, 0.0] |
---|
186 | b = [0.0, 5.0] |
---|
187 | c = [5.0, 0.0] |
---|
188 | points = [a, b, c] |
---|
189 | triangles = [ [1,0,2] ] #bac |
---|
190 | |
---|
191 | d1 = [1.0, 1.0] |
---|
192 | d2 = [1.0, 3.0] |
---|
193 | d3 = [3.0, 1.0] |
---|
194 | z1 = [2, 4] |
---|
195 | z2 = [4, 8] |
---|
196 | z3 = [4, 8] |
---|
197 | data_coords = [d1, d2, d3] |
---|
198 | |
---|
199 | z = [z1, z2, z3] |
---|
200 | interp = Interpolation(points, triangles, z, |
---|
201 | data_coords, alpha=0.0) |
---|
202 | f = interp.fit_points(z) |
---|
203 | answer = [[0,0], [5., 10.], [5., 10.]] |
---|
204 | assert allclose(f, answer) |
---|
205 | |
---|
206 | |
---|
207 | def test_fit_and_interpolation(self): |
---|
208 | from mesh import Mesh |
---|
209 | |
---|
210 | a = [0.0, 0.0] |
---|
211 | b = [0.0, 2.0] |
---|
212 | c = [2.0, 0.0] |
---|
213 | d = [0.0, 4.0] |
---|
214 | e = [2.0, 2.0] |
---|
215 | f = [4.0, 0.0] |
---|
216 | |
---|
217 | points = [a, b, c, d, e, f] |
---|
218 | #bac, bce, ecf, dbe, daf, dae |
---|
219 | triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] |
---|
220 | |
---|
221 | #Get (enough) datapoints |
---|
222 | data_points = [[ 0.66666667, 0.66666667], |
---|
223 | [ 1.33333333, 1.33333333], |
---|
224 | [ 2.66666667, 0.66666667], |
---|
225 | [ 0.66666667, 2.66666667], |
---|
226 | [ 0.0, 1.0], |
---|
227 | [ 0.0, 3.0], |
---|
228 | [ 1.0, 0.0], |
---|
229 | [ 1.0, 1.0], |
---|
230 | [ 1.0, 2.0], |
---|
231 | [ 1.0, 3.0], |
---|
232 | [ 2.0, 1.0], |
---|
233 | [ 3.0, 0.0], |
---|
234 | [ 3.0, 1.0]] |
---|
235 | |
---|
236 | z = linear_function(data_points) |
---|
237 | interp = Interpolation(points, triangles, z, |
---|
238 | data_points, alpha=0.0) |
---|
239 | |
---|
240 | answer = linear_function(points) |
---|
241 | |
---|
242 | f = interp.fit(z) |
---|
243 | |
---|
244 | #print "f",f |
---|
245 | #print "answer",answer |
---|
246 | assert allclose(f, answer) |
---|
247 | |
---|
248 | |
---|
249 | def test_smoothing_and_interpolation(self): |
---|
250 | |
---|
251 | a = [0.0, 0.0] |
---|
252 | b = [0.0, 2.0] |
---|
253 | c = [2.0, 0.0] |
---|
254 | d = [0.0, 4.0] |
---|
255 | e = [2.0, 2.0] |
---|
256 | f = [4.0, 0.0] |
---|
257 | |
---|
258 | points = [a, b, c, d, e, f] |
---|
259 | #bac, bce, ecf, dbe, daf, dae |
---|
260 | triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] |
---|
261 | |
---|
262 | #Get (too few!) datapoints |
---|
263 | data_points = [[ 0.66666667, 0.66666667], |
---|
264 | [ 1.33333333, 1.33333333], |
---|
265 | [ 2.66666667, 0.66666667], |
---|
266 | [ 0.66666667, 2.66666667]] |
---|
267 | |
---|
268 | z = linear_function(data_points) |
---|
269 | answer = linear_function(points) |
---|
270 | |
---|
271 | #Make interpolator with too few data points and no smoothing |
---|
272 | interp = Interpolation(points, triangles, z, |
---|
273 | data_points, alpha=0.0) |
---|
274 | #Must raise an exception |
---|
275 | try: |
---|
276 | f = interp.fit(z) |
---|
277 | except: |
---|
278 | pass |
---|
279 | |
---|
280 | #Now try with smoothing parameter |
---|
281 | interp = Interpolation(points, triangles, z, |
---|
282 | data_points, alpha=1.0e-13) |
---|
283 | |
---|
284 | f = interp.fit(z) |
---|
285 | #f will be different from answer due to smoothing |
---|
286 | assert allclose(f, answer,atol=5) |
---|
287 | |
---|
288 | #Map back |
---|
289 | #z1 = interp.interpolate(f) |
---|
290 | #assert allclose(z, z1) |
---|
291 | |
---|
292 | #------------------------------------------------------------- |
---|
293 | if __name__ == "__main__": |
---|
294 | suite = unittest.makeSuite(Test_Least_Squares,'test') |
---|
295 | #suite = unittest.makeSuite(Test_Least_Squares,'test_smoothing_and_interpolation') |
---|
296 | #suite = unittest.makeSuite(Test_Least_Squares,'test_smooth_attributes_to_mesh_one_point') |
---|
297 | runner = unittest.TextTestRunner(verbosity=1) |
---|
298 | runner.run(suite) |
---|
299 | |
---|
300 | |
---|
301 | |
---|
302 | |
---|
303 | |
---|