Source code for openquake.hazardlib.mfd.evenly_discretized
# The Hazard Library# Copyright (C) 2012-2025 GEM Foundation## This program is free software: you can redistribute it and/or modify# it under the terms of the GNU Affero General Public License as# published by the Free Software Foundation, either version 3 of the# License, or (at your option) any later version.## This program 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 Affero General Public License for more details.## You should have received a copy of the GNU Affero General Public License# along with this program. If not, see <http://www.gnu.org/licenses/>."""Module :mod:`openquake.hazardlib.mfd.evenly_discretized` defines an evenlydiscretized MFD."""fromopenquake.hazardlib.mfd.baseimportBaseMFD
[docs]classEvenlyDiscretizedMFD(BaseMFD):""" Evenly discretized MFD is defined as a precalculated histogram. :param min_mag: Positive float value representing the middle point of the first bin in the histogram. :param bin_width: A positive float value -- the width of a single histogram bin. :param occurrence_rates: The list of non-negative float values representing the actual annual occurrence rates. The resulting histogram has as many bins as this list length. """MODIFICATIONS={'set_mfd'}def__init__(self,min_mag,bin_width,occurrence_rates):self.min_mag=min_magself.bin_width=bin_widthself.occurrence_rates=occurrence_ratesself.check_constraints()
[docs]defcheck_constraints(self):""" Checks the following constraints: * Bin width is positive. * Occurrence rates list is not empty. * Each number in occurrence rates list is non-negative. * Minimum magnitude is positive. """ifnotself.bin_width>0:raiseValueError('bin width must be positive')iflen(self.occurrence_rates)==0:raiseValueError('at least one bin must be specified')ifnotall(value>=0forvalueinself.occurrence_rates):raiseValueError('all occurrence rates must not be negative')ifnotany(value>0forvalueinself.occurrence_rates):raiseValueError('at least one occurrence rate must be positive')ifnotself.min_mag>=0:raiseValueError('minimum magnitude must be non-negative')
[docs]defget_annual_occurrence_rates(self):""" Returns the predefined annual occurrence rates. """return[(self.min_mag+i*self.bin_width,occurrence_rate)fori,occurrence_rateinenumerate(self.occurrence_rates)]
[docs]defget_min_max_mag(self):""" Returns the minumun and maximum magnitudes """returnself.min_mag,self.min_mag+self.bin_width*(len(self.occurrence_rates)-1)
[docs]defmodify_set_mfd(self,min_mag,bin_width,occurrence_rates):""" Applies absolute modification of the MFD from the ``min_mag``, ``bin_width`` and ``occurrence_rates`` modification. :param min_mag: Positive float value representing the middle point of the first bin in the histogram. :param bin_width: A positive float value -- the width of a single histogram bin. :param occurrence_rates: The list of non-negative float values representing the actual annual occurrence rates. The resulting histogram has as many bins as this list length. """self.min_mag=min_magself.bin_width=bin_widthself.occurrence_rates=occurrence_ratesself.check_constraints()