def test_geo_im_from_array(self): values = np.array([1, 2.0, 5, 1]) coord = np.array([[-17, 178], [-10, 180], [-27, 175], [-16, 186]]) var_name = 'test' title = 'test' projection = ccrs.PlateCarree() cmap = 'viridis' ax = u_plot.geo_im_from_array(values, coord, var_name, title, proj=projection, smooth=True, axes=None, figsize=(9, 13), cmap=cmap) self.assertEqual(var_name, ax.get_title()) self.assertAlmostEqual(np.max(values), ax.collections[0].colorbar.vmax) self.assertAlmostEqual(np.min(values), ax.collections[0].colorbar.vmin) self.assertEqual(cmap, ax.collections[0].cmap.name) plt.close()
def _event_plot(self, event_id, mat_var, col_name, **kwargs): """"Plot an event of the input matrix. Parameters: event_id (int or np.array(int)): If event_id > 0, plot mat_var of event with id = event_id. If event_id = 0, plot maximum mat_var in each centroid. If event_id < 0, plot abs(event_id)-largest event. mat_var (sparse matrix): Sparse matrix where each row is an event col_name (sparse matrix): Colorbar label kwargs (optional): arguments for pcolormesh matplotlib function Returns: matplotlib.figure.Figure, matplotlib.axes._subplots.AxesSubplot """ if not isinstance(event_id, np.ndarray): event_id = np.array([event_id]) array_val = list() l_title = list() for ev_id in event_id: if ev_id > 0: try: event_pos = np.where(self.event_id == ev_id)[0][0] except IndexError: LOGGER.error('Wrong event id: %s.', ev_id) raise ValueError from IndexError im_val = mat_var[event_pos, :].todense().transpose() title = 'Event ID %s: %s' % (str(self.event_id[event_pos]), \ self.event_name[event_pos]) elif ev_id < 0: max_inten = np.asarray(np.sum(mat_var, axis=1)).reshape(-1) event_pos = np.argpartition(max_inten, ev_id)[ev_id:] event_pos = event_pos[np.argsort(max_inten[event_pos])][0] im_val = mat_var[event_pos, :].todense().transpose() title = '%s-largest Event. ID %s: %s' % (np.abs(ev_id), \ str(self.event_id[event_pos]), self.event_name[event_pos]) else: im_val = np.max(mat_var, axis=0).todense().transpose() title = '%s max intensity at each point' % self.tag.haz_type array_val.append(im_val) l_title.append(title) return u_plot.geo_im_from_array(array_val, self.centroids.coord, col_name, l_title, **kwargs)
def plot_rp_imp(self, return_periods=(25, 50, 100, 250), log10_scale=True, smooth=True, axis=None, **kwargs): """Compute and plot exceedance impact maps for different return periods. Calls local_exceedance_imp. Parameters: return_periods (tuple(int), optional): return periods to consider log10_scale (boolean, optional): plot impact as log10(impact) smooth (bool, optional): smooth plot to plot.RESOLUTIONxplot.RESOLUTION kwargs (optional): arguments for pcolormesh matplotlib function used in event plots Returns: matplotlib.axes._subplots.AxesSubplot, np.ndarray (return_periods.size x num_centroids) """ imp_stats = self.local_exceedance_imp(np.array(return_periods)) if imp_stats == []: LOGGER.error('Error: Attribute imp_mat is empty. Recalculate Impact'\ 'instance with parameter save_mat=True') raise ValueError if log10_scale: if np.min(imp_stats) < 0: imp_stats_log = np.log10(abs(imp_stats) + 1) colbar_name = 'Log10(abs(Impact)+1) (' + self.unit + ')' elif np.min(imp_stats) < 1: imp_stats_log = np.log10(imp_stats + 1) colbar_name = 'Log10(Impact+1) (' + self.unit + ')' else: imp_stats_log = np.log10(imp_stats) colbar_name = 'Log10(Impact) (' + self.unit + ')' else: imp_stats_log = imp_stats colbar_name = 'Impact (' + self.unit + ')' title = list() for ret in return_periods: title.append('Return period: ' + str(ret) + ' years') axis = u_plot.geo_im_from_array(imp_stats_log, self.coord_exp, \ colbar_name, title, smooth=smooth, axes=axis, **kwargs) return axis, imp_stats
def plot_rp_intensity(self, return_periods=(25, 50, 100, 250), **kwargs): """Compute and plot hazard exceedance intensity maps for different return periods. Calls local_exceedance_inten. Parameters: return_periods (tuple(int), optional): return periods to consider kwargs (optional): arguments for pcolormesh matplotlib function used in event plots Returns: matplotlib.figure.Figure, matplotlib.axes._subplots.AxesSubplot, np.ndarray (return_periods.size x num_centroids) """ inten_stats = self.local_exceedance_inten(np.array(return_periods)) colbar_name = 'Intensity (' + self.units + ')' title = list() for ret in return_periods: title.append('Return period: ' + str(ret) + ' years') fig, axis = u_plot.geo_im_from_array(inten_stats, self.centroids.coord, colbar_name, title, **kwargs) return fig, axis, inten_stats