def check_levels(imls, imt): """ Raise a ValueError if the given levels are invalid. :param imls: a list of intensity measure and levels :param imt: the intensity measure type >>> check_levels([0.1, 0.2], 'PGA') # ok >>> check_levels([0.1], 'PGA') Traceback (most recent call last): ... ValueError: Not enough imls for PGA: [0.1] >>> check_levels([0.2, 0.1], 'PGA') Traceback (most recent call last): ... ValueError: The imls for PGA are not sorted: [0.2, 0.1] >>> check_levels([0.2, 0.2], 'PGA') Traceback (most recent call last): ... ValueError: Found duplicated levels for PGA: [0.2, 0.2] """ if len(imls) < 2: raise ValueError('Not enough imls for %s: %s' % (imt, imls)) elif imls != sorted(imls): raise ValueError('The imls for %s are not sorted: %s' % (imt, imls)) elif len(distinct(imls)) < len(imls): raise ValueError("Found duplicated levels for %s: %s" % (imt, imls))
def check_levels(imls, imt, min_iml=1E-10): """ Raise a ValueError if the given levels are invalid. :param imls: a list of intensity measure and levels :param imt: the intensity measure type :param min_iml: minimum intensity measure level (default 1E-10) >>> check_levels([0.1, 0.2], 'PGA') # ok >>> check_levels([], 'PGA') Traceback (most recent call last): ... ValueError: No imls for PGA: [] >>> check_levels([0.2, 0.1], 'PGA') Traceback (most recent call last): ... ValueError: The imls for PGA are not sorted: [0.2, 0.1] >>> check_levels([0.2, 0.2], 'PGA') Traceback (most recent call last): ... ValueError: Found duplicated levels for PGA: [0.2, 0.2] """ if len(imls) < 1: raise ValueError('No imls for %s: %s' % (imt, imls)) elif imls != sorted(imls): raise ValueError('The imls for %s are not sorted: %s' % (imt, imls)) elif len(distinct(imls)) < len(imls): raise ValueError("Found duplicated levels for %s: %s" % (imt, imls)) elif imls[0] == 0 and imls[1] <= min_iml: # apply the cutoff raise ValueError("The min_iml %s=%s is larger than the second level " "for %s" % (imt, min_iml, imls)) elif imls[0] == 0 and imls[1] > min_iml: # apply the cutoff imls[0] = min_iml
def intensity_measure_types(value): """ :param value: input string :returns: non-empty list of ordered Intensity Measure Type objects >>> intensity_measure_types('') [] >>> intensity_measure_types('PGA') ['PGA'] >>> intensity_measure_types('PGA, SA(1.00)') ['PGA', 'SA(1.0)'] >>> intensity_measure_types('SA(0.1), SA(0.10)') Traceback (most recent call last): ... ValueError: Duplicated IMTs in SA(0.1), SA(0.10) >>> intensity_measure_types('PGV, SA(1), PGA') ['PGA', 'PGV', 'SA(1.0)'] """ if not value: return [] imts = [] for chunk in value.split(','): imts.append(imt.from_string(chunk.strip())) sorted_imts = sorted(imts, key=lambda im: getattr(im, 'period', 1)) if len(distinct(imts)) < len(imts): raise ValueError('Duplicated IMTs in %s' % value) return [str(imt) for imt in sorted_imts]
def intensity_measure_types(value): """ :param value: input string :returns: non-empty list of Intensity Measure Type objects >>> intensity_measure_types('PGA') ['PGA'] >>> intensity_measure_types('PGA, SA(1.00)') ['PGA', 'SA(1.0)'] >>> intensity_measure_types('SA(0.1), SA(0.10)') Traceback (most recent call last): ... ValueError: Duplicated IMTs in SA(0.1), SA(0.10) >>> intensity_measure_types('SA(1), PGA') Traceback (most recent call last): ... ValueError: The IMTs are not sorted by period: SA(1), PGA """ imts = [] for chunk in value.split(','): imts.append(imt.from_string(chunk.strip())) sorted_imts = sorted(imts, key=lambda im: getattr(im, 'period', 1)) if len(distinct(imts)) < len(imts): raise ValueError('Duplicated IMTs in %s' % value) if sorted_imts != imts: raise ValueError('The IMTs are not sorted by period: %s' % value) return [str(imt) for imt in imts]
def intensity_measure_types(value): """ :param value: input string :returns: non-empty list of Intensity Measure Type objects >>> intensity_measure_types('PGA') ['PGA'] >>> intensity_measure_types('PGA, SA(1.00)') ['PGA', 'SA(1.0)'] >>> intensity_measure_types('SA(0.1), SA(0.10)') Traceback (most recent call last): ... ValueError: Duplicated IMTs in SA(0.1), SA(0.10) """ imts = [] for chunk in value.split(','): imts.append(str(imt.from_string(chunk.strip()))) if len(distinct(imts)) < len(imts): raise ValueError('Duplicated IMTs in %s' % value) return imts