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