Source code for climada.entity.impact_funcs.base

"""
This file is part of CLIMADA.

Copyright (C) 2017 ETH Zurich, CLIMADA contributors listed in AUTHORS.

CLIMADA is free software: you can redistribute it and/or modify it under the
terms of the GNU Lesser General Public License as published by the Free
Software Foundation, version 3.

CLIMADA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with CLIMADA. If not, see <https://www.gnu.org/licenses/>.

---

Define ImpactFunc class.
"""

__all__ = ['ImpactFunc']

import logging
import numpy as np
import matplotlib.pyplot as plt

import climada.util.checker as check

LOGGER = logging.getLogger(__name__)

[docs]class ImpactFunc(): """Contains the definition of one impact function. Attributes: haz_type (str): hazard type acronym (e.g. 'TC') id (int or str): id of the impact function. Exposures of the same type will refer to the same impact function id name (str): name of the ImpactFunc intensity_unit (str): unit of the intensity intensity (np.array): intensity values mdd (np.array): mean damage (impact) degree for each intensity (numbers in [0,1]) paa (np.array): percentage of affected assets (exposures) for each intensity (numbers in [0,1]) """
[docs] def __init__(self): """ Empty initialization.""" self.id = '' self.name = '' self.intensity_unit = '' self.haz_type = '' # Followng values defined for each intensity value self.intensity = np.array([]) self.mdd = np.array([]) self.paa = np.array([])
[docs] def calc_mdr(self, inten): """ Interpolate impact function to a given intensity. Parameters: inten (float or np.array): intensity, the x-coordinate of the interpolated values. Returns: np.array """ # return np.interp(inten, self.intensity, self.mdd * self.paa) return np.interp(inten, self.intensity, self.paa) * \ np.interp(inten, self.intensity, self.mdd)
[docs] def plot(self, axis=None, **kwargs): """Plot the impact functions MDD, MDR and PAA in one graph, where MDR = PAA * MDD. Parameters: axis (matplotlib.axes._subplots.AxesSubplot, optional): axis to use kwargs (optional): arguments for plot matplotlib function, e.g. marker='x' Returns: matplotlib.axes._subplots.AxesSubplot """ if not axis: _, axis = plt.subplots(1, 1) title = '%s %s' % (self.haz_type, str(self.id)) if self.name != str(self.id): title += ': %s' % self.name axis.set_xlabel('Intensity (' + self.intensity_unit + ')') axis.set_ylabel('Impact (%)') axis.set_title(title) axis.plot(self.intensity, self.mdd * 100, 'b', label='MDD', **kwargs) axis.plot(self.intensity, self.paa * 100, 'r', label='PAA', **kwargs) axis.plot(self.intensity, self.mdd * self.paa * 100, 'k--', label='MDR', **kwargs) axis.set_xlim((self.intensity.min(), self.intensity.max())) axis.legend() return axis
[docs] def check(self): """ Check consistent instance data. Raises: ValueError """ num_exp = len(self.intensity) check.size(num_exp, self.mdd, 'ImpactFunc.mdd') check.size(num_exp, self.paa, 'ImpactFunc.paa')