def read_mat(self, file_name, var_names=None): """Read MATLAB file and store variables in exposures. Parameters: file_name (str): absolute path file var_names (dict, optional): dictionary containing the name of the MATLAB variables. Default: DEF_VAR_MAT. """ LOGGER.info('Reading %s', file_name) if not var_names: var_names = DEF_VAR_MAT data = u_hdf5.read(file_name) try: data = data[var_names['sup_field_name']] except KeyError: pass try: data = data[var_names['field_name']] exposures = dict() _read_mat_obligatory(exposures, data, var_names) _read_mat_optional(exposures, data, var_names) except KeyError as var_err: LOGGER.error("Not existing variable: %s", str(var_err)) raise var_err self.gdf = GeoDataFrame(data=exposures, crs=self.crs) _read_mat_metadata(self, data, file_name, var_names)
def read_mat(self, file_name, var_names=DEF_VAR_MAT): """Read MATLAB file and store variables in exposures. Parameters: file_name (str): absolute path file var_names (dict, optional): dictionary containing the name of the MATLAB variables. Default: DEF_VAR_MAT. """ if var_names is None: var_names = DEF_VAR_MAT data = hdf5.read(file_name) try: data = data[var_names['sup_field_name']] except KeyError: pass try: data = data[var_names['field_name']] exposures = dict() _read_mat_obligatory(exposures, data, var_names) _read_mat_optional(exposures, data, var_names) except KeyError as var_err: LOGGER.error("Not existing variable: %s", str(var_err)) raise var_err Exposures.__init__(self, data=exposures) _read_mat_metadata(self, data, file_name, var_names)
def read_mat(self, file_name, description='', var_names=DEF_VAR_MAT): """Read MATLAB file generated with previous MATLAB CLIMADA version. Parameters: file_name (str): absolute file name description (str, optional): description of the data var_names (dict, optional): name of the variables in the file """ disc = hdf5.read(file_name) self.clear() self.tag.file_name = file_name self.tag.description = description try: disc = disc[var_names['sup_field_name']] except KeyError: pass try: disc = disc[var_names['field_name']] self.years = np.squeeze(disc[var_names['var_name']['year']]). \ astype(int, copy=False) self.rates = np.squeeze(disc[var_names['var_name']['disc']]) except KeyError as err: LOGGER.error("Not existing variable: %s", str(err)) raise err
def read_mat(self, file_name, description='', var_names=DEF_VAR_MAT): """ Read MATLAB file generated with previous MATLAB CLIMADA version. Parameters ---------- file_name: str filename including path and extension description: str, optional description of the data. The default is '' var_names: dict, optional name of the variables in the file. The Default is DEF_VAR_MAT = {'sup_field_name': 'entity', 'field_name': 'discount', 'var_name': {'year': 'year', 'disc': 'discount_rate'}} """ disc = u_hdf5.read(file_name) self.clear() self.tag.file_name = str(file_name) self.tag.description = description try: disc = disc[var_names['sup_field_name']] except KeyError: pass try: disc = disc[var_names['field_name']] self.years = np.squeeze(disc[var_names['var_name']['year']]). \ astype(int, copy=False) self.rates = np.squeeze(disc[var_names['var_name']['disc']]) except KeyError as err: raise KeyError("Not existing variable: %s" % str(err)) from err
def read_mat(self, file_name, description='', centroids=None, var_names=DEF_VAR_MAT): """Read climada hazard generate with the MATLAB code. Parameters: file_name (str): absolute file name description (str, optional): description of the data centroids (Centroids, optional): provide centroids if not contained in the file var_names (dict, default): name of the variables in the file, default: DEF_VAR_MAT constant Raises: KeyError """ self.clear() self.tag.file_name = file_name self.tag.description = description try: data = hdf5.read(file_name) try: data = data[var_names['field_name']] except KeyError: pass haz_type = hdf5.get_string(data[var_names['var_name']['per_id']]) self.tag.haz_type = haz_type self._read_centroids(centroids=centroids, var_names=var_names['var_cent']) self._read_att_mat(data, file_name, var_names) except KeyError as var_err: LOGGER.error("Not existing variable: %s", str(var_err)) raise var_err
def read_mat(self, file_name, var_names=None): """Read MATLAB file and store variables in exposures. Parameters: file_name (str): absolute path file var_names (dict, optional): dictionary containing the name of the MATLAB variables. Default: DEF_VAR_MAT. """ LOGGER.info('Reading %s', file_name) if not var_names: var_names = DEF_VAR_MAT data = u_hdf5.read(file_name) try: data = data[var_names['sup_field_name']] except KeyError: pass try: data = data[var_names['field_name']] exposures = dict() _read_mat_obligatory(exposures, data, var_names) _read_mat_optional(exposures, data, var_names) except KeyError as var_err: raise KeyError(f"Variable not in MAT file: {var_names.get('field_name')}")\ from var_err self.set_gdf(GeoDataFrame(data=exposures)) _read_mat_metadata(self, data, file_name, var_names)
def test_with_refs_pass(self): '''Allow to load references of the matlab file''' # Load input refs = True contents = hdf5.read(HAZ_TEST_MAT, refs) # Check read contents self.assertEqual(2, len(contents)) self.assertTrue('hazard' in contents.keys()) self.assertTrue('#refs#' in contents.keys())
def test_hazard_pass(self): '''Checking result against matlab atl_prob.mat file''' # Load input contents = hdf5.read(HAZ_TEST_MAT) # Check read contents self.assertEqual(1, len(contents)) self.assertTrue('hazard' in contents.keys()) self.assertEqual(False, '#refs#' in contents.keys()) hazard = contents['hazard'] self.assertTrue('reference_year' in hazard.keys()) self.assertTrue('lon' in hazard.keys()) self.assertTrue('lat' in hazard.keys()) self.assertTrue('centroid_ID' in hazard.keys()) self.assertTrue('orig_years' in hazard.keys()) self.assertTrue('orig_event_count' in hazard.keys()) self.assertTrue('event_count' in hazard.keys()) self.assertTrue('event_ID' in hazard.keys()) self.assertTrue('category' in hazard.keys()) self.assertTrue('orig_event_flag' in hazard.keys()) self.assertTrue('yyyy' in hazard.keys()) self.assertTrue('mm' in hazard.keys()) self.assertTrue('dd' in hazard.keys()) self.assertTrue('datenum' in hazard.keys()) self.assertTrue('scenario' in hazard.keys()) self.assertTrue('intensity' in hazard.keys()) self.assertTrue('name' in hazard.keys()) self.assertTrue('frequency' in hazard.keys()) self.assertTrue('matrix_density' in hazard.keys()) self.assertTrue('windfield_comment' in hazard.keys()) self.assertTrue('peril_ID' in hazard.keys()) self.assertTrue('filename' in hazard.keys()) self.assertTrue('comment' in hazard.keys()) self.assertTrue('date' in hazard.keys()) self.assertTrue('units' in hazard.keys()) self.assertTrue('orig_yearset' in hazard.keys()) self.assertTrue('fraction' in hazard.keys()) self.assertEqual(27, len(hazard.keys())) # Check some random values mat_shape = (len(contents['hazard']['event_ID']), \ len(contents['hazard']['centroid_ID'])) sp_mat = hdf5.get_sparse_csr_mat(hazard['intensity'], mat_shape) self.assertTrue(np.array_equal(np.array([[84], [67]]), \ hazard['peril_ID'])) self.assertEqual(34.537289477809473, sp_mat[2862, 97]) self.assertEqual(-80, hazard['lon'][46]) self.assertEqual(28, hazard['lat'][87]) self.assertEqual(2016, hazard['reference_year'])
def read_mat(self, file_name, var_names=DEF_VAR_MAT): """Read centroids from CLIMADA's MATLAB version. Parameters ---------- file_name : str absolute or relative file name var_names : dict, default name of the variables Raises ------ KeyError """ LOGGER.info('Reading %s', file_name) if var_names is None: var_names = DEF_VAR_MAT cent = u_hdf5.read(file_name) # Try open encapsulating variable FIELD_NAMES num_try = 0 for field in var_names['field_names']: try: cent = cent[field] break except KeyError: num_try += 1 if num_try == len(var_names['field_names']): LOGGER.warning("Variables are not under: %s.", var_names['field_names']) try: cen_lat = np.squeeze(cent[var_names['var_name']['lat']]) cen_lon = np.squeeze(cent[var_names['var_name']['lon']]) self.set_lat_lon(cen_lat, cen_lon) try: self.dist_coast = np.squeeze( cent[var_names['var_name']['dist_coast']]) except KeyError: pass try: self.region_id = np.squeeze( cent[var_names['var_name']['region_id']]) except KeyError: pass except KeyError as err: LOGGER.error("Not existing variable: %s", str(err)) raise err
def test_get_string_pass(self): '''Check function to get a string from input integer array''' # Load input contents = hdf5.read(HAZ_TEST_MAT) # Convert several strings str_date = hdf5.get_string(contents['hazard']['date']) str_comment = hdf5.get_string(contents['hazard']['comment']) str_wf = hdf5.get_string(contents['hazard']['windfield_comment']) str_fn = hdf5.get_string(contents['hazard']['filename']) # Check results self.assertEqual('14-Nov-2017 10:09:05', str_date) self.assertEqual( 'TC hazard event set, generated 14-Nov-2017 10:09:05', \ str_comment) self.assertEqual( 'generating 14450 windfields took 0.25 min ' + \ '(0.0010 sec/event)', str_wf) self.assertEqual('/Users/aznarsig/Documents/MATLAB/climada_data/' + \ 'hazards/atl_prob.mat', str_fn)
def from_mat(cls, file_name, description='', var_names=None): """ Read MATLAB file generated with previous MATLAB CLIMADA version. Parameters ---------- file_name: str filename including path and extension description: str, optional description of the data. The default is '' var_names: dict, optional name of the variables in the file. The Default is DEF_VAR_MAT = {'sup_field_name': 'entity', 'field_name': 'discount', 'var_name': {'year': 'year', 'disc': 'discount_rate'}} Returns ------- climada.entity.DiscRates : The disc rates from matlab """ if var_names is None: var_names = DEF_VAR_MAT disc = u_hdf5.read(file_name) tag = Tag(file_name=str(file_name), description=description) try: disc = disc[var_names['sup_field_name']] except KeyError: pass try: disc = disc[var_names['field_name']] years = np.squeeze(disc[var_names['var_name']['year']]). \ astype(int, copy=False) rates = np.squeeze(disc[var_names['var_name']['disc']]) except KeyError as err: raise KeyError("Not existing variable: %s" % str(err)) from err return cls(years=years, rates=rates, tag=tag)
def test_get_sparse_mat_pass(self): '''Check contents of imported sparse matrix, using the function \ to build a sparse matrix from the read HDF5 variable''' # Load input contents = hdf5.read(HAZ_TEST_MAT) # get matrix size mat_shape = (len(contents['hazard']['event_ID']), \ len(contents['hazard']['centroid_ID'])) spr_mat = hdf5.get_sparse_csr_mat(contents['hazard']['intensity'], \ mat_shape) self.assertEqual(mat_shape[0], spr_mat.shape[0]) self.assertEqual(mat_shape[1], spr_mat.shape[1]) self.assertEqual(0, spr_mat[0, 0]) self.assertEqual(9.2029355562400745, spr_mat[7916, 98]) self.assertEqual(34.426829019435729, spr_mat[12839, 96]) self.assertEqual(61.81217342446773, spr_mat[1557, 97]) self.assertEqual(6.4470644550625753, spr_mat[6658, 97]) self.assertEqual(0, spr_mat[298, 9]) self.assertEqual(19.385821399329995, spr_mat[15, 0]) self.assertEqual(52.253444444444447, spr_mat[76, 95]) self.assertEqual(0, spr_mat[126, 86])
def read_mat(self, file_name, description='', var_names=DEF_VAR_MAT): """Read MATLAB file generated with previous MATLAB CLIMADA version. Parameters: file_name (str): absolute file name description (str, optional): description of the data var_names (dict, optional): name of the variables in the file """ def read_att_mat(measures, data, file_name, var_names): """Read MATLAB measures attributes""" num_mes = len(data[var_names['var_name']['name']]) for idx in range(0, num_mes): meas = Measure() meas.name = hdf5.get_str_from_ref( file_name, data[var_names['var_name']['name']][idx][0]) color_str = hdf5.get_str_from_ref( file_name, data[var_names['var_name']['color']][idx][0]) meas.color_rgb = np.fromstring(color_str, dtype=float, sep=' ') meas.cost = data[var_names['var_name']['cost']][idx][0] meas.haz_type = hdf5.get_str_from_ref( file_name, data[var_names['var_name']['haz']][idx][0]) meas.hazard_freq_cutoff = data[var_names['var_name'] ['haz_frq']][idx][0] meas.hazard_set = hdf5.get_str_from_ref( file_name, data[var_names['var_name']['haz_set']][idx][0]) try: meas.hazard_inten_imp = ( data[var_names['var_name']['haz_int_a']][idx][0], data[var_names['var_name']['haz_int_b']][0][idx]) except KeyError: meas.hazard_inten_imp = ( data[var_names['var_name']['haz_int_a'][:-2]][idx][0], 0) # different convention of signes followed in MATLAB! meas.mdd_impact = ( data[var_names['var_name']['mdd_a']][idx][0], data[var_names['var_name']['mdd_b']][idx][0]) meas.paa_impact = ( data[var_names['var_name']['paa_a']][idx][0], data[var_names['var_name']['paa_b']][idx][0]) meas.imp_fun_map = hdf5.get_str_from_ref( file_name, data[var_names['var_name']['fun_map']][idx][0]) meas.exposures_set = hdf5.get_str_from_ref( file_name, data[var_names['var_name']['exp_set']][idx][0]) exp_region_id = data[var_names['var_name']['exp_reg']][idx][0] if exp_region_id: meas.exp_region_id = [exp_region_id] meas.risk_transf_attach = data[var_names['var_name'] ['risk_att']][idx][0] meas.risk_transf_cover = data[var_names['var_name'] ['risk_cov']][idx][0] measures.append(meas) data = hdf5.read(file_name) self.clear() self.tag.file_name = file_name self.tag.description = description try: data = data[var_names['sup_field_name']] except KeyError: pass try: data = data[var_names['field_name']] read_att_mat(self, data, file_name, var_names) except KeyError as var_err: LOGGER.error("Not existing variable %s", str(var_err)) raise var_err
def read_mat(self, file_name, description='', var_names=DEF_VAR_MAT): """Read MATLAB file generated with previous MATLAB CLIMADA version. Parameters: file_name (str): absolute file name description (str, optional): description of the data var_names (dict, optional): name of the variables in the file """ def _get_hdf5_funcs(imp, file_name, var_names): """Get rows that fill every impact function and its name.""" func_pos = dict() for row, (fun_id, fun_type) in enumerate( zip(imp[var_names['var_name']['fun_id']].squeeze(), imp[var_names['var_name']['peril']].squeeze())): type_str = u_hdf5.get_str_from_ref(file_name, fun_type) key = (type_str, int(fun_id)) if key not in func_pos: func_pos[key] = list() func_pos[key].append(row) return func_pos def _get_hdf5_str(imp, idxs, file_name, var_name): """Get rows with same string in var_name.""" prev_str = "" for row in idxs: cur_str = u_hdf5.get_str_from_ref(file_name, imp[var_name][row][0]) if prev_str == "": prev_str = cur_str elif prev_str != cur_str: LOGGER.error("Impact function with two different %s.", var_name) raise ValueError return prev_str imp = u_hdf5.read(file_name) self.clear() self.tag.file_name = str(file_name) self.tag.description = description try: imp = imp[var_names['sup_field_name']] except KeyError: pass try: imp = imp[var_names['field_name']] funcs_idx = _get_hdf5_funcs(imp, file_name, var_names) for imp_key, imp_rows in funcs_idx.items(): func = ImpactFunc() func.haz_type = imp_key[0] func.id = imp_key[1] # check that this function only has one intensity unit, if provided try: func.intensity_unit = _get_hdf5_str(imp, imp_rows, file_name, var_names['var_name']['unit']) except KeyError: pass # check that this function only has one name try: func.name = _get_hdf5_str(imp, imp_rows, file_name, var_names['var_name']['name']) except KeyError: func.name = str(func.id) func.intensity = np.take(imp[var_names['var_name']['inten']], imp_rows) func.mdd = np.take(imp[var_names['var_name']['mdd']], imp_rows) func.paa = np.take(imp[var_names['var_name']['paa']], imp_rows) self.append(func) except KeyError as err: LOGGER.error("Not existing variable: %s", str(err)) raise err
def from_mat(cls, file_name, description='', var_names=DEF_VAR_MAT): """Read MATLAB file generated with previous MATLAB CLIMADA version. Parameters ---------- file_name : str absolute file name description : str, optional description of the data var_names : dict, optional name of the variables in the file Returns ------- meas_set: climada.entity.MeasureSet() Measure Set from matlab file """ def read_att_mat(measures, data, file_name, var_names): """Read MATLAB measures attributes""" num_mes = len(data[var_names['var_name']['name']]) for idx in range(0, num_mes): meas = Measure() meas.name = u_hdf5.get_str_from_ref( file_name, data[var_names['var_name']['name']][idx][0]) color_str = u_hdf5.get_str_from_ref( file_name, data[var_names['var_name']['color']][idx][0]) meas.color_rgb = np.fromstring(color_str, dtype=float, sep=' ') meas.cost = data[var_names['var_name']['cost']][idx][0] meas.haz_type = u_hdf5.get_str_from_ref( file_name, data[var_names['var_name']['haz']][idx][0]) meas.hazard_freq_cutoff = data[var_names['var_name'] ['haz_frq']][idx][0] meas.hazard_set = u_hdf5.get_str_from_ref( file_name, data[var_names['var_name']['haz_set']][idx][0]) try: meas.hazard_inten_imp = ( data[var_names['var_name']['haz_int_a']][idx][0], data[var_names['var_name']['haz_int_b']][0][idx]) except KeyError: meas.hazard_inten_imp = ( data[var_names['var_name']['haz_int_a'][:-2]][idx][0], 0) # different convention of signes followed in MATLAB! meas.mdd_impact = ( data[var_names['var_name']['mdd_a']][idx][0], data[var_names['var_name']['mdd_b']][idx][0]) meas.paa_impact = ( data[var_names['var_name']['paa_a']][idx][0], data[var_names['var_name']['paa_b']][idx][0]) meas.imp_fun_map = u_hdf5.get_str_from_ref( file_name, data[var_names['var_name']['fun_map']][idx][0]) meas.exposures_set = u_hdf5.get_str_from_ref( file_name, data[var_names['var_name']['exp_set']][idx][0]) exp_region_id = data[var_names['var_name']['exp_reg']][idx][0] if exp_region_id: meas.exp_region_id = [exp_region_id] meas.risk_transf_attach = data[var_names['var_name'] ['risk_att']][idx][0] meas.risk_transf_cover = data[var_names['var_name'] ['risk_cov']][idx][0] measures.append(meas) data = u_hdf5.read(file_name) meas_set = cls() meas_set.tag.file_name = str(file_name) meas_set.tag.description = description try: data = data[var_names['sup_field_name']] except KeyError: pass try: data = data[var_names['field_name']] read_att_mat(meas_set, data, file_name, var_names) except KeyError as var_err: raise KeyError("Variable not in MAT file: " + str(var_err)) from var_err return meas_set
def from_mat(cls, file_name, description='', var_names=None): """Read MATLAB file generated with previous MATLAB CLIMADA version. Parameters ---------- file_name : str absolute file name description : str, optional description of the data var_names : dict, optional name of the variables in the file Return ------ impf_set : climada.entity.impact_func_set.ImpactFuncSet Impact func set as defined in matlab file. """ if var_names is None: var_names = DEF_VAR_MAT def _get_hdf5_funcs(imp, file_name, var_names): """Get rows that fill every impact function and its name.""" func_pos = dict() for row, (fun_id, fun_type) in enumerate( zip(imp[var_names['var_name']['fun_id']].squeeze(), imp[var_names['var_name']['peril']].squeeze())): type_str = u_hdf5.get_str_from_ref(file_name, fun_type) key = (type_str, int(fun_id)) if key not in func_pos: func_pos[key] = list() func_pos[key].append(row) return func_pos def _get_hdf5_str(imp, idxs, file_name, var_name): """Get rows with same string in var_name.""" prev_str = "" for row in idxs: cur_str = u_hdf5.get_str_from_ref(file_name, imp[var_name][row][0]) if prev_str == "": prev_str = cur_str elif prev_str != cur_str: raise ValueError("Impact function with two different %s." % var_name) return prev_str imp = u_hdf5.read(file_name) impf_set = cls() impf_set.tag.file_name = str(file_name) impf_set.tag.description = description try: imp = imp[var_names['sup_field_name']] except KeyError: pass try: imp = imp[var_names['field_name']] funcs_idx = _get_hdf5_funcs(imp, file_name, var_names) for imp_key, imp_rows in funcs_idx.items(): func = ImpactFunc() func.haz_type = imp_key[0] func.id = imp_key[1] # check that this function only has one intensity unit, if provided try: func.intensity_unit = _get_hdf5_str( imp, imp_rows, file_name, var_names['var_name']['unit']) except KeyError: pass # check that this function only has one name try: func.name = _get_hdf5_str(imp, imp_rows, file_name, var_names['var_name']['name']) except KeyError: func.name = str(func.id) func.intensity = np.take(imp[var_names['var_name']['inten']], imp_rows) func.mdd = np.take(imp[var_names['var_name']['mdd']], imp_rows) func.paa = np.take(imp[var_names['var_name']['paa']], imp_rows) impf_set.append(func) except KeyError as err: raise KeyError("Not existing variable: %s" % str(err)) from err return impf_set