Changeset 5910
 Timestamp:
 Nov 6, 2008, 4:29:25 PM (15 years ago)
 Location:
 anuga_core/source_numpy_conversion/anuga/load_mesh
 Files:

 2 edited
Legend:
 Unmodified
 Added
 Removed

anuga_core/source_numpy_conversion/anuga/load_mesh/loadASCII.py
r4903 r5910 57 57 58 58 from string import find, rfind 59 from Numeric import array, Float, Int16, Int32, Character,reshape, concatenate, take 59 import numpy 60 60 from os.path import splitext 61 61 … … 644 644 # in more detail. 645 645 646 IntType = Int32646 IntType = numpy.int32 647 647 #IntType = Int 648 648 649 649 # 650 650 #the triangulation 651 mesh['vertices'] = array(mesh['vertices']).astype(Float)651 mesh['vertices'] = numpy.array(mesh['vertices']).astype(numpy.float) 652 652 if mesh['vertex_attributes'] != None: 653 653 mesh['vertex_attributes'] = \ 654 array(mesh['vertex_attributes']).astype(Float)655 mesh['vertex_attribute_titles'] = array(mesh['vertex_attribute_titles']).astype(Character)656 mesh['segments'] = array(mesh['segments']).astype(IntType)657 mesh['segment_tags'] = array(mesh['segment_tags']).astype(Character)658 mesh['triangles'] = array(mesh['triangles']).astype(IntType)659 mesh['triangle_tags'] = array(mesh['triangle_tags']) #.astype(Character)660 mesh['triangle_neighbors'] = array(mesh['triangle_neighbors']).astype(IntType)654 numpy.array(mesh['vertex_attributes']).astype(numpy.float) 655 mesh['vertex_attribute_titles'] = numpy.array(mesh['vertex_attribute_titles']).astype(numpy.character) 656 mesh['segments'] = numpy.array(mesh['segments']).astype(IntType) 657 mesh['segment_tags'] = numpy.array(mesh['segment_tags']).astype(numpy.character) 658 mesh['triangles'] = numpy.array(mesh['triangles']).astype(IntType) 659 mesh['triangle_tags'] = numpy.array(mesh['triangle_tags']) #.astype(numpy.character) 660 mesh['triangle_neighbors'] = numpy.array(mesh['triangle_neighbors']).astype(IntType) 661 661 662 662 #the outline 663 mesh['points'] = array(mesh['points']).astype(Float)664 mesh['point_attributes'] = array(mesh['point_attributes']).astype(Float)665 mesh['outline_segments'] = array(mesh['outline_segments']).astype(IntType)666 mesh['outline_segment_tags'] = array(mesh['outline_segment_tags']).astype(Character)667 mesh['holes'] = array(mesh['holes']).astype(Float)668 mesh['regions'] = array(mesh['regions']).astype(Float)669 mesh['region_tags'] = array(mesh['region_tags']).astype(Character)670 mesh['region_max_areas'] = array(mesh['region_max_areas']).astype(Float)663 mesh['points'] = numpy.array(mesh['points']).astype(numpy.float) 664 mesh['point_attributes'] = numpy.array(mesh['point_attributes']).astype(numpy.float) 665 mesh['outline_segments'] = numpy.array(mesh['outline_segments']).astype(IntType) 666 mesh['outline_segment_tags'] = numpy.array(mesh['outline_segment_tags']).astype(numpy.character) 667 mesh['holes'] = numpy.array(mesh['holes']).astype(numpy.float) 668 mesh['regions'] = numpy.array(mesh['regions']).astype(numpy.float) 669 mesh['region_tags'] = numpy.array(mesh['region_tags']).astype(numpy.character) 670 mesh['region_max_areas'] = numpy.array(mesh['region_max_areas']).astype(numpy.float) 671 671 672 672 #mesh = mesh_dict2array(mesh) … … 699 699 if (mesh['vertices'].shape[0] > 0): 700 700 outfile.createDimension('num_of_vertices', mesh['vertices'].shape[0]) 701 outfile.createVariable('vertices', Float, ('num_of_vertices',701 outfile.createVariable('vertices', numpy.float, ('num_of_vertices', 702 702 'num_of_dimensions')) 703 703 outfile.variables['vertices'][:] = mesh['vertices'] … … 709 709 mesh['vertex_attribute_titles'].shape[1]) 710 710 outfile.createVariable('vertex_attributes', 711 Float,711 numpy.float, 712 712 ('num_of_vertices', 713 713 'num_of_vertex_attributes')) 714 714 outfile.createVariable('vertex_attribute_titles', 715 Character,715 numpy.character, 716 716 ( 'num_of_vertex_attributes', 717 717 'num_of_vertex_attribute_title_chars' )) … … 732 732 mesh['segment_tags'].shape[1]) 733 733 outfile.createVariable('segment_tags', 734 Character,734 numpy.character, 735 735 ('num_of_segments', 736 736 'num_of_segment_tag_chars')) … … 755 755 mesh['triangle_tags'].shape[1]) 756 756 outfile.createVariable('triangle_tags', 757 Character,757 numpy.character, 758 758 ('num_of_triangles', 759 759 'num_of_triangle_tag_chars')) … … 765 765 if (mesh['points'].shape[0] > 0): 766 766 outfile.createDimension('num_of_points', mesh['points'].shape[0]) 767 outfile.createVariable('points', Float, ('num_of_points',767 outfile.createVariable('points', numpy.float, ('num_of_points', 768 768 'num_of_dimensions')) 769 769 outfile.variables['points'][:] = mesh['points'] … … 772 772 mesh['point_attributes'].shape[1]) 773 773 outfile.createVariable('point_attributes', 774 Float,774 numpy.float, 775 775 ('num_of_points', 776 776 'num_of_point_attributes')) … … 789 789 mesh['outline_segment_tags'].shape[1]) 790 790 outfile.createVariable('outline_segment_tags', 791 Character,791 numpy.character, 792 792 ('num_of_outline_segments', 793 793 'num_of_outline_segment_tag_chars')) … … 796 796 if (mesh['holes'].shape[0] > 0): 797 797 outfile.createDimension('num_of_holes', mesh['holes'].shape[0]) 798 outfile.createVariable('holes', Float, ('num_of_holes',798 outfile.createVariable('holes', numpy.float, ('num_of_holes', 799 799 'num_of_dimensions')) 800 800 outfile.variables['holes'][:] = mesh['holes'] … … 802 802 if (mesh['regions'].shape[0] > 0): 803 803 outfile.createDimension('num_of_regions', mesh['regions'].shape[0]) 804 outfile.createVariable('regions', Float, ('num_of_regions',804 outfile.createVariable('regions', numpy.float, ('num_of_regions', 805 805 'num_of_dimensions')) 806 806 outfile.createVariable('region_max_areas', 807 Float,807 numpy.float, 808 808 ('num_of_regions',)) 809 809 outfile.variables['regions'][:] = mesh['regions'] … … 813 813 mesh['region_tags'].shape[1]) 814 814 outfile.createVariable('region_tags', 815 Character,815 numpy.character, 816 816 ('num_of_regions', 817 817 'num_of_region_tag_chars')) … … 848 848 mesh['vertices'] = fid.variables['vertices'][:] 849 849 except KeyError: 850 mesh['vertices'] = array([])850 mesh['vertices'] = numpy.array([]) 851 851 try: 852 852 mesh['vertex_attributes'] = fid.variables['vertex_attributes'][:] … … 866 866 mesh['segments'] = fid.variables['segments'][:] 867 867 except KeyError: 868 mesh['segments'] = array([])868 mesh['segments'] = numpy.array([]) 869 869 mesh['segment_tags'] =[] 870 870 try: … … 879 879 mesh['triangle_neighbors'] = fid.variables['triangle_neighbors'][:] 880 880 except KeyError: 881 mesh['triangles'] = array([])882 mesh['triangle_neighbors'] = array([])881 mesh['triangles'] = numpy.array([]) 882 mesh['triangle_neighbors'] = numpy.array([]) 883 883 mesh['triangle_tags'] =[] 884 884 try: … … 904 904 mesh['outline_segments'] = fid.variables['outline_segments'][:] 905 905 except KeyError: 906 mesh['outline_segments'] = array([])906 mesh['outline_segments'] = numpy.array([]) 907 907 mesh['outline_segment_tags'] =[] 908 908 try: … … 916 916 mesh['holes'] = fid.variables['holes'][:] 917 917 except KeyError: 918 mesh['holes'] = array([])918 mesh['holes'] = numpy.array([]) 919 919 try: 920 920 mesh['regions'] = fid.variables['regions'][:] 921 921 except KeyError: 922 mesh['regions'] = array([])922 mesh['regions'] = numpy.array([]) 923 923 mesh['region_tags'] =[] 924 924 try: … … 932 932 mesh['region_max_areas'] = fid.variables['region_max_areas'][:] 933 933 except KeyError: 934 mesh['region_max_areas'] = array([])934 mesh['region_max_areas'] = numpy.array([]) 935 935 #mesh[''] = fid.variables[''][:] 936 936 … … 985 985 986 986 def extent(points): 987 points = array(points).astype(Float)987 points = numpy.array(points).astype(numpy.float) 988 988 max_x = min_x = points[0][0] 989 989 max_y = min_y = points[0][1] … … 995 995 if y > max_y: max_y = y 996 996 if y < min_y: min_y = y 997 extent = array([[min_x,min_y],[max_x,min_y],[max_x,max_y],[min_x,max_y]])997 extent = numpy.array([[min_x,min_y],[max_x,min_y],[max_x,max_y],[min_x,max_y]]) 998 998 #print "extent",extent 999 999 return extent … … 1027 1027 1028 1028 def point_atts2array(point_atts): 1029 point_atts['pointlist'] = array(point_atts['pointlist']).astype(Float)1029 point_atts['pointlist'] = numpy.array(point_atts['pointlist']).astype(numpy.float) 1030 1030 1031 1031 for key in point_atts['attributelist'].keys(): 1032 point_atts['attributelist'][key]= array(point_atts['attributelist'][key]).astype(Float)1032 point_atts['attributelist'][key]= numpy.array(point_atts['attributelist'][key]).astype(numpy.float) 1033 1033 return point_atts 1034 1034 … … 1046 1046 return list of attribute titles, array of attributes 1047 1047 """ 1048 point_attributes = array([]).astype(Float)1048 point_attributes = numpy.array([]).astype(numpy.float) 1049 1049 keys = dic.keys() 1050 1050 key = keys.pop(0) 1051 point_attributes = reshape(dic[key],(dic[key].shape[0],1))1051 point_attributes = numpy.reshape(dic[key],(dic[key].shape[0],1)) 1052 1052 for key in keys: 1053 #point_attributes = concatenate([point_attributes, dic[key]], axis=1)1054 reshaped = reshape(dic[key],(dic[key].shape[0],1))1055 point_attributes = concatenate([point_attributes, reshaped], axis=1)1053 #point_attributes = numpy.concatenate([point_attributes, dic[key]], axis=1) 1054 reshaped = numpy.reshape(dic[key],(dic[key].shape[0],1)) 1055 point_attributes = numpy.concatenate([point_attributes, reshaped], axis=1) 1056 1056 return dic.keys(), point_attributes 1057 1057 … … 1060 1060 dict = point_atts2array(dict) 1061 1061 #FIXME maybe the points data structure should become a class? 1062 dict['pointlist'] = take(dict['pointlist'],indices_to_keep)1062 dict['pointlist'] = numpy.take(dict['pointlist'],indices_to_keep) 1063 1063 1064 1064 for key in dict['attributelist'].keys(): 1065 dict['attributelist'][key]= take(dict['attributelist'][key],1065 dict['attributelist'][key]= numpy.take(dict['attributelist'][key], 1066 1066 indices_to_keep) 1067 1067 return dict … … 1074 1074 1075 1075 combined = {} 1076 combined['pointlist'] = concatenate((dict2['pointlist'],1076 combined['pointlist'] = numpy.concatenate((dict2['pointlist'], 1077 1077 dict1['pointlist']),axis=0) 1078 1078 atts = {} 1079 1079 for key in dict2['attributelist'].keys(): 1080 atts[key]= concatenate((dict2['attributelist'][key],1080 atts[key]= numpy.concatenate((dict2['attributelist'][key], 1081 1081 dict1['attributelist'][key]), axis=0) 1082 1082 combined['attributelist']=atts 
anuga_core/source_numpy_conversion/anuga/load_mesh/test_loadASCII.py
r5875 r5910 10 10 from os.path import splitext 11 11 12 from Numeric import array, allclose, alltrue 13 12 import numpy 13 14 14 from anuga.load_mesh.loadASCII import * 15 15 from anuga.coordinate_transforms.geo_reference import Geo_reference … … 179 179 #print "*(*(" 180 180 181 self.failUnless( alltrue(array(meshDict['vertices']) ==182 array(loadedDict['vertices'])),181 self.failUnless(numpy.array(meshDict['vertices']) == 182 numpy.array(loadedDict['vertices']), 183 183 'test_export_mesh_file failed. Test 1') 184 self.failUnless( alltrue(array(meshDict['triangles']) ==185 array(loadedDict['triangles'])),184 self.failUnless(numpy.array(meshDict['triangles']) == 185 numpy.array(loadedDict['triangles']), 186 186 'test_export_mesh_file failed. Test 2') 187 self.failUnless( alltrue(array(meshDict['segments']) ==188 array(loadedDict['segments'])),187 self.failUnless(numpy.array(meshDict['segments']) == 188 numpy.array(loadedDict['segments']), 189 189 'test_export_mesh_file failed. Test 3') 190 self.failUnless( alltrue(array(meshDict['triangle_tags']) ==191 array(loadedDict['triangle_tags'])),190 self.failUnless(numpy.array(meshDict['triangle_tags']) == 191 numpy.array(loadedDict['triangle_tags']), 192 192 'test_export_mesh_file failed. Test 4') 193 193 194 194 self.failUnless(meshDict['vertex_attributes'] == 195 195 loadedDict['vertex_attributes'], 196 196 'test_export_mesh_file failed. Test 5') 197 self.failUnless( alltrue(array(meshDict['triangle_neighbors']) ==198 array(loadedDict['triangle_neighbors'])),197 self.failUnless(numpy.array(meshDict['triangle_neighbors']) == 198 numpy.array(loadedDict['triangle_neighbors']), 199 199 'test_export_mesh_file failed. Test 6') 200 self.failUnless( alltrue(array(meshDict['segment_tags']) ==201 array(loadedDict['segment_tags'])),200 self.failUnless(numpy.array(meshDict['segment_tags']) == 201 numpy.array(loadedDict['segment_tags']), 202 202 'test_export_mesh_file failed. Test 7') 203 self.failUnless( alltrue(array(meshDict['vertex_attribute_titles']) ==204 array(loadedDict['vertex_attribute_titles'])),203 self.failUnless(numpy.array(meshDict['vertex_attribute_titles']) == 204 numpy.array(loadedDict['vertex_attribute_titles']), 205 205 'test_export_mesh_file failed. Test 8') 206 self.failUnless( alltrue(array(meshDict['geo_reference']) ==207 array(loadedDict['geo_reference'])),206 self.failUnless(numpy.array(meshDict['geo_reference']) == 207 numpy.array(loadedDict['geo_reference']), 208 208 'test_export_mesh_file failed. Test 9') 209 209 … … 439 439 440 440 def check_mesh_dicts(self, loaded_dict, dict, fail_string ): 441 assert allclose(array(loaded_dict['points']),442 array(dict['points']))441 assert numpy.allclose(numpy.array(loaded_dict['points']), 442 numpy.array(dict['points'])) 443 443 444 assert allclose(array(loaded_dict['point_attributes']),445 array(dict['point_attributes']))446 assert allclose(array(loaded_dict['outline_segments']),447 array(dict['outline_segments']))444 assert numpy.allclose(numpy.array(loaded_dict['point_attributes']), 445 numpy.array(dict['point_attributes'])) 446 assert numpy.allclose(numpy.array(loaded_dict['outline_segments']), 447 numpy.array(dict['outline_segments'])) 448 448 449 449 self.failUnless(loaded_dict['outline_segment_tags'] == … … 452 452 453 453 454 assert allclose(array(loaded_dict['regions']), 455 array(dict['regions'])) 456 454 assert numpy.allclose(numpy.array(loaded_dict['regions']), 455 numpy.array(dict['regions'])) 457 456 self.failUnless(loaded_dict['region_tags'] == 458 457 dict['region_tags'], 459 458 fail_string + ' failed!! Test 5') 460 459 461 assert allclose(array(loaded_dict['region_max_areas']),462 array(dict['region_max_areas']))460 assert numpy.allclose(numpy.array(loaded_dict['region_max_areas']), 461 numpy.array(dict['region_max_areas'])) 463 462 464 assert allclose(array(loaded_dict['holes']),465 array(dict['holes']))463 assert numpy.allclose(numpy.array(loaded_dict['holes']), 464 numpy.array(dict['holes'])) 466 465 467 assert allclose(array(dict['vertices']),468 array(loaded_dict['vertices']))466 assert numpy.allclose(numpy.array(dict['vertices']), 467 numpy.array(loaded_dict['vertices'])) 469 468 470 assert allclose(array(dict['triangles']),471 array(loaded_dict['triangles']))469 assert numpy.allclose(numpy.array(dict['triangles']), 470 numpy.array(loaded_dict['triangles'])) 472 471 473 assert allclose(array(dict['segments']),474 array(loaded_dict['segments']))472 assert numpy.allclose(numpy.array(dict['segments']), 473 numpy.array(loaded_dict['segments'])) 475 474 for ob, ldob in map(None,dict['triangle_tags'], 476 475 loaded_dict['triangle_tags']): 477 self.failUnless(ob == ldob,476 self.failUnless(ob == ldob, 478 477 fail_string + ' failed!! Test triangle_tags') 479 478 # A bit hacky … … 484 483 fail_string + ' failed!! Test vertex_attributes') 485 484 486 assert allclose(array(dict['triangle_neighbors']),487 array(loaded_dict['triangle_neighbors']))485 assert numpy.allclose(numpy.array(dict['triangle_neighbors']), 486 numpy.array(loaded_dict['triangle_neighbors'])) 488 487 489 488 for seg, ldseg in map(None,dict['segment_tags'], … … 492 491 fail_string + ' failed!! Test 8') 493 492 try: 494 assert allclose(array(dict['vertex_attribute_titles']),495 array(loaded_dict['vertex_attribute_titles']))493 assert numpy.allclose(numpy.array(dict['vertex_attribute_titles']), 494 numpy.array(loaded_dict['vertex_attribute_titles'])) 496 495 except TypeError: 497 self.failUnless(alltrue(array(loaded_dict['vertex_attribute_titles']) == array(dict['vertex_attribute_titles'])), 496 self.failUnless(numpy.array(loaded_dict['vertex_attribute_titles']) == 497 numpy.array(dict['vertex_attribute_titles']), 498 498 fail_string + ' failed!! Test 8') 499 499 try:
Note: See TracChangeset
for help on using the changeset viewer.