Changeset 6154


Ignore:
Timestamp:
Jan 13, 2009, 3:03:31 PM (10 years ago)
Author:
rwilson
Message:

Change Numeric imports to general form - ready to change to NumPy?.

Location:
anuga_core/source/anuga/load_mesh
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/load_mesh/loadASCII.py

    r6086 r6154  
    5858
    5959from string import  find, rfind
    60 from Numeric import array, Float, Int16, Int32, Character,reshape, \
    61                     concatenate, take
     60import Numeric as num
    6261from os.path import splitext
    6362
     
    650649    # in more detail.
    651650
    652     IntType = Int32
     651    IntType = num.Int32
    653652    #IntType = Int
    654653
    655654    #the triangulation
    656     mesh['vertices'] = array(mesh['vertices']).astype(Float)
     655    mesh['vertices'] = num.array(mesh['vertices']).astype(num.Float)
    657656    if mesh['vertex_attributes'] != None:
    658657        mesh['vertex_attributes'] = \
    659             array(mesh['vertex_attributes']).astype(Float)
     658            num.array(mesh['vertex_attributes']).astype(num.Float)
    660659    mesh['vertex_attribute_titles'] = \
    661         array(mesh['vertex_attribute_titles']).astype(Character)
    662     mesh['segments'] = array(mesh['segments']).astype(IntType)
    663     mesh['segment_tags'] = array(mesh['segment_tags']).astype(Character)
    664     mesh['triangles'] = array(mesh['triangles']).astype(IntType)
    665     mesh['triangle_tags'] = array(mesh['triangle_tags']) #.astype(Character)
     660        num.array(mesh['vertex_attribute_titles']).astype(num.Character)
     661    mesh['segments'] = num.array(mesh['segments']).astype(IntType)
     662    mesh['segment_tags'] = num.array(mesh['segment_tags']).astype(num.Character)
     663    mesh['triangles'] = num.array(mesh['triangles']).astype(IntType)
     664    mesh['triangle_tags'] = num.array(mesh['triangle_tags']) #.astype(Character)
    666665    mesh['triangle_neighbors'] = \
    667         array(mesh['triangle_neighbors']).astype(IntType)
     666        num.array(mesh['triangle_neighbors']).astype(IntType)
    668667
    669668    #the outline
    670     mesh['points'] = array(mesh['points']).astype(Float)
    671     mesh['point_attributes'] = array(mesh['point_attributes']).astype(Float)
    672     mesh['outline_segments'] = array(mesh['outline_segments']).astype(IntType)
     669    mesh['points'] = num.array(mesh['points']).astype(num.Float)
     670    mesh['point_attributes'] = num.array(mesh['point_attributes']).astype(num.Float)
     671    mesh['outline_segments'] = num.array(mesh['outline_segments']).astype(IntType)
    673672    mesh['outline_segment_tags'] = \
    674         array(mesh['outline_segment_tags']).astype(Character)
    675     mesh['holes'] = array(mesh['holes']).astype(Float)
    676     mesh['regions'] = array(mesh['regions']).astype(Float)
    677     mesh['region_tags'] = array(mesh['region_tags']).astype(Character)
    678     mesh['region_max_areas'] = array(mesh['region_max_areas']).astype(Float)
     673        num.array(mesh['outline_segment_tags']).astype(num.Character)
     674    mesh['holes'] = num.array(mesh['holes']).astype(num.Float)
     675    mesh['regions'] = num.array(mesh['regions']).astype(num.Float)
     676    mesh['region_tags'] = num.array(mesh['region_tags']).astype(num.Character)
     677    mesh['region_max_areas'] = num.array(mesh['region_max_areas']).astype(num.Float)
    679678
    680679    #mesh = mesh_dict2array(mesh)
     
    707706    if (mesh['vertices'].shape[0] > 0):
    708707        outfile.createDimension('num_of_vertices', mesh['vertices'].shape[0])
    709         outfile.createVariable('vertices', Float, ('num_of_vertices',
    710                                                    'num_of_dimensions'))
     708        outfile.createVariable('vertices', num.Float, ('num_of_vertices',
     709                                                       'num_of_dimensions'))
    711710        outfile.variables['vertices'][:] = mesh['vertices']
    712711        if mesh['vertex_attributes']  != None \
     
    718717                                    mesh['vertex_attribute_titles'].shape[1])
    719718            outfile.createVariable('vertex_attributes',
    720                                    Float,
     719                                   num.Float,
    721720                                   ('num_of_vertices',
    722721                                    'num_of_vertex_attributes'))
    723722            outfile.createVariable('vertex_attribute_titles',
    724                                    Character,
     723                                   num.Character,
    725724                                   ('num_of_vertex_attributes',
    726725                                    'num_of_vertex_attribute_title_chars'))
     
    740739                                    mesh['segment_tags'].shape[1])
    741740            outfile.createVariable('segment_tags',
    742                                    Character,
     741                                   num.Character,
    743742                                   ('num_of_segments',
    744743                                    'num_of_segment_tag_chars'))
     
    761760                                    mesh['triangle_tags'].shape[1])
    762761            outfile.createVariable('triangle_tags',
    763                                    Character,
     762                                   num.Character,
    764763                                   ('num_of_triangles',
    765764                                    'num_of_triangle_tag_chars'))
     
    770769    if (mesh['points'].shape[0] > 0):
    771770        outfile.createDimension('num_of_points', mesh['points'].shape[0])
    772         outfile.createVariable('points', Float, ('num_of_points',
    773                                                  'num_of_dimensions'))
     771        outfile.createVariable('points', num.Float, ('num_of_points',
     772                                                     'num_of_dimensions'))
    774773        outfile.variables['points'][:] = mesh['points']
    775774        if mesh['point_attributes'].shape[0] > 0  \
     
    778777                                    mesh['point_attributes'].shape[1])
    779778            outfile.createVariable('point_attributes',
    780                                    Float,
     779                                   num.Float,
    781780                                   ('num_of_points', 'num_of_point_attributes'))
    782781            outfile.variables['point_attributes'][:] = mesh['point_attributes']
     
    795794                                    mesh['outline_segment_tags'].shape[1])
    796795            outfile.createVariable('outline_segment_tags',
    797                                    Character,
     796                                   num.Character,
    798797                                   ('num_of_outline_segments',
    799798                                    'num_of_outline_segment_tag_chars'))
     
    804803    if (mesh['holes'].shape[0] > 0):
    805804        outfile.createDimension('num_of_holes', mesh['holes'].shape[0])
    806         outfile.createVariable('holes', Float, ('num_of_holes',
    807                                                 'num_of_dimensions'))
     805        outfile.createVariable('holes', num.Float, ('num_of_holes',
     806                                                    'num_of_dimensions'))
    808807        outfile.variables['holes'][:] = mesh['holes']
    809808
     
    811810    if (mesh['regions'].shape[0] > 0):
    812811        outfile.createDimension('num_of_regions', mesh['regions'].shape[0])
    813         outfile.createVariable('regions', Float, ('num_of_regions',
    814                                                   'num_of_dimensions'))
    815         outfile.createVariable('region_max_areas', Float, ('num_of_regions',))
     812        outfile.createVariable('regions', num.Float, ('num_of_regions',
     813                                                      'num_of_dimensions'))
     814        outfile.createVariable('region_max_areas', num.Float, ('num_of_regions',))
    816815        outfile.variables['regions'][:] = mesh['regions']
    817816        outfile.variables['region_max_areas'][:] = mesh['region_max_areas']
     
    819818            outfile.createDimension('num_of_region_tag_chars',
    820819                                    mesh['region_tags'].shape[1])
    821             outfile.createVariable('region_tags', Character,
     820            outfile.createVariable('region_tags', num.Character,
    822821                                   ('num_of_regions',
    823822                                    'num_of_region_tag_chars'))
     
    854853        mesh['vertices'] = fid.variables['vertices'][:]
    855854    except KeyError:
    856         mesh['vertices'] = array([])
     855        mesh['vertices'] = num.array([])
    857856
    858857    try:
     
    874873        mesh['segments'] = fid.variables['segments'][:]
    875874    except KeyError:
    876         mesh['segments'] = array([])
     875        mesh['segments'] = num.array([])
    877876
    878877    mesh['segment_tags'] =[]
     
    889888        mesh['triangle_neighbors'] = fid.variables['triangle_neighbors'][:]
    890889    except KeyError:
    891         mesh['triangles'] = array([])
    892         mesh['triangle_neighbors'] = array([])
     890        mesh['triangles'] = num.array([])
     891        mesh['triangle_neighbors'] = num.array([])
    893892
    894893    mesh['triangle_tags'] =[]
     
    917916        mesh['outline_segments'] = fid.variables['outline_segments'][:]
    918917    except KeyError:
    919         mesh['outline_segments'] = array([])
     918        mesh['outline_segments'] = num.array([])
    920919
    921920    mesh['outline_segment_tags'] =[]
     
    931930        mesh['holes'] = fid.variables['holes'][:]
    932931    except KeyError:
    933         mesh['holes'] = array([])
     932        mesh['holes'] = num.array([])
    934933
    935934    try:
    936935        mesh['regions'] = fid.variables['regions'][:]
    937936    except KeyError:
    938         mesh['regions'] = array([])
     937        mesh['regions'] = num.array([])
    939938
    940939    mesh['region_tags'] =[]
     
    950949        mesh['region_max_areas'] = fid.variables['region_max_areas'][:]
    951950    except KeyError:
    952         mesh['region_max_areas'] = array([])
     951        mesh['region_max_areas'] = num.array([])
    953952    #mesh[''] = fid.variables[''][:]
    954953
     
    10191018#                                  [max_x, max_y], [min_x, max_y]]
    10201019def extent(points):
    1021     points = array(points).astype(Float)
     1020    points = num.array(points).astype(num.Float)
    10221021
    10231022    max_x = min_x = points[0][0]
     
    10331032        if y < min_y: min_y = y
    10341033
    1035     extent = array([[min_x, min_y],
    1036                     [max_x, min_y],
    1037                     [max_x, max_y],
    1038                     [min_x, max_y]])
     1034    extent = num.array([[min_x, min_y],
     1035                        [max_x, min_y],
     1036                        [max_x, max_y],
     1037                        [min_x, max_y]])
    10391038
    10401039    return extent
     
    10961095def point_atts2array(point_atts):
    10971096    # convert attribute list to array of floats
    1098     point_atts['pointlist'] = array(point_atts['pointlist']).astype(Float)
     1097    point_atts['pointlist'] = num.array(point_atts['pointlist']).astype(num.Float)
    10991098
    11001099    for key in point_atts['attributelist'].keys():
    11011100        point_atts['attributelist'][key] = \
    1102             array(point_atts['attributelist'][key]).astype(Float)
     1101            num.array(point_atts['attributelist'][key]).astype(num.Float)
    11031102
    11041103    return point_atts
     
    11281127    """
    11291128
    1130     point_attributes = array([]).astype(Float)
     1129    point_attributes = num.array([]).astype(num.Float)
    11311130    keys = dic.keys()
    11321131    key = keys.pop(0)
    1133     point_attributes = reshape(dic[key],(dic[key].shape[0],1))
     1132    point_attributes = num.reshape(dic[key],(dic[key].shape[0],1))
    11341133    for key in keys:
    11351134        #point_attributes = concatenate([point_attributes, dic[key]], axis=1)
    1136         reshaped = reshape(dic[key],(dic[key].shape[0],1))
    1137         point_attributes = concatenate([point_attributes, reshaped], axis=1)
     1135        reshaped = num.reshape(dic[key],(dic[key].shape[0],1))
     1136        point_attributes = num.concatenate([point_attributes, reshaped], axis=1)
    11381137
    11391138    return dic.keys(), point_attributes
     
    11491148    dict = point_atts2array(dict)
    11501149    #FIXME maybe the points data structure should become a class?
    1151     dict['pointlist'] = take(dict['pointlist'],indices_to_keep)
     1150    dict['pointlist'] = num.take(dict['pointlist'],indices_to_keep)
    11521151
    11531152    for key in dict['attributelist'].keys():
    1154         dict['attributelist'][key]= take(dict['attributelist'][key],
    1155                                          indices_to_keep)
     1153        dict['attributelist'][key]= num.take(dict['attributelist'][key],
     1154                                             indices_to_keep)
    11561155
    11571156    return dict
     
    11701169
    11711170    combined = {}
    1172     combined['pointlist'] = concatenate((dict2['pointlist'],
    1173                                          dict1['pointlist']),axis=0)
     1171    combined['pointlist'] = num.concatenate((dict2['pointlist'],
     1172                                             dict1['pointlist']),axis=0)
    11741173
    11751174    atts = {}
    11761175    for key in dict2['attributelist'].keys():
    1177         atts[key]= concatenate((dict2['attributelist'][key],
    1178                                 dict1['attributelist'][key]), axis=0)
     1176        atts[key]= num.concatenate((dict2['attributelist'][key],
     1177                                    dict1['attributelist'][key]), axis=0)
    11791178    combined['attributelist']=atts
    11801179    combined['geo_reference'] = dict1['geo_reference']
  • anuga_core/source/anuga/load_mesh/test_loadASCII.py

    r6074 r6154  
    1010from os.path import splitext
    1111
    12 from Numeric import array, allclose, alltrue
     12import Numeric as num
    1313   
    1414from anuga.load_mesh.loadASCII import *
     
    179179        #print "*(*("
    180180       
    181         self.failUnless(alltrue(array(meshDict['vertices'])  ==
    182                         array(loadedDict['vertices'])),
    183                          'test_export_mesh_file failed. Test 1')
    184         self.failUnless(alltrue(array(meshDict['triangles'])  ==
    185                         array(loadedDict['triangles'])),
    186                          'test_export_mesh_file failed. Test 2')
    187         self.failUnless(alltrue(array(meshDict['segments'])  ==
    188                         array(loadedDict['segments'])),
    189                          'test_export_mesh_file failed. Test 3')
    190         self.failUnless(alltrue(array(meshDict['triangle_tags'])  ==
    191                         array(loadedDict['triangle_tags'])),
    192                          'test_export_mesh_file failed. Test 4')
     181        self.failUnless(num.alltrue(num.array(meshDict['vertices'])  ==
     182                                    num.array(loadedDict['vertices'])),
     183                        'test_export_mesh_file failed. Test 1')
     184        self.failUnless(num.alltrue(num.array(meshDict['triangles'])  ==
     185                                    num.array(loadedDict['triangles'])),
     186                        'test_export_mesh_file failed. Test 2')
     187        self.failUnless(num.alltrue(num.array(meshDict['segments'])  ==
     188                                    num.array(loadedDict['segments'])),
     189                        'test_export_mesh_file failed. Test 3')
     190        self.failUnless(num.alltrue(num.array(meshDict['triangle_tags'])  ==
     191                                    num.array(loadedDict['triangle_tags'])),
     192                        'test_export_mesh_file failed. Test 4')
    193193
    194194        self.failUnless(meshDict['vertex_attributes']  ==
    195195                        loadedDict['vertex_attributes'],
    196                          'test_export_mesh_file failed. Test 5')
    197         self.failUnless(alltrue(array(meshDict['triangle_neighbors'])  ==
    198                         array(loadedDict['triangle_neighbors'])),
    199                          'test_export_mesh_file failed. Test 6')
    200         self.failUnless(alltrue(array(meshDict['segment_tags'])  ==
    201                         array(loadedDict['segment_tags'])),
    202                          'test_export_mesh_file failed. Test 7')
    203         self.failUnless(alltrue(array(meshDict['vertex_attribute_titles'])  ==
    204                         array(loadedDict['vertex_attribute_titles'])),
    205                          'test_export_mesh_file failed. Test 8')
    206         self.failUnless(alltrue(array(meshDict['geo_reference'])  ==
    207                         array(loadedDict['geo_reference'])),
    208                          'test_export_mesh_file failed. Test 9')
     196                        'test_export_mesh_file failed. Test 5')
     197        self.failUnless(num.alltrue(num.array(meshDict['triangle_neighbors'])  ==
     198                                    num.array(loadedDict['triangle_neighbors'])),
     199                        'test_export_mesh_file failed. Test 6')
     200        self.failUnless(num.alltrue(num.array(meshDict['segment_tags'])  ==
     201                                    num.array(loadedDict['segment_tags'])),
     202                        'test_export_mesh_file failed. Test 7')
     203        self.failUnless(num.alltrue(num.array(meshDict['vertex_attribute_titles'])  ==
     204                                    num.array(loadedDict['vertex_attribute_titles'])),
     205                        'test_export_mesh_file failed. Test 8')
     206        self.failUnless(num.alltrue(num.array(meshDict['geo_reference'])  ==
     207                                    num.array(loadedDict['geo_reference'])),
     208                        'test_export_mesh_file failed. Test 9')
    209209           
    210210        os.remove(fileName)
     
    439439                         
    440440    def check_mesh_dicts(self, loaded_dict, dict, fail_string ):
    441         assert allclose(array(loaded_dict['points']),
    442                         array(dict['points']))
     441        assert num.allclose(num.array(loaded_dict['points']),
     442                            num.array(dict['points']))
    443443                       
    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 num.allclose(num.array(loaded_dict['point_attributes']),
     445                            num.array(dict['point_attributes']))
     446        assert num.allclose(num.array(loaded_dict['outline_segments']),
     447                            num.array(dict['outline_segments']))
    448448       
    449449        self.failUnless(loaded_dict['outline_segment_tags']  ==
     
    452452       
    453453           
    454         assert allclose(array(loaded_dict['regions']),
    455                         array(dict['regions']))
     454        assert num.allclose(num.array(loaded_dict['regions']),
     455                            num.array(dict['regions']))
    456456                       
    457457        self.failUnless(loaded_dict['region_tags']  ==
     
    459459                        fail_string + ' failed!! Test 5')
    460460       
    461         assert allclose(array(loaded_dict['region_max_areas']),
    462                         array(dict['region_max_areas']))
     461        assert num.allclose(num.array(loaded_dict['region_max_areas']),
     462                            num.array(dict['region_max_areas']))
    463463 
    464         assert allclose(array(loaded_dict['holes']),
    465                         array(dict['holes']))
     464        assert num.allclose(num.array(loaded_dict['holes']),
     465                            num.array(dict['holes']))
    466466 
    467         assert allclose(array(dict['vertices']),
    468                         array(loaded_dict['vertices']))
     467        assert num.allclose(num.array(dict['vertices']),
     468                            num.array(loaded_dict['vertices']))
    469469 
    470         assert allclose(array(dict['triangles']),
    471                         array(loaded_dict['triangles']))
     470        assert num.allclose(num.array(dict['triangles']),
     471                            num.array(loaded_dict['triangles']))
    472472 
    473         assert allclose(array(dict['segments']),
    474                         array(loaded_dict['segments']))
     473        assert num.allclose(num.array(dict['segments']),
     474                            num.array(loaded_dict['segments']))
    475475        for ob, ldob in map(None,dict['triangle_tags'],
    476476                              loaded_dict['triangle_tags']):
     
    484484                        fail_string + ' failed!! Test vertex_attributes')
    485485 
    486         assert allclose(array(dict['triangle_neighbors']),
    487                         array(loaded_dict['triangle_neighbors']))
     486        assert num.allclose(num.array(dict['triangle_neighbors']),
     487                            num.array(loaded_dict['triangle_neighbors']))
    488488
    489489        for seg, ldseg in map(None,dict['segment_tags'],
     
    492492                        fail_string + ' failed!! Test 8')
    493493        try:
    494             assert allclose(array(dict['vertex_attribute_titles']),
    495                             array(loaded_dict['vertex_attribute_titles']))
     494            assert num.allclose(num.array(dict['vertex_attribute_titles']),
     495                                num.array(loaded_dict['vertex_attribute_titles']))
    496496        except TypeError:
    497             self.failUnless(alltrue(array(loaded_dict['vertex_attribute_titles']) == array(dict['vertex_attribute_titles'])),
    498                         fail_string + ' failed!! Test 8')
     497            self.failUnless(num.alltrue(num.array(loaded_dict['vertex_attribute_titles']) == num.array(dict['vertex_attribute_titles'])),
     498                            fail_string + ' failed!! Test 8')
    499499        try:   
    500500            self.failUnless(loaded_dict['geo_reference']  ==
Note: See TracChangeset for help on using the changeset viewer.