Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functions to compute, plot, store the local hazard exceedence intensity and RP maps #857

Closed
wants to merge 14 commits into from
Closed
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
303 changes: 303 additions & 0 deletions climada/hazard/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import sparse as sp
from scipy import sparse
import xarray as xr
import netCDF4 as nc
from scipy.interpolate import griddata

Check warning on line 45 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

unused-import

NORMAL: Unused griddata imported from scipy.interpolate
Raw output
Used when an imported module or variable is not used.

Check warning on line 45 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

ungrouped-imports

LOW: Imports from package scipy are not grouped
Raw output
Used when imports are not grouped by packages

from climada.hazard.centroids.centr import Centroids
import climada.util.plot as u_plot
Expand Down Expand Up @@ -1541,6 +1543,48 @@
Reason: no negative intensity values were found in hazard.')
inten_stats[inten_stats < 0] = 0
return inten_stats

Check warning on line 1546 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
def local_return_period(self, hazard_intensities):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a confusing method signature to me. Why would a method of the class Hazard require the user to supply hazard intensities? These are already an attribute of the object.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here the variable hazard_intensities does not refer to the intensities of the Hazard object but to the threshold intensities for which the return period per grid point should be calculated. To avoid confusion we could rename this to "threshold_intensities"?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 threshold_intensities would be much better. 😄

"""Compute local return periods for given hazard intensities.

Check warning on line 1549 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
Parameters
----------
hazard_intensities : np.array
Hazard intensities to consider.
Comment on lines +1324 to +1325
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be careful, this might lead to very large memory usage. Hazard intensities are on purpose sparse matrices.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above, hazard_intensities usually only includes a few values.


Check warning on line 1554 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
Returns
-------
return_periods : np.array
Array containing computed local return periods for given hazard intensities.
"""
# Ensure hazard_intensities is a numpy array
hazard_intensities = np.array(hazard_intensities)

Check warning on line 1562 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
num_cen = self.intensity.shape[1]
return_periods = np.zeros((len(hazard_intensities), num_cen)) # Adjusted for 2D structure

Check warning on line 1565 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
# Process each centroid in chunks as in local_exceedance_inten
cen_step = CONFIG.max_matrix_size.int() // self.intensity.shape[0]
if not cen_step:
raise ValueError('Increase max_matrix_size configuration parameter to >'
f'{self.intensity.shape[0]}')

Check warning on line 1571 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
chk = -1
for chk in range(int(num_cen / cen_step)):
self._loc_return_period(
hazard_intensities,
self.intensity[:, chk * cen_step:(chk + 1) * cen_step].toarray(),
return_periods[:, chk * cen_step:(chk + 1) * cen_step])

Check warning on line 1578 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
if (chk + 1) * cen_step < num_cen: # Check if there's a remainder
self._loc_return_period(
hazard_intensities,
self.intensity[:, (chk + 1) * cen_step:].toarray(),
return_periods[:, (chk + 1) * cen_step:])

Check warning on line 1584 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
return return_periods

Check warning on line 1585 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Code Coverage

Not covered lines

Lines 1561-1585 are not covered by tests



def plot_rp_intensity(self, return_periods=(25, 50, 100, 250),
smooth=True, axis=None, figsize=(9, 13), adapt_fontsize=True,
Expand Down Expand Up @@ -1576,6 +1620,39 @@
colbar_name, title, smooth=smooth, axes=axis,
figsize=figsize, adapt_fontsize=adapt_fontsize, **kwargs)
return axis, inten_stats

Check warning on line 1623 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
import matplotlib.pyplot as plt

Check warning on line 1624 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

reimported

NORMAL: Reimport 'matplotlib.pyplot' (imported line 34)
Raw output
Used when a module is reimported multiple times.

Check warning on line 1624 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

import-outside-toplevel

LOW: Import outside toplevel (matplotlib.pyplot)
Raw output
no description found


def plot_local_rp(self, hazard_intensities, smooth=True, axis=None, figsize=(9, 13), adapt_fontsize=True, **kwargs):

Check warning on line 1627 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

line-too-long

LOW: Line too long (120/100)
Raw output
Used when a line is longer than a given number of characters.
"""Plot hazard local return periods for given hazard intensities.

Check warning on line 1629 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
Parameters
----------
hazard_intensities: np.array
Hazard intensities to consider for calculating return periods.
smooth: bool, optional
Smooth plot to plot.RESOLUTION x plot.RESOLUTION.
axis: matplotlib.axes._subplots.AxesSubplot, optional
Axis to use.
figsize: tuple, optional
Figure size for plt.subplots.
kwargs: optional
Arguments for pcolormesh matplotlib function used in event plots.

Check warning on line 1642 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
Returns
-------
axis: matplotlib.axes._subplots.AxesSubplot
Matplotlib axis with the plot.
"""
self._set_coords_centroids()
return_periods = self.local_return_period(hazard_intensities)
colbar_name = 'Return Period (years)'
axis = u_plot.geo_im_from_array(return_periods, self.centroids.coord,
colbar_name, "Local Return Periods", smooth=smooth, axes=axis,

Check warning on line 1652 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

line-too-long

LOW: Line too long (102/100)
Raw output
Used when a line is longer than a given number of characters.
figsize=figsize, adapt_fontsize=adapt_fontsize, **kwargs)
return axis

Check warning on line 1654 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Code Coverage

Not covered lines

Lines 1648-1654 are not covered by tests


def plot_intensity(self, event=None, centr=None, smooth=True, axis=None, adapt_fontsize=True,
**kwargs):
Expand Down Expand Up @@ -1890,6 +1967,148 @@
LOGGER.warning("The use of Hazard.read_hdf5 is deprecated."
"Use Hazard.from_hdf5 instead.")
self.__dict__ = self.__class__.from_hdf5(*args, **kwargs).__dict__

Check warning on line 1970 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.

def write_raster_local_exceedance_inten(self, return_periods, filename):

Check warning on line 1972 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

invalid-name

LOW: Method name "write_raster_local_exceedance_inten" doesn't conform to '(([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$' pattern
Raw output
Used when the name doesn't match the regular expression associated to its type(constant, variable, class...).
"""
Generates exceedance intensity data for specified return periods and

Check warning on line 1974 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
saves it into a GeoTIFF file.

Check warning on line 1976 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
Parameters
----------
return_periods : np.array or list
Array or list of return periods (in years) for which to calculate

Check warning on line 1980 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
and store exceedance intensities.
filename : str
Path and name of the file to write in tif format.
"""
inten_stats = self.local_exceedance_inten(return_periods=return_periods)
num_bands = inten_stats.shape[0]

Check warning on line 1987 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
if not self.centroids.meta:
raise ValueError("centroids.meta is required but not set.")

Check warning on line 1990 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
pixel_geom = self.centroids.calc_pixels_polygons()
profile = self.centroids.meta.copy()
profile.update(driver='GTiff', dtype='float32', count=num_bands)

Check warning on line 1994 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
with rasterio.open(filename, 'w', **profile) as dst:
LOGGER.info('Writing %s', filename)
for band in range(num_bands):
raster = rasterize(
[(x, val) for (x, val) in zip(pixel_geom, inten_stats[band].reshape(-1))],

Check warning on line 1999 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

unnecessary-comprehension

LOW: Unnecessary use of a comprehension
Raw output
no description found
out_shape=(profile['height'], profile['width']),
transform=profile['transform'], fill=0,
all_touched=True, dtype=profile['dtype'])
dst.write(raster, band + 1)

Check warning on line 2004 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
band_name = f"Exceedance intensity for RP {return_periods[band]} years"
dst.set_band_description(band + 1, band_name)

Check warning on line 2006 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.

Check warning on line 2006 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Code Coverage

Not covered lines

Lines 1985-2006 are not covered by tests

Check warning on line 2007 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.

def write_netcdf_local_exceedance_inten(self, return_periods, filename):

Check warning on line 2009 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

invalid-name

LOW: Method name "write_netcdf_local_exceedance_inten" doesn't conform to '(([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$' pattern
Raw output
Used when the name doesn't match the regular expression associated to its type(constant, variable, class...).
"""
Generates exceedance intensity data for specified return periods and

Check warning on line 2011 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
saves it into a NetCDF file.

Check warning on line 2013 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
Parameters
----------
return_periods : np.array or list
Array or list of return periods (in years) for which to calculate

Check warning on line 2017 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
and store exceedance intensities.
filename : str
Path and name of the file to write the NetCDF data.
"""
inten_stats = self.local_exceedance_inten(return_periods=return_periods)
coords = self.centroids.coord

Check warning on line 2024 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
with nc.Dataset(filename, 'w', format='NETCDF4') as dataset:
centroids_dim = dataset.createDimension('centroids', coords.shape[0])

Check warning on line 2026 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

unused-variable

NORMAL: Unused variable 'centroids_dim'
Raw output
Used when a variable is defined but not used.

Check warning on line 2027 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
latitudes = dataset.createVariable('latitude', 'f4', ('centroids',))
longitudes = dataset.createVariable('longitude', 'f4', ('centroids',))
latitudes[:] = coords[:, 0]
longitudes[:] = coords[:, 1]
latitudes.units = 'degrees_north'
longitudes.units = 'degrees_east'

Check warning on line 2034 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
for i, period in enumerate(return_periods):
dataset_name = f'intensity_RP{period}'
intensity_rp = dataset.createVariable(dataset_name, 'f4', ('centroids',))
intensity_rp[:] = inten_stats[i, :]
intensity_rp.units = self.units
intensity_rp.description = f'Exceedance intensity map for {period}-year return period'

Check warning on line 2040 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

line-too-long

LOW: Line too long (102/100)
Raw output
Used when a line is longer than a given number of characters.

Check warning on line 2041 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
dataset.description = 'Exceedance intensity data for various return periods'

Check warning on line 2042 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Code Coverage

Not covered lines

Lines 2022-2042 are not covered by tests

Check warning on line 2043 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.

Check warning on line 2044 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
def write_raster_local_return_periods(self, hazard_intensities, filename):

Check warning on line 2045 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

invalid-name

LOW: Method name "write_raster_local_return_periods" doesn't conform to '(([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$' pattern
Raw output
Used when the name doesn't match the regular expression associated to its type(constant, variable, class...).
"""Write local return periods map as GeoTIFF file.

Check warning on line 2047 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
Parameters
----------
hazard_intensities: np.array
Hazard intensities to consider for calculating return periods.
file_name: str
File name to write in tif format.
"""
variable = self.local_return_period(hazard_intensities)

Check warning on line 2056 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
num_bands = variable.shape[0]
if not self.centroids.meta:
raise ValueError("centroids.meta is required but not set.")

pixel_geom = self.centroids.calc_pixels_polygons()
profile = self.centroids.meta.copy()
profile.update(driver='GTiff', dtype='float32', count=num_bands)

Check warning on line 2064 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
with rasterio.open(filename, 'w', **profile) as dst:
LOGGER.info('Writing %s', filename)
for band in range(num_bands):
raster = rasterize(
[(x, val) for (x, val) in zip(pixel_geom, variable[band].reshape(-1))],

Check warning on line 2069 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

unnecessary-comprehension

LOW: Unnecessary use of a comprehension
Raw output
no description found
out_shape=(profile['height'], profile['width']),
transform=profile['transform'], fill=0,
all_touched=True, dtype=profile['dtype'])
dst.write(raster, band + 1)

Check warning on line 2074 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
band_name = f"RP of intensity {hazard_intensities[band]} {self.units}"
dst.set_band_description(band + 1, band_name)

Check warning on line 2076 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Code Coverage

Not covered lines

Lines 2055-2076 are not covered by tests



def write_netcdf_local_return_periods(self, hazard_intensities, filename):

Check warning on line 2080 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

invalid-name

LOW: Method name "write_netcdf_local_return_periods" doesn't conform to '(([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$' pattern
Raw output
Used when the name doesn't match the regular expression associated to its type(constant, variable, class...).
"""Generates local return period data and saves it into a NetCDF file.

Parameters
----------
hazard_intensities: np.array
Hazard intensities to consider for calculating return periods.
filename: str
Path and name of the file to write the NetCDF data.
"""
return_periods = self.local_return_period(hazard_intensities)
coords = self.centroids.coord

Check warning on line 2092 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
with nc.Dataset(filename, 'w', format='NETCDF4') as dataset:
centroids_dim = dataset.createDimension('centroids', coords.shape[0])

Check warning on line 2094 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

unused-variable

NORMAL: Unused variable 'centroids_dim'
Raw output
Used when a variable is defined but not used.

Check warning on line 2095 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
latitudes = dataset.createVariable('latitude', 'f4', ('centroids',))
longitudes = dataset.createVariable('longitude', 'f4', ('centroids',))
latitudes[:] = coords[:, 0]
longitudes[:] = coords[:, 1]
latitudes.units = 'degrees_north'
longitudes.units = 'degrees_east'

Check warning on line 2102 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
for i, intensity in enumerate(hazard_intensities):
dataset_name = f'return_period_intensity_{int(intensity)}'
return_period_var = dataset.createVariable(dataset_name, 'f4', ('centroids',))
return_period_var[:] = return_periods[i, :]
return_period_var.units = 'years'
return_period_var.description = f'Local return period for hazard intensity {intensity} {self.units}'

Check warning on line 2108 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

line-too-long

LOW: Line too long (116/100)
Raw output
Used when a line is longer than a given number of characters.

Check warning on line 2109 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
dataset.description = 'Local return period data for given hazard intensities'

Check warning on line 2110 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Code Coverage

Not covered lines

Lines 2090-2110 are not covered by tests


@classmethod
def from_hdf5(cls, file_name):
Expand Down Expand Up @@ -2094,6 +2313,46 @@
exc_inten[:, cen_idx] = self._cen_return_inten(
inten_sort[:, cen_idx], freq_sort[:, cen_idx],
self.intensity_thres, return_periods)

Check warning on line 2316 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.

Check warning on line 2317 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
def _loc_return_period(self, hazard_intensities, inten, return_periods):
"""Compute local return periods for given hazard intensities for a specific chunk of data.

Check warning on line 2320 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
Parameters
----------
hazard_intensities: np.array
Given hazard intensities for which to calculate return periods.
inten: np.array
The intensity array for a specific chunk of data.
return_periods: np.array
Array to store computed return periods for the given hazard intensities.
"""
# Assuming inten is sorted and calculating cumulative frequency
sort_pos = np.argsort(inten, axis=0)[::-1, :]
inten_sort = inten[sort_pos, np.arange(inten.shape[1])]
freq_sort = self.frequency[sort_pos]
np.cumsum(freq_sort, axis=0, out=freq_sort)

Check warning on line 2335 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
for cen_idx in range(inten.shape[1]):
sorted_inten_cen = inten_sort[:, cen_idx]
cum_freq_cen = freq_sort[:, cen_idx]

Check warning on line 2339 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
for i, intensity in enumerate(hazard_intensities):
# Find the first occurrence where the intensity is less than the sorted intensities
exceedance_index = np.searchsorted(sorted_inten_cen[::-1], intensity, side='right')

Check warning on line 2343 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
# Calculate exceedance probability
if exceedance_index < len(cum_freq_cen):
exceedance_probability = cum_freq_cen[-exceedance_index - 1]
else:
exceedance_probability = 0 # Or set a default minimal probability

Check warning on line 2349 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
# Calculate and store return period
if exceedance_probability > 0:
return_periods[i, cen_idx] = 1 / exceedance_probability
else:
return_periods[i, cen_idx] = np.nan

Check warning on line 2354 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.

Check warning on line 2354 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Code Coverage

Not covered lines

Lines 2331-2354 are not covered by tests

Check warning on line 2355 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.

def _check_events(self):
"""Check that all attributes but centroids contain consistent data.
Expand Down Expand Up @@ -2157,9 +2416,53 @@
inten_fit[wrong_inten] = 0.

return inten_fit

Check warning on line 2419 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
@staticmethod
def _cen_return_period(inten, freq, inten_th, hazard_intensities):
"""Estimate the return periods for given hazard intensities using the polynomial

Check warning on line 2422 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
relationship derived from cumulative frequency and intensity values.

Check warning on line 2424 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
Parameters
----------
inten: np.array
Sorted intensity at centroid.
freq: np.array
Cumulative frequency at centroid.
inten_th: float
Intensity threshold.
hazard_intensities: np.array
Hazard intensities for which to estimate return periods.

Check warning on line 2435 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
Returns
-------
return_periods: np.array
Estimated return periods for the given hazard intensities.
"""
inten_above_threshold = inten > inten_th
inten_cen = inten[inten_above_threshold]
freq_cen = freq[inten_above_threshold]

Check warning on line 2444 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
if not inten_cen.size:
return np.inf * np.ones(hazard_intensities.size)

Check warning on line 2447 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
try:
with warnings.catch_warnings():
warnings.simplefilter("ignore")
pol_coef = np.polyfit(inten_cen, np.log(freq_cen), deg=1)
except ValueError:
return np.inf * np.ones(hazard_intensities.size)

Check warning on line 2454 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
log_rp_estimates = np.polyval(pol_coef, hazard_intensities)

Check warning on line 2456 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
return_periods = 1 / np.exp(log_rp_estimates)

Check warning on line 2458 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
out_of_range = (hazard_intensities < np.min(inten_cen)) | (hazard_intensities > np.max(inten_cen))

Check warning on line 2459 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

line-too-long

LOW: Line too long (106/100)
Raw output
Used when a line is longer than a given number of characters.
return_periods[out_of_range] = np.inf

Check warning on line 2461 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
return return_periods

Check warning on line 2462 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Code Coverage

Not covered lines

Lines 2441-2462 are not covered by tests

@staticmethod
def _read_att_mat(data, file_name, var_names, centroids):

Check warning on line 2465 in climada/hazard/base.py

View check run for this annotation

Jenkins - WCR / Pylint

too-complex

LOW: '_read_att_mat' is too complex. The McCabe rating is 14
Raw output
no description found
"""Read MATLAB hazard's attributes."""
attrs = dict()
attrs["frequency"] = np.squeeze(data[var_names['var_name']['freq']])
Expand Down
Loading