def test_dict2csv_convert(self): (handle, file_name) = tempfile.mkstemp('.csv', 'test_csv_interface_') os.close(handle) attribute_dic = {'LONGITUDE': self.LONGITUDE, 'LATITUDE': self.LATITUDE, 'WALLS': self.WALLS} # This is actually assumed to be a list in the code... title_index_dic = {'LONGITUDE': 0, 'LATITUDE': 1, 'WALLS': 2} convert = {'LONGITUDE': float, 'LATITUDE': float, 'WALLS' :str} util.dict2csv(file_name, title_index_dic, attribute_dic) attribute_dic_new, title_index_dic_new = \ csvi.csv2dict(file_name, title_check_list=title_index_dic, convert=convert) self.assert_(attribute_dic_new['LONGITUDE'] == self.LONGITUDE) self.assert_(attribute_dic_new['LATITUDE'] == self.LATITUDE) self.assert_(attribute_dic_new['WALLS'] == self.WALLS) self.assert_(attribute_dic_new == attribute_dic) self.assert_(title_index_dic_new == title_index_dic_new) os.remove(file_name)
def test_dict2csv_bad(self): (handle, file_name) = tempfile.mkstemp('.csv', 'test_csv_interface_') os.close(handle) attribute_dic = {'LONGITUDE': self.LONGITUDE, 'LATITUDE': self.LATITUDE, 'WALLS': self.WALLS} title_index_dic = {'LONGITUDE': 0, 'LATITUDE': 1, 'WALLS': 2} util.dict2csv(file_name, title_index_dic, attribute_dic) try: attribute_dic_new, title_index_dic_new = \ csvi.csv2dict(file_name, title_check_list={'wire': 8}) except IOError: os.remove(file_name) else: os.remove(file_name) self.failUnless(False, "Error not thrown")
def test_dict2csv_bad(self): (handle, file_name) = tempfile.mkstemp('.csv', 'test_csv_interface_') os.close(handle) attribute_dic = { 'LONGITUDE': self.LONGITUDE, 'LATITUDE': self.LATITUDE, 'WALLS': self.WALLS } title_index_dic = {'LONGITUDE': 0, 'LATITUDE': 1, 'WALLS': 2} util.dict2csv(file_name, title_index_dic, attribute_dic) try: attribute_dic_new, title_index_dic_new = \ csvi.csv2dict(file_name, title_check_list={'wire': 8}) except IOError: os.remove(file_name) else: os.remove(file_name) self.failUnless(False, "Error not thrown")
def load_site_class2Vs30(file_name): """ Load the data used to convert a site class to a Vs30 value. args: file_name: The file to be opened, with extension, or a file handle. Return: class2Vs30_dict: A dic of 'site_class' and 'Vs30' keys. Raises: IOError if the file is not present or the file does not have site_class and Vs30 in the header. """ csv_columns_dict, _ = csv2dict(file_name, ('vs30', 'site_class'), convert={'vs30': float}) site_class2Vs30_dict = {} for siteclass, Vs30 in map(None, csv_columns_dict['site_class'], csv_columns_dict['vs30']): site_class2Vs30_dict[siteclass] = Vs30 return site_class2Vs30_dict
def test_dict2csv(self): (handle, file_name) = tempfile.mkstemp('.csv', 'test_csv_interface_') os.close(handle) attribute_dic = {'LONGITUDE': self.LONGITUDE, 'LATITUDE': self.LATITUDE, 'WALLS': self.WALLS} title_index_dic = {'LONGITUDE': 0, 'LATITUDE': 1, 'WALLS': 2} util.dict2csv(file_name, title_index_dic, attribute_dic) (attribute_dic_new, title_index_dic_new) = \ csvi.csv2dict(file_name, title_check_list=title_index_dic) attribute_dic_new['LONGITUDE'] = \ [float(x) for x in attribute_dic_new['LONGITUDE']] attribute_dic_new['LATITUDE'] = \ [float(x) for x in attribute_dic_new['LATITUDE']] self.assert_(attribute_dic_new == attribute_dic) self.assert_(title_index_dic_new == title_index_dic_new) os.remove(file_name)
def test_dict2csv_convert(self): (handle, file_name) = tempfile.mkstemp('.csv', 'test_csv_interface_') os.close(handle) attribute_dic = { 'LONGITUDE': self.LONGITUDE, 'LATITUDE': self.LATITUDE, 'WALLS': self.WALLS } # This is actually assumed to be a list in the code... title_index_dic = {'LONGITUDE': 0, 'LATITUDE': 1, 'WALLS': 2} convert = {'LONGITUDE': float, 'LATITUDE': float, 'WALLS': str} util.dict2csv(file_name, title_index_dic, attribute_dic) attribute_dic_new, title_index_dic_new = \ csvi.csv2dict(file_name, title_check_list=title_index_dic, convert=convert) self.assert_(attribute_dic_new['LONGITUDE'] == self.LONGITUDE) self.assert_(attribute_dic_new['LATITUDE'] == self.LATITUDE) self.assert_(attribute_dic_new['WALLS'] == self.WALLS) self.assert_(attribute_dic_new == attribute_dic) self.assert_(title_index_dic_new == title_index_dic_new) os.remove(file_name)
def test_dict2csv(self): (handle, file_name) = tempfile.mkstemp('.csv', 'test_csv_interface_') os.close(handle) attribute_dic = { 'LONGITUDE': self.LONGITUDE, 'LATITUDE': self.LATITUDE, 'WALLS': self.WALLS } title_index_dic = {'LONGITUDE': 0, 'LATITUDE': 1, 'WALLS': 2} util.dict2csv(file_name, title_index_dic, attribute_dic) (attribute_dic_new, title_index_dic_new) = \ csvi.csv2dict(file_name, title_check_list=title_index_dic) attribute_dic_new['LONGITUDE'] = \ [float(x) for x in attribute_dic_new['LONGITUDE']] attribute_dic_new['LATITUDE'] = \ [float(x) for x in attribute_dic_new['LATITUDE']] self.assert_(attribute_dic_new == attribute_dic) self.assert_(title_index_dic_new == title_index_dic_new) os.remove(file_name)
def build_replacement_ratios(usage_per_struct, buildings_usage_classification): """Return an array of the building components replacement cost ratios. shape (# of buildings, 3 (# of components in a structure)) A structure has 3 components; structural nonstructural acceleration sensitive nonstructural drift sensitive The ratio of the replacement cost for each component differs for different usage types. Also, there are two scales of usage types; Functional classification of buildings (FCB)(ABS usage) HAZUS usage Parameters: usage_per_struct: An array, shape (# of buildings) of usage values for each structure. buildings_usage_classification: The usage scale used. Return: A dictionary of cost ratios for the strucutures. The keys are the structural components; 'structural', 'nonstructural drift sensitive', 'nonstructural acceleration sensitive' Note: This should only be used once, since it is loading a file. """ usage_column = 'Functional classification of buildings (ABS usage)' components = ['structural', 'nonstructural drift sensitive', 'nonstructural acceleration sensitive'] convert = {} # extract usage dependent parameters if buildings_usage_classification is 'HAZUS': # HAZUS_USAGE file = 'replacement_cost_ratios_wrt_HAZUS_Usage.csv' elif buildings_usage_classification is 'FCB': # FCB_USAGE file = 'replacement_cost_ratios_wrt_FCB_Usage.csv' convert[usage_column] = int else: msg = ('b_usage_type_flat = ' + str(buildings_usage_classification) + ' not "FCB" or "HAZUS"') raise ValueError(msg) for comp in components: convert[comp] = float root_dir = determine_eqrm_path() data_dir = join(root_dir, 'resources', 'data') (att_dic, _) = csv2dict(join(data_dir, file), convert=convert) # This way does not have an assert # Build a dict with # key (usage, component), value = cost ratio # cost_ratios = {} # for comp in components: # tmp = {} # for i,usage in enumerate(att_dic[usage_column]): # tmp[usage] = att_dic[comp][i] # cost_ratios[comp] = tmp # replacement_cost_ratios = {} # for comp in components: # this_cost_ratios = cost_ratios[comp] # tmp = [this_cost_ratios[usage] for usage in usage_per_struct] # replacement_cost_ratios[comp] = array(tmp) cost_ratios = {} for (i, usage) in enumerate(att_dic[usage_column]): cost_ratios[usage] = [att_dic[components[0]][i], att_dic[components[1]][i], att_dic[components[2]][i]] rcp_array = asarray([cost_ratios[usage] for usage in usage_per_struct]) assert allclose(sum(rcp_array), rcp_array.shape[0], 0.0000001) replacement_cost_ratios = {} for (i, comp) in enumerate(components): replacement_cost_ratios[comp] = rcp_array[:, i] return replacement_cost_ratios
def build_replacement_ratios(usage_per_struct, buildings_usage_classification): """Return an array of the building components replacement cost ratios. shape (# of buildings, 3 (# of components in a structure)) A structure has 3 components; structural nonstructural acceleration sensitive nonstructural drift sensitive The ratio of the replacement cost for each component differs for different usage types. Also, there are two scales of usage types; Functional classification of buildings (FCB)(ABS usage) HAZUS usage Parameters: usage_per_struct: An array, shape (# of buildings) of usage values for each structure. buildings_usage_classification: The usage scale used. Return: A dictionary of cost ratios for the strucutures. The keys are the structural components; 'structural', 'nonstructural drift sensitive', 'nonstructural acceleration sensitive' Note: This should only be used once, since it is loading a file. """ usage_column = 'Functional classification of buildings (ABS usage)' components = [ 'structural', 'nonstructural drift sensitive', 'nonstructural acceleration sensitive' ] convert = {} # extract usage dependent parameters if buildings_usage_classification is 'HAZUS': # HAZUS_USAGE file = 'replacement_cost_ratios_wrt_HAZUS_Usage.csv' elif buildings_usage_classification is 'FCB': # FCB_USAGE file = 'replacement_cost_ratios_wrt_FCB_Usage.csv' convert[usage_column] = int else: msg = ('b_usage_type_flat = ' + str(buildings_usage_classification) + ' not "FCB" or "HAZUS"') raise ValueError(msg) for comp in components: convert[comp] = float root_dir = determine_eqrm_path() data_dir = join(root_dir, 'resources', 'data') (att_dic, _) = csv2dict(join(data_dir, file), convert=convert) # This way does not have an assert # Build a dict with # key (usage, component), value = cost ratio # cost_ratios = {} # for comp in components: # tmp = {} # for i,usage in enumerate(att_dic[usage_column]): # tmp[usage] = att_dic[comp][i] # cost_ratios[comp] = tmp # replacement_cost_ratios = {} # for comp in components: # this_cost_ratios = cost_ratios[comp] # tmp = [this_cost_ratios[usage] for usage in usage_per_struct] # replacement_cost_ratios[comp] = array(tmp) cost_ratios = {} for (i, usage) in enumerate(att_dic[usage_column]): cost_ratios[usage] = [ att_dic[components[0]][i], att_dic[components[1]][i], att_dic[components[2]][i] ] rcp_array = asarray([cost_ratios[usage] for usage in usage_per_struct]) assert allclose(sum(rcp_array), rcp_array.shape[0], 0.0000001) replacement_cost_ratios = {} for (i, comp) in enumerate(components): replacement_cost_ratios[comp] = rcp_array[:, i] return replacement_cost_ratios