Changeset 5905 for anuga_core/source_numpy_conversion/anuga/fit_interpolate
- Timestamp:
- Nov 6, 2008, 3:41:41 PM (16 years ago)
- Location:
- anuga_core/source_numpy_conversion/anuga/fit_interpolate
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source_numpy_conversion/anuga/fit_interpolate/benchmark_least_squares.py
r4872 r5905 22 22 import tempfile 23 23 import profile , pstats 24 from math import sqrt25 from Numeric import array24 ##from math import sqrt 25 ##import numpy 26 26 27 27 from anuga.fit_interpolate.search_functions import search_times, \ -
anuga_core/source_numpy_conversion/anuga/fit_interpolate/fit.py
r5855 r5905 28 28 import types 29 29 30 from Numeric import zeros, Float, ArrayType,take, Int 30 import numpy 31 31 32 32 from anuga.abstract_2d_finite_volumes.neighbour_mesh import Mesh … … 252 252 if len(z.shape) > 1: 253 253 att_num = z.shape[1] 254 self.Atz = zeros((m,att_num), Float)254 self.Atz = numpy.zeros((m,att_num), numpy.float) 255 255 else: 256 256 att_num = 1 257 self.Atz = zeros((m,), Float)257 self.Atz = numpy.zeros((m,), numpy.float) 258 258 assert z.shape[0] == point_coordinates.shape[0] 259 259 … … 437 437 # Convert input to Numeric arrays 438 438 if z is not None: 439 z = ensure_numeric(z, Float)439 z = ensure_numeric(z, numpy.float) 440 440 else: 441 441 msg = 'z not specified' … … 443 443 z = point_coordinates.get_attributes(attribute_name) 444 444 445 point_coordinates = ensure_numeric(point_coordinates, Float)445 point_coordinates = ensure_numeric(point_coordinates, numpy.float) 446 446 self._build_matrix_AtA_Atz(point_coordinates, z, verbose) 447 447 … … 584 584 585 585 #Convert input to Numeric arrays 586 triangles = ensure_numeric(triangles, Int)586 triangles = ensure_numeric(triangles, numpy.int) 587 587 vertex_coordinates = ensure_absolute(vertex_coordinates, 588 588 geo_reference = mesh_origin) … … 651 651 vertex_coordinates = mesh_dict['vertices'] 652 652 triangles = mesh_dict['triangles'] 653 if type(mesh_dict['vertex_attributes']) == ArrayType:653 if isinstance(mesh_dict['vertex_attributes'], numpy.ndarray): 654 654 old_point_attributes = mesh_dict['vertex_attributes'].tolist() 655 655 else: 656 656 old_point_attributes = mesh_dict['vertex_attributes'] 657 657 658 if type(mesh_dict['vertex_attribute_titles']) == ArrayType:658 if isinstance(mesh_dict['vertex_attribute_titles'], numpy.ndarray): 659 659 old_title_list = mesh_dict['vertex_attribute_titles'].tolist() 660 660 else: -
anuga_core/source_numpy_conversion/anuga/fit_interpolate/general_fit_interpolate.py
r5361 r5905 21 21 from warnings import warn 22 22 23 from Numeric import zeros, array, Float, Int, dot, transpose, concatenate, \ 24 ArrayType, allclose, take, NewAxis, arange 23 import numpy 25 24 26 25 from anuga.caching.caching import cache … … 97 96 98 97 #Convert input to Numeric arrays 99 triangles = ensure_numeric(triangles, Int)98 triangles = ensure_numeric(triangles, numpy.int) 100 99 vertex_coordinates = ensure_absolute(vertex_coordinates, 101 100 geo_reference = mesh_origin) -
anuga_core/source_numpy_conversion/anuga/fit_interpolate/interpolate.py
r5869 r5905 24 24 from csv import writer, DictWriter 25 25 26 from Numeric import zeros, array, Float, Int, dot, transpose, concatenate, \ 27 ArrayType, allclose, take, NewAxis, arange 26 import numpy 28 27 29 28 from anuga.caching.caching import cache … … 116 115 117 116 # Create interpolation object with matrix 118 args = (ensure_numeric(vertex_coordinates, Float),117 args = (ensure_numeric(vertex_coordinates, numpy.float), 119 118 ensure_numeric(triangles)) 120 119 kwargs = {'mesh_origin': mesh_origin, … … 224 223 225 224 from utilities.polygon import point_on_line 226 from Numeric import ones 227 z=ones(len(point_coordinates),Float) 225 z=numpy.ones(len(point_coordinates),numpy.float) 228 226 229 227 msg='point coordinates are not given (interpolate.py)' … … 343 341 # creating a dummy array to concatenate to. 344 342 345 f = ensure_numeric(f, Float)343 f = ensure_numeric(f, numpy.float) 346 344 #print "f.shape",f.shape 347 345 if len(f.shape) > 1: 348 z = zeros((0,f.shape[1]))346 z = numpy.zeros((0,f.shape[1]), numpy.float) 349 347 else: 350 z = zeros((0,))348 z = numpy.zeros((0,), numpy.float) 351 349 352 350 for end in range(start_blocking_len, … … 358 356 #print "t", t 359 357 #print "z", z 360 z = concatenate((z,t))358 z = numpy.concatenate((z,t)) 361 359 start = end 362 360 … … 364 362 t = self.interpolate_block(f, point_coordinates[start:end], 365 363 verbose=verbose) 366 z = concatenate((z,t))364 z = numpy.concatenate((z,t)) 367 365 return z 368 366 … … 389 387 390 388 # Convert lists to Numeric arrays if necessary 391 point_coordinates = ensure_numeric(point_coordinates, Float)392 f = ensure_numeric(f, Float)389 point_coordinates = ensure_numeric(point_coordinates, numpy.float) 390 f = ensure_numeric(f, numpy.float) 393 391 394 392 from anuga.caching import myhash 395 from Numeric import alltrue396 393 import sys 397 394 if use_cache is True: … … 412 409 if self.interpolation_matrices.has_key(key): 413 410 X, stored_points = self.interpolation_matrices[key] 414 if alltrue(stored_points == point_coordinates):411 if numpy.alltrue(stored_points == point_coordinates): 415 412 reuse_A = True # Reuse interpolation matrix 416 413 … … 487 484 488 485 # Convert point_coordinates to Numeric arrays, in case it was a list. 489 point_coordinates = ensure_numeric(point_coordinates, Float)486 point_coordinates = ensure_numeric(point_coordinates, numpy.float) 490 487 491 488 … … 752 749 """ 753 750 754 from Numeric import array, zeros, Float, alltrue, concatenate,\755 reshape, ArrayType756 757 758 751 from anuga.config import time_format 759 752 import types 760 761 753 762 754 # Check temporal info … … 764 756 msg = 'Time must be a monotonuosly ' 765 757 msg += 'increasing sequence %s' %time 766 assert alltrue(time[1:] - time[:-1] >= 0 ), msg758 assert numpy.alltrue(time[1:] - time[:-1] >= 0 ), msg 767 759 768 760 … … 795 787 # Thin timesteps if needed 796 788 # Note array() is used to make the thinned arrays contiguous in memory 797 self.time = array(time[::time_thinning])789 self.time = numpy.array(time[::time_thinning]) 798 790 for name in quantity_names: 799 791 if len(quantities[name].shape) == 2: 800 quantities[name] = array(quantities[name][::time_thinning,:])792 quantities[name] = numpy.array(quantities[name][::time_thinning,:]) 801 793 802 794 # Save for use with statistics 803 795 self.quantities_range = {} 804 796 for name in quantity_names: 805 q = quantities[name][:].flat 797 ## q = quantities[name][:].ravel() 798 q = numpy.ravel(quantities[name][:]) 806 799 self.quantities_range[name] = [min(q), max(q)] 807 800 … … 863 856 if sys.platform == 'win32': # FIXME (Ole): Why only Windoze? 864 857 from anuga.utilities.polygon import plot_polygons 865 #out_interp_pts = take(interpolation_points,[indices])858 #out_interp_pts = numpy.take(interpolation_points,[indices], axis=0) 866 859 title = 'Interpolation points fall outside specified mesh' 867 860 plot_polygons([mesh_boundary_polygon, … … 905 898 906 899 for name in quantity_names: 907 self.precomputed_values[name] = zeros((p, m), Float)900 self.precomputed_values[name] = numpy.zeros((p, m), numpy.float) 908 901 909 902 # Build interpolator … … 995 988 996 989 from math import pi, cos, sin, sqrt 997 from Numeric import zeros, Float998 990 from anuga.abstract_2d_finite_volumes.util import mean 999 991 … … 1031 1023 1032 1024 # Compute interpolated values 1033 q = zeros(len(self.quantity_names), Float)1025 q = numpy.zeros(len(self.quantity_names), numpy.float) 1034 1026 # print "self.precomputed_values", self.precomputed_values 1035 1027 for i, name in enumerate(self.quantity_names): … … 1086 1078 return q 1087 1079 else: 1088 from Numeric import ones, Float1089 1080 # x is a vector - Create one constant column for each value 1090 1081 N = len(x) … … 1092 1083 res = [] 1093 1084 for col in q: 1094 res.append(col* ones(N, Float))1085 res.append(col*numpy.ones(N, numpy.float)) 1095 1086 1096 1087 return res … … 1129 1120 minq, maxq = self.quantities_range[name] 1130 1121 str += ' %s in [%f, %f]\n' %(name, minq, maxq) 1131 #q = quantities[name][:]. flat1122 #q = quantities[name][:].ravel() 1132 1123 #str += ' %s in [%f, %f]\n' %(name, min(q), max(q)) 1133 1124 … … 1142 1133 1143 1134 for name in quantity_names: 1144 q = precomputed_values[name][:].flat 1135 ##NumPy q = precomputed_values[name][:].ravel() 1136 q = numpy.ravel(precomputed_values[name][:]) 1145 1137 str += ' %s at interpolation points in [%f, %f]\n'\ 1146 1138 %(name, min(q), max(q)) … … 1165 1157 1166 1158 #Add the x and y together 1167 vertex_coordinates = concatenate((x[:,NewAxis], y[:,NewAxis]),axis=1)1159 vertex_coordinates = numpy.concatenate((x[:,numpy.newaxis], y[:,numpy.newaxis]),axis=1) 1168 1160 1169 1161 #Will return the quantity values at the specified times and locations -
anuga_core/source_numpy_conversion/anuga/fit_interpolate/search_functions.py
r5775 r5905 6 6 7 7 """ 8 from Numeric import dot9 8 import time 10 9 11 from Numeric import array10 import numpy 12 11 13 12 from anuga.utilities.numerical_tools import get_machine_precision … … 19 18 20 19 #FIXME test what happens if a 21 LAST_TRIANGLE = [[-10,[( array([max_float,max_float]),22 array([max_float,max_float]),23 array([max_float,max_float])),24 ( array([1,1]),array([0,0]),array([-1.1,-1.1]))]]]20 LAST_TRIANGLE = [[-10,[(numpy.array([max_float,max_float]), 21 numpy.array([max_float,max_float]), 22 numpy.array([max_float,max_float])), 23 (numpy.array([1,1]),numpy.array([0,0]),numpy.array([-1.1,-1.1]))]]] 25 24 26 25 def search_tree_of_vertices(root, mesh, x): … … 189 188 # print "dot((xi0-xi1), n0)", dot((xi0-xi1), n0) 190 189 191 sigma0 = dot((x-xi1), n0)/dot((xi0-xi1), n0)190 sigma0 = numpy.dot((x-xi1), n0)/numpy.dot((xi0-xi1), n0) 192 191 if sigma0 < -epsilon: 193 192 return False,0,0,0 194 sigma1 = dot((x-xi2), n1)/dot((xi1-xi2), n1)193 sigma1 = numpy.dot((x-xi2), n1)/numpy.dot((xi1-xi2), n1) 195 194 if sigma1 < -epsilon: 196 195 return False,0,0,0 197 sigma2 = dot((x-xi0), n2)/dot((xi2-xi0), n2)196 sigma2 = numpy.dot((x-xi0), n2)/numpy.dot((xi2-xi0), n2) 198 197 if sigma2 < -epsilon: 199 198 return False,0,0,0 -
anuga_core/source_numpy_conversion/anuga/fit_interpolate/test_fit.py
r5349 r5905 12 12 import tempfile 13 13 import os 14 from Numeric import zeros, take, compress, Float, Int, dot, concatenate, \ 15 ArrayType, allclose, array14 15 import numpy 16 16 17 17 from fit import * … … 24 24 25 25 def distance(x, y): 26 return sqrt( sum( ( array(x)-array(y))**2 ))26 return sqrt( sum( (numpy.array(x)-numpy.array(y))**2 )) 27 27 28 28 def linear_function(point): 29 point = array(point)29 point = numpy.array(point) 30 30 return point[:,0]+point[:,1] 31 31 … … 64 64 #print "z",z 65 65 66 assert allclose(fit.Atz, [2.8, 3.6, 3.6], atol=1e-7)66 assert numpy.allclose(fit.Atz, [2.8, 3.6, 3.6], atol=1e-7) 67 67 68 68 f = fit.fit() … … 73 73 #print "answer\n",answer 74 74 75 assert allclose(f, answer, atol=1e-7)75 assert numpy.allclose(f, answer, atol=1e-7) 76 76 77 77 def test_smooth_att_to_meshII(self): … … 96 96 #print "answer\n",answer 97 97 98 assert allclose(f, answer)98 assert numpy.allclose(f, answer) 99 99 100 100 def test_smooth_attributes_to_meshIII(self): … … 132 132 #print "f\n",f 133 133 #print "answer\n",answer 134 assert allclose(f, answer)134 assert numpy.allclose(f, answer) 135 135 136 136 … … 158 158 f = fit.fit(data_coords,z) 159 159 answer = [[0,0], [5., 10.], [5., 10.]] 160 assert allclose(f, answer)160 assert numpy.allclose(f, answer) 161 161 162 162 def test_smooth_attributes_to_mesh_build_fit_subset(self): … … 204 204 #print "f\n",f 205 205 #print "answer\n",answer 206 assert allclose(f, answer)206 assert numpy.allclose(f, answer) 207 207 208 208 # test fit 2 mesh as well. … … 244 244 #print "f\n",f 245 245 #print "answer\n",answer 246 assert allclose(f, answer)246 assert numpy.allclose(f, answer) 247 247 os.remove(fileName) 248 248 … … 280 280 #print "f",f 281 281 #print "answer",answer 282 assert allclose(f, answer)282 assert numpy.allclose(f, answer) 283 283 284 284 # Delete file! … … 323 323 #print "f\n",f 324 324 #print "answer\n",answer 325 assert allclose(f, answer)325 assert numpy.allclose(f, answer) 326 326 os.remove(fileName) 327 327 … … 362 362 #print "f\n",f 363 363 #print "answer\n",answer 364 assert allclose(f, answer)364 assert numpy.allclose(f, answer) 365 365 os.remove(fileName) 366 366 os.remove(fileName_pts) … … 402 402 #print "f\n",f 403 403 #print "answer\n",answer 404 assert allclose(f, answer)404 assert numpy.allclose(f, answer) 405 405 os.remove(fileName) 406 406 os.remove(fileName_pts) … … 442 442 #print "f\n",f 443 443 #print "answer\n",answer 444 assert allclose(f, answer)444 assert numpy.allclose(f, answer) 445 445 446 446 os.remove(fileName) … … 483 483 #print "f\n",f 484 484 #print "answer\n",answer 485 assert allclose(f, answer)485 assert numpy.allclose(f, answer) 486 486 os.remove(fileName) 487 487 … … 524 524 #print "f",f 525 525 #print "answer",answer 526 assert allclose(f, answer)526 assert numpy.allclose(f, answer) 527 527 528 528 … … 562 562 f = interp.fit(data_points,z) 563 563 #f will be different from answer due to smoothing 564 assert allclose(f, answer,atol=5)564 assert numpy.allclose(f, answer,atol=5) 565 565 566 566 567 567 #Tests of smoothing matrix 568 568 def test_smoothing_matrix_one_triangle(self): 569 from Numeric import dot569 ## from numpy.oldnumeric import dot 570 570 a = [0.0, 0.0] 571 571 b = [0.0, 2.0] … … 577 577 interp = Fit(points, vertices) 578 578 579 assert allclose(interp.get_D(), [[1, -0.5, -0.5],579 assert numpy.allclose(interp.get_D(), [[1, -0.5, -0.5], 580 580 [-0.5, 0.5, 0], 581 581 [-0.5, 0, 0.5]]) 582 582 583 583 #Define f(x,y) = x 584 f = array([0,0,2]) #Value at global vertex 2584 f = numpy.array([0,0,2]) #Value at global vertex 2 585 585 586 586 #Check that int (df/dx)**2 + (df/dy)**2 dx dy = 587 587 # int 1 dx dy = area = 2 588 assert dot(dot(f, interp.get_D()), f) == 2588 assert numpy.dot(numpy.dot(f, interp.get_D()), f) == 2 589 589 590 590 #Define f(x,y) = y 591 f = array([0,2,0]) #Value at global vertex 1591 f = numpy.array([0,2,0]) #Value at global vertex 1 592 592 593 593 #Check that int (df/dx)**2 + (df/dy)**2 dx dy = 594 594 # int 1 dx dy = area = 2 595 assert dot(dot(f, interp.get_D()), f) == 2595 assert numpy.dot(numpy.dot(f, interp.get_D()), f) == 2 596 596 597 597 #Define f(x,y) = x+y 598 f = array([0,2,2]) #Values at global vertex 1 and 2598 f = numpy.array([0,2,2]) #Values at global vertex 1 and 2 599 599 600 600 #Check that int (df/dx)**2 + (df/dy)**2 dx dy = 601 601 # int 2 dx dy = 2*area = 4 602 assert dot(dot(f, interp.get_D()), f) == 4602 assert numpy.dot(numpy.dot(f, interp.get_D()), f) == 4 603 603 604 604 605 605 def test_smoothing_matrix_more_triangles(self): 606 from Numeric import dot606 ## from numpy.oldnumeric import dot 607 607 608 608 a = [0.0, 0.0] … … 625 625 626 626 #Define f(x,y) = x 627 f = array([0,0,2,0,2,4]) #f evaluated at points a-f627 f = numpy.array([0,0,2,0,2,4]) #f evaluated at points a-f 628 628 629 629 #Check that int (df/dx)**2 + (df/dy)**2 dx dy = 630 630 # int 1 dx dy = total area = 8 631 assert dot(dot(f, interp.get_D()), f) == 8631 assert numpy.dot(numpy.dot(f, interp.get_D()), f) == 8 632 632 633 633 #Define f(x,y) = y 634 f = array([0,2,0,4,2,0]) #f evaluated at points a-f634 f = numpy.array([0,2,0,4,2,0]) #f evaluated at points a-f 635 635 636 636 #Check that int (df/dx)**2 + (df/dy)**2 dx dy = 637 637 # int 1 dx dy = area = 8 638 assert dot(dot(f, interp.get_D()), f) == 8638 assert numpy.dot(numpy.dot(f, interp.get_D()), f) == 8 639 639 640 640 #Define f(x,y) = x+y 641 f = array([0,2,2,4,4,4]) #f evaluated at points a-f641 f = numpy.array([0,2,2,4,4,4]) #f evaluated at points a-f 642 642 643 643 #Check that int (df/dx)**2 + (df/dy)**2 dx dy = 644 644 # int 2 dx dy = 2*area = 16 645 assert dot(dot(f, interp.get_D()), f) == 16645 assert numpy.dot(numpy.dot(f, interp.get_D()), f) == 16 646 646 647 647 … … 709 709 f1 = interp.fit(data_points1,z) 710 710 711 assert allclose(f,f1), 'Fit should have been unaltered'711 assert numpy.allclose(f,f1), 'Fit should have been unaltered' 712 712 713 713 … … 736 736 answer = [[0, 0], [5., 10.], [5., 10.]] 737 737 738 assert allclose(f, answer)738 assert numpy.allclose(f, answer) 739 739 740 740 def test_fit_to_mesh_w_georef(self): … … 773 773 mesh_origin = mesh_geo.get_origin(), 774 774 alpha = 0) 775 assert allclose( zz, [0,5,5] )775 assert numpy.allclose( zz, [0,5,5] ) 776 776 777 777 … … 819 819 [5.0, 10.0], 820 820 [5.0,10.0]] 821 assert allclose(mesh_dic['vertex_attributes'],ans)821 assert numpy.allclose(mesh_dic['vertex_attributes'],ans) 822 822 823 823 self.failUnless(mesh_dic['vertex_attribute_titles'] == … … 827 827 828 828 answer = [0., 5., 5.] 829 assert allclose(domain.quantities['elevation'].vertex_values,829 assert numpy.allclose(domain.quantities['elevation'].vertex_values, 830 830 answer) 831 831 #clean up … … 878 878 [5.0, 10.0], 879 879 [5.0,10.0]] 880 assert allclose(mesh_dic['vertex_attributes'],ans)880 assert numpy.allclose(mesh_dic['vertex_attributes'],ans) 881 881 882 882 self.failUnless(mesh_dic['vertex_attribute_titles'] == … … 929 929 mesh_dic = import_mesh_file(mesh_output_file) 930 930 931 assert allclose(mesh_dic['vertex_attributes'],931 assert numpy.allclose(mesh_dic['vertex_attributes'], 932 932 [[1.0, 2.0,0.0, 0.0], 933 933 [1.0, 2.0,5.0, 10.0], -
anuga_core/source_numpy_conversion/anuga/fit_interpolate/test_interpolate.py
r5775 r5905 14 14 15 15 from Scientific.IO.NetCDF import NetCDFFile 16 from Numeric import allclose, array, transpose, zeros, Float, sometrue, \ 17 alltrue, take, where 18 16 import numpy 19 17 20 18 # ANUGA code imports … … 28 26 29 27 def distance(x, y): 30 return sqrt( sum( (array(x)-array(y))**2 ))28 return sqrt( numpy.sum( (numpy.array(x)-numpy.array(y))**2 )) 31 29 32 30 def linear_function(point): 33 point = array(point)31 point = numpy.array(point) 34 32 return point[:,0]+point[:,1] 35 33 … … 66 64 67 65 bed = domain.quantities['elevation'].vertex_values 68 stage = zeros(bed.shape, Float)66 stage = numpy.zeros(bed.shape, numpy.float) 69 67 70 68 h = 0.3 … … 104 102 interp = Interpolate(points, vertices) 105 103 A, _, _ = interp._build_interpolation_matrix_A(data) 106 assert allclose(A.todense(),104 assert numpy.allclose(A.todense(), 107 105 [[1./3, 1./3, 1./3]]) 108 106 … … 113 111 from mesh_factory import rectangular 114 112 from shallow_water import Domain 115 from Numeric import zeros, Float116 113 from abstract_2d_finite_volumes.quantity import Quantity 117 114 … … 130 127 131 128 x, y, vertex_values, triangles = quantity.get_vertex_values(xy=True, smooth=False) 132 vertex_coordinates = concatenate( (x[:, NewAxis], y[:, NewAxis]), axis=1 )129 vertex_coordinates = numpy.concatenate( (x[:, numpy.newaxis], y[:, numpy.newaxis]), axis=1 ) 133 130 # FIXME: This concat should roll into get_vertex_values 134 131 … … 140 137 I = Interpolate(vertex_coordinates, triangles) 141 138 result = I.interpolate(vertex_values, interpolation_points) 142 assert allclose(result, answer)139 assert numpy.allclose(result, answer) 143 140 144 141 … … 150 147 151 148 x, y, vertex_values, triangles = quantity.get_vertex_values(xy=True, smooth=False) 152 vertex_coordinates = concatenate( (x[:, NewAxis], y[:, NewAxis]), axis=1 )149 vertex_coordinates = numpy.concatenate( (x[:, numpy.newaxis], y[:, numpy.newaxis]), axis=1 ) 153 150 # FIXME: This concat should roll into get_vertex_values 154 151 … … 160 157 I = Interpolate(vertex_coordinates, triangles) 161 158 result = I.interpolate(vertex_values, interpolation_points) 162 assert allclose(result, answer)159 assert numpy.allclose(result, answer) 163 160 164 161 … … 167 164 from mesh_factory import rectangular 168 165 from shallow_water import Domain 169 from Numeric import zeros, Float170 166 from abstract_2d_finite_volumes.quantity import Quantity 171 167 … … 184 180 185 181 x, y, vertex_values, triangles = quantity.get_vertex_values(xy=True, smooth=False) 186 vertex_coordinates = concatenate( (x[:, NewAxis], y[:, NewAxis]), axis=1 )182 vertex_coordinates = numpy.concatenate( (x[:, numpy.newaxis], y[:, numpy.newaxis]), axis=1 ) 187 183 # FIXME: This concat should roll into get_vertex_values 188 184 … … 193 189 194 190 result = interpolate(vertex_coordinates, triangles, vertex_values, interpolation_points) 195 assert allclose(result, answer)191 assert numpy.allclose(result, answer) 196 192 197 193 … … 203 199 204 200 x, y, vertex_values, triangles = quantity.get_vertex_values(xy=True, smooth=False) 205 vertex_coordinates = concatenate( (x[:, NewAxis], y[:, NewAxis]), axis=1 )201 vertex_coordinates = numpy.concatenate( (x[:, numpy.newaxis], y[:, numpy.newaxis]), axis=1 ) 206 202 # FIXME: This concat should roll into get_vertex_values 207 203 … … 213 209 result = interpolate(vertex_coordinates, triangles, 214 210 vertex_values, interpolation_points) 215 assert allclose(result, answer)211 assert numpy.allclose(result, answer) 216 212 217 213 … … 220 216 from mesh_factory import rectangular 221 217 from shallow_water import Domain 222 from Numeric import zeros, Float223 218 from abstract_2d_finite_volumes.quantity import Quantity 224 219 … … 236 231 237 232 x, y, vertex_values, triangles = quantity.get_vertex_values(xy=True, smooth=False) 238 vertex_coordinates = concatenate( (x[:, NewAxis], y[:, NewAxis]), axis=1 )233 vertex_coordinates = numpy.concatenate( (x[:, numpy.newaxis], y[:, numpy.newaxis]), axis=1 ) 239 234 # FIXME: This concat should roll into get_vertex_values 240 235 … … 248 243 use_cache=True, 249 244 verbose=False) 250 assert allclose(result, answer)245 assert numpy.allclose(result, answer) 251 246 252 247 # Second call using the cache … … 255 250 use_cache=True, 256 251 verbose=False) 257 assert allclose(result, answer)252 assert numpy.allclose(result, answer) 258 253 259 254 … … 289 284 290 285 A,_,_ = interp._build_interpolation_matrix_A(data) 291 assert allclose(A.todense(), answer)286 assert numpy.allclose(A.todense(), answer) 292 287 293 288 #interp.set_point_coordinates([[-30, -30]]) #point outside of mesh … … 298 293 299 294 A,_,_ = interp._build_interpolation_matrix_A(data) 300 assert allclose(A.todense(), answer)295 assert numpy.allclose(A.todense(), answer) 301 296 302 297 … … 309 304 310 305 A,_,_ = interp._build_interpolation_matrix_A(data) 311 assert allclose(A.todense(), answer)306 assert numpy.allclose(A.todense(), answer) 312 307 313 308 … … 330 325 331 326 A,_,_ = interp._build_interpolation_matrix_A(data) 332 assert allclose(A.todense(), answer)327 assert numpy.allclose(A.todense(), answer) 333 328 334 329 … … 351 346 352 347 A,_,_ = interp._build_interpolation_matrix_A(data) 353 assert allclose(A.todense(), answer)348 assert numpy.allclose(A.todense(), answer) 354 349 355 350 def test_datapoints_on_edges(self): … … 372 367 373 368 A,_,_ = interp._build_interpolation_matrix_A(data) 374 assert allclose(A.todense(), answer)369 assert numpy.allclose(A.todense(), answer) 375 370 376 371 … … 379 374 380 375 381 from Numeric import sum382 376 383 377 a = [0.0, 0.0] … … 394 388 A,_,_ = interp._build_interpolation_matrix_A(data) 395 389 results = A.todense() 396 assert allclose(sum(results, axis=1), 1.0)390 assert numpy.allclose(numpy.sum(results, axis=1), 1.0) 397 391 398 392 def test_arbitrary_datapoints_some_outside(self): … … 401 395 402 396 403 from Numeric import sum404 397 405 398 a = [0.0, 0.0] … … 415 408 A,_,_ = interp._build_interpolation_matrix_A(data) 416 409 results = A.todense() 417 assert allclose(sum(results, axis=1), [1,1,1,0])410 assert numpy.allclose(numpy.sum(results, axis=1), [1,1,1,0]) 418 411 419 412 … … 448 441 for i in range(A.shape[0]): 449 442 for j in range(A.shape[1]): 450 if not allclose(A[i,j], answer[i][j]):443 if not numpy.allclose(A[i,j], answer[i][j]): 451 444 print i,j,':',A[i,j], answer[i][j] 452 445 … … 454 447 #results = interp._build_interpolation_matrix_A(data).todense() 455 448 456 assert allclose(A, answer)449 assert numpy.allclose(A, answer) 457 450 458 451 def test_geo_ref(self): … … 481 474 #print "z",z 482 475 #print "answer",answer 483 assert allclose(z, answer)476 assert numpy.allclose(z, answer) 484 477 485 478 … … 489 482 #print "z",z 490 483 #print "answer",answer 491 assert allclose(z, answer)484 assert numpy.allclose(z, answer) 492 485 493 486 … … 516 509 #print "z",z 517 510 #print "answer",answer 518 assert allclose(z, answer)511 assert numpy.allclose(z, answer) 519 512 520 513 … … 524 517 #print "z",z 525 518 #print "answer",answer 526 assert allclose(z, answer)519 assert numpy.allclose(z, answer) 527 520 528 521 … … 552 545 #print "z",z 553 546 #print "answer",answer 554 assert allclose(z, answer)547 assert numpy.allclose(z, answer) 555 548 556 549 z = interp.interpolate(f, point_coords, start_blocking_len = 2) … … 559 552 #print "z",z 560 553 #print "answer",answer 561 assert allclose(z, answer)554 assert numpy.allclose(z, answer) 562 555 563 556 def test_interpolate_attributes_to_points(self): … … 581 574 #print "z",z 582 575 #print "answer",answer 583 assert allclose(z, answer)576 assert numpy.allclose(z, answer) 584 577 585 578 … … 589 582 #print "z",z 590 583 #print "answer",answer 591 assert allclose(z, answer)584 assert numpy.allclose(z, answer) 592 585 593 586 def test_interpolate_attributes_to_pointsII(self): … … 622 615 #print "z",z 623 616 #print "answer",answer 624 assert allclose(z, answer)617 assert numpy.allclose(z, answer) 625 618 626 619 z = interp.interpolate(f, point_coords, start_blocking_len = 2) … … 629 622 #print "z",z 630 623 #print "answer",answer 631 assert allclose(z, answer)624 assert numpy.allclose(z, answer) 632 625 633 626 def test_interpolate_attributes_to_pointsIII(self): … … 683 676 #print "***********" 684 677 685 assert allclose(z, answer)678 assert numpy.allclose(z, answer) 686 679 687 680 … … 690 683 #print "z",z 691 684 #print "answer",answer 692 assert allclose(z, answer)685 assert numpy.allclose(z, answer) 693 686 694 687 def test_interpolate_point_outside_of_mesh(self): … … 718 711 719 712 z = interp.interpolate(f, point_coords) #, verbose=True) 720 answer = array([ [NAN, NAN, NAN, NAN]]) # (-1,-1)713 answer = numpy.array([ [NAN, NAN, NAN, NAN]]) # (-1,-1) 721 714 722 715 #print "***********" … … 767 760 768 761 interp = Interpolate(vertices, triangles) 769 f = array([linear_function(vertices),2*linear_function(vertices) ])770 f = transpose(f)762 f = numpy.array([linear_function(vertices),2*linear_function(vertices) ]) 763 f = numpy.transpose(f) 771 764 #print "f",f 772 765 z = interp.interpolate(f, point_coords) 773 766 answer = [linear_function(point_coords), 774 767 2*linear_function(point_coords) ] 775 answer = transpose(answer)768 answer = numpy.transpose(answer) 776 769 #print "z",z 777 770 #print "answer",answer 778 assert allclose(z, answer)771 assert numpy.allclose(z, answer) 779 772 780 773 z = interp.interpolate(f, point_coords, start_blocking_len = 2) … … 782 775 #print "z",z 783 776 #print "answer",answer 784 assert allclose(z, answer)777 assert numpy.allclose(z, answer) 785 778 786 779 def test_interpolate_blocking(self): … … 810 803 811 804 interp = Interpolate(vertices, triangles) 812 f = array([linear_function(vertices),2*linear_function(vertices) ])813 f = transpose(f)805 f = numpy.array([linear_function(vertices),2*linear_function(vertices) ]) 806 f = numpy.transpose(f) 814 807 #print "f",f 815 808 for blocking_max in range(len(point_coords)+2): … … 820 813 answer = [linear_function(point_coords), 821 814 2*linear_function(point_coords) ] 822 answer = transpose(answer)815 answer = numpy.transpose(answer) 823 816 #print "z",z 824 817 #print "answer",answer 825 assert allclose(z, answer)818 assert numpy.allclose(z, answer) 826 819 827 f = array([linear_function(vertices),2*linear_function(vertices),820 f = numpy.array([linear_function(vertices),2*linear_function(vertices), 828 821 2*linear_function(vertices) - 100 ]) 829 f = transpose(f)822 f = numpy.transpose(f) 830 823 #print "f",f 831 824 for blocking_max in range(len(point_coords)+2): … … 834 827 z = interp.interpolate(f, point_coords, 835 828 start_blocking_len=blocking_max) 836 answer = array([linear_function(point_coords),829 answer = numpy.array([linear_function(point_coords), 837 830 2*linear_function(point_coords) , 838 831 2*linear_function(point_coords)-100 ]) 839 z = transpose(z)832 z = numpy.transpose(z) 840 833 #print "z",z 841 834 #print "answer",answer 842 assert allclose(z, answer)835 assert numpy.allclose(z, answer) 843 836 844 837 def test_interpolate_geo_spatial(self): … … 872 865 873 866 interp = Interpolate(vertices, triangles) 874 f = array([linear_function(vertices),2*linear_function(vertices) ])875 f = transpose(f)867 f = numpy.array([linear_function(vertices),2*linear_function(vertices) ]) 868 f = numpy.transpose(f) 876 869 #print "f",f 877 870 for blocking_max in range(14): … … 884 877 2*linear_function(point_coords.get_data_points( \ 885 878 absolute = True)) ] 886 answer = transpose(answer)879 answer = numpy.transpose(answer) 887 880 #print "z",z 888 881 #print "answer",answer 889 assert allclose(z, answer)882 assert numpy.allclose(z, answer) 890 883 891 f = array([linear_function(vertices),2*linear_function(vertices),884 f = numpy.array([linear_function(vertices),2*linear_function(vertices), 892 885 2*linear_function(vertices) - 100 ]) 893 f = transpose(f)886 f = numpy.transpose(f) 894 887 #print "f",f 895 888 for blocking_max in range(14): … … 898 891 z = interp.interpolate(f, point_coords, 899 892 start_blocking_len=blocking_max) 900 answer = array([linear_function(point_coords.get_data_points( \893 answer = numpy.array([linear_function(point_coords.get_data_points( \ 901 894 absolute = True)), 902 895 2*linear_function(point_coords.get_data_points( \ … … 904 897 2*linear_function(point_coords.get_data_points( \ 905 898 absolute = True))-100 ]) 906 z = transpose(z)899 z = numpy.transpose(z) 907 900 #print "z",z 908 901 #print "answer",answer 909 assert allclose(z, answer)902 assert numpy.allclose(z, answer) 910 903 911 904 z = interp.interpolate(f, point_coords, start_blocking_len = 2) … … 913 906 #print "z",z 914 907 #print "answer",answer 915 assert allclose(z, answer)908 assert numpy.allclose(z, answer) 916 909 917 910 def test_interpolate_geo_spatial(self): … … 945 938 946 939 interp = Interpolate(vertices, triangles) 947 f = array([linear_function(vertices),2*linear_function(vertices) ])948 f = transpose(f)940 f = numpy.array([linear_function(vertices),2*linear_function(vertices) ]) 941 f = numpy.transpose(f) 949 942 #print "f",f 950 943 z = interp.interpolate_block(f, point_coords) … … 953 946 2*linear_function(point_coords.get_data_points( \ 954 947 absolute = True)) ] 955 answer = transpose(answer)948 answer = numpy.transpose(answer) 956 949 #print "z",z 957 950 #print "answer",answer 958 assert allclose(z, answer)951 assert numpy.allclose(z, answer) 959 952 960 953 z = interp.interpolate(f, point_coords, start_blocking_len = 2) … … 962 955 #print "z",z 963 956 #print "answer",answer 964 assert allclose(z, answer)957 assert numpy.allclose(z, answer) 965 958 966 959 … … 991 984 992 985 interp = Interpolate(vertices, triangles) 993 f = array([linear_function(vertices),2*linear_function(vertices) ])994 f = transpose(f)986 f = numpy.array([linear_function(vertices),2*linear_function(vertices) ]) 987 f = numpy.transpose(f) 995 988 z = interp.interpolate(f, point_coords, 996 989 start_blocking_len=20) 997 990 answer = [linear_function(point_coords), 998 991 2*linear_function(point_coords) ] 999 answer = transpose(answer)992 answer = numpy.transpose(answer) 1000 993 #print "z",z 1001 994 #print "answer",answer 1002 assert allclose(z, answer)1003 assert allclose(interp._A_can_be_reused, True)995 assert numpy.allclose(z, answer) 996 assert numpy.allclose(interp._A_can_be_reused, True) 1004 997 1005 998 z = interp.interpolate(f) 1006 assert allclose(z, answer)999 assert numpy.allclose(z, answer) 1007 1000 1008 1001 # This causes blocking to occur. 1009 1002 z = interp.interpolate(f, start_blocking_len=10) 1010 assert allclose(z, answer)1011 assert allclose(interp._A_can_be_reused, False)1003 assert numpy.allclose(z, answer) 1004 assert numpy.allclose(interp._A_can_be_reused, False) 1012 1005 1013 1006 #A is recalculated 1014 1007 z = interp.interpolate(f) 1015 assert allclose(z, answer)1016 assert allclose(interp._A_can_be_reused, True)1008 assert numpy.allclose(z, answer) 1009 assert numpy.allclose(interp._A_can_be_reused, True) 1017 1010 1018 1011 interp = Interpolate(vertices, triangles) … … 1054 1047 1055 1048 interp = Interpolate(vertices, triangles) 1056 f = array([linear_function(vertices),2*linear_function(vertices) ])1057 f = transpose(f)1049 f = numpy.array([linear_function(vertices),2*linear_function(vertices) ]) 1050 f = numpy.transpose(f) 1058 1051 z = interp.interpolate(f, point_coords) 1059 1052 answer = [linear_function(point_coords), 1060 1053 2*linear_function(point_coords) ] 1061 answer = transpose(answer)1062 1063 assert allclose(z, answer)1064 assert allclose(interp._A_can_be_reused, True)1054 answer = numpy.transpose(answer) 1055 1056 assert numpy.allclose(z, answer) 1057 assert numpy.allclose(interp._A_can_be_reused, True) 1065 1058 1066 1059 1067 1060 z = interp.interpolate(f) # None 1068 assert allclose(z, answer)1061 assert numpy.allclose(z, answer) 1069 1062 z = interp.interpolate(f, point_coords) # Repeated (not really a test) 1070 assert allclose(z, answer)1063 assert numpy.allclose(z, answer) 1071 1064 1072 1065 … … 1085 1078 1086 1079 #One quantity 1087 Q = zeros( (3,6), Float )1080 Q = numpy.zeros( (3,6), numpy.float ) 1088 1081 1089 1082 #Linear in time and space … … 1111 1104 #Check temporal interpolation 1112 1105 for i in [0,1,2]: 1113 assert allclose(I(time[i]), mean(Q[i,:]))1106 assert numpy.allclose(I(time[i]), mean(Q[i,:])) 1114 1107 1115 1108 #Midway 1116 assert allclose(I( (time[0] + time[1])/2 ),1109 assert numpy.allclose(I( (time[0] + time[1])/2 ), 1117 1110 (I(time[0]) + I(time[1]))/2 ) 1118 1111 1119 assert allclose(I( (time[1] + time[2])/2 ),1112 assert numpy.allclose(I( (time[1] + time[2])/2 ), 1120 1113 (I(time[1]) + I(time[2]))/2 ) 1121 1114 1122 assert allclose(I( (time[0] + time[2])/2 ),1115 assert numpy.allclose(I( (time[0] + time[2])/2 ), 1123 1116 (I(time[0]) + I(time[2]))/2 ) 1124 1117 1125 1118 #1/3 1126 assert allclose(I( (time[0] + time[2])/3 ),1119 assert numpy.allclose(I( (time[0] + time[2])/3 ), 1127 1120 (I(time[0]) + I(time[2]))/3 ) 1128 1121 … … 1191 1184 for j in range(50): #t in [1, 6] 1192 1185 for id in range(len(interpolation_points)): 1193 assert allclose(I(t, id), answer[id])1186 assert numpy.allclose(I(t, id), answer[id]) 1194 1187 t += 0.1 1195 1188 … … 1232 1225 1233 1226 #One quantity 1234 Q = zeros( (3,6), Float )1227 Q = numpy.zeros( (3,6), numpy.float ) 1235 1228 1236 1229 #Linear in time and space … … 1250 1243 for j in range(50): #t in [1, 6] 1251 1244 for id in range(len(interpolation_points)): 1252 assert allclose(I(t, id), t*answer[id])1245 assert numpy.allclose(I(t, id), t*answer[id]) 1253 1246 t += 0.1 1254 1247 … … 1292 1285 1293 1286 #One quantity 1294 Q = zeros( (8,6), Float )1287 Q = numpy.zeros( (8,6), numpy.float ) 1295 1288 1296 1289 #Linear in time and space … … 1312 1305 for j in range(50): #t in [1, 6] 1313 1306 for id in range(len(interpolation_points)): 1314 assert allclose(I(t, id), t*answer[id])1307 assert numpy.allclose(I(t, id), t*answer[id]) 1315 1308 t += 0.1 1316 1309 … … 1326 1319 1327 1320 assert len(I.time) == 4 1328 assert( allclose(I.time, [1.0, 4.0, 7.0, 9.0] ))1321 assert( numpy.allclose(I.time, [1.0, 4.0, 7.0, 9.0] )) 1329 1322 1330 1323 answer = linear_function(interpolation_points) … … 1333 1326 for j in range(50): #t in [1, 6] 1334 1327 for id in range(len(interpolation_points)): 1335 assert allclose(I(t, id), t*answer[id])1328 assert numpy.allclose(I(t, id), t*answer[id]) 1336 1329 t += 0.1 1337 1330 … … 1361 1354 1362 1355 #One quantity 1363 Q = zeros( (2,6), Float )1356 Q = numpy.zeros( (2,6), numpy.float ) 1364 1357 1365 1358 #Linear in time and space … … 1371 1364 1372 1365 interp = Interpolate(points, triangles) 1373 f = array([linear_function(points),2*linear_function(points) ])1374 f = transpose(f)1366 f = numpy.array([linear_function(points),2*linear_function(points) ]) 1367 f = numpy.transpose(f) 1375 1368 #print "f",f 1376 1369 z = interp.interpolate(f, interpolation_points) 1377 1370 answer = [linear_function(interpolation_points), 1378 1371 2*linear_function(interpolation_points) ] 1379 answer = transpose(answer)1372 answer = numpy.transpose(answer) 1380 1373 #print "z",z 1381 1374 #print "answer",answer 1382 assert allclose(z, answer)1375 assert numpy.allclose(z, answer) 1383 1376 1384 1377 … … 1393 1386 1394 1387 msg = 'Interpolation failed' 1395 assert allclose(I.precomputed_values['Attribute'][1], [60, 60]), msg1388 assert numpy.allclose(I.precomputed_values['Attribute'][1], [60, 60]), msg 1396 1389 #self.failUnless( I.precomputed_values['Attribute'][1] == 60.0, 1397 1390 # ' failed') … … 1427 1420 1428 1421 # One quantity 1429 Q = zeros( (3,6), Float )1422 Q = numpy.zeros( (3,6), numpy.float ) 1430 1423 1431 1424 # Linear in time and space … … 1442 1435 1443 1436 1444 assert alltrue(I.precomputed_values['Attribute'][:,4] != NAN)1445 assert sometrue(I.precomputed_values['Attribute'][:,5] == NAN)1437 assert numpy.alltrue(I.precomputed_values['Attribute'][:,4] != NAN) 1438 assert numpy.sometrue(I.precomputed_values['Attribute'][:,5] == NAN) 1446 1439 1447 1440 #X = I.precomputed_values['Attribute'][1,:] … … 1455 1448 for j in range(50): #t in [1, 6] 1456 1449 for id in range(len(interpolation_points)-1): 1457 assert allclose(I(t, id), t*answer[id])1450 assert numpy.allclose(I(t, id), t*answer[id]) 1458 1451 t += 0.1 1459 1452 … … 1477 1470 1478 1471 1479 time = array(\1472 time = numpy.array(\ 1480 1473 [0.00000000e+00, 5.00000000e-02, 1.00000000e-01, 1.50000000e-01, 1481 1474 2.00000000e-01, 2.50000000e-01, 3.00000000e-01, 3.50000000e-01, … … 1616 1609 1617 1610 #One quantity 1618 Q = zeros( (len(time),6), Float )1611 Q = numpy.zeros( (len(time),6), numpy.float ) 1619 1612 1620 1613 #Linear in time and space … … 1661 1654 1662 1655 interp = Interpolate(vertices, triangles) 1663 f = array([linear_function(vertices),2*linear_function(vertices) ])1664 f = transpose(f)1656 f = numpy.array([linear_function(vertices),2*linear_function(vertices) ]) 1657 f = numpy.transpose(f) 1665 1658 #print "f",f 1666 1659 z = interp.interpolate(f, geo_data) … … 1668 1661 answer = [linear_function(point_coords), 1669 1662 2*linear_function(point_coords) ] 1670 answer = transpose(answer)1663 answer = numpy.transpose(answer) 1671 1664 answer[2,:] = [NAN, NAN] 1672 1665 answer[3,:] = [NAN, NAN] … … 1674 1667 #print "z",z 1675 1668 #print "answer _ fixed",answer 1676 assert allclose(z[0:1], answer[0:1])1677 assert allclose(z[4:10], answer[4:10])1669 assert numpy.allclose(z[0:1], answer[0:1]) 1670 assert numpy.allclose(z[4:10], answer[4:10]) 1678 1671 for i in [2,3,11]: 1679 1672 self.failUnless( z[i,1] == answer[11,1], 'Fail!') … … 1765 1758 #print "velocity_answers_array", velocity_answers[i] 1766 1759 msg = 'Interpolation failed' 1767 assert allclose(float(depths[i]), depth_answers[i]), msg1768 assert allclose(float(velocitys[i]), velocity_answers[i]), msg1760 assert numpy.allclose(float(depths[i]), depth_answers[i]), msg 1761 assert numpy.allclose(float(velocitys[i]), velocity_answers[i]), msg 1769 1762 1770 1763 velocity_y_file_handle = file(velocity_y_file) … … 1780 1773 #print "velocity_answers_array", velocity_answers[i] 1781 1774 msg = 'Interpolation failed' 1782 assert allclose(float(depths[i]), depth_answers[i]), msg1783 assert allclose(float(velocitys[i]), velocity_answers[i]), msg1775 assert numpy.allclose(float(depths[i]), depth_answers[i]), msg 1776 assert numpy.allclose(float(velocitys[i]), velocity_answers[i]), msg 1784 1777 1785 1778 # clean up … … 1852 1845 #print "z",z 1853 1846 #print "answer",answer 1854 assert allclose(z, answer)1847 assert numpy.allclose(z, answer) 1855 1848 1856 1849 #------------------------------------------------------------- -
anuga_core/source_numpy_conversion/anuga/fit_interpolate/test_search_functions.py
r4859 r5905 5 5 from search_functions import search_tree_of_vertices, set_last_triangle 6 6 from search_functions import _search_triangles_of_vertices 7 8 9 from Numeric import zeros, array, allclose10 7 11 8 from anuga.abstract_2d_finite_volumes.neighbour_mesh import Mesh
Note: See TracChangeset
for help on using the changeset viewer.